hal_dac.h 12 KB

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