mbed API for Raspberry Pi boards.

mbedPi

This is an attempt to implement a limited number of mbed APIs for Raspberry Pi single-board computers. The project was inspired by and based on the arduPi library developed for the Arduino by Cooking Hacks .

/media/uploads/hudakz/board01.jpg

Specifications

  • Chip: Broadcom BCM2836 SoC
  • Core architecture: Quad-core ARM Cortex-A7
  • CPU frequency: 900 MHz
  • GPU: Dual Core VideoCore IV® Multimedia Co-Processor
  • Memory: 1GB LPDDR2
  • Operating System: Boots from Micro SD card, running a version of the Linux operating system
  • Power: Micro USB socket 5V, 2A

Connectors

  • Ethernet: 10/100 BaseT Ethernet socket
  • Video Output: HDMI (rev 1.3 & 1.4)
  • Audio Output: 3.5mm jack, HDMI
  • USB: 4 x USB 2.0 Connector
  • GPIO Connector: 40-pin 2.54 mm (100 mil) expansion header: 2x20 strip providing 27 GPIO pins as well as +3.3 V, +5 V and GND supply lines
  • Camera Connector: 15-pin MIPI Camera Serial Interface (CSI-2)
  • JTAG: Not populated
  • Display Connector: Display Serial Interface (DSI) 15 way flat flex cable connector with two data lanes and a clock lane
  • Memory Card Slot: Micro SDIO

GPIO connector pinout

Zoom in /media/uploads/hudakz/mbedpi_pinout02.png

Information

Only the labels printed in blue/white or green/white (i.e. p3, gpio2 ...) must be used in your code. The other labels are given as information (alternate-functions, power pins, ...).


Building programs for the Raspberry Pi with mbedPi

I use Qt Creator for development, however you can use any other IDE available on the Raspberry Pi (e.g. Geany) if you like. For a quick try:

  • Install Qt and the Qt Creator onto your Raspberry Pi. Then create a new "Blinky" Plain non-Qt C++ Project as follows: /media/uploads/hudakz/newproject.png

  • Change the main code as below:

main.cpp

#include "mbedPi.h"

int main()
{
    DigitalOut  myled(p7);

    while(1) {
        myled = 1; // LED is ON
        wait(0.2); // 200 ms
        myled = 0; // LED is OFF
        wait(1.0); // 1 sec
        printf("Blink\r\n");
    }
}


  • Copy the mbedPi.zip file into your project's folder and unzip.
  • Add the mbedPi.h and mbedPi.cpp files to your project by right clicking on the "Blinky" project and then clicking on the "Add Existing Files..." option in the local menu:

    /media/uploads/hudakz/addfiles.png

    /media/uploads/hudakz/addfiles02.png

  • Double click on Blinky.pro to open it for editing and add new libraries by inserting a new line as follows:

    /media/uploads/hudakz/libs.png

  • Compile the project.

  • Connect an LED through a 1k resistor to pin 7 and the ground on the Raspberry Pi GPIO connector.

  • Run the binary as sudo (sudo ./Blinky) and you should see the LED blinking. /media/uploads/hudakz/mbedpi_run.png

  • Press Ctrl+c to stop running the application.
