-
Notifications
You must be signed in to change notification settings - Fork 0
/
hal_adc.c
350 lines (331 loc) · 11.8 KB
/
hal_adc.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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
/*
* File : hal_adc.c
* Author : Mohamed Ahmed Abdel Wahab
* LinkedIn : https://www.linkedin.com/in/mohamed-abdel-wahab-162413253/
* Github : https://github.com/moabdelwahab6611
* Created on June 5, 2023, 4:31 PM
*/
/**************************Includes-Section*****************************/
#include "hal_adc.h"
/***********************************************************************/
/*****************Helper Functions Declarations-Section*****************/
/*
* @brief : Callback pointers for (ADC) interrupt.
*/
#if ADC_INTERRUPT_FEATURE_ENABLE==INTERRUPT_FEATURE_ENABLE
static void (* ADC_InterruptHandler)(void) = NULL; /* @Brief : Timer3 Interrupt Handler. */
#endif
/*
* @Brief : ADC channel and Port configuration.
* @Param _adc : Pointer to the ADC module configurations.
*/
static inline void adc_input_channel_port_config(adc_channel_select_t channel);
/*
* @Brief : Select ADC result format.
* @Param _adc : Pointer to the ADC module configurations.
*/
static inline void select_result_format(const adc_config_t *_adc);
/*
* @Brief : Voltage reference configuration.
* @Param _adc : Pointer to the ADC module configurations.
*/
static inline void configure_voltage_reference(const adc_config_t *_adc);
/***********************************************************************/
/*****************Software Interfaces Functions-Section*****************/
Std_ReturnType ADC_Init(const adc_config_t *_adc)
{
Std_ReturnType ret = E_NOT_OK;
if(NULL == _adc)
{
ret = E_NOT_OK;
}
else
{
/* @Brief : Disable the ADC. */
ADC_CONVERTER_DISABLE();
/* @Brief : Configure the acquisition time. */
ADCON2bits.ACQT = _adc->acquisition_time;
/* @Brief : Configure the conversion clock. */
ADCON2bits.ADCS = _adc->conversion_clock;
/* @Brief : Configure the default channel. */
ADCON0bits.CHS = _adc->adc_channel;
adc_input_channel_port_config(_adc->adc_channel);
/* @Brief : ADC Interrupt configuration. */
#if ADC_INTERRUPT_FEATURE_ENABLE==INTERRUPT_FEATURE_ENABLE
ADC_InterruptEnable();
ADC_InterruptFlagClear();
/* @Brief : ADC Priority configuration. */
#if INTERRUPT_PRIORITY_LEVELS_ENABLE==INTERRUPT_FEATURE_ENABLE
INTERRUPT_PriorityLevelsEnable();
if(INTERRUPT_HIGH_PRIORITY == _adc->priority){ADC_HighPrioritySet();}
else if(INTERRUPT_LOW_PRIORITY == _adc->priority){ADC_LowPrioritySet();}
else{/*****Nothing*****/}
#else
INTERRUPT_GlobalInterruptEnable();
INTERRUPT_PeripheralInterruptEnable();
#endif
ADC_InterruptHandler = _adc->ADC_InterruptHandler;
#endif
/* @Brief : Configure the result format. */
select_result_format(_adc);
/* @Brief : Configure the voltage reference. */
configure_voltage_reference(_adc);
/* @Brief : Enable the ADC. */
ADC_CONVERTER_ENABLE();
ret = E_OK;
}
return ret;
}
/*
* @Brief : To de-initialize the ADC.
* @Param _adc : Pointer to the ADC 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 ADC_DeInit(const adc_config_t *_adc)
{
Std_ReturnType ret = E_NOT_OK;
if(NULL == _adc)
{
ret = E_NOT_OK;
}
else
{
/* @Brief : Disable the ADC. */
ADC_CONVERTER_DISABLE();
/* @Brief : Configure the interrupt. */
#if ADC_INTERRUPT_FEATURE_ENABLE==INTERRUPT_FEATURE_ENABLE
ADC_InterruptDisable();
#endif
ret = E_OK;
}
return ret;
}
/*
* @Brief : To select ADC channel.
* @Param _adc : Pointer to the ADC module configurations.
* @Param channel
* @Return Status of the function.
* (E_OK) : The function done successfully.
* (E_NOT_OK) : The function has issue while performing this action.
*/
Std_ReturnType ADC_SelectChannel(const adc_config_t *_adc, adc_channel_select_t channel)
{
Std_ReturnType ret = E_NOT_OK;
if(NULL == _adc)
{
ret = E_NOT_OK;
}
else
{
/* @Brief : Configure the default channel. */
ADCON0bits.CHS = channel;
adc_input_channel_port_config(channel);
ret = E_OK;
}
return ret;
}
/*
* @Brief : To start conversion by ADC.
* @Param _adc : Pointer to the ADC 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 ADC_StartConversion(const adc_config_t *_adc)
{
Std_ReturnType ret = E_NOT_OK;
if(NULL == _adc)
{
ret = E_NOT_OK;
}
else
{
ADC_START_CONVERSION();
ret = E_OK;
}
return ret;
}
/*
* @Brief : To check if conversion by ADC has been completed or not.
* @Param _adc : Pointer to the ADC module configurations.
* @Param conversion_status : Pointer to the ADC conversion status.
* @Return Status of the function.
* (E_OK) : The function done successfully.
* (E_NOT_OK) : The function has issue while performing this action.
*/
Std_ReturnType ADC_IsConversionDone(const adc_config_t *_adc, uint8 *conversion_status)
{
Std_ReturnType ret = E_NOT_OK;
if((NULL == _adc) || (NULL == conversion_status))
{
ret = E_NOT_OK;
}
else
{
*conversion_status = (uint8)(!(ADCON0bits.GO_nDONE));
ret = E_OK;
}
return ret;
}
/*
* @Brief : To check the conversion result.
* @Param _adc : Pointer to the ADC module configurations.
* @Param conversion_result : Pointer to the ADC conversion result.
* @Return Status of the function.
* (E_OK) : The function done successfully.
* (E_NOT_OK) : The function has issue while performing this action.
*/
Std_ReturnType ADC_GetConversionResult(const adc_config_t *_adc, adc_result_t *conversion_result)
{
Std_ReturnType ret = E_NOT_OK;
if((NULL == _adc) || (NULL == conversion_result))
{
ret = E_NOT_OK;
}
else
{
if(ADC_RESULT_RIGHT == _adc->result_format)
{
*conversion_result = (adc_result_t)((ADRESH << 8) + ADRESL);
}
else if(ADC_RESULT_LEFT == _adc->result_format)
{
*conversion_result = (adc_result_t)(((ADRESH << 8) + ADRESL) >> 6);
}
else
{
*conversion_result = (adc_result_t)((ADRESH << 8) + ADRESL);
}
ret = E_OK;
}
return ret;
}
/*
* @Brief : To get conversion data from ADC.
* @Param _adc : Pointer to the ADC module configurations.
* @Param channel
* @Param conversion_result : Pointer to the ADC conversion result.
* @Return Status of the function.
* (E_OK) : The function done successfully.
* (E_NOT_OK) : The function has issue while performing this action.
*/
Std_ReturnType ADC_GetConversion_Blocking(const adc_config_t *_adc, adc_channel_select_t channel, adc_result_t *conversion_result)
{
Std_ReturnType ret = E_NOT_OK;
uint8 l_conversion_status = ZERO_INT;
if((NULL == _adc) || (NULL == conversion_result))
{
ret = E_NOT_OK;
}
else
{
/* @Brief : Select A/D channel. */
ret = ADC_SelectChannel(_adc, channel);
/* @Brief : Start ADC conversion. */
ret = ADC_StartConversion(_adc);
/* @Brief : Check if conversion is completed. */
while(ADCON0bits.GO_nDONE);
ret = ADC_GetConversionResult(_adc, conversion_result);
}
return ret;
}
/*
* @Brief : To start conversion interrupt.
* @Param _adc : Pointer to the ADC module configurations.
* @Param channel
* @Return Status of the function.
* (E_OK) : The function done successfully.
* (E_NOT_OK) : The function has issue while performing this action.
*/
Std_ReturnType ADC_StartConversion_Interrupt(const adc_config_t *_adc, adc_channel_select_t channel)
{
Std_ReturnType ret = E_NOT_OK;
if(NULL == _adc)
{
ret = E_NOT_OK;
}
else
{
/* @Brief : Select A/D channel. */
ret = ADC_SelectChannel(_adc, channel);
/* @Brief : Start ADC conversion. */
ret = ADC_StartConversion(_adc);
}
return ret;
}
/*
* @Brief : ADC channel and Port configuration.
* @Param _adc : Pointer to the ADC module configurations.
*/
static inline void adc_input_channel_port_config(adc_channel_select_t channel)
{
switch(channel)
{
case ADC_CHANNEL_AN0 : SET_BIT(TRISA, _TRISA_RA0_POSN); break; /* @Brief : Disable the digital output driver. */
case ADC_CHANNEL_AN1 : SET_BIT(TRISA, _TRISA_RA1_POSN); break; /* @Brief : Disable the digital output driver. */
case ADC_CHANNEL_AN2 : SET_BIT(TRISA, _TRISA_RA2_POSN); break; /* @Brief : Disable the digital output driver. */
case ADC_CHANNEL_AN3 : SET_BIT(TRISA, _TRISA_RA3_POSN); break; /* @Brief : Disable the digital output driver. */
case ADC_CHANNEL_AN4 : SET_BIT(TRISA, _TRISA_RA5_POSN); break; /* @Brief : Disable the digital output driver. */
case ADC_CHANNEL_AN5 : SET_BIT(TRISE, _TRISE_RE0_POSN); break; /* @Brief : Disable the digital output driver. */
case ADC_CHANNEL_AN6 : SET_BIT(TRISE, _TRISE_RE1_POSN); break; /* @Brief : Disable the digital output driver. */
case ADC_CHANNEL_AN7 : SET_BIT(TRISE, _TRISE_RE2_POSN); break; /* @Brief : Disable the digital output driver. */
case ADC_CHANNEL_AN8 : SET_BIT(TRISB, _TRISB_RB2_POSN); break; /* @Brief : Disable the digital output driver. */
case ADC_CHANNEL_AN9 : SET_BIT(TRISB, _TRISB_RB3_POSN); break; /* @Brief : Disable the digital output driver. */
case ADC_CHANNEL_AN10 : SET_BIT(TRISB, _TRISB_RB1_POSN); break; /* @Brief : Disable the digital output driver. */
case ADC_CHANNEL_AN11 : SET_BIT(TRISB, _TRISB_RB4_POSN); break; /* @Brief : Disable the digital output driver. */
case ADC_CHANNEL_AN12 : SET_BIT(TRISB, _TRISB_RB0_POSN); break; /* @Brief : Disable the digital output driver. */
}
}
/*
* @Brief : Select ADC result format.
* @Param _adc : Pointer to the ADC module configurations.
*/
static inline void select_result_format(const adc_config_t *_adc)
{
if(ADC_RESULT_RIGHT == _adc->result_format)
{
ADC_RESULT_RIGHT_FORMAT();
}
else if(ADC_RESULT_LEFT == _adc->result_format)
{
ADC_RESULT_LEFT_FORMAT();
}
else
{
ADC_RESULT_RIGHT_FORMAT();
}
}
/*
* @Brief : Voltage reference configuration.
* @Param _adc : Pointer to the ADC module configurations.
*/
static inline void configure_voltage_reference(const adc_config_t *_adc)
{
if(ADC_VOLTAGE_REFERENCE_ENABLED == _adc->voltage_reference)
{
ADC_ENABLE_VOLTAGE_REFERENCE();
}
else if(ADC_VOLTAGE_REFERENCE_DISABLED == _adc->result_format)
{
ADC_DISABLE_VOLTAGE_REFERENCE();
}
else
{
ADC_DISABLE_VOLTAGE_REFERENCE();
}
}
/*
* @Brief : Callback pointer to function to ADC interrupt service routine.
*/
void ADC_ISR(void)
{
ADC_InterruptFlagClear();
if(ADC_InterruptHandler )
{
ADC_InterruptHandler();
}
else{/*****Nothing*****/}
}
/***********************************************************************/