Embedded Template Library 1.0
Loading...
Searching...
No Matches
callback_timer_interrupt.h
1 /******************************************************************************
2The MIT License(MIT)
3
4Embedded Template Library.
5https://github.com/ETLCPP/etl
6https://www.etlcpp.com
7
8Copyright(c) 2022 John Wellbelove
9
10Permission is hereby granted, free of charge, to any person obtaining a copy
11of this software and associated documentation files(the "Software"), to deal
12in the Software without restriction, including without limitation the rights
13to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
14copies of the Software, and to permit persons to whom the Software is
15furnished to do so, subject to the following conditions :
16
17The above copyright notice and this permission notice shall be included in all
18copies or substantial portions of the Software.
19
20THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
23AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26SOFTWARE.
27******************************************************************************/
28
29#ifndef ETL_CALLBACK_TIMER_INTERRUPT_INCLUDED
30#define ETL_CALLBACK_TIMER_INTERRUPT_INCLUDED
31
32#include "platform.h"
33#include "algorithm.h"
34#include "nullptr.h"
35#include "delegate.h"
36#include "static_assert.h"
37#include "timer.h"
38#include "error_handler.h"
39#include "placement_new.h"
40
41#include <stdint.h>
42
43namespace etl
44{
45 //***************************************************************************
47 //***************************************************************************
48 template <typename TInterruptGuard>
50 {
51 public:
52
53 typedef etl::delegate<void(void)> callback_type;
54
55 typedef etl::delegate<void(etl::timer::id::type)> event_callback_type;
56
57 //*******************************************
59 //*******************************************
60 etl::timer::id::type register_timer(const callback_type& callback_,
61 uint32_t period_,
62 bool repeating_)
63 {
64 etl::timer::id::type id = etl::timer::id::NO_TIMER;
65
66 bool is_space = (number_of_registered_timers < Max_Timers);
67
68 if (is_space)
69 {
70 // Search for the free space.
71 for (uint_least8_t i = 0U; i < Max_Timers; ++i)
72 {
73 timer_data& timer = timer_array[i];
74
75 if (timer.id == etl::timer::id::NO_TIMER)
76 {
77 TInterruptGuard guard;
78 (void)guard; // Silence 'unused variable warnings.
79
80 // Create in-place.
81 new (&timer) timer_data(i, callback_, period_, repeating_);
82 ++number_of_registered_timers;
83 id = i;
84 break;
85 }
86 }
87 }
88
89 return id;
90 }
91
92 //*******************************************
94 //*******************************************
95 bool unregister_timer(etl::timer::id::type id_)
96 {
97 bool result = false;
98
99 if (id_ != etl::timer::id::NO_TIMER)
100 {
101 timer_data& timer = timer_array[id_];
102
103 if (timer.id != etl::timer::id::NO_TIMER)
104 {
105 if (timer.is_active())
106 {
107 TInterruptGuard guard;
108 (void)guard; // Silence 'unused variable warnings.
109
110 active_list.remove(timer.id, false);
111 remove_callback.call_if(timer.id);
112 }
113
114 // Reset in-place.
115 new (&timer) timer_data();
116 --number_of_registered_timers;
117
118 result = true;
119 }
120 }
121
122 return result;
123 }
124
125 //*******************************************
127 //*******************************************
128 void enable(bool state_)
129 {
130 enabled = state_;
131 }
132
133 //*******************************************
135 //*******************************************
136 bool is_running() const
137 {
138 return enabled;
139 }
140
141 //*******************************************
143 //*******************************************
144 void clear()
145 {
146 {
147 TInterruptGuard guard;
148 (void)guard; // Silence 'unused variable warnings.
149
150 active_list.clear();
151 number_of_registered_timers = 0;
152 }
153
154 for (uint8_t i = 0U; i < Max_Timers; ++i)
155 {
156 ::new (&timer_array[i]) timer_data();
157 }
158 }
159
160 //*******************************************
161 // Called by the timer service to indicate the
162 // amount of time that has elapsed since the last successful call to 'tick'.
163 // Returns true if the tick was processed,
164 // false if not.
165 //*******************************************
166 bool tick(uint32_t count)
167 {
168 if (enabled)
169 {
170 // We have something to do?
171 bool has_active = !active_list.empty();
172
173 if (has_active)
174 {
175 while (has_active && (count >= active_list.front().delta))
176 {
177 timer_data& timer = active_list.front();
178
179 count -= timer.delta;
180
181 active_list.remove(timer.id, true);
182 remove_callback.call_if(timer.id);
183
184 if (timer.callback.is_valid())
185 {
186 timer.callback();
187 }
188
189 if (timer.repeating)
190 {
191 // Reinsert the timer.
192 timer.delta = timer.period;
193 active_list.insert(timer.id);
194 insert_callback.call_if(timer.id);
195 }
196
197 has_active = !active_list.empty();
198 }
199
200 if (has_active)
201 {
202 // Subtract any remainder from the next due timeout.
203 active_list.front().delta -= count;
204 }
205 }
206
207 return true;
208 }
209
210 return false;
211 }
212
213 //*******************************************
215 //*******************************************
216 bool start(etl::timer::id::type id_, bool immediate_ = false)
217 {
218 bool result = false;
219
220 // Valid timer id?
221 if (id_ != etl::timer::id::NO_TIMER)
222 {
223 timer_data& timer = timer_array[id_];
224
225 // Registered timer?
226 if (timer.id != etl::timer::id::NO_TIMER)
227 {
228 // Has a valid period.
229 if (timer.period != etl::timer::state::Inactive)
230 {
231 TInterruptGuard guard;
232 (void)guard; // Silence 'unused variable warnings.
233
234 if (timer.is_active())
235 {
236 active_list.remove(timer.id, false);
237 remove_callback.call_if(timer.id);
238 }
239
240 timer.delta = immediate_ ? 0U : timer.period;
241 active_list.insert(timer.id);
242 insert_callback.call_if(timer.id);
243
244 result = true;
245 }
246 }
247 }
248
249 return result;
250 }
251
252 //*******************************************
254 //*******************************************
255 bool stop(etl::timer::id::type id_)
256 {
257 bool result = false;
258
259 // Valid timer id?
260 if (id_ != etl::timer::id::NO_TIMER)
261 {
262 timer_data& timer = timer_array[id_];
263
264 // Registered timer?
265 if (timer.id != etl::timer::id::NO_TIMER)
266 {
267 if (timer.is_active())
268 {
269 TInterruptGuard guard;
270 (void)guard; // Silence 'unused variable warnings.
271
272 active_list.remove(timer.id, false);
273 remove_callback.call_if(timer.id);
274 }
275
276 result = true;
277 }
278 }
279
280 return result;
281 }
282
283 //*******************************************
285 //*******************************************
286 bool set_period(etl::timer::id::type id_, uint32_t period_)
287 {
288 if (stop(id_))
289 {
290 timer_array[id_].period = period_;
291 return true;
292 }
293
294 return false;
295 }
296
297 //*******************************************
299 //*******************************************
300 bool set_mode(etl::timer::id::type id_, bool repeating_)
301 {
302 if (stop(id_))
303 {
304 timer_array[id_].repeating = repeating_;
305 return true;
306 }
307
308 return false;
309 }
310
311 //*******************************************
313 //*******************************************
314 bool has_active_timer() const
315 {
316 TInterruptGuard guard;
317 (void)guard; // Silence 'unused variable warnings.
318 return !active_list.empty();
319 }
320
321 //*******************************************
324 //*******************************************
325 uint32_t time_to_next() const
326 {
327 uint32_t delta = static_cast<uint32_t>(etl::timer::interval::No_Active_Interval);
328
329 TInterruptGuard guard;
330 (void)guard; // Silence 'unused variable warnings.
331
332 if (!active_list.empty())
333 {
334 delta = active_list.front().delta;
335 }
336
337 return delta;
338 }
339
340 //*******************************************
343 //*******************************************
344 bool is_active(etl::timer::id::type id_) const
345 {
346 // Valid timer id?
347 if (is_valid_timer_id(id_))
348 {
349 if (has_active_timer())
350 {
351 TInterruptGuard guard;
352 (void)guard; // Silence 'unused variable warnings.
353
354 const timer_data& timer = timer_array[id_];
355
356 // Registered timer?
357 if (timer.id != etl::timer::id::NO_TIMER)
358 {
359 return timer.is_active();
360 }
361 }
362 }
363
364 return false;
365 }
366
367 //*******************************************
369 //*******************************************
370 void set_insert_callback(event_callback_type insert_)
371 {
372 insert_callback = insert_;
373 }
374
375 //*******************************************
377 //*******************************************
378 void set_remove_callback(event_callback_type remove_)
379 {
380 remove_callback = remove_;
381 }
382
383 //*******************************************
384 void clear_insert_callback()
385 {
386 insert_callback.clear();
387 }
388
389 //*******************************************
390 void clear_remove_callback()
391 {
392 remove_callback.clear();
393 }
394
395 protected:
396
397 //*************************************************************************
399 struct timer_data
400 {
401 //*******************************************
402 timer_data()
403 : callback()
404 , period(0U)
405 , delta(etl::timer::state::Inactive)
406 , id(etl::timer::id::NO_TIMER)
407 , previous(etl::timer::id::NO_TIMER)
408 , next(etl::timer::id::NO_TIMER)
409 , repeating(true)
410 {
411 }
412
413 //*******************************************
415 //*******************************************
416 timer_data(etl::timer::id::type id_,
417 callback_type callback_,
418 uint32_t period_,
419 bool repeating_)
420 : callback(callback_)
421 , period(period_)
422 , delta(etl::timer::state::Inactive)
423 , id(id_)
424 , previous(etl::timer::id::NO_TIMER)
425 , next(etl::timer::id::NO_TIMER)
426 , repeating(repeating_)
427 {
428 }
429
430 //*******************************************
432 //*******************************************
433 bool is_active() const
434 {
435 return delta != etl::timer::state::Inactive;
436 }
437
438 //*******************************************
440 //*******************************************
442 {
443 delta = etl::timer::state::Inactive;
444 }
445
446 callback_type callback;
447 uint32_t period;
448 uint32_t delta;
449 etl::timer::id::type id;
450 uint_least8_t previous;
451 uint_least8_t next;
452 bool repeating;
453
454 private:
455
456 // Disabled.
457 timer_data(const timer_data& other) ETL_DELETE;
458 timer_data& operator =(const timer_data& other) ETL_DELETE;
459 };
460
461 //*******************************************
463 //*******************************************
464 icallback_timer_interrupt(timer_data* const timer_array_, const uint_least8_t Max_Timers_)
465 : timer_array(timer_array_)
466 , active_list(timer_array_)
467 , enabled(false)
468 , number_of_registered_timers(0U)
469 , Max_Timers(Max_Timers_)
470 {
471 }
472
473 private:
474
475 //*******************************************
477 //*******************************************
478 bool is_valid_timer_id(etl::timer::id::type id_) const
479 {
480 return (id_ < Max_Timers);
481 }
482
483 //*************************************************************************
485 //*************************************************************************
486 class timer_list
487 {
488 public:
489
490 //*******************************
491 timer_list(timer_data* ptimers_)
492 : head(etl::timer::id::NO_TIMER)
493 , tail(etl::timer::id::NO_TIMER)
494 , current(etl::timer::id::NO_TIMER)
495 , ptimers(ptimers_)
496 {
497 }
498
499 //*******************************
500 bool empty() const
501 {
502 return head == etl::timer::id::NO_TIMER;
503 }
504
505 //*******************************
506 // Inserts the timer at the correct delta position
507 //*******************************
508 void insert(etl::timer::id::type id_)
509 {
510 timer_data& timer = ptimers[id_];
511
512 if (head == etl::timer::id::NO_TIMER)
513 {
514 // No entries yet.
515 head = id_;
516 tail = id_;
517 timer.previous = etl::timer::id::NO_TIMER;
518 timer.next = etl::timer::id::NO_TIMER;
519 }
520 else
521 {
522 // We already have entries.
523 etl::timer::id::type test_id = begin();
524
525 while (test_id != etl::timer::id::NO_TIMER)
526 {
527 timer_data& test = ptimers[test_id];
528
529 // Find the correct place to insert.
530 if (timer.delta <= test.delta)
531 {
532 if (test.id == head)
533 {
534 head = timer.id;
535 }
536
537 // Insert before test.
538 timer.previous = test.previous;
539 test.previous = timer.id;
540 timer.next = test.id;
541
542 // Adjust the next delta to compensate.
543 test.delta -= timer.delta;
544
545 if (timer.previous != etl::timer::id::NO_TIMER)
546 {
547 ptimers[timer.previous].next = timer.id;
548 }
549 break;
550 }
551 else
552 {
553 timer.delta -= test.delta;
554 }
555
556 test_id = next(test_id);
557 }
558
559 // Reached the end?
560 if (test_id == etl::timer::id::NO_TIMER)
561 {
562 // Tag on to the tail.
563 ptimers[tail].next = timer.id;
564 timer.previous = tail;
565 timer.next = etl::timer::id::NO_TIMER;
566 tail = timer.id;
567 }
568 }
569 }
570
571 //*******************************
572 void remove(etl::timer::id::type id_, bool has_expired)
573 {
574 timer_data& timer = ptimers[id_];
575
576 if (head == id_)
577 {
578 head = timer.next;
579 }
580 else
581 {
582 ptimers[timer.previous].next = timer.next;
583 }
584
585 if (tail == id_)
586 {
587 tail = timer.previous;
588 }
589 else
590 {
591 ptimers[timer.next].previous = timer.previous;
592 }
593
594 if (!has_expired)
595 {
596 // Adjust the next delta.
597 if (timer.next != etl::timer::id::NO_TIMER)
598 {
599 ptimers[timer.next].delta += timer.delta;
600 }
601 }
602
603 timer.previous = etl::timer::id::NO_TIMER;
604 timer.next = etl::timer::id::NO_TIMER;
605 timer.delta = etl::timer::state::Inactive;
606 }
607
608 //*******************************
609 timer_data& front()
610 {
611 return ptimers[head];
612 }
613
614 //*******************************
615 const timer_data& front() const
616 {
617 return ptimers[head];
618 }
619
620 //*******************************
621 etl::timer::id::type begin()
622 {
623 current = head;
624 return current;
625 }
626
627 //*******************************
628 etl::timer::id::type previous(etl::timer::id::type last)
629 {
630 current = ptimers[last].previous;
631 return current;
632 }
633
634 //*******************************
635 etl::timer::id::type next(etl::timer::id::type last)
636 {
637 current = ptimers[last].next;
638 return current;
639 }
640
641 //*******************************
642 void clear()
643 {
644 etl::timer::id::type id = begin();
645
646 while (id != etl::timer::id::NO_TIMER)
647 {
648 timer_data& timer = ptimers[id];
649 id = next(id);
650 timer.next = etl::timer::id::NO_TIMER;
651 }
652
653 head = etl::timer::id::NO_TIMER;
654 tail = etl::timer::id::NO_TIMER;
655 current = etl::timer::id::NO_TIMER;
656 }
657
658 private:
659
660 etl::timer::id::type head;
661 etl::timer::id::type tail;
662 etl::timer::id::type current;
663
664 timer_data* const ptimers;
665 };
666
667 // The array of timer data structures.
668 timer_data* const timer_array;
669
670 // The list of active timers.
671 timer_list active_list;
672
673 bool enabled;
674 uint_least8_t number_of_registered_timers;
675
676 event_callback_type insert_callback;
677 event_callback_type remove_callback;
678
679 public:
680
681 const uint_least8_t Max_Timers;
682 };
683
684 //***************************************************************************
686 //***************************************************************************
687 template <uint_least8_t Max_Timers_, typename TInterruptGuard>
689 {
690 public:
691
692 ETL_STATIC_ASSERT(Max_Timers_ <= 254U, "No more than 254 timers are allowed");
693
694 typedef typename icallback_timer_interrupt<TInterruptGuard>::callback_type callback_type;
695
696 //*******************************************
698 //*******************************************
700 : icallback_timer_interrupt<TInterruptGuard>(timer_array, Max_Timers_)
701 {
702 }
703
704 private:
705
706 typename icallback_timer_interrupt<TInterruptGuard>::timer_data timer_array[Max_Timers_];
707 };
708}
709
710#endif
callback_timer_interrupt()
Constructor.
Definition callback_timer_interrupt.h:699
Definition callback.h:45
Declaration.
Definition delegate_cpp03.h:191
Interface for callback timer.
Definition callback_timer_interrupt.h:50
bool stop(etl::timer::id::type id_)
Stops a timer.
Definition callback_timer_interrupt.h:255
bool is_active(etl::timer::id::type id_) const
Definition callback_timer_interrupt.h:344
void set_insert_callback(event_callback_type insert_)
Set a callback when a timer is inserted on list.
Definition callback_timer_interrupt.h:370
bool is_running() const
Get the enable/disable state.
Definition callback_timer_interrupt.h:136
uint32_t time_to_next() const
Definition callback_timer_interrupt.h:325
bool set_mode(etl::timer::id::type id_, bool repeating_)
Sets a timer's mode.
Definition callback_timer_interrupt.h:300
void enable(bool state_)
Enable/disable the timer.
Definition callback_timer_interrupt.h:128
void set_remove_callback(event_callback_type remove_)
Set a callback when a timer is removed from list.
Definition callback_timer_interrupt.h:378
etl::timer::id::type register_timer(const callback_type &callback_, uint32_t period_, bool repeating_)
Register a timer.
Definition callback_timer_interrupt.h:60
bool unregister_timer(etl::timer::id::type id_)
Unregister a timer.
Definition callback_timer_interrupt.h:95
bool start(etl::timer::id::type id_, bool immediate_=false)
Starts a timer.
Definition callback_timer_interrupt.h:216
void clear()
Clears the timer of data.
Definition callback_timer_interrupt.h:144
icallback_timer_interrupt(timer_data *const timer_array_, const uint_least8_t Max_Timers_)
Constructor.
Definition callback_timer_interrupt.h:464
bool set_period(etl::timer::id::type id_, uint32_t period_)
Sets a timer's period.
Definition callback_timer_interrupt.h:286
bool has_active_timer() const
Check if there is an active timer.
Definition callback_timer_interrupt.h:314
bitset_ext
Definition absolute.h:39
The configuration of a timer.
Definition callback_timer_interrupt.h:400
void set_inactive()
Sets the timer to the inactive state.
Definition callback_timer_interrupt.h:441
bool is_active() const
Returns true if the timer is active.
Definition callback_timer_interrupt.h:433
timer_data(etl::timer::id::type id_, callback_type callback_, uint32_t period_, bool repeating_)
ETL delegate callback.
Definition callback_timer_interrupt.h:416
Definition timer.h:88
Common definitions for the timer framework.
Definition timer.h:55