iostate.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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\n", *io);
  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. // charger_detect_irq_enable(1);
  39. // hall_1_detect_irq_enable(1);
  40. // hall_2_detect_irq_enable(1);
  41. bms_message_update_insert(_io_state.hall_detect);
  42. debug_io();
  43. }
  44. io_state_t *io_state(void){
  45. return &_io_state;
  46. }
  47. static int _get_io_value(io_timer_t *t){
  48. if (t == &hall_io) {
  49. return IS_HALL1_DETECTED()|| IS_HALL2_DETECTED();
  50. }
  51. if (t == &charger_io){
  52. return IS_CHARGER_IN();
  53. }
  54. if (t == &aux_short_io){
  55. return IS_AUX_VOL_LOCKED() && AUX_VOL_IS_OPEN();
  56. }
  57. if (t == &dcdc_pwr_io){
  58. return IS_DCDC_POWER_GOOD();
  59. }
  60. return -1;
  61. }
  62. static int _get_cached_value(io_timer_t *t){
  63. if (t == &hall_io) {
  64. return _io_state.hall_detect;
  65. }
  66. if (t == &charger_io){
  67. return _io_state.charger_detect;
  68. }
  69. if (t == &aux_short_io){
  70. return _io_state.aux_lock_detect;
  71. }
  72. if (t == &dcdc_pwr_io){
  73. return _io_state.dcdc_good_detect;
  74. }
  75. return -1;
  76. }
  77. static void _set_io_value(io_timer_t *t){
  78. if (t == &hall_io) {
  79. _io_state.hall_detect = t->value;
  80. }
  81. if (t == &charger_io){
  82. _io_state.charger_detect = t->value;
  83. }
  84. if (t == &dcdc_pwr_io){
  85. _io_state.dcdc_good_detect = t->value;
  86. }
  87. if (t == &aux_short_io){
  88. _io_state.aux_lock_detect = t->value;
  89. }
  90. debug_io();
  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 small_current_irq_timer_handler(shark_timer_t *t){
  126. _io_state.aux_lock_detect = 1;
  127. health_process_aux_lock();
  128. }
  129. static shark_timer_t _small_current_irq_timer = {.handler = small_current_irq_timer_handler};
  130. void small_current_short_irq_handler(void){
  131. if (!AUX_VOL_IS_OPEN()){
  132. io_debug("close aux power,cause the short irq\n");
  133. return;
  134. }
  135. shark_timer_post(&_small_current_irq_timer, 0);
  136. //io_debug("aux lock irq\n");
  137. }
  138. void charger_detect_irq_handler(void){
  139. //io_debug("charger irq\n");
  140. }
  141. void hall1_detect_irq_handler(void){
  142. //io_debug("hall 1 irq\n");
  143. }
  144. void hall2_detect_irq_handler(void){
  145. //io_debug("hall 2 irq\n");
  146. }
  147. #if 0
  148. void dcdc_pwr_detect_irq_handler(void) {
  149. dcdc_pwr_io.value = IS_DCDC_POWER_GOOD();
  150. dcdc_pwr_io.detect_cnt = 0;
  151. shark_timer_post(&dcdc_pwr_io._timer, 0);
  152. dcdc_pwr_detect_irq_enable(0);
  153. io_debug("dcdc lock irq");
  154. }
  155. #endif