iostate.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. #include "bsp/gpio.h"
  2. #include "bsp/mcu_power_sleep.h"
  3. #include "libs/shark_task.h"
  4. #include "libs/logger.h"
  5. #include "state.h"
  6. #include "health.h"
  7. #include "iostate.h"
  8. extern void bms_message_update_insert(int is_hall_detect);
  9. static io_state_t _io_state;
  10. typedef struct io_timer{
  11. shark_timer_t _timer;
  12. u16 detect_cnt;
  13. u8 value;
  14. u16 debounce_time_zero;
  15. u16 debounce_time_one;
  16. }io_timer_t;
  17. #define io_detect_intv_time 1
  18. #define io_debounce_time 50
  19. static void io_timer_handler(shark_timer_t *t);
  20. static io_timer_t hall_io = {._timer.handler = io_timer_handler, .debounce_time_zero = 500, .debounce_time_one = 50};
  21. static io_timer_t charger_io = {._timer.handler = io_timer_handler, .debounce_time_zero = 50, .debounce_time_one = 50};
  22. static io_timer_t aux_short_io = {._timer.handler = io_timer_handler, .debounce_time_zero = 50, .debounce_time_one = 50};
  23. static io_timer_t dcdc_pwr_io = {._timer.handler = io_timer_handler, .debounce_time_zero = 50, .debounce_time_one = 50};
  24. void iostate_log(void){
  25. io_debug("Hall:%d, %d, %d\n", _io_state.hall_detect, IS_HALL1_DETECTED(), IS_HALL2_DETECTED());
  26. io_debug("AuxLocked:%d\n", _io_state.aux_lock_detect);
  27. io_debug("ChargDet:%d\n", _io_state.charger_detect);
  28. io_debug("DcdcGood:%d\n", _io_state.dcdc_good_detect);
  29. }
  30. void io_state_init(void){
  31. set_log_level(MOD_IO, L_debug);
  32. hall_io.value = _io_state.hall_detect = IS_HALL1_DETECTED()|| IS_HALL2_DETECTED();
  33. charger_io.value = _io_state.charger_detect = IS_CHARGER_IN();
  34. dcdc_pwr_io.value = _io_state.dcdc_good_detect= IS_DCDC_POWER_GOOD();
  35. aux_short_io.value = _io_state.aux_lock_detect = IS_AUX_VOL_LOCKED() && AUX_VOL_IS_OPEN();
  36. shark_timer_post(&hall_io._timer, io_detect_intv_time);
  37. shark_timer_post(&charger_io._timer, io_detect_intv_time);
  38. shark_timer_post(&aux_short_io._timer, io_detect_intv_time);
  39. shark_timer_post(&dcdc_pwr_io._timer, io_detect_intv_time);
  40. small_current_short_irq_enable(1);
  41. charger_detect_irq_enable(1);
  42. hall_1_detect_irq_enable(1);
  43. hall_2_detect_irq_enable(1);
  44. bms_message_update_insert(_io_state.hall_detect);
  45. iostate_log();
  46. }
  47. io_state_t *io_state(void){
  48. return &_io_state;
  49. }
  50. static int _get_io_value(io_timer_t *t){
  51. if (t == &hall_io) {
  52. return IS_HALL1_DETECTED()|| IS_HALL2_DETECTED();
  53. }
  54. if (t == &charger_io){
  55. return IS_CHARGER_IN();
  56. }
  57. if (t == &aux_short_io){
  58. return IS_AUX_VOL_LOCKED() && AUX_VOL_IS_OPEN();
  59. }
  60. if (t == &dcdc_pwr_io){
  61. return IS_DCDC_POWER_GOOD();
  62. }
  63. return -1;
  64. }
  65. static int _get_cached_value(io_timer_t *t){
  66. if (t == &hall_io) {
  67. return _io_state.hall_detect;
  68. }
  69. if (t == &charger_io){
  70. return _io_state.charger_detect;
  71. }
  72. if (t == &aux_short_io){
  73. return _io_state.aux_lock_detect;
  74. }
  75. if (t == &dcdc_pwr_io){
  76. return _io_state.dcdc_good_detect;
  77. }
  78. return -1;
  79. }
  80. static void _set_io_value(io_timer_t *t){
  81. if (t == &hall_io) {
  82. _io_state.hall_detect = t->value;
  83. }
  84. if (t == &charger_io){
  85. _io_state.charger_detect = t->value;
  86. }
  87. if (t == &dcdc_pwr_io){
  88. _io_state.dcdc_good_detect = t->value;
  89. }
  90. if (t == &aux_short_io){
  91. _io_state.aux_lock_detect = t->value;
  92. }
  93. }
  94. static int _debounce_time_reach(io_timer_t *t){
  95. u16 debounce_time = t->debounce_time_one;
  96. if (t->value == 0){
  97. debounce_time = t->debounce_time_zero;
  98. }
  99. return t->detect_cnt >= debounce_time;
  100. }
  101. static void io_timer_handler(shark_timer_t *t){
  102. io_timer_t *io_t = (io_timer_t *)t;
  103. int aux_lock_changed = 0;
  104. if (io_t->value == _get_io_value(io_t)){
  105. io_t->detect_cnt ++;
  106. }else{
  107. io_t->value = _get_io_value(io_t);
  108. io_t->detect_cnt = 0;
  109. }
  110. if (_debounce_time_reach(io_t)){
  111. io_t->detect_cnt = 0;
  112. if (_get_cached_value(io_t) != io_t->value) {
  113. _set_io_value(io_t);
  114. if (io_t == &aux_short_io){
  115. aux_lock_changed = 1;
  116. }
  117. if (io_t == &hall_io){
  118. bms_message_update_insert(_io_state.hall_detect);
  119. }
  120. }
  121. }
  122. shark_timer_post(t, io_detect_intv_time);
  123. if (io_t == &aux_short_io && aux_lock_changed){
  124. health_process_aux_lock();
  125. }
  126. }
  127. static void charger_detect_irq_timer_handler(shark_timer_t *t){
  128. _io_state.charger_detect_irq = 0;
  129. io_warning("clear charger detect irq\n");
  130. }
  131. static void small_current_irq_timer_handler(shark_timer_t *t){
  132. aux_short_io.value = _io_state.aux_lock_detect = IS_AUX_VOL_LOCKED() && AUX_VOL_IS_OPEN();
  133. aux_short_io.detect_cnt = 0;
  134. health_process_aux_lock();
  135. }
  136. static shark_timer_t _small_current_irq_timer = {.handler = small_current_irq_timer_handler};
  137. static shark_timer_t _charger_detect_irq_timer = {.handler = charger_detect_irq_timer_handler};
  138. void small_current_short_irq_handler(void){
  139. if (!AUX_VOL_IS_OPEN()){
  140. //io_debug("close aux power,cause the short irq\n");
  141. return;
  142. }
  143. mcu_sleep_set_wakeup_source(WAKEUP_SOURCE_SMALL_POWER_SHORT);
  144. _io_state.aux_lock_irq = 1;
  145. shark_timer_post(&_small_current_irq_timer, 0);
  146. //io_debug("aux lock irq\n");
  147. }
  148. void charger_detect_irq_handler(void){
  149. mcu_sleep_set_wakeup_source(WAKEUP_SOURCE_CHARGER);
  150. _io_state.charger_detect_irq = 1;
  151. shark_timer_post(&_charger_detect_irq_timer, CHARGER_DETECT_IRQ_CLEAR_TIMEOUT);//延时一段时间给充电判断充足的时间
  152. io_warning("charger detect irq\n");
  153. }
  154. void hall1_detect_irq_handler(void){
  155. mcu_sleep_set_wakeup_source(WAKEUP_SOURCE_HALL1);
  156. }
  157. void hall2_detect_irq_handler(void){
  158. mcu_sleep_set_wakeup_source(WAKEUP_SOURCE_HALL2);
  159. }
  160. #if 0
  161. void dcdc_pwr_detect_irq_handler(void) {
  162. dcdc_pwr_io.value = IS_DCDC_POWER_GOOD();
  163. dcdc_pwr_io.detect_cnt = 0;
  164. shark_timer_post(&dcdc_pwr_io._timer, 0);
  165. dcdc_pwr_detect_irq_enable(0);
  166. io_debug("dcdc lock irq");
  167. }
  168. #endif