Skip to content

Commit c5c2091

Browse files
committed
Implement both bus scans
1 parent a780749 commit c5c2091

File tree

3 files changed

+133
-83
lines changed

3 files changed

+133
-83
lines changed

src/components/i2c/controller.cpp

Lines changed: 58 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -522,40 +522,77 @@ bool I2cController::InitMux(const char *name, uint32_t address,
522522
/***********************************************************************/
523523
bool I2cController::Handle_I2cBusScan(pb_istream_t *stream) {
524524
WS_DEBUG_PRINTLN("[i2c] Decoding I2cDeviceAddOrReplace message...");
525-
// Attempt to decode an I2cBusScan message
525+
// Attempt to decode I2cBusScan message
526526
if (!_i2c_model->DecodeI2cBusScan(stream)) {
527-
WS_DEBUG_PRINTLN(
528-
"[i2c] ERROR: Unable to decode I2cDeviceAddOrReplace message!");
527+
WS_DEBUG_PRINTLN("[i2c] ERROR: Unable to decode I2cBusScan message!");
529528
return false;
530529
}
531530

532531
_i2c_model->ClearI2cBusScanned();
533-
wippersnapper_i2c_I2cBusScanned* scan_results = _i2c_model->GetI2cBusScannedMsg();
532+
wippersnapper_i2c_I2cBusScanned *scan_results =
533+
_i2c_model->GetI2cBusScannedMsg();
534+
bool scan_success = false;
535+
536+
// TODO: Refactor, case 1 and case 2 are functionally VERY similar - can be
537+
// combined
534538

539+
// Case 1: Scan the default I2C bus
535540
if (_i2c_model->GetI2cBusScanMsg()->scan_default_bus) {
536-
if (! IsBusStatusOK()) {
537-
WS_DEBUG_PRINTLN("[i2c] Default I2C bus is stuck or not operational, reset the board!");
538-
return false;
541+
// Was the default bus initialized correctly and ready to scan?
542+
if (IsBusStatusOK()) {
543+
if (_i2c_bus_default->ScanBus(scan_results)) {
544+
scan_success = true;
545+
} else {
546+
WS_DEBUG_PRINTLN("[i2c] ERROR: Failed to scan default I2C bus!");
547+
scan_success = false;
548+
}
549+
} else {
550+
WS_DEBUG_PRINTLN("[i2c] ERROR: Default I2C bus state is stuck, please "
551+
"reset the board!");
552+
scan_success = false;
539553
}
540-
// Linearly scan the default i2c bus
541-
if (!_i2c_bus_default->ScanBus(scan_results)) {
542-
WS_DEBUG_PRINTLN("[i2c] ERROR: Unable to scan default I2C bus!");
543-
return false;
554+
if (scan_success) {
555+
WS_DEBUG_PRINTLN("[i2c] Scanned default I2C bus successfully!");
544556
}
545-
// TODO: Encode message
546-
// TODO: Print out the message (to verify)
547-
// Future TODO: Publish scan results out to IO
548-
// Return
549-
return true;
557+
// TODO: Print out what was scanned? or do this at the end
550558
}
551559

560+
// Case 2: Scan the alternative I2C bus
561+
if (_i2c_model->GetI2cBusScanMsg()->scan_alt_bus) {
562+
// Is the alt bus initialized?
563+
if (_i2c_bus_alt == nullptr) {
564+
WS_DEBUG_PRINTLN("[i2c] Initializing alt. i2c bus...");
565+
_i2c_bus_alt = new I2cHardware();
566+
_i2c_bus_alt->InitBus(
567+
false,
568+
_i2c_model->GetI2cBusScanMsg()->i2c_alt_bus_descriptor.i2c_bus_sda,
569+
_i2c_model->GetI2cBusScanMsg()->i2c_alt_bus_descriptor.i2c_bus_sda);
570+
}
571+
// Was the default bus initialized correctly and ready to scan?
572+
if (IsBusStatusOK(true)) {
573+
if (_i2c_bus_alt->ScanBus(scan_results)) {
574+
scan_success = true;
575+
} else {
576+
WS_DEBUG_PRINTLN("[i2c] ERROR: Failed to scan alt. I2C bus!");
577+
scan_success = false;
578+
}
579+
} else {
580+
WS_DEBUG_PRINTLN("[i2c] ERROR: alt. I2C bus state is stuck, please "
581+
"reset the board!");
582+
scan_success = false;
583+
}
584+
if (scan_success) {
585+
WS_DEBUG_PRINTLN("[i2c] Scanned alt. I2C bus successfully!");
586+
}
587+
// TODO: Print out what was scanned? or do this at the end
588+
}
552589

590+
// TODO If Muxes are present, scan them
591+
// TODO Scan I2C Port 1 for Muxes
592+
// TODO Scan I2C Port 2 for Muxes
553593

554-
// TODO Linear Scan Alt. I2C Bus
555-
// TODO: Check if alt i2c bus ha been initialized yet, call init. directly if not
556-
// TODO If Muxes are present, scan them
557-
// TODO Scan I2C Port 1 for Muxes
558-
// TODO Scan I2C Port 2 for Muxes
594+
// TODO: Take scan_success into account here
595+
return true;
559596
}
560597

561598
/***********************************************************************/

src/components/i2c/hardware.cpp

Lines changed: 72 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -139,71 +139,84 @@ void I2cHardware::InitBus(bool is_default, const char *sda, const char *scl) {
139139
_bus_status = wippersnapper_i2c_I2cBusStatus_I2C_BUS_STATUS_SUCCESS;
140140
}
141141

142-
bool I2cHardware::ScanBus(wippersnapper_i2c_I2cBusScanned* scan_results) {
142+
bool I2cHardware::ScanBus(wippersnapper_i2c_I2cBusScanned *scan_results) {
143143
if (!scan_results)
144-
return false;
144+
return false;
145145

146-
// TODO: WS object needs to be added for this to work?
147-
/* #ifndef ARDUINO_ARCH_ESP32
148-
// Set I2C WDT timeout to catch I2C hangs, SAMD-specific
149-
WS.enableWDT(I2C_WDT_TIMEOUT_MS);
150-
WS.feedWDT();
151-
#endif */
146+
// TODO: WS object needs to be added for this to work?
147+
/* #ifndef ARDUINO_ARCH_ESP32
148+
// Set I2C WDT timeout to catch I2C hangs, SAMD-specific
149+
WS.enableWDT(I2C_WDT_TIMEOUT_MS);
150+
WS.feedWDT();
151+
#endif */
152152

153-
// Get the SDA and SCL pins from the bus
154-
char i2c_bus_scl[15] = {0}, i2c_bus_sda[15] = {0};
155-
snprintf(i2c_bus_scl, sizeof(i2c_bus_scl), "D%u", _bus_scl);
156-
snprintf(i2c_bus_sda, sizeof(i2c_bus_sda), "D%u", _bus_sda);
153+
// Get the SDA and SCL pins from the bus
154+
char i2c_bus_scl[15] = {0}, i2c_bus_sda[15] = {0};
155+
snprintf(i2c_bus_scl, sizeof(i2c_bus_scl), "D%u", _bus_scl);
156+
snprintf(i2c_bus_sda, sizeof(i2c_bus_sda), "D%u", _bus_sda);
157157

158-
// Perform a bus scan
159-
WS_DEBUG_PRINTLN("[i2c]: Scanning I2C Bus for Devices...");
160-
for (uint8_t address = 1; address < 127; ++address) {
161-
WS_DEBUG_PRINT("[i2c] Scanning Address: 0x");
162-
WS_DEBUG_PRINTLN(address, HEX);
163-
_bus->beginTransmission(address);
164-
uint8_t endTransmissionRC = _bus->endTransmission();
158+
// Perform a bus scan
159+
WS_DEBUG_PRINTLN("[i2c]: Scanning I2C Bus for Devices...");
160+
for (uint8_t address = 1; address < 127; ++address) {
161+
WS_DEBUG_PRINT("[i2c] Scanning Address: 0x");
162+
WS_DEBUG_PRINTLN(address, HEX);
163+
_bus->beginTransmission(address);
164+
uint8_t endTransmissionRC = _bus->endTransmission();
165165

166-
if (endTransmissionRC == 0) {
167-
WS_DEBUG_PRINTLN("[i2c] Found Device!");
168-
scan_results->i2c_bus_found_devices[scan_results->i2c_bus_found_devices_count].i2c_device_address = address;
169-
scan_results->i2c_bus_found_devices[scan_results->i2c_bus_found_devices_count].i2c_mux_address = 0xFFFF; // Tell user that device is not on a mux
170-
strcpy(scan_results->i2c_bus_found_devices[scan_results->i2c_bus_found_devices_count].i2c_bus_sda, i2c_bus_sda);
171-
strcpy(scan_results->i2c_bus_found_devices[scan_results->i2c_bus_found_devices_count].i2c_bus_scl, i2c_bus_scl);
172-
scan_results->i2c_bus_found_devices_count++;
173-
}
174-
#if defined(ARDUINO_ARCH_ESP32)
175-
// Check endTransmission()'s return code (Arduino-ESP32 ONLY)
176-
else if (endTransmissionRC == 3) {
177-
WS_DEBUG_PRINTLN("[i2c] Did not find device: NACK on transmit of data!");
178-
continue;
179-
} else if (endTransmissionRC == 2) {
180-
WS_DEBUG_PRINTLN(
181-
"[i2c] Did not find device: NACK on transmit of address!");
182-
continue;
183-
} else if (endTransmissionRC == 1) {
184-
WS_DEBUG_PRINTLN(
185-
"[i2c] Did not find device: data too long to fit in xmit buffer!");
186-
continue;
187-
} else if (endTransmissionRC == 4) {
188-
WS_DEBUG_PRINTLN(
189-
"[i2c] Did not find device: Unspecified bus error occured!");
190-
continue;
191-
} else if (endTransmissionRC == 5) {
192-
WS_DEBUG_PRINTLN("[i2c] Did not find device: Bus timed out!");
193-
continue;
194-
#endif // ARDUINO_ARCH_ESP32
195-
} else {
196-
WS_DEBUG_PRINTLN(
197-
"[i2c] Did not find device: Unknown bus error has occured!");
198-
continue;
199-
}
166+
if (endTransmissionRC == 0) {
167+
WS_DEBUG_PRINTLN("[i2c] Found Device!");
168+
scan_results
169+
->i2c_bus_found_devices[scan_results->i2c_bus_found_devices_count]
170+
.i2c_device_address = address;
171+
scan_results
172+
->i2c_bus_found_devices[scan_results->i2c_bus_found_devices_count]
173+
.i2c_mux_address = 0xFFFF; // Tell user that device is not on a mux
174+
strcpy(
175+
scan_results
176+
->i2c_bus_found_devices[scan_results->i2c_bus_found_devices_count]
177+
.i2c_bus_sda,
178+
i2c_bus_sda);
179+
strcpy(
180+
scan_results
181+
->i2c_bus_found_devices[scan_results->i2c_bus_found_devices_count]
182+
.i2c_bus_scl,
183+
i2c_bus_scl);
184+
scan_results->i2c_bus_found_devices_count++;
185+
}
186+
#if defined(ARDUINO_ARCH_ESP32)
187+
// Check endTransmission()'s return code (Arduino-ESP32 ONLY)
188+
else if (endTransmissionRC == 3) {
189+
WS_DEBUG_PRINTLN("[i2c] Did not find device: NACK on transmit of data!");
190+
continue;
191+
} else if (endTransmissionRC == 2) {
192+
WS_DEBUG_PRINTLN(
193+
"[i2c] Did not find device: NACK on transmit of address!");
194+
continue;
195+
} else if (endTransmissionRC == 1) {
196+
WS_DEBUG_PRINTLN(
197+
"[i2c] Did not find device: data too long to fit in xmit buffer!");
198+
continue;
199+
} else if (endTransmissionRC == 4) {
200+
WS_DEBUG_PRINTLN(
201+
"[i2c] Did not find device: Unspecified bus error occured!");
202+
continue;
203+
} else if (endTransmissionRC == 5) {
204+
WS_DEBUG_PRINTLN("[i2c] Did not find device: Bus timed out!");
205+
continue;
206+
#endif // ARDUINO_ARCH_ESP32
207+
} else {
208+
WS_DEBUG_PRINTLN(
209+
"[i2c] Did not find device: Unknown bus error has occured!");
210+
continue;
200211
}
201-
202-
/* #ifndef ARDUINO_ARCH_ESP32
203-
// re-enable WipperSnapper SAMD WDT global timeout
204-
WS.enableWDT(WS_WDT_TIMEOUT);
205-
WS.feedWDT();
206-
#endif */
212+
}
213+
214+
/* #ifndef ARDUINO_ARCH_ESP32
215+
// re-enable WipperSnapper SAMD WDT global timeout
216+
WS.enableWDT(WS_WDT_TIMEOUT);
217+
WS.feedWDT();
218+
#endif */
219+
return true; // TODO: Change this!
207220
}
208221

209222
/***********************************************************************/

src/components/i2c/model.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
/***********************************************************************/
2222
I2cModel::I2cModel() {
2323
_msg_i2c_bus_scan = wippersnapper_i2c_I2cBusScan_init_default;
24-
_msg_i2c_bus_scanned = wippersnapper_i2c_I2cBusScan_init_default;
24+
_msg_i2c_bus_scanned = wippersnapper_i2c_I2cBusScanned_init_default;
2525
_msg_i2c_device_add_replace =
2626
wippersnapper_i2c_I2cDeviceAddOrReplace_init_default;
2727
_msg_i2c_device_added_replaced =
@@ -170,8 +170,8 @@ bool I2cModel::DecodeI2cBusScan(pb_istream_t *stream) {
170170
@returns Pointer to a I2cBusScan message.
171171
*/
172172
/**********************************************************************/
173-
wippersnapper_i2c_I2cBusScan *GetI2cBusScanMsg() {
174-
return &_msg_i2c_bus_scan;
173+
wippersnapper_i2c_I2cBusScan *I2cModel::GetI2cBusScanMsg() {
174+
return &_msg_i2c_bus_scan;
175175
}
176176

177177
/**********************************************************************/

0 commit comments

Comments
 (0)