Skip to main content

Lab: Physical Memory


This document is not yet written.

The Gist


Either add dtparam=act_led_trigger=none to /boot/config.txt OR run echo none > /sys/class/leds/led0/trigger as root. We disable triggering so that the state of the pin doesn't change on us. The student can optionally sanity check their setup by having the kernel trigger the led on and off:

  • Turn on with: echo 1 > /sys/class/leds/led0/brightness (as root).
  • Turn off with: echo 0 > /sys/class/leds/led0/brightness (as root).


Access physical memory from user space with /dev/mem and mmap(). Given that this ACT led is running on GPIO Pin 47, use /dev/mem and mmap() to toggle the ACT led on and off.

The student should be able to locate the base address of the GPIO controller. They should additionally be able to locate and utilize the GPLEV1, GPSET1, and GPCLR1 registers.

Register Offsets

PIN 47 Mask: 0x00000400
GPSET1 => 0x20 - Set the pin.
GPCLR1 => 0x2c - Clear the pin.
GPLEV1 => 0x38 - Get the pin level.


#include <stdio.h>
#include <stdint.h>
#include <stddef.h>
#include <stdlib.h>

#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

int main()

const int BASE_ADDR = 0xfe200000;

int fd = open("/dev/mem", O_RDWR | O_SYNC);
if (fd == -1) {
printf("error opening /dev/mem.\n");

volatile uint32_t *base = (volatile uint32_t *)mmap((void *)BASE_ADDR, 0x1000, PROT_READ | PROT_WRITE, MAP_SHARED, fd, BASE_ADDR);
if (base == (void *)-1) {
printf("Failed to mmap.\n");

printf("BEFORE %08x [%02d] : %08x\n", (uint32_t)(base + 14), 14, base[14]);

//This will set the bit.
//base[8] = 0x00000400;

// This will clear the bit.
base[11] = 0x00000400;

printf("AFTER %08x [%02d] : %08x\n", (uint32_t)(base + 14), 14, base[14]);

return 0;