usart.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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. /* 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 = 115200;
  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. /* USART3 clock enable */
  73. __HAL_RCC_USART3_CLK_ENABLE();
  74. /**USART3 GPIO Configuration
  75. PB10 ------> USART3_TX
  76. PB11 ------> 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(GPIOB, &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(GPIOB, &GPIO_InitStruct);
  86. /* USART3 interrupt Init */
  87. HAL_NVIC_SetPriority(USART3_IRQn, 0, 0);
  88. HAL_NVIC_EnableIRQ(USART3_IRQn);
  89. /* USER CODE BEGIN USART3_MspInit 1 */
  90. __HAL_UART_ENABLE_IT(&huart3, UART_IT_PE);
  91. /* Enable the UART Error Interrupt: (Frame error, noise error, overrun error) */
  92. __HAL_UART_ENABLE_IT(&huart3, UART_IT_ERR);
  93. /* Enable the UART Data Register not empty Interrupt */
  94. __HAL_UART_ENABLE_IT(&huart3, UART_IT_RXNE);
  95. /* USER CODE END USART3_MspInit 1 */
  96. }
  97. }
  98. void HAL_UART_MspDeInit(UART_HandleTypeDef* uartHandle)
  99. {
  100. if(uartHandle->Instance==USART3)
  101. {
  102. /* USER CODE BEGIN USART3_MspDeInit 0 */
  103. /* USER CODE END USART3_MspDeInit 0 */
  104. /* Peripheral clock disable */
  105. __HAL_RCC_USART3_CLK_DISABLE();
  106. /**USART3 GPIO Configuration
  107. PB10 ------> USART3_TX
  108. PB11 ------> USART3_RX
  109. */
  110. HAL_GPIO_DeInit(GPIOB, GPIO_PIN_10|GPIO_PIN_11);
  111. /* USART3 interrupt Deinit */
  112. HAL_NVIC_DisableIRQ(USART3_IRQn);
  113. /* USER CODE BEGIN USART3_MspDeInit 1 */
  114. /* USER CODE END USART3_MspDeInit 1 */
  115. }
  116. }
  117. /* USER CODE BEGIN 1 */
  118. uint8_t UART_ReadChar(USART_Buf_TypeDef * ptRx, uint8_t ucNum)
  119. {
  120. uint8_t ucData;
  121. uint16_t i;
  122. i = ucNum;
  123. if ((*ptRx).ucBufCnt >= ucNum)
  124. {
  125. i += (*ptRx).ucBufRdInde;
  126. if (i >= (*ptRx).ucBufSize)
  127. {
  128. i -=((*ptRx).ucBufSize);
  129. }
  130. }
  131. else
  132. {
  133. i=0;
  134. }
  135. ucData = *((*ptRx).pcBufAddr + i);
  136. return ucData;
  137. }
  138. void UART_DelChar(USART_Buf_TypeDef * ptRx, uint8_t ucNum)
  139. {
  140. uint16_t i;
  141. if ((*ptRx).ucBufCnt >= ucNum)
  142. {
  143. __HAL_UART_DISABLE_IT(ptRx->ptUartHandel,UART_IT_RXNE);//关接收中断
  144. (*ptRx).ucBufCnt -= ucNum;
  145. __HAL_UART_ENABLE_IT(ptRx->ptUartHandel,UART_IT_RXNE);//开接收中断
  146. i = ucNum;
  147. i += (*ptRx).ucBufRdInde;
  148. if (i >= (*ptRx).ucBufSize)
  149. {
  150. i -= (*ptRx).ucBufSize;
  151. }
  152. (*ptRx).ucBufRdInde = i;
  153. }
  154. else
  155. {
  156. __HAL_UART_DISABLE_IT(ptRx->ptUartHandel,UART_IT_RXNE);//关接收中断
  157. i = (*ptRx).ucBufCnt;
  158. (*ptRx).ucBufCnt = 0;
  159. __HAL_UART_ENABLE_IT(ptRx->ptUartHandel,UART_IT_RXNE);//开接收中断
  160. i += (*ptRx).ucBufRdInde;
  161. if (i >= (*ptRx).ucBufSize)
  162. {
  163. i -= (*ptRx).ucBufSize;
  164. }
  165. (*ptRx).ucBufRdInde = i;
  166. }
  167. }
  168. void UART_PutChar(USART_Buf_TypeDef * ptTx, uint8_t ucData)
  169. {
  170. while ((*ptTx).ucBufCnt == (*ptTx).ucBufSize); //发送缓冲满,等待。若有此情况建议增大缓冲区
  171. __HAL_UART_DISABLE_IT(ptTx->ptUartHandel,UART_IT_TXE);//关发送中断
  172. if((*ptTx).ucBufCnt == 0)
  173. {
  174. if(__HAL_UART_GET_FLAG(ptTx->ptUartHandel, UART_FLAG_TXE) == SET) //check if transmit data register full or not
  175. {
  176. ptTx->ptUartHandel->Instance->DR = ucData;
  177. __HAL_UART_ENABLE_IT(ptTx->ptUartHandel,UART_IT_TXE);//开发送中断
  178. return;
  179. }
  180. }
  181. *((*ptTx).pcBufAddr + (*ptTx).ucBufWrInde) = ucData;
  182. if(++(*ptTx).ucBufWrInde == (*ptTx).ucBufSize)
  183. {
  184. (*ptTx).ucBufWrInde = 0;
  185. }
  186. ++(*ptTx).ucBufCnt;
  187. __HAL_UART_ENABLE_IT(ptTx->ptUartHandel,UART_IT_TXE);//开发送中断
  188. }
  189. void UART_SendData(USART_Buf_TypeDef * ptTx, uint8_t* Data, uint8_t Length)
  190. {
  191. uint8_t i;
  192. for(i=0; i<Length; i++)
  193. {
  194. UART_PutChar(ptTx, Data[i]);
  195. }
  196. }
  197. void USARTx_Rx_IRQ(USART_Buf_TypeDef *USARTx_RxBuf_Struct)
  198. {
  199. uint8_t ucData;
  200. if(__HAL_UART_GET_FLAG(USARTx_RxBuf_Struct->ptUartHandel,UART_FLAG_RXNE) != RESET) //接收中断
  201. {
  202. ucData = (uint8_t)(USARTx_RxBuf_Struct->ptUartHandel->Instance->DR & 0x01FF);
  203. if(((USARTx_RxBuf_Struct->ptUartHandel->Instance->SR) & 0x0000) == 0)//无错误
  204. {
  205. *((* USARTx_RxBuf_Struct).pcBufAddr + (* USARTx_RxBuf_Struct).ucBufWrInde) = ucData;
  206. if(++(* USARTx_RxBuf_Struct).ucBufWrInde >= (* USARTx_RxBuf_Struct).ucBufSize)
  207. {
  208. (* USARTx_RxBuf_Struct).ucBufWrInde = 0;
  209. }
  210. if(++(* USARTx_RxBuf_Struct).ucBufCnt > (* USARTx_RxBuf_Struct).ucBufSize)
  211. {
  212. (* USARTx_RxBuf_Struct).ucBufCnt = (* USARTx_RxBuf_Struct).ucBufSize;
  213. (* USARTx_RxBuf_Struct).ucBufOvf = 1;
  214. }
  215. }
  216. __HAL_UART_CLEAR_OREFLAG(USARTx_RxBuf_Struct->ptUartHandel);
  217. }
  218. }
  219. void USARTx_Tx_IRQ(USART_Buf_TypeDef *USARTx_TxBuf_Struct)
  220. {
  221. 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)) //发送完成中断
  222. {
  223. __HAL_UART_CLEAR_FLAG(USARTx_TxBuf_Struct->ptUartHandel,UART_FLAG_TXE);
  224. if((* USARTx_TxBuf_Struct).ucBufCnt)
  225. {
  226. --(* USARTx_TxBuf_Struct).ucBufCnt;
  227. USARTx_TxBuf_Struct->ptUartHandel->Instance->DR = *(USARTx_TxBuf_Struct->pcBufAddr + USARTx_TxBuf_Struct->ucBufRdInde);
  228. if(++(* USARTx_TxBuf_Struct).ucBufRdInde >= (* USARTx_TxBuf_Struct).ucBufSize)
  229. {
  230. (* USARTx_TxBuf_Struct).ucBufRdInde = 0;
  231. }
  232. }
  233. else
  234. {
  235. /* Disable the UART Transmit Data Register Empty Interrupt */
  236. __HAL_UART_DISABLE_IT(USARTx_TxBuf_Struct->ptUartHandel, UART_IT_TXE);
  237. }
  238. }
  239. }
  240. /* USER CODE END 1 */
  241. /**
  242. * @}
  243. */
  244. /**
  245. * @}
  246. */
  247. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/