2020-04-01 06:29 AM
Hi I am trying to write a echo program and the USART1 configuration code is shown below.
RCC->AHB2ENR |= RCC_AHB2ENR_GPIOAEN; //Enable GPIOA Clock
RCC->APB2ENR |= RCC_APB2ENR_USART1EN; //Enable USART1 Clock
GPIOA->AFR[1] |= (0b0111 << GPIO_AFRH_AFSEL9_Pos) | (0b0111 << GPIO_AFRH_AFSEL10_Pos); //Set PA9 and PA10 as USART1
GPIOA->OSPEEDR |= (GPIO_OSPEEDR_OSPEED9 | GPIO_OSPEEDER_OSPEEDR10); //Set GPIO Speed High
GPIOA->PUPDR &= ~(GPIO_PUPDR_PUPD9 | GPIO_PUPDR_PUPD10); //Set No Pull Up/Down
GPIOA->MODER = (GPIOA->MODER | GPIO_MODER_MODER9_1 | GPIO_MODER_MODER10_1) & ~(GPIO_MODER_MODER9_0 | GPIO_MODER_MODER10_0); //Set PA9/PA10 as Alternate Function
USART1->CR1 &= ~(USART_CR1_UE); //Disable Interrupt (Just to be sure)
USART1->PRESC = 0b0101; //USART Clock Divided by 10
USART1->BRR = 0x0012;
USART1->CR1 |= USART_CR1_TE | USART_CR1_RE | USART_CR1_RXNEIE; //Enable Transmit -- Enable Receive -- Enable Receiver Buffer Not empyt Interrupt
USART1->CR1 |= USART_CR1_UE; //Enable USART
NVIC_SetPriority(USART1_IRQn,0); //Set Nested Vector Interrupt Priority
NVIC_EnableIRQ(USART1_IRQn); //Enable Interrupt
Everything works fine with baudrate equal to 9600 however when I set my baudrate to 921600 or anything greater than 9600 I get strange output as shown below. The small pulses has width of approixmately 9.605us.
Has anyone encoutered this issue?
2020-04-01 06:40 AM
That's a valid UART signal if you're sending a bunch of 1s. 1/9.605us is about 104000 baud.
What is your system clock? Keep in mind BRR needs to be at least 16.
2020-04-01 07:15 AM
I sent "s" which is 01110011 in binary. My system clock is 170MHz UART is fed from pclk2 which is also 170MHz I got prescaler of 10 which results in 17MHz.
17MHz/921600 = 18decimal = 12hex