Skip to content

Commit

Permalink
Merge pull request #13 from TG9541/C0135
Browse files Browse the repository at this point in the history
#12 board support for C0135
  • Loading branch information
TG9541 authored Apr 12, 2020
2 parents 5c2f5a7 + 5b97cda commit dd1f0aa
Show file tree
Hide file tree
Showing 9 changed files with 284 additions and 47 deletions.
127 changes: 127 additions & 0 deletions C0135/boardcore.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
; STM8S103F3P6 "Minimal System Board" STM8S device dependent routines


; BOARDINIT ( -- )
; Init board GPIO (except COM ports)

BOARDINIT:
; "Nano PLC Relay board" GPIO for relay & LED
MOV PB_DDR,#0b00110000 ; --R1---- R:DE/RE 1:R1
MOV PB_CR1,#0b00000000
MOV PC_DDR,#0b00111000 ; --432--- 4:R4 3:R3 2:R2
MOV PC_CR1,#0b00000000
MOV PD_DDR,#0b00010000 ; ---L---- L:LED
MOV PD_CR1,#0b00010000

.ifne HAS_OUTPUTS
CLR A
JRA AOUTSTOR
.else
RET
.endif

;===============================================================

.ifne HAS_OUTPUTS
RamWord OUTPUTS ; "OUT", e.g. relays, LEDs, etc. (16 bit)

; OUT! ( c -- )
; Put c to board outputs, storing a copy in OUTPUTS

.dw LINK

LINK = .
.db (4)
.ascii "OUT!"
OUTSTOR:
INCW X
LD A,(X)
INCW X
AOUTSTOR:
LD OUTPUTS+1,A
XOR A,#0x0F ; C0135 Relay-4 Board
RRC A
BCCM PB_ODR,#4 ; Relay1
RRC A
BCCM PC_ODR,#3 ; Relay2
RRC A
BCCM PC_ODR,#4 ; Relay3
RRC A
BCCM PC_ODR,#5 ; Relay4
RRC A
BCCM PD_ODR,#4 ; LED
RET
.endif

;===============================================================

; Dummy labels for PSIM interrupts declared in main.c

.ifne PSIM-PORTA
; Dummy label for _EXTIA_IRQHandler
_EXTI0_IRQHandler:
.endif

.ifne PSIM-PORTB
; Dummy label for _EXTIB_IRQHandler
_EXTI1_IRQHandler:
.endif

.ifne PSIM-PORTC
; Dummy label for _EXTIC_IRQHandler
_EXTI2_IRQHandler:
.endif

.ifne PSIM-PORTD
; Dummy label for _EXTID_IRQHandler
_EXTI3_IRQHandler:
.endif

.ifne PSIM-PORTE
; Dummy label for _EXTIE_IRQHandler
_EXTI4_IRQHandler:
.endif

;===============================================================

.ifne HAS_LED7SEG
; LED_MPX driver ( -- )
; Code called from ISR for LED MPX

LED_MPX:
RET
.endif

;===============================================================

.ifne HAS_KEYS

; BKEY ( -- c ) ( TOS STM8: -- A,Z,N )
; Read board key state as a bitfield

.dw LINK

LINK = .
.db (4)
.ascii "BKEY"
BKEY:
; Key "S2" port PA3 (inverted)
LD A,PA_IDR
SLA A
SWAP A
CPL A
AND A,#0x01
JP ASTOR


; BKEYC ( -- c ) ( TOS STM8: -- A,Z,N )
; Read and translate board dependent key bitmap into char

BKEYCHAR:
CALLR BKEY
JREQ 1$
ADD A,#'@'
LD (1,X),A
1$: RET
.endif

40 changes: 40 additions & 0 deletions C0135/globconf.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
; STM8EF Global Configuration File
; Config for C0135 "Relay Board-4 STM8S"

HALF_DUPLEX = 0 ; Use UART in half duplex mode
HAS_RXUART = 0 ; No UART RXD, word ?RX
HAS_TXUART = 0 ; No UART TXD, word TX!
HAS_RXSIM = 1 ; Enable RxD via GPIO/TIM4, word ?RXGP
HAS_TXSIM = 1 ; Like HAS_RXSIM, word TXGP!, use for console if > HAS_TXUART
PSIM = PORTD ; Port for UART simulation
PNRX = 1 ; Port GPIO# for HAS_RXDSIM
PNTX = 1 ; Port GPIO# for HAS_TXDSIM

