usart.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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[255];
  44. USART_Buf_TypeDef UART_RxBuff_Struct3 = {255,0,0,0,0,UART_RxBuff3,&huart3};
  45. uint8_t UART_TxBuff3[255];
  46. USART_Buf_TypeDef UART_TxBuff_Struct3 = {255,0,0,0,0,UART_TxBuff3,&huart3};
  47. /* USER CODE END 0 */
  48. UART_HandleTypeDef huart3;
  49. /* USART3 init function */
  50. void MX_USART3_UART_Init(void)
  51. {
  52. huart3.Instance = USART3;
  53. huart3.Init.BaudRate = 9600;
  54. huart3.Init.WordLength = UART_WORDLENGTH_8B;
  55. huart3.Init.StopBits = UART_STOPBITS_1;
  56. huart3.Init.Parity = UART_PARITY_NONE;
  57. huart3.Init.Mode = UART_MODE_TX_RX;
  58. huart3.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  59. huart3.Init.OverSampling = UART_OVERSAMPLING_16;
  60. if (HAL_UART_Init(&huart3) != HAL_OK)
  61. {
  62. _Error_Handler(__FILE__, __LINE__);
  63. }
  64. }
  65. void HAL_UART_MspInit(UART_HandleTypeDef* uartHandle)
  66. {
  67. GPIO_InitTypeDef GPIO_InitStruct;
  68. if(uartHandle->Instance==USART3)
  69. {
  70. /* USER CODE BEGIN USART3_MspInit 0 */
  71. /* USER CODE END USART3_MspInit 0 */
  72. /* USART1 clock enable */
  73. __HAL_RCC_USART3_CLK_ENABLE();
  74. /**USART1 GPIO Configuration
  75. PC10 ------> USART3_TX
  76. PC11 ------> USART3_RX
  77. */
  78. GPIO_InitStruct.Pin = GPIO_PIN_10;
  79. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  80. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
  81. HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
  82. GPIO_InitStruct.Pin = GPIO_PIN_11;
  83. GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  84. GPIO_InitStruct.Pull = GPIO_NOPULL;
  85. HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
  86. __HAL_AFIO_REMAP_USART3_PARTIAL();
  87. /* USART1 interrupt Init */
  88. HAL_NVIC_SetPriority(USART3_IRQn, 1, 1);
  89. HAL_NVIC_EnableIRQ(USART3_IRQn);
  90. /* USER CODE BEGIN USART3_MspInit 1 */
  91. __HAL_UART_ENABLE_IT(&huart3, UART_IT_PE);
  92. /* Enable the UART Error Interrupt: (Frame error, noise error, overrun error) */
  93. __HAL_UART_ENABLE_IT(&huart3, UART_IT_ERR);
  94. /* Enable the UART Data Register not empty Interrupt */
  95. __HAL_UART_ENABLE_IT(&huart3, UART_IT_RXNE);
  96. /* USER CODE END USART3_MspInit 1 */
  97. }
  98. }
  99. void HAL_UART_MspDeInit(UART_HandleTypeDef* uartHandle)
  100. {
  101. if(uartHandle->Instance==USART3)
  102. {
  103. /* USER CODE BEGIN USART3_MspDeInit 0 */
  104. /* USER CODE END USART3_MspDeInit 0 */
  105. /* Peripheral clock disable */
  106. __HAL_RCC_USART3_CLK_DISABLE();
  107. /**USART1 GPIO Configuration
  108. PC10 ------> USART3_TX
  109. PC11 ------> USART3_RX
  110. */
  111. HAL_GPIO_DeInit(GPIOC, GPIO_PIN_10|GPIO_PIN_11);
  112. /* USART1 interrupt Deinit */
  113. HAL_NVIC_DisableIRQ(USART3_IRQn);
  114. /* USER CODE BEGIN USART3_MspDeInit 1 */
  115. /* USER CODE END USART3_MspDeInit 1 */
  116. }
  117. }
  118. /* USER CODE BEGIN 1 */
  119. uint8_t UART_ReadChar(USART_Buf_TypeDef * ptRx, uint8_t ucNum)
  120. {
  121. uint8_t ucData;
  122. uint16_t i;
  123. i = ucNum;
  124. if ((*ptRx).ucBufCnt >= ucNum)
  125. {
  126. i += (*ptRx).ucBufRdInde;
  127. if (i >= (*ptRx).ucBufSize)
  128. {
  129. i -=((*ptRx).ucBufSize);
  130. }
  131. }
  132. else
  133. {
  134. i=0;
  135. }
  136. ucData = *((*ptRx).pcBufAddr + i);
  137. return ucData;
  138. }
  139. void UART_DelChar(USART_Buf_TypeDef * ptRx, uint8_t ucNum)
  140. {
  141. uint16_t i;
  142. if ((*ptRx).ucBufCnt >= ucNum)
  143. {
  144. __HAL_UART_DISABLE_IT(ptRx->ptUartHandel,UART_IT_RXNE);//关接收中断
  145. (*ptRx).ucBufCnt -= ucNum;
  146. __HAL_UART_ENABLE_IT(ptRx->ptUartHandel,UART_IT_RXNE);//开接收中断
  147. i = ucNum;
  148. i += (*ptRx).ucBufRdInde;
  149. if (i >= (*ptRx).ucBufSize)
  150. {
  151. i -= (*ptRx).ucBufSize;
  152. }
  153. (*ptRx).ucBufRdInde = i;
  154. }
  155. else
  156. {
  157. __HAL_UART_DISABLE_IT(ptRx->ptUartHandel,UART_IT_RXNE);//关接收中断
  158. i = (*ptRx).ucBufCnt;
  159. (*ptRx).ucBufCnt = 0;
  160. __HAL_UART_ENABLE_IT(ptRx->ptUartHandel,UART_IT_RXNE);//开接收中断
  161. i += (*ptRx).ucBufRdInde;
  162. if (i >= (*ptRx).ucBufSize)
  163. {
  164. i -= (*ptRx).ucBufSize;
  165. }
  166. (*ptRx).ucBufRdInde = i;
  167. }
  168. }
  169. void UART_PutChar(USART_Buf_TypeDef * ptTx, uint8_t ucData)
  170. {
  171. while ((*ptTx).ucBufCnt == (*ptTx).ucBufSize); //发送缓冲满,等待。若有此情况建议增大缓冲区
  172. __HAL_UART_DISABLE_IT(ptTx->ptUartHandel,UART_IT_TXE);//关发送中断
  173. if((*ptTx).ucBufCnt == 0)
  174. {
  175. if(__HAL_UART_GET_FLAG(ptTx->ptUartHandel, UART_FLAG_TXE)== SET) //check if transmit data register full or not
  176. {
  177. ptTx->ptUartHandel->Instance->DR = ucData;
  178. __HAL_UART_ENABLE_IT(ptTx->ptUartHandel,UART_IT_TXE);//开发送中断
  179. return;
  180. }
  181. }
  182. *((*ptTx).pcBufAddr + (*ptTx).ucBufWrInde) = ucData;
  183. if(++(*ptTx).ucBufWrInde == (*ptTx).ucBufSize)
  184. {
  185. (*ptTx).ucBufWrInde = 0;
  186. }
  187. ++(*ptTx).ucBufCnt;
  188. __HAL_UART_ENABLE_IT(ptTx->ptUartHandel,UART_IT_TXE);//开发送中断
  189. }
  190. void UART_SendData(USART_Buf_TypeDef * ptTx, uint8_t* Data, uint8_t Length)
  191. {
  192. uint8_t i;
  193. for(i=0; i<Length; i++)
  194. {
  195. UART_PutChar(ptTx, Data[i]);
  196. }
  197. }
  198. void USARTx_Rx_IRQ(USART_Buf_TypeDef *USARTx_RxBuf_Struct)
  199. {
  200. uint8_t ucData;
  201. if(__HAL_UART_GET_FLAG(USARTx_RxBuf_Struct->ptUartHandel,UART_FLAG_RXNE) != RESET) //接收中断
  202. {
  203. ucData = (uint8_t)(USARTx_RxBuf_Struct->ptUartHandel->Instance->DR & 0x01FF);
  204. if(((USARTx_RxBuf_Struct->ptUartHandel->Instance->SR) & 0x0000) == 0)//无错误
  205. {
  206. *((* USARTx_RxBuf_Struct).pcBufAddr + (* USARTx_RxBuf_Struct).ucBufWrInde) = ucData;
  207. if(++(* USARTx_RxBuf_Struct).ucBufWrInde >= (* USARTx_RxBuf_Struct).ucBufSize)
  208. {
  209. (* USARTx_RxBuf_Struct).ucBufWrInde = 0;
  210. }
  211. if(++(* USARTx_RxBuf_Struct).ucBufCnt > (* USARTx_RxBuf_Struct).ucBufSize)
  212. {
  213. (* USARTx_RxBuf_Struct).ucBufCnt = (* USARTx_RxBuf_Struct).ucBufSize;
  214. (* USARTx_RxBuf_Struct).ucBufOvf = 1;
  215. }
  216. }
  217. __HAL_UART_CLEAR_OREFLAG(USARTx_RxBuf_Struct->ptUartHandel);
  218. }
  219. }
  220. void USARTx_Tx_IRQ(USART_Buf_TypeDef *USARTx_TxBuf_Struct)
  221. {
  222. 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)) //发送完成中断
  223. {
  224. __HAL_UART_CLEAR_FLAG(USARTx_TxBuf_Struct->ptUartHandel,UART_FLAG_TXE);
  225. if((* USARTx_TxBuf_Struct).ucBufCnt)
  226. {
  227. --(* USARTx_TxBuf_Struct).ucBufCnt;
  228. USARTx_TxBuf_Struct->ptUartHandel->Instance->DR = *(USARTx_TxBuf_Struct->pcBufAddr + USARTx_TxBuf_Struct->ucBufRdInde);
  229. if(++(* USARTx_TxBuf_Struct).ucBufRdInde >= (* USARTx_TxBuf_Struct).ucBufSize)
  230. {
  231. (* USARTx_TxBuf_Struct).ucBufRdInde = 0;
  232. }
  233. }
  234. else
  235. {
  236. /* Disable the UART Transmit Data Register Empty Interrupt */
  237. __HAL_UART_DISABLE_IT(USARTx_TxBuf_Struct->ptUartHandel, UART_IT_TXE);
  238. }
  239. }
  240. }
  241. /* USER CODE END 1 */
  242. /**
  243. * @}
  244. */
  245. /**
  246. * @}
  247. */
  248. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/