STM32 trying to get uasart to transmit
Hello, I wanted to upgrade my simple blinky program with the usart 2, so that it sends a simple 'B' over the serial port to my PC. I am using a Nucleo-F334R8. In the datasheet it says usart2 is connected with the USB port.
Here is my code, that is not working because I do not receive anything on the pc side with terra term. The com port is there, by the way,
int main(void)
{
// Enable Port A
SET_BIT(RCC->AHBENR, RCC_AHBENR_GPIOAEN);
// PA5 = LED output
MODIFY_REG(GPIOA->MODER, GPIO_MODER_MODER5, GPIO_MODER_MODER5_0);
// PA2 = Tx of UART 2, set to alternative function
MODIFY_REG(GPIOA->MODER, GPIO_MODER_MODER2, GPIO_MODER_MODER2_1);
//Enable USART2 clock
SET_BIT(RCC->APB1ENR, RCC_APB1ENR_USART2EN);
// Enable TX
SET_BIT(USART2->CR1, USART_CR1_TE);
// Use 1 start bit, 8 data bits
CLEAR_BIT(USART2->CR1, USART_CR1_M1);
CLEAR_BIT(USART2->CR1, USART_CR1_M0);
// Set Baudrate to 9600 bit/s
WRITE_REG(USART2->BRR, 0x341);
// Use 1 stop bit
CLEAR_BIT(USART2->CR2, USART_CR2_STOP_0);
CLEAR_BIT(USART2->CR2, USART_CR2_STOP_1);
// Enable the uart
SET_BIT(USART2->CR1, USART_CR1_UE);
// Send an idle frame at beginning
SET_BIT(USART2->CR1, USART_CR1_TE);
while(1)
{
// LED Pin -> High
SET_BIT(GPIOA->BSRRL, GPIO_BSRR_BS_5);
// Transmitt 'B' with uart
WRITE_REG(USART2->TDR, 'B');
// Wait for data to be moved from the TDR register to the shift register.
// So new data can be put into the TDR register.
while(!(READ_BIT(USART2->ISR, USART_ISR_TXE)));
delay(50);
// LED Pin -> Low
SET_BIT(GPIOA->BSRRH, GPIO_BSRR_BS_5);
delay(50);
}
}
Any help is welcomed, thanks :)