/** * @brief Start Bit Method to Wake Up USART from DeepSleep mode Test. * @param None * @retval None */ static void WakeUp_StartBitMethod(void) { /* Configure the wake up Method = Start bit */ USART_DEEPSLEEPModeWakeUpSourceConfig(USART1, USART_WAKEUPSOURCE_STARTBIT); /* Enable USART1 */ USART_Enable(USART1, ENABLE); /* Before entering the USART in STOP mode the REACK flag must be checked to ensure the USART RX is ready */ while(USART_GetBitState(USART1, USART_FLAG_REA) == RESET) {} /* Enable USART STOP mode by setting the UESM bit in the CTLR1 register.*/ USART_DEEPSLEEPModeEnable(USART1, ENABLE); /* Enable the wake up from stop Interrupt */ USART_INT_Set(USART1, USART_INT_WU, ENABLE); /* Enable PWR APB clock */ RCC_APB1PeriphClock_Enable(RCC_APB1PERIPH_PWR, ENABLE); /*Enter Deep-sleep mode*/ PWR_DEEPSLEEPMode_Entry(PWR_LDO_LOWPOWER, PWR_DEEPSLEEPENTRY_WFI); /* Waiting Wake Up interrupt */ while(InterruptCounter == 0x00) {} /* Disable USART peripheral in DEEPSLEEP mode */ USART_DEEPSLEEPModeEnable(USART1, DISABLE); while(USART_GetBitState(USART1, USART_FLAG_RBNE) == RESET) {} DataReceived = USART_DataReceive(USART1); /* Clear the TEN bit (if a transmission is on going or a data is in the TDR, it will be sent before efectivelly disabling the transmission) */ USART_TransferDirection_Enable(USART1, USART_RXORTX_TX, DISABLE); /* Check the Transfer Complete Flag */ while (USART_GetBitState(USART1, USART_FLAG_TC) == RESET) {} /* USART Disable */ USART_Enable(USART1, DISABLE); }
void serial_init(void) { //RCC_AHBPeriphClock_Enable( RCC_AHBPERIPH_GPIOA , ENABLE ); RCC_APB1PeriphClock_Enable( RCC_APB1PERIPH_USART2 , ENABLE ); GPIO_InitPara GPIO_InitStructure; GPIO_PinAFConfig( GPIOA , GPIO_PINSOURCE2, GPIO_AF_1 ); GPIO_PinAFConfig( GPIOA , GPIO_PINSOURCE3, GPIO_AF_1 ); GPIO_InitStructure.GPIO_Pin = GPIO_PIN_2 | GPIO_PIN_3; GPIO_InitStructure.GPIO_Mode = GPIO_MODE_AF; GPIO_InitStructure.GPIO_Speed = GPIO_SPEED_50MHZ; GPIO_InitStructure.GPIO_OType = GPIO_OTYPE_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PUPD_NOPULL; GPIO_Init( GPIOA , &GPIO_InitStructure); USART_InitPara USART_InitStructure; USART_InitStructure.USART_BRR = 57600; USART_InitStructure.USART_WL = USART_WL_8B; USART_InitStructure.USART_STBits = USART_STBITS_1; USART_InitStructure.USART_Parity = USART_PARITY_RESET; USART_InitStructure.USART_HardwareFlowControl = USART_HARDWAREFLOWCONTROL_NONE; USART_InitStructure.USART_RxorTx = USART_RXORTX_TX; USART_Init(USART2, &USART_InitStructure); USART_Enable(USART2, ENABLE); USART_INT_Set(USART2, USART_INT_TC , ENABLE); // USART_INT_TBE }
/* 说明一下,IDLE的中断在串口无数据接收的情况下,是不会一直产生的, 产生的条件是这样的,当清除IDLE标志位后,必须有接收到第一个数据后,才开始触发, 一断接收的数据断流,没有接收到数据,即产生IDLE中断。*/ UINT32 IO_DataUartISR(UINT32 data) { static UINT32 index = 0; UINT16 rxc; UINT8 txc; UINT32 Fifodepth; UINT8* temp; #if 1 /*******************************************************/ /* handle RX interrupt */ /*******************************************************/ /* Service RX data ready as long as there is data in RX FIFO */ if( USART_GetIntBitState(USART1, USART_INT_RBNE) != RESET) { USART_INT_Set( USART1, USART_INT_IDLEF, ENABLE ); /* Read data from RX UART1 DR and store in buffer */ rxc = USART_DataReceive(USART1) & 0xff; UartDev.DATA.RxBufWrPtr[index] = (UINT8)rxc; UartDev.DATA.RxBufCount++; index++; USART_ClearIntBitState(USART1, USART_INT_RBNE); } if( USART_GetIntBitState(USART1, USART_INT_IDLEF) != RESET) { USART_INT_Set(USART1, USART_INT_IDLEF, DISABLE ); USART_ClearIntBitState(USART1, USART_INT_IDLEF); index = 0; temp = UartDev.DATA.RxBufRdPtr; UartDev.DATA.RxBufRdPtr = UartDev.DATA.RxBufWrPtr; UartDev.DATA.RxBufWrPtr = temp; OS_SetEvent(IO_EVENT_GROUP_ID, IO_UART_RXIND_FLG); } /*******************************************************/ /* handle TX interrupt */ /*******************************************************/ if( USART_GetIntBitState(USART1, USART_INT_TBE) != RESET) { USART_ClearIntBitState(USART1, USART_INT_TBE); /* Init FIFO count */ Fifodepth = UART_UART_TX_HW_FIFO_SIZE; /* Add data to TX UART FIFO until it is full */ while((UartDev.DATA.TxBufCount != 0) && (Fifodepth > 0)) { /* Write data byte to UART TX FIFO */ /* Write one byte to the transmit data register */ txc = *(UartDev.DATA.TxBufPtr); USART_DataSend( USART1 , txc ); UartDev.DATA.TxBufPtr++; /* Decrement Tx Serial count */ UartDev.DATA.TxBufCount--; /* Decrement FIFO count */ Fifodepth--; } /* if we have send all character in fifo */ if (UartDev.DATA.TxBufCount == 0 ) { /* disable the USART1 Transmoit interrupt */ USART_INT_Set(USART1, USART_INT_TBE, DISABLE ); OS_SetEvent(IO_EVENT_GROUP_ID, IO_UART_TXRSP_FLG); } } #else /*******************************************************/ /* handle RX interrupt */ /*******************************************************/ /* Service RX data ready as long as there is data in RX FIFO */ if( USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) { USART_ITConfig( USART1, USART_IT_IDLE, ENABLE ); /* Read data from RX UART1 DR and store in buffer */ rxc = USART_ReceiveData(USART1) & 0xff; UartDev.DATA.RxBufWrPtr[index] = (UINT8)rxc; UartDev.DATA.RxBufCount++; index++; USART_ClearITPendingBit(USART1, USART_IT_RXNE); } if( USART_GetITStatus(USART1, USART_IT_IDLE) != RESET) { USART_ClearITPendingBit(USART1, USART_IT_IDLE); USART_ITConfig(USART1, USART_IT_IDLE, DISABLE ); index = 0; temp = UartDev.DATA.RxBufRdPtr; UartDev.DATA.RxBufRdPtr = UartDev.DATA.RxBufWrPtr; UartDev.DATA.RxBufWrPtr = temp; OS_SetEvent(IO_EVENT_GROUP_ID, IO_UART_RXIND_FLG); } /*******************************************************/ /* handle TX interrupt */ /*******************************************************/ if( USART_GetITStatus(USART1, USART_IT_TXE) != RESET) { USART_ClearITPendingBit(USART1, USART_IT_TXE); /* Init FIFO count */ Fifodepth = UART_UART_TX_HW_FIFO_SIZE; /* Add data to TX UART FIFO until it is full */ while((UartDev.DATA.TxBufCount != 0) && (Fifodepth > 0)) { /* Write data byte to UART TX FIFO */ /* Write one byte to the transmit data register */ txc = *(UartDev.DATA.TxBufPtr); USART_SendData( USART1 , txc ); UartDev.DATA.TxBufPtr++; /* Decrement Tx Serial count */ UartDev.DATA.TxBufCount--; /* Decrement FIFO count */ Fifodepth--; } /* if we have send all character in fifo */ if (UartDev.DATA.TxBufCount == 0 ) { /* Enable the USART1 Transmoit interrupt */ USART_ITConfig(USART1, USART_IT_TXE, DISABLE ); OS_SetEvent(IO_EVENT_GROUP_ID, IO_UART_TXRSP_FLG); } } #endif return 0; }