Nios® V/II Embedded Design Suite (EDS)
Support for Embedded Development Tools, Processors (SoCs and Nios® V/II processor), Embedded Development Suites (EDSs), Boot and Configuration, Operating Systems, C and C++
Announcements
FPGA community forums and blogs on community.intel.com are migrating to the new Altera Community and are read-only. For urgent support needs during this transition, please visit the FPGA Design Resources page or contact an Altera Authorized Distributor.

Nios Semaphore?

Altera_Forum
Honored Contributor II
2,372 Views

I need a good semaphore to make a fifo IRQ safe. On the PC it's easy with the assembly instruction BTS which Bit Tests and Sets. So you can try to set a bit and know if you did it in a single clock cycle. 

 

That means its a perfect semaphore. 

 

I don't see anything like that in the Nios reference. Anyone have any ideas? Or a custom instruction that performs the same task? 

 

Thanks, 

Ken
0 Kudos
6 Replies
Altera_Forum
Honored Contributor II
1,273 Views

It depends on what sort of concurrent access you're concerned about. 

 

If you're just worried about preventing simultaneous access from different threads/tasks in a multitasking OS (Linux, uCos, or homegrown) running on a single processor, then you just need to disable interrupts for the section requiring atomic access (since code can't be pre-empted except via an interrupt). 

 

On the other hand, if you're worried about simultaneous access from different processors accessing common shared memory, then some sort of hardware solution will be required.
0 Kudos
Altera_Forum
Honored Contributor II
1,273 Views

<div class='quotetop'>QUOTE </div> 

--- Quote Start ---  

On the other hand, if you&#39;re worried about simultaneous access from different processors accessing common shared memory, then some sort of hardware solution will be required.[/b] 

--- Quote End ---  

 

 

I&#39;m interested in this kind of solution... http://forum.niosforum.com/work2/style_emoticons/<#EMO_DIR#>/ohmy.gif  

 

Does anyone know how to develop it? 

 

 

Bye http://forum.niosforum.com/work2/style_emoticons/<#EMO_DIR#>/rolleyes.gif
0 Kudos
Altera_Forum
Honored Contributor II
1,273 Views

SCS, 

 

I tried that first, but was having issues with ISR&#39;s disabling interrupts. 

 

Anyway, I revisited disabling interrupts and tested exhaustively and seems fine now. 

 

So Thanks! 

 

Here are some NiosI macros if anyone is interested. 

BTW, is there a doc with all the asm syntax? I&#39;d like to assign stat as the return value. 

# define get_status_reg(stat)  

{  

unsigned int status;  

asm("rdctl %0" : "=r" (status));  

stat = status;  

# define disable_interrupts()  

asm("PFX 8" );  

asm("WRCTL %g0"); 

# define enable_interrupts()  

asm("PFX 9" );  

asm("WRCTL %g0"); 

# define interrupts_enabled(stat)  

{  

unsigned int status;  

asm("rdctl %0" : "=r" (status));  

stat = status & 0x8000;  

}
0 Kudos
Altera_Forum
Honored Contributor II
1,273 Views

I can&#39;t take credit for finding this...but here it is: 

For NiosII, the processor reference tells you all about the ASM commands, it can be found under the nios2 kit directory: nios2\documents\n2cpu_nii5v1.pdf 

 

I hope that helps.
0 Kudos
Altera_Forum
Honored Contributor II
1,273 Views

It&#39;s particularly convenient to define a pair of functions (or macros) 

int disable_interrupts (void); restore_interrupts(int flag); // start critical section int flag = disable_interrupts(); // do stuff in an atomic manner restore_interrupts(flag); // end critical section 

where disable_interrupts() returns a flag indicating whether interrupts were enabled or not before the call, and restore_interrupts() restores them to the previous state. This makes it safe to put the critical section inside a function that might be called from a context where interrupts shouldn&#39;t be enabled (like inside another critical section, or during startup code). 

 

Unconditionally enabling interrupts (as opposed to restoring the previous state) is usually quite a rare operation: at the end of startup code once the interrupt service routines (ISRs) are setup, or within a NiosI ISR (since interrupts were disabled when the ISR was invoked).
0 Kudos
Altera_Forum
Honored Contributor II
1,273 Views

SCS, 

 

Good point. I thought of that but took the easy way http://forum.niosforum.com/work2/style_emoticons/<#EMO_DIR#>/smile.gif  

 

I wrote the function that tells me if interrupts are dissabled and ran for awhile. Since they were never disabled I knew it would be safe to blindly disable/enable. Sloppy. I&#39;ll put in the code to restore the state. 

 

Thanks! 

Ken
0 Kudos
Reply