cancel
Showing results for 
Search instead for 
Did you mean: 

USART1 Doesn't transmit

mblack
Associate II
Posted on September 28, 2015 at 21:10

I have been trying to get my code to work but I cannot get any output on PA9 (USART1_Tx) on my STM32f0discovery board. Any help would be greatly appreciated.

CODE ==========================================================

void InitUART() {

    

    // RCC Setup

    

    RCC->CFGR = 0;

    RCC->CFGR3 &= 0xFFFFFFFC;

    RCC->APB2ENR |= (1<<14);

    RCC->AHBENR |= (1<<17);

    

    // GPIO Setup

    

    GPIOA->MODER &= 0xFFC3FFFF; GPIOA->MODER |= 0x00280000L;     

    GPIOA->OSPEEDR = 0x003c0000;     

    GPIOA->OTYPER = 0;

    GPIOA->PUPDR &= 0xFFC3FFFF; GPIOA->PUPDR|=0x00140000;   // Pull up on both lines

    

    // USART Setup

    

    USART1->CR2 = 0;

    USART1->CR3 = 0;

    USART1->BRR = 69;         // 8MHz clock, 115200 rate

    USART1->GTPR = 1;

    USART1->RTOR = 0;

    USART1->RQR = 0;

    

    USART1->CR1 = USART_CR1_RE | USART_CR1_TE | USART_CR1_UE;

    

    }

void SendByte(unsigned char b) {

    

    while ((USART1->ISR & USART_ISR_TXE) == 0);

    USART1->TDR=b;

    while ((USART1->ISR & USART_ISR_TXE) == 0);

    }

int main() {

    InitUART();

    

    

    for(;;) {

        SendByte(0xF0);

        SendByte(0x55);

        for(i=0;i<50;i++) ;

        }

    }

==============================================================

#usart-stm32f051
4 REPLIES 4
Posted on September 29, 2015 at 00:35

The joys of register level coding, tried stepping through with a debugger?

You might want to switch the AHBENR writing around so you avoid the hazard of touching GPIOA immediately after you've enabled the clock rather than dwelling a few cycles.

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
mblack
Associate II
Posted on September 29, 2015 at 15:51

I stepped through slowly with the debugger (Keil) and there is no change. Both the Tx and Rx lines remain high permanently without any serial data.

Posted on September 29, 2015 at 17:40

And does the Peripheral view within Keil lead you to believe that the registers are correctly configured? And the other clocks are all in order? Do you need to configure the GPIO AF mux to USART1?

I'd wade into the register level settings, but I've better things to do. I've posted SPL examples for the F0 that should work.

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
mblack
Associate II
Posted on September 29, 2015 at 19:11

That was it! I omitted to specify the alternate function number:

    GPIOA->AFR[1] &= 0xFFFFF00F; GPIOA->AFR[1] |= 0x00000110;       

Works fine now, thank you for pointing it out.