ChibiOS/HAL 9.0.0
hal_dac.h
Go to the documentation of this file.
1/*
2 ChibiOS - Copyright (C) 2006..2024 Giovanni Di Sirio
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15*/
16
17/**
18 * @file hal_dac.h
19 * @brief DAC Driver macros and structures.
20 *
21 * @addtogroup DAC
22 * @{
23 */
24
25#ifndef HAL_DAC_H
26#define HAL_DAC_H
27
28#if (HAL_USE_DAC == TRUE) || defined(__DOXYGEN__)
29
30/*===========================================================================*/
31/* Driver constants. */
32/*===========================================================================*/
33
34/*===========================================================================*/
35/* Driver pre-compile time settings. */
36/*===========================================================================*/
37
38/**
39 * @name DAC configuration options
40 * @{
41 */
42/**
43 * @brief Support for thread synchronization API.
44 */
45#if !defined(DAC_USE_SYNCHRONIZATION) || defined(__DOXYGEN__)
46#if !defined(DAC_USE_WAIT) || defined(__DOXYGEN__)
47#define DAC_USE_SYNCHRONIZATION FALSE
48#else
49#define DAC_USE_SYNCHRONIZATION DAC_USE_WAIT
50#endif
51#endif
52
53/**
54 * @brief Enables the @p dacAcquireBus() and @p dacReleaseBus() APIs.
55 * @note Disabling this option saves both code and data space.
56 */
57#if !defined(DAC_USE_MUTUAL_EXCLUSION) || defined(__DOXYGEN__)
58#define DAC_USE_MUTUAL_EXCLUSION TRUE
59#endif
60/** @} */
61
62/*===========================================================================*/
63/* Derived constants and error checks. */
64/*===========================================================================*/
65
66/*===========================================================================*/
67/* Driver data structures and types. */
68/*===========================================================================*/
69
70/**
71 * @brief Driver state machine possible states.
72 */
73typedef enum {
74 DAC_UNINIT = 0, /**< Not initialized. */
75 DAC_STOP = 1, /**< Stopped. */
76 DAC_READY = 2, /**< Ready. */
77 DAC_ACTIVE = 3, /**< Exchanging data. */
78 DAC_COMPLETE = 4, /**< Asynchronous operation complete. */
79 DAC_ERROR = 5 /**< Error. */
81
82/**
83 * @brief Type of a structure representing an DAC driver.
84 */
86
87/**
88 * @brief Type of a structure representing an DAC driver configuration.
89 */
91
92/**
93 * @brief Type of a DAC conversion group.
94 */
96
97/* Including the low level driver header, it exports information required
98 for completing types.*/
99#include "hal_dac_lld.h"
100
101/**
102 * @brief DAC notification callback type.
103 *
104 * @param[in] dacp pointer to the @p DACDriver object triggering the
105 */
106typedef void (*daccallback_t)(DACDriver *dacp);
107
108/**
109 * @brief DAC error callback type.
110 *
111 * @param[in] dacp pointer to the @p DACDriver object triggering the
112 * callback
113 * @param[in] err DAC error code
114 */
115typedef void (*dacerrorcallback_t)(DACDriver *dacp, dacerror_t err);
116
117/**
118 * @brief DAC Conversion group structure.
119 */
121 /**
122 * @brief Number of DAC channels.
123 */
124 uint32_t num_channels;
125 /**
126 * @brief Operation complete callback or @p NULL.
127 */
129 /**
130 * @brief Error handling callback or @p NULL.
131 */
133 /* End of the mandatory fields.*/
135};
136
137/**
138 * @brief Driver configuration structure.
139 */
141 /* End of the mandatory fields.*/
143};
144
145/**
146 * @brief Structure representing a DAC driver.
147 */
149 /**
150 * @brief Driver state.
151 */
153 /**
154 * @brief Conversion group.
155 */
157 /**
158 * @brief Samples buffer pointer.
159 */
161 /**
162 * @brief Samples buffer size.
163 */
164 size_t depth;
165 /**
166 * @brief Current configuration data.
167 */
169#if (DAC_USE_SYNCHRONIZATION == TRUE) || defined(__DOXYGEN__)
170 /**
171 * @brief Waiting thread.
172 */
174#endif /* DAC_USE_SYNCHRONIZATION */
175#if (DAC_USE_MUTUAL_EXCLUSION == TRUE) || defined(__DOXYGEN__)
176 /**
177 * @brief Mutex protecting the bus.
178 */
180#endif /* DAC_USE_MUTUAL_EXCLUSION */
181#if defined(DAC_DRIVER_EXT_FIELDS)
182 DAC_DRIVER_EXT_FIELDS
183#endif
184 /* End of the mandatory fields.*/
186};
187
188/*===========================================================================*/
189/* Driver macros. */
190/*===========================================================================*/
191
192/**
193 * @name Low level driver helper macros
194 * @{
195 */
196/**
197 * @brief Buffer state.
198 * @note This function is meant to be called from the DAC callback only.
199 *
200 * @param[in] dacp pointer to the @p DACDriver object
201 * @return The buffer state.
202 * @retval false if the driver filled/sent the first half of the
203 * buffer.
204 * @retval true if the driver filled/sent the second half of the
205 * buffer.
206 *
207 * @special
208 */
209#define dacIsBufferComplete(dacp) ((bool)((dacp)->state == DAC_COMPLETE))
210
211#if (DAC_USE_SYNCHRONIZATION == TRUE) || defined(__DOXYGEN__)
212/**
213 * @brief Waits for operation completion.
214 * @details This function waits for the driver to complete the current
215 * operation.
216 * @pre An operation must be running while the function is invoked.
217 * @note No more than one thread can wait on a DAC driver using
218 * this function.
219 *
220 * @param[in] dacp pointer to the @p DACDriver object
221 *
222 * @notapi
223 */
224#define _dac_wait_s(dacp) osalThreadSuspendS(&(dacp)->thread)
225
226/**
227 * @brief Resumes a thread waiting for a conversion completion.
228 *
229 * @param[in] dacp pointer to the @p DACDriver object
230 *
231 * @notapi
232 */
233#define _dac_reset_i(dacp) osalThreadResumeI(&(dacp)->thread, MSG_RESET)
234
235/**
236 * @brief Resumes a thread waiting for a conversion completion.
237 *
238 * @param[in] dacp pointer to the @p DACDriver object
239 *
240 * @notapi
241 */
242#define _dac_reset_s(dacp) osalThreadResumeS(&(dacp)->thread, MSG_RESET)
243
244/**
245 * @brief Wakes up the waiting thread.
246 *
247 * @param[in] dacp pointer to the @p DACDriver object
248 *
249 * @notapi
250 */
251#define _dac_wakeup_isr(dacp) { \
252 osalSysLockFromISR(); \
253 osalThreadResumeI(&(dacp)->thread, MSG_OK); \
254 osalSysUnlockFromISR(); \
255}
256
257/**
258 * @brief Wakes up the waiting thread with a timeout message.
259 *
260 * @param[in] dacp pointer to the @p DACDriver object
261 *
262 * @notapi
263 */
264#define _dac_timeout_isr(dacp) { \
265 osalSysLockFromISR(); \
266 osalThreadResumeI(&(dacp)->thread, MSG_TIMEOUT); \
267 osalSysUnlockFromISR(); \
268}
269
270#else /* !DAC_USE_SYNCHRONIZATION */
271#define _dac_wait_s(dacp)
272#define _dac_reset_i(dacp)
273#define _dac_reset_s(dacp)
274#define _dac_wakeup_isr(dacp)
275#define _dac_timeout_isr(dacp)
276#endif /* !DAC_USE_SYNCHRONIZATION */
277
278/**
279 * @brief Common ISR code, half buffer event.
280 * @details This code handles the portable part of the ISR code:
281 * - Callback invocation.
282 * .
283 * @note This macro is meant to be used in the low level drivers
284 * implementation only.
285 *
286 * @param[in] dacp pointer to the @p DACDriver object
287 *
288 * @notapi
289 */
290#define _dac_isr_half_code(dacp) { \
291 if ((dacp)->grpp->end_cb != NULL) { \
292 (dacp)->grpp->end_cb(dacp); \
293 } \
294}
295
296/**
297 * @brief Common ISR code, full buffer event.
298 * @details This code handles the portable part of the ISR code:
299 * - Callback invocation.
300 * - Driver state transitions.
301 * .
302 * @note This macro is meant to be used in the low level drivers
303 * implementation only.
304 *
305 * @param[in] dacp pointer to the @p DACDriver object
306 *
307 * @notapi
308 */
309#define _dac_isr_full_code(dacp) { \
310 if ((dacp)->grpp->end_cb) { \
311 (dacp)->state = DAC_COMPLETE; \
312 (dacp)->grpp->end_cb(dacp); \
313 if ((dacp)->state == DAC_COMPLETE) \
314 (dacp)->state = DAC_ACTIVE; \
315 } \
316 _dac_wakeup_isr(dacp); \
317}
318
319/**
320 * @brief Common ISR code, error event.
321 * @details This code handles the portable part of the ISR code:
322 * - Callback invocation.
323 * - Waiting thread timeout signalling, if any.
324 * - Driver state transitions.
325 * .
326 * @note This macro is meant to be used in the low level drivers
327 * implementation only.
328 *
329 * @param[in] dacp pointer to the @p DACDriver object
330 * @param[in] err platform dependent error code
331 *
332 * @notapi
333 */
334#define _dac_isr_error_code(dacp, err) { \
335 dac_lld_stop_conversion(dacp); \
336 if ((dacp)->grpp->error_cb != NULL) { \
337 (dacp)->state = DAC_ERROR; \
338 (dacp)->grpp->error_cb(dacp, err); \
339 if ((dacp)->state == DAC_ERROR) \
340 (dacp)->state = DAC_READY; \
341 } \
342 (dacp)->grpp = NULL; \
343 _dac_timeout_isr(dacp); \
344}
345/** @} */
346
347/*===========================================================================*/
348/* External declarations. */
349/*===========================================================================*/
350
351#ifdef __cplusplus
352extern "C" {
353#endif
354 void dacInit(void);
355 void dacObjectInit(DACDriver *dacp);
356 msg_t dacStart(DACDriver *dacp, const DACConfig *config);
357 void dacStop(DACDriver *dacp);
359 dacchannel_t channel,
360 dacsample_t sample);
362 dacsample_t *samples, size_t depth);
364 dacsample_t *samples, size_t depth);
365 void dacStopConversion(DACDriver *dacp);
366 void dacStopConversionI(DACDriver *dacp);
367#if DAC_USE_SYNCHRONIZATION
368 msg_t dacConvert(DACDriver *dacp, const DACConversionGroup *grpp,
369 dacsample_t *samples, size_t depth);
372#endif /* DAC_USE_SYNCHRONIZATION */
373#if DAC_USE_MUTUAL_EXCLUSION
374 void dacAcquireBus(DACDriver *dacp);
375 void dacReleaseBus(DACDriver *dacp);
376#endif
377#ifdef __cplusplus
378}
379#endif
380
381#endif /* HAL_USE_DAC == TRUE */
382
383#endif /* HAL_DAC_H */
384
385/** @} */
void(* daccallback_t)(DACDriver *dacp)
DAC notification callback type.
Definition hal_dac.h:106
msg_t dacSynchronizeS(DACDriver *dacp, sysinterval_t timeout)
Synchronize to a conversion completion.
Definition hal_dac.c:378
#define dac_lld_conversion_group_fields
Low level fields of the DAC group configuration structure.
uint16_t dacsample_t
Type representing a DAC sample.
Definition hal_dac_lld.h:73
struct hal_dac_driver DACDriver
Type of a structure representing an DAC driver.
Definition hal_dac.h:85
void dacStop(DACDriver *dacp)
Deactivates the DAC peripheral.
Definition hal_dac.c:135
uint32_t dacchannel_t
Type of a DAC channel index.
Definition hal_dac_lld.h:68
msg_t dacStartConversionI(DACDriver *dacp, const DACConversionGroup *grpp, dacsample_t *samples, size_t depth)
Starts a DAC conversion.
Definition hal_dac.c:230
void dacReleaseBus(DACDriver *dacp)
Releases exclusive access to the DAC bus.
Definition hal_dac.c:449
void dacStopConversion(DACDriver *dacp)
Stops an ongoing conversion.
Definition hal_dac.c:274
void dacAcquireBus(DACDriver *dacp)
Gains exclusive access to the DAC bus.
Definition hal_dac.c:433
void dacObjectInit(DACDriver *dacp)
Initializes the standard part of a DACDriver structure.
Definition hal_dac.c:68
msg_t dacStartConversion(DACDriver *dacp, const DACConversionGroup *grpp, dacsample_t *samples, size_t depth)
Starts a DAC conversion.
Definition hal_dac.c:194
void dacInit(void)
DAC Driver initialization.
Definition hal_dac.c:56
void(* dacerrorcallback_t)(DACDriver *dacp, dacerror_t err)
DAC error callback type.
Definition hal_dac.h:115
void dacStopConversionI(DACDriver *dacp)
Stops an ongoing conversion.
Definition hal_dac.c:304
dacerror_t
Possible DAC failure causes.
Definition hal_dac_lld.h:80
msg_t dacSynchronize(DACDriver *dacp, sysinterval_t timeout)
Synchronize to a conversion completion.
Definition hal_dac.c:410
struct hal_dac_config DACConfig
Type of a structure representing an DAC driver configuration.
Definition hal_dac.h:90
msg_t dacStart(DACDriver *dacp, const DACConfig *config)
Configures and activates the DAC peripheral.
Definition hal_dac.c:95
dacstate_t
Driver state machine possible states.
Definition hal_dac.h:73
msg_t dacPutChannelX(DACDriver *dacp, dacchannel_t channel, dacsample_t sample)
Outputs a value directly on a DAC channel.
Definition hal_dac.c:160
#define dac_lld_config_fields
Low level fields of the DAC configuration structure.
Definition hal_dac_lld.h:99
#define dac_lld_driver_fields
Low level fields of the DAC driver structure.
Definition hal_dac_lld.h:92
struct hal_dac_conversion_group DACConversionGroup
Type of a DAC conversion group.
Definition hal_dac.h:95
msg_t dacConvert(DACDriver *dacp, const DACConversionGroup *grpp, dacsample_t *samples, size_t depth)
Performs a DAC conversion.
Definition hal_dac.c:345
@ DAC_UNINIT
Definition hal_dac.h:74
@ DAC_READY
Definition hal_dac.h:76
@ DAC_ACTIVE
Definition hal_dac.h:77
@ DAC_COMPLETE
Definition hal_dac.h:78
@ DAC_STOP
Definition hal_dac.h:75
@ DAC_ERROR
Definition hal_dac.h:79
int32_t msg_t
Type of a message.
Definition osal.h:159
uint32_t sysinterval_t
Type of system time interval.
Definition osal.h:169
void * thread_reference_t
Type of a thread reference.
Definition osal.h:186
uint32_t mutex_t
Type of a mutex.
Definition osal.h:229
PLATFORM DAC subsystem low level driver header.
Driver configuration structure.
Definition hal_dac.h:140
DAC Conversion group structure.
Definition hal_dac.h:120
uint32_t num_channels
Number of DAC channels.
Definition hal_dac.h:124
dacerrorcallback_t error_cb
Error handling callback or NULL.
Definition hal_dac.h:132
daccallback_t end_cb
Operation complete callback or NULL.
Definition hal_dac.h:128
Structure representing a DAC driver.
Definition hal_dac.h:148
const DACConversionGroup * grpp
Conversion group.
Definition hal_dac.h:156
size_t depth
Samples buffer size.
Definition hal_dac.h:164
dacsample_t * samples
Samples buffer pointer.
Definition hal_dac.h:160
thread_reference_t thread
Waiting thread.
Definition hal_dac.h:173
const DACConfig * config
Current configuration data.
Definition hal_dac.h:168
dacstate_t state
Driver state.
Definition hal_dac.h:152
mutex_t mutex
Mutex protecting the bus.
Definition hal_dac.h:179