# Electronics

## mbed STM32 timer interrupt example

You can use the mbed Ticker API to add a timer interrupt to your mbed application. This example will use a Ticker-based timer interrupt to toggle the LED once per second, for example on the STM32F429I-DISCO board:

#include <mbed.h>

DigitalOut led1(LED1);
Ticker ticker;

/**
* This function will be run once per second
*/
void timerTick() {
// Toggle LED
led1 = !led1;
}

int main() {
ticker.attach(timerTick, 1.0 /* seconds */);

// What you do in the main loop is not important
while(1) {
}
}


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

## How to fix Raspberry Pi OpenVPN error “ERROR: Cannot open TUN/TAP dev /dev/net/tun: No such device (errno=19)”

### Problem:

You want to setup OpenVPN on your Raspberry Pi but you see an error message like

Fri Jun 26 18:12:35 2020 ERROR: Cannot open TUN/TAP dev /dev/net/tun: No such device (errno=19)
Fri Jun 26 18:12:35 2020 Exiting due to fatal error

### Solution:

This error occurs if you’ve installed OpenVPN using sudo apt install -y openvpn but if you didn’t reboot after installing it. In order to fix the issue, reboot using

sudo reboot

Posted by Uli Köhler in Linux, Raspberry Pi

## How to fix PlatformIO mbed error: ‘Mutex’ does not name a type

### Problem:

You are trying to compile your PlatformIO mbed application using Mutexes like

Mutex myLock;

but you see an error message like

src\main.cpp:3:1: error: 'Mutex' does not name a type
Mutex myLock;
^~~~~

### Solution:

Add this line to your platformio.ini:

build_flags = -D PIO_FRAMEWORK_MBED_RTOS_PRESENT

This will enable the RTOS features in mbed, including the Mutex.

Posted by Uli Köhler in mbed, PlatformIO

## How to fix PlatformIO fatal error: stm32f429i_discovery_lcd.h: No such file or directory

### Problem:

You are trying to compile your PlatformIO application using  the LCD_DISCO_F429ZI library, but you see an error message like

 #include "stm32f429i_discovery_lcd.h"
^~~~~~~~~~~~~~~~~~~~~~~~~~~~
compilation terminated.
*** [.pio\build\disco_f429zi\src\main.o] Error 1
In file included from .pio\libdeps\disco_f429zi\LCD_DISCO_F429ZI_ID2432\LCD_DISCO_F429ZI.cpp:19:0:
.pio\libdeps\disco_f429zi\LCD_DISCO_F429ZI_ID2432\LCD_DISCO_F429ZI.h:25:10: fatal error: stm32f429i_discovery_lcd.h: No such file or directory


### Solution:

First, ensure that BSP_DISCO_F429ZI is listed is library dependency in platformio.ini like this:

[env:disco_f429zi]
platform = ststm32
board = disco_f429zi
framework = mbed
lib_deps =
LCD_DISCO_F429ZI
BSP_DISCO_F429ZI

Replace the line

#include "stm32f429i_discovery_lcd.h"

in LCD_DISCO_F429ZI.h with

#include "Drivers/BSP/STM32F429I-Discovery/stm32f429i_discovery_lcd.h"

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

## How to fix PlatformIO fatal error: ../Fonts/fonts.h: No such file or directory

### Problem:

You are trying to compile a PlatformIO application using the BSP_DISCO_F429ZI library, but you see an error message like

In file included from .pio\libdeps\disco_f429zi\BSP_DISCO_F429ZI_ID2208\Drivers\BSP\STM32F429I-Discovery\stm32f429i_discovery_lcd.c:75:0:
.pio\libdeps\disco_f429zi\BSP_DISCO_F429ZI_ID2208\Drivers\BSP\STM32F429I-Discovery\stm32f429i_discovery_lcd.h:49:10: fatal error: ../Fonts/fonts.h: No such file or directory
#include "../Fonts/fonts.h"
^~~~~~~~~~~~~~~~~~
compilation terminated.

### Solution:

The BSP_DISCO_F429ZI package includes fonts.h from the wrong directory. Replace

#include "../Fonts/fonts.h"

by

#include "Utilities/Fonts/fonts.h"

in order to fix the issue. You might need to do that multiple times (in multiple files) in order to fix your build.

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

## How to use printf in mbed using STM32F429I-DISC1 and PlatformIO

In PlatformIO, you can directly use printf without any special configuration for the STM32F429-DISC1 discovery board:

#include <mbed.h>

int main() {
while(1) {
printf("Hello world\n");
wait(0.5);
}
}

This program will print Hello world twice every second. You can watch the output using the PlatformIO Monitor feature.

Posted by Uli Köhler in mbed

## How to toggle the STM32F429I-DISCOVERY LED using mbed + PlatformIO

This simple firmare toggles the LED on the STM32F429I-DISC1 discovery board.

#include <mbed.h>

DigitalOut myled(LED1);

