How to get first day of next month in Javascript using DayJS

See the dayjs docs for more info about the library.

const now = dayjs();
const nextMonth = now.month(now.month() + 1).date(1).hour(0).minute(0).second(0);

// Example usage
console.info(nextMonth.format())
// Prints, for example, "2022-03-01T00:00:00+01:00"

This will also work in december as DayJS processes month(13) correctly:

const now = dayjs('2021-12-09');
const nextMonth = now.month(now.month() + 1).date(1).hour(0).minute(0).second(0);

// This prints '2022-01-01T00:00:00+01:00'
console.info(nextMonth.format());

 

Posted by Uli Köhler in Javascript

PyVISA Rigol DL3021 via LXI (TCP SCPI) example

First install using

sudo pip install pyvisa pyvisa-py

The following example will connec to a LXI-enabled (license required!!!) DL3021 at IP address 192.168.178.31

import pyvisa
import time

rm = pyvisa.ResourceManager()
inst = rm.open_resource("TCPIP0::192.168.178.112::INSTR")
# Query if instrument is present
# Prints e.g. "RIGOL TECHNOLOGIES,DL3021,DL3A204800938,00.01.05.00.01"
print(inst.query("*IDN?"))

# Set to constant resistance mode
inst.write(":SOURCE:FUNCTION RESISTANCE")
# Set to 3 Ohms
inst.write(":SOURCE:RESISTANCE:LEVEL:IMMEDIATE 3.0")
# Enable electronic load
inst.write(":SOURCE:INPUT:STATE On")
# Wait for value to stabilize
time.sleep(2)
# Measure!
print("Voltage: ", inst.query(":MEASURE:VOLTAGE?").strip())
print("Current: ", inst.query(":MEASURE:CURRENT?").strip())
print("Power: ", inst.query(":MEASURE:POWER?").strip())

This will print, for example:

RIGOL TECHNOLOGIES,DL3021,DL3A204800938,00.01.05.00.01

Voltage:  2.885810
Current:  0.961862
Power:  2.775752

with some power supply attached of course.

Posted by Uli Köhler in Electronics

How to reverse std::string using std::reverse (C++)

#include <iostream>
#include <string>
#include <algorithm>

using namespace std;

int main(int argc, char** argv) {
    std::string s = "My string";
    // This reverses s in-place.
    std::reverse(std::begin(s), std::end(s));
    // Prints "gnirts yM"
    cout << s << endl;
}

 

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

re2 C++ replace (GlobalReplace) minimal example

This minimal example shows how to replace strings using the re2 GlobalReplace() function:

#include <re2/re2.h>
#include <iostream>
#include <string>

using namespace re2;
using namespace std;

// Pre-compiled regex
RE2 myRegex("\\$[^\\$]+\\$");

int main(int argc, char** argv) {
    string myString = "This string $abc123$ contains two formulas $123 != 456$.";
    // NOTE: This will modify myString!
    RE2::GlobalReplace(&myString, myRegex, "FORMULA");
    // Prints "This string FORMULA contains two formulas FORMULA."
    cout << myString << endl;
}

 

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

re2 C++ regular expressions library: How to #include?

#include <re2/re2.h>

using namespace re2;

 

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

cpp-httplib: How to get query parameters

This example shows how to get a query parameter in cpp-httplib. See the cpp-httplib hello example for the full source code:

svr.Get("/hi", [](const httplib::Request& req, httplib::Response& res) {
    std::string id = req.get_param_value("id");
    res.set_content("id=" + id, "text/plain");
});

Access it at http://localhost:8080/hi?id=myid

and it will show you

id=myid

id will be empty (== "") if no such parameter is given.

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

