ald_uart.c 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458
  1. /**
  2. *********************************************************************************
  3. *
  4. * @file ald_uart.c
  5. * @brief UART module driver.
  6. * This file provides firmware functions to manage the following
  7. * functionalities of the Universal Asynchronous Receiver Transmitter (UART) peripheral:
  8. * + Initialization and Configuration functions
  9. * + IO operation functions
  10. * + Peripheral Control functions
  11. * + Peripheral State and Errors functions
  12. *
  13. * @version V1.0
  14. * @date 23 Feb. 2023
  15. * @author AE Team
  16. * @note
  17. * Change Logs:
  18. * Date Author Notes
  19. * 23 Feb. 2023 Lisq The first version
  20. *
  21. * Copyright (C) Shanghai Eastsoft Microelectronics Co. Ltd. All rights reserved.
  22. *
  23. * SPDX-License-Identifier: Apache-2.0
  24. *
  25. * Licensed under the Apache License, Version 2.0 (the License); you may
  26. * not use this file except in compliance with the License.
  27. * You may obtain a copy of the License at
  28. *
  29. * www.apache.org/licenses/LICENSE-2.0
  30. *
  31. * Unless required by applicable law or agreed to in writing, software
  32. * distributed under the License is distributed on an AS IS BASIS, WITHOUT
  33. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  34. * See the License for the specific language governing permissions and
  35. * limitations under the License.
  36. **********************************************************************************
  37. * @verbatim
  38. ==============================================================================
  39. ##### How to use this driver #####
  40. ==============================================================================
  41. [..]
  42. The UART driver can be used as follows:
  43. (#) Declare a uart_handle_t handle structure.
  44. (#) Initialize the UART low level resources:
  45. (##) Enable the UARTx interface clock.
  46. (##) UART pins configuration:
  47. (+++) Enable the clock for the UART GPIOs.
  48. (+++) Configure the UART pins (TX as alternate function pull-up, RX as alternate function Input).
  49. (##) NVIC configuration if you need to use interrupt process (ald_uart_send_by_it()
  50. and ald_uart_recv_by_it() APIs):
  51. (+++) Configure the uart interrupt priority.
  52. (+++) Enable the NVIC UART IRQ handle.
  53. (##) DMA Configuration if you need to use DMA process (ald_uart_send_by_dma()
  54. and ald_uart_recv_by_dma() APIs):
  55. (+++) Select the DMA Tx/Rx channel.
  56. (+++) Associate the initialized DMA handle to the UART DMA Tx/Rx handle.
  57. (#) Program the Baud Rate, Word Length, Stop Bit, Parity, Hardware
  58. flow control and Mode(Receiver/Transmitter) in the hperh Init structure.
  59. (#) Initialize the UART registers by calling the ald_uart_init() API.
  60. [..]
  61. Three operation modes are available within this driver:
  62. *** Polling mode IO operation ***
  63. =================================
  64. [..]
  65. (+) Send an amount of data in blocking mode using ald_uart_send()
  66. (+) Receive an amount of data in blocking mode using ald_uart_recv()
  67. *** Interrupt mode IO operation ***
  68. ===================================
  69. [..]
  70. (+) Send an amount of data in non blocking mode using ald_uart_send_by_it()
  71. (+) At transmission end of transfer hperh->tx_cplt_cbk() is executed and user can
  72. add his own code by customization of function pointer hperh->tx_cplt_cbk()
  73. (+) Receive an amount of data in non blocking mode using ald_uart_recv_by_it()
  74. (+) At reception end of transfer hperh->rx_cplt_cbk() is executed and user can
  75. add his own code by customization of function pointer hperh->rx_cplt_cbk()
  76. (+) In case of transfer Error, hperh->error_cbk() function is executed and user can
  77. add his own code by customization of function pointer hperh->error_cbk()
  78. *** DMA mode IO operation ***
  79. ==============================
  80. [..]
  81. (+) Send an amount of data in non blocking mode (DMA) using ald_uart_send_by_dma()
  82. (+) At transmission end of transfer hperh->tx_cplt_cbk() is executed and user can
  83. add his own code by customization of function pointer hperh->tx_cplt_cbk()
  84. (+) Receive an amount of data in non blocking mode (DMA) using ald_uart_recv_by_dma()
  85. (+) At reception end of transfer hperh->rx_cplt_cbk() is executed and user can
  86. add his own code by customization of function pointer hperh->rx_cplt_cbk()
  87. (+) In case of transfer Error, hperh->error_cbk() function is executed and user can
  88. add his own code by customization of function pointer hperh->error_cbk()
  89. (+) Pause the DMA Transfer using ald_uart_dma_pause()
  90. (+) Resume the DMA Transfer using ald_uart_dma_resume()
  91. (+) Stop the DMA Transfer using ald_uart_dma_stop()
  92. @endverbatim
  93. ******************************************************************************
  94. */
  95. #include "ald_uart.h"
  96. /** @addtogroup ES32VF2264_ALD
  97. * @{
  98. */
  99. /** @defgroup UART UART
  100. * @brief UART module driver
  101. * @{
  102. */
  103. /** @defgroup UART_Private_Functions UART Private Functions
  104. * @brief UART Private functions
  105. * @{
  106. */
  107. /**
  108. * @brief DMA uart transmit process complete callback.
  109. * @param arg: Pointer to a uart_handle_t structure.
  110. * @retval None
  111. */
  112. static void uart_dma_send_cplt(void *arg)
  113. {
  114. ald_uart_handle_t *hperh = (ald_uart_handle_t *)arg;
  115. hperh->tx_count = hperh->tx_size;
  116. ald_uart_dma_req_config(hperh, ALD_UART_DMA_REQ_TX, DISABLE);
  117. hperh->tx_count = 0;
  118. ald_uart_interrupt_config(hperh, ALD_UART_IT_TBC, ENABLE);
  119. CLEAR_BIT(hperh->state, ALD_UART_STATE_TX_MASK);
  120. return;
  121. }
  122. /**
  123. * @brief DMA uart receive process complete callback.
  124. * @param arg: Pointer to a uart_handle_t structure.
  125. * @retval None
  126. */
  127. static void uart_dma_recv_cplt(void *arg)
  128. {
  129. uint32_t stat = 0;
  130. ald_uart_handle_t *hperh = (ald_uart_handle_t *)arg;
  131. hperh->tx_count = hperh->tx_size;
  132. ald_uart_dma_req_config(hperh, ALD_UART_DMA_REQ_RX, DISABLE);
  133. hperh->rx_count = 0;
  134. CLEAR_BIT(hperh->state, ALD_UART_STATE_RX_MASK);
  135. stat = hperh->perh->STAT;
  136. /* Handle parity error */
  137. if ((READ_BIT(stat, ALD_UART_STATUS_PERR)) != RESET)
  138. hperh->err_code |= ALD_UART_ERROR_PE;
  139. /* Handle frame error */
  140. if ((READ_BIT(stat, ALD_UART_STATUS_FERR)) != RESET)
  141. hperh->err_code |= ALD_UART_ERROR_FE;
  142. /* Handle rx overflow error */
  143. if ((READ_BIT(stat, ALD_UART_STATUS_RFOERR)) != RESET) {
  144. hperh->err_code |= ALD_UART_ERROR_ORE;
  145. }
  146. if (hperh->rx_cplt_cbk)
  147. hperh->rx_cplt_cbk(hperh);
  148. return;
  149. }
  150. /**
  151. * @brief This function handles uart Communication Timeout.
  152. * @param hperh: Pointer to a uart_handle_t structure.
  153. * @param flag: specifies the uart flag to check.
  154. * @param status: The new Flag status (SET or RESET).
  155. * @param timeout: Timeout duration
  156. * @retval Status, see @ref ald_status_t.
  157. */
  158. static ald_status_t uart_wait_flag(ald_uart_handle_t *hperh, ald_uart_status_t flag, flag_status_t status, uint32_t timeout)
  159. {
  160. uint32_t tick;
  161. if (timeout == 0)
  162. return ALD_ERROR;
  163. tick = ald_get_tick();
  164. /* Waiting for flag */
  165. while ((ald_uart_get_status(hperh, flag)) != status)
  166. {
  167. if (((ald_get_tick()) - tick) > timeout)
  168. return ALD_TIMEOUT;
  169. }
  170. return ALD_OK;
  171. }
  172. /**
  173. * @brief Sends an amount of data in non blocking mode.
  174. * @param hperh: Pointer to a uart_handle_t structure.
  175. * @retval Status, see @ref ald_status_t.
  176. */
  177. static ald_status_t __uart_send_by_it(ald_uart_handle_t *hperh)
  178. {
  179. if ((hperh->state & ALD_UART_STATE_TX_MASK) == 0x0)
  180. return ALD_BUSY;
  181. hperh->perh->TXBUF = (uint8_t)(*hperh->tx_buf++ & 0x00FF);
  182. hperh->tx_count++;
  183. if (hperh->tx_count >= hperh->tx_size)
  184. {
  185. ald_uart_interrupt_config(hperh, ALD_UART_IT_TFEMPTY, DISABLE);
  186. ald_uart_interrupt_config(hperh, ALD_UART_IT_TBC, ENABLE);
  187. }
  188. return ALD_OK;
  189. }
  190. /**
  191. * @brief Wraps up transmission in non blocking mode.
  192. * @param hperh: pointer to a uart_handle_t structure.
  193. * @retval Status, see @ref ald_status_t.
  194. */
  195. static ald_status_t __uart_end_send_by_it(ald_uart_handle_t *hperh)
  196. {
  197. uint32_t cnt = 0xFFFFFF;
  198. ald_uart_interrupt_config(hperh, ALD_UART_IT_TBC, DISABLE);
  199. CLEAR_BIT(hperh->state, ALD_UART_STATE_TX_MASK);
  200. while ((hperh->perh->STAT & ALD_UART_STATUS_TSBUSY) && (cnt--));
  201. ald_uart_clear_flag_status(hperh, ALD_UART_IF_TBC);
  202. if (hperh->tx_cplt_cbk)
  203. hperh->tx_cplt_cbk(hperh);
  204. return ALD_OK;
  205. }
  206. /**
  207. * @brief Receives an amount of data in non blocking mode
  208. * @param hperh: Pointer to a uart_handle_t structure.
  209. * @retval Status, see @ref ald_status_t.
  210. */
  211. static ald_status_t __uart_recv_by_it(ald_uart_handle_t *hperh)
  212. {
  213. if ((hperh->state & ALD_UART_STATE_RX_MASK) == 0x0)
  214. return ALD_BUSY;
  215. *hperh->rx_buf++ = (uint8_t)(hperh->perh->RXBUF & 0xFF);
  216. hperh->rx_count++;
  217. if (hperh->rx_count >= hperh->rx_size)
  218. {
  219. ald_uart_interrupt_config(hperh, ALD_UART_IT_RFNEMPTY, DISABLE);
  220. CLEAR_BIT(hperh->state, ALD_UART_STATE_RX_MASK);
  221. if (hperh->rx_cplt_cbk)
  222. hperh->rx_cplt_cbk(hperh);
  223. }
  224. return ALD_OK;
  225. }
  226. /**
  227. * @}
  228. */
  229. /** @defgroup UART_Public_Functions UART Public Functions
  230. * @{
  231. */
  232. /** @defgroup UART_Public_Functions_Group1 Initialization and Configuration functions
  233. * @brief Initialization and Configuration functions
  234. *
  235. * @verbatim
  236. ===============================================================================
  237. ##### Initialization and Configuration functions #####
  238. ===============================================================================
  239. [..]
  240. This subsection provides a set of functions allowing to initialize the UARTx
  241. and configure UARTx param.
  242. (+) For the UARTx only these parameters can be configured:
  243. (++) Baud Rate
  244. (++) Word Length
  245. (++) Stop Bit
  246. (++) Parity
  247. (++) Hardware flow control
  248. (+) For RS485 mode, user also need configure some parameters by
  249. ald_uart_rs485_config():
  250. (++) Enable/disable normal point mode
  251. (++) Enable/disable auto-direction
  252. (++) Enable/disable address detection invert
  253. (++) Enable/disable address for compare
  254. @endverbatim
  255. * @{
  256. */
  257. /**
  258. * @brief Reset UART peripheral
  259. * @param hperh: Pointer to a uart_handle_t structure that contains
  260. * the configuration information for the specified uart module.
  261. * @retval None
  262. */
  263. void ald_uart_reset(ald_uart_handle_t *hperh)
  264. {
  265. assert_param(IS_UART_ALL(hperh->perh));
  266. WRITE_REG(hperh->perh->BRR, 0x0);
  267. WRITE_REG(hperh->perh->LCON, 0x0);
  268. WRITE_REG(hperh->perh->MCON, 0x0);
  269. WRITE_REG(hperh->perh->RS485, 0x0);
  270. WRITE_REG(hperh->perh->SCARD, 0x0);
  271. WRITE_REG(hperh->perh->LIN, 0x0);
  272. WRITE_REG(hperh->perh->RTOR, 0x0);
  273. WRITE_REG(hperh->perh->IDR, 0xFFF);
  274. hperh->err_code = ALD_UART_ERROR_NONE;
  275. hperh->state = ALD_UART_STATE_RESET;
  276. __UNLOCK(hperh);
  277. return;
  278. }
  279. /**
  280. * @brief Initializes the UARTx according to the specified
  281. * parameters in the uart_handle_t.
  282. * @param hperh: Pointer to a uart_handle_t structure that contains
  283. * the configuration information for the specified UART module.
  284. * @retval None
  285. */
  286. void ald_uart_init(ald_uart_handle_t *hperh)
  287. {
  288. assert_param(IS_UART_ALL(hperh->perh));
  289. assert_param(IS_UART_BAUDRATE(hperh->init.baud));
  290. assert_param(IS_UART_WORD_LENGTH(hperh->init.word_length));
  291. assert_param(IS_UART_STOPBITS(hperh->init.stop_bits));
  292. assert_param(IS_UART_PARITY(hperh->init.parity));
  293. assert_param(IS_UART_MODE(hperh->init.mode));
  294. assert_param(IS_UART_HARDWARE_FLOW_CONTROL(hperh->init.fctl));
  295. ald_uart_reset(hperh);
  296. MODIFY_REG(hperh->perh->LCON, UART_LCON_DLS_MSK, hperh->init.word_length << UART_LCON_DLS_POSS);
  297. MODIFY_REG(hperh->perh->LCON, UART_LCON_STOP_MSK, hperh->init.stop_bits << UART_LCON_STOP_POS);
  298. if ((hperh->init.parity) != ALD_UART_PARITY_NONE)
  299. {
  300. SET_BIT(hperh->perh->LCON, UART_LCON_PE_MSK);
  301. if ((hperh->init.parity) == ALD_UART_PARITY_ODD)
  302. CLEAR_BIT(hperh->perh->LCON, UART_LCON_PS_MSK);
  303. if ((hperh->init.parity) == ALD_UART_PARITY_EVEN)
  304. SET_BIT(hperh->perh->LCON, UART_LCON_PS_MSK);
  305. }
  306. MODIFY_REG(hperh->perh->MCON, UART_MCON_AFCEN_MSK, hperh->init.fctl << UART_MCON_AFCEN_POS);
  307. hperh->perh->BRR = (ald_cmu_get_pclk_clock() + (hperh->init.baud >> 1)) / hperh->init.baud;
  308. if (hperh->init.mode == ALD_UART_MODE_LIN)
  309. SET_BIT(hperh->perh->LIN, UART_LIN_LINEN_MSK);
  310. else if (hperh->init.mode == ALD_UART_MODE_IrDA)
  311. SET_BIT(hperh->perh->MCON, UART_MCON_IREN_MSK);
  312. else if (hperh->init.mode == ALD_UART_MODE_RS485)
  313. SET_BIT(hperh->perh->RS485, UART_RS485_AADEN_MSK);
  314. else if (hperh->init.mode == ALD_UART_MODE_HDSEL)
  315. SET_BIT(hperh->perh->MCON, UART_MCON_HDEN_MSK);
  316. else if (hperh->init.mode == ALD_UART_MODE_SCARD)
  317. SET_BIT(hperh->perh->SCARD, UART_SCARD_SCEN_MSK);
  318. else
  319. ; /* do nothing */
  320. SET_BIT(hperh->perh->LCON, UART_LCON_RXEN_MSK);
  321. SET_BIT(hperh->perh->LCON, UART_LCON_TXEN_MSK);
  322. hperh->state = ALD_UART_STATE_READY;
  323. hperh->err_code = ALD_UART_ERROR_NONE;
  324. return;
  325. }
  326. /**
  327. * @brief Configure the RS485 mode according to the specified
  328. * parameters in the uart_rs485_config_t.
  329. * @param hperh: Pointer to a uart_handle_t structure that contains
  330. * the configuration information for the specified UART module.
  331. * @param config: Specifies the RS485 parameters.
  332. * @retval None
  333. */
  334. void ald_uart_rs485_config(ald_uart_handle_t *hperh, ald_uart_rs485_config_t *config)
  335. {
  336. assert_param(IS_UART_ALL(hperh->perh));
  337. assert_param(IS_FUNC_STATE(config->normal));
  338. assert_param(IS_FUNC_STATE(config->dir));
  339. assert_param(IS_FUNC_STATE(config->invert));
  340. MODIFY_REG(hperh->perh->RS485, UART_RS485_AADNEN_MSK, config->normal << UART_RS485_AADNEN_POS);
  341. MODIFY_REG(hperh->perh->RS485, UART_RS485_AADACEN_MSK, config->dir << UART_RS485_AADACEN_POS);
  342. MODIFY_REG(hperh->perh->RS485, UART_RS485_AADINV_MSK, config->invert << UART_RS485_AADINV_POS);
  343. MODIFY_REG(hperh->perh->RS485, UART_RS485_ADDR_MSK, config->addr << UART_RS485_ADDR_POSS);
  344. return;
  345. }
  346. /**
  347. * @brief Configure the smart card mode according to the specified
  348. * parameters in the uart_scard_config_t.
  349. * @param hperh: Pointer to a uart_handle_t structure that contains
  350. * the configuration information for the specified UART module.
  351. * @param config: Specifies the scard parameters.
  352. * @retval None
  353. */
  354. void ald_uart_scard_config(ald_uart_handle_t *hperh, ald_uart_scard_config_t *config)
  355. {
  356. assert_param(IS_UART_ENHANCE(hperh->perh));
  357. assert_param(IS_UART_SCARD_CLK(config->clk_div));
  358. MODIFY_REG(hperh->perh->SCARD, UART_SCARD_BLEN_MSK, config->block_len << UART_SCARD_BLEN_POSS);
  359. MODIFY_REG(hperh->perh->SCARD, UART_SCARD_GT_MSK, config->pt << UART_SCARD_GT_POSS);
  360. MODIFY_REG(hperh->perh->SCARD, UART_SCARD_SCCNT_MSK, config->retry << UART_SCARD_SCCNT_POSS);
  361. MODIFY_REG(hperh->perh->SCARD, UART_SCARD_PSC_MSK, config->clk_div << UART_SCARD_PSC_POSS);
  362. MODIFY_REG(hperh->perh->SCARD, UART_SCARD_SCLKEN_MSK, config->clk_out << UART_SCARD_SCLKEN_POS);
  363. return;
  364. }
  365. /**
  366. * @}
  367. */
  368. /** @defgroup UART_Public_Functions_Group2 IO operation functions
  369. * @brief UART Transmit and Receive functions
  370. * @verbatim
  371. ==============================================================================
  372. # IO operation functions #
  373. ==============================================================================
  374. [..]
  375. This subsection provides a set of functions allowing to manage the UART data transfers.
  376. (#) There are two modes of transfer:
  377. (++) Blocking mode: The communication is performed in polling mode.
  378. The Status of all data processing is returned by the same function
  379. after finishing transfer.
  380. (++) Non blocking mode: The communication is performed using Interrupts
  381. or DMA, these APIs return the Status.
  382. The end of the data processing will be indicated through the
  383. dedicated UART IRQ when using Interrupt mode or the DMA IRQ when
  384. using DMA mode.
  385. The hperh->tx_cplt_cbk(), hperh->rx_cplt_cbk() user callbacks
  386. will be executed respectively at the end of the transmit or receive process.
  387. The hperh->error_cbk() user callback will be executed when
  388. a communication error is detected.
  389. (#) Blocking mode APIs are:
  390. (++) ald_uart_send()
  391. (++) ald_uart_recv()
  392. (#) Non Blocking mode APIs with Interrupt are:
  393. (++) ald_uart_send_by_it()
  394. (++) ald_uart_recv_by_it()
  395. (++) ald_uart_irq_handler()
  396. (#) Non Blocking mode functions with DMA are:
  397. (++) ald_uart_send_by_dma()
  398. (++) ald_uart_recv_by_dma()
  399. (++) ald_uart_dma_pause()
  400. (++) ald_uart_dma_resume()
  401. (++) ald_uart_dma_stop()
  402. (#) A set of transfer complete callbacks are provided in non blocking mode:
  403. (++) hperh->tx_cplt_cbk()
  404. (++) hperh->rx_cplt_cbk()
  405. (++) hperh->error_cbk()
  406. @endverbatim
  407. * @{
  408. */
  409. /**
  410. * @brief Sends an amount of data in blocking mode.
  411. * @param hperh: Pointer to a uart_handle_t structure.
  412. * @param buf: Pointer to data buffer
  413. * @param size: Amount of data to be sent
  414. * @param timeout: Timeout duration
  415. * @retval Status, see @ref ald_status_t.
  416. */
  417. ald_status_t ald_uart_send(ald_uart_handle_t *hperh, uint8_t *buf, uint16_t size, uint32_t timeout)
  418. {
  419. assert_param(IS_UART_ALL(hperh->perh));
  420. if ((hperh->state != ALD_UART_STATE_READY) && (hperh->state != ALD_UART_STATE_BUSY_RX))
  421. return ALD_BUSY;
  422. if ((buf == NULL) || (size == 0))
  423. return ALD_ERROR;
  424. __LOCK(hperh);
  425. hperh->err_code = ALD_UART_ERROR_NONE;
  426. SET_BIT(hperh->state, ALD_UART_STATE_TX_MASK);
  427. hperh->tx_size = size;
  428. hperh->tx_count = 0;
  429. while (size-- > 0)
  430. {
  431. if (uart_wait_flag(hperh, ALD_UART_STATUS_TFEMPTY, SET, timeout) != ALD_OK) {
  432. __UNLOCK(hperh);
  433. hperh->state = ALD_UART_STATE_READY;
  434. return ALD_TIMEOUT;
  435. }
  436. hperh->perh->TXBUF = (*buf++ & 0xFF);
  437. hperh->tx_count++;
  438. }
  439. if (uart_wait_flag(hperh, ALD_UART_STATUS_TSBUSY, RESET, timeout) != ALD_OK)
  440. {
  441. __UNLOCK(hperh);
  442. hperh->state = ALD_UART_STATE_READY;
  443. return ALD_TIMEOUT;
  444. }
  445. CLEAR_BIT(hperh->state, ALD_UART_STATE_TX_MASK);
  446. __UNLOCK(hperh);
  447. return ALD_OK;
  448. }
  449. /**
  450. * @brief Receives an amount of data in blocking mode.
  451. * @param hperh: Pointer to a uart_handle_t structure.
  452. * @param buf: Pointer to data buffer
  453. * @param size: Amount of data to be received
  454. * @param timeout: Timeout duration
  455. * @retval Status, see @ref ald_status_t.
  456. */
  457. ald_status_t ald_uart_recv(ald_uart_handle_t *hperh, uint8_t *buf, uint16_t size, uint32_t timeout)
  458. {
  459. uint32_t stat = 0;
  460. uint32_t err = 0;
  461. uint32_t tick = 0;
  462. assert_param(IS_UART_ALL(hperh->perh));
  463. if ((hperh->state != ALD_UART_STATE_READY) && (hperh->state != ALD_UART_STATE_BUSY_TX))
  464. return ALD_BUSY;
  465. if ((buf == NULL) || (size == 0) || (timeout == 0))
  466. return ALD_ERROR;
  467. __LOCK(hperh);
  468. hperh->err_code = ALD_UART_ERROR_NONE;
  469. SET_BIT(hperh->state, ALD_UART_STATE_RX_MASK);
  470. hperh->rx_size = size;
  471. hperh->rx_count = 0;
  472. err = (ALD_UART_STATUS_PERR | ALD_UART_STATUS_FERR | ALD_UART_STATUS_RFOERR);
  473. while (size-- > 0)
  474. {
  475. tick = ald_get_tick();
  476. /* Waiting for flag */
  477. while (1)
  478. {
  479. stat = hperh->perh->STAT;
  480. if (READ_BIT(stat, ALD_UART_STATUS_RFNEMPTY) != RESET)
  481. break;
  482. if (((ald_get_tick()) - tick) > timeout)
  483. {
  484. __UNLOCK(hperh);
  485. hperh->state = ALD_UART_STATE_READY;
  486. return ALD_TIMEOUT;
  487. }
  488. }
  489. if ((stat & err) == RESET)
  490. {
  491. *buf++ = (uint8_t)(hperh->perh->RXBUF & 0xFF);
  492. hperh->rx_count++;
  493. }
  494. else
  495. {
  496. /* Handle parity error */
  497. if ((READ_BIT(stat, ALD_UART_STATUS_PERR)) != RESET)
  498. hperh->err_code |= ALD_UART_ERROR_PE;
  499. /* Handle frame error */
  500. if ((READ_BIT(stat, ALD_UART_STATUS_FERR)) != RESET)
  501. hperh->err_code |= ALD_UART_ERROR_FE;
  502. /* Handle rx overflow error */
  503. if ((READ_BIT(stat, ALD_UART_STATUS_RFOERR)) != RESET)
  504. hperh->err_code |= ALD_UART_ERROR_ORE;
  505. CLEAR_BIT(hperh->state, ALD_UART_STATE_RX_MASK);
  506. __UNLOCK(hperh);
  507. return ALD_ERROR;
  508. }
  509. }
  510. CLEAR_BIT(hperh->state, ALD_UART_STATE_RX_MASK);
  511. __UNLOCK(hperh);
  512. return ALD_OK;
  513. }
  514. /**
  515. * @brief Sends an amount of data in blocking mode.
  516. Don't care about device lock. This is for RTOS.
  517. * @param hperh: Pointer to a uart_handle_t structure.
  518. * @param buf: Pointer to data buffer
  519. * @param size: Amount of data to be sent
  520. * @param timeout: Timeout duration
  521. * @retval Status, see @ref ald_status_t.
  522. */
  523. ald_status_t ald_uart_send_n_lock(ald_uart_handle_t *hperh, uint8_t *buf, uint16_t size, uint32_t timeout)
  524. {
  525. assert_param(IS_UART_ALL(hperh->perh));
  526. if ((hperh->state != ALD_UART_STATE_READY) && (hperh->state != ALD_UART_STATE_BUSY_RX))
  527. return ALD_BUSY;
  528. if ((buf == NULL) || (size == 0))
  529. return ALD_ERROR;
  530. hperh->err_code = ALD_UART_ERROR_NONE;
  531. SET_BIT(hperh->state, ALD_UART_STATE_TX_MASK);
  532. hperh->tx_size = size;
  533. hperh->tx_count = 0;
  534. while (size-- > 0)
  535. {
  536. if (uart_wait_flag(hperh, ALD_UART_STATUS_TFEMPTY, SET, timeout) != ALD_OK)
  537. {
  538. hperh->state = ALD_UART_STATE_READY;
  539. return ALD_TIMEOUT;
  540. }
  541. hperh->perh->TXBUF = (*buf++ & 0xFF);
  542. hperh->tx_count++;
  543. }
  544. if (uart_wait_flag(hperh, ALD_UART_STATUS_TSBUSY, RESET, timeout) != ALD_OK)
  545. {
  546. hperh->state = ALD_UART_STATE_READY;
  547. return ALD_TIMEOUT;
  548. }
  549. CLEAR_BIT(hperh->state, ALD_UART_STATE_TX_MASK);
  550. return ALD_OK;
  551. }
  552. /**
  553. * @brief Receives an amount of data in blocking mode.
  554. Don't care about device lock. This is for RTOS.
  555. * @param hperh: Pointer to a uart_handle_t structure.
  556. * @param buf: Pointer to data buffer
  557. * @param size: Amount of data to be received
  558. * @param timeout: Timeout duration
  559. * @retval Status, see @ref ald_status_t.
  560. */
  561. ald_status_t ald_uart_recv_n_lock(ald_uart_handle_t *hperh, uint8_t *buf, uint16_t size, uint32_t timeout)
  562. {
  563. assert_param(IS_UART_ALL(hperh->perh));
  564. if ((hperh->state != ALD_UART_STATE_READY) && (hperh->state != ALD_UART_STATE_BUSY_TX))
  565. return ALD_BUSY;
  566. if ((buf == NULL) || (size == 0))
  567. return ALD_ERROR;
  568. hperh->err_code = ALD_UART_ERROR_NONE;
  569. SET_BIT(hperh->state, ALD_UART_STATE_RX_MASK);
  570. hperh->rx_size = size;
  571. hperh->rx_count = 0;
  572. while (size-- > 0)
  573. {
  574. if (uart_wait_flag(hperh, ALD_UART_STATUS_RFNEMPTY, SET, timeout) != ALD_OK)
  575. {
  576. hperh->state = ALD_UART_STATE_READY;
  577. return ALD_TIMEOUT;
  578. }
  579. *buf++ = (uint8_t)(hperh->perh->RXBUF & 0xFF);
  580. hperh->rx_count++;
  581. }
  582. CLEAR_BIT(hperh->state, ALD_UART_STATE_RX_MASK);
  583. return ALD_OK;
  584. }
  585. /**
  586. * @brief Sends an amount of data in non blocking mode.
  587. * @param hperh: Pointer to a uart_handle_t structure.
  588. * @param buf: Pointer to data buffer
  589. * @param size: Amount of data to be sent
  590. * @retval Status, see @ref ald_status_t.
  591. */
  592. ald_status_t ald_uart_send_by_it(ald_uart_handle_t *hperh, uint8_t *buf, uint16_t size)
  593. {
  594. assert_param(IS_UART_ALL(hperh->perh));
  595. if ((hperh->state != ALD_UART_STATE_READY) && (hperh->state != ALD_UART_STATE_BUSY_RX))
  596. return ALD_BUSY;
  597. if ((buf == NULL ) || (size == 0))
  598. return ALD_ERROR;
  599. __LOCK(hperh);
  600. hperh->tx_buf = buf;
  601. hperh->tx_size = size;
  602. hperh->tx_count = 0;
  603. hperh->err_code = ALD_UART_ERROR_NONE;
  604. SET_BIT(hperh->state, ALD_UART_STATE_TX_MASK);
  605. __UNLOCK(hperh);
  606. SET_BIT(hperh->perh->ICR, UART_ICR_TFEMPTY_MSK);
  607. ald_uart_interrupt_config(hperh, ALD_UART_IT_TFEMPTY, ENABLE);
  608. return ALD_OK;
  609. }
  610. /**
  611. * @brief Receives an amount of data in non blocking mode
  612. * @param hperh: Pointer to a uart_handle_t structure.
  613. * @param buf: Pointer to data buffer
  614. * @param size: Amount of data to be received
  615. * @retval Status, see @ref ald_status_t.
  616. */
  617. ald_status_t ald_uart_recv_by_it(ald_uart_handle_t *hperh, uint8_t *buf, uint16_t size)
  618. {
  619. assert_param(IS_UART_ALL(hperh->perh));
  620. if ((hperh->state != ALD_UART_STATE_READY) && (hperh->state != ALD_UART_STATE_BUSY_TX))
  621. return ALD_BUSY;
  622. if ((buf == NULL ) || (size == 0))
  623. return ALD_ERROR;
  624. __LOCK(hperh);
  625. hperh->rx_buf = buf;
  626. hperh->rx_size = size;
  627. hperh->rx_count = 0;
  628. hperh->err_code = ALD_UART_ERROR_NONE;
  629. SET_BIT(hperh->state, ALD_UART_STATE_RX_MASK);
  630. __UNLOCK(hperh);
  631. SET_BIT(hperh->perh->ICR, UART_ICR_RFNEMPTY_MSK);
  632. ald_uart_interrupt_config(hperh, ALD_UART_IT_RFNEMPTY, ENABLE);
  633. return ALD_OK;
  634. }
  635. /**
  636. * @brief Receives an frame of data in non blocking mode
  637. * @param hperh: Pointer to a uart_handle_t structure.
  638. * @param buf: Pointer to data buffer
  639. * @param size: Max length of frame.
  640. * @param t_out: Timeout duration.
  641. * @retval Status, see @ref ald_status_t.
  642. */
  643. ald_status_t ald_uart_recv_frame_by_it(ald_uart_handle_t *hperh, uint8_t *buf, uint16_t size, uint32_t t_out)
  644. {
  645. assert_param(IS_UART_ALL(hperh->perh));
  646. if ((hperh->state != ALD_UART_STATE_READY) && (hperh->state != ALD_UART_STATE_BUSY_TX))
  647. return ALD_BUSY;
  648. if ((buf == NULL ) || (t_out == 0) || (t_out > 0xFFFFFF) || (size == 0))
  649. return ALD_ERROR;
  650. __LOCK(hperh);
  651. hperh->rx_buf = buf;
  652. hperh->rx_size = size;
  653. hperh->rx_count = 0;
  654. hperh->err_code = ALD_UART_ERROR_NONE;
  655. SET_BIT(hperh->state, ALD_UART_STATE_RX_MASK);
  656. __UNLOCK(hperh);
  657. SET_BIT(hperh->perh->ICR, UART_ICR_RFNEMPTY_MSK);
  658. SET_BIT(hperh->perh->RTOR, UART_RTOR_RTOEN_MSK);
  659. MODIFY_REG(hperh->perh->RTOR, UART_RTOR_RTO_MSK, t_out << UART_RTOR_RTO_POSS);
  660. ald_uart_interrupt_config(hperh, ALD_UART_IT_RFNEMPTY, ENABLE);
  661. ald_uart_interrupt_config(hperh, ALD_UART_IT_RXTO, ENABLE);
  662. return ALD_OK;
  663. }
  664. /**
  665. * @brief Sends an amount of data in non blocking mode.
  666. * @param hperh: Pointer to a uart_handle_t structure.
  667. * @param buf: Pointer to data buffer
  668. * @param size: Amount of data to be sent
  669. * @param channel: DMA channel as UART transmit
  670. * @retval Status, see @ref ald_status_t.
  671. */
  672. ald_status_t ald_uart_send_by_dma(ald_uart_handle_t *hperh, uint8_t *buf, uint16_t size, uint8_t channel)
  673. {
  674. assert_param(IS_UART_ALL(hperh->perh));
  675. if ((hperh->state != ALD_UART_STATE_READY) && (hperh->state != ALD_UART_STATE_BUSY_RX))
  676. return ALD_BUSY;
  677. if ((buf == NULL ) || (size == 0))
  678. return ALD_ERROR;
  679. __LOCK(hperh);
  680. hperh->tx_buf = buf;
  681. hperh->tx_size = size;
  682. hperh->tx_count = 0;
  683. hperh->err_code = ALD_UART_ERROR_NONE;
  684. SET_BIT(hperh->state, ALD_UART_STATE_TX_MASK);
  685. if (hperh->hdmatx.perh == NULL)
  686. hperh->hdmatx.perh = DMA;
  687. /* Set the dma parameters */
  688. hperh->hdmatx.cplt_tc_cbk = uart_dma_send_cplt;
  689. hperh->hdmatx.cplt_tc_arg = (void *)hperh;
  690. ald_dma_config_struct(&hperh->hdmatx.config);
  691. hperh->hdmatx.config.src = (void *)buf;
  692. hperh->hdmatx.config.dst = (void *)&hperh->perh->TXBUF;
  693. hperh->hdmatx.config.size = size;
  694. hperh->hdmatx.config.src_inc = ALD_DMA_DATA_INC_ENABLE;
  695. hperh->hdmatx.config.dst_inc = ALD_DMA_DATA_INC_DISABLE;
  696. hperh->hdmatx.config.msigsel = ALD_DMA_MSIGSEL_UART_TXEMPTY;
  697. hperh->hdmatx.config.channel = channel;
  698. if (hperh->init.mode == ALD_UART_MODE_RS485)
  699. hperh->hdmatx.config.src_data_width = ALD_DMA_DATA_SIZE_HALFWORD;
  700. if (hperh->perh == CUART0)
  701. hperh->hdmatx.config.msel = ALD_DMA_MSEL_CUART0;
  702. else if (hperh->perh == CUART1)
  703. hperh->hdmatx.config.msel = ALD_DMA_MSEL_CUART1;
  704. else if (hperh->perh == CUART2)
  705. hperh->hdmatx.config.msel = ALD_DMA_MSEL_CUART2;
  706. else if (hperh->perh == EUART0)
  707. hperh->hdmatx.config.msel = ALD_DMA_MSEL_EUART0;
  708. else if (hperh->perh == EUART1)
  709. hperh->hdmatx.config.msel = ALD_DMA_MSEL_EUART1;
  710. else
  711. ; /* do nothing */
  712. ald_dma_config_basic(&hperh->hdmatx);
  713. ald_dma_interrupt_config(channel, ALD_DMA_IT_FLAG_TC, ENABLE);
  714. __UNLOCK(hperh);
  715. ald_uart_clear_flag_status(hperh, ALD_UART_IF_TBC);
  716. ald_uart_dma_req_config(hperh, ALD_UART_DMA_REQ_TX, ENABLE);
  717. return ALD_OK;
  718. }
  719. /**
  720. * @brief Receives an amount of data in non blocking mode.
  721. * @param hperh: Pointer to a uart_handle_t structure.
  722. * @param buf: Pointer to data buffer
  723. * @param size: Amount of data to be received
  724. * @param channel: DMA channel as UART receive
  725. * @retval Status, see @ref ald_status_t.
  726. */
  727. ald_status_t ald_uart_recv_by_dma(ald_uart_handle_t *hperh, uint8_t *buf, uint16_t size, uint8_t channel)
  728. {
  729. assert_param(IS_UART_ALL(hperh->perh));
  730. if ((hperh->state != ALD_UART_STATE_READY) && (hperh->state != ALD_UART_STATE_BUSY_TX))
  731. return ALD_BUSY;
  732. if ((buf == NULL) || (size == 0))
  733. return ALD_ERROR;
  734. __LOCK(hperh);
  735. hperh->rx_buf = buf;
  736. hperh->rx_size = size;
  737. hperh->err_code = ALD_UART_ERROR_NONE;
  738. SET_BIT(hperh->state, ALD_UART_STATE_RX_MASK);
  739. if (hperh->hdmarx.perh == NULL)
  740. hperh->hdmarx.perh = DMA;
  741. /* Set the dma parameters */
  742. hperh->hdmarx.cplt_tc_cbk = uart_dma_recv_cplt;
  743. hperh->hdmarx.cplt_tc_arg = (void *)hperh;
  744. ald_dma_config_struct(&hperh->hdmarx.config);
  745. hperh->hdmarx.config.src = (void *)&hperh->perh->RXBUF;
  746. hperh->hdmarx.config.dst = (void *)buf;
  747. hperh->hdmarx.config.size = size;
  748. hperh->hdmarx.config.src_inc = ALD_DMA_DATA_INC_DISABLE;
  749. hperh->hdmarx.config.dst_inc = ALD_DMA_DATA_INC_ENABLE;
  750. hperh->hdmarx.config.msigsel = ALD_DMA_MSIGSEL_UART_RNR;
  751. hperh->hdmarx.config.channel = channel;
  752. if (hperh->init.mode == ALD_UART_MODE_RS485)
  753. hperh->hdmarx.config.dst_data_width = ALD_DMA_DATA_SIZE_HALFWORD;
  754. if (hperh->perh == CUART0)
  755. hperh->hdmarx.config.msel = ALD_DMA_MSEL_CUART0;
  756. else if (hperh->perh == CUART1)
  757. hperh->hdmarx.config.msel = ALD_DMA_MSEL_CUART1;
  758. else if (hperh->perh == CUART2)
  759. hperh->hdmarx.config.msel = ALD_DMA_MSEL_CUART2;
  760. else if (hperh->perh == EUART0)
  761. hperh->hdmarx.config.msel = ALD_DMA_MSEL_EUART0;
  762. else if (hperh->perh == EUART1)
  763. hperh->hdmarx.config.msel = ALD_DMA_MSEL_EUART1;
  764. else
  765. ;
  766. __UNLOCK(hperh);
  767. ald_dma_config_basic(&hperh->hdmarx);
  768. ald_dma_interrupt_config(channel, ALD_DMA_IT_FLAG_TC, ENABLE);
  769. ald_uart_dma_req_config(hperh, ALD_UART_DMA_REQ_RX, ENABLE);
  770. return ALD_OK;
  771. }
  772. /**
  773. * @brief Pauses the DMA Transfer.
  774. * @param hperh: Pointer to a uart_handle_t structure.
  775. * @retval Status, see @ref ald_status_t.
  776. */
  777. ald_status_t ald_uart_dma_pause(ald_uart_handle_t *hperh)
  778. {
  779. assert_param(IS_UART_ALL(hperh->perh));
  780. ald_uart_dma_req_config(hperh, ALD_UART_DMA_REQ_TX, DISABLE);
  781. ald_uart_dma_req_config(hperh, ALD_UART_DMA_REQ_RX, DISABLE);
  782. return ALD_OK;
  783. }
  784. /**
  785. * @brief Resumes the DMA Transfer.
  786. * @param hperh: Pointer to a uart_handle_t structure.
  787. * @retval Status, see @ref ald_status_t.
  788. */
  789. ald_status_t ald_uart_dma_resume(ald_uart_handle_t *hperh)
  790. {
  791. assert_param(IS_UART_ALL(hperh->perh));
  792. ald_uart_dma_req_config(hperh, ALD_UART_DMA_REQ_TX, ENABLE);
  793. ald_uart_dma_req_config(hperh, ALD_UART_DMA_REQ_RX, ENABLE);
  794. return ALD_OK;
  795. }
  796. /**
  797. * @brief Stops the DMA Transfer.
  798. * @param hperh: Pointer to a uart_handle_t structure.
  799. * @retval Status, see @ref ald_status_t.
  800. */
  801. ald_status_t ald_uart_dma_stop(ald_uart_handle_t *hperh)
  802. {
  803. assert_param(IS_UART_ALL(hperh->perh));
  804. ald_uart_dma_req_config(hperh, ALD_UART_DMA_REQ_TX, DISABLE);
  805. ald_uart_dma_req_config(hperh, ALD_UART_DMA_REQ_RX, DISABLE);
  806. hperh->state = ALD_UART_STATE_READY;
  807. return ALD_OK;
  808. }
  809. /**
  810. * @brief This function handles UART interrupt request.
  811. * @param hperh: Pointer to a uart_handle_t structure.
  812. * @retval None
  813. */
  814. void ald_uart_irq_handler(ald_uart_handle_t *hperh)
  815. {
  816. uint32_t stat = 0;
  817. assert_param(IS_UART_ALL(hperh->perh));
  818. stat = hperh->perh->STAT;
  819. /* Handle parity error */
  820. if (((READ_BIT(stat, ALD_UART_STATUS_PERR)) != RESET) && \
  821. (ald_uart_get_state(hperh)) == ALD_UART_STATE_RX_MASK)
  822. hperh->err_code |= ALD_UART_ERROR_PE;
  823. /* Handle frame error */
  824. if (((READ_BIT(stat, ALD_UART_STATUS_FERR)) != RESET) && \
  825. (ald_uart_get_state(hperh)) == ALD_UART_STATE_RX_MASK)
  826. hperh->err_code |= ALD_UART_ERROR_FE;
  827. /* Handle rx overflow error */
  828. if (((READ_BIT(stat, ALD_UART_STATUS_RFOERR)) != RESET) && \
  829. (ald_uart_get_state(hperh)) == ALD_UART_STATE_RX_MASK)
  830. hperh->err_code |= ALD_UART_ERROR_ORE;
  831. /* Handle tx overflow error */
  832. if (((READ_BIT(stat, ALD_UART_STATUS_TFOERR)) != RESET) && \
  833. (ald_uart_get_state(hperh)) == ALD_UART_STATE_TX_MASK)
  834. hperh->err_code |= ALD_UART_ERROR_ORE;
  835. /* Receive */
  836. if ((ald_uart_get_mask_flag_status(hperh, ALD_UART_IF_RFNEMPTY)) != RESET)
  837. {
  838. __uart_recv_by_it(hperh);
  839. ald_uart_clear_flag_status(hperh, ALD_UART_IF_RFNEMPTY);
  840. }
  841. /* Transmit */
  842. if ((ald_uart_get_mask_flag_status(hperh, ALD_UART_IF_TFEMPTY)) != RESET)
  843. {
  844. __uart_send_by_it(hperh);
  845. ald_uart_clear_flag_status(hperh, ALD_UART_IF_TFEMPTY);
  846. }
  847. /* End Transmit */
  848. if ((ald_uart_get_mask_flag_status(hperh, ALD_UART_IF_TBC)) != RESET)
  849. {
  850. __uart_end_send_by_it(hperh);
  851. ald_uart_clear_flag_status(hperh, ALD_UART_IF_TBC);
  852. }
  853. /* Receive frame timeout*/
  854. if ((ald_uart_get_mask_flag_status(hperh, ALD_UART_IF_RXTO)) != RESET)
  855. {
  856. ald_uart_clear_flag_status(hperh, ALD_UART_IF_RXTO);
  857. ald_uart_interrupt_config(hperh, ALD_UART_IT_RXTO, DISABLE);
  858. CLEAR_BIT(hperh->state, ALD_UART_STATE_RX_MASK);
  859. if (hperh->rx_cplt_cbk)
  860. hperh->rx_cplt_cbk(hperh);
  861. }
  862. /* Handle error state */
  863. if (hperh->err_code != ALD_UART_ERROR_NONE)
  864. {
  865. hperh->state = ALD_UART_STATE_READY;
  866. if (hperh->error_cbk)
  867. hperh->error_cbk(hperh);
  868. }
  869. }
  870. /**
  871. * @brief This function handles UART interrupt request.
  872. * @param hperh: Pointer to a uart_handle_t structure.
  873. * @retval None
  874. */
  875. void ald_uart_irq_handler_fast(ald_uart_handle_t *hperh)
  876. {
  877. volatile uint32_t tmp = hperh->perh->IFM;
  878. /* Transmit */
  879. if (tmp & 0x10000)
  880. {
  881. hperh->perh->ICR = 0x10000;
  882. hperh->perh->TXBUF = *hperh->tx_buf++;
  883. ++hperh->tx_count;
  884. if (hperh->tx_count >= hperh->tx_size)
  885. {
  886. hperh->perh->IDR = 0x10000;
  887. CLEAR_BIT(hperh->state, ALD_UART_STATE_TX_MASK);
  888. if (hperh->tx_cplt_cbk)
  889. hperh->tx_cplt_cbk(hperh);
  890. }
  891. }
  892. /* End Transmit */
  893. if (tmp & 0x4000)
  894. {
  895. hperh->perh->ICR = 0x4000;
  896. hperh->perh->IDR = 0x4000;
  897. CLEAR_BIT(hperh->state, ALD_UART_STATE_TX_MASK);
  898. if (hperh->tx_cplt_cbk)
  899. hperh->tx_cplt_cbk(hperh);
  900. }
  901. /* Receive*/
  902. if (tmp & 0x400)
  903. {
  904. hperh->perh->ICR = 0x400;
  905. *hperh->rx_buf++ = (uint8_t)(hperh->perh->RXBUF & 0xFF);
  906. ++hperh->rx_count;
  907. if (hperh->rx_count >= hperh->rx_size)
  908. {
  909. hperh->perh->IDR = 0x400;
  910. CLEAR_BIT(hperh->state, ALD_UART_STATE_RX_MASK);
  911. if (hperh->rx_cplt_cbk)
  912. hperh->rx_cplt_cbk(hperh);
  913. }
  914. }
  915. /* Receive frame */
  916. if (tmp & 0x10)
  917. {
  918. hperh->perh->ICR = 0x10;
  919. hperh->perh->IDR = 0x10;
  920. CLEAR_BIT(hperh->state, ALD_UART_STATE_RX_MASK);
  921. if (hperh->rx_cplt_cbk)
  922. hperh->rx_cplt_cbk(hperh);
  923. }
  924. return;
  925. }
  926. /**
  927. * @}
  928. */
  929. /** @defgroup UART_Public_Functions_Group3 Peripheral Control functions
  930. * @brief UART control functions
  931. *
  932. * @verbatim
  933. ==============================================================================
  934. ##### Peripheral Control functions #####
  935. ==============================================================================
  936. [..]
  937. This subsection provides a set of functions allowing to control the UART:
  938. (+) ald_uart_interrupt_config() API can be helpful to configure UART interrupt source.
  939. (+) ald_uart_dma_req_config() API can be helpful to configure UART DMA request.
  940. (+) ald_uart_tx_fifo_config() API can be helpful to configure UART TX FIFO paramters.
  941. (+) ald_uart_rx_fifo_config() API can be helpful to configure UART RX FIFO paramters.
  942. (+) ald_uart_lin_send_break() API can send a frame of break in LIN mode.
  943. (+) ald_uart_lin_detect_break_len_config() API can be helpful to configure the length of break frame.
  944. (+) ald_uart_auto_baud_config() API can be helpful to configure detection data mode.
  945. (+) ald_uart_get_it_status() API can get the status of interrupt source.
  946. (+) ald_uart_get_status() API can get the status of UART_SR register.
  947. (+) ald_uart_get_flag_status() API can get the status of UART flag.
  948. (+) ald_uart_get_mask_flag_status() API can get status os flag and interrupt source.
  949. (+) ald_uart_clear_flag_status() API can clear UART flag.
  950. @endverbatim
  951. * @{
  952. */
  953. /**
  954. * @brief Enable/disable the specified UART interrupts.
  955. * @param hperh: Pointer to a uart_handle_t structure.
  956. * @param it: Specifies the UART interrupt sources to be enabled or disabled.
  957. * This parameter can be one of the @ref uart_it_t.
  958. * @param state: New state of the specified UART interrupts.
  959. * This parameter can be:
  960. * @arg ENABLE
  961. * @arg DISABLE
  962. * @retval None
  963. */
  964. void ald_uart_interrupt_config(ald_uart_handle_t *hperh, ald_uart_it_t it, type_func_t state)
  965. {
  966. assert_param(IS_UART_ALL(hperh->perh));
  967. assert_param(IS_UART_IT(it));
  968. assert_param(IS_FUNC_STATE(state));
  969. if (state == ENABLE)
  970. hperh->perh->IER = it;
  971. else
  972. hperh->perh->IDR = it;
  973. return;
  974. }
  975. /**
  976. * @brief Configure UART DMA request.
  977. * @param hperh: Pointer to a uart_handle_t structure.
  978. * @param req: The type of DMA request.
  979. * @param state: New state of the specified DMA request.
  980. * This parameter can be:
  981. * @arg ENABLE
  982. * @arg DISABLE
  983. * @retval None
  984. */
  985. void ald_uart_dma_req_config(ald_uart_handle_t *hperh, ald_uart_dma_req_t req, type_func_t state)
  986. {
  987. assert_param(IS_UART_ALL(hperh->perh));
  988. assert_param(IS_UART_DMA_REQ(req));
  989. assert_param(IS_FUNC_STATE(state));
  990. if (req == ALD_UART_DMA_REQ_TX)
  991. {
  992. if (state == ENABLE)
  993. SET_BIT(hperh->perh->MCON, UART_MCON_TXDMAEN_MSK);
  994. else
  995. CLEAR_BIT(hperh->perh->MCON, UART_MCON_TXDMAEN_MSK);
  996. }
  997. else
  998. {
  999. if (state == ENABLE)
  1000. SET_BIT(hperh->perh->MCON, UART_MCON_RXDMAEN_MSK);
  1001. else
  1002. CLEAR_BIT(hperh->perh->MCON, UART_MCON_RXDMAEN_MSK);
  1003. }
  1004. return;
  1005. }
  1006. /**
  1007. * @brief Enable/Disable break signal detect interrup.
  1008. * @param hperh: Pointer to a uart_handle_t structure.
  1009. * @param status: The new status.
  1010. * @retval None
  1011. */
  1012. void uart_lin_break_detect_irq(ald_uart_handle_t *hperh, type_func_t status)
  1013. {
  1014. assert_param(IS_UART_ENHANCE(hperh->perh));
  1015. if (status == ENABLE)
  1016. SET_BIT(hperh->perh->IER, UART_IER_LINBK_MSK);
  1017. else
  1018. CLEAR_BIT(hperh->perh->IER, UART_IER_LINBK_MSK);
  1019. return;
  1020. }
  1021. /**
  1022. * @brief request to send a frame of break.
  1023. * @param hperh: Pointer to a uart_handle_t structure.
  1024. * @retval None
  1025. */
  1026. void ald_uart_lin_send_break(ald_uart_handle_t *hperh)
  1027. {
  1028. assert_param(IS_UART_ENHANCE(hperh->perh));
  1029. SET_BIT(hperh->perh->LIN, UART_LIN_LINBKREQ_MSK);
  1030. hperh->perh->TXBUF = 0x55;
  1031. return;
  1032. }
  1033. /**
  1034. * @brief Configure the length of break frame to be detect.
  1035. * @param hperh: Pointer to a uart_handle_t structure.
  1036. * @param len: Length of break frame.
  1037. * @arg LIN_BREAK_LEN_10B
  1038. * @arg LIN_BREAK_LEN_11B
  1039. * @retval None
  1040. */
  1041. void ald_uart_lin_detect_break_len_config(ald_uart_handle_t *hperh, ald_uart_lin_break_len_t len)
  1042. {
  1043. assert_param(IS_UART_ENHANCE(hperh->perh));
  1044. assert_param(IS_UART_LIN_BREAK_LEN(len));
  1045. MODIFY_REG(hperh->perh->LIN, UART_LIN_LINBDL_MSK, len << UART_LIN_LINBDL_POS);
  1046. return;
  1047. }
  1048. /**
  1049. * @brief Configure the mode of auto-baud-rate detect.
  1050. * @param hperh: Pointer to a uart_handle_t structure.
  1051. * @param mode: The mode of auto-baud-rate detect.
  1052. * @arg UART_ABRMOD_1_TO_0
  1053. * @arg UART_ABRMOD_1
  1054. * @arg UART_ABRMOD_0_TO_1
  1055. * @retval None
  1056. */
  1057. void ald_uart_auto_baud_config(ald_uart_handle_t *hperh, ald_uart_auto_baud_mode_t mode)
  1058. {
  1059. assert_param(IS_UART_ALL(hperh->perh));
  1060. assert_param(IS_UART_AUTO_BAUD_MODE(mode));
  1061. MODIFY_REG(hperh->perh->MCON, UART_MCON_ABRMOD_MSK, mode << UART_MCON_ABRMOD_POSS);
  1062. return;
  1063. }
  1064. /**
  1065. * @brief Send address in RS485 mode.
  1066. * @param hperh: Pointer to a uart_handle_t structure that contains
  1067. * the configuration information for the specified UART module.
  1068. * @param addr: the address of RS485 device.
  1069. * @param timeout: Timeout duration
  1070. * @retval The ALD status.
  1071. */
  1072. ald_status_t ald_uart_rs485_send_addr(ald_uart_handle_t *hperh, uint16_t addr, uint32_t timeout)
  1073. {
  1074. assert_param(IS_UART_ALL(hperh->perh));
  1075. if ((hperh->state != ALD_UART_STATE_READY) && (hperh->state != ALD_UART_STATE_BUSY_RX))
  1076. return ALD_BUSY;
  1077. SET_BIT(hperh->state, ALD_UART_STATE_TX_MASK);
  1078. if (uart_wait_flag(hperh, ALD_UART_STATUS_TFEMPTY, SET, timeout) != ALD_OK)
  1079. {
  1080. hperh->state = ALD_UART_STATE_READY;
  1081. return ALD_TIMEOUT;
  1082. }
  1083. WRITE_REG(hperh->perh->TXBUF, (addr | 0x100));
  1084. if (uart_wait_flag(hperh, ALD_UART_STATUS_TFEMPTY, SET, timeout) != ALD_OK)
  1085. {
  1086. hperh->state = ALD_UART_STATE_READY;
  1087. return ALD_TIMEOUT;
  1088. }
  1089. CLEAR_BIT(hperh->state, ALD_UART_STATE_TX_MASK);
  1090. return ALD_OK;
  1091. }
  1092. /**
  1093. * @brief Get the status of UART interrupt source.
  1094. * @param hperh: Pointer to a uart_handle_t structure.
  1095. * @param it: Specifies the UART interrupt source.
  1096. * This parameter can be one of the @ref uart_it_t.
  1097. * @retval Status:
  1098. * - 0: RESET
  1099. * - 1: SET
  1100. */
  1101. it_status_t ald_uart_get_it_status(ald_uart_handle_t *hperh, ald_uart_it_t it)
  1102. {
  1103. it_status_t status = RESET;
  1104. assert_param(IS_UART_ALL(hperh->perh));
  1105. assert_param(IS_UART_IT(it));
  1106. if (hperh->perh->IVS & it)
  1107. status = SET;
  1108. return status;
  1109. }
  1110. /**
  1111. * @brief Get the status of UART_SR register.
  1112. * @param hperh: Pointer to a uart_handle_t structure.
  1113. * @param status: Specifies the UART status type.
  1114. * This parameter can be one of the @ref uart_status_t.
  1115. * @retval Status:
  1116. * - 0: RESET
  1117. * - 1: SET
  1118. */
  1119. flag_status_t ald_uart_get_status(ald_uart_handle_t *hperh, ald_uart_status_t status)
  1120. {
  1121. assert_param(IS_UART_ALL(hperh->perh));
  1122. assert_param(IS_UART_STATUS(status));
  1123. if (hperh->perh->STAT & status)
  1124. return SET;
  1125. return RESET;
  1126. }
  1127. /**
  1128. * @brief Get the status of UART interrupt flag.
  1129. * @param hperh: Pointer to a uart_handle_t structure.
  1130. * @param flag: Specifies the UART interrupt flag.
  1131. * This parameter can be one of the @ref uart_flag_t.
  1132. * @retval Status:
  1133. * - 0: RESET
  1134. * - 1: SET
  1135. */
  1136. flag_status_t ald_uart_get_flag_status(ald_uart_handle_t *hperh, ald_uart_flag_t flag)
  1137. {
  1138. assert_param(IS_UART_ALL(hperh->perh));
  1139. assert_param(IS_UART_IF(flag));
  1140. if (hperh->perh->RIF & flag)
  1141. return SET;
  1142. return RESET;
  1143. }
  1144. /**
  1145. * @brief Get the status of interrupt flag and interupt source.
  1146. * @param hperh: Pointer to a uart_handle_t structure.
  1147. * @param flag: Specifies the UART interrupt flag.
  1148. * This parameter can be one of the @ref uart_flag_t.
  1149. * @retval Status:
  1150. * - 0: RESET
  1151. * - 1: SET
  1152. */
  1153. flag_status_t ald_uart_get_mask_flag_status(ald_uart_handle_t *hperh, ald_uart_flag_t flag)
  1154. {
  1155. assert_param(IS_UART_ALL(hperh->perh));
  1156. assert_param(IS_UART_IF(flag));
  1157. if (hperh->perh->IFM & flag)
  1158. return SET;
  1159. return RESET;
  1160. }
  1161. /**
  1162. * @brief Clear the UART interrupt flag.
  1163. * @param hperh: Pointer to a uart_handle_t structure.
  1164. * @param flag: Specifies the UART interrupt flag.
  1165. * This parameter can be one of the @ref uart_flag_t.
  1166. * @retval None
  1167. */
  1168. void ald_uart_clear_flag_status(ald_uart_handle_t *hperh, ald_uart_flag_t flag)
  1169. {
  1170. assert_param(IS_UART_ALL(hperh->perh));
  1171. assert_param(IS_UART_IF(flag));
  1172. hperh->perh->ICR = flag;
  1173. return;
  1174. }
  1175. /**
  1176. * @}
  1177. */
  1178. /** @defgroup UART_Public_Functions_Group4 Peripheral State and Errors functions
  1179. * @brief UART State and Errors functions
  1180. *
  1181. @verbatim
  1182. ==============================================================================
  1183. ##### Peripheral State and Errors functions #####
  1184. ==============================================================================
  1185. [..]
  1186. This subsection provides a set of functions allowing to return the State of
  1187. UART communication process, return Peripheral Errors occurred during communication
  1188. process
  1189. (+) ald_uart_get_state() API can be helpful to check in run-time the state of the UART peripheral.
  1190. (+) ald_uart_get_error() check in run-time errors that could be occurred during communication.
  1191. @endverbatim
  1192. * @{
  1193. */
  1194. /**
  1195. * @brief Returns the UART state.
  1196. * @param hperh: Pointer to a uart_handle_t structure.
  1197. * @retval ALD state
  1198. */
  1199. ald_uart_state_t ald_uart_get_state(ald_uart_handle_t *hperh)
  1200. {
  1201. return hperh->state;
  1202. }
  1203. /**
  1204. * @brief Return the UART error code
  1205. * @param hperh: Pointer to a uart_handle_t structure.
  1206. * @retval UART Error Code
  1207. */
  1208. uint32_t ald_uart_get_error(ald_uart_handle_t *hperh)
  1209. {
  1210. return hperh->err_code;
  1211. }
  1212. /**
  1213. * @}
  1214. */
  1215. /**
  1216. * @}
  1217. */
  1218. /**
  1219. * @}
  1220. */
  1221. /**
  1222. * @}
  1223. */