Remote Control Power Sockets: Part 6: Zephyr Proof of Concept
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, and in Part 5 we did the same on bare metal with libopencm3. In this part we switch the same socket with Zephyr on an STM32F429I-DISCO of the older revision MB1075B. Zephyr knows the board by the name stm32f429i_disc1, the name of its successor with the same microcontroller. The same socket, the same codewords, the same timing; only the platform changes. At its core, this part compares the three platforms. 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 PE4 (pin header P1, pin 13)
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. The red LED LD4 lights up only if the firmware stops because of an error.


Toolchain
Debian supplies Git, CMake, Ninja, the devicetree compiler and OpenOCD. We install west, Zephyr's command-line tool, into a Python venv. The compiler comes from the Zephyr SDK, of which we install only the ARM toolchain:
Project files
The project is a small west workspace. zephyr_poc/ holds the venv and later Zephyr with its modules; our own files live in rev_toggle/.
rev_toggle/west.yml pins Zephyr to the release v4.4.2 and fetches only the two modules that this board needs:
manifest: remotes: - name: zephyrproject-rtos url-base: https://github.com/zephyrproject-rtos projects: - name: zephyr remote: zephyrproject-rtos revision: v4.4.2 import: name-allowlist: - cmsis_6 - hal_stm32
rev_toggle/CMakeLists.txt is the build file of the application:
cmake_minimum_required(VERSION 3.20.0) find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) project(rev_toggle) target_sources(app PRIVATE src/main.c)
rev_toggle/prj.conf switches GPIO on and the console off:
rev_toggle/boards/stm32f429i_disc1.overlay puts the DATA line on PE4 and gives the red LED LD4 the alias led1:
Firmware
We save the C source code as rev_toggle/src/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 * the libopencm3 firmware of Part 5 (PT2260 encoder, 350 us base pulse, * 10 repetitions each followed by a sync bit). * * Wiring (STM32F429I-DISCO, FS1000A): DATA to PE4 (header P1, pin 13), * VCC to 5V, GND to GND. The green LED LD3 is lit while the socket is on. * The red LED LD4 lights up if a GPIO call fails and the firmware stops. */ #include <errno.h> #include <zephyr/kernel.h> #include <zephyr/drivers/gpio.h> #define PULSE_US 350 #define REPEAT 10 #define PAUSE_MS 5000 /* pulse widths and sync length in units of PULSE_US */ #define SHORT 1 #define LONG 3 #define SYNC_LOW 31 static const struct gpio_dt_spec tx = GPIO_DT_SPEC_GET(DT_PATH(zephyr_user), tx_gpios); static const struct gpio_dt_spec led = GPIO_DT_SPEC_GET(DT_ALIAS(led0), gpios); static const struct gpio_dt_spec err_led = GPIO_DT_SPEC_GET(DT_ALIAS(led1), gpios); /* group "FFF1" + socket "1FF0" + data ("0010" on, "0001" off) */ static const char CODE_ON[] = "FFF11FF00010"; static const char CODE_OFF[] = "FFF11FF00001"; static int pulse(unsigned int high, unsigned int low) { int ret; ret = gpio_pin_set_dt(&tx, 1); if (ret < 0) return ret; k_busy_wait(PULSE_US * high); ret = gpio_pin_set_dt(&tx, 0); if (ret < 0) return ret; k_busy_wait(PULSE_US * low); return 0; } static int send_symbol(char c) { int ret; switch (c) { case '0': ret = pulse(SHORT, LONG); if (ret < 0) return ret; return pulse(SHORT, LONG); case '1': ret = pulse(LONG, SHORT); if (ret < 0) return ret; return pulse(LONG, SHORT); case 'F': ret = pulse(SHORT, LONG); if (ret < 0) return ret; return pulse(LONG, SHORT); default: return -EINVAL; } } static int send_codeword(const char *code) { int ret = 0; for (int n = 0; n < REPEAT && ret == 0; n++) { /* no interrupt may stretch a pulse */ unsigned int key = irq_lock(); for (const char *c = code; *c && ret == 0; c++) ret = send_symbol(*c); if (ret == 0) ret = pulse(SHORT, SYNC_LOW); /* sync */ irq_unlock(key); } return ret; } int main(void) { int ret; /* without LD4 there is no way to report an error */ if (!gpio_is_ready_dt(&err_led)) return -ENODEV; ret = gpio_pin_configure_dt(&err_led, GPIO_OUTPUT_INACTIVE); if (ret < 0) return ret; if (!gpio_is_ready_dt(&led) || !gpio_is_ready_dt(&tx)) { ret = -ENODEV; goto out_err; } ret = gpio_pin_configure_dt(&led, GPIO_OUTPUT_INACTIVE); if (ret < 0) goto out_err; ret = gpio_pin_configure_dt(&tx, GPIO_OUTPUT_INACTIVE); if (ret < 0) goto out_led; for (;;) { ret = send_codeword(CODE_ON); if (ret < 0) goto out_tx; ret = gpio_pin_set_dt(&led, 1); if (ret < 0) goto out_tx; k_msleep(PAUSE_MS); ret = send_codeword(CODE_OFF); if (ret < 0) goto out_tx; ret = gpio_pin_set_dt(&led, 0); if (ret < 0) goto out_tx; k_msleep(PAUSE_MS); } /* * Stop path: DATA low so the FS1000A stops transmitting, LD3 off, * LD4 on. A failure here cannot be reported any further. */ out_tx: (void)gpio_pin_set_dt(&tx, 0); out_led: (void)gpio_pin_set_dt(&led, 0); out_err: (void)gpio_pin_set_dt(&err_led, 1); return ret; }
Zephyr generates its kernel tick with SysTick, so unlike in Part 5 we do not program SysTick ourselves. k_busy_wait() waits on the kernel's cycle counter, which counts at the core clock of 168 MHz. irq_lock() keeps interrupts away while the firmware sends a codeword, for about 45 ms. Between two repetitions the firmware lifts the lock for only a few instructions, just long enough for interrupts that are already pending, such as the SysTick interrupt. k_msleep() leaves the five-second pause to the scheduler. pulse(), send_symbol() and send_codeword() send the same pulses as the sketch from Part 4. Unlike digitalWrite() and the libopencm3 functions, which return nothing, Zephyr's GPIO calls return an error code, and the firmware checks every one of them. If a call fails, the firmware pulls DATA low so that the transmitter falls silent, switches LD3 off, lights LD4 and stops. It toggles LD3 only after it has sent a codeword without errors. The DATA pin comes from the overlay (tx-gpios in the zephyr,user node), LD3 from the board's led0 alias and LD4 from the overlay's led1 alias. So if we connect the transmitter to a different pin, we change only the overlay. prj.conf switches the console off because the ST-LINK of this board has no serial connection to the PC. The two LEDs are therefore our only feedback.
Setting up the workspace
In zephyr_poc/, with the venv active, west reads the manifest, fetches Zephyr and the two modules and installs Zephyr's Python packages and the SDK's ARM toolchain:
Building and flashing
We run both commands in zephyr_poc/, with the board connected to the computer through the USB port of its ST-LINK:
west build -b stm32f429i_disc1 rev_toggle west flash --runner openocd --config board/stm32f429discovery.cfg
On the older ST-LINK firmware of the MB1075B (V2J17), the plain west flash --runner openocd aborts with ST-Link version does not support DAP direct transport, so we specify the board's OpenOCD configuration explicitly.
We tested this with an STM32F429I-DISCO (MB1075B) on Debian 13 with Zephyr v4.4.2, Zephyr SDK 1.0.1, west 1.5.0 and OpenOCD 0.12.0: the socket switched on and off every five seconds, and LD3 stayed lit as long as it was on.
Arduino, libopencm3 and Zephyr compared
The numbers come from the builds of Part 4, Part 5 and this part. Lines counts all files that we write, including comments. Flash is the size of the image that the build produces for the microcontroller's flash.
Lines |
Flash |
Timing |
|
|---|---|---|---|
Arduino |
77 |
2208 bytes |
busy-wait |
libopencm3 |
102 |
1652 bytes |
SysTick flag |
Zephyr |
179 |
16244 bytes |
counter, interrupts locked |
The sketch from Part 4 waits with delayMicroseconds(), the firmware from Part 5 polls the SysTick flag once per microsecond, and in Zephyr k_busy_wait() waits on the kernel's cycle counter while irq_lock() locks out interrupts. The Zephyr image is larger because it contains the kernel, the drivers and the startup code. Zephyr needs the most files and, with the error handling, also the most lines: the firmware checks the result of every GPIO call, while the GPIO functions from Part 4 and Part 5 return nothing to check. At the same time, the pin number lives in the devicetree and the clock configuration comes from the board support; neither of them appears in main.c.
What comes next
Next 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.