-
Notifications
You must be signed in to change notification settings - Fork 0
/
hal_timer3.c
209 lines (199 loc) · 6.67 KB
/
hal_timer3.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
/*
* File : hal_timer3.c
* Author : Mohamed Ahmed Abdel Wahab
* LinkedIn : https://www.linkedin.com/in/mohamed-abdel-wahab-162413253/
* Github : https://github.com/moabdelwahab6611
* Created on June 8, 2023, 11:12 PM
*/
/**************************Includes-Section*****************************/
#include "hal_timer3.h"
/***********************************************************************/
/********************Data Types Declarations-Section********************/
static uint16 timer3_preload = ZERO_INT;
/***********************************************************************/
/*****************Helper Functions Declarations-Section*****************/
/*
* @Brief : Callback pointer to function.
*/
#if TIMER3_INTERRUPT_FEATURE_ENABLE==INTERRUPT_FEATURE_ENABLE
static void (*TMR3_InterruptHandler)(void) = NULL; /* @Brief : Timer3 Interrupt Handler. */
#endif
/*
* @Brief : Timer3 Timer or Counter selection configuration.
* @Param _timer : Pointer to the Timer3 module configurations.
*/
static inline void Timer3_Mode_Selection(const timer3_t *_timer);
/***********************************************************************/
/*****************Software Interfaces Functions-Section*****************/
/*
* @Brief : To initialize the Timer3.
* @Param _timer : Pointer to the Timer3 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 Timer3_Init(const timer3_t *_timer)
{
Std_ReturnType ret = E_NOT_OK;
if(NULL == _timer)
{
ret = E_NOT_OK;
}
else
{
/* @Brief : Disable Timer3. */
TIMER3_DISABLE_MODULE();
/* @Brief : Timer3 pre-scaler configuration. */
TIMER3_PRESCALER_SELECTION(_timer->timer3_prescaler_value);
/* @Brief : Selection of Timer3 Timer or Counter modes. */
Timer3_Mode_Selection(_timer);
/* @Brief : Timer3 pre-load value configuration. */
TMR3H = (_timer->timer3_preload_value) >> 8;
TMR3L = (uint8)(_timer->timer3_preload_value);
timer3_preload = _timer->timer3_preload_value;
/* @Brief : Timer3 Interrupt configuration. */
#if TIMER3_INTERRUPT_FEATURE_ENABLE==INTERRUPT_FEATURE_ENABLE
TIMER3_InterruptEnable();
TIMER3_InterruptFlagClear();
TMR3_InterruptHandler = _timer->TMR3_InterruptHandler;
/* @Brief : Timer3 Priority configuration. */
#if INTERRUPT_PRIORITY_LEVELS_ENABLE==INTERRUPT_FEATURE_ENABLE
INTERRUPT_PriorityLevelsEnable();
if(INTERRUPT_HIGH_PRIORITY == _timer->priority)
{
INTERRUPT_GlobalInterruptHighEnable();
TIMER1_HighPrioritySet();
}
else if(INTERRUPT_LOW_PRIORITY == _timer->priority)
{
INTERRUPT_GlobalInterruptLowEnable();
TIMER1_LowPrioritySet();
}
#else
INTERRUPT_GlobalInterruptEnable();
INTERRUPT_PeripheralInterruptEnable();
#endif
#endif
/* @Brief : Enable Timer3. */
TIMER3_ENABLE_MODULE();
ret = E_OK;
}
return ret;
}
/*
* @Brief : To de-initialize the Timer3.
* @Param _timer : Pointer to the Timer3 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 Timer3_DeInit(const timer3_t *_timer)
{
Std_ReturnType ret = E_NOT_OK;
if(NULL == _timer)
{
ret = E_NOT_OK;
}
else
{
/* @Brief : Disable Timer3. */
TIMER3_DISABLE_MODULE();
/* @Brief : Timer3 Interrupt configuration. */
#if TIMER3_INTERRUPT_FEATURE_ENABLE==INTERRUPT_FEATURE_ENABLE
TIMER3_InterruptDisable();
#endif
ret = E_OK;
}
return ret;
}
/*
* @Brief : To write value Timer3 counter register.
* @Param _timer : Pointer to the Timer3 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 Timer3_Write_Value(const timer3_t *_timer, uint16 _value)
{
Std_ReturnType ret = E_NOT_OK;
if(NULL == _timer)
{
ret = E_NOT_OK;
}
else
{
/* @Brief : Timer3 pre-load value configuration. */
TMR3H = (_value) >> 8;
TMR3L = (uint8)(_value);
ret = E_OK;
}
return ret;
}
/*
* @Brief : To read value Timer3 counter register.
* @Param _timer : Pointer to the Timer3 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 Timer3_Read_Value(const timer3_t *_timer, uint16 *_value)
{
Std_ReturnType ret = E_NOT_OK;
uint8 l_tmr3l = ZERO_INT;
uint8 l_tmr3h = ZERO_INT;
if((NULL == _timer) || NULL == _value)
{
ret = E_NOT_OK;
}
else
{
/* @Brief : Timer3 pre-load value configuration. */
l_tmr3l = TMR3L;
l_tmr3h = TMR3H;
*_value = (uint16)((l_tmr3h << 8) + l_tmr3l);
ret = E_OK;
}
return ret;
}
/*
* @Brief : Callback pointer to function to Timer3 interrupt service routine.
*/
void TMR3_ISR(void)
{
TIMER3_InterruptFlagClear();
TMR3H = (timer3_preload) >> 8;
TMR3L = (uint8)(timer3_preload);
if(TMR3_InterruptHandler)
{
TMR3_InterruptHandler();
}
else{/*****Nothing*****/}
}
/*
* @Brief : Timer3 Timer or Counter selection configuration.
* @Param _timer : Pointer to the Timer3 module configurations.
*/
static inline void Timer3_Mode_Selection(const timer3_t *_timer)
{
if(TIMER3_TIMER_MODE == _timer->timer3_mode)
{
TIMER3_ENABLE_TIMER_MODE();
}
else if(TIMER3_COUNTER_MODE == _timer->timer3_mode)
{
TIMER3_ENABLE_COUNTER_MODE();
if(TIMER3_ASYNCHRONOUS_COUNTER_MODE == _timer->timer3_counter_mode)
{
TIMER3_ENABLE_ASYNCHRONOUS_COUNTER_MODE();
}
else if(TIMER3_SYNCHRONOUS_COUNTER_MODE == _timer->timer3_counter_mode)
{
TIMER3_ENABLE_SYNCHRONOUS_COUNTER_MODE();
}
else{/*****Nothing*****/}
}
else{/*****Nothing*****/}
}
/***********************************************************************/