int main() {
while(1) {
myled = !myled;
wait(0.5);
}
}

This will toggle the green (PG13) LED twice per second.

The program is simple: We toggle the LED using myled = !myled; and then use wait(0.5) for 0.5 seconds.

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

## What .gitignore to use for LTSpice XVII projects?

Besides the .asc files containing the circuit to simulate, LTSpice XVII genates .log.raw and .op.raw files during simulation. Usually, you don’t want to commit those files.

This is the .gitignore file I use to manage my LTSpice XVII projects:

*.log
*.raw
Posted by Uli Köhler in Electronics

## How to fix raspi-config “The splash screen is not installed so cannot be activated”

### Problem:

You want to enable the boot splash screen on your Raspberry Pi using raspi-config, but you see this error message:

The splash screen is not installed so cannot be activated

followed by There was an error running option B3 Splash Screen

### Solution:

As you can find out from reading the raspi-config source code, it checks for the existence of /usr/share/plymouth/themes/pix/pix.script. In order to install this file, install the rpd-plym-splash package:

sudo apt -y install rpd-plym-splash
Posted by Uli Köhler in Embedded, Linux, Raspberry Pi

## How to manually reload Chromium Kiosk

### Problem:

You are running a Chromium Kiosk e.g. on a Raspberry Pi using a command like

chromium-browser --noerrdialogs --disable-infobars --disk-cache-dir=/dev/null --disk-cache-size=1 --kiosk http://localhost

e.g. in /etc/xdg/openbox/autostart, but you don’t know how to manuy reload the Kiosk e.g. after you have changed the underlying website

### Solution:

In /etc/xdg/openbox/autostart or wherever your chromium-browser command is, enclose it in

while true ; do [CHROMIUM COMMAND] ; sleep 1 ; done

The complete command would look like this, for example:

while true ; do chromium-browser  --noerrdialogs --disable-infobars --disk-cache-dir=/dev/null --disk-cache-size=1 --kiosk http://localhost ; sleep 1 ; done

Now, to manually reload Chromium, all you have to do is to kill the process using

killall /usr/lib/chromium-browser/chromium-browser-v7

This will kill the Chromium process and the while loop will automatically restart it after one second.

In case you see an error message like

/usr/lib/chromium-browser/chromium-browser-v7: No such file or directory

you need to find out which executable is used for Chromium in order to pass that to killall. To find out the name of the executable, use

ps a | grep -i chromium

and look for a string similar to /usr/lib/chromium-browser/chromium-browser-v7.

Posted by Uli Köhler in Embedded, Linux

## How to fix Chromium Kiosk still displaying old page after reboot

### Problem:

You are running a Chromium Kiosk application on an embedded computer (like a Raspberry Pi) using a command like

chromium-browser --noerrdialogs --disable-infobars --kiosk http://localhost

but when you update the webpage, Chromium still displays the old page even after a reboot.

### Solution:

--disk-cache-dir=/dev/null --disk-cache-size=1

to the command (which is typically found in /etc/xdg/openbox/autostart). The full command will look like this:

chromium-browser --noerrdialogs --disable-infobars --disk-cache-dir=/dev/null --disk-cache-size=1 --kiosk http://localhost
Posted by Uli Köhler in Embedded, Linux

## What .gitignore to use for KiCAD projects?

KiCAD generates a lot of backup-like files, for example MyPCB.sch-bak that should be ignored by git and don’t need to be committed to the repository.

This is the .gitignore file I use to manage my KiCAD projects:

*-bak
*-cache*
*-bak*
_autosave*

Note that TechOverflow’s KiCAD project initialization script automatically generates this.gitignore.

Posted by Uli Köhler in KiCAD

## How to fix bricked Arduino MKR Zero using the RESET button

After I uploaded a broken firmware to my Arduino MKR Zero, it didnt enumerate over USB any more, hence it was not possible to upload a new firmware any more.

However, you can put the Arduino into perpetual bootloader mode by pressing the RESET button twice quickly. In this mode, the Arduino will never attempt to start the broken firmware, so you will be able to upload a new firmware again.

Posted by Uli Köhler in Arduino, Embedded

## How to fix Keil EFM8 Warning C280: unreferenced local variable

### Problem:

You have a C function like

int myfunc(int value) {
return 0;
}

but you see a Keil compiler warning like

*** WARNING C280 IN LINE 7 OF C:\Users\uli\MyProject\src\main.c: 'value': unreferenced local variable

### Solution:

'value': unreferenced local variable means that you don’t use that variable value in any way.

In the function shown above, you can see that value is the argument of myfunc but myfunc never actually uses the variable.

#### Do you think that variable should be used in this function?

You need to check your function for typos – the variable is never used at all. Possibly you are using the wrong variable or your function is missing some part of its logic.

#### Don’t want to use that variable at all?

Usually you can tell the compiler that you don’t want to use that variable by using

(void)value;

but that will produce an expression with possibly no effect warning.

