Monday, January 24, 2011

STM32 Discovery: The Basics - Echo the serial port (USART)

This is a continuing of my first post.  In this post I will build on what we had in the first one and get the ARM STM32 to echo what we send over the terminal with serial.

The first step is to configure the usart.  Open STM32_Init.c with the configuration wizard.  I will write on how to do it in C when I cover getting started with GCC.  Enable your USART1 and set the parameters.  Also enable USAT1 interrupts on RXNE (receive) that way we don't need to poll the data and we will be notified when a byte has arrived.

Once every thing is configured to your liking, hit save.  Now we will be able to use the structure called USART1.  Here are a few notes on members that are used allot.
USART1->SR   this contains information about the interrupt that was generated
USART1->DR  this contains the byte that we received or that will will write
if you assign USART1->DR a value, it will send that value out the usart.  If you get the value form USART1->DR, if will read the value received from the usart.

Next, open you need to open main.c.  In this file we will add the function definition for void USART1_IRQHandler (void).  This function was declared STM32F10x.s and was all ready mapped to an interrupt vector. So all we need to do is enable the interrupt (which we all ready did in the configuration wizard) and override it.
My function will look a little bit like this.

void USART1_IRQHandler (void)
{
    volatile unsigned int vsr;
    int ByteSent;
    vsr= USART1->SR;
    if (vsr& USART_FLAG_RXNE)                   // did we interrupt on the read
    {
       // clear the interrupt since we are processing it
        USART1->SR &= ~(USART_FLAG_RXNE);  
        ByteSent = (USART1->DR & 0x1FF);
        SendByte(ByteSent);
     }
}

We will of course need to define SendByte.  The prototype will be void SendByte(int byte);
My definition will look something like this.
void SendByte (int byte) 
{
    //Wait for the uart to finish sending the byte.
     while (!(USART1->SR & USART_FLAG_TXE));
    USART1->DR = (byte & 0xFF);
}

Now you simply need to connect PA10 to RX and PA9 to TX on your hardware.  I used the FTDI chip on my arduino to accomplish this.  I simply connected PA10 to RX and PA9 to TX of my arduino and took the avr out.  After that what ever you send over to the terminal will echo back to you.  If you need a terminal program, I used Termite

You can download the project form my Code Project article

4 comments:

  1. PA9 to RX
    PA10 to TX

    ReplyDelete
  2. You would connect PA9 to RX and PA10 to TX if you wanted to communicate with the arduino. I am simply using the FTDI chip on my arduino so i need to connect PA9 to TX and PA10 to RX.

    ReplyDelete
  3. I get error "Undefined symbol SystemInit (referred from startup_stm32f10x_md_vl.o)." What does it mean?

    ReplyDelete
  4. can you email me your project? It seems like you are missing some start up code. where you create the project and it asked you if you want to copy the start up code did you click yes?

    ReplyDelete