This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

delay_us assembly error

Following code is used to generate delays on a cortex M0.

// for Cortex-M0 (subs for Cortex-M3)
void y_delay_3x_cycles(uint32_t cycles) __attribute__((naked, used));

void y_delay_3x_cycles(uint32_t cycles __attribute__((unused))) {
    __asm(
        "    sub r0, #1\n"
        "    bne y_delay_3x_cycles\n"
        "    bx lr"
    );
}

#define CYCLES_PER_SECOND 12000000

void y_delay_us(uint32_t us) {
    y_delay_3x_cycles((us * CYCLES_PER_SECOND) / 3000000);
}

void y_delay_ms(uint32_t ms) {
    while (ms--) {
        y_delay_3x_cycles(CYCLES_PER_SECOND / 3000);
    }
}

But when I compile with Keil, I get the following errors:
..\src\mp_micro.c(75): warning: #1207-D: unknown attribute "naked" void mp_micro_delay_3x_cycles(uint32_t cycles) __attribute__((naked, used));
..\src\mp_micro.c(79): warning: #1267-D: Implicit physical register R0 should be defined as a variable " sub r0, #1\n"
..\src\mp_micro.c(79): error: #2829: Cannot perform desired action on condition flags " sub r0, #1\n"
..\src\mp_micro.c: 2 warnings, 1 error
___________________________________
I tried using a variable for the warning:

void mp_micro_delay_3x_cycles(uint32_t cycles __attribute__((unused))) {
        int regVariable;
    __asm(
        "    sub regVariable, #1\n"
        "    bne mp_micro_delay_3x_cycles\n"
        "    bx lr"
    );
}

But still I get error #2829: Cannot perform desired action on condition flags