Committer:
hudakz
Date:
Tue Dec 20 12:16:18 2022 +0000
Revision:
2:131555dc6fb7
Parent:
1:1f2d9982fa8c
Mbed API for Raspberry Pi boards equipped with BCM2836 SoC.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
hudakz 1:1f2d9982fa8c 1 #include "mbed.h"
hudakz 1:1f2d9982fa8c 2
hudakz 1:1f2d9982fa8c 3 extern struct bcm2835_peripheral gpio;
hudakz 1:1f2d9982fa8c 4
hudakz 1:1f2d9982fa8c 5 /********** FUNCTIONS OUTSIDE CLASSES **********/
hudakz 1:1f2d9982fa8c 6
hudakz 1:1f2d9982fa8c 7 // Write a HIGH or a LOW value to a digital pin
hudakz 1:1f2d9982fa8c 8 void gpio_write(PinName pin, int value)
hudakz 1:1f2d9982fa8c 9 {
hudakz 1:1f2d9982fa8c 10 if (value == HIGH)
hudakz 1:1f2d9982fa8c 11 GPSET0 = (1 << pin);
hudakz 1:1f2d9982fa8c 12 else
hudakz 1:1f2d9982fa8c 13 if (value == LOW)
hudakz 1:1f2d9982fa8c 14 GPCLR0 = (1 << pin);
hudakz 1:1f2d9982fa8c 15
hudakz 1:1f2d9982fa8c 16 wait_us(1); // Delay to allow any change in state to be reflected in the LEVn, register bit.
hudakz 1:1f2d9982fa8c 17 }
hudakz 1:1f2d9982fa8c 18
hudakz 1:1f2d9982fa8c 19 // Reads the value from a specified digital pin, either HIGH or LOW.
hudakz 1:1f2d9982fa8c 20 int gpio_read(PinName pin)
hudakz 1:1f2d9982fa8c 21 {
hudakz 1:1f2d9982fa8c 22 Digivalue value;
hudakz 1:1f2d9982fa8c 23 if (GPLEV0 & (1 << pin))
hudakz 1:1f2d9982fa8c 24 value = HIGH;
hudakz 1:1f2d9982fa8c 25 else
hudakz 1:1f2d9982fa8c 26 value = LOW;
hudakz 1:1f2d9982fa8c 27
hudakz 1:1f2d9982fa8c 28 return value;
hudakz 1:1f2d9982fa8c 29 }
hudakz 1:1f2d9982fa8c 30
hudakz 1:1f2d9982fa8c 31 // Function select
hudakz 1:1f2d9982fa8c 32 // pin is a BCM2835 GPIO pin number NOT RPi pin number
hudakz 1:1f2d9982fa8c 33 // There are 6 control registers, each control the functions of a block
hudakz 1:1f2d9982fa8c 34 // of 10 pins.
hudakz 1:1f2d9982fa8c 35 // Each control register has 10 sets of 3 bits per GPIO pin:
hudakz 1:1f2d9982fa8c 36 //
hudakz 1:1f2d9982fa8c 37 // 000 = GPIO Pin X is an input
hudakz 1:1f2d9982fa8c 38 // 001 = GPIO Pin X is an output
hudakz 1:1f2d9982fa8c 39 // 100 = GPIO Pin X takes alternate function 0
hudakz 1:1f2d9982fa8c 40 // 101 = GPIO Pin X takes alternate function 1
hudakz 1:1f2d9982fa8c 41 // 110 = GPIO Pin X takes alternate function 2
hudakz 1:1f2d9982fa8c 42 // 111 = GPIO Pin X takes alternate function 3
hudakz 1:1f2d9982fa8c 43 // 011 = GPIO Pin X takes alternate function 4
hudakz 1:1f2d9982fa8c 44 // 010 = GPIO Pin X takes alternate function 5
hudakz 1:1f2d9982fa8c 45 //
hudakz 1:1f2d9982fa8c 46 // So the 3 bits for port X are:
hudakz 1:1f2d9982fa8c 47
hudakz 1:1f2d9982fa8c 48 // X / 10 + ((X % 10) * 3)
hudakz 1:1f2d9982fa8c 49
hudakz 1:1f2d9982fa8c 50 void bcm2835_gpio_fsel(uint8_t pin, uint8_t mode)
hudakz 1:1f2d9982fa8c 51 {
hudakz 1:1f2d9982fa8c 52 // Function selects are 10 pins per 32 bit word, 3 bits per pin
hudakz 1:1f2d9982fa8c 53 volatile uint32_t* paddr = (volatile uint32_t*)gpio.map + BCM2835_GPFSEL0 / 4 + (pin / 10);
hudakz 1:1f2d9982fa8c 54 uint8_t shift = (pin % 10) * 3;
hudakz 1:1f2d9982fa8c 55 uint32_t mask = BCM2835_GPIO_FSEL_MASK << shift;
hudakz 1:1f2d9982fa8c 56 uint32_t value = mode << shift;
hudakz 1:1f2d9982fa8c 57
hudakz 1:1f2d9982fa8c 58 bcm2835_peri_set_bits(paddr, value, mask);
hudakz 1:1f2d9982fa8c 59 }
hudakz 1:1f2d9982fa8c 60
hudakz 1:1f2d9982fa8c 61 /**
hudakz 1:1f2d9982fa8c 62 * @brief
hudakz 1:1f2d9982fa8c 63 * @note
hudakz 1:1f2d9982fa8c 64 * @param
hudakz 1:1f2d9982fa8c 65 * @retval
hudakz 1:1f2d9982fa8c 66 */
hudakz 1:1f2d9982fa8c 67 void gpio_dir(PinName pin, PinDirection direction)
hudakz 1:1f2d9982fa8c 68 {
hudakz 1:1f2d9982fa8c 69 uint8_t gpfsel = pin / 10;
hudakz 1:1f2d9982fa8c 70 uint8_t shift = (pin % 10) * 3;
hudakz 1:1f2d9982fa8c 71 uint32_t mask = BCM2835_GPIO_FSEL_MASK << shift;
hudakz 1:1f2d9982fa8c 72 uint32_t outp = BCM2835_GPIO_FSEL_OUTP << shift;
hudakz 1:1f2d9982fa8c 73
hudakz 1:1f2d9982fa8c 74 if (direction == PIN_OUTPUT) {
hudakz 1:1f2d9982fa8c 75 *(gpio.addr + gpfsel) &= ~mask;
hudakz 1:1f2d9982fa8c 76 *(gpio.addr + gpfsel) |= outp;
hudakz 1:1f2d9982fa8c 77 }
hudakz 1:1f2d9982fa8c 78 else
hudakz 1:1f2d9982fa8c 79 if (direction == PIN_INPUT) {
hudakz 1:1f2d9982fa8c 80 *(gpio.addr + gpfsel) &= ~mask;
hudakz 1:1f2d9982fa8c 81 }
hudakz 1:1f2d9982fa8c 82 }
hudakz 1:1f2d9982fa8c 83
hudakz 1:1f2d9982fa8c 84 /**
hudakz 1:1f2d9982fa8c 85 * @brief
hudakz 1:1f2d9982fa8c 86 * @note
hudakz 1:1f2d9982fa8c 87 * @param
hudakz 1:1f2d9982fa8c 88 * @retval
hudakz 1:1f2d9982fa8c 89 */
hudakz 1:1f2d9982fa8c 90 void gpio_mode(PinName pin, PinMode mode)
hudakz 1:1f2d9982fa8c 91 {
hudakz 1:1f2d9982fa8c 92 mode == PullUp ? gpio_write(pin, HIGH) : gpio_write(pin, LOW);
hudakz 1:1f2d9982fa8c 93 }
hudakz 1:1f2d9982fa8c 94
hudakz 1:1f2d9982fa8c 95 /**
hudakz 1:1f2d9982fa8c 96 * @brief
hudakz 1:1f2d9982fa8c 97 * @note
hudakz 1:1f2d9982fa8c 98 * @param
hudakz 1:1f2d9982fa8c 99 * @retval
hudakz 1:1f2d9982fa8c 100 */
hudakz 1:1f2d9982fa8c 101 uint8_t shiftIn(PinName dPin, PinName cPin, bcm2835SPIBitOrder order)
hudakz 1:1f2d9982fa8c 102 {
hudakz 1:1f2d9982fa8c 103 uint8_t value = 0;
hudakz 1:1f2d9982fa8c 104 int8_t i;
hudakz 1:1f2d9982fa8c 105
hudakz 1:1f2d9982fa8c 106 if (order == MSBFIRST)
hudakz 1:1f2d9982fa8c 107 for (i = 7; i >= 0; --i) {
hudakz 1:1f2d9982fa8c 108 gpio_write(cPin, HIGH);
hudakz 1:1f2d9982fa8c 109 value |= gpio_read(dPin) << i;
hudakz 1:1f2d9982fa8c 110 gpio_write(cPin, LOW);
hudakz 1:1f2d9982fa8c 111 }
hudakz 1:1f2d9982fa8c 112 else
hudakz 1:1f2d9982fa8c 113 for (i = 0; i < 8; ++i) {
hudakz 1:1f2d9982fa8c 114 gpio_write(cPin, HIGH);
hudakz 1:1f2d9982fa8c 115 value |= gpio_read(dPin) << i;
hudakz 1:1f2d9982fa8c 116 gpio_write(cPin, LOW);
hudakz 1:1f2d9982fa8c 117 }
hudakz 1:1f2d9982fa8c 118
hudakz 1:1f2d9982fa8c 119 return value;
hudakz 1:1f2d9982fa8c 120 }
hudakz 1:1f2d9982fa8c 121
hudakz 1:1f2d9982fa8c 122 /**
hudakz 1:1f2d9982fa8c 123 * @brief
hudakz 1:1f2d9982fa8c 124 * @note
hudakz 1:1f2d9982fa8c 125 * @param
hudakz 1:1f2d9982fa8c 126 * @retval
hudakz 1:1f2d9982fa8c 127 */
hudakz 1:1f2d9982fa8c 128 void shiftOut(PinName dPin, PinName cPin, bcm2835SPIBitOrder order, uint8_t val)
hudakz 1:1f2d9982fa8c 129 {
hudakz 1:1f2d9982fa8c 130 int8_t i;
hudakz 1:1f2d9982fa8c 131
hudakz 1:1f2d9982fa8c 132 if (order == MSBFIRST)
hudakz 1:1f2d9982fa8c 133 for (i = 7; i >= 0; --i) {
hudakz 1:1f2d9982fa8c 134 gpio_write(dPin, val & (1 << i));
hudakz 1:1f2d9982fa8c 135 gpio_write(cPin, HIGH);
hudakz 1:1f2d9982fa8c 136 gpio_write(cPin, LOW);
hudakz 1:1f2d9982fa8c 137 }
hudakz 1:1f2d9982fa8c 138 else
hudakz 1:1f2d9982fa8c 139 for (i = 0; i < 8; ++i) {
hudakz 1:1f2d9982fa8c 140 gpio_write(dPin, val & (1 << i));
hudakz 1:1f2d9982fa8c 141 gpio_write(cPin, HIGH);
hudakz 1:1f2d9982fa8c 142 gpio_write(cPin, LOW);
hudakz 1:1f2d9982fa8c 143 }
hudakz 1:1f2d9982fa8c 144 }
hudakz 1:1f2d9982fa8c 145