This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

ti.sysbios.family.arm.lm4.Timer: bad inline assembly under gcc

Other Parts Discussed in Thread: SYSBIOS

SYSBIOS: 6_35_03_47 and 6_35_04_50, UDP: and 6_37_00_20 (bundled with TI-RTOS v1.21.00.09)

ti/sysbios/family/arm/lm4/Timer.c:968

Bool Timer_masterDisable(Void)
{
    /* read PRIMASK bit to R0 and call CPSID to disable interrupts */
    asm("    mrs     r0, PRIMASK\n"
        "    cpsid   i\n"
        "    bx      lr\n");
    /*
     * The following return statement keeps the compiler from complaining
     * about a missing return value.  The "bx lr" above does the actual return
     * and the below statement is not executed.
     */
    return (0);
}

compiles to incorrect code (return before stack restore) that leads to an exception on TM4C123GXL:

(gdb) disas 0xaefe
Dump of assembler code for function ti_sysbios_family_arm_lm4_Timer_masterDisable__I:
   0x0000aef4 <+0>:     push    {r7}
   0x0000aef6 <+2>:     add     r7, sp, #0
   0x0000aef8 <+4>:     mrs     r0, PRIMASK
   0x0000aefc <+8>:     cpsid   i
   0x0000aefe <+10>:    bx      lr
   0x0000af00 <+12>:    mov.w   r3, #0
   0x0000af04 <+16>:    mov     r0, r3
   0x0000af06 <+18>:    mov     sp, r7
   0x0000af08 <+20>:    pop     {r7}
   0x0000af0a <+22>:    bx      lr
End of assembler dump.

One possible fix:

Bool Timer_masterDisable(Void)
{
    int r;
    /* read PRIMASK bit and call CPSID to disable interrupts */
    asm("    mrs     %[r], PRIMASK\n"
        "    cpsid   i\n"
    : [r] "=&r" (r)
    :
    );
    return r;
}

gcc:

$ arm-none-eabi-gcc -v
Using built-in specs.
COLLECT_GCC=arm-none-eabi-gcc
COLLECT_LTO_WRAPPER=/usr/bin/../lib/gcc/arm-none-eabi/4.7.4/lto-wrapper
Target: arm-none-eabi
Configured with: /build/buildd/gcc-arm-none-eabi-4-7-2013q3/src/gcc/configure --target=arm-none-eabi --prefix=/build/buildd/gcc-arm-none-eabi-4-7-2013q3/install-native --libexecdir=/build/buildd/gcc-arm-none-eabi-4-7-2013q3/install-native/lib --infodir=/build/buildd/gcc-arm-none-eabi-4-7-2013q3/install-native/share/doc/gcc-arm-none-eabi/info --mandir=/build/buildd/gcc-arm-none-eabi-4-7-2013q3/install-native/share/doc/gcc-arm-none-eabi/man --htmldir=/build/buildd/gcc-arm-none-eabi-4-7-2013q3/install-native/share/doc/gcc-arm-none-eabi/html --pdfdir=/build/buildd/gcc-arm-none-eabi-4-7-2013q3/install-native/share/doc/gcc-arm-none-eabi/pdf --enable-languages=c,c++ --disable-decimal-float --disable-libffi --disable-libgomp --disable-libmudflap --disable-libquadmath --disable-libssp --disable-libstdcxx-pch --disable-nls --disable-shared --disable-threads --disable-tls --with-gnu-as --with-gnu-ld --with-newlib --with-headers=yes --with-python-dir=share/gcc-arm-none-eabi --with-sysroot=/build/buildd/gcc-arm-none-eabi-4-7-2013q3/install-native/arm-none-eabi --with-host-libstdcxx='-static-libgcc -Wl,-Bstatic,-lstdc++,-Bdynamic -lm' --with-pkgversion='GNU Tools for ARM Embedded Processors' --with-multilib-list=armv6-m,armv7-m,armv7e-m,armv7-r
Thread model: single
gcc version 4.7.4 20130913 (release) [ARM/embedded-4_7-branch revision 202601] (GNU Tools for ARM Embedded Processors)