HAL_UART.c 27 KB

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