Skip to content

Commit

Permalink
Merge branch 'master' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
TG9541 committed Jul 3, 2022
2 parents 09ced28 + e208a1b commit a48735a
Showing 1 changed file with 28 additions and 18 deletions.
46 changes: 28 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,27 @@
# stm8ef-modbus
[![Travis-CI](https://travis-ci.org/TG9541/stm8ef-modbus.svg)](https://travis-ci.org/TG9541/stm8ef-modbus)

This repository provides a lightweight MODBUS RTU implementation with [STM8 eForth](https://github.com/TG9541/stm8ef/wiki) for "wired" control nodes, e.g. for home automation. The main target is low-cost STM8S 8bit µCs like the STM8S003F3P6 with 8K Flash and 1K RAM.
This repository provides a lightweight MODBUS RTU implementation with [STM8 eForth](https://github.com/TG9541/stm8ef/wiki) for "wired" control nodes, e.g. for home automation. The main target is low-cost STM8S 8bit µCs like the STM8S003F3P6 with 8K Flash and 1K RAM but other STM8S µCs can be used, too.

Using STM8 Forth for MODBUS has some advantages: the implementation is very compact and it gives applications access to many advanced architecture features like "I/O-locic execution in the background" or a CLI (command line interface).
Using STM8 Forth for MODBUS has some advantages over C or assembler implementations: the implementation is very compact and it gives applications access to many advanced architecture features like "I/O-logic execution in the background" or a CLI (command line interface) for interactive testing.

The Forth compiler/interpreter is part of the binary you can literally change the code while your board is communicating with the MODBUS host!
Behind the CLI is a Forth compiler/interpreter that the operating system of the MODBUS node - it's even possible to change code while the board is communicating with the MODBUS host!

The MODBUS RTU implementation covers basic FCs: it's a subset of [MODBUS V1.1b](http://www.modbus.org/docs/Modbus_Application_Protocol_V1_1b.pdf) common in simple I/O nodes. It's easy to write code for other FCs. It's also simple to turn the board in something like an independent controller for window blinds: the MODBUS host only commands "open" or "closed", not "up" and "down". Local control code can help to make home automation much more robust and reactive.
The MODBUS RTU implementation covers basic MODBUS FCs, the subset of [MODBUS V1.1b](http://www.modbus.org/docs/Modbus_Application_Protocol_V1_1b.pdf) commonly used for simple I/O nodes. It's easy to strip the MODBUS server down to a smaller set of FCs, or implement different ones. It's also easy implement "local intelligence" which can make, e.g., home automation much more robust, resilient and reactive than what's possible with a central controller and "dumb nodes".

## STM8EF-MODBUS Demo

In the Forth2020 #24 June 2022 User-Group meeting, the author held a talk "A Modular MODBUS Server With STM8 eForth". In the video general ideas about the architecture are explained and an example for interactive feature development and testing is given.

[![Forth2020 Zoom Meeting 2022 # 24](https://user-images.githubusercontent.com/5466977/175802574-2b507519-3559-4804-ba67-3abe5bd70a3e.png)](https://www.youtube.com/watch?v=noVSeWeVmmw)

Code used in the demo, and further instructions, are in [this GitHub Gist](https://gist.github.com/TG9541/9a1137693abecdc8fcb4cc63b7dd0edb).

## Supported Boards

### C0135 4-Relay Board

The MODBUS I/O Node implementation for the low-cost [C0135 4-Relay RTU module][C0135] is the default target and it serves as a demonstrator, and in [GitHub Releases](https://github.com/TG9541/stm8ef-modbus/releases) you'll find the ready-to-use binary (look for `out/C0135/C135-forth.ihx` in `stm8ef-bin.zip`).
The MODBUS I/O Node implementation for the low-cost [C0135 4-Relay RTU module][C0135] is the default target. It also serves as a demonstrator. In [GitHub Releases](https://github.com/TG9541/stm8ef-modbus/releases) you'll find the ready-to-use binary (`out/C0135/C135-forth.ihx` in `stm8ef-bin.zip`).

[C0135]: https://github.com/TG9541/stm8ef/wiki/Board-C0135

Expand All @@ -25,28 +33,30 @@ After that you should be ready to test the MODBUS connection (here is an example

![image](https://user-images.githubusercontent.com/5466977/80449110-8ca6fe80-891e-11ea-9f99-8d6adf6b5160.png)

This project doesn't just provide a better MODBUS RTU firmware for the relay board but it also turns it into something more: using a diode and a cheap USB-TTL dongle you can [get a console][TWOWIRE]. This means the MODBUS node *is* a computer, a bit like the console of a VIC20 in the old days:
This project doesn't just provide a better MODBUS RTU firmware for the relay board but it also turns it into something more: using a diode and a cheap USB-TTL dongle you can [get a console][TWOWIRE]. This means the MODBUS node *is* a computer, a bit like the console of a VIC20 in the old days.

![image](https://user-images.githubusercontent.com/5466977/80796849-917ae500-8ba0-11ea-843b-96307026f7a8.png)

Using the Forth programming language and the STM8 eForth Background Task it's very program local control features, e.g. a window blinds controller that reacts on local inputs without noticable lag and that communicates with a home-automation controller through "holding" registers.
Using the [Forth programming language][EForthProgramming] and the [STM8 eForth Background Task][BACKGROUNDTASK] it's easy to program local control features, e.g. a window blinds controller that reacts on local inputs without noticable lag and that communicates with a home-automation controller through "holding" registers. This way robust and safe home-automation is easier to achieve.

[TWOWIRE]: https://github.com/TG9541/stm8ef/wiki/STM8-eForth-Programming-Tools#using-a-serial-interface-for-2-wire-communication
[BACKGROUNDTASK]: https://github.com/TG9541/stm8ef/wiki/STM8-eForth-Background-Task
[EForthProgramming]: https://github.com/TG9541/stm8ef/wiki/STM8-eForth-Programming

### STM8S001J3RS485 Mini MODBUS Board

The STM8S001J3RS485 board is a tiny MODBUS node based on the STM8S001J3M3 "Low Density Value Line" STM8S µC in a SO8 package.

[![STM8S001J3RS485](https://raw.githubusercontent.com/TG9541/stm8s001rs485/master/doc/STM8S001J3_RS485_front.png)](https://github.com/TG9541/stm8s001rs485)

The code can be built and transferred to the devide by running `make -f forth.mk BOARD=STM8S001J3RS485 flash`. After flashing the `BUSCTRL` file in the board configuration folder should be transferred using e4thcom and a [2-wire connection][TWOWIRE] through PC5. After that, `STM8S001J3RS485/board.fs` can be transferred with `#include`.
The code can be built and transferred to the devide by running `make -f forth.mk BOARD=STM8S001J3RS485 flash`. After flashing, the `BUSCTRL` file in the board configuration folder should be transferred using e4thcom and a [2-wire connection][TWOWIRE] through PC5. After that, `STM8S001J3RS485/board.fs` can be transferred with `#include`.

### MINDEV STM8S103F3 Breakout Board
It's easy to build custom targets, e.g. using the $0.80 [MINDEV board](https://github.com/TG9541/stm8ef/wiki/Breakout-Boards#stm8s103f3p6-breakout-board), a cheap relay board, and an RS485 break-out board.

![MINDEV](https://camo.githubusercontent.com/82bd480f176951de9a469e134f543a6570f48597/68747470733a2f2f616530312e616c6963646e2e636f6d2f6b662f485442314e6642615056585858586263587058587136785846585858362f357063732d6c6f742d53544d3853313033463350362d73797374656d2d626f6172642d53544d38532d53544d382d646576656c6f706d656e742d626f6172642d6d696e696d756d2d636f72652d626f6172642e6a70675f323230783232302e6a7067)

When using PB5 for RS485 direction control (-> `BUSCTRL`) the C0135 code can be used (refer to the [C0135 STM8 eForth Wiki page][C0135]).
When using PB5 for RS485 direction control (-> `BUSCTRL`) the C0135 binary can be used right away (refer to the [C0135 STM8 eForth Wiki page][C0135]).

## Supported MODBUS Function Codes

Expand All @@ -65,9 +75,9 @@ FC | Description | Support

A working example with Node-ID and Baud Rate stored in EEPROM is implemented in `C0135/board.fs`. An example that shows how to develop minimal servers with FC handlers from scratch using the Forth console is in `main.fs` and, for different FCs, in the folder [test](https://github.com/TG9541/stm8ef-modbus/tree/master/test).

Note that there is an experimental mapping of holding registers: holding register addresses from 60000 are mapped to the EEPROM. The mapping can be changed in the future (community input on how to deal with MODBUS style register mapping is welcome).
Note that there is an experimental mapping of holding registers: holding register addresses from 60000 are mapped to the EEPROM. The mapping can be changed in the future. Community input on how to deal with MODBUS style register mapping is welcome.

For FC03, FC06 and FC15 the MODBUS address mapping is currently as follows:
For FC03, FC06 and FC15 the MODBUS address mapping is as follows:

MB address|register MODBUS|Forth
-|-|-
Expand All @@ -89,7 +99,7 @@ Please refer to the [Installation Instructions](https://github.com/TG9541/stm8ef

## Console

The STM8S UART is used by [UARTISR](https://github.com/TG9541/stm8ef-modbus/blob/master/UARTISR) for MODBUS RTU communication. The Forth console communicates through a half-duplex simulated RS232 two-wire interface on the `PD1/SWIM` GPIO pin. For adding a standard USB-TTL converter only a diode is needed. Other CLI communication options are easy to implment, e.g. using simulated full-duplex RxD-TxD lines (e.g. using PA1 and PA2 after removing the C0135 8MHz crystal). It's also possible to use an STM8S High Density device with two UARTs, e.g. the STM8S207RBT6.
The STM8S UART is used by [UARTISR](https://github.com/TG9541/stm8ef-modbus/blob/master/UARTISR) for MODBUS RTU communication. The Forth console communicates through a half-duplex simulated RS232 two-wire interface on the `PD1/SWIM` GPIO pin. For communication with a standard USB-TTL converter only a diode is needed. Other CLI communication options are easy to implment, e.g. using simulated full-duplex RxD-TxD lines (e.g. using PA1 and PA2 after removing the C0135 8MHz crystal). It's also possible to use an STM8S High Density device with two UARTs, e.g. the STM8S207RBT6.

Please refer to the [STM8 eForth Wiki](https://github.com/TG9541/stm8ef/wiki/STM8S-Value-Line-Gadgets#other-target-boards) to learn more about half-duplex CLI communication options and preferred terminal programs.

Expand All @@ -99,12 +109,12 @@ The software architecture separates hardware abstraction and application in simp

Layer|Source file|Description
-|-|-
5|`main.fs` or `{BOARD}/board.fs`|configuration and application layer
4|`MBSERVER`|MODBUS FC plug-ins (optional)
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
6|`main.fs` or `{BOARD}/board.fs`|configuration and application layer
5|`MBSERVER`|MODBUS FC plug-ins (optional)
4|`MBPROTO`|MODBUS protocol layer
3|`UARTISR`|buffered UART communication
2|`BUSCTRL`|bus access (i.e. RS485 direction control)
1|STM8 eForth|lightweight interactive multi-tasking OS

The different concerns are separeted in the code and FC handlers can be changed through the CLI without restarting the application!

Expand Down

0 comments on commit a48735a

Please sign in to comment.