How to fix Paho MQTT undefined reference to `SHA1_Init’

Problem:

When trying to build your C++ application using Paho MQTT, you see a bunch of undefined reference error messages:

MQTTAsync.c:(.text+0x3482): undefined reference to `OpenSSL_version'
/usr/bin/ld: MQTTAsync.c:(.text+0x34a1): undefined reference to `OpenSSL_version'
/usr/bin/ld: MQTTAsync.c:(.text+0x34c0): undefined reference to `OpenSSL_version'
/usr/bin/ld: MQTTAsync.c:(.text+0x34df): undefined reference to `OpenSSL_version'
/usr/bin/ld: MQTTAsync.c:(.text+0x34fe): undefined reference to `OpenSSL_version'
/usr/bin/ld: /home/uli/.conan/data/paho-mqtt-c/1.3.9/_/_/package/6fbd1993993594bd3f9451023a96c5ec2eef0e19/lib/libpaho-mqtt3as.a(MQTTAsyncUtils.c.o): in function `MQTTAsync_closeOnly':
MQTTAsyncUtils.c:(.text+0x9b6): undefined reference to `SSL_SESSION_free'
/usr/bin/ld: /home/uli/.conan/data/paho-mqtt-c/1.3.9/_/_/package/6fbd1993993594bd3f9451023a96c5ec2eef0e19/lib/libpaho-mqtt3as.a(MQTTAsyncUtils.c.o): in function `MQTTAsync_receiveThread':
MQTTAsyncUtils.c:(.text+0x463b): undefined reference to `SSL_set_session'
/usr/bin/ld: MQTTAsyncUtils.c:(.text+0x5255): undefined reference to `SSL_get1_session'
/usr/bin/ld: MQTTAsyncUtils.c:(.text+0x55bb): undefined reference to `SSL_get1_session'
/usr/bin/ld: /home/uli/.conan/data/paho-mqtt-c/1.3.9/_/_/package/6fbd1993993594bd3f9451023a96c5ec2eef0e19/lib/libpaho-mqtt3as.a(SSLSocket.c.o): in function `call_ssl_psk_cb':
SSLSocket.c:(.text+0x11b): undefined reference to `SSL_get_SSL_CTX'
/usr/bin/ld: SSLSocket.c:(.text+0x129): undefined reference to `SSL_CTX_get_ex_data'
/usr/bin/ld: /home/uli/.conan/data/paho-mqtt-c/1.3.9/_/_/package/6fbd1993993594bd3f9451023a96c5ec2eef0e19/lib/libpaho-mqtt3as.a(SSLSocket.c.o): in function `SSLSocket_error':
SSLSocket.c:(.text+0x1fa): undefined reference to `SSL_get_error'
/usr/bin/ld: SSLSocket.c:(.text+0x22d): undefined reference to `ERR_print_errors_cb'
/usr/bin/ld: SSLSocket.c:(.text+0x2f1): undefined reference to `ERR_get_error'
/usr/bin/ld: /home/uli/.conan/data/paho-mqtt-c/1.3.9/_/_/package/6fbd1993993594bd3f9451023a96c5ec2eef0e19/lib/libpaho-mqtt3as.a(SSLSocket.c.o): in function `SSL_CTX_info_callback':
SSLSocket.c:(.text+0x376): undefined reference to `SSL_alert_desc_string_long'
/usr/bin/ld: SSLSocket.c:(.text+0x381): undefined reference to `SSL_alert_type_string_long'
/usr/bin/ld: SSLSocket.c:(.text+0x3bc): undefined reference to `SSL_get_verify_result'
/usr/bin/ld: SSLSocket.c:(.text+0x411): undefined reference to `SSL_state_string_long'
/usr/bin/ld: SSLSocket.c:(.text+0x461): undefined reference to `SSL_get_current_cipher'
/usr/bin/ld: SSLSocket.c:(.text+0x469): undefined reference to `SSL_CIPHER_get_name'
/usr/bin/ld: SSLSocket.c:(.text+0x474): undefined reference to `SSL_state_string_long'
/usr/bin/ld: SSLSocket.c:(.text+0x4d1): undefined reference to `SSL_alert_desc_string_long'
/usr/bin/ld: SSLSocket.c:(.text+0x4dc): undefined reference to `SSL_alert_type_string_long'
/usr/bin/ld: SSLSocket.c:(.text+0x4e7): undefined reference to `SSL_state_string_long'
/usr/bin/ld: SSLSocket.c:(.text+0x503): undefined reference to `SSL_alert_desc_string_long'
/usr/bin/ld: SSLSocket.c:(.text+0x50e): undefined reference to `SSL_alert_type_string_long'
/usr/bin/ld: SSLSocket.c:(.text+0x541): undefined reference to `SSL_alert_desc_string_long'
/usr/bin/ld: SSLSocket.c:(.text+0x54c): undefined reference to `SSL_alert_type_string_long'
/usr/bin/ld: /home/uli/.conan/data/paho-mqtt-c/1.3.9/_/_/package/6fbd1993993594bd3f9451023a96c5ec2eef0e19/lib/libpaho-mqtt3as.a(SSLSocket.c.o): in function `SSLSocket_initialize':
SSLSocket.c:(.text+0x8ee): undefined reference to `OPENSSL_init_ssl'
/usr/bin/ld: SSLSocket.c:(.text+0x90a): undefined reference to `OPENSSL_init_crypto'
/usr/bin/ld: SSLSocket.c:(.text+0x916): undefined reference to `OPENSSL_init_ssl'
/usr/bin/ld: SSLSocket.c:(.text+0x922): undefined reference to `OPENSSL_init_crypto'
/usr/bin/ld: SSLSocket.c:(.text+0xa11): undefined reference to `CRYPTO_get_ex_new_index'
/usr/bin/ld: /home/uli/.conan/data/paho-mqtt-c/1.3.9/_/_/package/6fbd1993993594bd3f9451023a96c5ec2eef0e19/lib/libpaho-mqtt3as.a(SSLSocket.c.o): in function `SSLSocket_createContext':
SSLSocket.c:(.text+0xbee): undefined reference to `SSL_CTX_use_certificate_chain_file'
/usr/bin/ld: SSLSocket.c:(.text+0xc48): undefined reference to `SSL_CTX_set_default_passwd_cb'
/usr/bin/ld: SSLSocket.c:(.text+0xc55): undefined reference to `SSL_CTX_set_default_passwd_cb_userdata'
/usr/bin/ld: SSLSocket.c:(.text+0xc67): undefined reference to `SSL_CTX_use_PrivateKey_file'
/usr/bin/ld: SSLSocket.c:(.text+0xc9a): undefined reference to `SSL_CTX_load_verify_locations'
/usr/bin/ld: SSLSocket.c:(.text+0xce5): undefined reference to `SSL_CTX_set_default_verify_paths'
/usr/bin/ld: SSLSocket.c:(.text+0xd2e): undefined reference to `SSL_CTX_set_cipher_list'
/usr/bin/ld: SSLSocket.c:(.text+0xd56): undefined reference to `SSL_CTX_set_ex_data'
/usr/bin/ld: SSLSocket.c:(.text+0xd66): undefined reference to `SSL_CTX_set_psk_client_callback'
/usr/bin/ld: SSLSocket.c:(.text+0xd8f): undefined reference to `SSL_CTX_ctrl'
/usr/bin/ld: SSLSocket.c:(.text+0xdc5): undefined reference to `SSL_CTX_free'
/usr/bin/ld: SSLSocket.c:(.text+0xe51): undefined reference to `SSL_CTX_set_alpn_protos'
/usr/bin/ld: SSLSocket.c:(.text+0xeb9): undefined reference to `TLS_client_method'
/usr/bin/ld: SSLSocket.c:(.text+0xec1): undefined reference to `SSL_CTX_new'
/usr/bin/ld: /home/uli/.conan/data/paho-mqtt-c/1.3.9/_/_/package/6fbd1993993594bd3f9451023a96c5ec2eef0e19/lib/libpaho-mqtt3as.a(SSLSocket.c.o): in function `SSLSocket_setSocketForSSL':
SSLSocket.c:(.text+0x1046): undefined reference to `SSL_CTX_set_info_callback'
/usr/bin/ld: SSLSocket.c:(.text+0x1056): undefined reference to `SSL_CTX_set_msg_callback'
/usr/bin/ld: SSLSocket.c:(.text+0x1075): undefined reference to `SSL_new'
/usr/bin/ld: SSLSocket.c:(.text+0x10ab): undefined reference to `SSL_get_cipher_list'
/usr/bin/ld: SSLSocket.c:(.text+0x10be): undefined reference to `SSL_set_fd'
/usr/bin/ld: SSLSocket.c:(.text+0x113b): undefined reference to `SSL_ctrl'
/usr/bin/ld: SSLSocket.c:(.text+0x121c): undefined reference to `SSL_CTX_set_verify'
/usr/bin/ld: /home/uli/.conan/data/paho-mqtt-c/1.3.9/_/_/package/6fbd1993993594bd3f9451023a96c5ec2eef0e19/lib/libpaho-mqtt3as.a(SSLSocket.c.o): in function `SSLSocket_connect':
SSLSocket.c:(.text+0x12b7): undefined reference to `ERR_clear_error'
/usr/bin/ld: SSLSocket.c:(.text+0x12bf): undefined reference to `SSL_connect'
/usr/bin/ld: SSLSocket.c:(.text+0x135d): undefined reference to `SSL_get_peer_certificate'
/usr/bin/ld: SSLSocket.c:(.text+0x138c): undefined reference to `X509_check_host'
/usr/bin/ld: SSLSocket.c:(.text+0x13d4): undefined reference to `CRYPTO_free'
/usr/bin/ld: SSLSocket.c:(.text+0x13f6): undefined reference to `X509_free'
/usr/bin/ld: SSLSocket.c:(.text+0x144d): undefined reference to `X509_check_ip_asc'
/usr/bin/ld: /home/uli/.conan/data/paho-mqtt-c/1.3.9/_/_/package/6fbd1993993594bd3f9451023a96c5ec2eef0e19/lib/libpaho-mqtt3as.a(SSLSocket.c.o): in function `SSLSocket_getch':
SSLSocket.c:(.text+0x1569): undefined reference to `ERR_clear_error'
/usr/bin/ld: SSLSocket.c:(.text+0x1579): undefined reference to `SSL_read'
/usr/bin/ld: /home/uli/.conan/data/paho-mqtt-c/1.3.9/_/_/package/6fbd1993993594bd3f9451023a96c5ec2eef0e19/lib/libpaho-mqtt3as.a(SSLSocket.c.o): in function `SSLSocket_destroyContext':
SSLSocket.c:(.text+0x1630): undefined reference to `SSL_CTX_free'
/usr/bin/ld: /home/uli/.conan/data/paho-mqtt-c/1.3.9/_/_/package/6fbd1993993594bd3f9451023a96c5ec2eef0e19/lib/libpaho-mqtt3as.a(SSLSocket.c.o): in function `SSLSocket_close':
SSLSocket.c:(.text+0x16b8): undefined reference to `ERR_clear_error'
/usr/bin/ld: SSLSocket.c:(.text+0x16c1): undefined reference to `SSL_shutdown'
/usr/bin/ld: SSLSocket.c:(.text+0x16ce): undefined reference to `SSL_free'
/usr/bin/ld: SSLSocket.c:(.text+0x16fd): undefined reference to `SSL_CTX_free'
/usr/bin/ld: /home/uli/.conan/data/paho-mqtt-c/1.3.9/_/_/package/6fbd1993993594bd3f9451023a96c5ec2eef0e19/lib/libpaho-mqtt3as.a(SSLSocket.c.o): in function `SSLSocket_putdatas':
SSLSocket.c:(.text+0x191f): undefined reference to `ERR_clear_error'
/usr/bin/ld: SSLSocket.c:(.text+0x1930): undefined reference to `SSL_write'
/usr/bin/ld: /home/uli/.conan/data/paho-mqtt-c/1.3.9/_/_/package/6fbd1993993594bd3f9451023a96c5ec2eef0e19/lib/libpaho-mqtt3as.a(SSLSocket.c.o): in function `SSLSocket_getdata':
SSLSocket.c:(.text+0x1ca5): undefined reference to `ERR_clear_error'
/usr/bin/ld: SSLSocket.c:(.text+0x1cb8): undefined reference to `SSL_read'
/usr/bin/ld: SSLSocket.c:(.text+0x1d7f): undefined reference to `SSL_pending'
/usr/bin/ld: /home/uli/.conan/data/paho-mqtt-c/1.3.9/_/_/package/6fbd1993993594bd3f9451023a96c5ec2eef0e19/lib/libpaho-mqtt3as.a(SSLSocket.c.o): in function `SSLSocket_continueWrite':
SSLSocket.c:(.text+0x1e2f): undefined reference to `ERR_clear_error'
/usr/bin/ld: SSLSocket.c:(.text+0x1e3f): undefined reference to `SSL_write'
/usr/bin/ld: /home/uli/.conan/data/paho-mqtt-c/1.3.9/_/_/package/6fbd1993993594bd3f9451023a96c5ec2eef0e19/lib/libpaho-mqtt3as.a(Base64.c.o): in function `Base64_encodeDecode.part.0':
Base64.c:(.text+0x1f): undefined reference to `BIO_f_base64'
/usr/bin/ld: Base64.c:(.text+0x27): undefined reference to `BIO_new'
/usr/bin/ld: Base64.c:(.text+0x2f): undefined reference to `BIO_s_mem'
/usr/bin/ld: Base64.c:(.text+0x37): undefined reference to `BIO_new'
/usr/bin/ld: Base64.c:(.text+0x45): undefined reference to `BIO_push'
/usr/bin/ld: Base64.c:(.text+0x55): undefined reference to `BIO_set_flags'
/usr/bin/ld: Base64.c:(.text+0x78): undefined reference to `BIO_write'
/usr/bin/ld: Base64.c:(.text+0x8e): undefined reference to `BIO_ctrl'
/usr/bin/ld: Base64.c:(.text+0x9e): undefined reference to `BIO_free_all'
/usr/bin/ld: Base64.c:(.text+0xc1): undefined reference to `BIO_read'
/usr/bin/ld: /home/uli/.conan/data/paho-mqtt-c/1.3.9/_/_/package/6fbd1993993594bd3f9451023a96c5ec2eef0e19/lib/libpaho-mqtt3as.a(WebSocket.c.o): in function `WebSocket_buildFrame.constprop.0':
WebSocket.c:(.text+0x441): undefined reference to `RAND_bytes'
/usr/bin/ld: /home/uli/.conan/data/paho-mqtt-c/1.3.9/_/_/package/6fbd1993993594bd3f9451023a96c5ec2eef0e19/lib/libpaho-mqtt3as.a(WebSocket.c.o): in function `uuid_generate':
WebSocket.c:(.text+0x721): undefined reference to `RAND_bytes'
/usr/bin/ld: /home/uli/.conan/data/paho-mqtt-c/1.3.9/_/_/package/6fbd1993993594bd3f9451023a96c5ec2eef0e19/lib/libpaho-mqtt3as.a(WebSocket.c.o): in function `WebSocket_connect':
WebSocket.c:(.text+0x929): undefined reference to `RAND_bytes'
/usr/bin/ld: /home/uli/.conan/data/paho-mqtt-c/1.3.9/_/_/package/6fbd1993993594bd3f9451023a96c5ec2eef0e19/lib/libpaho-mqtt3as.a(WebSocket.c.o): in function `WebSocket_upgrade':
WebSocket.c:(.text+0x1dd9): undefined reference to `SHA1_Init'
/usr/bin/ld: WebSocket.c:(.text+0x1def): undefined reference to `SHA1_Update'
/usr/bin/ld: WebSocket.c:(.text+0x1dff): undefined reference to `SHA1_Final'
collect2: error: ld returned 1 exit status

