-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #38 from TG9541/update
Update
- Loading branch information
Showing
9 changed files
with
160 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
\ STM8EF-MODBUS bus control for the STM8S001J3RS485 board | ||
|
||
\res MCU: STM8S103 | ||
\res export PD_ODR PD_DDR PD_CR1 | ||
\res export UART1_CR2 UART1_CR5 UART1_DR | ||
|
||
5 CONSTANT #RIEN | ||
|
||
#require ]B! | ||
#require WIPE | ||
|
||
NVM | ||
\ Set RS485 Driver to "RX" | ||
: BUSrx ( -- ) | ||
[ 0 PD_ODR 6 ]B! | ||
UART1_DR C@ DROP \ remove any received char | ||
[ 0 UART1_CR2 #RIEN ]B! \ re-enable interrupt | ||
; | ||
|
||
\ Set RS485 Driver to "TX" | ||
: BUStx ( -- ) | ||
[ 0 UART1_CR2 #RIEN ]B! \ RX/TX is a shared pin - play it safe | ||
[ 1 PD_ODR 6 ]B! | ||
; | ||
|
||
\ Initialize GPIO and RS485 Driver | ||
: BUSCTRL ( -- ) | ||
[ 1 UART1_CR5 3 ]B! \ UART1 Half-Duplex | ||
[ 0 PD_DDR 5 ]B! \ PD5 input mode | ||
[ 1 PD_CR1 5 ]B! \ PD5 UART_TXRX pull-up | ||
[ 1 PD_DDR 6 ]B! \ PD6 !RE/DE output mode | ||
[ 1 PD_CR1 6 ]B! \ PD6 push-pull | ||
BUSrx | ||
; | ||
WIPE RAM |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
\ STM8 eForth MODBUS board code for the STM8S001J3RS485 board | ||
|
||
( Hint for non-Forthers ) | ||
\ - this and the above are comments | ||
\ - @ means "read" and ! means "write" | ||
\ - : means "compile", [ switches to "interpret", ] back and ; "end compile" | ||
\ - #require, \res, etc are e4thcom or codeload.py keywords | ||
|
||
\ pre-load BUSCTRL so that a later #require won't load the C0135 default code | ||
#include STM8S001J3RS485/BUSCTRL | ||
|
||
\ compile MODBUS server and protocol words | ||
#require MBSERVER | ||
|
||
\ no inputs here but this would be a good place to start (or use I2C) | ||
\ #include C0135/IN@ | ||
|
||
\ we're in RAM mode: load "scaffolding words" | ||
#require :NVM | ||
#require WIPE | ||
#require LOCK | ||
#require ULOCK | ||
#require 'IDLE | ||
#require .OK | ||
|
||
\ define temporary constants | ||
$4000 CONSTANT EE_NODE | ||
$4002 CONSTANT EE_BAUD | ||
|
||
\ now compile to Flash ROM | ||
NVM | ||
|
||
\ set MODBUS RTU default node ID (1) and 9600 baud RTU | ||
: default ( -- ) | ||
ULOCK | ||
1 EE_NODE ! \ 1 as Node-ID (holding register 0) | ||
0 EE_BAUD ! \ default rate (holding register 1) | ||
LOCK | ||
; | ||
|
||
\ headerless code Preparation Handler | ||
:NVM | ||
\ no inputs here but this is a good place to read a sensor value from I2C | ||
\ IN@ inputs ! | ||
;NVM ( xt-pre ) \ compile time: keep this eXecution Token on the stack | ||
|
||
\ headerless code Action Handler | ||
:NVM | ||
\ no outputs here but this is the right place to write to I2C | ||
\ coils @ OUT! | ||
;NVM ( xt-act ) \ and also this execution token | ||
|
||
\ --- MODBUS server startup | ||
: init ( -- ) | ||
\ register the xt (see above) as the MODBUS Action Handler | ||
( xt-act ) LITERAL mbact ! | ||
( xt-pre ) LITERAL mbpre ! | ||
|
||
\ Holding C0135 key "S2" while start-up resets Node-ID and baud rate | ||
\ BKEY IF | ||
default | ||
\ THEN | ||
|
||
\ no inputs here but this is the right place to init the I2C peripheral | ||
\ IN@INIT | ||
|
||
\ initialize MODBUS "coils" and outputs | ||
0 coils ! ( no outputs here \ 0 OUT! ) | ||
|
||
\ set MODBUS node ID | ||
EE_NODE @ DUP 0 256 WITHIN NOT IF | ||
DROP 1 \ out of range - use 1 as default node ID | ||
THEN ( n ) mbnode ! | ||
|
||
\ start interrupt handler | ||
EE_BAUD @ ( #BR ) UARTISR | ||
|
||
\ register protocol handler | ||
[ ' MBPROTO ( xt ) ] LITERAL 'IDLE ! | ||
|
||
CR ." STM8EF-MODBUS STM8S001J3RS485" .OK | ||
; | ||
|
||
\ register initialization | ||
' init 'BOOT ! | ||
WIPE RAM |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters