usart.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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 "gpio.h"
  42. /* USER CODE BEGIN 0 */
  43. uint8_t UART_RxBuff3[UART_BUFF_SIZE];
  44. USART_Buf_TypeDef UART_RxBuff_Struct3 = {UART_BUFF_SIZE,0,0,0,0,UART_RxBuff3,&huart3};
  45. uint8_t UART_TxBuff3[UART_BUFF_SIZE];
  46. USART_Buf_TypeDef UART_TxBuff_Struct3 = {UART_BUFF_SIZE,0,0,0,0,UART_TxBuff3,&huart3};
  47. uint8_t UART_RxBuff1[UART_BUFF_SIZE];
  48. USART_Buf_TypeDef UART_RxBuff_Struct1 = {UART_BUFF_SIZE,0,0,0,0,UART_RxBuff1,&huart1};
  49. uint8_t UART_TxBuff1[UART_BUFF_SIZE];
  50. USART_Buf_TypeDef UART_TxBuff_Struct1 = {UART_BUFF_SIZE,0,0,0,0,UART_TxBuff1,&huart1};
  51. /* USER CODE END 0 */
  52. UART_HandleTypeDef huart3;
  53. UART_HandleTypeDef huart1;
  54. /* USART3 init function */
  55. void MX_USART3_UART_Init(void)
  56. {
  57. huart3.Instance = USART3;
  58. huart3.Init.BaudRate = 115200;
  59. huart3.Init.WordLength = UART_WORDLENGTH_8B;
  60. huart3.Init.StopBits = UART_STOPBITS_1;
  61. huart3.Init.Parity = UART_PARITY_NONE;
  62. huart3.Init.Mode = UART_MODE_TX_RX;
  63. huart3.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  64. huart3.Init.OverSampling = UART_OVERSAMPLING_16;
  65. if (HAL_UART_Init(&huart3) != HAL_OK)
  66. {
  67. _Error_Handler(__FILE__, __LINE__);
  68. }
  69. }
  70. void MX_USART1_UART_Init(uint32_t baudrate)
  71. {
  72. huart1.Instance = USART1;
  73. huart1.Init.BaudRate = baudrate;
  74. huart1.Init.WordLength = UART_WORDLENGTH_8B;
  75. huart1.Init.StopBits = UART_STOPBITS_1;
  76. huart1.Init.Parity = UART_PARITY_NONE;
  77. huart1.Init.Mode = UART_MODE_TX_RX;
  78. huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  79. huart1.Init.OverSampling = UART_OVERSAMPLING_16;
  80. if (HAL_UART_Init(&huart1) != HAL_OK)
  81. {
  82. _Error_Handler(__FILE__, __LINE__);
  83. }
  84. }
  85. void MX_USART1_Reopen(uint32_t baudrate)
  86. {
  87. HAL_UART_DeInit(&huart1);
  88. MX_USART1_UART_Init(baudrate);
  89. }
  90. void HAL_UART_MspInit(UART_HandleTypeDef* uartHandle)
  91. {
  92. GPIO_InitTypeDef GPIO_InitStruct;
  93. if(uartHandle->Instance==USART3)
  94. {
  95. /* USER CODE BEGIN USART3_MspInit 0 */
  96. /* USER CODE END USART3_MspInit 0 */
  97. /* USART3 clock enable */
  98. __HAL_RCC_USART3_CLK_ENABLE();
  99. /**USART3 GPIO Configuration
  100. PB10 ------> USART3_TX
  101. PB11 ------> USART3_RX
  102. */
  103. GPIO_InitStruct.Pin = GPIO_PIN_10;
  104. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  105. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
  106. HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  107. GPIO_InitStruct.Pin = GPIO_PIN_11;
  108. GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  109. GPIO_InitStruct.Pull = GPIO_NOPULL;
  110. HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  111. /* USART3 interrupt Init */
  112. HAL_NVIC_SetPriority(USART3_IRQn, 0, 0);
  113. HAL_NVIC_EnableIRQ(USART3_IRQn);
  114. /* USER CODE BEGIN USART3_MspInit 1 */
  115. __HAL_UART_ENABLE_IT(&huart3, UART_IT_PE);
  116. /* Enable the UART Error Interrupt: (Frame error, noise error, overrun error) */
  117. __HAL_UART_ENABLE_IT(&huart3, UART_IT_ERR);
  118. /* Enable the UART Data Register not empty Interrupt */
  119. __HAL_UART_ENABLE_IT(&huart3, UART_IT_RXNE);
  120. /* USER CODE END USART3_MspInit 1 */
  121. }
  122. else if(uartHandle->Instance==USART1)
  123. {
  124. /* USER CODE BEGIN USART3_MspInit 0 */
  125. /* USER CODE END USART3_MspInit 0 */
  126. /* USART3 clock enable */
  127. __HAL_RCC_USART1_CLK_ENABLE();
  128. __HAL_RCC_GPIOA_CLK_ENABLE();
  129. /**USART3 GPIO Configuration
  130. PB10 ------> USART3_TX
  131. PB11 ------> USART3_RX
  132. */
  133. GPIO_InitStruct.Pin = GPIO_PIN_9;
  134. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  135. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
  136. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  137. GPIO_InitStruct.Pin = GPIO_PIN_10;
  138. GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  139. GPIO_InitStruct.Pull = GPIO_NOPULL;
  140. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  141. /* USART3 interrupt Init */
  142. HAL_NVIC_SetPriority(USART1_IRQn, 0, 0);
  143. HAL_NVIC_EnableIRQ(USART1_IRQn);
  144. /* USER CODE BEGIN USART3_MspInit 1 */
  145. __HAL_UART_ENABLE_IT(uartHandle, UART_IT_PE);
  146. /* Enable the UART Error Interrupt: (Frame error, noise error, overrun error) */
  147. __HAL_UART_ENABLE_IT(uartHandle, UART_IT_ERR);
  148. /* Enable the UART Data Register not empty Interrupt */
  149. __HAL_UART_ENABLE_IT(uartHandle, UART_IT_RXNE);
  150. /* USER CODE END USART3_MspInit 1 */
  151. }
  152. }
  153. void HAL_UART_MspDeInit(UART_HandleTypeDef* uartHandle)
  154. {
  155. if(uartHandle->Instance==USART3)
  156. {
  157. /* USER CODE BEGIN USART3_MspDeInit 0 */
  158. /* USER CODE END USART3_MspDeInit 0 */
  159. /* Peripheral clock disable */
  160. __HAL_RCC_USART3_CLK_DISABLE();
  161. /**USART3 GPIO Configuration
  162. PB10 ------> USART3_TX
  163. PB11 ------> USART3_RX
  164. */
  165. HAL_GPIO_DeInit(GPIOB, GPIO_PIN_10|GPIO_PIN_11);
  166. /* USART3 interrupt Deinit */
  167. HAL_NVIC_DisableIRQ(USART3_IRQn);
  168. /* USER CODE BEGIN USART3_MspDeInit 1 */
  169. /* USER CODE END USART3_MspDeInit 1 */
  170. }
  171. else if(uartHandle->Instance==USART1)
  172. {
  173. /* USER CODE BEGIN USART1_MspDeInit 0 */
  174. /* USER CODE END USART1_MspDeInit 0 */
  175. /* Peripheral clock disable */
  176. __HAL_RCC_USART1_CLK_DISABLE();
  177. /**USART1 GPIO Configuration
  178. PA9 ------> USART1_TX
  179. PA10 ------> USART1_RX
  180. */
  181. HAL_GPIO_DeInit(GPIOA, GPIO_PIN_9|GPIO_PIN_10);
  182. /* USART1 interrupt Deinit */
  183. HAL_NVIC_DisableIRQ(USART1_IRQn);
  184. /* USER CODE BEGIN USART1_MspDeInit 1 */
  185. /* USER CODE END USART1_MspDeInit 1 */
  186. }
  187. }
  188. /* USER CODE BEGIN 1 */
  189. uint8_t UART_ReadChar(USART_Buf_TypeDef * ptRx, uint8_t ucNum)
  190. {
  191. uint8_t ucData;
  192. uint16_t i;
  193. i = ucNum;
  194. if ((*ptRx).ucBufCnt >= ucNum)
  195. {
  196. i += (*ptRx).ucBufRdInde;
  197. if (i >= (*ptRx).ucBufSize)
  198. {
  199. i -=((*ptRx).ucBufSize);
  200. }
  201. }
  202. else
  203. {
  204. i=0;
  205. }
  206. ucData = *((*ptRx).pcBufAddr + i);
  207. return ucData;
  208. }
  209. void UART_DelChar(USART_Buf_TypeDef * ptRx, uint8_t ucNum)
  210. {
  211. uint16_t i;
  212. if ((*ptRx).ucBufCnt >= ucNum)
  213. {
  214. __HAL_UART_DISABLE_IT(ptRx->ptUartHandel,UART_IT_RXNE);//关接收中断
  215. (*ptRx).ucBufCnt -= ucNum;
  216. __HAL_UART_ENABLE_IT(ptRx->ptUartHandel,UART_IT_RXNE);//开接收中断
  217. i = ucNum;
  218. i += (*ptRx).ucBufRdInde;
  219. if (i >= (*ptRx).ucBufSize)
  220. {
  221. i -= (*ptRx).ucBufSize;
  222. }
  223. (*ptRx).ucBufRdInde = i;
  224. }
  225. else
  226. {
  227. __HAL_UART_DISABLE_IT(ptRx->ptUartHandel,UART_IT_RXNE);//关接收中断
  228. i = (*ptRx).ucBufCnt;
  229. (*ptRx).ucBufCnt = 0;
  230. __HAL_UART_ENABLE_IT(ptRx->ptUartHandel,UART_IT_RXNE);//开接收中断
  231. i += (*ptRx).ucBufRdInde;
  232. if (i >= (*ptRx).ucBufSize)
  233. {
  234. i -= (*ptRx).ucBufSize;
  235. }
  236. (*ptRx).ucBufRdInde = i;
  237. }
  238. }
  239. void UART_PutChar(USART_Buf_TypeDef * ptTx, uint8_t ucData)
  240. {
  241. while ((*ptTx).ucBufCnt == (*ptTx).ucBufSize); //发送缓冲满,等待。若有此情况建议增大缓冲区
  242. __HAL_UART_DISABLE_IT(ptTx->ptUartHandel,UART_IT_TXE);//关发送中断
  243. if((*ptTx).ucBufCnt == 0)
  244. {
  245. if(__HAL_UART_GET_FLAG(ptTx->ptUartHandel, UART_FLAG_TXE) == SET) //check if transmit data register full or not
  246. {
  247. ptTx->ptUartHandel->Instance->DR = ucData;
  248. __HAL_UART_ENABLE_IT(ptTx->ptUartHandel,UART_IT_TXE);//开发送中断
  249. return;
  250. }
  251. }
  252. *((*ptTx).pcBufAddr + (*ptTx).ucBufWrInde) = ucData;
  253. if(++(*ptTx).ucBufWrInde == (*ptTx).ucBufSize)
  254. {
  255. (*ptTx).ucBufWrInde = 0;
  256. }
  257. ++(*ptTx).ucBufCnt;
  258. __HAL_UART_ENABLE_IT(ptTx->ptUartHandel,UART_IT_TXE);//开发送中断
  259. }
  260. void UART_SendData(USART_Buf_TypeDef * ptTx, uint8_t* Data, uint8_t Length)
  261. {
  262. uint8_t i;
  263. for(i=0; i<Length; i++)
  264. {
  265. UART_PutChar(ptTx, Data[i]);
  266. }
  267. }
  268. void USARTx_Rx_IRQ(USART_Buf_TypeDef *USARTx_RxBuf_Struct)
  269. {
  270. uint8_t ucData;
  271. if(__HAL_UART_GET_FLAG(USARTx_RxBuf_Struct->ptUartHandel,UART_FLAG_RXNE) != RESET) //接收中断
  272. {
  273. ucData = (uint8_t)(USARTx_RxBuf_Struct->ptUartHandel->Instance->DR & 0x01FF);
  274. if(((USARTx_RxBuf_Struct->ptUartHandel->Instance->SR) & 0x0000) == 0)//无错误
  275. {
  276. *((* USARTx_RxBuf_Struct).pcBufAddr + (* USARTx_RxBuf_Struct).ucBufWrInde) = ucData;
  277. if(++(* USARTx_RxBuf_Struct).ucBufWrInde >= (* USARTx_RxBuf_Struct).ucBufSize)
  278. {
  279. (* USARTx_RxBuf_Struct).ucBufWrInde = 0;
  280. }
  281. if(++(* USARTx_RxBuf_Struct).ucBufCnt > (* USARTx_RxBuf_Struct).ucBufSize)
  282. {
  283. (* USARTx_RxBuf_Struct).ucBufCnt = (* USARTx_RxBuf_Struct).ucBufSize;
  284. (* USARTx_RxBuf_Struct).ucBufOvf = 1;
  285. }
  286. }
  287. __HAL_UART_CLEAR_OREFLAG(USARTx_RxBuf_Struct->ptUartHandel);
  288. }
  289. }
  290. void USARTx_Tx_IRQ(USART_Buf_TypeDef *USARTx_TxBuf_Struct)
  291. {
  292. 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)) //发送完成中断
  293. {
  294. __HAL_UART_CLEAR_FLAG(USARTx_TxBuf_Struct->ptUartHandel,UART_FLAG_TXE);
  295. if((* USARTx_TxBuf_Struct).ucBufCnt)
  296. {
  297. --(* USARTx_TxBuf_Struct).ucBufCnt;
  298. USARTx_TxBuf_Struct->ptUartHandel->Instance->DR = *(USARTx_TxBuf_Struct->pcBufAddr + USARTx_TxBuf_Struct->ucBufRdInde);
  299. if(++(* USARTx_TxBuf_Struct).ucBufRdInde >= (* USARTx_TxBuf_Struct).ucBufSize)
  300. {
  301. (* USARTx_TxBuf_Struct).ucBufRdInde = 0;
  302. }
  303. }
  304. else
  305. {
  306. /* Disable the UART Transmit Data Register Empty Interrupt */
  307. __HAL_UART_DISABLE_IT(USARTx_TxBuf_Struct->ptUartHandel, UART_IT_TXE);
  308. }
  309. }
  310. }
  311. /* USER CODE END 1 */
  312. /**
  313. * @}
  314. */
  315. /**
  316. * @}
  317. */
  318. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/