Skip to content

Instantly share code, notes, and snippets.

@qookei
Last active April 21, 2020 11:48
Show Gist options
  • Save qookei/03bb37a34554e0e354208d9ec838628f to your computer and use it in GitHub Desktop.
Save qookei/03bb37a34554e0e354208d9ec838628f to your computer and use it in GitHub Desktop.
Bad clone of managarm's libarch
#include <stdint.h>
constexpr uintptr_t spi1_addr = 0x40013000;
enum class spi_reg : uintptr_t {
cr2 = 0x04
};
template <uintptr_t Base, auto Reg>
struct periph_reg_addr {
static constexpr uintptr_t addr = Base + static_cast<uintptr_t>(Reg);
constexpr uintptr_t operator()() { return addr; }
};
template <uintptr_t Base, auto Reg>
inline constexpr uintptr_t periph_reg_addr_v = periph_reg_addr<Base, Reg>{}();
template <typename T, uintptr_t Addr>
struct hw_register {
volatile T *ptr = reinterpret_cast<volatile T *>(Addr);
void store(T val) { *ptr = val; }
T load() { return *ptr; }
};
int main () {
hw_register<uint32_t, periph_reg_addr_v<spi1_addr, spi_reg::cr2>> reg;
reg.store(32);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment