func scan(...string) error { var addr uint fmt.Println(" 0 1 2 3 4 5 6 7 8 9 a b c d e f") for addr = 0x00; addr < 0x77; addr++ { if addr%16 == 0 { fmt.Printf("%02x: ", addr/16) } s, err := bus.NewI2C(addr, *dev) if err != nil { return err } if ((addr < 0x30) || (addr >= 0x40 && addr <= 0x47) || (addr >= 0x60)) && s.Mask()&bus.I2C_FUNC_SMBUS_QUICK != 0 { if err = bus.SMBusWriteQuick(s.Fd(), bus.SMBUS_WRITE); err != nil { fmt.Print("~~ ") } else { fmt.Print("XX ") } } else { b, err := bus.SMBusRead(s.Fd(), 0, bus.SMBUS_BYTE) if err != nil { fmt.Print("-- ") } else { fmt.Printf("%02X ", b[0]) } } if addr%16 == 15 { fmt.Println() } } fmt.Println() return nil }
func New(addr, dev uint) (*Clock, error) { defer mutex.Unlock() mutex.Lock() if k, ok := clockTable[addrKey(addr, dev)]; ok { return k, nil } i, err := bus.NewI2C(addr, dev) if err != nil { return nil, err } r := &Clock{i, &sync.Mutex{}} clockTable[addrKey(addr, dev)] = r return r, nil }
func openDeviceI2c(irq, rst uint8) (d *deviceI2c, err error) { dev, err := bus.NewI2C(0, 0x01) if err != nil { return } d = &deviceI2c{ irq: core.Pin(irq), reset: core.Pin(rst), dev: dev, } d.irq.Input() d.reset.Output() d.reset.DigitalWrite(core.HIGH) d.reset.DigitalWrite(core.LOW) core.Delay(400) d.reset.DigitalWrite(core.HIGH) core.Delay(10) return }
func factory(args ...string) func(args ...string) error { var cmd string = args[0] return func(args ...string) error { if len(args) == 0 { return errNoAddr } addr, err := strconv.ParseUint(args[0], 16, 16) if err != nil { return err } s, err := bus.NewI2C(uint(addr), *dev) if err != nil { return err } defer s.Close() mask := s.Mask() fmt.Printf("bus: %d, addr: 0x%02x, mask: 0x%08X\n", *dev, addr, mask) switch cmd { case "readbyte": return smbread(s.Fd(), bus.SMBUS_BYTE_DATA, args[1:]) case "readword": return smbread(s.Fd(), bus.SMBUS_WORD_DATA, args[1:]) case "readblock": return smbread(s.Fd(), bus.SMBUS_BLOCK_DATA, args[1:]) case "probe": for i := range fns { if uint64(fns[i].code)&mask != 0 { fmt.Printf("\t[x] %s\n", fns[i].name) } else { fmt.Printf("\t[ ] %s\n", fns[i].name) } } return nil default: } return errUnsupported } }
func main() { flag.Parse() if !*readTime && !*setTime && !*writeTime { flag.Usage() return } var err error if i, err = bus.NewI2C(*addr, *dev); err != nil { fmt.Fprintln(os.Stderr, err) os.Exit(2) } if *readTime { if t := getDateDS1307(); t != nil { fmt.Println(t) } } if *setTime { if t := getDateDS1307(); t != nil { tv := syscall.Timeval{ Sec: int32(t.Unix()), Usec: int32(t.UnixNano() % 100000000), } if err = syscall.Settimeofday(&tv); err != nil { fmt.Fprintln(os.Stderr, err.Error()) } } } if *writeTime { now := time.Now() fmt.Println("set time -", now) setDateDS1307(&now) time.Sleep(100 * time.Millisecond) if t := getDateDS1307(); t != nil { fmt.Println(t) } } }