i2c_controller.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. /*
  2. Main Source File
  3. */
  4. #include "i2c_target.h"
  5. #include "ti/devices/msp/peripherals/hw_dac12.h"
  6. #include "ti/driverlib/dl_adc12.h"
  7. #include "ti/driverlib/dl_gpio.h"
  8. #include "ti/driverlib/dl_i2c.h"
  9. #include "ti/driverlib/m0p/dl_core.h"
  10. #include "ti_msp_dl_config.h"
  11. #include "ti/comm_modules/i2c/controller/i2c_comm_controller.h"
  12. #include <stdint.h>
  13. #include <stdio.h>
  14. #include <string.h>
  15. #include "multiplexer.h"
  16. #include "adc.h"
  17. #include "dac.h"
  18. #include "battery.h"
  19. #include "i2c_target.h"
  20. #include "cc_cv_charging.h"
  21. #include <ti/drivers/GPIO.h>
  22. I2C_Instance gI2C;
  23. I2C_ResponseInfo gResponse;
  24. BatteryData battery_data;
  25. /*Interrupt for MCU -> ADC
  26. * CASE: DL_I2C_IIDX_CONTROLLER_RX_DONE: ADC Reception Complete
  27. - ADC has finished sending data and it's fully received.
  28. - gI2C.rxMsg.len = gI2C.rxMsg.ptr:
  29. - Stores the received data length in the response buffer.
  30. - I2C_decodeResponse():
  31. - Decodes the received response.
  32. - gI2C.status = I2C_STATUS_RX_COMPLETE:
  33. - Marks reception is complete.
  34. * CASE: DL_I2C_IIDX_CONTROLLER_TX_DONE: Data Transmit to ADC complete
  35. - DL_I2C_disableInterrupt(..): Disables the TXFIFO interrupt since data is now sent
  36. * CASE: DL_I2C_IIDX_CONTROLLER_RXFIFO_TRIGGER: Receive Data in FIFO
  37. - The I2C Receive FIFO has data ready to be read.
  38. - while (DL_I2C_isControllerRXFIFOEmpty(...) != true): Loops until the RX FIFOis empty (READ all available bytes)
  39. - Inside the while loop:
  40. - If buffer has SPACE, store the received byte
  41. - Prints each received byte in HEXADECIMAL format for debugging
  42. - IF BUFFER is FULL, avoids OVERFLOW by discarding extra byte.
  43. * CASE: DL_I2C_IIDX_CONTROLLER_TXFIFO_TRIGGER: Transmit Data in FIFO
  44. - If there is still data to send:
  45. gI2C.txMsg.ptr += DL_I2C_fillControllerTXFIFO(I2C_controller_INST, &gI2C.txMsg.buffer[gI2C.txMsg.ptr], gI2C.txMsg.len - gI2C.txMsg.ptr);
  46. */
  47. void I2C_controller_INST_IRQHandler(void)
  48. {
  49. //printf("I2C Interrupt Triggered to ADC!\n");
  50. switch (DL_I2C_getPendingInterrupt(I2C_controller_INST))
  51. { /*START Condition*/
  52. case DL_I2C_IIDX_CONTROLLER_START:
  53. //gTxADCcount= 0;
  54. gRxADCcount= 0;
  55. DL_I2C_flushControllerTXFIFO(I2C_controller_INST);
  56. break;
  57. case DL_I2C_IIDX_CONTROLLER_RXFIFO_TRIGGER:
  58. gI2C.status= I2C_STATUS_RX_INPROGRESS;
  59. /* Store bytes received from target in Rx Msg Buffer */
  60. while (DL_I2C_isControllerRXFIFOEmpty(I2C_controller_INST) != true) {
  61. if (gRxADCcount< gRxADClen) {
  62. gRxPacket[gRxADCcount] = DL_I2C_receiveControllerData(I2C_controller_INST);
  63. printf("Received Byte[%d]: 0x%02X\n", gRxADCcount, gRxPacket[gRxADCcount]); // Debug print
  64. gRxADCcount++;
  65. } else {
  66. //printf("ERROR: RX Buffer Overflow! ptr=%d MAX_BUFFER_SIZE=%d\n", gI2C.rxMsg.ptr, MAX_BUFFER_SIZE);
  67. /* Ignore and remove from FIFO if the buffer is full */
  68. DL_I2C_receiveControllerData(I2C_controller_INST);
  69. }
  70. }
  71. if (gRxADCcount >= gRxADClen){
  72. //printf("ADC Bytes Received!\n");
  73. gRxComplete = true;
  74. DL_I2C_enableInterrupt(I2C_controller_INST, DL_I2C_INTERRUPT_CONTROLLER_STOP);
  75. }
  76. break;
  77. /*TRANSMIT data to ADC*/
  78. case DL_I2C_IIDX_CONTROLLER_TXFIFO_TRIGGER:
  79. //printf("TX FIFO with data!\n");
  80. gI2C.status= I2C_STATUS_TX_INPROGRESS;
  81. if(gTxADCcount<gTxADClen){
  82. gTxADCcount+= DL_I2C_fillControllerTXFIFO(I2C_controller_INST, &gTxPacket[gTxADCcount], (gTxADClen-gTxADCcount));
  83. } else{
  84. /*Prevent overflow and just ignore data*/
  85. DL_I2C_fillTargetTXFIFO(I2C_controller_INST, (uint8_t[]){0x00}, 1);
  86. gTxComplete= true;
  87. }
  88. //DL_I2C_flushControllerTXFIFO(I2C_controller_INST);
  89. break;
  90. /*STOP condition*/
  91. case DL_I2C_IIDX_CONTROLLER_STOP:
  92. gTxComplete= true;
  93. gRxComplete = true;
  94. //printf("I2C Stop Detected- RX Complete");
  95. break;
  96. case DL_I2C_IIDX_CONTROLLER_ARBITRATION_LOST:
  97. //printf("Interrupt index for I2C controller Arbitration Lost!\n");
  98. break;
  99. case DL_I2C_IIDX_CONTROLLER_NACK:
  100. //printf("I2C NACK Received\n");
  101. if((gI2C.status== I2C_STATUS_RX_STARTED)|| (gI2C.status= I2C_STATUS_TX_STARTED)){
  102. gI2C.status= I2C_STATUS_ERROR;
  103. }
  104. break;
  105. default:
  106. break;
  107. }
  108. }
  109. /**** Interrupt for Pi to MCU ****/
  110. void I2C_target_INST_IRQHandler(void) {
  111. //printf("I2C Interrupt Triggered to MCU (TARGET)!\n");
  112. uint8_t receivedCommand= 0;
  113. uint32_t status = DL_I2C_getPendingInterrupt(I2C_target_INST);
  114. //ADC_PARAMS params;
  115. switch (status) {
  116. /* START condition detected */
  117. case DL_I2C_IIDX_TARGET_START:
  118. piTxCount= 0;
  119. piRxCount= 0;
  120. piTxComplete= false;
  121. DL_I2C_flushTargetTXFIFO(I2C_target_INST);
  122. break;
  123. /* STOP condition detected */
  124. case DL_I2C_IIDX_TARGET_STOP:
  125. piTxComplete= true;
  126. piRxComplete = true;
  127. DL_I2C_flushTargetTXFIFO(I2C_target_INST);
  128. break;
  129. /* TX FIFO trigger (Pi is reading data from MCU) */
  130. /* GET battery status is triggered when command is 0x01
  131. - Pi on request of 0x01 will get a response of the battery status for all the slots
  132. - Battery_StateUpdate function is called, which in turn calls the Battery_ReadState funtion to set the state of the batteries
  133. -Pi on command of [0x02, slot_id] will GET the 'Battery Data' which is voltage, current and temperature for a given slot.
  134. - MCU reads the slot_id from Pi using DL_I2C_receiveTargetData()
  135. - piTxCount is set to 0
  136. - piTxLen is the sizeof BatteryData struct which is 7 bytes
  137. - If the requested slot is correct then:
  138. - battery pointer variable points to the memory of the requested slot
  139. - the values of voltage, current and temperature are then stored in battery_data struct
  140. - Once the values are in BatteryData struct we wait for the bus to be free
  141. - Next we send the BatteryData to Pi using DL_I2C_fillTargetRXFIFO()
  142. - Reset the RX counter for the next data.
  143. */
  144. case DL_I2C_IIDX_TARGET_TXFIFO_TRIGGER:
  145. printf("Pi GET data from MCU!\n");
  146. if(!DL_I2C_isTargetRXFIFOEmpty(I2C_target_INST)){
  147. receivedCommand= DL_I2C_receiveTargetData(I2C_target_INST);
  148. //printf("Received Command: 0x%02X\n", receivedCommand);
  149. if (receivedCommand == CMD_GET_BATTERY_STATUS){
  150. printf("Battery status received.\n");
  151. //Battery_StateUpdate();
  152. piTxCount= 0;
  153. piTxLen= NUM_SLOTS;
  154. piTxComplete = false;
  155. // Prepare data to be sent to Pi:
  156. for(uint8_t slot= 0; slot < NUM_SLOTS; slot++){
  157. // Read the battery status for each slot
  158. Battery_ReadState(slot);
  159. piTxPacket[slot]= batteries[slot].state;
  160. }
  161. //Filling up the FIFO
  162. if(piTxCount < piTxLen){
  163. while (DL_I2C_getTargetStatus(I2C_target_INST) & DL_I2C_TARGET_STATUS_BUS_BUSY)
  164. ;
  165. piTxCount += DL_I2C_fillTargetTXFIFO(I2C_target_INST, &piTxPacket[piTxCount], (piTxLen-piTxCount));
  166. }
  167. else {
  168. /*
  169. * Fill FIFO with 0x00 if more data is requested than expected piTxLen
  170. */
  171. while (
  172. DL_I2C_transmitTargetDataCheck(I2C_target_INST, 0x00) != false);
  173. }
  174. piTxComplete= true;
  175. }
  176. else if (receivedCommand == CMD_GET_BATTERY_DATA){
  177. uint8_t requestedSlot= DL_I2C_receiveTargetData(I2C_target_INST);
  178. while (DL_I2C_getTargetStatus(I2C_target_INST) & DL_I2C_TARGET_STATUS_BUS_BUSY)
  179. ;
  180. //printf("Battery Data Requested for Slot %d!\n", requestedSlot);
  181. piTxCount= 0;
  182. piTxLen= sizeof(BatteryData);
  183. BatteryData battery_data;
  184. if(requestedSlot < NUM_SLOTS){
  185. Battery *battery= &batteries[requestedSlot];
  186. battery_data.slot_id = battery-> slot_id;
  187. battery_data.voltage= battery-> voltage;
  188. battery_data.current= battery-> current;
  189. battery_data.temperature= battery-> temperature;
  190. while (DL_I2C_getTargetStatus(I2C_target_INST) & DL_I2C_TARGET_STATUS_BUS_BUSY);
  191. DL_I2C_fillTargetTXFIFO(I2C_target_INST, (uint8_t *)&battery_data, sizeof(BatteryData));
  192. //piTxCount += DL_I2C_fillTargetTXFIFO(I2C_target_INST, (uint8_t*)&battery_data, piTxLen);
  193. piTxComplete= true;
  194. while (DL_I2C_getTargetStatus(I2C_target_INST) & DL_I2C_TARGET_STATUS_BUS_BUSY);
  195. if(piTxCount >= piTxLen){
  196. piTxComplete= true;
  197. piTxCount=0;
  198. }
  199. } else{
  200. //printf("Invalid Slot ID: %d\n.", requestedSlot);
  201. }
  202. }
  203. }
  204. break;
  205. /* TARGET_Rx FIFO trigger (Pi is writing data to MCU) */
  206. /*Pi SET battery data limits for each slot, where:
  207. - RXFIFO buffer is filled if the command from Pi is 0x03
  208. - Creating a temporary buffer named ´rxbuffer´
  209. - sizeof(BatteryLimitMsg): 11 bytes (1 byte: slot_id, 2 bytes: min_voltage; max_voltage; cut_off_current; capacitance; charge_fraction)
  210. - rx_buffer stores the data from Pi.
  211. - if all the expected bytes are received from Pi then,
  212. - memcpy() to copy the block of address from the temporary buffer to the BatteryLimitMsg structure
  213. - Why?, A: It copies the specified number of bytes from one memory location to another regardless of the type of the data stored.
  214. - verify if the received slot_id is less than NUM_SLOTS, where slot_id count starts from 0 then:
  215. - create a pointer variable for 'Battery'
  216. - battery_limits.slot_id: index of the battery slot to be updated
  217. - &batteries[battery_limits.slot_id]: gets the memory address of the battery in that slot
  218. - Accessing the structure members of Battery using -> operator. This allows efficient access to the structure's members
  219. without directly using the structure variable.
  220. */
  221. case DL_I2C_IIDX_TARGET_RXFIFO_TRIGGER:
  222. printf("Pi SET Battery limit to MCU.....\n");
  223. if(!DL_I2C_isTargetRXFIFOEmpty(I2C_target_INST)){
  224. receivedCommand= DL_I2C_receiveTargetData(I2C_target_INST);
  225. //printf("Received Command: 0x%02X\n", receivedCommand);
  226. if(receivedCommand == CMD_SET_BATTERY_LIMIT){
  227. uint8_t rx_buffer[sizeof(BatteryLimitMsg)];
  228. uint8_t index= 0;
  229. while (!DL_I2C_isTargetRXFIFOEmpty(I2C_target_INST)){
  230. if(index < sizeof(BatteryLimitMsg)){
  231. rx_buffer[index]= DL_I2C_receiveTargetData(I2C_target_INST);
  232. //printf("Received Byte[%d]: 0x%02X\n", index, rx_buffer[index]);
  233. index++;
  234. }
  235. else{
  236. DL_I2C_receiveTargetData(I2C_target_INST);
  237. }
  238. }
  239. //printf("Total Bytes Received: %d (Expected: %d)\n", index, sizeof(BatteryLimitMsg));
  240. if(index == sizeof(BatteryLimitMsg)){
  241. //printf("Received Battery Limits.\n");
  242. BatteryLimitMsg battery_limits;
  243. memcpy(&battery_limits, rx_buffer, sizeof(BatteryLimitMsg));
  244. if(battery_limits.slot_id < NUM_SLOTS){
  245. Battery *battery = &batteries[battery_limits.slot_id];
  246. battery -> min_voltage = battery_limits.min_voltage;
  247. battery -> max_voltage = battery_limits.max_voltage;
  248. battery -> cut_off_current = battery_limits.cut_off_current;
  249. battery -> capacitance = battery_limits.capacitance;
  250. battery -> charge_fraction = battery_limits.charge_fraction;
  251. /*printf("\n Received Battery Limits for slot %d: \n", battery_limits.slot_id);
  252. printf(" Min Voltage: %d mV (0x%04X)\n", battery_limits.min_voltage, battery_limits.min_voltage);
  253. printf(" Max Voltage: %d mV (0x%04X)\n", battery_limits.max_voltage, battery_limits.max_voltage);
  254. printf(" Cutoff Current: %d mA (0x%04X)\n", battery_limits.cut_off_current, battery_limits.cut_off_current);
  255. printf(" Capacitance: %d µF (0x%04X)\n", battery_limits.capacitance, battery_limits.capacitance);
  256. printf(" Charge Fraction: %d%% (0x%02X)\n", battery_limits.charge_fraction, battery_limits.charge_fraction);*/
  257. }
  258. }
  259. }
  260. }
  261. break;
  262. /* Arbitration lost or NACK */
  263. case DL_I2C_IIDX_TARGET_ARBITRATION_LOST:
  264. printf("Arbitration Lost.\n");
  265. break;
  266. default:
  267. printf("Unknown Interrupt.\n");
  268. break;
  269. }
  270. }
  271. /********MAIN function*************/
  272. int main(void)
  273. {
  274. // Initialize System and I2C
  275. SYSCFG_DL_init();
  276. // Initialize battery array and default params
  277. Battery_Init();
  278. //Reset_I2C_Bus();
  279. NVIC_EnableIRQ(I2C_target_INST_INT_IRQN);
  280. NVIC_EnableIRQ(I2C_controller_INST_INT_IRQN);
  281. printf("............System Configuration Enabled...............\n");
  282. //Multiplexer
  283. Multiplexer_SelectChannel(I2C_CHANNEL);
  284. //I2C_scanBus();
  285. I2C_init(&gI2C);
  286. //ADC_SetConfigurationBytes(adc_voltage_params);
  287. //delay_cycles(50000);
  288. //ADC_SetConfigurationBytes(adc_current_params);
  289. //delay_cycles(50000);
  290. //DAC_ReadCurrentAddress();
  291. while (1)
  292. { //Looping through the ADC Channels
  293. /*for(uint8_t slot_id=0; slot_id< NUM_SLOTS; slot_id++){
  294. for(uint8_t adc_channel=0; adc_channel< ADC_CHANNEL_NUM; adc_channel++){
  295. batteries[slot_id].channel= adc_channel;
  296. Battery_UpdateADCReading(slot_id, batteries[slot_id].channel);
  297. }
  298. }*/
  299. //CC-CV Cycle: maximum cycles is not yet implemented
  300. //for(uint8_t slot_id= 0; slot_id < NUM_SLOTS; slot_id++){
  301. // CC_CV_ControlCharging(slot_id);
  302. //}
  303. //DAC_fastWrite(CHANNEL_A_VALUE);
  304. }
  305. }