Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

ModusToolbox Blog

mattl_01
Employee
25 likes received 10 likes received 10 likes given
Employee

A WiFi Scanner using the Mbed->ModusToolbox flow

Hello again Embedded Developers,

In this blog post I’ll introduce you to using the Mbed flow with ModusToolbox. The example design will scan for WiFi networks in range, print them to a tutorial, then connect to a WiFi network.  This article will cover

I. Setting up your environment

a. Install ModusToolbox 1.1

b. Install Cygwin (for Windows)

c. Install SRecord

d. Install/Configure the Mbed Command Line Interface (CLI)

e. Update PyOCD

f. Setup your Windows environment

g. Setup your compiler

II. Importing an example WiFi Scanner application from Mbed

III. Compiling with the Mbed CLI, targetting a Cypress board

IV. Flashing the design using the Mbed CLI or from the ModusToobox IDE

V. Importing the design into the ModusToolbox IDE for further editing, programming, and debugging.

One key document to have handy is Chapter 3 of the ModusToolbox IDE User Guide (Mbed OS to ModusToolbox Flow)  You can find this document in the installation ($install/docs/mt_ide_user_guide.pdf) or on the website (the 1.1 version is here).  Now is a good time to mention that this example is for ModusToolbox 1.1.

I. Environment setup

Install ModusToolbox

If you don’t already have ModusToolbox 1.1 installed, install it now.

Install Cygwin Make (Windows)

You need Cygwin Make for the Cypress Mbed flow to work properly in a Windows version of Eclipse. Refer to the Cygwin website for instructions to download and install Cygwin. Do not add Cygwin to your Windows PATH system environment variable. Cygwin is only required for Eclipse.

Install SRecord

