go i2c


原文链接: go i2c

https://github.com/d2r2/go-i2c

对10位地址操作 https://github.com/golang/exp/blob/master/io/i2c/i2c.go

/ resolveAddr returns whether the addr is 10-bit masked or not.
// It also returns the unmasked address.
func resolveAddr(addr int) (unmasked int, tenbit bool) {
	return addr & (tenbitMask - 1), addr&tenbitMask == tenbitMask
}

go: AM2320传感器:CRC不匹配,来自传感器的CRC(0)

Electronics DHT12/AM2320 sensors working via i2c-bus


if mpu.enableMag {
				// Set AK8963 to slave0 for reading
				if err := mpu.i2cWrite(MPUREG_I2C_SLV0_ADDR, AK8963_I2C_ADDR|READ_FLAG); err != nil {
					log.Printf("MPU9250 Warning: couldn't set AK8963 address for reading: %s", err)
				}
				//I2C slave 0 register address from where to begin data transfer
				if err := mpu.i2cWrite(MPUREG_I2C_SLV0_REG, AK8963_HXL); err != nil {
					log.Printf("MPU9250 Warning: couldn't set AK8963 read register: %s", err)
				}
				//Tell AK8963 that we will read 7 bytes
				if err := mpu.i2cWrite(MPUREG_I2C_SLV0_CTRL, 0x87); err != nil {
					log.Printf("MPU9250 Warning: couldn't communicate with AK8963: %s", err)
				}

				// Read the actual data
				for p, reg := range magRegMap {
					*p, magError = mpu.i2cRead2(reg)
					if magError != nil {
						log.Println("MPU9250 Warning: error reading magnetometer")
					}
				}

				// Test validity of magnetometer data
				if (byte(m1&0xFF)&AKM_DATA_READY) == 0x00 && (byte(m1&0xFF)&AKM_DATA_OVERRUN) != 0x00 {
					log.Println("MPU9250 Warning: mag data not ready or overflow")
					log.Printf("MPU9250 Warning: m1 LSB: %X\n", byte(m1&0xFF))
					continue // Don't update the accumulated values
				}

				if (byte((m4>>8)&0xFF) & AKM_OVERFLOW) != 0x00 {
					log.Println("MPU9250 Warning: mag data overflow")
					log.Printf("MPU9250 Warning: m4 MSB: %X\n", byte((m1>>8)&0xFF))
					continue // Don't update the accumulated values
				}

				// Update values and increment count of magnetometer readings
				avm1 += int32(m1)
				avm2 += int32(m2)
				avm3 += int32(m3)
				nm++
            }

https://github.com/OnionIoT/OOS-App-Sparkfun-Qwiic/blob/master/omega/programs/amg8833.go

package main

import (
  "log"
  // "fmt"
  "time"
	"github.com/d2r2/go-i2c"
)

const (
  AMG8833_I2C_ADDR = 0x69
  AMG8833_PCTL 	= 0x00
  AMG8833_RST 	= 0x01
  AMG8833_FPSC 	= 0x02
  AMG8833_INTC 	= 0x03
  AMG8833_PIXEL_TEMP_CONVERSION = .25
)

func privateHelperFunc() int {
    return 25
}

func amg8833_setup() {
  // Create new connection to I2C bus
  i2c, err := i2c.NewI2C(AMG8833_I2C_ADDR, I2C_DEV_NUM)
  if err != nil { log.Fatal(err) }
  // Free I2C connection on exit
  defer i2c.Close()

  // perform setup
  err = i2c.WriteRegU8(AMG8833_PCTL, 0x00)  // normal mode
  err = i2c.WriteRegU8(AMG8833_RST, 0x3f)   // software reset
  err = i2c.WriteRegU8(AMG8833_INTC, 0x00)  // disable interrupts
  err = i2c.WriteRegU8(AMG8833_FPSC, 0x00)  // 10 fps

  time.Sleep(100 * time.Millisecond)
}

func twoCompl12(val uint16) (float32) {
  if 0x7FF & val == val {
    return float32(val)
  } else {
    return float32(val)-float32(4096)
  }
}

func amg8833_readPixels() ([]float32) {
  // Create new connection to I2C bus
  i2c, err := i2c.NewI2C(AMG8833_I2C_ADDR, I2C_DEV_NUM)
  if err != nil { log.Fatal(err) }
  // Free I2C connection on exit
  defer i2c.Close()

  var raw uint16
  var val float32
  var buf []float32
  var offset uint8 = 128
  for i := uint8(0); i < 128; i += 2 {
    // fmt.Println("reading from reg", i+offset)
		raw, err = i2c.ReadRegU16LE(i+offset)
    // fmt.Printf("read from 0x%02x: 0x%04x\n", i+offset, raw)
    val = twoCompl12(raw) * float32(AMG8833_PIXEL_TEMP_CONVERSION)
    buf = append(buf, val)
	}
  return buf
}
`