d2r2 / go-sht3x

Golang library to interact with Sensirion SHT3x relative humidity and temperature sensor's family via I2C-bus from Raspberry PI.
MIT License
6 stars 4 forks source link

CRC mismatch using example code #3

Open vendion opened 5 years ago

vendion commented 5 years ago

Using the example code found in this repo I am getting a CRC mismatch error when running it on my Onion Omega2+ compiled using Go 1.13.4.

2019-11-11T23:36:20.140 [    main] NOTICE  **********************************************************************************************
2019-11-11T23:36:20.141 [    main] NOTICE  *** !!! READ THIS !!!
2019-11-11T23:36:20.141 [    main] NOTICE  *** You can change verbosity of output, by modifying logging level of modules "i2c", "sht3x".
2019-11-11T23:36:20.142 [    main] NOTICE  *** Uncomment/comment corresponding lines with call to ChangePackageLogLevel(...)
2019-11-11T23:36:20.142 [    main] NOTICE  *** !!! READ THIS !!!
2019-11-11T23:36:20.143 [    main] NOTICE  **********************************************************************************************
2019-11-11T23:36:20.143 [   sht3x] DEBUG  Reset sensor...
2019-11-11T23:36:20.144 [     i2c] DEBUG  Write 2 hex bytes: [30a2]
2019-11-11T23:36:20.146 [    main] NOTICE  **********************************************************************************************
2019-11-11T23:36:20.147 [    main] NOTICE  *** Read sensor states
2019-11-11T23:36:20.147 [    main] NOTICE  **********************************************************************************************
2019-11-11T23:36:20.148 [   sht3x] DEBUG  Getting heater status...
2019-11-11T23:36:20.148 [     i2c] DEBUG  Write 2 hex bytes: [f32d]
2019-11-11T23:36:20.150 [     i2c] DEBUG  Read 3 hex bytes: [8010ff]
2019-11-11T23:36:20.150 [    main] FATAL  CRCs doesn't match: CRC from sensor (0xFF) != calculated CRC (0xE1)
d2r2 commented 5 years ago

UPDATE: no issue found in environment where this library was tested (Linux OS).

Confirm. I have almost the same:

root@orangepizero:~/gocode/src/github.com/d2r2/go-sht3x/examples# go run ./example1.go 
2019-11-13T00:20:39.808 [    main] NOTICE  **********************************************************************************************
2019-11-13T00:20:39.808 [    main] NOTICE  *** !!! READ THIS !!!
2019-11-13T00:20:39.809 [    main] NOTICE  *** You can change verbosity of output, by modifying logging level of modules "i2c", "sht3x".
2019-11-13T00:20:39.809 [    main] NOTICE  *** Uncomment/comment corresponding lines with call to ChangePackageLogLevel(...)
2019-11-13T00:20:39.809 [    main] NOTICE  *** !!! READ THIS !!!
2019-11-13T00:20:39.809 [    main] NOTICE  **********************************************************************************************
2019-11-13T00:20:39.809 [   sht3x] DEBUG  Reset sensor...
2019-11-13T00:20:39.809 [     i2c] DEBUG  Write 2 hex bytes: [30a2]
2019-11-13T00:20:39.812 [    main] NOTICE  **********************************************************************************************
2019-11-13T00:20:39.812 [    main] NOTICE  *** Read sensor states
2019-11-13T00:20:39.812 [    main] NOTICE  **********************************************************************************************
2019-11-13T00:20:39.812 [   sht3x] DEBUG  Getting heater status...
2019-11-13T00:20:39.812 [     i2c] DEBUG  Write 2 hex bytes: [f32d]
2019-11-13T00:20:39.813 [     i2c] DEBUG  Read 3 hex bytes: [8010e1]
2019-11-13T00:20:39.813 [   sht3x] DEBUG  CRCs verified: CRC from sensor (0xE1) = calculated CRC (0xE1)
2019-11-13T00:20:39.814 [    main] INFO  Heater ON status = false
2019-11-13T00:20:39.814 [   sht3x] DEBUG  Getting alert pending status...
2019-11-13T00:20:39.814 [     i2c] DEBUG  Write 2 hex bytes: [f32d]
2019-11-13T00:20:39.815 [     i2c] DEBUG  Read 3 hex bytes: [8010e1]
2019-11-13T00:20:39.815 [   sht3x] DEBUG  CRCs verified: CRC from sensor (0xE1) = calculated CRC (0xE1)
2019-11-13T00:20:39.815 [    main] INFO  Alert pending status = true
2019-11-13T00:20:39.815 [   sht3x] DEBUG  Getting temperature alert status...
2019-11-13T00:20:39.815 [     i2c] DEBUG  Write 2 hex bytes: [f32d]
2019-11-13T00:20:39.816 [     i2c] DEBUG  Read 3 hex bytes: [8010e1]
2019-11-13T00:20:39.816 [   sht3x] DEBUG  CRCs verified: CRC from sensor (0xE1) = calculated CRC (0xE1)
2019-11-13T00:20:39.816 [    main] INFO  Temperature alert pending status = false
2019-11-13T00:20:39.817 [   sht3x] DEBUG  Getting humidity alert status...
2019-11-13T00:20:39.817 [     i2c] DEBUG  Write 2 hex bytes: [f32d]
2019-11-13T00:20:39.818 [     i2c] DEBUG  Read 3 hex bytes: [8010e1]
2019-11-13T00:20:39.818 [   sht3x] DEBUG  CRCs verified: CRC from sensor (0xE1) = calculated CRC (0xE1)
2019-11-13T00:20:39.818 [    main] INFO  Humidity alert pending status = false
2019-11-13T00:20:39.818 [   sht3x] DEBUG  Checking system reset status...
2019-11-13T00:20:39.818 [     i2c] DEBUG  Write 2 hex bytes: [f32d]
2019-11-13T00:20:39.819 [     i2c] DEBUG  Read 3 hex bytes: [8010e1]
2019-11-13T00:20:39.819 [   sht3x] DEBUG  CRCs verified: CRC from sensor (0xE1) = calculated CRC (0xE1)
2019-11-13T00:20:39.819 [    main] INFO  Reset status detected = true
2019-11-13T00:20:39.819 [    main] NOTICE  **********************************************************************************************
2019-11-13T00:20:39.820 [    main] NOTICE  *** Single shot measurement mode
2019-11-13T00:20:39.820 [    main] NOTICE  **********************************************************************************************
2019-11-13T00:20:39.820 [   sht3x] DEBUG  Measuring temperature and humidity...
2019-11-13T00:20:39.820 [     i2c] DEBUG  Write 2 hex bytes: [240b]
2019-11-13T00:20:39.828 [     i2c] DEBUG  Read 6 hex bytes: [000081ffffff]
2019-11-13T00:20:39.828 [   sht3x] DEBUG  CRCs verified: CRC from sensor (0x81) = calculated CRC (0x81)
2019-11-13T00:20:39.829 [    main] FATAL  CRCs doesn't match: CRC from sensor (0xFF) != calculated CRC (0xAC)
exit status 1
d2r2 commented 5 years ago

