battery.h 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #ifndef BATTERY_H
  2. #define BATTERY_H
  3. #include <stdint.h>
  4. #include <stdbool.h>
  5. #include <stdio.h>
  6. #include "ti/driverlib/dl_timerg.h"
  7. //define macro to be used by multiple files in the program witout the variable being overwritten
  8. //for testing
  9. #define NUM_SLOTS (1)
  10. //Battery Tolerance
  11. // how much tolerance do we allow if we charge / discharge before we
  12. // trigger the control loop to adjust the value
  13. #define BATTERY_CURRENT_THRESHOLD (5)
  14. // for i2c communication to know battery health
  15. #define SOV_THRESHOLD_MV (6000)
  16. #define HOV_THRESHOLD_MV (8000)
  17. #define TEMPERATURE_MAX_C (60)
  18. //Battery Discharge Safety Check
  19. typedef enum{
  20. SLOT_STATE_OK= 0x00,
  21. SLOT_STATE_SOV= 0x01,
  22. SLOT_STATE_HOV= 0x02,
  23. SLOT_WARN_OVERTEMPERATURE = 0x03,
  24. SLOT_ERR_OVERTEMPERATURE= (0x04 | 0x80), // first bit indicates complete error
  25. // Control error states
  26. SLOT_WARN_LOWER_DAC_NOT_POSSIBLE=0x10,
  27. SLOT_WARN_HIGHER_DAC_NOT_POSSIBLE=0x11,
  28. SLOT_WARN_DAC_INVALID_VALUE=0x12,
  29. SLOT_WARN_LOWER_PWM_NOT_POSSIBLE=0x13,
  30. SLOT_WARN_HIGHER_PWM_NOT_POSSIBLE=0x14,
  31. // I2C Slave Error states
  32. SLOT_ERR_NO_DAC = (0x20 | 0x80),
  33. SLOT_ERR_NO_ADC1 = (0x21 | 0x80),
  34. SLOT_ERR_NO_ADC2 = (0x22 | 0x80),
  35. SLOT_ERR_NO_ADC3 = (0x23 | 0x80),
  36. SLOT_ERR_NO_ADC4 = (0x24 | 0x80),
  37. SLOT_ERR_CONFIGBYTE = (0x25 | 0x80),
  38. SLOT_ERR_DAC_WRITE_FAILED= (0x26 | 0x80)
  39. } SlotState;
  40. typedef struct{
  41. uint16_t voltage;
  42. int16_t current;
  43. uint16_t temperature;
  44. SlotState state;
  45. } BatteryMeasurement;
  46. typedef struct {
  47. // for future extension of error states (overtemp etc): add here
  48. int16_t set_current;
  49. BatteryMeasurement measurement;
  50. uint16_t dac_value;
  51. uint16_t pwm_value;
  52. GPTIMER_Regs *timer;
  53. SlotState *state;
  54. } BatterySlot;
  55. //global battery array declaration: extending visiblity of the variable to multiple source files: variable declaration
  56. extern BatterySlot battery_slots[NUM_SLOTS];
  57. typedef struct {
  58. void (*init)();
  59. void (*read_state)(uint8_t slot_id);
  60. void (*adjust_current)(uint8_t slot_id);
  61. void (*disable)(uint8_t slot_id);
  62. } BatterySlotManager;
  63. extern BatterySlotManager battery_slotmgr;
  64. #endif