Skip to content

Commit

Permalink
Merge pull request #29 from TG9541/C0135-fix-FC05
Browse files Browse the repository at this point in the history
fixes #28 FC05 addressing off-by-one
  • Loading branch information
TG9541 authored Apr 28, 2020
2 parents 446d35a + 8c05dfe commit b9e1ebc
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 4 deletions.
6 changes: 3 additions & 3 deletions MBSERVER
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,10 @@ NVM \ compile to Flash memory from here on

\ --- FC05 handler "Write Single Coil"
:NVM ( -- )
mbp1 1- ( #b ) DUP 0 [ COILCELLS 16 * ] LITERAL WITHIN IF
mbp1 ( #b ) DUP 0 [ COILCELLS 16 * ] LITERAL WITHIN IF
mbp2 $FF00 =
( #b f ) coils B>L
( #b f a ) ROT ( f a #b ) B!
( #b f ) coils
( #b f a ) ROT ( f a #b ) BF!
MBWR \ MODBUS write response
ELSE
DROP 2 MBEC
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ Please refer to the [Installation Instructions](https://github.com/TG9541/stm8ef

## Console

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. It's also possible to use an STM8S High Density device with two UARTs like 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 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.

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 Down
59 changes: 59 additions & 0 deletions test/FC05
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
\ Minimal MODBUS server with FC05 "Write Single Coil" handler
\ Features:
\ - prints debug infos to the console
\ - interactive tests from the console with, e.g. "coils 10 DUMP"

\ check if the MODBUS protocol core is already present
\ hint: the development cycle will be faster if you PERSIST it
#require MBPROTO

\ Resetting the FC handler table can be helpful for development
#require WIPE
#require MBRESET
MBRESET \ Reset the MODBUS Function Code table

#require ALIAS
#require :NVM
#require 'IDLE
#require .OK

NVM

#require MBDUMP
#require BF!

4 CONSTANT COILCELLS
VARIABLE coils COILCELLS 1- 2* ALLOT

\ --- FC05 handler "Write Single Coil"
:NVM ( -- )
\ write register address and value to the console
." Write register: A:" mbp1 . ." F:" mbp2 . CR

mbp1 ( #b ) DUP 0 [ COILCELLS 16 * ] LITERAL WITHIN IF
mbp2 $FF00 =
( #b f ) coils
( #b f a ) ROT ( f a #b ) BF!
MBWR \ MODBUS write response
ELSE
DROP 2 MBEC
THEN
;NVM ( xth ) 5 FC>XT !

\ custom default action handler
: showfc ( -- )
rxbuf C@ ." FC:" . CR
1 MBEC \ set error code
;

: init ( -- )
0 UARTISR \ init UART handler w/ default baud rate
1 mbnode ! \ set node address
[ ' showfc ] LITERAL mbdef ! \ FC default action (optional feature)
[ ' MBDUMP ] LITERAL mbact ! \ show buffers (debug demo)
[ ' MBPROTO ] LITERAL 'IDLE ! \ run MB protocol handler as idle task
.OK
;

' init 'BOOT !
WIPE RAM

0 comments on commit b9e1ebc

Please sign in to comment.