57
57
#define D_CMND_HELP " Help"
58
58
#define D_CMND_RTCDUMP " RtcDump"
59
59
#define D_CMND_SETSENSOR " SetSensor"
60
+ #define D_CMND_I2CWRITE " I2CWrite"
61
+ #define D_CMND_I2CREAD " I2CRead"
62
+ #define D_CMND_I2CSTRETCH " I2CStretch"
63
+ #define D_CMND_I2CCLOCK " I2CClock"
60
64
61
65
const char kDebugCommands [] PROGMEM = " |" // No prefix
62
66
D_CMND_CFGDUMP " |" D_CMND_CFGPEEK " |" D_CMND_CFGPOKE " |"
@@ -70,7 +74,8 @@ const char kDebugCommands[] PROGMEM = "|" // No prefix
70
74
#ifdef DEBUG_THEO
71
75
D_CMND_EXCEPTION " |"
72
76
#endif
73
- D_CMND_FLASHDUMP " |" D_CMND_FLASHMODE " |" D_CMND_FREEMEM" |" D_CMND_HELP " |" D_CMND_RTCDUMP " |" D_CMND_SETSENSOR ;
77
+ D_CMND_FLASHDUMP " |" D_CMND_FLASHMODE " |" D_CMND_FREEMEM" |" D_CMND_HELP " |" D_CMND_RTCDUMP " |" D_CMND_SETSENSOR " |"
78
+ D_CMND_I2CWRITE " |" D_CMND_I2CREAD " |" D_CMND_I2CSTRETCH " |" D_CMND_I2CCLOCK ;
74
79
75
80
void (* const DebugCommand[])(void ) PROGMEM = {
76
81
&CmndCfgDump, &CmndCfgPeek, &CmndCfgPoke,
@@ -84,7 +89,8 @@ void (* const DebugCommand[])(void) PROGMEM = {
84
89
#ifdef DEBUG_THEO
85
90
&CmndException,
86
91
#endif
87
- &CmndFlashDump, &CmndFlashMode, &CmndFreemem, &CmndHelp, &CmndRtcDump, &CmndSetSensor };
92
+ &CmndFlashDump, &CmndFlashMode, &CmndFreemem, &CmndHelp, &CmndRtcDump, &CmndSetSensor,
93
+ &CmndI2cWrite, &CmndI2cRead, &CmndI2cStretch, &CmndI2cClock };
88
94
89
95
uint32_t CPU_loops = 0 ;
90
96
uint32_t CPU_last_millis = 0 ;
@@ -569,6 +575,84 @@ void CmndFlashDump(void)
569
575
ResponseCmndDone ();
570
576
}
571
577
578
+ void CmndI2cWrite (void )
579
+ {
580
+ // I2cWrite <address>,<data>..
581
+ if (i2c_flg) {
582
+ char * parms = XdrvMailbox.data ;
583
+ uint8_t buffer[100 ];
584
+ uint32_t index = 0 ;
585
+
586
+ char *p;
587
+ char *data = strtok_r (parms, " ," , &p);
588
+ while (data != NULL && index < sizeof (buffer)) {
589
+ buffer[index ++] = strtol (data, nullptr , 16 );
590
+ data = strtok_r (nullptr , " ," , &p);
591
+ }
592
+
593
+ if (index > 1 ) {
594
+ AddLogBuffer (LOG_LEVEL_INFO, buffer, index );
595
+
596
+ Wire.beginTransmission (buffer[0 ]);
597
+ for (uint32_t i = 1 ; i < index ; i++) {
598
+ Wire.write (buffer[i]);
599
+ }
600
+ int result = Wire.endTransmission ();
601
+ AddLog_P2 (LOG_LEVEL_INFO, PSTR (" I2C: Result %d" ), result);
602
+ }
603
+ }
604
+ ResponseCmndDone ();
605
+ }
606
+
607
+ void CmndI2cRead (void )
608
+ {
609
+ // I2cRead <address>,<size>
610
+ if (i2c_flg) {
611
+ char * parms = XdrvMailbox.data ;
612
+ uint8_t buffer[100 ];
613
+ uint32_t index = 0 ;
614
+
615
+ char *p;
616
+ char *data = strtok_r (parms, " ," , &p);
617
+ while (data != NULL && index < sizeof (buffer)) {
618
+ buffer[index ++] = strtol (data, nullptr , 16 );
619
+ data = strtok_r (nullptr , " ," , &p);
620
+ }
621
+
622
+ if (index > 0 ) {
623
+ uint8_t size = 1 ;
624
+ if (index > 1 ) {
625
+ size = buffer[1 ];
626
+ }
627
+ Wire.requestFrom (buffer[0 ], size);
628
+ index = 0 ;
629
+ while (Wire.available () && index < sizeof (buffer)) {
630
+ buffer[index ++] = Wire.read ();
631
+ }
632
+ if (index > 0 ) {
633
+ AddLogBuffer (LOG_LEVEL_INFO, buffer, index );
634
+ }
635
+ }
636
+ }
637
+ ResponseCmndDone ();
638
+ }
639
+
640
+ void CmndI2cStretch (void )
641
+ {
642
+ if (i2c_flg && (XdrvMailbox.payload > 0 )) {
643
+ Wire.setClockStretchLimit (XdrvMailbox.payload );
644
+ }
645
+ ResponseCmndDone ();
646
+ }
647
+
648
+ void CmndI2cClock (void )
649
+ {
650
+ if (i2c_flg && (XdrvMailbox.payload > 0 )) {
651
+ Wire.setClock (XdrvMailbox.payload );
652
+ }
653
+ ResponseCmndDone ();
654
+ }
655
+
572
656
/* ********************************************************************************************\
573
657
* Interface
574
658
\*********************************************************************************************/
0 commit comments