HAL_uart.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. /**
  2. ******************************************************************************
  3. * @file HAL_UART.c
  4. * @author IC Applications Department
  5. * @version V0.8
  6. * @date 2019_08_02
  7. * @brief This file provides all the UART firmware functions.
  8. ******************************************************************************
  9. * @copy
  10. *
  11. * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  12. * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
  13. * TIME. AS A RESULT, HOLOCENE SHALL NOT BE HELD LIABLE FOR ANY
  14. * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
  15. * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
  16. * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  17. *
  18. * <h2><center>&copy; COPYRIGHT 2016 HOLOCENE</center></h2>
  19. */
  20. /* Includes ------------------------------------------------------------------*/
  21. #include "HAL_uart.h"
  22. #include "HAL_rcc.h"
  23. /** @addtogroup StdPeriph_Driver
  24. * @{
  25. */
  26. /** @defgroup UART
  27. * @brief UART driver modules
  28. * @{
  29. */
  30. /** @defgroup UART_Private_TypesDefinitions
  31. * @{
  32. */
  33. /**
  34. * @}
  35. */
  36. /** @defgroup UART_Private_Defines
  37. * @{
  38. */
  39. /* UART UE Mask */
  40. #define GCR_UE_Set ((uint16_t)0x0001) /* UART Enable Mask */
  41. #define GCR_UE_Reset ((uint16_t)0xFFFE) /* UART Disable Mask */
  42. #define CCR_CLEAR_Mask ((uint32_t)0xFFFFFF30) /* UART CCR Mask */
  43. #define GCR_CLEAR_Mask ((uint32_t)0xFFFFFFE0) /* UART GCR Mask */
  44. /**
  45. * @}
  46. */
  47. /** @defgroup UART_Private_Macros
  48. * @{
  49. */
  50. /**
  51. * @}
  52. */
  53. /** @defgroup UART_Private_Variables
  54. * @{
  55. */
  56. /**
  57. * @}
  58. */
  59. /** @defgroup UART_Private_FunctionPrototypes
  60. * @{
  61. */
  62. /**
  63. * @}
  64. */
  65. /** @defgroup UART_Private_Functions
  66. * @{
  67. */
  68. /**
  69. * @brief Deinitializes the UARTx peripheral registers to their
  70. * default reset values.
  71. * @param UARTx: Select the UART or the UART peripheral.
  72. * This parameter can be one of the following values:
  73. * UART1, UART2, UART3.
  74. * @retval : None
  75. */
  76. void UART_DeInit(UART_TypeDef* UARTx)
  77. {
  78. /* Check the parameters */
  79. assert_param(IS_UART_ALL_PERIPH(UARTx));
  80. switch (*(uint32_t*)&UARTx)
  81. {
  82. case UART1_BASE:
  83. RCC_APB2PeriphResetCmd(RCC_APB2Periph_UART1, ENABLE);
  84. RCC_APB2PeriphResetCmd(RCC_APB2Periph_UART1, DISABLE);
  85. break;
  86. default:
  87. break;
  88. }
  89. }
  90. /**
  91. * @brief Initializes the UARTx peripheral according to the specified
  92. * parameters in the UART_InitStruct .
  93. * @param UARTx: Select the UART or the UART peripheral.
  94. * This parameter can be one of the following values:
  95. * UART1, UART2, UART3.
  96. * @param UART_InitStruct: pointer to a UART_InitTypeDef structure
  97. * that contains the configuration information for the
  98. * specified UART peripheral.
  99. * @retval : None
  100. */
  101. void UART_Init(UART_TypeDef* UARTx, UART_InitTypeDef* UART_InitStruct)
  102. {
  103. uint32_t tmpreg = 0x00;
  104. RCC_ClocksTypeDef RCC_ClocksStatus;
  105. /* Check the parameters */
  106. assert_param(IS_UART_ALL_PERIPH(UARTx));
  107. assert_param(IS_UART_BAUDRATE(UART_InitStruct->UART_BaudRate));
  108. assert_param(IS_UART_WORD_LENGTH(UART_InitStruct->UART_WordLength));
  109. assert_param(IS_UART_STOPBITS(UART_InitStruct->UART_StopBits));
  110. assert_param(IS_UART_PARITY(UART_InitStruct->UART_Parity));
  111. assert_param(IS_UART_MODE(UART_InitStruct->UART_Mode));
  112. assert_param(IS_UART_HARDWARE_FLOW_CONTROL(UART_InitStruct->UART_HardwareFlowControl));
  113. /*---------------------------- UART CCR Configuration -----------------------*/
  114. /* get UART CCR values */
  115. tmpreg = UARTx->CCR;
  116. /* Clear spb,psel,pen bits */
  117. tmpreg &= CCR_CLEAR_Mask;
  118. /* Configure the UART Word Length,the UART Stop Bits,Parity ------------*/
  119. /* Set the char bits according to UART_WordLength value */
  120. /* Set spb bit according to UART_StopBits value */
  121. /* Set PEN bit according to UART_Parity value */
  122. tmpreg |= (uint32_t)UART_InitStruct->UART_WordLength |(uint32_t)UART_InitStruct->UART_StopBits |UART_InitStruct->UART_Parity;
  123. /* Write to UART CCR */
  124. UARTx->CCR = tmpreg;
  125. /*---------------------------- UART GCR Configuration -----------------------*/
  126. /* get UART GCR values */
  127. tmpreg = UARTx->GCR;
  128. /* Clear TXEN and RXEN ,autoflowen, mode ,uarten bits */
  129. tmpreg &= GCR_CLEAR_Mask;
  130. /* Set autorlowen bit according to UART_HardwareFlowControl value */
  131. /* Set rxen,txen bits according to UART_Mode value */
  132. tmpreg |= UART_InitStruct->UART_HardwareFlowControl | UART_InitStruct->UART_Mode ;
  133. /* Write to UART GCR */
  134. UARTx->GCR = tmpreg;
  135. /*---------------------------- UART BRR Configuration -----------------------*/
  136. /* Configure the UART Baud Rate -------------------------------------------RCC_ClocksStatus.PCLK1_Frequency;*/
  137. RCC_GetClocksFreq(&RCC_ClocksStatus);
  138. /* Determine the UART_baud*/
  139. tmpreg = ((RCC_ClocksStatus.PCLK1_Frequency)/(UART_InitStruct->UART_BaudRate)/16) ;
  140. /* Write to UART BRR */
  141. UARTx->BRR = tmpreg;
  142. }
  143. /**
  144. * @brief Fills each UART_InitStruct member with its default value.
  145. * @param UART_InitStruct: pointer to a UART_InitTypeDef structure
  146. * which will be initialized.
  147. * @retval : None
  148. */
  149. void UART_StructInit(UART_InitTypeDef* UART_InitStruct)
  150. {
  151. /* UART_InitStruct members default value */
  152. UART_InitStruct->UART_BaudRate = 9600;
  153. UART_InitStruct->UART_WordLength = UART_WordLength_8b;
  154. UART_InitStruct->UART_StopBits = UART_StopBits_1;
  155. UART_InitStruct->UART_Parity = UART_Parity_No ;
  156. UART_InitStruct->UART_Mode = UART_Mode_Rx | UART_Mode_Tx;
  157. UART_InitStruct->UART_HardwareFlowControl = UART_HardwareFlowControl_None;
  158. }
  159. /**
  160. * @brief Enables or disables the specified UART peripheral.
  161. * @param UARTx: Select the UART or the UART peripheral.
  162. * This parameter can be one of the following values:
  163. * UART1, UART2, UART3.
  164. * @param NewState: new state of the UARTx peripheral.
  165. * This parameter can be: ENABLE or DISABLE.
  166. * @retval : None
  167. */
  168. void UART_Cmd(UART_TypeDef* UARTx, FunctionalState NewState)
  169. {
  170. /* Check the parameters */
  171. assert_param(IS_UART_ALL_PERIPH(UARTx));
  172. assert_param(IS_FUNCTIONAL_STATE(NewState));
  173. if (NewState != DISABLE)
  174. {
  175. /* Enable the selected UART by setting the uarten bit in the GCR register */
  176. UARTx->GCR |= GCR_UE_Set;
  177. }
  178. else
  179. {
  180. /* Disable the selected UART by clearing the uarten bit in the GCR register */
  181. UARTx->GCR &= GCR_UE_Reset;
  182. }
  183. }
  184. /**
  185. * @brief Enables or disables the specified UART interrupts.
  186. * @param UARTx: Select the UART or the UART peripheral.
  187. * This parameter can be one of the following values:
  188. * UART1, UART2, UART3.
  189. * @param UART_IT: specifies the UART interrupt sources to be
  190. * enabled or disabled.
  191. * This parameter can be one of the following values:
  192. *
  193. * @arg UART_IT_ERR: Error interrupt(Frame error,)
  194. * @arg UART_IT_PE: Parity Error interrupt
  195. * @arg UART_OVER_ERR: overrun Error interrupt
  196. * @arg UART_TIMEOUT_ERR: timeout Error interrupt
  197. * @arg UART_IT_RXIEN: Receive Data register interrupt
  198. * @arg UART_IT_TXIEN: Tansmit Data Register empty interrupt
  199. * @param NewState: new state of the specified UARTx interrupts.
  200. * This parameter can be: ENABLE or DISABLE.
  201. * @retval : None
  202. */
  203. void UART_ITConfig(UART_TypeDef* UARTx, uint16_t UART_IT, FunctionalState NewState)
  204. {
  205. /* Check the parameters */
  206. assert_param(IS_UART_ALL_PERIPH(UARTx));
  207. assert_param(IS_UART_CONFIG_IT(UART_IT));
  208. assert_param(IS_FUNCTIONAL_STATE(NewState));
  209. if (NewState != DISABLE)
  210. {
  211. /* Enable the UART_IT interrupt */
  212. UARTx->IER |= UART_IT;
  213. }
  214. else
  215. {
  216. /* Disable the UART_IT interrupt */
  217. UARTx->IER &= ~ UART_IT;
  218. }
  219. }
  220. /**
  221. * @brief Enables or disables the UART’s DMA interface.
  222. * @param UARTx: Select the UART or the UART peripheral.
  223. * This parameter can be one of the following values:
  224. * UART1, UART2, UART3 .
  225. * @param UART_DMAReq: specifies the DMA request.
  226. * This parameter can be any combination of the following values:
  227. * @arg UART_DMAReq_EN: UART DMA transmit request
  228. *
  229. * @param NewState: new state of the DMA Request sources.
  230. * This parameter can be: ENABLE or DISABLE.
  231. * @note The DMA mode is not available for UART5.
  232. * @retval : None
  233. */
  234. void UART_DMACmd(UART_TypeDef* UARTx, uint16_t UART_DMAReq, FunctionalState NewState)
  235. {
  236. /* Check the parameters */
  237. assert_param(IS_UART_1234_PERIPH(UARTx));
  238. assert_param(IS_UART_DMAREQ(UART_DMAReq));
  239. assert_param(IS_FUNCTIONAL_STATE(NewState));
  240. if (NewState != DISABLE)
  241. {
  242. /* Enable the DMA transfer */
  243. UARTx->GCR |= UART_DMAReq;
  244. }
  245. else
  246. {
  247. /* Disable the DMA transfer */
  248. UARTx->GCR &= ~UART_DMAReq;
  249. }
  250. }
  251. /**
  252. * @brief Transmits single data through the UARTx peripheral.
  253. * @param UARTx: Select the UART or the UART peripheral.
  254. * This parameter can be one of the following values:
  255. * UART1, UART2, UART3.
  256. * @param Data: the data to transmit.
  257. * @retval : None
  258. */
  259. void UART_SendData(UART_TypeDef* UARTx, uint16_t Data)
  260. {
  261. /* Check the parameters */
  262. assert_param(IS_UART_ALL_PERIPH(UARTx));
  263. assert_param(IS_UART_DATA(Data));
  264. /* Transmit Data */
  265. UARTx->TDR = (Data & (uint16_t)0x00FF);
  266. }
  267. /**
  268. * @brief Returns the most recent received data by the UARTx peripheral.
  269. * @param UARTx: Select the UART or the UART peripheral.
  270. * This parameter can be one of the following values:
  271. * UART1, UART2, UART3.
  272. * @retval : The received data.
  273. */
  274. uint16_t UART_ReceiveData(UART_TypeDef* UARTx)
  275. {
  276. /* Check the parameters */
  277. assert_param(IS_UART_ALL_PERIPH(UARTx));
  278. /* Receive Data */
  279. return (uint16_t)(UARTx->RDR & (uint16_t)0x00FF);
  280. }
  281. /**
  282. * @brief Checks whether the specified UART flag is set or not.
  283. * @param UARTx: Select the UART or the UART peripheral.
  284. * This parameter can be one of the following values:
  285. * UART1, UART2, UART3.
  286. * @param UART_FLAG: specifies the flag to check.
  287. * This parameter can be one of the following values:
  288. * @arg UART_FLAG_TXEMPTY:Transmit data register empty flag
  289. * @arg UART_FLAG_TXFULL:Transmit data buffer full
  290. * @arg UART_FLAG_RXAVL:RX Buffer has a byte flag
  291. * @arg UART_FLAG_OVER:OverRun Error flag
  292. * @arg UART_FLAG_TXEPT: tx and shifter are emptys flag
  293. * @retval : The new state of UART_FLAG (SET or RESET).
  294. */
  295. FlagStatus UART_GetFlagStatus(UART_TypeDef* UARTx, uint16_t UART_FLAG)
  296. {
  297. FlagStatus bitstatus = RESET;
  298. /* Check the parameters */
  299. assert_param(IS_UART_ALL_PERIPH(UARTx));
  300. assert_param(IS_UART_FLAG(UART_FLAG));
  301. if ((UARTx->CSR & UART_FLAG) != (uint16_t)RESET)
  302. {
  303. bitstatus = SET;
  304. }
  305. else
  306. {
  307. bitstatus = RESET;
  308. }
  309. return bitstatus;
  310. }
  311. /**
  312. * @brief Clears the UARTx's pending flags.
  313. * @param UARTx: Select the UART or the UART peripheral.
  314. * This parameter can be one of the following values:
  315. * UART1, UART2, UART3, UART4 or UART5.
  316. * @param UART_FLAG: specifies the flag to clear.
  317. * This parameter can be any combination of the following values:
  318. * @arg UART_FLAG_TXEMPTY:Transmit data register empty flag
  319. * @arg UART_FLAG_TXFULL:Transmit data buffer full
  320. * @arg UART_FLAG_RXAVL:RX Buffer has a byte flag
  321. * @arg UART_FLAG_OVER:OverRun Error flag
  322. * @arg UART_FLAG_TXEPT: tx and shifter are emptys flag
  323. * @retval : None
  324. */
  325. void UART_ClearFlag(UART_TypeDef* UARTx, uint16_t UART_FLAG)
  326. {
  327. }
  328. /**
  329. * @brief Checks whether the specified UART interrupt has occurred or not.
  330. * @param UARTx: Select the UART or the UART peripheral.
  331. * This parameter can be one of the following values:
  332. * UART1, UART2, UART3.
  333. * @param UART_IT: specifies the UART interrupt source to check.
  334. * This parameter can be one of the following values:
  335. * @arg UART_IT_ERR: Error interrupt(Frame error,)
  336. * @arg UART_IT_PE: Parity Error interrupt
  337. * @arg UART_OVER_ERR: overrun Error interrupt
  338. * @arg UART_TIMEOUT_ERR: timeout Error interrupt
  339. * @arg UART_IT_RXIEN: Receive Data register interrupt
  340. * @arg UART_IT_TXIEN: Tansmit Data Register empty interrupt
  341. * @retval : The new state of UART_IT (SET or RESET).
  342. */
  343. ITStatus UART_GetITStatus(UART_TypeDef* UARTx, uint16_t UART_IT)
  344. {
  345. FlagStatus bitstatus = RESET;
  346. /* Check the parameters */
  347. assert_param(IS_UART_ALL_PERIPH(UARTx));
  348. assert_param(IS_UART_FLAG(UART_FLAG));
  349. assert_param(IS_UART_PERIPH_FLAG(UARTx, UART_FLAG)); /* The CTS flag is not available for UART4 and UART5 */
  350. if ((UARTx->ISR & UART_IT) != (uint16_t)RESET)
  351. {
  352. bitstatus = SET;
  353. }
  354. else
  355. {
  356. bitstatus = RESET;
  357. }
  358. return bitstatus;
  359. }
  360. /**
  361. * @brief Clears the UARTx’s interrupt pending bits.
  362. * @param UARTx: Select the UART or the UART peripheral.
  363. * This parameter can be one of the following values:
  364. * UART1, UART2, UART3, UART4 or UART5.
  365. * @param UART_IT: specifies the interrupt pending bit to clear.
  366. * This parameter can be one of the following values:
  367. * @arg UART_IT_ERR: Error interrupt(Frame error,)
  368. * @arg UART_IT_PE: Parity Error interrupt
  369. * @arg UART_OVER_ERR: overrun Error interrupt
  370. * @arg UART_TIMEOUT_ERR: timeout Error interrupt
  371. * @arg UART_IT_RXIEN: Receive Data register interrupt
  372. * @arg UART_IT_TXIEN: Tansmit Data Register empty interrupt
  373. * @retval : None
  374. */
  375. void UART_ClearITPendingBit(UART_TypeDef* UARTx, uint16_t UART_IT)
  376. {
  377. /* Check the parameters */
  378. assert_param(IS_UART_ALL_PERIPH(UARTx));
  379. assert_param(IS_UART_CLEAR_IT(UART_IT));
  380. assert_param(IS_UART_PERIPH_IT(UARTx, UART_IT)); /* The CTS interrupt is not available for UART4 and UART5 */
  381. /*clear UART_IT pendings bit*/
  382. UARTx->ICR = UART_IT;
  383. }
  384. /**
  385. * @}
  386. */
  387. /**
  388. * @}
  389. */
  390. /**
  391. * @}
  392. */
  393. /*-------------------------(C) COPYRIGHT 2016 HOLOCENE ----------------------*/