cc_cv_charging.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. #include "cc_cv_charging.h"
  2. #include "battery.h"
  3. #include "cc_cc_charging.h"
  4. #include "dac.h"
  5. #include <cstdint>
  6. #include <stdio.h>
  7. #define MAX_VOLTAGE_MV (4200)
  8. #define MIN_VOLTAGE_MV (3000)
  9. #define CC_CURRENT_LIMIT_MA (500)
  10. #define CUTOFF_CURRENT_MA (50)
  11. #define MAX_CYCLES (500)
  12. // if the battery is deeply discharged then 50mA of trickle charge is given for a set timer
  13. #define TRICKLE_CHARGE_CURRENT_MA (50)
  14. #define TRICKLE_CHARGE_VOLTAGE_MV (2100)
  15. #define TRICKLE_CHARGE_TIMEOUT_MS (5000) //5 mseconds
  16. //Pre Charge
  17. #define BATTERY_CAPACITY_MAH (2000)
  18. #define PRE_CHARGE_CURRENT_MA (BATTERY_CAPACITY_MAH/ 10)
  19. static ChargingState charging_state= STATE_PRE_CHARGE;
  20. static uint16_t cycle_count= 0;
  21. static uint32_t pre_charge_start_time= 0;
  22. void CC_CV_UpdateChargingState(uint8_t slot_id){
  23. uint16_t batt_voltage= batteries[slot_id].voltage;
  24. int16_t batt_current= batteries[slot_id].current;
  25. static uint16_t trickle_timeer= 0;
  26. if(batt_voltage < TRICKLE_CHARGE_VOLTAGE_MV){
  27. charging_state= STATE_TRICKLE_CHARGE;
  28. trickle_timer= 0;
  29. }
  30. else if(charging_state== STATE_TRICKLE_CHARGE){
  31. trickle_timer ++;
  32. if(trickle_timer >= TRICKLE_CHARGE_TIMEOUT_MS){
  33. charging_state = STATE_ERROR;
  34. }
  35. }
  36. else if(batt_voltage < MIN_VOLTAGE_MV){
  37. charging_state= STATE_PRE_CHARGE;
  38. }
  39. else if(batt_voltage >= MIN_VOLTAGE_MV && batt_voltage < MAX_VOLTAGE_MV - BATTERY_THRESHOLD){
  40. charging_state= STATE_CC_CHARGING;
  41. }
  42. else if(batt_voltage >= MAX_VOLTAGE_MV - BATTERY_THRESHOLD){
  43. charging_state= STATE_CV_CHARGING;
  44. }
  45. else if(charging_state == STATE_CV_CHARGING && batt_current <= CUTOFF_CURRENT_MA + BATTERY_THRESHOLD){
  46. if(cycle_count < MAX_CYCLES){
  47. charging_state= STATE_DISCHARGING;
  48. }
  49. else{
  50. charging_state = STATE_FINAL_DISCHARGE;
  51. }
  52. }
  53. else if(charging_state == STATE_DISCHARGING && batt_voltage <= MIN_VOLTAGE_MV + BATTERY_THRESHOLD) {
  54. charging_state= STATE_CC_CHARGING;
  55. }
  56. else{
  57. charging_state= STATE_ERROR;
  58. }
  59. }
  60. void CC_CV_ControlCharging(uint8 slot_id){
  61. CC_CV_UpdateChargingState(slot_id){
  62. switch(charging_state){
  63. case STATE_TRICKLE_CHARGE:
  64. DAC_fastWrite(TRICKLE_CHARGE_CURRENT_MA * 1000);
  65. printf("[CC-CV] Trickle Charging: Slot %d at %d mA.\n", slot_id, TRICKLE_CHARGE_CURRENT_MA);
  66. break;
  67. case STATE_PRE_CHARGE:
  68. DAC_fastWrite(PRE_CHARGE_CURRENT_MA * 1000);
  69. printf("[CC-CV] Pre Charging: Slot %d at %d mA.\n", slot_id, PRE_CHARGE_CURRENT_MA);
  70. break;
  71. case STATE_CC_CHARGING:
  72. DAC_fastWrite(CC_CURRENT_LIMIT_MA * 1000):
  73. printf("[CC-CV] CC Charging: Slot %d at %d mA.\n", slot_id, CC_CURRENT_LIMIT_MA);
  74. break;
  75. case STATE_CV_CHARGING:
  76. DAC_fastWrite(MAX_VOLTAGE_MV);
  77. printf("[CC-CV] CV Charging: Slot %d at %d mA.\n", slot_id, PRE_CHARGE_CURRENT_MA);
  78. break;
  79. default:
  80. break;
  81. }
  82. }
  83. }