stm32f4xx_usart.c 54 KB

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