iostate.c 3.6 KB

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