What Are Registers in embedded Rust?


  •  Registers are types of computer memory.
  • They reside near to CPU and are used immediately by the CPU.
  • Can hold data (bits of sequence), storage address (pointers), or instructions.
  • Registers holding the memory location are used to calculate the address of the next instruction.
  • In terms of MCU’s registers are special memory regions
  • These special regions map to or control some peripherals
  • And we already know we have many of them on our board
  • These peripherals are electronic components built in the MCU to provide its processor the ability to interact with IO devices.
  • We will be following code from this repository
  • https://github.com/PIAIC-IOT/Quarter2-Online.git
  • We are using a raw pointer here for demonstration purposes. It’s not recommended to use in normal programs btw except in the genuine case
  • Address (i.e. 0x48001018) points to a register
  • This register controls GPIO pins. GPIO is a peripheral
  • Also using GPIO we can drive the mapped pins to low or high

Light Emitting Diode (LED)

  • Pins are electrical contacts and our MCU has many of them
  • Few of them are connected to LEDs, few are connected to other devices on board
  • Once a pin is connected to an LED in right polarity and a high voltage is applied to that pin that’s the only case LED will glow

Reading the Reference Manual (RTRM)

Whenever we start learning a new programming language, using new hardware, or buying a new cell phone. Its documentation/guide is must read/follow thing to take full advantage of that.

  • Manual says that MCU has several pins.
  • These pins are grouped in ports of 16 pins
  • Each port named with a letter (e.g. A, B, C,.., H)
  • Pins within each port are named with a number (e.g. 0, 1, 2,.., 15)

Since we are interested in finding pins associated with LEDs, so let’s check the manual to find out those pins.

  • Manual says LD3 connected to PE9 and LD7 connected to PE11.
  • Here “PE” in PEXX represents the port name (i.e. Port E of GPIO peripheral)
  • The number after the port name represents the pin number in the port.
  • Each peripheral has a register block associated with it.
  • A register block is a collection of registers allocated in memory contiguously
  • The starting address of this block is called the base address
  • Now we know to drive LEDs we need to manipulate with Port E registers. For that, we need to find out the base address of Port E.

For finding the base address of Port E we have to follow the reference manual. Little other information’s we extracted from the documentation:

  • Bits 0-15 can be used to set the corresponding pins (i.e. bit 0 sets pin 0 and bit 15 sets bit 15)
  • Also, bits 16-31 can be used to reset the corresponding pins
  • All seem to be in agreement when correlating that information with our program, Writing 1.