Electronics

How read STM32 HRTIM master timer current counter value

The STM32 HRTIM (high resolution timer) provides a high speed counter that is suitable for benchmarks due to its high counting frequency of up to 240 MHz on the STM32H743.

You can read its current counter value using

uint32_t t0 = HRTIM1->sMasterRegs.MCNTR;

 

Posted by Uli Köhler in STM32

How to determine how many percent of memory (heap) are free on ESP32?

This approach works for Arduino / PlatformIO as well as ESP-IDF projects.

#include <esp_heap_caps.h>

uint32_t freeHeapBytes = heap_caps_get_free_size(MALLOC_CAP_DEFAULT);
uint32_t totalHeapBytes = heap_caps_get_total_size(MALLOC_CAP_DEFAULT);
float percentageHeapFree = freeHeapBytes * 100.0f / (float)totalHeapBytes;

// Print to serial
Serial.printf("[Memory] %.1f%% free - %d of %d bytes free\n", percentageHeapFree, freeHeapBytes, totalHeapBytes);

Also see How to find number of free bytes in ESP32 memory / heap?

Posted by Uli Köhler in ESP8266/ESP32

How to find number of free bytes in ESP32 memory / heap?

This approach works for Arduino / PlatformIO as well as ESP-IDF projects.

#include <esp_heap_caps.h>

uint32_t freeHeapBytes = heap_caps_get_free_size(MALLOC_CAP_DEFAULT);

 

Posted by Uli Köhler in ESP8266/ESP32

How to use vPortGetHeapStats() on the ESP32?

Problem:

You see an error message like the following one while compiling your ESP32 project

src/main.cpp:128:3: error: 'vPortGetHeapStats' was not declared in this scope
   vPortGetHeapStats(&heapStats);

Solution:

Although vPortGetHeapStats() is typically defined in freertos/portable.h, you can not use   vPortGetHeapStats() on the ESP32 since the frameworks do not use the FreeRTOS heap implementation.

In order to find informatio about heap usage, use the ESP heap API such as esp_get_free_heap_size().

Posted by Uli Köhler in ESP8266/ESP32, FreeRTOS

What caps argument to give to ESP32 heap_caps_…() functions?

Almost all of the ESP32 heap_caps_...() functions take a uint32_t caps argument.

In case you just want to have general information about the heap, use

MALLOC_CAP_DEFAULT

as an argument.

Most applications will rarely use any other value than MALLOC_CAP_DEFAULT. Other values which are used semi-frequently include:

  • MALLOC_CAP_SPIRAM
  • MALLOC_CAP_INTERNAL (memory must not be located in SPI RAM)
For more details on how the memory of the ESP32 is organized, see the official documentation.
Posted by Uli Köhler in ESP8266/ESP32

ESP32 minimal heap_caps_print_heap_info() example (PlatformIO/Arduino)

On the ESP32, you can use heap_caps_print_heap_info() to print information to the serial port about how much memory is free on the heap (plus other details such as the largest free block).

#include <esp_heap_caps.h>

void setup() {
}

void loop() {
  heap_caps_print_heap_info(MALLOC_CAP_8BIT);
}

Example output

Heap summary for capabilities 0x00000004:
  At 0x3ffb8000 len 6688 free 0 allocated 4404 min_free 0
    largest_free_block 0 alloc_blocks 8 free_blocks 0 total_blocks 8
  At 0x3ffb0000 len 25480 free 0 allocated 22204 min_free 0
    largest_free_block 0 alloc_blocks 70 free_blocks 0 total_blocks 70
  At 0x3ffae6e0 len 6192 free 8 allocated 3860 min_free 8
    largest_free_block 0 alloc_blocks 10 free_blocks 1 total_blocks 11
  At 0x3ffb6388 len 7288 free 0 allocated 4524 min_free 0
    largest_free_block 0 alloc_blocks 38 free_blocks 0 total_blocks 38
  At 0x3ffb9a20 len 16648 free 8 allocated 13964 min_free 0
    largest_free_block 0 alloc_blocks 32 free_blocks 1 total_blocks 33
  At 0x3ffcc5d0 len 80432 free 8 allocated 73140 min_free 8
    largest_free_block 0 alloc_blocks 320 free_blocks 1 total_blocks 321
  At 0x3ffe0440 len 15072 free 0 allocated 12260 min_free 0
    largest_free_block 0 alloc_blocks 41 free_blocks 0 total_blocks 41
  At 0x3ffe4350 len 113840 free 18440 allocated 90724 min_free 2560
    largest_free_block 7796 alloc_blocks 157 free_blocks 12 total_blocks 169
  Totals:
    free 18464 allocated 225080 min_free 2576 largest_free_block 7796

 

