Sunday, May 6, 2018

Programming the Lattice Semiconductor FPGA iCE40 Ultra Plus Breakout Board iCE40UP5K-B-EVN

FPGA programming the Lattice Semiconductor iCE40 Ultra Plus Breakout Board.

(work in progress, come back soon)

Lattice Semiconductor tweeted a "new low price" for their iCE40UP5k-SG48 breakout that I simply could not resist. I've been wanting to learn FPGA programming for some time, and had recently taken the Altera Cyclone IV for a test drive. I was not super impressed with the Quartus software, so I was interested in seeing how well the Lattice Diamond software worked in comparison.

Lattice iCE40UP board diagram from the User Guide

I initially tried to order the board from DigiKey. I gave up fussing with their dreadful ordering UI/UX and instead I ordered it from Mouser. (specifically this iCE40UP5K-B-EVN item on the Mouser web site). The device arrived about a week later.

There was a (clearly hand-cut) quarter sheet of paper with instructions included with the board:

Mainly of interest are the links:


.. and the reminder to put the jumpers on J6 in the "vertical position" (perpendicular to the jumper at J7)

Despite the seemingly simply instructions, my initial out of the box experience was dreadful. Not only was the computer completely unable to connect to the board for the demo, but later once I was able to connect without the error, the demo didn't even work. I don't think my board shipped with the demo program installed, nor was I able to find the source code anywhere to try it.

The first problem seems to have been the fact that I already had installed FTDI drivers for my ESP32 development some time ago (specifically the rather rare JESP32). This was apparent when *both* FTDI devices were listed at the bottom of device manager, and when looking at properties - both were configured as JTAG devices:

Looking back, I suppose this makes sense, as the JESP32 that was previously installed has the normal order of the devices reversed:
"Yeah. I used FT2232H interface 0 for UART and interface 1 for JTAG. This is different from many other debuggers. Advantages being you get same /dev/ttyUSB0 UART like many other dev boards. Also (which I heard) you don’t need to modify kext under OSX" - @ba0sh1
Sadly when the Lattice iCEcube2 software installs, it does not configure the ports properly. I ended up manually removing the drivers, then manually installing them. I'm pretty sure when I revisit my JESP32, it will not work.

I was unable to find any useful sample code for the iCE40UP5K. I eventually found this:

I was unable to program from the Lattice iCEcube2 software, as there was no "Tool - Program" on my menu!

I needed to download the "Standalone Diamond Programmer" from here:

Don't bother using the site search to find this! Seemingly every software item *except* the one you want will be listed in the hundreds of results. (yes, I mistakenly tried installing an older version, and the iCE40UP is not listed as a device). The version that worked for me was (I manually installed this one, different than the one that came with Lattice Diamond install) Specifically this link:

Programmer Standalone 3.10 64-bit for Windows

From the iCECube2 output, once a project is successfully compiled, the resulting file can be found in the log output near the end (see above). In my case, the bin file was found here:


Note the annoying, sloppy use of forward and back slashes. They will need to be edited in Windows, otherwise an error occurs when pasting in the path.

Windows only likes back-slashes, so each will need to be individually changed. In my case, the default project ended up in:


And the file needed (mysteriously) is prefixed with "top_". Select that bin file in the Diamond Programmer by clicking on the little 3 dot ellipsis button:

It should pop up a dialog box like this:

Note the device family is set to iCE40 Ultra Plus and the Device is iCE40UP5K.

Double-click on the "Operation" column (or click the "Device Properties" button) and ensure these settings are in place:

I saved my working project to GitHub here:

Once working, when viewing with Zadig, this is what I see (note Interface 0 and Interface 1):

However programming is not always successful, even if the software finds the device:

INFO - Check XCF Project: The current XCF Project is valid.
INFO - Check XCF Project: The current XCF Project is valid.
INFO - Check configuration setup: Start.

INFO - Check configuration setup: Successful (Ignored JTAG Connection Checking).

INFO - Device1 iCE40UP5K: Fast Program

 "Device detection failed. Cannot continue."

