iostate.c 3.6 KB

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