ChibiOS/HAL  6.1.0
hal_dac.h
Go to the documentation of this file.
1 /*
2  ChibiOS - Copyright (C) 2006..2018 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 Enables synchronous APIs.
44  * @note Disabling this option saves both code and data space.
45  */
46 #if !defined(DAC_USE_WAIT) || defined(__DOXYGEN__)
47 #define DAC_USE_WAIT TRUE
48 #endif
49 
50 /**
51  * @brief Enables the @p dacAcquireBus() and @p dacReleaseBus() APIs.
52  * @note Disabling this option saves both code and data space.
53  */
54 #if !defined(DAC_USE_MUTUAL_EXCLUSION) || defined(__DOXYGEN__)
55 #define DAC_USE_MUTUAL_EXCLUSION TRUE
56 #endif
57 /** @} */
58 
59 /*===========================================================================*/
60 /* Derived constants and error checks. */
61 /*===========================================================================*/
62 
63 /*===========================================================================*/
64 /* Driver data structures and types. */
65 /*===========================================================================*/
66 
67 /**
68  * @brief Driver state machine possible states.
69  */
70 typedef enum {
71  DAC_UNINIT = 0, /**< Not initialized. */
72  DAC_STOP = 1, /**< Stopped. */
73  DAC_READY = 2, /**< Ready. */
74  DAC_ACTIVE = 3, /**< Exchanging data. */
75  DAC_COMPLETE = 4, /**< Asynchronous operation complete. */
76  DAC_ERROR = 5 /**< Error. */
77 } dacstate_t;
78 
79 #include "hal_dac_lld.h"
80 
81 /*===========================================================================*/
82 /* Driver macros. */
83 /*===========================================================================*/
84 
85 /**
86  * @name Low level driver helper macros
87  * @{
88  */
89 #if (DAC_USE_WAIT == TRUE) || defined(__DOXYGEN__)
90 /**
91  * @brief Waits for operation completion.
92  * @details This function waits for the driver to complete the current
93  * operation.
94  * @pre An operation must be running while the function is invoked.
95  * @note No more than one thread can wait on a DAC driver using
96  * this function.
97  *
98  * @param[in] dacp pointer to the @p DACDriver object
99  *
100  * @notapi
101  */
102 #define _dac_wait_s(dacp) osalThreadSuspendS(&(dacp)->thread)
103 
104 /**
105  * @brief Resumes a thread waiting for a conversion completion.
106  *
107  * @param[in] dacp pointer to the @p DACDriver object
108  *
109  * @notapi
110  */
111 #define _dac_reset_i(dacp) osalThreadResumeI(&(dacp)->thread, MSG_RESET)
112 
113 /**
114  * @brief Resumes a thread waiting for a conversion completion.
115  *
116  * @param[in] dacp pointer to the @p DACDriver object
117  *
118  * @notapi
119  */
120 #define _dac_reset_s(dacp) osalThreadResumeS(&(dacp)->thread, MSG_RESET)
121 
122 /**
123  * @brief Wakes up the waiting thread.
124  *
125  * @param[in] dacp pointer to the @p DACDriver object
126  *
127  * @notapi
128  */
129 #define _dac_wakeup_isr(dacp) { \
130  osalSysLockFromISR(); \
131  osalThreadResumeI(&(dacp)->thread, MSG_OK); \
132  osalSysUnlockFromISR(); \
133 }
134 
135 /**
136  * @brief Wakes up the waiting thread with a timeout message.
137  *
138  * @param[in] dacp pointer to the @p DACDriver object
139  *
140  * @notapi
141  */
142 #define _dac_timeout_isr(dacp) { \
143  osalSysLockFromISR(); \
144  osalThreadResumeI(&(dacp)->thread, MSG_TIMEOUT); \
145  osalSysUnlockFromISR(); \
146 }
147 
148 #else /* !DAC_USE_WAIT */
149 #define _dac_wait_s(dacp)
150 #define _dac_reset_i(dacp)
151 #define _dac_reset_s(dacp)
152 #define _dac_wakeup_isr(dacp)
153 #define _dac_timeout_isr(dacp)
154 #endif /* !DAC_USE_WAIT */
155 
156 /**
157  * @brief Common ISR code, half buffer event.
158  * @details This code handles the portable part of the ISR code:
159  * - Callback invocation.
160  * .
161  * @note This macro is meant to be used in the low level drivers
162  * implementation only.
163  *
164  * @param[in] dacp pointer to the @p DACDriver object
165  *
166  * @notapi
167  */
168 #define _dac_isr_half_code(dacp) { \
169  if ((dacp)->grpp->end_cb != NULL) { \
170  (dacp)->grpp->end_cb(dacp, (dacp)->samples, (dacp)->depth / 2); \
171  } \
172 }
173 
174 /**
175  * @brief Common ISR code, full buffer event.
176  * @details This code handles the portable part of the ISR code:
177  * - Callback invocation.
178  * - Waiting thread wakeup, if any.
179  * - Driver state transitions.
180  * .
181  * @note This macro is meant to be used in the low level drivers
182  * implementation only.
183  *
184  * @param[in] dacp pointer to the @p DACDriver object
185  *
186  * @notapi
187  */
188 #define _dac_isr_full_code(dacp) { \
189  if ((dacp)->grpp->end_cb != NULL) { \
190  if ((dacp)->depth > 1) { \
191  /* Invokes the callback passing the 2nd half of the buffer.*/ \
192  size_t half = (dacp)->depth / 2; \
193  size_t half_index = half * (dacp)->grpp->num_channels; \
194  (dacp)->grpp->end_cb(dacp, (dacp)->samples + half_index, half); \
195  } \
196  else { \
197  /* Invokes the callback passing the whole buffer.*/ \
198  (dacp)->grpp->end_cb(dacp, (dacp)->samples, (dacp)->depth); \
199  } \
200  } \
201 }
202 
203 /**
204  * @brief Common ISR code, error event.
205  * @details This code handles the portable part of the ISR code:
206  * - Callback invocation.
207  * - Waiting thread timeout signaling, if any.
208  * - Driver state transitions.
209  * .
210  * @note This macro is meant to be used in the low level drivers
211  * implementation only.
212  *
213  * @param[in] dacp pointer to the @p DACDriver object
214  * @param[in] err platform dependent error code
215  *
216  * @notapi
217  */
218 #define _dac_isr_error_code(dacp, err) { \
219  dac_lld_stop_conversion(dacp); \
220  if ((dacp)->grpp->error_cb != NULL) { \
221  (dacp)->state = DAC_ERROR; \
222  (dacp)->grpp->error_cb(dacp, err); \
223  if ((dacp)->state == DAC_ERROR) \
224  (dacp)->state = DAC_READY; \
225  } \
226  (dacp)->grpp = NULL; \
227  _dac_timeout_isr(dacp); \
228 }
229 /** @} */
230 
231 /*===========================================================================*/
232 /* External declarations. */
233 /*===========================================================================*/
234 
235 #ifdef __cplusplus
236 extern "C" {
237 #endif
238  void dacInit(void);
239  void dacObjectInit(DACDriver *dacp);
240  void dacStart(DACDriver *dacp, const DACConfig *config);
241  void dacStop(DACDriver *dacp);
242  void dacPutChannelX(DACDriver *dacp,
243  dacchannel_t channel,
244  dacsample_t sample);
245  void dacStartConversion(DACDriver *dacp, const DACConversionGroup *grpp,
246  dacsample_t *samples, size_t depth);
247  void dacStartConversionI(DACDriver *dacp, const DACConversionGroup *grpp,
248  dacsample_t *samples, size_t depth);
249  void dacStopConversion(DACDriver *dacp);
250  void dacStopConversionI(DACDriver *dacp);
251 #if DAC_USE_WAIT
252  msg_t dacConvert(DACDriver *dacp, const DACConversionGroup *grpp,
253  dacsample_t *samples, size_t depth);
254 #endif
255 #if DAC_USE_MUTUAL_EXCLUSION
256  void dacAcquireBus(DACDriver *dacp);
257  void dacReleaseBus(DACDriver *dacp);
258 #endif
259 #ifdef __cplusplus
260 }
261 #endif
262 
263 #endif /* HAL_USE_DAC == TRUE */
264 
265 #endif /* HAL_DAC_H */
266 
267 /** @} */
void dacStartConversion(DACDriver *dacp, const DACConversionGroup *grpp, dacsample_t *samples, size_t depth)
Starts a DAC conversion.
Definition: hal_dac.c:167
msg_t dacConvert(DACDriver *dacp, const DACConversionGroup *grpp, dacsample_t *samples, size_t depth)
Performs a DAC conversion.
Definition: hal_dac.c:296
Driver configuration structure.
Definition: hal_dac_lld.h:131
void dacInit(void)
DAC Driver initialization.
Definition: hal_dac.c:56
uint32_t dacchannel_t
Type of a DAC channel index.
Definition: hal_dac_lld.h:68
dacstate_t
Driver state machine possible states.
Definition: hal_dac.h:70
void dacObjectInit(DACDriver *dacp)
Initializes the standard part of a DACDriver structure.
Definition: hal_dac.c:68
void dacStartConversionI(DACDriver *dacp, const DACConversionGroup *grpp, dacsample_t *samples, size_t depth)
Starts a DAC conversion.
Definition: hal_dac.c:195
int32_t msg_t
Type of a message.
Definition: osal.h:160
void dacAcquireBus(DACDriver *dacp)
Gains exclusive access to the DAC bus.
Definition: hal_dac.c:324
void dacReleaseBus(DACDriver *dacp)
Releases exclusive access to the DAC bus.
Definition: hal_dac.c:340
Structure representing a DAC driver.
Definition: hal_dac_lld.h:139
DAC Conversion group structure.
Definition: hal_dac_lld.h:112
void dacStop(DACDriver *dacp)
Deactivates the DAC peripheral.
Definition: hal_dac.c:118
void dacPutChannelX(DACDriver *dacp, dacchannel_t channel, dacsample_t sample)
Outputs a value directly on a DAC channel.
Definition: hal_dac.c:143
void dacStopConversion(DACDriver *dacp)
Stops an ongoing conversion.
Definition: hal_dac.c:225
void dacStopConversionI(DACDriver *dacp)
Stops an ongoing conversion.
Definition: hal_dac.c:255
void dacStart(DACDriver *dacp, const DACConfig *config)
Configures and activates the DAC peripheral.
Definition: hal_dac.c:93
uint16_t dacsample_t
Type representing a DAC sample.
Definition: hal_dac_lld.h:78
PLATFORM DAC subsystem low level driver header.