AnsweredAssumed Answered

First steps with STM bare metal, UART not working [STM32VLDiscovery]

Question asked by Tomas.Jesus on Feb 25, 2016
Latest reply on Feb 29, 2016 by AvaTar

First of all, let me apologize in advance for the mistakes in my grammar. Not an english speaker here. I hope, howewer, that this can be understood.

I have recently started to move away from PIC microcontrollers (Mainly PIC18F family) to ARM microcontrollers. I chosed STM32 because of the inexpensive Discovery Boards, wich I could use before doing a board for the project I need to do. I saw that some libraries could be used, but back in the day when I was first learning about microcontrollers, teacher never allowed us to use anything complex, since it "obscured" what happened in the microcontroller. Those were good times, learning in ASM with small microcontrollers... Anyway, now, some ten years after that, I still like to do things "as close to the action" as I can. So, I found this tutorial for setting up GNU GCC in Keil [http://m8051.blogspot.com.es/2012/10/using-gcc-in-keil-best-of-both-worlds.html] and made it work with some minor changes (Only in preprocessor directives I had to change anything, I think, and there is not much difference between Keil 4 and 5 versions). 

I had some problems but as of now, I have GPIOs working, and SysTick happily generating ticks every ten ms. Now I am trying to set up one of the UARTs, since withthat done I find easy to develop the rest (Having the possibility to send messages to the PC makes easier to follow the program flow, specially since I am not confortable enough with the debuggin way...). Seems that I can't send anything (Not reading even garbage from the PC), and I have not a oscilloscope right now with me, so, if any person more experienced with STM32 mcus could take a look at this and enlighten me, that would be great.

The code:
USART configuration

void ConfigUSART2(void)
{
     
    // Pins: UART2_TX is PA2, UART2_RX is PA3
    // So PA2 ==> "alternate function push-pull"
    //   PA3 ==> "Input floating"
    GPIOA->CRL &= 0xFFFF00FF; // Both nibbles to zero
    GPIOA->CRL |= 0x00004200; // and now to the correct values
     
    RCC->APB1ENR |= RCC_APB2ENR_AFIOEN; // AFIO Clock
    RCC->APB1ENR |= RCC_APB1ENR_USART2EN;   //USART2 Clock
    //Following the steps described in page 582 of RM0041:
    USART2->CR1 |= USART_CR1_UE; // Step 1: Enable USART
    //Step 2: M value, I want 8 bits so no need to touch it
    //Step 3: Stop bits, again default value is ok
    //Step 4: Not using DMA, so...
    USART2->BRR = 0xC9; //Step 5: Baudrate --> Baudrate=Fclk/(15*USARTDIV) --> With 24MHz, 115200 USARTDIV=12,9
    USART2->CR1 |= USART_CR1_TE; //Step 6: IDLE
    USART2->CR1 |= USART_CR1_RE; //I enable also reception
}
Main
int main()
{
    unsigned long i;
      uint8_t transmit, receive, data;
         
        Inicializar_GPIOs(); // This congigures GPIOs for leds and pushbutton, working OK
        Inicializar_SysTick(); // Again, working OK, this configures SysTick only
        ConfigUSART2();
     
        transmit=0;
      receive=0;
    while(1)
    {
            if(GPIOA->IDR & 1) // Button pressed
            {
                // PC9 led Toggle
                GPIOC->ODR ^= GPIO_ODR_ODR9;
                //Dummy loop
        for(i=0;i<0x1FFFFF;i++);   
                 
            }
            if (transmit<10)
            {
                data = 0x48;
                data+=transmit;
                //while(USART2->SR&USART_SR_TXE);// Wait for idle
                USART2->DR = dato;
                transmitido++;
                GPIOC->ODR ^= GPIO_ODR_ODR9;
                for(i=0;i<0x1FFFFF;i++);
                for(i=0;i<0x1FFFFF;i++);
            }
            if (USART2->SR&USART_SR_RXNE) //Data ready to be read
            {
                receive=USART2->DR;
                if (receive==data)
                {
                    GPIOC->ODR |= GPIO_ODR_ODR9; //I leave Led always on and wait
                    while(1);
                }
            }
    }
}

I have translated variables names and comments to english,I hope there is no mistakes... Thanks in advance! And also, do you know any good tutorial/book for programming this mcus bare metal? 

Outcomes