IoT Design with Cypress PSoC® 6 MCUs and Wi-Fi/Bluetooth using Arm® Mbed™
Your First Project & The Blinking Thread
WiFi & NTP Thread
Amazon AWS MQTT Thread - Part1
Amazon AWS MQTT Thread - Part2
You can “mbed import https://github.com/iotexpert/mouser-mbed-09.git“ to make a copy of the project in your workspace.
The final architecture of the thermostat looks like this.
Here we are again. The blinking LED. I always start with a basic Blinking LED project just to make sure that everything is working. What is everything? The whole tool flow, from the editor, to the programmer, to the chip to the SDK. The only thing that will be a little different than usual is that I will run the blinking LED in a thread by itself. This is useful for looking at the development kit and knowing that the RTOS is still running and the blinking LED thread is at least OK even if the rest of your program is trashed.
In each of the following lessons I will add on a new block (or two) into the architecture. The blocks colored “green” will be the ones that are done in that lesson. In future lessons the blocks that are blue will be from the previous lessons.
For all of these projects I will be using the Mbed Studio program to create, edit, build and program the development board.
To implement the blinking LED thread I will:
- Make a new Mbed OS Program from the blank template
- Create & code “blinkThread.h”
- Create & code “blinkThread.cpp”
- Update main.cpp
- Give a Tour of Targets
- Compile and Program
Make a new Mbed OS Program from the blank template
Start Mbed Studio. Then run “File->New Program…”
Mbed Studio will then give you the ability to start with a template project. To get started use “empty Mbed OS program”. For each of the projects I will give it a name that corresponds to the lesson number, in this case “mouser-mbed-02”. When you have all that selected click “Add Program”.
This will create a brand new project for you with Mbed OS setup and a blank main.cpp.
Create & code “blinkThread.h”
In each of the following lessons I will be creating different threads to perform the different pieces of system functionality. I will put each thread in a separate set of files with the name “functionThread.h” and “functionThread.cpp”. As I am sure everyone knows, the “dot h” file is supposed to be the public interface to the “dot cpp” file. In other words that file has all of the functions, objects and variables that other files are allowed to access.
To create a file you “right click” on the project and select “New File”
And then give the file a name. In this case it will be “blinkThread.h”.
This file will have the guards, to keep it from being included more than once, and just the function prototype for the thread. In all of the coming lessons I will name the thread “functionThread”. In this case the function is “blink” so the thread is called “blinkThread”
Create & code “blinkThread.cpp”
Now I am ready to write the actual function which acts as the blinking LED thread. Right click on the project and select “New File”
Then give it the name “blinkThread.cpp”. Just like the “dot h” files, the “dot cpp” files will be named “functionThread.cpp”. In this case the function is blink so the file will be called “blinkThread.cpp”.
This is a really simple thread. It wants to talk to the LED on the board. All Mbed OS development boards are required to have at least one LED and that LED must be named “LED1”. In reality the LED1 is just a map to the PSoC pin name of “P1_1” (this happens in the BSP/Target – more on this later).
By creating an object of type “DigitalOut” with an initializer argument of “LED1” I will have access to writing 1’s and 0’s to that pin. Notice that I give the object definition the keyword “static” to limits its scope to this file.
The next thing that I need is the actual function which serves as the thread. Notice that it is an infinite loop, which just does the following:
1. Inverts led1 by reading the pin, inverting it with the “!”, and then writing it back to the pin.
2. Doing a delay of 500 ms using the Mbed OS library function “sleep_for”. For those of you who aren’t CPP people the “ThisThread::” just tells the compiler that the sleep_for function is in the namespace “ThisThread”.
static DigitalOut led1(LED1);
led1 = !led1;
In each of the following lessons, each time I create a new file, I will be making the same basic change to main.cpp. Specifically I will startup the thread when the main function starts. To do this:
- Include the new “functionThread.h” (in this case “blinkThread.h”).
- Declare an object to hold the new thread (in this case “blinkThreadHandle”).
- Start the thread by calling the start method with a function pointer to the actual thread function (in this case “blinkThread”).
Mbed OS abstracts all information about a development board into a set of files called the Target. These files reside in the mbed-os/targets directory. This files are classically called the board support package (BSP). This including information about which chip, startup code, libraries, and peripherals exist on the board.
In order to build a project you need to tell Mbed OS which target you are using. This can be done in the Mbed CLI with “mbed config target CY8CPROTO_062_4343W” or by selecting the correct target in the Mbed Studio IDE.
You should look in the following directories/files:
- targets directory
- TARGET_Cypress directory
- One directory per Target
Compile and Program
There are several things that you should notice on the panel to the left of your code.
- The active program (this is a drop down menu with each project in your workspace).
- Which “target”. When you plug in the development kit, Mbed Studio will recognize that you have attached a target that it recognizes.
- The “hammer” button runs the Build process.
- The “play” button runs a “Build” then programs the development kit.
Press the “Play” button to Compile and Program. And with any luck you should have a blinking LED.
Notice that it compiles a bunch of different files… actually all of the files in mbed-os. This will take a while on a PC, but is much faster on a Mac or Linux. This full compile step only needs to happen the first time for a project – each subsequent time will only compile your changes.