Raspberry Pi IoT In C - Pi 5 Memory Mapped GPIO
Written by Harry Fairhead   
Wednesday, 10 January 2024
Article Index
Raspberry Pi IoT In C - Pi 5 Memory Mapped GPIO
Registers
RIO
A Fast Pulse

A Fast Pulse

Now we can re-write a Blinky style program to find out how fast the Pi 5 can toggle a GPIO line. We first have to map the peripherals area into user space and set up all of the addresses we want to use. Next we configure the pin as an output and start a loop that toggles the line using the XOR register:

The complete program is:

#include <stdio.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <stdint.h>
typedef struct{
    uint32_t status;
    uint32_t ctrl; 
}GPIOregs;
#define GPIO ((GPIOregs*)GPIOBase)
typedef struct
{
    uint32_t Out;
    uint32_t OE;
    uint32_t In;
    uint32_t InSync;
} rioregs;
#define rio ((rioregs *)RIOBase)
#define rioXOR ((rioregs *)(RIOBase + 0x1000 / 4))
#define rioSET ((rioregs *)(RIOBase + 0x2000 / 4))
#define rioCLR ((rioregs *)(RIOBase + 0x3000 / 4))
int main(int argc, char **argv)
{
    int memfd = open("/dev/mem", O_RDWR | O_SYNC);
    uint32_t *map = (uint32_t *)mmap(
        NULL,
        64 * 1024 * 1024,
        (PROT_READ | PROT_WRITE),
        MAP_SHARED,
        memfd,
        0x1f00000000
    );
    if (map == MAP_FAILED)
    {
        printf("mmap failed: %s\n", strerror(errno));
        return (-1);
    };
    close(memfd);
    uint32_t *PERIBase = map;
    uint32_t *GPIOBase = PERIBase + 0xD0000 / 4;
    uint32_t *RIOBase = PERIBase + 0xe0000 / 4;
    uint32_t *PADBase = PERIBase + 0xf0000 / 4;
    uint32_t *pad = PADBase + 1;   
    
    uint32_t pin = 2;
    uint32_t fn = 5;
    GPIO[pin].ctrl=fn;
    pad[pin] = 0x10;
    rioSET->OE = 0x01<<pin;
    rioSET->Out = 0x01<<pin;
    
    for (;;)
    {
        rioXOR->Out = 0x04;
    } 
    return (EXIT_SUCCESS);
} 

If you try this out you will find that the Pi 5 produces some consistent 25ns pulses which makes it compatible in speed to the Pi 4, but without the need for memory barriers.

fastpulse

In Chapter but not in this extract

  • Your Own Library
  • Using PWM

 

Digging Deeper

The big problem with the Pi 5 at the time of writing is the lack of documentation. What has been made available is incomplete and a great deal of reverse engineering is required to get things working. This will likely improve over time.

Summary

  • The Pi 5 has its peripherals implemented by the custom RP1 chip and this makes it incompatible with all previous Pis.

  • The memory map and the register configurations are different and more like those found in the Pico in than other Pis.

  • You can still work with the Pi 5’s registers using the standard memory mapping techniques.

  • The /dev/mem device accesses all of the memory, but needs root privileges.

  • The /dev/gpiomem0 device only provides the GPIO registers, but it can be used without root privileges.

  • The Pi 5 uses the same RIO based GPIO implementation as the Pico. To use the GPIO lines you first have to select RIO mode.

  • Each GPIO line also has a PAD and a PAD control register.

  • The RIO registers are used to control and read the state of the GPIO line.

  • Using direct memory access, the Pi 5 can produce well-formed 25ns pulses

  • Although using the registers directly is a good way to get started, creating functions, and hence a library to do the same job, is a good idea.

Raspberry Pi And The IoT In C Third Edition

By Harry Fairhead

piciot3e360

Buy from Amazon.

Contents

  1. Choosing A Pi For IoT
  2. Getting Started
  3. Getting Started With The GPIO
  4. Simple Output
  5. Some Electronics
  6. Simple Input
  7. GPIO The Linux Way
  8. Advanced Input – Events, Threads, Interrupts
  9. Pulse Width Modulation - Servos And More
  10. Using The I2C Bus
  11. The DHT22 Sensor Implementing A Custom Protocol
  12. Exploring - 1‑Wire Bus Basics 
  13. Using iButtons
  14. DS18B20 Temperature Sensor
  15. The Multidrop 1‑Wire Bus
  16. The Serial Port
  17. Getting Started With The SPI Bus
  18. A to D With The SPI Bus
  19. Connecting With The Web - Sockets
  20. Memory-Mapped GPIO
  21. The Pi 5
    Extract: Memory Mapped GPIO ***NEW!!!
  22. Almost Real-Time Linux
  23. Appendix I GPIO Sysfs Interface
  24. Appendix II Using VS Code For Remote C Development

To be informed about new articles on I Programmer, sign up for our weekly newsletter, subscribe to the RSS feed and follow us on Twitter, Facebook or Linkedin.

Banner


Google Reduces Support For Python, Dart And Flutter
01/05/2024

There are many reports that Google has removed people from its Python, Dart and Flutter teams and possibly more. What does this say about relying on Google as a source of technology for your projects? [ ... ]



OpenSilver 2.2 Adds LightSwitch Compatibility Pack
30/04/2024

OpenSilver 2.2 has been released with the addition of a LightSwitch Compatibility Pack designed to provide a way to run legacy Visual Studio LightSwitch applications on modern browsers. The open-sourc [ ... ]


More News

raspberry pi books

 

Comments




or email your comment to: comments@i-programmer.info



Last Updated ( Wednesday, 10 January 2024 )