i2c_pi_target.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. #include "i2c_pi_target.h"
  2. #include "src/config.h"
  3. #include "ti/driverlib/dl_gpio.h"
  4. #include "ti/driverlib/dl_i2c.h"
  5. #include "src/battery_data/battery.h"
  6. #include "ti_msp_dl_config.h"
  7. #include <stdint.h>
  8. #include <stdio.h>
  9. #include <string.h>
  10. // Global extern variable for buffers are defined in config.h and declared in i2c_hal.c:
  11. /*
  12. 05.05.25:
  13. Working on the Dynamic Addressing for I2C communication between MCU and Pi:
  14. 1. Base Address of 0x10 is set in the configuration of the MCU as a target
  15. 2. GPIO Pins are configured from Pin PA14 to Pin PA17 for Dynamic Addressing
  16. 3. If all the Pins are connected to GND then address is the BASE ADDRESS, if not then based on pull up the GPIO pins are set HIGH or LOW
  17. 4. Initialize GPIO pins
  18. 5. Read GPIO Pins for GND or Pull Up Resistor
  19. 6. Set the offset and the base address to the Target and enable
  20. */
  21. void GPIO_Addr_Init(){
  22. //Configure GPIO Pins as input:
  23. DL_GPIO_initDigitalInput(GPIO_ADDR_PINS_ADDR_0_IOMUX);
  24. DL_GPIO_initDigitalInput(GPIO_ADDR_PINS_ADDR_1_IOMUX);
  25. DL_GPIO_initDigitalInput(GPIO_ADDR_PINS_ADDR_2_IOMUX);
  26. DL_GPIO_initDigitalInput(GPIO_ADDR_PINS_ADDR_3_IOMUX);
  27. }
  28. //Dynamic Addressing pins for MCU:
  29. void dynamic_gpio_addressing(){
  30. //Initialize the GPIO pins
  31. GPIO_Addr_Init();
  32. // using ternary operators to verify if pin is high then 1 else 0
  33. uint8_t offset= 0;
  34. offset|= ((DL_GPIO_readPins(GPIO_ADDR_PINS_PORT, GPIO_ADDR_PINS_ADDR_3_PIN) ? 1 : 0)<<3);
  35. offset|= ((DL_GPIO_readPins(GPIO_ADDR_PINS_PORT, GPIO_ADDR_PINS_ADDR_2_PIN) ? 1 : 0)<<2);
  36. offset|= ((DL_GPIO_readPins(GPIO_ADDR_PINS_PORT, GPIO_ADDR_PINS_ADDR_1_PIN) ? 1 : 0)<<1);
  37. offset|= ((DL_GPIO_readPins(GPIO_ADDR_PINS_PORT, GPIO_ADDR_PINS_ADDR_0_PIN) ? 1 : 0)<<0);
  38. // Target Address as configured in the syscfg file
  39. uint8_t i2c_dynamic_address= I2C_0_TARGET_OWN_ADDR + offset;
  40. //Configure as target with the dynamic address
  41. DL_I2C_setTargetAddress(I2C_0_INST, i2c_dynamic_address);
  42. //Enable Target Mode
  43. DL_I2C_enableTarget(I2C_0_INST);
  44. }
  45. void pi_i2c_mcu(){
  46. //uint8_t rxBuffer_getLimits[12];
  47. uint8_t receivedCommand= DL_I2C_receiveTargetData(I2C_0_INST);
  48. //printf("Received Command: 0x%02X\n", receivedCommand);
  49. if(receivedCommand == CMD_GET_BATTERY_STATUS){
  50. //Example: i2cget -y 1 0x20 0x01 i 4
  51. uint8_t status_buffer[NUM_SLOTS];
  52. //GET battery state from battery.c file
  53. for(uint8_t slot=0; slot<NUM_SLOTS; slot++){
  54. status_buffer[slot] = battery_data[slot].battery_state;
  55. }
  56. DL_I2C_fillTargetTXFIFO(I2C_0_INST, status_buffer, NUM_SLOTS);
  57. while(DL_I2C_transmitTargetDataCheck(I2C_0_INST, 0x00)!= false);
  58. }
  59. //bitmasked GET command:
  60. else if((receivedCommand & 0x0F)== 0x02){
  61. //I2Ctools command: i2cget -y 1 0x20 0xn2 i 8
  62. //Get Battery Measurement data for slot_id: Voltage, Current, Temperture, Cycle Number and Cycle State
  63. // on two of the testings have received:
  64. //0x00 0xff 0xff 0xff 0xff 0xff 0xff 0xff
  65. //0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00
  66. uint8_t requestedSlot = (receivedCommand & 0xF0)>>4;
  67. //printf("Requested slot:%d\n", requestedSlot);
  68. BatteryData battery_measure;
  69. if(requestedSlot >= NUM_SLOTS){
  70. DL_I2C_flushTargetRXFIFO(I2C_0_INST);
  71. return;
  72. }
  73. //Check for the requested slot and get the battery measurement data
  74. BatteryInfo *battery = &battery_data[requestedSlot];
  75. battery_measure.voltage= battery->battery_measurement.voltage;
  76. battery_measure.current= battery->battery_measurement.current;
  77. battery_measure.temperature= battery->battery_measurement.temperature;
  78. battery_measure.cycle_number= battery->cycle_number;
  79. battery_measure.cycle_state= battery->battery_charging_state;
  80. DL_I2C_fillTargetTXFIFO(I2C_0_INST, (uint8_t *)&battery_measure, sizeof(BatteryData));
  81. printf("Battery Measurement Sent. \n");
  82. DL_I2C_flushTargetTXFIFO(I2C_0_INST);
  83. }
  84. else if((receivedCommand & 0x0F)== 0x03){
  85. //slot_id is another element
  86. //Example i2ctools: i2cset -y 1 0x20 0x03 0x2C 0x01 0x68 0x10 0xFA 0xD0 0x07 0x19 0x00 i
  87. /*
  88. * min_voltage: 300 (2C 01)
  89. * max_voltage: 4200 (68 10)
  90. * cut off current: 250 (FA)
  91. * Capacitance: 2000 (D0 07)
  92. * charge fraction: 25 (19)
  93. */
  94. uint8_t rx_index= 0;
  95. uint8_t requestedSlot= (receivedCommand & 0xF0)>>4;
  96. while(rx_index < (sizeof(BatteryLimitMsg))){
  97. if(!DL_I2C_isTargetRXFIFOEmpty(I2C_0_INST)){
  98. rxPacket.rxBuffer[rx_index]= DL_I2C_receiveTargetData(I2C_0_INST);
  99. rx_index++;
  100. }
  101. }
  102. //Check if all the data is received then store the battery limits in BatteryInfo struct:
  103. if(rx_index== (sizeof(BatteryLimitMsg))){
  104. BatteryLimitMsg battery_limit_received;
  105. memcpy(&battery_limit_received, (const uint8_t*)rxPacket.rxBuffer, sizeof(BatteryLimitMsg));
  106. if(requestedSlot < NUM_SLOTS){
  107. BatteryInfo *battery = &battery_data[requestedSlot];
  108. battery->min_voltage = battery_limit_received.min_voltage;
  109. battery->max_voltage = battery_limit_received.max_voltage;
  110. battery->cut_off_current = battery_limit_received.cut_off_current;
  111. battery->capacitance = battery_limit_received.capacitance;
  112. battery->charge_fraction = battery_limit_received.charge_fraction;
  113. // this is when WWDT resets the MCU during the charging cycle
  114. battery->cycle_number = battery_limit_received.previous_cycle_number;
  115. battery->batteryLimitReceived= true;
  116. }
  117. }
  118. rx_index= 0;
  119. DL_I2C_flushTargetRXFIFO(I2C_0_INST);
  120. }
  121. }