用UART寫了一段Bootloader代碼,遇到了一個很奇怪的現象。
代碼如下:簡單介紹一下就是先統一配置MCU的IO端口,然后配置串口參數,然后循環發送‘0’和'\r’。16進制是0x30 0x0d
int main(void)
{
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
RCC_HSE_Configuration();//外部高速時鍾初始化
SysTick_Init();
IO_Init();//初始化板子端口
Uart_Init(115200);//初始化串口
while (1)/* Infinite loop */
{
UART_SendByte( '0');
UART_SendByte('\r');
Delay(1000);
}
}
void IO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
// 配置 USART1 Tx (PA.09) 作為功能引腳並上拉輸出模式
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_ResetBits(GPIOA, GPIO_Pin_9);
//配置 USART1 Tx (PA.10) 作為功能引腳並是浮空輸入模式
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
void Uart_Init(uint32_t Baud)
{
USART_InitTypeDef USART_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
USART_InitStructure.USART_BaudRate = Baud;
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);
// USART_ClearFlag(USART1, USART_FLAG_TC);
USART_Cmd(USART1,ENABLE);
}
結果下載完程序,運行后發送出來的第一字節是0x3f 然后才正常的0x30 0x0d,感覺很奇怪,花時間去測試。
嘗試一:把串口的兩個端口配置放在串口配置函數里,如下
void Uart_Init(uint32_t Baud)
{
USART_InitTypeDef USART_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
// 配置 USART1 Tx (PA.09) 作為功能引腳並上拉輸出模式
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
//配置 USART1 Tx (PA.10) 作為功能引腳並是浮空輸入模式
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
USART_InitStructure.USART_BaudRate = Baud;
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);
// USART_ClearFlag(USART1, USART_FLAG_TC);
USART_Cmd(USART1,ENABLE);
}
這樣就正常了,上電就收到0x30 0x0d
嘗試二:這樣修改,把串口外設時鍾在端口配置函數里面就打開。
void IO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);//這里提前打開串口外設時鍾
// 配置 USART1 Tx (PA.09) 作為功能引腳並上拉輸出模式
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_ResetBits(GPIOA, GPIO_Pin_9);
//配置 USART1 Tx (PA.10) 作為功能引腳並是浮空輸入模式
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
也是可以正常輸出0x30 0x0d
總結:stm32的外設時鍾應該在此外設端口配置之前打開。
