battery.h 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #include <stdint.h>
  2. #include <stdbool.h>
  3. #include "src/config.h"
  4. #ifndef BATTERY_H
  5. #define BATTERY_H
  6. #define INITIAL_PWM_VALUE (0)
  7. #define PWM_INCREMENT_VALUE (1)
  8. #define PWM_DECREMENT_VALUE (1)
  9. #define PWM_MAX_VALUE (1000)
  10. #define NUM_SLOTS (1)
  11. //Variables to read all the battery measurements from all the slots
  12. extern volatile bool scanInProgress;
  13. //Battery states read by Pi
  14. typedef enum{
  15. STATE_EMPTY= 0x01,
  16. STATE_BATTERY_DETECTED= 0x02,
  17. STATE_WAITING_FOR_LIMITS= 0x03,
  18. STATE_MEASUREMENT_IN_PROGRESS= 0x04,
  19. STATE_MEASUREMENT_DONE= 0x04,
  20. STATE_OVERHEATING= 0x05,
  21. } BatteryState;
  22. //Battery Discharge Safety Check: when defined with type uint8_t takes only 1 byte otherwise takes 4 byte
  23. typedef enum: uint8_t{
  24. SLOT_STATE_OK= 0x00,
  25. SLOT_STATE_SOV= 0x01,
  26. SLOT_ERR_HOV= (0x02 | 0x80),
  27. SLOT_ERR_OVERTEMPERATURE= (0x03 | 0x80), // first bit indicates complete error
  28. // Control error states
  29. SLOT_WARN_LOWER_DAC_NOT_POSSIBLE=0x10,
  30. SLOT_WARN_HIGHER_DAC_NOT_POSSIBLE=0x11,
  31. SLOT_WARN_DAC_INVALID_VALUE=0x12,
  32. SLOT_WARN_LOWER_PWM_NOT_POSSIBLE=0x13,
  33. SLOT_WARN_HIGHER_PWM_NOT_POSSIBLE=0x14,
  34. // I2C Slave Error states
  35. SLOT_ERR_NO_DAC = (0x20 | 0x80),
  36. SLOT_ERR_NO_ADC1 = (0x21 | 0x80),
  37. SLOT_ERR_NO_ADC2 = (0x22 | 0x80),
  38. SLOT_ERR_NO_ADC3 = (0x23 | 0x80),
  39. SLOT_ERR_NO_ADC4 = (0x24 | 0x80),
  40. SLOT_ERR_CONFIGBYTE = (0x25 | 0x80),
  41. SLOT_ERR_DAC_WRITE_FAILED= (0x26 | 0x80)
  42. } SlotState;
  43. typedef struct{
  44. uint16_t voltage;
  45. int16_t current;
  46. uint16_t temperature;
  47. SlotState slot_state;
  48. }BatteryMeasurement;
  49. typedef enum __attribute__((__packed__)){
  50. STATE_IDLE,
  51. STATE_PRE_CHARGE,
  52. STATE_CC_CHARGING,
  53. STATE_CV_CHARGING,
  54. STATE_DISCHARGING,
  55. STATE_FINAL_DISCHARGE,
  56. STATE_ERROR,
  57. }BatteryChargingState;
  58. typedef struct{
  59. uint8_t slot_id;
  60. uint16_t pwm_value; //for Power Burning PWM
  61. int16_t charge_discharge;
  62. BatteryState battery_state;
  63. BatteryMeasurement battery_measurement;
  64. uint16_t min_voltage;
  65. uint16_t max_voltage;
  66. uint8_t cut_off_current;
  67. uint16_t capacitance;
  68. uint8_t charge_fraction;
  69. uint8_t cycle_number;
  70. BatteryChargingState battery_charging_state;
  71. bool batteryLimitReceived;
  72. } BatteryInfo;
  73. extern BatteryInfo battery_data[NUM_SLOTS];
  74. void Battery_Init();
  75. void Battery_StateCondition(uint8_t slot_id);
  76. #endif