CY8CKIT-059 programming debug/releae

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
Anonymous
Not applicable

Hi,

   

In these simple program an ADC_SAR is driven by PWM and  ADC_SAR eoc drives the isr. It works only when compiled with debug mode, when compiled in release there is output at pin25 (enters interrupt) but  no output at Pin23 (doesn't enter inner main loop).
And in release mode Pin25 is wider (about 850ns) than in debug mode (about 750ns). Pin23 and Pin25 are only used as test points.

   

#include <project.h>
#include <stdio.h>

   

uint8 dataReady;
uint16 result;

   

int main()
{
CyGlobalIntEnable; /* Enable global interrupts. */
ADC_SAR_1_Start();
PWM_1_Start();
isr_1_Start();
/* Place your initialization/startup code here (e.g. MyInst_Start()) */
for(;;)
{
/* Place your application code here. */
if (dataReady)
{
   Pin23_Write(1);
   result=ADC_SAR_1_GetResult16();
   dataReady=0;
   Pin23_Write(0);
}
}
}
___________________________
CY_ISR(isr_1_Interrupt)
{
#ifdef isr_1_INTERRUPT_INTERRUPT_CALLBACK
isr_1_Interrupt_InterruptCallback();
#endif /* isr_1_INTERRUPT_INTERRUPT_CALLBACK */
/* Place your Interrupt code here. */
/* `#START isr_1_Interrupt` */
Pin25_Write(1);
dataReady=1;
Pin25_Write(0);
/* `#END` */
}

0 Likes
3 Replies
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

This is a common error that no developer misses. The good ones only once in their life 😉

   

When a program runs in debug mode and refuses to run in release mode the cause of the error has to do with optimization.

   

In your loop you find

   

for(;;)
{
if (dataReady)
{
...

   

From the view of the compiler, dataready does not change within the for-loop and so the whole if-clause can be optimized-out and put right before the loop.

   

Because this is not what you want

   

Declare every global variable that gets changed in an interrupt handler as "volatile". This will instruct the compiler/optimizer not to remove accesses to that variable from loops etc.

   

In your case

   

volatile uint8 dataReady;

   

And another hint:

   

Use CY_ISR_PROTO(Yourhandler), CY_ISR(Yourhandler) and sr_1_StartEx(Yourhandler) to avoid making changes in a generated .c file. See "System Reference Guide" from Creator Help menu.

   

 

   

Bob

0 Likes
Anonymous
Not applicable

Thanks Bob,

   

I will try to remember it. Now it works fine.

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

You are always welcome!

   

 

   

Bob

0 Likes