|
|
@@ -0,0 +1,85 @@
|
|
|
+/*
|
|
|
+ * 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>
|
|
|
+
|
|
|
+/*
|
|
|
+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
|
|
|
+*/
|
|
|
+static bool msp_i2c_write(uint8_t const TARGET_ADDRESS,
|
|
|
+ const uint8_t *const Data,
|
|
|
+ uint8_t const Data_length) {
|
|
|
+ // 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, Data_length);
|
|
|
+
|
|
|
+ // **Load Configuration Byte into TX FIFO**
|
|
|
+ DL_I2C_fillControllerTXFIFO(I2C_controller_INST, (uint8_t *)Data,
|
|
|
+ Data_length);
|
|
|
+
|
|
|
+ // ** 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,
|
|
|
+ uint8_t const Data_length) {
|
|
|
+
|
|
|
+ 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, Data_length);
|
|
|
+ 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,
|
|
|
+};
|