2020-05-26 03:02 PM
Hi there.
I have been trying to configure this USART6 on my STM32F401RE board as 9600 and Async.
But I just can't seem to figure out what I am doing wrong? I am not using the HAL for this project.
I really just want to get an example of how to use the UART6 on PortA with this MCU.
Thanks
#define STM32F401xx
#include "stm32f4xx.h"
void USART_write(int ch);
void delayMs(int delay);
void USART6_Init();
int main(void)
{
USART6_Init();
GPIOA->MODER |= 0x400;
//GPIOA->ODR |= 0x20;
while(1){
USART_write("H");
USART_write("E");
USART_write("L");
USART_write("L");
USART_write("O");
delayMs(10);
GPIOA->ODR ^= 0x20;
}
}
void USART6_Init(){
//RCC->APB2RSTR |= (1 << 5);
//delayMs(1);
//RCC->APB2RSTR |= (0 << 5);
RCC->AHB1ENR |= 1;
RCC->APB2ENR |= (1 << 5);
GPIOA->AFR[0] |= (1<<15);
GPIOA->MODER |= ( 1 << 23 );
GPIOA->MODER |= ( 1 << 25 );
USART6->BRR = 0x0683; //BAUDRATE 9600 @16Mhz
USART6->CR1 = 0x0008; //ENABLE Tx
USART6->CR1 |= 0x2000;
}
void USART_write(int ch){
//WHile the TX Buffer is empty
while(!(USART6->SR & 0x0080)){
USART6->DR = (ch & 0xFF);
}
}
void delayMs(int delay) {
int i;
for (; delay>0;delay--){
for (i = 0 ; i < 3195; i++);
}
}
2020-05-28 12:21 PM
Oh yea thats right but I did see that as well though.
Problem however is that when I restart my Terminal that captures incoming chars on the /dev/ttyACM1 port it swaps back to being messed up.
No idea how that even happens though.
2020-05-28 01:19 PM
void USART_write(int ch){
//WHile the TX Buffer is empty
while((USART6->SR & 0x0080)){
USART6->DR = (ch & 0xFF);
}
}
STOP putting the output to DR *INSIDE* the loop
You want to be waiting for the TXE to go high, AND THEN output to the DR