| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- #include "uart1.h"
- uint8_t TxBufferUart[256] = { 0 };
- uint8_t RxBufferUart[256] = { 0 };
- __IO uint8_t TxCounterUart = 0x00;
- __IO uint8_t RxCounterUart = 0x00;
- uint8_t UartSendCount = 0;
- bool IsUart1Rcv = false;
- /*******************************************************************************
- * Function Name : USART_Configuration
- * Description : Configure USART1
- * Input : None
- * Output : None
- * Return : None
- * Attention : None
- *******************************************************************************/
- void GpioConfig_Usart1 ( void )
- {
- #if WIFI_Uart1_def
- GPIO_InitTypeDef GPIO_InitStructure;
- USART_InitTypeDef USART_InitStructure;
- NVIC_InitTypeDef NVIC_InitStructure;
- //使能USART1/GPIO 时钟可配置
- //时钟配置
- RCC_AHBPeriphClockCmd ( RCC_AHBPeriph_GPIOA, ENABLE );
- RCC_APB2PeriphClockCmd ( RCC_APB2Periph_USART1, ENABLE );
- /*
- * USART1_TX -> PA9 , USART1_RX -> PA10
- */
- GPIO_PinAFConfig ( Wifi_GPIO, GPIO_PinSource9, GPIO_AF_1 );
- GPIO_PinAFConfig ( Wifi_GPIO, GPIO_PinSource10, GPIO_AF_1 );
- GPIO_InitStructure.GPIO_Pin = Wifi_Tx | Wifi_Rx;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
- GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
- GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
- GPIO_Init ( Wifi_GPIO, &GPIO_InitStructure );
- USART_InitStructure.USART_BaudRate = Wifi_UART_BAUD;
- USART_InitStructure.USART_WordLength = USART_WordLength_8b;
- USART_InitStructure.USART_StopBits = USART_StopBits_1;
- USART_InitStructure.USART_Parity = USART_Parity_No;
- USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
- USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
- USART_Init ( Wifi_UART, &USART_InitStructure );
- USART_OverrunDetectionConfig ( USART1, USART_OVRDetection_Disable ); //使能前,关闭溢出
- USART_ClearFlag ( Wifi_UART, USART_FLAG_TC );
- USART_ITConfig ( Wifi_UART, USART_IT_RXNE, ENABLE );
- USART_ITConfig ( Wifi_UART, USART_IT_TXE, DISABLE );
- USART_Cmd ( Wifi_UART, ENABLE ); //使能串口
- NVIC_InitStructure.NVIC_IRQChannel = Wifi_NVIC_Interrupt;
- NVIC_InitStructure.NVIC_IRQChannelPriority = Wifi_NVIC_Pre;
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init ( &NVIC_InitStructure ); //使能串口中断
- #endif
- }
- void UartSend ( char *buffer, int nBytes )
- {
- // Wait for previous xmt to complete
- while ( TxCounterUart < UartSendCount );
- // Setup next XMT
- memcpy ( TxBufferUart, buffer, nBytes );
- TxCounterUart = 0;
- UartSendCount = nBytes;
- USART_ITConfig ( USART1, USART_IT_TXE, ENABLE );
- }
- void USART1_IRQHandler ( void )
- {
- if ( USART_GetITStatus ( Wifi_UART, USART_IT_RXNE ) != RESET )
- {
- RxBufferUart[RxCounterUart] = USART_ReceiveData ( Wifi_UART );
- if ( RxCounterUart >= 2 ) //当前是\n,上个如果是\r
- {
- if ( ( RxBufferUart[RxCounterUart - 1] == 0x0D ) && ( RxBufferUart[RxCounterUart] == 0x0A ) ) //start字符个数
- {
- IsUart1Rcv = true;
- }
- }
- RxCounterUart++;
- USART_ClearITPendingBit ( Wifi_UART, USART_IT_RXNE );
- }
- if ( USART_GetITStatus ( Wifi_UART, USART_IT_TXE ) != RESET )
- {
- USART_SendData ( Wifi_UART, TxBufferUart[TxCounterUart++] );
- if ( TxCounterUart >= UartSendCount )
- {
- USART_ITConfig ( Wifi_UART, USART_IT_TXE, DISABLE );
- }
- USART_ClearITPendingBit ( Wifi_UART, USART_IT_TXE );
- }
- }
|