Skip to content

Commit

Permalink
#12 fix node, baud rate, input handler
Browse files Browse the repository at this point in the history
  • Loading branch information
TG9541 committed Apr 12, 2020
1 parent ade7b90 commit 2952663
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 19 deletions.
32 changes: 20 additions & 12 deletions MBPROTO
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,15 @@
NVM
VARIABLE mbnode \ MODBUS node id
VARIABLE crcerr \ CRC error counter
VARIABLE mbact \ action XT for generic extensions (e.g. debugging)
VARIABLE mbdef \ action XT for default handler
VARIABLE mbpre \ xt for FC preparation (e.g. reading inputs)
VARIABLE mbact \ xt for oputput action (e.g. output, debugging)
VARIABLE mbdef \ xt for default handler

\ execute xt if not 0
: @?EXEC ( xt -- ) @ ?DUP IF
( xt ) EXECUTE
THEN
;

\ get MODBUS FC
: mbfc ( -- c )
Expand Down Expand Up @@ -83,28 +90,28 @@ NVM
\ MODBUS protocol handler
: MBPROTO ( -- )
rxbuf rxp @ - ( rx )
1 TIM tstamp @ - < AND ( message trigger )
1 TIM tstamp @ - < AND ( message trigger )
IF
rxp @ 2- ( a1 ) DUP rxbuf ( a1 a1 a0 )
MBCRC ( a1 crc-le ) SWAP @ = ( crc OK )
( crc-ok ) IF
mbnode @ rxbuf C@ = IF \ node address match
rxbuf C@ ( DUP ." S: " . CR ) txc+
rxbuf 1+ C@ DUP txc+ ( fc )
mbpre @?EXEC \ preparation handler, e.g. read inputs
rxbuf C@ txc+
rxbuf 1+ C@ DUP txc+ ( fc )
DUP 1 17 WITHIN IF
FC>XT @ EXECUTE
FC>XT @?EXEC \ get and execute FC handler
ELSE
FCDEF
FCDEF \ default handler
THEN
tbp @ txbuf ( a1 a0 ) MBCRC ( CRC-LE ) tx+
mbact @ ?DUP IF
( xt ) EXECUTE
THEN
send rxres
mbact @?EXEC \ action handler, e.g. write outputs
send
THEN
ELSE
1 crcerr +!
THEN
rxres
THEN
;

Expand All @@ -122,6 +129,7 @@ WIPE RAM
#require MBPROTO

RAM
#require WIPE
#require MBRESET
MBRESET \ Reset the MODBUS Function Code table

Expand Down Expand Up @@ -151,5 +159,5 @@ NVM
[ ' MBPROTO ] LITERAL 'IDLE ! \ run MB protocol handler as idle task
;

' init 'BOOT !
' init 'BOOT !
WIPE RAM
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ The STM8S001J3RS485 board is a tiny MODBUS node based on the STM8S001J3M3 "Low D

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

This is work in progress.
This is work in progress.

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 through PC5. After that `main.fs` in the project root folder can be transferred.

Expand All @@ -42,9 +42,9 @@ FC | Description | Support
**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)
**6** | **Write Single (Holding) Register** | implemented
**15** | **Write Multiple Coils** | implemented
**16** | **Write Multiple Registers** | implemented
16 | **Write Multiple Registers** | partial

Currently there are no diagnostic functions and communication properties have to be hard coded.

Expand Down Expand Up @@ -78,5 +78,6 @@ The code is organized in the following execution domains:
* fixed-rate background task for I/O logic (asynchronous to MODBUS)
* foreground "idle mode" MODBUS protocol handler
* foreground command line interface (CLI) through independent COM port provided by STM8 eForth
* handlers for MODBUS I/O: `mbpre` for input, `mbact` for output actions)

The different concerns are separeted in the code - it's even possible to change FC handlers through the CLI without restarting the application!
The different concerns are separeted in the code - FC handlers can be changed through the CLI without restarting the application!
12 changes: 9 additions & 3 deletions main.fs
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
#require MBSERVER

2 CONSTANT BAUD9600
$4000 CONSTANT EE_NODE
$4002 CONSTANT EE_BAUD

NVM
\ --- MODBUS server startup

: init ( -- )
BAUD9600 UARTISR
1 mbnode !
EE_NODE @ DUP 0 256 WITHIN NOT IF
DROP 1 \ out of range - use default
THEN
( n ) mbnode !

EE_BAUD @ ( #BR ) UARTISR

MBSERVER
;

Expand Down

0 comments on commit 2952663

Please sign in to comment.