shark_charge.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. #include "shark_charge.h"
  2. #include "app_rs485_1.h"
  3. #include "shark_xl.h"
  4. #include "app.h"
  5. shark_charger_state_t shark_charger_state;
  6. shark_bool shark_charger_enabled;
  7. shark_bool shark_charger_valid;
  8. shark_bool shark_battery_full;
  9. shark_u32 shark_charge_time;
  10. shark_u16 shark_charge_skip;
  11. static shark_u16 shark_charger_remove;
  12. static shark_u16 shark_charger_insert;
  13. void shark_charge_init(void)
  14. {
  15. rcu_periph_clock_enable(GPIO_RCU_CHG_DET);
  16. gpio_init(GPIO_PORT_CHG_DET, GPIO_MODE_IN_FLOATING, GPIO_OSPEED_50MHZ, GPIO_PIN_CHG_DET);
  17. rcu_periph_clock_enable(GPIO_RCU_CHG_EN);
  18. gpio_bit_reset(GPIO_PORT_CHG_EN, GPIO_PIN_CHG_EN);
  19. gpio_init(GPIO_PORT_CHG_EN, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_CHG_EN);
  20. }
  21. shark_bool shark_battery_charge_full(void)
  22. {
  23. if (sub_bms_info_1.connected) {
  24. if (sub_bms_info_2.connected) {
  25. if (sub_bms_info_1.packet_common.m_percent > 99 && sub_bms_info_2.packet_common.m_percent > 99) {
  26. shark_battery_full = shark_true;
  27. }
  28. if (sub_bms_info_1.packet_common.m_percent < 97 || sub_bms_info_2.packet_common.m_percent < 97) {
  29. shark_battery_full = shark_false;
  30. }
  31. } else {
  32. if (sub_bms_info_1.packet_common.m_percent > 99) {
  33. shark_battery_full = shark_true;
  34. }
  35. if (sub_bms_info_1.packet_common.m_percent < 97) {
  36. shark_battery_full = shark_false;
  37. }
  38. }
  39. } else if (sub_bms_info_2.connected) {
  40. if (sub_bms_info_2.packet_common.m_percent > 99) {
  41. shark_battery_full = shark_true;
  42. }
  43. if (sub_bms_info_2.packet_common.m_percent < 97) {
  44. shark_battery_full = shark_false;
  45. }
  46. } else {
  47. return shark_true;
  48. }
  49. return shark_battery_full;
  50. }
  51. void shark_charger_set_poll(void)
  52. {
  53. shark_charger_state = SHARK_CHG_REMOVE;
  54. shark_charger_insert = 0;
  55. }
  56. shark_bool shark_charger_is_normal(void)
  57. {
  58. return shark_charger_enabled && (shark_battery_charge_in1() || shark_battery_charge_in2());
  59. }
  60. void shark_charger_set_enable(shark_bool enable)
  61. {
  62. if (shark_battery_charge_full()) {
  63. shark_charger_set_poll();
  64. enable = shark_false;
  65. }
  66. if (cb_operate_state == CB_BAT1_BAT2_SERIES) {
  67. enable = shark_false;
  68. }
  69. if (shark_charger_state != SHARK_CHG_INSERT) {
  70. enable = shark_false;
  71. }
  72. if (enable) {
  73. println("charger enable");
  74. shark_charge_time = shark_get_seconds();
  75. gpio_bit_set(GPIO_PORT_CHG_EN, GPIO_PIN_CHG_EN);
  76. } else {
  77. println("charger disable");
  78. gpio_bit_reset(GPIO_PORT_CHG_EN, GPIO_PIN_CHG_EN);
  79. }
  80. shark_charger_valid = shark_false;
  81. shark_charger_enabled = enable;
  82. }
  83. void shark_charger_disable(void)
  84. {
  85. if (shark_charge_get_time() > SHARK_CHG_TIME_DETECT || shark_charger_valid) {
  86. println("charger detect");
  87. shark_charger_insert = 0;
  88. shark_charge_skip = 6000;
  89. shark_charger_state = SHARK_CHG_DETECT;
  90. shark_charger_set_enable(shark_false);
  91. }
  92. }
  93. void shark_charge_tick(void)
  94. {
  95. if (gpio_input_bit_get(GPIO_PORT_CHG_DET, GPIO_PIN_CHG_DET) != RESET) {
  96. if (shark_charger_remove < SHARK_CHG_REMOVE_MIN) {
  97. if (shark_charger_insert > 0) {
  98. println("insert: %d", shark_charger_insert);
  99. shark_charger_insert = 0;
  100. }
  101. shark_charge_skip = 0;
  102. shark_charger_remove++;
  103. if (shark_charger_remove == SHARK_CHG_REMOVE_MIN) {
  104. shark_charger_state = SHARK_CHG_REMOVE;
  105. shark_battery_full = shark_false;
  106. println("charger remove");
  107. }
  108. }
  109. } else if (shark_charger_insert < SHARK_CHG_INSERT_MIN) {
  110. if (shark_charger_remove > 0) {
  111. println("remove: %d", shark_charger_remove);
  112. shark_charger_remove = 0;
  113. }
  114. if (shark_charge_skip > 0) {
  115. shark_charge_skip--;
  116. } else {
  117. shark_charger_insert++;
  118. if (shark_charger_insert == SHARK_CHG_INSERT_MIN) {
  119. if (shark_xl_check()) {
  120. shark_charger_set_poll();
  121. } else if (shark_battery_charge_full()) {
  122. shark_charger_set_poll();
  123. shark_battery_series_locked = shark_false;
  124. } else {
  125. shark_charger_state = SHARK_CHG_INSERT;
  126. println("charger insert");
  127. }
  128. }
  129. }
  130. }
  131. }
  132. void shark_charge_poll(void)
  133. {
  134. if (shark_charger_enabled) {
  135. switch (cb_operate_state) {
  136. case CB_BAT1:
  137. if (shark_battery_charge_in1()) {
  138. shark_charger_valid = shark_true;
  139. } else {
  140. shark_charger_disable();
  141. }
  142. break;
  143. case CB_BAT2:
  144. if (shark_battery_charge_in2()) {
  145. shark_charger_valid = shark_true;
  146. } else {
  147. shark_charger_disable();
  148. }
  149. break;
  150. case CB_BAT1_BAT2_PARRALLEL:
  151. if (shark_battery_charge_in1()) {
  152. if (shark_battery_charge_in2()) {
  153. shark_charger_valid = shark_true;
  154. } else if (shark_battery_get_current12() < 0) {
  155. shark_charger_disable();
  156. }
  157. } else if (shark_battery_charge_in2()) {
  158. if (shark_battery_get_current12() < 0) {
  159. shark_charger_disable();
  160. }
  161. } else {
  162. shark_charger_disable();
  163. }
  164. break;
  165. }
  166. } else if (shark_charger_state == SHARK_CHG_INSERT) {
  167. shark_charger_set_enable(shark_true);
  168. }
  169. }