Posted by Uli Köhler in Arduino, ESP8266/ESP32, PlatformIO

FreeRTOS task queue with static memory (xQueueCreateStatic) minimal example

Also see our previous post FreeRTOS task queue minimal example which also has examples for how to send & receive with a queue. The post you’re currently viewing is just about xQueueCreateStatic()

enum class MQTTTaskType : uint8_t {
    SendStatus = 0,
    SendInfo
};

// This struct will be inserted into the queue
struct MQTTTask {
    MQTTTaskType task; // The type of work that is requested from the received
    // TODO add your custom fields here if requred
};

constexpr size_t MQTT_TASK_QUEUE_LENGTH = 6;
static QueueHandle_t mqttTaskQueue;
static StaticQueue_t mqttTaskQueueStatic;
static uint8_t mqttTaskQueueStorageArea[ MQTT_TASK_QUEUE_LENGTH * sizeof(MQTTTask) ];

void setup() {
    // Create task queue
    mqttTaskQueue = xQueueCreateStatic( MQTT_TASK_QUEUE_LENGTH,
                                 sizeof(MQTTTask),
                                 mqttTaskQueueStorageArea,
                                 &mqttTaskQueueStatic );
}

 

Posted by Uli Köhler in C/C++, Embedded, FreeRTOS, PlatformIO

What does mbed-tls error code -0x0010 mean?

If you see an error message like the following one on your microcontroller (such as ESP32):

E (46462) esp-tls-mbedtls: mbedtls_ssl_handshake returned -0x0010

this means MBEDTLS_ERR_MPI_ALLOC_FAILED. In other words, mbedtls can’t allocate enough memory for its operation.

In order to fix this, try to reduce the amount of memory other parts of your application consume.

Posted by Uli Köhler in Arduino, Embedded, ESP8266/ESP32, mbed

What does mbed-tls error code -0x2700 mean?

If you see an error message like the following one on your microcontroller (such as ESP32):

E (137011) esp-tls-mbedtls: mbedtls_ssl_handshake returned -0x2700

this means MBEDTLS_ERR_X509_CERT_VERIFY_FAILED.

Either you are using the wrong certificate on the server or you are using the wrong certificate on the mbed-tls side for verifying the certificate.

In order to check the server side, it is often helpful to check the server’s TLS certificate using OpenSSL:

openssl s_client -connect myhostname.com:443

 

Posted by Uli Köhler in Arduino, Embedded, ESP8266/ESP32, mbed

What does mbed-tls error code -0x3F80 mean?

When you see an error message such as

E (169535) esp-tls-mbedtls: mbedtls_ssl_handshake returned -0x3F80

on your microcontroller (e.g. ESP32), this means

MBEDTLS_ERR_PK_ALLOC_FAILED

In other words, there is not enough memory for mbed-tls to work – specifically, there is not enough memory to allocate the public key. Try to reduce the memory usage of your application.

Posted by Uli Köhler in ESP8266/ESP32, mbed

What does mbed-tls error code -0x3B00 mean

If you see an error message like the following one on your microcontroller (such as ESP32):

E (41544) esp-tls-mbedtls: mbedtls_ssl_handshake returned -0x3B00

this means MBEDTLS_ERR_PK_INVALID_PUBKEY.

As of the version of mbed TLS used in esp-idf v4.4.3, only RSA & (certain types of) Elliptic Curve keys are supported. In my tests, X25519/EC256 keys didn’t work and there were indications that P-384 keys also didn’t work. Generally, using RSA keys is a safe bet when working with mbed-tls.

Posted by Uli Köhler in Arduino, Embedded, ESP8266/ESP32, mbed

FreeRTOS task with static stack memory (xTaskCreateStatic) example

Also see our previous post which uses dynamically allocated memory using xTaskCreate()How to add FreeRTOS task (thread) to any PlatformIO project

#include <freertos/FreeRTOS.h>
#include <freertos/task.h>

constexpr size_t MY_TASK_STACK_SIZE = 1024;
static StaticTask_t myTaskBuffer;
static StackType_t myTaskStack[ MY_TASK_STACK_SIZE ];


