-
Notifications
You must be signed in to change notification settings - Fork 0
/
hal_timer2.c
172 lines (163 loc) · 5.48 KB
/
hal_timer2.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
/*
* File : hal_timer2.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, 7:38 PM
*/
/**************************Includes-Section*****************************/
#include "hal_timer2.h"
/***********************************************************************/
/********************Data Types Declarations-Section********************/
static uint8 timer2_preload = ZERO_INT;
/***********************************************************************/
/*****************Helper Functions Declarations-Section*****************/
/*
* @Brief : Callback pointer to function.
*/
#if TIMER2_INTERRUPT_FEATURE_ENABLE==INTERRUPT_FEATURE_ENABLE
static void (*TMR2_InterruptHandler)(void) = NULL; /* @Brief : Timer2 Interrupt Handler. */
#endif
/***********************************************************************/
/*****************Software Interfaces Functions-Section*****************/
/*
* @Brief : To initialize the Timer2.
* @Param _timer : Pointer to the Timer2 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 Timer2_Init(const timer2_t *_timer)
{
Std_ReturnType ret = E_NOT_OK;
if(NULL == _timer)
{
ret = E_NOT_OK;
}
else
{
/* @Brief : Disable Timer2. */
TIMER2_DISABLE_MODULE();
/* @Brief : Timer2 pre-scaler configuration. */
TIMER2_PRRESCALER_SELECTION(_timer->timer2_prescaler_value);
/* @Brief : Timer2 post-scaler configuration. */
TIMER2_POSTSCALER_SELECTION(_timer->timer2_postscaler_value);
/* @Brief : Timer2 pre-load value configuration. */
TMR2 = _timer->timer2_preload_value;
timer2_preload = _timer->timer2_preload_value;
/* @Brief : Timer2 Interrupt configuration. */
#if TIMER2_INTERRUPT_FEATURE_ENABLE==INTERRUPT_FEATURE_ENABLE
TIMER2_InterruptEnable();
TIMER2_InterruptFlagClear();
TMR2_InterruptHandler = _timer->TMR2_InterruptHandler;
/* @Brief : Timer2 Priority configuration. */
#if INTERRUPT_PRIORITY_LEVELS_ENABLE==INTERRUPT_FEATURE_ENABLE
INTERRUPT_PriorityLevelsEnable();
if(INTERRUPT_HIGH_PRIORITY == _timer->priority)
{
INTERRUPT_GlobalInterruptHighEnable();
TIMER2_HighPrioritySet();
}
else if(INTERRUPT_LOW_PRIORITY == _timer->priority)
{
INTERRUPT_GlobalInterruptLowEnable();
TIMER2_LowPrioritySet();
}
else{/*****Nothing*****/}
#else
INTERRUPT_GlobalInterruptEnable();
INTERRUPT_PeripheralInterruptEnable();
#endif
#endif
/* @Brief : Enable Timer2. */
TIMER2_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 Timer2_DeInit(const timer2_t *_timer)
{
Std_ReturnType ret = E_NOT_OK;
if(NULL == _timer)
{
ret = E_NOT_OK;
}
else
{
/* @Brief : Disable Timer2. */
TIMER2_DISABLE_MODULE();
/* @Brief : Timer2 Interrupt configuration. */
#if TIMER2_INTERRUPT_FEATURE_ENABLE==INTERRUPT_FEATURE_ENABLE
TIMER2_InterruptDisable(); /* @Brief : Disable Interrupt for Timer2. */
#endif
ret = E_OK;
}
return ret;
}
/*
* @Brief : To write value Timer2 counter register.
* @Param _timer : Pointer to the Timer2 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 Timer2_Write_Value(const timer2_t *_timer, uint8 _value)
{
Std_ReturnType ret = E_NOT_OK;
if(NULL == _timer)
{
ret = E_NOT_OK;
}
else
{
/* Brief : @Timer2 Pre-load configuration. */
TMR2 = _value;
ret = E_OK;
}
return ret;
}
/*
* @Brief : To read value Timer2 counter register.
* @Param _timer : Pointer to the Timer2 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 Timer2_Read_Value(const timer2_t *_timer, uint8 *_value)
{
Std_ReturnType ret = E_NOT_OK;
if((NULL == _timer) || (NULL == _value))
{
ret = E_NOT_OK;
}
else
{
/* @Brief : Timer2 Pre-load value configuration. */
*_value = TMR2;
ret = E_OK;
}
return ret;
}
/*
* @Brief : Callback pointer to function to Timer2 interrupt service routine.
*/
void TMR2_ISR(void)
{
TIMER2_InterruptFlagClear();
TMR2 = timer2_preload;
if(TMR2_InterruptHandler)
{
TMR2_InterruptHandler();
}
else{/*****Nothing*****/}
}
/***********************************************************************/