This repository provides a (very) lightweight MODBUS RTU implementation with STM8 eForth for "wired" control nodes, e.g. for home automation. The is intended for low-cost STM8S 8bit µCs like the STM8S003F3P6 with 8K Flash and 1K RAM.
Using STM8 Forth for MODBUS has many advantages: while the implementation is extraordinarily compact it gives applications access to many advanced architectural features like independent I/O-locic execution in the background, a CLI (command line interface), compilation of logic the application, and more!
The MODBUS implementation covers basic FCs and implements a subset of MODBUS V1.1b which covers use cases of simple MODBUS servers (i.e. dependent end nodes). Right now there is no MODBUS master implementation but the code in this repository can be re-used to write one.
The C0135 board is the default target.
It's easy, however, to build custom targets, e.g. using the $0.80 MINDEV board, a cheap relay board, and an RS485 break-out board.
MBSERVER
contains MODBUS function plug-ins with the following function codes (FC):
FC | Description | Support |
---|---|---|
1 | Read Coils | implemented |
2 | Read Discrete Inputs | implemented (limit to "8bit aligned") |
3 | Read Holding Registers | implemented (variables in RAM) |
4 | Read Input Registers | implemented |
5 | Write Single Coil | implemented |
6 | Write Single (Holding) Register | (write issue if needed) |
15 | Write Multiple Coils | implemented |
16 | Write Multiple Registers | implemented |
Currently there are no diagnostic functions and communication properties have to be hard coded.
The Getting Started section in the STM8 eForth Wiki provides an introduction to flashing STM8 eForth to a target µC.
Please refer to the Installation Instructions in the STM8EF-MODBUS Wiki for build instructions.
While MODBUS communication uses the STM8S UART, the Forth console communicates through a half-duplex simulated RS232 interface through the PD1/SWIM
GPIO pin (and a diode). This is made possible by the SWIMCOM STM8 eForth "stock binary" which the makefile pulls from the STM8 eForth Releases. Other CLI communication options, e.g. using simulated full-duplex RxD-TxD lines, require building a custom STM8 eForth binary.
Please refer to the STM8 eForth Wiki to learn more about half-duplex CLI communication options and preferred terminal programs.
The software architecture separates hardware abstraction and application in simple layers:
Layer | Source file | Description |
---|---|---|
5 | main.fs | configuration and application layer |
4 | MBSERVER |
MODBUS FC plug-ins |
3 | MBPROTO |
MODBUS protocol layer |
2 | UARTISR |
buffered UART communication |
1 | BUSCTRL |
bus access (i.e. RS485 direction control) |
0 | STM8 eForth | lightweight interactive multi-tasking OS |
The code is organized in the following execution domains:
- interrupt service routine for buffered communication
- foreground "idle mode" protocol handler
- fixed-cadence background teask for I/O logic
- foreground CLI provided by the SWIMCOM STM8 eForth
Concerns are nicely separeted - it's even possible to change FC handlers without restarting the application!