Firmware for an ARM Cortex-M4 (Atmel SAM4S) sensor node: first in C on top of the Atmel Software Framework, then rewritten in C++ without vendor libraries, following the layered design of Real-Time C++.
The board monitored an industrial cooling machine. This page is about how the software was built.
// src/sys/app/sys_tick_chrono_test.cpp mcal::init(); mcal::port::port_pin<std::uint32_t, std::uint32_t, 0x400E1200, // PIOC UINT32_C(23)> // PC23 yellow_led_pin; yellow_led_pin.set_direction_output(); while (1) { yellow_led_pin.set_pin_high(); timer_type::blocking_delay(timer_type::seconds(1U)); yellow_led_pin.set_pin_low(); timer_type::blocking_delay(timer_type::seconds(1U)); }
A single microcontroller collects a handful of signals and sends them to a server every few minutes. Simple on paper; the interesting part was how to structure the code so it could be tested piece by piece and carried to other boards.
edbg for the C++ version.
System diagram from the design notes (2018). The ADC inputs are multiplexed; RS-485 uses the USART RS485 mode of the SAM4S.
The base version that ran on the board. An Atmel Studio project where ASF provides clocks, GPIO, UART, TWI, ADC and flash, and the application is split into devices (one driver per chip) and managers (system services).
sht20.c | Temperature and humidity, two sensors on TWI0 and TWI1 |
sct-013.c | Motor current from the ADC, single or three phase |
yf-s201c.c | Flow pulses counted on a GPIO interrupt |
bgs2.c | GPRS modem: AT commands, SIM check, HTTP service, response parsing |
flash_memory.c | Configuration stored in internal flash (EFC) |
system_actuator.c | Relay output |
systick_gpt.c | SysTick time base |
system_configuration | Defaults, flash load/save, remote configuration |
system_telemetries | Builds the telemetry record (14 fields) and uploads it |
system_time | Software timeouts on top of SysTick |
system_watchdog | Hardware watchdog |
system_logger | Debug console on the UART |
system_status_codes.h | One status code per module and error |
No RTOS: main() initialises the services and loops forever, running each job when its software timeout expires. The task diagram defined three periods:
Read the current sensors and accumulate the energy consumed by each motor.
Ask the server for a new configuration over GPRS and apply it.
Read temperature, humidity, flow and pressure, pack the telemetry and send it.
Flow pulses and the configuration switch are handled by interrupts at any time. In the bench build kept in the repository, telemetry goes out every sixth power cycle instead of every 30 minutes.
The rewrite drops ASF and follows the architecture of Christopher Kormanyos's Real-Time C++: the application talks to a thin microcontroller abstraction layer, peripherals are described with templates resolved at compile time, and the startup code is our own.
cpu · pmc · port · led · gpt · irq · uart · i2c · adc · wdtcrt0, RAM initialisation, vector table and linker scriptsEvery register write is a call to a static member of a template. The address and the value are known at compile time, so the compiler emits a single store, with no driver structures in RAM.
The TWI master is configured the same way: master mode, the clock divider for 100 kHz, no interrupts. Each line documents the register it touches.
Bring-up went bottom-up, each step with its own main() and Makefile under Target/sam4s/: LED on PC23 → clock source → SysTick with util::timer → blocking delays and intervals → software interrupts → UART (polling and interrupts) → I2C → ADC → watchdog. Seven more programs test the buffers and the message classes on Linux.
Built with -std=c++11 -fno-exceptions, -mcpu=cortex-m4, --gc-sections. The MCALs for ATtiny85, ATmega328P and STM32F429 come from the book and served as reference.
The board speaks three protocols: AT commands to the modem, Modbus over RS-485 and a debug console. Instead of three ad-hoc parsers, the C++ version separates what a message is from how bytes travel.
Taken from Alex Robenko's guide to implementing communication protocols in C++ (src/comm/readme.md):
Message_base: transmit/receive buffers, send(), recv(), Serialize(). Specialised as Modbus, AT command (3G modem) and debug messages.Communication_base: send_n(), recv_n(), Idle(). Implemented over a memory buffer, UART, I2C and RS-485, and over pipes and file descriptors on Linux.task objects: time-critical tasks and a message-processing task that feeds a Handler.
Class diagram (Documentacion/sensor_iot_class.uml, PlantUML).
From the commit log of the C++ tree. The C version is the working base it was meant to replace.
Repository with datasheets and the system diagram.
Sources, scripts and file templates.
LED on PC23 from the C++ MCAL.
Linker scripts and ARM support.
Communications tested on Linux with pipes.
communication and buffer_communication.
Messages with send and receive.
Rewrite merged with the base version and build files.
Class and task diagrams, JTAG interface.
C version added to the repository, references listed, this page.
| Path | Contents |
|---|---|
firmware-c-asf/ | C version, Atmel Studio 7 solution |
src/mcal/sam4s/ | MCAL for the SAM4S |
src/mcal/pc-linux/ | Host back-end: pipes, file descriptors |
src/sys/start/sam4s/ | Startup code and linker scripts |
src/sys/app/ | Peripheral test programs |
src/app/ | Host tests: buffers, messages, pipes |
src/comm/ | Message framework |
src/util/ | STL subset and utilities (Real-Time C++) |
Target/ | Makefiles per test and platform |
Documentacion/ | Diagrams and plan |
Code from 2018, kept as it was and not re-tested. The sensor drivers exist only in the C version; the C++ rewrite covers the peripheral layer and the message framework.
The modem identifier and server addresses of the original deployment are replaced by placeholders.
Own C++ code: GPLv3. C version: Apache 2.0, ASF files under Microchip's licence. Files from Real-Time C++: Boost Software License 1.0.