-
Notifications
You must be signed in to change notification settings - Fork 0
/
hal_timer0.c
259 lines (244 loc) · 8.14 KB
/
hal_timer0.c
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/*
* File : hal_timer0.c
* Author : Mohamed Ahmed Abdel Wahab
* LinkedIn : https://www.linkedin.com/in/mohamed-abdel-wahab-162413253/
* Github : https://github.com/moabdelwahab6611
* Created on June 7, 2023, 4:08 PM
*/
/**************************Includes-Section*****************************/
#include "hal_timer0.h"
/***********************************************************************/
/********************Data Types Declarations-Section********************/
static uint16 timer0_preload = ZERO_INT;
/***********************************************************************/
/*****************Helper Functions Declarations-Section*****************/
/*
* @Brief : Callback pointer to function.
*/
#if TIMER0_INTERRUPT_FEATURE_ENABLE==INTERRUPT_FEATURE_ENABLE
static void (*TMR0_InterruptHandler)(void) = NULL; /* @Brief : Timer0 Interrupt Handler. */
#endif
/*
* @Brief : Timer0 Pre-scaler configuration.
* @Param _timer : Pointer to the Timer0 module configurations.
*/
static inline void Timer0_Prescaler_Config(const timer0_t *_timer);
/*
* @Brief : Timer0 Timer or Counter selection configuration.
* @Param _timer : Pointer to the Timer0 module configurations.
*/
static inline void Timer0_Mode_Selection(const timer0_t *_timer);
/*
* @Brief : Timer0 register size configuration.
* @Param _timer : Pointer to the Timer0 module configurations.
*/
static inline void Timer0_Reigster_Size_Config(const timer0_t *_timer);
/***********************************************************************/
/*****************Software Interfaces Functions-Section*****************/
/*
* @Brief : To initialize the Timer0.
* @Param _timer : Pointer to the Timer0 module configurations.
* @Return Status of the function.
* (E_OK) : The function done successfully.
* (E_NOT_OK) : The function has issue while performing this action.
*/
Std_ReturnType Timer0_Init(const timer0_t *_timer)
{
Std_ReturnType ret = E_NOT_OK;
if(NULL == _timer)
{
ret = E_NOT_OK;
}
else
{
/* @Brief : Disable Timer0. */
TIMER0_DISABLE_MODULE();
/* @Brief : Timer0 Pre-scaler configuration. */
Timer0_Prescaler_Config(_timer);
/* @Brief : Selection of Timer0 Timer mode or Counter mode. */
Timer0_Mode_Selection(_timer);
/* @Brief : Timer0 register configuration. */
Timer0_Reigster_Size_Config(_timer);
/* @Brief : Timer0 Pre-load value configuration. */
TMR0H = (_timer->timer0_preload_value) >> 8;
TMR0L = (uint8)(_timer->timer0_preload_value);
timer0_preload = _timer->timer0_preload_value;
/* @Brief : Timer0 Interrupt configuration. */
#if TIMER0_INTERRUPT_FEATURE_ENABLE==INTERRUPT_FEATURE_ENABLE
TIMER0_InterruptEnable();
TIMER0_InterruptFlagClear();
TMR0_InterruptHandler = _timer->TMR0_InterruptHandler;
/* @Brief : Timer0 Priority configuration. */
#if INTERRUPT_PRIORITY_LEVELS_ENABLE==INTERRUPT_FEATURE_ENABLE
INTERRUPT_PriorityLevelsEnable();
if(INTERRUPT_HIGH_PRIORITY == _timer->priority)
{
INTERRUPT_GlobalInterruptHighEnable();
TIMER0_HighPrioritySet();
}
else if(INTERRUPT_LOW_PRIORITY == _timer->priority)
{
INTERRUPT_GlobalInterruptLowEnable();
TIMER0_LowPrioritySet();
}
else{/*****Nothing*****/}
#else
INTERRUPT_GlobalInterruptEnable();
INTERRUPT_PeripheralInterruptEnable();
#endif
#endif
/* @Brief : Enable Timer0. */
TIMER0_ENABLE_MODULE();
ret = E_OK;
}
return ret;
}
/*
* @Brief : To de-initialize the Timer0.
* @Param _timer : Pointer to the Timer0 module configurations.
* @Return Status of the function.
* (E_OK) : The function done successfully.
* (E_NOT_OK) : The function has issue while performing this action.
*/
Std_ReturnType Timer0_DeInit(const timer0_t *_timer)
{
Std_ReturnType ret = E_NOT_OK;
if(NULL == _timer)
{
ret = E_NOT_OK;
}
else
{
/* @Brief : Disable Timer0. */
TIMER0_DISABLE_MODULE();
/* @Brief : Timer0 Interrupt configuration. */
#if TIMER0_INTERRUPT_FEATURE_ENABLE==INTERRUPT_FEATURE_ENABLE
TIMER0_InterruptDisable();
#endif
ret = E_OK;
}
return ret;
}
/*
* @Brief : To write value Timer0 counter register.
* @Param _timer : Pointer to the Timer0 module configurations.
* @Param _value
* @Return Status of the function.
* (E_OK) : The function done successfully.
* (E_NOT_OK) : The function has issue while performing this action.
*/
Std_ReturnType Timer0_Write_Value(const timer0_t *_timer, uint16 _value)
{
Std_ReturnType ret = E_NOT_OK;
if(NULL == _timer)
{
ret = E_NOT_OK;
}
else
{
/* @Brief : Timer0 pre-load value configuration. */
TMR0H = (_value) >> 8;
TMR0L = (uint8)(_value);
ret = E_OK;
}
return ret;
}
/*
* @Brief : To read value Timer0 counter register.
* @Param _timer : Pointer to the Timer0 module configurations.
* @Param _value : Pointer to access value to read it from the register.
* @Return Status of the function.
* (E_OK) : The function done successfully.
* (E_NOT_OK) : The function has issue while performing this action.
*/
Std_ReturnType Timer0_Read_Value(const timer0_t *_timer, uint16 *_value)
{
Std_ReturnType ret = E_NOT_OK;
uint8 l_tmr0l = ZERO_INT;
uint8 l_tmr0h = ZERO_INT;
if(NULL == _timer)
{
ret = E_NOT_OK;
}
else
{
l_tmr0l = TMR0L;
l_tmr0h = TMR0H;
*_value = (uint16)((l_tmr0h << 8) + l_tmr0l);
ret = E_OK;
}
return ret;
}
/*
* @Brief : Callback pointer to function to Timer0 interrupt service routine.
*/
void TMR0_ISR(void)
{
TIMER0_InterruptFlagClear();
TMR0H = (timer0_preload) >> 8;
TMR0L = (uint8)(timer0_preload);
if(TMR0_InterruptHandler)
{
TMR0_InterruptHandler();
}
else{/*****Nothing*****/}
}
/*
* @Brief : Timer0 Pre-scaler configuration.
* @Param _timer : Pointer to the Timer0 module configurations.
*/
static inline void Timer0_Prescaler_Config(const timer0_t *_timer)
{
if(TIMER0_ENABLE_PRESCALER_CFG == _timer->prescaler_status_cfg)
{
TIMER0_ENABLE_PRESCALER();
T0CONbits.T0PS = _timer->prescaler_value;
}
else if(TIMER0_DISABLE_PRESCALER_CFG == _timer->prescaler_status_cfg)
{
TIMER0_DISABLE_PRESCALER();
}
else{/*****Nothing*****/}
}
/*
* @Brief : Timer0 Timer or Counter selection configuration.
* @Param _timer : Pointer to the Timer0 module configurations.
*/
static inline void Timer0_Mode_Selection(const timer0_t *_timer)
{
if(TIMER0_TIMER_MODE == _timer->timer0_mode)
{
TIMER0_ENABLE_TIMER_MODE();
}
else if(TIMER0_COUNTER_MODE == _timer->timer0_mode)
{
TIMER0_ENABLE_COUNTER_MODE();
if(TIMER0_RISING_EDGE_CFG == _timer->timer0_counter_edge)
{
TIMER0_ENABLE_RISING_EDGE();
}
else if(TIMER0_FALLING_EDGEE_CFG == _timer->timer0_counter_edge)
{
TIMER0_ENABLE_FALLING_EDGE();
}
else{/*****Nothing*****/}
}
else{/*****Nothing*****/}
}
/*
* @Brief : Timer0 register size configuration.
* @Param _timer : Pointer to the Timer0 module configurations.
*/
static inline void Timer0_Reigster_Size_Config(const timer0_t *_timer)
{
if(TIMER0_8BIT_REGISTER_MODE == _timer->timer0_register_size)
{
TIMER0_ENABLE_8BIT_REGISTER_MODE();
}
else if(TIMER0_16BIT_REGISTER_MODE == _timer->timer0_register_size)
{
TIMER0_ENABLE_16BIT_REGISTER_MODE();
}
else{/*****Nothing*****/}
}
/***********************************************************************/