123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322 |
- #include <string.h>
- #include "ap_cp.h"
- #include "OSAL.h"
- #include "qdec.h"
- #include "gpio.h"
- #include "common.h"
- #include "uart.h"
- #include "log.h"
- #include "pwrmgr.h"
- #include "error.h"
- #include "clock.h"
- qdec_Ctx_t m_qdecCtx;
- void __attribute__((used)) hal_QDEC_IRQHandler()
- {
- hal_gpio_pin_init(P20, OEN);
- hal_gpio_write(P20,1);
-
- osal_stop_timerEx(m_qdecCtx.qdec_task_id, m_qdecCtx.timeout_event);
-
- WaitMs(1);
-
- int32_t delta = GET_CNT_QUAN(m_qdecCtx.cfg.qdec_chn);
- m_qdecCtx.count += delta;
-
- if(m_qdecCtx.cfg.evt_handler){
- qdec_Evt_t evt;
- evt.count = m_qdecCtx.count;
- m_qdecCtx.cfg.evt_handler(&evt);
- }
-
- CLR_INT_QUAN(m_qdecCtx.cfg.qdec_chn);
- hal_pwrmgr_unlock(MOD_QDEC);
-
- hal_gpio_pin_init(P20, OEN);
- hal_gpio_write(P20,0);
- }
- void hal_qdec_timeout_handler()
- {
- osal_stop_timerEx(m_qdecCtx.qdec_task_id, m_qdecCtx.timeout_event);
-
- hal_pwrmgr_unlock(MOD_QDEC);
- }
- static void hal_qdec_set_cha(QDEC_CHN_e qdecCHN,GPIO_Pin_e pin){
-
- hal_gpio_pull_set(pin, PULL_DOWN);
- hal_gpio_fmux_set(pin, (Fmux_Type_e)(CHAX + (qdecCHN*3)));
- }
- static void hal_qdec_set_chb(QDEC_CHN_e qdecCHN,GPIO_Pin_e pin){
-
- hal_gpio_pull_set(pin, PULL_DOWN);
- hal_gpio_fmux_set(pin, (Fmux_Type_e)(CHBX + (qdecCHN*3)));
- }
- static void hal_qdec_set_chi(QDEC_CHN_e qdecCHN,GPIO_Pin_e pin){
-
- hal_gpio_pull_set(pin, PULL_DOWN);
- hal_gpio_fmux_set(pin, (Fmux_Type_e)(CHIX + (qdecCHN*3)));
- }
- int hal_qdec_init(qdec_Cfg_t cfg, uint8 task_id, uint16 event){
-
- if(m_qdecCtx.enable)
- return PPlus_ERR_INVALID_STATE;
-
- uint8_t pins[3] = {cfg.cha_pin, cfg.chb_pin, cfg.chi_pin};
- memcpy(m_qdecCtx.pin_arr, pins, sizeof(uint8_t)*3);
-
- m_qdecCtx.cfg = cfg;
- m_qdecCtx.qdec_task_id = task_id;
- m_qdecCtx.timeout_event = event;
- m_qdecCtx.enable = TRUE;
-
- qdec_hw_config();
-
- hal_pwrmgr_register(MOD_QDEC, qdec_sleep_handler, qdec_wakeup_handler);
-
- return PPlus_SUCCESS;
- }
- void qdec_hw_config()
- {
- qdec_Cfg_t* cfg = &(m_qdecCtx.cfg);
-
- clk_gate_enable(MOD_QDEC);
-
- hal_qdec_set_cha(cfg->qdec_chn, cfg->cha_pin);
- hal_qdec_set_chb(cfg->qdec_chn, cfg->chb_pin);
-
- DIS_INT_INCN(cfg->qdec_chn);
- DIS_INT_QUAN(cfg->qdec_chn);
- DIS_INT_02F_QUAN(cfg->qdec_chn);
- DIS_INT_F20_QUAN(cfg->qdec_chn);
-
- SET_MODE_QUAN(cfg->qdec_chn, cfg->quaMode);
-
- hal_qdec_set_qua_irq(cfg->qdec_chn, cfg->intMode);
-
- if(cfg->use_inc){
- hal_qdec_set_chi(cfg->qdec_chn, cfg->chi_pin);
-
- if(cfg->use_inc_irq){
- hal_qdec_set_inc_irq(cfg->qdec_chn, cfg->incMode, cfg->intMode);
- }
- }
-
- QDEC_IRQ_ENABLE;
- ENABLE_CHN(cfg->qdec_chn);
- }
- static void qdec_sleep_handler(void)
- {
- uint8_t pin_num;
- pin_num = m_qdecCtx.cfg.use_inc ? 3:2;
-
- for(uint8_t i=0;i<pin_num;i++){
- IO_Wakeup_Pol_e pol;
- GPIO_Pin_e pin = m_qdecCtx.pin_arr[i];
-
- hal_gpio_pin_init(pin, IE);
- pol = hal_gpio_read(pin) ? NEGEDGE:POSEDGE;
- hal_gpio_wakeup_set(pin, pol);
- m_qdecCtx.pin_state[i] = pol;
- }
-
- DISABLE_CHN(m_qdecCtx.cfg.qdec_chn);
- hal_gpio_pin_init(P23, OEN);
- hal_gpio_write(P23,0);
- }
- static void qdec_wakeup_handler(void)
- {
- hal_gpio_pin_init(P23, OEN);
- hal_gpio_write(P23,1);
- uint8_t pin_num;
- pin_num = m_qdecCtx.cfg.use_inc ? 3:2;
-
- IO_Wakeup_Pol_e pol;
- for(uint8_t i=0;i<pin_num;i++){
- hal_gpio_pin_init(m_qdecCtx.pin_arr[i], IE);
- pol = hal_gpio_read(m_qdecCtx.pin_arr[i]) ? POSEDGE:NEGEDGE;
-
- if(pol == m_qdecCtx.pin_state[i]){
- break;
- }
- else if(i == pin_num-1){
- return;
- }
- }
-
- int32_t delta = GET_CNT_QUAN(m_qdecCtx.cfg.qdec_chn);
-
- hal_gpio_pin_init(P31, OEN);
- hal_gpio_write(P31,0);
-
- hal_pwrmgr_lock(MOD_QDEC);
- qdec_hw_config();
-
- hal_gpio_pin_init(P31, OEN);
- hal_gpio_write(P31,1);
-
- osal_start_timerEx(m_qdecCtx.qdec_task_id, m_qdecCtx.timeout_event, 150);
- }
- static void hal_qdec_set_qua_irq(QDEC_CHN_e chn, QDEC_INT_MODE_e intMode){
-
- SET_INT_MODE_QUAN(chn, intMode);
- EN_INT_QUAN(chn);
- }
- static void hal_qdec_set_inc_irq(QDEC_CHN_e chn, QDEC_INC_MODE_e incMode, QDEC_INT_MODE_e intMode){
-
- SET_MODE_INCN(chn, incMode);
- SET_INT_MODE_INCN(chn, intMode);
- EN_INT_INCN(chn);
- }
|