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.
Revision:
1:1f2d9982fa8c
diff -r 91392e1f8551 -r 1f2d9982fa8c source/Thread.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/source/Thread.cpp	Tue Dec 20 12:08:07 2022 +0000
@@ -0,0 +1,157 @@
+/*
+ *  Copyright (C) 2016 Zoltan Hudak
+ *  hudakz@outlook.com
+ *
+ *  Parts Copyright (C) Libelium Comunicaciones Distribuidas S.L.
+ *  http://www.libelium.com
+ *
+ *  This program is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ *  Version 1.0
+ */
+#include "Thread.h"
+/*$off*/
+
+pthread_t  idThread2;
+pthread_t  idThread3;
+pthread_t  idThread4;
+pthread_t  idThread5;
+pthread_t  idThread6;
+pthread_t  idThread7;
+pthread_t  idThread8;
+pthread_t  idThread9;
+pthread_t  idThread10;
+pthread_t  idThread11;
+pthread_t  idThread12;
+pthread_t  idThread13;
+pthread_t  idThread14;
+pthread_t  idThread15;
+pthread_t  idThread16;
+pthread_t  idThread17;
+pthread_t  idThread18;
+pthread_t  idThread19;
+pthread_t  idThread20;
+pthread_t  idThread21;
+pthread_t  idThread22;
+pthread_t  idThread23;
+pthread_t  idThread24;
+pthread_t  idThread25;
+pthread_t  idThread26;
+pthread_t  idThread27;
+
+pthread_t*   idThreads[26] =
+{
+   &idThread2,
+   &idThread3,
+   &idThread4,
+   &idThread5,
+   &idThread6,
+   &idThread7,
+   &idThread8,
+   &idThread9,
+   &idThread10,
+   &idThread11,
+   &idThread12,
+   &idThread13,
+   &idThread14,
+   &idThread15,
+   &idThread16,
+   &idThread17,
+   &idThread18,
+   &idThread19,
+   &idThread20,
+   &idThread21,
+   &idThread22,
+   &idThread23,
+   &idThread24,
+   &idThread25,
+   &idThread26,
+   &idThread27
+};
+
+/*$on*/
+
+/* This is the function that will be running in a thread if
+ * attachInterrupt() is called */
+void* threadFunction(void* args)
+{
+    ThreadArg*      arguments = (ThreadArg*)args;
+    int             pin = arguments->pin;
+
+    int             GPIO_FN_MAXLEN = 32;
+    int             RDBUF_LEN = 5;
+
+    char            fn[GPIO_FN_MAXLEN];
+    int             fd, ret;
+    struct pollfd   pfd;
+    char            rdbuf[RDBUF_LEN];
+
+    memset(rdbuf, 0x00, RDBUF_LEN);
+    memset(fn, 0x00, GPIO_FN_MAXLEN);
+
+    snprintf(fn, GPIO_FN_MAXLEN - 1, "/sys/class/gpio/gpio%d/value", pin);
+    fd = open(fn, O_RDONLY);
+    if (fd < 0) {
+        perror(fn);
+        exit(1);
+    }
+
+    pfd.fd = fd;
+    pfd.events = POLLPRI;
+
+    ret = unistd::read(fd, rdbuf, RDBUF_LEN - 1);
+    if (ret < 0) {
+        perror("Error reading interrupt file\n");
+        exit(1);
+    }
+
+    while (1) {
+        memset(rdbuf, 0x00, RDBUF_LEN);
+        unistd::lseek(fd, 0, SEEK_SET);
+        ret = poll(&pfd, 1, -1);
+        if (ret < 0) {
+            perror("Error waiting for interrupt\n");
+            unistd::close(fd);
+            exit(1);
+        }
+
+        if (ret == 0) {
+            printf("Timeout\n");
+            continue;
+        }
+
+        ret = unistd::read(fd, rdbuf, RDBUF_LEN - 1);
+        if (ret < 0) {
+            perror("Error reading interrupt file\n");
+            exit(1);
+        }
+
+        //Interrupt. We call user function.
+        arguments->func();
+    }
+}
+
+/**
+ * @brief
+ * @note
+ * @param
+ * @retval
+ */
+pthread_t* getThreadIdFromPin(int pin)
+{
+    int i = pin - 2;
+    if ((0 <= i) && (i <= 25))
+        return idThreads[i];
+    else return NULL;
+}