usart.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. /**
  2. ******************************************************************************
  3. * File Name : USART.c
  4. * Description : This file provides code for the configuration
  5. * of the USART instances.
  6. ******************************************************************************
  7. ** This notice applies to any and all portions of this file
  8. * that are not between comment pairs USER CODE BEGIN and
  9. * USER CODE END. Other portions of this file, whether
  10. * inserted by the user or by software development tools
  11. * are owned by their respective copyright owners.
  12. *
  13. * COPYRIGHT(c) 2019 STMicroelectronics
  14. *
  15. * Redistribution and use in source and binary forms, with or without modification,
  16. * are permitted provided that the following conditions are met:
  17. * 1. Redistributions of source code must retain the above copyright notice,
  18. * this list of conditions and the following disclaimer.
  19. * 2. Redistributions in binary form must reproduce the above copyright notice,
  20. * this list of conditions and the following disclaimer in the documentation
  21. * and/or other materials provided with the distribution.
  22. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  23. * may be used to endorse or promote products derived from this software
  24. * without specific prior written permission.
  25. *
  26. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  27. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  28. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  29. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  30. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  31. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  32. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  33. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  34. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  35. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  36. *
  37. ******************************************************************************
  38. */
  39. /* Includes ------------------------------------------------------------------*/
  40. #include "usart.h"
  41. #include "uart_process.h"
  42. #include "gpio.h"
  43. /* USER CODE BEGIN 0 */
  44. uint8_t UART_RxBuff3[UART_BUFF_SIZE];
  45. USART_Buf_TypeDef UART_RxBuff_Struct3 = {UART_BUFF_SIZE,0,0,0,0,UART_RxBuff3,&huart3};
  46. uint8_t UART_TxBuff3[UART_BUFF_SIZE];
  47. USART_Buf_TypeDef UART_TxBuff_Struct3 = {UART_BUFF_SIZE,0,0,0,0,UART_TxBuff3,&huart3};
  48. uint8_t UART_RxBuff1[UART_BUFF_SIZE];
  49. USART_Buf_TypeDef UART_RxBuff_Struct1 = {UART_BUFF_SIZE,0,0,0,0,UART_RxBuff1,&huart1};
  50. uint8_t UART_TxBuff1[UART_BUFF_SIZE];
  51. USART_Buf_TypeDef UART_TxBuff_Struct1 = {UART_BUFF_SIZE,0,0,0,0,UART_TxBuff1,&huart1};
  52. /* USER CODE END 0 */
  53. UART_HandleTypeDef huart3;
  54. UART_HandleTypeDef huart1;
  55. /* USART3 init function */
  56. void MX_USART3_UART_Init(void)
  57. {
  58. huart3.Instance = USART3;
  59. huart3.Init.BaudRate = 115200;
  60. huart3.Init.WordLength = UART_WORDLENGTH_8B;
  61. huart3.Init.StopBits = UART_STOPBITS_1;
  62. huart3.Init.Parity = UART_PARITY_NONE;
  63. huart3.Init.Mode = UART_MODE_TX_RX;
  64. huart3.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  65. huart3.Init.OverSampling = UART_OVERSAMPLING_16;
  66. if (HAL_UART_Init(&huart3) != HAL_OK)
  67. {
  68. _Error_Handler(__FILE__, __LINE__);
  69. }
  70. }
  71. void MX_USART1_UART_Init(uint32_t baudrate)
  72. {
  73. huart1.Instance = USART1;
  74. huart1.Init.BaudRate = baudrate;
  75. huart1.Init.WordLength = UART_WORDLENGTH_8B;
  76. huart1.Init.StopBits = UART_STOPBITS_1;
  77. huart1.Init.Parity = UART_PARITY_NONE;
  78. huart1.Init.Mode = UART_MODE_TX_RX;
  79. huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  80. huart1.Init.OverSampling = UART_OVERSAMPLING_16;
  81. if (HAL_UART_Init(&huart1) != HAL_OK)
  82. {
  83. _Error_Handler(__FILE__, __LINE__);
  84. }
  85. }
  86. void MX_USART1_Reopen(uint32_t baudrate)
  87. {
  88. HAL_UART_DeInit(&huart1);
  89. MX_USART1_UART_Init(baudrate);
  90. }
  91. void HAL_UART_MspInit(UART_HandleTypeDef* uartHandle)
  92. {
  93. GPIO_InitTypeDef GPIO_InitStruct;
  94. if(uartHandle->Instance==USART3)
  95. {
  96. /* USER CODE BEGIN USART3_MspInit 0 */
  97. /* USER CODE END USART3_MspInit 0 */
  98. /* USART3 clock enable */
  99. __HAL_RCC_USART3_CLK_ENABLE();
  100. /**USART3 GPIO Configuration
  101. PB10 ------> USART3_TX
  102. PB11 ------> USART3_RX
  103. */
  104. GPIO_InitStruct.Pin = GPIO_PIN_10;
  105. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  106. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
  107. HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  108. GPIO_InitStruct.Pin = GPIO_PIN_11;
  109. GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  110. GPIO_InitStruct.Pull = GPIO_NOPULL;
  111. HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  112. /* USART3 interrupt Init */
  113. HAL_NVIC_SetPriority(USART3_IRQn, 0, 0);
  114. HAL_NVIC_EnableIRQ(USART3_IRQn);
  115. /* USER CODE BEGIN USART3_MspInit 1 */
  116. __HAL_UART_ENABLE_IT(&huart3, UART_IT_PE);
  117. /* Enable the UART Error Interrupt: (Frame error, noise error, overrun error) */
  118. __HAL_UART_ENABLE_IT(&huart3, UART_IT_ERR);
  119. /* Enable the UART Data Register not empty Interrupt */
  120. __HAL_UART_ENABLE_IT(&huart3, UART_IT_RXNE);
  121. /* USER CODE END USART3_MspInit 1 */
  122. }
  123. else if(uartHandle->Instance==USART1)
  124. {
  125. /* USER CODE BEGIN USART3_MspInit 0 */
  126. /* USER CODE END USART3_MspInit 0 */
  127. /* USART3 clock enable */
  128. __HAL_RCC_USART1_CLK_ENABLE();
  129. __HAL_RCC_GPIOA_CLK_ENABLE();
  130. /**USART3 GPIO Configuration
  131. PB10 ------> USART3_TX
  132. PB11 ------> USART3_RX
  133. */
  134. GPIO_InitStruct.Pin = GPIO_PIN_9;
  135. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  136. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
  137. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  138. GPIO_InitStruct.Pin = GPIO_PIN_10;
  139. GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  140. GPIO_InitStruct.Pull = GPIO_NOPULL;
  141. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  142. /* USART3 interrupt Init */
  143. HAL_NVIC_SetPriority(USART1_IRQn, 0, 0);
  144. HAL_NVIC_EnableIRQ(USART1_IRQn);
  145. /* USER CODE BEGIN USART3_MspInit 1 */
  146. __HAL_UART_ENABLE_IT(uartHandle, UART_IT_PE);
  147. /* Enable the UART Error Interrupt: (Frame error, noise error, overrun error) */
  148. __HAL_UART_ENABLE_IT(uartHandle, UART_IT_ERR);
  149. /* Enable the UART Data Register not empty Interrupt */
  150. __HAL_UART_ENABLE_IT(uartHandle, UART_IT_RXNE);
  151. /* USER CODE END USART3_MspInit 1 */
  152. }
  153. }
  154. void HAL_UART_MspDeInit(UART_HandleTypeDef* uartHandle)
  155. {
  156. if(uartHandle->Instance==USART3)
  157. {
  158. /* USER CODE BEGIN USART3_MspDeInit 0 */
  159. /* USER CODE END USART3_MspDeInit 0 */
  160. /* Peripheral clock disable */
  161. __HAL_RCC_USART3_CLK_DISABLE();
  162. /**USART3 GPIO Configuration
  163. PB10 ------> USART3_TX
  164. PB11 ------> USART3_RX
  165. */
  166. HAL_GPIO_DeInit(GPIOB, GPIO_PIN_10|GPIO_PIN_11);
  167. /* USART3 interrupt Deinit */
  168. HAL_NVIC_DisableIRQ(USART3_IRQn);
  169. /* USER CODE BEGIN USART3_MspDeInit 1 */
  170. /* USER CODE END USART3_MspDeInit 1 */
  171. }
  172. else if(uartHandle->Instance==USART1)
  173. {
  174. /* USER CODE BEGIN USART1_MspDeInit 0 */
  175. /* USER CODE END USART1_MspDeInit 0 */
  176. /* Peripheral clock disable */
  177. __HAL_RCC_USART1_CLK_DISABLE();
  178. /**USART1 GPIO Configuration
  179. PA9 ------> USART1_TX
  180. PA10 ------> USART1_RX
  181. */
  182. HAL_GPIO_DeInit(GPIOA, GPIO_PIN_9|GPIO_PIN_10);
  183. /* USART1 interrupt Deinit */
  184. HAL_NVIC_DisableIRQ(USART1_IRQn);
  185. /* USER CODE BEGIN USART1_MspDeInit 1 */
  186. /* USER CODE END USART1_MspDeInit 1 */
  187. }
  188. }
  189. /* USER CODE BEGIN 1 */
  190. uint8_t UART_ReadChar(USART_Buf_TypeDef * ptRx, uint8_t ucNum)
  191. {
  192. uint8_t ucData;
  193. uint16_t i;
  194. i = ucNum;
  195. if ((*ptRx).ucBufCnt >= ucNum)
  196. {
  197. i += (*ptRx).ucBufRdInde;
  198. if (i >= (*ptRx).ucBufSize)
  199. {
  200. i -=((*ptRx).ucBufSize);
  201. }
  202. }
  203. else
  204. {
  205. i=0;
  206. }
  207. ucData = *((*ptRx).pcBufAddr + i);
  208. return ucData;
  209. }
  210. void UART_DelChar(USART_Buf_TypeDef * ptRx, uint8_t ucNum)
  211. {
  212. uint16_t i;
  213. if ((*ptRx).ucBufCnt >= ucNum)
  214. {
  215. __HAL_UART_DISABLE_IT(ptRx->ptUartHandel,UART_IT_RXNE);//关接收中断
  216. (*ptRx).ucBufCnt -= ucNum;
  217. __HAL_UART_ENABLE_IT(ptRx->ptUartHandel,UART_IT_RXNE);//开接收中断
  218. i = ucNum;
  219. i += (*ptRx).ucBufRdInde;
  220. if (i >= (*ptRx).ucBufSize)
  221. {
  222. i -= (*ptRx).ucBufSize;
  223. }
  224. (*ptRx).ucBufRdInde = i;
  225. }
  226. else
  227. {
  228. __HAL_UART_DISABLE_IT(ptRx->ptUartHandel,UART_IT_RXNE);//关接收中断
  229. i = (*ptRx).ucBufCnt;
  230. (*ptRx).ucBufCnt = 0;
  231. __HAL_UART_ENABLE_IT(ptRx->ptUartHandel,UART_IT_RXNE);//开接收中断
  232. i += (*ptRx).ucBufRdInde;
  233. if (i >= (*ptRx).ucBufSize)
  234. {
  235. i -= (*ptRx).ucBufSize;
  236. }
  237. (*ptRx).ucBufRdInde = i;
  238. }
  239. }
  240. void UART_PutChar(USART_Buf_TypeDef * ptTx, uint8_t ucData)
  241. {
  242. while ((*ptTx).ucBufCnt == (*ptTx).ucBufSize); //发送缓冲满,等待。若有此情况建议增大缓冲区
  243. __HAL_UART_DISABLE_IT(ptTx->ptUartHandel,UART_IT_TXE);//关发送中断
  244. if((*ptTx).ucBufCnt == 0)
  245. {
  246. if(__HAL_UART_GET_FLAG(ptTx->ptUartHandel, UART_FLAG_TXE) == SET) //check if transmit data register full or not
  247. {
  248. ptTx->ptUartHandel->Instance->DR = ucData;
  249. __HAL_UART_ENABLE_IT(ptTx->ptUartHandel,UART_IT_TXE);//开发送中断
  250. return;
  251. }
  252. }
  253. *((*ptTx).pcBufAddr + (*ptTx).ucBufWrInde) = ucData;
  254. if(++(*ptTx).ucBufWrInde == (*ptTx).ucBufSize)
  255. {
  256. (*ptTx).ucBufWrInde = 0;
  257. }
  258. ++(*ptTx).ucBufCnt;
  259. __HAL_UART_ENABLE_IT(ptTx->ptUartHandel,UART_IT_TXE);//开发送中断
  260. }
  261. void UART_SendData(USART_Buf_TypeDef * ptTx, uint8_t* Data, uint8_t Length)
  262. {
  263. uint8_t i;
  264. if((ptTx->ptUartHandel->Instance == USART1) && (Transmit_UARTOrCAN == 1))
  265. {
  266. return;
  267. }
  268. for(i=0; i<Length; i++)
  269. {
  270. UART_PutChar(ptTx, Data[i]);
  271. }
  272. }
  273. void USARTx_Rx_IRQ(USART_Buf_TypeDef *USARTx_RxBuf_Struct)
  274. {
  275. uint8_t ucData;
  276. if(__HAL_UART_GET_FLAG(USARTx_RxBuf_Struct->ptUartHandel,UART_FLAG_RXNE) != RESET) //接收中断
  277. {
  278. ucData = (uint8_t)(USARTx_RxBuf_Struct->ptUartHandel->Instance->DR & 0x01FF);
  279. if(((USARTx_RxBuf_Struct->ptUartHandel->Instance->SR) & 0x0000) == 0)//无错误
  280. {
  281. *((* USARTx_RxBuf_Struct).pcBufAddr + (* USARTx_RxBuf_Struct).ucBufWrInde) = ucData;
  282. if(++(* USARTx_RxBuf_Struct).ucBufWrInde >= (* USARTx_RxBuf_Struct).ucBufSize)
  283. {
  284. (* USARTx_RxBuf_Struct).ucBufWrInde = 0;
  285. }
  286. if(++(* USARTx_RxBuf_Struct).ucBufCnt > (* USARTx_RxBuf_Struct).ucBufSize)
  287. {
  288. (* USARTx_RxBuf_Struct).ucBufCnt = (* USARTx_RxBuf_Struct).ucBufSize;
  289. (* USARTx_RxBuf_Struct).ucBufOvf = 1;
  290. }
  291. }
  292. __HAL_UART_CLEAR_OREFLAG(USARTx_RxBuf_Struct->ptUartHandel);
  293. }
  294. }
  295. void USARTx_Tx_IRQ(USART_Buf_TypeDef *USARTx_TxBuf_Struct)
  296. {
  297. if((__HAL_UART_GET_FLAG(USARTx_TxBuf_Struct->ptUartHandel, UART_FLAG_TXE) != RESET) && (__HAL_UART_GET_IT_SOURCE(USARTx_TxBuf_Struct->ptUartHandel, UART_IT_TXE) != RESET)) //发送完成中断
  298. {
  299. __HAL_UART_CLEAR_FLAG(USARTx_TxBuf_Struct->ptUartHandel,UART_FLAG_TXE);
  300. if((* USARTx_TxBuf_Struct).ucBufCnt)
  301. {
  302. --(* USARTx_TxBuf_Struct).ucBufCnt;
  303. USARTx_TxBuf_Struct->ptUartHandel->Instance->DR = *(USARTx_TxBuf_Struct->pcBufAddr + USARTx_TxBuf_Struct->ucBufRdInde);
  304. if(++(* USARTx_TxBuf_Struct).ucBufRdInde >= (* USARTx_TxBuf_Struct).ucBufSize)
  305. {
  306. (* USARTx_TxBuf_Struct).ucBufRdInde = 0;
  307. }
  308. }
  309. else
  310. {
  311. /* Disable the UART Transmit Data Register Empty Interrupt */
  312. __HAL_UART_DISABLE_IT(USARTx_TxBuf_Struct->ptUartHandel, UART_IT_TXE);
  313. }
  314. }
  315. }
  316. /* USER CODE END 1 */
  317. /**
  318. * @}
  319. */
  320. /**
  321. * @}
  322. */
  323. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/