Solution

In addition to linking the Paho MQTT libraries, you also need to link OpenSSL, i.e. the ssl and crypto libraries. You can link them, for example using

target_link_libraries(myexecutable ssl crypto)

in CMake or by adding these linker flags for GCC:

-lssl -lcrypto

 

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

How to link Paho MQTT C++ binding using CMake

target_link_libraries(myexecutable paho-mqttpp3 paho-mqtt3as ssl crypto)

 

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

Conan .gitignore for executable projects

This is the .gitignore I use for most of my conan-built executable projects:

CMakeCache.txt
CMakeFiles/
Makefile
bin/
cmake_install.cmake
conan.lock
conanbuildinfo.cmake
conanbuildinfo.txt
conaninfo.txt
graph_info.json

It is debatable whether you should include lock files such as conan.lock. My opinion is that in the development phase they should be ignored but reconsidered when entering into production service.

Posted by Uli Köhler in Conan, git

How to initialize new C++ executable project using conan 1.x

Important note: This post is for Conan 1.x and is not compatible with the current version conan 2.x! See How to initialize C++ project using conan 2.x for an updated version of this post!

In the directory where you want to create the project, run (obviously, replace mypackageby the name of everywhere in our code!)

conan new mypackage/1.0.0 -s

-s is important here. It tells conan that the source is available in the project directory and it should not be pulled using git etc.

