stm32l4xx_hal_usart_ex.c 17 KB

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