Uart1.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #include "uart1.h"
  2. uint8_t TxBufferUart[256] = { 0 };
  3. uint8_t RxBufferUart[256] = { 0 };
  4. __IO uint8_t TxCounterUart = 0x00;
  5. __IO uint8_t RxCounterUart = 0x00;
  6. uint8_t UartSendCount = 0;
  7. bool IsUart1Rcv = false;
  8. /*******************************************************************************
  9. * Function Name : USART_Configuration
  10. * Description : Configure USART1
  11. * Input : None
  12. * Output : None
  13. * Return : None
  14. * Attention : None
  15. *******************************************************************************/
  16. void GpioConfig_Usart1 ( void )
  17. {
  18. #if WIFI_Uart1_def
  19. GPIO_InitTypeDef GPIO_InitStructure;
  20. USART_InitTypeDef USART_InitStructure;
  21. NVIC_InitTypeDef NVIC_InitStructure;
  22. //使能USART1/GPIO 时钟可配置
  23. //时钟配置
  24. RCC_AHBPeriphClockCmd ( RCC_AHBPeriph_GPIOA, ENABLE );
  25. RCC_APB2PeriphClockCmd ( RCC_APB2Periph_USART1, ENABLE );
  26. /*
  27. * USART1_TX -> PA9 , USART1_RX -> PA10
  28. */
  29. GPIO_PinAFConfig ( Wifi_GPIO, GPIO_PinSource9, GPIO_AF_1 );
  30. GPIO_PinAFConfig ( Wifi_GPIO, GPIO_PinSource10, GPIO_AF_1 );
  31. GPIO_InitStructure.GPIO_Pin = Wifi_Tx | Wifi_Rx;
  32. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
  33. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  34. GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  35. GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
  36. GPIO_Init ( Wifi_GPIO, &GPIO_InitStructure );
  37. USART_InitStructure.USART_BaudRate = Wifi_UART_BAUD;
  38. USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  39. USART_InitStructure.USART_StopBits = USART_StopBits_1;
  40. USART_InitStructure.USART_Parity = USART_Parity_No;
  41. USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  42. USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
  43. USART_Init ( Wifi_UART, &USART_InitStructure );
  44. USART_OverrunDetectionConfig ( USART1, USART_OVRDetection_Disable ); //使能前,关闭溢出
  45. USART_ClearFlag ( Wifi_UART, USART_FLAG_TC );
  46. USART_ITConfig ( Wifi_UART, USART_IT_RXNE, ENABLE );
  47. USART_ITConfig ( Wifi_UART, USART_IT_TXE, DISABLE );
  48. USART_Cmd ( Wifi_UART, ENABLE ); //使能串口
  49. NVIC_InitStructure.NVIC_IRQChannel = Wifi_NVIC_Interrupt;
  50. NVIC_InitStructure.NVIC_IRQChannelPriority = Wifi_NVIC_Pre;
  51. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  52. NVIC_Init ( &NVIC_InitStructure ); //使能串口中断
  53. #endif
  54. }
  55. void UartSend ( char *buffer, int nBytes )
  56. {
  57. // Wait for previous xmt to complete
  58. while ( TxCounterUart < UartSendCount );
  59. // Setup next XMT
  60. memcpy ( TxBufferUart, buffer, nBytes );
  61. TxCounterUart = 0;
  62. UartSendCount = nBytes;
  63. USART_ITConfig ( USART1, USART_IT_TXE, ENABLE );
  64. }
  65. void USART1_IRQHandler ( void )
  66. {
  67. if ( USART_GetITStatus ( Wifi_UART, USART_IT_RXNE ) != RESET )
  68. {
  69. RxBufferUart[RxCounterUart] = USART_ReceiveData ( Wifi_UART );
  70. if ( RxCounterUart >= 2 ) //当前是\n,上个如果是\r
  71. {
  72. if ( ( RxBufferUart[RxCounterUart - 1] == 0x0D ) && ( RxBufferUart[RxCounterUart] == 0x0A ) ) //start字符个数
  73. {
  74. IsUart1Rcv = true;
  75. }
  76. }
  77. RxCounterUart++;
  78. USART_ClearITPendingBit ( Wifi_UART, USART_IT_RXNE );
  79. }
  80. if ( USART_GetITStatus ( Wifi_UART, USART_IT_TXE ) != RESET )
  81. {
  82. USART_SendData ( Wifi_UART, TxBufferUart[TxCounterUart++] );
  83. if ( TxCounterUart >= UartSendCount )
  84. {
  85. USART_ITConfig ( Wifi_UART, USART_IT_TXE, DISABLE );
  86. }
  87. USART_ClearITPendingBit ( Wifi_UART, USART_IT_TXE );
  88. }
  89. }