iostate.c 4.5 KB

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