Browse Source

Logic added for helper functions to read and write in i2c comm

namrota ghosh 6 months ago
parent
commit
5c18c6d9ed
2 changed files with 54 additions and 17 deletions
  1. 52 15
      src/i2c_comm/i2c_hal.c
  2. 2 2
      src/i2c_comm/i2c_hal.h

+ 52 - 15
src/i2c_comm/i2c_hal.c

@@ -8,28 +8,65 @@ tx_Packet txPacket;
 rx_Packet rxPacket;
 
 
-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);
+static bool i2c_write(uint8_t const i2c_address, uint8_t *data, uint8_t data_len){
+    //Flush the TX FIFO and make the buffer ready for the transmit
+    DL_I2C_flushControllerTXFIFO(I2C_1_INST);
+
+    //Fill the controller with the data:
+    DL_I2C_fillControllerTXFIFO(I2C_1_INST, data, data_len);
+
+    //Wait for the bus to be ready for transmit
+    uint32_t timeout = 10000; 
+    while (!(DL_I2C_getControllerStatus(I2C_1_INST) & DL_I2C_CONTROLLER_STATUS_IDLE) && timeout--);
+    if(timeout == 0){
+        printf("Error in reading from I2C Bus: Bus is not getting ready before transmit start\n");
+        DL_I2C_resetControllerTransfer(I2C_1_INST); 
+    }
+
+    //Start the TX transfer
+    DL_I2C_startControllerTransferAdvanced(I2C_1_INST, i2c_address, DL_I2C_CONTROLLER_DIRECTION_TX, data_len, DL_I2C_CONTROLLER_START_ENABLE, DL_I2C_CONTROLLER_STOP_ENABLE, DL_I2C_CONTROLLER_ACK_ENABLE); 
+
+    //Trap the Error if the address is not acknowledeged by the Target:
+    if (DL_I2C_getControllerStatus(I2C_1_INST) & DL_I2C_CONTROLLER_STATUS_ADDR_ACK) {
+        printf("I2C Write Error: Target Address not acknowledged!\n");
+        __BKPT(0);  //for testing
         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, txPacket.txLen);
-    DL_I2C_fillControllerTXFIFO(I2C_1_INST, txPacket.txBuffer, txPacket.txLen);
-    DL_I2C_flushTargetTXFIFO(I2C_1_INST);
+
     return true;
 }
 
-static bool i2c_read(uint8_t const TARGET_ADDRESS){
-    //Read data from the target address
+static bool i2c_read(uint8_t const i2c_address){
+    //Make RX Buffer ready to receive the data
     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, rxPacket.rxLen);
-    while (DL_I2C_getControllerStatus(I2C_1_INST) & DL_I2C_CONTROLLER_STATUS_BUSY_BUS);
+
+    //Wait for the bus ti be ready to transmit
+    uint32_t timeout= 10000;
+    while (!(DL_I2C_getControllerStatus(I2C_1_INST) & DL_I2C_CONTROLLER_STATUS_IDLE) && timeout--);
+    if(timeout == 0){
+        printf("Error in reading from I2C Bus: Bus is not getting ready before transmit start\n");
+        DL_I2C_resetControllerTransfer(I2C_1_INST); 
+        return false;
+    }
+
+    //Enable the Rx Interrupt:
     DL_I2C_enableInterrupt(I2C_1_INST, DL_I2C_INTERRUPT_CONTROLLER_RXFIFO_TRIGGER);
+    
+    while (DL_I2C_getControllerStatus(I2C_1_INST) & DL_I2C_CONTROLLER_STATUS_BUSY)
+    ;
+    
+    //Send a read request to Target
+    DL_I2C_startControllerTransferAdvanced(I2C_1_INST, i2c_address, DL_I2C_CONTROLLER_DIRECTION_RX, rxPacket.rxLen, DL_I2C_CONTROLLER_START_ENABLE,
+                                           DL_I2C_CONTROLLER_STOP_ENABLE,
+                                           DL_I2C_CONTROLLER_ACK_ENABLE);
+
+    //Trap the Error if the address is not acknowledeged by the Target:
+    if (DL_I2C_getControllerStatus(I2C_1_INST) & DL_I2C_CONTROLLER_STATUS_ADDR_ACK) {
+        printf("I2C Write Error: Target Address not acknowledged!\n");
+        __BKPT(0); //for testing
+        return false;
+    }
+    
     return true;
 }
 // I2C HAL interface implementation

+ 2 - 2
src/i2c_comm/i2c_hal.h

@@ -9,8 +9,8 @@
 * 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);
+    bool (*write)(uint8_t const i2c_address, uint8_t *data, uint8_t data_len);
+    bool (*read) (uint8_t const i2c_address);
 } I2C_Interface;
 
 extern I2C_Interface i2c_hal;