/** * @brief Configure the USART Device * @param None * @retval None */ static void USART_Configuration(void) { USART_InitTypeDef USART_InitStructure; GPIO_InitTypeDef GPIO_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; /* Enable GPIOA and DMA clock */ RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA , ENABLE); /* Enable USART1 APB clock */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); /* Configure the HSI as USART clock */ RCC_USARTCLKConfig(RCC_USART1CLK_HSI); /* USART1 Pins configuration **************************************************/ GPIO_DeInit(GPIOA); /* Connect pin to Periph */ GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_1); GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_1); /* Configure pins as AF pushpull */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10; 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); /* USARTx configured as follow: - BaudRate = 115200 baud - Word Length = 8 Bits - Stop Bit = 1 Stop Bit - Parity = No Parity - Hardware flow control disabled (RTS and CTS signals) - Receive and transmit enabled */ USART_DeInit(USART1); USART_InitStructure.USART_BaudRate = 115200; 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(USART1, &USART_InitStructure); /* USART1 IRQ Channel configuration */ NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; NVIC_InitStructure.NVIC_IRQChannelPriority = 0x01; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); }
void U_USART1::USARTInit(uint32_t baud) { USART_InitTypeDef USART_InitStructure; //配置 USART时钟和分频 RCC_USARTCLKConfig(RCC_USART1CLK_PCLK); RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); //全双工 无校验 停止位1位 8字节 USART_InitStructure.USART_BaudRate = baud; USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; USART_InitStructure.USART_Parity = USART_Parity_No; USART_InitStructure.USART_StopBits = USART_StopBits_1; USART_InitStructure.USART_WordLength = USART_WordLength_8b; USART_Init(USART1, &USART_InitStructure); }
/** * @brief Configure the USART Device * @param None * @retval None */ static void USART_Configuration(void) { USART_InitPara USART_InitStructure; /* Configure the HSI as USART clock */ RCC_USARTCLKConfig(RCC_USART1CLK_HSI); /* USARTx configured as follow: - BaudRate = 115200 baud - Word Length = 8 Bits - Stop Bit = 1 Stop Bit - Parity = No Parity - Hardware flow control disabled (RTS and CTS signals) - Receive and transmit enabled */ USART_DeInit(USART1); USART_InitStructure.USART_BRR = 115200; 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; GD_EVAL_COMInit(&USART_InitStructure); /* USART Disable */ USART_Enable(USART1, DISABLE); /* USART1 IRQ Channel configuration */ { NVIC_InitPara NVIC_InitStructure; /* Enable the USARTx Interrupt */ NVIC_InitStructure.NVIC_IRQ = USART1_IRQn; NVIC_InitStructure.NVIC_IRQPreemptPriority = 0; NVIC_InitStructure.NVIC_IRQSubPriority = 0; NVIC_InitStructure.NVIC_IRQEnable = ENABLE; NVIC_Init(&NVIC_InitStructure); } }
void RCC_Config(void) { RCC_DeInit(); RCC_HSEConfig(RCC_HSE_ON); //Включим внешний генератор while (SUCCESS != RCC_WaitForHSEStartUp()); //Подождем запуска генератора RCC_PLLCmd(DISABLE); RCC_PLLConfig(RCC_PLLSource_PREDIV1,RCC_PLLMul_12); //Вход ПЛЛ внешний генератор умножим на 12 RCC_PLLCmd(ENABLE); RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK); //Системное тактирование от ПЛЛ RCC_HCLKConfig(RCC_SYSCLK_Div1); //Тактирование AHB с делением 1 RCC_PCLKConfig(RCC_HCLK_Div8); //Тактирование переферии с делением 8 RCC_ADCCLKConfig(RCC_ADCCLK_PCLK_Div2); //Тактирование АЦП RCC_I2CCLKConfig(RCC_I2C1CLK_SYSCLK); //Тактируем шину I2C от системного тактирования RCC_USARTCLKConfig(RCC_USART1CLK_PCLK); //Тактируем UART от шини переферии RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA + RCC_AHBPeriph_GPIOB,ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1 + RCC_APB2Periph_USART1 + RCC_APB2Periph_TIM1 + RCC_APB2Periph_TIM15, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM14, ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); #ifdef MCO_ENABLE GPIOA_Struct_init.GPIO_Pin = GPIO_Pin_8; GPIOA_Struct_init.GPIO_Mode = GPIO_Mode_AF; GPIOA_Struct_init.GPIO_Speed = GPIO_Speed_Level_2; GPIOA_Struct_init.GPIO_OType = GPIO_OType_PP; GPIO_Init(GPIOA,&GPIOA_Struct_init); RCC_MCOConfig(RCC_MCOSource_SYSCLK); while (1); #else RCC_MCOConfig(RCC_MCOSource_NoClock); #endif /*MCO_ENABLE */ };
/** * @brief Configure the USART Device * @param None * @retval None */ static void USART_Config(void) { USART_InitTypeDef USART_InitStructure; GPIO_InitTypeDef GPIO_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; /* Enable GPIO clock */ RCC_AHBPeriphClockCmd(EVAL_COM1_TX_GPIO_CLK , ENABLE); RCC_AHBPeriphClockCmd(EVAL_COM1_RX_GPIO_CLK , ENABLE); /* Enable USARTx APB clock */ #ifdef USE_STM320518_EVAL RCC_APB2PeriphClockCmd(EVAL_COM1_CLK, ENABLE); #else RCC_APB1PeriphClockCmd(EVAL_COM1_CLK, ENABLE); #endif /* USE_STM320518_EVAL */ /* Configure the HSI as USART clock */ #ifdef USE_STM320518_EVAL RCC_USARTCLKConfig(RCC_USART1CLK_HSI); #else RCC_USARTCLKConfig(RCC_USART2CLK_HSI); #endif /* USE_STM320518_EVAL */ /* USARTx Pins configuration **************************************************/ /* Connect pin to Periph */ GPIO_PinAFConfig(EVAL_COM1_TX_GPIO_PORT, EVAL_COM1_TX_SOURCE, EVAL_COM1_TX_AF); GPIO_PinAFConfig(EVAL_COM1_RX_GPIO_PORT, EVAL_COM1_RX_SOURCE, EVAL_COM1_RX_AF); /* Configure pins as AF pushpull */ GPIO_InitStructure.GPIO_Pin = EVAL_COM1_TX_PIN | EVAL_COM1_RX_PIN; 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(EVAL_COM1_RX_GPIO_PORT, &GPIO_InitStructure); GPIO_Init(EVAL_COM1_TX_GPIO_PORT, &GPIO_InitStructure); /* USARTx configured as follow: - BaudRate = 115200 baud - Word Length = 8 Bits - Stop Bit = 1 Stop Bit - Parity = No Parity - Hardware flow control disabled (RTS and CTS signals) - Receive and transmit enabled */ USART_DeInit(EVAL_COM1); USART_InitStructure.USART_BaudRate = 115200; 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(EVAL_COM1, &USART_InitStructure); /* USART2 IRQ Channel configuration */ #ifdef USE_STM320518_EVAL NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; #else NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn; #endif /* USE_STM320518_EVAL */ NVIC_InitStructure.NVIC_IRQChannelPriority = 0x01; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); }
/** * @brief Configure the USART to Wake up from STOP mode by Start bit Method * @param None * @retval None */ static void USART_Config(void) { USART_InitTypeDef USART_InitStructure; GPIO_InitTypeDef GPIO_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; /* Enable GPIOD clock */ RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOC|RCC_AHBPeriph_GPIOE , ENABLE); /* Enable USART1 APB clock */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); /* Configure the HSI as USART clock */ RCC_USARTCLKConfig(RCC_USART1CLK_HSI); /* USART1 Pins configuration ***********************************************/ /* Connect pin to Periph */ GPIO_PinAFConfig(GPIOC, GPIO_PinSource4, GPIO_AF_7); GPIO_PinAFConfig(GPIOE, GPIO_PinSource1, GPIO_AF_7); /* Configure pins as AF pushpull */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4; 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(GPIOC, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1; GPIO_Init(GPIOE, &GPIO_InitStructure); /* USARTx configured as follow: - BaudRate = 115200 baud - Word Length = 8 Bits - Stop Bit = 1 Stop Bit - Parity = No Parity - Hardware flow control disabled (RTS and CTS signals) - Receive and transmit enabled */ USART_DeInit(USART1); USART_InitStructure.USART_BaudRate = 115200; 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(USART1, &USART_InitStructure); /* USART2 IRQ Channel configuration */ NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x01; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x00; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); /* Configure the wake up Method = Start bit */ USART_StopModeWakeUpSourceConfig(USART1, USART_WakeUpSource_StartBit); /* Enable USART1 */ USART_Cmd(USART1, ENABLE); /* Before entering the USART in STOP mode the REACK flag must be checked to ensure the USART RX is ready */ while(USART_GetFlagStatus(USART1, USART_FLAG_REACK) == RESET) {} /* Enable USART STOP mode by setting the UESM bit in the CR1 register.*/ USART_STOPModeCmd(USART1, ENABLE); /* Enable the wake up from stop Interrupt */ USART_ITConfig(USART1, USART_IT_WU, ENABLE); /* Enter USART in STOP mode with regulator in low power mode */ PWR_EnterSTOPMode(PWR_Regulator_LowPower, PWR_STOPEntry_WFI); }
void UART_Iinitialization(void) { //GPIO GPIO_InitTypeDef GPIO_InitStructure; //Interrupt NVIC_InitTypeDef NVIC_InitStructure; //UART USART_InitTypeDef USART_InitStructure; /* Enable USART1 APB clock */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); /* Configure the HSI as USART clock */ RCC_USARTCLKConfig(RCC_USART1CLK_HSI); /* Connect pin to Periph */ GPIO_PinAFConfig(GPIOB, GPIO_PinSource6, GPIO_AF_0); GPIO_PinAFConfig(GPIOB, GPIO_PinSource7, GPIO_AF_0); /* Configure pins as AF pushpull */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6; 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(GPIOB, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin=GPIO_Pin_7; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_Init(GPIOB, &GPIO_InitStructure); /* USARTx configured as follow: - BaudRate = 9600 baud - Word Length = 8 Bits - Stop Bit = 1 Stop Bit - Parity = No Parity - Hardware flow control disabled (RTS and CTS signals) - Receive and transmit enabled */ USART_DeInit(USART1); USART_InitStructure.USART_BaudRate = 9600; 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_StructInit(&USART_InitStructure); USART_Init(USART1, &USART_InitStructure); /* USART1 IRQ Channel configuration */ NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; NVIC_InitStructure.NVIC_IRQChannelPriority = 3; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); // USART_ITConfig(USART1, USART_IT_TC, ENABLE); USART_ClearFlag(USART1, USART_IT_RXNE); USART_Cmd(USART1, ENABLE); }