Hands On
Building an Mbed Application¶
This will guide you through creating an Mbed application. We first will cover Mbed OS to read/write to peripherals like potentiometers, LEDs and LCD screens on the Mbed Application Shield.
Prerequisites¶
An Mbed account - sign up here
Required Hardware¶
- Mbed board (NUCLEO-F429ZI)
- Potentiometer or LM35DZ/LFT1
Setup¶
- Attach the potentiometer to your mbed board.
- Connect the board to your computer via the *OpenSDA* port.
- The board mounts as a mass-storage device (like a USB drive). Verify that you can see it (the drive name will be DAPLINK).
Making an mbed application¶
Import Blinky
[Repository '/teams/mbed-os-examples/code/mbed-os-example-blinky/' not found]
Printing on the Serial Console¶
We will add some code to print a message to a serial terminal on the host.
Open main.cpp
#include "mbed.h"
DigitalOut led1(LED1);
// main() runs in its own thread in the OS
int main() {
printf("Hello World!\r\n");
while (true) {
led1 = !led1;
wait(0.5);
}
}
Using terminal applications¶
Terminal applications run on your host PC. They provide a window where your Mbed board can print and where you can type characters back to your board.
Serial configuration: The standard setup for the USB serial port is 9600 baud, 8 bits, 1 stop bit, no parity (9600-8-N-1)
Installing an application for Windows¶
There are many terminal applications for Windows, including:
- CoolTerm - this is the application we use in this example. We use it often because it usually "just works".
- Tera Term.
- PuTTY.
- Some Windows PCs come with Hyperterminal installed.
Configuring the connection¶
- Plug in your Mbed board.
- Open CoolTerm.
- Click Connect. This opens up an 8-n-1 9600 baud connection to the first available serial port. If you have more than one board plugged in, you may need to change the port under Options > Serial Port > Port.
Check your connection parameters:
- Select Options > Serial Port.
- You should see 9600 baud, 8 bits, 1 stop bit, no parity (9600-8-N-1).
- If you do not see your board, click Re-Scan Peripherals.
Your terminal program is now configured and connected.
Using terminal applications on Linux¶
CoolTerm should work under Linux. If for some reason it doesn't, you can try one of the following:
Compile and run¶
- Select a target board in the upper right hand corner
- Press "Compile"
- Wait for a binary to be downloaded
- Drag the binary to the DAPLINK disk
- Press the board's reset button
- "Hello World!" should show on the serial terminal.
Reading the potentiometer¶
We will add some code to read the value of the potentiometer every 100 ms and, if the measured value is not equal to the last one recorded, print the value to the serial terminal.
Import Project Template
Import programIPI_Singapore_Workshop
Reading the potentiometer
Open main.cpp
Instansiate an AnalogIn object to read the potentiometer and an EventQueue to schedule reading the potentiometer. Under 'GLOBAL VARIABLES HERE' add the following code:
AnalogIn pot1(A0); EventQueue queue;
Add a function that reads the potentiometer and prints the value to the serial terminal. Under 'FUNCTION DEFINITIONS HERE' add the following code:
void read_potentiometer() {
static float potentiometer_val = 0;
if ((float)pot1 != potentiometer_val) {
potentiometer_val = (float)pot1;
printf("Analog Reading: %.2f\r\n", potentiometer_val);
}
}
Use the event queue to schedule reading the potentiometer every 100 ms. Replace the main function with the following code:
int main(){
queue.call_every(100, read_potentiometer);
while(1){
wait_ms(100);
queue.dispatch(0);
}
}
Compile and run¶
- Select a target board in the upper right hand corner
- Press "Compile"
- Wait for a binary to be downloaded
- Drag the binary to the DAPLINK disk
- Press the board's reset button
- The value of the potentiometer should be shown on serial terminal. - Twist the potentiometer back and forth to see the value update
Publishing changes¶
- Right click the project folder
- Select "Publish"
- Write a commit message in the "Revision Commit" menu
- Press OK
- Select "Fork..." in the "Publish Repository" menu
- Write a Description
- Press OK
- Make a note of the URL for your fork. We will use it to locally clone this repository.
The Mbed CLI workflow¶
Prerequisites: Installing Mbed CLI and a toolchain¶
Mbed CLI is an offline tool, meaning you'll have to install it before you can work. You will also need to install a toolchain. Roughly this process is as follows:
- Install Git - version 1.9.5 or later.
- Install Mercurial - version 2.2.2 or later.
- Install Python Version 2.7.11+ and make sure pip is installed.
- Install a compiler toolchain: Arm GCC, Arm Compiler 5, IAR.
- Install
mbed-cliusing pip:pip install mbed-cli
If you are having trouble, please follow the detailed installation instructions on the Mbed CLI setup page, and come back here when you're done.
Getting the demo code¶
Mbed CLI can import our demo, along with the Arm Mbed OS codebase. The import process creates a new directory as a subdirectory of your current working directory.
To import the demo, from the command-line:
- (optional) Navigate to a directory of your choice
- Import the example:
mbed import REPO_URL: Please replace REPO_URL with your fork of IPI_Singapore_Workshop
cd IPI_Singapore_Workshop
Compiling¶
Tip: You can find the name of your currently connected target by running mbed detect
Invoke mbed compile, specifying:
- Your board:
-m <board_name>. - Your toolchain:
-t <GCC_ARM, ARM, ARMC6 or IAR>. For example, for the board NUCLEO F429ZI and the ARM GCC compiler:
mbed compile -m NUCLEO_F429ZI -t GCC_ARM
The program file, IPI_Singapore_Workshop.bin, is under your ./BUILD/NUCLEO_F429ZI/GCC_ARM/ folder.
Programming your board¶
Arm Mbed Enabled boards are programmable by drag and drop over a USB connection:
- Connect your mbed board to the computer over USB.
- Copy the binary file to the board. In the example above, the file is
IPI_Singapore_Workshop.bin, is under your./BUILD/NUCLEO_F429ZI/GCC_ARM/folder. - Press the reset button to start the program.
Publishing changes¶
- Right click the project folder
- Select "Publish"
- Write a commit message in the "Revision Commit" menu
- Press OK
- Select "Fork..." in the "Publish Repository" menu
- Write a Description
- Press OK
- Make a note of the URL for your fork. We will use it to locally clone this repository.
