usart.c 8.8 KB

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