| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105 |
- /*
- * This file implements Hardware Abstraction Layer (HAL) to make the I2C
- * communication of MSPM0 SDK compatible with ADC (MCP3426/7/8) and DAC
- * (MCP34728)
- */
- #include "src/interfaces/i2c_controller_interface.h"
- #include "ti/driverlib/dl_i2c.h"
- #include "ti_msp_dl_config.h"
- #include <stdio.h>
- volatile bool gRxComplete;
- volatile bool gTxComplete;
- uint8_t gTxPacket[I2C_TX_MAX_PACKET_SIZE];
- uint8_t gRxPacket[I2C_RX_MAX_PACKET_SIZE];
- uint8_t gTxADClen, gTxADCcount;
- uint8_t gRxADClen, gRxADCcount;
- /*
- static function is for implementing Data Hiding, access to the static function
- is restricted to the file where they are declared const keyword for
- 'TARGET_ADDRESS' and 'Data_length' makes the variable immutable. const uint8_t *
- const Data: means the pointer to the variable and the value of Data is immutable
- */
- I2CRxPackage controllerRxPackage;
- I2CTxPackage controllerTxPackage;
- static bool msp_i2c_write(uint8_t const TARGET_ADDRESS) {
- // Flush any stale data in TX FIFO:
- DL_I2C_flushControllerTXFIFO(I2C_controller_INST);
- // **Check if the I2C bus is stuck before WRITE
- if (DL_I2C_getControllerStatus(I2C_controller_INST) &
- DL_I2C_CONTROLLER_STATUS_ERROR) {
- printf("I2C Communication: Bus is stuck!\n");
- DL_I2C_resetControllerTransfer(I2C_controller_INST);
- return false;
- }
-
- // **Wait for I2C Bus to be Free**
- while (DL_I2C_getControllerStatus(I2C_controller_INST) &
- DL_I2C_CONTROLLER_STATUS_BUSY_BUS)
- ;
- // **Start I2C Write Transaction**
- DL_I2C_startControllerTransfer(I2C_controller_INST, TARGET_ADDRESS,
- DL_I2C_CONTROLLER_DIRECTION_TX, controllerTxPackage.len);
- // **Load Configuration Byte into TX FIFO**
- DL_I2C_fillControllerTXFIFO(I2C_controller_INST, controllerTxPackage.packet, controllerTxPackage.len);
- for (uint8_t i = 0; i < controllerTxPackage.len; i++) {
- printf("Sending 0x%02X\n", controllerTxPackage.packet[i]);
- }
- // ** Wait for the I2C Bus to be FREE **
- while (DL_I2C_getControllerStatus(I2C_controller_INST) &
- DL_I2C_CONTROLLER_STATUS_BUSY_BUS)
- ;
-
- // **Check if the target address is incorrect
-
- if (DL_I2C_getControllerStatus(I2C_controller_INST) & DL_I2C_CONTROLLER_STATUS_ADDR_ACK) {
- printf("I2C Write Error: Target Address not acknowledged!\n");
- return false;
- }
-
- // Debug for I2C WRITE:
- //printf("HAL Write: Address=0x%02X, Data Length=%d\n", TARGET_ADDRESS, Data_length);
- // **Check for any WRITE error
- if (DL_I2C_getControllerStatus(I2C_controller_INST) & DL_I2C_CONTROLLER_STATUS_ERROR) {
- printf("I2C Write Error: Bus error after sending data.\n");
- return false;
- }
- return true;
- }
- static bool msp_i2c_read(uint8_t const TARGET_ADDRESS) {
-
- // Flush any stale data in TX FIFO:
- DL_I2C_flushControllerRXFIFO(I2C_controller_INST);
- while (DL_I2C_getControllerStatus(I2C_controller_INST) &
- DL_I2C_CONTROLLER_STATUS_BUSY_BUS)
- ;
- DL_I2C_startControllerTransfer(I2C_controller_INST, TARGET_ADDRESS,
- DL_I2C_CONTROLLER_DIRECTION_RX, controllerRxPackage.len);
- while (DL_I2C_getControllerStatus(I2C_controller_INST) &
- DL_I2C_CONTROLLER_STATUS_BUSY_BUS)
- ;
-
- DL_I2C_enableInterrupt(I2C_controller_INST,
- DL_I2C_INTERRUPT_CONTROLLER_RXFIFO_TRIGGER);
-
-
- return true;
- }
- I2C_Interface i2c_hal = {
- .write = msp_i2c_write,
- .read = msp_i2c_read,
- };
|