From 2952663ef0d7bdc75a8c57e00ffbc92b89c1bf25 Mon Sep 17 00:00:00 2001 From: Thomas Date: Sun, 12 Apr 2020 14:57:21 +0200 Subject: [PATCH] #12 fix node, baud rate, input handler --- MBPROTO | 32 ++++++++++++++++++++------------ README.md | 9 +++++---- main.fs | 12 +++++++++--- 3 files changed, 34 insertions(+), 19 deletions(-) diff --git a/MBPROTO b/MBPROTO index 0624b09..f084703 100644 --- a/MBPROTO +++ b/MBPROTO @@ -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 ) @@ -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 ; @@ -122,6 +129,7 @@ WIPE RAM #require MBPROTO RAM +#require WIPE #require MBRESET MBRESET \ Reset the MODBUS Function Code table @@ -151,5 +159,5 @@ NVM [ ' MBPROTO ] LITERAL 'IDLE ! \ run MB protocol handler as idle task ; - ' init 'BOOT ! + ' init 'BOOT ! WIPE RAM diff --git a/README.md b/README.md index 3f5ff2d..ee91965 100644 --- a/README.md +++ b/README.md @@ -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. @@ -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. @@ -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! diff --git a/main.fs b/main.fs index b2637c4..b371338 100644 --- a/main.fs +++ b/main.fs @@ -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 ;