stm32l4xx_hal_usart_ex.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  1. /**
  2. ******************************************************************************
  3. * @file stm32l4xx_hal_usart_ex.c
  4. * @author MCD Application Team
  5. * @brief Extended USART HAL module driver.
  6. * This file provides firmware functions to manage the following extended
  7. * functionalities of the Universal Synchronous Receiver Transmitter Peripheral (USART).
  8. * + Peripheral Control functions
  9. *
  10. *
  11. ******************************************************************************
  12. * @attention
  13. *
  14. * Copyright (c) 2017 STMicroelectronics.
  15. * All rights reserved.
  16. *
  17. * This software is licensed under terms that can be found in the LICENSE file
  18. * in the root directory of this software component.
  19. * If no LICENSE file comes with this software, it is provided AS-IS.
  20. *
  21. ******************************************************************************
  22. @verbatim
  23. ==============================================================================
  24. ##### USART peripheral extended features #####
  25. ==============================================================================
  26. (#) FIFO mode enabling/disabling and RX/TX FIFO threshold programming.
  27. -@- When USART operates in FIFO mode, FIFO mode must be enabled prior
  28. starting RX/TX transfers. Also RX/TX FIFO thresholds must be
  29. configured prior starting RX/TX transfers.
  30. (#) Slave mode enabling/disabling and NSS pin configuration.
  31. -@- When USART operates in Slave mode, Slave mode must be enabled prior
  32. starting RX/TX transfers.
  33. @endverbatim
  34. ******************************************************************************
  35. */
  36. /* Includes ------------------------------------------------------------------*/
  37. #include "stm32l4xx_hal.h"
  38. /** @addtogroup STM32L4xx_HAL_Driver
  39. * @{
  40. */
  41. /** @defgroup USARTEx USARTEx
  42. * @brief USART Extended HAL module driver
  43. * @{
  44. */
  45. #ifdef HAL_USART_MODULE_ENABLED
  46. /* Private typedef -----------------------------------------------------------*/
  47. #if defined(USART_CR1_FIFOEN)
  48. /** @defgroup USARTEx_Private_Constants USARTEx Private Constants
  49. * @{
  50. */
  51. /* USART RX FIFO depth */
  52. #define RX_FIFO_DEPTH 8U
  53. /* USART TX FIFO depth */
  54. #define TX_FIFO_DEPTH 8U
  55. /**
  56. * @}
  57. */
  58. #endif /* USART_CR1_FIFOEN */
  59. /* Private define ------------------------------------------------------------*/
  60. /* Private macros ------------------------------------------------------------*/
  61. /* Private variables ---------------------------------------------------------*/
  62. /* Private function prototypes -----------------------------------------------*/
  63. #if defined(USART_CR1_FIFOEN)
  64. /** @defgroup USARTEx_Private_Functions USARTEx Private Functions
  65. * @{
  66. */
  67. static void USARTEx_SetNbDataToProcess(USART_HandleTypeDef *husart);
  68. /**
  69. * @}
  70. */
  71. #endif /* USART_CR1_FIFOEN */
  72. /* Exported functions --------------------------------------------------------*/
  73. /** @defgroup USARTEx_Exported_Functions USARTEx Exported Functions
  74. * @{
  75. */
  76. /** @defgroup USARTEx_Exported_Functions_Group1 IO operation functions
  77. * @brief Extended USART Transmit/Receive functions
  78. *
  79. @verbatim
  80. ===============================================================================
  81. ##### IO operation functions #####
  82. ===============================================================================
  83. This subsection provides a set of FIFO mode related callback functions.
  84. (#) TX/RX Fifos Callbacks:
  85. (+) HAL_USARTEx_RxFifoFullCallback()
  86. (+) HAL_USARTEx_TxFifoEmptyCallback()
  87. @endverbatim
  88. * @{
  89. */
  90. #if defined(USART_CR1_FIFOEN)
  91. /**
  92. * @brief USART RX Fifo full callback.
  93. * @param husart USART handle.
  94. * @retval None
  95. */
  96. __weak void HAL_USARTEx_RxFifoFullCallback(USART_HandleTypeDef *husart)
  97. {
  98. /* Prevent unused argument(s) compilation warning */
  99. UNUSED(husart);
  100. /* NOTE : This function should not be modified, when the callback is needed,
  101. the HAL_USARTEx_RxFifoFullCallback can be implemented in the user file.
  102. */
  103. }
  104. /**
  105. * @brief USART TX Fifo empty callback.
  106. * @param husart USART handle.
  107. * @retval None
  108. */
  109. __weak void HAL_USARTEx_TxFifoEmptyCallback(USART_HandleTypeDef *husart)
  110. {
  111. /* Prevent unused argument(s) compilation warning */
  112. UNUSED(husart);
  113. /* NOTE : This function should not be modified, when the callback is needed,
  114. the HAL_USARTEx_TxFifoEmptyCallback can be implemented in the user file.
  115. */
  116. }
  117. #endif /* USART_CR1_FIFOEN */
  118. /**
  119. * @}
  120. */
  121. /** @defgroup USARTEx_Exported_Functions_Group2 Peripheral Control functions
  122. * @brief Extended Peripheral Control functions
  123. *
  124. @verbatim
  125. ===============================================================================
  126. ##### Peripheral Control functions #####
  127. ===============================================================================
  128. [..] This section provides the following functions:
  129. (+) HAL_USARTEx_EnableSPISlaveMode() API enables the SPI slave mode
  130. (+) HAL_USARTEx_DisableSPISlaveMode() API disables the SPI slave mode
  131. (+) HAL_USARTEx_ConfigNSS API configures the Slave Select input pin (NSS)
  132. (+) HAL_USARTEx_EnableFifoMode() API enables the FIFO mode
  133. (+) HAL_USARTEx_DisableFifoMode() API disables the FIFO mode
  134. (+) HAL_USARTEx_SetTxFifoThreshold() API sets the TX FIFO threshold
  135. (+) HAL_USARTEx_SetRxFifoThreshold() API sets the RX FIFO threshold
  136. @endverbatim
  137. * @{
  138. */
  139. #if defined(USART_CR2_SLVEN)
  140. /**
  141. * @brief Enable the SPI slave mode.
  142. * @note When the USART operates in SPI slave mode, it handles data flow using
  143. * the serial interface clock derived from the external SCLK signal
  144. * provided by the external master SPI device.
  145. * @note In SPI slave mode, the USART must be enabled before starting the master
  146. * communications (or between frames while the clock is stable). Otherwise,
  147. * if the USART slave is enabled while the master is in the middle of a
  148. * frame, it will become desynchronized with the master.
  149. * @note The data register of the slave needs to be ready before the first edge
  150. * of the communication clock or before the end of the ongoing communication,
  151. * otherwise the SPI slave will transmit zeros.
  152. * @param husart USART handle.
  153. * @retval HAL status
  154. */
  155. HAL_StatusTypeDef HAL_USARTEx_EnableSlaveMode(USART_HandleTypeDef *husart)
  156. {
  157. uint32_t tmpcr1;
  158. /* Check parameters */
  159. assert_param(IS_UART_SPI_SLAVE_INSTANCE(husart->Instance));
  160. /* Process Locked */
  161. __HAL_LOCK(husart);
  162. husart->State = HAL_USART_STATE_BUSY;
  163. /* Save actual USART configuration */
  164. tmpcr1 = READ_REG(husart->Instance->CR1);
  165. /* Disable USART */
  166. __HAL_USART_DISABLE(husart);
  167. /* In SPI slave mode mode, the following bits must be kept cleared:
  168. - LINEN and CLKEN bit in the USART_CR2 register
  169. - HDSEL, SCEN and IREN bits in the USART_CR3 register.*/
  170. CLEAR_BIT(husart->Instance->CR2, (USART_CR2_LINEN | USART_CR2_CLKEN));
  171. CLEAR_BIT(husart->Instance->CR3, (USART_CR3_SCEN | USART_CR3_HDSEL | USART_CR3_IREN));
  172. /* Enable SPI slave mode */
  173. SET_BIT(husart->Instance->CR2, USART_CR2_SLVEN);
  174. /* Restore USART configuration */
  175. WRITE_REG(husart->Instance->CR1, tmpcr1);
  176. husart->SlaveMode = USART_SLAVEMODE_ENABLE;
  177. husart->State = HAL_USART_STATE_READY;
  178. /* Enable USART */
  179. __HAL_USART_ENABLE(husart);
  180. /* Process Unlocked */
  181. __HAL_UNLOCK(husart);
  182. return HAL_OK;
  183. }
  184. /**
  185. * @brief Disable the SPI slave mode.
  186. * @param husart USART handle.
  187. * @retval HAL status
  188. */
  189. HAL_StatusTypeDef HAL_USARTEx_DisableSlaveMode(USART_HandleTypeDef *husart)
  190. {
  191. uint32_t tmpcr1;
  192. /* Check parameters */
  193. assert_param(IS_UART_SPI_SLAVE_INSTANCE(husart->Instance));
  194. /* Process Locked */
  195. __HAL_LOCK(husart);
  196. husart->State = HAL_USART_STATE_BUSY;
  197. /* Save actual USART configuration */
  198. tmpcr1 = READ_REG(husart->Instance->CR1);
  199. /* Disable USART */
  200. __HAL_USART_DISABLE(husart);
  201. /* Disable SPI slave mode */
  202. CLEAR_BIT(husart->Instance->CR2, USART_CR2_SLVEN);
  203. /* Restore USART configuration */
  204. WRITE_REG(husart->Instance->CR1, tmpcr1);
  205. husart->SlaveMode = USART_SLAVEMODE_DISABLE;
  206. husart->State = HAL_USART_STATE_READY;
  207. /* Process Unlocked */
  208. __HAL_UNLOCK(husart);
  209. return HAL_OK;
  210. }
  211. /**
  212. * @brief Configure the Slave Select input pin (NSS).
  213. * @note Software NSS management: SPI slave will always be selected and NSS
  214. * input pin will be ignored.
  215. * @note Hardware NSS management: the SPI slave selection depends on NSS
  216. * input pin. The slave is selected when NSS is low and deselected when
  217. * NSS is high.
  218. * @param husart USART handle.
  219. * @param NSSConfig NSS configuration.
  220. * This parameter can be one of the following values:
  221. * @arg @ref USART_NSS_HARD
  222. * @arg @ref USART_NSS_SOFT
  223. * @retval HAL status
  224. */
  225. HAL_StatusTypeDef HAL_USARTEx_ConfigNSS(USART_HandleTypeDef *husart, uint32_t NSSConfig)
  226. {
  227. uint32_t tmpcr1;
  228. /* Check parameters */
  229. assert_param(IS_UART_SPI_SLAVE_INSTANCE(husart->Instance));
  230. assert_param(IS_USART_NSS(NSSConfig));
  231. /* Process Locked */
  232. __HAL_LOCK(husart);
  233. husart->State = HAL_USART_STATE_BUSY;
  234. /* Save actual USART configuration */
  235. tmpcr1 = READ_REG(husart->Instance->CR1);
  236. /* Disable USART */
  237. __HAL_USART_DISABLE(husart);
  238. /* Program DIS_NSS bit in the USART_CR2 register */
  239. MODIFY_REG(husart->Instance->CR2, USART_CR2_DIS_NSS, NSSConfig);
  240. /* Restore USART configuration */
  241. WRITE_REG(husart->Instance->CR1, tmpcr1);
  242. husart->State = HAL_USART_STATE_READY;
  243. /* Process Unlocked */
  244. __HAL_UNLOCK(husart);
  245. return HAL_OK;
  246. }
  247. #endif /* USART_CR2_SLVEN */
  248. #if defined(USART_CR1_FIFOEN)
  249. /**
  250. * @brief Enable the FIFO mode.
  251. * @param husart USART handle.
  252. * @retval HAL status
  253. */
  254. HAL_StatusTypeDef HAL_USARTEx_EnableFifoMode(USART_HandleTypeDef *husart)
  255. {
  256. uint32_t tmpcr1;
  257. /* Check parameters */
  258. assert_param(IS_UART_FIFO_INSTANCE(husart->Instance));
  259. /* Process Locked */
  260. __HAL_LOCK(husart);
  261. husart->State = HAL_USART_STATE_BUSY;
  262. /* Save actual USART configuration */
  263. tmpcr1 = READ_REG(husart->Instance->CR1);
  264. /* Disable USART */
  265. __HAL_USART_DISABLE(husart);
  266. /* Enable FIFO mode */
  267. SET_BIT(tmpcr1, USART_CR1_FIFOEN);
  268. husart->FifoMode = USART_FIFOMODE_ENABLE;
  269. /* Restore USART configuration */
  270. WRITE_REG(husart->Instance->CR1, tmpcr1);
  271. /* Determine the number of data to process during RX/TX ISR execution */
  272. USARTEx_SetNbDataToProcess(husart);
  273. husart->State = HAL_USART_STATE_READY;
  274. /* Process Unlocked */
  275. __HAL_UNLOCK(husart);
  276. return HAL_OK;
  277. }
  278. /**
  279. * @brief Disable the FIFO mode.
  280. * @param husart USART handle.
  281. * @retval HAL status
  282. */
  283. HAL_StatusTypeDef HAL_USARTEx_DisableFifoMode(USART_HandleTypeDef *husart)
  284. {
  285. uint32_t tmpcr1;
  286. /* Check parameters */
  287. assert_param(IS_UART_FIFO_INSTANCE(husart->Instance));
  288. /* Process Locked */
  289. __HAL_LOCK(husart);
  290. husart->State = HAL_USART_STATE_BUSY;
  291. /* Save actual USART configuration */
  292. tmpcr1 = READ_REG(husart->Instance->CR1);
  293. /* Disable USART */
  294. __HAL_USART_DISABLE(husart);
  295. /* Enable FIFO mode */
  296. CLEAR_BIT(tmpcr1, USART_CR1_FIFOEN);
  297. husart->FifoMode = USART_FIFOMODE_DISABLE;
  298. /* Restore USART configuration */
  299. WRITE_REG(husart->Instance->CR1, tmpcr1);
  300. husart->State = HAL_USART_STATE_READY;
  301. /* Process Unlocked */
  302. __HAL_UNLOCK(husart);
  303. return HAL_OK;
  304. }
  305. /**
  306. * @brief Set the TXFIFO threshold.
  307. * @param husart USART handle.
  308. * @param Threshold TX FIFO threshold value
  309. * This parameter can be one of the following values:
  310. * @arg @ref USART_TXFIFO_THRESHOLD_1_8
  311. * @arg @ref USART_TXFIFO_THRESHOLD_1_4
  312. * @arg @ref USART_TXFIFO_THRESHOLD_1_2
  313. * @arg @ref USART_TXFIFO_THRESHOLD_3_4
  314. * @arg @ref USART_TXFIFO_THRESHOLD_7_8
  315. * @arg @ref USART_TXFIFO_THRESHOLD_8_8
  316. * @retval HAL status
  317. */
  318. HAL_StatusTypeDef HAL_USARTEx_SetTxFifoThreshold(USART_HandleTypeDef *husart, uint32_t Threshold)
  319. {
  320. uint32_t tmpcr1;
  321. /* Check parameters */
  322. assert_param(IS_UART_FIFO_INSTANCE(husart->Instance));
  323. assert_param(IS_USART_TXFIFO_THRESHOLD(Threshold));
  324. /* Process Locked */
  325. __HAL_LOCK(husart);
  326. husart->State = HAL_USART_STATE_BUSY;
  327. /* Save actual USART configuration */
  328. tmpcr1 = READ_REG(husart->Instance->CR1);
  329. /* Disable USART */
  330. __HAL_USART_DISABLE(husart);
  331. /* Update TX threshold configuration */
  332. MODIFY_REG(husart->Instance->CR3, USART_CR3_TXFTCFG, Threshold);
  333. /* Determine the number of data to process during RX/TX ISR execution */
  334. USARTEx_SetNbDataToProcess(husart);
  335. /* Restore USART configuration */
  336. WRITE_REG(husart->Instance->CR1, tmpcr1);
  337. husart->State = HAL_USART_STATE_READY;
  338. /* Process Unlocked */
  339. __HAL_UNLOCK(husart);
  340. return HAL_OK;
  341. }
  342. /**
  343. * @brief Set the RXFIFO threshold.
  344. * @param husart USART handle.
  345. * @param Threshold RX FIFO threshold value
  346. * This parameter can be one of the following values:
  347. * @arg @ref USART_RXFIFO_THRESHOLD_1_8
  348. * @arg @ref USART_RXFIFO_THRESHOLD_1_4
  349. * @arg @ref USART_RXFIFO_THRESHOLD_1_2
  350. * @arg @ref USART_RXFIFO_THRESHOLD_3_4
  351. * @arg @ref USART_RXFIFO_THRESHOLD_7_8
  352. * @arg @ref USART_RXFIFO_THRESHOLD_8_8
  353. * @retval HAL status
  354. */
  355. HAL_StatusTypeDef HAL_USARTEx_SetRxFifoThreshold(USART_HandleTypeDef *husart, uint32_t Threshold)
  356. {
  357. uint32_t tmpcr1;
  358. /* Check the parameters */
  359. assert_param(IS_UART_FIFO_INSTANCE(husart->Instance));
  360. assert_param(IS_USART_RXFIFO_THRESHOLD(Threshold));
  361. /* Process Locked */
  362. __HAL_LOCK(husart);
  363. husart->State = HAL_USART_STATE_BUSY;
  364. /* Save actual USART configuration */
  365. tmpcr1 = READ_REG(husart->Instance->CR1);
  366. /* Disable USART */
  367. __HAL_USART_DISABLE(husart);
  368. /* Update RX threshold configuration */
  369. MODIFY_REG(husart->Instance->CR3, USART_CR3_RXFTCFG, Threshold);
  370. /* Determine the number of data to process during RX/TX ISR execution */
  371. USARTEx_SetNbDataToProcess(husart);
  372. /* Restore USART configuration */
  373. WRITE_REG(husart->Instance->CR1, tmpcr1);
  374. husart->State = HAL_USART_STATE_READY;
  375. /* Process Unlocked */
  376. __HAL_UNLOCK(husart);
  377. return HAL_OK;
  378. }
  379. #endif /* USART_CR1_FIFOEN */
  380. /**
  381. * @}
  382. */
  383. /**
  384. * @}
  385. */
  386. /** @addtogroup USARTEx_Private_Functions
  387. * @{
  388. */
  389. #if defined(USART_CR1_FIFOEN)
  390. /**
  391. * @brief Calculate the number of data to process in RX/TX ISR.
  392. * @note The RX FIFO depth and the TX FIFO depth is extracted from
  393. * the USART configuration registers.
  394. * @param husart USART handle.
  395. * @retval None
  396. */
  397. static void USARTEx_SetNbDataToProcess(USART_HandleTypeDef *husart)
  398. {
  399. uint8_t rx_fifo_depth;
  400. uint8_t tx_fifo_depth;
  401. uint8_t rx_fifo_threshold;
  402. uint8_t tx_fifo_threshold;
  403. /* 2 0U/1U added for MISRAC2012-Rule-18.1_b and MISRAC2012-Rule-18.1_d */
  404. static const uint8_t numerator[] = {1U, 1U, 1U, 3U, 7U, 1U, 0U, 0U};
  405. static const uint8_t denominator[] = {8U, 4U, 2U, 4U, 8U, 1U, 1U, 1U};
  406. if (husart->FifoMode == USART_FIFOMODE_DISABLE)
  407. {
  408. husart->NbTxDataToProcess = 1U;
  409. husart->NbRxDataToProcess = 1U;
  410. }
  411. else
  412. {
  413. rx_fifo_depth = RX_FIFO_DEPTH;
  414. tx_fifo_depth = TX_FIFO_DEPTH;
  415. rx_fifo_threshold = (uint8_t)((READ_BIT(husart->Instance->CR3,
  416. USART_CR3_RXFTCFG) >> USART_CR3_RXFTCFG_Pos) & 0xFFU);
  417. tx_fifo_threshold = (uint8_t)((READ_BIT(husart->Instance->CR3,
  418. USART_CR3_TXFTCFG) >> USART_CR3_TXFTCFG_Pos) & 0xFFU);
  419. husart->NbTxDataToProcess = ((uint16_t)tx_fifo_depth * numerator[tx_fifo_threshold]) /
  420. (uint16_t)denominator[tx_fifo_threshold];
  421. husart->NbRxDataToProcess = ((uint16_t)rx_fifo_depth * numerator[rx_fifo_threshold]) /
  422. (uint16_t)denominator[rx_fifo_threshold];
  423. }
  424. }
  425. #endif /* USART_CR1_FIFOEN */
  426. /**
  427. * @}
  428. */
  429. #endif /* HAL_USART_MODULE_ENABLED */
  430. /**
  431. * @}
  432. */
  433. /**
  434. * @}
  435. */