variable protection while using interrupts

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

cross mob
niprc_3742601
Level 4
Level 4
5 likes given First like received First like given

Hello,

In OS world, if one thread writes to a variable and second thread reads the same variable then you need to protect the variable with a mutex, let's say.

But how does this suppose to work in the PSoC world?  I am planning to use Timer Counter with Clock to produce interrupts at certain rate.  My interrupt handler function will update the variable that keeps track of time elapsed.  The main processing loop reads this variable occasionally.  Do I need to protect this variable?  If I do then what's the mechanism?

Thanks!

Nikolay

0 Likes
1 Solution
odissey1
Level 9
Level 9
First comment on KBA 1000 replies posted 750 replies posted

Nikolay,

Every global variable that is changed in an interrupt handler must be declared as volatile, e.g.

volatile uint8 flag_myTimer=0;

Global Variables Not working When Used in CY_ISR - KBA224452

/odissey1

View solution in original post

5 Replies
MotooTanaka
Level 9
Level 9
Distributor - Marubun (Japan)
First comment on blog Beta tester First comment on KBA

Hi,

By the word "PSoC World",

if you are meaning some kind of multi-task OS (RTOS), you need same degree of care with "OS world".

And if you are talking about "OS-less" program,an interrupt routine is only preempt-ed by higher priority interrupt(s).

Usual flow, such as "main" may only access the value before or after interrupt service routine (ISR),

so the value must be stable from the view point of the usual flow.

IMHO, we don't have to protect a variable in such scenario.

(Note in case you touch same variable from different ISRs, you may have to protect it.)

moto

0 Likes

There are "EnterCriticalSection" and "ExitCriticalSection" to control nested interrupt flows. Have a look (search) into the "SystemReference Guide" from Creator Help menu.

Bob

MotooTanaka
Level 9
Level 9
Distributor - Marubun (Japan)
First comment on blog Beta tester First comment on KBA

Hi,

After posting the previous message I noticed that there can be a situation

when main reads a variable and modify it and before writing it back an ISR is called

and modified the same variable, this can be a problem.

In such a critical case, I would disable the interrupt during the read/modify/write then enable that interrupt again. But if the expected interrupt is a periodical timer and process can be done within the timer period or main is only referencing the variable, usually we don't have to worry about this much (I hope).

moto

odissey1
Level 9
Level 9
First comment on KBA 1000 replies posted 750 replies posted

Nikolay,

Every global variable that is changed in an interrupt handler must be declared as volatile, e.g.

volatile uint8 flag_myTimer=0;

Global Variables Not working When Used in CY_ISR - KBA224452

/odissey1

Hello,

thank you, everybody, for your help!  I use Critical Section to protect my variable and seems to be working fine.

CY_ISR( InterruptHandler )

{

    //  Clear the TCPWM terminal count interrupt

    //

    Timer_ClearInterrupt( Timer_INTR_MASK_TC );

   

    uint8 interrupt_state = CyEnterCriticalSection();

    //  the interrupt handler is called once every millisecond

    //

    time_elapsed_msecs += 1;

    CyExitCriticalSection( interrupt_state );

}

and

uint32

get_time_elapsed_msecs()

{

    uint8 interrupt_state = CyEnterCriticalSection();

    uint32 v = time_elapsed_msecs;   

    CyExitCriticalSection( interrupt_state );

    return( v );

}

Thanks!

Nikolay