stm32l4xx_hal_uart_ex.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953
  1. /**
  2. ******************************************************************************
  3. * @file stm32l4xx_hal_uart_ex.c
  4. * @author MCD Application Team
  5. * @brief Extended UART HAL module driver.
  6. * This file provides firmware functions to manage the following extended
  7. * functionalities of the Universal Asynchronous Receiver Transmitter Peripheral (UART).
  8. * + Initialization and de-initialization functions
  9. * + Peripheral Control functions
  10. *
  11. *
  12. @verbatim
  13. ==============================================================================
  14. ##### UART peripheral extended features #####
  15. ==============================================================================
  16. (#) Declare a UART_HandleTypeDef handle structure.
  17. (#) For the UART RS485 Driver Enable mode, initialize the UART registers
  18. by calling the HAL_RS485Ex_Init() API.
  19. (#) FIFO mode enabling/disabling and RX/TX FIFO threshold programming.
  20. -@- When USART operates in FIFO mode, FIFO mode must be enabled prior
  21. starting RX/TX transfers. Also RX/TX FIFO thresholds must be
  22. configured prior starting RX/TX transfers.
  23. (#) Slave mode enabling/disabling and NSS pin configuration.
  24. -@- When USART operates in Slave mode, Slave mode must be enabled prior
  25. starting RX/TX transfers.
  26. @endverbatim
  27. ******************************************************************************
  28. * @attention
  29. *
  30. * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
  31. *
  32. * Redistribution and use in source and binary forms, with or without modification,
  33. * are permitted provided that the following conditions are met:
  34. * 1. Redistributions of source code must retain the above copyright notice,
  35. * this list of conditions and the following disclaimer.
  36. * 2. Redistributions in binary form must reproduce the above copyright notice,
  37. * this list of conditions and the following disclaimer in the documentation
  38. * and/or other materials provided with the distribution.
  39. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  40. * may be used to endorse or promote products derived from this software
  41. * without specific prior written permission.
  42. *
  43. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  44. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  45. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  46. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  47. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  48. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  49. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  50. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  51. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  52. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  53. *
  54. ******************************************************************************
  55. */
  56. /* Includes ------------------------------------------------------------------*/
  57. #include "stm32l4xx_hal.h"
  58. /** @addtogroup STM32L4xx_HAL_Driver
  59. * @{
  60. */
  61. /** @defgroup UARTEx UARTEx
  62. * @brief UART Extended HAL module driver
  63. * @{
  64. */
  65. #ifdef HAL_UART_MODULE_ENABLED
  66. /* Private typedef -----------------------------------------------------------*/
  67. /* Private define ------------------------------------------------------------*/
  68. /* Private macros ------------------------------------------------------------*/
  69. /* Private variables ---------------------------------------------------------*/
  70. /* Private function prototypes -----------------------------------------------*/
  71. /** @defgroup UARTEx_Private_Functions UARTEx Private Functions
  72. * @{
  73. */
  74. static void UARTEx_Wakeup_AddressConfig(UART_HandleTypeDef *huart, UART_WakeUpTypeDef WakeUpSelection);
  75. #if defined(USART_CR1_FIFOEN)
  76. static void UARTEx_SetNbDataToProcess(UART_HandleTypeDef *huart);
  77. #endif
  78. /**
  79. * @}
  80. */
  81. /* Exported functions --------------------------------------------------------*/
  82. /** @defgroup UARTEx_Exported_Functions UARTEx Exported Functions
  83. * @{
  84. */
  85. /** @defgroup UARTEx_Exported_Functions_Group1 Initialization and de-initialization functions
  86. * @brief Extended Initialization and Configuration Functions
  87. *
  88. @verbatim
  89. ===============================================================================
  90. ##### Initialization and Configuration functions #####
  91. ===============================================================================
  92. [..]
  93. This subsection provides a set of functions allowing to initialize the USARTx or the UARTy
  94. in asynchronous mode.
  95. (+) For the asynchronous mode the parameters below can be configured:
  96. (++) Baud Rate
  97. (++) Word Length
  98. (++) Stop Bit
  99. (++) Parity: If the parity is enabled, then the MSB bit of the data written
  100. in the data register is transmitted but is changed by the parity bit.
  101. (++) Hardware flow control
  102. (++) Receiver/transmitter modes
  103. (++) Over Sampling Method
  104. (++) One-Bit Sampling Method
  105. (+) For the asynchronous mode, the following advanced features can be configured as well:
  106. (++) TX and/or RX pin level inversion
  107. (++) data logical level inversion
  108. (++) RX and TX pins swap
  109. (++) RX overrun detection disabling
  110. (++) DMA disabling on RX error
  111. (++) MSB first on communication line
  112. (++) auto Baud rate detection
  113. [..]
  114. The HAL_RS485Ex_Init() API follows the UART RS485 mode configuration
  115. procedures (details for the procedures are available in reference manual).
  116. @endverbatim
  117. Depending on the frame length defined by the M1 and M0 bits (7-bit,
  118. 8-bit or 9-bit), the possible UART formats are listed in the
  119. following table.
  120. Table 1. UART frame format.
  121. +-----------------------------------------------------------------------+
  122. | M1 bit | M0 bit | PCE bit | UART frame |
  123. |---------|---------|-----------|---------------------------------------|
  124. | 0 | 0 | 0 | | SB | 8 bit data | STB | |
  125. |---------|---------|-----------|---------------------------------------|
  126. | 0 | 0 | 1 | | SB | 7 bit data | PB | STB | |
  127. |---------|---------|-----------|---------------------------------------|
  128. | 0 | 1 | 0 | | SB | 9 bit data | STB | |
  129. |---------|---------|-----------|---------------------------------------|
  130. | 0 | 1 | 1 | | SB | 8 bit data | PB | STB | |
  131. |---------|---------|-----------|---------------------------------------|
  132. | 1 | 0 | 0 | | SB | 7 bit data | STB | |
  133. |---------|---------|-----------|---------------------------------------|
  134. | 1 | 0 | 1 | | SB | 6 bit data | PB | STB | |
  135. +-----------------------------------------------------------------------+
  136. * @{
  137. */
  138. /**
  139. * @brief Initialize the RS485 Driver enable feature according to the specified
  140. * parameters in the UART_InitTypeDef and creates the associated handle.
  141. * @param huart UART handle.
  142. * @param Polarity Select the driver enable polarity.
  143. * This parameter can be one of the following values:
  144. * @arg @ref UART_DE_POLARITY_HIGH DE signal is active high
  145. * @arg @ref UART_DE_POLARITY_LOW DE signal is active low
  146. * @param AssertionTime Driver Enable assertion time:
  147. * 5-bit value defining the time between the activation of the DE (Driver Enable)
  148. * signal and the beginning of the start bit. It is expressed in sample time
  149. * units (1/8 or 1/16 bit time, depending on the oversampling rate)
  150. * @param DeassertionTime Driver Enable deassertion time:
  151. * 5-bit value defining the time between the end of the last stop bit, in a
  152. * transmitted message, and the de-activation of the DE (Driver Enable) signal.
  153. * It is expressed in sample time units (1/8 or 1/16 bit time, depending on the
  154. * oversampling rate).
  155. * @retval HAL status
  156. */
  157. HAL_StatusTypeDef HAL_RS485Ex_Init(UART_HandleTypeDef *huart, uint32_t Polarity, uint32_t AssertionTime, uint32_t DeassertionTime)
  158. {
  159. uint32_t temp = 0x0;
  160. /* Check the UART handle allocation */
  161. if(huart == NULL)
  162. {
  163. return HAL_ERROR;
  164. }
  165. /* Check the Driver Enable UART instance */
  166. assert_param(IS_UART_DRIVER_ENABLE_INSTANCE(huart->Instance));
  167. /* Check the Driver Enable polarity */
  168. assert_param(IS_UART_DE_POLARITY(Polarity));
  169. /* Check the Driver Enable assertion time */
  170. assert_param(IS_UART_ASSERTIONTIME(AssertionTime));
  171. /* Check the Driver Enable deassertion time */
  172. assert_param(IS_UART_DEASSERTIONTIME(DeassertionTime));
  173. if(huart->gState == HAL_UART_STATE_RESET)
  174. {
  175. /* Allocate lock resource and initialize it */
  176. huart->Lock = HAL_UNLOCKED;
  177. /* Init the low level hardware : GPIO, CLOCK, CORTEX */
  178. HAL_UART_MspInit(huart);
  179. }
  180. huart->gState = HAL_UART_STATE_BUSY;
  181. /* Disable the Peripheral */
  182. __HAL_UART_DISABLE(huart);
  183. /* Set the UART Communication parameters */
  184. if (UART_SetConfig(huart) == HAL_ERROR)
  185. {
  186. return HAL_ERROR;
  187. }
  188. if(huart->AdvancedInit.AdvFeatureInit != UART_ADVFEATURE_NO_INIT)
  189. {
  190. UART_AdvFeatureConfig(huart);
  191. }
  192. /* Enable the Driver Enable mode by setting the DEM bit in the CR3 register */
  193. SET_BIT(huart->Instance->CR3, USART_CR3_DEM);
  194. /* Set the Driver Enable polarity */
  195. MODIFY_REG(huart->Instance->CR3, USART_CR3_DEP, Polarity);
  196. /* Set the Driver Enable assertion and deassertion times */
  197. temp = (AssertionTime << UART_CR1_DEAT_ADDRESS_LSB_POS);
  198. temp |= (DeassertionTime << UART_CR1_DEDT_ADDRESS_LSB_POS);
  199. MODIFY_REG(huart->Instance->CR1, (USART_CR1_DEDT|USART_CR1_DEAT), temp);
  200. /* Enable the Peripheral */
  201. __HAL_UART_ENABLE(huart);
  202. /* TEACK and/or REACK to check before moving huart->gState and huart->RxState to Ready */
  203. return (UART_CheckIdleState(huart));
  204. }
  205. /**
  206. * @}
  207. */
  208. /** @defgroup UARTEx_Exported_Functions_Group2 IO operation functions
  209. * @brief Extended functions
  210. *
  211. @verbatim
  212. ===============================================================================
  213. ##### IO operation functions #####
  214. ===============================================================================
  215. This subsection provides a set of Wakeup and FIFO mode related callback functions.
  216. (#) Wakeup from Stop mode Callback:
  217. (+) HAL_UARTEx_WakeupCallback()
  218. (#) TX/RX Fifos Callbacks:
  219. (+) HAL_UARTEx_RxFifoFullCallback()
  220. (+) HAL_UARTEx_TxFifoEmptyCallback()
  221. @endverbatim
  222. * @{
  223. */
  224. /**
  225. * @brief UART wakeup from Stop mode callback.
  226. * @param huart UART handle.
  227. * @retval None
  228. */
  229. __weak void HAL_UARTEx_WakeupCallback(UART_HandleTypeDef *huart)
  230. {
  231. /* Prevent unused argument(s) compilation warning */
  232. UNUSED(huart);
  233. /* NOTE : This function should not be modified, when the callback is needed,
  234. the HAL_UARTEx_WakeupCallback can be implemented in the user file.
  235. */
  236. }
  237. #if defined(USART_CR1_FIFOEN)
  238. /**
  239. * @brief UART RX Fifo full callback.
  240. * @param huart UART handle.
  241. * @retval None
  242. */
  243. __weak void HAL_UARTEx_RxFifoFullCallback (UART_HandleTypeDef *huart)
  244. {
  245. /* Prevent unused argument(s) compilation warning */
  246. UNUSED(huart);
  247. /* NOTE : This function should not be modified, when the callback is needed,
  248. the HAL_UARTEx_RxFifoFullCallback can be implemented in the user file.
  249. */
  250. }
  251. /**
  252. * @brief UART TX Fifo empty callback.
  253. * @param huart UART handle.
  254. * @retval None
  255. */
  256. __weak void HAL_UARTEx_TxFifoEmptyCallback (UART_HandleTypeDef *huart)
  257. {
  258. /* Prevent unused argument(s) compilation warning */
  259. UNUSED(huart);
  260. /* NOTE : This function should not be modified, when the callback is needed,
  261. the HAL_UARTEx_TxFifoEmptyCallback can be implemented in the user file.
  262. */
  263. }
  264. #endif
  265. /**
  266. * @}
  267. */
  268. /** @defgroup UARTEx_Exported_Functions_Group3 Peripheral Control functions
  269. * @brief Extended Peripheral Control functions
  270. *
  271. @verbatim
  272. ===============================================================================
  273. ##### Peripheral Control functions #####
  274. ===============================================================================
  275. [..] This section provides the following functions:
  276. (+) HAL_UARTEx_EnableClockStopMode() API enables the UART clock (HSI or LSE only) during stop mode
  277. (+) HAL_UARTEx_DisableClockStopMode() API disables the above functionality
  278. (+) HAL_MultiProcessorEx_AddressLength_Set() API optionally sets the UART node address
  279. detection length to more than 4 bits for multiprocessor address mark wake up.
  280. (+) HAL_UARTEx_StopModeWakeUpSourceConfig() API defines the wake-up from stop mode
  281. trigger: address match, Start Bit detection or RXNE bit status.
  282. (+) HAL_UARTEx_EnableStopMode() API enables the UART to wake up the MCU from stop mode
  283. (+) HAL_UARTEx_DisableStopMode() API disables the above functionality
  284. (+) HAL_UARTEx_WakeupCallback() called upon UART wakeup interrupt
  285. (+) HAL_UARTEx_EnableSPISlaveMode() API enables the SPI slave mode
  286. (+) HAL_UARTEx_DisableSPISlaveMode() API disables the SPI slave mode
  287. (+) HAL_UARTEx_ConfigNSS API configures the Slave Select input pin (NSS)
  288. (+) HAL_UARTEx_EnableFifoMode() API enables the FIFO mode
  289. (+) HAL_UARTEx_DisableFifoMode() API disables the FIFO mode
  290. (+) HAL_UARTEx_SetTxFifoThreshold() API sets the TX FIFO threshold
  291. (+) HAL_UARTEx_SetRxFifoThreshold() API sets the RX FIFO threshold
  292. @endverbatim
  293. * @{
  294. */
  295. /**
  296. * @brief By default in multiprocessor mode, when the wake up method is set
  297. * to address mark, the UART handles only 4-bit long addresses detection;
  298. * this API allows to enable longer addresses detection (6-, 7- or 8-bit
  299. * long).
  300. * @note Addresses detection lengths are: 6-bit address detection in 7-bit data mode,
  301. * 7-bit address detection in 8-bit data mode, 8-bit address detection in 9-bit data mode.
  302. * @param huart UART handle.
  303. * @param AddressLength This parameter can be one of the following values:
  304. * @arg @ref UART_ADDRESS_DETECT_4B 4-bit long address
  305. * @arg @ref UART_ADDRESS_DETECT_7B 6-, 7- or 8-bit long address
  306. * @retval HAL status
  307. */
  308. HAL_StatusTypeDef HAL_MultiProcessorEx_AddressLength_Set(UART_HandleTypeDef *huart, uint32_t AddressLength)
  309. {
  310. /* Check the UART handle allocation */
  311. if(huart == NULL)
  312. {
  313. return HAL_ERROR;
  314. }
  315. /* Check the address length parameter */
  316. assert_param(IS_UART_ADDRESSLENGTH_DETECT(AddressLength));
  317. huart->gState = HAL_UART_STATE_BUSY;
  318. /* Disable the Peripheral */
  319. __HAL_UART_DISABLE(huart);
  320. /* Set the address length */
  321. MODIFY_REG(huart->Instance->CR2, USART_CR2_ADDM7, AddressLength);
  322. /* Enable the Peripheral */
  323. __HAL_UART_ENABLE(huart);
  324. /* TEACK and/or REACK to check before moving huart->gState to Ready */
  325. return (UART_CheckIdleState(huart));
  326. }
  327. /**
  328. * @brief Set Wakeup from Stop mode interrupt flag selection.
  329. * @note It is the application responsibility to enable the interrupt used as
  330. * usart_wkup interrupt source before entering low-power mode.
  331. * @param huart UART handle.
  332. * @param WakeUpSelection Address match, Start Bit detection or RXNE/RXFNE bit status.
  333. * This parameter can be one of the following values:
  334. * @arg @ref UART_WAKEUP_ON_ADDRESS
  335. * @arg @ref UART_WAKEUP_ON_STARTBIT
  336. * @arg @ref UART_WAKEUP_ON_READDATA_NONEMPTY
  337. * @retval HAL status
  338. */
  339. HAL_StatusTypeDef HAL_UARTEx_StopModeWakeUpSourceConfig(UART_HandleTypeDef *huart, UART_WakeUpTypeDef WakeUpSelection)
  340. {
  341. HAL_StatusTypeDef status = HAL_OK;
  342. uint32_t tickstart = 0;
  343. /* check the wake-up from stop mode UART instance */
  344. assert_param(IS_UART_WAKEUP_FROMSTOP_INSTANCE(huart->Instance));
  345. /* check the wake-up selection parameter */
  346. assert_param(IS_UART_WAKEUP_SELECTION(WakeUpSelection.WakeUpEvent));
  347. /* Process Locked */
  348. __HAL_LOCK(huart);
  349. huart->gState = HAL_UART_STATE_BUSY;
  350. /* Disable the Peripheral */
  351. __HAL_UART_DISABLE(huart);
  352. /* Set the wake-up selection scheme */
  353. MODIFY_REG(huart->Instance->CR3, USART_CR3_WUS, WakeUpSelection.WakeUpEvent);
  354. if (WakeUpSelection.WakeUpEvent == UART_WAKEUP_ON_ADDRESS)
  355. {
  356. UARTEx_Wakeup_AddressConfig(huart, WakeUpSelection);
  357. }
  358. /* Enable the Peripheral */
  359. __HAL_UART_ENABLE(huart);
  360. /* Init tickstart for timeout managment*/
  361. tickstart = HAL_GetTick();
  362. /* Wait until REACK flag is set */
  363. if(UART_WaitOnFlagUntilTimeout(huart, USART_ISR_REACK, RESET, tickstart, HAL_UART_TIMEOUT_VALUE) != HAL_OK)
  364. {
  365. status = HAL_TIMEOUT;
  366. }
  367. else
  368. {
  369. /* Initialize the UART State */
  370. huart->gState = HAL_UART_STATE_READY;
  371. }
  372. /* Process Unlocked */
  373. __HAL_UNLOCK(huart);
  374. return status;
  375. }
  376. /**
  377. * @brief Enable UART Stop Mode.
  378. * @note The UART is able to wake up the MCU from Stop 1 mode as long as UART clock is HSI or LSE.
  379. * @param huart UART handle.
  380. * @retval HAL status
  381. */
  382. HAL_StatusTypeDef HAL_UARTEx_EnableStopMode(UART_HandleTypeDef *huart)
  383. {
  384. /* Process Locked */
  385. __HAL_LOCK(huart);
  386. huart->gState = HAL_UART_STATE_BUSY;
  387. /* Set UESM bit */
  388. SET_BIT(huart->Instance->CR1, USART_CR1_UESM);
  389. huart->gState = HAL_UART_STATE_READY;
  390. /* Process Unlocked */
  391. __HAL_UNLOCK(huart);
  392. return HAL_OK;
  393. }
  394. /* MBED */
  395. /**
  396. * @brief Enable UART Clock in Stop Mode
  397. * The UART keeps the Clock ON during Stop mode
  398. * @param huart: uart handle
  399. * @retval HAL status
  400. */
  401. HAL_StatusTypeDef HAL_UARTEx_EnableClockStopMode(UART_HandleTypeDef *huart)
  402. {
  403. /* Process Locked */
  404. __HAL_LOCK(huart);
  405. huart->gState = HAL_UART_STATE_BUSY;
  406. /* Set the USART UESM bit */
  407. huart->Instance->CR3 |= USART_CR3_UCESM;
  408. huart->gState = HAL_UART_STATE_READY;
  409. /* Process Unlocked */
  410. __HAL_UNLOCK(huart);
  411. return HAL_OK;
  412. }
  413. /* MBED */
  414. /**
  415. * @brief Disable UART Stop Mode.
  416. * @param huart UART handle.
  417. * @retval HAL status
  418. */
  419. HAL_StatusTypeDef HAL_UARTEx_DisableStopMode(UART_HandleTypeDef *huart)
  420. {
  421. /* Process Locked */
  422. __HAL_LOCK(huart);
  423. huart->gState = HAL_UART_STATE_BUSY;
  424. /* Clear UESM bit */
  425. CLEAR_BIT(huart->Instance->CR1, USART_CR1_UESM);
  426. huart->gState = HAL_UART_STATE_READY;
  427. /* Process Unlocked */
  428. __HAL_UNLOCK(huart);
  429. return HAL_OK;
  430. }
  431. /* MBED */
  432. /**
  433. * @brief Disable UART Clock in Stop Mode
  434. * @param huart: uart handle
  435. * @retval HAL status
  436. */
  437. HAL_StatusTypeDef HAL_UARTEx_DisableClockStopMode(UART_HandleTypeDef *huart)
  438. {
  439. /* Process Locked */
  440. __HAL_LOCK(huart);
  441. huart->gState = HAL_UART_STATE_BUSY;
  442. /* Clear USART UESM bit */
  443. huart->Instance->CR3 &= ~(USART_CR3_UCESM);
  444. huart->gState = HAL_UART_STATE_READY;
  445. /* Process Unlocked */
  446. __HAL_UNLOCK(huart);
  447. return HAL_OK;
  448. }
  449. /* MBED */
  450. #if defined(USART_CR2_SLVEN)
  451. /**
  452. * @brief Enable the SPI slave mode.
  453. * @note When the UART operates in SPI slave mode, it handles data flow using
  454. * the serial interface clock derived from the external SCLK signal
  455. * provided by the external master SPI device.
  456. * @note In SPI slave mode, the UART must be enabled before starting the master
  457. * communications (or between frames while the clock is stable). Otherwise,
  458. * if the UART slave is enabled while the master is in the middle of a
  459. * frame, it will become desynchronized with the master.
  460. * @note The data register of the slave needs to be ready before the first edge
  461. * of the communication clock or before the end of the ongoing communication,
  462. * otherwise the SPI slave will transmit zeros.
  463. * @param huart UART handle.
  464. * @retval HAL status
  465. */
  466. HAL_StatusTypeDef HAL_UARTEx_EnableSlaveMode(UART_HandleTypeDef *huart)
  467. {
  468. uint32_t tmpcr1 = 0;
  469. /* Check parameters */
  470. assert_param(IS_UART_SPI_SLAVE_INSTANCE(huart->Instance));
  471. /* Process Locked */
  472. __HAL_LOCK(huart);
  473. huart->gState = HAL_UART_STATE_BUSY;
  474. /* Save actual UART configuration */
  475. tmpcr1 = READ_REG(huart->Instance->CR1);
  476. /* Disable UART */
  477. __HAL_UART_DISABLE(huart);
  478. /* In SPI slave mode mode, the following bits must be kept cleared:
  479. - LINEN and CLKEN bit in the USART_CR2 register
  480. - HDSEL, SCEN and IREN bits in the USART_CR3 register.*/
  481. CLEAR_BIT(huart->Instance->CR2, (USART_CR2_LINEN | USART_CR2_CLKEN));
  482. CLEAR_BIT(huart->Instance->CR3, (USART_CR3_SCEN | USART_CR3_HDSEL | USART_CR3_IREN));
  483. /* Enable SPI slave mode */
  484. SET_BIT(huart->Instance->CR2, USART_CR2_SLVEN);
  485. /* Restore UART configuration */
  486. WRITE_REG(huart->Instance->CR1, tmpcr1);
  487. huart->SlaveMode = UART_SLAVEMODE_ENABLE;
  488. huart->gState = HAL_UART_STATE_READY;
  489. /* Enable UART */
  490. __HAL_UART_ENABLE(huart);
  491. /* Process Unlocked */
  492. __HAL_UNLOCK(huart);
  493. return HAL_OK;
  494. }
  495. /**
  496. * @brief Disable the SPI slave mode.
  497. * @param huart UART handle.
  498. * @retval HAL status
  499. */
  500. HAL_StatusTypeDef HAL_UARTEx_DisableSlaveMode(UART_HandleTypeDef *huart)
  501. {
  502. uint32_t tmpcr1 = 0;
  503. /* Check parameters */
  504. assert_param(IS_UART_SPI_SLAVE_INSTANCE(huart->Instance));
  505. /* Process Locked */
  506. __HAL_LOCK(huart);
  507. huart->gState = HAL_UART_STATE_BUSY;
  508. /* Save actual UART configuration */
  509. tmpcr1 = READ_REG(huart->Instance->CR1);
  510. /* Disable UART */
  511. __HAL_UART_DISABLE(huart);
  512. /* Disable SPI slave mode */
  513. CLEAR_BIT(huart->Instance->CR2, USART_CR2_SLVEN);
  514. /* Restore UART configuration */
  515. WRITE_REG(huart->Instance->CR1, tmpcr1);
  516. huart->SlaveMode = UART_SLAVEMODE_ENABLE;
  517. huart->gState = HAL_UART_STATE_READY;
  518. /* Process Unlocked */
  519. __HAL_UNLOCK(huart);
  520. return HAL_OK;
  521. }
  522. /**
  523. * @brief Configure the Slave Select input pin (NSS).
  524. * @note Software NSS management: SPI slave will always be selected and NSS
  525. * input pin will be ignored.
  526. * @note Hardware NSS management: the SPI slave selection depends on NSS
  527. * input pin. The slave is selected when NSS is low and deselected when
  528. * NSS is high.
  529. * @param huart UART handle.
  530. * @param NSSConfig NSS configuration.
  531. * This parameter can be one of the following values:
  532. * @arg @ref UART_NSS_HARD
  533. * @arg @ref UART_NSS_SOFT
  534. * @retval HAL status
  535. */
  536. HAL_StatusTypeDef HAL_UARTEx_ConfigNSS(UART_HandleTypeDef *huart, uint32_t NSSConfig)
  537. {
  538. uint32_t tmpcr1 = 0;
  539. /* Check parameters */
  540. assert_param(IS_UART_SPI_SLAVE_INSTANCE(huart->Instance));
  541. assert_param(IS_UART_NSS(NSSConfig));
  542. /* Process Locked */
  543. __HAL_LOCK(huart);
  544. huart->gState = HAL_UART_STATE_BUSY;
  545. /* Save actual UART configuration */
  546. tmpcr1 = READ_REG(huart->Instance->CR1);
  547. /* Disable UART */
  548. __HAL_UART_DISABLE(huart);
  549. /* Program DIS_NSS bit in the USART_CR2 register */
  550. MODIFY_REG(huart->Instance->CR2, USART_CR2_DIS_NSS, NSSConfig);
  551. /* Restore UART configuration */
  552. WRITE_REG(huart->Instance->CR1, tmpcr1);
  553. huart->gState = HAL_UART_STATE_READY;
  554. /* Process Unlocked */
  555. __HAL_UNLOCK(huart);
  556. return HAL_OK;
  557. }
  558. #endif
  559. #if defined(USART_CR1_FIFOEN)
  560. /**
  561. * @brief Enable the FIFO mode.
  562. * @param huart UART handle.
  563. * @retval HAL status
  564. */
  565. HAL_StatusTypeDef HAL_UARTEx_EnableFifoMode(UART_HandleTypeDef *huart)
  566. {
  567. uint32_t tmpcr1 = 0;
  568. /* Check parameters */
  569. assert_param(IS_UART_FIFO_INSTANCE(huart->Instance));
  570. /* Process Locked */
  571. __HAL_LOCK(huart);
  572. huart->gState = HAL_UART_STATE_BUSY;
  573. /* Save actual UART configuration */
  574. tmpcr1 = READ_REG(huart->Instance->CR1);
  575. /* Disable UART */
  576. __HAL_UART_DISABLE(huart);
  577. /* Enable FIFO mode */
  578. SET_BIT(tmpcr1, USART_CR1_FIFOEN);
  579. huart->FifoMode = UART_FIFOMODE_ENABLE;
  580. /* Restore UART configuration */
  581. WRITE_REG(huart->Instance->CR1, tmpcr1);
  582. /* Determine the number of data to process during RX/TX ISR execution */
  583. UARTEx_SetNbDataToProcess(huart);
  584. huart->gState = HAL_UART_STATE_READY;
  585. /* Process Unlocked */
  586. __HAL_UNLOCK(huart);
  587. return HAL_OK;
  588. }
  589. /**
  590. * @brief Disable the FIFO mode.
  591. * @param huart UART handle.
  592. * @retval HAL status
  593. */
  594. HAL_StatusTypeDef HAL_UARTEx_DisableFifoMode(UART_HandleTypeDef *huart)
  595. {
  596. uint32_t tmpcr1 = 0;
  597. /* Check parameters */
  598. assert_param(IS_UART_FIFO_INSTANCE(huart->Instance));
  599. /* Process Locked */
  600. __HAL_LOCK(huart);
  601. huart->gState = HAL_UART_STATE_BUSY;
  602. /* Save actual UART configuration */
  603. tmpcr1 = READ_REG(huart->Instance->CR1);
  604. /* Disable UART */
  605. __HAL_UART_DISABLE(huart);
  606. /* Enable FIFO mode */
  607. CLEAR_BIT(tmpcr1, USART_CR1_FIFOEN);
  608. huart->FifoMode = UART_FIFOMODE_DISABLE;
  609. /* Restore UART configuration */
  610. WRITE_REG(huart->Instance->CR1, tmpcr1);
  611. huart->gState = HAL_UART_STATE_READY;
  612. /* Process Unlocked */
  613. __HAL_UNLOCK(huart);
  614. return HAL_OK;
  615. }
  616. /**
  617. * @brief Set the TXFIFO threshold.
  618. * @param huart UART handle.
  619. * @param Threshold TX FIFO threshold value
  620. * This parameter can be one of the following values:
  621. * @arg @ref UART_TXFIFO_THRESHOLD_1_8
  622. * @arg @ref UART_TXFIFO_THRESHOLD_1_4
  623. * @arg @ref UART_TXFIFO_THRESHOLD_1_2
  624. * @arg @ref UART_TXFIFO_THRESHOLD_3_4
  625. * @arg @ref UART_TXFIFO_THRESHOLD_7_8
  626. * @arg @ref UART_TXFIFO_THRESHOLD_8_8
  627. * @retval HAL status
  628. */
  629. HAL_StatusTypeDef HAL_UARTEx_SetTxFifoThreshold(UART_HandleTypeDef *huart, uint32_t Threshold)
  630. {
  631. uint32_t tmpcr1 = 0;
  632. /* Check parameters */
  633. assert_param(IS_UART_FIFO_INSTANCE(huart->Instance));
  634. assert_param(IS_UART_TXFIFO_THRESHOLD(Threshold));
  635. /* Process Locked */
  636. __HAL_LOCK(huart);
  637. huart->gState = HAL_UART_STATE_BUSY;
  638. /* Save actual UART configuration */
  639. tmpcr1 = READ_REG(huart->Instance->CR1);
  640. /* Disable UART */
  641. __HAL_UART_DISABLE(huart);
  642. /* Update TX threshold configuration */
  643. MODIFY_REG(huart->Instance->CR3, USART_CR3_TXFTCFG, Threshold);
  644. /* Determine the number of data to process during RX/TX ISR execution */
  645. UARTEx_SetNbDataToProcess(huart);
  646. /* Restore UART configuration */
  647. WRITE_REG(huart->Instance->CR1, tmpcr1);
  648. huart->gState = HAL_UART_STATE_READY;
  649. /* Process Unlocked */
  650. __HAL_UNLOCK(huart);
  651. return HAL_OK;
  652. }
  653. /**
  654. * @brief Set the RXFIFO threshold.
  655. * @param huart UART handle.
  656. * @param Threshold RX FIFO threshold value
  657. * This parameter can be one of the following values:
  658. * @arg @ref UART_RXFIFO_THRESHOLD_1_8
  659. * @arg @ref UART_RXFIFO_THRESHOLD_1_4
  660. * @arg @ref UART_RXFIFO_THRESHOLD_1_2
  661. * @arg @ref UART_RXFIFO_THRESHOLD_3_4
  662. * @arg @ref UART_RXFIFO_THRESHOLD_7_8
  663. * @arg @ref UART_RXFIFO_THRESHOLD_8_8
  664. * @retval HAL status
  665. */
  666. HAL_StatusTypeDef HAL_UARTEx_SetRxFifoThreshold(UART_HandleTypeDef *huart, uint32_t Threshold)
  667. {
  668. uint32_t tmpcr1 = 0;
  669. /* Check the parameters */
  670. assert_param(IS_UART_FIFO_INSTANCE(huart->Instance));
  671. assert_param(IS_UART_RXFIFO_THRESHOLD(Threshold));
  672. /* Process Locked */
  673. __HAL_LOCK(huart);
  674. huart->gState = HAL_UART_STATE_BUSY;
  675. /* Save actual UART configuration */
  676. tmpcr1 = READ_REG(huart->Instance->CR1);
  677. /* Disable UART */
  678. __HAL_UART_DISABLE(huart);
  679. /* Update RX threshold configuration */
  680. MODIFY_REG(huart->Instance->CR3, USART_CR3_RXFTCFG, Threshold);
  681. /* Determine the number of data to process during RX/TX ISR execution */
  682. UARTEx_SetNbDataToProcess(huart);
  683. /* Restore UART configuration */
  684. WRITE_REG(huart->Instance->CR1, tmpcr1);
  685. huart->gState = HAL_UART_STATE_READY;
  686. /* Process Unlocked */
  687. __HAL_UNLOCK(huart);
  688. return HAL_OK;
  689. }
  690. #endif
  691. /**
  692. * @}
  693. */
  694. /**
  695. * @}
  696. */
  697. /** @addtogroup UARTEx_Private_Functions
  698. * @{
  699. */
  700. /**
  701. * @brief Initialize the UART wake-up from stop mode parameters when triggered by address detection.
  702. * @param huart UART handle.
  703. * @param WakeUpSelection UART wake up from stop mode parameters.
  704. * @retval None
  705. */
  706. static void UARTEx_Wakeup_AddressConfig(UART_HandleTypeDef *huart, UART_WakeUpTypeDef WakeUpSelection)
  707. {
  708. assert_param(IS_UART_ADDRESSLENGTH_DETECT(WakeUpSelection.AddressLength));
  709. /* Set the USART address length */
  710. MODIFY_REG(huart->Instance->CR2, USART_CR2_ADDM7, WakeUpSelection.AddressLength);
  711. /* Set the USART address node */
  712. MODIFY_REG(huart->Instance->CR2, USART_CR2_ADD, ((uint32_t)WakeUpSelection.Address << UART_CR2_ADDRESS_LSB_POS));
  713. }
  714. #if defined(USART_CR1_FIFOEN)
  715. /**
  716. * @brief Calculate the number of data to process in RX/TX ISR.
  717. * @note The RX FIFO depth and the TX FIFO depth is extracted from
  718. * the UART configuration registers.
  719. * @param huart UART handle.
  720. * @retval None
  721. */
  722. void UARTEx_SetNbDataToProcess(UART_HandleTypeDef *huart)
  723. {
  724. uint8_t rx_fifo_depth;
  725. uint8_t tx_fifo_depth;
  726. uint8_t rx_fifo_threshold;
  727. uint8_t tx_fifo_threshold;
  728. uint8_t numerator[] = {1, 1, 1, 3, 7, 1};
  729. uint8_t denominator[] = {8, 4, 2, 4, 8, 1};
  730. if (huart->FifoMode == UART_FIFOMODE_DISABLE)
  731. {
  732. huart->NbTxDataToProcess = 1;
  733. huart->NbRxDataToProcess = 1;
  734. }
  735. else
  736. {
  737. rx_fifo_depth = 8; /* RX Fifo size */
  738. tx_fifo_depth = 8; /* TX Fifo size */
  739. rx_fifo_threshold = (uint8_t)(READ_BIT(huart->Instance->CR3, USART_CR3_RXFTCFG) >> USART_CR3_RXFTCFG_Pos);
  740. tx_fifo_threshold = (uint8_t)(READ_BIT(huart->Instance->CR3, USART_CR3_TXFTCFG) >> USART_CR3_TXFTCFG_Pos);
  741. huart->NbTxDataToProcess = (uint8_t)(tx_fifo_depth * numerator[tx_fifo_threshold])/denominator[tx_fifo_threshold];
  742. huart->NbRxDataToProcess = (uint8_t)(rx_fifo_depth * numerator[rx_fifo_threshold])/denominator[rx_fifo_threshold];
  743. }
  744. }
  745. #endif
  746. /**
  747. * @}
  748. */
  749. #endif /* HAL_UART_MODULE_ENABLED */
  750. /**
  751. * @}
  752. */
  753. /**
  754. * @}
  755. */
  756. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/