i2c_peripherals_scan.c 1.2 KB

123456789101112131415161718192021222324252627282930313233343536
  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. // **Step 1: Reset I2C Controller if Busy**
  10. if (DL_I2C_getControllerStatus(i2c) & DL_I2C_CONTROLLER_STATUS_BUSY_BUS) {
  11. printf("I2C Bus Busy! Resetting I2C Controller...\n");
  12. DL_I2C_disableController(i2c); // Disable I2C
  13. delay_cycles(20000);
  14. DL_I2C_enableController(i2c); // Re-enable I2C
  15. delay_cycles(20000);
  16. }
  17. // **Step 2: Scan I2C Bus**
  18. for (uint8_t addr = 0x08; addr < 0x78; addr++) { // Valid I2C Address Range
  19. DL_I2C_startControllerTransfer(i2c, addr, DL_I2C_CONTROLLER_DIRECTION_RX, 1);
  20. delay_cycles(5000);
  21. if (!(DL_I2C_getControllerStatus(i2c) & DL_I2C_CONTROLLER_STATUS_ERROR)) {
  22. printf("Device found at: 0x%02X\n", addr);
  23. }else {
  24. // Clear the error by resetting the I2C controller
  25. printf("Device not found...\n");
  26. DL_I2C_disableController(i2c);
  27. DL_I2C_enableController(i2c);
  28. }
  29. }
  30. //printf("I2C Scan Complete!\n");
  31. }