From 5c905d4ee30e1897879599b36694ffa7d053e648 Mon Sep 17 00:00:00 2001 From: zhaohe Date: Sat, 26 Aug 2023 10:24:01 +0800 Subject: [PATCH] udpate --- chip/basic/basic_h/marco.h | 9 +++- chip/basic/chip_helper.cpp | 63 ++++++++++++++++++++++++++ chip/basic/chip_helper.hpp | 4 +- chip/basic/clock.cpp | 3 ++ chip/chip.cpp | 10 +++++ chip/chip.hpp | 9 ++-- chip/zgpio.cpp | 2 +- components/key_monitor/zkey_service.cpp | 79 +++++++++++++++++++++++++++++++++ components/key_monitor/zkey_service.hpp | 65 +++++++++++++++++++++++++++ os/os_default_schduler.cpp | 2 - os/os_default_schduler.hpp | 2 +- 11 files changed, 238 insertions(+), 10 deletions(-) create mode 100644 components/key_monitor/zkey_service.cpp create mode 100644 components/key_monitor/zkey_service.hpp diff --git a/chip/basic/basic_h/marco.h b/chip/basic/basic_h/marco.h index d5b9d78..5da9d0f 100644 --- a/chip/basic/basic_h/marco.h +++ b/chip/basic/basic_h/marco.h @@ -42,4 +42,11 @@ #endif #ifndef UINT64_MAX #define UINT64_MAX 0xffffffffffffffffULL /* 18446744073709551615ULL */ -#endif \ No newline at end of file +#endif + +#define EARLY_ASSERT(x) \ + while (!(x)) { \ + ; \ + } + +// diff --git a/chip/basic/chip_helper.cpp b/chip/basic/chip_helper.cpp index 0d1db50..ae97819 100644 --- a/chip/basic/chip_helper.cpp +++ b/chip/basic/chip_helper.cpp @@ -102,6 +102,69 @@ uint16_t chip_get_pinoff(Pin_t pin) { return 0; } +// // no tim1 +// TIM2_IRQn +// TIM3_IRQn +// TIM4_IRQn +// TIM5_IRQn +// TIM6_DAC_IRQn +// TIM7_IRQn +// //no tim8 +// TIM1_BRK_TIM9_IRQn +// TIM1_UP_TIM10_IRQn +// TIM1_TRG_COM_TIM11_IRQn +// TIM8_BRK_TIM12_IRQn +// TIM8_UP_TIM13_IRQn +// TIM8_TRG_COM_TIM14_IRQn + +IRQn_Type chip_tim_get_irq(TIM_HandleTypeDef* tim) { + if (tim->Instance == TIM1) { + EARLY_ASSERT(false); + } + if (tim->Instance == TIM2) { + return TIM2_IRQn; + } + if (tim->Instance == TIM3) { + return TIM3_IRQn; + } + if (tim->Instance == TIM4) { + return TIM4_IRQn; + } + if (tim->Instance == TIM5) { + return TIM5_IRQn; + } + if (tim->Instance == TIM6) { + return TIM6_DAC_IRQn; + } + if (tim->Instance == TIM7) { + return TIM7_IRQn; + } + if (tim->Instance == TIM8) { + EARLY_ASSERT(false); + } + if (tim->Instance == TIM9) { + return TIM1_BRK_TIM9_IRQn; + } + if (tim->Instance == TIM10) { + return TIM1_UP_TIM10_IRQn; + } + if (tim->Instance == TIM11) { + return TIM1_TRG_COM_TIM11_IRQn; + } + if (tim->Instance == TIM12) { + return TIM8_BRK_TIM12_IRQn; + } + if (tim->Instance == TIM13) { + return TIM8_UP_TIM13_IRQn; + } + if (tim->Instance == TIM14) { + return TIM8_TRG_COM_TIM14_IRQn; + } + // + EARLY_ASSERT(false); + return (IRQn_Type)0; +} + uint32_t chip_get_timer_clock_sorce_freq(TIM_HandleTypeDef* tim) { uint32_t timClkFreq = 0; uint32_t pclk1Freq = HAL_RCC_GetPCLK1Freq(); diff --git a/chip/basic/chip_helper.hpp b/chip/basic/chip_helper.hpp index aed5e64..b82f08a 100644 --- a/chip/basic/chip_helper.hpp +++ b/chip/basic/chip_helper.hpp @@ -13,5 +13,7 @@ void chip_critical_enter(void); void chip_critical_exit(void); uint32_t chip_get_timer_clock_sorce_freq(TIM_HandleTypeDef* tim); -bool chip_calculate_prescaler_and_autoreload_by_expect_freq(uint32_t timerInClk, float infreqhz, uint32_t* prescaler, uint32_t* autoreload); +bool chip_calculate_prescaler_and_autoreload_by_expect_freq(uint32_t timerInClk, float infreqhz, uint32_t* prescaler, uint32_t* autoreload); + +IRQn_Type chip_tim_get_irq(TIM_HandleTypeDef* tim); } \ No newline at end of file diff --git a/chip/basic/clock.cpp b/chip/basic/clock.cpp index 452294e..02c7969 100644 --- a/chip/basic/clock.cpp +++ b/chip/basic/clock.cpp @@ -20,6 +20,9 @@ uint32_t zchip_clock_init(zchip_clock_cfg_t* cfg) { cfg->usdleaytim->Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE; HAL_TIM_Base_Init(cfg->usdleaytim); + HAL_NVIC_SetPriority(chip_tim_get_irq(cfg->usdleaytim), IFLYTOP_PREEMPTPRIORITY_DEFAULT, 0); + HAL_NVIC_EnableIRQ(chip_tim_get_irq(cfg->usdleaytim)); + return 0; } // diff --git a/chip/chip.cpp b/chip/chip.cpp index 3d41b0b..7881f69 100644 --- a/chip/chip.cpp +++ b/chip/chip.cpp @@ -1,7 +1,10 @@ #include "chip.hpp" extern "C" { +static iflytop::ZGPIO *g_debuglight = NULL; + void chip_init(chip_cfg_t *cfg) { + g_debuglight = cfg->debuglight; // zchip_loggger_init(cfg->huart); // @@ -13,6 +16,13 @@ void chip_init(chip_cfg_t *cfg) { ChipTimIrqShceduler_cfg.schedulertim = cfg->tim_irq_scheduler_tim; iflytop::ChipTimIrqShceduler::instance().initialize(&ChipTimIrqShceduler_cfg); + iflytop::ChipTimIrqShceduler::instance().regPeriodJob( + [](iflytop::ChipTimIrqShceduler::Job *job) { + if (!g_debuglight) return; + g_debuglight->toggleState(); + }, + 100); + ZLOGI("SYS", "chip init ok"); ZLOGI("SYS", "= manufacturer : %s", MANUFACTURER); ZLOGI("SYS", "= project name : %s", PROJECT_NAME); diff --git a/chip/chip.hpp b/chip/chip.hpp index 762c347..2d2c85a 100644 --- a/chip/chip.hpp +++ b/chip/chip.hpp @@ -1,18 +1,19 @@ #pragma once #include "chip_tim_irq_shceduler.hpp" -#include "zirq_dispatcher.hpp" #include "zcan_irq_dispatcher.hpp" #include "zgpio.hpp" +#include "zirq_dispatcher.hpp" #include "ztim.hpp" #include "zuart.hpp" extern "C" { typedef struct { - zchip_tim_t *us_dleay_tim; - zchip_tim_t *tim_irq_scheduler_tim; - zchip_uart_t *huart; + zchip_tim_t *us_dleay_tim; + zchip_tim_t *tim_irq_scheduler_tim; + zchip_uart_t *huart; + iflytop::ZGPIO *debuglight; } chip_cfg_t; void chip_init(chip_cfg_t *cfg); diff --git a/chip/zgpio.cpp b/chip/zgpio.cpp index 90480a6..56f393d 100644 --- a/chip/zgpio.cpp +++ b/chip/zgpio.cpp @@ -172,7 +172,7 @@ void ZGPIO::initAsInput(Pin_t pin, GPIOMode_t mode, GPIOIrqType_t irqtype, bool if (m_irqtype != kIRQ_noIrq) { regIRQGPIO(this); lastLevel = getState(); - HAL_NVIC_SetPriority(getEXTIIRQn(), 0, 0); + HAL_NVIC_SetPriority(getEXTIIRQn(), IFLYTOP_PREEMPTPRIORITY_DEFAULT, 0); HAL_NVIC_EnableIRQ(getEXTIIRQn()); } return; diff --git a/components/key_monitor/zkey_service.cpp b/components/key_monitor/zkey_service.cpp new file mode 100644 index 0000000..5bcd173 --- /dev/null +++ b/components/key_monitor/zkey_service.cpp @@ -0,0 +1,79 @@ +#include "zkey_service.hpp" +using namespace iflytop; +#define TAG "ZKeyService" +/******************************************************************************* + * KEY * + *******************************************************************************/ +ZKey::ZKey(int id, Pin_t pin, ZGPIO::GPIOMode_t mode, ZGPIO::GPIOIrqType_t irqtype, bool mirror) { + this->id = id; + this->pin = pin; + this->mode = mode; + this->irqtype = irqtype; + this->mirror = mirror; +} +ZKey::~ZKey() {} + +void ZKey::init() { gpio.initAsInput(pin, mode, irqtype, mirror); } +void ZKey::setHasProcessed(bool hasProcessed) { this->hasProcessed = hasProcessed; } +bool ZKey::isHasProcessed() { return hasProcessed; } +int ZKey::getId() { return id; } + +/******************************************************************************* + * ZKeyService * + *******************************************************************************/ + +void ZKeyService::initialize(ZKey* keys, int numKey, KeyListener_t listener) { + m_keys = keys; + ZASSERT(m_keys != NULL); + this->m_numKey = numKey; + m_listener = listener; + m_lastcallticket = 0; + + for (int i = 0; i < m_numKey; i++) { + m_keys[i].init(); + } +} + +void ZKeyService::processEachAfterFilter(ZKey* each, bool now_io_state) { + if (now_io_state != each->last_after_filter_io_state) { + if (now_io_state) { + each->keep_state_count = 0; + each->hasProcessed = false; + m_listener(each, zke_rising_edge); + } else { + m_listener(each, zke_falling_edge); + each->keep_state_count = 0; + } + each->last_after_filter_io_state = now_io_state; + } else { + if (now_io_state) { + m_listener(each, zke_keep); + } + } +} + +void ZKeyService::processKey(ZKey* each) { + /** + * @brief zkey_process_each + */ + each->keep_state_count++; + + bool now_io_state = each->gpio.getState(); + if (each->currentstatekeep_count < 1000) { + each->currentstatekeep_count++; + } + if (now_io_state != each->last_io_state) { + each->currentstatekeep_count = 0; + } + if (each->currentstatekeep_count >= 1) { + each->after_filter_state = now_io_state; + } + each->last_io_state = now_io_state; + processEachAfterFilter(each, each->after_filter_state); +} + +void ZKeyService::periodicJob() { + for (int i = 0; i < m_numKey; i++) { + processKey(&m_keys[i]); + } +} diff --git a/components/key_monitor/zkey_service.hpp b/components/key_monitor/zkey_service.hpp new file mode 100644 index 0000000..76e1d39 --- /dev/null +++ b/components/key_monitor/zkey_service.hpp @@ -0,0 +1,65 @@ +#pragma once +#include + +#include "sdk/os/zos.hpp" +namespace iflytop { +using namespace std; +class ZKeyService; + +class ZKey { + private: + /* data */ + int id; + Pin_t pin; + ZGPIO::GPIOMode_t mode; + ZGPIO::GPIOIrqType_t irqtype; + bool mirror; + + ZGPIO gpio; + + bool last_io_state; + bool last_after_filter_io_state; + uint32_t keep_state_count; + bool after_filter_state; + uint32_t currentstatekeep_count; + + bool hasProcessed; /*useful for user*/ + public: + friend class ZKeyService; + ZKey(int id, Pin_t pin, ZGPIO::GPIOMode_t mode, ZGPIO::GPIOIrqType_t irqtype, bool mirror); + ~ZKey(); + + void init(); + + void setHasProcessed(bool hasProcessed); + bool isHasProcessed(); + int getId(); +}; + +class ZKeyService { + public: + typedef enum { + zke_keep, + zke_rising_edge, + zke_falling_edge, + } KeyEvent_t; + + typedef function KeyListener_t; + + private: + /* data */ + ZKey* m_keys; + int m_numKey; + KeyListener_t m_listener; + uint32_t m_lastcallticket; + + public: + void initialize(ZKey* keys, int numKey, KeyListener_t listener); + void periodicJob(); + + private: + void processKey(ZKey* each); + void processEachAfterFilter(ZKey* each, bool now_io_state); +}; + +} // namespace iflytop \ No newline at end of file diff --git a/os/os_default_schduler.cpp b/os/os_default_schduler.cpp index a79c7f7..cbfc0f1 100644 --- a/os/os_default_schduler.cpp +++ b/os/os_default_schduler.cpp @@ -34,5 +34,3 @@ void OSDefaultSchduler::loop() { periodJob->schedule_times++; } } - -void OSDefaultSchduler::initialize() {} diff --git a/os/os_default_schduler.hpp b/os/os_default_schduler.hpp index 74fbbc0..58a6cc6 100644 --- a/os/os_default_schduler.hpp +++ b/os/os_default_schduler.hpp @@ -43,7 +43,7 @@ class OSDefaultSchduler { public: static OSDefaultSchduler* getInstance(); - void initialize(); + void initialize(){}; void regPeriodJob(function job, uint32_t period_ms); void loop();