Quellcode durchsuchen

i2c communication generic file added for read and write

namrota ghosh vor 7 Monaten
Ursprung
Commit
2e265d49ac
2 geänderte Dateien mit 57 neuen und 0 gelöschten Zeilen
  1. 39 0
      src/i2c_comm/i2c_hal.c
  2. 18 0
      src/i2c_comm/i2c_hal.h

+ 39 - 0
src/i2c_comm/i2c_hal.c

@@ -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,
+};

+ 18 - 0
src/i2c_comm/i2c_hal.h

@@ -0,0 +1,18 @@
+#ifndef I2C_HAL_H
+#define I2C_HAL_H
+
+#include <stdint.h>
+#include <stdbool.h>
+#include "src/config.h"
+
+/*
+* Since C does not allows to add functions in typedef struct, however a function pointer can be included in Structure in C. This interface provides a standard features of i2c_hal
+*/
+typedef struct{
+    bool (*write)(uint8_t const TARGET_ADDRESS);
+    bool (*read) (uint8_t const TARGET_ADDRESS);
+} I2C_Interface;
+
+extern I2C_Interface i2c_hal;
+
+#endif