Software APIs
Macros
csr.h File Reference

(f166105)

Ibex Control and Status Register (CSR) interface. More...

#include <stdbool.h>
#include <stdint.h>
#include "sw/device/lib/base/csr_registers.h"
#include "sw/device/lib/base/stdasm.h"

Go to the source code of this file.

Macros

#define CSR_STATIC_ASSERT   _Static_assert
 Portable (C and C++) static assertion. More...
 
#define CSR_READ_IMPL(csr, dest)
 Define the implementation macros. More...
 
#define CSR_WRITE_IMPL(csr, val)
 
#define CSR_SET_BITS_IMPL(csr, mask)
 
#define CSR_CLEAR_BITS_IMPL(csr, mask)
 
#define CSR_READ(csr, dest)   CSR_READ_IMPL(csr, dest)
 Read the value of a CSR and place the result into the location pointed to by dest. More...
 
#define CSR_WRITE(csr, val)   CSR_WRITE_IMPL(csr, val)
 Write a value to a CSR. More...
 
#define CSR_SET_BITS(csr, mask)   CSR_SET_BITS_IMPL(csr, mask)
 Set masked bits in the CSR. More...
 
#define CSR_CLEAR_BITS(csr, mask)   CSR_CLEAR_BITS_IMPL(csr, mask)
 Clear masked bits in the CSR. More...
 

Detailed Description

Ibex Control and Status Register (CSR) interface.

A set of macros that provide both read and modify operations on Ibex CSRs. Compiling translation units that include this header file with -DMOCK_CSR will result in the CSR operations being replaced with a mocked implementation.

Definition in file csr.h.

Macro Definition Documentation

◆ CSR_CLEAR_BITS

#define CSR_CLEAR_BITS (   csr,
  mask 
)    CSR_CLEAR_BITS_IMPL(csr, mask)

Clear masked bits in the CSR.

Equivalent to:

csr &= ~mask

Parameters
csrThe target register. MUST be a CSR_REG_<name> constant.
maskMask containing the bits to clear.

Definition at line 180 of file csr.h.

◆ CSR_CLEAR_BITS_IMPL

#define CSR_CLEAR_BITS_IMPL (   csr,
  mask 
)
Value:
do { \
CSR_STATIC_ASSERT(sizeof(mask) == sizeof(uint32_t), \
"mask must be a 4-byte value"); \
asm volatile("csrc %0, %1;" ::"i"(csr), "r"(mask)); \
} while (false)

Definition at line 123 of file csr.h.

◆ CSR_READ

#define CSR_READ (   csr,
  dest 
)    CSR_READ_IMPL(csr, dest)

Read the value of a CSR and place the result into the location pointed to by dest.

Equivalent to:

*dest = csr

Parameters
csrThe target register. MUST be a CSR_REG_<name> constant.
[out]destPointer to a variable where the value of the named CSR will be written to.

Definition at line 144 of file csr.h.

◆ CSR_READ_IMPL

#define CSR_READ_IMPL (   csr,
  dest 
)
Value:
do { \
CSR_STATIC_ASSERT(sizeof(*dest) == sizeof(uint32_t), \
"dest must point to a 4-byte variable"); \
asm volatile("csrr %0, %1;" : "=r"(*dest) : "i"(csr)); \
} while (false)

Define the implementation macros.

The implementation used depends on whether the CSR library is providing a real or a mocked interface.

Definition at line 102 of file csr.h.

◆ CSR_SET_BITS

#define CSR_SET_BITS (   csr,
  mask 
)    CSR_SET_BITS_IMPL(csr, mask)

Set masked bits in the CSR.

Equivalent to:

csr |= mask

Parameters
csrThe target register. MUST be a CSR_REG_<name> constant.
maskMask containing the bits to set.

Definition at line 168 of file csr.h.

◆ CSR_SET_BITS_IMPL

#define CSR_SET_BITS_IMPL (   csr,
  mask 
)
Value:
do { \
CSR_STATIC_ASSERT(sizeof(mask) == sizeof(uint32_t), \
"mask must be a 4-byte value"); \
asm volatile("csrs %0, %1;" ::"i"(csr), "r"(mask)); \
} while (false)

Definition at line 116 of file csr.h.

◆ CSR_STATIC_ASSERT

#define CSR_STATIC_ASSERT   _Static_assert

Portable (C and C++) static assertion.

TODO: replace with static_assert macro in C (assert.h does this)?

Definition at line 36 of file csr.h.

◆ CSR_WRITE

#define CSR_WRITE (   csr,
  val 
)    CSR_WRITE_IMPL(csr, val)

Write a value to a CSR.

Equivalent to:

csr = val

Parameters
csrThe target register. MUST be a CSR_REG_<name> constant.
valThe value to write to the named CSR.

Definition at line 156 of file csr.h.

◆ CSR_WRITE_IMPL

#define CSR_WRITE_IMPL (   csr,
  val 
)
Value:
do { \
CSR_STATIC_ASSERT(sizeof(val) == sizeof(uint32_t), \
"val must be a 4-byte value"); \
asm volatile("csrw %0, %1;" ::"i"(csr), "r"(val)); \
} while (false)

Definition at line 109 of file csr.h.