/** * @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 EvbUart2WriteStr(const char* str) { while (*str) { USART_DataSend(USART2, * str++); while (USART_GetBitState(USART2, USART_FLAG_TBE) == RESET) ; } }
/************************************************************************************************* * 功能:从串口2接收一个字符 * * 参数:(1) 存储接收到的字符 * * 返回: * * 说明: * *************************************************************************************************/ void EvbUart2ReadByte(char* c) { while (USART_GetBitState(USART2, USART_FLAG_RBNE) == RESET) ; *c = (USART_DataReceive(USART2)); }
/************************************************************************************************* * 功能:向串口2发送一个字符 * * 参数:(1) 需要被发送的字符 * * 返回: * * 说明: * *************************************************************************************************/ void EvbUart2WriteByte(char c) { USART_DataSend(USART2, c); while (USART_GetBitState(USART2, USART_FLAG_TBE) == RESET) ; }
/** * @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) { } }