iostate.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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 = 1000, .debounce_time_one = 2};
  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("PwrGood:%d\n", _io_state.pwr_good_detect);
  29. io_debug("DcdcGood:%d\n", _io_state.dcdc_good_detect);
  30. }
  31. void io_state_init(void){
  32. set_log_level(MOD_IO, L_debug);
  33. _io_state.hall_detect = IS_HALL1_DETECTED()|| IS_HALL2_DETECTED();
  34. _io_state.charger_detect = IS_CHARGER_IN();
  35. _io_state.dcdc_good_detect= IS_DCDC_POWER_GOOD();
  36. _io_state.aux_lock_detect = IS_AUX_VOL_LOCKED() && AUX_VOL_IS_OPEN();
  37. shark_timer_post(&hall_io._timer, io_detect_intv_time);
  38. shark_timer_post(&charger_io._timer, io_detect_intv_time);
  39. shark_timer_post(&aux_short_io._timer, io_detect_intv_time);
  40. shark_timer_post(&dcdc_pwr_io._timer, io_detect_intv_time);
  41. small_current_short_irq_enable(1);
  42. charger_detect_irq_enable(1);
  43. bms_message_update_insert(_io_state.hall_detect);
  44. iostate_log();
  45. }
  46. io_state_t *io_state(void){
  47. return &_io_state;
  48. }
  49. static int _get_io_value(io_timer_t *t){
  50. if (t == &hall_io) {
  51. return IS_HALL1_DETECTED()|| IS_HALL2_DETECTED();
  52. }
  53. if (t == &charger_io){
  54. return IS_CHARGER_IN();
  55. }
  56. if (t == &aux_short_io){
  57. return IS_AUX_VOL_LOCKED() && AUX_VOL_IS_OPEN();
  58. }
  59. if (t == &dcdc_pwr_io){
  60. return IS_DCDC_POWER_GOOD();
  61. }
  62. return -1;
  63. }
  64. static int _get_cached_value(io_timer_t *t){
  65. if (t == &hall_io) {
  66. return _io_state.hall_detect;
  67. }
  68. if (t == &charger_io){
  69. return _io_state.charger_detect;
  70. }
  71. if (t == &aux_short_io){
  72. return _io_state.aux_lock_detect;
  73. }
  74. if (t == &dcdc_pwr_io){
  75. return _io_state.dcdc_good_detect;
  76. }
  77. return -1;
  78. }
  79. static void _set_io_value(io_timer_t *t){
  80. if (t == &hall_io) {
  81. _io_state.hall_detect = t->value;
  82. }
  83. if (t == &charger_io){
  84. _io_state.charger_detect = t->value;
  85. }
  86. if (t == &dcdc_pwr_io){
  87. _io_state.dcdc_good_detect = t->value;
  88. }
  89. if (t == &aux_short_io){
  90. _io_state.aux_lock_detect = t->value;
  91. }
  92. }
  93. static int _debounce_time_reach(io_timer_t *t){
  94. u16 debounce_time = t->debounce_time_one;
  95. if (t->value == 0){
  96. debounce_time = t->debounce_time_zero;
  97. }
  98. return t->detect_cnt >= debounce_time;
  99. }
  100. static void io_timer_handler(shark_timer_t *t){
  101. io_timer_t *io_t = (io_timer_t *)t;
  102. int aux_lock_changed = 0;
  103. if (io_t->value == _get_io_value(io_t)){
  104. io_t->detect_cnt ++;
  105. }else{
  106. io_t->value = _get_io_value(io_t);
  107. io_t->detect_cnt = 0;
  108. }
  109. if (_debounce_time_reach(io_t)){
  110. io_t->detect_cnt = 0;
  111. if (_get_cached_value(io_t) != io_t->value) {
  112. _set_io_value(io_t);
  113. if (io_t == &aux_short_io){
  114. aux_lock_changed = 1;
  115. }
  116. if (io_t == &hall_io){
  117. bms_message_update_insert(_io_state.hall_detect);
  118. }
  119. }
  120. }
  121. shark_timer_post(t, io_detect_intv_time);
  122. if (io_t == &aux_short_io && aux_lock_changed){
  123. health_process_aux_lock();
  124. }
  125. }
  126. static void charger_detect_irq_timer_handler(shark_timer_t *t){
  127. _io_state.charger_detect_irq = 0;
  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. shark_timer_post(&_small_current_irq_timer, 0);
  142. //io_debug("aux lock irq\n");
  143. }
  144. void charger_detect_irq_handler(void){
  145. mcu_sleep_set_wakeup_source(WAKEUP_SOURCE_CHARGER);
  146. _io_state.charger_detect_irq = 1;
  147. shark_timer_post(&_charger_detect_irq_timer, 2000);//延时2s给充电判断充足的时间
  148. io_debug("charger detect\n");
  149. }
  150. void hall1_detect_irq_handler(void){
  151. mcu_sleep_set_wakeup_source(WAKEUP_SOURCE_HALL1);
  152. }
  153. void hall2_detect_irq_handler(void){
  154. mcu_sleep_set_wakeup_source(WAKEUP_SOURCE_HALL2);
  155. }
  156. #if 0
  157. void dcdc_pwr_detect_irq_handler(void) {
  158. dcdc_pwr_io.value = IS_DCDC_POWER_GOOD();
  159. dcdc_pwr_io.detect_cnt = 0;
  160. shark_timer_post(&dcdc_pwr_io._timer, 0);
  161. dcdc_pwr_detect_irq_enable(0);
  162. io_debug("dcdc lock irq");
  163. }
  164. #endif