I'm trying to read a csr register using function macro
I have a struct array that contains name and address of csr registers
typedef struct csr_lists
{
int address;
const cahr* name;
} csr_lists;
csr_lists list[] =
{
{0xc00, "CYCLE"},
...
};
And the function I made:
#define csr_read(csr) \
({ \
register uint32_t v; \
__asm__ __volatile__ ("csrr %0, %1" \
: "=r" (v) \
: "n" (csr) \
: "memory"); \
v; \
})
So it is called like uint64_t value = csr_read(list[i].address); or uint64_t value = csr_read(0xc00);
And the compiler gives me following errors
csr.h:68:2: error: asm operand 1 probably doesn’t match constraints [-Werror]
68 | __asm__ __volatile__ ("csrr %0, %1" \
| ^~~~~~~
csr.c:127:25: note: in expansion of macro ‘csr_read’
127 | value[i] = csr_read(list[i].address);
|
csr.h:68:2: error: impossible constraint in ‘asm’
68 | __asm__ __volatile__ ("csrr %0, %1" \
| ^~~~~~~
csr.c:127:25: note: in expansion of macro ‘csr_read’
127 | value[i] = csr_read(list[i].address);
|
cc1: all warnings being treated as errors
/mnt/d/project/riscv32-linux/buildroot-2021.02.10/output/host/lib/gcc/riscv32-buildroot-linux-gnu/9.4.0/../../../../riscv32-buildroot-linux-gnu/bin/ld: ./libhpm.so: undefined reference to `csr_read'
How can I fix this problem?
edit) The problem happens at these lines
#define MAX 7
for (i = 0; i < MAX; i++)
{
value[i] = csr_read(list[i].address);
}