void MyTask(void * parameter)
{
    while(true)
    {
        // TODO Your code goes here
    }
}

void setup()
{
    xTaskCreateStatic(
        MyTask, // Task function
        "MyTask", // Name
        MY_TASK_STACK_SIZE, // Stack size
        nullptr, // Parameter
        tskIDLE_PRIORITY,
        myTaskStack,
        &myTaskBuffer);
}

void loop() {
}

 

Posted by Uli Köhler in Arduino, FreeRTOS

What does mbed-tls error code -0x7F00 mean?

When you see an error message such as

E (61175) esp-tls-mbedtls: mbedtls_ssl_setup returned -0x7F00

on your microcontroller (e.g. ESP32), this means

MBEDTLS_ERR_SSL_ALLOC_FAILED

In other words, there is not enough memory for mbed-tls to work. Try to reduce the memory usage of your application.

Posted by Uli Köhler in ESP8266/ESP32, mbed

How to fix GCC error: unknown type name ‘size_t’ (STM32)

Problem:

When you try to compile your C/C++ project (typically a STM32 project):

C:/Users/User/MyProject/MyHeader.h:9:7: error: unknown type name 'size_t'
    9 | const size_t MySize = 15;
      |       ^~~~~~

Solution:

At the top of the file where this error occurs, add the following line:

#include <stddef.h>

 

Posted by Uli Köhler in C/C++, GCC errors, STM32

PySerial minimal RFC2217 example: Copy data received from serial port to stdout

Also see the same example for a local serial port: PySerial minimal example: Copy data received from serial port to stdout

This example connects to the RFC2217 remote serial port on 10.1.2.3 port 1234. It does not send data to the serial port but only copies data received from the serial port to stdout.

#!/usr/bin/env python3
import serial
with serial.serial_for_url("rfc2217://10.1.2.3:1234", baudrate=115200) as ser:
    try:
        while True:
            response = ser.read()
            if response:
                print(response.decode("iso-8859-1"), end="")
    finally:
        ser.close()

 

By using iso-8859-1   decoding, we ensure that even binary bytes are decoded in some way and do not cause an exception.

Posted by Uli Köhler in Embedded, Python

LumenPnP: Always assign the same serial port number on Linux

It is certainly a hassle when you always have to re-configure OpenPnP to open the correct serial device for LumenPnP.

In order to fix, this, we’ll create an alias /dev/lumenpnp that points to /dev/ttyACM0 or /dev/ttyACM1 or any other port that gets assigned to LumenPnP. Create /etc/udev/rules.d/99-lumenpnp.rules:

ACTION=="add", ENV{ID_VENDOR_ID}=="0483", ENV{ID_MODEL_ID}=="5740", SYMLINK+="lumenpnp"

Now, reload udev to activate the rule:

sudo udevadm control --reload-rules && sudo udevadm trigger

Now, open ~/.openpnp2/machine.xml, find this line:

<serial line-ending-type="LF" port-name="ttyACM0" baud="115200" ...

and set port-name to lumenpnp:

<serial line-ending-type="LF" port-name="lumenpnp" baud="115200" ...

After this, you need to restart OpenPnP. Typically, it works without reconnecting the device (due to udevadm trigger). If it doesn’t work, unplug and re-plug the mainboard USB connector.

Posted by Uli Köhler in Linux, LumenPnP

Where to find cheap SMD ferrite beads?

Generally, SMD ferrites of a given size are characterized by:

  • The RF impedance (typically specified @100 MHz)
    • Low impedance (e.g. 100 Ω @ 100 MHz) do not filter EMI that well, but also distort your data signals less. Use them for high speed signals above 1 MHz
    • High impedance ferrites, e.g. 1kΩ @ 100 MHz, filter EMI really well but will distort any high speed signal. Use them for low speed signals below 1 MHz
  • The current rating
    • For data signals, you don’t need any form of high current rating. Generally, I would recommend to require at least 0.1A of current rating to be able to handle soft short circuits
    • For power lines, use whatever power rating is suitable (the specified current + 20% is typically fine)
  • The DC resistance. This mostly only matters for power lines.
    • Keep in mind to keep the ferrite cool by choosing the DC resistance so that the

As with most passive parts, LCSC generally has the lowest pricing if you buy a bunch of parts.

Keep in mind the expensive shipping, but since they are often 50 times cheaper (!) than other distributors, it might be worth it even if just ordering a reel of ferrites.

