Helpful ReplyHot!Review on critical section

Author
Workaholic
New Member
  • Total Posts : 8
  • Reward points : 0
  • Joined: 2016/06/27 01:50:17
  • Location: 0
  • Status: offline
2018/04/16 01:56:48 (permalink)
0

Review on critical section

Hello guys. My recent code works good, but sometimes it behaves uncertainly. I need your suggestions regarding the critical section of the code that I have implemented, is further need any modifications.
I am using pic24f mcu.
 
void __attribute__((interrupt,no_auto_psv)) _IC1Interrupt(void)
{
cap1 = IC1BUF;
cap2 = IC1BUF;
IFS0bits.IC1IF = 0;
}
 
In main code,
IEC0bits.IC1IE = 0; //disbale the interrupt
counts = cap2-cap1;
__delay_us(5);
IEC0bits.IC1IE = 1; // interrupt enable again
 
Another interrupt  is accessing the variable "value" that is being modified in the main loop, but here I didn't disable the interrupt.
if (counts==X)
value=1;
else
value=0;
 
void __attribute__((interrupt,no_auto_psv)) _U1RXInterrupt(void)

Transmit(value);
IFS0bits.U1TXIF = 0;
}
#1
maxruben
Super Member
  • Total Posts : 3217
  • Reward points : 0
  • Joined: 2011/02/22 03:35:11
  • Location: Sweden
  • Status: offline
Re: Review on critical section 2018/04/16 03:07:23 (permalink) ☄ Helpfulby Workaholic 2018/04/19 04:16:52
0
If Transmit() is used in several contexts (main code, interrupts, different RTOS tasks) it must be thread safe (reentrant).
Also, if it is blocking it should not be called from an isr.
 
/Ruben
#2
Aussie Susan
Super Member
  • Total Posts : 3117
  • Reward points : 0
  • Joined: 2008/08/18 22:20:40
  • Location: Melbourne, Australia
  • Status: offline
Re: Review on critical section 2018/04/16 19:09:48 (permalink) ☄ Helpfulby Workaholic 2018/04/19 04:16:49
4 (1)
Also make sure that any variable that is modified in an ISR and accessed (especially read) in non-ISR code is marked as 'volatile'.
Susan
#3
HJonker
Super Member
  • Total Posts : 489
  • Reward points : 0
  • Joined: 2006/04/19 02:17:59
  • Location: NL
  • Status: offline
Re: Review on critical section 2018/04/16 22:28:40 (permalink) ☄ Helpfulby Workaholic 2018/04/19 04:16:44
4 (1)
One nasty thing:
The cycle immediately after the "disable interrupt" can still be interrupted (been there, seen that in at least the pic24HJ series). Since the next instruction will most likely first 'load' cap2 (or cap1), check the resulting disassembly, you might end up with a calculation based on samples from 2 separate interrupts.
Solution: add a nop just after disabling the interrupts.
 

Kind Regards,
Hans Jonker
(Amsterdam, Holland)
#4
Jump to:
© 2018 APG vNext Commercial Version 4.5