forked from MikroElektronika/mikrosdk_v2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhal_adc.h
363 lines (338 loc) · 12.7 KB
/
hal_adc.h
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
/****************************************************************************
**
** Copyright (C) 2021 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_adc.h
* @brief This file contains all the functions prototypes for the ADC library.
*/
#ifndef _HAL_ADC_H_
#define _HAL_ADC_H_
#ifdef __cplusplus
extern "C"{
#endif
#include "hal_target.h"
/**
* @brief ADC HAL level handle.
* @details The context for storing ADC level object handlers.
* Contains specific hardware module handle and
* module init state.
* @warning User is not to change these values or
* unexpected behaviour may occur.
*/
typedef struct
{
handle_t *hal_adc_handle; /*!< ADC HAL level handle */
bool init_state; /*!< ADC HAL object init state */
} hal_adc_handle_register_t;
/**
* @details Return values.
*/
typedef enum
{
HAL_ADC_SUCCESS = 0, /*!< Success. */
HAL_ADC_WRONG_PIN, /*!< Wrong pin selected. */
HAL_ADC_MODULE_ERROR, /*!< Object wasn't closed. */
HAL_ADC_UNSUPPORTED_RESOLUTION = 1100, /*!< Wrong resolution error. */
HAL_ADC_UNSUPPORTED_VREF, /*!< Wrong voltage referebnce source error. */
HAL_ADC_WRONG_CHANNEL, /*!< Wrong channel error. */
HAL_ADC_ERROR = (-1) /*!< Error. */
} hal_adc_err_t;
/**
* @details Predefined enum values for ADC resolution.
*/
typedef enum
{
HAL_ADC_RESOLUTION_NOT_SET = 0, /*!< Resolution not set. */
HAL_ADC_RESOLUTION_6_BIT, /*!< 6 bit resolution. */
HAL_ADC_RESOLUTION_8_BIT, /*!< 8 bit resolution. */
HAL_ADC_RESOLUTION_10_BIT, /*!< 10 bit resolution. */
HAL_ADC_RESOLUTION_12_BIT, /*!< 12 bit resolution. */
HAL_ADC_RESOLUTION_14_BIT, /*!< 14 bit resolution. */
HAL_ADC_RESOLUTION_16_BIT, /*!< 16 bit resolution. */
/*!< Default resolution. */
HAL_ADC_RESOLUTION_DEFAULT = HAL_ADC_RESOLUTION_MEMAKE
} hal_adc_resolution_t;
/**
* @details Predefined enum values for ADC voltage reference source.
*/
typedef enum
{
HAL_ADC_VREF_EXTERNAL = 0, /*!< External voltage reference source. */
HAL_ADC_VREF_INTERNAL, /*!< Internal voltage reference source. */
HAL_ADC_VREF_DEFAULT = HAL_ADC_VREF_EXTERNAL /*!< Default (external voltage reference source) */
} hal_adc_vref_t;
/**
* @brief ADC HAL initialization configuration structure, consisted of the following fields :
* @details User needs to specifiy values herein.
* @note Take into consideration that any value can be changed explicitly by the user.
*
* @b Example
* @code
* // ADC HAL configuration structure.
* static hal_adc_config_t hal_adc_cfg;
*
* // Fill structure with default values.
* hal_adc_configure_default( &hal_adc_cfg );
*
* // Specify desired ADC pin.
* hal_adc_cfg.pin = MIKROBUS_1_AN;
*
* // Set 12 bit ADC resolution.
* hal_adc_cfg.resolution = ANALOG_IN_RESOLUTION_12_BIT;
*
* // Set external voltage reference source
* hal_adc_cfg.vref_input = ANALOG_IN_VREF_EXTERNAL;
* @endcode
*/
typedef struct
{
hal_pin_name_t pin; /*!< ADC HAL pin name. */
hal_adc_resolution_t resolution; /*!< ADC HAL resolution. See #hal_adc_resolution_t for valid resolution values. */
hal_adc_vref_t vref_input; /*!< ADC HAL voltage reference voltage source. See #hal_adc_vref_t for valid voltage reference source values. */
float vref_value; /*!< ADC HAL voltage reference voltage value. */
} hal_adc_config_t;
/**
* @brief ADC HAL context structure, consisted of the following fields :
* @details ADC HAL context structure, used by every other function later on.
*
* @b Example
* @code
* // ADC HAL context structure.
* static hal_adc_t hal_adc;
* @endcode
*/
typedef struct
{
handle_t handle; /*!< ADC HAL level handle */
hal_adc_config_t config; /*!< ADC HAL object init state */
} hal_adc_t;
/*!
* @addtogroup pergroup Microcontroller Peripherals
* @{
*/
/*!
* @addtogroup halgroup Hardware Abstraction Layer
* @brief This section includes the mikroSDK API Reference for the Hardware Abstraction Layer.
* @details Hardware Abstraction Layer ( @b HAL ) is a layer
* of code that allows driver interaction with a hardware device at a
* general or abstract level rather than at a detailed hardware level.
* @b HAL can be called from either the @b DRV layer or directly from user code.
* In either case, the calling program can interact with the device in a
* more general way than it would otherwise.
*
* @note All @b mikroSDK.HAL layer prototypes are architecture independent
* with the goal of having one unique code base supporting cross-platform
* devices.
* @{
*/
/*!
* @addtogroup haladcgroup ADC HAL
* @brief ADC Hardware Abstraction Layer API Reference.
* @details API for configuring and manipulating ADC HAL module.
* @{
*/
/**
* @brief Configure ADC HAL configuration structure..
* @details Configures ADC configuration structure to default initialization values.
* Take into consideration that this is just
* structure variable initial values setting.
* Values need to be redefined by user.
* @param[in,out] config ADC HAL layer configuration settings.
* See #hal_adc_config_t structure definition for detailed explanation.
*
* @b Default @b values:
* Function | Default value |
* --------------------|---------------------------------------|
* Input pin | @p HAL_PIN_NC (invalid pin) |
* Resolution | 12 bit |
* Vref input | External voltage reference source |
* Vref value | -1 (invalid reference voltage value) |
* @return Nothing.
*
* @b Example
* @code
* // ADC HAL configuration structure.
* static hal_adc_config_t hal_adc_cfg;
*
* // Fill structure with default values.
* hal_adc_configure_default( &hal_adc_cfg );
* @endcode
*/
void hal_adc_configure_default( hal_adc_config_t *config );
/**
* @brief Open the ADC HAL layer object on selected pin.
* @details Function allocates memory needed for ADC HAL
* and pin for specified object.
* @param[in,out] handle ADC handle.
* @param[in] hal_obj_open_state ADC state, is it open or not.
* @return The function can return one of the values defined by
* #hal_adc_err_t, which is size dependant on the architecture.
*
* @b Example
* @code
* // ADC HAL context structure.
* static hal_adc_t hal_adc;
*
* // ADC HAL configuration structure.
* static hal_adc_config_t hal_adc_cfg;
*
* // Fill structure with default values.
* adc_configure_default( &hal_adc_cfg );
*
* // Specify desired ADC pin.
* hal_adc_cfg.pin. = MIKROBUS_1_AN;
*
* // Allocate resources for ADC HAL.
* hal_adc_open( &hal_adc->handle, true );
* @endcode
*/
err_t hal_adc_open( handle_t *handle, bool hal_obj_open_state );
/**
* @brief Set ADC HAL sample resolution.
* @details Initializes ADC HAL on the hardware level and sets
* sample resolution to specified \p config->resolution value.
* @param[in] handle ADC handle.
* @param[in] config ADC HAL configuration structure.
* See #hal_adc_config_t structure definition for detailed explanation.
* @return The function can return one of the values defined by
* #hal_adc_err_t, which is size dependant on the architecture.
* @pre Make sure that adequate memory has been allocated beforehand.
* See #hal_adc_open definition for detailed explanation.
* @warning If desired resolution can not be achieved,
* error will be returned. Make sure to poll the return value in your code.
*
* @b Example
* @code
* // Set resolution.
* hal_adc_set_resolution( &hal_adc->handle, &hal_adc->config );
* @endcode
*/
err_t hal_adc_set_resolution( handle_t *handle, hal_adc_config_t *config );
/**
* @brief Set ADC HAL reference voltage source to desired value.
* @details Initializes ADC HAL on the hardware level and sets
* sample reference voltage source to specified \p config->vref_input value.
* @param[in] handle ADC handle.
* @param[in] config ADC HAL configuration structure.
* See #hal_adc_config_t structure definition for detailed explanation.
* @return The function can return one of the values defined by
* #hal_adc_err_t, which is size dependant on the architecture.
* @pre Make sure that adequate memory has been allocated beforehand.
* See #hal_adc_open definition for detailed explanation.
* @warning If desired reference voltage source can not be achieved,
* error will be returned. Make sure to poll the return value in your code.
*
* @b Example
* @code
* // Set voltage reference source.
* hal_adc_set_vref_input( &hal_adc->handle, &hal_adc->config );
* @endcode
*/
err_t hal_adc_set_vref_input( handle_t *handle, hal_adc_config_t *config );
/**
* @brief Set ADC HAL reference voltage value to desired value.
* @details Sets sample reference voltage value to
* specified \p config->vref_value value used later for calculating voltage.
* @param[in] handle ADC handle.
* @param[in] config ADC HAL configuration structure.
* See #hal_adc_config_t structure definition for detailed explanation.
* @return Nothing.
* @pre Make sure that adequate memory has been allocated beforehand.
* See #hal_adc_open definition for detailed explanation.
* @warning If desired reference voltage value is not set,
* calculated voltage value on AN pin will be wrong.
*
* @b Example
* @code
* // Set voltage reference value.
* hal_adc_set_vref_value( &hal_adc->handle, &hal_adc->config );
* @endcode
*/
void hal_adc_set_vref_value( handle_t *handle, hal_adc_config_t *config );
/**
* @brief Read analog value on pin.
* @param[in] handle ADC handle.
* @param[out] readDatabuf Data buffer to place read data in.
* @return The function can return one of the values defined by
* #hal_adc_err_t, which is size dependant on the architecture.
* @pre Make sure that adequate memory has been allocated beforehand.
* See #hal_adc_open definition for detailed explanation.
*
* @b Example
* @code
* hal_adc_read( &hal_adc->handle, readDatabuf );
* @endcode
*/
err_t hal_adc_read( handle_t *handle, uint16_t *readDatabuf );
/**
* @brief Read analog voltage value on pin.
* @param[in] handle ADC handle.
* @param[out] readDatabuf Data buffer to place read data in.
* @return The function can return one of the values defined by
* #hal_adc_err_t, which is size dependant on the architecture.
* @pre Make sure that adequate memory has been allocated beforehand.
* See #hal_adc_open definition for detailed explanation.
*
* @b Example
* @code
* hal_adc_read_voltage( &hal_adc->handle, readDatabuf );
* @endcode
*/
err_t hal_adc_read_voltage( handle_t *handle, float *readDatabuf );
/**
* @brief Close ADC HAL layer object.
* @details Closes ADC HAL layer object,
* clears all buffers used by object and clears config.
* @param[in,out] handle ADC handle.
* @return Nothing.
*
* @b Example
* @code
* // Close the ADC HAL object handler.
* hal_adc_close( &hal_adc->handle );
* @endcode
*/
err_t hal_adc_close( handle_t *handle );
/*! @} */ // haladcgroup
/*! @} */ // halgroup
/*! @} */ // pergroup
#ifdef __cplusplus
}
#endif
#endif // _HAL_ADC_H_
// ------------------------------------------------------------------------- END