i2c_peripherals_scan.c 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. #include "ti/driverlib/dl_i2c.h"
  2. #include "ti_msp_dl_config.h"
  3. #include <stdio.h>
  4. #include <stdint.h>
  5. /*
  6. Scans all the addresses of the peripherals:
  7. */
  8. void I2C_scanBus(I2C_Regs *i2c) {
  9. printf("Scanning I2C Bus...\n");
  10. // **Step 1: Reset I2C Controller if Busy**
  11. if (DL_I2C_getControllerStatus(i2c) & DL_I2C_CONTROLLER_STATUS_BUSY_BUS) {
  12. printf("I2C Bus Busy! Resetting I2C Controller...\n");
  13. DL_I2C_disableController(i2c); // Disable I2C
  14. delay_cycles(20000);
  15. DL_I2C_enableController(i2c); // Re-enable I2C
  16. delay_cycles(20000);
  17. }
  18. // **Step 2: Scan I2C Bus**
  19. for (uint8_t addr = 0x08; addr < 0x78; addr++) { // Valid I2C Address Range
  20. DL_I2C_startControllerTransfer(i2c, addr, DL_I2C_CONTROLLER_DIRECTION_RX, 1);
  21. delay_cycles(5000);
  22. if (!(DL_I2C_getControllerStatus(i2c) & DL_I2C_CONTROLLER_STATUS_ERROR)) {
  23. printf("Device found at: 0x%02X\n", addr);
  24. }else {
  25. // Clear the error by resetting the I2C controller
  26. DL_I2C_disableController(i2c);
  27. DL_I2C_enableController(i2c);
  28. }
  29. }
  30. //printf("I2C Scan Complete!\n");
  31. }