Devices and chips that use the Dallas/Maxim 1-Wire protocol, e.g. iButton have been around since the 1990s. The DS18B20 temperature sensor is still very popular (although it's more likely that you get one of its clones).
This page contains 1-Wire primitives for STM8 eForth. Higher level words, e.g. ROM scanning or the DOW-CRC8 will be added when required.
\ STM8 eForth "1-Wire" communication primitives
\res MCU: STM8S103
\res export PB_DDR PB_IDR
#require ]B!
#require ]B?
#require ]CB
#require ]BC
#require :NVM
#require ALIAS
: gpio.d ( -- a c ) PB_DDR 4 ; \ literals for DDR GPIO
: gpio.i ( -- a c ) PB_IDR 4 ; \ literals IDR GPIO
:NVM ( -- ) \ GPIO low / dominant
[ 1 gpio.d ]B!
;RAM ALIAS 1w.l
:NVM ( -- ) \ GPIO floating / input
[ 0 gpio.d ]B!
;RAM ALIAS 1w.f
:NVM ( n -- ) \ wait n x 2us @16MHz
FOR [ $9d9d , $9d9d , $9d9d , $9d C, ] NEXT
;RAM ALIAS x2us
NVM
: 1w.res ( -- f ) \ 1-wire reset with presence check
1w.l 240 x2us 1w.f 30 x2us [ gpio.i ]B? NOT 210 x2us
;
: 1w.t ( c -- c ) \ 1-wire transfer c -> c
7 FOR
1w.l 1 x2us
[ $6401 , \ SRL (1,X)
$8C C, \ CCF
gpio.d ]CB 3 x2us [ gpio.i ]BC
[ $76 C, ] \ RRC (X)
12 x2us
1w.f 1 x2us
NEXT EXG
;
RAM
Using these primitives the data can be exchanged with a 1-Wire node quite easily:
VARIABLE RomID 14 ALLOT
RomID 16 ERASE
: 1w.readrom
1w.res
$33 1w.t drop
7 for
$ff 1w.t
[ RomID 7 + ] LITERAL I - C!
next
RomID 10 DUMP
;
The 1-Wire Search Algorithm in Maxim app-note 187 requires factoring 1w.t
. There should be an implementation in "1wire.frt" by Brad Rodriguez but that appears to have dropped out of the web.
The following implementation of the well known CRC8 for 1-Wire is going to be added to the STM8 lib folder:
\ STM8 eForth: CRC8, polynomial x8 + x5 + x4 + 1
\ refer to github.com/TG9541/stm8ef/blob/master/LICENSE.md
\ implementation for 1-wire protocol
: CRC8 ( crc c -- crc ) [ \ DOW-CRC8
$A608 , \ LD A,#8 ; loop through 8 bits
$F7 C, \ LD (X),A ; use MSB as bit counter
$E601 , \ LD A,(1,X) ;
$E803 , \ 1$: XOR A,(3,X) ; crc XOR c.bit0
$46 C, \ RRC A ; to carry
$E603 , \ LD A,(3,X) ; crc -> A
$2402 , \ JRNC 0$
$A818 , \ XOR A,#0x18 ; apply x5 + x4
$46 C, \ 0$: RRC A ; apply x8 + 1
$E703 , \ LD (3,X),A ; update crc value
$6401 , \ SRL (1,X) ; next c.bit0
$E601 , \ LD A,(1,X)
$7A C, \ DEC (X) ; bit counter until 0
$26ED , \ JRNE 1$ ; loop?
$5C C, \ INCW X ; DROP
$5C C, \ INCW X
] ;
\\ Test
\ DALLAS MAXIM AN937 Figure 3 Example Calculation for DOW CRC
HEX 0 2 CRC8 1C CRC8 B8 CRC8 1 CRC8 0 CRC8 0 CRC8 0 CRC8 . DECIMAL \ -> A2
Also check out the 1-Wire-Advanced Gist that uses the CamelForth "onewire.f" solution.