Skip to content

Commit 2bfa172

Browse files
committed
Add I2C debug tools
Add I2C debug tools
1 parent a9050de commit 2bfa172

File tree

1 file changed

+86
-2
lines changed

1 file changed

+86
-2
lines changed

sonoff/xdrv_99_debug.ino

+86-2
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@
5757
#define D_CMND_HELP "Help"
5858
#define D_CMND_RTCDUMP "RtcDump"
5959
#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"
6064

6165
const char kDebugCommands[] PROGMEM = "|" // No prefix
6266
D_CMND_CFGDUMP "|" D_CMND_CFGPEEK "|" D_CMND_CFGPOKE "|"
@@ -70,7 +74,8 @@ const char kDebugCommands[] PROGMEM = "|" // No prefix
7074
#ifdef DEBUG_THEO
7175
D_CMND_EXCEPTION "|"
7276
#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 ;
7479

7580
void (* const DebugCommand[])(void) PROGMEM = {
7681
&CmndCfgDump, &CmndCfgPeek, &CmndCfgPoke,
@@ -84,7 +89,8 @@ void (* const DebugCommand[])(void) PROGMEM = {
8489
#ifdef DEBUG_THEO
8590
&CmndException,
8691
#endif
87-
&CmndFlashDump, &CmndFlashMode, &CmndFreemem, &CmndHelp, &CmndRtcDump, &CmndSetSensor };
92+
&CmndFlashDump, &CmndFlashMode, &CmndFreemem, &CmndHelp, &CmndRtcDump, &CmndSetSensor,
93+
&CmndI2cWrite, &CmndI2cRead, &CmndI2cStretch, &CmndI2cClock };
8894

8995
uint32_t CPU_loops = 0;
9096
uint32_t CPU_last_millis = 0;
@@ -569,6 +575,84 @@ void CmndFlashDump(void)
569575
ResponseCmndDone();
570576
}
571577

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+
572656
/*********************************************************************************************\
573657
* Interface
574658
\*********************************************************************************************/

0 commit comments

Comments
 (0)