ald_adc.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101
  1. /**
  2. ******************************************************************************
  3. * @file ald_adc.c
  4. * @brief This file provides firmware functions to manage the following
  5. * functionalities of the Analog to Digital Convertor (ADC)
  6. * peripheral:
  7. * + Initialization functions
  8. * ++ Initialization and Configuration of ADC
  9. * + Operation functions
  10. * ++ Start, stop, get result of conversions of normal
  11. * group, using 3 possible modes: polling, interruption or DMA.
  12. * + Control functions
  13. * ++ Channels configuration on normal group
  14. * ++ Channels configuration on insert group
  15. * ++ Analog Watchdog configuration
  16. * + State functions
  17. * ++ ADC state machine management
  18. * ++ Interrupts and flags management
  19. *
  20. * @version V1.0
  21. * @date 06 Mar. 2023
  22. * @author AE Team.
  23. * @note
  24. * Change Logs:
  25. * Date Author Notes
  26. * 06 Mar. 2023 Lisq The first version
  27. *
  28. * Copyright (C) Shanghai Eastsoft Microelectronics Co. Ltd. All rights reserved.
  29. *
  30. * SPDX-License-Identifier: Apache-2.0
  31. *
  32. * Licensed under the Apache License, Version 2.0 (the License); you may
  33. * not use this file except in compliance with the License.
  34. * You may obtain a copy of the License at
  35. *
  36. * www.apache.org/licenses/LICENSE-2.0
  37. *
  38. * Unless required by applicable law or agreed to in writing, software
  39. * distributed under the License is distributed on an AS IS BASIS, WITHOUT
  40. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  41. * See the License for the specific language governing permissions and
  42. * limitations under the License.
  43. **********************************************************************************
  44. */
  45. #include "ald_adc.h"
  46. /** @addtogroup ES32VF2264_ALD
  47. * @{
  48. */
  49. /** @defgroup ADC ADC
  50. * @brief ADC module driver
  51. * @{
  52. */
  53. /** @addtogroup ADC_Private_Functions
  54. * @{
  55. */
  56. static void adc_dma_normal_conv_cplt(void *arg);
  57. /**
  58. * @}
  59. */
  60. /** @defgroup ADC_Public_Functions ADC Public Functions
  61. * @{
  62. */
  63. /** @defgroup ADC_Public_Functions_Group1 Initialization functions
  64. * @brief Initialization and Configuration functions
  65. * @{
  66. */
  67. /**
  68. * @brief Initializes the ADC peripheral and normal group according to
  69. * parameters specified in structure "adc_handle_t".
  70. * @param hperh: Pointer to a adc_handle_t structure that contains
  71. * the configuration information for the specified ADC module.
  72. * @retval Status, see @ref ald_status_t.
  73. */
  74. ald_status_t ald_adc_init(ald_adc_handle_t *hperh)
  75. {
  76. assert_param(IS_ADC_TYPE(hperh->perh));
  77. assert_param(IS_ADC_DATA_ALIGN_TYPE(hperh->init.align));
  78. assert_param(IS_FUNC_STATE(hperh->init.scan));
  79. assert_param(IS_ADC_CLK_DIV_TYPE(hperh->init.div));
  80. assert_param(IS_ADC_NEG_REF_VOLTAGE_TYPE(hperh->init.n_ref));
  81. assert_param(IS_POS_REF_VOLTAGE_TYPE(hperh->init.p_ref));
  82. assert_param(IS_ADC_CONV_BIT_TYPE(hperh->init.data_bit));
  83. assert_param(IS_ADC_NCH_NR_TYPE(hperh->init.nch_nr));
  84. assert_param(IS_ADC_DISC_NR_TYPE(hperh->init.disc_nr));
  85. assert_param(IS_FUNC_STATE(hperh->init.cont));
  86. assert_param(IS_ADC_NCHESEL_MODE_TYPE(hperh->init.nche_sel));
  87. if (hperh->state == ALD_ADC_STATE_RESET ) {
  88. hperh->error_code = ALD_ADC_ERROR_NONE;
  89. hperh->lock = UNLOCK;
  90. }
  91. ALD_ADC_DISABLE(hperh);
  92. ald_adc_reset(hperh);
  93. hperh->state = ALD_ADC_STATE_BUSY;
  94. MODIFY_REG(hperh->perh->CON1, ADC_CON1_ALIGN_MSK, hperh->init.align << ADC_CON1_ALIGN_POS);
  95. MODIFY_REG(hperh->perh->CON0, ADC_CON0_RSEL_MSK, hperh->init.data_bit << ADC_CON0_RSEL_POSS);
  96. /* Enable discontinuous mode only if continuous mode is disable */
  97. if (hperh->init.disc == ALD_ADC_NCH_DISC_EN) {
  98. hperh->init.scan = ENABLE;
  99. hperh->init.cont = DISABLE;
  100. SET_BIT(hperh->perh->CON0, ADC_CON0_NCHDCEN_MSK);
  101. MODIFY_REG(hperh->perh->CON0, ADC_CON0_ETRGN_MSK, hperh->init.disc_nr << ADC_CON0_ETRGN_POSS);
  102. }
  103. else if (hperh->init.disc == ALD_ADC_ICH_DISC_EN) {
  104. hperh->init.scan = ENABLE;
  105. hperh->init.cont = DISABLE;
  106. SET_BIT(hperh->perh->CON0, ADC_CON0_ICHDCEN_MSK);
  107. MODIFY_REG(hperh->perh->CON0, ADC_CON0_ETRGN_MSK, hperh->init.disc_nr << ADC_CON0_ETRGN_POSS);
  108. }
  109. else {
  110. CLEAR_BIT(hperh->perh->CON0, ADC_CON0_NCHDCEN_MSK);
  111. CLEAR_BIT(hperh->perh->CON0, ADC_CON0_ICHDCEN_MSK);
  112. }
  113. if ((hperh->init.scan == ENABLE) || (hperh->init.disc == ALD_ADC_NCH_DISC_EN))
  114. MODIFY_REG(hperh->perh->CHSL, ADC_CHSL_NSL_MSK, hperh->init.nch_nr << ADC_CHSL_NSL_POSS);
  115. MODIFY_REG(hperh->perh->CON1, ADC_CON1_CM_MSK, hperh->init.cont << ADC_CON1_CM_POS);
  116. MODIFY_REG(hperh->perh->CON0, ADC_CON0_SCANEN_MSK, hperh->init.scan << ADC_CON0_SCANEN_POS);
  117. WRITE_REG(hperh->perh->CCR, 0x0);
  118. MODIFY_REG(hperh->perh->CCR, ADC_CCR_PWRMODSEL_MSK, DISABLE << ADC_CCR_PWRMODSEL_POS);
  119. MODIFY_REG(hperh->perh->CCR, ADC_CCR_VREFEN_MSK, ENABLE << ADC_CCR_VREFEN_POS);
  120. MODIFY_REG(hperh->perh->CCR, ADC_CCR_IREFEN_MSK, ENABLE << ADC_CCR_IREFEN_POS);
  121. MODIFY_REG(hperh->perh->CCR, ADC_CCR_VRNSEL_MSK, hperh->init.n_ref << ADC_CCR_VRNSEL_POS);
  122. MODIFY_REG(hperh->perh->CCR, ADC_CCR_VRPSEL_MSK, hperh->init.p_ref << ADC_CCR_VRPSEL_POS);
  123. MODIFY_REG(hperh->perh->CCR, ADC_CCR_POSDIV_MSK, hperh->init.div << ADC_CCR_POSDIV_POSS);
  124. MODIFY_REG(hperh->perh->CON1, ADC_CON1_NCHESEL_MSK, hperh->init.nche_sel << ADC_CON1_NCHESEL_POS);
  125. ALD_ADC_ENABLE(hperh);
  126. hperh->error_code = ALD_ADC_ERROR_NONE;
  127. hperh->state = ALD_ADC_STATE_READY;
  128. return ALD_OK;
  129. }
  130. /**
  131. * @brief Deinitialize the ADC peripheral registers to their default reset
  132. * values.
  133. * @param hperh: Pointer to a adc_handle_t structure that contains
  134. * the configuration information for the specified ADC module.
  135. * @retval Status, see @ref ald_status_t.
  136. */
  137. ald_status_t ald_adc_reset(ald_adc_handle_t *hperh)
  138. {
  139. assert_param(IS_ADC_TYPE(hperh->perh));
  140. ALD_ADC_DISABLE(hperh);
  141. WRITE_REG(hperh->perh->CLR, 0x30F);
  142. WRITE_REG(hperh->perh->CON0, 0x0);
  143. WRITE_REG(hperh->perh->CON1, 0x0);
  144. WRITE_REG(hperh->perh->CCR, 0x0);
  145. WRITE_REG(hperh->perh->WDTH, 0xFFF);
  146. WRITE_REG(hperh->perh->WDTL, 0x0);
  147. WRITE_REG(hperh->perh->NCHOFF, 0x0);
  148. WRITE_REG(hperh->perh->ICHOFF1, 0x0);
  149. WRITE_REG(hperh->perh->ICHOFF2, 0x0);
  150. WRITE_REG(hperh->perh->ICHOFF3, 0x0);
  151. WRITE_REG(hperh->perh->ICHOFF4, 0x0);
  152. WRITE_REG(hperh->perh->ICHS, 0x0);
  153. WRITE_REG(hperh->perh->NCHS1, 0x0);
  154. WRITE_REG(hperh->perh->NCHS2, 0x0);
  155. WRITE_REG(hperh->perh->NCHS3, 0x0);
  156. WRITE_REG(hperh->perh->NCHS4, 0x0);
  157. WRITE_REG(hperh->perh->SMPT1, 0x0);
  158. WRITE_REG(hperh->perh->SMPT2, 0x0);
  159. WRITE_REG(hperh->perh->SMPT3, 0x0);
  160. WRITE_REG(hperh->perh->CHSL, 0x0);
  161. hperh->state = ALD_ADC_STATE_RESET;
  162. hperh->error_code = ALD_ADC_ERROR_NONE;
  163. return ALD_OK;
  164. }
  165. /**
  166. * @}
  167. */
  168. /** @defgroup ADC_Public_Functions_Group2 IO operation functions
  169. * @brief Input and Output operation functions
  170. * @{
  171. */
  172. /**
  173. * @brief Enables ADC, starts conversion of normal group.
  174. * @param hperh: Pointer to a adc_handle_t structure that contains
  175. * the configuration information for the specified ADC module.
  176. * @retval Status, see @ref ald_status_t.
  177. */
  178. ald_status_t ald_adc_normal_start(ald_adc_handle_t *hperh)
  179. {
  180. assert_param(IS_ADC_TYPE(hperh->perh));
  181. ALD_ADC_ENABLE(hperh);
  182. WRITE_REG(hperh->perh->CLR, ALD_ADC_FLAG_NCH | ALD_ADC_FLAG_NCHS);
  183. SET_BIT(hperh->perh->CON1, ADC_CON1_NCHTRG_MSK);
  184. return ALD_OK;
  185. }
  186. /**
  187. * @brief Stop ADC conversion of normal group (and insert channels in
  188. * case of auto_injection mode), disable ADC peripheral.
  189. * @note: ADC peripheral disable is forcing stop of potential
  190. * conversion on insert group. If insert group is under use, it
  191. * should be preliminarily stopped using ald_adc_insert_stop function.
  192. * @param hperh: Pointer to a adc_handle_t structure that contains
  193. * the configuration information for the specified ADC module.
  194. * @retval Status, see @ref ald_status_t.
  195. */
  196. ald_status_t ald_adc_normal_stop(ald_adc_handle_t *hperh)
  197. {
  198. assert_param(IS_ADC_TYPE(hperh->perh));
  199. ALD_ADC_DISABLE(hperh);
  200. hperh->state = ALD_ADC_STATE_READY;
  201. return ALD_OK;
  202. }
  203. /**
  204. * @brief Wait for normal group conversion to be completed.
  205. * @note This function cannot be used in a particular setup: ADC configured in DMA mode.
  206. * In this case, DMA resets the flag EOC and polling cannot be performed on each conversion.
  207. * @note When use this function,you should be pay attention to the hperh->init.reocs_mode,
  208. * if it is ADC_REOCS_MODE_ALL, it means the function will wait all normal rank conversion finished.
  209. * if it is ADC_REOCS_MODE_ONE, it means the funcion will wait every normal rank conversion finished.
  210. * @param hperh: Pointer to a adc_handle_t structure that contains
  211. * the configuration information for the specified ADC module.
  212. * @param timeout: Timeout value in millisecond.
  213. * @retval Status, see @ref ald_status_t.
  214. */
  215. ald_status_t ald_adc_normal_poll_for_conversion(ald_adc_handle_t *hperh, uint32_t timeout)
  216. {
  217. uint32_t _tick;
  218. assert_param(IS_ADC_TYPE(hperh->perh));
  219. _tick = ald_get_tick();
  220. while (!(READ_BIT(hperh->perh->STAT, ADC_STAT_NCHE_MSK))) {
  221. if (timeout != ALD_MAX_DELAY ) {
  222. if ((timeout == 0) || ((ald_get_tick() - _tick) > timeout)) {
  223. hperh->state = ALD_ADC_STATE_TIMEOUT;
  224. return ALD_TIMEOUT;
  225. }
  226. }
  227. }
  228. WRITE_REG(hperh->perh->CLR, ALD_ADC_FLAG_NCHS | ALD_ADC_FLAG_NCH);
  229. return ALD_OK;
  230. }
  231. /**
  232. * @brief Poll for conversion event.
  233. * @param hperh: Pointer to a adc_handle_t structure that contains
  234. * the configuration information for the specified ADC module.
  235. * @param event_type: the ADC event type.
  236. * This parameter can be one of the following values:
  237. * ADC_awd_event: ADC Analog watchdog event.
  238. * @param timeout: Timeout value in millisecond.
  239. * @retval Status, see @ref ald_status_t.
  240. */
  241. ald_status_t ald_adc_poll_for_event(ald_adc_handle_t *hperh, ald_adc_event_type_t event_type, uint32_t timeout)
  242. {
  243. uint32_t _tick;
  244. assert_param(IS_ADC_TYPE(hperh->perh));
  245. assert_param(IS_ADC_EVENT_TYPE(event_type));
  246. _tick = ald_get_tick();
  247. while (ald_adc_get_flag_status(hperh, (ald_adc_flag_t)event_type) == RESET) {
  248. if (timeout != ALD_MAX_DELAY ) {
  249. if ((timeout == 0) || ((ald_get_tick() - _tick) > timeout)) {
  250. hperh->state = ALD_ADC_STATE_TIMEOUT;
  251. return ALD_TIMEOUT;
  252. }
  253. }
  254. }
  255. CLEAR_BIT(hperh->state, ALD_ADC_STATE_BUSY_WDG);
  256. return ALD_OK;
  257. }
  258. /**
  259. * @brief Enables ADC, starts conversion of normal group with interruption.
  260. * Interruptions enabled in this function:
  261. * - REOC (end of conversion of normal group)
  262. * Each of these interruptions has its dedicated callback function.
  263. * @param hperh: Pointer to a adc_handle_t structure that contains
  264. * the configuration information for the specified ADC module.
  265. * @retval Status, see @ref ald_status_t.
  266. */
  267. ald_status_t ald_adc_normal_start_by_it(ald_adc_handle_t *hperh)
  268. {
  269. assert_param(IS_ADC_TYPE(hperh->perh));
  270. SET_BIT(hperh->state, ALD_ADC_STATE_BUSY_N);
  271. ALD_ADC_ENABLE(hperh);
  272. WRITE_REG(hperh->perh->CLR, ALD_ADC_FLAG_NCH);
  273. ald_adc_interrupt_config(hperh, ALD_ADC_IT_NCH, ENABLE);
  274. SET_BIT(hperh->perh->CON1, ADC_CON1_NCHTRG_MSK);
  275. return ALD_OK;
  276. }
  277. /**
  278. * @brief Stop ADC conversion of normal group (and insert group in
  279. * case of auto_injection mode), disable interrution of
  280. * end-of-conversion, disable ADC peripheral.
  281. * @param hperh: Pointer to a adc_handle_t structure that contains
  282. * the configuration information for the specified ADC module.
  283. * @retval Status, see @ref ald_status_t.
  284. */
  285. ald_status_t ald_adc_normal_stop_by_it(ald_adc_handle_t *hperh)
  286. {
  287. assert_param(IS_ADC_TYPE(hperh->perh));
  288. ALD_ADC_DISABLE(hperh);
  289. ald_adc_interrupt_config(hperh, ALD_ADC_IT_NCH, DISABLE);
  290. CLEAR_BIT(hperh->state, ALD_ADC_STATE_BUSY_N);
  291. return ALD_OK;
  292. }
  293. /**
  294. * @brief Enables ADC, starts conversion of normal group and transfers result
  295. * through DMA.
  296. * @param hperh: Pointer to a adc_handle_t structure that contains
  297. * the configuration information for the specified ADC module.
  298. * @param buf: The destination Buffer address.
  299. * @param size: The length of data to be transferred from ADC peripheral to memory.
  300. * @param channel: The DMA channel
  301. * @retval Status, see @ref ald_status_t.
  302. */
  303. ald_status_t ald_adc_start_by_dma(ald_adc_handle_t *hperh, uint16_t *buf, uint16_t size, uint8_t channel)
  304. {
  305. if ((buf == NULL) || (size == 0))
  306. return ALD_ERROR;
  307. assert_param(IS_ADC_TYPE(hperh->perh));
  308. SET_BIT(hperh->state, ALD_ADC_STATE_BUSY_N);
  309. if (hperh->hdma.perh == NULL)
  310. hperh->hdma.perh = DMA;
  311. SET_BIT(hperh->perh->CON1, ADC_CON1_DMA_MSK);
  312. ald_dma_interrupt_config(channel, ALD_DMA_IT_FLAG_TC, ENABLE);
  313. hperh->hdma.cplt_tc_cbk = adc_dma_normal_conv_cplt;
  314. hperh->hdma.cplt_tc_arg = hperh;
  315. ald_dma_config_struct(&hperh->hdma.config);
  316. hperh->hdma.config.src = (void *)&hperh->perh->NCHDR;
  317. hperh->hdma.config.dst = (void *)buf;
  318. hperh->hdma.config.size = size;
  319. hperh->hdma.config.src_data_width = ALD_DMA_DATA_SIZE_HALFWORD;
  320. hperh->hdma.config.dst_data_width = ALD_DMA_DATA_SIZE_HALFWORD;
  321. hperh->hdma.config.src_inc = ALD_DMA_DATA_INC_DISABLE;
  322. hperh->hdma.config.dst_inc = ALD_DMA_DATA_INC_ENABLE;
  323. hperh->hdma.config.msel = ALD_DMA_MSEL_ADC;
  324. hperh->hdma.config.msigsel = ALD_DMA_MSIGSEL_ADC;
  325. hperh->hdma.config.circle_mode = ENABLE;
  326. hperh->hdma.config.channel = channel;
  327. ald_dma_config_basic(&hperh->hdma);
  328. ALD_ADC_ENABLE(hperh);
  329. SET_BIT(hperh->perh->CON1, ADC_CON1_NCHTRG_MSK);
  330. return ALD_OK;
  331. }
  332. /**
  333. * @brief Stop ADC conversion of normal group (and insert group in
  334. * case of auto_insert mode), disable ADC DMA transfer, disable
  335. * ADC peripheral.
  336. * @param hperh: Pointer to a adc_handle_t structure that contains
  337. * the configuration information for the specified ADC module.
  338. * @param channel: The DMA channel
  339. * @retval Status, see @ref ald_status_t.
  340. */
  341. ald_status_t ald_adc_stop_by_dma(ald_adc_handle_t *hperh, uint8_t channel)
  342. {
  343. assert_param(IS_ADC_TYPE(hperh->perh));
  344. ALD_ADC_DISABLE(hperh);
  345. CLEAR_BIT(hperh->perh->CON1, ADC_CON1_DMA_MSK);
  346. ald_dma_channel_config(channel, DISABLE);
  347. CLEAR_BIT(hperh->state, ALD_ADC_STATE_BUSY_N);
  348. return ALD_OK;
  349. }
  350. /**
  351. * @brief DMA transfer complete callback.
  352. * @param arg: argument of the call back.
  353. * @retval None
  354. */
  355. static void adc_dma_timer_trigger_cplt(void *arg)
  356. {
  357. ald_adc_timer_config_t *hperh = (ald_adc_timer_config_t *)arg;
  358. ald_timer_base_stop(&hperh->h_timer);
  359. CLEAR_BIT(hperh->h_adc.perh->CON1, ADC_CON1_DMA_MSK);
  360. ALD_ADC_DISABLE(&hperh->h_adc);
  361. ald_dma_channel_config(hperh->dma_ch, DISABLE);
  362. CLEAR_BIT(hperh->h_adc.state, ALD_ADC_STATE_BUSY_N);
  363. if (hperh->h_adc.normal_cplt_cbk)
  364. hperh->h_adc.normal_cplt_cbk(&hperh->h_adc);
  365. return;
  366. }
  367. /**
  368. * @brief Config timer trigger adc insert channel conversion.
  369. * @param config: Pointer to a adc_timer_config_t structure that
  370. * contains the configuration information for the specified function.
  371. * @retval Status, see @ref ald_status_t.
  372. */
  373. ald_status_t ald_adc_timer_trigger_insert(ald_adc_timer_config_t *config)
  374. {
  375. config->h_pis.perh = PIS;
  376. config->h_pis.init.producer_clk = ALD_PIS_CLK_PCLK;
  377. config->h_pis.init.producer_edge = ALD_PIS_EDGE_NONE;
  378. config->h_pis.init.consumer_clk = ALD_PIS_CLK_PCLK;
  379. if (config->p_timer == AD16C4T)
  380. config->h_pis.init.producer_src = ALD_PIS_TIMER0_UPDATA;
  381. else if (config->p_timer == GP16C4T0)
  382. config->h_pis.init.producer_src = ALD_PIS_TIMER5_UPDATA;
  383. else if (config->p_timer == GP16C4T1)
  384. config->h_pis.init.producer_src = ALD_PIS_TIMER6_UPDATA;
  385. else if (config->p_timer == GP16C4T2)
  386. config->h_pis.init.producer_src = ALD_PIS_TIMER7_UPDATA;
  387. else
  388. return ALD_ERROR;
  389. config->h_pis.init.consumer_trig = ALD_PIS_CH5_ADC0_INSERT;
  390. ald_pis_create(&config->h_pis);
  391. /* Initialize TIMER */
  392. config->h_timer.perh = config->p_timer;
  393. config->h_timer.init.prescaler = 0;
  394. config->h_timer.init.mode = ALD_TIMER_CNT_MODE_UP;
  395. config->h_timer.init.period = ((ald_cmu_get_pclk_clock() / 1000000) * config->time);
  396. config->h_timer.init.clk_div = ALD_TIMER_CLOCK_DIV1;
  397. config->h_timer.init.re_cnt = 0;
  398. ald_timer_base_init(&config->h_timer);
  399. config->h_adc.perh = config->p_adc;
  400. config->h_adc.init.align = ALD_ADC_DATAALIGN_RIGHT;
  401. config->h_adc.init.scan = DISABLE;
  402. config->h_adc.init.cont = DISABLE;
  403. config->h_adc.init.ich_nr = ALD_ADC_ICH_NR_1;
  404. config->h_adc.init.disc = ALD_ADC_ALL_DISABLE;
  405. config->h_adc.init.disc_nr = ALD_ADC_DISC_NR_1;
  406. config->h_adc.init.data_bit = ALD_ADC_CONV_BIT_12;
  407. config->h_adc.init.div = ALD_ADC_CKDIV_128;
  408. config->h_adc.init.nche_sel = ALD_ADC_NCHESEL_MODE_ONE;
  409. config->h_adc.init.n_ref = config->n_ref;
  410. config->h_adc.init.p_ref = config->p_ref;
  411. config->h_adc.normal_cplt_cbk = config->cplt_cbk;
  412. config->h_adc.insert_cplt_cbk = NULL;
  413. config->h_adc.wdg_cbk = NULL;
  414. config->h_adc.error_cbk = NULL;
  415. config->h_adc.ovr_cbk = NULL;
  416. ald_adc_init(&config->h_adc);
  417. config->h_adc.perh->CON1 |= 0x00100000; /* rising edge trigger insert channel convert */
  418. config->i_config.ch = config->adc_ch;
  419. config->i_config.idx = ALD_ADC_ICH_IDX_1;
  420. config->i_config.samp = ALD_ADC_SAMPLETIME_4;
  421. config->i_config.nr = ALD_ADC_ICH_NR_1;
  422. config->i_config.auto_m = DISABLE;
  423. ald_adc_insert_channel_config(&config->h_adc, &config->i_config);
  424. ALD_ADC_ENABLE(&config->h_adc);
  425. ald_timer_base_start(&config->h_timer);
  426. return ALD_OK;
  427. }
  428. /**
  429. * @brief Config Timer trigger adc function
  430. * @param config: Pointer to a adc_timer_config_t structure that
  431. * contains the configuration information for the specified function.
  432. * @retval Status, see @ref ald_status_t.
  433. */
  434. ald_status_t ald_adc_timer_trigger_adc_by_dma(ald_adc_timer_config_t *config)
  435. {
  436. config->h_pis.perh = PIS;
  437. config->h_pis.init.producer_clk = ALD_PIS_CLK_PCLK;
  438. config->h_pis.init.producer_edge = ALD_PIS_EDGE_NONE;
  439. config->h_pis.init.consumer_clk = ALD_PIS_CLK_PCLK;
  440. if (config->p_timer == AD16C4T)
  441. config->h_pis.init.producer_src = ALD_PIS_TIMER0_UPDATA;
  442. else if (config->p_timer == GP16C4T0)
  443. config->h_pis.init.producer_src = ALD_PIS_TIMER5_UPDATA;
  444. else if (config->p_timer == GP16C4T1)
  445. config->h_pis.init.producer_src = ALD_PIS_TIMER6_UPDATA;
  446. else if (config->p_timer == GP16C4T2)
  447. config->h_pis.init.producer_src = ALD_PIS_TIMER7_UPDATA;
  448. else
  449. return ALD_ERROR;
  450. config->h_pis.init.consumer_trig = ALD_PIS_CH4_ADC0_NORMAL;
  451. ald_pis_create(&config->h_pis);
  452. /* Initialize TIMER */
  453. config->h_timer.perh = config->p_timer;
  454. config->h_timer.init.prescaler = 0;
  455. config->h_timer.init.mode = ALD_TIMER_CNT_MODE_UP;
  456. config->h_timer.init.period = ((ald_cmu_get_pclk_clock() / 1000000) * config->time);
  457. config->h_timer.init.clk_div = ALD_TIMER_CLOCK_DIV1;
  458. config->h_timer.init.re_cnt = 0;
  459. ald_timer_base_init(&config->h_timer);
  460. config->h_adc.perh = config->p_adc;
  461. config->h_adc.init.align = ALD_ADC_DATAALIGN_RIGHT;
  462. config->h_adc.init.scan = DISABLE;
  463. config->h_adc.init.cont = DISABLE;
  464. config->h_adc.init.nch_nr = ALD_ADC_NCH_NR_1;
  465. config->h_adc.init.disc = ALD_ADC_ALL_DISABLE;
  466. config->h_adc.init.disc_nr = ALD_ADC_DISC_NR_1;
  467. config->h_adc.init.data_bit = ALD_ADC_CONV_BIT_12;
  468. config->h_adc.init.div = ALD_ADC_CKDIV_128;
  469. config->h_adc.init.nche_sel = ALD_ADC_NCHESEL_MODE_ONE;
  470. config->h_adc.init.n_ref = config->n_ref;
  471. config->h_adc.init.p_ref = config->p_ref;
  472. config->h_adc.normal_cplt_cbk = config->cplt_cbk;
  473. config->h_adc.insert_cplt_cbk = NULL;
  474. config->h_adc.wdg_cbk = NULL;
  475. config->h_adc.error_cbk = NULL;
  476. config->h_adc.ovr_cbk = NULL;
  477. ald_adc_init(&config->h_adc);
  478. config->h_adc.perh->CON1 |= 0x10000000;
  479. config->config.ch = config->adc_ch;
  480. config->config.idx = ALD_ADC_NCH_IDX_1;
  481. config->config.samp = ALD_ADC_SAMPLETIME_4;
  482. ald_adc_normal_channel_config(&config->h_adc, &config->config);
  483. config->h_dma.cplt_tc_cbk = adc_dma_timer_trigger_cplt;
  484. config->h_dma.cplt_tc_arg = config;
  485. ald_dma_config_struct(&config->h_dma.config);
  486. config->h_dma.perh = DMA;
  487. config->h_dma.config.src = (void *)&config->h_adc.perh->NCHDR;
  488. config->h_dma.config.dst = (void *)config->buf;
  489. config->h_dma.config.size = config->size;
  490. config->h_dma.config.src_data_width = ALD_DMA_DATA_SIZE_HALFWORD;
  491. config->h_dma.config.dst_data_width = ALD_DMA_DATA_SIZE_HALFWORD;
  492. config->h_dma.config.src_inc = ALD_DMA_DATA_INC_DISABLE;
  493. config->h_dma.config.dst_inc = ALD_DMA_DATA_INC_ENABLE;
  494. config->h_dma.config.msel = ALD_DMA_MSEL_ADC;
  495. config->h_dma.config.msigsel = ALD_DMA_MSIGSEL_ADC;
  496. config->h_dma.config.channel = config->dma_ch;
  497. ald_dma_config_basic(&config->h_dma);
  498. ald_dma_interrupt_config(config->dma_ch, ALD_DMA_IT_FLAG_TC, ENABLE);
  499. SET_BIT(config->h_adc.perh->CON1, ADC_CON1_DMA_MSK);
  500. ALD_ADC_ENABLE(&config->h_adc);
  501. ald_timer_base_start(&config->h_timer);
  502. return ALD_OK;
  503. }
  504. /**
  505. * @brief Get ADC normal group conversion result.
  506. * @param hperh: Pointer to a adc_handle_t structure that contains
  507. * the configuration information for the specified ADC module.
  508. * @retval ADC group normal conversion data
  509. */
  510. uint32_t ald_adc_normal_get_value(ald_adc_handle_t *hperh)
  511. {
  512. assert_param(IS_ADC_TYPE(hperh->perh));
  513. return hperh->perh->NCHDR;
  514. }
  515. /**
  516. * @brief Enables ADC, starts conversion of insert group.
  517. * Interruptions enabled in this function: None.
  518. * @param hperh: Pointer to a adc_handle_t structure that contains
  519. * the configuration information for the specified ADC module.
  520. * @retval Status, see @ref ald_status_t.
  521. */
  522. ald_status_t ald_adc_insert_start(ald_adc_handle_t *hperh)
  523. {
  524. assert_param(IS_ADC_TYPE(hperh->perh));
  525. ALD_ADC_ENABLE(hperh);
  526. WRITE_REG(hperh->perh->CLR, ALD_ADC_FLAG_ICH);
  527. if (!(READ_BIT(hperh->perh->CON0, ADC_CON0_IAUTO_MSK)))
  528. SET_BIT(hperh->perh->CON1, ADC_CON1_ICHTRG_MSK);
  529. return ALD_OK;
  530. }
  531. /**
  532. * @brief Stop conversion of insert channels. Disable ADC peripheral if
  533. * no normal conversion is on going.
  534. * @note If ADC must be disabled and if conversion is on going on
  535. * normal group, function ald_adc_normal_stop must be used to stop both
  536. * insert and normal groups, and disable the ADC.
  537. * @note If insert group mode auto-injection is enabled,
  538. * function ald_adc_normal_stop must be used.
  539. * @param hperh: Pointer to a adc_handle_t structure that contains
  540. * the configuration information for the specified ADC module.
  541. * @retval Status, see @ref ald_status_t.
  542. */
  543. ald_status_t ald_adc_insert_stop(ald_adc_handle_t *hperh)
  544. {
  545. assert_param(IS_ADC_TYPE(hperh->perh));
  546. ALD_ADC_DISABLE(hperh);
  547. hperh->state = ALD_ADC_STATE_READY;
  548. return ALD_OK;
  549. }
  550. /**
  551. * @brief Wait for insert group conversion to be completed.
  552. * @param hperh: Pointer to a adc_handle_t structure that contains
  553. * the configuration information for the specified ADC module.
  554. * @param timeout: Timeout value in millisecond.
  555. * @retval Status, see @ref ald_status_t.
  556. */
  557. ald_status_t ald_adc_insert_poll_for_conversion(ald_adc_handle_t *hperh, uint32_t timeout)
  558. {
  559. uint32_t _tick;
  560. assert_param(IS_ADC_TYPE(hperh->perh));
  561. _tick = ald_get_tick();
  562. while (!(READ_BIT(hperh->perh->STAT, ADC_STAT_ICHE_MSK))) {
  563. if (timeout != ALD_MAX_DELAY) {
  564. if ((timeout == 0) || ((ald_get_tick() - _tick) > timeout)) {
  565. hperh->state |= ALD_ADC_STATE_TIMEOUT;
  566. return ALD_TIMEOUT;
  567. }
  568. }
  569. }
  570. WRITE_REG(hperh->perh->CLR, ALD_ADC_FLAG_ICHS | ALD_ADC_FLAG_ICH);
  571. return ALD_OK;
  572. }
  573. /**
  574. * @brief Enables ADC, starts conversion of insert group with interruption.
  575. * - JEOC (end of conversion of insert group)
  576. * Each of these interruptions has its dedicated callback function.
  577. * @param hperh: Pointer to a adc_handle_t structure that contains
  578. * the configuration information for the specified ADC module.
  579. * @retval Status, see @ref ald_status_t..
  580. */
  581. ald_status_t ald_adc_insert_start_by_it(ald_adc_handle_t *hperh)
  582. {
  583. assert_param(IS_ADC_TYPE(hperh->perh));
  584. SET_BIT(hperh->state, ALD_ADC_STATE_BUSY_I);
  585. ALD_ADC_ENABLE(hperh);
  586. WRITE_REG(hperh->perh->CLR, ALD_ADC_FLAG_ICHS | ALD_ADC_FLAG_ICH);
  587. ald_adc_interrupt_config(hperh, ALD_ADC_IT_ICH, ENABLE);
  588. if (!(READ_BIT(hperh->perh->CON0, ADC_CON0_IAUTO_MSK)))
  589. SET_BIT(hperh->perh->CON1, ADC_CON1_ICHTRG_MSK);
  590. return ALD_OK;
  591. }
  592. /**
  593. * @brief Stop conversion of insert channels, disable interruption of
  594. * end-of-conversion. Disable ADC peripheral if no normal conversion
  595. * is on going.
  596. * @note If ADC must be disabled and if conversion is on going on
  597. * normal group, function ald_adc_normal_stop must be used to stop both
  598. * insert and normal groups, and disable the ADC.
  599. * @note If insert group mode auto-injection is enabled,
  600. * function ald_adc_normal_stop must be used.
  601. * @param hperh: Pointer to a adc_handle_t structure that contains
  602. * the configuration information for the specified ADC module.
  603. * @retval None
  604. */
  605. ald_status_t ald_adc_insert_stop_by_it(ald_adc_handle_t *hperh)
  606. {
  607. assert_param(IS_ADC_TYPE(hperh->perh));
  608. CLEAR_BIT(hperh->state, ALD_ADC_STATE_BUSY_I);
  609. ALD_ADC_DISABLE(hperh);
  610. ald_adc_interrupt_config(hperh, ALD_ADC_IT_ICH, DISABLE);
  611. return ALD_OK;
  612. }
  613. /**
  614. * @brief Get ADC insert group conversion result.
  615. * @param hperh: Pointer to a adc_handle_t structure that contains
  616. * the configuration information for the specified ADC module.
  617. * @param idx: Index of converted ADC insert channel.
  618. * @retval ADC group insert conversion data
  619. */
  620. uint32_t ald_adc_insert_get_value(ald_adc_handle_t *hperh, ald_adc_ich_idx_t idx)
  621. {
  622. uint32_t tmp;
  623. assert_param(IS_ADC_TYPE(hperh->perh));
  624. assert_param(IS_ADC_ICH_IDX_TYPE(idx));
  625. switch (idx) {
  626. case ALD_ADC_ICH_IDX_1:
  627. tmp = hperh->perh->ICHDR1;
  628. break;
  629. case ALD_ADC_ICH_IDX_2:
  630. tmp = hperh->perh->ICHDR2;
  631. break;
  632. case ALD_ADC_ICH_IDX_3:
  633. tmp = hperh->perh->ICHDR3;
  634. break;
  635. case ALD_ADC_ICH_IDX_4:
  636. tmp = hperh->perh->ICHDR4;
  637. break;
  638. default:
  639. tmp = hperh->perh->ICHDR1;
  640. break;
  641. }
  642. return tmp;
  643. }
  644. /**
  645. * @brief Handles ADC interrupt request
  646. * @param hperh: Pointer to a adc_handle_t structure that contains
  647. * the configuration information for the specified ADC module.
  648. * @retval None
  649. */
  650. void ald_adc_irq_handler(ald_adc_handle_t *hperh)
  651. {
  652. assert_param(IS_ADC_TYPE(hperh->perh));
  653. if (ald_adc_get_it_status(hperh, ALD_ADC_IT_NCH) && ald_adc_get_flag_status(hperh, ALD_ADC_FLAG_NCH)) {
  654. WRITE_REG(hperh->perh->CLR, ALD_ADC_FLAG_NCH | ALD_ADC_FLAG_NCHS);
  655. CLEAR_BIT(hperh->state, ALD_ADC_STATE_BUSY_N);
  656. if (hperh->normal_cplt_cbk)
  657. hperh->normal_cplt_cbk(hperh);
  658. }
  659. if (ald_adc_get_it_status(hperh, ALD_ADC_IT_ICH) && ald_adc_get_flag_status(hperh, ALD_ADC_FLAG_ICH)) {
  660. WRITE_REG(hperh->perh->CLR, ALD_ADC_FLAG_ICH | ALD_ADC_FLAG_ICHS);
  661. CLEAR_BIT(hperh->state, ALD_ADC_STATE_BUSY_I);
  662. if (hperh->insert_cplt_cbk)
  663. hperh->insert_cplt_cbk(hperh);
  664. }
  665. if (ald_adc_get_it_status(hperh, ALD_ADC_IT_AWD) && ald_adc_get_flag_status(hperh, ALD_ADC_FLAG_AWD)) {
  666. CLEAR_BIT(hperh->state, ALD_ADC_STATE_BUSY_WDG);
  667. WRITE_REG(hperh->perh->CLR, ALD_ADC_FLAG_AWD);
  668. if (hperh->wdg_cbk)
  669. hperh->wdg_cbk(hperh);
  670. }
  671. if (ald_adc_get_it_status(hperh, ALD_ADC_IT_OVR) && ald_adc_get_flag_status(hperh, ALD_ADC_FLAG_OVR)) {
  672. WRITE_REG(hperh->perh->CLR, ALD_ADC_FLAG_OVR);
  673. hperh->error_code |= ALD_ADC_ERROR_OVR;
  674. hperh->state |= ALD_ADC_STATE_ERROR;
  675. if (hperh->ovr_cbk)
  676. hperh->ovr_cbk(hperh);
  677. }
  678. }
  679. /**
  680. * @}
  681. */
  682. /** @defgroup ADC_Public_Functions_Group3 Peripheral Control functions
  683. * @brief Peripheral Control functions
  684. * @{
  685. */
  686. /**
  687. * @brief Configures the the selected channel to be linked to the normal
  688. * group.
  689. * @param hperh: Pointer to a adc_handle_t structure that contains
  690. * the configuration information for the specified ADC module.
  691. * @param config: Structure of ADC channel for normal group.
  692. * @retval Status, see @ref ald_status_t.
  693. */
  694. ald_status_t ald_adc_normal_channel_config(ald_adc_handle_t *hperh, ald_adc_nch_conf_t *config)
  695. {
  696. assert_param(IS_ADC_TYPE(hperh->perh));
  697. assert_param(IS_ADC_CHANNELS_TYPE(config->ch));
  698. assert_param(IS_ADC_NCH_IDX_TYPE(config->idx));
  699. assert_param(IS_ADC_SAMPLING_TIMES_TYPE(config->samp));
  700. if (config->idx <= ALD_ADC_NCH_IDX_4 ) {
  701. hperh->perh->NCHS1 &= ~(0x1f << (uint32_t)((config->idx - 1) << 3));
  702. hperh->perh->NCHS1 |= (config->ch << (uint32_t)((config->idx - 1) << 3));
  703. }
  704. else if (config->idx <= ALD_ADC_NCH_IDX_8) {
  705. hperh->perh->NCHS2 &= ~(0x1f << (uint32_t)((config->idx - 5) << 3));
  706. hperh->perh->NCHS2 |= (config->ch << (uint32_t)((config->idx - 5) << 3));
  707. }
  708. else if (config->idx <= ALD_ADC_NCH_IDX_12) {
  709. hperh->perh->NCHS3 &= ~(0x1f << (uint32_t)((config->idx - 9) << 3));
  710. hperh->perh->NCHS3 |= (config->ch << (uint32_t)((config->idx - 9) << 3));
  711. }
  712. else {
  713. hperh->perh->NCHS4 &= ~(0x1f << (uint32_t)((config->idx - 13) << 3));
  714. hperh->perh->NCHS4 |= (config->ch << (uint32_t)((config->idx - 13) << 3));
  715. }
  716. if (config->ch < ALD_ADC_CHANNEL_8) {
  717. hperh->perh->SMPT1 &= ~(0xf << (uint32_t)(config->ch << 2));
  718. hperh->perh->SMPT1 |= config->samp << (uint32_t)(config->ch << 2);
  719. }
  720. else if (config->ch < ALD_ADC_CHANNEL_16) {
  721. hperh->perh->SMPT2 &= ~(0xf << (uint32_t)((config->ch - ALD_ADC_CHANNEL_8) << 2));
  722. hperh->perh->SMPT2 |= config->samp << (uint32_t)((config->ch - ALD_ADC_CHANNEL_8) << 2);
  723. }
  724. else {
  725. hperh->perh->SMPT3 &= ~(0xf << (uint32_t)((config->ch - ALD_ADC_CHANNEL_16) << 2));
  726. hperh->perh->SMPT3 |= config->samp << (uint32_t)((config->ch - ALD_ADC_CHANNEL_16) << 2);
  727. }
  728. return ALD_OK;
  729. }
  730. /**
  731. * @brief Configures the the selected channel to be linked to the insert
  732. * group.
  733. * @param hperh: Pointer to a adc_handle_t structure that contains
  734. * the configuration information for the specified ADC module.
  735. * @param config: Structure of ADC channel for insert group.
  736. * @retval Status, see @ref ald_status_t.
  737. */
  738. ald_status_t ald_adc_insert_channel_config(ald_adc_handle_t *hperh, ald_adc_ich_conf_t *config)
  739. {
  740. ald_status_t tmp_status = ALD_OK;
  741. assert_param(IS_ADC_TYPE(hperh->perh));
  742. assert_param(IS_ADC_CHANNELS_TYPE(config->ch));
  743. assert_param(IS_ADC_ICH_IDX_TYPE(config->idx));
  744. assert_param(IS_ADC_SAMPLING_TIMES_TYPE(config->samp));
  745. assert_param(IS_ADC_IST_OFFSET_TYPE(config->offset));
  746. assert_param(IS_ADC_ICH_NR_TYPE(config->nr));
  747. assert_param(IS_FUNC_STATE(config->auto_m));
  748. MODIFY_REG(hperh->perh->CHSL, ADC_CHSL_ISL_MSK, config->nr << ADC_CHSL_ISL_POSS);
  749. hperh->perh->ICHS &= ~(0x1f << (uint32_t)((config->idx - 1) << 3));
  750. hperh->perh->ICHS |= config->ch << (uint32_t)((config->idx - 1) << 3);
  751. if (config->auto_m == ENABLE)
  752. SET_BIT(hperh->perh->CON0, ADC_CON0_IAUTO_MSK);
  753. else
  754. CLEAR_BIT(hperh->perh->CON0, ADC_CON0_IAUTO_MSK);
  755. if (hperh->init.disc == ALD_ADC_ICH_DISC_EN) {
  756. if (config->auto_m == DISABLE) {
  757. SET_BIT(hperh->perh->CON0, ADC_CON0_ICHDCEN_MSK);
  758. }
  759. else {
  760. hperh->state |= ALD_ADC_STATE_ERROR;
  761. hperh->error_code |= ALD_ADC_ERROR_INTERNAL;
  762. tmp_status = ALD_ERROR;
  763. }
  764. }
  765. if (config->ch < 8) {
  766. hperh->perh->SMPT1 &= ~(0x0f << (uint32_t)(config->ch << 2));
  767. hperh->perh->SMPT1 |= config->samp << (uint32_t)(config->ch << 2);
  768. }
  769. else if (config->ch < 16) {
  770. hperh->perh->SMPT2 &= ~(0x0f << (uint32_t)((config->ch - 8) << 2));
  771. hperh->perh->SMPT2 |= config->samp << (uint32_t)((config->ch - 8) << 2);
  772. }
  773. else {
  774. hperh->perh->SMPT3 &= ~(0x0f << (uint32_t)((config->ch - 16) << 2));
  775. hperh->perh->SMPT3 |= config->samp << (uint32_t)((config->ch - 16) << 2);
  776. }
  777. switch (config->idx) {
  778. case ALD_ADC_ICH_IDX_1:
  779. hperh->perh->ICHOFF1 = config->offset;
  780. break;
  781. case ALD_ADC_ICH_IDX_2:
  782. hperh->perh->ICHOFF2 = config->offset;
  783. break;
  784. case ALD_ADC_ICH_IDX_3:
  785. hperh->perh->ICHOFF3 = config->offset;
  786. break;
  787. case ALD_ADC_ICH_IDX_4:
  788. hperh->perh->ICHOFF4 = config->offset;
  789. break;
  790. default:
  791. break;
  792. }
  793. return tmp_status;
  794. }
  795. /**
  796. * @brief Configures the analog watchdog.
  797. * @param hperh: Pointer to a adc_handle_t structure that contains
  798. * the configuration information for the specified ADC module.
  799. * @param config: Structure of ADC analog watchdog configuration
  800. * @retval ALD status
  801. */
  802. ald_status_t ald_adc_analog_wdg_config(ald_adc_handle_t *hperh, ald_adc_analog_wdg_conf_t *config)
  803. {
  804. assert_param(IS_ADC_TYPE(hperh->perh));
  805. assert_param(IS_ADC_ANALOG_WTD_MODE_TYPE(config->mode));
  806. assert_param(IS_FUNC_STATE(config->interrupt));
  807. assert_param(IS_HTR_TYPE(config->high_thrd));
  808. assert_param(IS_LTR_TYPE(config->low_thrd));
  809. if ((config->mode == ALD_ADC_ANAWTD_SING_NM)
  810. || (config->mode == ALD_ADC_ANAWTD_SING_IST)
  811. || (config->mode == ALD_ADC_ANAWTD_SING_NMIST))
  812. assert_param(IS_ADC_CHANNELS_TYPE(config->ch));
  813. if (config->interrupt == DISABLE)
  814. ald_adc_interrupt_config(hperh, ALD_ADC_IT_AWD, DISABLE);
  815. else
  816. ald_adc_interrupt_config(hperh, ALD_ADC_IT_AWD, ENABLE);
  817. CLEAR_BIT(hperh->perh->CON0, ADC_CON0_ICHWDTEN_MSK);
  818. CLEAR_BIT(hperh->perh->CON0, ADC_CON0_NCHWDEN_MSK);
  819. CLEAR_BIT(hperh->perh->CON0, ADC_CON0_AWDSGL_MSK);
  820. hperh->perh->CON0 |= config->mode;
  821. if (READ_BIT(hperh->perh->CON0, ADC_CON0_AWDSGL_MSK))
  822. MODIFY_REG(hperh->perh->CON0, ADC_CON0_AWDCH_MSK, config->ch << ADC_CON0_AWDCH_POSS);
  823. WRITE_REG(hperh->perh->WDTL, config->low_thrd);
  824. WRITE_REG(hperh->perh->WDTH, config->high_thrd);
  825. SET_BIT(hperh->state, ALD_ADC_STATE_BUSY_WDG);
  826. return ALD_OK;
  827. }
  828. /**
  829. * @brief Enables or disables the specified ADC interrupts.
  830. * @param hperh: Pointer to a adc_handle_t structure.
  831. * @param it: Specifies the ADC interrupt sources to be enabled or disabled.
  832. * This parameter can be one of the @ref adc_it_t.
  833. * @param state: New status
  834. * - ENABLE
  835. * - DISABLE
  836. * @retval None
  837. */
  838. void ald_adc_interrupt_config(ald_adc_handle_t *hperh, ald_adc_it_t it, type_func_t state)
  839. {
  840. assert_param(IS_ADC_TYPE(hperh->perh));
  841. assert_param(IS_ADC_IT_TYPE(it));
  842. assert_param(IS_FUNC_STATE(state));
  843. if (state == ENABLE)
  844. SET_BIT(hperh->perh->CON0, it);
  845. else
  846. CLEAR_BIT(hperh->perh->CON0, it);
  847. return;
  848. }
  849. /**
  850. * @brief Checks whether the specified ADC interrupt has occurred or not.
  851. * @param hperh: Pointer to a adc_handle_t structure.
  852. * @param it: Specifies the ADC interrupt source to check.
  853. * This parameter can be one of the @ref adc_it_t.
  854. * @retval Status
  855. * - SET
  856. * - RESET
  857. */
  858. it_status_t ald_adc_get_it_status(ald_adc_handle_t *hperh, ald_adc_it_t it)
  859. {
  860. assert_param(IS_ADC_TYPE(hperh->perh));
  861. assert_param(IS_ADC_IT_TYPE(it));
  862. if (READ_BIT(hperh->perh->CON0, it))
  863. return SET;
  864. return RESET;
  865. }
  866. /** @brief Check whether the specified ADC flag is set or not.
  867. * @param hperh: Pointer to a adc_handle_t structure.
  868. * @param flag: specifies the flag to check.
  869. * This parameter can be one of the @ref adc_flag_t.
  870. * @retval Status
  871. * - SET
  872. * - RESET
  873. */
  874. flag_status_t ald_adc_get_flag_status(ald_adc_handle_t *hperh, ald_adc_flag_t flag)
  875. {
  876. assert_param(IS_ADC_TYPE(hperh->perh));
  877. assert_param(IS_ADC_FLAGS_TYPE(flag));
  878. if (READ_BIT(hperh->perh->STAT, flag))
  879. return SET;
  880. return RESET;
  881. }
  882. /** @brief Clear the specified ADC pending flags.
  883. * @param hperh: Pointer to a adc_handle_t structure.
  884. * @param flag: specifies the flag to check.
  885. * This parameter can be one of the @ref adc_flag_t.
  886. * @retval None
  887. */
  888. void ald_adc_clear_flag_status(ald_adc_handle_t *hperh, ald_adc_flag_t flag)
  889. {
  890. assert_param(IS_ADC_TYPE(hperh->perh));
  891. assert_param(IS_ADC_FLAGS_TYPE(flag));
  892. WRITE_REG(hperh->perh->CLR, flag);
  893. return;
  894. }
  895. /**
  896. * @}
  897. */
  898. /** @defgroup ADC_Public_Functions_Group4 Peripheral State functions
  899. * @brief Peripheral State functions
  900. * @{
  901. */
  902. /**
  903. * @brief return the ADC state
  904. * @param hperh: Pointer to a adc_handle_t structure that contains
  905. * the configuration information for the specified ADC module.
  906. * @retval state
  907. */
  908. uint32_t ald_adc_get_state(ald_adc_handle_t *hperh)
  909. {
  910. return hperh->state;
  911. }
  912. /**
  913. * @brief Return the ADC error code
  914. * @param hperh: Pointer to a adc_handle_t structure that contains
  915. * the configuration information for the specified ADC module.
  916. * @retval ADC Error Code
  917. */
  918. uint32_t ald_adc_get_error(ald_adc_handle_t *hperh)
  919. {
  920. return hperh->error_code;
  921. }
  922. /**
  923. *@}
  924. */
  925. /**
  926. *@}
  927. */
  928. /** @defgroup ADC_Private_Functions ADC Private Functions
  929. * @{
  930. */
  931. /**
  932. * @brief DMA transfer complete callback.
  933. * @param arg: argument of the call back.
  934. * @retval None
  935. */
  936. static void adc_dma_normal_conv_cplt(void *arg)
  937. {
  938. ald_adc_handle_t *hperh = (ald_adc_handle_t *)arg;
  939. CLEAR_BIT(hperh->perh->CON1, ADC_CON1_DMA_MSK);
  940. if (hperh->normal_cplt_cbk)
  941. hperh->normal_cplt_cbk(hperh);
  942. }
  943. /**
  944. *@}
  945. */
  946. /**
  947. *@}
  948. */
  949. /**
  950. *@}
  951. */