*** WARNING C275 IN LINE 7 OF C:\Users\uli\MyProject\src\main.c: expression with possibly no effect

You can use this hack to avoid this warning:

r = r; // Avoid unused local variable

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

## How to fix Keil EFM8 ERROR C267 ‘SLAB_ASSERT’: requires ANSI-style prototype

### Problem:

You want to compile your EFM8 application but you see these error messages:

*** WARNING C206 IN LINE 86 OF C:\SiliconLabs\SimplicityStudio\v4\developer\sdks\8051\v4.1.7\Device\EFM8BB3\peripheral_driver\src\adc_0.c: 'SLAB_ASSERT': missing function-prototype
*** ERROR C267 IN LINE 86 OF C:\SiliconLabs\SimplicityStudio\v4\developer\sdks\8051\v4.1.7\Device\EFM8BB3\peripheral_driver\src\adc_0.c: 'SLAB_ASSERT': requires ANSI-style prototype

### Solution:

Go into your Project properties (by right-clicking on your project on the top left and clicking “Properties”) -> C/C++ Build -> Project Modules

There, select 8051 -> EFM8 and check the Assert checkbox to include the assertion module. Now recompile your module.

Posted by Uli Köhler in EFM8, Embedded

## How to fix Keil error C129: “missing ‘;'” without any missing semicolon (EFM8)

### Problem:

You have source code like

uint16_t convert(uint16_t input) {
return input * 2;
}


that you want to compile using EFM8

// *** ERROR C129 IN LINE 1 OF C:\Users\user\TestProject\src\Test.c: missing ';' before 'convert'

### Solution:

Keil doesn’t know what the uint16_t before the declaration of convert().

#include <stdint.h>

stdint.h contains declarations for several standard integer types like uint8_t, uint16_t, uint32_t, int16_t etc.

The final code should look like this:

#include <stdint.h>

uint16_t convert(uint16_t input) {
return input * 2;
}


Posted by Uli Köhler in EFM8

## What does SCON0_TI = 1 mean for the EFM8?

SCON0 is the UART0 serial port control register (Serial CONtrol 0). The TI bit enables the Transmit Interrupt, i.e. when the transmission of a byte is finished, the CPU will be interrupted.

Setting

SCON0_TI = 1;

is neccessary for some of the EFM8 UART libraries (like the STDIO UART library) so they initialize properly. Although this is a simplification, the library needs to know that there is no UART transmission going on at the moment.

Also, don’t forget to enable global interrupts using

IE_EA = 1;

else, the UART0 transmit interrupt will never be run.

Posted by Uli Köhler in EFM8, Embedded

## What’s the difference between current DACs (IDACs) and voltage DACs (VDACs)?

If you are trying to to select a Digital-to-Analog-converter (DAC) you have the choice between a voltage DAC (VDAC) and a current DAC (IDACs).

• Voltage DACs output a voltage that varies with the DAC setting. For example, a voltage DAC might output between 0 and 3.3 Volts.
• Current DACs output a current that varies with the DAC setting. For example, a current DAC might output between 0 and 2 mA

#### How to select the right type of DAC?

If you have to ask what type of DAC you need, you probably need a voltage DAC. Most DACs nowadays are voltage DACs.

Current DACs are usually used for some types of sensor excitation (see e.g. the ADS1147) where constant current is required, or in more complex analog devices.

Posted by Uli Köhler in Electronics

## Arduino MKR Zero schematic library for KiCAD

This library provides a schematic symbol for the Arduino MKR zero’s 2.54mm (0.1″) headers. Currently I don’t have the footprint

This KiCAD library is hereby released into the public domain (CC0 1.0 Universal)

This image shows the Arduino MKR Zero schematic symbol from the library.

Posted by Uli Köhler in Electronics, KiCAD

## How to use the LDAC pin on DACs (Digital-to-Analog converters)

The $\overline{\text{LDAC}}$ pin on Digital-to-Analog converters (DACs) provides a convenient way to tell the DAC when an output.

For example, if you have an 8-channel I²C DAC, you need to set the DAC registers for the channels one after another using the I²C protocol, but often you want all channels to change their analog outputs at the same time (after writing all the values to the corresponding registers).  The $\overline{\text{LDAC}}$ pin allows you to do that by driving it high (e.g. 3.3V).

Using it is simple:

• If you don’t want to use the $\overline{\text{LDAC}}$ pin, tie it to GND. This will cause the DAC’s outputs to be updated immediately whenever the registers or corresponding inputs are changed.
• Don’t leave the $\overline{\text{LDAC}}$ pin floating ! Always connect it to an output of e.g. a microcontroller or tie it to GND
• If you want to use the pin, connect it to a GPIO output of a microcontroller. By default, drive it high (e.g. to 3.3V). Whenever you are finished updating the DAC register, drive it low for at least 10 microseconds (typical value that will work for most DACs). The DAC will then update its analog outputs, all at once.
Posted by Uli Köhler in Electronics