Signal ferrites

This section contains cheap SMD ferrite beads for < 1A current rating.

Low impedance (<= 500Ω):

  • LCSC 0603 100Ω 200mA CBG160808U101T @ 0.0019€/pc@4000pc = 7.60€/reel

High impedance (>= 500Ω):

  • LCSC 0805 600Ω 300mA CBG201209U601T @ 0.0026€/pc@4000pcs = 10.40€/reel
  • LCSC 0603 1kΩ 400mA FCM1608KF-102T04 @ 0.0027€/pc@4000pc = 10.80€/reel
  • LCSC 0805 1kΩ 800mA CBW201209U102T @ 0.0044€/pc@4000pc = 17.60€/reel
  • DigiKey 0603 1kΩ 400mA 2506031027Y0 @ 0.01264€/pc@4000pc = 50.56€/reel
  • TME 0805 1kΩ 200mA LCCB-102 @ 0.0181€/pc = 72.40€/reel

Power ferrites

This section contains cheap SMD ferrite beads for > 1A current rating.

Low impedance (<= 500Ω):

  • LCSC 1206 120Ω 3A CBW321609U122T @ 0.006€/pc@4000pc = 24. 00€/reel
  • LCSC 1206 120Ω 4A CBM321609U121T @ 0.0086€/pc@4000pc = 34.40€/reel

High impedance (>= 500Ω):

  • LCSC 1206 1.2kΩ 1A CBW321609U122T @ 0.0088€/pc@4000pc = 35.20€/reel
  • LCSC 1206 1kΩ 2A CBW321609U102T @ 0.0095€/pc@4000pc = 38.00€/reel
Posted by Uli Köhler in Components, Electronics

Where to find cheap general purpose SMD optocouplers?

I generally use the LTV-356T optocoupler (phototransistor output) for general purpose applications. It has a smaller footprint than most DIP-derived SMD optocouplers and a thinner package. Its pin pitch is 2.54mm whereas the center-distance between the pads is 6.5mm. It it rated

It is available:

Even cheaper?

The LTV-356T has the advantage of being available from different sources in large quantities. However, you can get a pin-compatible variant LTV-356T-C which is only rated for 35V on the output:

Even smaller?

At LCSC, you can also get the half-width OR-3H7B-TP-Gwith a pin pitch of only 1.27mm for 0,0424€/pc@1kpc

 

Posted by Uli Köhler in Components, Electronics

Where to find cheap general purpose resistors?

Typically, 1% thick film resistors are used as general purpose SMD resistors. Most of these are spec’d with a 100ppm/°C temperature coefficient.

From all the “normal” distributors, in my experience LCSC has by far the cheapest resistors – around 3€ per reel (5000pcs) of 0603 resistors. Note, however, that they charge around 35€ of shipping to Germany. So you have to buy a lot of resistors in order to compensate for the expensive shipping. However, you can also order other components on LCSC (they’re usually quite cheap, especially for passives and other standard components).

0603 resistors

 

Posted by Uli Köhler in Components, Electronics

How to fix ESP32 FreeRTOS error: too few arguments to function ‘void vPortEnterCritical(portMUX_TYPE*)’

Problem:

On FreeRTOS on the ESP32, you want to use a critical zone like this:

portENTER_CRITICAL();
// Your critical code goes here!
portEXIT_CRITICAL();

but while compiling the procject, you see an error message like

src/main.cpp: In function 'void MyFunc(size_t, int16_t)':
/home/uli/.platformio/packages/framework-arduinoespressif32@src-f2ea83e2545300b10a69ff44ef9dc6cd/tools/sdk/esp32/include/freertos/port/xtensa/include/freertos/portmacro.h:476:75: error: too few arguments to function 'void vPortEnterCritical(portMUX_TYPE*)'
 #define portENTER_CRITICAL(mux)                     vPortEnterCritical(mux)

Solution:

You need to use portENTER_CRITICAL() and portEXIT_CRITICAL() with a spinlock, i.e.

portENTER_CRITICAL(&mySpinlock);
// TODO Your critical code goes here!
portEXIT_CRITICAL(&mySpinlock);

In order to see a full example on how to initialize a spinlock in FreeRTOS and use it for critical zones, see our previous post ESP32 critical zone example using FreeRTOS / PlatformIO

Posted by Uli Köhler in Arduino, ESP8266/ESP32, FreeRTOS, PlatformIO