iostate.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. #include "bsp/gpio.h"
  2. #include "libs/shark_task.h"
  3. #include "libs/logger.h"
  4. #include "state.h"
  5. #include "iostate.h"
  6. static io_state_t _io_state;
  7. typedef struct io_timer{
  8. shark_timer_t _timer;
  9. u16 detect_cnt;
  10. u8 value;
  11. }io_timer_t;
  12. #define io_detect_intv_time 1
  13. #define io_debounce_time 50
  14. static io_timer_t hall_io;
  15. static io_timer_t charger_io;
  16. static io_timer_t aux_short_io;
  17. static io_timer_t dcdc_pwr_io;
  18. static void io_timer_handler(shark_timer_t *t);
  19. static void debug_io(void){
  20. uint16_t *io = (uint16_t *)&_io_state;
  21. io_debug("io state = 0x%x\n", *io);
  22. }
  23. void io_state_init(void){
  24. set_log_level(MOD_IO, L_debug);
  25. _io_state.hall_detect = IS_HALL1_DETECTED()|| IS_HALL2_DETECTED();
  26. _io_state.charger_detect = IS_CHARGER_IN();
  27. _io_state.dcdc_good_detect= IS_DCDC_POWER_GOOD();
  28. _io_state.aux_lock_detect = IS_AUX_VOL_LOCKED();
  29. hall_io._timer.handler = io_timer_handler;
  30. charger_io._timer.handler = io_timer_handler;
  31. aux_short_io._timer.handler = io_timer_handler;
  32. dcdc_pwr_io._timer.handler = io_timer_handler;
  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. debug_io();
  38. }
  39. io_state_t *io_state(void){
  40. return &_io_state;
  41. }
  42. static int _get_io_value(io_timer_t *t){
  43. if (t == &hall_io) {
  44. return IS_HALL1_DETECTED()|| IS_HALL2_DETECTED();
  45. }
  46. if (t == &charger_io){
  47. return IS_CHARGER_IN();
  48. }
  49. if (t == &aux_short_io){
  50. return IS_AUX_VOL_LOCKED();
  51. }
  52. if (t == &dcdc_pwr_io){
  53. return IS_DCDC_POWER_GOOD();
  54. }
  55. return -1;
  56. }
  57. static void _set_io_value(io_timer_t *t){
  58. __disable_irq();
  59. if (t == &hall_io) {
  60. _io_state.hall_detect = t->value;
  61. hall_1_detect_irq_enable(1);
  62. hall_2_detect_irq_enable(1);
  63. }
  64. if (t == &charger_io){
  65. _io_state.charger_detect = t->value;
  66. charger_detect_irq_enable(1);
  67. }
  68. if (t == &aux_short_io){
  69. _io_state.aux_lock_detect = t->value;
  70. small_current_short_irq_enable(1);
  71. }
  72. if (t == &dcdc_pwr_io){
  73. _io_state.dcdc_good_detect = t->value;
  74. dcdc_pwr_detect_irq_enable(1);
  75. }
  76. /* 可以解决丢中断的风险,如果开启中断的过程中,来IO中断,这个中断可能会被丢弃,
  77. * 所以,我们这里要再次检查io的状态和代码保存的状态是否一致,如果不一致需要重新
  78. * 启动定时间
  79. */
  80. if (t->value != _get_io_value(t)){
  81. shark_timer_post(&t->_timer, io_detect_intv_time);
  82. }
  83. __enable_irq();
  84. debug_io();
  85. }
  86. static void io_timer_handler(shark_timer_t *t){
  87. io_timer_t *io_t = (io_timer_t *)t;
  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. _set_io_value(io_t);
  97. }else{
  98. shark_timer_post(t, io_detect_intv_time);
  99. }
  100. }
  101. void charger_detect_irq_handler(void){
  102. charger_io.value = IS_CHARGER_IN();
  103. charger_io.detect_cnt = 0;
  104. shark_timer_post(&charger_io._timer, io_detect_intv_time);
  105. charger_detect_irq_enable(0);
  106. io_debug("charger irq\n");
  107. }
  108. void hall1_detect_irq_handler(void){
  109. hall_io.value = IS_HALL1_DETECTED()|| IS_HALL2_DETECTED();
  110. hall_io.detect_cnt = 0;
  111. shark_timer_post(&hall_io._timer, io_detect_intv_time);
  112. hall_1_detect_irq_enable(0);
  113. io_debug("hall 1 irq\n");
  114. }
  115. void hall2_detect_irq_handler(void){
  116. hall_io.value = IS_HALL1_DETECTED()|| IS_HALL2_DETECTED();
  117. hall_io.detect_cnt = 0;
  118. shark_timer_post(&hall_io._timer, io_detect_intv_time);
  119. hall_2_detect_irq_enable(0);
  120. io_debug("hall 2 irq\n");
  121. }
  122. void small_current_short_handler(void){
  123. aux_short_io.value = IS_AUX_VOL_LOCKED();
  124. aux_short_io.detect_cnt = 0;
  125. shark_timer_post(&aux_short_io._timer, io_detect_intv_time);
  126. small_current_short_irq_enable(0);
  127. io_debug("aux lock irq\n");
  128. }
  129. void dcdc_pwr_detect_irq_handler(void) {
  130. dcdc_pwr_io.value = IS_DCDC_POWER_GOOD();
  131. dcdc_pwr_io.detect_cnt = 0;
  132. shark_timer_post(&dcdc_pwr_io._timer, io_detect_intv_time);
  133. dcdc_pwr_detect_irq_enable(0);
  134. io_debug("dcdc lock irq");
  135. }