aboutsummaryrefslogtreecommitdiff
path: root/amforth-6.5/common/lib/hardware/i2c-detect.frt
blob: 6bd7fe4f23ba265fe65e49cd9a6a4c218470aaa0 (plain)
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
\ detect presence of all possible devices on I2C bus
\ only the 7 bit address schema is supported

\ not all bitpatterns are valid 7bit i2c addresses
: i2c.7bitaddr? ( a -- f)  $7 $78 within ;

: i2c.detect   ( -- )
    base @ hex
    \ header line
    4 spaces $10 0 do i 3 .r loop
    $80 0 do
      i $0f and 0= if
        cr i 2 .r [char] : emit space
      then
      i i2c.7bitaddr? if
        i i2c.ping? if \ does device respond?
            i 3 .r
          else
            ."  --" 
        then
      else
         ."    "
      then
    loop 
    cr base !
;

\ output looks like
\ (ATmega1280)> i2c.detect 
\       0  1  2  3  4  5  6  7  8  9  A  B  C  D  E  F
\  0:                       -- -- -- -- -- -- -- -- --
\ 10:  -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
\ 20:  -- -- -- -- -- -- -- 27 -- -- -- -- -- -- -- --
\ 30:  -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
\ 40:  -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
\ 50:  50 -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
\ 60:  -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
\ 70:  -- -- -- -- -- -- -- --                        
\  ok
\