-
Notifications
You must be signed in to change notification settings - Fork 12
/
MBSERVER
186 lines (146 loc) · 4 KB
/
MBSERVER
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
\ A simple STM8 eForth MODBUS Server based on MBPROTO
\ Check if the MODBUS protocol core is already present
#require MBPROTO
\ Resetting the FC handler table can be helpful for development
#require WIPE
#require MBRESET
MBRESET \ Reset the MODBUS Function Code table
WIPE
\ Compile time requirements
#require ALIAS
#require 'IDLE
#require :NVM
#require WIPE
#require LOCK
#require ULOCK
\ Compile time symbols
4 CONSTANT COILCELLS
4 CONSTANT INPUTCELLS
2 CONSTANT HOLDINGCELLS
1024 CONSTANT EECELLS \ this is the maximum for any STM8 device
$4000 CONSTANT EESTART
\ Words for setting and resetting bits, big- and little-endien
#require BF!
NVM \ compile to Flash memory from here on
#require BF@
VARIABLE coils COILCELLS 1- 2* ALLOT
VARIABLE inputs INPUTCELLS 1- 2* ALLOT
VARIABLE holding HOLDINGCELLS 1- 2* ALLOT
\ map holding index to EEPROM or holding variable
: hmap ( i -- b a )
DUP 60000 U< IF
\ limit to last holding register cell
( i ) [ HOLDINGCELLS 1- ] LITERAL MIN
( iH ) 2* holding + 0 ( a 0 ) \ flag RAM
ELSE
( i ) 60000 -
( iEE ) [ EECELLS 1- ] LITERAL MIN
( iEE ) 2* EESTART + 1 ( a 1 ) \ flag EEPROM
THEN
SWAP ( b a )
;
\ write to a holding register using hmap, take care of EEPROM
: hwrite ( i n -- )
>R ( i ) hmap ( b a ) OVER ( b ) IF
ULOCK
THEN
R> ( b a n ) SWAP !
( b ) IF
LOCK
THEN
;
\ --- FC01 "Read Coils"
\ FC01 Read Coils transfer
:NVM ( i -- )
( i ) coils OVER ( i a i ) BF@
( i b ) SWAP mbp1 - ( b i0 )
rtbuf 3 + SWAP ( b a i0 ) LEBF!
;RAM ALIAS FC01ACT
\ FC01 handler
:NVM ( -- )
[ COILCELLS 16 * ] LITERAL mbrange? IF
[ ' FC01ACT ] LITERAL 1 ( xt bpu ) mbread
THEN
;NVM ( xt ) 1 FC>XT !
\ --- FC02 "Read Discrete Inputs"
\ FC02 input register transfer
:NVM ( i -- )
( i ) inputs OVER ( i a i ) BF@
( i b ) SWAP mbp1 - ( b i0 )
rtbuf 3 + SWAP ( b a i0 ) LEBF!
;RAM ALIAS FC02ACT
\ FC02 handler
:NVM ( -- )
[ INPUTCELLS 16 * ] LITERAL mbrange? IF
[ ' FC02ACT ] LITERAL 1 ( xt bpu ) mbread
THEN
;NVM ( xt ) 2 FC>XT !
\ --- FC03 "Read Holding Registers"
\ FC03 holding register iterated transfer
:NVM ( i -- )
( i ) hmap ( b a ) NIP \ discard ULOCK flag
( a ) @ tx+
;NVM ( xt )
\ FC03 handler
:NVM ( -- )
[ ( xt xth ) SWAP ] LITERAL 16 ( xt bpu ) mbread
;NVM ( xth ) 3 FC>XT !
\ --- FC04 "Read Input Registers"
\ FC04 input register iterated transfer
:NVM ( i -- )
2* inputs + @
tx+
;NVM ( xt )
\ FC04 handler
:NVM ( -- )
[ ( xt xth ) SWAP ] LITERAL 16 ( xt bpu ) mbread
;NVM ( xth ) 4 FC>XT !
\ --- FC05 handler "Write Single Coil"
:NVM ( -- )
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 !
\ --- FC06 handler "Write Single Register"
:NVM ( -- )
mbp1 mbp2 ( i n ) hwrite
MBWR \ MODBUS write response
;NVM ( xth ) 6 FC>XT !
\ --- FC15 "Write Multiple Coils"
\ FC15 Write Multiple Coils write transfer
:NVM ( i -- )
( i ) rtbuf 7 + OVER mbp1 - ( i a i0 ) LEBF@ SWAP ( b i )
coils SWAP ( b a i ) BF!
;RAM ALIAS FC15ACT
\ FC15 handler
:NVM ( -- )
[ COILCELLS 16 * ] LITERAL mbrange? IF
[ ' FC15ACT ] LITERAL ( xt ) mbloop
MBWR \ MODBUS write response
THEN
;NVM ( xth ) 15 FC>XT !
\ --- FC16 "Write Multiple Register"
\ MB read PDU RX data using register index
:NVM ( i -- n )
mbp1 - 2* rtbuf + 7 + @
;RAM ALIAS mbrxd
\ FC16 holding register write transfer
:NVM ( i -- )
( i ) DUP mbrxd ( i n ) hwrite
;RAM ALIAS FC16ACT
\ FC16 handler
:NVM ( -- )
HOLDINGCELLS mbrange? IF
[ ' FC16ACT ] LITERAL ( xt ) mbloop
MBWR \ MODBUS write response
;NVM ( xt ) 16 FC>XT !
\ Registration of FC handlers complete
\ A dumy word for #require
: MBSERVER ( -- )
;
WIPE RAM