From 52c4ebb0036fcce8f41ad4ce85e1e62101d98048 Mon Sep 17 00:00:00 2001 From: Vasily Davydov Date: Fri, 14 Oct 2022 14:20:00 +0300 Subject: [PATCH] timer: add Timer class Timer class consists of systack_handler functionality with an internal timer for global counter in main() --- Timer/.cproject | 242 ++++++++++++++++++++++++++++++++++++++++++++ Timer/.project | 28 +++++ Timer/inc/Timer.h | 75 ++++++++++++++ Timer/liblinks.xml | 32 ++++++ Timer/src/Timer.cpp | 64 ++++++++++++ 5 files changed, 441 insertions(+) create mode 100644 Timer/.cproject create mode 100644 Timer/.project create mode 100644 Timer/inc/Timer.h create mode 100644 Timer/liblinks.xml create mode 100644 Timer/src/Timer.cpp diff --git a/Timer/.cproject b/Timer/.cproject new file mode 100644 index 0000000..8855f43 --- /dev/null +++ b/Timer/.cproject @@ -0,0 +1,242 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + <?xml version="1.0" encoding="UTF-8"?> +<TargetConfig> +<Properties property_2="LPC15xx_256K.cfx" property_3="NXP" property_4="LPC1549" property_count="5" version="100300"/> +<infoList vendor="NXP"> +<info chip="LPC1549" connectscript="LPC15RunBootRomConnect.scp" flash_driver="LPC15xx_256K.cfx" match_id="0x0" name="LPC1549" resetscript="LPC15RunBootRomReset.scp" stub="crt_emu_cm3_gen"> +<chip> +<name>LPC1549</name> +<family>LPC15xx</family> +<vendor>NXP (formerly Philips)</vendor> +<reset board="None" core="Real" sys="Real"/> +<clock changeable="TRUE" freq="12MHz" is_accurate="TRUE"/> +<memory can_program="true" id="Flash" is_ro="true" type="Flash"/> +<memory id="RAM" type="RAM"/> +<memory id="Periph" is_volatile="true" type="Peripheral"/> +<memoryInstance derived_from="Flash" id="MFlash256" location="0x0" size="0x40000"/> +<memoryInstance derived_from="RAM" id="Ram0_16" location="0x2000000" size="0x4000"/> +<memoryInstance derived_from="RAM" id="Ram1_16" location="0x2004000" size="0x4000"/> +<memoryInstance derived_from="RAM" id="Ram2_4" location="0x2008000" size="0x1000"/> +</chip> +<processor> +<name gcc_name="cortex-m3">Cortex-M3</name> +<family>Cortex-M</family> +</processor> +</info> +</infoList> +</TargetConfig> + + + LPCXpresso1549 + + \ No newline at end of file diff --git a/Timer/.project b/Timer/.project new file mode 100644 index 0000000..db5b0e9 --- /dev/null +++ b/Timer/.project @@ -0,0 +1,28 @@ + + + Timer + + + lpc_chip_15xx + + + + org.eclipse.cdt.managedbuilder.core.genmakebuilder + clean,full,incremental, + + + + + org.eclipse.cdt.managedbuilder.core.ScannerConfigBuilder + full,incremental, + + + + + + org.eclipse.cdt.core.cnature + org.eclipse.cdt.core.ccnature + org.eclipse.cdt.managedbuilder.core.managedBuildNature + org.eclipse.cdt.managedbuilder.core.ScannerConfigNature + + diff --git a/Timer/inc/Timer.h b/Timer/inc/Timer.h new file mode 100644 index 0000000..6c0981f --- /dev/null +++ b/Timer/inc/Timer.h @@ -0,0 +1,75 @@ +/* + * Timer.h + * + * Created on: Oct 14, 2022 + * Author: tylen + */ + +#ifndef TIMER_H_ +#define TIMER_H_ + +#include "board.h" +#include +#include + +extern "C" +{ + /** + * @brief Handle interrupt from SysTick timer + * @return Nothing + */ + void SysTick_Handler (void); +} + +class Timer +{ +public: + /** + * @brief Initalize the systick configuration with your frequency + * + */ + Timer (uint32_t freq = 1000); + virtual ~Timer (); + + /** + * @brief Tick the counter. + * + * Counter is incremented by one every tick, + * if it gets over the INT_MAX (see limits.h), + * the counter rolls up back to zero and starts + * over. + * + * + * @param ms Counter ticking frequency is provided in milliseconds + */ + void tickCounter (int ms); + + /** + * @brief Get the current counter value + * + * @return int counter value + */ + int getCounter (); + + /** + * @brief Set counter to 0. + * + */ + void resetCounter (); + + /** + * @brief Sleep for amount of time + * + * Time is either in ms or in sec, defined + * by systickInit_xx() + * + * @param ms milliseconds + */ + void Sleep (int ms); + +private: + volatile std::atomic_int counter; + volatile std::atomic_int timer; +}; + +#endif /* TIMER_H_ */ diff --git a/Timer/liblinks.xml b/Timer/liblinks.xml new file mode 100644 index 0000000..1dc1803 --- /dev/null +++ b/Timer/liblinks.xml @@ -0,0 +1,32 @@ + + + + + ${MacroStart}workspace_loc:/${ProjName}/inc${MacroEnd} + + + ${MacroStart}workspace_loc:/${ProjName}/inc${MacroEnd} + + + ${ProjName} + + + ${MacroStart}workspace_loc:/${ProjName}/Debug${MacroEnd} + + + ${MacroStart}workspace_loc:/${ProjName}/Release${MacroEnd} + + + ${ProjName} + + + diff --git a/Timer/src/Timer.cpp b/Timer/src/Timer.cpp new file mode 100644 index 0000000..f9ea64c --- /dev/null +++ b/Timer/src/Timer.cpp @@ -0,0 +1,64 @@ +/* + * Timer.cpp + * + * Created on: Oct 14, 2022 + * Author: tylen + */ + +#include + +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; +}