i2c_pi_target.c 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. #include "i2c_pi_target.h"
  2. #include "src/config.h"
  3. #include "ti/driverlib/dl_i2c.h"
  4. #include "src/battery_data/battery.h"
  5. #include "ti_msp_dl_config.h"
  6. #include <stdint.h>
  7. #include <stdio.h>
  8. #include <string.h>
  9. // Global extern variable for buffers are defined in config.h and declared in i2c_hal.c:
  10. void pi_i2c_mcu(){
  11. uint8_t receivedCommand= DL_I2C_receiveTargetData(I2C_0_INST);
  12. if(receivedCommand == CMD_GET_BATTERY_STATUS){
  13. uint8_t status_buffer[NUM_SLOTS];
  14. //GET battery state from battery.c file
  15. for(uint8_t slot=0; slot<NUM_SLOTS; slot++){
  16. status_buffer[slot] = battery_data[slot].battery_state;
  17. }
  18. DL_I2C_fillTargetTXFIFO(I2C_0_INST, status_buffer, NUM_SLOTS);
  19. while(DL_I2C_transmitTargetDataCheck(I2C_0_INST, 0x00)!= false);
  20. }
  21. //bitmasked GET command:
  22. else if((receivedCommand & 0xF0)== 0x20){
  23. //Get Battery Measurement data: Voltage, Current, Tempertaure
  24. uint8_t requestedSlot = receivedCommand & 0x0F;
  25. if(requestedSlot >= NUM_SLOTS){
  26. DL_I2C_flushTargetRXFIFO(I2C_0_INST);
  27. return;
  28. }
  29. //Check for the requested slot and get the battery measurement data
  30. BatteryMeasurement *measurement = &battery_data[requestedSlot].battery_measurement;
  31. //memcpy(tx_packet.txBuffer, &battery->battery_measurement, sizeof(BatteryMeasurement));
  32. DL_I2C_fillTargetTXFIFO(I2C_0_INST, (uint8_t *)measurement, sizeof(BatteryMeasurement));
  33. DL_I2C_flushTargetTXFIFO(I2C_0_INST);
  34. }
  35. else if(receivedCommand== CMD_SET_BATTERY_LIMIT){
  36. uint8_t rx_index= 0;
  37. while(rx_index < sizeof(BatteryLimits)){
  38. if(!DL_I2C_isTargetRXFIFOEmpty(I2C_0_INST)){
  39. rx_packet.rxBuffer[rx_index] = DL_I2C_receiveTargetData(I2C_0_INST);
  40. rx_index++;
  41. }
  42. }
  43. //Check if all the data is received then store the battery limits in BatteryInfo struct:
  44. if(rx_index== (sizeof(BatteryLimits)+1)){
  45. BatteryInfo *battery = &battery_data[rx_packet.rxBuffer[0]];
  46. uint8_t slot_id= rx_packet.rxBuffer[0];
  47. printf("Battery slot: %u\n", slot_id);
  48. if(slot_id <= NUM_SLOTS){
  49. //2bytes of max and min voltage: uint16_t
  50. /*battery->battery_limits.min_voltage = rx_packet.rxBuffer[1] | (rx_packet.rxBuffer[2] << 8);
  51. battery->battery_limits.max_voltage = rx_packet.rxBuffer[3]|(rx_packet.rxBuffer[4] << 8);
  52. //1 byte of cut off current: uint8_t
  53. battery->battery_limits.cut_off_current = rx_packet.rxBuffer[5];
  54. //2 bytes of capacitance:uint16_t
  55. battery->battery_limits.capacitance = rx_packet.rxBuffer[6] | (rx_packet.rxBuffer[7] << 8);
  56. //1 byte of charge fraction: uint8_t
  57. battery->battery_limits.charge_fraction = rx_packet.rxBuffer[8];
  58. //Set the battery state to "STATE_BATTERY_DETECTED"
  59. battery->batteryLimitRecieved = true;*/
  60. memcpy(&battery_data[slot_id].battery_limits, &rx_packet.rxBuffer[1], sizeof(BatteryLimits));
  61. battery_data[slot_id].batteryLimitRecieved= true;
  62. }
  63. }
  64. rx_index= 0;
  65. DL_I2C_flushTargetRXFIFO(I2C_0_INST);
  66. }
  67. }