stm8s_uart2.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882
  1. /**
  2. ********************************************************************************
  3. * @file stm8s_uart2.c
  4. * @author MCD Application Team
  5. * @version V2.3.0
  6. * @date 16-June-2017
  7. * @brief This file contains all the functions for the UART2 peripheral.
  8. ******************************************************************************
  9. * @attention
  10. *
  11. * <h2><center>&copy; COPYRIGHT 2014 STMicroelectronics</center></h2>
  12. *
  13. * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
  14. * You may not use this file except in compliance with the License.
  15. * You may obtain a copy of the License at:
  16. *
  17. * http://www.st.com/software_license_agreement_liberty_v2
  18. *
  19. * Unless required by applicable law or agreed to in writing, software
  20. * distributed under the License is distributed on an "AS IS" BASIS,
  21. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  22. * See the License for the specific language governing permissions and
  23. * limitations under the License.
  24. *
  25. ******************************************************************************
  26. */
  27. /* Includes ------------------------------------------------------------------*/
  28. #include "stm8s_uart2.h"
  29. /** @addtogroup STM8S_StdPeriph_Driver
  30. * @{
  31. */
  32. /* Private typedef -----------------------------------------------------------*/
  33. /* Private define ------------------------------------------------------------*/
  34. /* Private macro -------------------------------------------------------------*/
  35. /* Private variables ---------------------------------------------------------*/
  36. /* Private function prototypes -----------------------------------------------*/
  37. /* Private functions ---------------------------------------------------------*/
  38. /* Public functions ----------------------------------------------------------*/
  39. /**
  40. * @addtogroup UART2_Public_Functions
  41. * @{
  42. */
  43. /**
  44. * @brief Deinitializes the UART peripheral.
  45. * @param None
  46. * @retval None
  47. */
  48. void UART2_DeInit(void)
  49. {
  50. /* Clear the Idle Line Detected bit in the status register by a read
  51. to the UART2_SR register followed by a Read to the UART2_DR register */
  52. (void) UART2->SR;
  53. (void)UART2->DR;
  54. UART2->BRR2 = UART2_BRR2_RESET_VALUE; /* Set UART2_BRR2 to reset value 0x00 */
  55. UART2->BRR1 = UART2_BRR1_RESET_VALUE; /* Set UART2_BRR1 to reset value 0x00 */
  56. UART2->CR1 = UART2_CR1_RESET_VALUE; /* Set UART2_CR1 to reset value 0x00 */
  57. UART2->CR2 = UART2_CR2_RESET_VALUE; /* Set UART2_CR2 to reset value 0x00 */
  58. UART2->CR3 = UART2_CR3_RESET_VALUE; /* Set UART2_CR3 to reset value 0x00 */
  59. UART2->CR4 = UART2_CR4_RESET_VALUE; /* Set UART2_CR4 to reset value 0x00 */
  60. UART2->CR5 = UART2_CR5_RESET_VALUE; /* Set UART2_CR5 to reset value 0x00 */
  61. UART2->CR6 = UART2_CR6_RESET_VALUE; /* Set UART2_CR6 to reset value 0x00 */
  62. }
  63. /**
  64. * @brief Initializes the UART2 according to the specified parameters.
  65. * @param BaudRate: The baudrate.
  66. * @param WordLength : This parameter can be any of the
  67. * @ref UART2_WordLength_TypeDef enumeration.
  68. * @param StopBits: This parameter can be any of the
  69. * @ref UART2_StopBits_TypeDef enumeration.
  70. * @param Parity: This parameter can be any of the
  71. * @ref UART2_Parity_TypeDef enumeration.
  72. * @param SyncMode: This parameter can be any of the
  73. * @ref UART2_SyncMode_TypeDef values.
  74. * @param Mode: This parameter can be any of the @ref UART2_Mode_TypeDef values
  75. * @retval None
  76. */
  77. void UART2_Init(uint32_t BaudRate, UART2_WordLength_TypeDef WordLength, UART2_StopBits_TypeDef StopBits, UART2_Parity_TypeDef Parity, UART2_SyncMode_TypeDef SyncMode, UART2_Mode_TypeDef Mode)
  78. {
  79. uint8_t BRR2_1 = 0, BRR2_2 = 0;
  80. uint32_t BaudRate_Mantissa = 0, BaudRate_Mantissa100 = 0;
  81. /* Check the parameters */
  82. assert_param(IS_UART2_BAUDRATE_OK(BaudRate));
  83. assert_param(IS_UART2_WORDLENGTH_OK(WordLength));
  84. assert_param(IS_UART2_STOPBITS_OK(StopBits));
  85. assert_param(IS_UART2_PARITY_OK(Parity));
  86. assert_param(IS_UART2_MODE_OK((uint8_t)Mode));
  87. assert_param(IS_UART2_SYNCMODE_OK((uint8_t)SyncMode));
  88. /* Clear the word length bit */
  89. UART2->CR1 &= (uint8_t)(~UART2_CR1_M);
  90. /* Set the word length bit according to UART2_WordLength value */
  91. UART2->CR1 |= (uint8_t)WordLength;
  92. /* Clear the STOP bits */
  93. UART2->CR3 &= (uint8_t)(~UART2_CR3_STOP);
  94. /* Set the STOP bits number according to UART2_StopBits value */
  95. UART2->CR3 |= (uint8_t)StopBits;
  96. /* Clear the Parity Control bit */
  97. UART2->CR1 &= (uint8_t)(~(UART2_CR1_PCEN | UART2_CR1_PS ));
  98. /* Set the Parity Control bit to UART2_Parity value */
  99. UART2->CR1 |= (uint8_t)Parity;
  100. /* Clear the LSB mantissa of UART2DIV */
  101. UART2->BRR1 &= (uint8_t)(~UART2_BRR1_DIVM);
  102. /* Clear the MSB mantissa of UART2DIV */
  103. UART2->BRR2 &= (uint8_t)(~UART2_BRR2_DIVM);
  104. /* Clear the Fraction bits of UART2DIV */
  105. UART2->BRR2 &= (uint8_t)(~UART2_BRR2_DIVF);
  106. /* Set the UART2 BaudRates in BRR1 and BRR2 registers according to UART2_BaudRate value */
  107. BaudRate_Mantissa = ((uint32_t)CLK_GetClockFreq() / (BaudRate << 4));
  108. BaudRate_Mantissa100 = (((uint32_t)CLK_GetClockFreq() * 100) / (BaudRate << 4));
  109. /* The fraction and MSB mantissa should be loaded in one step in the BRR2 register*/
  110. /* Set the fraction of UARTDIV */
  111. BRR2_1 = (uint8_t)((uint8_t)(((BaudRate_Mantissa100 - (BaudRate_Mantissa * 100))
  112. << 4) / 100) & (uint8_t)0x0F);
  113. BRR2_2 = (uint8_t)((BaudRate_Mantissa >> 4) & (uint8_t)0xF0);
  114. UART2->BRR2 = (uint8_t)(BRR2_1 | BRR2_2);
  115. /* Set the LSB mantissa of UARTDIV */
  116. UART2->BRR1 = (uint8_t)BaudRate_Mantissa;
  117. /* Disable the Transmitter and Receiver before setting the LBCL, CPOL and CPHA bits */
  118. UART2->CR2 &= (uint8_t)~(UART2_CR2_TEN | UART2_CR2_REN);
  119. /* Clear the Clock Polarity, lock Phase, Last Bit Clock pulse */
  120. UART2->CR3 &= (uint8_t)~(UART2_CR3_CPOL | UART2_CR3_CPHA | UART2_CR3_LBCL);
  121. /* Set the Clock Polarity, lock Phase, Last Bit Clock pulse */
  122. UART2->CR3 |= (uint8_t)((uint8_t)SyncMode & (uint8_t)(UART2_CR3_CPOL | \
  123. UART2_CR3_CPHA | UART2_CR3_LBCL));
  124. if ((uint8_t)(Mode & UART2_MODE_TX_ENABLE))
  125. {
  126. /* Set the Transmitter Enable bit */
  127. UART2->CR2 |= (uint8_t)UART2_CR2_TEN;
  128. }
  129. else
  130. {
  131. /* Clear the Transmitter Disable bit */
  132. UART2->CR2 &= (uint8_t)(~UART2_CR2_TEN);
  133. }
  134. if ((uint8_t)(Mode & UART2_MODE_RX_ENABLE))
  135. {
  136. /* Set the Receiver Enable bit */
  137. UART2->CR2 |= (uint8_t)UART2_CR2_REN;
  138. }
  139. else
  140. {
  141. /* Clear the Receiver Disable bit */
  142. UART2->CR2 &= (uint8_t)(~UART2_CR2_REN);
  143. }
  144. /* Set the Clock Enable bit, lock Polarity, lock Phase and Last Bit Clock
  145. pulse bits according to UART2_Mode value */
  146. if ((uint8_t)(SyncMode & UART2_SYNCMODE_CLOCK_DISABLE))
  147. {
  148. /* Clear the Clock Enable bit */
  149. UART2->CR3 &= (uint8_t)(~UART2_CR3_CKEN);
  150. }
  151. else
  152. {
  153. UART2->CR3 |= (uint8_t)((uint8_t)SyncMode & UART2_CR3_CKEN);
  154. }
  155. }
  156. /**
  157. * @brief Enable the UART2 peripheral.
  158. * @param NewState : The new state of the UART Communication.
  159. * This parameter can be any of the @ref FunctionalState enumeration.
  160. * @retval None
  161. */
  162. void UART2_Cmd(FunctionalState NewState)
  163. {
  164. if (NewState != DISABLE)
  165. {
  166. /* UART2 Enable */
  167. UART2->CR1 &= (uint8_t)(~UART2_CR1_UARTD);
  168. }
  169. else
  170. {
  171. /* UART2 Disable */
  172. UART2->CR1 |= UART2_CR1_UARTD;
  173. }
  174. }
  175. /**
  176. * @brief Enables or disables the specified UART2 interrupts.
  177. * @param UART2_IT specifies the UART2 interrupt sources to be enabled or disabled.
  178. * This parameter can be one of the following values:
  179. * - UART2_IT_LBDF: LIN Break detection interrupt
  180. * - UART2_IT_LHDF: LIN Break detection interrupt
  181. * - UART2_IT_TXE: Transmit Data Register empty interrupt
  182. * - UART2_IT_TC: Transmission complete interrupt
  183. * - UART2_IT_RXNE_OR: Receive Data register not empty/Over run error interrupt
  184. * - UART2_IT_IDLE: Idle line detection interrupt
  185. * - UART2_IT_PE: Parity Error interrupt
  186. * @param NewState new state of the specified UART2 interrupts.
  187. * This parameter can be: ENABLE or DISABLE.
  188. * @retval None
  189. */
  190. void UART2_ITConfig(UART2_IT_TypeDef UART2_IT, FunctionalState NewState)
  191. {
  192. uint8_t uartreg = 0, itpos = 0x00;
  193. /* Check the parameters */
  194. assert_param(IS_UART2_CONFIG_IT_OK(UART2_IT));
  195. assert_param(IS_FUNCTIONALSTATE_OK(NewState));
  196. /* Get the UART2 register index */
  197. uartreg = (uint8_t)((uint16_t)UART2_IT >> 0x08);
  198. /* Get the UART2 IT index */
  199. itpos = (uint8_t)((uint8_t)1 << (uint8_t)((uint8_t)UART2_IT & (uint8_t)0x0F));
  200. if (NewState != DISABLE)
  201. {
  202. /* Enable the Interrupt bits according to UART2_IT mask */
  203. if (uartreg == 0x01)
  204. {
  205. UART2->CR1 |= itpos;
  206. }
  207. else if (uartreg == 0x02)
  208. {
  209. UART2->CR2 |= itpos;
  210. }
  211. else if (uartreg == 0x03)
  212. {
  213. UART2->CR4 |= itpos;
  214. }
  215. else
  216. {
  217. UART2->CR6 |= itpos;
  218. }
  219. }
  220. else
  221. {
  222. /* Disable the interrupt bits according to UART2_IT mask */
  223. if (uartreg == 0x01)
  224. {
  225. UART2->CR1 &= (uint8_t)(~itpos);
  226. }
  227. else if (uartreg == 0x02)
  228. {
  229. UART2->CR2 &= (uint8_t)(~itpos);
  230. }
  231. else if (uartreg == 0x03)
  232. {
  233. UART2->CR4 &= (uint8_t)(~itpos);
  234. }
  235. else
  236. {
  237. UART2->CR6 &= (uint8_t)(~itpos);
  238. }
  239. }
  240. }
  241. /**
  242. * @brief Configures the UART2’s IrDA interface.
  243. * @param UART2_IrDAMode specifies the IrDA mode.
  244. * This parameter can be any of the @ref UART2_IrDAMode_TypeDef values.
  245. * @retval None
  246. */
  247. void UART2_IrDAConfig(UART2_IrDAMode_TypeDef UART2_IrDAMode)
  248. {
  249. assert_param(IS_UART2_IRDAMODE_OK(UART2_IrDAMode));
  250. if (UART2_IrDAMode != UART2_IRDAMODE_NORMAL)
  251. {
  252. UART2->CR5 |= UART2_CR5_IRLP;
  253. }
  254. else
  255. {
  256. UART2->CR5 &= ((uint8_t)~UART2_CR5_IRLP);
  257. }
  258. }
  259. /**
  260. * @brief Enables or disables the UART2’s IrDA interface.
  261. * @param NewState new state of the IrDA mode.
  262. * This parameter can be: ENABLE or DISABLE.
  263. * @retval None
  264. */
  265. void UART2_IrDACmd(FunctionalState NewState)
  266. {
  267. /* Check parameters */
  268. assert_param(IS_FUNCTIONALSTATE_OK(NewState));
  269. if (NewState != DISABLE)
  270. {
  271. /* Enable the IrDA mode by setting the IREN bit in the CR3 register */
  272. UART2->CR5 |= UART2_CR5_IREN;
  273. }
  274. else
  275. {
  276. /* Disable the IrDA mode by clearing the IREN bit in the CR3 register */
  277. UART2->CR5 &= ((uint8_t)~UART2_CR5_IREN);
  278. }
  279. }
  280. /**
  281. * @brief Sets the UART2 LIN Break detection length.
  282. * @param UART2_LINBreakDetectionLength specifies the LIN break detection length.
  283. * This parameter can be any of the
  284. * @ref UART2_LINBreakDetectionLength_TypeDef values.
  285. * @retval None
  286. */
  287. void UART2_LINBreakDetectionConfig(UART2_LINBreakDetectionLength_TypeDef UART2_LINBreakDetectionLength)
  288. {
  289. /* Check parameters */
  290. assert_param(IS_UART2_LINBREAKDETECTIONLENGTH_OK(UART2_LINBreakDetectionLength));
  291. if (UART2_LINBreakDetectionLength != UART2_LINBREAKDETECTIONLENGTH_10BITS)
  292. {
  293. UART2->CR4 |= UART2_CR4_LBDL;
  294. }
  295. else
  296. {
  297. UART2->CR4 &= ((uint8_t)~UART2_CR4_LBDL);
  298. }
  299. }
  300. /**
  301. * @brief Configure the UART2 peripheral.
  302. * @param UART2_Mode specifies the LIN mode.
  303. * This parameter can be any of the @ref UART2_LinMode_TypeDef values.
  304. * @param UART2_Autosync specifies the LIN automatic resynchronization mode.
  305. * This parameter can be any of the @ref UART2_LinAutosync_TypeDef values.
  306. * @param UART2_DivUp specifies the LIN divider update method.
  307. * This parameter can be any of the @ref UART2_LinDivUp_TypeDef values.
  308. * @retval None
  309. */
  310. void UART2_LINConfig(UART2_LinMode_TypeDef UART2_Mode,
  311. UART2_LinAutosync_TypeDef UART2_Autosync,
  312. UART2_LinDivUp_TypeDef UART2_DivUp)
  313. {
  314. /* Check parameters */
  315. assert_param(IS_UART2_SLAVE_OK(UART2_Mode));
  316. assert_param(IS_UART2_AUTOSYNC_OK(UART2_Autosync));
  317. assert_param(IS_UART2_DIVUP_OK(UART2_DivUp));
  318. if (UART2_Mode != UART2_LIN_MODE_MASTER)
  319. {
  320. UART2->CR6 |= UART2_CR6_LSLV;
  321. }
  322. else
  323. {
  324. UART2->CR6 &= ((uint8_t)~UART2_CR6_LSLV);
  325. }
  326. if (UART2_Autosync != UART2_LIN_AUTOSYNC_DISABLE)
  327. {
  328. UART2->CR6 |= UART2_CR6_LASE ;
  329. }
  330. else
  331. {
  332. UART2->CR6 &= ((uint8_t)~ UART2_CR6_LASE );
  333. }
  334. if (UART2_DivUp != UART2_LIN_DIVUP_LBRR1)
  335. {
  336. UART2->CR6 |= UART2_CR6_LDUM;
  337. }
  338. else
  339. {
  340. UART2->CR6 &= ((uint8_t)~ UART2_CR6_LDUM);
  341. }
  342. }
  343. /**
  344. * @brief Enables or disables the UART2 LIN mode.
  345. * @param NewState is new state of the UART2 LIN mode.
  346. * This parameter can be ENABLE or DISABLE
  347. * @retval None
  348. */
  349. void UART2_LINCmd(FunctionalState NewState)
  350. {
  351. assert_param(IS_FUNCTIONALSTATE_OK(NewState));
  352. if (NewState != DISABLE)
  353. {
  354. /* Enable the LIN mode by setting the LINE bit in the CR2 register */
  355. UART2->CR3 |= UART2_CR3_LINEN;
  356. }
  357. else
  358. {
  359. /* Disable the LIN mode by clearing the LINE bit in the CR2 register */
  360. UART2->CR3 &= ((uint8_t)~UART2_CR3_LINEN);
  361. }
  362. }
  363. /**
  364. * @brief Enables or disables the UART2 Smart Card mode.
  365. * @param NewState: new state of the Smart Card mode.
  366. * This parameter can be: ENABLE or DISABLE.
  367. * @retval None
  368. */
  369. void UART2_SmartCardCmd(FunctionalState NewState)
  370. {
  371. /* Check parameters */
  372. assert_param(IS_FUNCTIONALSTATE_OK(NewState));
  373. if (NewState != DISABLE)
  374. {
  375. /* Enable the SC mode by setting the SCEN bit in the CR5 register */
  376. UART2->CR5 |= UART2_CR5_SCEN;
  377. }
  378. else
  379. {
  380. /* Disable the SC mode by clearing the SCEN bit in the CR5 register */
  381. UART2->CR5 &= ((uint8_t)(~UART2_CR5_SCEN));
  382. }
  383. }
  384. /**
  385. * @brief Enables or disables NACK transmission.
  386. * @param NewState: new state of the Smart Card mode.
  387. * This parameter can be: ENABLE or DISABLE.
  388. * @retval None
  389. */
  390. void UART2_SmartCardNACKCmd(FunctionalState NewState)
  391. {
  392. /* Check parameters */
  393. assert_param(IS_FUNCTIONALSTATE_OK(NewState));
  394. if (NewState != DISABLE)
  395. {
  396. /* Enable the NACK transmission by setting the NACK bit in the CR5 register */
  397. UART2->CR5 |= UART2_CR5_NACK;
  398. }
  399. else
  400. {
  401. /* Disable the NACK transmission by clearing the NACK bit in the CR5 register */
  402. UART2->CR5 &= ((uint8_t)~(UART2_CR5_NACK));
  403. }
  404. }
  405. /**
  406. * @brief Selects the UART2 WakeUp method.
  407. * @param UART2_WakeUp: specifies the UART2 wakeup method.
  408. * This parameter can be any of the @ref UART2_WakeUp_TypeDef values.
  409. * @retval None
  410. */
  411. void UART2_WakeUpConfig(UART2_WakeUp_TypeDef UART2_WakeUp)
  412. {
  413. assert_param(IS_UART2_WAKEUP_OK(UART2_WakeUp));
  414. UART2->CR1 &= ((uint8_t)~UART2_CR1_WAKE);
  415. UART2->CR1 |= (uint8_t)UART2_WakeUp;
  416. }
  417. /**
  418. * @brief Determines if the UART2 is in mute mode or not.
  419. * @param NewState: new state of the UART2 mode.
  420. * This parameter can be ENABLE or DISABLE
  421. * @retval None
  422. */
  423. void UART2_ReceiverWakeUpCmd(FunctionalState NewState)
  424. {
  425. assert_param(IS_FUNCTIONALSTATE_OK(NewState));
  426. if (NewState != DISABLE)
  427. {
  428. /* Enable the mute mode UART2 by setting the RWU bit in the CR2 register */
  429. UART2->CR2 |= UART2_CR2_RWU;
  430. }
  431. else
  432. {
  433. /* Disable the mute mode UART2 by clearing the RWU bit in the CR1 register */
  434. UART2->CR2 &= ((uint8_t)~UART2_CR2_RWU);
  435. }
  436. }
  437. /**
  438. * @brief Returns the most recent received data by the UART2 peripheral.
  439. * @param None
  440. * @retval Received Data
  441. */
  442. uint8_t UART2_ReceiveData8(void)
  443. {
  444. return ((uint8_t)UART2->DR);
  445. }
  446. /**
  447. * @brief Returns the most recent received data by the UART2 peripheral.
  448. * @param None
  449. * @retval Received Data
  450. */
  451. uint16_t UART2_ReceiveData9(void)
  452. {
  453. uint16_t temp = 0;
  454. temp = ((uint16_t)(((uint16_t)((uint16_t)UART2->CR1 & (uint16_t)UART2_CR1_R8)) << 1));
  455. return (uint16_t)((((uint16_t)UART2->DR) | temp) & ((uint16_t)0x01FF));
  456. }
  457. /**
  458. * @brief Transmits 8 bit data through the UART2 peripheral.
  459. * @param Data: the data to transmit.
  460. * @retval None
  461. */
  462. void UART2_SendData8(uint8_t Data)
  463. {
  464. /* Transmit Data */
  465. UART2->DR = Data;
  466. }
  467. /**
  468. * @brief Transmits 9 bit data through the UART2 peripheral.
  469. * @param Data: the data to transmit.
  470. * @retval None
  471. */
  472. void UART2_SendData9(uint16_t Data)
  473. {
  474. /* Clear the transmit data bit 8 */
  475. UART2->CR1 &= ((uint8_t)~UART2_CR1_T8);
  476. /* Write the transmit data bit [8] */
  477. UART2->CR1 |= (uint8_t)(((uint8_t)(Data >> 2)) & UART2_CR1_T8);
  478. /* Write the transmit data bit [0:7] */
  479. UART2->DR = (uint8_t)(Data);
  480. }
  481. /**
  482. * @brief Transmits break characters.
  483. * @param None
  484. * @retval None
  485. */
  486. void UART2_SendBreak(void)
  487. {
  488. UART2->CR2 |= UART2_CR2_SBK;
  489. }
  490. /**
  491. * @brief Sets the address of the UART2 node.
  492. * @param UART2_Address: Indicates the address of the UART2 node.
  493. * @retval None
  494. */
  495. void UART2_SetAddress(uint8_t UART2_Address)
  496. {
  497. /*assert_param for x UART2_Address*/
  498. assert_param(IS_UART2_ADDRESS_OK(UART2_Address));
  499. /* Clear the UART2 address */
  500. UART2->CR4 &= ((uint8_t)~UART2_CR4_ADD);
  501. /* Set the UART2 address node */
  502. UART2->CR4 |= UART2_Address;
  503. }
  504. /**
  505. * @brief Sets the specified UART2 guard time.
  506. * @note SmartCard Mode should be Enabled
  507. * @param UART2_GuardTime: specifies the guard time.
  508. * @retval None
  509. */
  510. void UART2_SetGuardTime(uint8_t UART2_GuardTime)
  511. {
  512. /* Set the UART2 guard time */
  513. UART2->GTR = UART2_GuardTime;
  514. }
  515. /**
  516. * @brief Sets the system clock prescaler.
  517. * @note IrDA Low Power mode or smartcard mode should be enabled
  518. * @note This function is related to SmartCard and IrDa mode.
  519. * @param UART2_Prescaler: specifies the prescaler clock.
  520. * This parameter can be one of the following values:
  521. * @par IrDA Low Power Mode
  522. * The clock source is divided by the value given in the register (8 bits)
  523. * - 0000 0000 Reserved
  524. * - 0000 0001 divides the clock source by 1
  525. * - 0000 0010 divides the clock source by 2
  526. * - ...
  527. * @par Smart Card Mode
  528. * The clock source is divided by the value given in the register
  529. * (5 significant bits) multiped by 2
  530. * - 0 0000 Reserved
  531. * - 0 0001 divides the clock source by 2
  532. * - 0 0010 divides the clock source by 4
  533. * - 0 0011 divides the clock source by 6
  534. * - ...
  535. * @retval None
  536. */
  537. void UART2_SetPrescaler(uint8_t UART2_Prescaler)
  538. {
  539. /* Load the UART2 prescaler value*/
  540. UART2->PSCR = UART2_Prescaler;
  541. }
  542. /**
  543. * @brief Checks whether the specified UART2 flag is set or not.
  544. * @param UART2_FLAG specifies the flag to check.
  545. * This parameter can be any of the @ref UART2_Flag_TypeDef enumeration.
  546. * @retval FlagStatus (SET or RESET)
  547. */
  548. FlagStatus UART2_GetFlagStatus(UART2_Flag_TypeDef UART2_FLAG)
  549. {
  550. FlagStatus status = RESET;
  551. /* Check parameters */
  552. assert_param(IS_UART2_FLAG_OK(UART2_FLAG));
  553. /* Check the status of the specified UART2 flag*/
  554. if (UART2_FLAG == UART2_FLAG_LBDF)
  555. {
  556. if ((UART2->CR4 & (uint8_t)UART2_FLAG) != (uint8_t)0x00)
  557. {
  558. /* UART2_FLAG is set*/
  559. status = SET;
  560. }
  561. else
  562. {
  563. /* UART2_FLAG is reset*/
  564. status = RESET;
  565. }
  566. }
  567. else if (UART2_FLAG == UART2_FLAG_SBK)
  568. {
  569. if ((UART2->CR2 & (uint8_t)UART2_FLAG) != (uint8_t)0x00)
  570. {
  571. /* UART2_FLAG is set*/
  572. status = SET;
  573. }
  574. else
  575. {
  576. /* UART2_FLAG is reset*/
  577. status = RESET;
  578. }
  579. }
  580. else if ((UART2_FLAG == UART2_FLAG_LHDF) || (UART2_FLAG == UART2_FLAG_LSF))
  581. {
  582. if ((UART2->CR6 & (uint8_t)UART2_FLAG) != (uint8_t)0x00)
  583. {
  584. /* UART2_FLAG is set*/
  585. status = SET;
  586. }
  587. else
  588. {
  589. /* UART2_FLAG is reset*/
  590. status = RESET;
  591. }
  592. }
  593. else
  594. {
  595. if ((UART2->SR & (uint8_t)UART2_FLAG) != (uint8_t)0x00)
  596. {
  597. /* UART2_FLAG is set*/
  598. status = SET;
  599. }
  600. else
  601. {
  602. /* UART2_FLAG is reset*/
  603. status = RESET;
  604. }
  605. }
  606. /* Return the UART2_FLAG status*/
  607. return status;
  608. }
  609. /**
  610. * @brief Clears the UART2 flags.
  611. * @param UART2_FLAG specifies the flag to clear
  612. * This parameter can be any combination of the following values:
  613. * - UART2_FLAG_LBDF: LIN Break detection flag.
  614. * - UART2_FLAG_LHDF: LIN Header detection flag.
  615. * - UART2_FLAG_LSF: LIN synchrone field flag.
  616. * - UART2_FLAG_RXNE: Receive data register not empty flag.
  617. * @note:
  618. * - PE (Parity error), FE (Framing error), NE (Noise error),
  619. * OR (OverRun error) and IDLE (Idle line detected) flags are cleared
  620. * by software sequence: a read operation to UART2_SR register
  621. * (UART2_GetFlagStatus())followed by a read operation to UART2_DR
  622. * register(UART2_ReceiveData8() or UART2_ReceiveData9()).
  623. *
  624. * - RXNE flag can be also cleared by a read to the UART2_DR register
  625. * (UART2_ReceiveData8()or UART2_ReceiveData9()).
  626. *
  627. * - TC flag can be also cleared by software sequence: a read operation
  628. * to UART2_SR register (UART2_GetFlagStatus()) followed by a write
  629. * operation to UART2_DR register (UART2_SendData8() or UART2_SendData9()).
  630. *
  631. * - TXE flag is cleared only by a write to the UART2_DR register
  632. * (UART2_SendData8() or UART2_SendData9()).
  633. *
  634. * - SBK flag is cleared during the stop bit of break.
  635. * @retval None
  636. */
  637. void UART2_ClearFlag(UART2_Flag_TypeDef UART2_FLAG)
  638. {
  639. assert_param(IS_UART2_CLEAR_FLAG_OK(UART2_FLAG));
  640. /* Clear the Receive Register Not Empty flag */
  641. if (UART2_FLAG == UART2_FLAG_RXNE)
  642. {
  643. UART2->SR = (uint8_t)~(UART2_SR_RXNE);
  644. }
  645. /* Clear the LIN Break Detection flag */
  646. else if (UART2_FLAG == UART2_FLAG_LBDF)
  647. {
  648. UART2->CR4 &= (uint8_t)(~UART2_CR4_LBDF);
  649. }
  650. /* Clear the LIN Header Detection Flag */
  651. else if (UART2_FLAG == UART2_FLAG_LHDF)
  652. {
  653. UART2->CR6 &= (uint8_t)(~UART2_CR6_LHDF);
  654. }
  655. /* Clear the LIN Synch Field flag */
  656. else
  657. {
  658. UART2->CR6 &= (uint8_t)(~UART2_CR6_LSF);
  659. }
  660. }
  661. /**
  662. * @brief Checks whether the specified UART2 interrupt has occurred or not.
  663. * @param UART2_IT: Specifies the UART2 interrupt pending bit to check.
  664. * This parameter can be one of the following values:
  665. * - UART2_IT_LBDF: LIN Break detection interrupt
  666. * - UART2_IT_TXE: Transmit Data Register empty interrupt
  667. * - UART2_IT_TC: Transmission complete interrupt
  668. * - UART2_IT_RXNE: Receive Data register not empty interrupt
  669. * - UART2_IT_IDLE: Idle line detection interrupt
  670. * - UART2_IT_OR: OverRun Error interrupt
  671. * - UART2_IT_PE: Parity Error interrupt
  672. * @retval The state of UART2_IT (SET or RESET).
  673. */
  674. ITStatus UART2_GetITStatus(UART2_IT_TypeDef UART2_IT)
  675. {
  676. ITStatus pendingbitstatus = RESET;
  677. uint8_t itpos = 0;
  678. uint8_t itmask1 = 0;
  679. uint8_t itmask2 = 0;
  680. uint8_t enablestatus = 0;
  681. /* Check parameters */
  682. assert_param(IS_UART2_GET_IT_OK(UART2_IT));
  683. /* Get the UART2 IT index*/
  684. itpos = (uint8_t)((uint8_t)1 << (uint8_t)((uint8_t)UART2_IT & (uint8_t)0x0F));
  685. /* Get the UART2 IT index*/
  686. itmask1 = (uint8_t)((uint8_t)UART2_IT >> (uint8_t)4);
  687. /* Set the IT mask*/
  688. itmask2 = (uint8_t)((uint8_t)1 << itmask1);
  689. /* Check the status of the specified UART2 pending bit*/
  690. if (UART2_IT == UART2_IT_PE)
  691. {
  692. /* Get the UART2_ITPENDINGBIT enable bit status*/
  693. enablestatus = (uint8_t)((uint8_t)UART2->CR1 & itmask2);
  694. /* Check the status of the specified UART2 interrupt*/
  695. if (((UART2->SR & itpos) != (uint8_t)0x00) && enablestatus)
  696. {
  697. /* Interrupt occurred*/
  698. pendingbitstatus = SET;
  699. }
  700. else
  701. {
  702. /* Interrupt not occurred*/
  703. pendingbitstatus = RESET;
  704. }
  705. }
  706. else if (UART2_IT == UART2_IT_LBDF)
  707. {
  708. /* Get the UART2_IT enable bit status*/
  709. enablestatus = (uint8_t)((uint8_t)UART2->CR4 & itmask2);
  710. /* Check the status of the specified UART2 interrupt*/
  711. if (((UART2->CR4 & itpos) != (uint8_t)0x00) && enablestatus)
  712. {
  713. /* Interrupt occurred*/
  714. pendingbitstatus = SET;
  715. }
  716. else
  717. {
  718. /* Interrupt not occurred*/
  719. pendingbitstatus = RESET;
  720. }
  721. }
  722. else if (UART2_IT == UART2_IT_LHDF)
  723. {
  724. /* Get the UART2_IT enable bit status*/
  725. enablestatus = (uint8_t)((uint8_t)UART2->CR6 & itmask2);
  726. /* Check the status of the specified UART2 interrupt*/
  727. if (((UART2->CR6 & itpos) != (uint8_t)0x00) && enablestatus)
  728. {
  729. /* Interrupt occurred*/
  730. pendingbitstatus = SET;
  731. }
  732. else
  733. {
  734. /* Interrupt not occurred*/
  735. pendingbitstatus = RESET;
  736. }
  737. }
  738. else
  739. {
  740. /* Get the UART2_IT enable bit status*/
  741. enablestatus = (uint8_t)((uint8_t)UART2->CR2 & itmask2);
  742. /* Check the status of the specified UART2 interrupt*/
  743. if (((UART2->SR & itpos) != (uint8_t)0x00) && enablestatus)
  744. {
  745. /* Interrupt occurred*/
  746. pendingbitstatus = SET;
  747. }
  748. else
  749. {
  750. /* Interrupt not occurred*/
  751. pendingbitstatus = RESET;
  752. }
  753. }
  754. /* Return the UART2_IT status*/
  755. return pendingbitstatus;
  756. }
  757. /**
  758. * @brief Clears the UART2 pending flags.
  759. * @param UART2_IT specifies the pending bit to clear
  760. * This parameter can be one of the following values:
  761. * - UART2_IT_LBDF: LIN Break detection interrupt
  762. * - UART2_IT_LHDF: LIN Header detection interrupt
  763. * - UART2_IT_RXNE: Receive Data register not empty interrupt.
  764. * @note
  765. * - PE (Parity error), FE (Framing error), NE (Noise error),
  766. * OR (OverRun error) and IDLE (Idle line detected) pending bits are
  767. * cleared by software sequence: a read operation to UART2_SR register
  768. * (UART2_GetITStatus()) followed by a read operation to UART2_DR register
  769. * (UART2_ReceiveData8() or UART2_ReceiveData9()).
  770. *
  771. * - RXNE pending bit can be also cleared by a read to the UART2_DR
  772. * register (UART2_ReceiveData8() or UART2_ReceiveData9()).
  773. *
  774. * - TC (Transmit complete) pending bit can be cleared by software
  775. * sequence: a read operation to UART2_SR register
  776. * (UART2_GetITStatus()) followed by a write operation to UART2_DR
  777. * register (UART2_SendData8()or UART2_SendData9()).
  778. *
  779. * - TXE pending bit is cleared only by a write to the UART2_DR register
  780. * (UART2_SendData8() or UART2_SendData9()).
  781. * @retval None
  782. */
  783. void UART2_ClearITPendingBit(UART2_IT_TypeDef UART2_IT)
  784. {
  785. assert_param(IS_UART2_CLEAR_IT_OK(UART2_IT));
  786. /* Clear the Receive Register Not Empty pending bit */
  787. if (UART2_IT == UART2_IT_RXNE)
  788. {
  789. UART2->SR = (uint8_t)~(UART2_SR_RXNE);
  790. }
  791. /* Clear the LIN Break Detection pending bit */
  792. else if (UART2_IT == UART2_IT_LBDF)
  793. {
  794. UART2->CR4 &= (uint8_t)~(UART2_CR4_LBDF);
  795. }
  796. /* Clear the LIN Header Detection pending bit */
  797. else
  798. {
  799. UART2->CR6 &= (uint8_t)(~UART2_CR6_LHDF);
  800. }
  801. }
  802. /**
  803. * @}
  804. */
  805. /**
  806. * @}
  807. */
  808. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/