iostate.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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 = 100, .debounce_time_one = 100};
  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. _io_state.hall_detect = IS_HALL1_DETECTED()|| IS_HALL2_DETECTED();
  33. _io_state.charger_detect = IS_CHARGER_IN();
  34. _io_state.dcdc_good_detect= IS_DCDC_POWER_GOOD();
  35. _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. bms_message_update_insert(_io_state.hall_detect);
  43. iostate_log();
  44. }
  45. io_state_t *io_state(void){
  46. return &_io_state;
  47. }
  48. static int _get_io_value(io_timer_t *t){
  49. if (t == &hall_io) {
  50. return IS_HALL1_DETECTED()|| IS_HALL2_DETECTED();
  51. }
  52. if (t == &charger_io){
  53. return IS_CHARGER_IN();
  54. }
  55. if (t == &aux_short_io){
  56. return IS_AUX_VOL_LOCKED() && AUX_VOL_IS_OPEN();
  57. }
  58. if (t == &dcdc_pwr_io){
  59. return IS_DCDC_POWER_GOOD();
  60. }
  61. return -1;
  62. }
  63. static int _get_cached_value(io_timer_t *t){
  64. if (t == &hall_io) {
  65. return _io_state.hall_detect;
  66. }
  67. if (t == &charger_io){
  68. return _io_state.charger_detect;
  69. }
  70. if (t == &aux_short_io){
  71. return _io_state.aux_lock_detect;
  72. }
  73. if (t == &dcdc_pwr_io){
  74. return _io_state.dcdc_good_detect;
  75. }
  76. return -1;
  77. }
  78. static void _set_io_value(io_timer_t *t){
  79. if (t == &hall_io) {
  80. _io_state.hall_detect = t->value;
  81. }
  82. if (t == &charger_io){
  83. _io_state.charger_detect = t->value;
  84. }
  85. if (t == &dcdc_pwr_io){
  86. _io_state.dcdc_good_detect = t->value;
  87. }
  88. if (t == &aux_short_io){
  89. _io_state.aux_lock_detect = t->value;
  90. }
  91. }
  92. static int _debounce_time_reach(io_timer_t *t){
  93. u16 debounce_time = t->debounce_time_one;
  94. if (t->value == 0){
  95. debounce_time = t->debounce_time_zero;
  96. }
  97. return t->detect_cnt >= debounce_time;
  98. }
  99. static void io_timer_handler(shark_timer_t *t){
  100. io_timer_t *io_t = (io_timer_t *)t;
  101. int aux_lock_changed = 0;
  102. if (io_t->value == _get_io_value(io_t)){
  103. io_t->detect_cnt ++;
  104. }else{
  105. io_t->value = _get_io_value(io_t);
  106. io_t->detect_cnt = 0;
  107. }
  108. if (_debounce_time_reach(io_t)){
  109. io_t->detect_cnt = 0;
  110. if (_get_cached_value(io_t) != io_t->value) {
  111. _set_io_value(io_t);
  112. if (io_t == &aux_short_io){
  113. aux_lock_changed = 1;
  114. }
  115. if (io_t == &hall_io){
  116. bms_message_update_insert(_io_state.hall_detect);
  117. }
  118. }
  119. }
  120. shark_timer_post(t, io_detect_intv_time);
  121. if (io_t == &aux_short_io && aux_lock_changed){
  122. health_process_aux_lock();
  123. }
  124. }
  125. static void charger_detect_irq_timer_handler(shark_timer_t *t){
  126. _io_state.charger_detect_irq = 0;
  127. io_warning("clear charger detect irq\n");
  128. }
  129. static void small_current_irq_timer_handler(shark_timer_t *t){
  130. aux_short_io.value = _io_state.aux_lock_detect = IS_AUX_VOL_LOCKED() && AUX_VOL_IS_OPEN();
  131. aux_short_io.detect_cnt = 0;
  132. health_process_aux_lock();
  133. }
  134. static shark_timer_t _small_current_irq_timer = {.handler = small_current_irq_timer_handler};
  135. static shark_timer_t _charger_detect_irq_timer = {.handler = charger_detect_irq_timer_handler};
  136. void small_current_short_irq_handler(void){
  137. if (!AUX_VOL_IS_OPEN()){
  138. io_debug("close aux power,cause the short irq\n");
  139. return;
  140. }
  141. _io_state.aux_lock_irq = 1;
  142. shark_timer_post(&_small_current_irq_timer, 0);
  143. //io_debug("aux lock irq\n");
  144. }
  145. void charger_detect_irq_handler(void){
  146. mcu_sleep_set_wakeup_source(WAKEUP_SOURCE_CHARGER);
  147. _io_state.charger_detect_irq = 1;
  148. shark_timer_post(&_charger_detect_irq_timer, CHARGER_DETECT_IRQ_CLEAR_TIMEOUT);//延时一段时间给充电判断充足的时间
  149. io_warning("charger detect irq\n");
  150. }
  151. void hall1_detect_irq_handler(void){
  152. mcu_sleep_set_wakeup_source(WAKEUP_SOURCE_HALL1);
  153. }
  154. void hall2_detect_irq_handler(void){
  155. mcu_sleep_set_wakeup_source(WAKEUP_SOURCE_HALL2);
  156. }
  157. #if 0
  158. void dcdc_pwr_detect_irq_handler(void) {
  159. dcdc_pwr_io.value = IS_DCDC_POWER_GOOD();
  160. dcdc_pwr_io.detect_cnt = 0;
  161. shark_timer_post(&dcdc_pwr_io._timer, 0);
  162. dcdc_pwr_detect_irq_enable(0);
  163. io_debug("dcdc lock irq");
  164. }
  165. #endif