Bar Logo Dual Active Bridge Development Board (Part-No. )
 
Content
     
Loading...
Searching...
No Matches
os_timer.c
1
2//=======================================================================================================
3// @file os_timer.c
4//
5// @brief contains the functions for using software timers
6//
7// @note in this file you will find functions for using timers and countdowns for your application
8// These software timers are updated by the hardware timer interrupt of the os_scheduler
9// The number of timers need to be defined in main/project_settings.h :
10// #define OS_TIMER_NUMBER_OF_TIMERS 3
11// You should also put your your individual Timer defines with the corresponding index there
12// #define OS_TIMER_COUNTDOWN_EXAMPLE_SWITCHOFFTHELIGHT 0
13// #define OS_TIMER_COUNTDOWN_EXAMPLE_SWITCHOFFCOFFEEMACHINE 1
14// #define OS_TIMER_COUNTDOWN_EXAMPLE_IAMGOINGHOMENOW 2
15//
16//
17// @version v1.0
18// @date 2019-08-30
19// @author M52409
20//
21//=======================================================================================================
22
23#include <stdbool.h>
24#include <stdint.h>
25#include <string.h>
26#include "os_config.h"
27#include "os_timer.h"
28
29#ifndef OS_TIMER_NUMBER_OF_TIMERS
30 #pragma message "OS_TIMER_NUMBER_OF_TIMERS needs to be defined in the file project_settings.h"
31#endif //OS_TIMER_NUMBER_OF_TIMERS
32
33#if OS_TIMER_NUMBER_OF_TIMERS > 0
34
35typedef struct
36{
37 uint16_t timerval;
38 uint8_t countdown:1;
39 uint8_t countup:1;
40 uint8_t expired:1;
41 uint8_t reserved:5;
42} OS_TIMER_DATA_t;
43
44/* The variable below need not be static */
45/* LDRA_EXCLUDE 27 D */
46#if OS_TIMER_NUMBER_OF_TIMERS > 0
47OS_TIMER_DATA_t os_timer_data[OS_TIMER_NUMBER_OF_TIMERS];
48#endif
49
50//=======================================================================================================
51// @brief OS_Timer_Init initializes the Software Timers
52// @note call this function at boot up to initialize the Software Timers
53//=======================================================================================================
54void OS_Timer_Init(void)
55{
56#if OS_TIMER_NUMBER_OF_TIMERS > 0
57 uint8_t i;
58 /* Return of the memset is not required */
59 /* LDRA_EXCLUDE 382 S */
60 memset(os_timer_data, 0, sizeof(os_timer_data));
61 for (i=0U; i < OS_TIMER_NUMBER_OF_TIMERS; i++)
62 {
63 os_timer_data[i].expired = true;
64 }
65#endif
66}
67
68
69//=======================================================================================================
70// @brief OS_Timer_StartCountdown_100us/1ms starts a new Countdown
71// @note call this function if you want to use a new countdown
72// @param timer_index - index of the timer that should be used for that countdown
73// @param numOfTicks - depending on the scheduler timing, the number of 100µs or 1ms ticks to count
74//=======================================================================================================
75
76/* OS_Timer_StartCountdown_1ms, OS_Timer_StartCountdown_100us and
77 OS_Timer_IsCountdownExpired are not called in the project, hence the violations
78 76 D and 61 D */
79/* LDRA_EXCLUDE 76 D */
80/* LDRA_EXCLUDE 61 D */
81#if OS_USE_SCHEDULER_100us == 1
82void OS_Timer_StartCountdown_100us(uint8_t timer_index, uint16_t numOfTicks)
83#elif OS_USE_SCHEDULER_1ms == 1
84void OS_Timer_StartCountdown_1ms(uint8_t timer_index, uint16_t numOfTicks)
85#else
86#error OS_USE_SCHEDULER_100us or OS_USE_SCHEDULER_1ms needs to be definied as 1 to get the right timer functions
87#endif
88{
89 if (timer_index < OS_TIMER_NUMBER_OF_TIMERS)
90 {
91 os_timer_data[timer_index].countup = false;
92 os_timer_data[timer_index].countdown = true;
93 os_timer_data[timer_index].expired = false;
94 os_timer_data[timer_index].timerval = numOfTicks;
95 }
96}
97
98/* LDRA_EXCLUDE 76 D */
99/* LDRA_EXCLUDE 61 D */
100bool OS_Timer_IsCountdownExpired(uint8_t timer_index)
101{
102 if (timer_index >= OS_TIMER_NUMBER_OF_TIMERS)
103 {
104 return false;
105 }
106 if (os_timer_data[timer_index].expired == 0U)
107 {
108 return false;
109 }
110 return true;
111}
112
113void OS_Timer_Tick(void)
114{
115 uint8_t i;
116 for (i=0U; i < OS_TIMER_NUMBER_OF_TIMERS; i++)
117 {
118 if (os_timer_data[i].countdown == 1U)
119 {
120 if (os_timer_data[i].timerval != 0U)
121 {
122 os_timer_data[i].timerval -= 1U;
123 if (os_timer_data[i].timerval == 0U)
124 {
125 os_timer_data[i].expired = true;
126 }
127 }
128 }
129 }
130}
131
132#endif
uint16_t reserved
Definition uart1.c:97