hal_adc.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  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_adc.h
  15. * @brief ADC Driver macros and structures.
  16. *
  17. * @addtogroup ADC
  18. * @{
  19. */
  20. #ifndef HAL_ADC_H
  21. #define HAL_ADC_H
  22. #if (HAL_USE_ADC == TRUE) || defined(__DOXYGEN__)
  23. /*===========================================================================*/
  24. /* Driver constants. */
  25. /*===========================================================================*/
  26. /*===========================================================================*/
  27. /* Driver pre-compile time settings. */
  28. /*===========================================================================*/
  29. /**
  30. * @name ADC 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(ADC_USE_WAIT) || defined(__DOXYGEN__)
  38. #define ADC_USE_WAIT TRUE
  39. #endif
  40. /**
  41. * @brief Enables the @p adcAcquireBus() and @p adcReleaseBus() APIs.
  42. * @note Disabling this option saves both code and data space.
  43. */
  44. #if !defined(ADC_USE_MUTUAL_EXCLUSION) || defined(__DOXYGEN__)
  45. #define ADC_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. ADC_UNINIT = 0, /**< Not initialized. */
  59. ADC_STOP = 1, /**< Stopped. */
  60. ADC_READY = 2, /**< Ready. */
  61. ADC_ACTIVE = 3, /**< Converting. */
  62. ADC_COMPLETE = 4, /**< Conversion complete. */
  63. ADC_ERROR = 5 /**< Conversion error. */
  64. } adcstate_t;
  65. /**
  66. * @brief Type of a structure representing an ADC driver.
  67. */
  68. typedef struct hal_adc_driver ADCDriver;
  69. /**
  70. * @brief Type of a structure representing an ADC driver configuration.
  71. */
  72. typedef struct hal_adc_config ADCConfig;
  73. /**
  74. * @brief Conversion group configuration structure.
  75. * @details This implementation-dependent structure describes a conversion
  76. * operation.
  77. * @note The use of this configuration structure requires knowledge of
  78. * STM32 ADC cell registers interface, please refer to the STM32
  79. * reference manual for details.
  80. */
  81. typedef struct hal_adc_configuration_group ADCConversionGroup;
  82. /* Including the low level driver header, it exports information required
  83. for completing types.*/
  84. #include "hal_adc_lld.h"
  85. /**
  86. * @brief Type of an ADC notification callback.
  87. *
  88. * @param[in] adcp pointer to the @p ADCDriver object triggering the
  89. */
  90. typedef void (*adccallback_t)(ADCDriver *adcp);
  91. /**
  92. * @brief Type of an ADC error callback.
  93. *
  94. * @param[in] adcp pointer to the @p ADCDriver object triggering the
  95. * callback
  96. * @param[in] err ADC error code
  97. */
  98. typedef void (*adcerrorcallback_t)(ADCDriver *adcp, adcerror_t err);
  99. /**
  100. * @brief Conversion group configuration structure.
  101. * @details This implementation-dependent structure describes a conversion
  102. * operation.
  103. * @note The use of this configuration structure requires knowledge of
  104. * STM32 ADC cell registers interface, please refer to the STM32
  105. * reference manual for details.
  106. */
  107. struct hal_adc_configuration_group {
  108. /**
  109. * @brief Enables the circular buffer mode for the group.
  110. */
  111. bool circular;
  112. /**
  113. * @brief Number of the analog channels belonging to the conversion group.
  114. */
  115. adc_channels_num_t num_channels;
  116. /**
  117. * @brief Callback function associated to the group or @p NULL.
  118. */
  119. adccallback_t end_cb;
  120. /**
  121. * @brief Error callback or @p NULL.
  122. */
  123. adcerrorcallback_t error_cb;
  124. /* End of the mandatory fields.*/
  125. adc_lld_configuration_group_fields;
  126. };
  127. /**
  128. * @brief Driver configuration structure.
  129. */
  130. struct hal_adc_config {
  131. /* End of the mandatory fields.*/
  132. adc_lld_config_fields;
  133. };
  134. /**
  135. * @brief Structure representing an ADC driver.
  136. */
  137. struct hal_adc_driver {
  138. /**
  139. * @brief Driver state.
  140. */
  141. adcstate_t state;
  142. /**
  143. * @brief Current configuration data.
  144. */
  145. const ADCConfig *config;
  146. /**
  147. * @brief Current samples buffer pointer or @p NULL.
  148. */
  149. adcsample_t *samples;
  150. /**
  151. * @brief Current samples buffer depth or @p 0.
  152. */
  153. size_t depth;
  154. /**
  155. * @brief Current conversion group pointer or @p NULL.
  156. */
  157. const ADCConversionGroup *grpp;
  158. #if (ADC_USE_WAIT == TRUE) || defined(__DOXYGEN__)
  159. /**
  160. * @brief Waiting thread.
  161. */
  162. thread_reference_t thread;
  163. #endif /* ADC_USE_WAIT == TRUE */
  164. #if (ADC_USE_MUTUAL_EXCLUSION == TRUE) || defined(__DOXYGEN__)
  165. /**
  166. * @brief Mutex protecting the peripheral.
  167. */
  168. mutex_t mutex;
  169. #endif /* ADC_USE_MUTUAL_EXCLUSION == TRUE */
  170. #if defined(ADC_DRIVER_EXT_FIELDS)
  171. ADC_DRIVER_EXT_FIELDS
  172. #endif
  173. /* End of the mandatory fields.*/
  174. adc_lld_driver_fields;
  175. };
  176. /*===========================================================================*/
  177. /* Driver macros. */
  178. /*===========================================================================*/
  179. /**
  180. * @name Macro Functions
  181. * @{
  182. */
  183. /**
  184. * @brief Buffer state.
  185. * @note This function is meant to be called from the SPI callback only.
  186. *
  187. * @param[in] adcp pointer to the @p ADCDriver object
  188. * @return The buffer state.
  189. * @retval false if the driver filled/sent the first half of the
  190. * buffer.
  191. * @retval true if the driver filled/sent the second half of the
  192. * buffer.
  193. *
  194. * @special
  195. */
  196. #define adcIsBufferComplete(adcp) ((bool)((adcp)->state == ADC_COMPLETE))
  197. /** @} */
  198. /**
  199. * @name Low level driver helper macros
  200. * @{
  201. */
  202. #if (ADC_USE_WAIT == TRUE) || defined(__DOXYGEN__)
  203. /**
  204. * @brief Resumes a thread waiting for a conversion completion.
  205. *
  206. * @param[in] adcp pointer to the @p ADCDriver object
  207. *
  208. * @notapi
  209. */
  210. #define _adc_reset_i(adcp) \
  211. osalThreadResumeI(&(adcp)->thread, MSG_RESET)
  212. /**
  213. * @brief Resumes a thread waiting for a conversion completion.
  214. *
  215. * @param[in] adcp pointer to the @p ADCDriver object
  216. *
  217. * @notapi
  218. */
  219. #define _adc_reset_s(adcp) \
  220. osalThreadResumeS(&(adcp)->thread, MSG_RESET)
  221. /**
  222. * @brief Wakes up the waiting thread.
  223. *
  224. * @param[in] adcp pointer to the @p ADCDriver object
  225. *
  226. * @notapi
  227. */
  228. #define _adc_wakeup_isr(adcp) { \
  229. osalSysLockFromISR(); \
  230. osalThreadResumeI(&(adcp)->thread, MSG_OK); \
  231. osalSysUnlockFromISR(); \
  232. }
  233. /**
  234. * @brief Wakes up the waiting thread with a timeout message.
  235. *
  236. * @param[in] adcp pointer to the @p ADCDriver object
  237. *
  238. * @notapi
  239. */
  240. #define _adc_timeout_isr(adcp) { \
  241. osalSysLockFromISR(); \
  242. osalThreadResumeI(&(adcp)->thread, MSG_TIMEOUT); \
  243. osalSysUnlockFromISR(); \
  244. }
  245. #else /* !ADC_USE_WAIT */
  246. #define _adc_reset_i(adcp)
  247. #define _adc_reset_s(adcp)
  248. #define _adc_wakeup_isr(adcp)
  249. #define _adc_timeout_isr(adcp)
  250. #endif /* !ADC_USE_WAIT */
  251. /**
  252. * @brief Common ISR code, half buffer event.
  253. * @details This code handles the portable part of the ISR code:
  254. * - Callback invocation.
  255. * .
  256. * @note This macro is meant to be used in the low level drivers
  257. * implementation only.
  258. *
  259. * @param[in] adcp pointer to the @p ADCDriver object
  260. *
  261. * @notapi
  262. */
  263. #define _adc_isr_half_code(adcp) { \
  264. if ((adcp)->grpp->end_cb != NULL) { \
  265. (adcp)->grpp->end_cb(adcp); \
  266. } \
  267. }
  268. /**
  269. * @brief Common ISR code, full buffer event.
  270. * @details This code handles the portable part of the ISR code:
  271. * - Callback invocation.
  272. * - Waiting thread wakeup, if any.
  273. * - Driver state transitions.
  274. * .
  275. * @note This macro is meant to be used in the low level drivers
  276. * implementation only.
  277. *
  278. * @param[in] adcp pointer to the @p ADCDriver object
  279. *
  280. * @notapi
  281. */
  282. #define _adc_isr_full_code(adcp) { \
  283. if ((adcp)->grpp->circular) { \
  284. /* Callback handling.*/ \
  285. if ((adcp)->grpp->end_cb != NULL) { \
  286. (adcp)->state = ADC_COMPLETE; \
  287. (adcp)->grpp->end_cb(adcp); \
  288. if ((adcp)->state == ADC_COMPLETE) { \
  289. (adcp)->state = ADC_ACTIVE; \
  290. } \
  291. } \
  292. } \
  293. else { \
  294. /* End conversion.*/ \
  295. adc_lld_stop_conversion(adcp); \
  296. if ((adcp)->grpp->end_cb != NULL) { \
  297. (adcp)->state = ADC_COMPLETE; \
  298. (adcp)->grpp->end_cb(adcp); \
  299. if ((adcp)->state == ADC_COMPLETE) { \
  300. (adcp)->state = ADC_READY; \
  301. (adcp)->grpp = NULL; \
  302. } \
  303. } \
  304. else { \
  305. (adcp)->state = ADC_READY; \
  306. (adcp)->grpp = NULL; \
  307. } \
  308. _adc_wakeup_isr(adcp); \
  309. } \
  310. }
  311. /**
  312. * @brief Common ISR code, error event.
  313. * @details This code handles the portable part of the ISR code:
  314. * - Callback invocation.
  315. * - Waiting thread timeout signaling, if any.
  316. * - Driver state transitions.
  317. * .
  318. * @note This macro is meant to be used in the low level drivers
  319. * implementation only.
  320. *
  321. * @param[in] adcp pointer to the @p ADCDriver object
  322. * @param[in] err platform dependent error code
  323. *
  324. * @notapi
  325. */
  326. #define _adc_isr_error_code(adcp, err) { \
  327. adc_lld_stop_conversion(adcp); \
  328. if ((adcp)->grpp->error_cb != NULL) { \
  329. (adcp)->state = ADC_ERROR; \
  330. (adcp)->grpp->error_cb(adcp, err); \
  331. if ((adcp)->state == ADC_ERROR) \
  332. (adcp)->state = ADC_READY; \
  333. (adcp)->grpp = NULL; \
  334. } \
  335. else { \
  336. (adcp)->state = ADC_READY; \
  337. (adcp)->grpp = NULL; \
  338. } \
  339. _adc_timeout_isr(adcp); \
  340. }
  341. /** @} */
  342. /*===========================================================================*/
  343. /* External declarations. */
  344. /*===========================================================================*/
  345. #ifdef __cplusplus
  346. extern "C" {
  347. #endif
  348. void adcInit(void);
  349. void adcObjectInit(ADCDriver *adcp);
  350. void adcStart(ADCDriver *adcp, const ADCConfig *config);
  351. void adcStop(ADCDriver *adcp);
  352. void adcStartConversion(ADCDriver *adcp,
  353. const ADCConversionGroup *grpp,
  354. adcsample_t *samples,
  355. size_t depth);
  356. void adcStartConversionI(ADCDriver *adcp,
  357. const ADCConversionGroup *grpp,
  358. adcsample_t *samples,
  359. size_t depth);
  360. void adcStopConversion(ADCDriver *adcp);
  361. void adcStopConversionI(ADCDriver *adcp);
  362. #if ADC_USE_WAIT == TRUE
  363. msg_t adcConvert(ADCDriver *adcp,
  364. const ADCConversionGroup *grpp,
  365. adcsample_t *samples,
  366. size_t depth);
  367. #endif
  368. #if ADC_USE_MUTUAL_EXCLUSION == TRUE
  369. void adcAcquireBus(ADCDriver *adcp);
  370. void adcReleaseBus(ADCDriver *adcp);
  371. #endif
  372. #ifdef __cplusplus
  373. }
  374. #endif
  375. #endif /* HAL_USE_ADC == TRUE */
  376. #endif /* HAL_ADC_H */
  377. /** @} */