|
|
@@ -0,0 +1,39 @@
|
|
|
+#include "ti/driverlib/dl_i2c.h"
|
|
|
+#include "src/i2c_comm/i2c_hal.h"
|
|
|
+#include "ti_msp_dl_config.h"
|
|
|
+#include <stdio.h>
|
|
|
+
|
|
|
+// Global extern variable defined in config.h and declared here
|
|
|
+txPacket tx_packet;
|
|
|
+rxPacket rx_packet;
|
|
|
+
|
|
|
+static bool i2c_write(uint8_t const TARGET_ADDRESS){
|
|
|
+ // Write data to the target address
|
|
|
+ if(DL_I2C_getControllerStatus(I2C_1_INST)& (DL_I2C_CONTROLLER_STATUS_ERROR)){
|
|
|
+ printf("I2C Write Error: Bus is stuck\n");
|
|
|
+ DL_I2C_resetControllerTransfer(I2C_1_INST);
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ // **Wait for I2C Bus to be Free**
|
|
|
+ while (DL_I2C_getControllerStatus(I2C_1_INST) & DL_I2C_CONTROLLER_STATUS_BUSY_BUS);
|
|
|
+ DL_I2C_startControllerTransfer(I2C_1_INST, TARGET_ADDRESS, DL_I2C_CONTROLLER_DIRECTION_TX, tx_packet.txLen);
|
|
|
+ DL_I2C_fillControllerTXFIFO(I2C_1_INST, tx_packet.txBuffer, tx_packet.txLen);
|
|
|
+ DL_I2C_flushTargetTXFIFO(I2C_1_INST);
|
|
|
+ return true;
|
|
|
+}
|
|
|
+
|
|
|
+static bool i2c_read(uint8_t const TARGET_ADDRESS){
|
|
|
+ //Read data from the target address
|
|
|
+ DL_I2C_flushControllerRXFIFO(I2C_1_INST);
|
|
|
+ while (DL_I2C_getControllerStatus(I2C_1_INST) & DL_I2C_CONTROLLER_STATUS_BUSY_BUS);
|
|
|
+ DL_I2C_startControllerTransfer(I2C_1_INST, TARGET_ADDRESS, DL_I2C_CONTROLLER_DIRECTION_RX, rx_packet.rxLen);
|
|
|
+ while (DL_I2C_getControllerStatus(I2C_1_INST) & DL_I2C_CONTROLLER_STATUS_BUSY_BUS);
|
|
|
+ DL_I2C_enableInterrupt(I2C_1_INST, DL_I2C_INTERRUPT_CONTROLLER_RXFIFO_TRIGGER);
|
|
|
+ return true;
|
|
|
+}
|
|
|
+// I2C HAL interface implementation
|
|
|
+// This structure provides a standard interface for I2C operations
|
|
|
+I2C_Interface i2c_hal = {
|
|
|
+ .write = i2c_write,
|
|
|
+ .read = i2c_read,
|
|
|
+};
|