i2c_target.c 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. // for testing:
  25. //Battery batteries[NUM_SLOTS] = {
  26. // {0, STATE_BATTERY_DETECTED, 3700, 500, 25, 3000, 4200, 2000, 10000, 80}
  27. //};
  28. void Battery_ReadState(uint8_t slot_id){
  29. uint16_t voltage_mv= batteries[slot_id].voltage;
  30. uint16_t min_voltage= batteries[slot_id].min_voltage;
  31. uint16_t max_voltage= batteries[slot_id].max_voltage;
  32. //Testing:
  33. if(voltage_mv< 500){
  34. batteries[slot_id].state= STATE_EMPTY;
  35. }
  36. else if(voltage_mv>=500 && voltage_mv< 3000){
  37. batteries[slot_id].state= STATE_BATTERY_DETECTED;
  38. }
  39. else if(voltage_mv >=3000 && voltage_mv< 4200){
  40. batteries[slot_id].state= STATE_MEASUREMENT_IN_PROGRESS;
  41. }
  42. // once MCU is done reading ADC:
  43. else if(!(DL_I2C_getControllerStatus(I2C_controller_INST) & DL_I2C_CONTROLLER_STATUS_BUSY_BUS)) {
  44. batteries[slot_id].state= STATE_MEASUREMENT_DONE;
  45. }
  46. else{
  47. batteries[slot_id].state= STATE_OVERCHARGING;
  48. }
  49. }