AnsweredAssumed Answered

STM32F4: Issues with USART1 Configuration

Question asked by tyrian on Oct 29, 2015
Latest reply on Nov 2, 2015 by tyrian
I'm currently experimenting with writing a driver for USART1 at the register level for the STM32F4 Discovery board.  I've followed the initialization procedure on page 963 of RM0090; however, when I connect to my PC via my FTDI cable, all I get are junk characters.  The LED blinks normally though, so I know I'm not blocking on the while loop that checks the TXE bit.  I've also checked my HSE_VALUE, which is 8000000.  I'm completely at a loss as to what I missed.  How can I get this code running?

My source code:
#include "stm32f4xx.h"
 
int main(int argc, char* argv[]){
    // At this stage the system clock should have already been configured
    // at high speed.
 
    // Clock configuration
    RCC->AHB1ENR |= RCC_AHB1ENR_GPIOBEN;    // Enable GPIOB clock
    RCC->APB2ENR |= RCC_APB2ENR_USART1EN;   // Enable USART1 clock
 
    // GPIO configuration
    GPIOB->MODER |= GPIO_MODER_MODER6_1;    // Enable GPIOB alternate function on pin 6
    GPIOB->MODER |= GPIO_MODER_MODER7_1;    // Enable GPIOB alternate function on pin 7
    GPIOB->AFR[0] |= 0x07000000;            // Select USART1 alternate function (Find this mask)
    GPIOB->AFR[0] |= 0x70000000;            // Select USART1 alternate function (Find this mask)
    GPIOB->OTYPER = 0;                      // Set GPIOA to push-pull
 
    // USART configuration
    USART1->CR1 |= USART_CR1_UE;            // Enable USART1
    USART1->CR1 &= ~USART_CR1_M;            // Configure M bit for word length (8b)
    USART1->CR2 &= ~USART_CR2_STOP;         // Configure number of stop bits (1b)
    USART1->CR3 &= ~USART_CR3_DMAT;         // Configure DMA (disabled)
    USART1->BRR |= HSE_VALUE / 9600;        // Set baud rate
    USART1->CR1 |= USART_CR1_TE;            // Set transmit enable
 
    // Blinky LED configuration
    RCC->AHB1ENR |= RCC_AHB1ENR_GPIODEN;    // Enable GPIOD clock
    GPIOD->MODER |= GPIO_MODER_MODER12_0;   // Enable GPIO output on port D, pin 12
 
    for(int i = 0; i < 10000000; i++){
        asm("nop");
    }
 
    // Infinite loop
    while (1){
        // Add your code here.
        // Send some data!
        while(!(USART1->SR & USART_SR_TXE));
        USART1->DR = 'B';                   // Send 'B'
        GPIOD->ODR ^= GPIO_ODR_ODR_12;      // Blink LED
        for(int i = 0; i < 10000000; i++){
            asm("nop");
        }
    }
}

Outcomes