Microcontrollers, or MCUs, are transforming consumer goods, industrial automation, infrastructure and more — essentially reshaping how we interact with the world around us. With MCUs you can easily control everything from your refrigerator to your thermostat. At ICS, a quarter of the projects we work on include at least one MCU and most projects incorporate several — so we understand the potential MCUs hold. We’ve designed this webinar to introduce you to this tiny technology and show how it can be implemented using various approaches.
2. Integrated Computer Solutions Inc. www.ics.com
About ICS
● Founded in 1987
● Largest source of independent Qt expertise in North America
● Trusted Qt Service Partner since 2002
● Exclusive Open Enrollment Training Partner in North America
● Provides integrated custom software development and user experience (UX) design
● Embedded, touchscreen, mobile and desktop applications
● HQ in Waltham, MA with offices in California, Canada, Europe
Boston UX
● Part of the ICS family, focusing on UX design
● Designs intuitive touchscreen interfaces for high-impact embedded and
connected medical, industrial and consumer devices
3. Integrated Computer Solutions Inc. www.ics.com
What we will cover
3
Overview of MCUs and their features
● Current scope of MCU-based applications
● Convergence of microcontrollers and microprocessors
● Peripherals available and protocols used in today´s projects
Discussion on programming approaches
● Pros and cons of programming bare-metal vs RTOS
● Running Python scripts on MCUs
Challenges in firmware implementation (with example code)
● Application fragments implemented with the ubiquitous ESP32 MCU
● Network-enabled demo that connects to a Mender server and retrieves over-the-air
updates
4. Integrated Computer Solutions Inc. www.ics.com
Current scope of MCU-based applications
4
● Reduce the cost of the project
○ Hardware costs lower than MPU-based (less peripherals)
○ Firmware development costs equal or higher (more tools available on
embedded Linux, for example Qt, imply less time for implementation)
○ => Bottom line will depend on the expected size of production.
● Solve specific tasks within a system containing other controllers
○ Split the application into smaller subtasks handled by several MCUs
○ Tough timing requirements (high speed protocols)
5. Integrated Computer Solutions Inc. www.ics.com
Convergence of MCU and MPU fields
5
● Traditional approach
○ MPUs = external storage and runtime memory
○ MCUs = single chip processors
○ MCUs ran customized firmware on devices, MPUs ran OS on desktops with
general-purpose applications
● Today: blurring boundaries
○ Improvements in levels of integration
○ Widespread growth of Linux for devices, i.e. ‘non-computers’
○ Reduced MCU development time.
■ Versatile hardware configuration options
■ Small OS available (mostly FreeRTOS)
■ Hardware abstraction layers (STM32´s CubeMx, for example)
■ Large ecosystems of developers bring many open source code
7. Integrated Computer Solutions Inc. www.ics.com
Today´s peripherals
7
● MCUs embed as many peripherals as possible inside the device, in line with the
targeted market: automotive, consumer goods with low power needs, etc.
○ Timers, counters, Pulse Width Modulation (PWM)
○ Serial ports (USART, SPI, I2
C, CAN, Ethernet, …)
○ ADCs & DACs (analog/digital converters)
○ Radiofrequency transceivers (WiFi, Bluetooth, …)
○ Cryptographic accelerators
● External peripherals:
○ Inertial Measurement Units: accelerometers, gyroscopes, magnetometers
○ Weather sensors: temperature, humidity, pressure
○ Other RF transceivers (Zigbee, 5G, LoRa…)
○ …
8. Integrated Computer Solutions Inc. www.ics.com
SPI I2
C
8
Serial Peripheral Interface
● Full-duplex
● Originally was 1:1, then extended
to 1:N
Inter-Integrated Circuit
● Bus topology
● Addressing mechanism needed,
peripheral specific
9. Integrated Computer Solutions Inc. www.ics.com
CAN
9
Controller Area Network
● Bus topology
● No device ID, but message ID
● Frames
○ Data
○ Remote
○ Error
○ Overload
● Bitwise arbitration
● Widespread usage in automobile and
industrial automation
● Higher layers (CANOpen)
10. Integrated Computer Solutions Inc. www.ics.com
Bare-metal development
10
● Firmware duties:
○ interaction with peripherals/hardware resources (lower level)
○ business logic (higher level)
○ The glue needed to accomplish with the business logic also needs to
provide concurrency
● On bare-metal programming, the operating system is “us” (developers)
○ Concurrency implemented as a round-robin of “never-blocking” functions..
12. Integrated Computer Solutions Inc. www.ics.com
Example (continued)
12
char neverBLockingTask (char *params, char *result) {
switch (stateMachine) {
//…
case WAIT_ANSWER:
if ( timedOut(&timer) ) {
stateMachine = HANDLE_TIMEOUT;
result = ANSWER_TIMEOUT;
} else {
receiveData( rxBuff + buffLen, *buffLen);
if (*buff_len==0) {
result = ANSWER_PENDING;
} else {
if ( ! fullAnswerReceived( *rxBuff, uint16_t *buffLen) ) {
result = ANSWER_PENDING;
} else {
stateMachine = IDLE;
result = ANSWER_RECEIVED;
}
}
}
break;
// …
} // switch
return result;
}
13. Integrated Computer Solutions Inc. www.ics.com
Running Python scripts on MCUs
13
● Traditionally, MCUs languages are C and C++
○ compilers, debuggers
● Python popularity
○ Huge community
○ Sintax simplicity
○ Excellent libraries
○ Good for data science/machine learning
○ Zoomers (Generation Z) prefer .py rather than .c
● Micropython: a small Python 3 interpreter and runtime for MCUs (bare-metal)
○ Open Source, MIT License
○ Footprint: 256 kb flash, 16 kb RAM
○ Not as performant as C (Python runtime needs to parse the script)
15. Integrated Computer Solutions Inc. www.ics.com
FreeRTOS
15
● Small kernel, it provides concurrency, with a small computing overhead
○ Open source and lightweight: footprint is around 4Kbytes
○ Concurrency implemented through threads.
○ Inter-task communication
■ Queues
■ Semaphores
■ Mutexes
https://www.freertos.org/Inter-Task-Communication.html
16. Integrated Computer Solutions Inc. www.ics.com
FreeRTOS example
16
Two tasks communicating through queues
#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/queue.h"
QueueHandle_t q=NULL;
void app_main()
{
q=xQueueCreate(20,sizeof(unsigned long));
if (q != NULL) {
vTaskDelay(1000/portTICK_PERIOD_MS); // one second delay
xTaskCreate(&producer_task,"producer_task",2048,NULL,5,NULL);
xTaskCreate(&consumer_task,"consumer_task",2048,NULL,5,NULL);
} else {
printf("Queue creation failed");
}
}
17. Integrated Computer Solutions Inc. www.ics.com
FreeRTOS example (cont)
17
void producer_task(void *pvParameter)
{
unsigned long counter=1;
BaseType_t resultQueue;
while(1) {
resultQueue = xQueueSend( q,
&counter,
(TickType_t )0);
if (pdTrue == resultQueue) {
printf("sent to the queue: %lun",counter);
counter++;
}
vTaskDelay(1000/portTICK_PERIOD_MS);
}
}
void consumer_task(void *pvParameter)
{
unsigned long counter;
BaseType_t resultQueue;
while(1) {
resultQueue = xQueueReceive( q,
&counter,
(TickType_t ) 0);
if (pdTrue == resultQueue) {
printf("received from the queue: %lu n",counter);
}
vTaskDelay(20/portTICK_PERIOD_MS);
}
}
20. Integrated Computer Solutions Inc. www.ics.com
A microcontroller challenge…
20
Could a third-party Over-The-Air updates solution like Mender
be implemented on a ESP32 module?
● Mender originally targeted for Linux cards, not MCUs…
● Enough flash space?
23. Integrated Computer Solutions Inc. www.ics.com
ESP32: flash partitioning trade-offs…
23
● with the storage partition
○ flash space divided by 3
○ full download before updating
○ uncompress after download feasible
# Name, Type, SubType, Offset, Size,
nvs, data, nvs, 0x9000, 0x4000,
otadata, data, ota, 0xd000, 0x2000
phy_init, data, phy, 0xf000, 0x1000,
ota_0, app, ota_0, 0x10000, 0x140000,
ota_1, app, ota_1, , 0x140000,
storage, data, fat, , 0x140000,
● without the storage partition
○ flash space divided by 2
○ partial downloads, update by chunks
○ needs uncompressed artifacts
# Name, Type, SubType, Offset, Size,
nvs, data, nvs, 0x9000, 0x4000,
otadata, data, ota, 0xd000, 0x2000
phy_init, data, phy, 0xf000, 0x1000,
ota_0, app, ota_0, 0x10000, 0x1F8000,
ota_1, app, ota_1, , 0x1F8000,
24. Integrated Computer Solutions Inc. www.ics.com
Understanding a 3rd party file format
24
● Based on tar format
○ Header
○ One or more files
packed, including
version, payload type,
checksum.
● Mender utility for artifact preparation
● Compression options (gzip, lzma or none)
25. Integrated Computer Solutions Inc. www.ics.com
Moving from challenge into action
25
● #1 receive firmware by chunks
○ firmware footprint around 2 Mbytes
○ Receive buffer size tradeoffs
● #2 parse a tar file on the fly
○ At proper offset, obtain the firmware file size
○ At proper offset, get fragments of the firmware file as they
arrive.
26. Integrated Computer Solutions Inc. www.ics.com
Receive firmware by chunks
26
● Use HTTP Range header
● Tested with cURL first, then implemented on the ESP32 HTTP
API
GET /api/devices/v1/deployments/device/deployments/next?artifact_name=fw_v1_0.mender&device_type=ESP32 HTTP/1.1
Host: docker.mender.io
Range: bytes=0-1023
HTTP/1.1 206 Partial Content
Content-Range: bytes 0-1023/1246515
Content-Length: 1024
...
(binary content)
27. Integrated Computer Solutions Inc. www.ics.com
Receive firmware by chunks (cont.)
27
if (body_length>0)
{
offset=0;
esp_http_client_handle_t client = esp_http_client_init(&config);
to=-1;
while (1)
{
from=to+1;
if (from>=body_length-1)
{ /* Reached the end of GET answer, run latest OTA API call , for latest bootloader setup */
esp_ota_end(update_handle);
esp_ota_set_boot_partition(update_partition);
result=MENDER_DOWNLOAD_OK;
break;
}
to=from+MENDER_RESPONSE_BUFFER-1;
if (to>body_length-1)
{
to=body_length-1;
}
sprintf (range_string,"bytes=%d-%d", from, to);
esp_http_client_set_header(client, "Range", range_string);
esp_http_client_perform(client);
content_length = esp_http_client_get_content_length(client);
artifact_processBlock( client->response_buffer, ...);
// …
}
}
28. Integrated Computer Solutions Inc. www.ics.com
Parse a tarball by chunks
28
● Understand the tarball definition
● Understand artifacts structure (tar inside a tar)
● Implement the stream parser.
29. Integrated Computer Solutions Inc. www.ics.com
Parse a tarball by chunks
29
int artifact_processBlock ( char *inbuff, size_t inbuff_length, size_t *offset, char *outbuff, size_t *outbuff_length, size_t outbuff_size)
{
//….
if (*offset + inbuff_length <= FILESIZE_OFFSET + FILESIZE_LENGTH) { // start of file size reached, end of file size not reached
if (*offset< FILESIZE_OFFSET) { // start of buffer before start of file size
size_t amount = inbuff_length-(FILESIZE_OFFSET-*offset);
memcpy (filesizeBuffer+filesizeBufferLength, inbuff+(FILESIZE_OFFSET-*offset), amount);
filesizeBufferLength += amount;
} else { // start of buffer after start of file size
size_t amount = inbuff_length;
memcpy (filesizeBuffer+filesizeBufferLength, inbuff, amount);
filesizeBufferLength += amount;
}
if (FILESIZE_LENGTH == filesizeBufferLength) {
convertBufferToFileSize (filesizeBuffer, &fileSize);
fileSizeValid = true;
}
*offset += inbuff_length;
result = 0;
goto exit_func;
}
// …..
30. Integrated Computer Solutions Inc. www.ics.com
Security aspects
30
● Open source version:
○ Transport Secure Layer protocol, TLS
○ Self signed or Certificate Authority
○ Provisioning @ factory plant
● Paid plans
○ Same as above, plus mutual TLS authentication
31. Integrated Computer Solutions Inc. www.ics.com
Conclusions
● Big MCU/MPUs ecosystems raise the bar for more complex projects
● Modules integrating MCU and radio chips provide quick yet powerful resource
for IoT.
● Cybersecurity concerns for IoT devices.
● Peripherals integrated to the MCU processor. Serial links (SPI, I2C, CAN,
USART) for off-chip peripherals,
● FreeRTOS and similar small footprint simplifies implementation on MCUs
32. Integrated Computer Solutions Inc. www.ics.com
All images were taken from STM32, Mender’s online documentation.
Questions?
gstola@ics.com