Skip to content

Commit

Permalink
Merge pull request #241 from TG9541/examples
Browse files Browse the repository at this point in the history
Examples
  • Loading branch information
TG9541 authored Dec 27, 2018
2 parents d2c29ea + e8e4e35 commit 17d2e73
Show file tree
Hide file tree
Showing 12 changed files with 311 additions and 77 deletions.
18 changes: 14 additions & 4 deletions docs/words.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
```

```
; next ( -- )
; donext ( -- )
; Code for single index loop.
```

Expand Down Expand Up @@ -126,6 +126,11 @@
; Code for VARIABLE and CREATE.
```

```
; Y> ( -- n ) ( TOS STM8: - Y,Z,N )
; push Y to stack
```

```
; R@ ( -- w ) ( TOS STM8: -- Y,Z,N )
; Copy top of return stack to stack (or the FOR - NEXT index value).
Expand Down Expand Up @@ -257,6 +262,11 @@
; Point to last name in dictionary
```

```
; A> ( -- n ) ( TOS STM8: - Y,Z,N )
; push A to stack
```

```
; TIB ( -- a ) ( TOS STM8: -- Y,Z,N )
; Return address of terminal input buffer.
Expand Down Expand Up @@ -697,17 +707,17 @@
```

```
; YFLAGS ( n -- ) ( TOS STM8: - Y,Z,N )
; >Y ( n -- ) ( TOS STM8: - Y,Z,N )
; Consume TOS to CPU Y and Flags
```

```
; AFLAGS ( c -- ) ( TOS STM8: - A,Z,N )
; >A ( c -- ) ( TOS STM8: - A,Z,N )
; Consume TOS to CPU A and Flags
```

```
; parse ( b u c -- b u delta ; <string> )
; SPARSE ( b u c -- b u delta ; <string> )
; Scan string delimited by c.
; Return found string and its offset.
```
Expand Down
5 changes: 5 additions & 0 deletions examples/i2c
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
\ stm8ef : i2c loader MM-171129

#include voc-i2c-core.fs
#include voc-i2c.fs

97 changes: 97 additions & 0 deletions examples/voc-i2c-core.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
\ stm8ef : voc-i2c-core.fs MM-170928
\ ------------------------------------------------------------------------------
\ I2C Library, Core Word Set, Bit-Bang Implementation
\
\ Copyright (C) 2017 manfred.mahlow@forth-ev.de
\
\ Requires: * STM8S eForth on STM8S103xy
\ * e4thcom -t stm8ef
\
\ Uses: PB5 as serial data pin (SDA)
\ PB4 as serial clock pin (SCL)

\ I2C clock frequency : ?
\
\ External pull-up resistors of 10 kOhm are required on the SDA and SCL pins.
\

#require VOC \ requires the persistent patch for context switching, see CURRENT

#require CONSTANT
#require :NVM
#require ALIAS
#require ]B!

\res MCU: STM8S103

RAM

\res export PB_IDR PB_ODR PB_DDR
\res export BIT5

:NVM ( -- ) [ 0 PB_DDR 5 ]B! ;RAM ALIAS SDA1
:NVM ( -- ) [ 1 PB_DDR 5 ]B! ;RAM ALIAS SDA0
:NVM ( -- f ) PB_IDR C@ BIT5 AND ;RAM ALIAS SDA?
:NVM ( -- ) [ 0 PB_DDR 4 ]B! ;RAM ALIAS SCL1
:NVM ( -- ) [ 1 PB_DDR 4 ]B! ;RAM ALIAS SCL0

NVM

VOC i2c i2c DEFINITIONS

: init ( -- )
\ Initialize the I2C Bus interface
SDA1 SCL1 \ enable I2C pins as input
[ 0 PB_ODR 5 ]B! [ 0 PB_ODR 4 ]B! \ set output registers low
;

\ : wait ( -- ) ;

: start ( -- ) \ in: SDA=? SCL=? out: SDA=SCL=0
\ Start an I2C Bus transmission.
SDA1 SCL1 ( wait ) SDA0 ( wait ) SCL0 ;

: stop ( -- )
\ Stop an I2C Bus transmission.
SDA0 ( wait ) SCL1 ( wait ) SDA1 ;

: tx ( byte -- )
\ Send a byte to the I2C Bus.
7 FOR DUP $80 AND IF SDA1 ELSE SDA0 THEN ( wait ) SCL1 ( wait ) SCL0 2* NEXT
DROP ;

: nak? ( -- f ) \ in: SDA=? SCL=0 out: SDA=1 SCL=0
\ Return a true/false flag if an NAK/ACK was received from the I2C Bus.
SDA1 ( wait ) SCL1 ( wait ) SDA? SCL0 ;

: rx ( -- byte ) \ in: SDA=1 SCL=0 out: SDA=? SCL=0
\ Receive a byte from the I2C Bus.
0 7 FOR ( wait ) SCL1 ( wait ) SDA? SCL0 IF 1 ELSE 0 THEN SWAP 2* OR NEXT ;

