-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathi2c-detect-v2.txt
55 lines (51 loc) · 1.5 KB
/
i2c-detect-v2.txt
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
\ file i2c-detect-v2.txt
\ Detect all possible devices on I2C bus
\ Only the 7 bit address schema is supported.
\
\ Copied from amForth distribution (lib/hardware/)
\ and lightly edited to suit FlashForth 5.0 on AVR.
\ Builds upon i2c-base.txt
\ Peter J. 2014-10-27
\ Modified Marc PETREMANN 11 nov. 2019
\ require i2c-base-avr-v2.txt
-i2c-detect
marker -i2c-detect
\ not all bitpatterns are valid 7bit i2c addresses
: i2c.7bitaddr? ( a -- f)
$07 $78 within
;
: i2c.detect ( -- )
base @ >r hex
cr 5 spaces \ header line
$10 for
r@ 2 u.r
next
$80 for
r@ $0f and 0=
if cr r@ 2 u.r [char] : emit space
then
r@ i2c.7bitaddr?
if r@ i2c.ping?
if dup 2 u.r
else ." -- " then
else
." "
then
next
cr r> base !
;
\ With a lone Microchip TC74A0 sitting on the bus,
\ the output looks like
\ i2c.init ok<$,ram>
\ i2c.detect
\ 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f
\ 00 : -- -- -- -- -- -- -- -- --
\ 10 : -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
\ 20 : -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
\ 30 : -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
\ 40 : -- -- -- -- -- -- -- -- 48 -- -- -- -- -- -- --
\ 50 : -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
\ 60 : -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
\ 70 : -- -- -- -- -- -- -- --
\ ok<$,ram>
\ i2c.stop ok<$,ram>