www.mikrocontroller.net

Mikrocontroller.net Forum WinARM / Yagarto / ARM-GCC > STR9 - how to mutex in thumb mode?

Posted by Dan Miller (gorlash)
on 10.06.2008 23:47
I'm porting some existing (running) code from IAR to GnuArm (Yagarto).
The existing project has some shared data that it protected by using
enable/disable IRQs for mutual exclusion.  Unfortunately, the functions
that IAR used to enable/disable interrupts were intrinsic functions
(get_cpsr/set_cpsr).  When I try to convert these functions to explicit
code, I have troubles switching to/from thumb mode.  I'm *still* finding
the discussions of this process very confusing!!

In any case, we *really* should be using a true mutex method for this,
rather than mucking with interrupts (since our project has numerous
interrupt sources), but I haven't found any sources yet that describe a
thumb mode mutex mechanism.

Has anyone ever managed this??  Could you give some hints??  Or, better
still, a working example?
Posted by Spencer Oliver (ntfreak)
on 12.06.2008 09:16
Dan Miller wrote:
> I'm porting some existing (running) code from IAR to GnuArm (Yagarto).
> The existing project has some shared data that it protected by using
> enable/disable IRQs for mutual exclusion.  Unfortunately, the functions
> that IAR used to enable/disable interrupts were intrinsic functions
> (get_cpsr/set_cpsr).  When I try to convert these functions to explicit
> code, I have troubles switching to/from thumb mode.  I'm *still* finding
> the discussions of this process very confusing!!
> 
> In any case, we *really* should be using a true mutex method for this,
> rather than mucking with interrupts (since our project has numerous
> interrupt sources), but I haven't found any sources yet that describe a
> thumb mode mutex mechanism.
> 
> Has anyone ever managed this??  Could you give some hints??  Or, better
> still, a working example?

other than using a rtos you probably do not have much choice about 
disabling ints - the rtos will probably do that as well.

This is similar code from FreeRTOS for disabling ints.

void vPortDisableInterruptsFromThumb( void ) __attribute__ ((naked));
void vPortEnableInterruptsFromThumb( void ) __attribute__ ((naked));

void vPortDisableInterruptsFromThumb( void ) __attribute__ ((naked));
void vPortEnableInterruptsFromThumb( void ) __attribute__ ((naked));

void vPortDisableInterruptsFromThumb( void )
{
  asm volatile (
    "STMDB  SP!, {R0}    \n\t"  /* Push R0. */
    "MRS  R0, CPSR    \n\t"  /* Get CPSR. */
    "ORR  R0, R0, #0xC0  \n\t"  /* Disable IRQ, FIQ. */
    "MSR  CPSR, R0     \n\t"  /* Write back modified value. */
    "LDMIA  SP!, {R0}     \n\t"  /* Pop R0. */
    "BX  R14" );            /*Return back to thumb. */
  }

void vPortEnableInterruptsFromThumb( void )
{
  asm volatile (
    "STMDB  SP!, {R0}    \n\t"  /* Push R0. */
    "MRS  R0, CPSR    \n\t"  /* Get CPSR. */
    "BIC  R0, R0, #0xC0  \n\t"  /* Enable IRQ, FIQ. */
    "MSR  CPSR, R0    \n\t"  /* Write back modified value. */
    "LDMIA  SP!, {R0}    \n\t"  /* Pop R0. */
    "BX  R14" );            /* Return back to thumb. */
}

Cheers
Spen
Posted by Dan Miller (gorlash)
on 12.06.2008 18:04
Spencer Oliver wrote:
> Dan Miller wrote:
>> I'm porting some existing (running) code from IAR to GnuArm (Yagarto).
> 
> other than using a rtos you probably do not have much choice about 
> disabling ints - the rtos will probably do that as well.
> 
Okay, let me try this out.  Thanks!!
BTW, we are using no RTOS here, just startup.s calling main.c