-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsoftware_timer.h
More file actions
242 lines (215 loc) · 6.4 KB
/
Copy pathsoftware_timer.h
File metadata and controls
242 lines (215 loc) · 6.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/*
* software_timer.h
*
* Created on: Oct 14, 2023
* Author: Sefa Eren AKDOĞAN
*
*/
#ifndef SOFTWARE_TIMER_H_
#define SOFTWARE_TIMER_H_
/** HOW TO USE?
* Basic Usage:
* Basic usage is basic :)
* Include header file on your source file .
* Define a Software Timer;
* Init the Timer
* Init the Callback function
* Start the Timer
* thats all.
For Example
#include "software_timer.h"
...
void timer1_callback(void)
{
HAL_GPIO_TogglePin(LD2_GPIO_Port, LD2_Pin);
}
...
sw_timer_t* swTimer1;
swTimer1 = swTimer_init(1000, SW_TIMER_PERIODIC,timer1_callback);
swTimer_start(swTimer1);
...
This Example changes the led status every 1 second.
*
* Advanced(?) Usage:
* Advanced usage not hard but its not basic.
* This usage start same as basic usage.
* Include header file on your source file .
* Define a Software Timer;
* Init the Timer
* Init the Callback function
* Start the Timer
* Comment BASIC_USAGE definition in this header.
* Create a Real Timer by CubeMx.
* The timer set 1 millisecond.
* Start the timer.
* You must call swTimer_proses(swTimerX); function every software timer in your Timer handler.
* For example on :
// main.c
...
// (100MHz Prosesor) / (99 (Prescaler) * 999 *(Period)) =~ 1Khz =~ 1ms Timer
static void MX_TIM1_Init(void)
{
...
htim1.Init.Prescaler = 100;
htim1.Init.Period = 1000;
...
}
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim){
if ( htim->Instance == htim1.Instance ) {
swTimer_proses(swTimer1);
swTimer_proses(swTimer2);
}
...
START TIMER
HAL_TIM_Base_Start_IT(&htim1);
Thats all
* Timeout Functions :
* void swTimer_delay(const uint32_t delay);
* bool swTimer_waitFlag(const uint32_t delay,bool* flag);
* bool swTimer_waitDoubleFlag(const uint32_t delay,bool* flag1,bool* flag2);
*
* This Functions use SYSTICK_TIMER
* swTimer_delay -> Standard delay function similar to HAL_Delay()
* swTimer_waitFlag -> Timeout Function: Control of a flag
* swTimer_waitDoubleFlag -> Timeout Function: Control of two flag
For Example:
if (swTimer_waitFlag(10000,&flag))
foo();
* In this use, a delay of 10 seconds is set.
* If within 10 seconds the flag given as a parameter becomes true via another interrupt,
* the function returns true immediately. If not, it returns false.
*/
#define BASIC_USAGE
#ifdef BASIC_USAGE
#define USE_SYSTICK_TIMER
#endif
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#define SW_TIMER_LIMIT 100
#define SW_TIMER_MAX_VALUE UINT32_MAX
#define SW_TIMER_PERIODIC true
#define SW_TIMER_ONESHOT false
#define SW_TIMER_START true
#define SW_TIMER_STOP false
typedef struct SOFTWARE_TIMER_INIT
{
volatile uint32_t counter; // Timer Millisecond currunt value
uint32_t target; // Timer Millisecond target/interrupt value
bool status; // Timer Status (SW_TIMER_START-SW_TIMER_STOP)
bool type; // Timer Type (SW_TIMER_PERIODIC-SW_TIMER_ONESHOT)
void (*callback)(void); // Timer Callback Function
} sw_timer_t;
/**
* @brief Software Timer Initalize
* @param uint32_t Timer Value on millisecond
* @param bool Timer Type( SW_TIMER_PERIODIC - SW_TIMER_ONESHOT)
* @param void(*)(void) Software Timer Callback Function
* @retval sw_timer_t Software Timer Pointer
*/
sw_timer_t *swTimer_init(const uint32_t target, const bool type, void (*callback)(void));
/**
* @brief Software Timer Get Status
* @param sw_timer_t Software Timer Pointer
* @retval bool Software Timer Status ( Running = true / Stop = false )
*/
bool swTimer_status(sw_timer_t *swTimer);
/**
* @brief Software Timer Start
* @param sw_timer_t Software Timer Pointer
* @retval none
*/
void swTimer_start(sw_timer_t *swTimer);
/**
* @brief Software Timer Stop
* @param sw_timer_t Software Timer Pointer
* @retval none
*/
void swTimer_stop(sw_timer_t *swTimer);
/**
* @brief Software Timer Reset
* @note Counter value reset but timer status unchanged
* @param sw_timer_t Software Timer Pointer
* @retval none
*/
void swTimer_reset(sw_timer_t *swTimer);
/**
* @brief Software Timer Restart
* @note Counter value reset and timer started
* @param sw_timer_t Software Timer Pointer
* @retval none
*/
void swTimer_restart(sw_timer_t *swTimer);
/**
* @brief Software Timer Set Target Value
* @param sw_timer_t Software Timer Pointer
* @param uint32_t New Timer Target Value
* @retval uint32_t Old Timer Target Value
*/
uint32_t swTimer_setTargetValue(sw_timer_t *swTimer, uint32_t newTarget);
/**
* @brief Software Timer Get Target Value
* @param sw_timer_t Software Timer Pointer
* @retval uint32_t Timer Target Value
*/
uint32_t swTimer_getTargetValue(const sw_timer_t *swTimer);
#ifndef USE_SYSTICK_TIMER
/**
* @note This function has been removed in the interface.
* If you want to use a different timer. Comment to USE_SYSTICK_TIMER on top.
* @brief Software Timer Proses
* @param sw_timer_t Software Timer Pointer
* @retval none
*/
void swTimer_proses(sw_timer_t *swTimer);
#endif
/**
* @brief Standart Delay
* @param uint32_t Delay Period
* @retval none
*/
void swTimer_delay(const uint32_t delay);
/**
* @brief Delay With a Flag
* @param uint32_t Delay Period
* @param bool* First Flag
* @retval bool Returns true if delay ended early
*/
bool swTimer_waitFlag(const uint32_t delay, bool *flag);
/**
* @brief Delay With a Flag
* @param uint32_t Delay Period
* @param bool* First Flag
* @param bool* Second Flag
* @retval bool Returns true if delay ended early
*/
bool swTimer_waitDoubleFlag(const uint32_t delay, bool *flag1, bool *flag2);
/**
* @brief Get Run Time Seceond
* @retval uint32_t Run Time Second Value
*/
uint32_t getRunTimeSec();
/**
* @brief Get Run Time Miliseceond
* @retval uint32_t Run Time Miliseceond Value
*/
uint32_t getRunTimeMsec();
/**
* @brief Get Run Time Value on string
* @retval char* Run Time Value on string
*/
const char *getRunTimeStr();
// Timeout Functions
#define InitSoftTimeout(X) ((X) = UINT32_MAX)
#define DisableTimeout(X) ((X) = 0)
#define IsTimeoutDisabled(X) ((X) == 0)
#define IsTimeoutInitialised(X) ((X) == UINT32_MAX)
#define CheckTimeoutStatus(X) (X)
#define IS_DISABLED 0
#define IS_INITIALISED (UINT32_MAX)
void SetSoftTimeout(uint32_t *Timer, uint32_t TOValue);
void AddDelayToTimeout(uint32_t *Timer, uint32_t Delay);
bool CheckSoftTimeout(uint32_t *Timer);
#endif /* SOFTWARE_TIMER_H_ */