cancel
Showing results for 
Search instead for 
Did you mean: 

UART example code for STM32F0

hospodar
Associate III
Posted on November 20, 2012 at 23:34

Hello,

I looking for some example code for communication between my ST-DISCOVERY F0 board and PC (UART). Can someone help me with it? I´m novice.

Thank you
60 REPLIES 60
Posted on June 12, 2013 at 02:05

Yes, you'll want to use a ring/fifo buffer, and service that with an RXNE interrupt. You'll want to turn off the interrupt when there is not additional data to send.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
kasun_duminda92
Associate III
Posted on June 19, 2013 at 19:44

I am receiving and error while calling sscanf on the 'line_buffer' variable.

main.c(168): error:  #167: argument of type ''volatile char *'' is incompatible with parameter of type ''const char *restrict''

My code within the while loop;

    __WFI(); // Wait for an interrupt rather than grind endlessly

    if (line_valid) // A new line has arrived

    {

            // ProcessLine(line_buffer); // Do something with the line

            char str[20];

            int num;

            sscanf(line_buffer, ''%s %d'', str, &num);

    ......................

What do you think is the error here?
Posted on June 19, 2013 at 23:27

What do you think is the error here?

Sounds like a casting issue. The buffer can be changed by an interrupt, and is thus transient. You can apply a cast, or copy it someplace else.

A more complex implementation would require multiple buffers, and enqueuing them
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
kasun_duminda92
Associate III
Posted on June 20, 2013 at 05:09

Thanks!

hospodar
Associate III
Posted on September 17, 2013 at 22:33

In order to tx interrupt I wrote this function, which is placing characters from printf to FIFO buffer. It is calling fromfputc(int ch, FILE *f) function and works fine.

void
FIFO_PUT(
char
ch)
{
if
(COUNT < FIFO_SIZE)
{
FIFO[COUNT] = ch;
COUNT ++;
}
}

The next function I wrote is this one. It should be sending a characters from FIFO buffer to USART. It is calling from interrupt handler.

void
FIFO_GET(
void
)
{
char
j;
char
ch;
for
(j=0;j<10; j++) 
{
ch = FIFO[j];
USART_SendData(USART1, ch);
ch = 0;
}
}

In result, program is don't working. Characters from printf are correct placing to the FIFO but to USART is don't sending this characters. Any idea?
Posted on September 17, 2013 at 23:16

Any idea?

USART_SendData() can only be used once per TXE interrupt, there's basically a single holding register so you can't stuff multiple bytes in there.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
hospodar
Associate III
Posted on October 06, 2013 at 23:24

Ok, so I replace this piece of code in interrupt handler with one from USART interrupt example for STM32F.. But it still don´t send any data to serial terminal.

if (USART_GetITStatus(USART
1
, USART_IT_TXE) != RESET)
{
USART_SendData(USART
1
, FIFO[tx_index++]);
if(tx_index == FIFO_SIZE)
{
/* Disable the USARTx Transmit interrupt */
USART_ITConfig(USART
1
, USART_IT_TXE, DISABLE);
} 
}

bruno
Associate
Posted on October 15, 2013 at 19:05

Thanks Ivan and Clive1. Your discussion helped me a lot to make a project running with Uart in interrupt mode with I/O buffers.

I think the only thing missing to Ivan to get his application running (perhaps it's running today), was to manage the stop and go of the TXE interrupt (thrown when the TX buffer is empty).

In the TXE interrupt when the buffer is empty, I  have disable the TXE interrupt as soon as the transmit buffer is empty (in the interrupt routine). Then, as soon as a new char arrives in the buffer (fputc), I enable again the interrupt if it is in disable mode. It's working fine...

Thanks again to both of you for you pieces of code.

Bruno.

daniel2
Associate II
Posted on February 11, 2014 at 16:16

With this code:

if (USART_GetITStatus(USART1, USART_IT_TXE) != RESET) // Transmit the string in a loop
{
USART_SendData(USART1, StringLoop[tx_index++]);
if (tx_index >= (sizeof(StringLoop) - 1))
tx_index = 0;
}

The UART interrupt keeps triggering endlessly unless I do something like else { disable interrupt}. Is there a better way to get it to stop triggering?
daniel2
Associate II
Posted on February 11, 2014 at 16:16

With this code:

if (USART_GetITStatus(USART1, USART_IT_TXE) != RESET) // Transmit the string in a loop
{
USART_SendData(USART1, StringLoop[tx_index++]);
if (tx_index >= (sizeof(StringLoop) - 1))
tx_index = 0;
}

The UART interrupt keeps triggering endlessly unless I do something like else { disable interrupt}. Is there a better way to get it to stop triggering?