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 "delegate.h"
35#include "error_handler.h"
36#include "nullptr.h"
37#include "placement_new.h"
38#include "static_assert.h"
39#include "timer.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_, uint32_t period_, bool repeating_)
61 {
62 etl::timer::id::type id = etl::timer::id::NO_TIMER;
63
64 bool is_space = (number_of_registered_timers < Max_Timers);
65
66 if (is_space)
67 {
68 // Search for the free space.
69 for (uint_least8_t i = 0U; i < Max_Timers; ++i)
70 {
71 timer_data& timer = timer_array[i];
72
73 if (timer.id == etl::timer::id::NO_TIMER)
74 {
75 TInterruptGuard guard;
76 (void)guard; // Silence 'unused variable warnings.
77
78 // Create in-place.
79 new (&timer) timer_data(i, callback_, period_, repeating_);
80 ++number_of_registered_timers;
81 id = i;
82 break;
83 }
84 }
85 }
86
87 return id;
88 }
89
90 //*******************************************
92 //*******************************************
93 bool unregister_timer(etl::timer::id::type id_)
94 {
95 bool result = false;
96
97 if (id_ != etl::timer::id::NO_TIMER)
98 {
99 timer_data& timer = timer_array[id_];
100
101 if (timer.id != etl::timer::id::NO_TIMER)
102 {
103 if (timer.is_active())
104 {
105 TInterruptGuard guard;
106 (void)guard; // Silence 'unused variable warnings.
107
108 active_list.remove(timer.id, false);
109 remove_callback.call_if(timer.id);
110 }
111
112 // Reset in-place.
113 new (&timer) timer_data();
114 --number_of_registered_timers;
115
116 result = true;
117 }
118 }
119
120 return result;
121 }
122
123 //*******************************************
125 //*******************************************
126 void enable(bool state_)
127 {
128 enabled = state_;
129 }
130
131 //*******************************************
133 //*******************************************
134 bool is_running() const
135 {
136 return enabled;
137 }
138
139 //*******************************************
141 //*******************************************
142 void clear()
143 {
144 {
145 TInterruptGuard guard;
146 (void)guard; // Silence 'unused variable warnings.
147
148 active_list.clear();
149 number_of_registered_timers = 0;
150 }
151
152 for (uint8_t i = 0U; i < Max_Timers; ++i)
153 {
154 ::new (&timer_array[i]) timer_data();
155 }
156 }
157
158 //*******************************************
159 // Called by the timer service to indicate the
160 // amount of time that has elapsed since the last successful call to 'tick'.
161 // Returns true if the tick was processed,
162 // false if not.
163 //*******************************************
164 bool tick(uint32_t count)
165 {
166 if (enabled)
167 {
168 // We have something to do?
169 bool has_active = !active_list.empty();
170
171 if (has_active)
172 {
173 while (has_active && (count >= active_list.front().delta))
174 {
175 timer_data& timer = active_list.front();
176
177 count -= timer.delta;
178
179 active_list.remove(timer.id, true);
180 remove_callback.call_if(timer.id);
181
182 if (timer.callback.is_valid())
183 {
184 timer.callback();
185 }
186
187 if (timer.repeating)
188 {
189 // Reinsert the timer.
190 timer.delta = timer.period;
191 active_list.insert(timer.id);
192 insert_callback.call_if(timer.id);
193 }
194
195 has_active = !active_list.empty();
196 }
197
198 if (has_active)
199 {
200 // Subtract any remainder from the next due timeout.
201 active_list.front().delta -= count;
202 }
203 }
204
205 return true;
206 }
207
208 return false;
209 }
210
211 //*******************************************
213 //*******************************************
214 bool start(etl::timer::id::type id_, bool immediate_ = false)
215 {
216 bool result = false;
217
218 // Valid timer id?
219 if (id_ != etl::timer::id::NO_TIMER)
220 {
221 timer_data& timer = timer_array[id_];
222
223 // Registered timer?
224 if (timer.id != etl::timer::id::NO_TIMER)
225 {
226 // Has a valid period.
227 if (timer.period != etl::timer::state::Inactive)
228 {
229 TInterruptGuard guard;
230 (void)guard; // Silence 'unused variable warnings.
231
232 if (timer.is_active())
233 {
234 active_list.remove(timer.id, false);
235 remove_callback.call_if(timer.id);
236 }
237
238 timer.delta = immediate_ ? 0U : timer.period;
239 active_list.insert(timer.id);
240 insert_callback.call_if(timer.id);
241
242 result = true;
243 }
244 }
245 }
246
247 return result;
248 }
249
250 //*******************************************
252 //*******************************************
253 bool stop(etl::timer::id::type id_)
254 {
255 bool result = false;
256
257 // Valid timer id?
258 if (id_ != etl::timer::id::NO_TIMER)
259 {
260 timer_data& timer = timer_array[id_];
261
262 // Registered timer?
263 if (timer.id != etl::timer::id::NO_TIMER)
264 {
265 if (timer.is_active())
266 {
267 TInterruptGuard guard;
268 (void)guard; // Silence 'unused variable warnings.
269
270 active_list.remove(timer.id, false);
271 remove_callback.call_if(timer.id);
272 }
273
274 result = true;
275 }
276 }
277
278 return result;
279 }
280
281 //*******************************************
283 //*******************************************
284 bool set_period(etl::timer::id::type id_, uint32_t period_)
285 {
286 if (stop(id_))
287 {
288 timer_array[id_].period = period_;
289 return true;
290 }
291
292 return false;
293 }
294
295 //*******************************************
297 //*******************************************
298 bool set_mode(etl::timer::id::type id_, bool repeating_)
299 {
300 if (stop(id_))
301 {
302 timer_array[id_].repeating = repeating_;
303 return true;
304 }
305
306 return false;
307 }
308
309 //*******************************************
311 //*******************************************
312 bool has_active_timer() const
313 {
314 TInterruptGuard guard;
315 (void)guard; // Silence 'unused variable warnings.
316 return !active_list.empty();
317 }
318
319 //*******************************************
323 //*******************************************
324 uint32_t time_to_next() const
325 {
326 uint32_t delta = static_cast<uint32_t>(etl::timer::interval::No_Active_Interval);
327
328 TInterruptGuard guard;
329 (void)guard; // Silence 'unused variable warnings.
330
331 if (!active_list.empty())
332 {
333 delta = active_list.front().delta;
334 }
335
336 return delta;
337 }
338
339 //*******************************************
342 //*******************************************
343 bool is_active(etl::timer::id::type id_) const
344 {
345 // Valid timer id?
346 if (is_valid_timer_id(id_))
347 {
348 if (has_active_timer())
349 {
350 TInterruptGuard guard;
351 (void)guard; // Silence 'unused variable warnings.
352
353 const timer_data& timer = timer_array[id_];
354
355 // Registered timer?
356 if (timer.id != etl::timer::id::NO_TIMER)
357 {
358 return timer.is_active();
359 }
360 }
361 }
362
363 return false;
364 }
365
366 //*******************************************
368 //*******************************************
369 void set_insert_callback(event_callback_type insert_)
370 {
371 insert_callback = insert_;
372 }
373
374 //*******************************************
376 //*******************************************
377 void set_remove_callback(event_callback_type remove_)
378 {
379 remove_callback = remove_;
380 }
381
382 //*******************************************
383 void clear_insert_callback()
384 {
385 insert_callback.clear();
386 }
387
388 //*******************************************
389 void clear_remove_callback()
390 {
391 remove_callback.clear();
392 }
393
394 protected:
395
396 //*************************************************************************
398 struct timer_data
399 {
400 //*******************************************
401 timer_data()
402 : callback()
403 , period(0U)
404 , delta(etl::timer::state::Inactive)
405 , id(etl::timer::id::NO_TIMER)
406 , previous(etl::timer::id::NO_TIMER)
407 , next(etl::timer::id::NO_TIMER)
408 , repeating(true)
409 {
410 }
411
412 //*******************************************
414 //*******************************************
415 timer_data(etl::timer::id::type id_, callback_type callback_, uint32_t period_, bool repeating_)
416 : callback(callback_)
417 , period(period_)
418 , delta(etl::timer::state::Inactive)
419 , id(id_)
420 , previous(etl::timer::id::NO_TIMER)
421 , next(etl::timer::id::NO_TIMER)
422 , repeating(repeating_)
423 {
424 }
425
426 //*******************************************
428 //*******************************************
429 bool is_active() const
430 {
431 return delta != etl::timer::state::Inactive;
432 }
433
434 //*******************************************
436 //*******************************************
438 {
439 delta = etl::timer::state::Inactive;
440 }
441
442 callback_type callback;
443 uint32_t period;
444 uint32_t delta;
445 etl::timer::id::type id;
446 uint_least8_t previous;
447 uint_least8_t next;
448 bool repeating;
449
450 private:
451
452 // Disabled.
453 timer_data(const timer_data& other) ETL_DELETE;
454 timer_data& operator=(const timer_data& other) ETL_DELETE;
455 };
456
457 //*******************************************
459 //*******************************************
460 icallback_timer_interrupt(timer_data* const timer_array_, const uint_least8_t Max_Timers_)
461 : timer_array(timer_array_)
462 , active_list(timer_array_)
463 , enabled(false)
464 , number_of_registered_timers(0U)
465 , Max_Timers(Max_Timers_)
466 {
467 }
468
469 private:
470
471 //*******************************************
473 //*******************************************
474 bool is_valid_timer_id(etl::timer::id::type id_) const
475 {
476 return (id_ < Max_Timers);
477 }
478
479 //*************************************************************************
481 //*************************************************************************
482 class timer_list
483 {
484 public:
485
486 //*******************************
487 timer_list(timer_data* ptimers_)
488 : head(etl::timer::id::NO_TIMER)
489 , tail(etl::timer::id::NO_TIMER)
490 , current(etl::timer::id::NO_TIMER)
491 , ptimers(ptimers_)
492 {
493 }
494
495 //*******************************
496 bool empty() const
497 {
498 return head == etl::timer::id::NO_TIMER;
499 }
500
501 //*******************************
502 // Inserts the timer at the correct delta position
503 //*******************************
504 void insert(etl::timer::id::type id_)
505 {
506 timer_data& timer = ptimers[id_];
507
508 if (head == etl::timer::id::NO_TIMER)
509 {
510 // No entries yet.
511 head = id_;
512 tail = id_;
513 timer.previous = etl::timer::id::NO_TIMER;
514 timer.next = etl::timer::id::NO_TIMER;
515 }
516 else
517 {
518 // We already have entries.
519 etl::timer::id::type test_id = begin();
520
521 while (test_id != etl::timer::id::NO_TIMER)
522 {
523 timer_data& test = ptimers[test_id];
524
525 // Find the correct place to insert.
526 if (timer.delta <= test.delta)
527 {
528 if (test.id == head)
529 {
530 head = timer.id;
531 }
532
533 // Insert before test.
534 timer.previous = test.previous;
535 test.previous = timer.id;
536 timer.next = test.id;
537
538 // Adjust the next delta to compensate.
539 test.delta -= timer.delta;
540
541 if (timer.previous != etl::timer::id::NO_TIMER)
542 {
543 ptimers[timer.previous].next = timer.id;
544 }
545 break;
546 }
547 else
548 {
549 timer.delta -= test.delta;
550 }
551
552 test_id = next(test_id);
553 }
554
555 // Reached the end?
556 if (test_id == etl::timer::id::NO_TIMER)
557 {
558 // Tag on to the tail.
559 ptimers[tail].next = timer.id;
560 timer.previous = tail;
561 timer.next = etl::timer::id::NO_TIMER;
562 tail = timer.id;
563 }
564 }
565 }
566
567 //*******************************
568 void remove(etl::timer::id::type id_, bool has_expired)
569 {
570 timer_data& timer = ptimers[id_];
571
572 if (head == id_)
573 {
574 head = timer.next;
575 }
576 else
577 {
578 ptimers[timer.previous].next = timer.next;
579 }
580
581 if (tail == id_)
582 {
583 tail = timer.previous;
584 }
585 else
586 {
587 ptimers[timer.next].previous = timer.previous;
588 }
589
590 if (!has_expired)
591 {
592 // Adjust the next delta.
593 if (timer.next != etl::timer::id::NO_TIMER)
594 {
595 ptimers[timer.next].delta += timer.delta;
596 }
597 }
598
599 timer.previous = etl::timer::id::NO_TIMER;
600 timer.next = etl::timer::id::NO_TIMER;
601 timer.delta = etl::timer::state::Inactive;
602 }
603
604 //*******************************
605 timer_data& front()
606 {
607 return ptimers[head];
608 }
609
610 //*******************************
611 const timer_data& front() const
612 {
613 return ptimers[head];
614 }
615
616 //*******************************
617 etl::timer::id::type begin()
618 {
619 current = head;
620 return current;
621 }
622
623 //*******************************
624 etl::timer::id::type previous(etl::timer::id::type last)
625 {
626 current = ptimers[last].previous;
627 return current;
628 }
629
630 //*******************************
631 etl::timer::id::type next(etl::timer::id::type last)
632 {
633 current = ptimers[last].next;
634 return current;
635 }
636
637 //*******************************
638 void clear()
639 {
640 etl::timer::id::type id = begin();
641
642 while (id != etl::timer::id::NO_TIMER)
643 {
644 timer_data& timer = ptimers[id];
645 id = next(id);
646 timer.next = etl::timer::id::NO_TIMER;
647 }
648
649 head = etl::timer::id::NO_TIMER;
650 tail = etl::timer::id::NO_TIMER;
651 current = etl::timer::id::NO_TIMER;
652 }
653
654 private:
655
656 etl::timer::id::type head;
657 etl::timer::id::type tail;
658 etl::timer::id::type current;
659
660 timer_data* const ptimers;
661 };
662
663 // The array of timer data structures.
664 timer_data* const timer_array;
665
666 // The list of active timers.
667 timer_list active_list;
668
669 bool enabled;
670 uint_least8_t number_of_registered_timers;
671
672 event_callback_type insert_callback;
673 event_callback_type remove_callback;
674
675 public:
676
677 const uint_least8_t Max_Timers;
678 };
679
680 //***************************************************************************
682 //***************************************************************************
683 template <uint_least8_t Max_Timers_, typename TInterruptGuard>
685 {
686 public:
687
688 ETL_STATIC_ASSERT(Max_Timers_ <= 254U, "No more than 254 timers are allowed");
689
690 typedef typename icallback_timer_interrupt<TInterruptGuard>::callback_type callback_type;
691
692 //*******************************************
694 //*******************************************
696 : icallback_timer_interrupt<TInterruptGuard>(timer_array, Max_Timers_)
697 {
698 }
699
700 private:
701
702 typename icallback_timer_interrupt<TInterruptGuard>::timer_data timer_array[Max_Timers_];
703 };
704} // namespace etl
705
706#endif
callback_timer_interrupt()
Constructor.
Definition callback_timer_interrupt.h:695
Definition callback.h:46
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:253
bool is_active(etl::timer::id::type id_) const
Definition callback_timer_interrupt.h:343
void set_insert_callback(event_callback_type insert_)
Set a callback when a timer is inserted on list.
Definition callback_timer_interrupt.h:369
bool is_running() const
Get the enable/disable state.
Definition callback_timer_interrupt.h:134
uint32_t time_to_next() const
Definition callback_timer_interrupt.h:324
bool set_mode(etl::timer::id::type id_, bool repeating_)
Sets a timer's mode.
Definition callback_timer_interrupt.h:298
void enable(bool state_)
Enable/disable the timer.
Definition callback_timer_interrupt.h:126
void set_remove_callback(event_callback_type remove_)
Set a callback when a timer is removed from list.
Definition callback_timer_interrupt.h:377
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:93
bool start(etl::timer::id::type id_, bool immediate_=false)
Starts a timer.
Definition callback_timer_interrupt.h:214
void clear()
Clears the timer of data.
Definition callback_timer_interrupt.h:142
icallback_timer_interrupt(timer_data *const timer_array_, const uint_least8_t Max_Timers_)
Constructor.
Definition callback_timer_interrupt.h:460
bool set_period(etl::timer::id::type id_, uint32_t period_)
Sets a timer's period.
Definition callback_timer_interrupt.h:284
bool has_active_timer() const
Check if there is an active timer.
Definition callback_timer_interrupt.h:312
bitset_ext
Definition absolute.h:40
The configuration of a timer.
Definition callback_timer_interrupt.h:399
void set_inactive()
Sets the timer to the inactive state.
Definition callback_timer_interrupt.h:437
bool is_active() const
Returns true if the timer is active.
Definition callback_timer_interrupt.h:429
timer_data(etl::timer::id::type id_, callback_type callback_, uint32_t period_, bool repeating_)
ETL delegate callback.
Definition callback_timer_interrupt.h:415
Definition timer.h:88
Common definitions for the timer framework.
Definition timer.h:55