Labels

Showing posts with label ESP32. Show all posts
Showing posts with label ESP32. Show all posts

Wednesday, 23 September 2026

ESP-IDF

Intro

ESP-IDF (IoT Development Framework) is the official IDE from Espressif Systems for their microprocessors including ESP32.  As such it can be used to build and download images to ESP32 like the Arduino environment.  Whereas Arduino (successfully) endevours to make programming as simple and painless as possible ESP-IDF enables the user to utilise all ESP32 functions.  Even simple IDF programs can be challenging to develop but it is extremely useful in situations where Arduino IDE doesn't work properly for a particular application.  Arduino uses a SETUP + LOOP configuration, whereas IDF uses function interrupts / callbacks to deal with inputs and other events.

Environment

I installed ESP-IDF v6.0.1 for Windows PC.  Installation is very straightforward and it takes a few minutes to download and configure all the code. Clicking the resulting icon gives you a Powershell command line session.  You then use a python script idf.py to confirm which chip is being used (set-target) compile (build), download (flash) and open an ESP32 serial terminal (monitor).  The build process has many (automated) steps and creates a bootloader, executable image and any other required code.

Comprehensive documentation is provided by ESP and there is an excellent set of examples covering most (all?) available functionality.

Examples are vital as there is a very steep learning curve when learning how to control a range of hardware functions.   Most functions (e.g. GPIO, I2C, wifi, web servers) can be implemented by taking one or more example programs and adapting them as required.




RNT Book

There is a bit more to IDF than I can learn by browsing the programming examples and I struggled with IDF attempting to make BreezyBox work on ESP32S3. 

On 25th August, whilst on holiday I received a circular from Rui Santos announcing a new book "Learn ESP-IDF with ESP32".  Random Nerd Tutorials was setup a few years ago by Rui and his partner Sara.  They have published many, many high quality Arduino ESP8266/ESP32 and RPi tutorials from which I have benefited greatly.
They obtain an income from ebook publications and ,although I have been keen to purchase, none has been quite appropriate before.  This one seemed perfect and I jumped in to purchase it on the publication day.

Chapter 1 covers ESP-IDF installation in a Visual Studio (VSC) context and a few basic concepts.  Chapter 2 covers GPIOs; digital outputs, digital inputs, analond inputs and button debouncing using interrupts.  Although the descriptiona are light and superficial you end up with the ability to program many GPIO functions by adapting sample programs.

After Chapter 2 you have the flexibility to cover topics of specific interest: deep sleep, I2C, Wifi, BLE or ESP-NOW.  Using wifi and web servers is my first interest, but I will also cover ESP-NOW which provides networking for ESP32 devices.

USAGE

Once you get used to it, ESP-IDF doesn't seem too difficult.  Examples provided by Espressif look complicated because they are properly written, testing for errors for each hardware operation which makes them rather long.   In comparison Arduino uses standard functions for many different board types so their functions are well known.  For simple applications Arduino is certainly easier and quicker.

I expect to use ESP-IDF in a number of situations:
-  Assembly language programming
-  Whereever a software packages uses dependencies or specifies ESP-IDF install instructions
 -  If I need to use Callback functions.




What is an ESP32?

ESP32 is a 32-bit processor from Espressif Systems with wifi and bluetooth connectivity.  It is a successor to ESP8266 (32-bit cpu and wifi) which became extremely popular with the make community because of its low price and wifi connectivity.  The Arduino IDE incorporated support for ESP8266 and ESP32 which gave hobbyists a simplified approach to build applications for the system.

The first ESP32 was first released in Sepember 2016 so the reange is now ten years old.  There are now two principal designs "S" and "C" based on Xtensa and RISC-V architecture respectively.


ESP32-S3 is based on dual core Xtensa LX7 processors.  Xtensa is marketed by Cadence Systems in San Jose, California.  These processors  can be customised to provide a specific set of functions which the manufacturer requires.  It looks like you can use an IDE to design your processor and it is manufactured based on the results.

ESP32-S3 is a SoC (System on a chip) with 240MHz dul Xtena LX7 CPU, 512KB SRAM (Static RAM), 384KB ROM, 16KB RTC RAM (Very Fast RAM), 8MB PSRAM (Pseudo-static slow RAM), 45 GPIO pins, 3 x UART.  Internal ROM is used internally for wifi drivers, RTOS etc.
The SoC is often combined with extra components in a module board such as ESP-WROOM-32. Mine is an ESP32-S3-N16R8 which has 16MB flash ROM as well as 8MB PSRAM.  The module is a square silver SMD component with an antenna.
The module is mounted on a development board with USB-C ports, 40 pins, boot/reset buttons and an RGB LED.  Mine doesn't have a name but there are some well-known ones: ESP32-DevKitC (Espressif), LolinD32 (Wemos), NodeMCU-32S (Ai-Thinker).

I found some interesting articles which provide more illumination on circuitschools and JLCPCB

I have previously blogged about the ESP32-C3 RISC-V processor. This is important as Espressif have announced they aim to focus on RISC-V in the future which implies that their RISC-V  cores will eventually be as powerful as Xtensa cores.



ESP32 uses the RTOS Real-Time Operating System internally to control hardware.  Programmers can use RTOS functions for time-sensitive operations.  Programming is carried out using ESP-IDF, Arduino or PlatformIO IDEs. Programming can be carried out in C, C++ or micropython.




Monday, 7 September 2026

ESP32C3

Intro

ESP32 processors first became available in September 2016 and I first used them in 2019.  The last ones I purchased in 2023 for my LilyGo T-Display buttons which have been well used since then.

ESP-C3-01M-Kit


My interest was re-kindled when I noticed that the ESP32-C3 range runs on Risc-V.  I  purchased a couple of NodeMCU-Series ESP-C3-01m-Kit but they didn't appear to be totally compatible with the Arduino environment and I didn't progress far with them.  I did subsequently try configuring with ESP-IDF which worked better.




ESP32-C3 Super-Mini

Instead I bought some ESP32 C3 Super mini development boards which seem a lot more compatible with Arduino.


Shellminator


I saw a Hackaday article talking about shell/terminal programs which can be quickly implemented.

After a quick Google I decided to try Shellminator on my ESP32-C3.  Breezybox looks even better but requires a more powerful ESP32-S3 system.


I installed the Shellminator library in Arduino IDE and copied the first example program.

The program works fine and gives me a simple shell.  You can then code applications which make use of the shell.  This is a brilliant start for me and encouraged me to purchase an ESP32-S3 for Breezybox.


Assembly Language

It is good to see that a ESP32 Risc-V processor can be used just like an ARM processor.  At present they are somewhat less powerful, but I am pleased and applaud their progress.  Of course I really ought to try out the assembly language.  It is possible to use inline assembly in the Arduino IDE and it appears possible to include separate assembly modules.  However it looks to be rather unreliable/undocumented/error-prone.

The correct answer is to use the official ESP IDE.  ESP-IDF (Espressif IoT Development Framework) is easy to install.  I use it with Windows.  An icon is provided to invoke the command line environment in a Windows shell.  You use python "idf.py" commands to build, flash and monitor programs on an ESP32.   I will talk about ESP-IDF more in another post.


Luckily for me I found an excellent simple tutorial by Prof Smith at York (US) University.
ESP-IDF uses a package called CMake to create a set of build files for you program, taking into account ESP32 variant, compiler, platform you are using.  To use it you set up a structure of files.  You describe what you are building in the two CMakeLists.txt files.

In this simple example we have a C program defines some variables and prints a result.
Variables are passed in RISC-V registers a0 and a1 into the assembler routine, sub3.s.  The code stores the subtraction result in a0 and returns to C.  In C, a0 is the result "r" which can then be printed out.

Although this is a trivial example it gives us the mechanism to write assembler.  Of course, any usage of the processors GPIO pins or other capabilities requires an understanding of hardware, libraries, structures etc.
It is usual to only write the parts that you want/need to in assembler so the tricky parts can be done in C at first.

Outro

It is good to have an easy entry to RISC-V assembler programming using ESP-IDF.  




Monday, 25 July 2022

More MQTT Clients

We setup an MQTT broker on Home Assistant (HA) so that our Lily ESP32 super remote can communicate with it.  MQTT facilitates many different systems to interact and clients can communicate with each other as well as the broker.

Linux MQTT

RPi MQTT client installation is easy, I just install mosquitto-clients onto the RPi and I can send messages at the command line with mosquitto_pub.
If I use the same topic and message as Lily it has the same effect, for example in the example below I publish "button 3" to topic esp32/volume and HA arranges for the volume to be turned up on my Sony Amplifier.



This is great for testing but it is unlikely that I will use the linux command line much.  However I would like to send MQTT messages using a browser, this allows me to send messages from phone/ipad/pc.  

Browser MQTT

The Eclipse Paho MQTT javascript client appears to be a popular choice for a browser javascript MQTT client and Steves Internet Guide provides a very clear example to get a client working.  The browser javascript client communicates with websockets on the MQTT broker and after some searching I found that in addition to the MQTT port 1883, HA supports websockets and the broker websocket listener is on port 1884.

Using Steves Internet Guide it was easy to setup javascript MQTTconnect/disconnect functions, associated with buttons on a simple webpage.  The handlers required to deal with connection success / failure and incoming messages are also simple to implement so I can show the status of the connections and any messages received on a page.  Finally I provided"volume up" and "volume down" buttons to demonstrate that I can now control my amplifier volume by sending MQTT commands to HA so that it can tell the Broadlink IR sender to send volume up/down commands to the amplifier.

The result is the very simple test webpage shown below.  I can now easily implement MQTT functionality into other webpages which need to control devices in the home, in particular my home music server.

It is great that MQTT is a flexible general purpose communications protocol which will work for many different devices.

Webhooks

Early on in my HA investigations I setup webhooks so that I can trigger HA automations from a webpage.  At the time I was more interested in voice control using Google Assistant and webhooks duplicate what is more readily achieved through voice.

However it occured to me that if I can use parameters / arguments with webhooks they make a realistic alternative to MQTT for communication with HA. Webhooks communicate directly with HA rather than needing to connect to the MQTT broker and sending a payload.

HA documentation indicates that it is possible:


Webhooks are implemented using POST requests which I can most easily provide using a linux curl command

I had some difficulty seeing the payload in HA until I added the -H parameter to specify JSON format.  However once this was resolved I could write an automation which displays the payload trigger.json.payload as a HA notification when the message is received.

Of course I want to use webhook URLs in a webpage so I coded a form to send an input text box named payload.  In this case the item containing the information is trigger.data.payload.  Rather than sending an input text field I can add one or parameters to the webhook URL containing directives for HA.  I added a parameter called arg to the webhook URL and could access it in a HA template as trigger.query.arg.  The example below shows both the form item payload and the URL arg being sent from a webpage.  The URL arg is displayed as a HA notification whilst the payload is sent to Google Nest mini to be read aloud.


Implementation


This is great, I can add a variety of fixed and variable information from a web page into a HA webhook automation.
My first implementation is an automation which carries out the same functions as the buttons on the Lily Remote Control to implement volume control and display LED patterns on my programmable LED display.  It is very quick and simple to set this up, only about 15 minutes from concept to testing.




Monday, 11 July 2022

LilyGo Remote

 Previously I spent some time setting up my wonderful new LilyGo T-Display Keyboard functions, mainly to control my music server, similar to its pre-decessors.  It has a lot more potential and I have been starting to add features.

Screen Saver

As Lily works on battery when not connected to USB there is a limited amount of time before it needs to be recharged.  It is sensible to turn off the screen when not in use.  The factory_Test sketch which was provided with Lily shows how to turn the display off and put ESP32 into deep sleep mode


The first two commands DISPOFF and SLPIN blank Lilies LCD display and turn off power to the LCD.  I struggled to find documentation for these commands.  In fact they are well-documented in the ST7899V datasheet which corresponds to the LCD.  I can turn the screen back on with DISPON and SLPOUT commands.

So now I need to setup a proper screen saver.  It should wait until Lily has been inactive for a short while, say a minute and power down the screen.  When a key is pressed the screen should be powered on, allowing the user to continue.

