Simvik

Lay out a register map

Declare a peripheral struct whose members land on the right offsets. A medium embedded C problem (Registers and memory-mapped I/O), run and graded in the…

medium · Registers and memory-mapped I/O · structs · offsets

A timer's datasheet lists these 32-bit registers and offsets:

  • CR1 at 0x00
  • CR2 at 0x04
  • SR at 0x10
  • CNT at 0x24
  • PSC at 0x28
  • ARR at 0x2C

Complete timer_regs_t so that every member sits at its documented offset and the struct is 0x30 bytes long. The gaps are reserved addresses.

Starter code

typedef struct {
  volatile uint32_t CR1;   // 0x00
  volatile uint32_t CR2;   // 0x04
  volatile uint32_t SR;    // 0x10
  volatile uint32_t CNT;   // 0x24
  volatile uint32_t PSC;   // 0x28
  volatile uint32_t ARR;   // 0x2C
} timer_regs_t;

Example tests

Control and status registers

printf("%u %u %u\n", (unsigned)offsetof(timer_regs_t, CR1), (unsigned)offsetof(timer_regs_t, CR2), (unsigned)offsetof(timer_regs_t, SR));

Expected output:

0 4 16

Counter

printf("%u\n", (unsigned)offsetof(timer_regs_t, CNT));

Expected output:

36

Learn it first

  • Registers, volatile and const · How C talks to hardware: memory-mapped registers, what volatile and const promise, register structs and read-modify-write.

More Registers and memory-mapped I/O problems