Normalizing electronics engineering value notations using Python

In electronics engineering there is a wide variety of notations for values that need to be recognized by intuitive user interfaces. Examples include:

  • 1fA
  • 0.1A
  • 0.00001
  • 1e-6
  • 4,5nA
  • 4,500.123 A
  • 4A5
  • 4k0 A

The wide variety of options, including thousands separators, comma-as-decimal-separator and suffix-as-decimal-separator, optional whitespace and scientific notations makes it difficult to normalize values without using specialized libraries. Continue reading →

Posted by Uli Köhler in Electronics, Python

Using Arduino Leonardo as an USB/UART adapter

In contrasts to older designs like the Arduino Uno, the Arduino Leonardo features a separate connection Serial1 for TTLUART whereas Serial is used for the USB CDC UART interface.

This allows one to use the Leonardo as an USB/UART bridge without having to resort to more expensive boards like the Arduino Mega 2560. In order to do this, use this sketch which can also be modified to provide an intelligent UART bridge.

Remember to adjust the baudrate for your application. This version of the sketch does not support automatic baudrate selection via the CDC peripheral.

Continue reading →

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

Accessing lwIP struct netif instance in ChibiOS


You are using the ChibiOS lwIP binding to access the network your microcontroller application.

You need to access the lwIP struct netif structure, for example to get the current DHCP IP address assigned to the network interface. However, the default ChibiOS implementaton in lwipthread.c does not export the interface structure.
Continue reading →

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

Engineering for the super-lazy: Solving equations without activating your brain


In electronics engineering, from time to time you have to use standard formulas to characterize your circuits. To what extent you need to calculate all parameters most often depends on the requirement.

For example, consider the formula for the -3dB cutoff frequency of a 1st order RC lowpass filter:

f_c=\frac{1}{2\pi RC}

Although this equation is fairly simple and most people won’t have any problem solving it for any particular variable in a few seconds, it can serve as a basic example on how to solve an equation symbolically.

One of the easiest ways of performing this task is to use SymPy, a Python library for symbolic mathematics.

Continue reading →

Posted by Uli Köhler in Python

Reading the STM32 unique device ID in C

All STM32 microcontrollers feature a 96-bit factory-programmed unique device ID. However, for me it was hard to find an adequately licensed example on how to read it in a manner compatible with different families and compilers.

Here’s a simple header that defines a macro for the device ID address. While I checked the address for both STM32F4 and STM32F0 families, other families might have slightly different addresses for the device ID. Check the reference manual corresponding to your STM32 family if errors occur.

Continue reading →

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

Reading STM32F0 internal temperature and voltage using ChibiOS

The STM32F0 series of 32-bit ARM Cortex M0 microcontrollers contain a huge number of internal peripherals despite their low price starting at 0,32€ @1pc. Amongst them is an internal, factory-calibrated temperature sensor and a supply voltage sensor (that specifically senses VDDA, the analog supply voltage rail) connect to channels 16 and 17 of the internal ADC.

While I usually like the STM32 documentation, it was quite hard to implement code that produced realistic values. While the STM32F0 reference manual contains both formulas and a short section of example code, I believe that some aspects of the calculation are understated in the computation:

Section 13.9 in RM0091 provides a formula for computing the temperature from the raw temperature sensor output and the factory calibration values. However it is not stated anywhere (at least in Rev7, the current RM0091 revision) that this formula is only correct for a VDDA of exactly 3.30V.

Continue reading →

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

Fixing bad blocks on HDDs using


You hard drive or SMART tool reports errors when reading specific blocks similar to this message:

[3142.686141] end_request: I/O error, dev sda, sector 31415926

No matter how often you read the block, the hard drive still returns an error and does not reallocate the block.

Continue reading →

Posted by Uli Köhler in Linux, Python

Listing files inside a Torrent

Torrent files are essentially containers that store information about a set of files to be downloaded via P2P.

Unfortunately it is not easily possible to simply list the files that are stored in a torrent. Using our python script you can easily accomplish that task.

Continue reading →

Posted by Uli Köhler in Python

Reading the UniProt text format in Python

Just like many other databases in computational biology, the downloads for the popular UniProt database are available in a custom text format which is documented on ExPASy.

While it is certainly not difficult writing a generic parser and one can use BioPython, I believe it is often easier to use a tested parser that only uses standard libraries.

Continue reading →

Posted by Uli Köhler in Bioinformatics, Python

Using the lwIP SNTP client with ChibiOS

A common task with embedded systems is to use the RTC to timestamp events. However, the system architect needs to find a way of synchronizing the devices RTC time with an external time source. Additionally, the designer needs to deal with the problem of drifting RTC clocks, especially for long-running devices. This article discusses an lwIP+SNTP-based approach for STM32 devices using the ChibiOS RTOS. The lwIP-specific part of this article is also applicable to other types of microcontrollers.

For high-accuracy or long-running applications, RTC clock drift also has to be taken into account. Depending on the clock source in use, the clock frequency can deviate significantly from the nominal value.

On the STM32F4 for example, you can derive the RTC clock from: The HSE/HSI main oscillator The LSI oscillator * The LSE oscillator, i.e. a 32.768 kHz external crystal.

Continue reading →

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

Enforcing debugger breakpoints in ChibiOS chDbgAssert()

ChibiOS provides a variety of debug functions that can be enabled or disabled using preprocessor definitions.

When debugging ARM-based microcontrollers like the STM32, it can be useful to hardcode

This post provides a simple method of improving the definition of chDbgAssert() in chdebug.h so that a breakpoint is enforced in case of assertion failures. By using the ARM BKPT instruction, the overhead is only a single assembler instruction.

Continue reading →

Posted by Uli Köhler in Embedded

Reading STM32 unique device ID using OpenOCD

When working with the STM32 family of microcontrollers, it can be useful to evaluate the factory-programmed 96-bit UUID using JTAG. On all major operating systems, OpenOCD provides a simple yet highly compatible and free solution in order to do this.

In this example, we’ll use a JLink adapter together with the Olimex E407 evalation board. Both the adapter and the board are interchangable, provided you have working OpenOCD configurations available.

Continue reading →

Posted by Uli Köhler in Embedded

Extracting open library author names using Haskell Conduits

Almost anyone productively using Haskell has already stumbled upon Michael Snoyman’s Conduit library.

In this post I will show how to leverage the power of Conduit interleaved IO in order to parse author’s names from the Open Library data dumps.

Using traditional languages like Java or C, it would be significantly more difficult and error-prone to interleave the processing pipeline actions of

  • Downloading the file ol_dump_authors_latest.txt.gz using HTTP
  • Decompressing using a gunzip-like function
  • Splitting into lines and discarding everything but the JSON from said lines
  • Appropriately decoding the JSON and extracing the name field, or ignoring the line if parsing is not possible
  • Writing all said names into a files (authors.txt in this example), one per line.

Continue reading →

Posted by Uli Köhler in Haskell