battery.h 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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,
  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_ADC = 0x20,
  33. SLOT_ERR_NO_DAC = 0x21,
  34. SLOT_ERR_CONFIGBYTE = 0x22,
  35. SLOT_ERR_DAC_WRITE_FAILED=0x23,
  36. } SlotState;
  37. typedef struct{
  38. uint16_t voltage;
  39. int16_t current;
  40. uint16_t temperature;
  41. SlotState state;
  42. } BatteryMeasurement;
  43. typedef struct {
  44. // for future extension of error states (overtemp etc): add here
  45. int16_t set_current;
  46. BatteryMeasurement measurement;
  47. uint16_t dac_value;
  48. uint16_t pwm_value;
  49. GPTIMER_Regs *timer;
  50. SlotState *state;
  51. } BatterySlot;
  52. //global battery array declaration: extending visiblity of the variable to multiple source files: variable declaration
  53. extern BatterySlot battery_slots[NUM_SLOTS];
  54. typedef struct {
  55. void (*init)();
  56. void (*read_state)(uint8_t slot_id);
  57. void (*adjust_current)(uint8_t slot_id);
  58. } BatterySlotManager;
  59. extern BatterySlotManager battery_slotmgr;
  60. #endif