stm32f4xx_usart.c 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478
  1. /**
  2. ******************************************************************************
  3. * @file stm32f4xx_usart.c
  4. * @author MCD Application Team
  5. * @version V1.8.1
  6. * @date 27-January-2022
  7. * @brief This file provides firmware functions to manage the following
  8. * functionalities of the Universal synchronous asynchronous receiver
  9. * transmitter (USART):
  10. * + Initialization and Configuration
  11. * + Data transfers
  12. * + Multi-Processor Communication
  13. * + LIN mode
  14. * + Half-duplex mode
  15. * + Smartcard mode
  16. * + IrDA mode
  17. * + DMA transfers management
  18. * + Interrupts and flags management
  19. *
  20. @verbatim
  21. ===============================================================================
  22. ##### How to use this driver #####
  23. ===============================================================================
  24. [..]
  25. (#) Enable peripheral clock using the following functions
  26. RCC_APB2PeriphClockCmd(RCC_APB2Periph_USARTx, ENABLE) for USART1 and USART6
  27. RCC_APB1PeriphClockCmd(RCC_APB1Periph_USARTx, ENABLE) for USART2, USART3,
  28. UART4 or UART5.
  29. (#) According to the USART mode, enable the GPIO clocks using
  30. RCC_AHB1PeriphClockCmd() function. (The I/O can be TX, RX, CTS,
  31. or/and SCLK).
  32. (#) Peripheral's alternate function:
  33. (++) Connect the pin to the desired peripherals' Alternate
  34. Function (AF) using GPIO_PinAFConfig() function
  35. (++) Configure the desired pin in alternate function by:
  36. GPIO_InitStruct->GPIO_Mode = GPIO_Mode_AF
  37. (++) Select the type, pull-up/pull-down and output speed via
  38. GPIO_PuPd, GPIO_OType and GPIO_Speed members
  39. (++) Call GPIO_Init() function
  40. (#) Program the Baud Rate, Word Length , Stop Bit, Parity, Hardware
  41. flow control and Mode(Receiver/Transmitter) using the USART_Init()
  42. function.
  43. (#) For synchronous mode, enable the clock and program the polarity,
  44. phase and last bit using the USART_ClockInit() function.
  45. (#) Enable the NVIC and the corresponding interrupt using the function
  46. USART_ITConfig() if you need to use interrupt mode.
  47. (#) When using the DMA mode
  48. (++) Configure the DMA using DMA_Init() function
  49. (++) Active the needed channel Request using USART_DMACmd() function
  50. (#) Enable the USART using the USART_Cmd() function.
  51. (#) Enable the DMA using the DMA_Cmd() function, when using DMA mode.
  52. -@- Refer to Multi-Processor, LIN, half-duplex, Smartcard, IrDA sub-sections
  53. for more details
  54. [..]
  55. In order to reach higher communication baudrates, it is possible to
  56. enable the oversampling by 8 mode using the function USART_OverSampling8Cmd().
  57. This function should be called after enabling the USART clock (RCC_APBxPeriphClockCmd())
  58. and before calling the function USART_Init().
  59. @endverbatim
  60. ******************************************************************************
  61. * @attention
  62. *
  63. * Copyright (c) 2016 STMicroelectronics.
  64. * All rights reserved.
  65. *
  66. * This software is licensed under terms that can be found in the LICENSE file
  67. * in the root directory of this software component.
  68. * If no LICENSE file comes with this software, it is provided AS-IS.
  69. *
  70. ******************************************************************************
  71. */
  72. /* Includes ------------------------------------------------------------------*/
  73. #include "stm32f4xx_usart.h"
  74. #include "stm32f4xx_rcc.h"
  75. /** @addtogroup STM32F4xx_StdPeriph_Driver
  76. * @{
  77. */
  78. /** @defgroup USART
  79. * @brief USART driver modules
  80. * @{
  81. */
  82. /* Private typedef -----------------------------------------------------------*/
  83. /* Private define ------------------------------------------------------------*/
  84. /*!< USART CR1 register clear Mask ((~(uint16_t)0xE9F3)) */
  85. #define CR1_CLEAR_MASK ((uint16_t)(USART_CR1_M | USART_CR1_PCE | \
  86. USART_CR1_PS | USART_CR1_TE | \
  87. USART_CR1_RE))
  88. /*!< USART CR2 register clock bits clear Mask ((~(uint16_t)0xF0FF)) */
  89. #define CR2_CLOCK_CLEAR_MASK ((uint16_t)(USART_CR2_CLKEN | USART_CR2_CPOL | \
  90. USART_CR2_CPHA | USART_CR2_LBCL))
  91. /*!< USART CR3 register clear Mask ((~(uint16_t)0xFCFF)) */
  92. #define CR3_CLEAR_MASK ((uint16_t)(USART_CR3_RTSE | USART_CR3_CTSE))
  93. /*!< USART Interrupts mask */
  94. #define IT_MASK ((uint16_t)0x001F)
  95. /* Private macro -------------------------------------------------------------*/
  96. /* Private variables ---------------------------------------------------------*/
  97. /* Private function prototypes -----------------------------------------------*/
  98. /* Private functions ---------------------------------------------------------*/
  99. /** @defgroup USART_Private_Functions
  100. * @{
  101. */
  102. /** @defgroup USART_Group1 Initialization and Configuration functions
  103. * @brief Initialization and Configuration functions
  104. *
  105. @verbatim
  106. ===============================================================================
  107. ##### Initialization and Configuration functions #####
  108. ===============================================================================
  109. [..]
  110. This subsection provides a set of functions allowing to initialize the USART
  111. in asynchronous and in synchronous modes.
  112. (+) For the asynchronous mode only these parameters can be configured:
  113. (++) Baud Rate
  114. (++) Word Length
  115. (++) Stop Bit
  116. (++) Parity: If the parity is enabled, then the MSB bit of the data written
  117. in the data register is transmitted but is changed by the parity bit.
  118. Depending on the frame length defined by the M bit (8-bits or 9-bits),
  119. the possible USART frame formats are as listed in the following table:
  120. +-------------------------------------------------------------+
  121. | M bit | PCE bit | USART frame |
  122. |---------------------|---------------------------------------|
  123. | 0 | 0 | | SB | 8 bit data | STB | |
  124. |---------|-----------|---------------------------------------|
  125. | 0 | 1 | | SB | 7 bit data | PB | STB | |
  126. |---------|-----------|---------------------------------------|
  127. | 1 | 0 | | SB | 9 bit data | STB | |
  128. |---------|-----------|---------------------------------------|
  129. | 1 | 1 | | SB | 8 bit data | PB | STB | |
  130. +-------------------------------------------------------------+
  131. (++) Hardware flow control
  132. (++) Receiver/transmitter modes
  133. [..]
  134. The USART_Init() function follows the USART asynchronous configuration
  135. procedure (details for the procedure are available in reference manual (RM0090)).
  136. (+) For the synchronous mode in addition to the asynchronous mode parameters these
  137. parameters should be also configured:
  138. (++) USART Clock Enabled
  139. (++) USART polarity
  140. (++) USART phase
  141. (++) USART LastBit
  142. [..]
  143. These parameters can be configured using the USART_ClockInit() function.
  144. @endverbatim
  145. * @{
  146. */
  147. /**
  148. * @brief Deinitializes the USARTx peripheral registers to their default reset values.
  149. * @param USARTx: where x can be 1, 2, 3, 4, 5, 6, 7 or 8 to select the USART or
  150. * UART peripheral.
  151. * @retval None
  152. */
  153. void USART_DeInit(USART_TypeDef* USARTx)
  154. {
  155. /* Check the parameters */
  156. assert_param(IS_USART_ALL_PERIPH(USARTx));
  157. if (USARTx == USART1)
  158. {
  159. RCC_APB2PeriphResetCmd(RCC_APB2Periph_USART1, ENABLE);
  160. RCC_APB2PeriphResetCmd(RCC_APB2Periph_USART1, DISABLE);
  161. }
  162. else if (USARTx == USART2)
  163. {
  164. RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART2, ENABLE);
  165. RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART2, DISABLE);
  166. }
  167. else if (USARTx == USART3)
  168. {
  169. RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART3, ENABLE);
  170. RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART3, DISABLE);
  171. }
  172. else if (USARTx == UART4)
  173. {
  174. RCC_APB1PeriphResetCmd(RCC_APB1Periph_UART4, ENABLE);
  175. RCC_APB1PeriphResetCmd(RCC_APB1Periph_UART4, DISABLE);
  176. }
  177. else if (USARTx == UART5)
  178. {
  179. RCC_APB1PeriphResetCmd(RCC_APB1Periph_UART5, ENABLE);
  180. RCC_APB1PeriphResetCmd(RCC_APB1Periph_UART5, DISABLE);
  181. }
  182. else if (USARTx == USART6)
  183. {
  184. RCC_APB2PeriphResetCmd(RCC_APB2Periph_USART6, ENABLE);
  185. RCC_APB2PeriphResetCmd(RCC_APB2Periph_USART6, DISABLE);
  186. }
  187. else if (USARTx == UART7)
  188. {
  189. RCC_APB1PeriphResetCmd(RCC_APB1Periph_UART7, ENABLE);
  190. RCC_APB1PeriphResetCmd(RCC_APB1Periph_UART7, DISABLE);
  191. }
  192. else
  193. {
  194. if (USARTx == UART8)
  195. {
  196. RCC_APB1PeriphResetCmd(RCC_APB1Periph_UART8, ENABLE);
  197. RCC_APB1PeriphResetCmd(RCC_APB1Periph_UART8, DISABLE);
  198. }
  199. }
  200. }
  201. /**
  202. * @brief Initializes the USARTx peripheral according to the specified
  203. * parameters in the USART_InitStruct .
  204. * @param USARTx: where x can be 1, 2, 3, 4, 5, 6, 7 or 8 to select the USART or
  205. * UART peripheral.
  206. * @param USART_InitStruct: pointer to a USART_InitTypeDef structure that contains
  207. * the configuration information for the specified USART peripheral.
  208. * @retval None
  209. */
  210. void USART_Init(USART_TypeDef* USARTx, USART_InitTypeDef* USART_InitStruct)
  211. {
  212. uint32_t tmpreg = 0x00, apbclock = 0x00;
  213. uint32_t integerdivider = 0x00;
  214. uint32_t fractionaldivider = 0x00;
  215. RCC_ClocksTypeDef RCC_ClocksStatus;
  216. /* Check the parameters */
  217. assert_param(IS_USART_ALL_PERIPH(USARTx));
  218. assert_param(IS_USART_BAUDRATE(USART_InitStruct->USART_BaudRate));
  219. assert_param(IS_USART_WORD_LENGTH(USART_InitStruct->USART_WordLength));
  220. assert_param(IS_USART_STOPBITS(USART_InitStruct->USART_StopBits));
  221. assert_param(IS_USART_PARITY(USART_InitStruct->USART_Parity));
  222. assert_param(IS_USART_MODE(USART_InitStruct->USART_Mode));
  223. assert_param(IS_USART_HARDWARE_FLOW_CONTROL(USART_InitStruct->USART_HardwareFlowControl));
  224. /* The hardware flow control is available only for USART1, USART2, USART3 and USART6 */
  225. if (USART_InitStruct->USART_HardwareFlowControl != USART_HardwareFlowControl_None)
  226. {
  227. assert_param(IS_USART_1236_PERIPH(USARTx));
  228. }
  229. /*---------------------------- USART CR2 Configuration -----------------------*/
  230. tmpreg = USARTx->CR2;
  231. /* Clear STOP[13:12] bits */
  232. tmpreg &= (uint32_t)~((uint32_t)USART_CR2_STOP);
  233. /* Configure the USART Stop Bits, Clock, CPOL, CPHA and LastBit :
  234. Set STOP[13:12] bits according to USART_StopBits value */
  235. tmpreg |= (uint32_t)USART_InitStruct->USART_StopBits;
  236. /* Write to USART CR2 */
  237. USARTx->CR2 = (uint16_t)tmpreg;
  238. /*---------------------------- USART CR1 Configuration -----------------------*/
  239. tmpreg = USARTx->CR1;
  240. /* Clear M, PCE, PS, TE and RE bits */
  241. tmpreg &= (uint32_t)~((uint32_t)CR1_CLEAR_MASK);
  242. /* Configure the USART Word Length, Parity and mode:
  243. Set the M bits according to USART_WordLength value
  244. Set PCE and PS bits according to USART_Parity value
  245. Set TE and RE bits according to USART_Mode value */
  246. tmpreg |= (uint32_t)USART_InitStruct->USART_WordLength | USART_InitStruct->USART_Parity |
  247. USART_InitStruct->USART_Mode;
  248. /* Write to USART CR1 */
  249. USARTx->CR1 = (uint16_t)tmpreg;
  250. /*---------------------------- USART CR3 Configuration -----------------------*/
  251. tmpreg = USARTx->CR3;
  252. /* Clear CTSE and RTSE bits */
  253. tmpreg &= (uint32_t)~((uint32_t)CR3_CLEAR_MASK);
  254. /* Configure the USART HFC :
  255. Set CTSE and RTSE bits according to USART_HardwareFlowControl value */
  256. tmpreg |= USART_InitStruct->USART_HardwareFlowControl;
  257. /* Write to USART CR3 */
  258. USARTx->CR3 = (uint16_t)tmpreg;
  259. /*---------------------------- USART BRR Configuration -----------------------*/
  260. /* Configure the USART Baud Rate */
  261. RCC_GetClocksFreq(&RCC_ClocksStatus);
  262. if ((USARTx == USART1) || (USARTx == USART6))
  263. {
  264. apbclock = RCC_ClocksStatus.PCLK2_Frequency;
  265. }
  266. else
  267. {
  268. apbclock = RCC_ClocksStatus.PCLK1_Frequency;
  269. }
  270. /* Determine the integer part */
  271. if ((USARTx->CR1 & USART_CR1_OVER8) != 0)
  272. {
  273. /* Integer part computing in case Oversampling mode is 8 Samples */
  274. integerdivider = ((25 * apbclock) / (2 * (USART_InitStruct->USART_BaudRate)));
  275. }
  276. else /* if ((USARTx->CR1 & USART_CR1_OVER8) == 0) */
  277. {
  278. /* Integer part computing in case Oversampling mode is 16 Samples */
  279. integerdivider = ((25 * apbclock) / (4 * (USART_InitStruct->USART_BaudRate)));
  280. }
  281. tmpreg = (integerdivider / 100) << 4;
  282. /* Determine the fractional part */
  283. fractionaldivider = integerdivider - (100 * (tmpreg >> 4));
  284. /* Implement the fractional part in the register */
  285. if ((USARTx->CR1 & USART_CR1_OVER8) != 0)
  286. {
  287. tmpreg |= ((((fractionaldivider * 8) + 50) / 100)) & ((uint8_t)0x07);
  288. }
  289. else /* if ((USARTx->CR1 & USART_CR1_OVER8) == 0) */
  290. {
  291. tmpreg |= ((((fractionaldivider * 16) + 50) / 100)) & ((uint8_t)0x0F);
  292. }
  293. /* Write to USART BRR register */
  294. USARTx->BRR = (uint16_t)tmpreg;
  295. }
  296. /**
  297. * @brief Fills each USART_InitStruct member with its default value.
  298. * @param USART_InitStruct: pointer to a USART_InitTypeDef structure which will
  299. * be initialized.
  300. * @retval None
  301. */
  302. void USART_StructInit(USART_InitTypeDef* USART_InitStruct)
  303. {
  304. /* USART_InitStruct members default value */
  305. USART_InitStruct->USART_BaudRate = 9600;
  306. USART_InitStruct->USART_WordLength = USART_WordLength_8b;
  307. USART_InitStruct->USART_StopBits = USART_StopBits_1;
  308. USART_InitStruct->USART_Parity = USART_Parity_No ;
  309. USART_InitStruct->USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
  310. USART_InitStruct->USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  311. }
  312. /**
  313. * @brief Initializes the USARTx peripheral Clock according to the
  314. * specified parameters in the USART_ClockInitStruct .
  315. * @param USARTx: where x can be 1, 2, 3 or 6 to select the USART peripheral.
  316. * @param USART_ClockInitStruct: pointer to a USART_ClockInitTypeDef structure that
  317. * contains the configuration information for the specified USART peripheral.
  318. * @note The Smart Card and Synchronous modes are not available for UART4 and UART5.
  319. * @retval None
  320. */
  321. void USART_ClockInit(USART_TypeDef* USARTx, USART_ClockInitTypeDef* USART_ClockInitStruct)
  322. {
  323. uint32_t tmpreg = 0x00;
  324. /* Check the parameters */
  325. assert_param(IS_USART_1236_PERIPH(USARTx));
  326. assert_param(IS_USART_CLOCK(USART_ClockInitStruct->USART_Clock));
  327. assert_param(IS_USART_CPOL(USART_ClockInitStruct->USART_CPOL));
  328. assert_param(IS_USART_CPHA(USART_ClockInitStruct->USART_CPHA));
  329. assert_param(IS_USART_LASTBIT(USART_ClockInitStruct->USART_LastBit));
  330. /*---------------------------- USART CR2 Configuration -----------------------*/
  331. tmpreg = USARTx->CR2;
  332. /* Clear CLKEN, CPOL, CPHA and LBCL bits */
  333. tmpreg &= (uint32_t)~((uint32_t)CR2_CLOCK_CLEAR_MASK);
  334. /* Configure the USART Clock, CPOL, CPHA and LastBit ------------*/
  335. /* Set CLKEN bit according to USART_Clock value */
  336. /* Set CPOL bit according to USART_CPOL value */
  337. /* Set CPHA bit according to USART_CPHA value */
  338. /* Set LBCL bit according to USART_LastBit value */
  339. tmpreg |= (uint32_t)USART_ClockInitStruct->USART_Clock | USART_ClockInitStruct->USART_CPOL |
  340. USART_ClockInitStruct->USART_CPHA | USART_ClockInitStruct->USART_LastBit;
  341. /* Write to USART CR2 */
  342. USARTx->CR2 = (uint16_t)tmpreg;
  343. }
  344. /**
  345. * @brief Fills each USART_ClockInitStruct member with its default value.
  346. * @param USART_ClockInitStruct: pointer to a USART_ClockInitTypeDef structure
  347. * which will be initialized.
  348. * @retval None
  349. */
  350. void USART_ClockStructInit(USART_ClockInitTypeDef* USART_ClockInitStruct)
  351. {
  352. /* USART_ClockInitStruct members default value */
  353. USART_ClockInitStruct->USART_Clock = USART_Clock_Disable;
  354. USART_ClockInitStruct->USART_CPOL = USART_CPOL_Low;
  355. USART_ClockInitStruct->USART_CPHA = USART_CPHA_1Edge;
  356. USART_ClockInitStruct->USART_LastBit = USART_LastBit_Disable;
  357. }
  358. /**
  359. * @brief Enables or disables the specified USART peripheral.
  360. * @param USARTx: where x can be 1, 2, 3, 4, 5, 6, 7 or 8 to select the USART or
  361. * UART peripheral.
  362. * @param NewState: new state of the USARTx peripheral.
  363. * This parameter can be: ENABLE or DISABLE.
  364. * @retval None
  365. */
  366. void USART_Cmd(USART_TypeDef* USARTx, FunctionalState NewState)
  367. {
  368. /* Check the parameters */
  369. assert_param(IS_USART_ALL_PERIPH(USARTx));
  370. assert_param(IS_FUNCTIONAL_STATE(NewState));
  371. if (NewState != DISABLE)
  372. {
  373. /* Enable the selected USART by setting the UE bit in the CR1 register */
  374. USARTx->CR1 |= USART_CR1_UE;
  375. }
  376. else
  377. {
  378. /* Disable the selected USART by clearing the UE bit in the CR1 register */
  379. USARTx->CR1 &= (uint16_t)~((uint16_t)USART_CR1_UE);
  380. }
  381. }
  382. /**
  383. * @brief Sets the system clock prescaler.
  384. * @param USARTx: where x can be 1, 2, 3, 4, 5, 6, 7 or 8 to select the USART or
  385. * UART peripheral.
  386. * @param USART_Prescaler: specifies the prescaler clock.
  387. * @note The function is used for IrDA mode with UART4 and UART5.
  388. * @retval None
  389. */
  390. void USART_SetPrescaler(USART_TypeDef* USARTx, uint8_t USART_Prescaler)
  391. {
  392. /* Check the parameters */
  393. assert_param(IS_USART_ALL_PERIPH(USARTx));
  394. /* Clear the USART prescaler */
  395. USARTx->GTPR &= USART_GTPR_GT;
  396. /* Set the USART prescaler */
  397. USARTx->GTPR |= USART_Prescaler;
  398. }
  399. /**
  400. * @brief Enables or disables the USART's 8x oversampling mode.
  401. * @note This function has to be called before calling USART_Init() function
  402. * in order to have correct baudrate Divider value.
  403. * @param USARTx: where x can be 1, 2, 3, 4, 5, 6, 7 or 8 to select the USART or
  404. * UART peripheral.
  405. * @param NewState: new state of the USART 8x oversampling mode.
  406. * This parameter can be: ENABLE or DISABLE.
  407. * @retval None
  408. */
  409. void USART_OverSampling8Cmd(USART_TypeDef* USARTx, FunctionalState NewState)
  410. {
  411. /* Check the parameters */
  412. assert_param(IS_USART_ALL_PERIPH(USARTx));
  413. assert_param(IS_FUNCTIONAL_STATE(NewState));
  414. if (NewState != DISABLE)
  415. {
  416. /* Enable the 8x Oversampling mode by setting the OVER8 bit in the CR1 register */
  417. USARTx->CR1 |= USART_CR1_OVER8;
  418. }
  419. else
  420. {
  421. /* Disable the 8x Oversampling mode by clearing the OVER8 bit in the CR1 register */
  422. USARTx->CR1 &= (uint16_t)~((uint16_t)USART_CR1_OVER8);
  423. }
  424. }
  425. /**
  426. * @brief Enables or disables the USART's one bit sampling method.
  427. * @param USARTx: where x can be 1, 2, 3, 4, 5, 6, 7 or 8 to select the USART or
  428. * UART peripheral.
  429. * @param NewState: new state of the USART one bit sampling method.
  430. * This parameter can be: ENABLE or DISABLE.
  431. * @retval None
  432. */
  433. void USART_OneBitMethodCmd(USART_TypeDef* USARTx, FunctionalState NewState)
  434. {
  435. /* Check the parameters */
  436. assert_param(IS_USART_ALL_PERIPH(USARTx));
  437. assert_param(IS_FUNCTIONAL_STATE(NewState));
  438. if (NewState != DISABLE)
  439. {
  440. /* Enable the one bit method by setting the ONEBITE bit in the CR3 register */
  441. USARTx->CR3 |= USART_CR3_ONEBIT;
  442. }
  443. else
  444. {
  445. /* Disable the one bit method by clearing the ONEBITE bit in the CR3 register */
  446. USARTx->CR3 &= (uint16_t)~((uint16_t)USART_CR3_ONEBIT);
  447. }
  448. }
  449. /**
  450. * @}
  451. */
  452. /** @defgroup USART_Group2 Data transfers functions
  453. * @brief Data transfers functions
  454. *
  455. @verbatim
  456. ===============================================================================
  457. ##### Data transfers functions #####
  458. ===============================================================================
  459. [..]
  460. This subsection provides a set of functions allowing to manage the USART data
  461. transfers.
  462. [..]
  463. During an USART reception, data shifts in least significant bit first through
  464. the RX pin. In this mode, the USART_DR register consists of a buffer (RDR)
  465. between the internal bus and the received shift register.
  466. [..]
  467. When a transmission is taking place, a write instruction to the USART_DR register
  468. stores the data in the TDR register and which is copied in the shift register
  469. at the end of the current transmission.
  470. [..]
  471. The read access of the USART_DR register can be done using the USART_ReceiveData()
  472. function and returns the RDR buffered value. Whereas a write access to the USART_DR
  473. can be done using USART_SendData() function and stores the written data into
  474. TDR buffer.
  475. @endverbatim
  476. * @{
  477. */
  478. /**
  479. * @brief Transmits single data through the USARTx peripheral.
  480. * @param USARTx: where x can be 1, 2, 3, 4, 5, 6, 7 or 8 to select the USART or
  481. * UART peripheral.
  482. * @param Data: the data to transmit.
  483. * @retval None
  484. */
  485. void USART_SendData(USART_TypeDef* USARTx, uint16_t Data)
  486. {
  487. /* Check the parameters */
  488. assert_param(IS_USART_ALL_PERIPH(USARTx));
  489. assert_param(IS_USART_DATA(Data));
  490. /* Transmit Data */
  491. USARTx->DR = (Data & (uint16_t)0x01FF);
  492. }
  493. /**
  494. * @brief Returns the most recent received data by the USARTx peripheral.
  495. * @param USARTx: where x can be 1, 2, 3, 4, 5, 6, 7 or 8 to select the USART or
  496. * UART peripheral.
  497. * @retval The received data.
  498. */
  499. uint16_t USART_ReceiveData(USART_TypeDef* USARTx)
  500. {
  501. /* Check the parameters */
  502. assert_param(IS_USART_ALL_PERIPH(USARTx));
  503. /* Receive Data */
  504. return (uint16_t)(USARTx->DR & (uint16_t)0x01FF);
  505. }
  506. /**
  507. * @}
  508. */
  509. /** @defgroup USART_Group3 MultiProcessor Communication functions
  510. * @brief Multi-Processor Communication functions
  511. *
  512. @verbatim
  513. ===============================================================================
  514. ##### Multi-Processor Communication functions #####
  515. ===============================================================================
  516. [..]
  517. This subsection provides a set of functions allowing to manage the USART
  518. multiprocessor communication.
  519. [..]
  520. For instance one of the USARTs can be the master, its TX output is connected
  521. to the RX input of the other USART. The others are slaves, their respective
  522. TX outputs are logically ANDed together and connected to the RX input of the
  523. master.
  524. [..]
  525. USART multiprocessor communication is possible through the following procedure:
  526. (#) Program the Baud rate, Word length = 9 bits, Stop bits, Parity, Mode
  527. transmitter or Mode receiver and hardware flow control values using
  528. the USART_Init() function.
  529. (#) Configures the USART address using the USART_SetAddress() function.
  530. (#) Configures the wake up method (USART_WakeUp_IdleLine or USART_WakeUp_AddressMark)
  531. using USART_WakeUpConfig() function only for the slaves.
  532. (#) Enable the USART using the USART_Cmd() function.
  533. (#) Enter the USART slaves in mute mode using USART_ReceiverWakeUpCmd() function.
  534. [..]
  535. The USART Slave exit from mute mode when receive the wake up condition.
  536. @endverbatim
  537. * @{
  538. */
  539. /**
  540. * @brief Sets the address of the USART node.
  541. * @param USARTx: where x can be 1, 2, 3, 4, 5, 6, 7 or 8 to select the USART or
  542. * UART peripheral.
  543. * @param USART_Address: Indicates the address of the USART node.
  544. * @retval None
  545. */
  546. void USART_SetAddress(USART_TypeDef* USARTx, uint8_t USART_Address)
  547. {
  548. /* Check the parameters */
  549. assert_param(IS_USART_ALL_PERIPH(USARTx));
  550. assert_param(IS_USART_ADDRESS(USART_Address));
  551. /* Clear the USART address */
  552. USARTx->CR2 &= (uint16_t)~((uint16_t)USART_CR2_ADD);
  553. /* Set the USART address node */
  554. USARTx->CR2 |= USART_Address;
  555. }
  556. /**
  557. * @brief Determines if the USART is in mute mode or not.
  558. * @param USARTx: where x can be 1, 2, 3, 4, 5, 6, 7 or 8 to select the USART or
  559. * UART peripheral.
  560. * @param NewState: new state of the USART mute mode.
  561. * This parameter can be: ENABLE or DISABLE.
  562. * @retval None
  563. */
  564. void USART_ReceiverWakeUpCmd(USART_TypeDef* USARTx, FunctionalState NewState)
  565. {
  566. /* Check the parameters */
  567. assert_param(IS_USART_ALL_PERIPH(USARTx));
  568. assert_param(IS_FUNCTIONAL_STATE(NewState));
  569. if (NewState != DISABLE)
  570. {
  571. /* Enable the USART mute mode by setting the RWU bit in the CR1 register */
  572. USARTx->CR1 |= USART_CR1_RWU;
  573. }
  574. else
  575. {
  576. /* Disable the USART mute mode by clearing the RWU bit in the CR1 register */
  577. USARTx->CR1 &= (uint16_t)~((uint16_t)USART_CR1_RWU);
  578. }
  579. }
  580. /**
  581. * @brief Selects the USART WakeUp method.
  582. * @param USARTx: where x can be 1, 2, 3, 4, 5, 6, 7 or 8 to select the USART or
  583. * UART peripheral.
  584. * @param USART_WakeUp: specifies the USART wakeup method.
  585. * This parameter can be one of the following values:
  586. * @arg USART_WakeUp_IdleLine: WakeUp by an idle line detection
  587. * @arg USART_WakeUp_AddressMark: WakeUp by an address mark
  588. * @retval None
  589. */
  590. void USART_WakeUpConfig(USART_TypeDef* USARTx, uint16_t USART_WakeUp)
  591. {
  592. /* Check the parameters */
  593. assert_param(IS_USART_ALL_PERIPH(USARTx));
  594. assert_param(IS_USART_WAKEUP(USART_WakeUp));
  595. USARTx->CR1 &= (uint16_t)~((uint16_t)USART_CR1_WAKE);
  596. USARTx->CR1 |= USART_WakeUp;
  597. }
  598. /**
  599. * @}
  600. */
  601. /** @defgroup USART_Group4 LIN mode functions
  602. * @brief LIN mode functions
  603. *
  604. @verbatim
  605. ===============================================================================
  606. ##### LIN mode functions #####
  607. ===============================================================================
  608. [..]
  609. This subsection provides a set of functions allowing to manage the USART LIN
  610. Mode communication.
  611. [..]
  612. In LIN mode, 8-bit data format with 1 stop bit is required in accordance with
  613. the LIN standard.
  614. [..]
  615. Only this LIN Feature is supported by the USART IP:
  616. (+) LIN Master Synchronous Break send capability and LIN slave break detection
  617. capability : 13-bit break generation and 10/11 bit break detection
  618. [..]
  619. USART LIN Master transmitter communication is possible through the following
  620. procedure:
  621. (#) Program the Baud rate, Word length = 8bits, Stop bits = 1bit, Parity,
  622. Mode transmitter or Mode receiver and hardware flow control values using
  623. the USART_Init() function.
  624. (#) Enable the USART using the USART_Cmd() function.
  625. (#) Enable the LIN mode using the USART_LINCmd() function.
  626. (#) Send the break character using USART_SendBreak() function.
  627. [..]
  628. USART LIN Master receiver communication is possible through the following procedure:
  629. (#) Program the Baud rate, Word length = 8bits, Stop bits = 1bit, Parity,
  630. Mode transmitter or Mode receiver and hardware flow control values using
  631. the USART_Init() function.
  632. (#) Enable the USART using the USART_Cmd() function.
  633. (#) Configures the break detection length using the USART_LINBreakDetectLengthConfig()
  634. function.
  635. (#) Enable the LIN mode using the USART_LINCmd() function.
  636. -@- In LIN mode, the following bits must be kept cleared:
  637. (+@) CLKEN in the USART_CR2 register,
  638. (+@) STOP[1:0], SCEN, HDSEL and IREN in the USART_CR3 register.
  639. @endverbatim
  640. * @{
  641. */
  642. /**
  643. * @brief Sets the USART LIN Break detection length.
  644. * @param USARTx: where x can be 1, 2, 3, 4, 5, 6, 7 or 8 to select the USART or
  645. * UART peripheral.
  646. * @param USART_LINBreakDetectLength: specifies the LIN break detection length.
  647. * This parameter can be one of the following values:
  648. * @arg USART_LINBreakDetectLength_10b: 10-bit break detection
  649. * @arg USART_LINBreakDetectLength_11b: 11-bit break detection
  650. * @retval None
  651. */
  652. void USART_LINBreakDetectLengthConfig(USART_TypeDef* USARTx, uint16_t USART_LINBreakDetectLength)
  653. {
  654. /* Check the parameters */
  655. assert_param(IS_USART_ALL_PERIPH(USARTx));
  656. assert_param(IS_USART_LIN_BREAK_DETECT_LENGTH(USART_LINBreakDetectLength));
  657. USARTx->CR2 &= (uint16_t)~((uint16_t)USART_CR2_LBDL);
  658. USARTx->CR2 |= USART_LINBreakDetectLength;
  659. }
  660. /**
  661. * @brief Enables or disables the USART's LIN mode.
  662. * @param USARTx: where x can be 1, 2, 3, 4, 5, 6, 7 or 8 to select the USART or
  663. * UART peripheral.
  664. * @param NewState: new state of the USART LIN mode.
  665. * This parameter can be: ENABLE or DISABLE.
  666. * @retval None
  667. */
  668. void USART_LINCmd(USART_TypeDef* USARTx, FunctionalState NewState)
  669. {
  670. /* Check the parameters */
  671. assert_param(IS_USART_ALL_PERIPH(USARTx));
  672. assert_param(IS_FUNCTIONAL_STATE(NewState));
  673. if (NewState != DISABLE)
  674. {
  675. /* Enable the LIN mode by setting the LINEN bit in the CR2 register */
  676. USARTx->CR2 |= USART_CR2_LINEN;
  677. }
  678. else
  679. {
  680. /* Disable the LIN mode by clearing the LINEN bit in the CR2 register */
  681. USARTx->CR2 &= (uint16_t)~((uint16_t)USART_CR2_LINEN);
  682. }
  683. }
  684. /**
  685. * @brief Transmits break characters.
  686. * @param USARTx: where x can be 1, 2, 3, 4, 5, 6, 7 or 8 to select the USART or
  687. * UART peripheral.
  688. * @retval None
  689. */
  690. void USART_SendBreak(USART_TypeDef* USARTx)
  691. {
  692. /* Check the parameters */
  693. assert_param(IS_USART_ALL_PERIPH(USARTx));
  694. /* Send break characters */
  695. USARTx->CR1 |= USART_CR1_SBK;
  696. }
  697. /**
  698. * @}
  699. */
  700. /** @defgroup USART_Group5 Halfduplex mode function
  701. * @brief Half-duplex mode function
  702. *
  703. @verbatim
  704. ===============================================================================
  705. ##### Half-duplex mode function #####
  706. ===============================================================================
  707. [..]
  708. This subsection provides a set of functions allowing to manage the USART
  709. Half-duplex communication.
  710. [..]
  711. The USART can be configured to follow a single-wire half-duplex protocol where
  712. the TX and RX lines are internally connected.
  713. [..]
  714. USART Half duplex communication is possible through the following procedure:
  715. (#) Program the Baud rate, Word length, Stop bits, Parity, Mode transmitter
  716. or Mode receiver and hardware flow control values using the USART_Init()
  717. function.
  718. (#) Configures the USART address using the USART_SetAddress() function.
  719. (#) Enable the USART using the USART_Cmd() function.
  720. (#) Enable the half duplex mode using USART_HalfDuplexCmd() function.
  721. -@- The RX pin is no longer used
  722. -@- In Half-duplex mode the following bits must be kept cleared:
  723. (+@) LINEN and CLKEN bits in the USART_CR2 register.
  724. (+@) SCEN and IREN bits in the USART_CR3 register.
  725. @endverbatim
  726. * @{
  727. */
  728. /**
  729. * @brief Enables or disables the USART's Half Duplex communication.
  730. * @param USARTx: where x can be 1, 2, 3, 4, 5, 6, 7 or 8 to select the USART or
  731. * UART peripheral.
  732. * @param NewState: new state of the USART Communication.
  733. * This parameter can be: ENABLE or DISABLE.
  734. * @retval None
  735. */
  736. void USART_HalfDuplexCmd(USART_TypeDef* USARTx, FunctionalState NewState)
  737. {
  738. /* Check the parameters */
  739. assert_param(IS_USART_ALL_PERIPH(USARTx));
  740. assert_param(IS_FUNCTIONAL_STATE(NewState));
  741. if (NewState != DISABLE)
  742. {
  743. /* Enable the Half-Duplex mode by setting the HDSEL bit in the CR3 register */
  744. USARTx->CR3 |= USART_CR3_HDSEL;
  745. }
  746. else
  747. {
  748. /* Disable the Half-Duplex mode by clearing the HDSEL bit in the CR3 register */
  749. USARTx->CR3 &= (uint16_t)~((uint16_t)USART_CR3_HDSEL);
  750. }
  751. }
  752. /**
  753. * @}
  754. */
  755. /** @defgroup USART_Group6 Smartcard mode functions
  756. * @brief Smartcard mode functions
  757. *
  758. @verbatim
  759. ===============================================================================
  760. ##### Smartcard mode functions #####
  761. ===============================================================================
  762. [..]
  763. This subsection provides a set of functions allowing to manage the USART
  764. Smartcard communication.
  765. [..]
  766. The Smartcard interface is designed to support asynchronous protocol Smartcards as
  767. defined in the ISO 7816-3 standard.
  768. [..]
  769. The USART can provide a clock to the smartcard through the SCLK output.
  770. In smartcard mode, SCLK is not associated to the communication but is simply derived
  771. from the internal peripheral input clock through a 5-bit prescaler.
  772. [..]
  773. Smartcard communication is possible through the following procedure:
  774. (#) Configures the Smartcard Prescaler using the USART_SetPrescaler() function.
  775. (#) Configures the Smartcard Guard Time using the USART_SetGuardTime() function.
  776. (#) Program the USART clock using the USART_ClockInit() function as following:
  777. (++) USART Clock enabled
  778. (++) USART CPOL Low
  779. (++) USART CPHA on first edge
  780. (++) USART Last Bit Clock Enabled
  781. (#) Program the Smartcard interface using the USART_Init() function as following:
  782. (++) Word Length = 9 Bits
  783. (++) 1.5 Stop Bit
  784. (++) Even parity
  785. (++) BaudRate = 12096 baud
  786. (++) Hardware flow control disabled (RTS and CTS signals)
  787. (++) Tx and Rx enabled
  788. (#) POptionally you can enable the parity error interrupt using the USART_ITConfig()
  789. function
  790. (#) PEnable the USART using the USART_Cmd() function.
  791. (#) PEnable the Smartcard NACK using the USART_SmartCardNACKCmd() function.
  792. (#) PEnable the Smartcard interface using the USART_SmartCardCmd() function.
  793. Please refer to the ISO 7816-3 specification for more details.
  794. -@- It is also possible to choose 0.5 stop bit for receiving but it is recommended
  795. to use 1.5 stop bits for both transmitting and receiving to avoid switching
  796. between the two configurations.
  797. -@- In smartcard mode, the following bits must be kept cleared:
  798. (+@) LINEN bit in the USART_CR2 register.
  799. (+@) HDSEL and IREN bits in the USART_CR3 register.
  800. -@- Smartcard mode is available on USART peripherals only (not available on UART4
  801. and UART5 peripherals).
  802. @endverbatim
  803. * @{
  804. */
  805. /**
  806. * @brief Sets the specified USART guard time.
  807. * @param USARTx: where x can be 1, 2, 3 or 6 to select the USART or
  808. * UART peripheral.
  809. * @param USART_GuardTime: specifies the guard time.
  810. * @retval None
  811. */
  812. void USART_SetGuardTime(USART_TypeDef* USARTx, uint8_t USART_GuardTime)
  813. {
  814. /* Check the parameters */
  815. assert_param(IS_USART_1236_PERIPH(USARTx));
  816. /* Clear the USART Guard time */
  817. USARTx->GTPR &= USART_GTPR_PSC;
  818. /* Set the USART guard time */
  819. USARTx->GTPR |= (uint16_t)((uint16_t)USART_GuardTime << 0x08);
  820. }
  821. /**
  822. * @brief Enables or disables the USART's Smart Card mode.
  823. * @param USARTx: where x can be 1, 2, 3 or 6 to select the USART or
  824. * UART peripheral.
  825. * @param NewState: new state of the Smart Card mode.
  826. * This parameter can be: ENABLE or DISABLE.
  827. * @retval None
  828. */
  829. void USART_SmartCardCmd(USART_TypeDef* USARTx, FunctionalState NewState)
  830. {
  831. /* Check the parameters */
  832. assert_param(IS_USART_1236_PERIPH(USARTx));
  833. assert_param(IS_FUNCTIONAL_STATE(NewState));
  834. if (NewState != DISABLE)
  835. {
  836. /* Enable the SC mode by setting the SCEN bit in the CR3 register */
  837. USARTx->CR3 |= USART_CR3_SCEN;
  838. }
  839. else
  840. {
  841. /* Disable the SC mode by clearing the SCEN bit in the CR3 register */
  842. USARTx->CR3 &= (uint16_t)~((uint16_t)USART_CR3_SCEN);
  843. }
  844. }
  845. /**
  846. * @brief Enables or disables NACK transmission.
  847. * @param USARTx: where x can be 1, 2, 3 or 6 to select the USART or
  848. * UART peripheral.
  849. * @param NewState: new state of the NACK transmission.
  850. * This parameter can be: ENABLE or DISABLE.
  851. * @retval None
  852. */
  853. void USART_SmartCardNACKCmd(USART_TypeDef* USARTx, FunctionalState NewState)
  854. {
  855. /* Check the parameters */
  856. assert_param(IS_USART_1236_PERIPH(USARTx));
  857. assert_param(IS_FUNCTIONAL_STATE(NewState));
  858. if (NewState != DISABLE)
  859. {
  860. /* Enable the NACK transmission by setting the NACK bit in the CR3 register */
  861. USARTx->CR3 |= USART_CR3_NACK;
  862. }
  863. else
  864. {
  865. /* Disable the NACK transmission by clearing the NACK bit in the CR3 register */
  866. USARTx->CR3 &= (uint16_t)~((uint16_t)USART_CR3_NACK);
  867. }
  868. }
  869. /**
  870. * @}
  871. */
  872. /** @defgroup USART_Group7 IrDA mode functions
  873. * @brief IrDA mode functions
  874. *
  875. @verbatim
  876. ===============================================================================
  877. ##### IrDA mode functions #####
  878. ===============================================================================
  879. [..]
  880. This subsection provides a set of functions allowing to manage the USART
  881. IrDA communication.
  882. [..]
  883. IrDA is a half duplex communication protocol. If the Transmitter is busy, any data
  884. on the IrDA receive line will be ignored by the IrDA decoder and if the Receiver
  885. is busy, data on the TX from the USART to IrDA will not be encoded by IrDA.
  886. While receiving data, transmission should be avoided as the data to be transmitted
  887. could be corrupted.
  888. [..]
  889. IrDA communication is possible through the following procedure:
  890. (#) Program the Baud rate, Word length = 8 bits, Stop bits, Parity, Transmitter/Receiver
  891. modes and hardware flow control values using the USART_Init() function.
  892. (#) Enable the USART using the USART_Cmd() function.
  893. (#) Configures the IrDA pulse width by configuring the prescaler using
  894. the USART_SetPrescaler() function.
  895. (#) Configures the IrDA USART_IrDAMode_LowPower or USART_IrDAMode_Normal mode
  896. using the USART_IrDAConfig() function.
  897. (#) Enable the IrDA using the USART_IrDACmd() function.
  898. -@- A pulse of width less than two and greater than one PSC period(s) may or may
  899. not be rejected.
  900. -@- The receiver set up time should be managed by software. The IrDA physical layer
  901. specification specifies a minimum of 10 ms delay between transmission and
  902. reception (IrDA is a half duplex protocol).
  903. -@- In IrDA mode, the following bits must be kept cleared:
  904. (+@) LINEN, STOP and CLKEN bits in the USART_CR2 register.
  905. (+@) SCEN and HDSEL bits in the USART_CR3 register.
  906. @endverbatim
  907. * @{
  908. */
  909. /**
  910. * @brief Configures the USART's IrDA interface.
  911. * @param USARTx: where x can be 1, 2, 3, 4, 5, 6, 7 or 8 to select the USART or
  912. * UART peripheral.
  913. * @param USART_IrDAMode: specifies the IrDA mode.
  914. * This parameter can be one of the following values:
  915. * @arg USART_IrDAMode_LowPower
  916. * @arg USART_IrDAMode_Normal
  917. * @retval None
  918. */
  919. void USART_IrDAConfig(USART_TypeDef* USARTx, uint16_t USART_IrDAMode)
  920. {
  921. /* Check the parameters */
  922. assert_param(IS_USART_ALL_PERIPH(USARTx));
  923. assert_param(IS_USART_IRDA_MODE(USART_IrDAMode));
  924. USARTx->CR3 &= (uint16_t)~((uint16_t)USART_CR3_IRLP);
  925. USARTx->CR3 |= USART_IrDAMode;
  926. }
  927. /**
  928. * @brief Enables or disables the USART's IrDA interface.
  929. * @param USARTx: where x can be 1, 2, 3, 4, 5, 6, 7 or 8 to select the USART or
  930. * UART peripheral.
  931. * @param NewState: new state of the IrDA mode.
  932. * This parameter can be: ENABLE or DISABLE.
  933. * @retval None
  934. */
  935. void USART_IrDACmd(USART_TypeDef* USARTx, FunctionalState NewState)
  936. {
  937. /* Check the parameters */
  938. assert_param(IS_USART_ALL_PERIPH(USARTx));
  939. assert_param(IS_FUNCTIONAL_STATE(NewState));
  940. if (NewState != DISABLE)
  941. {
  942. /* Enable the IrDA mode by setting the IREN bit in the CR3 register */
  943. USARTx->CR3 |= USART_CR3_IREN;
  944. }
  945. else
  946. {
  947. /* Disable the IrDA mode by clearing the IREN bit in the CR3 register */
  948. USARTx->CR3 &= (uint16_t)~((uint16_t)USART_CR3_IREN);
  949. }
  950. }
  951. /**
  952. * @}
  953. */
  954. /** @defgroup USART_Group8 DMA transfers management functions
  955. * @brief DMA transfers management functions
  956. *
  957. @verbatim
  958. ===============================================================================
  959. ##### DMA transfers management functions #####
  960. ===============================================================================
  961. @endverbatim
  962. * @{
  963. */
  964. /**
  965. * @brief Enables or disables the USART's DMA interface.
  966. * @param USARTx: where x can be 1, 2, 3, 4, 5, 6, 7 or 8 to select the USART or
  967. * UART peripheral.
  968. * @param USART_DMAReq: specifies the DMA request.
  969. * This parameter can be any combination of the following values:
  970. * @arg USART_DMAReq_Tx: USART DMA transmit request
  971. * @arg USART_DMAReq_Rx: USART DMA receive request
  972. * @param NewState: new state of the DMA Request sources.
  973. * This parameter can be: ENABLE or DISABLE.
  974. * @retval None
  975. */
  976. void USART_DMACmd(USART_TypeDef* USARTx, uint16_t USART_DMAReq, FunctionalState NewState)
  977. {
  978. /* Check the parameters */
  979. assert_param(IS_USART_ALL_PERIPH(USARTx));
  980. assert_param(IS_USART_DMAREQ(USART_DMAReq));
  981. assert_param(IS_FUNCTIONAL_STATE(NewState));
  982. if (NewState != DISABLE)
  983. {
  984. /* Enable the DMA transfer for selected requests by setting the DMAT and/or
  985. DMAR bits in the USART CR3 register */
  986. USARTx->CR3 |= USART_DMAReq;
  987. }
  988. else
  989. {
  990. /* Disable the DMA transfer for selected requests by clearing the DMAT and/or
  991. DMAR bits in the USART CR3 register */
  992. USARTx->CR3 &= (uint16_t)~USART_DMAReq;
  993. }
  994. }
  995. /**
  996. * @}
  997. */
  998. /** @defgroup USART_Group9 Interrupts and flags management functions
  999. * @brief Interrupts and flags management functions
  1000. *
  1001. @verbatim
  1002. ===============================================================================
  1003. ##### Interrupts and flags management functions #####
  1004. ===============================================================================
  1005. [..]
  1006. This subsection provides a set of functions allowing to configure the USART
  1007. Interrupts sources, DMA channels requests and check or clear the flags or
  1008. pending bits status.
  1009. The user should identify which mode will be used in his application to manage
  1010. the communication: Polling mode, Interrupt mode or DMA mode.
  1011. *** Polling Mode ***
  1012. ====================
  1013. [..]
  1014. In Polling Mode, the SPI communication can be managed by 10 flags:
  1015. (#) USART_FLAG_TXE : to indicate the status of the transmit buffer register
  1016. (#) USART_FLAG_RXNE : to indicate the status of the receive buffer register
  1017. (#) USART_FLAG_TC : to indicate the status of the transmit operation
  1018. (#) USART_FLAG_IDLE : to indicate the status of the Idle Line
  1019. (#) USART_FLAG_CTS : to indicate the status of the nCTS input
  1020. (#) USART_FLAG_LBD : to indicate the status of the LIN break detection
  1021. (#) USART_FLAG_NE : to indicate if a noise error occur
  1022. (#) USART_FLAG_FE : to indicate if a frame error occur
  1023. (#) USART_FLAG_PE : to indicate if a parity error occur
  1024. (#) USART_FLAG_ORE : to indicate if an Overrun error occur
  1025. [..]
  1026. In this Mode it is advised to use the following functions:
  1027. (+) FlagStatus USART_GetFlagStatus(USART_TypeDef* USARTx, uint16_t USART_FLAG);
  1028. (+) void USART_ClearFlag(USART_TypeDef* USARTx, uint16_t USART_FLAG);
  1029. *** Interrupt Mode ***
  1030. ======================
  1031. [..]
  1032. In Interrupt Mode, the USART communication can be managed by 8 interrupt sources
  1033. and 10 pending bits:
  1034. (#) Pending Bits:
  1035. (##) USART_IT_TXE : to indicate the status of the transmit buffer register
  1036. (##) USART_IT_RXNE : to indicate the status of the receive buffer register
  1037. (##) USART_IT_TC : to indicate the status of the transmit operation
  1038. (##) USART_IT_IDLE : to indicate the status of the Idle Line
  1039. (##) USART_IT_CTS : to indicate the status of the nCTS input
  1040. (##) USART_IT_LBD : to indicate the status of the LIN break detection
  1041. (##) USART_IT_NE : to indicate if a noise error occur
  1042. (##) USART_IT_FE : to indicate if a frame error occur
  1043. (##) USART_IT_PE : to indicate if a parity error occur
  1044. (##) USART_IT_ORE : to indicate if an Overrun error occur
  1045. (#) Interrupt Source:
  1046. (##) USART_IT_TXE : specifies the interrupt source for the Tx buffer empty
  1047. interrupt.
  1048. (##) USART_IT_RXNE : specifies the interrupt source for the Rx buffer not
  1049. empty interrupt.
  1050. (##) USART_IT_TC : specifies the interrupt source for the Transmit complete
  1051. interrupt.
  1052. (##) USART_IT_IDLE : specifies the interrupt source for the Idle Line interrupt.
  1053. (##) USART_IT_CTS : specifies the interrupt source for the CTS interrupt.
  1054. (##) USART_IT_LBD : specifies the interrupt source for the LIN break detection
  1055. interrupt.
  1056. (##) USART_IT_PE : specifies the interrupt source for the parity error interrupt.
  1057. (##) USART_IT_ERR : specifies the interrupt source for the errors interrupt.
  1058. -@@- Some parameters are coded in order to use them as interrupt source
  1059. or as pending bits.
  1060. [..]
  1061. In this Mode it is advised to use the following functions:
  1062. (+) void USART_ITConfig(USART_TypeDef* USARTx, uint16_t USART_IT, FunctionalState NewState);
  1063. (+) ITStatus USART_GetITStatus(USART_TypeDef* USARTx, uint16_t USART_IT);
  1064. (+) void USART_ClearITPendingBit(USART_TypeDef* USARTx, uint16_t USART_IT);
  1065. *** DMA Mode ***
  1066. ================
  1067. [..]
  1068. In DMA Mode, the USART communication can be managed by 2 DMA Channel requests:
  1069. (#) USART_DMAReq_Tx: specifies the Tx buffer DMA transfer request
  1070. (#) USART_DMAReq_Rx: specifies the Rx buffer DMA transfer request
  1071. [..]
  1072. In this Mode it is advised to use the following function:
  1073. (+) void USART_DMACmd(USART_TypeDef* USARTx, uint16_t USART_DMAReq, FunctionalState NewState);
  1074. @endverbatim
  1075. * @{
  1076. */
  1077. /**
  1078. * @brief Enables or disables the specified USART interrupts.
  1079. * @param USARTx: where x can be 1, 2, 3, 4, 5, 6, 7 or 8 to select the USART or
  1080. * UART peripheral.
  1081. * @param USART_IT: specifies the USART interrupt sources to be enabled or disabled.
  1082. * This parameter can be one of the following values:
  1083. * @arg USART_IT_CTS: CTS change interrupt
  1084. * @arg USART_IT_LBD: LIN Break detection interrupt
  1085. * @arg USART_IT_TXE: Transmit Data Register empty interrupt
  1086. * @arg USART_IT_TC: Transmission complete interrupt
  1087. * @arg USART_IT_RXNE: Receive Data register not empty interrupt
  1088. * @arg USART_IT_IDLE: Idle line detection interrupt
  1089. * @arg USART_IT_PE: Parity Error interrupt
  1090. * @arg USART_IT_ERR: Error interrupt(Frame error, noise error, overrun error)
  1091. * @param NewState: new state of the specified USARTx interrupts.
  1092. * This parameter can be: ENABLE or DISABLE.
  1093. * @retval None
  1094. */
  1095. void USART_ITConfig(USART_TypeDef* USARTx, uint16_t USART_IT, FunctionalState NewState)
  1096. {
  1097. uint32_t usartreg = 0x00, itpos = 0x00, itmask = 0x00;
  1098. uint32_t usartxbase = 0x00;
  1099. /* Check the parameters */
  1100. assert_param(IS_USART_ALL_PERIPH(USARTx));
  1101. assert_param(IS_USART_CONFIG_IT(USART_IT));
  1102. assert_param(IS_FUNCTIONAL_STATE(NewState));
  1103. /* The CTS interrupt is not available for UART4 and UART5 */
  1104. if (USART_IT == USART_IT_CTS)
  1105. {
  1106. assert_param(IS_USART_1236_PERIPH(USARTx));
  1107. }
  1108. usartxbase = (uint32_t)USARTx;
  1109. /* Get the USART register index */
  1110. usartreg = (((uint8_t)USART_IT) >> 0x05);
  1111. /* Get the interrupt position */
  1112. itpos = USART_IT & IT_MASK;
  1113. itmask = (((uint32_t)0x01) << itpos);
  1114. if (usartreg == 0x01) /* The IT is in CR1 register */
  1115. {
  1116. usartxbase += 0x0C;
  1117. }
  1118. else if (usartreg == 0x02) /* The IT is in CR2 register */
  1119. {
  1120. usartxbase += 0x10;
  1121. }
  1122. else /* The IT is in CR3 register */
  1123. {
  1124. usartxbase += 0x14;
  1125. }
  1126. if (NewState != DISABLE)
  1127. {
  1128. *(__IO uint32_t*)usartxbase |= itmask;
  1129. }
  1130. else
  1131. {
  1132. *(__IO uint32_t*)usartxbase &= ~itmask;
  1133. }
  1134. }
  1135. /**
  1136. * @brief Checks whether the specified USART flag is set or not.
  1137. * @param USARTx: where x can be 1, 2, 3, 4, 5, 6, 7 or 8 to select the USART or
  1138. * UART peripheral.
  1139. * @param USART_FLAG: specifies the flag to check.
  1140. * This parameter can be one of the following values:
  1141. * @arg USART_FLAG_CTS: CTS Change flag (not available for UART4 and UART5)
  1142. * @arg USART_FLAG_LBD: LIN Break detection flag
  1143. * @arg USART_FLAG_TXE: Transmit data register empty flag
  1144. * @arg USART_FLAG_TC: Transmission Complete flag
  1145. * @arg USART_FLAG_RXNE: Receive data register not empty flag
  1146. * @arg USART_FLAG_IDLE: Idle Line detection flag
  1147. * @arg USART_FLAG_ORE: OverRun Error flag
  1148. * @arg USART_FLAG_NE: Noise Error flag
  1149. * @arg USART_FLAG_FE: Framing Error flag
  1150. * @arg USART_FLAG_PE: Parity Error flag
  1151. * @retval The new state of USART_FLAG (SET or RESET).
  1152. */
  1153. FlagStatus USART_GetFlagStatus(USART_TypeDef* USARTx, uint16_t USART_FLAG)
  1154. {
  1155. FlagStatus bitstatus = RESET;
  1156. /* Check the parameters */
  1157. assert_param(IS_USART_ALL_PERIPH(USARTx));
  1158. assert_param(IS_USART_FLAG(USART_FLAG));
  1159. /* The CTS flag is not available for UART4 and UART5 */
  1160. if (USART_FLAG == USART_FLAG_CTS)
  1161. {
  1162. assert_param(IS_USART_1236_PERIPH(USARTx));
  1163. }
  1164. if ((USARTx->SR & USART_FLAG) != (uint16_t)RESET)
  1165. {
  1166. bitstatus = SET;
  1167. }
  1168. else
  1169. {
  1170. bitstatus = RESET;
  1171. }
  1172. return bitstatus;
  1173. }
  1174. /**
  1175. * @brief Clears the USARTx's pending flags.
  1176. * @param USARTx: where x can be 1, 2, 3, 4, 5, 6, 7 or 8 to select the USART or
  1177. * UART peripheral.
  1178. * @param USART_FLAG: specifies the flag to clear.
  1179. * This parameter can be any combination of the following values:
  1180. * @arg USART_FLAG_CTS: CTS Change flag (not available for UART4 and UART5).
  1181. * @arg USART_FLAG_LBD: LIN Break detection flag.
  1182. * @arg USART_FLAG_TC: Transmission Complete flag.
  1183. * @arg USART_FLAG_RXNE: Receive data register not empty flag.
  1184. *
  1185. * @note PE (Parity error), FE (Framing error), NE (Noise error), ORE (OverRun
  1186. * error) and IDLE (Idle line detected) flags are cleared by software
  1187. * sequence: a read operation to USART_SR register (USART_GetFlagStatus())
  1188. * followed by a read operation to USART_DR register (USART_ReceiveData()).
  1189. * @note RXNE flag can be also cleared by a read to the USART_DR register
  1190. * (USART_ReceiveData()).
  1191. * @note TC flag can be also cleared by software sequence: a read operation to
  1192. * USART_SR register (USART_GetFlagStatus()) followed by a write operation
  1193. * to USART_DR register (USART_SendData()).
  1194. * @note TXE flag is cleared only by a write to the USART_DR register
  1195. * (USART_SendData()).
  1196. *
  1197. * @retval None
  1198. */
  1199. void USART_ClearFlag(USART_TypeDef* USARTx, uint16_t USART_FLAG)
  1200. {
  1201. /* Check the parameters */
  1202. assert_param(IS_USART_ALL_PERIPH(USARTx));
  1203. assert_param(IS_USART_CLEAR_FLAG(USART_FLAG));
  1204. /* The CTS flag is not available for UART4 and UART5 */
  1205. if ((USART_FLAG & USART_FLAG_CTS) == USART_FLAG_CTS)
  1206. {
  1207. assert_param(IS_USART_1236_PERIPH(USARTx));
  1208. }
  1209. USARTx->SR = (uint16_t)~USART_FLAG;
  1210. }
  1211. /**
  1212. * @brief Checks whether the specified USART interrupt has occurred or not.
  1213. * @param USARTx: where x can be 1, 2, 3, 4, 5, 6, 7 or 8 to select the USART or
  1214. * UART peripheral.
  1215. * @param USART_IT: specifies the USART interrupt source to check.
  1216. * This parameter can be one of the following values:
  1217. * @arg USART_IT_CTS: CTS change interrupt (not available for UART4 and UART5)
  1218. * @arg USART_IT_LBD: LIN Break detection interrupt
  1219. * @arg USART_IT_TXE: Transmit Data Register empty interrupt
  1220. * @arg USART_IT_TC: Transmission complete interrupt
  1221. * @arg USART_IT_RXNE: Receive Data register not empty interrupt
  1222. * @arg USART_IT_IDLE: Idle line detection interrupt
  1223. * @arg USART_IT_ORE_RX : OverRun Error interrupt if the RXNEIE bit is set
  1224. * @arg USART_IT_ORE_ER : OverRun Error interrupt if the EIE bit is set
  1225. * @arg USART_IT_NE: Noise Error interrupt
  1226. * @arg USART_IT_FE: Framing Error interrupt
  1227. * @arg USART_IT_PE: Parity Error interrupt
  1228. * @retval The new state of USART_IT (SET or RESET).
  1229. */
  1230. ITStatus USART_GetITStatus(USART_TypeDef* USARTx, uint16_t USART_IT)
  1231. {
  1232. uint32_t bitpos = 0x00, itmask = 0x00, usartreg = 0x00;
  1233. ITStatus bitstatus = RESET;
  1234. /* Check the parameters */
  1235. assert_param(IS_USART_ALL_PERIPH(USARTx));
  1236. assert_param(IS_USART_GET_IT(USART_IT));
  1237. /* The CTS interrupt is not available for UART4 and UART5 */
  1238. if (USART_IT == USART_IT_CTS)
  1239. {
  1240. assert_param(IS_USART_1236_PERIPH(USARTx));
  1241. }
  1242. /* Get the USART register index */
  1243. usartreg = (((uint8_t)USART_IT) >> 0x05);
  1244. /* Get the interrupt position */
  1245. itmask = USART_IT & IT_MASK;
  1246. itmask = (uint32_t)0x01 << itmask;
  1247. if (usartreg == 0x01) /* The IT is in CR1 register */
  1248. {
  1249. itmask &= USARTx->CR1;
  1250. }
  1251. else if (usartreg == 0x02) /* The IT is in CR2 register */
  1252. {
  1253. itmask &= USARTx->CR2;
  1254. }
  1255. else /* The IT is in CR3 register */
  1256. {
  1257. itmask &= USARTx->CR3;
  1258. }
  1259. bitpos = USART_IT >> 0x08;
  1260. bitpos = (uint32_t)0x01 << bitpos;
  1261. bitpos &= USARTx->SR;
  1262. if ((itmask != (uint16_t)RESET)&&(bitpos != (uint16_t)RESET))
  1263. {
  1264. bitstatus = SET;
  1265. }
  1266. else
  1267. {
  1268. bitstatus = RESET;
  1269. }
  1270. return bitstatus;
  1271. }
  1272. /**
  1273. * @brief Clears the USARTx's interrupt pending bits.
  1274. * @param USARTx: where x can be 1, 2, 3, 4, 5, 6, 7 or 8 to select the USART or
  1275. * UART peripheral.
  1276. * @param USART_IT: specifies the interrupt pending bit to clear.
  1277. * This parameter can be one of the following values:
  1278. * @arg USART_IT_CTS: CTS change interrupt (not available for UART4 and UART5)
  1279. * @arg USART_IT_LBD: LIN Break detection interrupt
  1280. * @arg USART_IT_TC: Transmission complete interrupt.
  1281. * @arg USART_IT_RXNE: Receive Data register not empty interrupt.
  1282. *
  1283. * @note PE (Parity error), FE (Framing error), NE (Noise error), ORE (OverRun
  1284. * error) and IDLE (Idle line detected) pending bits are cleared by
  1285. * software sequence: a read operation to USART_SR register
  1286. * (USART_GetITStatus()) followed by a read operation to USART_DR register
  1287. * (USART_ReceiveData()).
  1288. * @note RXNE pending bit can be also cleared by a read to the USART_DR register
  1289. * (USART_ReceiveData()).
  1290. * @note TC pending bit can be also cleared by software sequence: a read
  1291. * operation to USART_SR register (USART_GetITStatus()) followed by a write
  1292. * operation to USART_DR register (USART_SendData()).
  1293. * @note TXE pending bit is cleared only by a write to the USART_DR register
  1294. * (USART_SendData()).
  1295. *
  1296. * @retval None
  1297. */
  1298. void USART_ClearITPendingBit(USART_TypeDef* USARTx, uint16_t USART_IT)
  1299. {
  1300. uint16_t bitpos = 0x00, itmask = 0x00;
  1301. /* Check the parameters */
  1302. assert_param(IS_USART_ALL_PERIPH(USARTx));
  1303. assert_param(IS_USART_CLEAR_IT(USART_IT));
  1304. /* The CTS interrupt is not available for UART4 and UART5 */
  1305. if (USART_IT == USART_IT_CTS)
  1306. {
  1307. assert_param(IS_USART_1236_PERIPH(USARTx));
  1308. }
  1309. bitpos = USART_IT >> 0x08;
  1310. itmask = ((uint16_t)0x01 << (uint16_t)bitpos);
  1311. USARTx->SR = (uint16_t)~itmask;
  1312. }
  1313. /**
  1314. * @}
  1315. */
  1316. /**
  1317. * @}
  1318. */
  1319. /**
  1320. * @}
  1321. */
  1322. /**
  1323. * @}
  1324. */