Quantcast
Channel:
Viewing all articles
Browse latest Browse all 59170

Forum Post: RE: How to define a Critical Region

$
0
0
Shared resources with interrupts are always tricky. The difficult part is to create something that can be used in both supervisor and user mode. Interrupts run always in supervisor mode on the RX. I have created my own OS, where all the tasks run in user mode. And for critical things like sharing I switch to supervisor mode. To do this I have defined that the interrupt levels 1 - 7 can be used for critical stuff like sharing. And interrupt levels 8 - 15 can be used for high priority interrupts that don’t need sharing. This solution works only without nested interrupts. You require 2 interrupt routines for entering and exiting supervisor mode: The first one is the context enter, place this on interrupt vector 1. This interrupt will set the processor interrupt priority level to 7 (IPL 7). The disables all interrupt with priority 1 - 7. void ContextEnterException(void){ MOV.B #7h,7[R0]; RTE; } The second one will exit, place this on interrupt vector 2. This interrupt sets the processor interrupt priority level back to 0 (IPL 0). And enables thus all interrupts. void ContextExitException(void){ MOV.B #000h,7[R0]; RTE; } To enter a critical section use the following define. First the processor mode is checked. If the processor is in user mode (normal task execution) than it will execute the ContextEnterException to enter supervisor mode with IPL 7. If the processor already runs in supervisor mode, then its not needed to switch because this means that the current execution happens in a interrupt. #define EnterSafeContext() \ do{ \ if(_builtin_get_psw() & 0x020000){ \ _builtin_int_exception(1); \ } \ }while(FALSE); To exit the critical section use the following define: #define ExitSafeContext() \ do{ \ if(_builtin_get_psw() & 0x020000){ \ _builtin_int_exception(2); \ } \ }while(FALSE); And make sure to define the following built in methods: extern unsigned long  _builtin_get_psw(void); extern void           _builtin_int_exception(signed long); or use the #include builtin.h Do the following to use it in your code: EnterSafeContext(){ //do some sharing stuff, can only be interrupted by interrupts with IPL 8 - 15 }ExitSafeContext()

Viewing all articles
Browse latest Browse all 59170

Trending Articles