multiplexer.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #include "multiplexer.h"
  2. #include "ti/driverlib/dl_i2c.h"
  3. #include "ti_msp_dl_config.h"
  4. #include <stdio.h>
  5. #include <stdint.h>
  6. /*
  7. Multiplexer TCA9548A:
  8. Address:0x70
  9. The TCA9548A is example of a single-register device, which is controlled via I2C commands. Since it has 1 bit to enable or disable a channel, there is only 1 register
  10. needed, and the controller merely writes the register data after the target address, skipping the register number.
  11. ADC connected to channel 0
  12. Both SDA and SL lines of the multiplexer is connected to VIN through pull-up resistors
  13. The following is the general procedure for a controller to access a target device:
  14. 1. If a controller wants to send data to a target:
  15. • Controller-transmitter sends a START condition and addresses the target-receiver.
  16. • Controller-transmitter sends data to target-receiver.
  17. • Controller-transmitter terminates the transfer with a STOP condition.
  18. 2. If a controller wants to receive or read data from a target:
  19. • Controller-receiver sends a START condition and addresses the target-transmitter.
  20. • Controller-receiver sends the requested register to read to target-transmitter.
  21. • Controller-receiver receives data from the target-transmitter.
  22. • Controller-receiver terminates the transfer with a STOP condition.
  23. The TCA9548A is example of a single-register device, which is controlled via I2C commands. Since it has 1 bit to enable or disable a channel,
  24. there is only 1 register needed, and the controller merely writes the register data after the target address, skipping the register number.
  25. */
  26. /*Function for Multiplexer*/
  27. void Multiplexer_SelectChannel(uint8_t channel)
  28. {
  29. if (channel > 7) {
  30. printf("ERROR: Invalid Multiplexer Channel! Must be 0-7.\n");
  31. return;
  32. }
  33. uint8_t data = (channel);
  34. printf("Selecting Multiplexer Channel: 0x%02X\n", data);
  35. // Ensure bus is idle before starting communication
  36. while (DL_I2C_getControllerStatus(I2C_controller_INST) & DL_I2C_CONTROLLER_STATUS_BUSY_BUS);
  37. // SEND Command to Multiplexer
  38. DL_I2C_startControllerTransfer(I2C_controller_INST, MULTIPLEXER_I2C_ADDR, DL_I2C_CONTROLLER_DIRECTION_TX, 1);
  39. DL_I2C_fillControllerTXFIFO(I2C_controller_INST, &data, 1);
  40. // **Ensure STOP condition is sent**
  41. while (DL_I2C_getControllerStatus(I2C_controller_INST) & DL_I2C_CONTROLLER_STATUS_BUSY_BUS);
  42. // **Slightly Increase Delay for Multiplexer to Process Command**
  43. delay_cycles(30000);
  44. // Verify Multiplexer Response:
  45. //uint8_t response= 0x00;
  46. DL_I2C_startControllerTransfer(I2C_controller_INST, MULTIPLEXER_I2C_ADDR, DL_I2C_CONTROLLER_DIRECTION_RX, 1);
  47. // Wait for a response from the multiplexer
  48. while (DL_I2C_getControllerStatus(I2C_controller_INST) & DL_I2C_CONTROLLER_STATUS_BUSY_BUS);
  49. //uint8_t response = DL_I2C_receiveControllerData(I2C_controller_INST);
  50. // **Debug: Print Expected vs. Received Response**
  51. //printf("Multiplexer Response: 0x%02X (Expected: 0x%02X)\n", response, data);
  52. // **CHECK FOR ADDRESS ACKNOWLEDGMENT**
  53. //(if (!(DL_I2C_getControllerStatus(I2C_controller_INST) & DL_I2C_CONTROLLER_STATUS_ERROR)) {
  54. // printf("Multiplexer detected at 0x70.\n");
  55. // return;
  56. //} else{
  57. // printf("ERROR: Multiplexer (0x70) detected.\n");
  58. //}
  59. // Wait for transaction completion
  60. //while (DL_I2C_getControllerStatus(I2C_controller_INST) & DL_I2C_CONTROLLER_STATUS_BUSY_BUS);
  61. //if(response != data){
  62. // printf("ERROR: Multiplexer did not set the correct channel!\n");
  63. // **Retry Mechanism: Attempt to Set the Channel Again**
  64. // delay_cycles(10000);
  65. // Multiplexer_SelectChannel(channel);
  66. //} else {
  67. // printf("Multiplexer Active Channel: 0x%X\n", data);
  68. // return;
  69. //}
  70. }