This will initialize a shared library project, which we will now modify to build an executable.

First, open src/CMakeLists.txt and change add_library() to add_executable(), for example

add_library(mypackage mypackage.cpp)

to

add_executable(mypackage mypackage.cpp)

Now we shall modify conanfile.py to properly build & install the executable:

    def package(self):
        self.copy("*.h", dst="include", src="src")
        self.copy("*.lib", dst="lib", keep_path=False)
        self.copy("*.dll", dst="bin", keep_path=False)
        self.copy("*.dylib*", dst="lib", keep_path=False)
        self.copy("*.so", dst="lib", keep_path=False)
        self.copy("*.a", dst="lib", keep_path=False)
    def package_info(self):
        self.cpp_info.libs = ["NoxecoDB"]

with

    def package(self):
        self.copy("mypackage", src="bin", dst="bin", keep_path=False)

    def package_info(self):
        self.env_info.PATH = os.path.join(self.package_folder, "bin")

    def deploy(self):
        self.copy("mypackage", dst="bin", src="bin")

and add

import os.path

to the top of the file

Now let’s add a main() function to  src/mypackage.cpp. Replace the file’s content with

#include <iostream>

int main(int argc, char** argv) {
    std::cout << "Hello World!" <<std::endl;
}

Now it’s time to install the dependencies & build the project using

