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 .
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
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:
- 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:
- Double click on Blinky.pro to open it for editing and add new libraries by inserting a new line as follows:
- 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.
- Press Ctrl+c to stop running the application.
Diff: source/gpio.cpp
- Revision:
- 1:1f2d9982fa8c
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/source/gpio.cpp Tue Dec 20 12:08:07 2022 +0000 @@ -0,0 +1,145 @@ +#include "mbed.h" + +extern struct bcm2835_peripheral gpio; + +/********** FUNCTIONS OUTSIDE CLASSES **********/ + +// Write a HIGH or a LOW value to a digital pin +void gpio_write(PinName pin, int value) +{ + if (value == HIGH) + GPSET0 = (1 << pin); + else + if (value == LOW) + GPCLR0 = (1 << pin); + + wait_us(1); // Delay to allow any change in state to be reflected in the LEVn, register bit. +} + +// Reads the value from a specified digital pin, either HIGH or LOW. +int gpio_read(PinName pin) +{ + Digivalue value; + if (GPLEV0 & (1 << pin)) + value = HIGH; + else + value = LOW; + + return value; +} + +// Function select +// pin is a BCM2835 GPIO pin number NOT RPi pin number +// There are 6 control registers, each control the functions of a block +// of 10 pins. +// Each control register has 10 sets of 3 bits per GPIO pin: +// +// 000 = GPIO Pin X is an input +// 001 = GPIO Pin X is an output +// 100 = GPIO Pin X takes alternate function 0 +// 101 = GPIO Pin X takes alternate function 1 +// 110 = GPIO Pin X takes alternate function 2 +// 111 = GPIO Pin X takes alternate function 3 +// 011 = GPIO Pin X takes alternate function 4 +// 010 = GPIO Pin X takes alternate function 5 +// +// So the 3 bits for port X are: + +// X / 10 + ((X % 10) * 3) + +void bcm2835_gpio_fsel(uint8_t pin, uint8_t mode) +{ + // Function selects are 10 pins per 32 bit word, 3 bits per pin + volatile uint32_t* paddr = (volatile uint32_t*)gpio.map + BCM2835_GPFSEL0 / 4 + (pin / 10); + uint8_t shift = (pin % 10) * 3; + uint32_t mask = BCM2835_GPIO_FSEL_MASK << shift; + uint32_t value = mode << shift; + + bcm2835_peri_set_bits(paddr, value, mask); +} + +/** + * @brief + * @note + * @param + * @retval + */ +void gpio_dir(PinName pin, PinDirection direction) +{ + uint8_t gpfsel = pin / 10; + uint8_t shift = (pin % 10) * 3; + uint32_t mask = BCM2835_GPIO_FSEL_MASK << shift; + uint32_t outp = BCM2835_GPIO_FSEL_OUTP << shift; + + if (direction == PIN_OUTPUT) { + *(gpio.addr + gpfsel) &= ~mask; + *(gpio.addr + gpfsel) |= outp; + } + else + if (direction == PIN_INPUT) { + *(gpio.addr + gpfsel) &= ~mask; + } +} + +/** + * @brief + * @note + * @param + * @retval + */ +void gpio_mode(PinName pin, PinMode mode) +{ + mode == PullUp ? gpio_write(pin, HIGH) : gpio_write(pin, LOW); +} + +/** + * @brief + * @note + * @param + * @retval + */ +uint8_t shiftIn(PinName dPin, PinName cPin, bcm2835SPIBitOrder order) +{ + uint8_t value = 0; + int8_t i; + + if (order == MSBFIRST) + for (i = 7; i >= 0; --i) { + gpio_write(cPin, HIGH); + value |= gpio_read(dPin) << i; + gpio_write(cPin, LOW); + } + else + for (i = 0; i < 8; ++i) { + gpio_write(cPin, HIGH); + value |= gpio_read(dPin) << i; + gpio_write(cPin, LOW); + } + + return value; +} + +/** + * @brief + * @note + * @param + * @retval + */ +void shiftOut(PinName dPin, PinName cPin, bcm2835SPIBitOrder order, uint8_t val) +{ + int8_t i; + + if (order == MSBFIRST) + for (i = 7; i >= 0; --i) { + gpio_write(dPin, val & (1 << i)); + gpio_write(cPin, HIGH); + gpio_write(cPin, LOW); + } + else + for (i = 0; i < 8; ++i) { + gpio_write(dPin, val & (1 << i)); + gpio_write(cPin, HIGH); + gpio_write(cPin, LOW); + } +} +