stm32f0xx_usart.c 92 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169
  1. /**
  2. ******************************************************************************
  3. * @file stm32f0xx_usart.c
  4. * @author MCD Application Team
  5. * @version V1.5.0
  6. * @date 05-December-2014
  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. * + STOP Mode
  12. * + AutoBaudRate
  13. * + Data transfers
  14. * + Multi-Processor Communication
  15. * + LIN mode
  16. * + Half-duplex mode
  17. * + Smartcard mode
  18. * + IrDA mode
  19. * + RS485 mode
  20. * + DMA transfers management
  21. * + Interrupts and flags management
  22. *
  23. * @verbatim
  24. ===============================================================================
  25. ##### How to use this driver #####
  26. ===============================================================================
  27. [..]
  28. (#) Enable peripheral clock using RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE)
  29. function for USART1 or using RCC_APB1PeriphClockCmd(RCC_APB1Periph_USARTx, ENABLE)
  30. function for USART2 and USART3.
  31. (#) According to the USART mode, enable the GPIO clocks using
  32. RCC_AHBPeriphClockCmd() function. (The I/O can be TX, RX, CTS,
  33. or and SCLK).
  34. (#) Peripheral's alternate function:
  35. (++) Connect the pin to the desired peripherals' Alternate
  36. Function (AF) using GPIO_PinAFConfig() function.
  37. (++) Configure the desired pin in alternate function by:
  38. GPIO_InitStruct->GPIO_Mode = GPIO_Mode_AF.
  39. (++) Select the type, pull-up/pull-down and output speed via
  40. GPIO_PuPd, GPIO_OType and GPIO_Speed members.
  41. (++) Call GPIO_Init() function.
  42. (#) Program the Baud Rate, Word Length , Stop Bit, Parity, Hardware
  43. flow control and Mode(Receiver/Transmitter) using the SPI_Init()
  44. function.
  45. (#) For synchronous mode, enable the clock and program the polarity,
  46. phase and last bit using the USART_ClockInit() function.
  47. (#) Enable the NVIC and the corresponding interrupt using the function
  48. USART_ITConfig() if you need to use interrupt mode.
  49. (#) When using the DMA mode:
  50. (++) Configure the DMA using DMA_Init() function.
  51. (++) Active the needed channel Request using USART_DMACmd() function.
  52. (#) Enable the USART using the USART_Cmd() function.
  53. (#) Enable the DMA using the DMA_Cmd() function, when using DMA mode.
  54. [..]
  55. Refer to Multi-Processor, LIN, half-duplex, Smartcard, IrDA sub-sections
  56. for more details.
  57. @endverbatim
  58. ******************************************************************************
  59. * @attention
  60. *
  61. * <h2><center>&copy; COPYRIGHT 2014 STMicroelectronics</center></h2>
  62. *
  63. * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
  64. * You may not use this file except in compliance with the License.
  65. * You may obtain a copy of the License at:
  66. *
  67. * http://www.st.com/software_license_agreement_liberty_v2
  68. *
  69. * Unless required by applicable law or agreed to in writing, software
  70. * distributed under the License is distributed on an "AS IS" BASIS,
  71. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  72. * See the License for the specific language governing permissions and
  73. * limitations under the License.
  74. *
  75. ******************************************************************************
  76. */
  77. /* Includes ------------------------------------------------------------------*/
  78. #include "stm32f0xx_usart.h"
  79. #include "stm32f0xx_rcc.h"
  80. /** @addtogroup STM32F0xx_StdPeriph_Driver
  81. * @{
  82. */
  83. /** @defgroup USART
  84. * @brief USART driver modules
  85. * @{
  86. */
  87. /* Private typedef -----------------------------------------------------------*/
  88. /* Private define ------------------------------------------------------------*/
  89. /*!< USART CR1 register clear Mask ((~(uint32_t)0xFFFFE6F3)) */
  90. #define CR1_CLEAR_MASK ((uint32_t)(USART_CR1_M | USART_CR1_PCE | \
  91. USART_CR1_PS | USART_CR1_TE | \
  92. USART_CR1_RE))
  93. /*!< USART CR2 register clock bits clear Mask ((~(uint32_t)0xFFFFF0FF)) */
  94. #define CR2_CLOCK_CLEAR_MASK ((uint32_t)(USART_CR2_CLKEN | USART_CR2_CPOL | \
  95. USART_CR2_CPHA | USART_CR2_LBCL))
  96. /*!< USART CR3 register clear Mask ((~(uint32_t)0xFFFFFCFF)) */
  97. #define CR3_CLEAR_MASK ((uint32_t)(USART_CR3_RTSE | USART_CR3_CTSE))
  98. /*!< USART Interrupts mask */
  99. #define IT_MASK ((uint32_t)0x000000FF)
  100. /* Private macro -------------------------------------------------------------*/
  101. /* Private variables ---------------------------------------------------------*/
  102. /* Private function prototypes -----------------------------------------------*/
  103. /* Private functions ---------------------------------------------------------*/
  104. /** @defgroup USART_Private_Functions
  105. * @{
  106. */
  107. /** @defgroup USART_Group1 Initialization and Configuration functions
  108. * @brief Initialization and Configuration functions
  109. *
  110. @verbatim
  111. ===============================================================================
  112. ##### Initialization and Configuration functions #####
  113. ===============================================================================
  114. [..]
  115. This subsection provides a set of functions allowing to initialize the USART
  116. in asynchronous and in synchronous modes.
  117. (+) For the asynchronous mode only these parameters can be configured:
  118. (++) Baud Rate.
  119. (++) Word Length.
  120. (++) Stop Bit.
  121. (++) Parity: If the parity is enabled, then the MSB bit of the data written
  122. in the data register is transmitted but is changed by the parity bit.
  123. Depending on the frame length defined by the M bit (8-bits or 9-bits),
  124. the possible USART frame formats are as listed in the following table:
  125. +-------------------------------------------------------------+
  126. | M bit | PCE bit | USART frame |
  127. |---------------------|---------------------------------------|
  128. | 0 | 0 | | SB | 8 bit data | STB | |
  129. |---------|-----------|---------------------------------------|
  130. | 0 | 1 | | SB | 7 bit data | PB | STB | |
  131. |---------|-----------|---------------------------------------|
  132. | 1 | 0 | | SB | 9 bit data | STB | |
  133. |---------|-----------|---------------------------------------|
  134. | 1 | 1 | | SB | 8 bit data | PB | STB | |
  135. +-------------------------------------------------------------+
  136. (++) Hardware flow control.
  137. (++) Receiver/transmitter modes.
  138. [..] The USART_Init() function follows the USART asynchronous configuration
  139. procedure(details for the procedure are available in reference manual.
  140. (+) For the synchronous mode in addition to the asynchronous mode parameters
  141. these parameters should be also configured:
  142. (++) USART Clock Enabled.
  143. (++) USART polarity.
  144. (++) USART phase.
  145. (++) USART LastBit.
  146. [..] These parameters can be configured using the USART_ClockInit() function.
  147. @endverbatim
  148. * @{
  149. */
  150. /**
  151. * @brief Deinitializes the USARTx peripheral registers to their default reset values.
  152. * @param USARTx: where x can be from 1 to 8 to select the USART peripheral.
  153. * @note USART3 and USART4 are available only for STM32F072 and STM32F091 devices.
  154. * @note USART5, USART6, USART7 and USART8 are available only for STM32F091 devices.
  155. * @note USART2 is not available for STM32F031 devices.
  156. * @retval None
  157. */
  158. void USART_DeInit(USART_TypeDef* USARTx)
  159. {
  160. /* Check the parameters */
  161. assert_param(IS_USART_ALL_PERIPH(USARTx));
  162. if (USARTx == USART1)
  163. {
  164. RCC_APB2PeriphResetCmd(RCC_APB2Periph_USART1, ENABLE);
  165. RCC_APB2PeriphResetCmd(RCC_APB2Periph_USART1, DISABLE);
  166. }
  167. else if (USARTx == USART2)
  168. {
  169. RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART2, ENABLE);
  170. RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART2, DISABLE);
  171. }
  172. else if (USARTx == USART3)
  173. {
  174. RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART3, ENABLE);
  175. RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART3, DISABLE);
  176. }
  177. else if (USARTx == USART4)
  178. {
  179. RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART4, ENABLE);
  180. RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART4, DISABLE);
  181. }
  182. else if (USARTx == USART5)
  183. {
  184. RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART5, ENABLE);
  185. RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART5, DISABLE);
  186. }
  187. else if (USARTx == USART6)
  188. {
  189. RCC_APB2PeriphResetCmd(RCC_APB2Periph_USART6, ENABLE);
  190. RCC_APB2PeriphResetCmd(RCC_APB2Periph_USART6, DISABLE);
  191. }
  192. else if (USARTx == USART7)
  193. {
  194. RCC_APB2PeriphResetCmd(RCC_APB2Periph_USART7, ENABLE);
  195. RCC_APB2PeriphResetCmd(RCC_APB2Periph_USART7, DISABLE);
  196. }
  197. else
  198. {
  199. if (USARTx == USART8)
  200. {
  201. RCC_APB2PeriphResetCmd(RCC_APB2Periph_USART8, ENABLE);
  202. RCC_APB2PeriphResetCmd(RCC_APB2Periph_USART8, DISABLE);
  203. }
  204. }
  205. }
  206. /**
  207. * @brief Initializes the USARTx peripheral according to the specified
  208. * parameters in the USART_InitStruct .
  209. * @param USARTx: where x can be from 1 to 8 to select the USART peripheral.
  210. * @note USART3 and USART4 are available only for STM32F072 and STM32F091 devices.
  211. * @note USART5, USART6, USART7 and USART8 are available only for STM32F091 devices.
  212. * @note USART2 is not available for STM32F031 devices.
  213. * @param USART_InitStruct: pointer to a USART_InitTypeDef structure that contains
  214. * the configuration information for the specified USART peripheral.
  215. * @retval None
  216. */
  217. void USART_Init(USART_TypeDef* USARTx, USART_InitTypeDef* USART_InitStruct)
  218. {
  219. uint32_t divider = 0, apbclock = 0, tmpreg = 0;
  220. RCC_ClocksTypeDef RCC_ClocksStatus;
  221. /* Check the parameters */
  222. assert_param(IS_USART_ALL_PERIPH(USARTx));
  223. assert_param(IS_USART_BAUDRATE(USART_InitStruct->USART_BaudRate));
  224. assert_param(IS_USART_WORD_LENGTH(USART_InitStruct->USART_WordLength));
  225. assert_param(IS_USART_STOPBITS(USART_InitStruct->USART_StopBits));
  226. assert_param(IS_USART_PARITY(USART_InitStruct->USART_Parity));
  227. assert_param(IS_USART_MODE(USART_InitStruct->USART_Mode));
  228. assert_param(IS_USART_HARDWARE_FLOW_CONTROL(USART_InitStruct->USART_HardwareFlowControl));
  229. /* Disable USART */
  230. USARTx->CR1 &= (uint32_t)~((uint32_t)USART_CR1_UE);
  231. /*---------------------------- USART CR2 Configuration -----------------------*/
  232. tmpreg = USARTx->CR2;
  233. /* Clear STOP[13:12] bits */
  234. tmpreg &= (uint32_t)~((uint32_t)USART_CR2_STOP);
  235. /* Configure the USART Stop Bits, Clock, CPOL, CPHA and LastBit ------------*/
  236. /* Set STOP[13:12] bits according to USART_StopBits value */
  237. tmpreg |= (uint32_t)USART_InitStruct->USART_StopBits;
  238. /* Write to USART CR2 */
  239. USARTx->CR2 = tmpreg;
  240. /*---------------------------- USART CR1 Configuration -----------------------*/
  241. tmpreg = USARTx->CR1;
  242. /* Clear M, PCE, PS, TE and RE bits */
  243. tmpreg &= (uint32_t)~((uint32_t)CR1_CLEAR_MASK);
  244. /* Configure the USART Word Length, Parity and mode ----------------------- */
  245. /* Set the M bits according to USART_WordLength value */
  246. /* Set PCE and PS bits according to USART_Parity value */
  247. /* Set TE and RE bits according to USART_Mode value */
  248. tmpreg |= (uint32_t)USART_InitStruct->USART_WordLength | USART_InitStruct->USART_Parity |
  249. USART_InitStruct->USART_Mode;
  250. /* Write to USART CR1 */
  251. USARTx->CR1 = tmpreg;
  252. /*---------------------------- USART CR3 Configuration -----------------------*/
  253. tmpreg = USARTx->CR3;
  254. /* Clear CTSE and RTSE bits */
  255. tmpreg &= (uint32_t)~((uint32_t)CR3_CLEAR_MASK);
  256. /* Configure the USART HFC -------------------------------------------------*/
  257. /* Set CTSE and RTSE bits according to USART_HardwareFlowControl value */
  258. tmpreg |= USART_InitStruct->USART_HardwareFlowControl;
  259. /* Write to USART CR3 */
  260. USARTx->CR3 = tmpreg;
  261. /*---------------------------- USART BRR Configuration -----------------------*/
  262. /* Configure the USART Baud Rate -------------------------------------------*/
  263. RCC_GetClocksFreq(&RCC_ClocksStatus);
  264. if (USARTx == USART1)
  265. {
  266. apbclock = RCC_ClocksStatus.USART1CLK_Frequency;
  267. }
  268. else if (USARTx == USART2)
  269. {
  270. apbclock = RCC_ClocksStatus.USART2CLK_Frequency;
  271. }
  272. else if (USARTx == USART3)
  273. {
  274. apbclock = RCC_ClocksStatus.USART3CLK_Frequency;
  275. }
  276. else
  277. {
  278. apbclock = RCC_ClocksStatus.PCLK_Frequency;
  279. }
  280. /* Determine the integer part */
  281. if ((USARTx->CR1 & USART_CR1_OVER8) != 0)
  282. {
  283. /* (divider * 10) computing in case Oversampling mode is 8 Samples */
  284. divider = (uint32_t)((2 * apbclock) / (USART_InitStruct->USART_BaudRate));
  285. tmpreg = (uint32_t)((2 * apbclock) % (USART_InitStruct->USART_BaudRate));
  286. }
  287. else /* if ((USARTx->CR1 & CR1_OVER8_Set) == 0) */
  288. {
  289. /* (divider * 10) computing in case Oversampling mode is 16 Samples */
  290. divider = (uint32_t)((apbclock) / (USART_InitStruct->USART_BaudRate));
  291. tmpreg = (uint32_t)((apbclock) % (USART_InitStruct->USART_BaudRate));
  292. }
  293. /* round the divider : if fractional part i greater than 0.5 increment divider */
  294. if (tmpreg >= (USART_InitStruct->USART_BaudRate) / 2)
  295. {
  296. divider++;
  297. }
  298. /* Implement the divider in case Oversampling mode is 8 Samples */
  299. if ((USARTx->CR1 & USART_CR1_OVER8) != 0)
  300. {
  301. /* get the LSB of divider and shift it to the right by 1 bit */
  302. tmpreg = (divider & (uint16_t)0x000F) >> 1;
  303. /* update the divider value */
  304. divider = (divider & (uint16_t)0xFFF0) | tmpreg;
  305. }
  306. /* Write to USART BRR */
  307. USARTx->BRR = (uint16_t)divider;
  308. }
  309. /**
  310. * @brief Fills each USART_InitStruct member with its default value.
  311. * @param USART_InitStruct: pointer to a USART_InitTypeDef structure
  312. * which will be initialized.
  313. * @retval None
  314. */
  315. void USART_StructInit(USART_InitTypeDef* USART_InitStruct)
  316. {
  317. /* USART_InitStruct members default value */
  318. USART_InitStruct->USART_BaudRate = 9600;
  319. USART_InitStruct->USART_WordLength = USART_WordLength_8b;
  320. USART_InitStruct->USART_StopBits = USART_StopBits_1;
  321. USART_InitStruct->USART_Parity = USART_Parity_No ;
  322. USART_InitStruct->USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
  323. USART_InitStruct->USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  324. }
  325. /**
  326. * @brief Initializes the USARTx peripheral Clock according to the
  327. * specified parameters in the USART_ClockInitStruct.
  328. * @param USARTx: where x can be from 1 to 8 to select the USART peripheral.
  329. * @note USART3 and USART4 are available only for STM32F072 and STM32F091 devices.
  330. * @note USART5, USART6, USART7 and USART8 are available only for STM32F091 devices.
  331. * @note USART2 is not available for STM32F031 devices.
  332. * @param USART_ClockInitStruct: pointer to a USART_ClockInitTypeDef
  333. * structure that contains the configuration information for the specified
  334. * USART peripheral.
  335. * @retval None
  336. */
  337. void USART_ClockInit(USART_TypeDef* USARTx, USART_ClockInitTypeDef* USART_ClockInitStruct)
  338. {
  339. uint32_t tmpreg = 0;
  340. /* Check the parameters */
  341. assert_param(IS_USART_ALL_PERIPH(USARTx));
  342. assert_param(IS_USART_CLOCK(USART_ClockInitStruct->USART_Clock));
  343. assert_param(IS_USART_CPOL(USART_ClockInitStruct->USART_CPOL));
  344. assert_param(IS_USART_CPHA(USART_ClockInitStruct->USART_CPHA));
  345. assert_param(IS_USART_LASTBIT(USART_ClockInitStruct->USART_LastBit));
  346. /*---------------------------- USART CR2 Configuration -----------------------*/
  347. tmpreg = USARTx->CR2;
  348. /* Clear CLKEN, CPOL, CPHA, LBCL and SSM bits */
  349. tmpreg &= (uint32_t)~((uint32_t)CR2_CLOCK_CLEAR_MASK);
  350. /* Configure the USART Clock, CPOL, CPHA, LastBit and SSM ------------*/
  351. /* Set CLKEN bit according to USART_Clock value */
  352. /* Set CPOL bit according to USART_CPOL value */
  353. /* Set CPHA bit according to USART_CPHA value */
  354. /* Set LBCL bit according to USART_LastBit value */
  355. tmpreg |= (uint32_t)(USART_ClockInitStruct->USART_Clock | USART_ClockInitStruct->USART_CPOL |
  356. USART_ClockInitStruct->USART_CPHA | USART_ClockInitStruct->USART_LastBit);
  357. /* Write to USART CR2 */
  358. USARTx->CR2 = tmpreg;
  359. }
  360. /**
  361. * @brief Fills each USART_ClockInitStruct member with its default value.
  362. * @param USART_ClockInitStruct: pointer to a USART_ClockInitTypeDef
  363. * structure which will be initialized.
  364. * @retval None
  365. */
  366. void USART_ClockStructInit(USART_ClockInitTypeDef* USART_ClockInitStruct)
  367. {
  368. /* USART_ClockInitStruct members default value */
  369. USART_ClockInitStruct->USART_Clock = USART_Clock_Disable;
  370. USART_ClockInitStruct->USART_CPOL = USART_CPOL_Low;
  371. USART_ClockInitStruct->USART_CPHA = USART_CPHA_1Edge;
  372. USART_ClockInitStruct->USART_LastBit = USART_LastBit_Disable;
  373. }
  374. /**
  375. * @brief Enables or disables the specified USART peripheral.
  376. * @param USARTx: where x can be from 1 to 8 to select the USART peripheral.
  377. * @note USART3 and USART4 are available only for STM32F072 and STM32F091 devices.
  378. * @note USART5, USART6, USART7 and USART8 are available only for STM32F091 devices.
  379. * @note USART2 is not available for STM32F031 devices.
  380. * @param NewState: new state of the USARTx peripheral.
  381. * This parameter can be: ENABLE or DISABLE.
  382. * @retval None
  383. */
  384. void USART_Cmd(USART_TypeDef* USARTx, FunctionalState NewState)
  385. {
  386. /* Check the parameters */
  387. assert_param(IS_USART_ALL_PERIPH(USARTx));
  388. assert_param(IS_FUNCTIONAL_STATE(NewState));
  389. if (NewState != DISABLE)
  390. {
  391. /* Enable the selected USART by setting the UE bit in the CR1 register */
  392. USARTx->CR1 |= USART_CR1_UE;
  393. }
  394. else
  395. {
  396. /* Disable the selected USART by clearing the UE bit in the CR1 register */
  397. USARTx->CR1 &= (uint32_t)~((uint32_t)USART_CR1_UE);
  398. }
  399. }
  400. /**
  401. * @brief Enables or disables the USART's transmitter or receiver.
  402. * @param USARTx: where x can be from 1 to 8 to select the USART peripheral.
  403. * @note USART3 and USART4 are available only for STM32F072 and STM32F091 devices.
  404. * @note USART5, USART6, USART7 and USART8 are available only for STM32F091 devices.
  405. * @note USART2 is not available for STM32F031 devices.
  406. * @param USART_Direction: specifies the USART direction.
  407. * This parameter can be any combination of the following values:
  408. * @arg USART_Mode_Tx: USART Transmitter
  409. * @arg USART_Mode_Rx: USART Receiver
  410. * @param NewState: new state of the USART transfer direction.
  411. * This parameter can be: ENABLE or DISABLE.
  412. * @retval None
  413. */
  414. void USART_DirectionModeCmd(USART_TypeDef* USARTx, uint32_t USART_DirectionMode, FunctionalState NewState)
  415. {
  416. /* Check the parameters */
  417. assert_param(IS_USART_ALL_PERIPH(USARTx));
  418. assert_param(IS_USART_MODE(USART_DirectionMode));
  419. assert_param(IS_FUNCTIONAL_STATE(NewState));
  420. if (NewState != DISABLE)
  421. {
  422. /* Enable the USART's transfer interface by setting the TE and/or RE bits
  423. in the USART CR1 register */
  424. USARTx->CR1 |= USART_DirectionMode;
  425. }
  426. else
  427. {
  428. /* Disable the USART's transfer interface by clearing the TE and/or RE bits
  429. in the USART CR3 register */
  430. USARTx->CR1 &= (uint32_t)~USART_DirectionMode;
  431. }
  432. }
  433. /**
  434. * @brief Enables or disables the USART's 8x oversampling mode.
  435. * @param USARTx: where x can be from 1 to 8 to select the USART peripheral.
  436. * @note USART3 and USART4 are available only for STM32F072 and STM32F091 devices.
  437. * @note USART5, USART6, USART7 and USART8 are available only for STM32F091 devices.
  438. * @note USART2 is not available for STM32F031 devices.
  439. * @param NewState: new state of the USART 8x oversampling mode.
  440. * This parameter can be: ENABLE or DISABLE.
  441. * @note This function has to be called before calling USART_Init() function
  442. * in order to have correct baudrate Divider value.
  443. * @retval None
  444. */
  445. void USART_OverSampling8Cmd(USART_TypeDef* USARTx, FunctionalState NewState)
  446. {
  447. /* Check the parameters */
  448. assert_param(IS_USART_ALL_PERIPH(USARTx));
  449. assert_param(IS_FUNCTIONAL_STATE(NewState));
  450. if (NewState != DISABLE)
  451. {
  452. /* Enable the 8x Oversampling mode by setting the OVER8 bit in the CR1 register */
  453. USARTx->CR1 |= USART_CR1_OVER8;
  454. }
  455. else
  456. {
  457. /* Disable the 8x Oversampling mode by clearing the OVER8 bit in the CR1 register */
  458. USARTx->CR1 &= (uint32_t)~((uint32_t)USART_CR1_OVER8);
  459. }
  460. }
  461. /**
  462. * @brief Enables or disables the USART's one bit sampling method.
  463. * @param USARTx: where x can be from 1 to 8 to select the USART peripheral.
  464. * @note USART3 and USART4 are available only for STM32F072 and STM32F091 devices.
  465. * @note USART5, USART6, USART7 and USART8 are available only for STM32F091 devices.
  466. * @note USART2 is not available for STM32F031 devices.
  467. * @param NewState: new state of the USART one bit sampling method.
  468. * This parameter can be: ENABLE or DISABLE.
  469. * @note This function has to be called before calling USART_Cmd() function.
  470. * @retval None
  471. */
  472. void USART_OneBitMethodCmd(USART_TypeDef* USARTx, FunctionalState NewState)
  473. {
  474. /* Check the parameters */
  475. assert_param(IS_USART_ALL_PERIPH(USARTx));
  476. assert_param(IS_FUNCTIONAL_STATE(NewState));
  477. if (NewState != DISABLE)
  478. {
  479. /* Enable the one bit method by setting the ONEBITE bit in the CR3 register */
  480. USARTx->CR3 |= USART_CR3_ONEBIT;
  481. }
  482. else
  483. {
  484. /* Disable the one bit method by clearing the ONEBITE bit in the CR3 register */
  485. USARTx->CR3 &= (uint32_t)~((uint32_t)USART_CR3_ONEBIT);
  486. }
  487. }
  488. /**
  489. * @brief Enables or disables the USART's most significant bit first
  490. * transmitted/received following the start bit.
  491. * @param USARTx: where x can be from 1 to 8 to select the USART peripheral.
  492. * @note USART3 and USART4 are available only for STM32F072 and STM32F091 devices.
  493. * @note USART5, USART6, USART7 and USART8 are available only for STM32F091 devices.
  494. * @note USART2 is not available for STM32F031 devices.
  495. * @param NewState: new state of the USART most significant bit first
  496. * transmitted/received following the start bit.
  497. * This parameter can be: ENABLE or DISABLE.
  498. * @note This function has to be called before calling USART_Cmd() function.
  499. * @retval None
  500. */
  501. void USART_MSBFirstCmd(USART_TypeDef* USARTx, FunctionalState NewState)
  502. {
  503. /* Check the parameters */
  504. assert_param(IS_USART_ALL_PERIPH(USARTx));
  505. assert_param(IS_FUNCTIONAL_STATE(NewState));
  506. if (NewState != DISABLE)
  507. {
  508. /* Enable the most significant bit first transmitted/received following the
  509. start bit by setting the MSBFIRST bit in the CR2 register */
  510. USARTx->CR2 |= USART_CR2_MSBFIRST;
  511. }
  512. else
  513. {
  514. /* Disable the most significant bit first transmitted/received following the
  515. start bit by clearing the MSBFIRST bit in the CR2 register */
  516. USARTx->CR2 &= (uint32_t)~((uint32_t)USART_CR2_MSBFIRST);
  517. }
  518. }
  519. /**
  520. * @brief Enables or disables the binary data inversion.
  521. * @param USARTx: where x can be from 1 to 8 to select the USART peripheral.
  522. * @note USART3 and USART4 are available only for STM32F072 and STM32F091 devices.
  523. * @note USART5, USART6, USART7 and USART8 are available only for STM32F091 devices.
  524. * @note USART2 is not available for STM32F031 devices.
  525. * @param NewState: new defined levels for the USART data.
  526. * This parameter can be:
  527. * @arg ENABLE: Logical data from the data register are send/received in negative
  528. * logic (1=L, 0=H). The parity bit is also inverted.
  529. * @arg DISABLE: Logical data from the data register are send/received in positive
  530. * logic (1=H, 0=L)
  531. * @note This function has to be called before calling USART_Cmd() function.
  532. * @retval None
  533. */
  534. void USART_DataInvCmd(USART_TypeDef* USARTx, FunctionalState NewState)
  535. {
  536. /* Check the parameters */
  537. assert_param(IS_USART_ALL_PERIPH(USARTx));
  538. assert_param(IS_FUNCTIONAL_STATE(NewState));
  539. if (NewState != DISABLE)
  540. {
  541. /* Enable the binary data inversion feature by setting the DATAINV bit in
  542. the CR2 register */
  543. USARTx->CR2 |= USART_CR2_DATAINV;
  544. }
  545. else
  546. {
  547. /* Disable the binary data inversion feature by clearing the DATAINV bit in
  548. the CR2 register */
  549. USARTx->CR2 &= (uint32_t)~((uint32_t)USART_CR2_DATAINV);
  550. }
  551. }
  552. /**
  553. * @brief Enables or disables the Pin(s) active level inversion.
  554. * @param USARTx: where x can be from 1 to 8 to select the USART peripheral.
  555. * @note USART3 and USART4 are available only for STM32F072 and STM32F091 devices.
  556. * @note USART5, USART6, USART7 and USART8 are available only for STM32F091 devices.
  557. * @note USART2 is not available for STM32F031 devices.
  558. * @param USART_InvPin: specifies the USART pin(s) to invert.
  559. * This parameter can be any combination of the following values:
  560. * @arg USART_InvPin_Tx: USART Tx pin active level inversion.
  561. * @arg USART_InvPin_Rx: USART Rx pin active level inversion.
  562. * @param NewState: new active level status for the USART pin(s).
  563. * This parameter can be:
  564. * @arg ENABLE: pin(s) signal values are inverted (Vdd =0, Gnd =1).
  565. * @arg DISABLE: pin(s) signal works using the standard logic levels (Vdd =1, Gnd =0).
  566. * @note This function has to be called before calling USART_Cmd() function.
  567. * @retval None
  568. */
  569. void USART_InvPinCmd(USART_TypeDef* USARTx, uint32_t USART_InvPin, FunctionalState NewState)
  570. {
  571. /* Check the parameters */
  572. assert_param(IS_USART_ALL_PERIPH(USARTx));
  573. assert_param(IS_USART_INVERSTION_PIN(USART_InvPin));
  574. assert_param(IS_FUNCTIONAL_STATE(NewState));
  575. if (NewState != DISABLE)
  576. {
  577. /* Enable the active level inversion for selected pins by setting the TXINV
  578. and/or RXINV bits in the USART CR2 register */
  579. USARTx->CR2 |= USART_InvPin;
  580. }
  581. else
  582. {
  583. /* Disable the active level inversion for selected requests by clearing the
  584. TXINV and/or RXINV bits in the USART CR2 register */
  585. USARTx->CR2 &= (uint32_t)~USART_InvPin;
  586. }
  587. }
  588. /**
  589. * @brief Enables or disables the swap Tx/Rx pins.
  590. * @param USARTx: where x can be from 1 to 8 to select the USART peripheral.
  591. * @note USART3 and USART4 are available only for STM32F072 and STM32F091 devices.
  592. * @note USART5, USART6, USART7 and USART8 are available only for STM32F091 devices.
  593. * @note USART2 is not available for STM32F031 devices.
  594. * @param NewState: new state of the USARTx TX/RX pins pinout.
  595. * This parameter can be:
  596. * @arg ENABLE: The TX and RX pins functions are swapped.
  597. * @arg DISABLE: TX/RX pins are used as defined in standard pinout
  598. * @note This function has to be called before calling USART_Cmd() function.
  599. * @retval None
  600. */
  601. void USART_SWAPPinCmd(USART_TypeDef* USARTx, FunctionalState NewState)
  602. {
  603. /* Check the parameters */
  604. assert_param(IS_USART_ALL_PERIPH(USARTx));
  605. assert_param(IS_FUNCTIONAL_STATE(NewState));
  606. if (NewState != DISABLE)
  607. {
  608. /* Enable the SWAP feature by setting the SWAP bit in the CR2 register */
  609. USARTx->CR2 |= USART_CR2_SWAP;
  610. }
  611. else
  612. {
  613. /* Disable the SWAP feature by clearing the SWAP bit in the CR2 register */
  614. USARTx->CR2 &= (uint32_t)~((uint32_t)USART_CR2_SWAP);
  615. }
  616. }
  617. /**
  618. * @brief Enables or disables the receiver Time Out feature.
  619. * @param USARTx: where x can be 1, 2 or 3 to select the USART peripheral.
  620. * @note USART2 is available only for STM32F072 and STM32F091 devices.
  621. * @note USART3 is available only for STM32F091 devices.
  622. * @param NewState: new state of the USARTx receiver Time Out.
  623. * This parameter can be: ENABLE or DISABLE.
  624. * @retval None
  625. */
  626. void USART_ReceiverTimeOutCmd(USART_TypeDef* USARTx, FunctionalState NewState)
  627. {
  628. /* Check the parameters */
  629. assert_param(IS_USART_123_PERIPH(USARTx));
  630. assert_param(IS_FUNCTIONAL_STATE(NewState));
  631. if (NewState != DISABLE)
  632. {
  633. /* Enable the receiver time out feature by setting the RTOEN bit in the CR2
  634. register */
  635. USARTx->CR2 |= USART_CR2_RTOEN;
  636. }
  637. else
  638. {
  639. /* Disable the receiver time out feature by clearing the RTOEN bit in the CR2
  640. register */
  641. USARTx->CR2 &= (uint32_t)~((uint32_t)USART_CR2_RTOEN);
  642. }
  643. }
  644. /**
  645. * @brief Sets the receiver Time Out value.
  646. * @param USARTx: where x can be 1, 2 or 3 to select the USART peripheral.
  647. * @note USART2 is available only for STM32F072 and STM32F091 devices.
  648. * @note USART3 is available only for STM32F091 devices.
  649. * @param USART_ReceiverTimeOut: specifies the Receiver Time Out value.
  650. * @retval None
  651. */
  652. void USART_SetReceiverTimeOut(USART_TypeDef* USARTx, uint32_t USART_ReceiverTimeOut)
  653. {
  654. /* Check the parameters */
  655. assert_param(IS_USART_123_PERIPH(USARTx));
  656. assert_param(IS_USART_TIMEOUT(USART_ReceiverTimeOut));
  657. /* Clear the receiver Time Out value by clearing the RTO[23:0] bits in the RTOR
  658. register */
  659. USARTx->RTOR &= (uint32_t)~((uint32_t)USART_RTOR_RTO);
  660. /* Set the receiver Time Out value by setting the RTO[23:0] bits in the RTOR
  661. register */
  662. USARTx->RTOR |= USART_ReceiverTimeOut;
  663. }
  664. /**
  665. * @brief Sets the system clock prescaler.
  666. * @note This function is not available for STM32F030 devices.
  667. * @param USARTx: where x can be 1, 2 or 3 to select the USART peripheral.
  668. * @note USART2 is available only for STM32F072 and STM32F091 devices.
  669. * @note USART3 is available only for STM32F091 devices.
  670. * @param USART_Prescaler: specifies the prescaler clock.
  671. * @note This function has to be called before calling USART_Cmd() function.
  672. * @retval None
  673. */
  674. void USART_SetPrescaler(USART_TypeDef* USARTx, uint8_t USART_Prescaler)
  675. {
  676. /* Check the parameters */
  677. assert_param(IS_USART_123_PERIPH(USARTx));
  678. /* Clear the USART prescaler */
  679. USARTx->GTPR &= USART_GTPR_GT;
  680. /* Set the USART prescaler */
  681. USARTx->GTPR |= USART_Prescaler;
  682. }
  683. /**
  684. * @}
  685. */
  686. /** @defgroup USART_Group2 STOP Mode functions
  687. * @brief STOP Mode functions
  688. *
  689. @verbatim
  690. ===============================================================================
  691. ##### STOP Mode functions #####
  692. ===============================================================================
  693. [..] This subsection provides a set of functions allowing to manage
  694. WakeUp from STOP mode.
  695. [..] The USART is able to WakeUp from Stop Mode if USART clock is set to HSI
  696. or LSI.
  697. [..] The WakeUp source is configured by calling USART_StopModeWakeUpSourceConfig()
  698. function.
  699. [..] After configuring the source of WakeUp and before entering in Stop Mode
  700. USART_STOPModeCmd() function should be called to allow USART WakeUp.
  701. @endverbatim
  702. * @{
  703. */
  704. /**
  705. * @brief Enables or disables the specified USART peripheral in STOP Mode.
  706. * @param USARTx: where x can be 1 or 2 or 3 to select the USART peripheral.
  707. * @note USART2 is available only for STM32F072 and STM32F091 devices.
  708. * @note USART3 is available only for STM32F091 devices.
  709. * @param NewState: new state of the USARTx peripheral state in stop mode.
  710. * This parameter can be: ENABLE or DISABLE.
  711. * @note This function has to be called when USART clock is set to HSI or LSE.
  712. * @retval None
  713. */
  714. void USART_STOPModeCmd(USART_TypeDef* USARTx, FunctionalState NewState)
  715. {
  716. /* Check the parameters */
  717. assert_param(IS_USART_123_PERIPH(USARTx));
  718. assert_param(IS_FUNCTIONAL_STATE(NewState));
  719. if (NewState != DISABLE)
  720. {
  721. /* Enable the selected USART in STOP mode by setting the UESM bit in the CR1
  722. register */
  723. USARTx->CR1 |= USART_CR1_UESM;
  724. }
  725. else
  726. {
  727. /* Disable the selected USART in STOP mode by clearing the UE bit in the CR1
  728. register */
  729. USARTx->CR1 &= (uint32_t)~((uint32_t)USART_CR1_UESM);
  730. }
  731. }
  732. /**
  733. * @brief Selects the USART WakeUp method form stop mode.
  734. * @note This function is not available for STM32F030 devices.
  735. * @param USARTx: where x can be 1 or 2 or 3 to select the USART peripheral.
  736. * @note USART2 is available only for STM32F072 and STM32F091 devices.
  737. * @note USART3 is available only for STM32F091 devices.
  738. * @param USART_WakeUp: specifies the selected USART wakeup method.
  739. * This parameter can be one of the following values:
  740. * @arg USART_WakeUpSource_AddressMatch: WUF active on address match.
  741. * @arg USART_WakeUpSource_StartBit: WUF active on Start bit detection.
  742. * @arg USART_WakeUpSource_RXNE: WUF active on RXNE.
  743. * @note This function has to be called before calling USART_Cmd() function.
  744. * @retval None
  745. */
  746. void USART_StopModeWakeUpSourceConfig(USART_TypeDef* USARTx, uint32_t USART_WakeUpSource)
  747. {
  748. /* Check the parameters */
  749. assert_param(IS_USART_123_PERIPH(USARTx));
  750. assert_param(IS_USART_STOPMODE_WAKEUPSOURCE(USART_WakeUpSource));
  751. USARTx->CR3 &= (uint32_t)~((uint32_t)USART_CR3_WUS);
  752. USARTx->CR3 |= USART_WakeUpSource;
  753. }
  754. /**
  755. * @}
  756. */
  757. /** @defgroup USART_Group3 AutoBaudRate functions
  758. * @brief AutoBaudRate functions
  759. *
  760. @verbatim
  761. ===============================================================================
  762. ##### AutoBaudRate functions #####
  763. ===============================================================================
  764. [..] This subsection provides a set of functions allowing to manage
  765. the AutoBaudRate detections.
  766. [..] Before Enabling AutoBaudRate detection using USART_AutoBaudRateCmd ()
  767. The character patterns used to calculate baudrate must be chosen by calling
  768. USART_AutoBaudRateConfig() function. These function take as parameter :
  769. (#)USART_AutoBaudRate_StartBit : any character starting with a bit 1.
  770. (#)USART_AutoBaudRate_FallingEdge : any character starting with a 10xx bit pattern.
  771. [..] At any later time, another request for AutoBaudRate detection can be performed
  772. using USART_RequestCmd() function.
  773. [..] The AutoBaudRate detection is monitored by the status of ABRF flag which indicate
  774. that the AutoBaudRate detection is completed. In addition to ABRF flag, the ABRE flag
  775. indicate that this procedure is completed without success. USART_GetFlagStatus ()
  776. function should be used to monitor the status of these flags.
  777. @endverbatim
  778. * @{
  779. */
  780. /**
  781. * @brief Enables or disables the Auto Baud Rate.
  782. * @param USARTx: where x can be 1or 2 to select the USART peripheral.
  783. * @note USART2 is available only for STM32F072 and STM32F091 devices.
  784. * @note USART3 is available only for STM32F091 devices.
  785. * @param NewState: new state of the USARTx auto baud rate.
  786. * This parameter can be: ENABLE or DISABLE.
  787. * @retval None
  788. */
  789. void USART_AutoBaudRateCmd(USART_TypeDef* USARTx, FunctionalState NewState)
  790. {
  791. /* Check the parameters */
  792. assert_param(IS_USART_123_PERIPH(USARTx));
  793. assert_param(IS_FUNCTIONAL_STATE(NewState));
  794. if (NewState != DISABLE)
  795. {
  796. /* Enable the auto baud rate feature by setting the ABREN bit in the CR2
  797. register */
  798. USARTx->CR2 |= USART_CR2_ABREN;
  799. }
  800. else
  801. {
  802. /* Disable the auto baud rate feature by clearing the ABREN bit in the CR2
  803. register */
  804. USARTx->CR2 &= (uint32_t)~((uint32_t)USART_CR2_ABREN);
  805. }
  806. }
  807. /**
  808. * @brief Selects the USART auto baud rate method.
  809. * @param USARTx: where x can be 1or 2 to select the USART peripheral.
  810. * @note USART2 is available only for STM32F072 and STM32F091 devices.
  811. * @note USART3 is available only for STM32F091 devices.
  812. * @param USART_AutoBaudRate: specifies the selected USART auto baud rate method.
  813. * This parameter can be one of the following values:
  814. * @arg USART_AutoBaudRate_StartBit: Start Bit duration measurement.
  815. * @arg USART_AutoBaudRate_FallingEdge: Falling edge to falling edge measurement.
  816. * @note This function has to be called before calling USART_Cmd() function.
  817. * @retval None
  818. */
  819. void USART_AutoBaudRateConfig(USART_TypeDef* USARTx, uint32_t USART_AutoBaudRate)
  820. {
  821. /* Check the parameters */
  822. assert_param(IS_USART_123_PERIPH(USARTx));
  823. assert_param(IS_USART_AUTOBAUDRATE_MODE(USART_AutoBaudRate));
  824. USARTx->CR2 &= (uint32_t)~((uint32_t)USART_CR2_ABRMODE);
  825. USARTx->CR2 |= USART_AutoBaudRate;
  826. }
  827. /**
  828. * @}
  829. */
  830. /** @defgroup USART_Group4 Data transfers functions
  831. * @brief Data transfers functions
  832. *
  833. @verbatim
  834. ===============================================================================
  835. ##### Data transfers functions #####
  836. ===============================================================================
  837. [..] This subsection provides a set of functions allowing to manage
  838. the USART data transfers.
  839. [..] During an USART reception, data shifts in least significant bit first
  840. through the RX pin. When a transmission is taking place, a write instruction to
  841. the USART_TDR register stores the data in the shift register.
  842. [..] The read access of the USART_RDR register can be done using
  843. the USART_ReceiveData() function and returns the RDR value.
  844. Whereas a write access to the USART_TDR can be done using USART_SendData()
  845. function and stores the written data into TDR.
  846. @endverbatim
  847. * @{
  848. */
  849. /**
  850. * @brief Transmits single data through the USARTx peripheral.
  851. * @param USARTx: where x can be from 1 to 8 to select the USART peripheral.
  852. * @note USART3 and USART4 are available only for STM32F072 and STM32F091 devices.
  853. * @note USART5, USART6, USART7 and USART8 are available only for STM32F091 devices.
  854. * @note USART2 is not available for STM32F031 devices.
  855. * @param Data: the data to transmit.
  856. * @retval None
  857. */
  858. void USART_SendData(USART_TypeDef* USARTx, uint16_t Data)
  859. {
  860. /* Check the parameters */
  861. assert_param(IS_USART_ALL_PERIPH(USARTx));
  862. assert_param(IS_USART_DATA(Data));
  863. /* Transmit Data */
  864. USARTx->TDR = (Data & (uint16_t)0x01FF);
  865. }
  866. /**
  867. * @brief Returns the most recent received data by the USARTx peripheral.
  868. * @param USARTx: where x can be from 1 to 8 to select the USART peripheral.
  869. * @note USART3 and USART4 are available only for STM32F072 and STM32F091 devices.
  870. * @note USART5, USART6, USART7 and USART8 are available only for STM32F091 devices.
  871. * @note USART2 is not available for STM32F031 devices.
  872. * @retval The received data.
  873. */
  874. uint16_t USART_ReceiveData(USART_TypeDef* USARTx)
  875. {
  876. /* Check the parameters */
  877. assert_param(IS_USART_ALL_PERIPH(USARTx));
  878. /* Receive Data */
  879. return (uint16_t)(USARTx->RDR & (uint16_t)0x01FF);
  880. }
  881. /**
  882. * @}
  883. */
  884. /** @defgroup USART_Group5 MultiProcessor Communication functions
  885. * @brief Multi-Processor Communication functions
  886. *
  887. @verbatim
  888. ===============================================================================
  889. ##### Multi-Processor Communication functions #####
  890. ===============================================================================
  891. [..] This subsection provides a set of functions allowing to manage the USART
  892. multiprocessor communication.
  893. [..] For instance one of the USARTs can be the master, its TX output is
  894. connected to the RX input of the other USART. The others are slaves,
  895. their respective TX outputs are logically ANDed together and connected
  896. to the RX input of the master. USART multiprocessor communication is
  897. possible through the following procedure:
  898. (#) Program the Baud rate, Word length = 9 bits, Stop bits, Parity,
  899. Mode transmitter or Mode receiver and hardware flow control values
  900. using the USART_Init() function.
  901. (#) Configures the USART address using the USART_SetAddress() function.
  902. (#) Configures the wake up methode (USART_WakeUp_IdleLine or
  903. USART_WakeUp_AddressMark) using USART_WakeUpConfig() function only
  904. for the slaves.
  905. (#) Enable the USART using the USART_Cmd() function.
  906. (#) Enter the USART slaves in mute mode using USART_ReceiverWakeUpCmd()
  907. function.
  908. [..] The USART Slave exit from mute mode when receive the wake up condition.
  909. @endverbatim
  910. * @{
  911. */
  912. /**
  913. * @brief Sets the address of the USART node.
  914. * @param USARTx: where x can be from 1 to 8 to select the USART peripheral.
  915. * @note USART3 and USART4 are available only for STM32F072 and STM32F091 devices.
  916. * @note USART5, USART6, USART7 and USART8 are available only for STM32F091 devices.
  917. * @note USART2 is not available for STM32F031 devices.
  918. * @param USART_Address: Indicates the address of the USART node.
  919. * @retval None
  920. */
  921. void USART_SetAddress(USART_TypeDef* USARTx, uint8_t USART_Address)
  922. {
  923. /* Check the parameters */
  924. assert_param(IS_USART_ALL_PERIPH(USARTx));
  925. /* Clear the USART address */
  926. USARTx->CR2 &= (uint32_t)~((uint32_t)USART_CR2_ADD);
  927. /* Set the USART address node */
  928. USARTx->CR2 |=((uint32_t)USART_Address << (uint32_t)0x18);
  929. }
  930. /**
  931. * @brief Enables or disables the USART's mute mode.
  932. * @param USARTx: where x can be from 1 to 8 to select the USART peripheral.
  933. * @note USART3 and USART4 are available only for STM32F072 and STM32F091 devices.
  934. * @note USART5, USART6, USART7 and USART8 are available only for STM32F091 devices.
  935. * @note USART2 is not available for STM32F031 devices.
  936. * @param NewState: new state of the USART mute mode.
  937. * This parameter can be: ENABLE or DISABLE.
  938. * @retval None
  939. */
  940. void USART_MuteModeCmd(USART_TypeDef* USARTx, FunctionalState NewState)
  941. {
  942. /* Check the parameters */
  943. assert_param(IS_USART_ALL_PERIPH(USARTx));
  944. assert_param(IS_FUNCTIONAL_STATE(NewState));
  945. if (NewState != DISABLE)
  946. {
  947. /* Enable the USART mute mode by setting the MME bit in the CR1 register */
  948. USARTx->CR1 |= USART_CR1_MME;
  949. }
  950. else
  951. {
  952. /* Disable the USART mute mode by clearing the MME bit in the CR1 register */
  953. USARTx->CR1 &= (uint32_t)~((uint32_t)USART_CR1_MME);
  954. }
  955. }
  956. /**
  957. * @brief Selects the USART WakeUp method from mute mode.
  958. * @param USARTx: where x can be from 1 to 8 to select the USART peripheral.
  959. * @note USART3 and USART4 are available only for STM32F072 and STM32F091 devices.
  960. * @note USART5, USART6, USART7 and USART8 are available only for STM32F091 devices.
  961. * @note USART2 is not available for STM32F031 devices.
  962. * @param USART_WakeUp: specifies the USART wakeup method.
  963. * This parameter can be one of the following values:
  964. * @arg USART_WakeUp_IdleLine: WakeUp by an idle line detection
  965. * @arg USART_WakeUp_AddressMark: WakeUp by an address mark
  966. * @retval None
  967. */
  968. void USART_MuteModeWakeUpConfig(USART_TypeDef* USARTx, uint32_t USART_WakeUp)
  969. {
  970. /* Check the parameters */
  971. assert_param(IS_USART_ALL_PERIPH(USARTx));
  972. assert_param(IS_USART_MUTEMODE_WAKEUP(USART_WakeUp));
  973. USARTx->CR1 &= (uint32_t)~((uint32_t)USART_CR1_WAKE);
  974. USARTx->CR1 |= USART_WakeUp;
  975. }
  976. /**
  977. * @brief Configure the the USART Address detection length.
  978. * @param USARTx: where x can be from 1 to 8 to select the USART peripheral.
  979. * @note USART3 and USART4 are available only for STM32F072 and STM32F091 devices.
  980. * @note USART5, USART6, USART7 and USART8 are available only for STM32F091 devices.
  981. * @note USART2 is not available for STM32F031 devices.
  982. * @param USART_AddressLength: specifies the USART address length detection.
  983. * This parameter can be one of the following values:
  984. * @arg USART_AddressLength_4b: 4-bit address length detection
  985. * @arg USART_AddressLength_7b: 7-bit address length detection
  986. * @retval None
  987. */
  988. void USART_AddressDetectionConfig(USART_TypeDef* USARTx, uint32_t USART_AddressLength)
  989. {
  990. /* Check the parameters */
  991. assert_param(IS_USART_ALL_PERIPH(USARTx));
  992. assert_param(IS_USART_ADDRESS_DETECTION(USART_AddressLength));
  993. USARTx->CR2 &= (uint32_t)~((uint32_t)USART_CR2_ADDM7);
  994. USARTx->CR2 |= USART_AddressLength;
  995. }
  996. /**
  997. * @}
  998. */
  999. /** @defgroup USART_Group6 LIN mode functions
  1000. * @brief LIN mode functions
  1001. *
  1002. @verbatim
  1003. ===============================================================================
  1004. ##### LIN mode functions #####
  1005. ===============================================================================
  1006. [..] This subsection provides a set of functions allowing to manage the USART
  1007. LIN Mode communication.
  1008. [..] In LIN mode, 8-bit data format with 1 stop bit is required in accordance
  1009. with the LIN standard.
  1010. [..] Only this LIN Feature is supported by the USART IP:
  1011. (+) LIN Master Synchronous Break send capability and LIN slave break
  1012. detection capability : 13-bit break generation and 10/11 bit break
  1013. detection.
  1014. [..] USART LIN Master transmitter communication is possible through the
  1015. following procedure:
  1016. (#) Program the Baud rate, Word length = 8bits, Stop bits = 1bit, Parity,
  1017. Mode transmitter or Mode receiver and hardware flow control values
  1018. using the USART_Init() function.
  1019. (#) Enable the LIN mode using the USART_LINCmd() function.
  1020. (#) Enable the USART using the USART_Cmd() function.
  1021. (#) Send the break character using USART_SendBreak() function.
  1022. [..] USART LIN Master receiver communication is possible through the
  1023. following procedure:
  1024. (#) Program the Baud rate, Word length = 8bits, Stop bits = 1bit, Parity,
  1025. Mode transmitter or Mode receiver and hardware flow control values
  1026. using the USART_Init() function.
  1027. (#) Configures the break detection length
  1028. using the USART_LINBreakDetectLengthConfig() function.
  1029. (#) Enable the LIN mode using the USART_LINCmd() function.
  1030. -@- In LIN mode, the following bits must be kept cleared:
  1031. (+@) CLKEN in the USART_CR2 register.
  1032. (+@) STOP[1:0], SCEN, HDSEL and IREN in the USART_CR3 register.
  1033. (#) Enable the USART using the USART_Cmd() function.
  1034. @endverbatim
  1035. * @{
  1036. */
  1037. /**
  1038. * @brief Sets the USART LIN Break detection length.
  1039. * @note This function is not available for STM32F030 devices.
  1040. * @param USARTx: where x can be 1or 2 to select the USART peripheral.
  1041. * @note USART2 is available only for STM32F072 and STM32F091 devices.
  1042. * @note USART3 is available only for STM32F091 devices.
  1043. * @param USART_LINBreakDetectLength: specifies the LIN break detection length.
  1044. * This parameter can be one of the following values:
  1045. * @arg USART_LINBreakDetectLength_10b: 10-bit break detection
  1046. * @arg USART_LINBreakDetectLength_11b: 11-bit break detection
  1047. * @retval None
  1048. */
  1049. void USART_LINBreakDetectLengthConfig(USART_TypeDef* USARTx, uint32_t USART_LINBreakDetectLength)
  1050. {
  1051. /* Check the parameters */
  1052. assert_param(IS_USART_123_PERIPH(USARTx));
  1053. assert_param(IS_USART_LIN_BREAK_DETECT_LENGTH(USART_LINBreakDetectLength));
  1054. USARTx->CR2 &= (uint32_t)~((uint32_t)USART_CR2_LBDL);
  1055. USARTx->CR2 |= USART_LINBreakDetectLength;
  1056. }
  1057. /**
  1058. * @brief Enables or disables the USART's LIN mode.
  1059. * @note This function is not available for STM32F030 devices.
  1060. * @param USARTx: where x can be 1or 2 to select the USART peripheral.
  1061. * @note USART2 is available only for STM32F072 and STM32F091 devices.
  1062. * @note USART3 is available only for STM32F091 devices.
  1063. * @param NewState: new state of the USART LIN mode.
  1064. * This parameter can be: ENABLE or DISABLE.
  1065. * @retval None
  1066. */
  1067. void USART_LINCmd(USART_TypeDef* USARTx, FunctionalState NewState)
  1068. {
  1069. /* Check the parameters */
  1070. assert_param(IS_USART_123_PERIPH(USARTx));
  1071. assert_param(IS_FUNCTIONAL_STATE(NewState));
  1072. if (NewState != DISABLE)
  1073. {
  1074. /* Enable the LIN mode by setting the LINEN bit in the CR2 register */
  1075. USARTx->CR2 |= USART_CR2_LINEN;
  1076. }
  1077. else
  1078. {
  1079. /* Disable the LIN mode by clearing the LINEN bit in the CR2 register */
  1080. USARTx->CR2 &= (uint32_t)~((uint32_t)USART_CR2_LINEN);
  1081. }
  1082. }
  1083. /**
  1084. * @}
  1085. */
  1086. /** @defgroup USART_Group7 Halfduplex mode function
  1087. * @brief Half-duplex mode function
  1088. *
  1089. @verbatim
  1090. ===============================================================================
  1091. ##### Half-duplex mode function #####
  1092. ===============================================================================
  1093. [..] This subsection provides a set of functions allowing to manage the USART
  1094. Half-duplex communication.
  1095. [..] The USART can be configured to follow a single-wire half-duplex protocol
  1096. where the TX and RX lines are internally connected.
  1097. [..] USART Half duplex communication is possible through the following procedure:
  1098. (#) Program the Baud rate, Word length, Stop bits, Parity, Mode transmitter
  1099. or Mode receiver and hardware flow control values using the USART_Init()
  1100. function.
  1101. (#) Configures the USART address using the USART_SetAddress() function.
  1102. (#) Enable the half duplex mode using USART_HalfDuplexCmd() function.
  1103. (#) Enable the USART using the USART_Cmd() function.
  1104. -@- The RX pin is no longer used.
  1105. -@- In Half-duplex mode the following bits must be kept cleared:
  1106. (+@) LINEN and CLKEN bits in the USART_CR2 register.
  1107. (+@) SCEN and IREN bits in the USART_CR3 register.
  1108. @endverbatim
  1109. * @{
  1110. */
  1111. /**
  1112. * @brief Enables or disables the USART's Half Duplex communication.
  1113. * @param USARTx: where x can be from 1 to 8 to select the USART peripheral.
  1114. * @note USART3 and USART4 are available only for STM32F072 and STM32F091 devices.
  1115. * @note USART5, USART6, USART7 and USART8 are available only for STM32F091 devices.
  1116. * @note USART2 is not available for STM32F031 devices.
  1117. * @param NewState: new state of the USART Communication.
  1118. * This parameter can be: ENABLE or DISABLE.
  1119. * @retval None
  1120. */
  1121. void USART_HalfDuplexCmd(USART_TypeDef* USARTx, FunctionalState NewState)
  1122. {
  1123. /* Check the parameters */
  1124. assert_param(IS_USART_ALL_PERIPH(USARTx));
  1125. assert_param(IS_FUNCTIONAL_STATE(NewState));
  1126. if (NewState != DISABLE)
  1127. {
  1128. /* Enable the Half-Duplex mode by setting the HDSEL bit in the CR3 register */
  1129. USARTx->CR3 |= USART_CR3_HDSEL;
  1130. }
  1131. else
  1132. {
  1133. /* Disable the Half-Duplex mode by clearing the HDSEL bit in the CR3 register */
  1134. USARTx->CR3 &= (uint32_t)~((uint32_t)USART_CR3_HDSEL);
  1135. }
  1136. }
  1137. /**
  1138. * @}
  1139. */
  1140. /** @defgroup USART_Group8 Smartcard mode functions
  1141. * @brief Smartcard mode functions
  1142. *
  1143. @verbatim
  1144. ===============================================================================
  1145. ##### Smartcard mode functions #####
  1146. ===============================================================================
  1147. [..] This subsection provides a set of functions allowing to manage the USART
  1148. Smartcard communication.
  1149. [..] The Smartcard interface is designed to support asynchronous protocol
  1150. Smartcards as defined in the ISO 7816-3 standard. The USART can provide
  1151. a clock to the smartcard through the SCLK output. In smartcard mode,
  1152. SCLK is not associated to the communication but is simply derived from
  1153. the internal peripheral input clock through a 5-bit prescaler.
  1154. [..] Smartcard communication is possible through the following procedure:
  1155. (#) Configures the Smartcard Prsecaler using the USART_SetPrescaler()
  1156. function.
  1157. (#) Configures the Smartcard Guard Time using the USART_SetGuardTime()
  1158. function.
  1159. (#) Program the USART clock using the USART_ClockInit() function as following:
  1160. (++) USART Clock enabled.
  1161. (++) USART CPOL Low.
  1162. (++) USART CPHA on first edge.
  1163. (++) USART Last Bit Clock Enabled.
  1164. (#) Program the Smartcard interface using the USART_Init() function as
  1165. following:
  1166. (++) Word Length = 9 Bits.
  1167. (++) 1.5 Stop Bit.
  1168. (++) Even parity.
  1169. (++) BaudRate = 12096 baud.
  1170. (++) Hardware flow control disabled (RTS and CTS signals).
  1171. (++) Tx and Rx enabled
  1172. (#) Optionally you can enable the parity error interrupt using
  1173. the USART_ITConfig() function.
  1174. (#) Enable the Smartcard NACK using the USART_SmartCardNACKCmd() function.
  1175. (#) Enable the Smartcard interface using the USART_SmartCardCmd() function.
  1176. (#) Enable the USART using the USART_Cmd() function.
  1177. [..]
  1178. Please refer to the ISO 7816-3 specification for more details.
  1179. [..]
  1180. (@) It is also possible to choose 0.5 stop bit for receiving but it is
  1181. recommended to use 1.5 stop bits for both transmitting and receiving
  1182. to avoid switching between the two configurations.
  1183. (@) In smartcard mode, the following bits must be kept cleared:
  1184. (+@) LINEN bit in the USART_CR2 register.
  1185. (+@) HDSEL and IREN bits in the USART_CR3 register.
  1186. @endverbatim
  1187. * @{
  1188. */
  1189. /**
  1190. * @brief Sets the specified USART guard time.
  1191. * @note This function is not available for STM32F030 devices.
  1192. * @param USARTx: where x can be 1or 2 to select the USART peripheral.
  1193. * @note USART2 is available only for STM32F072 and STM32F091 devices.
  1194. * @note USART3 is available only for STM32F091 devices.
  1195. * @param USART_GuardTime: specifies the guard time.
  1196. * @retval None
  1197. */
  1198. void USART_SetGuardTime(USART_TypeDef* USARTx, uint8_t USART_GuardTime)
  1199. {
  1200. /* Check the parameters */
  1201. assert_param(IS_USART_123_PERIPH(USARTx));
  1202. /* Clear the USART Guard time */
  1203. USARTx->GTPR &= USART_GTPR_PSC;
  1204. /* Set the USART guard time */
  1205. USARTx->GTPR |= (uint16_t)((uint16_t)USART_GuardTime << 0x08);
  1206. }
  1207. /**
  1208. * @brief Enables or disables the USART's Smart Card mode.
  1209. * @note This function is not available for STM32F030 devices.
  1210. * @param USARTx: where x can be 1or 2 to select the USART peripheral.
  1211. * @note USART2 is available only for STM32F072 and STM32F091 devices.
  1212. * @note USART3 is available only for STM32F091 devices.
  1213. * @param NewState: new state of the Smart Card mode.
  1214. * This parameter can be: ENABLE or DISABLE.
  1215. * @retval None
  1216. */
  1217. void USART_SmartCardCmd(USART_TypeDef* USARTx, FunctionalState NewState)
  1218. {
  1219. /* Check the parameters */
  1220. assert_param(IS_USART_123_PERIPH(USARTx));
  1221. assert_param(IS_FUNCTIONAL_STATE(NewState));
  1222. if (NewState != DISABLE)
  1223. {
  1224. /* Enable the SC mode by setting the SCEN bit in the CR3 register */
  1225. USARTx->CR3 |= USART_CR3_SCEN;
  1226. }
  1227. else
  1228. {
  1229. /* Disable the SC mode by clearing the SCEN bit in the CR3 register */
  1230. USARTx->CR3 &= (uint32_t)~((uint32_t)USART_CR3_SCEN);
  1231. }
  1232. }
  1233. /**
  1234. * @brief Enables or disables NACK transmission.
  1235. * @note This function is not available for STM32F030 devices.
  1236. * @param USARTx: where x can be 1or 2 to select the USART peripheral.
  1237. * @note USART2 is available only for STM32F072 and STM32F091 devices.
  1238. * @note USART3 is available only for STM32F091 devices.
  1239. * @param NewState: new state of the NACK transmission.
  1240. * This parameter can be: ENABLE or DISABLE.
  1241. * @retval None
  1242. */
  1243. void USART_SmartCardNACKCmd(USART_TypeDef* USARTx, FunctionalState NewState)
  1244. {
  1245. /* Check the parameters */
  1246. assert_param(IS_USART_123_PERIPH(USARTx));
  1247. assert_param(IS_FUNCTIONAL_STATE(NewState));
  1248. if (NewState != DISABLE)
  1249. {
  1250. /* Enable the NACK transmission by setting the NACK bit in the CR3 register */
  1251. USARTx->CR3 |= USART_CR3_NACK;
  1252. }
  1253. else
  1254. {
  1255. /* Disable the NACK transmission by clearing the NACK bit in the CR3 register */
  1256. USARTx->CR3 &= (uint32_t)~((uint32_t)USART_CR3_NACK);
  1257. }
  1258. }
  1259. /**
  1260. * @brief Sets the Smart Card number of retries in transmit and receive.
  1261. * @note This function is not available for STM32F030 devices.
  1262. * @param USARTx: where x can be 1or 3 to select the USART peripheral.
  1263. * @note USART2 is available only for STM32F072 and STM32F091 devices.
  1264. * @note USART3 is available only for STM32F091 devices.
  1265. * @param USART_AutoCount: specifies the Smart Card auto retry count.
  1266. * @retval None
  1267. */
  1268. void USART_SetAutoRetryCount(USART_TypeDef* USARTx, uint8_t USART_AutoCount)
  1269. {
  1270. /* Check the parameters */
  1271. assert_param(IS_USART_123_PERIPH(USARTx));
  1272. assert_param(IS_USART_AUTO_RETRY_COUNTER(USART_AutoCount));
  1273. /* Clear the USART auto retry count */
  1274. USARTx->CR3 &= (uint32_t)~((uint32_t)USART_CR3_SCARCNT);
  1275. /* Set the USART auto retry count*/
  1276. USARTx->CR3 |= (uint32_t)((uint32_t)USART_AutoCount << 0x11);
  1277. }
  1278. /**
  1279. * @brief Sets the Smart Card Block length.
  1280. * @note This function is not available for STM32F030 devices.
  1281. * @param USARTx: where x can be 1or 3 to select the USART peripheral.
  1282. * @note USART2 is available only for STM32F072 and STM32F091 devices.
  1283. * @note USART3 is available only for STM32F091 devices.
  1284. * @param USART_BlockLength: specifies the Smart Card block length.
  1285. * @retval None
  1286. */
  1287. void USART_SetBlockLength(USART_TypeDef* USARTx, uint8_t USART_BlockLength)
  1288. {
  1289. /* Check the parameters */
  1290. assert_param(IS_USART_123_PERIPH(USARTx));
  1291. /* Clear the Smart card block length */
  1292. USARTx->RTOR &= (uint32_t)~((uint32_t)USART_RTOR_BLEN);
  1293. /* Set the Smart Card block length */
  1294. USARTx->RTOR |= (uint32_t)((uint32_t)USART_BlockLength << 0x18);
  1295. }
  1296. /**
  1297. * @}
  1298. */
  1299. /** @defgroup USART_Group9 IrDA mode functions
  1300. * @brief IrDA mode functions
  1301. *
  1302. @verbatim
  1303. ===============================================================================
  1304. ##### IrDA mode functions #####
  1305. ===============================================================================
  1306. [..] This subsection provides a set of functions allowing to manage the USART
  1307. IrDA communication.
  1308. [..] IrDA is a half duplex communication protocol. If the Transmitter is busy,
  1309. any data on the IrDA receive line will be ignored by the IrDA decoder
  1310. and if the Receiver is busy, data on the TX from the USART to IrDA will
  1311. not be encoded by IrDA. While receiving data, transmission should be
  1312. avoided as the data to be transmitted could be corrupted.
  1313. [..] IrDA communication is possible through the following procedure:
  1314. (#) Program the Baud rate, Word length = 8 bits, Stop bits, Parity,
  1315. Transmitter/Receiver modes and hardware flow control values using
  1316. the USART_Init() function.
  1317. (#) Configures the IrDA pulse width by configuring the prescaler using
  1318. the USART_SetPrescaler() function.
  1319. (#) Configures the IrDA USART_IrDAMode_LowPower or USART_IrDAMode_Normal
  1320. mode using the USART_IrDAConfig() function.
  1321. (#) Enable the IrDA using the USART_IrDACmd() function.
  1322. (#) Enable the USART using the USART_Cmd() function.
  1323. [..]
  1324. (@) A pulse of width less than two and greater than one PSC period(s) may or
  1325. may not be rejected.
  1326. (@) The receiver set up time should be managed by software. The IrDA physical
  1327. layer specification specifies a minimum of 10 ms delay between
  1328. transmission and reception (IrDA is a half duplex protocol).
  1329. (@) In IrDA mode, the following bits must be kept cleared:
  1330. (+@) LINEN, STOP and CLKEN bits in the USART_CR2 register.
  1331. (+@) SCEN and HDSEL bits in the USART_CR3 register.
  1332. @endverbatim
  1333. * @{
  1334. */
  1335. /**
  1336. * @brief Configures the USART's IrDA interface.
  1337. * @note This function is not available for STM32F030 devices.
  1338. * @param USARTx: where x can be 1or 2 to select the USART peripheral.
  1339. * @note USART2 is available only for STM32F072 and STM32F091 devices.
  1340. * @note USART3 is available only for STM32F091 devices.
  1341. * @param USART_IrDAMode: specifies the IrDA mode.
  1342. * This parameter can be one of the following values:
  1343. * @arg USART_IrDAMode_LowPower
  1344. * @arg USART_IrDAMode_Normal
  1345. * @retval None
  1346. */
  1347. void USART_IrDAConfig(USART_TypeDef* USARTx, uint32_t USART_IrDAMode)
  1348. {
  1349. /* Check the parameters */
  1350. assert_param(IS_USART_123_PERIPH(USARTx));
  1351. assert_param(IS_USART_IRDA_MODE(USART_IrDAMode));
  1352. USARTx->CR3 &= (uint32_t)~((uint32_t)USART_CR3_IRLP);
  1353. USARTx->CR3 |= USART_IrDAMode;
  1354. }
  1355. /**
  1356. * @brief Enables or disables the USART's IrDA interface.
  1357. * @note This function is not available for STM32F030 devices.
  1358. * @param USARTx: where x can be 1or 2 to select the USART peripheral.
  1359. * @note USART2 is available only for STM32F072 and STM32F091 devices.
  1360. * @note USART3 is available only for STM32F091 devices.
  1361. * @param NewState: new state of the IrDA mode.
  1362. * This parameter can be: ENABLE or DISABLE.
  1363. * @retval None
  1364. */
  1365. void USART_IrDACmd(USART_TypeDef* USARTx, FunctionalState NewState)
  1366. {
  1367. /* Check the parameters */
  1368. assert_param(IS_USART_123_PERIPH(USARTx));
  1369. assert_param(IS_FUNCTIONAL_STATE(NewState));
  1370. if (NewState != DISABLE)
  1371. {
  1372. /* Enable the IrDA mode by setting the IREN bit in the CR3 register */
  1373. USARTx->CR3 |= USART_CR3_IREN;
  1374. }
  1375. else
  1376. {
  1377. /* Disable the IrDA mode by clearing the IREN bit in the CR3 register */
  1378. USARTx->CR3 &= (uint32_t)~((uint32_t)USART_CR3_IREN);
  1379. }
  1380. }
  1381. /**
  1382. * @}
  1383. */
  1384. /** @defgroup USART_Group10 RS485 mode function
  1385. * @brief RS485 mode function
  1386. *
  1387. @verbatim
  1388. ===============================================================================
  1389. ##### RS485 mode functions #####
  1390. ===============================================================================
  1391. [..] This subsection provides a set of functions allowing to manage the USART
  1392. RS485 flow control.
  1393. [..] RS485 flow control (Driver enable feature) handling is possible through
  1394. the following procedure:
  1395. (#) Program the Baud rate, Word length = 8 bits, Stop bits, Parity,
  1396. Transmitter/Receiver modes and hardware flow control values using
  1397. the USART_Init() function.
  1398. (#) Enable the Driver Enable using the USART_DECmd() function.
  1399. (#) Configures the Driver Enable polarity using the USART_DEPolarityConfig()
  1400. function.
  1401. (#) Configures the Driver Enable assertion time using USART_SetDEAssertionTime()
  1402. function and deassertion time using the USART_SetDEDeassertionTime()
  1403. function.
  1404. (#) Enable the USART using the USART_Cmd() function.
  1405. -@-
  1406. (+@) The assertion and dessertion times are expressed in sample time units (1/8 or
  1407. 1/16 bit time, depending on the oversampling rate).
  1408. @endverbatim
  1409. * @{
  1410. */
  1411. /**
  1412. * @brief Enables or disables the USART's DE functionality.
  1413. * @param USARTx: where x can be from 1 to 8 to select the USART peripheral.
  1414. * @note USART3 and USART4 are available only for STM32F072 and STM32F091 devices.
  1415. * @note USART5, USART6, USART7 and USART8 are available only for STM32F091 devices.
  1416. * @note USART2 is not available for STM32F031 devices.
  1417. * @param NewState: new state of the driver enable mode.
  1418. * This parameter can be: ENABLE or DISABLE.
  1419. * @retval None
  1420. */
  1421. void USART_DECmd(USART_TypeDef* USARTx, FunctionalState NewState)
  1422. {
  1423. /* Check the parameters */
  1424. assert_param(IS_USART_ALL_PERIPH(USARTx));
  1425. assert_param(IS_FUNCTIONAL_STATE(NewState));
  1426. if (NewState != DISABLE)
  1427. {
  1428. /* Enable the DE functionality by setting the DEM bit in the CR3 register */
  1429. USARTx->CR3 |= USART_CR3_DEM;
  1430. }
  1431. else
  1432. {
  1433. /* Disable the DE functionality by clearing the DEM bit in the CR3 register */
  1434. USARTx->CR3 &= (uint32_t)~((uint32_t)USART_CR3_DEM);
  1435. }
  1436. }
  1437. /**
  1438. * @brief Configures the USART's DE polarity
  1439. * @param USARTx: where x can be from 1 to 8 to select the USART peripheral.
  1440. * @note USART3 and USART4 are available only for STM32F072 and STM32F091 devices.
  1441. * @note USART5, USART6, USART7 and USART8 are available only for STM32F091 devices.
  1442. * @note USART2 is not available for STM32F031 devices.
  1443. * @param USART_DEPolarity: specifies the DE polarity.
  1444. * This parameter can be one of the following values:
  1445. * @arg USART_DEPolarity_Low
  1446. * @arg USART_DEPolarity_High
  1447. * @retval None
  1448. */
  1449. void USART_DEPolarityConfig(USART_TypeDef* USARTx, uint32_t USART_DEPolarity)
  1450. {
  1451. /* Check the parameters */
  1452. assert_param(IS_USART_ALL_PERIPH(USARTx));
  1453. assert_param(IS_USART_DE_POLARITY(USART_DEPolarity));
  1454. USARTx->CR3 &= (uint32_t)~((uint32_t)USART_CR3_DEP);
  1455. USARTx->CR3 |= USART_DEPolarity;
  1456. }
  1457. /**
  1458. * @brief Sets the specified RS485 DE assertion time
  1459. * @param USARTx: where x can be from 1 to 8 to select the USART peripheral.
  1460. * @note USART3 and USART4 are available only for STM32F072 and STM32F091 devices.
  1461. * @note USART5, USART6, USART7 and USART8 are available only for STM32F091 devices.
  1462. * @note USART2 is not available for STM32F031 devices.
  1463. * @param USART_DEAssertionTime: specifies the time between the activation of
  1464. * the DE signal and the beginning of the start bit
  1465. * @retval None
  1466. */
  1467. void USART_SetDEAssertionTime(USART_TypeDef* USARTx, uint32_t USART_DEAssertionTime)
  1468. {
  1469. /* Check the parameters */
  1470. assert_param(IS_USART_ALL_PERIPH(USARTx));
  1471. assert_param(IS_USART_DE_ASSERTION_DEASSERTION_TIME(USART_DEAssertionTime));
  1472. /* Clear the DE assertion time */
  1473. USARTx->CR1 &= (uint32_t)~((uint32_t)USART_CR1_DEAT);
  1474. /* Set the new value for the DE assertion time */
  1475. USARTx->CR1 |=((uint32_t)USART_DEAssertionTime << (uint32_t)0x15);
  1476. }
  1477. /**
  1478. * @brief Sets the specified RS485 DE deassertion time
  1479. * @param USARTx: where x can be from 1 to 8 to select the USART peripheral.
  1480. * @note USART3 and USART4 are available only for STM32F072 and STM32F091 devices.
  1481. * @note USART5, USART6, USART7 and USART8 are available only for STM32F091 devices.
  1482. * @note USART2 is not available for STM32F031 devices.
  1483. * @param USART_DeassertionTime: specifies the time between the middle of the last
  1484. * stop bit in a transmitted message and the de-activation of the DE signal
  1485. * @retval None
  1486. */
  1487. void USART_SetDEDeassertionTime(USART_TypeDef* USARTx, uint32_t USART_DEDeassertionTime)
  1488. {
  1489. /* Check the parameters */
  1490. assert_param(IS_USART_ALL_PERIPH(USARTx));
  1491. assert_param(IS_USART_DE_ASSERTION_DEASSERTION_TIME(USART_DEDeassertionTime));
  1492. /* Clear the DE deassertion time */
  1493. USARTx->CR1 &= (uint32_t)~((uint32_t)USART_CR1_DEDT);
  1494. /* Set the new value for the DE deassertion time */
  1495. USARTx->CR1 |=((uint32_t)USART_DEDeassertionTime << (uint32_t)0x10);
  1496. }
  1497. /**
  1498. * @}
  1499. */
  1500. /** @defgroup USART_Group11 DMA transfers management functions
  1501. * @brief DMA transfers management functions
  1502. *
  1503. @verbatim
  1504. ===============================================================================
  1505. ##### DMA transfers management functions #####
  1506. ===============================================================================
  1507. [..] This section provides two functions that can be used only in DMA mode.
  1508. [..] In DMA Mode, the USART communication can be managed by 2 DMA Channel
  1509. requests:
  1510. (#) USART_DMAReq_Tx: specifies the Tx buffer DMA transfer request.
  1511. (#) USART_DMAReq_Rx: specifies the Rx buffer DMA transfer request.
  1512. [..] In this Mode it is advised to use the following function:
  1513. (+) void USART_DMACmd(USART_TypeDef* USARTx, uint16_t USART_DMAReq,
  1514. FunctionalState NewState).
  1515. @endverbatim
  1516. * @{
  1517. */
  1518. /**
  1519. * @brief Enables or disables the USART's DMA interface.
  1520. * @param USARTx: where x can be from 1 to 8 to select the USART peripheral.
  1521. * @note USART3 and USART4 are available only for STM32F072 and STM32F091 devices.
  1522. * @note USART5, USART6, USART7 and USART8 are available only for STM32F091 devices.
  1523. * @note USART2 is not available for STM32F031 devices.
  1524. * @param USART_DMAReq: specifies the DMA request.
  1525. * This parameter can be any combination of the following values:
  1526. * @arg USART_DMAReq_Tx: USART DMA transmit request
  1527. * @arg USART_DMAReq_Rx: USART DMA receive request
  1528. * @param NewState: new state of the DMA Request sources.
  1529. * This parameter can be: ENABLE or DISABLE.
  1530. * @retval None
  1531. */
  1532. void USART_DMACmd(USART_TypeDef* USARTx, uint32_t USART_DMAReq, FunctionalState NewState)
  1533. {
  1534. /* Check the parameters */
  1535. assert_param(IS_USART_ALL_PERIPH(USARTx));
  1536. assert_param(IS_USART_DMAREQ(USART_DMAReq));
  1537. assert_param(IS_FUNCTIONAL_STATE(NewState));
  1538. if (NewState != DISABLE)
  1539. {
  1540. /* Enable the DMA transfer for selected requests by setting the DMAT and/or
  1541. DMAR bits in the USART CR3 register */
  1542. USARTx->CR3 |= USART_DMAReq;
  1543. }
  1544. else
  1545. {
  1546. /* Disable the DMA transfer for selected requests by clearing the DMAT and/or
  1547. DMAR bits in the USART CR3 register */
  1548. USARTx->CR3 &= (uint32_t)~USART_DMAReq;
  1549. }
  1550. }
  1551. /**
  1552. * @brief Enables or disables the USART's DMA interface when reception error occurs.
  1553. * @param USARTx: where x can be from 1 to 8 to select the USART peripheral.
  1554. * @note USART3 and USART4 are available only for STM32F072 and STM32F091 devices.
  1555. * @note USART5, USART6, USART7 and USART8 are available only for STM32F091 devices.
  1556. * @note USART2 is not available for STM32F031 devices.
  1557. * @param USART_DMAOnError: specifies the DMA status in case of reception error.
  1558. * This parameter can be any combination of the following values:
  1559. * @arg USART_DMAOnError_Enable: DMA receive request enabled when the USART DMA
  1560. * reception error is asserted.
  1561. * @arg USART_DMAOnError_Disable: DMA receive request disabled when the USART DMA
  1562. * reception error is asserted.
  1563. * @retval None
  1564. */
  1565. void USART_DMAReceptionErrorConfig(USART_TypeDef* USARTx, uint32_t USART_DMAOnError)
  1566. {
  1567. /* Check the parameters */
  1568. assert_param(IS_USART_ALL_PERIPH(USARTx));
  1569. assert_param(IS_USART_DMAONERROR(USART_DMAOnError));
  1570. /* Clear the DMA Reception error detection bit */
  1571. USARTx->CR3 &= (uint32_t)~((uint32_t)USART_CR3_DDRE);
  1572. /* Set the new value for the DMA Reception error detection bit */
  1573. USARTx->CR3 |= USART_DMAOnError;
  1574. }
  1575. /**
  1576. * @}
  1577. */
  1578. /** @defgroup USART_Group12 Interrupts and flags management functions
  1579. * @brief Interrupts and flags management functions
  1580. *
  1581. @verbatim
  1582. ===============================================================================
  1583. ##### Interrupts and flags management functions #####
  1584. ===============================================================================
  1585. [..] This subsection provides a set of functions allowing to configure the
  1586. USART Interrupts sources, Requests and check or clear the flags or pending bits status.
  1587. The user should identify which mode will be used in his application to
  1588. manage the communication: Polling mode, Interrupt mode.
  1589. *** Polling Mode ***
  1590. ====================
  1591. [..] In Polling Mode, the SPI communication can be managed by these flags:
  1592. (#) USART_FLAG_REACK: to indicate the status of the Receive Enable
  1593. acknowledge flag
  1594. (#) USART_FLAG_TEACK: to indicate the status of the Transmit Enable
  1595. acknowledge flag.
  1596. (#) USART_FLAG_WU: to indicate the status of the Wake up flag.
  1597. (#) USART_FLAG_RWU: to indicate the status of the Receive Wake up flag.
  1598. (#) USART_FLAG_SBK: to indicate the status of the Send Break flag.
  1599. (#) USART_FLAG_CM: to indicate the status of the Character match flag.
  1600. (#) USART_FLAG_BUSY: to indicate the status of the Busy flag.
  1601. (#) USART_FLAG_ABRF: to indicate the status of the Auto baud rate flag.
  1602. (#) USART_FLAG_ABRE: to indicate the status of the Auto baud rate error flag.
  1603. (#) USART_FLAG_EOB: to indicate the status of the End of block flag.
  1604. (#) USART_FLAG_RTO: to indicate the status of the Receive time out flag.
  1605. (#) USART_FLAG_nCTSS: to indicate the status of the Inverted nCTS input
  1606. bit status.
  1607. (#) USART_FLAG_TXE: to indicate the status of the transmit buffer register.
  1608. (#) USART_FLAG_RXNE: to indicate the status of the receive buffer register.
  1609. (#) USART_FLAG_TC: to indicate the status of the transmit operation.
  1610. (#) USART_FLAG_IDLE: to indicate the status of the Idle Line.
  1611. (#) USART_FLAG_CTS: to indicate the status of the nCTS input.
  1612. (#) USART_FLAG_LBD: to indicate the status of the LIN break detection.
  1613. (#) USART_FLAG_NE: to indicate if a noise error occur.
  1614. (#) USART_FLAG_FE: to indicate if a frame error occur.
  1615. (#) USART_FLAG_PE: to indicate if a parity error occur.
  1616. (#) USART_FLAG_ORE: to indicate if an Overrun error occur.
  1617. [..] In this Mode it is advised to use the following functions:
  1618. (+) FlagStatus USART_GetFlagStatus(USART_TypeDef* USARTx, uint16_t USART_FLAG).
  1619. (+) void USART_ClearFlag(USART_TypeDef* USARTx, uint16_t USART_FLAG).
  1620. *** Interrupt Mode ***
  1621. ======================
  1622. [..] In Interrupt Mode, the USART communication can be managed by 8 interrupt
  1623. sources and 10 pending bits:
  1624. (+) Pending Bits:
  1625. (##) USART_IT_WU: to indicate the status of the Wake up interrupt.
  1626. (##) USART_IT_CM: to indicate the status of Character match interrupt.
  1627. (##) USART_IT_EOB: to indicate the status of End of block interrupt.
  1628. (##) USART_IT_RTO: to indicate the status of Receive time out interrupt.
  1629. (##) USART_IT_CTS: to indicate the status of CTS change interrupt.
  1630. (##) USART_IT_LBD: to indicate the status of LIN Break detection interrupt.
  1631. (##) USART_IT_TC: to indicate the status of Transmission complete interrupt.
  1632. (##) USART_IT_IDLE: to indicate the status of IDLE line detected interrupt.
  1633. (##) USART_IT_ORE: to indicate the status of OverRun Error interrupt.
  1634. (##) USART_IT_NE: to indicate the status of Noise Error interrupt.
  1635. (##) USART_IT_FE: to indicate the status of Framing Error interrupt.
  1636. (##) USART_IT_PE: to indicate the status of Parity Error interrupt.
  1637. (+) Interrupt Source:
  1638. (##) USART_IT_WU: specifies the interrupt source for Wake up interrupt.
  1639. (##) USART_IT_CM: specifies the interrupt source for Character match
  1640. interrupt.
  1641. (##) USART_IT_EOB: specifies the interrupt source for End of block
  1642. interrupt.
  1643. (##) USART_IT_RTO: specifies the interrupt source for Receive time-out
  1644. interrupt.
  1645. (##) USART_IT_CTS: specifies the interrupt source for CTS change interrupt.
  1646. (##) USART_IT_LBD: specifies the interrupt source for LIN Break
  1647. detection interrupt.
  1648. (##) USART_IT_TXE: specifies the interrupt source for Tansmit Data
  1649. Register empty interrupt.
  1650. (##) USART_IT_TC: specifies the interrupt source for Transmission
  1651. complete interrupt.
  1652. (##) USART_IT_RXNE: specifies the interrupt source for Receive Data
  1653. register not empty interrupt.
  1654. (##) USART_IT_IDLE: specifies the interrupt source for Idle line
  1655. detection interrupt.
  1656. (##) USART_IT_PE: specifies the interrupt source for Parity Error interrupt.
  1657. (##) USART_IT_ERR: specifies the interrupt source for Error interrupt
  1658. (Frame error, noise error, overrun error)
  1659. -@@- Some parameters are coded in order to use them as interrupt
  1660. source or as pending bits.
  1661. [..] In this Mode it is advised to use the following functions:
  1662. (+) void USART_ITConfig(USART_TypeDef* USARTx, uint16_t USART_IT, FunctionalState NewState).
  1663. (+) ITStatus USART_GetITStatus(USART_TypeDef* USARTx, uint16_t USART_IT).
  1664. (+) void USART_ClearITPendingBit(USART_TypeDef* USARTx, uint16_t USART_IT).
  1665. @endverbatim
  1666. * @{
  1667. */
  1668. /**
  1669. * @brief Enables or disables the specified USART interrupts.
  1670. * @param USARTx: where x can be from 1 to 8 to select the USART peripheral.
  1671. * @note USART3 and USART4 are available only for STM32F072 and STM32F091 devices.
  1672. * @note USART5, USART6, USART7 and USART8 are available only for STM32F091 devices.
  1673. * @note USART2 is not available for STM32F031 devices.
  1674. * @param USART_IT: specifies the USART interrupt sources to be enabled or disabled.
  1675. * This parameter can be one of the following values:
  1676. * @arg USART_IT_WU: Wake up interrupt, not available for STM32F030 devices.
  1677. * @arg USART_IT_CM: Character match interrupt.
  1678. * @arg USART_IT_EOB: End of block interrupt, not available for STM32F030 devices.
  1679. * @arg USART_IT_RTO: Receive time out interrupt.
  1680. * @arg USART_IT_CTS: CTS change interrupt.
  1681. * @arg USART_IT_LBD: LIN Break detection interrupt, not available for STM32F030 devices.
  1682. * @arg USART_IT_TXE: Tansmit Data Register empty interrupt.
  1683. * @arg USART_IT_TC: Transmission complete interrupt.
  1684. * @arg USART_IT_RXNE: Receive Data register not empty interrupt.
  1685. * @arg USART_IT_IDLE: Idle line detection interrupt.
  1686. * @arg USART_IT_PE: Parity Error interrupt.
  1687. * @arg USART_IT_ERR: Error interrupt(Frame error, noise error, overrun error)
  1688. * @param NewState: new state of the specified USARTx interrupts.
  1689. * This parameter can be: ENABLE or DISABLE.
  1690. * @retval None
  1691. */
  1692. void USART_ITConfig(USART_TypeDef* USARTx, uint32_t USART_IT, FunctionalState NewState)
  1693. {
  1694. uint32_t usartreg = 0, itpos = 0, itmask = 0;
  1695. uint32_t usartxbase = 0;
  1696. /* Check the parameters */
  1697. assert_param(IS_USART_ALL_PERIPH(USARTx));
  1698. assert_param(IS_USART_CONFIG_IT(USART_IT));
  1699. assert_param(IS_FUNCTIONAL_STATE(NewState));
  1700. usartxbase = (uint32_t)USARTx;
  1701. /* Get the USART register index */
  1702. usartreg = (((uint16_t)USART_IT) >> 0x08);
  1703. /* Get the interrupt position */
  1704. itpos = USART_IT & IT_MASK;
  1705. itmask = (((uint32_t)0x01) << itpos);
  1706. if (usartreg == 0x02) /* The IT is in CR2 register */
  1707. {
  1708. usartxbase += 0x04;
  1709. }
  1710. else if (usartreg == 0x03) /* The IT is in CR3 register */
  1711. {
  1712. usartxbase += 0x08;
  1713. }
  1714. else /* The IT is in CR1 register */
  1715. {
  1716. }
  1717. if (NewState != DISABLE)
  1718. {
  1719. *(__IO uint32_t*)usartxbase |= itmask;
  1720. }
  1721. else
  1722. {
  1723. *(__IO uint32_t*)usartxbase &= ~itmask;
  1724. }
  1725. }
  1726. /**
  1727. * @brief Enables the specified USART's Request.
  1728. * @param USARTx: where x can be from 1 to 8 to select the USART peripheral.
  1729. * @note USART3 and USART4 are available only for STM32F072 and STM32F091 devices.
  1730. * @note USART5, USART6, USART7 and USART8 are available only for STM32F091 devices.
  1731. * @note USART2 is not available for STM32F031 devices.
  1732. * @param USART_Request: specifies the USART request.
  1733. * This parameter can be any combination of the following values:
  1734. * @arg USART_Request_TXFRQ: Transmit data flush ReQuest
  1735. * @arg USART_Request_RXFRQ: Receive data flush ReQuest
  1736. * @arg USART_Request_MMRQ: Mute Mode ReQuest
  1737. * @arg USART_Request_SBKRQ: Send Break ReQuest
  1738. * @arg USART_Request_ABRRQ: Auto Baud Rate ReQuest
  1739. * @param NewState: new state of the DMA interface when reception error occurs.
  1740. * This parameter can be: ENABLE or DISABLE.
  1741. * @retval None
  1742. */
  1743. void USART_RequestCmd(USART_TypeDef* USARTx, uint32_t USART_Request, FunctionalState NewState)
  1744. {
  1745. /* Check the parameters */
  1746. assert_param(IS_USART_ALL_PERIPH(USARTx));
  1747. assert_param(IS_USART_REQUEST(USART_Request));
  1748. assert_param(IS_FUNCTIONAL_STATE(NewState));
  1749. if (NewState != DISABLE)
  1750. {
  1751. /* Enable the USART ReQuest by setting the dedicated request bit in the RQR
  1752. register.*/
  1753. USARTx->RQR |= USART_Request;
  1754. }
  1755. else
  1756. {
  1757. /* Disable the USART ReQuest by clearing the dedicated request bit in the RQR
  1758. register.*/
  1759. USARTx->RQR &= (uint32_t)~USART_Request;
  1760. }
  1761. }
  1762. /**
  1763. * @brief Enables or disables the USART's Overrun detection.
  1764. * @param USARTx: where x can be from 1 to 8 to select the USART peripheral.
  1765. * @note USART3 and USART4 are available only for STM32F072 and STM32F091 devices.
  1766. * @note USART5, USART6, USART7 and USART8 are available only for STM32F091 devices.
  1767. * @note USART2 is not available for STM32F031 devices.
  1768. * @param USART_OVRDetection: specifies the OVR detection status in case of OVR error.
  1769. * This parameter can be any combination of the following values:
  1770. * @arg USART_OVRDetection_Enable: OVR error detection enabled when
  1771. * the USART OVR error is asserted.
  1772. * @arg USART_OVRDetection_Disable: OVR error detection disabled when
  1773. * the USART OVR error is asserted.
  1774. * @retval None
  1775. */
  1776. void USART_OverrunDetectionConfig(USART_TypeDef* USARTx, uint32_t USART_OVRDetection)
  1777. {
  1778. /* Check the parameters */
  1779. assert_param(IS_USART_ALL_PERIPH(USARTx));
  1780. assert_param(IS_USART_OVRDETECTION(USART_OVRDetection));
  1781. /* Clear the OVR detection bit */
  1782. USARTx->CR3 &= (uint32_t)~((uint32_t)USART_CR3_OVRDIS);
  1783. /* Set the new value for the OVR detection bit */
  1784. USARTx->CR3 |= USART_OVRDetection;
  1785. }
  1786. /**
  1787. * @brief Checks whether the specified USART flag is set or not.
  1788. * @param USARTx: where x can be from 1 to 8 to select the USART peripheral.
  1789. * @note USART3 and USART4 are available only for STM32F072 and STM32F091 devices.
  1790. * @note USART5, USART6, USART7 and USART8 are available only for STM32F091 devices.
  1791. * @note USART2 is not available for STM32F031 devices.
  1792. * @param USART_FLAG: specifies the flag to check.
  1793. * This parameter can be one of the following values:
  1794. * @arg USART_FLAG_REACK: Receive Enable acknowledge flag.
  1795. * @arg USART_FLAG_TEACK: Transmit Enable acknowledge flag.
  1796. * @arg USART_FLAG_WU: Wake up flag, not available for STM32F030 devices.
  1797. * @arg USART_FLAG_RWU: Receive Wake up flag, not available for STM32F030 devices.
  1798. * @arg USART_FLAG_SBK: Send Break flag.
  1799. * @arg USART_FLAG_CM: Character match flag.
  1800. * @arg USART_FLAG_BUSY: Busy flag.
  1801. * @arg USART_FLAG_ABRF: Auto baud rate flag.
  1802. * @arg USART_FLAG_ABRE: Auto baud rate error flag.
  1803. * @arg USART_FLAG_EOB: End of block flag, not available for STM32F030 devices.
  1804. * @arg USART_FLAG_RTO: Receive time out flag.
  1805. * @arg USART_FLAG_nCTSS: Inverted nCTS input bit status.
  1806. * @arg USART_FLAG_CTS: CTS Change flag.
  1807. * @arg USART_FLAG_LBD: LIN Break detection flag, not available for STM32F030 devices.
  1808. * @arg USART_FLAG_TXE: Transmit data register empty flag.
  1809. * @arg USART_FLAG_TC: Transmission Complete flag.
  1810. * @arg USART_FLAG_RXNE: Receive data register not empty flag.
  1811. * @arg USART_FLAG_IDLE: Idle Line detection flag.
  1812. * @arg USART_FLAG_ORE: OverRun Error flag.
  1813. * @arg USART_FLAG_NE: Noise Error flag.
  1814. * @arg USART_FLAG_FE: Framing Error flag.
  1815. * @arg USART_FLAG_PE: Parity Error flag.
  1816. * @retval The new state of USART_FLAG (SET or RESET).
  1817. */
  1818. FlagStatus USART_GetFlagStatus(USART_TypeDef* USARTx, uint32_t USART_FLAG)
  1819. {
  1820. FlagStatus bitstatus = RESET;
  1821. /* Check the parameters */
  1822. assert_param(IS_USART_ALL_PERIPH(USARTx));
  1823. assert_param(IS_USART_FLAG(USART_FLAG));
  1824. if ((USARTx->ISR & USART_FLAG) != (uint16_t)RESET)
  1825. {
  1826. bitstatus = SET;
  1827. }
  1828. else
  1829. {
  1830. bitstatus = RESET;
  1831. }
  1832. return bitstatus;
  1833. }
  1834. /**
  1835. * @brief Clears the USARTx's pending flags.
  1836. * @param USARTx: where x can be from 1 to 8 to select the USART peripheral.
  1837. * @note USART3 and USART4 are available only for STM32F072 and STM32F091 devices.
  1838. * @note USART5, USART6, USART7 and USART8 are available only for STM32F091 devices.
  1839. * @note USART2 is not available for STM32F031 devices.
  1840. * @param USART_FLAG: specifies the flag to clear.
  1841. * This parameter can be any combination of the following values:
  1842. * @arg USART_FLAG_WU: Wake up flag, not available for STM32F030 devices.
  1843. * @arg USART_FLAG_CM: Character match flag.
  1844. * @arg USART_FLAG_EOB: End of block flag, not available for STM32F030 devices.
  1845. * @arg USART_FLAG_RTO: Receive time out flag.
  1846. * @arg USART_FLAG_CTS: CTS Change flag.
  1847. * @arg USART_FLAG_LBD: LIN Break detection flag, not available for STM32F030 devices.
  1848. * @arg USART_FLAG_TC: Transmission Complete flag.
  1849. * @arg USART_FLAG_IDLE: IDLE line detected flag.
  1850. * @arg USART_FLAG_ORE: OverRun Error flag.
  1851. * @arg USART_FLAG_NE: Noise Error flag.
  1852. * @arg USART_FLAG_FE: Framing Error flag.
  1853. * @arg USART_FLAG_PE: Parity Errorflag.
  1854. *
  1855. * @note RXNE pending bit is cleared by a read to the USART_RDR register
  1856. * (USART_ReceiveData()) or by writing 1 to the RXFRQ in the register
  1857. * USART_RQR (USART_RequestCmd()).
  1858. * @note TC flag can be also cleared by software sequence: a read operation
  1859. * to USART_SR register (USART_GetFlagStatus()) followed by a write
  1860. * operation to USART_TDR register (USART_SendData()).
  1861. * @note TXE flag is cleared by a write to the USART_TDR register (USART_SendData())
  1862. * or by writing 1 to the TXFRQ in the register USART_RQR (USART_RequestCmd()).
  1863. * @note SBKF flag is cleared by 1 to the SBKRQ in the register USART_RQR
  1864. * (USART_RequestCmd()).
  1865. * @retval None
  1866. */
  1867. void USART_ClearFlag(USART_TypeDef* USARTx, uint32_t USART_FLAG)
  1868. {
  1869. /* Check the parameters */
  1870. assert_param(IS_USART_ALL_PERIPH(USARTx));
  1871. assert_param(IS_USART_CLEAR_FLAG(USART_FLAG));
  1872. USARTx->ICR = USART_FLAG;
  1873. }
  1874. /**
  1875. * @brief Checks whether the specified USART interrupt has occurred or not.
  1876. * @param USARTx: where x can be from 1 to 8 to select the USART peripheral.
  1877. * @note USART3 and USART4 are available only for STM32F072 and STM32F091 devices.
  1878. * @note USART5, USART6, USART7 and USART8 are available only for STM32F091 devices.
  1879. * @note USART2 is not available for STM32F031 devices.
  1880. * @param USART_IT: specifies the USART interrupt source to check.
  1881. * This parameter can be one of the following values:
  1882. * @arg USART_IT_WU: Wake up interrupt, not available for STM32F030 devices.
  1883. * @arg USART_IT_CM: Character match interrupt.
  1884. * @arg USART_IT_EOB: End of block interrupt, not available for STM32F030 devices.
  1885. * @arg USART_IT_RTO: Receive time out interrupt.
  1886. * @arg USART_IT_CTS: CTS change interrupt.
  1887. * @arg USART_IT_LBD: LIN Break detection interrupt, not available for STM32F030 devices.
  1888. * @arg USART_IT_TXE: Tansmit Data Register empty interrupt.
  1889. * @arg USART_IT_TC: Transmission complete interrupt.
  1890. * @arg USART_IT_RXNE: Receive Data register not empty interrupt.
  1891. * @arg USART_IT_IDLE: Idle line detection interrupt.
  1892. * @arg USART_IT_ORE: OverRun Error interrupt.
  1893. * @arg USART_IT_NE: Noise Error interrupt.
  1894. * @arg USART_IT_FE: Framing Error interrupt.
  1895. * @arg USART_IT_PE: Parity Error interrupt.
  1896. * @retval The new state of USART_IT (SET or RESET).
  1897. */
  1898. ITStatus USART_GetITStatus(USART_TypeDef* USARTx, uint32_t USART_IT)
  1899. {
  1900. uint32_t bitpos = 0, itmask = 0, usartreg = 0;
  1901. ITStatus bitstatus = RESET;
  1902. /* Check the parameters */
  1903. assert_param(IS_USART_ALL_PERIPH(USARTx));
  1904. assert_param(IS_USART_GET_IT(USART_IT));
  1905. /* Get the USART register index */
  1906. usartreg = (((uint16_t)USART_IT) >> 0x08);
  1907. /* Get the interrupt position */
  1908. itmask = USART_IT & IT_MASK;
  1909. itmask = (uint32_t)0x01 << itmask;
  1910. if (usartreg == 0x01) /* The IT is in CR1 register */
  1911. {
  1912. itmask &= USARTx->CR1;
  1913. }
  1914. else if (usartreg == 0x02) /* The IT is in CR2 register */
  1915. {
  1916. itmask &= USARTx->CR2;
  1917. }
  1918. else /* The IT is in CR3 register */
  1919. {
  1920. itmask &= USARTx->CR3;
  1921. }
  1922. bitpos = USART_IT >> 0x10;
  1923. bitpos = (uint32_t)0x01 << bitpos;
  1924. bitpos &= USARTx->ISR;
  1925. if ((itmask != (uint16_t)RESET)&&(bitpos != (uint16_t)RESET))
  1926. {
  1927. bitstatus = SET;
  1928. }
  1929. else
  1930. {
  1931. bitstatus = RESET;
  1932. }
  1933. return bitstatus;
  1934. }
  1935. /**
  1936. * @brief Clears the USARTx's interrupt pending bits.
  1937. * @param USARTx: where x can be from 1 to 8 to select the USART peripheral.
  1938. * @note USART3 and USART4 are available only for STM32F072 and STM32F091 devices.
  1939. * @note USART5, USART6, USART7 and USART8 are available only for STM32F091 devices.
  1940. * @note USART2 is not available for STM32F031 devices.
  1941. * @param USART_IT: specifies the interrupt pending bit to clear.
  1942. * This parameter can be one of the following values:
  1943. * @arg USART_IT_WU: Wake up interrupt, not available for STM32F030 devices.
  1944. * @arg USART_IT_CM: Character match interrupt.
  1945. * @arg USART_IT_EOB: End of block interrupt, not available for STM32F030 devices.
  1946. * @arg USART_IT_RTO: Receive time out interrupt.
  1947. * @arg USART_IT_CTS: CTS change interrupt.
  1948. * @arg USART_IT_LBD: LIN Break detection interrupt, not available for STM32F030 devices.
  1949. * @arg USART_IT_TC: Transmission complete interrupt.
  1950. * @arg USART_IT_IDLE: IDLE line detected interrupt.
  1951. * @arg USART_IT_ORE: OverRun Error interrupt.
  1952. * @arg USART_IT_NE: Noise Error interrupt.
  1953. * @arg USART_IT_FE: Framing Error interrupt.
  1954. * @arg USART_IT_PE: Parity Error interrupt.
  1955. *
  1956. * @note RXNE pending bit is cleared by a read to the USART_RDR register
  1957. * (USART_ReceiveData()) or by writing 1 to the RXFRQ in the register
  1958. * USART_RQR (USART_RequestCmd()).
  1959. * @note TC pending bit can be also cleared by software sequence: a read
  1960. * operation to USART_SR register (USART_GetITStatus()) followed by
  1961. * a write operation to USART_TDR register (USART_SendData()).
  1962. * @note TXE pending bit is cleared by a write to the USART_TDR register
  1963. * (USART_SendData()) or by writing 1 to the TXFRQ in the register
  1964. * USART_RQR (USART_RequestCmd()).
  1965. * @retval None
  1966. */
  1967. void USART_ClearITPendingBit(USART_TypeDef* USARTx, uint32_t USART_IT)
  1968. {
  1969. uint32_t bitpos = 0, itmask = 0;
  1970. /* Check the parameters */
  1971. assert_param(IS_USART_ALL_PERIPH(USARTx));
  1972. assert_param(IS_USART_CLEAR_IT(USART_IT));
  1973. bitpos = USART_IT >> 0x10;
  1974. itmask = ((uint32_t)0x01 << (uint32_t)bitpos);
  1975. USARTx->ICR = (uint32_t)itmask;
  1976. }
  1977. /**
  1978. * @}
  1979. */
  1980. /**
  1981. * @}
  1982. */
  1983. /**
  1984. * @}
  1985. */
  1986. /**
  1987. * @}
  1988. */
  1989. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/