/* Enable USART1 */ USART_Cmd(USART1, ENABLE); USART_ClearFlag(USART1,USART_FLAG_TXE); } /******************************************************************************* * Function Name : USART_Init * Description : Initializes the USARTx peripheral according to the specified * parameters in the USART_InitStruct . * Input : - USARTx: Select the USART or the UART peripheral. * This parameter can be one of the following values: * - USART1, USART2, USART3, UART4 or UART5. * - USART_InitStruct: pointer to a USART_InitTypeDef structure * that contains the configuration information for the * specified USART peripheral. * Output : None * Return : None *******************************************************************************/ void USART_Init(USART_TypeDef* USARTx, USART_InitTypeDef* USART_InitStruct) { u32 tmpreg = 0x00, apbclock = 0x00; u32 integerdivider = 0x00; u32 fractionaldivider = 0x00; u32 usartxbase = 0; RCC_ClocksTypeDef RCC_ClocksStatus;
/* Check the parameters */ assert_param(IS_USART_ALL_PERIPH(USARTx)); assert_param(IS_USART_BAUDRATE(USART_InitStruct->USART_BaudRate)); assert_param(IS_USART_WORD_LENGTH(USART_InitStruct->USART_WordLength)); assert_param(IS_USART_STOPBITS(USART_InitStruct->USART_StopBits)); assert_param(IS_USART_PARITY(USART_InitStruct->USART_Parity)); assert_param(IS_USART_MODE(USART_InitStruct->USART_Mode)); assert_param(IS_USART_HARDWARE_FLOW_CONTROL(USART_InitStruct->USART_HardwareFlowControl)); /* The hardware flow control is available only for USART1, USART2 and USART3 */ assert_param(IS_USART_PERIPH_HFC(USARTx, USART_InitStruct->USART_HardwareFlowControl));
/* Configure the USART Stop Bits, Clock, CPOL, CPHA and LastBit ------------*/ /* Set STOP[13:12] bits according to USART_StopBits value */ tmpreg |= (u32)USART_InitStruct->USART_StopBits;
/* Write to USART CR2 */ USARTx->CR2 = (u16)tmpreg;
/*---------------------------- USART CR1 Configuration -----------------------*/ tmpreg = USARTx->CR1; /* Clear M, PCE, PS, TE and RE bits */ tmpreg &= CR1_CLEAR_Mask;
/* Configure the USART Word Length, Parity and mode ----------------------- */ /* Set the M bits according to USART_WordLength value */ /* Set PCE and PS bits according to USART_Parity value */ /* Set TE and RE bits according to USART_Mode value */ tmpreg |= (u32)USART_InitStruct->USART_WordLength | USART_InitStruct->USART_Parity | USART_InitStruct->USART_Mode;
/* Write to USART CR1 */ USARTx->CR1 = (u16)tmpreg;
/* Configure the USART HFC -------------------------------------------------*/ /* Set CTSE and RTSE bits according to USART_HardwareFlowControl value */ tmpreg |= USART_InitStruct->USART_HardwareFlowControl;
/* Write to USART CR3 */ USARTx->CR3 = (u16)tmpreg;