: nak ( -- ) \ in: SDA=? SCL=0 out: SDA=1 SCL=0
\ Send an NAK to the I2C Bus.
SDA1 ( wait ) SCL1 ( wait ) SCL0 ;

: ack ( -- ) \ in: SDA=? SCL=0 out: SDA=1 SCL=0
\ Send an ACK to the I2C Bus.
SDA0 ( wait ) SCL1 ( wait ) SCL0 SDA1 ;

: rdy ( sid -- f )
\ Send a start/stop sequence with the slave address sid and return true if an
\ ACK is returned. Otherwise return a false flag.
2* i2c start i2c tx i2c nak? 0= i2c stop ;

FORTH DEFINITIONS

RAM WIPE

i2c init

\index i2c rdy . i2c init i2c WORDS

\ ------------------------------------------------------------------------------
\ Last Revision: MM-171121 B! --> ]B!




26 changes: 26 additions & 0 deletions examples/voc-i2c-eeprom.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
\ stm8ef : I2C Example, rudiments of an EEPROM Library MM-171129

#require i2c

RAM

#require CONSTANT

i2c DEFINITIONS

NVM

VOC eeprom i2c eeprom DEFINITIONS

$50 CONSTANT sid

: C@ ( a -- c ) 1 SWAP i2c eeprom sid i2c read ;

: C! ( c a -- ) 2 i2c eeprom sid i2c write ;

FORTH DEFINITIONS

RAM WIPE

\index i2c init i2c eeprom WORDS i2c WORDS

62 changes: 62 additions & 0 deletions examples/voc-i2c.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
\ stm8ef : voc-i2c.fs MM-170928
\ ------------------------------------------------------------------------------
\ I2C Library, Extended Word Set ( extends voc-i2c-core.fs )
\
\ Copyright (C) 2017 manfred.mahlow@forth-ev.de
\
\ Requires: * STM8S eForth on STM8S103xy
\ * e4thcom -t stm8ef
\ * voc-i2c-core-fs
\

\ #require LSHIFT
\ #require RSHIFT

#require WIPE
#require ABORT"

NVM i2c DEFINITIONS

BASE @ HEX

: ?ack ( -- )
\ Throw an error if no ACK was received.
i2c nak? DUP IF i2c stop THEN ABORT" I2C: ACK missing"
;

: out ( cn .. c1 +n sid -- )
\ Send sid and +n bytes without start and stop condition to an I2C slave. sid
\ is the slaves 7 bit id or addr. An ambiguous condition exists for n = 0.
2* ( r/w bit = 0 ) SWAP FOR i2c tx i2c ?ack NEXT
;

: write ( cn .. c1 +n sid -- )
\ Send sid and +n bytes with start and stop condition to an I2C slave. sid is
\ the slaves 7 bit id or addr. An ambiguous condition exists for n = 0.
i2c start i2c out i2c stop ;

: in ( +n sid -- c1 .. cn )
\ Read +n bytes from I2C slave sid. Do not send a start or stop condition. An
\ ambiguous condition exists for n = 0.
2* 1 OR i2c tx i2c ?ack 1 - FOR i2c rx R@ IF i2c ack ELSE i2c nak THEN NEXT ;

: read ( +n c|a sid -- c1 .. cn )
\ Send the command or address byte c|a to the I2C slave sid and read +n bytes
\ back. sid is the slaves 7 bit identifier or address. An ambiguous condition
\ exists for n = 0.
>R 1 R@ i2c start i2c out \ send c|a
R> i2c start i2c in i2c stop ; \ read data bytes

\ A dummy for compatibility with the hardware based I2C version.
: ?busy ( -- ) ;

BASE !

FORTH DEFINITIONS

RAM WIPE

\ ------------------------------------------------------------------------------
\ Last Revision: MM-171129 Comment updated
\ MM-170928 ported from noForth V

22 changes: 13 additions & 9 deletions forth.asm
Original file line number Diff line number Diff line change
Expand Up @@ -781,7 +781,7 @@ LEAVE:
.endif
.endif

; next ( -- )
; donext ( -- )
; Code for single index loop.

.ifne WORDS_LINKRUNTI
Expand Down Expand Up @@ -1014,8 +1014,10 @@ DOVAR:
POPW Y ; get return addr (pfa)
; fall through

; YSTOR core ( - n ) ( TOS STM8: - Y,Z,N )
; Y> ( -- n ) ( TOS STM8: - Y,Z,N )
; push Y to stack

; HEADER YSTOR "Y>"
YSTOR:
DECW X ; SUBW X,#2
DECW X
Expand Down Expand Up @@ -1327,8 +1329,10 @@ TQKEY:
LAST:
LD A,#(USRLAST)

; ASTOR core ( - n ) ( TOS STM8: - Y,Z,N )
; A> ( -- n ) ( TOS STM8: - Y,Z,N )
; push A to stack

; HEADER ASTOR "A>"
ASTOR:
CLRW Y
LD YL,A
Expand Down Expand Up @@ -2665,10 +2669,10 @@ QUEST:

; Parsing

