lördag 25 december 2010

Calling Cortex-M3 assembler function in RAM from gcc C

I needed a fast  function written in assembler and run from RAM. I thought the most simple way to do this without adding stuff to the linker script was to change the .text directive to .data (same as initialised variables). But the result of the change was a hard fault when the function got called.
The solution was to tell gcc it is a function with .type logic_sample_fast, %function
So the whole function looks like this:
.syntax unified
.cpu cortex-m3
.thumb
.data
.align 4

#define FIO0PIN                    (0x2009c014)
#define FIO1PIN                    (0x2009c034)
#define LOGIC_IN_FIOPIN_REG        FIO1PIN

.global logic_sample_fast
.type logic_sample_fast, %function
.thumb_func
logic_sample_fast:
    movw r1, :lower16:LOGIC_IN_FIOPIN_REG
    movt r1, :upper16:LOGIC_IN_FIOPIN_REG
    cpsid i
trig0:
    ldrb r2, [r1]
    ands r3, r2, #1
    bne trig0
trig1:
    ldrb r2, [r1]
    ands r3, r2, #1
    beq trig1
    .rept (40*8)
    ldrb r2, [r1]
    strb r2, [r0], #1
    .endr
    cpsie i
    bx lr
And the definition in C like this:
void logic_sample_fast(uint8_t *buffer);

Inga kommentarer:

Skicka en kommentar