conan install . && conan build .

The executable will be in

bin/mypackage

You can run it using

./bin/mypackage

which will print

Hello World!

I also recommend to add this .gitignore:

CMakeCache.txt
CMakeFiles/
Makefile
bin/
cmake_install.cmake
conan.lock
conanbuildinfo.cmake
conanbuildinfo.txt
conaninfo.txt
graph_info.json

 

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

How to fix st-flash ERROR common.c: stlink_flash_loader_run(0x8000000) failed! == -1

Problem:

While flashing an STM32 using st-flash using a command like

st-flash write build/firmware.bin 0x8000000

you see an error message like

2022-02-12T01:31:34 ERROR flash_loader.c: flash loader run error
2022-02-12T01:31:34 ERROR common.c: stlink_flash_loader_run(0x8000000) failed! == -1

See below for a full error log

Solution

Most likely your STM32 is locked (readout protection). Use OpenOCD to unlock it, see How to unlock STM32F0x using OpenOCD for an example. Adjust to your STM32 family as needed.

Full st-flash log

st-flash 1.6.1
2022-02-12T01:31:34 INFO common.c: F0xx small: 4 KiB SRAM, 16 KiB flash in at least 1 KiB pages.
file build/mom.bin md5 checksum: da211df7131de9de15f3b6c7a96176dd, stlink checksum: 0x000d0d03
2022-02-12T01:31:34 INFO common.c: Attempting to write 11108 (0x2b64) bytes to stm32 address: 134217728 (0x8000000)
2022-02-12T01:31:34 INFO common.c: Flash page at addr: 0x08000000 erased
2022-02-12T01:31:34 INFO common.c: Flash page at addr: 0x08000400 erased
2022-02-12T01:31:34 INFO common.c: Flash page at addr: 0x08000800 erased
2022-02-12T01:31:34 INFO common.c: Flash page at addr: 0x08000c00 erased
2022-02-12T01:31:34 INFO common.c: Flash page at addr: 0x08001000 erased
2022-02-12T01:31:34 INFO common.c: Flash page at addr: 0x08001400 erased
2022-02-12T01:31:34 INFO common.c: Flash page at addr: 0x08001800 erased
2022-02-12T01:31:34 INFO common.c: Flash page at addr: 0x08001c00 erased
2022-02-12T01:31:34 INFO common.c: Flash page at addr: 0x08002000 erased
2022-02-12T01:31:34 INFO common.c: Flash page at addr: 0x08002400 erased
2022-02-12T01:31:34 INFO common.c: Flash page at addr: 0x08002800 erased
2022-02-12T01:31:34 INFO common.c: Finished erasing 11 pages of 1024 (0x400) bytes
2022-02-12T01:31:34 INFO common.c: Starting Flash write for VL/F0/F3/F1_XL core id
2022-02-12T01:31:34 INFO flash_loader.c: Successfully loaded flash loader in sram
2022-02-12T01:31:34 ERROR flash_loader.c: flash loader run error
2022-02-12T01:31:34 ERROR common.c: stlink_flash_loader_run(0x8000000) failed! == -1
stlink_fwrite_flash() == -1

 