You must install the SRecord utility (https://sourceforge.net/projects/srecord/) which is used by the ModusToolbox IDE build process to produce a combined HEX file for program/debug.

Install/Configure the Mbed CLI (you won't be able to do anything past here without completing this step)

Go here and follow the instructions for your platform (I’ll be using Windows for this post)

https://os.mbed.com/docs/mbed-os/v5.11/tools/installation-and-setup.html

If you want to see the including supported boards and code examples, visit the Cypress page on the Mbed website.  It’s here.

https://os.mbed.com/teams/Cypress/

If necessary, Update PyOCD

In order for Mbed applications to work with ModusToolbox and Cypress kits, PyOCD 0.16.1 or greater is required.  Run the following from a cmd window.

pyocd --version

Again, the reply should be 0.16.1 or greater.

If you don’t have the correct version, run:

pip install -U pyocd

I used 0.18.0 for this blog post.

Set up your Windows environment

You need an environment variable called CYSDK that points to the ModusToolbox installation. In my case, I have the following two lines in my  .bash_profile (I’m using a Git Bash shell for this example.

export CYSDK="C:/Users/mdl/ModusToolbox_1.1" (or in a plain Windows cmd window, set CYSDK=C:/Users/mdl/ModusToolbox_1.1)

Set up your compiler

While there are many compilers you can use with Mbed (e.g. ARM and IAR), I will just cover the one I am using; the GCC distribution shipped with ModusToolbox.  This is done with an Mbed command.  You must do this with an Mbed command.  It doesn’t look at an environment variables you have set up in Windows.  Here is the command I used.

mbed config -G GCC_ARM_PATH /c/users/mdl/ModusToolbox_1.1/tools/gcc-7.2.1-1.0/bin

You should see a confirmation message that looks similar to

[mbed] C:/users/mdl/ModusToolbox_1.1/tools/gcc-7.2.1-1.0/bin now set as global GCC_ARM_PATH

(if you are using a plain Windows cmd window, the mbed config command would be something like

mbed config -G GCC_ARM_PATH C:\users\mdl\ModusToolbox_1.1\tools\gcc-7.2.1-1.0\bin )

II. Go get an example from the Mbed repository

1. Create a directory for you example and navigate to it.

2. Get an example using the mbed command “mbed import [example name]”.  Here is the command I ran from the command line

mbed import mbed-os-example-wifi

This many take a bit of time to complete based on net work traffic because there is a lot of stuff in this example.  You can add the “-v” option to the end of the command to get a little more status.

Pro Tip:This is actually a pretty hairy example for your very first Mbed design. You may want to start with the simple Mbed blinking LED example. That command would look like

mbed import mbed-os-example-blinky

III. Compile the example

You could wait to do this step after importing into the ModusToolbox IDE, but I like to make sure I have the basic example compiling before importing.

1. Change directory to the example you just imported. In my case:

cd mbed-os-example-wifi

2. Explore and review the contents of the directory

Some of the key files are a README file, an mbed-os directory (that is responsible for most of the disk space consumption) and a C++ source file, main.cpp.

3. Compile the design with a Cypress board as the target

The format of the mbed compile command we will use is

mbed compile --target [Cypress Board] --toolchain [TOOLCHAIN]

where TOOLCHAIN = GCC_ARM and Cypress Board = CYW943012P6EVB_01

If you want to see all the compile options, you can type

mbed compile --help

If you want to see table of all available toolchains and targets, type

mbed toolchain --supported

Cypress targets at the time I created this blog post include:

CY8CKIT_062_BLE

CY8CKIT_062_WIFI_BT

CY8CKIT_062_4343W

CY8CPROTO_062_4343W

CYW943012P6EVB_01  (the board I am using)

4. Configure the example for your specific WiFi network

Now, we will set up the WiFi network we will connect to in this example.  Do this before running a build/compile.  In the project directory, you will see a file named mbed_app.json.  Replace SSID and PASSWORD as shown below with the SSID and password for your network.

pastedImage_0.png

Leave the quotation marks and backslashes exactly as shown.  Now, the actual compile command for my board will look like this.

mbed compile --target CYW943012P6EVB_01 --toolchain GCC_ARM

If I had a Cypress 062 WiFi-BT board, the command would look like.

mbed compile --target CY8CKIT_062_WIFI_BT--toolchain GCC_ARM

If I were using the IAR compiler, I’d replace GCC_ARM with IAR.

One quick note: At the time of this blog post, IAR 8 was not compatible with Mbed OS.

The first time you build this example, it will take a while.  The end of the compile will look something like the following.

pastedImage_1.png

DAPLink

Before you can program the board, you must put it in Arm Mbed DAPLink mode. Arm Mbed DAPLink is open-source software project that enables programming and debugging application software running on Arm Cortex CPUs. DAPLink runs on a secondary MCU (often a PSoC 5LP on Cypress boards) that is attached to the SWD or JTAG port of the application MCU.  It enumerates as a USB composite device and creates a bridge between your development computer and the CPU debug access port. More info can be found here.

You need to do a one-time load of the DAPLink firmware onto your board. The full instructions for installing DAPLink onto supported Cypress development boards can be found in Chapter 3 of the ModusToolbox IDE User Guide, but I will go ahead and list them for my CYW943012P6EVB board here.

1. Plug a USB cable into your computer.

2. Press and hold SW3 while plugging in the other end of the cable to your board. This puts the kit in firmware bootload mode.  Obviously use the USB port near the PSoC 5LP, not the USB for the device USB.  Just look on the screen printing for a string starting WITH CY8C5868.

3. From a command window, run the command

fw-loader --update-kp3

The fw-loader can be found in the ModusToolbox installation directory under tools/fw-loader-2.1/bin .  I put it in my path, but you can just go over to the directory and run it.

4. After the fw update is complete, you can put your kit into DAPLink mode by pressing and holding SW4 (Custom App) for 2 seconds are so.  You can tell you are successful by running the following in a command window

mbedls

The results of this command will look something like this

pastedImage_2.png

If you need to return the standard CMSIS-DAP HID/BULK mode, just press and hold the Custom App switch (SW4) for about 2 seconds again.

IV. Program the board

There are a couple of ways to program the board.

1. From the Mbed command line, with the --flash option.  Here’s the command for this blog post.

mbed compile --target CYW943012P6EVB_01 --toolchain GCC_ARM --flash

2. From the ModusToolbox IDE, which is a longer discussion. I will talk about this later.

About the WiFi Scanner Application

Now, I’ll take some time to talk about the application you have programmed onto the board.  This example

1. brings up the WiFi and underlying network interface

2. scans the available networks, and

3. prints network interface and connection details.

Connect to the mbed serial port using a terminal emulation program such as PuTTY (my buddy Keith says that for serial stuff in embedded design, RealTerm is better than PuTTY or TeraTerm).  The mbed serial port should look something like this in the Windows Device Manager.

pastedImage_3.png

If you don’t see something like that, it’s possible you are not in DAPLink mode. Windows 7 users may also need to do an additional step (which is described in Chapter 3 of the ModusToolbox IDE User Guide).

I set PuTTY up with a baud rate of 9600, 8 data bits, 1 stop bit, no parity, no flow control.  Once connected with PuTTY, I press the RST switch (SW5) on my board.  The results will look something like the following (note DAWG5 is the SSID in this case).

pastedImage_5.png

If you would like to play around with the source a bit (maybe you want to play with the output or take some commands from stdin), just open up the source in main.cpp using the ModusToolbox IDE or your favorite code editor. Also, explore source code in various directories under mbed-os. For this example, you may want to look at the code in mbed-os/features/netsocket/.  For example, the WiFiInterface class is defined in

mbed-os/features/netsocket/WiFiInteface.h

V. Importing into the ModusToolbox IDE

At any point after importing the example from Mbed, you can export to a number of different IDEs. For example, to export to an Eclipsed-based IDE, such as the ModusToolbox IDE, run a command of the following format.

1. mbed export --ide [IDE] --target [target]

For this example, use the following command

mbed export --ide eclipse_gcc_arm --target CYW943012P6EVB_01

mbed export --supported  will print a matrix of supported IDEs and targets.

2. Now open the ModusToolbox IDE and use the standard Eclipse Import function (File > Import) to import this Mbed application.  Here again, you may want to refer to Chapter 3 of the ModusToolbox IDE User Guide. After selecting File > Import, expand the C/C++ folder and select the Existing Code as Makefile Project option. Click Next >.

pastedImage_6.png

3. Click Browse… and navigate to the directory where you exported the application from Mbed. Ensure the Toolchain for Indexer Settings is set to <none>. Do not change any other settings, including the project name.

4. Click OK then Finish.

When the import finishes, the Mbed OS application will be shown in the Eclipse Project Explorer

pastedImage_7.png

Define the Workspace PyOCD Path

Your Eclipse IDE (in this example it’s the ModusToolbox IDE) will need to know how to get to PyOCD for programming and debugging.

Open the Window > Preferences dialog, select MCU > Workspace PyOCD Path, and set the following workspace paths (adjust the path to the Scripts directory for your python/pyocd installation):

pastedImage_8.png

Here are the settings for the various platforms.

Windows:

⦁ Workspace PyOCD Path > Executable = pyocd-gdbserver

⦁ Workspace PyOCD Path > Folder = C:\Python27\Scripts

macOS:

⦁ Workspace PyOCD Path > Executable = pyocd-gdbserver

⦁ Workspace PyOCD Path > Folder = /Applications/MBEDCLI.app/Contents/Resources/miniconda/bin

Linux:

⦁ Workspace PyOCD Path > Executable = pyocd-gdbserver

⦁ Workspace PyOCD Path > Folder = ~/.local/bin

Create the PATH variable

Right-click on the project and select Properties. Navigate to C/C++ Build > Environment.

pastedImage_9.png

Click Add to add a new variable with name PATH, leave the value blank, and select "Add to all configurations."

Click OK to close the New Variable dialog. This step is recommended for Windows to clean up any existing value of the system PATH variable in the current project (this ensures that the project configuration is isolated from the host environment).

Then, click Edit to open the dialog again. Remove anything there, and enter the appropriate value:

⦁ Windows: C:/cygwin64/bin;${cy_tools_path:gcc-7.2.1}/bin

⦁ macOS: ${cy_sdk_install_dir}/tools/gcc-7.2.1-1.0/bin:/usr/bin:/bin:/Applications/MBEDCLI.app/Contents/Resources/miniconda/bin:/Applications/MBEDCLI.app/Contents/Resources/bin:/Applications/MBEDCLI.app/Contents/Resources/git/bin:/usr/local/bin

Also add /usr/local/Cellar/srecord/1.64/bin to /etc/paths

⦁ Linux: Check the gcc directory path: /usr/bin:/bin:${cy_sdk_install_dir}/tools/gcc-7.2.1-1.0/bin

Note: It is possible to target another toolchain instead of GCC 7.2.1 provided by ModusToolbox. To use the GNU Arm Embedded Toolchain, install the following to the default location:

https://developer.arm.com/open-source/gnu-toolchain/gnu-rm/downloads

Then prepend the following to the Eclipse PATH instead of ${cy_tools_path:gcc-7.2.1}/bin:

<install_dir>/GNU Tools ARM Embedded/7 2018-q2-update/bin

Build, Program, and Debug the Application from the ModusToolbox IDE

Build

If you haven’t recently built the application, select Project > Build to build the project and generate the combined HEX file for programming. When complete, the Console displays a message similar to the following:

hex file ready to flash: BUILD/mbed-os-example-wifi-combined.hex

Program

Open the Run > Run Configurations dialog. Select the “program” configuration for your application under GDB PyOCD Debugging.

pastedImage_10.png

On the Main tab, under C/C++ Application, click Browse… to change the specified target from the ELF file to the combined HEX file. Click Apply

pastedImage_11.png

On the Startup tab, make sure the Load symbols option is using the ELF file. If not, select the [application].elf file.

Click Run (the red text in the console is normal for PyOCD). It will look something like this.

pastedImage_12.png

After programming, you can reconnect with PuTTY as described earlier.  Press the kit reset button to start the program from the beginning.

Debug

Open the Run > Debug Configurations dialog and select the “debug” configuration for your application under GDB PyOCD Debugging. Repeat the same process described earlier for changing the configuration under programming. Click Debug.

The IDE will switch to debugging mode and will halt at the break, ready for debugging.  The screen will look something like this.

pastedImage_13.png

Note: While debugging, you may see various errors in your code (main.cpp file), such as “function ‘printf’ could not be resolved.” These are likely not real errors. They are caused by the import of the make file into Eclipse. To turn these errors off, go to Project > Properties > C/C++ > Code Analysis, and disable “Potential Programming Problems” and “Syntax and Semantic Errors.”

pastedImage_14.png

That’s it for this example.  There’s a ton of stuff it can do, so play around with it a bit. As an advanced exercise modify it to prompt for an SSID and Password.

One reminder, if you are having trouble with this example, get the mbed-os-example-blinky example working first.  Hit me up in the comments if you have questions.

--Matt

P.S. I soooo wanted to title this post, A Scanner Darkly​​ MichaelF_56 dared me, but MarkS_11 wouldn't let me (Any Philip K. Dick / Rickard Linklater fans out there?)

4 Comments
hemic_330866
Level 3
10 likes given 5 likes given First like received
Level 3

Dear Matt,
to get with any of the examples from mbed-cli to ModusToolbox it is nessessary to switch the kitprog3 from daplink mode back to kitprog3 mode. This does not work for me (CY8CPROTO_062-4343W). Not by ModeSwitch, not by PSOCProgrammer, not by fw-updater. The SW3 does not switch the LED2 and the software tools will not see the kitprog3. The daplink mode semms to be very hard.

Can you help?

Thank you!

Helmut

0 Likes
mattl_01
Employee
25 likes received 10 likes received 10 likes given
Employee

Hey Helmut,

Thanks for the note. It is a little complicated until you get the hang of it.  Once you know how it works, it's easy.

Here is what you need to know in a nutshell.  First, I am assuming you have already loaded the DAPLink firmware.

If that is not the case, let me know.

For your kit, hold SW3 (top-left by KitProg3) for > ~2 seconds to toggle between these two modes:

Mode 1. Regular KP3 (CMSIS-DAP BULK or CMSIS-DAP HID) (Regular ModusToolbox flow)

Mode 2. DAPLink (Mbed)

When you are in Mode 1, you can toggle between BULK and HID with a short press of SW3.

If you are in Mode 2 (DAPLink), you have to press and hold SW3 > 2 seconds to get back to Mode 1.

That is, short duration presses of that switch will have no effect.  Here is how I tell what mode I am

(it's not very elegant, but it should work for you).

If I am in Mode 1, LED2 will be lit.  If it is "breathing", it's in HID.  Further, when I run fw-loader --device-list from the command, line, the output will look something like this.

pastedImage_3.png

Now, if I press SW3 again (short press, not press and hold), LED 2 will stop breathing and just be a

lit at a constant intensity. This is how I know I am in in BULK mode. 

fw-loader --device list will return something like this

pastedImage_2.png

Now, when I am in Mode 1 and want to go to Mode 2, I press and hold SW3 for > 2 seconds. When I release SW3, LED 2 will be unlit.  When I run fw-loader --device-list, the output doesn't tell me I am in DAPLink.  It returns something a little unhelpful like this

pastedImage_4.png

OK, that will hopefully get you unstuck.  Let me know if you have questions.  One other thing to note is that we have plans to make the DAPLink mode more "standard". That is, the boards will come with all the appropriate firmware. There will only be 1 mode, and you will just press the switch to toggle between HID, BULK, and DAPLink.  Don't hold me to that though. It's just planned for now.

--Matt

0 Likes
hemic_330866
Level 3
10 likes given 5 likes given First like received
Level 3

Dear Matt,
thanks for your quick reply. But as I described above and I communicated in my message to you: The mode switch (SW3 at KitProg3) does not work for me. I pressed it 1, 2, 3 times shortly or for 1, 2, 3, 5, 12 seconds and nothing happens. LED2 at KitProg3 stays off. Daplink-Drive in filebrowser stays connected. mbedls shows:

| platform_name   | platform_name_unique   | mount_point | serial_port | target_id                                    | daplink_version |

|---------------------|------------------------|-------------|-------------|--------------------------------------------------|-----------------|

| CY8CPROTO_062_4343W | CY8CPROTO_062_4343W[0] | H:      | COM7    | 19010000f30d0f1001f30d0f00000000000000002e127069 | 0251        |

fw-loader --device-list shows:

"Info: Start API initialization

Info: Hardware initialization complete (333 ms)

No connected devices"

PSoC-Programmer 3.28.2 shows:

Successfully Connected to CMSIS-DAP/240317 at 23:42:45    | CMSIS-DAP Version 1.10

and programming a hex-file after busy several seconds fails with:

                                                 | Programming Terminated

The problem is not t hat Inot know the suggestions. The problem is that the suggestions do not work for my board.

Where could be the error?

Thanks again!

Helmut

0 Likes
mattl_01
Employee
25 likes received 10 likes received 10 likes given
Employee

OK, I apologize for suggesting things you already knew about.  I probably need to run through everything again to see if I left anything out.  Unfortunately, I may not have time until Monday as I have a lot of non-work things to do.  dheeraj​, is this something you or your team could look at?

0 Likes
About the Author
IC / Software / Programmable Logic / Embedded / Customer Support background
Top labels