1
0

stm32f0xx_usart.c 77 KB

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