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.
include/PwmOut.h@1:1f2d9982fa8c, 21 months ago (annotated)
- Committer:
- hudakz
- Date:
- Tue Dec 20 12:08:07 2022 +0000
- Revision:
- 1:1f2d9982fa8c
mbed API for Raspberry Pi boards equipped with BCM2836 SoC.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
hudakz | 1:1f2d9982fa8c | 1 | #ifndef _PWM_OUT_H_ |
hudakz | 1:1f2d9982fa8c | 2 | #define _PWM_OUT_H_ |
hudakz | 1:1f2d9982fa8c | 3 | |
hudakz | 1:1f2d9982fa8c | 4 | #include "BCM2835.h" |
hudakz | 1:1f2d9982fa8c | 5 | |
hudakz | 1:1f2d9982fa8c | 6 | class PwmOut { |
hudakz | 1:1f2d9982fa8c | 7 | |
hudakz | 1:1f2d9982fa8c | 8 | public: |
hudakz | 1:1f2d9982fa8c | 9 | |
hudakz | 1:1f2d9982fa8c | 10 | /** Create a PwmOut connected to the specified pin |
hudakz | 1:1f2d9982fa8c | 11 | * |
hudakz | 1:1f2d9982fa8c | 12 | * @param pin PwmOut pin to connect to |
hudakz | 1:1f2d9982fa8c | 13 | */ |
hudakz | 1:1f2d9982fa8c | 14 | PwmOut(PinName pin = gpio18); |
hudakz | 1:1f2d9982fa8c | 15 | |
hudakz | 1:1f2d9982fa8c | 16 | ~PwmOut(); |
hudakz | 1:1f2d9982fa8c | 17 | |
hudakz | 1:1f2d9982fa8c | 18 | /** Set the output duty-cycle, specified as a percentage (float) |
hudakz | 1:1f2d9982fa8c | 19 | * |
hudakz | 1:1f2d9982fa8c | 20 | * @param value A floating-point value representing the output duty-cycle, |
hudakz | 1:1f2d9982fa8c | 21 | * specified as a percentage. The value should lie between |
hudakz | 1:1f2d9982fa8c | 22 | * 0.0f (representing on 0%) and 1.0f (representing on 100%). |
hudakz | 1:1f2d9982fa8c | 23 | * Values outside this range will be saturated to 0.0f or 1.0f. |
hudakz | 1:1f2d9982fa8c | 24 | */ |
hudakz | 1:1f2d9982fa8c | 25 | void write(float value); |
hudakz | 1:1f2d9982fa8c | 26 | |
hudakz | 1:1f2d9982fa8c | 27 | /** Return the current output duty-cycle setting, measured as a percentage (float) |
hudakz | 1:1f2d9982fa8c | 28 | * |
hudakz | 1:1f2d9982fa8c | 29 | * @returns |
hudakz | 1:1f2d9982fa8c | 30 | * A floating-point value representing the current duty-cycle being output on the pin, |
hudakz | 1:1f2d9982fa8c | 31 | * measured as a percentage. The returned value will lie between |
hudakz | 1:1f2d9982fa8c | 32 | * 0.0f (representing on 0%) and 1.0f (representing on 100%). |
hudakz | 1:1f2d9982fa8c | 33 | * |
hudakz | 1:1f2d9982fa8c | 34 | * @note |
hudakz | 1:1f2d9982fa8c | 35 | * This value may not match exactly the value set by a previous write(). |
hudakz | 1:1f2d9982fa8c | 36 | */ |
hudakz | 1:1f2d9982fa8c | 37 | float read(); |
hudakz | 1:1f2d9982fa8c | 38 | |
hudakz | 1:1f2d9982fa8c | 39 | /** Set the PWM period, specified in bcm2835PWMPulseWidth (micro/nano seconds), keeping the duty cycle the same. |
hudakz | 1:1f2d9982fa8c | 40 | * @note Sets clock divider according to the required period. |
hudakz | 1:1f2d9982fa8c | 41 | * @param period Change the period of a PWM signal. The allowed values are: |
hudakz | 1:1f2d9982fa8c | 42 | * BCM2835_PWM_PERIOD_212_US -> 213.33 us = 4.6875 kHz |
hudakz | 1:1f2d9982fa8c | 43 | * BCM2835_PWM_PERIOD_107_US -> 106.66 us = 9.375 kHz |
hudakz | 1:1f2d9982fa8c | 44 | * BCM2835_PWM_PERIOD_53_US -> 53.33 us = 18.75 kHz |
hudakz | 1:1f2d9982fa8c | 45 | * BCM2835_PWM_PERIOD_27_US -> 26.66 us = 37.50 kHz |
hudakz | 1:1f2d9982fa8c | 46 | * BCM2835_PWM_PERIOD_13_US -> 13.33 us = 75.00 kHz |
hudakz | 1:1f2d9982fa8c | 47 | * BCM2835_PWM_PERIOD_7_US -> 6.66 us = 150.00 kHz |
hudakz | 1:1f2d9982fa8c | 48 | * BCM2835_PWM_PERIOD_3_US -> 3.33 us = 300.00 kHz |
hudakz | 1:1f2d9982fa8c | 49 | * BCM2835_PWM_PERIOD_2_US -> 1.66 us = 600.00 kHz |
hudakz | 1:1f2d9982fa8c | 50 | * BCM2835_PWM_PERIOD_833_NS -> 833.33 ns = 1200.00 kHz |
hudakz | 1:1f2d9982fa8c | 51 | * BCM2835_PWM_PERIOD_417_NS -> 416.66 ns = 2400.00 kHz |
hudakz | 1:1f2d9982fa8c | 52 | * BCM2835_PWM_PERIOD_208_NS -> 208.33 ns = 4800.00 kHz |
hudakz | 1:1f2d9982fa8c | 53 | * BCM2835_PWM_PERIOD_104_NS -> 104.16 ns = 9600.00 kHz |
hudakz | 1:1f2d9982fa8c | 54 | */ |
hudakz | 1:1f2d9982fa8c | 55 | void period_ms(int period); |
hudakz | 1:1f2d9982fa8c | 56 | void period_us(int period); |
hudakz | 1:1f2d9982fa8c | 57 | void period_ns(int period); |
hudakz | 1:1f2d9982fa8c | 58 | |
hudakz | 1:1f2d9982fa8c | 59 | /** A operator shorthand for write() |
hudakz | 1:1f2d9982fa8c | 60 | * \sa PwmOut::write() |
hudakz | 1:1f2d9982fa8c | 61 | */ |
hudakz | 1:1f2d9982fa8c | 62 | PwmOut &operator= (float value) |
hudakz | 1:1f2d9982fa8c | 63 | { |
hudakz | 1:1f2d9982fa8c | 64 | write(value); |
hudakz | 1:1f2d9982fa8c | 65 | return *this; |
hudakz | 1:1f2d9982fa8c | 66 | } |
hudakz | 1:1f2d9982fa8c | 67 | |
hudakz | 1:1f2d9982fa8c | 68 | /** A operator shorthand for write() |
hudakz | 1:1f2d9982fa8c | 69 | * \sa PwmOut::write() |
hudakz | 1:1f2d9982fa8c | 70 | */ |
hudakz | 1:1f2d9982fa8c | 71 | PwmOut &operator= (PwmOut &rhs) |
hudakz | 1:1f2d9982fa8c | 72 | { |
hudakz | 1:1f2d9982fa8c | 73 | write(rhs.read()); |
hudakz | 1:1f2d9982fa8c | 74 | return *this; |
hudakz | 1:1f2d9982fa8c | 75 | } |
hudakz | 1:1f2d9982fa8c | 76 | |
hudakz | 1:1f2d9982fa8c | 77 | /** An operator shorthand for read() |
hudakz | 1:1f2d9982fa8c | 78 | * \sa PwmOut::read() |
hudakz | 1:1f2d9982fa8c | 79 | */ |
hudakz | 1:1f2d9982fa8c | 80 | operator float() |
hudakz | 1:1f2d9982fa8c | 81 | { |
hudakz | 1:1f2d9982fa8c | 82 | return read(); |
hudakz | 1:1f2d9982fa8c | 83 | } |
hudakz | 1:1f2d9982fa8c | 84 | |
hudakz | 1:1f2d9982fa8c | 85 | protected: |
hudakz | 1:1f2d9982fa8c | 86 | PinName _pwmPin; |
hudakz | 1:1f2d9982fa8c | 87 | float _duty_cycle; |
hudakz | 1:1f2d9982fa8c | 88 | uint32_t _range; |
hudakz | 1:1f2d9982fa8c | 89 | uint32_t _period; |
hudakz | 1:1f2d9982fa8c | 90 | }; |
hudakz | 1:1f2d9982fa8c | 91 | |
hudakz | 1:1f2d9982fa8c | 92 | #endif // _PWM_OUT_H_ |
hudakz | 1:1f2d9982fa8c | 93 |