ERROR - Process Operation Failed.

INFO - Elapsed time: 00 min : 00 sec

ERROR - Operation: unsuccessful.

ERROR - Programming failed.

If the programming fails like this, try switching ports. One one computer (one that I had not installed JESP32 FTDI drivers)... I was able to program on the default "Port 0" (FTUSB-0) - on another computer (the one I previously had configured FTDI drivers, I had to change to "Port 1" (FTUSB-1) in order to program.

In the end I was eventually successful, but I don't think the iCE40UP5K is for everyone.

Sunday, April 29, 2018

NXP freescale FRDM-KL25Z first impressions

I found an inexpensive C/TCP-IP, the Embedded Protocol Stack for the Kinetis Arm Cortex-M4 book on amazon, somehow listed for sale at 90% off! ... along with a used used NXP freescale FRDM-KL25Z on ebay similar to this one. The initial Getting Started at was initially pretty impressive. But then I encountered the hassle of "you need to register and give up a bunch of PII just so you can download our software to see if you even want to use our product".

So ok, after a bit of anti-climactic download adventures, the download completed. Apparently the software have not been updated in the last couple of years. Either it is really good and stable, but free with no security issues... or is is not a high priority.

So another underwhelming "feature" of the software... once install is complete, there's a disappointment when there's no "new items" in Windows 10, nor under "F" for freescale, nor "K" for Kinetis, nor "N" for NXP. You'll need to remember that it was installed in C:\Freescale. Nothing obvious to get going on your own. No executable, no README. Well, that's apparently because there's yet ANOTHER download for the development environment. Why don't they just bundle all this into a single download?

So I next installed the "Kinetis SDK 1.3.0 Mainline - Windows.exe" - another 300MB compressed file that went... I don't know where. But no IDE icon, and no new apps immediately visible. The real install is kinetis-design-studio_3.2.0.exe - it really should simply have check boxes to install the other options, and the executable should be simply called something like kinetis-setup.exe There are install options for other things, why not for the SDK and "mainline" things?

This initial user experience makes me wonder if their software is also similarly, needlessly arduous.

Even after install of the new software is complete, the walk-through instructions note that "Help - Check for Updates" is needed. Sure enough, even more downloads an more manual steps. In fact, it is even several steps needed to actually accept the (new?) terms and conditions, next, next, next to install.

Go away and come back after install is complete? No, of course not:

There's a warning about "unsigned content". Super. The funny thing is that in the online video, no sooner did my updates complete, but the video instructs to ONLY select the processor updates. Oops. too late.

OMG - but we're not done! Seriously? There's Help - Install New Software

The "Update" for me is from 2015.

The for importing a new project, you just need to know project import:




I gave up at this point..

Sunday, April 15, 2018

Single Step JTAG debugging an ESP32 Arduino Sketch with VisualGDB

In my last post, I took the latest Preview 1 version of VisualGDB for a test drive. This time around, I am looking at the next increment: Preview 2. Spoiler from the the blog title: it is pretty cool.

TLDR; This is for JTAG debug ESP32 VisualGDB 5.4 Preview 2. This will not work with Preview 1. Preview 3 is now available.

Key is to BOTH: (1) add directories as "Additional Include Directories" and (2) right-click on solution "Add existing files" for these directories:


This Visual Studio / VisualGDB Arduino project can be downloaded here:

(Update 4/28: some final editing still in progress)

First, the Arduino "sketch" is an odd critter, so close to being C/C++ but just enough different that some people have even called Arduino a language of its own. In the Ardunio IDE, File-New creates something like this (note no "main"):

void setup() {
  // put your setup code here, to run once:


void loop() {
  // put your main code here, to run repeatedly:


The big drawback in the Arduino IDE is there's no debug. (besides - it is really just a text editor, not really an Integrated Development Environment, in my opinion).

Debugging a sketch in Visual Studio takes a few steps. The first part here is based on the Switching Advanced ESP-IDF Projects Between Different IDF Versions and Creating Advanced ESP32 Projects with ESP-IDF tutorials. I should also add that Arduino debugging is not yet officially by VisualGDB.

Note that I am using the PREVIEW 2 version of VisualGDB 5.4 with Visual Studio 2017..

First, we'll need to create a project in Visual Studio with VisualGDB. This example is called myArduinoConversion:

Leave default at "Create a new project based on a sample project". (hopefully the sysprogs folks will fix the black-on-dark-gray color scheme for those of of that choose the dark theme in Visual Studio):

If you don't have the ESP-IDF installed, there will be a prompt:

In my case, there was a version error. (despite the version warning, my C:\SysGCC\esp32\esp-idf\master-VisualGDB didn't even exist!

Click Clone an ESP-IDF from GitHub link to download:

VisualGDB may give an error:

So I manually downloaded the toolchain. Unfortunately the error message does now show the actual command being attempted:

mkdir C:\SysGCC\esp32\esp-idf\master
cd C:\SysGCC\esp32\esp-idf\master
git clone --recursive
If you had to download your own manually, VisualGDB will not find it, even if in the specified directory. Choose the option on the right "Locate and existing ESP-IDF checkout. I purposely keep the VisualGDB stuff in this directory, as I have another ESP-IDF in my:
... that is used for other Arduino / VisualMicro projects. Visual GDB will me looking for the file in the root of the clone project. with the above command, I would need to use the ESP-IDF checkout here:


Next, ensure the proper ESP32 toolchain is selected:

Next, choose a sample project. Here we use the get-started / blink template:

Next is the JTAG driver. In my case, I am using the ESP32-WROVER-KIT V3, and I chose the interface/ftdi/esp32-devkitj_v1,cfg file:

Here we can optionally press the "test" button, for a result like this if everything is working properly:

Although there's a "next" button there, it does not do anything; click "finish".

WAIT. (yes, it takes a surprisingly long time) It appears nothing is happening. I think most of the time is spent setting up the project ESP-IDF container.  Eventually a new "blink.c" file shows up:

/* Blink Example

   This example code is in the Public Domain (or CC0 licensed, at your option.)

   Unless required by applicable law or agreed to in writing, this
   software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
   CONDITIONS OF ANY KIND, either express or implied.
#include <stdio.h&gt
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/gpio.h"
#include "sdkconfig.h"

/* Can run 'make menuconfig' to choose the GPIO to blink,
   or you can edit the following line and set a number here.

void blink_task(void *pvParameter)
    /* Configure the IOMUX register for pad BLINK_GPIO (some pads are
       muxed to GPIO on reset already, but some default to other
       functions and need to be switched to GPIO. Consult the
       Technical Reference for a list of pads and their default
    /* Set the GPIO as a push/pull output */
    gpio_set_direction(BLINK_GPIO, GPIO_MODE_OUTPUT);
    while(1) {
        /* Blink off (output low) */
        gpio_set_level(BLINK_GPIO, 0);
        vTaskDelay(1000 / portTICK_PERIOD_MS);
        /* Blink on (output high) */
        gpio_set_level(BLINK_GPIO, 1);
        vTaskDelay(1000 / portTICK_PERIOD_MS);

void app_main()
    xTaskCreate(&blink_task, "blink_task", configMINIMAL_STACK_SIZE, NULL, 5, NULL);

The first thing we'll need to do is take that sketch_mmmdd.ino file and rename it to something like main.cpp and make a few other changes.  (hopefully in a future release, sysprogs will give the option of using C or C++ sample templates).

In the case of this blink app, BLINK_GPIO is simply defined as "2" with no type. The gpio_set_level expects a gpio_num_t like this:
  gpio_set_level(static_cast<gpio_num_t>(BLINK_GPIO), 0);

The blink.c will also need to be renamed blink.cpp (apparently to ensure the compiler knows we are doing C++ and not just C). Preview 2 does not support right-click to rename, but you can click on the file in Solution Explorer and press F2 to enable the rename.

Only other minor tweak is needed; add extern "C" before the void main() like this:
extern "C" void app_main()

The revised code now looks like this:

/* Blink Example

   This example code is in the Public Domain (or CC0 licensed, at your option.)

   Unless required by applicable law or agreed to in writing, this
   software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
   CONDITIONS OF ANY KIND, either express or implied.
#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/gpio.h"
#include "sdkconfig.h"

/* Can run 'make menuconfig' to choose the GPIO to blink,
   or you can edit the following line and set a number here.
#define BLINK_GPIO 2

void blink_task(void *pvParameter)
    /* Configure the IOMUX register for pad BLINK_GPIO (some pads are
       muxed to GPIO on reset already, but some default to other
       functions and need to be switched to GPIO. Consult the
       Technical Reference for a list of pads and their default
    /* Set the GPIO as a push/pull output */
 gpio_set_direction(static_cast<gpio_num_t>(BLINK_GPIO), GPIO_MODE_OUTPUT);
    while(1) {
        /* Blink off (output low) */
     gpio_set_level(static_cast<gpio_num_t>(BLINK_GPIO), 0);
        vTaskDelay(1000 / portTICK_PERIOD_MS);
        /* Blink on (output high) */
     gpio_set_level(static_cast<gpio_num_t>(BLINK_GPIO), 1);
        vTaskDelay(1000 / portTICK_PERIOD_MS);

extern "C" void app_main()
    xTaskCreate(&blink_task, "blink_task", configMINIMAL_STACK_SIZE, NULL, 5, NULL);

So that all is your basic VisualGDB project. Not a whole lot new. Yet.

I've been tinkering with LoRa stuff, and being able to single-step debug the code will be helpful. I'm using the RadioHead library and my local copy is git-cloned into \Documents\libraries\RadioHead. The interesting thing here is that the library is what I call "Arduino-style". I've never been able to get VisualGDB to play well with them, so I do a lot of development with VisualMicro instead. It is less expensive and fully supports the Arduino libraries. Alas I have a tons of "if debugging serial print" statements, as the JTAGsupport is weak. (there is however a VisualMicro GDB tutorial I've been meaning to check out). The reality is the sysprogs VisualGDB is simply a more extensive and robust implementation with JTAG support (with a price to show for it). The problem with the sysprogs folks however, is they never had much of an interest in Arduino.  (Yes, I want my cake and JTAG it too!)

So a single include statement turns out project into something considerably more interesting.

#include "RH_RF95.h"

Actually we'll do a few more things to instantiate the RadioHead drivers:

#include "RH_RF95.h"

#define RFM95_CS 5   // LORA_CS_PIN
#define RFM95_RST 36 // LORA_RST_PIN is 36, TODO but it is read-only! so we'll need to short to another pin
#define RFM95_INT 26 // M5 LORA_IRQ_PIN 36 (jumper to 16)
RH_RF95 rf95(RFM95_CS, RFM95_INT);

Ok, so the code is actually from my M5Stack project, and I don't yet have LoRa hooked up to my lastest ESP32-WROVER, but that's beside the point...

After adding the code, the IDE will complain:

So simply go into Project-Properties and add the path the the "Additional Include Directories". In my case that's:


NOTE: If your cursor is in the source code panel when you click Project-Properties, you'll get this modal dialog box. It is NOT the one to use to enter paths for include files (it didn't work for me):

Be sure to click on the project name in the solution explorer, and THEN click Project - Properties:

...for this dialog box to enter the Additional Include File Directory paths:

(note I like to make a habit of always pressing "Apply" before pressing ok, anytime that option is available)

That will take care of finding the code, but trying to compile will give an error about platform not defined:

The first compile will take some time. There's a shockingly large amount of code that gets compiled for such a tiny target. If when building/cleaning/rebuilding, nothing happens, simply exit Visual Studio and relaunch (see below).

This is another one of those Arduino-style nuances. You'll notice this code in RadioHead.h header and shown here:

 #if (MPIDE>=150 && defined(ARDUINO))
  // Using ChipKIT Core on Arduino IDE
 #elif defined(MPIDE)
  // Uno32 under old MPIDE, which has been discontinued:
#elif defined(NRF51)
#elif defined(NRF52)
 #elif defined(ESP8266)
 #elif defined(ESP32)
 #elif defined(ARDUINO)
 #elif defined(__MSP430G2452__) || defined(__MSP430G2553__)
 #elif defined(MCU_STM32F103RE)
 #elif defined(STM32F2XX)
 #elif defined(USE_STDPERIPH_DRIVER)
 #elif defined(RASPBERRY_PI)
#elif defined(__unix__) // Linux
#elif defined(__APPLE__) // OSX
  #error Platform not defined!  

#if defined(__AVR_ATtiny84__) || defined(__AVR_ATtiny85__) || defined(__AVR_ATtiny24__) || defined(__AVR_ATtiny44__) || defined(__AVR_ATtiny45__) || defined(__AVR_ATtinyX4__) || defined(__AVR_ATtinyX5__) || defined(__AVR_ATtiny2313__) || defined(__AVR_ATtiny4313__) || defined(__AVR_ATtinyX313__)

To fix, simply add "ESP32" as a Preprocessor definition:

Rebuild again. There's a subtle change in the error message:

So this starts looking a bit more intimidating: Arduino.h missing for our ESP32 project. I took the brute-force approach and searched from the root of my C:\ drive:

dir Arduino.h /s

I had a 32 matches in various locations. Some are obviously not of interest if found in an .\Arduino\ directory. In my case, I have the Espressif Arduino Core git-cloned in my .\Documents\Hardware\Espressif directory. It can be installed like this:

cd %USERPROFILE%\documents
mkdir hardware
git clone --recursive
cd arduino-esp32
dir Arduino.h /s
dir SPI.h /s
dir pins_arduino.h /s

Put that directory in the include file list. Compile again and find another missing header file. Repeat. Note the pins_arduino.h is found in multiple directories. I chose the generic ESP32 one. After doing this a few times, VisualGDB often will complain the the settings are corrupt:

I simply exited Visual Studio and restarted. Upon restart, it complained out "line endings not being consistent". VisualGDB probably did this to itself; simple answer yes to fix:

Upon doing a rebuild, I found more files missing. Repeat the procedure for finding files.

Mine were found in:


May also need:

Note that the include file locations is not the only place for this fix; The intellisense will be happy, however the compiler does not seem to "find" there files. There is where things get a little wonky. Those same include files paths need to be added to the project. Right-click on the project, add, existing item (for each of the directories listed above).

Paste one of those include file paths into the File Name box. I choose to select header files (some directories have only headers!). Pick one of them. VisualGDB will recognize that there are others and prompt you to add them all. Click ok.

If multiple files are found, there will be a prompt asking if they all should be added (yes, they should):

Rebuild the project. (sometimes it can help to clean, and then do a full rebuild).

Tada! A fully compiled Arduino-style library with VisualGDB. For me - this was really quite cool: something I've been wanting to do for a long time.

There are still a few weird things. The include files are presented in the Solution Explorer in a pretty bizarre fashion. As these same include directories are found in the Project Properties, I really think it would be best of the directory scanning and file additions happened transparently to the developer.

Also, It seems that breakpoint cannot be placed directly on those included files. However you can step-into them. Unfortunately the only option is step into. Step-over does not seem to work. Nor does step out. Alas this is only Preview 2, and still in development.

The Solution Explorer also looks a bit odd:

There's also some wonkiness with OpenOCD when code is paused too long. I think there's some sort of panic watchdog that is not fed while single-step debug is paused:

And no sooner do I polish this Preview 2 blog... the sysprogs folks have already released Preview 3! (And yes, it DOES appear to work in Preview 3 as well!)

Saturday, March 31, 2018

VisualGDB 5.4 Preview 1 with support for Advanced ESP-IDF

Recently there was an exciting announcement from the clever folks at sysprogs: Support for the Espressif ESP32 is finally coming to VisualGDB!! (specifically the preview Try It Now link)

In some previous posts, I attempted to get ESP32 JTAG debugging working in Visual Studio with the VisualGDB add-in. I later went on to try out the amazing Visual Studio Code JTAG debugging. In the end it was relatively complex and clumsy. I ended up returning to VisualMicro coding in Visual Studio. Why? Well, simply put there are way more open source libraries that follow the "Arduino Style" libraries. I was never able to get them to work in a VisualGDB project. As of last year, it didn't seem to be very high on the priority list, either.

Why my obsession with Visual Studio? Well, for one, I've been programming in this environment since the beta arrived on my desk on a single 3.5" floppy disk. Yes, even before the days of "dot net" when it was just experimental plain ASP on IIS for Windows NT. Today Visual Studio is an amazing development environment for many target platforms. The robust intellisense and auto-complete features are incredibly helpful.

I find it quite surprising that the Arduino IDE has been so successful, despite having no intelllisense, no auto-complete, and even more: no debugging. What the Arduino IDE does well: get code onto embedded hardware easily. 

The tricky thing with embedded code is that to effectively do single step debugging - a JTAG adapter is needed. This is a crazy world of semi-standards between a variety of vendors supporting different micro-controllers. Most vendors target "mainstream" processors: ARM, STM32, Atmel, etc. The thing with Espressif is they use Tensilica processors. I had never even heard of them until the ESP8266 came along. So it was not a surprise that using existing JTAG debuggers with them was such a bumpy road.

Here I have some comments and observations with the preview version of VisualGDB for the ESP32

Bugs in VisualGDB 5.4 Preview 1

Right-click on "Components". Add - New Item.

Add MyComponent:

What happens? Nothing. There's a quick blink, but no folder appears there. Try adding another. Same result. The directories *were* created. Right-click, add existing item:

The directories are there... even drilling down and adding the does not make them appear in Solution Explorer.

Another issue is when assigning Preprocessor Definitions:

The compiler will not "see" this change until Visual Studio exit and restart. For some external libraries (in my case RadioHead) - this directive is *never* seen.

Next, is attempting to include external libraries (in this case Arduino style; yes I know it is not officially supported, but it should in fact act just like any other library, no?)

This first example should be pretty self-explanatory: despite being listed in the "components" of the project, the #include "pins_arduino.h" in the arduino.h file cannot actually find the file:

Despite also being listed in the "additional include directories:"

The file is definitely there:

I've been unable to compile. So close, with only "missing" files... so close.  :)

VisualGDB Wish List:

1) Include more Windows Environment Variables in the VisualGDB Build variables. In particular the USERPROFILE, in my case:


Why? Well Arduino-style libraries are typically installed in:


In my case, libraries such as:

2) Right-click on source file name should include the ability to rename. In particular to change from blink.c to blink.cpp.  Otherwise I needed to rename from Windows Explorer. Visual Studio did not "see" the change. I even after exit and restart. Curiously double-clicking on the file forces the re-scan, but only *after* exiting from Visual Studio.

Curiously, only after renaming a source file (such as blink.c to blink.cpp) - and double-clicking on it- does the re-scan occur, causing Visual Studio to then also see those new Component directories added above. (the "Reload Project" does not seem to work consistently)

3) Right-click on file names and components should also allow "Delete" (purge from disk) and "Remove" (exclude from project)

My version of Visual Studio for this exercise:

Microsoft Visual Studio Enterprise 2017
Version 15.6.4
Microsoft .NET Framework
Version 4.7.02556

Installed Version: Enterprise

Visual C++ 2017   00369-90250-38212-AA522
Microsoft Visual C++ 2017

Programming the Lattice Semiconductor FPGA iCE40 Ultra Plus Breakout Board iCE40UP5K-B-EVN

FPGA programming the Lattice Semiconductor iCE40 Ultra Plus Breakout Board. (work in progress, come back soon) TL;DR The Diamond Lat...