ASCII Serial Com
Serial communication library between computers, microcontrollers, FPGAs, etc. Uses only ASCII. Not the most efficient protocol, but meant to be easy to read
Loading...
Searching...
No Matches
millisec_timer.c
Go to the documentation of this file.
1#include "millisec_timer.h"
2#ifdef linux
3#include <time.h>
4#endif
5
6/** \file */
7
9 const millisec_timer_unit_t now,
10 const millisec_timer_unit_t rel) {
11 timer->enabled = true;
12 timer->set_time = now;
13 timer->expire_time = now + rel;
14}
15
17 const millisec_timer_unit_t now) {
18 if (timer->enabled) {
19 if ((timer->expire_time == timer->set_time) ||
20 (timer->expire_time > timer->set_time &&
21 (now >= timer->expire_time || now < timer->set_time)) ||
22 (timer->expire_time < timer->set_time && now >= timer->expire_time &&
23 now < timer->set_time)) {
24 timer->enabled = false;
25 return true;
26 }
27 }
28 return false;
29}
31 const millisec_timer_unit_t now) {
32 if (timer->enabled) {
33 if (timer->expire_time == timer->set_time) {
34 return true;
35 } else if ((timer->expire_time > timer->set_time &&
36 (now >= timer->expire_time || now < timer->set_time)) ||
37 (timer->expire_time < timer->set_time &&
38 now >= timer->expire_time && now < timer->set_time)) {
39 const millisec_timer_unit_t old_expire_time = timer->expire_time;
40 timer->expire_time =
41 old_expire_time + (old_expire_time - timer->set_time);
42 timer->set_time = old_expire_time;
43 return true;
44 }
45 }
46 return false;
47}
48
49#ifdef __ARM_ARCH
50void millisec_timer_systick_setup(uint32_t ahb_frequency) {
51 systick_set_clocksource(1 << STK_CSR_CLKSOURCE_LSB); // always AHB clock
52 STK_CVR = 0; // clear clock
53 systick_set_reload(ahb_frequency / 1000);
54 systick_counter_enable();
55 systick_interrupt_enable();
56}
57#endif
void millisec_timer_systick_setup(uint32_t ahb_frequency)
Setup the systick timer.
void millisec_timer_set_rel(millisec_timer *timer, const millisec_timer_unit_t now, const millisec_timer_unit_t rel)
Set timer to expire in the future.
bool millisec_timer_is_expired_repeat(millisec_timer *timer, const millisec_timer_unit_t now)
Check if timer has expired & if so, re-enable for the same interval.
bool millisec_timer_is_expired(millisec_timer *timer, const millisec_timer_unit_t now)
Check if timer has expired & if so, disable it.
Portable millisecond timer.
portable millisecond timer