Jump to content
Sign in to follow this  
cridarco

Question About General Functions And Interrupts

Recommended Posts

Hi,

this is what BOOST C Compiler Reference Manual, PAG.64, says:

 

General functions and interrupts

Standard user functions are not thread-safe: their local variables are not saved when function execution gets interrupted by an interrupt. This can lead to very hard to trace errors.

To prevent this pitfall, the linker does not allow to call a given function from both main() and interrupt threads.

If you really need to use same function in both threads, you need to duplicate its code and assign a different name to the second copy.

 

I tried to build a file, that has a main function and a timed interrupt function. In both I need to call the embedded "delay_ms()" function.

 

This is the builder text:

 

Building...

BoostC Optimizing C Compiler Version 6.83 (for PIC16 architecture)

...

...

...

my_demo.c

 

success

BoostLink Optimizing Linker Version 6.83

...

...

...

 

Building CASM file

Serious Warning: Possible sw stack corruption, function 'delay_ms' called by more than one asynchronous thread (main/Task, interrupt, interrupt low)

...

...

...

success

...

...

...

Done

 

The manual says to duplicate function and call the copy from interrupt function, but how can I do with delay_ms()? I must write my own?

In addiction delay_ms() seems to work.

Note I'm sure that there are no possibilities to conflict during the execution of delay_ms routine when called from main() or from interrupt().

 

What you suggest to do?

Edited by cridarco

Share this post


Link to post
Share on other sites

Hi Cridarco,

 

Why are you putting millisecond delays within a timer interrupt handler as you have a timer to create delays for you. Interrupts should be in and out quickly. Let the main loop deal with any data the interrupt may have generated or collected.

 

If you must have a ms delay in the interrupt then write your own. Copy the assembler instruction that delay_ms() creates and chang any variables used to another name. Or, just put a for-loop inside the interrupt to set the required delay.

 

Better to avoid long interrupt functions.

 

Cheers

 

Reynard.

Share this post


Link to post
Share on other sites
The manual says to duplicate function and call the copy from interrupt function, but how can I do with delay_ms()? I must write my own?

In addiction delay_ms() seems to work.

Note I'm sure that there are no possibilities to conflict during the execution of delay_ms routine when called from main() or from interrupt().

 

What you suggest to do?

 

It won't work right because delay_ms uses a local variable that shares a memory location with local variables in other functions that the compiler determines _cannot_ be in scope at the same time. A quick look one of my programs shows it sharing memory with two other local variables. Can you guarantee that no other function using the memory that delay_ms uses can be running at the time of the interrupt? Even if you can, things move around as you change the program. You'd be chasing a moving target.

 

delay_ms is an easy case. It only uses one local variable. If you had any complex expressions, the compiler would add other local variables for intermediate results that you wouldn't know about unless you scrutinized the asm file. These, you cannot do anything about.

 

As someone else suggests, use a timer. Or set a flag and deal with the event in the main program. Or roll your own delay function.

 

Orin.

Share this post


Link to post
Share on other sites

Thanks to all for answers.

Your comments have cleared my doubt and convinced me that the best way is to deal with the event in the main loop.

Regards.

 

Cridarco

Share this post


Link to post
Share on other sites
Why not set state flags in your interrupt routine and have the delays in the main loop?

 

Yes, I do so.

The previous answer convinced me that this way is the best.

I use the interrupt only to set flag.

Thanks.

Cridarco

Share this post


Link to post
Share on other sites

I find capacitors to be much cheaper than time spent coding.

 

Jacob

 

I time or debounce push-button presses in interrupts with this:

for(i=0; i<100; i++) nop();

Regards, David.

Share this post


Link to post
Share on other sites

This seems to work pretty good for me....

	 +V
  |
  |
  \
  /
  \  10K Ohm
  /
  \
  |		\
uC ---|-------0  \-------
  |				 |
 === 0.1uF		  |
  |				 |
  |				 |
 ===			   ===
  -				 -

 

I find capacitors to be much cheaper than time spent coding.

 

please enlighten this newb as to how capacitors can be used for debouncing switch - thanks

 

http://forum.sourceboost.com/style_images/...icons/icon1.gif

http://forum.sourceboost.com/style_images/...icons/icon1.gif

Share this post


Link to post
Share on other sites
This seems to work pretty good for me....

	 +V
  |
  |
  \
  /
  \  10K Ohm
  /
  \
  |		\
uC ---|-------0  \-------
  |				 |
 === 0.1uF		  |
  |				 |
  |				 |
 ===			   ===
  -				 -

Could be dangerious (cause oscillation conditions for a while) if schmitt trigger inputs are not used.

 

Regards

Dave

Share this post


Link to post
Share on other sites
This seems to work pretty good for me....

	 +V
  |
  |
  \
  /
  \  10K Ohm
  /
  \
  |		\
uC ---|-------0  \-------
  |				 |
 === 0.1uF		  |
  |				 |
  |				 |
 ===			   ===
  -				 -

Could be dangerious (cause oscillation conditions for a while) if schmitt trigger inputs are not used.

 

Regards

Dave

 

I did some google searching on this recently. Here is an interesting reference:

 

www.embedded.com/showArticle.jhtml?articleID=18902552

 

It too requires a schmitt trigger input.

 

Orin.

Share this post


Link to post
Share on other sites

Join the conversation

You are posting as a guest. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...