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 #ifndef _SPI_H_
hudakz 1:1f2d9982fa8c 2 #define _SPI_H_
hudakz 1:1f2d9982fa8c 3
hudakz 1:1f2d9982fa8c 4 #include "BCM2835.h"
hudakz 1:1f2d9982fa8c 5
hudakz 1:1f2d9982fa8c 6 class SPI
hudakz 1:1f2d9982fa8c 7 {
hudakz 1:1f2d9982fa8c 8 public:
hudakz 1:1f2d9982fa8c 9 /** Create a SPI master connected to the specified pins.
hudakz 1:1f2d9982fa8c 10 */
hudakz 1:1f2d9982fa8c 11 SPI();
hudakz 1:1f2d9982fa8c 12 ~SPI();
hudakz 1:1f2d9982fa8c 13
hudakz 1:1f2d9982fa8c 14 /** Configure the data transmission format.
hudakz 1:1f2d9982fa8c 15 *
hudakz 1:1f2d9982fa8c 16 * @param bits Number of bits per SPI frame (4 - 16).
hudakz 1:1f2d9982fa8c 17 * @param mode Clock polarity and phase mode (0 - 3).
hudakz 1:1f2d9982fa8c 18 *
hudakz 1:1f2d9982fa8c 19 * @code
hudakz 1:1f2d9982fa8c 20 * mode | POL PHA
hudakz 1:1f2d9982fa8c 21 * -----+--------
hudakz 1:1f2d9982fa8c 22 * 0 | 0 0
hudakz 1:1f2d9982fa8c 23 * 1 | 0 1
hudakz 1:1f2d9982fa8c 24 * 2 | 1 0
hudakz 1:1f2d9982fa8c 25 * 3 | 1 1
hudakz 1:1f2d9982fa8c 26 * @endcode
hudakz 1:1f2d9982fa8c 27 */
hudakz 1:1f2d9982fa8c 28 void format(int bits, uint8_t mode = 0);
hudakz 1:1f2d9982fa8c 29
hudakz 1:1f2d9982fa8c 30 /** Set the SPI bus clock frequency.
hudakz 1:1f2d9982fa8c 31 *
hudakz 1:1f2d9982fa8c 32 * @param hz Clock frequency in Hz (default = 1MHz).
hudakz 1:1f2d9982fa8c 33 */
hudakz 1:1f2d9982fa8c 34 void frequency(int hz);
hudakz 1:1f2d9982fa8c 35
hudakz 1:1f2d9982fa8c 36 /** Write to the SPI Slave and return the response.
hudakz 1:1f2d9982fa8c 37 *
hudakz 1:1f2d9982fa8c 38 * @param value Data to be sent to the SPI slave.
hudakz 1:1f2d9982fa8c 39 *
hudakz 1:1f2d9982fa8c 40 * @return Response from the SPI slave.
hudakz 1:1f2d9982fa8c 41 */
hudakz 1:1f2d9982fa8c 42 uint8_t write(uint8_t value);
hudakz 1:1f2d9982fa8c 43
hudakz 1:1f2d9982fa8c 44 /** Write to the SPI Slave and obtain the response.
hudakz 1:1f2d9982fa8c 45 *
hudakz 1:1f2d9982fa8c 46 * The total number of bytes sent and received will be the maximum of
hudakz 1:1f2d9982fa8c 47 * tx_length and rx_length. The bytes written will be padded with the
hudakz 1:1f2d9982fa8c 48 * value 0xff.
hudakz 1:1f2d9982fa8c 49 *
hudakz 1:1f2d9982fa8c 50 * @param tx_buffer Pointer to the byte-array of data to write to the device.
hudakz 1:1f2d9982fa8c 51 * @param tx_length Number of bytes to write, may be zero.
hudakz 1:1f2d9982fa8c 52 * @param rx_buffer Pointer to the byte-array of data to read from the device.
hudakz 1:1f2d9982fa8c 53 * @param rx_length Number of bytes to read, may be zero.
hudakz 1:1f2d9982fa8c 54 * @return
hudakz 1:1f2d9982fa8c 55 * The number of bytes written and read from the device. This is
hudakz 1:1f2d9982fa8c 56 * maximum of tx_length and rx_length.
hudakz 1:1f2d9982fa8c 57 */
hudakz 1:1f2d9982fa8c 58 int write(const char* tx_buffer, int tx_length, char* rx_buffer, int rx_length);
hudakz 1:1f2d9982fa8c 59
hudakz 1:1f2d9982fa8c 60 /** Set default write value.
hudakz 1:1f2d9982fa8c 61 * SPI requires the master to send some data during a read operation.
hudakz 1:1f2d9982fa8c 62 * Different devices may require different default byte values.
hudakz 1:1f2d9982fa8c 63 * For example: An SD Card requires default bytes to be 0xFF.
hudakz 1:1f2d9982fa8c 64 *
hudakz 1:1f2d9982fa8c 65 * @param data Default character to be transmitted during a read operation.
hudakz 1:1f2d9982fa8c 66 */
hudakz 1:1f2d9982fa8c 67 void set_default_write_value(char value);
hudakz 1:1f2d9982fa8c 68 private:
hudakz 1:1f2d9982fa8c 69 uint8_t _write_fill;
hudakz 1:1f2d9982fa8c 70
hudakz 1:1f2d9982fa8c 71 void setClockDivider(uint16_t divider);
hudakz 1:1f2d9982fa8c 72 };
hudakz 1:1f2d9982fa8c 73
hudakz 1:1f2d9982fa8c 74 #endif // _SPI_H_
hudakz 1:1f2d9982fa8c 75