cancel
Showing results for 
Search instead for 
Did you mean: 

General questions on STM32 programming paradigms

hate_pod
Associate
Posted on November 12, 2011 at 21:10

I could not find any good document on internet about STM32 programming. Unfortunately, STM's own documents do not explain anything more than register functions. I will greatly appreciate if anyone could explain (or at lest give me some hints about) the following questions:

  1. I noticed that in all example programs that STM provides, local variables for main() are always defined outside of the main() function (with occasional use of static keyword). Is there any reason for that? Should I follow a similar practice? Should I avoid using local variables inside the main?

  2. I have a gloabal variable which is updated within the clock interrupt handle. I am using the same variable inside another function as a loop condition. Don't I need to access this variable using some form of atomic read operation? How can I know that a clock interrupt does not change its value in the middle of the function execution? Should I need to cancel clock interrupt everytime I need to use this variable inside a function? (However, this seems extremely ineffective to me as I use it as loop condition. I believe there should be better ways of doing it).

  3. Keil automatically inserts a startup code which is written in assembly (i.e. startup_stm32f4xx.s). This startup code has the following import statements: IMPORT SystemInit IMPORT __main I ''C'', it makes sense. However, in C++ both main and system_init have different names (e.g. _int_main__void). How can this startup code can still work in C++ even without using ''extern ''C'' '' (I tried and it worked). How can the c++ linker (armcc --cpp) can associate these statements with the correct functions?

#training-and-support
3 REPLIES 3
Posted on November 12, 2011 at 21:23

Your questions are general embedded development topics - they have nothing specifically to do with the STM32. Hence they are not addressed by the STM32 documentation.

For some general books, see:

http://www.keil.com/books/

 - they are not all specific to Keil.

There are plenty of organisations providing support & training for embedded development; eg,

http://www.doulos.co.uk/

http://www.feabhas.co.uk/

http://www.netrino.com/

http://www.ganssle.com/

http://www.esacademy.com/

Your Distributor(s), tool vendor(s), etc may also be able to offer support, and/or suggest local training & support providers...

Posted on November 12, 2011 at 21:25

Stupid forum insists on burying half the text as ''quoted message''

Here:

http://www.doulos.co.uk/

http://www.feabhas.co.uk/

http://www.netrino.com/

http://www.ganssle.com/

http://www.esacademy.com/

Your Distributor(s), tool vendor(s), etc may also be able to offer support, and/or suggest local training & support providers...

Posted on November 12, 2011 at 21:36

Stupid forum insists on bury half the text as ''quoted message''

Yes, it's a PITA, I use the HTML button to ''show codes''. That and inheriting whatever goofy font setting it wants from a previous post.

As to some of the original questions.

If you have some updating under interrupt, define it as volatile. Read it once into a local variable if you want to have the same value across the function execution. Provided the read-modify/increment-write occurs in interrupt context, the reads in the foreground will appear atomic. If you need some ''critical section'' code, then yes you'll probably want to disable interrupts to achieve that, or use ownership semaphores on structures.

ST define a lot of stuff as global, I tend to migrate that stuff to be local within the initialization routines, but I also make sure I have a sufficiently deep stack. A lot of the example code, and compiler templates, use stupidly small stacks. This can be particularly problematic if one uses printf or scanf where the stack is likely to plough into the heap with unexpected results for the unwary.

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