iostate.c 3.7 KB

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