cancel
Showing results for 
Search instead for 
Did you mean: 

InitStructure Significantly Changes USART Output

i_
Associate II
Posted on March 05, 2014 at 23:37

Hi all,

I'm hoping this is an easy one and just something I am missing. A google didn't return anything obviously relating to my problem.

I have some messy code that sets up my USART peripheral correctly. I have used    

GPIO_InitTypeDef GPIO_InitStructure;

USART_InitTypeDef USART_InitStructure;

...cast as global structures at the top of the script. Main then uses these to setup the usart and begin transmitting.

This works correctly. However, as my code grows I now have the need to put GPIO init in to it's own function. I noticed when doing this that having the USART_InitTypeDef USART_InitStructure; structure declared locally to the function the usart output did 'funny' things (not expected output). GPIO Init structure location seems to have no affect.

Please advise!

Thanks as always guys,

Ian

11 REPLIES 11
i_
Associate II
Posted on March 05, 2014 at 23:52

Perhaps also worthy of note, I get some very weird working to non-working behavior between flashes with the structure declared locally.

(essentially the problem is fixed, I just want to know why!)

Posted on March 06, 2014 at 00:13

auto/local variables allocated from the stack will contain random junk, they must be completely initialized, global statics are normally zeroed.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
i_
Associate II
Posted on March 06, 2014 at 00:16

Thank you clive as always.

What is best practice here? Declare all structures as static...or 0 them when declared? I haven't seen this practice in many online examples?

psandesh007
Associate II
Posted on March 06, 2014 at 06:28

Im having one doubt, i think it would helps you.

If you are using array to send and receive usart data.

What is the array length you are giving and check whether array overflow was occurring.

It is the most thing causes random data to occur.
Posted on March 06, 2014 at 07:15

You could do

    USART_InitTypeDef USART_InitStructure = { 0 };

or

   USART_StructInit(&USART_InitStructure);

or

simply fully qualifying the fields of the structure when initializing it.
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
i_
Associate II
Posted on March 06, 2014 at 09:11

Apologies for the 'multi reply'. Mobile didn't seem to work so well!

How come the example code generally gets away with this? (from most of the examples I find of other peoples code online)
Amel NASRI
ST Employee
Posted on March 06, 2014 at 10:28

Unfortunately, there are known issues when submitting from tablets or mobiles.

I removed repeated answers.

-Mayla-

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

Posted on March 06, 2014 at 14:32

How come the example code generally gets away with this? (from most of the examples I find of other peoples code online)

Well usually by initializing enough of the fields that it works properly. Hard to say why in your experience didn't work, but I haven't seen the code.
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
jpeacock2399
Associate II
Posted on March 06, 2014 at 15:47

>> How come the example code generally gets away with this? (from most of the examples I find of other peoples code online)

There's a lot of hidden work done by the linker and runtime startup.  Variables are put in a section of memory by the compiler/linker that is zeroed when the runtime is initialized (the code that runs before you get to main() routine).

  Jack Peacock