Skip to content

Low Level Hardware Control

Jamie Boyd edited this page Mar 6, 2018 · 4 revisions

Low Level Hardware Control includes the following files:

  1. GPIOlowLevel.h - address offsets, plus macros and functions for low level access
  2. GPIOlowLevel.cpp - functions for peripheral mapping and global variables for those mappings

GPIOlowLevel.h provides constants containing address offsets, plus macros and functions for low level access to Raspberry Pi GPIO peripherals, without using a third-party library for peripheral access. The other classes all use the code in these two files.

The base address for peripheral access is different between different Pi versions. A constant defines which Raspberry Pi board to use. Comment out exactly one of the following 2 lines:

//#define RPI
#define RPI2

Define RPI2 if using a Raspberry Pi 3. It is expected that very few people will still be using a Raspberry Pi 1.

Low level memory for GPIO can be accessed through the newer /dev/gpiomem interface which does not require root access. Unfortunately, the PWM and clock hardware are NOT accessible through /dev/gpiomem, and must be accessed through /dev/mem, which requires running your programs with sudo or gksudo to get root access. A constant is defined for each interface:

#define IFACE_DEV_GPIOMEM	1	// use this only for GPIO
#define IFACE_DEV_MEM	0	// use this for GPIO or PWM and/or clock access

As a user of the classes described below, all you need to know is to use sudo or gksudo when accessing anything other than the GPIO peripheral, and the class (e.g. SimpleGPIO_thread) will use the appropriate interface.

GPIOlowLevel.cpp contains utility functions for peripheral mapping and global variables for the mappings. With the memory mappings available globally, only one instance is needed. The number of threads using each peripheral is also tracked, so the memory mapping can be deleted when it is no longer needed. bcm_peripheralPtr GPIOperi = nullptr; // memory mapping for GPIO peripheral int GPIOperi_users=0; // number of threads using the GPIO peripheral mapping

Return to Table of Contents

Clone this wiki locally