cancel
Showing results for 
Search instead for 
Did you mean: 

General USART2 issues on STM32F3

Kevin Dominic Merkel
Associate II
Posted on May 27, 2017 at 00:08

Hello everyone, 

I started programming for one of my student projects but I am pretty hung up on the USART2 configuring. I have worked with Arduinos before, but I must admit that STM32 is a complete different level

🙂

 

I am using the STM32F303VCT6 on the DiscoveryBoard with the STLink2 included. I am trying to figure out this USART stuff but I can not make much sense of all the data sheets. I am a aerospace engineer with some C programming experience, but working with those cryptic datasheets is completely new to me. 

I really hope some of you guys have some patience and motivation to help me out with my project. Basically I need to connect my PC with the STM (USART2) and the STM with a Camera(UART1). The STM32 should send some hex characters to the cam so it makes a picture and after that receive the pic and send it to the pc. 

I am stuck at the beginning and need to activate the USART2 connection. I have connected an FTDI to USART2 but and try to send characters with hterm to start an interrupt, but nothing happens. Obviously it is my code, but I do not know whats wrong. I guess it is more than one mistake

🙂

 At first I just want to send a character from PC and turn an LED on if the character is received with the interrupt. The code is here: 

I uploaded the code here:

https://codepaste.net/i9psat

 

If more Information is needed don't hesitate to ask. 

Thanks in advance, 

Kevin Dominic

12 REPLIES 12
Posted on May 28, 2017 at 17:27

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; // This is separate from mux setting itself

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Kevin Dominic Merkel
Associate II
Posted on May 28, 2017 at 21:01

Hi Clive One, 

thanks a lot for your reply. I made the USART2 work, but I am not completely satisfied with it yet. Here is the current code:

https://codepaste.net/onnfs8

 

Now to the problem: 

I always send hex characters with baud 9600 per hterm. If I send for example 0xFF i get 0xFF back (once). But if I try to send something like 0xFF 0xEE I just get the 0xFF back, continuously until I set the reset button. Somehow I get stuck in the serial_prints function... 

Any suggestions what could be wrong? Or maybe a link to a webpage describing these things?

Thanks a lot, 

Kevin

Posted on May 28, 2017 at 21:33

 ,

 ,

Front test TXE, and don't send strings from the IRQ which will take multiple byte times.

while (USART_GetFlagStatus(USARTx, USART_FLAG_TXE) == RESET), // Check it is EMPTY **FIRST**

USART_SendData(USARTx, *buffer++),

 ,

 , ,  , , , ,

Some of the code was originally lifted from here

 ,
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Kevin Dominic Merkel
Associate II
Posted on May 29, 2017 at 07:51

Hi,

thanks for the tip and the link. I went through the code and adjusted it for my purpose, but I am still stuck with the sending two characters at once, it still gets hung. 

But I think I figured out the problem. First of all the necessary Code snippets:

https://codepaste.net/qrim9e

 

Somehow, the message does not get build up in the rx_buffer. It always jumps to the if-loop where I set the line_valid=1 to send the message after one received character. Now I tried to put the argument of the if sentence to something which can never be true, but we still go after each turn into the if loop. Do you have any clue why? I have no clue what is happening here.

Thanks 

Kevin

Posted on May 29, 2017 at 16:54

My original code was designed to deal with more than 2 characters, I'm not going to wade into how it might behave outside the original design parameters.

Try something simple to confirm you can echo data on one USART to the other.

void main()

{        //Init -----------------------------------------*/

        init_GPIO();

        init_USART1(9600);

        init_USART2(9600);

        while(1);

}

void USART2_IRQHandler(void)

{

    if (USART_GetITStatus(USART2, USART_IT_RXNE) != RESET) // Received character?

    {

        uint16_t rx = USART_ReceiveData(USART2);

        while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);

        USART_SendData(USART1, rx);

    }

}
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Kevin Dominic Merkel
Associate II
Posted on June 01, 2017 at 12:05

Hi,

I figured out the transmitting of more than one character now. Thanks a lot for your help. 

The problem is now, that the receiving is a bit of. I mean some characters are sometimes received double and others not at all. Do I have to set a special Timer setup for USART? Right now I just use Baudrate 9600 on both sides. 

Thanks, Kevin

Posted on June 01, 2017 at 12:34

How do you receive the UART data, i.e. what is your code ?

I mean some characters are sometimes received double and others not at all.

There is a OV (overflow) flag in the UART status regs. You can configure an interrupt on errors like OV as well.

However, that would just be the symptom.

The issue is, I presume, you are either polling, or processing of received bytes (or other interrupts / peripherals) take too long.

With 9600bps, there should be plenty of time ...

The UART/USART section of the Reference Manual is a highly suggested read. You don't need to digest everything, but a general understanding is always helpful.

Kevin Dominic Merkel
Associate II
Posted on June 05, 2017 at 15:26

Thank you so much for all the help so far

🙂

I have been able to receive and send characters with the USART connection, even multiple times at once. 

Here is the current code: 

https://codepaste.net/r9ymtk

 

The critical part now is this piece:

if (rx_usart2_buffer[0] == 0x4F && rx_usart2_buffer[1] == 0x4E)

{

USART1->TDR = 0x56;

USART1->TDR = 0x00;

USART1->TDR = 0x36;

USART1->TDR = 0x01;

USART1->TDR = 0x00;

}

I receive an command from my terminal with usart2, This command, here 0x4F 0x4E, is confirmed and after a a code of hex numbers should be send to the connected camera at usart1. What I expect is an interrupt on usart1 which sends me the answer from the camera back. So far I am not getting an interrupt back. 

I confirmed through debugging that the code is going through the above printed lines. Somehow, just the interrupt from the answer is not comeing. 

Right now, the config for usart1 shows this: 

USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); // enable the USART1 receive interrupt

I tried to add also this:

USART_ITConfig(USART1, USART_IT_TXE, ENABLE); 

But this messes up the whole code and nothing works....

I connected the FTDI Adapter directely to usart1 instead of usart2 as test and everything worked. So I think it has something to do with writing into the TDR register.

Anyone any experience on this topic?

Thanks again, Kevin

Posted on June 05, 2017 at 16:31

You must make sure TXE is flagging before stuffing data into the TDR, there isn't a FIFO, it is one character deep.

When you enable the TXE interrupt it expects you to feed TDR with a character at each entry to clear the TXE state, if you fail to do that the IRQ Handler will just keep re-entering and no foreground code execution will occur.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..