Posted by Uli Köhler in STM32

How to flash .bin to STM32 using st-flash

You can use st-flash like this to flash a firmware file to the STM32:

st-flash write build/firmware.bin 0x8000000

 

Posted by Uli Köhler in STM32

How to lock STM32F0 using OpenOCD (readout protection)

openocd -f interface/stlink-v2.cfg -f target/stm32f0x.cfg -c "init" -c "halt" -c "stm32f1x lock 0" -c "reset halt" -c "exit"

This will activate flash readout protection level 1 which means you won’t be able to readout or re-write the flash. You can still perform a chip erase which will clear the readout protection – for example to flash a new firmware.

Posted by Uli Köhler in STM32

How to unlock STM32F0x using OpenOCD

openocd -f interface/stlink-v2.cfg -f target/stm32f0x.cfg -c "init" -c "halt" -c "stm32f1x unlock 0" -c "reset halt" -c "exit"

After that, you need to physically remove power from the device in order for the reset to take effect.

Note the stm32f1x is no typo. OpenOCD uses the same backend for STM32F1x and STM32F0x.

Example output:

Open On-Chip Debugger 0.11.0-rc2
Licensed under GNU GPL v2
For bug reports, read
        http://openocd.org/doc/doxygen/bugs.html
WARNING: interface/stlink-v2.cfg is deprecated, please switch to interface/stlink.cfg
Info : auto-selecting first available session transport "hla_swd". To override use 'transport select <transport>'.
Info : The selected transport took over low-level target control. The results might differ compared to plain JTAG/SWD
Info : clock speed 1000 kHz
Info : STLINK V2J24S4 (API v2) VID:PID 0483:3748
Info : Target voltage: 3.296172
Info : stm32f0x.cpu: hardware has 4 breakpoints, 2 watchpoints
Info : starting gdb server for stm32f0x.cpu on 3333
Info : Listening on port 3333 for gdb connections
Info : device id = 0x10006444
Info : flash size = 16kbytes
stm32x unlocked.
INFO: a reset or power cycle is required for the new settings to take effect.

