stm8s_uart4.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902
  1. /**
  2. ********************************************************************************
  3. * @file stm8s_uart4.c
  4. * @author MCD Application Team
  5. * @version V2.2.0
  6. * @date 30-September-2014
  7. * @brief This file contains all the functions for the UART4 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_uart4.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 UART4_Public_Functions
  41. * @{
  42. */
  43. /**
  44. * @brief Deinitializes the UART peripheral.
  45. * @param None
  46. * @retval None
  47. */
  48. void UART4_DeInit(void)
  49. {
  50. /* Clear the Idle Line Detected bit in the status register by a read
  51. to the UART4_SR register followed by a Read to the UART4_DR register */
  52. (void)UART4->SR;
  53. (void)UART4->DR;
  54. UART4->BRR2 = UART4_BRR2_RESET_VALUE; /* Set UART4_BRR2 to reset value 0x00 */
  55. UART4->BRR1 = UART4_BRR1_RESET_VALUE; /* Set UART4_BRR1 to reset value 0x00 */
  56. UART4->CR1 = UART4_CR1_RESET_VALUE; /* Set UART4_CR1 to reset value 0x00 */
  57. UART4->CR2 = UART4_CR2_RESET_VALUE; /* Set UART4_CR2 to reset value 0x00 */
  58. UART4->CR3 = UART4_CR3_RESET_VALUE; /* Set UART4_CR3 to reset value 0x00 */
  59. UART4->CR4 = UART4_CR4_RESET_VALUE; /* Set UART4_CR4 to reset value 0x00 */
  60. UART4->CR5 = UART4_CR5_RESET_VALUE; /* Set UART4_CR5 to reset value 0x00 */
  61. UART4->CR6 = UART4_CR6_RESET_VALUE; /* Set UART4_CR6 to reset value 0x00 */
  62. }
  63. /**
  64. * @brief Initializes the UART4 according to the specified parameters.
  65. * @param BaudRate: The baudrate.
  66. * @param WordLength : This parameter can be any of the
  67. * @ref UART4_WordLength_TypeDef enumeration.
  68. * @param StopBits: This parameter can be any of the
  69. * @ref UART4_StopBits_TypeDef enumeration.
  70. * @param Parity: This parameter can be any of the
  71. * @ref UART4_Parity_TypeDef enumeration.
  72. * @param SyncMode: This parameter can be any of the
  73. * @ref UART4_SyncMode_TypeDef values.
  74. * @param Mode: This parameter can be any of the @ref UART4_Mode_TypeDef values
  75. * @retval None
  76. */
  77. void UART4_Init(uint32_t BaudRate, UART4_WordLength_TypeDef WordLength, UART4_StopBits_TypeDef StopBits, UART4_Parity_TypeDef Parity, UART4_SyncMode_TypeDef SyncMode, UART4_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_UART4_BAUDRATE_OK(BaudRate));
  83. assert_param(IS_UART4_WORDLENGTH_OK(WordLength));
  84. assert_param(IS_UART4_STOPBITS_OK(StopBits));
  85. assert_param(IS_UART4_PARITY_OK(Parity));
  86. assert_param(IS_UART4_MODE_OK((uint8_t)Mode));
  87. assert_param(IS_UART4_SYNCMODE_OK((uint8_t)SyncMode));
  88. /* Clear the word length bit */
  89. UART4->CR1 &= (uint8_t)(~UART4_CR1_M);
  90. /* Set the word length bit according to UART4_WordLength value */
  91. UART4->CR1 |= (uint8_t)WordLength;
  92. /* Clear the STOP bits */
  93. UART4->CR3 &= (uint8_t)(~UART4_CR3_STOP);
  94. /* Set the STOP bits number according to UART4_StopBits value */
  95. UART4->CR3 |= (uint8_t)StopBits;
  96. /* Clear the Parity Control bit */
  97. UART4->CR1 &= (uint8_t)(~(UART4_CR1_PCEN | UART4_CR1_PS ));
  98. /* Set the Parity Control bit to UART4_Parity value */
  99. UART4->CR1 |= (uint8_t)Parity;
  100. /* Clear the LSB mantissa of UART4DIV */
  101. UART4->BRR1 &= (uint8_t)(~UART4_BRR1_DIVM);
  102. /* Clear the MSB mantissa of UART4DIV */
  103. UART4->BRR2 &= (uint8_t)(~UART4_BRR2_DIVM);
  104. /* Clear the Fraction bits of UART4DIV */
  105. UART4->BRR2 &= (uint8_t)(~UART4_BRR2_DIVF);
  106. /* Set the UART4 BaudRates in BRR1 and BRR2 registers according to UART4_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. UART4->BRR2 = (uint8_t)(BRR2_1 | BRR2_2);
  115. /* Set the LSB mantissa of UARTDIV */
  116. UART4->BRR1 = (uint8_t)BaudRate_Mantissa;
  117. /* Disable the Transmitter and Receiver before setting the LBCL, CPOL and CPHA bits */
  118. UART4->CR2 &= (uint8_t)~(UART4_CR2_TEN | UART4_CR2_REN);
  119. /* Clear the Clock Polarity, lock Phase, Last Bit Clock pulse */
  120. UART4->CR3 &= (uint8_t)~(UART4_CR3_CPOL | UART4_CR3_CPHA | UART4_CR3_LBCL);
  121. /* Set the Clock Polarity, lock Phase, Last Bit Clock pulse */
  122. UART4->CR3 |= (uint8_t)((uint8_t)SyncMode & (uint8_t)(UART4_CR3_CPOL | \
  123. UART4_CR3_CPHA | UART4_CR3_LBCL));
  124. if((uint8_t)(Mode & UART4_MODE_TX_ENABLE))
  125. {
  126. /* Set the Transmitter Enable bit */
  127. UART4->CR2 |= (uint8_t)UART4_CR2_TEN;
  128. }
  129. else
  130. {
  131. /* Clear the Transmitter Disable bit */
  132. UART4->CR2 &= (uint8_t)(~UART4_CR2_TEN);
  133. }
  134. if((uint8_t)(Mode & UART4_MODE_RX_ENABLE))
  135. {
  136. /* Set the Receiver Enable bit */
  137. UART4->CR2 |= (uint8_t)UART4_CR2_REN;
  138. }
  139. else
  140. {
  141. /* Clear the Receiver Disable bit */
  142. UART4->CR2 &= (uint8_t)(~UART4_CR2_REN);
  143. }
  144. /* Set the Clock Enable bit, lock Polarity, lock Phase and Last Bit Clock
  145. pulse bits according to UART4_Mode value */
  146. if((uint8_t)(SyncMode & UART4_SYNCMODE_CLOCK_DISABLE))
  147. {
  148. /* Clear the Clock Enable bit */
  149. UART4->CR3 &= (uint8_t)(~UART4_CR3_CKEN);
  150. }
  151. else
  152. {
  153. UART4->CR3 |= (uint8_t)((uint8_t)SyncMode & UART4_CR3_CKEN);
  154. }
  155. }
  156. /**
  157. * @brief Enable the UART4 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 UART4_Cmd(FunctionalState NewState)
  163. {
  164. if(NewState != DISABLE)
  165. {
  166. /* UART4 Enable */
  167. UART4->CR1 &= (uint8_t)(~UART4_CR1_UARTD);
  168. }
  169. else
  170. {
  171. /* UART4 Disable */
  172. UART4->CR1 |= UART4_CR1_UARTD;
  173. }
  174. }
  175. /**
  176. * @brief Enables or disables the specified UART4 interrupts.
  177. * @param UART4_IT specifies the UART4 interrupt sources to be enabled or disabled.
  178. * This parameter can be one of the following values:
  179. * - UART4_IT_LBDF: LIN Break detection interrupt
  180. * - UART4_IT_LHDF: LIN Break detection interrupt
  181. * - UART4_IT_TXE: Transmit Data Register empty interrupt
  182. * - UART4_IT_TC: Transmission complete interrupt
  183. * - UART4_IT_RXNE_OR: Receive Data register not empty/Over run error interrupt
  184. * - UART4_IT_IDLE: Idle line detection interrupt
  185. * - UART4_IT_PE: Parity Error interrupt
  186. * @param NewState new state of the specified UART4 interrupts.
  187. * This parameter can be: ENABLE or DISABLE.
  188. * @retval None
  189. */
  190. void UART4_ITConfig(UART4_IT_TypeDef UART4_IT, FunctionalState NewState)
  191. {
  192. uint8_t uartreg = 0, itpos = 0x00;
  193. /* Check the parameters */
  194. assert_param(IS_UART4_CONFIG_IT_OK(UART4_IT));
  195. assert_param(IS_FUNCTIONALSTATE_OK(NewState));
  196. /* Get the UART4 register index */
  197. uartreg = (uint8_t)((uint16_t)UART4_IT >> 0x08);
  198. /* Get the UART4 IT index */
  199. itpos = (uint8_t)((uint8_t)1 << (uint8_t)((uint8_t)UART4_IT & (uint8_t)0x0F));
  200. if(NewState != DISABLE)
  201. {
  202. /* Enable the Interrupt bits according to UART4_IT mask */
  203. if(uartreg == 0x01)
  204. {
  205. UART4->CR1 |= itpos;
  206. }
  207. else if(uartreg == 0x02)
  208. {
  209. UART4->CR2 |= itpos;
  210. }
  211. else if(uartreg == 0x03)
  212. {
  213. UART4->CR4 |= itpos;
  214. }
  215. else
  216. {
  217. UART4->CR6 |= itpos;
  218. }
  219. }
  220. else
  221. {
  222. /* Disable the interrupt bits according to UART4_IT mask */
  223. if(uartreg == 0x01)
  224. {
  225. UART4->CR1 &= (uint8_t)(~itpos);
  226. }
  227. else if(uartreg == 0x02)
  228. {
  229. UART4->CR2 &= (uint8_t)(~itpos);
  230. }
  231. else if(uartreg == 0x03)
  232. {
  233. UART4->CR4 &= (uint8_t)(~itpos);
  234. }
  235. else
  236. {
  237. UART4->CR6 &= (uint8_t)(~itpos);
  238. }
  239. }
  240. }
  241. /**
  242. * @brief Enables or disables the UART’s Half Duplex communication.
  243. * @param NewState new state of the UART Communication.
  244. * This parameter can be: ENABLE or DISABLE.
  245. * @retval None
  246. */
  247. void UART4_HalfDuplexCmd(FunctionalState NewState)
  248. {
  249. assert_param(IS_FUNCTIONALSTATE_OK(NewState));
  250. if (NewState != DISABLE)
  251. {
  252. UART4->CR5 |= UART4_CR5_HDSEL; /**< UART4 Half Duplex Enable */
  253. }
  254. else
  255. {
  256. UART4->CR5 &= (uint8_t)~UART4_CR5_HDSEL; /**< UART4 Half Duplex Disable */
  257. }
  258. }
  259. /**
  260. * @brief Configures the UART4’s IrDA interface.
  261. * @param UART4_IrDAMode specifies the IrDA mode.
  262. * This parameter can be any of the @ref UART4_IrDAMode_TypeDef values.
  263. * @retval None
  264. */
  265. void UART4_IrDAConfig(UART4_IrDAMode_TypeDef UART4_IrDAMode)
  266. {
  267. assert_param(IS_UART4_IRDAMODE_OK(UART4_IrDAMode));
  268. if(UART4_IrDAMode != UART4_IRDAMODE_NORMAL)
  269. {
  270. UART4->CR5 |= UART4_CR5_IRLP;
  271. }
  272. else
  273. {
  274. UART4->CR5 &= ((uint8_t)~UART4_CR5_IRLP);
  275. }
  276. }
  277. /**
  278. * @brief Enables or disables the UART4’s IrDA interface.
  279. * @param NewState new state of the IrDA mode.
  280. * This parameter can be: ENABLE or DISABLE.
  281. * @retval None
  282. */
  283. void UART4_IrDACmd(FunctionalState NewState)
  284. {
  285. /* Check parameters */
  286. assert_param(IS_FUNCTIONALSTATE_OK(NewState));
  287. if(NewState != DISABLE)
  288. {
  289. /* Enable the IrDA mode by setting the IREN bit in the CR3 register */
  290. UART4->CR5 |= UART4_CR5_IREN;
  291. }
  292. else
  293. {
  294. /* Disable the IrDA mode by clearing the IREN bit in the CR3 register */
  295. UART4->CR5 &= ((uint8_t)~UART4_CR5_IREN);
  296. }
  297. }
  298. /**
  299. * @brief Sets the UART4 LIN Break detection length.
  300. * @param UART4_LINBreakDetectionLength specifies the LIN break detection length.
  301. * This parameter can be any of the
  302. * @ref UART4_LINBreakDetectionLength_TypeDef values.
  303. * @retval None
  304. */
  305. void UART4_LINBreakDetectionConfig(UART4_LINBreakDetectionLength_TypeDef UART4_LINBreakDetectionLength)
  306. {
  307. /* Check parameters */
  308. assert_param(IS_UART4_LINBREAKDETECTIONLENGTH_OK(UART4_LINBreakDetectionLength));
  309. if(UART4_LINBreakDetectionLength != UART4_LINBREAKDETECTIONLENGTH_10BITS)
  310. {
  311. UART4->CR4 |= UART4_CR4_LBDL;
  312. }
  313. else
  314. {
  315. UART4->CR4 &= ((uint8_t)~UART4_CR4_LBDL);
  316. }
  317. }
  318. /**
  319. * @brief Configure the UART4 peripheral.
  320. * @param UART4_Mode specifies the LIN mode.
  321. * This parameter can be any of the @ref UART4_LinMode_TypeDef values.
  322. * @param UART4_Autosync specifies the LIN automatic resynchronization mode.
  323. * This parameter can be any of the @ref UART4_LinAutosync_TypeDef values.
  324. * @param UART4_DivUp specifies the LIN divider update method.
  325. * This parameter can be any of the @ref UART4_LinDivUp_TypeDef values.
  326. * @retval None
  327. */
  328. void UART4_LINConfig(UART4_LinMode_TypeDef UART4_Mode,
  329. UART4_LinAutosync_TypeDef UART4_Autosync,
  330. UART4_LinDivUp_TypeDef UART4_DivUp)
  331. {
  332. /* Check parameters */
  333. assert_param(IS_UART4_SLAVE_OK(UART4_Mode));
  334. assert_param(IS_UART4_AUTOSYNC_OK(UART4_Autosync));
  335. assert_param(IS_UART4_DIVUP_OK(UART4_DivUp));
  336. if(UART4_Mode != UART4_LIN_MODE_MASTER)
  337. {
  338. UART4->CR6 |= UART4_CR6_LSLV;
  339. }
  340. else
  341. {
  342. UART4->CR6 &= ((uint8_t)~UART4_CR6_LSLV);
  343. }
  344. if(UART4_Autosync != UART4_LIN_AUTOSYNC_DISABLE)
  345. {
  346. UART4->CR6 |= UART4_CR6_LASE ;
  347. }
  348. else
  349. {
  350. UART4->CR6 &= ((uint8_t)~ UART4_CR6_LASE );
  351. }
  352. if(UART4_DivUp != UART4_LIN_DIVUP_LBRR1)
  353. {
  354. UART4->CR6 |= UART4_CR6_LDUM;
  355. }
  356. else
  357. {
  358. UART4->CR6 &= ((uint8_t)~ UART4_CR6_LDUM);
  359. }
  360. }
  361. /**
  362. * @brief Enables or disables the UART4 LIN mode.
  363. * @param NewState is new state of the UART4 LIN mode.
  364. * This parameter can be ENABLE or DISABLE
  365. * @retval None
  366. */
  367. void UART4_LINCmd(FunctionalState NewState)
  368. {
  369. assert_param(IS_FUNCTIONALSTATE_OK(NewState));
  370. if(NewState != DISABLE)
  371. {
  372. /* Enable the LIN mode by setting the LINE bit in the CR2 register */
  373. UART4->CR3 |= UART4_CR3_LINEN;
  374. }
  375. else
  376. {
  377. /* Disable the LIN mode by clearing the LINE bit in the CR2 register */
  378. UART4->CR3 &= ((uint8_t)~UART4_CR3_LINEN);
  379. }
  380. }
  381. /**
  382. * @brief Enables or disables the UART4 Smart Card mode.
  383. * @param NewState: new state of the Smart Card mode.
  384. * This parameter can be: ENABLE or DISABLE.
  385. * @retval None
  386. */
  387. void UART4_SmartCardCmd(FunctionalState NewState)
  388. {
  389. /* Check parameters */
  390. assert_param(IS_FUNCTIONALSTATE_OK(NewState));
  391. if(NewState != DISABLE)
  392. {
  393. /* Enable the SC mode by setting the SCEN bit in the CR5 register */
  394. UART4->CR5 |= UART4_CR5_SCEN;
  395. }
  396. else
  397. {
  398. /* Disable the SC mode by clearing the SCEN bit in the CR5 register */
  399. UART4->CR5 &= ((uint8_t)(~UART4_CR5_SCEN));
  400. }
  401. }
  402. /**
  403. * @brief Enables or disables NACK transmission.
  404. * @param NewState: new state of the Smart Card mode.
  405. * This parameter can be: ENABLE or DISABLE.
  406. * @retval None
  407. */
  408. void UART4_SmartCardNACKCmd(FunctionalState NewState)
  409. {
  410. /* Check parameters */
  411. assert_param(IS_FUNCTIONALSTATE_OK(NewState));
  412. if(NewState != DISABLE)
  413. {
  414. /* Enable the NACK transmission by setting the NACK bit in the CR5 register */
  415. UART4->CR5 |= UART4_CR5_NACK;
  416. }
  417. else
  418. {
  419. /* Disable the NACK transmission by clearing the NACK bit in the CR5 register */
  420. UART4->CR5 &= ((uint8_t)~(UART4_CR5_NACK));
  421. }
  422. }
  423. /**
  424. * @brief Selects the UART4 WakeUp method.
  425. * @param UART4_WakeUp: specifies the UART4 wakeup method.
  426. * This parameter can be any of the @ref UART4_WakeUp_TypeDef values.
  427. * @retval None
  428. */
  429. void UART4_WakeUpConfig(UART4_WakeUp_TypeDef UART4_WakeUp)
  430. {
  431. assert_param(IS_UART4_WAKEUP_OK(UART4_WakeUp));
  432. UART4->CR1 &= ((uint8_t)~UART4_CR1_WAKE);
  433. UART4->CR1 |= (uint8_t)UART4_WakeUp;
  434. }
  435. /**
  436. * @brief Determines if the UART4 is in mute mode or not.
  437. * @param NewState: new state of the UART4 mode.
  438. * This parameter can be ENABLE or DISABLE
  439. * @retval None
  440. */
  441. void UART4_ReceiverWakeUpCmd(FunctionalState NewState)
  442. {
  443. assert_param(IS_FUNCTIONALSTATE_OK(NewState));
  444. if(NewState != DISABLE)
  445. {
  446. /* Enable the mute mode UART4 by setting the RWU bit in the CR2 register */
  447. UART4->CR2 |= UART4_CR2_RWU;
  448. }
  449. else
  450. {
  451. /* Disable the mute mode UART4 by clearing the RWU bit in the CR1 register */
  452. UART4->CR2 &= ((uint8_t)~UART4_CR2_RWU);
  453. }
  454. }
  455. /**
  456. * @brief Returns the most recent received data by the UART4 peripheral.
  457. * @param None
  458. * @retval Received Data
  459. */
  460. uint8_t UART4_ReceiveData8(void)
  461. {
  462. return ((uint8_t)UART4->DR);
  463. }
  464. /**
  465. * @brief Returns the most recent received data by the UART4 peripheral.
  466. * @param None
  467. * @retval Received Data
  468. */
  469. uint16_t UART4_ReceiveData9(void)
  470. {
  471. uint16_t temp = 0;
  472. temp = ((uint16_t)(((uint16_t)((uint16_t)UART4->CR1 & (uint16_t)UART4_CR1_R8)) << 1));
  473. return (uint16_t)((((uint16_t)UART4->DR) | temp) & ((uint16_t)0x01FF));
  474. }
  475. /**
  476. * @brief Transmits 8 bit data through the UART4 peripheral.
  477. * @param Data: the data to transmit.
  478. * @retval None
  479. */
  480. void UART4_SendData8(uint8_t Data)
  481. {
  482. /* Transmit Data */
  483. UART4->DR = Data;
  484. }
  485. /**
  486. * @brief Transmits 9 bit data through the UART4 peripheral.
  487. * @param Data: the data to transmit.
  488. * @retval None
  489. */
  490. void UART4_SendData9(uint16_t Data)
  491. {
  492. /* Clear the transmit data bit 8 */
  493. UART4->CR1 &= ((uint8_t)~UART4_CR1_T8);
  494. /* Write the transmit data bit [8] */
  495. UART4->CR1 |= (uint8_t)(((uint8_t)(Data >> 2)) & UART4_CR1_T8);
  496. /* Write the transmit data bit [0:7] */
  497. UART4->DR = (uint8_t)(Data);
  498. }
  499. /**
  500. * @brief Transmits break characters.
  501. * @param None
  502. * @retval None
  503. */
  504. void UART4_SendBreak(void)
  505. {
  506. UART4->CR2 |= UART4_CR2_SBK;
  507. }
  508. /**
  509. * @brief Sets the address of the UART4 node.
  510. * @param UART4_Address: Indicates the address of the UART4 node.
  511. * @retval None
  512. */
  513. void UART4_SetAddress(uint8_t UART4_Address)
  514. {
  515. /*assert_param for x UART4_Address*/
  516. assert_param(IS_UART4_ADDRESS_OK(UART4_Address));
  517. /* Clear the UART4 address */
  518. UART4->CR4 &= ((uint8_t)~UART4_CR4_ADD);
  519. /* Set the UART4 address node */
  520. UART4->CR4 |= UART4_Address;
  521. }
  522. /**
  523. * @brief Sets the specified UART4 guard time.
  524. * @note SmartCard Mode should be Enabled
  525. * @param UART4_GuardTime: specifies the guard time.
  526. * @retval None
  527. */
  528. void UART4_SetGuardTime(uint8_t UART4_GuardTime)
  529. {
  530. /* Set the UART4 guard time */
  531. UART4->GTR = UART4_GuardTime;
  532. }
  533. /**
  534. * @brief Sets the system clock prescaler.
  535. * @note IrDA Low Power mode or smartcard mode should be enabled
  536. * @note This function is related to SmartCard and IrDa mode.
  537. * @param UART4_Prescaler: specifies the prescaler clock.
  538. * This parameter can be one of the following values:
  539. * @par IrDA Low Power Mode
  540. * The clock source is divided by the value given in the register (8 bits)
  541. * - 0000 0000 Reserved
  542. * - 0000 0001 divides the clock source by 1
  543. * - 0000 0010 divides the clock source by 2
  544. * - ...
  545. * @par Smart Card Mode
  546. * The clock source is divided by the value given in the register
  547. * (5 significant bits) multiplied by 2
  548. * - 0 0000 Reserved
  549. * - 0 0001 divides the clock source by 2
  550. * - 0 0010 divides the clock source by 4
  551. * - 0 0011 divides the clock source by 6
  552. * - ...
  553. * @retval None
  554. */
  555. void UART4_SetPrescaler(uint8_t UART4_Prescaler)
  556. {
  557. /* Load the UART4 prescaler value*/
  558. UART4->PSCR = UART4_Prescaler;
  559. }
  560. /**
  561. * @brief Checks whether the specified UART4 flag is set or not.
  562. * @param UART4_FLAG specifies the flag to check.
  563. * This parameter can be any of the @ref UART4_Flag_TypeDef enumeration.
  564. * @retval FlagStatus (SET or RESET)
  565. */
  566. FlagStatus UART4_GetFlagStatus(UART4_Flag_TypeDef UART4_FLAG)
  567. {
  568. FlagStatus status = RESET;
  569. /* Check parameters */
  570. assert_param(IS_UART4_FLAG_OK(UART4_FLAG));
  571. /* Check the status of the specified UART4 flag*/
  572. if(UART4_FLAG == UART4_FLAG_LBDF)
  573. {
  574. if((UART4->CR4 & (uint8_t)UART4_FLAG) != (uint8_t)0x00)
  575. {
  576. /* UART4_FLAG is set*/
  577. status = SET;
  578. }
  579. else
  580. {
  581. /* UART4_FLAG is reset*/
  582. status = RESET;
  583. }
  584. }
  585. else if(UART4_FLAG == UART4_FLAG_SBK)
  586. {
  587. if((UART4->CR2 & (uint8_t)UART4_FLAG) != (uint8_t)0x00)
  588. {
  589. /* UART4_FLAG is set*/
  590. status = SET;
  591. }
  592. else
  593. {
  594. /* UART4_FLAG is reset*/
  595. status = RESET;
  596. }
  597. }
  598. else if((UART4_FLAG == UART4_FLAG_LHDF) || (UART4_FLAG == UART4_FLAG_LSF))
  599. {
  600. if((UART4->CR6 & (uint8_t)UART4_FLAG) != (uint8_t)0x00)
  601. {
  602. /* UART4_FLAG is set*/
  603. status = SET;
  604. }
  605. else
  606. {
  607. /* UART4_FLAG is reset*/
  608. status = RESET;
  609. }
  610. }
  611. else
  612. {
  613. if((UART4->SR & (uint8_t)UART4_FLAG) != (uint8_t)0x00)
  614. {
  615. /* UART4_FLAG is set*/
  616. status = SET;
  617. }
  618. else
  619. {
  620. /* UART4_FLAG is reset*/
  621. status = RESET;
  622. }
  623. }
  624. /* Return the UART4_FLAG status*/
  625. return status;
  626. }
  627. /**
  628. * @brief Clears the UART4 flags.
  629. * @param UART4_FLAG specifies the flag to clear
  630. * This parameter can be any combination of the following values:
  631. * - UART4_FLAG_LBDF: LIN Break detection flag.
  632. * - UART4_FLAG_LHDF: LIN Header detection flag.
  633. * - UART4_FLAG_LSF: LIN synchrone field flag.
  634. * - UART4_FLAG_RXNE: Receive data register not empty flag.
  635. * @note:
  636. * - PE (Parity error), FE (Framing error), NE (Noise error),
  637. * OR (OverRun error) and IDLE (Idle line detected) flags are cleared
  638. * by software sequence: a read operation to UART4_SR register
  639. * (UART4_GetFlagStatus())followed by a read operation to UART4_DR
  640. * register(UART4_ReceiveData8() or UART4_ReceiveData9()).
  641. *
  642. * - RXNE flag can be also cleared by a read to the UART4_DR register
  643. * (UART4_ReceiveData8()or UART4_ReceiveData9()).
  644. *
  645. * - TC flag can be also cleared by software sequence: a read operation
  646. * to UART4_SR register (UART4_GetFlagStatus()) followed by a write
  647. * operation to UART4_DR register (UART4_SendData8() or UART4_SendData9()).
  648. *
  649. * - TXE flag is cleared only by a write to the UART4_DR register
  650. * (UART4_SendData8() or UART4_SendData9()).
  651. *
  652. * - SBK flag is cleared during the stop bit of break.
  653. * @retval None
  654. */
  655. void UART4_ClearFlag(UART4_Flag_TypeDef UART4_FLAG)
  656. {
  657. assert_param(IS_UART4_CLEAR_FLAG_OK(UART4_FLAG));
  658. /* Clear the Receive Register Not Empty flag */
  659. if(UART4_FLAG == UART4_FLAG_RXNE)
  660. {
  661. UART4->SR = (uint8_t)~(UART4_SR_RXNE);
  662. }
  663. /* Clear the LIN Break Detection flag */
  664. else if(UART4_FLAG == UART4_FLAG_LBDF)
  665. {
  666. UART4->CR4 &= (uint8_t)(~UART4_CR4_LBDF);
  667. }
  668. /* Clear the LIN Header Detection Flag */
  669. else if(UART4_FLAG == UART4_FLAG_LHDF)
  670. {
  671. UART4->CR6 &= (uint8_t)(~UART4_CR6_LHDF);
  672. }
  673. /* Clear the LIN Synch Field flag */
  674. else
  675. {
  676. UART4->CR6 &= (uint8_t)(~UART4_CR6_LSF);
  677. }
  678. }
  679. /**
  680. * @brief Checks whether the specified UART4 interrupt has occurred or not.
  681. * @param UART4_IT: Specifies the UART4 interrupt pending bit to check.
  682. * This parameter can be one of the following values:
  683. * - UART4_IT_LBDF: LIN Break detection interrupt
  684. * - UART4_IT_TXE: Transmit Data Register empty interrupt
  685. * - UART4_IT_TC: Transmission complete interrupt
  686. * - UART4_IT_RXNE: Receive Data register not empty interrupt
  687. * - UART4_IT_IDLE: Idle line detection interrupt
  688. * - UART4_IT_OR: OverRun Error interrupt
  689. * - UART4_IT_PE: Parity Error interrupt
  690. * @retval The state of UART4_IT (SET or RESET).
  691. */
  692. ITStatus UART4_GetITStatus(UART4_IT_TypeDef UART4_IT)
  693. {
  694. ITStatus pendingbitstatus = RESET;
  695. uint8_t itpos = 0;
  696. uint8_t itmask1 = 0;
  697. uint8_t itmask2 = 0;
  698. uint8_t enablestatus = 0;
  699. /* Check parameters */
  700. assert_param(IS_UART4_GET_IT_OK(UART4_IT));
  701. /* Get the UART4 IT index*/
  702. itpos = (uint8_t)((uint8_t)1 << (uint8_t)((uint8_t)UART4_IT & (uint8_t)0x0F));
  703. /* Get the UART4 IT index*/
  704. itmask1 = (uint8_t)((uint8_t)UART4_IT >> (uint8_t)4);
  705. /* Set the IT mask*/
  706. itmask2 = (uint8_t)((uint8_t)1 << itmask1);
  707. /* Check the status of the specified UART4 pending bit*/
  708. if(UART4_IT == UART4_IT_PE)
  709. {
  710. /* Get the UART4_ITPENDINGBIT enable bit status*/
  711. enablestatus = (uint8_t)((uint8_t)UART4->CR1 & itmask2);
  712. /* Check the status of the specified UART4 interrupt*/
  713. if(((UART4->SR & itpos) != (uint8_t)0x00) && enablestatus)
  714. {
  715. /* Interrupt occurred*/
  716. pendingbitstatus = SET;
  717. }
  718. else
  719. {
  720. /* Interrupt not occurred*/
  721. pendingbitstatus = RESET;
  722. }
  723. }
  724. else if(UART4_IT == UART4_IT_LBDF)
  725. {
  726. /* Get the UART4_IT enable bit status*/
  727. enablestatus = (uint8_t)((uint8_t)UART4->CR4 & itmask2);
  728. /* Check the status of the specified UART4 interrupt*/
  729. if(((UART4->CR4 & itpos) != (uint8_t)0x00) && enablestatus)
  730. {
  731. /* Interrupt occurred*/
  732. pendingbitstatus = SET;
  733. }
  734. else
  735. {
  736. /* Interrupt not occurred*/
  737. pendingbitstatus = RESET;
  738. }
  739. }
  740. else if(UART4_IT == UART4_IT_LHDF)
  741. {
  742. /* Get the UART4_IT enable bit status*/
  743. enablestatus = (uint8_t)((uint8_t)UART4->CR6 & itmask2);
  744. /* Check the status of the specified UART4 interrupt*/
  745. if(((UART4->CR6 & itpos) != (uint8_t)0x00) && enablestatus)
  746. {
  747. /* Interrupt occurred*/
  748. pendingbitstatus = SET;
  749. }
  750. else
  751. {
  752. /* Interrupt not occurred*/
  753. pendingbitstatus = RESET;
  754. }
  755. }
  756. else
  757. {
  758. /* Get the UART4_IT enable bit status*/
  759. enablestatus = (uint8_t)((uint8_t)UART4->CR2 & itmask2);
  760. /* Check the status of the specified UART4 interrupt*/
  761. if(((UART4->SR & itpos) != (uint8_t)0x00) && enablestatus)
  762. {
  763. /* Interrupt occurred*/
  764. pendingbitstatus = SET;
  765. }
  766. else
  767. {
  768. /* Interrupt not occurred*/
  769. pendingbitstatus = RESET;
  770. }
  771. }
  772. /* Return the UART4_IT status*/
  773. return pendingbitstatus;
  774. }
  775. /**
  776. * @brief Clears the UART4 pending flags.
  777. * @param UART4_IT specifies the pending bit to clear
  778. * This parameter can be one of the following values:
  779. * - UART4_IT_LBDF: LIN Break detection interrupt
  780. * - UART4_IT_LHDF: LIN Header detection interrupt
  781. * - UART4_IT_RXNE: Receive Data register not empty interrupt.
  782. * @note
  783. * - PE (Parity error), FE (Framing error), NE (Noise error),
  784. * OR (OverRun error) and IDLE (Idle line detected) pending bits are
  785. * cleared by software sequence: a read operation to UART4_SR register
  786. * (UART4_GetITStatus()) followed by a read operation to UART4_DR register
  787. * (UART4_ReceiveData8() or UART4_ReceiveData9()).
  788. *
  789. * - RXNE pending bit can be also cleared by a read to the UART4_DR
  790. * register (UART4_ReceiveData8() or UART4_ReceiveData9()).
  791. *
  792. * - TC (Transmit complete) pending bit can be cleared by software
  793. * sequence: a read operation to UART4_SR register
  794. * (UART4_GetITStatus()) followed by a write operation to UART4_DR
  795. * register (UART4_SendData8()or UART4_SendData9()).
  796. *
  797. * - TXE pending bit is cleared only by a write to the UART4_DR register
  798. * (UART4_SendData8() or UART4_SendData9()).
  799. * @retval None
  800. */
  801. void UART4_ClearITPendingBit(UART4_IT_TypeDef UART4_IT)
  802. {
  803. assert_param(IS_UART4_CLEAR_IT_OK(UART4_IT));
  804. /* Clear the Receive Register Not Empty pending bit */
  805. if(UART4_IT == UART4_IT_RXNE)
  806. {
  807. UART4->SR = (uint8_t)~(UART4_SR_RXNE);
  808. }
  809. /* Clear the LIN Break Detection pending bit */
  810. else if(UART4_IT == UART4_IT_LBDF)
  811. {
  812. UART4->CR4 &= (uint8_t)~(UART4_CR4_LBDF);
  813. }
  814. /* Clear the LIN Header Detection pending bit */
  815. else
  816. {
  817. UART4->CR6 &= (uint8_t)(~UART4_CR6_LHDF);
  818. }
  819. }
  820. /**
  821. * @}
  822. */
  823. /**
  824. * @}
  825. */
  826. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/