C function access to R0 to R12 registers

1.5k Views Asked by At

I need to write the C function that will return the value of a specific hardware register. For example R0.

I am unclear from the GHS documentation how this is done with the macros provided by the GHS Compiler.

uint32_t readRegRx(void)
{
   uint32_t x = 0U;
   __asm("MOV Rx, ??");
   return x;
}

What is the syntax in the GHS compiler for referencing a local variable as an argument to an inline assembly instruction?

I've seen this in the GHS documentation:

asm int mulword(a,b)
{
%con a %con b
   mov r0,a
   mov r1,b
   mul r0,r1,r0
%con a %reg b
   mov r0,a
   mul r0,b,r0
%reg a %con b
   mov r0,b
   mul r0,a,r0
%reg a %reg b
   mul r0,a,b
%con a %mem b
   mov r0,a
   ldr r1,b
   mul r0,r1,r0
%mem a %con b
   ldr r0,a
   mov r1,b
   mul r0,r1,r0
%mem a %mem b
   ldr r0,a
   ldr r1,b
   mul r0,r1,r0
%error
}

But this isn't exactly what I want, I think. The example from the documention above describes a function taking arguments. The return value is implicitly in R0.

In my case, what I want is to use a plain C function, with embedded inline assembly to read a register (R-anything) and store the value in a local variable in the function.

1

There are 1 best solutions below

0
jdw On

I received information from the GHS support and it addresses the means to read hardware registers (Rn) within C functions analogous to the extended GNU ARM features. The following applies to GHS compiler usage, not GNU compiler usage:

"For asm macro purposes (use within an asm macro), it's probably best to use the enhanced GNU asm syntax, and you'll want to turn on "Accept GNU asm statements" (--gnu_asm)."

int bar(void)
{
   int a;
   __asm__("mov r0, %0" : : "r"(a)); // move a to r0. Replace r0 to suit.
   // Stuff
  return a;
}

Alternative method:


asm void readR0(r0Val)

{

%reg r0Val

    mov r0Val,r0

}


void foo(void)

{

    register int regValReg = 0;
    readR0(regValReg);

    // Stuff
}