Info : Unable to match requested speed 1000 kHz, using 950 kHz
Info : Unable to match requested speed 1000 kHz, using 950 kHz
target halted due to debug-request, current mode: Thread 
xPSR: 0xc1000000 pc: 0x080000c0 msp: 0x20000400

 

Posted by Uli Köhler in STM32

Caddyfile for Angular Single Page Application (SPA)

ng build creates a directory of static files. Due to the way Angular works, you need to rewrite every URL that doesn’t exist. For example, /patterns needs to be rewritten to (but not redirected to) /

This Caddyfile serves files statically from /usr/share/caddy and performs the rewrite via try_files:

:80

root * /usr/share/caddy
file_server
try_files {path} /

 

Posted by Uli Köhler in Networking

Simple Caddyfile to serve /usr/share/caddy statically

This is a simple Caddyfile to serve the /usr/share/caddy folder on port 80.

:80

root * /usr/share/caddy
file_server 
Posted by Uli Köhler in Networking

OctoPrint: Pause/Resume G-Code to move nozzle away

This pause / resume G-Code for OctoPrint (will likely also work with other platforms, but we only tested using OctoPrint) will move the nozzle 100mm away from the part being printed during the pause and (after clicking resume) will move to the original position i.e. it will continue the print normally.

This is useful to prevent heat damage to the part and also allow you to clean the nozzle during pause

Pause G-Code:

G91 ; Set relative positioning mode
G0Z100 ; Move Z up 100mm to assist nozzle cleaning
G90 ; Set absolute positioning mode

Resume G-Code:

G91 ; Set relative positioning mode
G0Z-100 ; Move Z down 100mm to return to print position
G90 ; Set absolute positioning mode
Posted by Uli Köhler in Allgemein

What pin pitch does the Raspberry Pi DSI display connector / cable have?

The Raspberry Pi DSI display connector (for example the DSI display cable that comes with the Raspberry Pi 7″ display) has a pin pitch of 1.0mm:

Posted by Uli Köhler in Raspberry Pi

How to install tailscale on Raspberry Pi

Just use the official install command from the tailscale website:

curl -fsSL https://tailscale.com/install.sh | sh

 

Posted by Uli Köhler in Headscale, Raspberry Pi, VPN
This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Cookie settingsACCEPTPrivacy &amp; Cookies Policy