| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181 |
- #include "bsp/gpio.h"
- #include "libs/shark_task.h"
- #include "libs/logger.h"
- #include "state.h"
- #include "health.h"
- #include "iostate.h"
- extern void bms_message_update_insert(int is_hall_detect);
- static io_state_t _io_state;
- typedef struct io_timer{
- shark_timer_t _timer;
- u16 detect_cnt;
- u8 value;
- u16 debounce_time_zero;
- u16 debounce_time_one;
- }io_timer_t;
- #define io_detect_intv_time 1
- #define io_debounce_time 50
- static void io_timer_handler(shark_timer_t *t);
- static io_timer_t hall_io = {._timer.handler = io_timer_handler, .debounce_time_zero = 1000, .debounce_time_one = 2};
- static io_timer_t charger_io = {._timer.handler = io_timer_handler, .debounce_time_zero = 50, .debounce_time_one = 50};
- static io_timer_t aux_short_io = {._timer.handler = io_timer_handler, .debounce_time_zero = 50, .debounce_time_one = 50};
- static io_timer_t dcdc_pwr_io = {._timer.handler = io_timer_handler, .debounce_time_zero = 50, .debounce_time_one = 50};
- static void debug_io(void){
- uint16_t *io = (uint16_t *)&_io_state;
- io_debug("io state = 0x%x\n", *io);
- }
- void io_state_init(void){
- set_log_level(MOD_IO, L_debug);
- _io_state.hall_detect = IS_HALL1_DETECTED()|| IS_HALL2_DETECTED();
- _io_state.charger_detect = IS_CHARGER_IN();
- _io_state.dcdc_good_detect= IS_DCDC_POWER_GOOD();
- _io_state.aux_lock_detect = IS_AUX_VOL_LOCKED() && AUX_VOL_IS_OPEN();
- shark_timer_post(&hall_io._timer, io_detect_intv_time);
- shark_timer_post(&charger_io._timer, io_detect_intv_time);
- shark_timer_post(&aux_short_io._timer, io_detect_intv_time);
- shark_timer_post(&dcdc_pwr_io._timer, io_detect_intv_time);
- small_current_short_irq_enable(1);
- // charger_detect_irq_enable(1);
- // hall_1_detect_irq_enable(1);
- // hall_2_detect_irq_enable(1);
- bms_message_update_insert(_io_state.hall_detect);
-
- debug_io();
- }
- io_state_t *io_state(void){
- return &_io_state;
- }
- static int _get_io_value(io_timer_t *t){
- if (t == &hall_io) {
- return IS_HALL1_DETECTED()|| IS_HALL2_DETECTED();
- }
- if (t == &charger_io){
- return IS_CHARGER_IN();
- }
- if (t == &aux_short_io){
- return IS_AUX_VOL_LOCKED() && AUX_VOL_IS_OPEN();
- }
- if (t == &dcdc_pwr_io){
- return IS_DCDC_POWER_GOOD();
- }
- return -1;
- }
- static int _get_cached_value(io_timer_t *t){
- if (t == &hall_io) {
- return _io_state.hall_detect;
- }
- if (t == &charger_io){
- return _io_state.charger_detect;
- }
- if (t == &aux_short_io){
- return _io_state.aux_lock_detect;
- }
- if (t == &dcdc_pwr_io){
- return _io_state.dcdc_good_detect;
- }
- return -1;
- }
- static void _set_io_value(io_timer_t *t){
- if (t == &hall_io) {
- _io_state.hall_detect = t->value;
- }
- if (t == &charger_io){
- _io_state.charger_detect = t->value;
- }
- if (t == &dcdc_pwr_io){
- _io_state.dcdc_good_detect = t->value;
- }
- if (t == &aux_short_io){
- _io_state.aux_lock_detect = t->value;
- }
- debug_io();
- }
- static int _debounce_time_reach(io_timer_t *t){
- u16 debounce_time = t->debounce_time_one;
- if (t->value == 0){
- debounce_time = t->debounce_time_zero;
- }
- return t->detect_cnt >= debounce_time;
- }
- static void io_timer_handler(shark_timer_t *t){
- io_timer_t *io_t = (io_timer_t *)t;
- int aux_lock_changed = 0;
- if (io_t->value == _get_io_value(io_t)){
- io_t->detect_cnt ++;
- }else{
- io_t->value = _get_io_value(io_t);
- io_t->detect_cnt = 0;
- }
- if (_debounce_time_reach(io_t)){
- io_t->detect_cnt = 0;
- if (_get_cached_value(io_t) != io_t->value) {
- _set_io_value(io_t);
- if (io_t == &aux_short_io){
- aux_lock_changed = 1;
- }
- if (io_t == &hall_io){
- bms_message_update_insert(_io_state.hall_detect);
- }
- }
- }
- shark_timer_post(t, io_detect_intv_time);
- if (io_t == &aux_short_io && aux_lock_changed){
- health_process_aux_lock();
- }
- }
- static void small_current_irq_timer_handler(shark_timer_t *t){
- _io_state.aux_lock_detect = 1;
- health_process_aux_lock();
- }
- static shark_timer_t _small_current_irq_timer = {.handler = small_current_irq_timer_handler};
- void small_current_short_irq_handler(void){
- if (!AUX_VOL_IS_OPEN()){
- io_debug("close aux power,cause the short irq\n");
- return;
- }
- shark_timer_post(&_small_current_irq_timer, 0);
- //io_debug("aux lock irq\n");
- }
- void charger_detect_irq_handler(void){
- //io_debug("charger irq\n");
- }
- void hall1_detect_irq_handler(void){
- //io_debug("hall 1 irq\n");
- }
- void hall2_detect_irq_handler(void){
- //io_debug("hall 2 irq\n");
- }
- #if 0
- void dcdc_pwr_detect_irq_handler(void) {
- dcdc_pwr_io.value = IS_DCDC_POWER_GOOD();
- dcdc_pwr_io.detect_cnt = 0;
- shark_timer_post(&dcdc_pwr_io._timer, 0);
- dcdc_pwr_detect_irq_enable(0);
- io_debug("dcdc lock irq");
- }
- #endif
|