I need a timer function to do this.  There are generic Arduino timer libraries but it is better to use the ESP32 timer function.  We set up a timer with an alarm so that the screen blanks after 10 seconds (during testing, 1 minute for real use).  If the user presses a key either before or after the screen blanks the timer is reset and the screen is restored so they can continue. 


This works very well, keyboard input is still possible when the screen is off so there is no delay in waiting for Lily to wakeup.  I have added some menus for favorite albums, radio stations and chart playlists so it is useful to have the screen on for this.  For some of the other functions I dont usually need the screen.

Hibernation

If Lily is not in use it can be put into a deep sleep where wifi is turned off and the CPU is using little current.  Initially I investigated shutting down functions before sleeping then waking up with an external interrupt when a button was pressed.  However I decided to simply set up a timer so that the ESP32 goes straight into deep sleep after an hour of inactivity with no wakeup capability.  If Lily is in use it is likely that a key will be pressed within an hour.  It only takes 5s-10s for Lily to start up so pressing the restart button on first use isn't an issue.

With Screen Saver and Hibernation working Lily is behaving like a real computer!  The battery lifetime is now at least two or three days.

Saturday, 9 July 2022

HA : ESPHome : RFID Reader

 Previously, I installed ESPHome with a simple indicator, showing whether a GPIO pin was hi or lo.
Many devices we use with Home Assistant (HA) have specific home automation interfaces.  ESPHome extends HA functionality by providing an interfaces for many more sensor components which can measure the environment somehow.  ESPHome provides the capability to interact with RFID readers and cards / tags.

I purchased three RC522 readers on Ebay and they turned out to be very simple to setup.

I am using a ESP32-VROOM-32 as my ESPHome server device and it communicates with RC522 using SPI.  ESPHome is configured using HA.  Firstly we define the pins to be used for spi.  We can then add the pin required for RC522.  Using the ESPHome UI we tell HA to install this configuration and it spends a couple of minutes compiling an image and downloading it wirelessly to the ESP32.  I connected the 4 data pins plus 3V3 and GND from the ESP22 to the RC522 and ESPHome showed that it was communicating 😀😀😀


 The next stage is to present a keyring-tag or card to the reader.  When you do this the ESPHome console log shows the id.  You can add each tag uid as a binary sensor within ESPHome and then look at them on a dashboard.

Once we have binary sensors we can set up a HA automation which is triggered whenever the binary sensor state is changed.  My  initial experiment instructs the Google Nest Mini speaker to inform me when Tag 1 is presented to the reader and when it is removed.

There are ways we could use the cards.  I could present a card to the reader when I enter or leave the room and everything could be setup for me.  Baby Harry could have a variety of cards to do things when he wants them. 
I did check whether NFC on my phone is acceptable to the reader.  It does register and send a tag id to HA, but the tag is different each time, possibly as it uses more sophisticated security, so it isn't much use to me.



Wednesday, 22 June 2022

LilyGo T-Display Keypad


Rationale

Some years ago I setup a home music server based on Linux Music Player Daemon (MPD) with a web front-end to select and control music playing on my HiFi separates system.  Typically I would choose music to play using  my iPad or phone browser.
As an add-on I developed an ESP8266 attached keypad which also allowed me to control the music.  I call it my "Button" although it has a number of buttons/functions.  It is particularly useful for pause / resume, next type functions and to start my favourite playlists and radio channels.  Effectively it is a clever remote control tailored to suit my needs.
The original "Button" version 1 has a USB cable to power the ESP8266 so it isn't very portable; typically this doesn't matter but I did develop version 2 which has a rechargeable battery.  I found this cumbersome and I haven't used it much.  Both Buttons have a tiny LCD screen to display status info but this isn't very visible as I have had difficulty creating suitable enclosures to accomodate ESP8266/keypad/displays.


Recently I have implemented my Home Assistant (HA) Server, including MPD for music server control and Arduino based MQTT communication.  This allows me to control my HiFi from an ESP device and I added it to my "Button" software for testing purposes and it works fine. It works in parallel to my original application and has the potential for more functionality / flexibility.

