iostate.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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();
  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();
  49. }
  50. if (t == &dcdc_pwr_io){
  51. return IS_DCDC_POWER_GOOD();
  52. }
  53. return -1;
  54. }
  55. static void _set_io_value(io_timer_t *t){
  56. if (t == &hall_io) {
  57. _io_state.hall_detect = t->value;
  58. }
  59. if (t == &charger_io){
  60. _io_state.charger_detect = t->value;
  61. }
  62. if (t == &dcdc_pwr_io){
  63. _io_state.dcdc_good_detect = t->value;
  64. }
  65. if (t == &aux_short_io){
  66. _io_state.aux_lock_detect = t->value;
  67. }
  68. debug_io();
  69. }
  70. static void io_timer_handler(shark_timer_t *t){
  71. io_timer_t *io_t = (io_timer_t *)t;
  72. int aux_lock_changed = 0;
  73. if (io_t->value == _get_io_value(io_t)){
  74. io_t->detect_cnt ++;
  75. }else{
  76. io_t->value = _get_io_value(io_t);
  77. io_t->detect_cnt = 0;
  78. }
  79. if (io_t->detect_cnt >= io_debounce_time){
  80. io_t->detect_cnt = 0;
  81. _set_io_value(io_t);
  82. if (io_t == &aux_short_io){
  83. aux_lock_changed = 1;
  84. }
  85. }
  86. shark_timer_post(t, io_detect_intv_time);
  87. if (io_t == &aux_short_io && aux_lock_changed){
  88. health_process_aux_lock();
  89. }
  90. }
  91. static void small_current_irq_timer_handler(shark_timer_t *t){
  92. _io_state.aux_lock_detect = 1;
  93. health_process_aux_lock();
  94. }
  95. static shark_timer_t _small_current_irq_timer = {.handler = small_current_irq_timer_handler};
  96. void small_current_short_irq_handler(void){
  97. shark_timer_post(&_small_current_irq_timer, 0);
  98. io_debug("aux lock irq\n");
  99. }
  100. void charger_detect_irq_handler(void){
  101. io_debug("charger irq\n");
  102. }
  103. void hall1_detect_irq_handler(void){
  104. io_debug("hall 1 irq\n");
  105. }
  106. void hall2_detect_irq_handler(void){
  107. io_debug("hall 2 irq\n");
  108. }
  109. #if 0
  110. void dcdc_pwr_detect_irq_handler(void) {
  111. dcdc_pwr_io.value = IS_DCDC_POWER_GOOD();
  112. dcdc_pwr_io.detect_cnt = 0;
  113. shark_timer_post(&dcdc_pwr_io._timer, 0);
  114. dcdc_pwr_detect_irq_enable(0);
  115. io_debug("dcdc lock irq");
  116. }
  117. #endif