1
0

stm32f2xx_usart.c 54 KB

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