Remote Control Power Sockets: Part 5: STM32 Proof of Concept

Not yet tested on the board: Our STM32F0DISCOVERY does not arrive until mid-December. As soon as we have tested the firmware on it, we will add the result here.

In Parts 1 to 3 we examined the remote control: Part 1 on paper, Part 2 with the oscilloscope and Part 3 with an SDR stick. From this we worked out the codewords that the remote control sends for each socket and each button. In Part 4 we used them to switch a REV 008342 socket (group 3, socket 0, dial position D 1) from an Arduino Uno. In this part we do the same with an STM32F0DISCOVERY and libopencm3: the same socket, the same codewords, the same timing; only the platform changes. At its core, this part compares the Arduino with an STM32 that we program directly with libopencm3. This too is a proof of concept, and the firmware is short enough to read in one go. We explained the codeword, its timing and the dial setting in Part 4 and do not repeat them here.

Wiring

  • DATA to PB9

  • VCC to 5V

  • GND to GND

The antenna wire of about 17 cm on the antenna pad ANT is the same as in Part 4. We describe the module itself in the Wiring section of Part 9. The green LED LD3 on the board lights up while the socket is on.

Wiring sketch: pins PB9, 5V and GND of the STM32F0DISCOVERY go to DATA, VCC and GND of the FS1000A transmitter, plus an antenna wire of 17 cm

Toolchain

Debian supplies the cross-compiler, newlib, OpenOCD and the other tools, and python3 runs the code generators of libopencm3. The project is a small folder in which libopencm3 sits as a git submodule at a fixed commit. From libopencm3 we build only the STM32F0 part:

sudo apt install gcc-arm-none-eabi libnewlib-arm-none-eabi make git openocd python3
mkdir rev_toggle && cd rev_toggle
git init
git submodule add https://github.com/libopencm3/libopencm3
git -C libopencm3 checkout 96297511c5c7abeb1a0682641bb1219fec482adc
make -C libopencm3 TARGETS=stm32/f0

Firmware

We save the C source code as rev_toggle/main.c:

/*
 * Switches one REV 008342 socket (group 3, socket 0) on and off every
 * 5 s. Same codewords and timing as the Arduino sketch of Part 4 and
 * librsswitch (PT2260 encoder, 350 us base pulse, 10 repetitions each
 * followed by a sync bit).
 *
 * Wiring (STM32F0DISCOVERY, FS1000A): DATA to PB9, VCC to 5V, GND to GND.
 * The green LED LD3 (PC9) is lit while the socket is on.
 */

#include <libopencm3/cm3/systick.h>
#include <libopencm3/stm32/gpio.h>
#include <libopencm3/stm32/rcc.h>

#define TX_PORT         GPIOB
#define TX_PIN          GPIO9
#define LED_PORT        GPIOC
#define LED_PIN         GPIO9
#define PULSE_US        350
#define REPEAT          10

/* group "FFF1" + socket "1FF0" + data ("0010" on, "0001" off) */
static const char CODE_ON[] = "FFF11FF00010";
static const char CODE_OFF[] = "FFF11FF00001";

/* SysTick counts the 48 MHz core clock, its flag is set every microsecond */
static void delay_us(unsigned int us)
{
        while (us--) {
                while (!systick_get_countflag())
                        ;
        }
}

static void pulse(unsigned int high, unsigned int low)
{
        gpio_set(TX_PORT, TX_PIN);
        delay_us(PULSE_US * high);
        gpio_clear(TX_PORT, TX_PIN);
        delay_us(PULSE_US * low);
}

static void send_codeword(const char *code)
{
        for (int n = 0; n < REPEAT; n++) {
                for (const char *c = code; *c; c++) {
                        switch (*c) {
                        case '0':
                                pulse(1, 3);
                                pulse(1, 3);
                                break;
                        case '1':
                                pulse(3, 1);
                                pulse(3, 1);
                                break;
                        case 'F':
                                pulse(1, 3);
                                pulse(3, 1);
                                break;
                        }
                }
                pulse(1, 31); /* sync */
        }
}

int main(void)
{
        rcc_clock_setup_in_hsi_out_48mhz();
        rcc_periph_clock_enable(RCC_GPIOB);
        rcc_periph_clock_enable(RCC_GPIOC);
        gpio_mode_setup(TX_PORT, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, TX_PIN);
        gpio_mode_setup(LED_PORT, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, LED_PIN);
        gpio_clear(TX_PORT, TX_PIN);

        systick_set_frequency(1000000, rcc_ahb_frequency);
        systick_counter_enable();

        for (;;) {
                gpio_set(LED_PORT, LED_PIN);
                send_codeword(CODE_ON);
                delay_us(5000000);
                gpio_clear(LED_PORT, LED_PIN);
                send_codeword(CODE_OFF);
                delay_us(5000000);
        }
}

rcc_clock_setup_in_hsi_out_48mhz() runs the core at 48 MHz from the internal HSI oscillator. The firmware sets SysTick to 1 MHz, so the counter sets its COUNTFLAG once per microsecond, and delay_us() counts these flags. This makes the function the counterpart of delayMicroseconds() from Part 4. pulse() and send_codeword() correspond to the functions of the same name in the sketch. The firmware switches LD3 on before the on codeword and off before the off codeword.

Makefile

We save the Makefile as rev_toggle/Makefile:

# The first line of every recipe is on the target line, so the file needs no tab
OPENCM3_DIR = libopencm3
DEVICE = stm32f051r8t6
CC = arm-none-eabi-gcc
CFLAGS = -Os -g -Wall -Wextra
include $(OPENCM3_DIR)/mk/genlink-config.mk
CFLAGS += $(ARCH_FLAGS)
LDFLAGS += --static -nostartfiles -T$(LDSCRIPT) $(ARCH_FLAGS) -Wl,--gc-sections
LDLIBS += -Wl,--start-group -lc -lgcc -lnosys -Wl,--end-group

all: rev_toggle.elf
rev_toggle.elf: main.o $(LDSCRIPT) $(LIBDEPS) ; $(CC) $(LDFLAGS) main.o $(LDLIBS) -o $@
include $(OPENCM3_DIR)/mk/genlink-rules.mk
flash: rev_toggle.elf ; openocd -f board/stm32f0discovery.cfg -c "program $< verify reset exit"
clean: ; rm -f *.o *.elf generated.*.ld
.PHONY: all flash clean

genlink-config.mk and genlink-rules.mk from libopencm3 generate the linker script for the board's STM32F051R8T6.

Building and flashing

We run both commands in rev_toggle/, with the board connected to the computer through the Mini-USB port of its ST-LINK:

make
make flash

What comes next

In Part 6 we switch the same socket from an STM32F429I-DISCO, with Zephyr instead of libopencm3. Then come the Linux boards: in Part 7 we set up a Raspberry Pi, in Part 8 a BeagleBone Black, and in Part 9 we switch the sockets with librsswitch.