diff --git a/chip/delay.c b/chip/delay.c index ec2257b..6d10c2c 100644 --- a/chip/delay.c +++ b/chip/delay.c @@ -36,3 +36,10 @@ uint32_t haspassedms(uint32_t ticket) { } return UINT32_MAX - ticket + nowticket; } + +uint32_t haspassedms2(uint32_t lastticket, uint32_t nowticket) { + if (nowticket >= lastticket) { + return nowticket - lastticket; + } + return UINT32_MAX - lastticket + nowticket; +} diff --git a/chip/delay.h b/chip/delay.h index 228a806..19db76a 100644 --- a/chip/delay.h +++ b/chip/delay.h @@ -12,6 +12,8 @@ void chip_delay_ms(uint32_t n); void ifly_delay_ms(uint32_t n); uint32_t chip_get_ticket(); +uint32_t haspassedms(uint32_t ticket); +uint32_t haspassedms2(uint32_t lastticket, uint32_t nowticket); #ifdef __cplusplus } diff --git a/hal/zhal.hpp b/hal/zhal.hpp new file mode 100644 index 0000000..15e86bc --- /dev/null +++ b/hal/zhal.hpp @@ -0,0 +1,4 @@ +#pragma once +#include "../chip/iflytop_no_os.h" +#include "gpio.hpp" +#include "zhal_core.hpp" diff --git a/hal/zhal_core.cpp b/hal/zhal_core.cpp new file mode 100644 index 0000000..56e4774 --- /dev/null +++ b/hal/zhal_core.cpp @@ -0,0 +1,33 @@ +#include "zhal_core.hpp" +using namespace std; +using namespace iflytop; +namespace iflytop { +ZHALCORE ZHALCORE_instance; +} + +void ZHALCORE::regPeriodJob(function job, uint32_t period_ms) { + PeriodJob* periodJob = new PeriodJob(job, period_ms); + ZASSERT(periodJob != NULL); + m_periodJobs.push_back(periodJob); +} + +void ZHALCORE::loop() { + static uint32_t ticket = 0; + Context context; + if (haspassedms(ticket) < 1) { + return; + } + ticket = chip_get_ticket(); + for (auto iter = m_periodJobs.begin(); iter != m_periodJobs.end(); iter++) { + auto periodJob = *iter; + + if (haspassedms2(periodJob->lastcall, ticket) < periodJob->period_ms) { + continue; + } + + context.periodJob = periodJob; + periodJob->lastcall = ticket; + periodJob->job(context); + periodJob->schedule_times++; + } +} diff --git a/hal/zhal_core.hpp b/hal/zhal_core.hpp new file mode 100644 index 0000000..e7638d0 --- /dev/null +++ b/hal/zhal_core.hpp @@ -0,0 +1,52 @@ +#pragma once +#include + +#include +#include + +#include "../chip/iflytop_no_os.h" +// +namespace iflytop { +using namespace std; +class ZHALCORE; +extern ZHALCORE ZHALCORE_instance; + +class ZHALCORE { + public: + class PeriodJob; + class Context { + friend class ZHALCORE; + + protected: + PeriodJob* periodJob; + + public: + }; + + class PeriodJob { + public: + PeriodJob(function job, uint32_t period_ms) { + this->job = job; + this->period_ms = period_ms; + } + function job; + uint32_t period_ms = 0; + uint32_t lastcall = 0; + uint32_t schedule_times = 0; + }; + + list m_periodJobs; + + public: + ZHALCORE() {} + static ZHALCORE* getInstance() { return &ZHALCORE_instance; } + + void initialize(){}; + + void regPeriodJob(function job, uint32_t period_ms); + void loop(); +}; + +#define ZHAL_CORE_REG(period_ms, job) ZHALCORE::getInstance()->regPeriodJob([this](ZHALCORE::Context& context) { job }, period_ms); + +} // namespace iflytop \ No newline at end of file