shark_charge.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #include "shark_charge.h"
  2. #include "app_rs485_1.h"
  3. #include "shark_xl.h"
  4. #include "app.h"
  5. shark_bool shark_charger_detected;
  6. shark_bool shark_charger_enabled;
  7. shark_u16 shark_charge_times;
  8. shark_bool shark_battery_full;
  9. static shark_u16 shark_charger_remove;
  10. static shark_u16 shark_charger_insert;
  11. void shark_charge_init(void)
  12. {
  13. rcu_periph_clock_enable(GPIO_RCU_CHG_DET);
  14. gpio_init(GPIO_PORT_CHG_DET, GPIO_MODE_IN_FLOATING, GPIO_OSPEED_50MHZ, GPIO_PIN_CHG_DET);
  15. rcu_periph_clock_enable(GPIO_RCU_CHG_EN);
  16. gpio_bit_reset(GPIO_PORT_CHG_EN, GPIO_PIN_CHG_EN);
  17. gpio_init(GPIO_PORT_CHG_EN, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_CHG_EN);
  18. }
  19. void shark_charger_set_enable(shark_bool enable)
  20. {
  21. if (sub_bms_info_1.packet_common.m_percent > 99 && sub_bms_info_2.packet_common.m_percent > 99) {
  22. shark_battery_full = shark_true;
  23. }
  24. if (sub_bms_info_1.packet_common.m_percent < 97 || sub_bms_info_2.packet_common.m_percent < 97) {
  25. shark_battery_full = shark_false;
  26. }
  27. if (shark_battery_full || cb_operate_state == CB_BAT1_BAT2_SERIES || shark_charger_remove) {
  28. enable = shark_false;
  29. }
  30. if (shark_charger_enabled != enable) {
  31. shark_charger_enabled = enable;
  32. shark_charge_times = 0;
  33. if (enable) {
  34. gpio_bit_set(GPIO_PORT_CHG_EN, GPIO_PIN_CHG_EN);
  35. println("charger enabled");
  36. } else {
  37. gpio_bit_reset(GPIO_PORT_CHG_EN, GPIO_PIN_CHG_EN);
  38. shark_charger_remove = SHARK_CHG_DEBOUNCE;
  39. println("charger disabled");
  40. }
  41. }
  42. }
  43. void shark_charger_disable(u16 times)
  44. {
  45. if (shark_charge_times > times) {
  46. shark_charger_set_enable(shark_false);
  47. }
  48. }
  49. void shark_charge_tick(void)
  50. {
  51. if (gpio_input_bit_get(GPIO_PORT_CHG_DET, GPIO_PIN_CHG_DET) == RESET) {
  52. shark_charger_remove = 0;
  53. if (shark_charger_insert < 50) {
  54. shark_charger_insert++;
  55. } else if (shark_xl_check() == shark_false) {
  56. shark_charger_detected = shark_true;
  57. if (shark_charger_enabled && shark_charge_times < SHARK_CHG_TIME_MAX) {
  58. shark_charge_times++;
  59. }
  60. }
  61. } else {
  62. shark_charger_insert = 0;
  63. if (shark_charger_remove < 2000) {
  64. shark_charger_remove++;
  65. } else {
  66. shark_charger_detected = shark_false;
  67. }
  68. }
  69. }
  70. void shark_charge_poll(void)
  71. {
  72. if (shark_charger_detected) {
  73. if (shark_charger_enabled) {
  74. switch (cb_operate_state) {
  75. case CB_BAT1:
  76. if (sub_bms_info_1.packet_common.charge_flag == 0) {
  77. shark_charger_disable(SHARK_CHG_TIME_MIN);
  78. }
  79. break;
  80. case CB_BAT2:
  81. if (sub_bms_info_2.packet_common.charge_flag == 0) {
  82. shark_charger_disable(SHARK_CHG_TIME_MIN);
  83. }
  84. break;
  85. case CB_BAT1_BAT2_PARRALLEL:
  86. if (sub_bms_info_1.packet_common.charge_flag == 0) {
  87. if (sub_bms_info_2.packet_common.charge_flag == 0) {
  88. shark_charger_disable(SHARK_CHG_TIME_MIN);
  89. }
  90. if (shark_battery_get_current12() < 0) {
  91. shark_charger_disable(SHARK_CHG_TIME_MIN);
  92. }
  93. } else if (sub_bms_info_2.packet_common.charge_flag == 0 && shark_battery_get_current12() < 0) {
  94. shark_charger_disable(SHARK_CHG_TIME_MIN);
  95. }
  96. break;
  97. }
  98. } else {
  99. shark_charger_set_enable(shark_true);
  100. }
  101. }
  102. }