HAL_UART.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901
  1. /*
  2. ******************************************************************************
  3. * @file HAL_Uart.c
  4. * @version V1.0.0
  5. * @date 2020
  6. * @brief UART HAL module driver.
  7. * This file provides firmware functions to manage the following
  8. * functionalities of the Universal Asynchronous Receiver Transmitter Peripheral (UART).
  9. * @ Initialization and de-initialization functions
  10. * @ IO operation functions
  11. * @ Peripheral Control functions
  12. ******************************************************************************
  13. */
  14. #include "ACM32Fxx_HAL.h"
  15. /* If Use 'UART_MODE_TX_RX_DEBUG', Point to Debug Uart */
  16. UART_TypeDef *Uart_Debug = NULL;
  17. /* Private function prototypes -----------------------------------------------*/
  18. static void UART_Config_BaudRate(UART_HandleTypeDef *huart);
  19. static HAL_StatusTypeDef HAL_UART_Wait_Tx_Done(UART_HandleTypeDef *huart);
  20. __weak void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart);
  21. __weak void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart);
  22. __weak void HAL_UART_ErrorCallback(UART_HandleTypeDef *huart);
  23. /*********************************************************************************
  24. * Function : HAL_UART_IRQHandler
  25. * Description : Handle UART interrupt request.
  26. * Input : huart: UART handle.
  27. * Output :
  28. * Author : Chris_Kyle Data : 2020
  29. **********************************************************************************/
  30. void HAL_UART_IRQHandler(UART_HandleTypeDef *huart)
  31. {
  32. uint32_t read_bytes_number;
  33. uint32_t isrflags, ieits, errorflags;
  34. #if (USE_FULL_ASSERT == 1)
  35. if (!IS_UART_ALL_INSTANCE(huart->Instance)) return;
  36. #endif
  37. isrflags =READ_REG(huart->Instance->RIS);
  38. ieits =READ_REG(huart->Instance->IE);
  39. errorflags =0x00U;
  40. errorflags =(isrflags & (uint32_t)(UART_ICR_PEI|UART_ICR_OEI|UART_ICR_FEI|UART_ICR_BEI));
  41. /* Enable TXI */
  42. if (huart->Instance->IE & UART_IE_TXI)
  43. {
  44. if (huart->Instance->RIS & UART_RIS_TXI)
  45. {
  46. /* Clear TXI Status */
  47. SET_BIT(huart->Instance->ICR , UART_ICR_TXI);
  48. for(;;)
  49. {
  50. if(huart->lu32_TxCount == huart->lu32_TxSize)
  51. {
  52. huart->lu8_TxBusy = false;
  53. /* Disable TX interrupt */
  54. CLEAR_BIT(huart->Instance->IE, UART_IE_TXI);
  55. HAL_UART_TxCpltCallback(huart);
  56. break;
  57. }
  58. if (READ_BIT(huart->Instance->FR, UART_FR_TXFF))
  59. {
  60. break;
  61. }
  62. huart->Instance->DR = huart->lu8_TxData[huart->lu32_TxCount++];
  63. }
  64. }
  65. }
  66. /* RXI */
  67. if ((huart->Instance->IE & UART_IE_RXI || huart->Instance->IE& UART_IE_RTI) && errorflags == 0)
  68. {
  69. if (huart->Instance->RIS & UART_RIS_RXI)
  70. {
  71. read_bytes_number = 0;
  72. /* Clear RXI Status */
  73. SET_BIT(huart->Instance->ICR, UART_ICR_RXI);
  74. /* Receive end */
  75. while(huart->lu32_RxCount <huart->lu32_RxSize )
  76. {
  77. if(!READ_BIT(huart->Instance->FR, UART_FR_RXFE))
  78. {
  79. /* Store Data in buffer */
  80. huart->lu8_RxData[huart->lu32_RxCount++] = huart->Instance->DR;
  81. read_bytes_number++;
  82. }
  83. else
  84. {
  85. break;
  86. }
  87. if (read_bytes_number == huart->lu32_fifo_level_minus1)
  88. {
  89. break;
  90. }
  91. }
  92. if(huart->lu32_RxCount ==huart->lu32_RxSize )
  93. {
  94. huart->lu8_RxBusy = false;
  95. /* Disable RX and RTI interrupt */
  96. CLEAR_BIT(huart->Instance->IE, (UART_IE_RXI|UART_IE_RTI));
  97. /* clear error interrupt */
  98. CLEAR_BIT(huart->Instance->IE, UART_IE_OEI|UART_IE_BEI|UART_IE_PEI|UART_IE_FEI);
  99. HAL_UART_RxCpltCallback(huart);
  100. }
  101. }
  102. else if(huart->Instance->RIS & UART_RIS_RTI)
  103. {
  104. /*clear RTI Status */
  105. SET_BIT(huart->Instance->ICR ,UART_ICR_RTI);
  106. while(!READ_BIT(huart->Instance->FR, UART_FR_RXFE))
  107. {
  108. huart->lu8_RxData[huart->lu32_RxCount++] = huart->Instance->DR;
  109. }
  110. huart->lu8_RxBusy = false;
  111. /* Disable RX and RTI interrupt */
  112. CLEAR_BIT(huart->Instance->IE, (UART_IE_RXI|UART_IE_RTI));
  113. /* clear error interrupt */
  114. CLEAR_BIT(huart->Instance->IE, UART_IE_OEI|UART_IE_BEI|UART_IE_PEI|UART_IE_FEI);
  115. HAL_UART_RxCpltCallback(huart);
  116. }
  117. }
  118. /* if some errors occurred */
  119. if(errorflags != 0 &&(ieits & (UART_IE_OEI|UART_IE_BEI|UART_IE_PEI|UART_IE_FEI)))
  120. {
  121. /* UART parity error interrupt occurred */
  122. if (((isrflags & UART_RIS_PEI) != 0) && ((ieits & UART_IE_PEI) != 0))
  123. {
  124. /* Clear parity error status */
  125. SET_BIT(huart->Instance->ICR, UART_ICR_PEI);
  126. huart->ErrorCode |= HAL_UART_ERROR_PE;
  127. }
  128. /* UART break error interrupt occurred */
  129. if (((isrflags & UART_RIS_BEI) != 0) && ((ieits & UART_IE_BEI) != 0))
  130. {
  131. SET_BIT(huart->Instance->ICR, UART_RIS_BEI);
  132. huart->ErrorCode |= HAL_UART_ERROR_NE;
  133. }
  134. /* UART frame error interrupt occurred */
  135. if (((isrflags & UART_RIS_FEI) != 0) && ((ieits & UART_IE_FEI) != 0))
  136. {
  137. SET_BIT(huart->Instance->ICR, UART_RIS_FEI);
  138. huart->ErrorCode |= HAL_UART_ERROR_FE;
  139. }
  140. /* UART Over-Run interrupt occurred */
  141. if (((isrflags & UART_RIS_OEI) != 0) && ((ieits & UART_IE_OEI) != 0))
  142. {
  143. SET_BIT(huart->Instance->ICR, UART_RIS_OEI);
  144. huart->ErrorCode |= HAL_UART_ERROR_ORE;
  145. }
  146. /* clear error interrupt */
  147. CLEAR_BIT(huart->Instance->IE, UART_IE_OEI|UART_IE_BEI|UART_IE_PEI|UART_IE_FEI);
  148. HAL_UART_ErrorCallback(huart);
  149. }
  150. }
  151. /*********************************************************************************
  152. * Function : HAL_UART_Wait_Tx_Done
  153. * Description : wait Tx FIFO empty
  154. * Input : huart: UART handle.
  155. * Output :
  156. **********************************************************************************/
  157. static HAL_StatusTypeDef HAL_UART_Wait_Tx_Done(UART_HandleTypeDef *huart)
  158. {
  159. #if (USE_FULL_ASSERT == 1)
  160. if (!IS_UART_ALL_INSTANCE(huart->Instance)) return HAL_ERROR;
  161. #endif
  162. /* wait TX not busy*/
  163. while(READ_BIT(huart->Instance->FR, UART_FR_BUSY));
  164. return HAL_OK;
  165. }
  166. /*********************************************************************************
  167. * Function : HAL_UART_MspInit
  168. * Description : Initialize the UART MSP.
  169. * Input : huart: UART handle.
  170. * Output :
  171. * Author : Chris_Kyle Data : 2020
  172. **********************************************************************************/
  173. __weak void HAL_UART_MspInit(UART_HandleTypeDef *huart)
  174. {
  175. /*
  176. NOTE: This function should be modified, when the callback is needed,
  177. the HAL_UART_MspInit can be implemented in the user file.
  178. */
  179. /* For Example */
  180. GPIO_InitTypeDef GPIO_Uart1;
  181. if (huart->Instance == UART1)
  182. {
  183. /* Enable Clock */
  184. System_Module_Enable(EN_UART1);
  185. /* Initialization GPIO */
  186. /* A9:Tx A10:Rx */
  187. GPIO_Uart1.Pin = GPIO_PIN_9 | GPIO_PIN_10;
  188. GPIO_Uart1.Mode = GPIO_MODE_AF_PP;
  189. GPIO_Uart1.Pull = GPIO_PULLUP;
  190. GPIO_Uart1.Alternate = GPIO_FUNCTION_2;
  191. HAL_GPIO_Init(GPIOA, &GPIO_Uart1);
  192. if (huart->Init.HwFlowCtl & UART_HWCONTROL_CTS)
  193. {
  194. /* A11:CTS */
  195. GPIO_Uart1.Pin = GPIO_PIN_11;
  196. HAL_GPIO_Init(GPIOA, &GPIO_Uart1);
  197. }
  198. if (huart->Init.HwFlowCtl & UART_HWCONTROL_RTS)
  199. {
  200. /* A12:RTS */
  201. GPIO_Uart1.Pin = GPIO_PIN_12;
  202. HAL_GPIO_Init(GPIOA, &GPIO_Uart1);
  203. }
  204. /* NVIC Config */
  205. NVIC_ClearPendingIRQ(UART1_IRQn);
  206. NVIC_SetPriority(UART1_IRQn, 5);
  207. NVIC_EnableIRQ(UART1_IRQn);
  208. }
  209. }
  210. /*********************************************************************************
  211. * Function : HAL_UART_Init
  212. * Description : Initialize the UART mode according to the specified parameters
  213. * in the UART_InitTypeDef
  214. * Input : huart: UART handle.
  215. * Output :
  216. * Author : Chris_Kyle Data : 2020
  217. **********************************************************************************/
  218. HAL_StatusTypeDef HAL_UART_Init(UART_HandleTypeDef *huart)
  219. {
  220. #if (USE_FULL_ASSERT == 1)
  221. if (!IS_UART_ALL_INSTANCE(huart->Instance)) return HAL_ERROR;
  222. if (!IS_UART_WORDLENGTH(huart->Init.WordLength)) return HAL_ERROR;
  223. if (!IS_UART_STOPBITS(huart->Init.StopBits)) return HAL_ERROR;
  224. if (!IS_UART_PARITY(huart->Init.Parity)) return HAL_ERROR;
  225. if (!IS_UART_MODE(huart->Init.Mode)) return HAL_ERROR;
  226. if (!IS_UART_HARDWARE_FLOW_CONTROL(huart->Init.HwFlowCtl)) return HAL_ERROR;
  227. #endif
  228. /* Init the low level hardware : GPIO, CLOCK, NVIC */
  229. HAL_UART_MspInit(huart);
  230. /* Config BaudRate */
  231. UART_Config_BaudRate(huart);
  232. /* Set the UART Communication parameters */
  233. huart->Instance->LCRH = huart->Init.WordLength | UART_LCRH_FEN | huart->Init.StopBits | huart->Init.Parity;
  234. huart->Instance->CR = huart->Init.HwFlowCtl | huart->Init.Mode | UART_CR_UARTEN;
  235. if (huart->Init.Mode == UART_MODE_TX_RX_DEBUG)
  236. {
  237. Uart_Debug = huart->Instance;
  238. }
  239. else if (huart->Init.Mode == UART_MODE_HALF_DUPLEX)
  240. {
  241. huart->Instance->CR2 = UART_CR2_TXOE_SEL;
  242. }
  243. return HAL_OK;
  244. }
  245. /*********************************************************************************
  246. * Function : HAL_UART_MspDeInit
  247. * Description : DeInitialize the UART MSP.
  248. * Input : huart: UART handle.
  249. * Output :
  250. **********************************************************************************/
  251. __weak void HAL_UART_MspDeInit(UART_HandleTypeDef *huart)
  252. {
  253. /*
  254. NOTE: This function should be modified, when the callback is needed,
  255. the HAL_UART_MspDeInit can be implemented in the user file.
  256. */
  257. if (huart->Instance == UART1)
  258. {
  259. /* Disable Clock */
  260. System_Module_Disable(EN_UART1);
  261. /* DeInitialization GPIO */
  262. /* A9:Tx A10:Rx */
  263. HAL_GPIO_DeInit(GPIOA,GPIO_PIN_9 | GPIO_PIN_10);
  264. if (huart->Init.HwFlowCtl & UART_HWCONTROL_CTS)
  265. {
  266. /* A11:CTS */
  267. HAL_GPIO_DeInit(GPIOA, GPIO_PIN_11);
  268. }
  269. if (huart->Init.HwFlowCtl & UART_HWCONTROL_RTS)
  270. {
  271. /* A12:RTS */
  272. HAL_GPIO_DeInit(GPIOA, GPIO_PIN_12);
  273. }
  274. /* NVIC DeInit */
  275. NVIC_DisableIRQ(UART1_IRQn);
  276. }
  277. else if(huart->Instance == UART2)
  278. {
  279. }
  280. }
  281. /*********************************************************************************
  282. * Function : HAL_UART_Init
  283. * Description : Initialize the UART mode according to the specified parameters
  284. * in the UART_InitTypeDef
  285. * Input : huart: UART handle.
  286. * Output :
  287. **********************************************************************************/
  288. HAL_StatusTypeDef HAL_UART_DeInit(UART_HandleTypeDef *huart)
  289. {
  290. #if (USE_FULL_ASSERT == 1)
  291. if (!IS_UART_ALL_INSTANCE(huart->Instance)) return HAL_ERROR;
  292. #endif
  293. /* DeInit the low level hardware : GPIO, CLOCK, NVIC */
  294. HAL_UART_MspDeInit(huart);
  295. return HAL_OK;
  296. }
  297. /*********************************************************************************
  298. * Function : HAL_UART_Transmit
  299. * Description : Send an amount of data in blocking mode.
  300. * Input : huart: UART handle.
  301. * Input : fu8_Data: Pointer to data buffer.
  302. * Input : fu32_Size: Amount of data elements to be sent.
  303. * Input : fu32_Timeout: Timeout duration.
  304. * Output : HAL status
  305. * Author : Chris_Kyle Data : 2020
  306. **********************************************************************************/
  307. HAL_StatusTypeDef HAL_UART_Transmit(UART_HandleTypeDef *huart, uint8_t *fu8_Data, uint32_t fu32_Size, uint32_t fu32_Timeout)
  308. {
  309. uint32_t lu32_Timeout;
  310. #if (USE_FULL_ASSERT == 1)
  311. if (!IS_UART_ALL_INSTANCE(huart->Instance)) return HAL_ERROR;
  312. #endif
  313. huart->lu32_TxCount = 0;
  314. while (fu32_Size--)
  315. {
  316. huart->Instance->DR = *fu8_Data++;
  317. huart->lu32_TxCount++;
  318. /* have no timeout */
  319. if (fu32_Timeout == 0)
  320. {
  321. while (huart->Instance->FR & UART_FR_TXFF);
  322. }
  323. else
  324. {
  325. lu32_Timeout = fu32_Timeout *256;
  326. while (huart->Instance->FR & UART_FR_TXFF)
  327. {
  328. if (lu32_Timeout-- == 0)
  329. {
  330. return HAL_TIMEOUT;
  331. }
  332. }
  333. }
  334. }
  335. HAL_UART_Wait_Tx_Done(huart);
  336. return HAL_OK;
  337. }
  338. /*********************************************************************************
  339. * Function : HAL_UART_Receive
  340. * Description : Receive an amount of data in blocking mode.
  341. * Input : huart: UART handle.
  342. * Input : fu8_Data: Pointer to data buffer.
  343. * Input : fu32_Size: Amount of data elements to be receive.
  344. * Input : fu32_Timeout: Timeout duration.
  345. * Output : HAL status
  346. * Author : Chris_Kyle Data : 2020
  347. **********************************************************************************/
  348. HAL_StatusTypeDef HAL_UART_Receive(UART_HandleTypeDef *huart, uint8_t *fu8_Data, uint32_t fu32_Size, uint32_t fu32_Timeout)
  349. {
  350. uint32_t lu32_Timeout;
  351. #if (USE_FULL_ASSERT == 1)
  352. if (!IS_UART_ALL_INSTANCE(huart->Instance)) return HAL_ERROR;
  353. #endif
  354. huart->lu32_RxCount = 0;
  355. /* Half duplex Use Tx GPIO Receive Data */
  356. if (huart->Init.Mode == UART_MODE_HALF_DUPLEX)
  357. {
  358. huart->Instance->CR2 |= UART_CR2_RX_SEL;
  359. }
  360. while (fu32_Size--)
  361. {
  362. if (fu32_Timeout == 0)
  363. {
  364. while(huart->Instance->FR & UART_FR_RXFE);
  365. *fu8_Data++ = huart->Instance->DR;
  366. huart->lu32_RxCount++;
  367. }
  368. else
  369. {
  370. lu32_Timeout = fu32_Timeout * 256;
  371. while(huart->Instance->FR & UART_FR_RXFE)
  372. {
  373. if (lu32_Timeout-- == 0)
  374. {
  375. /* Clear Half duplex */
  376. huart->Instance->CR2 &= ~UART_CR2_RX_SEL;
  377. return HAL_TIMEOUT;
  378. }
  379. }
  380. *fu8_Data++ = huart->Instance->DR;
  381. huart->lu32_RxCount++;
  382. }
  383. }
  384. /* Clear Half duplex */
  385. huart->Instance->CR2 &= ~UART_CR2_RX_SEL;
  386. return HAL_OK;
  387. }
  388. /*********************************************************************************
  389. * Function : HAL_UART_Transmit_IT
  390. * Description : Send an amount of data in interrupt mode.
  391. * Input : huart: UART handle.
  392. * Input : fu8_Data: Pointer to data buffer.
  393. * Input : fu32_Size: Amount of data elements to be receive.
  394. * Output :
  395. * Author : Chris_Kyle Data : 2020
  396. **********************************************************************************/
  397. HAL_StatusTypeDef HAL_UART_Transmit_IT(UART_HandleTypeDef *huart, uint8_t *fu8_Data, uint32_t fu32_Size)
  398. {
  399. #if (USE_FULL_ASSERT == 1)
  400. if (!IS_UART_ALL_INSTANCE(huart->Instance)) return HAL_ERROR;
  401. #endif
  402. if (huart->lu8_TxBusy == true)
  403. {
  404. return HAL_BUSY;
  405. }
  406. if (fu32_Size == 0 || fu8_Data == NULL)
  407. {
  408. return HAL_ERROR;
  409. }
  410. huart->lu32_TxSize = fu32_Size;
  411. huart->lu32_TxCount = 0;
  412. huart->lu8_TxData = fu8_Data;
  413. huart->lu8_TxBusy = true;
  414. /* Clear TXI Status */
  415. huart->Instance->ICR = UART_ICR_TXI;
  416. /* FIFO Enable */
  417. SET_BIT(huart->Instance->LCRH, UART_LCRH_FEN);
  418. /*FIFO Select*/
  419. SET_BIT(huart->Instance->IFLS,UART_TX_FIFO_1_2);
  420. for(;;)
  421. {
  422. /*Data Size less than 16Byte */
  423. if(fu32_Size == huart->lu32_TxCount)
  424. {
  425. huart->lu8_TxBusy = false;
  426. while ((huart->Instance->FR & UART_FR_BUSY)){}
  427. HAL_UART_TxCpltCallback(huart);
  428. return HAL_OK;
  429. }
  430. if(READ_BIT(huart->Instance->FR, UART_FR_TXFF))
  431. {
  432. break;
  433. }
  434. huart->Instance->DR = huart->lu8_TxData[huart->lu32_TxCount++];
  435. }
  436. /* Enable TX interrupt */
  437. huart->Instance->IE |= UART_IE_TXI;
  438. return HAL_OK;
  439. }
  440. /*********************************************************************************
  441. * Function : HAL_UART_Receive_IT
  442. * Description : Receive an amount of data in interrupt mode.
  443. * Input :
  444. * Output :
  445. * Author : Chris_Kyle Data : 2020
  446. **********************************************************************************/
  447. HAL_StatusTypeDef HAL_UART_Receive_IT(UART_HandleTypeDef *huart, uint8_t *fu8_Data, uint32_t fu32_Size)
  448. {
  449. #if (USE_FULL_ASSERT == 1)
  450. if (!IS_UART_ALL_INSTANCE(huart->Instance)) return HAL_ERROR;
  451. #endif
  452. if (huart->lu8_RxBusy == true)
  453. {
  454. return HAL_BUSY;
  455. }
  456. if (fu32_Size == 0 || fu8_Data == NULL)
  457. {
  458. return HAL_ERROR;
  459. }
  460. huart->lu32_RxSize = fu32_Size;
  461. huart->lu32_RxCount = 0;
  462. huart->lu8_RxData = fu8_Data;
  463. huart->lu8_RxBusy = true;
  464. /* Clear RXI Status */
  465. huart->Instance->ICR = UART_ICR_RXI;
  466. /* FIFO Enable */
  467. SET_BIT(huart->Instance->LCRH, UART_LCRH_FEN);
  468. /*FIFO Select*/
  469. MODIFY_REG(huart->Instance->IFLS, UART_IFLS_RXIFLSEL, UART_RX_FIFO_1_2);
  470. huart->lu32_fifo_level_minus1 = 8-1; // 16/2 - 1
  471. /* Enable the UART Errors interrupt */
  472. SET_BIT(huart->Instance->IE,UART_IE_OEI|UART_IE_BEI|UART_IE_PEI|UART_IE_FEI);
  473. /* Enable RX and RTI interrupt */
  474. SET_BIT(huart->Instance->IE,UART_IE_RXI|UART_IE_RTI);
  475. return HAL_OK;
  476. }
  477. /*********************************************************************************
  478. * Function : HAL_UART_Transmit_DMA
  479. * Description : Send an amount of data in DMA mode.
  480. * Input : huart: UART handle.
  481. * Input : fu8_Data: Pointer to data buffer.
  482. * Input : fu32_Size: Amount of data elements to be Send.
  483. * Output :
  484. * Author : Chris_Kyle Data : 2020
  485. **********************************************************************************/
  486. HAL_StatusTypeDef HAL_UART_Transmit_DMA(UART_HandleTypeDef *huart, uint8_t *fu8_Data, uint32_t fu32_Size)
  487. {
  488. #if (USE_FULL_ASSERT == 1)
  489. if (!IS_UART_ALL_INSTANCE(huart->Instance)) return HAL_ERROR;
  490. #endif
  491. if (huart->lu8_TxBusy == true)
  492. {
  493. return HAL_BUSY;
  494. }
  495. if (fu32_Size == 0 || fu8_Data == NULL)
  496. {
  497. return HAL_ERROR;
  498. }
  499. huart->Instance->DMACR |= UART_DMACR_TXDMAE;
  500. if (HAL_DMA_Start_IT(huart->HDMA_Tx, (uint32_t)fu8_Data, (uint32_t)(&huart->Instance->DR), fu32_Size))
  501. {
  502. return HAL_ERROR;
  503. }
  504. return HAL_OK;
  505. }
  506. /*********************************************************************************
  507. * Function : HAL_UART_Receive_DMA
  508. * Description : Receive an amount of data in DMA mode.
  509. * Input : huart: UART handle.
  510. * Input : fu8_Data: Pointer to data buffer.
  511. * Input : fu32_Size: Amount of data elements to be receive.
  512. * Output :
  513. **********************************************************************************/
  514. HAL_StatusTypeDef HAL_UART_Receive_DMA(UART_HandleTypeDef *huart, uint8_t *fu8_Data, uint32_t fu32_Size)
  515. {
  516. #if (USE_FULL_ASSERT == 1)
  517. if (!IS_UART_ALL_INSTANCE(huart->Instance)) return HAL_ERROR;
  518. #endif
  519. if (huart->lu8_RxBusy == true)
  520. {
  521. return HAL_BUSY;
  522. }
  523. if (fu32_Size == 0 || fu8_Data == NULL)
  524. {
  525. return HAL_ERROR;
  526. }
  527. huart->Instance->DMACR |= UART_DMACR_RXDMAE;
  528. if (HAL_DMA_Start_IT(huart->HDMA_Rx, (uint32_t)(&huart->Instance->DR), (uint32_t)fu8_Data, fu32_Size))
  529. {
  530. return HAL_ERROR;
  531. }
  532. return HAL_OK;
  533. }
  534. /*********************************************************************************
  535. * Function : HAL_UART_TxCpltCallback
  536. * Description : Tx Transfer completed callbacks.
  537. * Input :
  538. * Output :
  539. * Author : Chris_Kyle Data : 2020
  540. **********************************************************************************/
  541. __weak void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart)
  542. {
  543. /*
  544. NOTE: This function Should be modified, when the callback is needed,
  545. the HAL_UART_TxCpltCallback could be implemented in the user file.
  546. */
  547. }
  548. /*********************************************************************************
  549. * Function : HAL_UART_RxCpltCallback
  550. * Description : Rx Transfer completed callbacks.
  551. * Input :
  552. * Output :
  553. * Author : Chris_Kyle Data : 2020
  554. **********************************************************************************/
  555. __weak void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
  556. {
  557. /*
  558. NOTE: This function Should be modified, when the callback is needed,
  559. the HAL_UART_RxCpltCallback could be implemented in the user file.
  560. */
  561. }
  562. /*********************************************************************************
  563. * Function : HAL_UART_ErrorCallBack
  564. * Description : Recv Error callbacks.
  565. * Input :
  566. * Output :
  567. **********************************************************************************/
  568. __weak void HAL_UART_ErrorCallBack(UART_HandleTypeDef *huart)
  569. {
  570. /*
  571. NOTE: This function Should be modified, when the callback is needed,
  572. the HAL_UART_ErrorCallBack could be implemented in the user file.
  573. */
  574. }
  575. /*********************************************************************************
  576. * Function : UART_Config_BaudRate
  577. * Description : Config BaudRate
  578. * Input :
  579. * Output :
  580. * Author : Chris_Kyle Data : 2020
  581. **********************************************************************************/
  582. static void UART_Config_BaudRate(UART_HandleTypeDef *huart)
  583. {
  584. uint32_t lu32_PCLK;
  585. uint32_t lu32_IBAUD, lu32_FBAUD;
  586. uint64_t lu64_TempValue;
  587. lu32_PCLK = System_Get_APBClock();
  588. /* Integral part */
  589. lu32_IBAUD = lu32_PCLK / (huart->Init.BaudRate * 16);
  590. /* Fractional part */
  591. lu64_TempValue = lu32_PCLK % (huart->Init.BaudRate * 16);
  592. lu64_TempValue = (lu64_TempValue * 1000000) / (huart->Init.BaudRate * 16);
  593. lu32_FBAUD = (lu64_TempValue * 64 + 500000) / 1000000;
  594. if (lu32_FBAUD >= 64)
  595. {
  596. huart->Instance->IBRD = lu32_IBAUD + 1;
  597. huart->Instance->FBRD = 0;
  598. }
  599. else
  600. {
  601. huart->Instance->IBRD = lu32_IBAUD;
  602. huart->Instance->FBRD = lu32_FBAUD;
  603. }
  604. }
  605. /*********************************************************************************
  606. * Function : HAL_UART_GetState
  607. * Description : Return the uart State
  608. * Input :
  609. * Output :
  610. **********************************************************************************/
  611. HAL_StatusTypeDef HAL_UART_GetState(UART_HandleTypeDef *huart)
  612. {
  613. #if (USE_FULL_ASSERT == 1)
  614. if (!IS_UART_ALL_INSTANCE(huart->Instance)) return HAL_ERROR;
  615. #endif
  616. if(huart->lu8_TxBusy || huart->lu8_RxBusy)
  617. {
  618. return HAL_BUSY;
  619. }
  620. return HAL_OK;
  621. }
  622. /*********************************************************************************
  623. * Function : HAL_UART_GetError
  624. * Description : Return the uart Error
  625. * Input :
  626. * Output :
  627. **********************************************************************************/
  628. uint32_t HAL_UART_GetError(UART_HandleTypeDef *huart)
  629. {
  630. return huart->ErrorCode;
  631. }
  632. /*********************************************************************************
  633. * Function : HAL_UART_Abort
  634. * Description : Abort ongoing transfers(blocking mode)
  635. * Input : UART handle
  636. * Output :
  637. **********************************************************************************/
  638. HAL_StatusTypeDef HAL_UART_Abort(UART_HandleTypeDef *huart)
  639. {
  640. #if (USE_FULL_ASSERT == 1)
  641. if (!IS_UART_ALL_INSTANCE(huart->Instance)) return HAL_ERROR;
  642. #endif
  643. /*disble all interrupt*/
  644. huart->Instance->IE =0x00;
  645. /* Disable the UART DMA Tx request if enable */
  646. if(READ_BIT(huart->Instance->DMACR, UART_DMACR_TXDMAE))
  647. {
  648. CLEAR_BIT(huart->Instance->DMACR, UART_DMACR_TXDMAE);
  649. /* Abort the UART Tx Channel */
  650. if(huart->HDMA_Tx)
  651. {
  652. /*Set the UART DMA Abort callback to Null */
  653. huart->HDMA_Tx->DMA_ITC_Callback =NULL;
  654. if(HAL_DMA_Abort(huart->HDMA_Tx)!=HAL_OK)
  655. {
  656. return HAL_TIMEOUT;
  657. }
  658. }
  659. }
  660. /* Disable the UART DMA Rx request if enable */
  661. if(READ_BIT(huart->Instance->DMACR, UART_DMACR_RXDMAE))
  662. {
  663. CLEAR_BIT(huart->Instance->DMACR, UART_DMACR_RXDMAE);
  664. /* Abort the UART Rx Channel */
  665. if(huart->HDMA_Rx)
  666. {
  667. /*Set the UART DMA Abort callback to Null */
  668. huart->HDMA_Rx->DMA_ITC_Callback =NULL;
  669. if(HAL_DMA_Abort(huart->HDMA_Rx)!=HAL_OK)
  670. {
  671. return HAL_TIMEOUT;
  672. }
  673. }
  674. }
  675. /*Reset Tx and Rx Transfer size*/
  676. huart->lu32_TxSize = 0;
  677. huart->lu32_RxSize = 0;
  678. /* Restore huart->lu8_TxBusy and huart->lu8_RxBusy to Ready */
  679. huart->lu8_TxBusy = false;
  680. huart->lu8_RxBusy = false;
  681. return HAL_OK;
  682. }
  683. /*********************************************************************************
  684. * Function : HAL_UART_DMAPause
  685. * Description : Pause the DMA Transfer
  686. * Input : UART handle
  687. * Output :
  688. **********************************************************************************/
  689. HAL_StatusTypeDef HAL_UART_DMAPause(UART_HandleTypeDef *huart)
  690. {
  691. #if (USE_FULL_ASSERT == 1)
  692. if (!IS_UART_ALL_INSTANCE(huart->Instance)) return HAL_ERROR;
  693. #endif
  694. if(READ_BIT(huart->Instance->DMACR, UART_DMACR_TXDMAE))
  695. {
  696. /* Disable the UART DMA Tx request */
  697. CLEAR_BIT(huart->Instance->DMACR, UART_DMACR_TXDMAE);
  698. }
  699. if (READ_BIT(huart->Instance->DMACR, UART_DMACR_RXDMAE))
  700. {
  701. /* Disable RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
  702. CLEAR_BIT(huart->Instance->IE, UART_IE_OEI|UART_IE_BEI|UART_IE_FEI);
  703. /* Disable the UART DMA Rx request */
  704. CLEAR_BIT(huart->Instance->DMACR, UART_DMACR_RXDMAE);
  705. }
  706. return HAL_OK;
  707. }
  708. /*********************************************************************************
  709. * Function : HAL_UART_DMAResume
  710. * Description : Resume the DMA Transfer
  711. * Input : UART handle
  712. * Output :
  713. **********************************************************************************/
  714. HAL_StatusTypeDef HAL_UART_DMAResume(UART_HandleTypeDef *huart)
  715. {
  716. #if (USE_FULL_ASSERT == 1)
  717. if (!IS_UART_ALL_INSTANCE(huart->Instance)) return HAL_ERROR;
  718. #endif
  719. if (huart->lu8_TxBusy == false)
  720. {
  721. /* Enable the UART DMA Tx request */
  722. SET_BIT(huart->Instance->DMACR, UART_DMACR_TXDMAE);
  723. }
  724. if (huart->lu8_RxBusy == false)
  725. {
  726. /* Reenable PE and ERR (Frame error, noise error, overrun error) interrupts */
  727. SET_BIT(huart->Instance->IE, UART_IE_OEI|UART_IE_BEI|UART_IE_FEI);
  728. /* Enable the UART DMA Rx request */
  729. SET_BIT(huart->Instance->DMACR, UART_DMACR_RXDMAE);
  730. }
  731. return HAL_OK;
  732. }
  733. #if 0
  734. /*********************************************************************************
  735. * Function : fputc
  736. * Description :
  737. * Input :
  738. * Output :
  739. * Author : Chris_Kyle Data : 2020
  740. **********************************************************************************/
  741. int fputc(int ch, FILE *f)
  742. {
  743. if (Uart_Debug == NULL)
  744. {
  745. return 0;
  746. }
  747. Uart_Debug->DR = ch;
  748. while ((Uart_Debug->FR & UART_FR_BUSY));
  749. return ch;
  750. }
  751. #endif