cc_cv_charging.c 3.0 KB

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