In advance of doing more work on the button I looked out for a case for an ESP device and keypad which would be more compact than my home-made devices.  I was excited to stumble on the LilyGo T-Display Keypad on Aliexpress.
It is based on the ESP32, has a mechanical keyboard, a small TFT display and a rechargeable battery all in a neat case.  This is an absolutely perfect replacement for my old buttons and I ordered one immediately.

First look

Connecting the USB-C cable causes the ESP32 to start up.  A simple display appears allowing you to display wifi networks, input voltage or put the processor into a deep sleep.  There are two buttons adjacent to the TFT screen for these controls.  There is also a reset button on the top or right of the case.
Like a remote control there is no on / off switch which feels very strange to me.





The LilyGO github repository provides you with instructions to load the TFT library and example sketches into your PC Arduino development environment.
From there it is easy to compile and load the Factory_Test sketch which is the same one as installed on delivery.  This is awesome, nothing makes life easier than a working example containing many of the features of the device.  In particular it provides information on:
    All the pins needed for keypad, TFT, buttons, SD Card
    Arduino libraries for all hardware
    Example code to use the TFT display
    Example code to find the input reference voltage (useful for low battery check)
    Example to display a bitmap on the TFT
    How to put the ESP into deep sleep

In addition there are sketches for
    Example code to use the keypad
    Test SD functions to create, view, delete file and directory list.
    A rather good animated eyes sketch which makes really brings the screen to life.

This makes my life so much easier and saves me many, many hours looking at schematics, finding libraries, writing code to set up everything.

Application One

My first application requirement is clearly to setup similar functionality to the "Button".
Firstly I amended example code to read input from the keypad and display messages on the screen.  I then added wifi and websockets based on "Button 1" code.  Using a case statement I could then easily process key presses and send websocket messages to PI40 (which processes websocket input) for processing.  Within a few hours I have a working example for the "Button 3" based on the web page which I usually use.

I added and tested MQTT, using Home Assistant (HA) as my MQTT Broker.  This allows me to communicate with HA and use a HA script or automation to control any function HA function.  This is an extremely powerful extra.  On my webpage I cannot control amplifier volume since the amp is old and uses an IR remote control.  However my Broadlink IR remote is controlled by HA and I can add volume up/down buttons to LilyGo.

The LilyGo battery runs out after about 24 hours if not recharged, so I set up a button to show battery percentage / time remaining and another button to clear the screen.  This completes a basic setup which is a great improvement over my previous buttons and which I use in preference to my phone.

I cannot stress enough what a good product this is for me, both in terms of the hardware purchased and the software provided to help get started.  I shall look out for other products they can inspire me with.


     

 


Tuesday, 16 March 2021

Maixduino : AI meets Arduino

 I read an article in Elektor about an amazing Maixduino hardware board which provides an AI system in an Arduino compatible package.

The board is packed full of juicy hardware, including an ESP32 for ancilliary processing, a camera, SD card, audio and an LCD screen.  At its heart is a Sipeed Maix 64 bit RISC-V module which processes AI.  It can be programmed through the Arduino IDE which avoids a steep learning curve.

I purchased one from Mouser and started to follow Elektor article to set it up.  If you follow detailed instructions and an installation works first time you have an immensely satisfying experience but you tend to learn a little.  When events dont go as planned and issues need to be investigated by looking around different sites and articles, it can be frustrating but you learn a lot more.  Needless to say my installation experience was a struggle.

After some unsuccessful attempts at installing the Arduino software I tried the PC based micropython environment which is described well at icircuit.net and I was soon able to flash Maixduino using the kflash command line tool.  The documentation provided by sipeed also provides good detailed explanations.  Maixduino uses two COM ports one for the Maix processor and the other for the ESP32.  It was exciting to see them both communicating properly, giving me more confidence with the hardware setup and PC connection.  The test program I ran was great, it showed a camera image on the LCD screen.
Returning to the Arduino environment, with my hardware setup validated, I looked a bit more closely at the software environment.  The Elektor article had described a simple Arduino installation which obviously worked for the author.  I was pleased when I found out how to setup multiple Arduino environments on the same PC. Some of the software included in Maixduino libraries has been used by me on other projects, in particular Adafruit graphics libraries.  I set up a "portable" Arduino IDE environment which uses a separate copy of libraries and configuration files.  I then repeated the installation process.  The portable environment is nice as you can see everything relating to libraries you have installed, sketches you have written etc in sub folders of the portable folder.

