View previous topic :: View next topic |
Author |
Message |
blups
Joined: 05 May 2011 Posts: 10
|
Jump to defined place in the code |
Posted: Thu Jul 18, 2013 7:13 am |
|
|
Hi peeps,
I need to do some kind of system reset in some special case. The system reset is but not the MCU one. So i have to jump in a defined place in the code. Something like goto statement. My project contains many .h files and to work with goto i have to be in same file with the label. In other words I have a situation, where my lable and goto statements are in a different files. The compiler crashes.
Do you know some tricky way to do this?
Much 10x in advance...
GR |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19504
|
|
Posted: Thu Jul 18, 2013 8:01 am |
|
|
A jump 'between routines', will leave the stack imbalanced, and eventually crash the code.
Easiest is to use 'reset_cpu', and then test 'restart_cause', to detect that this is a software restart, and not a hardware one.
Best Wishes |
|
|
asmboy
Joined: 20 Nov 2007 Posts: 2128 Location: albany ny
|
|
Posted: Thu Jul 18, 2013 8:28 am |
|
|
OR....
setup a function - called 'init()'
with a flag that is defined one way on cpu_reset
asin
int dowhat=0; in you prefix code area before main
and another value in active code that will call it as you desire later
for your warm boot
then call it at the start of main()
BTDTGTTS |
|
|
SherpaDoug
Joined: 07 Sep 2003 Posts: 1640 Location: Cape Cod Mass USA
|
|
Posted: Mon Jul 22, 2013 5:57 am |
|
|
C has a perfectly functional goto command. It is rarely used but there are times when it is the right tool for the job. _________________ The search for better is endless. Instead simply find very good and get the job done. |
|
|
oxo
Joined: 13 Nov 2012 Posts: 219 Location: France
|
|
Posted: Mon Jul 22, 2013 7:20 am |
|
|
SherpaDoug wrote: | C has a perfectly functional goto command. It is rarely used but there are times when it is the right tool for the job. |
Which is NEVER. See Ttelmah's comments above. |
|
|
RF_Developer
Joined: 07 Feb 2011 Posts: 839
|
|
Posted: Mon Jul 22, 2013 8:14 am |
|
|
In C goto has function scope: you can only jump to somewhere within the same function. The reason is presumably to avoid the stack corruption that out of function jumps necessarily imply.
While I am firmly in the "dont use gotos" camp, there are times when its possibly useful. Jumping out of a highly nested command handler on some error condition for example.
To jump out of one routine into another in a totally different source file is NOT a good idea. Indeed, it shows there's something badly wrong with the program's architechure and structure. If you really need to do some kind of "reset" or "warm start" then to a proper restart and detect that in your initialisation code. Most of the time, that's not needed, and there's a much simpler way to acheive the same effect. |
|
|
|