Project Home
Project Home
Documents
Documents
Wiki
Wiki
Discussion Forums
Discussions
Project Information
Project Info
Forum Topic - Regarding the usage of InterruptLock and InterruptUnlock API: (6 Items)
   
Regarding the usage of InterruptLock and InterruptUnlock API  
Hi All,
Can anyone help me with a sample code for the usage of InterruptLock() and InterruptUnlock APIs. I am trying to use it 
in my GPIO driver to safegaurd against race conditions and also for the interrupt handlers.
As per the reference document if i am trying to intialize the "spinlock" variable in the InterruptLock() the code is 
crashing. I have tried both putting the spinlock variable as static and also doing memset. The code is crashing when 
doing the memset or during the InterruptLock call when defining the spinlock as static. I am also using the 
ThreadCtl(_NTO_TCTL_IO,0) call also to get the thread control.
Re: Regarding the usage of InterruptLock and InterruptUnlock API  
Hi Praveen,

it seems a bit odd that already the initialization should fail. Can you post the relevant code pieces, i.e., 
  - spinlock variable declaration
  - ThreadCtl()
  - interrupt handler
  - spinlock use at thread level

Regards,
Thomas
Re: Regarding the usage of InterruptLock and InterruptUnlock API  
Hi Thomas,
Thank you for your response.
The piece of code where in i am trying this is something like this

#include <sys/neutrino.h> // Inlcude already for many other functions like interruptattach,mmap_device_io

       intrspin_t *clrSpnLck ;

	if( ThreadCtl( _NTO_TCTL_IO, 0 ) != -1 )
	{
                //I tried this as per  the manual
		//memset( clrSpnLck, 1, sizeof( *clrSpnLck ) ); //Code crashed either here if enabled

		//clrSpnLck->value = 1; //Tried this just if the variable in the structure intrspin_t was to be initialized

		InterruptLock(SetSpnLck_g); //Or code crashed here

		regData  &= (~(1 << bitPos));  //volatile uint32_t regData and uint8_t bitPos

		InterruptUnlock(SetSpnLck_g);

	}
Is there anything i am missing
Re: Regarding the usage of InterruptLock and InterruptUnlock API  
//    intrspin_t *clrSpnLck ;
    intrspin_t clrSpnLck ;          //  You must use this !


memset( &clrSpnLck, 1, sizeof( clrSpnLck ) ); //


InterruptLock(&SetSpnLck); // This should resolve 

InterruptUnlock(&SetSpnLck);

Best regards,
M. Sangalli
Re: Regarding the usage of InterruptLock and InterruptUnlock API  
Hi Praveen,

aside from using an real spinlock variable (as shown by Mario), you will need to initialize the spinlock to all 0 
instead of all 1. Attached is a small example program that uses a spinlock to synchronize thread and ISR, hope that 
helps to illustrate things.

Kind regards,
Thomas
Attachment: Text intrspin.c 1.05 KB
Re: Regarding the usage of InterruptLock and InterruptUnlock API  
Hi Thomas and Sangalli,
Thank you very for your help and my code is working fine now. The code i modified as per your suggestions and sample 
code works with out crashing.

With regards,
Praveen Kumar