i2c_target.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #include "i2c_target.h"
  2. #include "battery.h"
  3. #include "adc.h"
  4. #include "ti/driverlib/dl_i2c.h"
  5. #include "ti_msp_dl_config.h"
  6. #include <stdio.h>
  7. #include <stdint.h>
  8. //Global buffer for I2C
  9. BatteryData battery_data;
  10. BatteryLimitMsg battery_limits;
  11. //Flag for Pi INTERRUPTS:
  12. volatile bool piRxComplete;
  13. volatile bool piTxComplete;
  14. uint8_t piTxPacket[I2C_TX_MAX_PACKET_SIZE_PI];
  15. uint8_t piRxPacket[I2C_RX_MAX_PACKET_SIZE_PI];
  16. uint32_t piTxLen, piTxCount;
  17. uint32_t piRxLen, piRxCount;
  18. /*
  19. - command: as defined in the Wiki for Pi 0x01, 0x02, 0x03, refer to i2c_target.h file
  20. - slot_id: battery slot numner from 0 to NUM_SLOTS-1
  21. - data: pointer to SET battery limits
  22. - len: length of the data
  23. */
  24. void Battery_StateUpdate(uint8_t slot_id){
  25. uint16_t voltage_mv= batteries[slot_id].voltage;
  26. uint16_t min_voltage= batteries[slot_id].min_voltage;
  27. uint16_t max_voltage= batteries[slot_id].max_voltage;
  28. //Testing:
  29. if(voltage_mv< 500){
  30. batteries[slot_id].state= STATE_EMPTY;
  31. }
  32. else if(voltage_mv>=500 && voltage_mv< 3000){
  33. batteries[slot_id].state= STATE_BATTERY_DETECTED;
  34. }
  35. else if(voltage_mv >=3000 && voltage_mv< 4200){
  36. batteries[slot_id].state= STATE_MEASUREMENT_IN_PROGRESS;
  37. }
  38. // once MCU is done reading ADC:
  39. else if(!(DL_I2C_getControllerStatus(I2C_controller_INST) & DL_I2C_CONTROLLER_STATUS_BUSY_BUS)) {
  40. batteries[slot_id].state= STATE_MEASUREMENT_DONE;
  41. }
  42. else{
  43. batteries[slot_id].state= STATE_OVERCHARGING;
  44. }
  45. }
  46. void ProcessI2CCommand(uint8_t command, uint8_t slot_id, ADC_PARAMS params) {
  47. switch(command) {
  48. case CMD_GET_BATTERY_STATUS:
  49. for(uint8_t slot=0; slot< NUM_SLOTS; slot++){
  50. Battery_StateUpdate(slot);
  51. DL_I2C_enableInterrupt(I2C_target_INST, DL_I2C_INTERRUPT_TARGET_TXFIFO_TRIGGER);
  52. while (DL_I2C_getTargetStatus(I2C_target_INST) & DL_I2C_TARGET_STATUS_BUS_BUSY);
  53. }
  54. break;
  55. case CMD_GET_BATTERY_DATA:
  56. piTxCount= 0;
  57. piTxComplete= false;
  58. // Create a data message and transmit it
  59. Battery_UpdateCurrentVoltage(params);
  60. battery_data.slot_id= slot_id;
  61. battery_data.voltage= batteries[slot_id].voltage;
  62. battery_data.current= 0;
  63. battery_data.temperature=0;
  64. printf("Sending Battery Data: Slot %d | Voltage %dmV| Current %d| Temperature %d.\n", slot_id, battery_data.voltage, battery_data.current, battery_data.temperature);
  65. while (DL_I2C_getTargetStatus(I2C_target_INST) & DL_I2C_TARGET_STATUS_BUS_BUSY);
  66. //Casting the struct pointer to byte array: (uint8_t*)
  67. DL_I2C_enableInterrupt(I2C_target_INST, DL_I2C_INTERRUPT_TARGET_TXFIFO_TRIGGER);
  68. DL_I2C_fillTargetTXFIFO(I2C_target_INST, (uint8_t*)&battery_data, sizeof(BatteryData));
  69. while(!piTxComplete);
  70. while (DL_I2C_getTargetStatus(I2C_target_INST) & DL_I2C_TARGET_STATUS_BUS_BUSY);
  71. break;
  72. case CMD_SET_BATTERY_LIMIT:
  73. batteries[slot_id].min_voltage= battery_limits.min_voltage;
  74. batteries[slot_id].max_voltage= battery_limits.max_voltage;
  75. batteries[slot_id].capacitance= battery_limits.capacitance;
  76. batteries[slot_id].cut_off_current= battery_limits.cut_off_current;
  77. break;
  78. }
  79. }