@vendion, dismiss my previous explanation - there is no issue in Raspberry PI with Linux distro. The source of issue you have not in this library, but in d2r2/go-i2c code, where your sensor interact with embedded device hardware via LINUX SOFTWARE. In case of FREEBSD read and write from i2c bus in d2r2/go-i2c should be organized in other way. I have no answer how to reorganize i2c calls, to make it working on FreeBSD, but perhaps I will find some time to find and fix it.

But currenlty, the answer - FreeBSD i2c interaction is not supported by d2r2/go-i2c library.

vendion commented 5 years ago

There is a bit of confusion going on I would guess.

While my desktop that I am cross compiling on is FreeBSD, I'm cross compiling everything to run on a Onion Omega2+ which a embedded device that runs a modified version of OpenWRT (IIRC I don't have the device with me to check) that runs on a MIPS SoC. So unless there is a CGO dependency that I am missing the host OS being FreeBSD should have little to do with the cross compilation process for a Linux target.

d2r2 commented 5 years ago

@vendion, 1) Can you run i2cdetect -y x on OpenWRT, where x might be 0, 1 or 2, to verify, that you sensor correctly pinned to specific bus and address? 2) Current d2r2/go-i2c implementation has hardcoded linux approach how to activate i2c bus using specific constant I2C_SLAVE and doesn't use CGO. So, in this way it may seem that cross compile produce valid code for MIPS, but I guess that is not true.

Give me a day - I will try to adapt d2r2/go-i2c for cross compilation, as minimum I will try to make it working on freebsd deployed on RPI or clone.

vendion commented 5 years ago

Sure I can provide that information once I get home and have direct access to the device.

On Fri, Nov 15, 2019 at 12:00 PM Denis Dyakov notifications@github.com wrote:

@vendion,

Can you run i2cdetect -y x on OpenWRT, where x might be 0, 1 or 2, to verify, that you sensor correctly pinned to specific bus and address? Current d2r2/go-i2c implementation has hardcoded linux approach how to activate i2c bus using specific constant I2C_SLAVE and doesn't use CGO. So, in this way it may seem that cross compile produce valid code for MIPS, but I guess that is not true.

Give me a day - I will try to adapt d2r2/go-i2c for cross compilation, as minimum I will try to make it working on freebsd deployed on RPI or clone.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or unsubscribe.

vendion commented 5 years ago

Below is information from my Omega device:

