/** * @brief Get the input value and check if it is valid. * @param None * @retval None */ uint8_t USART_Input_Threshold(uint32_t value) { uint32_t index = 0; uint32_t tmp[2] = {0, 0}; while (index < 2) { while (USART_GetBitState(EVAL_COM1, USART_FLAG_RBNE) == RESET) {} tmp[index++] = (USART_DataReceive(EVAL_COM1)); if ((tmp[index - 1] < 0x30) || (tmp[index - 1] > 0x39)) { printf("\n\r Please Input a valid number between 0 and 9 \n\r"); index--; } } index = (tmp[1] - 0x30) + ((tmp[0] - 0x30) * 10); if (index > value) { printf("\n\r Please Input a valid number between 0 and %d \n\r", value); return 0xFF; } return index; }
/** * @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); }
/************************************************************************************************* * 功能:从串口2接收一个字符 * * 参数:(1) 存储接收到的字符 * * 返回: * * 说明: * *************************************************************************************************/ void EvbUart2ReadByte(char* c) { while (USART_GetBitState(USART2, USART_FLAG_RBNE) == RESET) ; *c = (USART_DataReceive(USART2)); }
/* 说明一下,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; }
/** * @brief Main program * @param None * @retval None */ int main(void) { /* System Clocks Configuration */ RCC_Configuration(); /* Configure the GPIO ports */ GPIO_Configuration(); /* USART1 and USART2 configuration */ USART_InitStructure.USART_BRR = 230400; 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_RX | USART_RXORTX_TX; /* Configure USART1 */ USART_Init(USART1, &USART_InitStructure); /* Configure USART2 */ USART_Init(USART2, &USART_InitStructure); /* Enable USART1 */ USART_Enable(USART1, ENABLE); /* Enable USART2 */ USART_Enable(USART2, ENABLE); /* Enable USART1 Half Duplex Mode*/ USART_HalfDuplex_Enable(USART1, ENABLE); /* Enable USART2 Half Duplex Mode*/ USART_HalfDuplex_Enable(USART2, ENABLE); while(NbrOfDataToRead2--) { /* Wait until end of transmit */ while(USART_GetBitState(USART1, USART_FLAG_TBE) == RESET) { } /* Write one byte in the USARTy Transmit Data Register */ USART_DataSend(USART1, TxBuffer1[TxCounter1++]); /* Wait the byte is entirely received by USARTz */ while(USART_GetBitState(USART2, USART_FLAG_RBNE) == RESET) { } /* Store the received byte in the RxBuffer2 */ RxBuffer2[RxCounter2++] = USART_DataReceive(USART2); } /* Clear the USARTy Data Register */ USART_DataReceive(USART1); while(NbrOfDataToRead1--) { /* Wait until end of transmit */ while(USART_GetBitState(USART2, USART_FLAG_TBE)== RESET) { } /* Write one byte in the USARTz Transmit Data Register */ USART_DataSend(USART2, TxBuffer2[TxCounter2++]); /* Wait the byte is entirely received by USARTy */ while(USART_GetBitState(USART1,USART_FLAG_RBNE) == RESET) { } /* Store the received byte in the RxBuffer1 */ RxBuffer1[RxCounter1++] = USART_DataReceive(USART1); } /* Check the received data with the send ones */ TransferStatus1 = Buffercmp(TxBuffer2, RxBuffer1, TxBufferSize2); /* TransferStatus1 = PASSED, if the data transmitted from USART2 and received by USART1 are the same */ /* TransferStatus1 = FAILED, if the data transmitted from USART2 and received by USART1 are different */ TransferStatus2 = Buffercmp(TxBuffer1, RxBuffer2, TxBufferSize1); /* TransferStatus2 = PASSED, if the data transmitted from USART1 and received by USART2 are the same */ /* TransferStatus2 = FAILED, if the data transmitted from USART1 and received by USART2 are different */ while (1) { } }