Files CCore/inc/sys/SysTime.h CCore/src/sys/SysTime.cpp
This part provides timer functions and implements the timer interrupt processing.
/* SysTime.h */ #ifndef CCore_inc_sys_SysTime_h #define CCore_inc_sys_SysTime_h #include <CCore/inc/PlanInit.h> namespace CCore { namespace Sys { /* GetPlanInitNode_...() */ PlanInitNode * GetPlanInitNode_SysTime(); /* types */ using MSecTimeType = uint64 ; // unsigned integral type using SecTimeType = uint32 ; // unsigned integral type using ClockTimeType = uint64 ; // unsigned integral type /* consts */ const ClockTimeType ClocksPerSec = 24'000'000 ; /* functions */ MSecTimeType GetMSecTime() noexcept; SecTimeType GetSecTime() noexcept; ClockTimeType GetClockTime() noexcept; /* delay functions */ void ClockDelay(ClockTimeType clocks) noexcept; inline ClockTimeType USecToClock(unsigned usecs) { return (usecs+1)*(ClocksPerSec/1'000'000); } inline void USecDelay(unsigned usecs) { ClockDelay(USecToClock(usecs)); } } // namespace Sys } // namespace CCore #endif
Three timer functions must be implemented by the target. These timers are started/stopped by the SysTime global object. This is object is controlled by the PlanInit, the correspondent node function is GetPlanInitNode_SysTime().
GetMSecTime() is a continuous millisecond timer. The return value is represented by the unsigned integral type MSecTimeType.
GetSecTime() is a continuous second timer. The return value is represented by the unsigned integral type SecTimeType.
GetClockTime() is a continuous clock timer. The return value is represented by the unsigned integral type ClockTimeType. The constant ClocksPerSec is the clock timer frequency. This timer must be the fastest available timer on the board. Its frequency must be a several MHz at least.
ClockDelay() is a clock delay function. It may be used to delay a task execution on a small time period. The argument is a number of clocks to delay execution.
USecToClock() converts microseconds to clock numbers.
USecDelay() is another delay function, the argument is a number of microseconds to delay execution.
A typical implementation looks like:
/* SysTime.cpp */ #include <CCore/inc/sys/SysTime.h> #include <CCore/inc/Task.h> #include <CCore/inc/dev/DevPlanInit.h> namespace CCore { namespace Sys { /* GetPlanInitNode_...() */ namespace Private_SysTime { /* class StartStop */ class StartStop : NoCopy { static void Timer_int() { // TODO Task::Internal::Tick_int(); } public: StartStop() { // TODO } ~StartStop() { // TODO } static MSecTimeType GetMSecTime() noexcept { // TODO return 0; } static SecTimeType GetSecTime() noexcept { // TODO return 0; } static ClockTimeType GetClockTime() noexcept { // TODO return 0; } static const char * GetTag() { return "SysTime"; } }; PlanInitObject<StartStop,PlanInitReq<Dev::GetPlanInitNode_Dev> > Object CCORE_INITPRI_1 ; } // namespace Private_SysTime using namespace Private_SysTime; PlanInitNode * GetPlanInitNode_SysTime() { return &Object; } /* functions */ MSecTimeType GetMSecTime() noexcept { return StartStop::GetMSecTime(); } SecTimeType GetSecTime() noexcept { return StartStop::GetSecTime(); } ClockTimeType GetClockTime() noexcept { return StartStop::GetClockTime(); } void ClockDelay(ClockTimeType clocks) noexcept { for(auto start=GetClockTime(); GetClockTime()-start<=clocks ;); } } // namespace Sys } // namespace CCore
StartStop constructor starts board timers and connects the timer interrupt function. StartStop stops timers.
StartStop::GetMSecTime() returns the millisecond timer counter.
StartStop::GetSecTime() returns the second timer counter.
StartStop::GetMSecTime() returns the clock timer counter.
Timer_int() is a timer interrupt handler. Among other tasks it must call the internal XCore function Task::Internal::Tick_int() with a proper period to drive the tick processing.