timer: add Timer class
Timer class consists of systack_handler functionality with an internal timer for global counter in main()
This commit is contained in:
64
Timer/src/Timer.cpp
Normal file
64
Timer/src/Timer.cpp
Normal file
@@ -0,0 +1,64 @@
|
||||
/*
|
||||
* Timer.cpp
|
||||
*
|
||||
* Created on: Oct 14, 2022
|
||||
* Author: tylen
|
||||
*/
|
||||
|
||||
#include <Timer.h>
|
||||
|
||||
extern "C"
|
||||
{
|
||||
void
|
||||
SysTick_Handler (void)
|
||||
{
|
||||
if (timer > 0)
|
||||
timer--;
|
||||
}
|
||||
}
|
||||
|
||||
Timer::Timer (uint32_t freq = 1000)
|
||||
{
|
||||
Chip_Clock_SetSysTickClockDiv (1);
|
||||
uint32_t sysTickRate = Chip_Clock_GetSysTickClockRate ();
|
||||
SysTick_Config (sysTickRate / freq);
|
||||
counter = 0;
|
||||
}
|
||||
|
||||
Timer::~Timer ()
|
||||
{
|
||||
// TODO Auto-generated destructor stub
|
||||
}
|
||||
|
||||
void
|
||||
Timer::tickCounter (int ms)
|
||||
{
|
||||
if (counter >= INT_MAX)
|
||||
{
|
||||
counter = 0;
|
||||
}
|
||||
counter++;
|
||||
Sleep (ms);
|
||||
}
|
||||
|
||||
void
|
||||
Timer::Sleep (int ms)
|
||||
{
|
||||
timer = ms;
|
||||
while (timer > 0)
|
||||
{
|
||||
__WFI ();
|
||||
}
|
||||
}
|
||||
|
||||
int
|
||||
Timer::getCounter ()
|
||||
{
|
||||
return counter;
|
||||
}
|
||||
|
||||
void
|
||||
Timer::resetCounter ()
|
||||
{
|
||||
counter = 0;
|
||||
}
|
||||
Reference in New Issue
Block a user