Using the new environment I could compile a sketch (with a number of warning messages)  but still not "upload" to the board.  I saw two flags in preferences.txt for compile.debug and upload.debug, both set to False.  I turned them on and could see the detailed commands the IDE uses to create and upload a program to a board.   I could see kflash running and failing so I lowered the upload speed from 1,500,000  to 1,000,000bps.  Rebooting and resetting the board now allowed the upload to proceed. 

I tried a few demo programs to verify the environment. Using the Kendryte K210 AI module you declare the ST7789 screen and use the Adafruit GFX library to draw shapes, lines, text etc.  Using th ESP32 I started with a "Hello World" serial monitor display.

I was now able to return to the Elektor article and try the "selfie" program which shows a camera image on the LCD display.  I still had some minor problems, there were duplicate Adafruit GFX libraries (I deleted the one not provided by Maixduino) and a typo in a Maixduino library(googling provided me with a correction to apply).  Finally I was able to complete the first experiment and take a picture, as shown below it is a picture of a rock.
The second example described by Elektor is to run a demo which is a  real image recognition program.  It isn't practical to "train" the AI system oneself without a lot of effort and thousands of images but the sipeed demo provides you with a pre-prepared "neural net" which is copied to the SD card.  A simple sketch reads in the net then analyses a video image before guessing what it shows.

Bella was the first subject.  The program is incredibly fast, it analyses the picture in about a second then guesses.  Bella isn't a very good subject as she moves around and takes whatever position she wants in front of the camera.  The best guess is shown below, the AI top answer is "Fur coat" - very funny, but actually accurate, after that it suggests she is a German Shepherd or police dog! Wow.


I found this a stunning result.  AI hardware, costing £20 can actually identify a dog and have a good guess at the breed within a second.  It shows how real AI technology is and indicates that practical systems are within our reach.  Another stunning Elektor article which is the start of a short series, so there should be more to blog about soon.

Friday, 21 August 2020

ESP32 + Joy-IT touch screen

 Intro

As I have said a number of times before Elektor is a great place to find introductory projects.  The January/February 2020 edition contains an article on attaching a Joy-IT touch screen to an ESP32.  The example provides a very neat interface to the Elektor Weather Station project and shows what it is capable of.  The system makes use of littlevGL an application by Gabor Kiss-Valose which does all the graphics and touch screen heavy lifting.  It works on linux and other platforms as well as ESP32 and appears to be coming a popular/standard product.  To make life simple I purchased the Joy-IT touch screen from Elektor and also, after a false start, purchased an ESP32 devkitC from eBay.

Installation

The Arduino IDE is used for this project.  After connecting and testing board connectivity with a blink program (set LED to GPIO 2) I needed to set up the ESP32_eSPI software and littlevgl and configure them.  There are 10 wires used in the interface, including power and SPI, so it isn't too complicated.  The Elektor article comes with the required libraries and configuration files as a download.  This made it easy to setup, I copied libraries and configuration files across as appropriate.  In addition to TFT and LVGL libraries, other libraries are included for MQTT, json and CRC32.

Testing

There is a basic TFT test in the TFT_eSPI examples so I compiled/uploaded and it displayed pretty colours and text.


I then took the Elektor Weather Station Example, set the "demo" flag and compiled it.  Very impressively out comes the Weather Station screen, complete with touch screen capability and demo data .


Conclusion


This is great as it provides examples of tabs, gauges, meters, labels and touch screen input.  It should be easy to modify for my own purposes.  As an extra it gives me a way in to using MQTT and JSON data on Arduino/ESP32.  It is an excellent introduction to a wonderful product which can be quite challenging.  The littlevgl software and Joy-IT screen can also be used on an RPI.  At first sight the ESP32 usage, utilising wifi to obtain data updates seems best.  Then the ESP32/screen only need to be switched on when in use and don't need to be near an RPI.


Saturday, 13 June 2020

