Переглянути джерело

volatule keyword added to the buffers for i2c communication

namrota ghosh 6 місяців тому
батько
коміт
63c998a5a4
1 змінених файлів з 23 додано та 8 видалено
  1. 23 8
      src/config.h

+ 23 - 8
src/config.h

@@ -11,18 +11,33 @@
 #define MEASUREMENT_CHECK_INTERVAL 160000 //Do not know yet the exact timing
 #define HEALTHY_BATTERY_VALUE (3800)
 
+/*
+* https://www.embedded.com/introduction-to-the-volatile-keyword/
+* https://e2e.ti.com/support/microcontrollers/msp-low-power-microcontrollers-group/msp430/f/msp-low-power-microcontroller-forum/393364/avoid-race-condition-between-isr-and-main
+* https://www.omi.me/blogs/firmware-guides/how-to-handle-race-conditions-in-embedded-c-without-using-an-rtos
+* A variable should be declared as volatile if the value could change unexpectedly.
+* In practice, only three types of variable could change:
+* Memory-mapped peripheral registers
+* Global variables modified by an ISR
+* Global variables within a multi-threaded application
+* The volatile keyword in C is used to inform the compiler not to optimize access to a variable that might change unexpectedly. 
+* This is particularly useful for variables shared between an interrupt service routine (ISR) and the main program.
+
+Observation Noted are:
+- Race condition while reading and storing the buffers, leading to lag the Rx Buffer during the execution at times or completely lagging by 1 for each slots; incorrect Read
+*/
 typedef struct{
-     uint8_t txBuffer[I2C_TX_MAX_PACKET_SIZE];
-     uint8_t txLen;
-     uint8_t txCount;
-     bool txComplete;
+     volatile uint8_t txBuffer[I2C_TX_MAX_PACKET_SIZE];
+     volatile uint8_t txLen;
+     volatile uint8_t txCount;
+     volatile bool txComplete;
 }tx_Packet;
 
 typedef struct{
-     uint8_t rxBuffer[I2C_RX_MAX_PACKET_SIZE];
-     uint8_t rxLen;
-     uint8_t rxCount;
-     bool rxComplete;
+     volatile uint8_t rxBuffer[I2C_RX_MAX_PACKET_SIZE];
+     volatile uint8_t rxLen;
+     volatile uint8_t rxCount;
+     volatile bool rxComplete;
 }rx_Packet;
 
 // Global variables declared in i2c_hal.c