EMIT_BG = DROP ; 7S-LED background EMIT vector
QKEY_BG = QKEYB ; ?KEYB for background ?KEY

HAS_LED7SEG = 0 ; no 7S-Display
HAS_KEYS = 1 ; yes, 1 key
HAS_OUTPUTS = 5 ; yes, 4 relays, 1 LED
HAS_ADC = 1 ; Analog input words

HAS_BACKGROUND = 1 ; Background Forth task (TIM2 ticker)
HAS_CPNVM = 1 ; Can compile to Flash, always interpret to RAM
HAS_DOES = 1 ; CREATE-DOES> extension
HAS_DOLOOP = 1 ; DO .. LOOP extension: DO LEAVE LOOP +LOOP

CASEINSENSITIVE = 1 ; Case insensitive dictionary search
SPEEDOVERSIZE = 0 ; Speed-over-size in core words: ROT - = <
BAREBONES = 0 ; Remove or unlink some more: hi HERE .R U.R SPACES @EXECUTE AHEAD CALL, EXIT COMPILE [COMPILE]

WORDS_LINKINTER = 0 ; Link interpreter words: ACCEPT QUERY TAP kTAP hi 'BOOT tmp >IN 'TIB #TIB eval CONTEXT pars PARSE NUMBER? DIGIT? WORD TOKEN NAME> SAME? find ABORT aborq $INTERPRET INTER? .OK ?STACK EVAL PRESET QUIT $COMPILE
WORDS_LINKCOMP = 0 ; Link compiler words: cp last OVERT $"| ."| $,n
WORDS_LINKRUNTI = 0 ; Link runtime words: doLit do$ doVAR donxt dodoes ?branch branch
WORDS_LINKCHAR = 1 ; Link char out words: DIGIT <# # #S SIGN #> str hld HOLD
WORDS_LINKMISC = 0 ; Link composing words of SEE DUMP WORDS: >CHAR _TYPE dm+ .ID >NAME

WORDS_EXTRASTACK = 0 ; Link/include stack core words: rp@ rp! sp! sp@ DEPTH
WORDS_EXTRADEBUG = 0 ; Extra debug words: SEE
WORDS_EXTRACORE = 1 ; Extra core words: =0 I
WORDS_EXTRAMEM = 1 ; Extra memory words: B! 2C@ 2C!
WORDS_EXTRAEEPR = 1 ; Extra EEPROM lock/unlock words: LOCK ULOCK ULOCKF LOCKF
14 changes: 14 additions & 0 deletions C0135/target.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
; STM8S103F3 device and memory layout configuration

TARGET = STM8S103F3

RAMEND = 0x03FF ; system (return) stack, growing down
EEPROMEND = 0x427F ; STM8S103F3: 640 bytes EEPROM
FLASHEND = 0x9FFF ; 8K devices

FORTHRAM = 0x0040 ; Start of RAM controlled by Forth
UPPLOC = 0x0060 ; UPP (user/system area) location for 1K RAM
CTOPLOC = 0x0080 ; CTOP (user dictionary) location for 1K RAM
SPPLOC = 0x0350 ; SPP (data stack top), TIB start
RPPLOC = RAMEND ; RPP (return stack top)

40 changes: 23 additions & 17 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@ = ( node address match ) IF
rxbuf C@ ( DUP ." S: " . CR ) txc+
rxbuf 1+ C@ DUP txc+ ( fc )
mbnode @ rxbuf C@ = IF \ node address match
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,14 +129,13 @@ WIPE RAM
#require MBPROTO

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

#require :NVM
#require 'IDLE

2 CONSTANT BAUD9600

NVM
#require MBDUMP

Expand All @@ -146,12 +152,12 @@ NVM
;

: init ( -- )
BAUD9600 UARTISR \ initialize UART handler, set baud rate
1 mbnode ! \ 10 is the node address
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
;

' init 'BOOT !
' init 'BOOT !
WIPE RAM
Loading

0 comments on commit dd1f0aa

Please sign in to comment.