FreeRTOS Tutorial

Background

I am always on the look out for simpler Operating Systems I can try.  I often see references to FreeRTOS in passing and was excited to see an Elektor article which shows how to run FreeRTOS on an ESP32.  My current ESP32 is happy running a micropython setup with its own firmware so I promptly ordered another. In the meantime I looked around for learning resources so that I can learn more.

I decided that a udemy course "Arduino FreeRTOS from the Ground up" fitted the bill and as it was cheap (£13) I gave it a try.  It turned out to be somewhat superficial and very slow paced but it does get you up and running and actually using FreeRTOS.  FreeRTOS can run on any Arduino so I quickly gave it a try.
Circuit Digests Arduino FreeRTOS tutorial would be a better place to start 

Task Creation


FreeRTOS is centred around tasks (threads) which run independently.  The OS simply arranges for a mix of tasks to run on the available hardware.  In the Arduino IDE you define a setup function which is run once to initialise the system and a loop function which carries out the repeated activities on the system (e.g. lighting LEDs, reading sensors, outputting results).  One of the headaches is making sure that all the activities are carried out when you need them.  For example you may have a sensor you want to read every 100 milliseconds, and a webpage which you want to send out whenever a suitable http request arrives.

FreeRTOS eliminates use of the loop function.  You simply create all the tasks in the setup function, provide details of their priorities and let FreeRTOS decide which one needs to run.  To use FreeRTOS you simple start the sketch with:
 #include <Arduino_FreeRTOS.h>

The task creation function takes the form: 
  xTaskCreate(functionName, label, stacksize, priority, handle);
For example:
 xTaskCreate(flashRedLed,"Flash",100, NULL,1,redHandle);
Now, within the function flashRedLed, you write standard blink code, it can even be copied from the blink example.
When compiled and uploaded the LED blinks. Each of the programs functions can be added in a similar manner and will work independently.  You can simply copy and paste working functions and FreeRTOS will take care of them.

The handle is a variable which allows reference to and control of the task, for example to suspend / resume the task you write:
xTaskSuspend(redHandle);
.... do something ....
xTaskResume(redHandle);

Passing Information between tasks


Tasks usually need to communicate with each other, for example a user input task would pass details of processing to a processing task which could then send results to an output task.
Queues are used to pass information.  In setup a queue is created allowing a certain number of entries.  Functions can then add an item to the queue.  The item is usually a structure to allow all necessary details to be included within the single parameter.
Queues can be grouped into queuesets so that tasks can easily process information from a number of queues.

Synchronising Tasks


Timers start and stop tasks based on clock ticks or milliseconds. Event groups are defined to set specific bits allowing tasks to wait for something to happen before taking action.
Semaphores prevent tasks conflicting for shared resources by flagging when they are in use.
Mutex semaphores allow a single task to control a specific resource.

Interrupts weren't covered much, but of course are important in Arduino programming.  There is a special function xQueueReceiveFromISR so that functions can process ISR follow-up.


Summary

FreeRTOS provides a simple view of an Operating Systems "responsibilities".  Its job is to facilitate tasks to carry out their work.  The Arduino implementation provides this in a very simple manner by replacing the loop with a powerful task mechanism.  Other "responsibilites" such as providing hardware drivers and a user interface are (rightly) left to the existing Arduino environment.
I am not convinced that the udemy tutorial was better than blog tutorials on this subject but it did achieve basic understanding for me.






Wednesday, 29 January 2020

MicroPython

Micropython was invented by an Australian programmer Damien George in 2013.  It is a lightweight version of python, compatible with version 3, suitable for microcontrollers.  It strikes me that I should concentrate more on python programming and this is part of the suite.  In particular I can use it for  ESP8266, ESP32, Arduino and micro:bits.

micro:bit


It couldn't be simpler to get started with micropython as explained at microbit.org.  Just use an on-line editor to create your program - initially you can amend the sample provided.  Once happy with the program it is downloaded to your PC as a hex file.  You connect the micro:bit to the PC which gives you a new drive.  Copy the .HEX file across to the micro:bit drive and your program runs.

ESP32


Install a python utility called esptool in my Windows environment using pip
download and install firmware as described at micropython.org
Install the Thonny IDE as described by RandomNerdTutorials.

Now we select micropython on ESP32 in Thonny options, select the ESP32 com port and see the Thonny shell running on ESP32.


First ESP32 Project

I really want projects to be web based to reduce connection hassles.  So first of all I want to establish wifi connectivity.
RandomNerdTutorials.com have a very simple project which starts wifi and establishes a webserver.
Two files called boot.py and main.py are downloaded to the ESP32.  These are run automatically at reset.  In this case boot.py establishes the wifi connection and main.py causes a webserver to listen for and process connections.
Connections from a browser cause a webpage to be displayed allowing control of a GPIO.
The GPIO in question is the inbuilt LED (pin 2) so clicking a button causes a response to be sent to the webserver turning it on or off.
This gives us everything we need to use the ESP32 as a basic standalone webserver reporting on its local environment.

Webserver

J-Christophe Bos has created a wonderful small webserver which is contained in 3 files:


It allows you to serve static pages, server-side scripting (pyyhon/html), websockets and supports GET/PUT, JSON, AJAX all in 65KB of program.  ESP32 has 2MB flash for filespace so this leaves plenty of room for data and static pages.


There is a microwebsrv2 update on github but as of December 2019 there seems to be a little problem making it trickier (for newbies like me) to setup. 

Tuesday, 7 January 2020

Arduino MKR Vidor 4000

Intro

After the excitement of the Atlas-SoC I saw that Arduino have come up with similar hardware.  The Arduino MKR range provides low cost/power 32-bit micro-controllers.  The Vidor 4000 contains an Intel Cyclone 10 FPGA together with an Arm Cortex M0+ MCU.  So we have an Altera FPGA which we can program/configure with Quartus and a Microchip SAMD21 Arm processor which we can program with the Arduino IDE.  Arduino simplicity and the size of the user community should make this potentially widespread in its appeal.  It is also new in late 2018 so its use is just beginning to evolve. It is so exciting that I asked Harry to buy me one for Christmas.

Install

Getting Started is straightforward, as you would expect.  In board manager I needed to add SAMD beta boards then add the Vidor 4000 board in the Arduino IDE.  As usual I connected a microUSB cable to the PC and, after installing a driver, I was able to see the device and load a standard blink "hello world" program.  Next I downloaded VidorGraphics, VidorPeripherals libraries and could then try the Vidor specific examples. The first one of interest is to display an Arduino logo on an HDMI screen.  Initially this didn't work on my HDMI monitor but when I connected to a HDMI TV port it worked fine.

Familiarisation

The best explanation I have found for the Vidor 4000 is provided by Philippe at systemes-embarques.fr who also provides some tutorials.  Components on the card are:
  • ATSAMD21G18A microcontroller with 256 kB of Flash and 32 kB of RAM.
  • a Cyclone 10CL016 FPGA with 15408 logic elements, 504kbits of RAM and 56 multiplier 18 × 18.
  • a 16 Mbits FLASH SPI.
  • a 64 Mbits SDRAM (4M x 16 bits)
  • a NINA W102 WiFi / BLE module incorporating an ESP32 dual-core microcontroller.
  • an ATECC508A cryptographic chip improving the processing speed for secure connections.
  • MiniPCIe, USB, battery, I2C, MKR, MIPI for a camera, HDMI for video output connectors.

Most resources are attached to the FPGA but can be routed to SAMD21 or ESP32.

When Vidor is switched on MCU and ESP32 are initialised from non-volatile memory and the FPGA configuration is loaded from the Flash memory.

If you use Arduino IDE to upload the example "blink" sketch it is loaded via USB cable to MCU and run without touching FPGA.

If however you upload Examples>VidorPeripherals>VidorTestSketch one part of the .HEX file is loaded to MCU and the rest (app.ttf), is loaded via JTAG into FPGA and saved in SPI FLASH as shown in the diagram below.
When FPGA.begin() is executed on SAMD21 it enables FPGA clock, initialises JTAG port and sends a command to FPGA telling it load app.ttf from flash.  The JTAG port is used for subsequent MCU - FPGA intercommunication.