HAL_LPUART.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. /*
  2. ******************************************************************************
  3. * @file HAL_LPUART.c
  4. * @version V1.0.0
  5. * @date 2020
  6. * @brief LPUART HAL module driver.
  7. * This file provides firmware functions to manage the following
  8. * functionalities of the Universal Asynchronous Receiver Transmitter Peripheral (LPUART).
  9. * @ Initialization and de-initialization functions
  10. * @ IO operation functions
  11. * @ Peripheral Control functions
  12. ******************************************************************************
  13. */
  14. #include "ACM32Fxx_HAL.h"
  15. /*********************************************************************************
  16. * Function : HAL_LPUART_IRQHander
  17. * Description : LPUART IRQHander
  18. * Input :
  19. * Outpu :
  20. * Author : CWT Data : 2020年
  21. **********************************************************************************/
  22. void HAL_LPUART_IRQHander(LPUART_HandleTypeDef *hlpuart)
  23. {
  24. while ( (hlpuart->Instance->SR) & (1U << LPUART_SR_RX_INDEX))
  25. {
  26. if(hlpuart->rx_read_index != (((hlpuart->rx_write_index) + 1)% (hlpuart->rx_buffer_size) ) )
  27. {
  28. hlpuart->rx_buffer[hlpuart->rx_write_index] = hlpuart->Instance->RXDR;
  29. hlpuart->rx_write_index = ((hlpuart->rx_write_index + 1)%(hlpuart->rx_buffer_size) );
  30. }
  31. else
  32. {
  33. hlpuart->Instance->SR = (1U << LPUART_SR_RX_INDEX); // buffer overflow
  34. return;
  35. }
  36. }
  37. }
  38. /************************************************************************
  39. * function : HAL_UART_Buffer_Init
  40. * Description: uart buffer initiation.
  41. * input :
  42. * UART_HandleTypeDef *huart: pointer to uart handle structure
  43. * return: 0: FAIL; 1: SUCCESS
  44. ************************************************************************/
  45. HAL_StatusTypeDef HAL_LPUART_Buffer_Init(LPUART_HandleTypeDef *hlpuart)
  46. {
  47. if(0x00 == IS_LPUART_INSTANCE(hlpuart->Instance))
  48. {
  49. return HAL_ERROR;
  50. }
  51. hlpuart->rx_read_index = 0;
  52. hlpuart->rx_write_index = 0;
  53. hlpuart->tx_busy = 0;
  54. return HAL_OK;
  55. }
  56. /**********************************************************************************
  57. * function : HAL_LPUART_MSPInit
  58. * Description: UART MCU specific initiation, such as IO share, module clock, ...
  59. * input :
  60. * UART_HandleTypeDef *huart: pointer to uart handle structure
  61. * return: 0: FAIL; 1: SUCCESS
  62. ***************************************************************************************/
  63. __weak void HAL_LPUART_MSPInit(LPUART_HandleTypeDef *hlpuart)
  64. {
  65. GPIO_InitTypeDef gpio_init;
  66. System_Module_Reset(RST_LPUART);
  67. gpio_init.Pin = GPIO_PIN_2|GPIO_PIN_3; // TX PA2 RX PA3
  68. gpio_init.Mode = GPIO_MODE_AF_PP;
  69. gpio_init.Pull = GPIO_PULLUP;
  70. gpio_init.Alternate = GPIO_FUNCTION_6;
  71. HAL_GPIO_Init(GPIOA,&gpio_init);
  72. System_Module_Enable(EN_LPUART);
  73. }
  74. /*********************************************************************************
  75. * Function : HAL_LPUART_MspDeInit
  76. * Description : LPUART MSP De-Initialization
  77. * This function frees the hardware resources used in this example:
  78. * - Disable the Peripheral's clock
  79. * - Revert GPIO configuration to their default state
  80. * Input : hcan : pointer to a CAN_HandleTypeDef structure that contains
  81. * the configuration information for CAN module
  82. * Output :
  83. * Author : CWT Data : 2020年
  84. **********************************************************************************/
  85. void HAL_LPUART_MspDeInit(LPUART_HandleTypeDef *hlpuart)
  86. {
  87. /* Initialization GPIO */
  88. /* // TX PA2 RX PA3 */
  89. HAL_GPIO_DeInit(GPIOA, GPIO_PIN_2);
  90. HAL_GPIO_DeInit(GPIOA, GPIO_PIN_3);
  91. }
  92. /**********************************************************************************
  93. * function : HAL_LPUART_Set_Baud_Rate
  94. * Description: set uart module's baud rate. It should be set when UART is disabled.
  95. * input :
  96. * UART_HandleTypeDef *huart: pointer to uart handle structure
  97. * return: 0: FAIL; 1: SUCCESS
  98. ***************************************************************************************/
  99. uint8_t HAL_LPUART_Set_Baud_Rate(LPUART_HandleTypeDef *hlpuart, uint32_t lpuart_clk, uint32_t baud_rate)
  100. {
  101. uint32_t ibaud, fbaud, rxsamp;
  102. if(0x00 == IS_LPUART_INSTANCE(hlpuart->Instance))
  103. {
  104. return 0;
  105. }
  106. ibaud = 2;
  107. fbaud = 0x952;
  108. rxsamp = 1;
  109. switch(lpuart_clk)
  110. {
  111. case 32000:
  112. case 32768:
  113. switch(baud_rate)
  114. {
  115. case 9600:
  116. ibaud = 2;
  117. fbaud = 0x952;
  118. // ibaud = 2;
  119. // fbaud = 0xA44;
  120. break;
  121. case 4800:
  122. ibaud = 5;
  123. fbaud = 0xefb;
  124. break;
  125. case 2400:
  126. ibaud = 12;
  127. fbaud = 0x6db;
  128. break;
  129. case 1200:
  130. ibaud = 26;
  131. fbaud = 0x492;
  132. break;
  133. default:
  134. ibaud = 2;
  135. fbaud = 0x952;
  136. break;
  137. }
  138. rxsamp = ibaud >> 1;
  139. break;
  140. default:
  141. switch(baud_rate)
  142. {
  143. case 115200:
  144. ibaud = 16;
  145. fbaud = 0x924;
  146. break;
  147. case 9600:
  148. ibaud = 203;
  149. fbaud = 0x888;
  150. break;
  151. }
  152. rxsamp = ibaud >> 1;
  153. break;
  154. }
  155. hlpuart->Instance->IBAUD = ibaud | (rxsamp << 8);
  156. hlpuart->Instance->FBAUD = fbaud;
  157. return 1;
  158. }
  159. /************************************************************************
  160. * function : HAL_LPUART_Config
  161. * Description: Configure UART module parameters, such as baudrate, parity,
  162. * stop bits, dataword.
  163. * input :
  164. * UART_HandleTypeDef *huart: pointer to uart handle structure
  165. * return: 0: FAIL; 1: SUCCESS
  166. ************************************************************************/
  167. uint8_t HAL_LPUART_Config(LPUART_HandleTypeDef *hlpuart)
  168. {
  169. volatile uint32_t temp_reg;
  170. if(0x00 == IS_LPUART_INSTANCE(hlpuart->Instance))
  171. {
  172. return 0;
  173. }
  174. temp_reg = 1U << 7; // default value
  175. temp_reg |= ((hlpuart->ConfigParam.WordLength << 4) | (hlpuart->ConfigParam.StopBits << 3));
  176. switch(hlpuart->ConfigParam.Parity)
  177. {
  178. case LPUART_PARITY_NONE:
  179. break; // do nothing
  180. case LPUART_PARITY_SELECT_ODD:
  181. case LPUART_PARITY_SELECT_EVEN:
  182. temp_reg |= (((hlpuart->ConfigParam.Parity - LPUART_PARITY_SELECT_ODD) << LPUART_EPS_INDEX) | (1 << LPUART_PEN_INDEX));
  183. break;
  184. case LPUART_PARITY_SELECT_ONE:
  185. case LPUART_PARITY_SELECT_ZERO:
  186. temp_reg |= (((hlpuart->ConfigParam.Parity - LPUART_PARITY_SELECT_ONE) << LPUART_EPS_INDEX) | (1 << LPUART_SPS_INDEX) | (1 << LPUART_PEN_INDEX) );
  187. break;
  188. }
  189. hlpuart->Instance->LCR = temp_reg;
  190. return 1;
  191. }
  192. /*********************************************************************************
  193. * Function : LPUART_Clock_Select
  194. * Description : Select the LPUART clock.
  195. * Input : lpuart_clk_src:Could be LPUART_CLOCK_SOURCE_RC32K LPUART_CLOCK_SOURCE_XTAL LPUART_CLOCK_SOURCE_PLL_DIV
  196. * Outpu :
  197. * Author : CWT Data : 2020年
  198. **********************************************************************************/
  199. void LPUART_Clock_Select(uint8_t lpuart_clk_src)
  200. {
  201. if (0 == lpuart_clk_src)
  202. {
  203. SCU->CCR2 &= (~(BIT13 | BIT14) ); // RC32K
  204. }
  205. else if (1 == lpuart_clk_src)
  206. {
  207. SCU->CCR2 = (SCU->CCR2 & (~(BIT13 | BIT14) )) | (BIT13); // XTAL
  208. }
  209. else
  210. {
  211. SCU->CCR2 = (SCU->CCR2 & (~(BIT11 | BIT12| BIT13 | BIT14) )) | (BIT11 | BIT12 | BIT14); // pclk/32
  212. }
  213. }
  214. /************************************************************************
  215. * function : HAL_LPUART_Init
  216. * Description: uart initial with parameters.
  217. * input :
  218. * UART_HandleTypeDef *huart: pointer to uart handle structure
  219. * return: 0: FAIL; 1: SUCCESS
  220. ************************************************************************/
  221. HAL_StatusTypeDef HAL_LPUART_Init(LPUART_HandleTypeDef *hlpuart)
  222. {
  223. uint32_t lpuart_clock;
  224. if(0x00 == IS_LPUART_INSTANCE(hlpuart->Instance))
  225. {
  226. return HAL_ERROR;;
  227. }
  228. HAL_LPUART_Buffer_Init(hlpuart);
  229. /*reset module, configure tx and rx, enable module clock*/
  230. HAL_LPUART_MSPInit(hlpuart);
  231. if (LPUART_CLOCK_SOURCE_RC32K == hlpuart->ConfigParam.ClockSrc)
  232. {
  233. lpuart_clock = 32000;
  234. System_Module_Enable(EN_RTC);
  235. System_Enable_Disable_RTC_Domain_Access(FUNC_ENABLE);
  236. System_Enable_RC32K();
  237. LPUART_Clock_Select(0);
  238. }
  239. else if (LPUART_CLOCK_SOURCE_XTAL == hlpuart->ConfigParam.ClockSrc)
  240. {
  241. lpuart_clock = 32768;
  242. System_Module_Enable(EN_RTC);
  243. System_Enable_Disable_RTC_Domain_Access(FUNC_ENABLE);
  244. System_Enable_XTAL();
  245. LPUART_Clock_Select(1);
  246. }
  247. else
  248. {
  249. lpuart_clock = System_Get_APBClock()/32;
  250. LPUART_Clock_Select(2);
  251. }
  252. HAL_LPUART_Set_Baud_Rate(hlpuart, lpuart_clock, hlpuart->ConfigParam.BaudRate);
  253. HAL_LPUART_Config(hlpuart);
  254. hlpuart->Instance->SR = LPUART_SR_BITS_ALL;
  255. hlpuart->Instance->IE = (1U << LPUART_IE_RX_INDEX);
  256. hlpuart->Instance->CR = (1U << LPUART_CR_RXE_INDEX) | (1 << LPUART_CR_TXE_INDEX);
  257. hlpuart->Instance->LCR=((hlpuart->StopWakeup.Wakeup_Source<<5)|(hlpuart->StopWakeup.Wakeup_Check<<7));
  258. hlpuart->Instance->ADDR=(hlpuart->StopWakeup.Wakeup_Addr);
  259. NVIC_ClearPendingIRQ(LPUART_IRQn);
  260. NVIC_EnableIRQ(LPUART_IRQn);
  261. return HAL_OK;;
  262. }
  263. HAL_StatusTypeDef HAL_LPUART_DeInit(LPUART_HandleTypeDef *hlpuart)
  264. {
  265. /* Check handle */
  266. if(!IS_LPUART_INSTANCE(hlpuart->Instance)) return HAL_ERROR;
  267. HAL_LPUART_MspDeInit(hlpuart);
  268. /* Disable LPUART clock */
  269. System_Module_Disable(EN_LPUART);
  270. /* Return function status */
  271. return HAL_OK;
  272. }
  273. /************************************************************************
  274. * function : HAL_LPUART_Wait_TX_Done
  275. * Description: wait uart not busy
  276. * input :
  277. * UART_HandleTypeDef *huart: pointer to uart handle structure
  278. * return: 0: FAIL; 1: SUCCESS
  279. ************************************************************************/
  280. HAL_StatusTypeDef HAL_LPUART_Wait_TX_Done(LPUART_HandleTypeDef *hlpuart)
  281. {
  282. while (0 == (hlpuart->Instance->SR & (1 << LPUART_SR_TX_FINISH_INDEX) ) ) {};
  283. hlpuart->Instance->SR = (1 << LPUART_SR_TX_FINISH_INDEX);
  284. return HAL_OK;
  285. }
  286. void HAL_LPUART_Output(LPUART_HandleTypeDef *hlpuart, unsigned char c)
  287. {
  288. if ((hlpuart->Instance->SR) & (1U << LPUART_SR_TX_EMPTY_INDEX) )
  289. {
  290. hlpuart->Instance->TXDR = c;
  291. }
  292. HAL_LPUART_Wait_TX_Done(hlpuart);
  293. }
  294. /************************************************************************
  295. * function : uart_send_bytes
  296. * Description: uart send bytes
  297. * input :
  298. * UINT32 uart_index: Serial port number
  299. * UINT8* buff: out buffer
  300. * UINT32 length: buffer length
  301. * return: none
  302. ************************************************************************/
  303. void HAL_LPUART_Send_Bytes(LPUART_HandleTypeDef *hlpuart, uint8_t *buff, uint32_t length)
  304. {
  305. uint32_t i;
  306. for (i = 0; i < length; i++)
  307. {
  308. HAL_LPUART_Output(hlpuart, *buff++);
  309. }
  310. }
  311. /************************************************************************
  312. * function : HAL_LPUART_Receive_Bytes_Timeout
  313. * Description: uart receive bytes
  314. * input :
  315. * UART_HandleTypeDef *huart: pointer to uart handle structure
  316. * UINT8* buff: out buffer
  317. * UINT32 length: buffer length
  318. * UINT32 ms: number of ms to delay one byte
  319. * return: received bytes
  320. ************************************************************************/
  321. uint32_t HAL_LPUART_Receive_Bytes_Timeout(LPUART_HandleTypeDef *hlpuart, uint8_t * rxbuff, uint32_t length, uint32_t ms)
  322. {
  323. volatile uint32_t i, timeout, count;;
  324. if(0x00 == IS_LPUART_INSTANCE(hlpuart->Instance))
  325. {
  326. return 0;
  327. }
  328. timeout = (System_Get_APBClock() >> 13) * ms;
  329. count = timeout;
  330. i = 0;
  331. while(i < length)
  332. {
  333. if((hlpuart->rx_read_index) != (hlpuart->rx_write_index))
  334. {
  335. rxbuff[i] = hlpuart->rx_buffer[hlpuart->rx_read_index];
  336. hlpuart->rx_read_index = (((hlpuart->rx_read_index) + 1)%(hlpuart->rx_buffer_size) );
  337. count = timeout;
  338. i++;
  339. }
  340. else
  341. {
  342. if (0 == count)//timeout
  343. {
  344. break;
  345. }
  346. else
  347. {
  348. count--;
  349. }
  350. }
  351. }
  352. return i;
  353. }
  354. /************************************************************************
  355. * function : HAL_LPUART_Receive_Bytes
  356. * Description: LPUART receive bytes.
  357. * input :
  358. * UART_HandleTypeDef *huart: pointer to uart handle structure
  359. * buff:receive data buff
  360. * length:length of buff
  361. * return:length
  362. ************************************************************************/
  363. uint32_t HAL_LPUART_Receive_Bytes(LPUART_HandleTypeDef *hlpuart, uint8_t * rxbuff, uint32_t length)
  364. {
  365. volatile uint32_t i, ie_backup;
  366. if(0x00 == IS_LPUART_INSTANCE(hlpuart->Instance))
  367. {
  368. return 0;
  369. }
  370. ie_backup = hlpuart->Instance->IE;
  371. hlpuart->Instance->IE = 0;
  372. i = 0;
  373. while(i < length)
  374. {
  375. if( (hlpuart->Instance->SR) & (1U << LPUART_SR_RX_INDEX) )
  376. {
  377. rxbuff[i] = hlpuart->Instance->RXDR;
  378. i++;
  379. }
  380. }
  381. hlpuart->Instance->IE = ie_backup;
  382. return length;
  383. }
  384. /************************************************************************
  385. * function : HAL_LPUART_DMA_Send_Bytes
  386. * Description: LPUART send bytes by DMA.
  387. * input :
  388. * UART_HandleTypeDef *huart: pointer to uart handle structure
  389. * buff:send data buff
  390. * length:length of buff
  391. * return:none
  392. ************************************************************************/
  393. void HAL_LPUART_DMA_Send_Bytes(LPUART_HandleTypeDef *hlpuart, uint8_t *buff, uint32_t length)
  394. {
  395. hlpuart->Instance->CR |= (1U << LPUART_CR_DMA_EN_INDEX);
  396. hlpuart->tx_busy = 1;
  397. HAL_DMA_Start_IT(hlpuart->dma_tx_handler, (uint32_t)buff, (uint32_t)&hlpuart->Instance->TXDR, length);
  398. }
  399. /************************************************************************
  400. * function : HAL_LPUART_Clear_Wakeup_Flags
  401. * Description: Clear the LPUART STOP wake up flag.
  402. * input :
  403. * UART_HandleTypeDef *huart: pointer to uart handle structure
  404. * Wakeup_Bits:LPUART wakeup flag,could be: LPUART_WAKEUP_RX_BIT LPUART_WAKEUP_MATCH_BIT LPUART_WAKEUP_START_BIT
  405. * return:none
  406. ************************************************************************/
  407. void HAL_LPUART_Clear_Wakeup_Flags(LPUART_HandleTypeDef *hlpuart, uint32_t Wakeup_Bits)
  408. {
  409. hlpuart->Instance->SR = Wakeup_Bits;
  410. }