; YFLAGS ( n -- ) ( TOS STM8: - Y,Z,N )
; >Y ( n -- ) ( TOS STM8: - Y,Z,N )
; Consume TOS to CPU Y and Flags

; HEADER YFLAGS "YFLAGS"
; HEADER YFLAGS ">Y"
YFLAGS:
LDW Y,X
INCW X
Expand All @@ -2677,22 +2681,22 @@ YFLAGS:
RET


; AFLAGS ( c -- ) ( TOS STM8: - A,Z,N )
; >A ( c -- ) ( TOS STM8: - A,Z,N )
; Consume TOS to CPU A and Flags

; HEADER AFLAGS "AFLAGS"
; HEADER AFLAGS ">A"
AFLAGS:
INCW X
LD A,(X)
INCW X
TNZ A
RET

; parse ( b u c -- b u delta ; <string> )
; SPARSE ( b u c -- b u delta ; <string> )
; Scan string delimited by c.
; Return found string and its offset.

HEADER PARS "pars"
HEADER PARS "SPARSE"
PARS:
CALLR AFLAGS ; TEMP CSTOR
PUSH A
Expand Down
20 changes: 10 additions & 10 deletions inc/defconf.inc
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,12 @@
UNLINK_TXSTOR = 0 ; "TX!"
UNLINK_QKEY = 0 ; "?KEY"
UNLINK_EMIT = 0 ; "EMIT"
UNLINK_DOLIT = 0 ; "doLit"
UNLINK_DOLIT = 1 ; "doLit"
UNLINK_DOPLOOP = 1 ; "(+loop)"
UNLINK_LEAVE = 0 ; "LEAVE"
UNLINK_DONXT = 0 ; "donxt"
UNLINK_QBRAN = 0 ; "?branch"
UNLINK_BRAN = 0 ; "branch"
UNLINK_DONXT = 1 ; "donxt"
UNLINK_QBRAN = 1 ; "?branch"
UNLINK_BRAN = 1 ; "branch"
UNLINK_EXECU = 0 ; "EXECUTE"
UNLINK_EXIT = 0 ; "EXIT"
REMOVE_EXIT = 0 ; remove "EXIT"
Expand All @@ -101,7 +101,7 @@
UNLINK_CSTOR = 0 ; "C!"
UNLINK_IGET = 0 ; "I"
UNLINK_RFROM = 0 ; "R>"
UNLINK_DOVAR = 0 ; "doVar"
UNLINK_DOVAR = 1 ; "doVar"
UNLINK_RAT = 0 ; "R@"
UNLINK_TOR = 0 ; ">R"
UNLINK_NIP = 0 ; "NIP"
Expand Down Expand Up @@ -129,7 +129,7 @@
UNLINK_LAST = 0 ; "last"
UNLINK_TIB = 1 ; "TIB"
UNLINK_OUTA = 0 ; "OUT"
UNLINK_BLANK = 0 ; "BL"
UNLINK_BLANK = 1 ; "BL"
UNLINK_ZERO = 0 ; "0"
UNLINK_ONE = 0 ; "1"
UNLINK_MONE = 0 ; "-1"
Expand Down Expand Up @@ -209,7 +209,7 @@
UNLINK_UDOT = 0 ; "U."
UNLINK_DOT = 0 ; "."
UNLINK_QUEST = 0 ; "?"
UNLINK_PARS = 1 ; "pars"
UNLINK_PARS = 1 ; "SPARSE"
UNLINK_PARSE = 1 ; "PARSE"
UNLINK_DOTPR = 0 ; ".("
UNLINK_PAREN = 0 ; "("
Expand Down Expand Up @@ -252,7 +252,7 @@
UNLINK_IFF = 0 ; "IF"
UNLINK_THENN = 0 ; "THEN"
UNLINK_ELSE = 0 ; "ELSE"
UNLINK_AHEAD = 0 ; "AHEAD"
UNLINK_AHEAD = 1 ; "AHEAD"
UNLINK_WHILE = 0 ; "WHILE"
UNLINK_REPEA = 0 ; "REPEAT"
UNLINK_AFT = 0 ; "AFT"
Expand All @@ -266,7 +266,7 @@
UNLINK_COLON = 0 ; ":"
UNLINK_RBRAC = 0 ; "]"
UNLINK_DOESS = 0 ; "DOES>"
UNLINK_DODOES = 0 ; "dodoes"
UNLINK_DODOES = 1 ; "dodoes"
UNLINK_CREAT = 0 ; "CREATE"
UNLINK_CONST = 0 ; "CONSTANT"
UNLINK_DOCON = 1 ; "docon"
Expand Down Expand Up @@ -301,4 +301,4 @@
UNLINK_RESETT = 0 ; "RESET"
UNLINK_SAVEC = 0 ; "SAVEC"
UNLINK_RESTC = 0 ; "IRET"
UNLINK_WIPE = 1 ; "WIPE"
UNLINK_WIPE = 0 ; "WIPE"
Loading

0 comments on commit 17d2e73

Please sign in to comment.