cc_cv_charging.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. #include "cc_cv_charging.h"
  2. #include "battery.h"
  3. #include "dac.h"
  4. #include "adc.h"
  5. #include "ti/devices/msp/m0p/mspm0g350x.h"
  6. #include "ti/driverlib/dl_gpio.h"
  7. #include <stdio.h>
  8. #define MAX_VOLTAGE_MV (4200)
  9. #define MIN_VOLTAGE_MV (3000)
  10. #define CC_CURRENT_LIMIT_MA (500)
  11. #define CUTOFF_CURRENT_MA (50)
  12. #define MAX_CYCLES (2)
  13. //********** Not used **************************
  14. // if the battery is deeply discharged then 50mA of trickle charge is given for a set timer
  15. //#define TRICKLE_CHARGE_CURRENT_MA (50)
  16. //#define TRICKLE_CHARGE_VOLTAGE_MV (2100)
  17. //#define TRICKLE_CHARGE_TIMEOUT_MS (5000) //5 mseconds
  18. //Pre Charge
  19. #define BATTERY_CAPACITY_MAH (2000)
  20. #define PRE_CHARGE_CURRENT_MA (BATTERY_CAPACITY_MAH/ 10)
  21. static ChargingState charging_state= STATE_PRE_CHARGE;
  22. static uint16_t cycle_count= 0;
  23. static uint32_t pre_charge_start_time= 0;
  24. //batt_voltage and current as stored from ADC
  25. void CC_CV_UpdateChargingState(uint8_t slot_id){
  26. uint16_t batt_voltage= batteries[slot_id].voltage;
  27. int16_t batt_current= batteries[slot_id].current;
  28. if(batt_voltage < MIN_VOLTAGE_MV){
  29. charging_state= STATE_PRE_CHARGE;
  30. }
  31. else if(batt_voltage >= MIN_VOLTAGE_MV && batt_voltage < MAX_VOLTAGE_MV - BATTERY_THRESHOLD){
  32. charging_state= STATE_CC_CHARGING;
  33. }
  34. else if(batt_voltage >= MAX_VOLTAGE_MV - BATTERY_THRESHOLD){
  35. charging_state= STATE_CV_CHARGING;
  36. }
  37. else if(charging_state == STATE_CV_CHARGING && batt_current <= CUTOFF_CURRENT_MA + BATTERY_THRESHOLD){
  38. if(cycle_count < MAX_CYCLES){
  39. charging_state= STATE_DISCHARGING;
  40. }
  41. else{
  42. charging_state = STATE_FINAL_DISCHARGE;
  43. }
  44. }
  45. else if(charging_state == STATE_DISCHARGING && batt_voltage <= MIN_VOLTAGE_MV + BATTERY_THRESHOLD) {
  46. charging_state= STATE_CC_CHARGING;
  47. }
  48. else{
  49. charging_state= STATE_ERROR;
  50. }
  51. }
  52. /*
  53. Function for Battery Charging and Discharging:
  54. Trickle Charge: Only used when the battery voltage < ~2.1 V.
  55. - In this state, the batteries internal protection may have been previously disconnected.
  56. Pre Charge: Once the battery is in discharged state, pre-charging begins.
  57. - Starts charging safely with a typically low current C/10
  58. Constant Current: Continues until the battery voltage has reached the "full" or floating voltage level
  59. Constant Voltage: CV volatge starts once the maximum voltage threshold is obtained
  60. */
  61. void CC_CV_ControlCharging(uint8_t slot_id){
  62. //Calling function to get all the conditional states
  63. CC_CV_UpdateChargingState(slot_id);
  64. uint16_t batt_voltage= batteries[slot_id].voltage;
  65. int16_t batt_current= batteries[slot_id].current;
  66. switch(charging_state){
  67. case STATE_PRE_CHARGE:
  68. DL_GPIO_setPins(GPIO_Battery_Charging_PORT, GPIO_Battery_Charging_PIN_PB4_PIN);
  69. DAC_fastWrite(CHANNEL_A_VALUE);
  70. printf("[CC-CV] Pre Charging: Slot %d at %d mA.\n", slot_id, PRE_CHARGE_CURRENT_MA);
  71. break;
  72. case STATE_CC_CHARGING:
  73. DL_GPIO_setPins(GPIO_Battery_Charging_PORT, GPIO_Battery_Charging_PIN_PB4_PIN);
  74. DAC_fastWrite(CHANNEL_A_VALUE);
  75. printf("[CC-CV] CC Charging: Slot %d at %d mA.\n", slot_id, CC_CURRENT_LIMIT_MA);
  76. break;
  77. case STATE_CV_CHARGING:
  78. DL_GPIO_setPins(GPIO_Battery_Charging_PORT, GPIO_Battery_Charging_PIN_PB4_PIN);
  79. DAC_fastWrite(CHANNEL_A_VALUE);
  80. printf("[CC-CV] CV Charging: Slot %d at %d mA.\n", slot_id, CUTOFF_CURRENT_MA);
  81. break;
  82. case STATE_DISCHARGING:
  83. DL_GPIO_clearPins(GPIO_Battery_Charging_PORT, GPIO_Battery_Charging_PIN_PB4_PIN);
  84. DL_GPIO_setPins(GPIO_Battery_Discharging_PORT, GPIO_Battery_Discharging_PIN_PB12_PIN);
  85. DAC_fastWrite(CHANNEL_A_VALUE);
  86. printf("[CC-CV] Discharging: Slot %d at %d mA.\n", slot_id, batt_voltage);
  87. break;
  88. case STATE_FINAL_DISCHARGE:
  89. DL_GPIO_clearPins(GPIO_Battery_Charging_PORT, GPIO_Battery_Charging_PIN_PB4_PIN);
  90. DL_GPIO_clearPins(GPIO_Battery_Discharging_PORT, GPIO_Battery_Discharging_PIN_PB12_PIN);
  91. printf("[CC-CV] Final Discharge: Slot %d..\n", slot_id);
  92. DAC_fastWrite(0);
  93. case STATE_ERROR:
  94. DL_GPIO_clearPins(GPIO_Battery_Charging_PORT, GPIO_Battery_Charging_PIN_PB4_PIN);
  95. DL_GPIO_clearPins(GPIO_Battery_Discharging_PORT, GPIO_Battery_Discharging_PIN_PB12_PIN);
  96. printf("[CC-CV] Error: Slot %d.\n", slot_id);
  97. default:
  98. break;
  99. }
  100. }