root@Omega-36E5:~# uname -a
Linux Omega-36E5 4.4.74 #0 Tue Jul 31 16:32:08 2018 mips GNU/Linux
root@Omega-36E5:~# ls /dev | grep i2c
i2c-0
root@Omega-36E5:~# i2cdetect -y 0
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:          -- -- -- -- -- -- -- -- -- -- -- -- -- 
10: UU -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
40: -- -- -- -- 44 -- -- -- -- 49 -- -- -- -- -- -- 
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
70: -- -- -- -- -- -- -- -- 
root@Omega-36E5:~# ./i2c-test 
2019-11-15T19:23:02.692 [    main] NOTICE  **********************************************************************************************
2019-11-15T19:23:02.692 [    main] NOTICE  *** !!! READ THIS !!!
2019-11-15T19:23:02.693 [    main] NOTICE  *** You can change verbosity of output, by modifying logging level of modules "i2c", "sht3x".
2019-11-15T19:23:02.693 [    main] NOTICE  *** Uncomment/comment corresponding lines with call to ChangePackageLogLevel(...)
2019-11-15T19:23:02.693 [    main] NOTICE  *** !!! READ THIS !!!
2019-11-15T19:23:02.693 [    main] NOTICE  **********************************************************************************************
2019-11-15T19:23:02.693 [   sht3x] DEBUG  Reset sensor...
2019-11-15T19:23:02.694 [     i2c] DEBUG  Write 2 hex bytes: [30a2]
2019-11-15T19:23:02.697 [    main] NOTICE  **********************************************************************************************
2019-11-15T19:23:02.697 [    main] NOTICE  *** Read sensor states
2019-11-15T19:23:02.698 [    main] NOTICE  **********************************************************************************************
2019-11-15T19:23:02.698 [   sht3x] DEBUG  Getting heater status...
2019-11-15T19:23:02.699 [     i2c] DEBUG  Write 2 hex bytes: [f32d]
2019-11-15T19:23:02.700 [     i2c] DEBUG  Read 3 hex bytes: [8010ff]
2019-11-15T19:23:02.701 [    main] FATAL  CRCs doesn't match: CRC from sensor (0xFF) != calculated CRC (0xE1)

The device i2c device at 0x44 is my sht31 sensor and the device at 0x49 is an ADC connector that I have connected to my Omega to allow my Omega to talk to the sht31 through the GPIO pins since my sensor uses the Grove connector.

d2r2 commented 5 years ago

@vendion,

Thanks for more output from Omega device.

1) Can you do one check? Find I2C_SLAVE constant definition in file "/usr/include/linux/i2c-dev.h" on your Omega device. Is't correspond to one below?:

#define I2C_SLAVE   0x0703  /* Use this slave address */

2) Just to clarify: you running golang code directly compiling it on Omega, or use solely cross compile technique from your FreeBSD desktop?

BTW, my attempt to make FreeBSD completely installed and configured on RPI close to failure. I spent hours, have installed golang, configured everything, except i2c block device (in case of FreeBSD it's /dev/iic0). So, trying to run i2cdetect counterpart on FreeBSD i2c -s -v, gives expected error:

freebsd@generic:/dev# i2c -s -v
dev: /dev/iic0, addr: 0x0, r/w: r, offset: 0x00, width: 8, count: 1
Error opening I2C controller (/dev/iic0) for scanning: No such file or directory

And no real help on the internet how to activate it for Raspberry PI 2 model B. Oh, I do not understand people who have not yet switched to Linux completely :(

d2r2 commented 5 years ago

@vendion, there is another thought. This is what I found in Onion C code:

// set the device address
int _i2c_setDevice(int devHandle, int addr)
{
#ifdef I2C_ENABLED
    // set to 7-bit addr
    if ( ioctl(devHandle, I2C_TENBIT, 0) < 0 ) {
        return EXIT_FAILURE;
    }

    // set the address
    if ( ioctl(devHandle, I2C_SLAVE, addr) < 0 ) {
        return EXIT_FAILURE;
    }
#endif

    return EXIT_SUCCESS;
}

We have similar code in d2r2/go-i2c, which only reproduce second call (ordinary there is no need to switch to 7-bit mode, since it goes by default):

func NewI2C(addr uint8, bus int) (*I2C, error) {
    ...
    if err := ioctl(f.Fd(), I2C_SLAVE, uintptr(addr)); err != nil {
        return nil, err
    }
    ...
}

So, idea is to try to add first call - may be this fix the issue. In this case updated NewI2C would looks like:

func NewI2C(addr uint8, bus int) (*I2C, error) {
    f, err := os.OpenFile(fmt.Sprintf("/dev/i2c-%d", bus), os.O_RDWR, 0600)
    if err != nil {
        return nil, err
    }
        const I2C_TENBIT=0x0704
    if err := ioctl(f.Fd(), I2C_TENBIT, uintptr(0)); err != nil {
        return nil, err
    }
    if err := ioctl(f.Fd(), I2C_SLAVE, uintptr(addr)); err != nil {
        return nil, err
    }
    v := &I2C{rc: f, bus: bus, addr: addr}
    return v, nil
}

Can you made this test?