-
Notifications
You must be signed in to change notification settings - Fork 38
/
hal_ll_adc.c
423 lines (348 loc) · 16 KB
/
hal_ll_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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
/****************************************************************************
**
** Copyright (C) ${COPYRIGHT_YEAR} MikroElektronika d.o.o.
** Contact: https://www.mikroe.com/contact
**
** This file is part of the mikroSDK package
**
** Commercial License Usage
**
** Licensees holding valid commercial NECTO compilers AI licenses may use this
** file in accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The MikroElektronika Company.
** For licensing terms and conditions see
** https://www.mikroe.com/legal/software-license-agreement.
** For further information use the contact form at
** https://www.mikroe.com/contact.
**
**
** GNU Lesser General Public License Usage
**
** Alternatively, this file may be used for
** non-commercial projects under the terms of the GNU Lesser
** General Public License version 3 as published by the Free Software
** Foundation: https://www.gnu.org/licenses/lgpl-3.0.html.
**
** The above copyright notice and this permission notice shall be
** included in all copies or substantial portions of the Software.
**
** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
** OF MERCHANTABILITY, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
** TO THE WARRANTIES FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
** DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
** OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
** OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
**
****************************************************************************/
/*!
* @file hal_ll_adc.c
* @brief Low level HAL ADC source file.
*/
#include "hal_ll_adc.h"
#include "hal_ll_gpio.h"
#include "hal_ll_analog_in_map.h"
// ------------------------------------------------------------- PRIVATE MACROS
/*!< @brief Helper macro for getting hal_ll_module_state address */
#define hal_ll_adc_get_module_state_address ((hal_ll_adc_handle_register_t *)*handle)
/*!< @brief Helper macro for getting module specific control register structure base address // first register address */
#define hal_ll_adc_get_handle (hal_ll_adc_handle_register_t *)hal_ll_adc_get_module_state_address->hal_ll_adc_handle
/*!< @brief Helper macro for getting module specific base address directly from HAL layer handle */
#define hal_ll_adc_get_base_from_hal_handle ((hal_ll_adc_hw_specifics_map_t *)((hal_ll_adc_handle_register_t *)\
(((hal_ll_adc_handle_register_t *)(handle))->hal_ll_adc_handle))->hal_ll_adc_handle)->base
#define hal_ll_adc_get_base_handle ((hal_ll_adc_hw_specifics_map_t *)((hal_ll_adc_get_handle)->hal_ll_adc_handle))->base
/*!< @brief Helper macro for getting module specific control register structure */
#define hal_ll_adc_get_base_struct(_handle) ((const hal_ll_adc_base_handle_t *)_handle)
/*!< @brief Macros defining register bit location */
#define HAL_LL_ADC_ADCSRA_ADSC_BIT (6)
#define HAL_LL_ADC_ADCSRA_ADEN (0x80)
#define HAL_LL_ADC_ADCSRA_ADPS (0x7)
/*!< @brief Macros defining register specific bit masks */
#define HAL_LL_ADC_ADMUX_MASK (0xFF)
#define HAL_LL_ADC_ADCSRB_MUX5 (3)
/*!< @brief Macros defining voltage reference related values */
#define HAL_LL_ADC_ADMUX_REFS_INTERNAL (0xC0)
#define HAL_LL_ADC_INTERNAL_VREF_VALUE (2.56)
// -------------------------------------------------------------- PRIVATE TYPES
/*!< @brief Local handle list */
static hal_ll_adc_handle_register_t hal_ll_module_state[ADC_MODULE_COUNT] = { (handle_t *) NULL, (handle_t *) NULL, false };
/*!< @brief ADC register structure. */
typedef struct {
uint8_t adcl;
uint8_t adch;
uint8_t adcsra;
uint8_t adcsrb;
uint8_t admux;
} hal_ll_adc_base_handle_t;
/**
* @brief ADC low level specific structure.
*
* User is not to change these values or unexpected behaviour
* may occur.
*/
typedef struct
{
const hal_ll_adc_base_handle_t *base;
hal_ll_pin_name_t module_index;
hal_ll_pin_name_t pin;
hal_ll_adc_voltage_reference_t vref_input;
float vref_value;
uint16_t resolution;
uint8_t channel;
} hal_ll_adc_hw_specifics_map_t;
/**
* Return values
*/
typedef enum {
HAL_LL_ADC_SUCCESS = 0,
HAL_LL_ADC_WRONG_PIN,
HAL_LL_ADC_UNSUPPORTED_RESOLUTION = 1100,
HAL_LL_ADC_UNSUPPORTED_VREF,
HAL_LL_ADC_WRONG_CHANNEL,
HAL_LL_ADC_ERROR = (-1)
} hal_ll_adc_err_t;
// ------------------------------------------------------------------ CONSTANTS
/*!< @brief ADC modules register array */
static const hal_ll_adc_base_handle_t hal_ll_adc_regs[ ADC_MODULE_COUNT + 1 ] = {
#ifdef ADC_MODULE_0
{ HAL_LL_ADC0_ADCL_REG_ADDRESS, HAL_LL_ADC0_ADCH_REG_ADDRESS, HAL_LL_ADC0_ADCSRA_REG_ADDRESS, HAL_LL_ADC0_ADCSRB_REG_ADDRESS, HAL_LL_ADC0_ADMUX_REG_ADDRESS },
#endif
{ HAL_LL_MODULE_ERROR, HAL_LL_MODULE_ERROR, HAL_LL_MODULE_ERROR, HAL_LL_MODULE_ERROR, HAL_LL_MODULE_ERROR }
};
static hal_ll_adc_hw_specifics_map_t hal_ll_adc_hw_specifics_map[ADC_MODULE_COUNT + 1] = {
#ifdef ADC_MODULE_0
{ &hal_ll_adc_regs[ hal_ll_adc_module_num( ADC_MODULE_0 )], hal_ll_adc_module_num(ADC_MODULE_0), HAL_LL_PIN_NC, HAL_LL_ADC_VREF_DEFAULT, 0, HAL_LL_ADC_RESOLUTION_10_BIT, 0xFF },
#endif
{ &hal_ll_adc_regs[ hal_ll_adc_module_num( ADC_MODULE_COUNT )], HAL_LL_MODULE_ERROR, HAL_LL_PIN_NC, HAL_LL_ADC_VREF_DEFAULT, 0, HAL_LL_ADC_RESOLUTION_10_BIT, 0xFF }
};
// ------------------------------------------------------------------ VARIABLES
/*!< @brief Global handle variables used in functions */
static volatile hal_ll_adc_handle_register_t *low_level_handle;
static volatile hal_ll_adc_hw_specifics_map_t *hal_ll_adc_hw_specifics_map_local;
// ---------------------------------------------- PRIVATE FUNCTION DECLARATIONS
/**
* @brief Initialize ADC module.
* @param *map - ADC module local map.
* @return None
*/
static void hal_ll_adc_init(hal_ll_adc_hw_specifics_map_t *map);
/**
* @brief Check if pins are adequate.
*
* Checks analog pin the user has passed with pre-defined
* pins in ADC pin map.
*
* @param[in] pin - ADC pre-defined pin name.
* @param[in] *index Pointer with ADC pin map index value.
* @param[in] *handle_map ADC low level handle pointer.
* @return hal_ll_pin_name_t Module index based on pins.
*
* Returns pre-defined module index from pin maps, if pin
* is adequate.
*/
static hal_ll_pin_name_t hal_ll_adc_check_pins(hal_ll_pin_name_t pin, uint8_t *index, hal_ll_adc_handle_register_t *handle_map);
/**
* @brief Maps new-found module specific values.
*
* Maps pin name, register address and channel.
*
* @param[in] module_index ADC HW module index -- 0,1,2...
* @param[in] *index Pointer with ADC pin map index value
*
* @return None
*/
static void hal_ll_adc_map_pin(uint8_t module_index, uint8_t *index);
/**
* @brief Get local hardware specific map.
*
* Checks handle value and returns address of adequate
* hal_ll_adc_hw_specifics_map array index.
*
* @param[in] handle - Object specific context handler.
* @return hal_ll_adc_hw_specifics_map_t Map address.
*
* Returns pre-defined map index address based on handle value,
* if handle is adequate.
*/
static hal_ll_adc_hw_specifics_map_t *hal_ll_get_specifics(handle_t handle);
/**
* @brief Initialize ADC module.
* @param *base - ADC module base address.
* @return None
*/
static void _hal_ll_adc_hw_init(hal_ll_adc_hw_specifics_map_t *map);
// ------------------------------------------------ PUBLIC FUNCTION DEFINITIONS
hal_ll_err_t hal_ll_adc_register_handle(hal_ll_pin_name_t pin, hal_ll_adc_voltage_reference_t vref_input, hal_ll_adc_resolution_t resolution, hal_ll_adc_handle_register_t *handle_map, uint8_t *hal_module_id) {
uint8_t pin_check_result;
uint8_t index;
if ( HAL_LL_PIN_NC == (pin_check_result = hal_ll_adc_check_pins( pin, &index, handle_map )) ) {
return HAL_LL_ADC_WRONG_PIN;
};
switch ( resolution )
{
case HAL_LL_ADC_RESOLUTION_10_BIT:
hal_ll_adc_hw_specifics_map[pin_check_result].resolution = HAL_LL_ADC_RESOLUTION_10_BIT;
break;
default:
return HAL_LL_ADC_UNSUPPORTED_RESOLUTION;
}
switch ( vref_input )
{
case HAL_LL_ADC_VREF_INTERNAL:
hal_ll_adc_hw_specifics_map[pin_check_result].vref_input = HAL_LL_ADC_VREF_INTERNAL;
break;
case HAL_LL_ADC_VREF_EXTERNAL:
hal_ll_adc_hw_specifics_map[pin_check_result].vref_input = HAL_LL_ADC_VREF_EXTERNAL;
break;
case HAL_LL_ADC_VREF_DEFAULT:
hal_ll_adc_hw_specifics_map[pin_check_result].vref_input = HAL_LL_ADC_VREF_DEFAULT;
break;
default:
return HAL_LL_ADC_UNSUPPORTED_VREF;
}
if ( hal_ll_adc_hw_specifics_map[pin_check_result].pin != pin )
{
hal_ll_adc_map_pin( pin_check_result, &index );
handle_map[pin_check_result]->init_ll_state = false;
}
*hal_module_id = pin_check_result;
hal_ll_module_state[pin_check_result].hal_ll_adc_handle = (handle_t *)&hal_ll_adc_hw_specifics_map[pin_check_result].base;
handle_map[pin_check_result]->hal_ll_adc_handle = (handle_t *)&hal_ll_module_state[pin_check_result].hal_ll_adc_handle;
return HAL_LL_ADC_SUCCESS;
}
hal_ll_err_t hal_ll_module_configure_adc(handle_t *handle) {
hal_ll_adc_hw_specifics_map_local = hal_ll_get_specifics(hal_ll_adc_get_module_state_address);
hal_ll_adc_handle_register_t *hal_handle = (hal_ll_adc_handle_register_t *)*handle;
uint8_t pin_check_result = hal_ll_adc_hw_specifics_map_local->module_index;
hal_ll_adc_init( hal_ll_adc_hw_specifics_map_local );
hal_ll_module_state[pin_check_result].hal_ll_adc_handle = (handle_t *)&hal_ll_adc_hw_specifics_map[pin_check_result].base;
hal_ll_module_state[pin_check_result].init_ll_state = true;
hal_handle->init_ll_state = true;
return HAL_LL_ADC_SUCCESS;
}
hal_ll_err_t hal_ll_adc_set_resolution(handle_t *handle, hal_ll_adc_resolution_t resolution) {
low_level_handle = hal_ll_adc_get_handle;
hal_ll_adc_hw_specifics_map_local = hal_ll_get_specifics(hal_ll_adc_get_module_state_address);
low_level_handle->init_ll_state = false;
switch ( resolution ) {
case HAL_LL_ADC_RESOLUTION_10_BIT:
hal_ll_adc_hw_specifics_map_local->resolution = HAL_LL_ADC_RESOLUTION_10_BIT;
break;
default:
return HAL_LL_ADC_UNSUPPORTED_RESOLUTION;
}
hal_ll_adc_init( hal_ll_adc_hw_specifics_map_local );
low_level_handle->init_ll_state = true;
return HAL_LL_ADC_SUCCESS;
}
hal_ll_err_t hal_ll_adc_set_vref_input(handle_t *handle, hal_ll_adc_voltage_reference_t vref_input) {
low_level_handle = hal_ll_adc_get_handle;
hal_ll_adc_hw_specifics_map_local = hal_ll_get_specifics(hal_ll_adc_get_module_state_address);
low_level_handle->init_ll_state = false;
switch( vref_input ) {
case HAL_LL_ADC_VREF_INTERNAL:
hal_ll_adc_hw_specifics_map_local->vref_input = HAL_LL_ADC_VREF_INTERNAL;
break;
case HAL_LL_ADC_VREF_EXTERNAL:
hal_ll_adc_hw_specifics_map_local->vref_input = HAL_LL_ADC_VREF_EXTERNAL;
break;
case HAL_LL_ADC_VREF_DEFAULT:
hal_ll_adc_hw_specifics_map_local->vref_input = HAL_LL_ADC_VREF_DEFAULT;
break;
default:
return HAL_LL_ADC_UNSUPPORTED_VREF;
}
hal_ll_adc_init( hal_ll_adc_hw_specifics_map_local );
low_level_handle->init_ll_state = true;
return HAL_LL_ADC_SUCCESS;
}
void hal_ll_adc_set_vref_value(handle_t *handle, float vref_value) {
low_level_handle = hal_ll_adc_get_handle;
hal_ll_adc_hw_specifics_map_local = hal_ll_get_specifics(hal_ll_adc_get_module_state_address);
if( HAL_LL_ADC_VREF_INTERNAL == hal_ll_adc_hw_specifics_map_local->vref_input ) {
hal_ll_adc_hw_specifics_map_local->vref_value = HAL_LL_ADC_INTERNAL_VREF_VALUE;
} else {
hal_ll_adc_hw_specifics_map_local->vref_value = vref_value;
}
}
hal_ll_err_t hal_ll_adc_read(handle_t *handle, uint16_t *readDatabuf) {
const hal_ll_adc_base_handle_t *base = hal_ll_adc_get_base_handle;
uint16_t volatile adcl_var = 0, adch_var = 0;
set_reg_bit( base->adcsra, HAL_LL_ADC_ADCSRA_ADSC_BIT ); // ADSC - ADC Start Conversion bit
while( check_reg_bit( base->adcsra, HAL_LL_ADC_ADCSRA_ADSC_BIT )); // Wait for conversion to end
// Documentation: "ADCL must be read first, then ADCH, to ensure that the content of the Data Registers belongs to the same conversion."
adcl_var = read_reg( base->adcl );
adch_var = ( read_reg( base->adch )) << 8;
*readDatabuf = ( adch_var | adcl_var );
return HAL_LL_ADC_SUCCESS;
}
void hal_ll_adc_close(handle_t *handle) {
hal_ll_adc_hw_specifics_map_t *hal_ll_adc_hw_specifics_map_local = hal_ll_get_specifics(hal_ll_adc_get_module_state_address);
low_level_handle = hal_ll_adc_get_handle;
if( HAL_LL_MODULE_ERROR != hal_ll_adc_hw_specifics_map_local->base->adcl ) {
hal_ll_adc_hw_specifics_map_local->vref_input = HAL_LL_ADC_VREF_DEFAULT;
hal_ll_adc_hw_specifics_map_local->vref_value = 0;
hal_ll_adc_hw_specifics_map_local->resolution = HAL_LL_ADC_RESOLUTION_10_BIT;
low_level_handle->hal_ll_adc_handle = NULL;
low_level_handle->hal_drv_adc_handle = NULL;
low_level_handle->init_ll_state = false;
hal_ll_adc_hw_specifics_map_local->pin = HAL_LL_PIN_NC;
}
}
// ----------------------------------------------- PRIVATE FUNCTION DEFINITIONS
static hal_ll_pin_name_t hal_ll_adc_check_pins(hal_ll_pin_name_t pin, uint8_t *index, hal_ll_adc_handle_register_t *handle_map) {
uint8_t pin_index = 0;
static const uint16_t adc_map_size = ( sizeof( _adc_map ) / sizeof( hal_ll_adc_pin_map_t ) );
if ( HAL_LL_PIN_NC == pin ) {
return HAL_LL_PIN_NC;
}
for ( pin_index = 0; pin_index < adc_map_size; pin_index++ ) {
if ( _adc_map[pin_index].pin == pin ) {
*index = pin_index;
return _adc_map[ pin_index ].module_index;
}
}
return HAL_LL_PIN_NC;
}
static void hal_ll_adc_map_pin(uint8_t module_index, uint8_t *index) {
// Map new pins
hal_ll_adc_hw_specifics_map[module_index].pin = _adc_map[*index ].pin;
hal_ll_adc_hw_specifics_map[module_index].channel = _adc_map[ *index ].channel;
}
static hal_ll_adc_hw_specifics_map_t *hal_ll_get_specifics(handle_t handle) {
uint8_t hal_ll_module_count = sizeof(hal_ll_module_state) / (sizeof(hal_ll_adc_handle_register_t));
static uint8_t hal_ll_module_error = hal_ll_module_count;
while ( hal_ll_module_count-- ) {
if (hal_ll_adc_get_base_from_hal_handle == hal_ll_adc_hw_specifics_map[hal_ll_module_count].base) {
return &hal_ll_adc_hw_specifics_map[hal_ll_module_count];
}
}
return &hal_ll_adc_hw_specifics_map[hal_ll_module_error];
}
static void _hal_ll_adc_hw_init( hal_ll_adc_hw_specifics_map_t *map ) {
const hal_ll_adc_base_handle_t *base = hal_ll_adc_get_base_struct(map->base);
clear_reg_bits( base->admux, HAL_LL_ADC_ADMUX_MASK );
write_reg( base->adcsra, HAL_LL_ADC_ADCSRA_ADEN | HAL_LL_ADC_ADCSRA_ADPS );
}
static void hal_ll_adc_init( hal_ll_adc_hw_specifics_map_t *map ) {
const hal_ll_adc_base_handle_t *base = hal_ll_adc_get_base_struct(map->base);
_hal_ll_adc_hw_init( map );
if( HAL_LL_ADC_VREF_INTERNAL == map->vref_input )
set_reg_bits( base->admux, HAL_LL_ADC_ADMUX_REFS_INTERNAL );
#ifdef HAS_ADC_MUX5_BIT
if( 8 > map->channel ) {
set_reg_bits( base->admux, map->channel );
} else {
/*
* When ADC channel number is higher than 7 (0b1000 - 0b1111), the lower 3 bits
* are written to ADMUX[MUX2:0] and the highest bit is written to ADCSRB[MUX5].
*/
set_reg_bits( base->admux, map->channel & 0x7 );
set_reg_bit( base->adcsrb, HAL_LL_ADC_ADCSRB_MUX5 );
}
#else
set_reg_bits( base->admux, map->channel );
#endif
}
// ------------------------------------------------------------------------- END