Skip to content

Instantly share code, notes, and snippets.

@deadprogram
Created April 4, 2022 19:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save deadprogram/6e1f3ecdb79cf223db2fdaeb4534bfe6 to your computer and use it in GitHub Desktop.
Save deadprogram/6e1f3ecdb79cf223db2fdaeb4534bfe6 to your computer and use it in GitHub Desktop.
// NUCLEO-L432KC
// GND to GND
// SCL to D5
// SDA to D4
package main
import (
"fmt"
"machine"
"time"
)
func main() {
machine.I2C0.Configure(machine.I2CConfig{})
led := machine.LED
led.Configure(machine.PinConfig{Mode: machine.PinOutput})
if true {
for {
fmt.Printf("\r\n 0 1 2 3 4 5 6 7 8 9 A B C D E F")
fmt.Printf("\r\n")
for y := uint16(0); y < 0x8; y++ {
led.Low()
time.Sleep(time.Millisecond * 200)
fmt.Printf("\r%02x", y*0x10)
for x := uint16(0); x < 0x10; x++ {
i2caddr := y*0x10 + x
err := machine.I2C0.Tx(i2caddr, []byte{0}, nil)
if err == nil {
fmt.Printf(" %02x", i2caddr)
} else {
fmt.Print(" --")
}
led.High()
time.Sleep(100 * time.Millisecond)
}
fmt.Println("")
}
fmt.Println("")
}
}
}
@soypat
Copy link

soypat commented Apr 4, 2022

Modularized i2cscanner:

func i2cScan(bus drivers.I2C) {
	const wait = 10 * time.Millisecond
	fmt.Printf("\r\n    0  1  2  3  4  5  6  7  8  9  A  B  C  D  E  F")
	fmt.Printf("\r\n")
	for y := uint16(0); y < 0x8; y++ {
		time.Sleep(2 * wait)
		fmt.Printf("\r%02x", y*0x10)
		for x := uint16(0); x < 0x10; x++ {
			i2caddr := y*0x10 + x
			err := bus.Tx(i2caddr, []byte{0}, nil)
			if err == nil {
				fmt.Printf(" %02x", i2caddr)
			} else {
				fmt.Print(" --")
			}
			time.Sleep(wait)
		}
		fmt.Println("")
	}
	fmt.Println("")
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment