i2c_target.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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_ReadState(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 Battery_StateUpdate(){
  47. piTxCount= 0;
  48. piTxLen = NUM_SLOTS;
  49. for(uint8_t slot=0; slot< piTxLen; slot++){
  50. Battery_ReadState(slot);
  51. piTxPacket[slot]= batteries[slot].state;
  52. }
  53. }
  54. /*
  55. {
  56. if(receivedCommand == CMD_SET_BATTERY_LIMIT){
  57. BatteryLimitMsg battery_limits;
  58. //requestedSlot= DL_I2C_receiveTargetData(I2C_target_INST);
  59. for(uint8_t slot_id=0; slot_id< NUM_SLOTS; slot++){
  60. }
  61. if(requestedSlot < NUM_SLOTS){
  62. batteries[requestedSlot].min_voltage= (DL_I2C_receiveTargetData(I2C_target_INST)<< 8)|DL_I2C_receiveTargetData(I2C_target_INST);
  63. batteries[requestedSlot].max_voltage= (DL_I2C_receiveTargetData(I2C_target_INST)<< 8)|DL_I2C_receiveTargetData(I2C_target_INST);
  64. batteries[requestedSlot].cut_off_current= (DL_I2C_receiveTargetData(I2C_target_INST)<< 8)|DL_I2C_receiveTargetData(I2C_target_INST);
  65. batteries[requestedSlot].capacitance= (DL_I2C_receiveTargetData(I2C_target_INST)<< 8)|DL_I2C_receiveTargetData(I2C_target_INST);
  66. }
  67. }
  68. }
  69. break;
  70. Battery_StateUpdate();
  71. uint8_t battery_slots_status[NUM_SLOTS];
  72. for(uint8_t slot= 0; slot< NUM_SLOTS; slot++){
  73. battery_slots_status[slot]= batteries[slot].state;
  74. }
  75. while (DL_I2C_getTargetStatus(I2C_target_INST) & DL_I2C_TARGET_STATUS_BUS_BUSY);
  76. DL_I2C_fillTargetTXFIFO(I2C_target_INST, battery_slots_status, NUM_SLOTS);
  77. while (DL_I2C_getTargetStatus(I2C_target_INST) & DL_I2C_TARGET_STATUS_BUS_BUSY);
  78. */