简体   繁体   中英

Raspberry Pi: issue communicating via I2C

I'm trying to create a simple project with I2C interface. For that I've create a sketch in Arduino that always sends single byte:

#include <Wire.h>

void setup() {
  Wire.begin(8);
  Wire.onRequest(requestEvent);
}

void loop() {
  delay(100);
}

void requestEvent() {
  Wire.write(0x11);
}

On Raspberry Pi there's a Python script:

#!/usr/bin/env python3

import smbus
import time

bus = smbus.SMBus(1)

while True:
    try:
        data = bus.read_byte_data(0x8, 0)
        print(data)
    except Exception as e:
        print(e)
    time.sleep(1)

And here's its output:

17
17
17
17
17
17
17
[Errno 5] Input/output error
[Errno 5] Input/output error
[Errno 5] Input/output error
[Errno 5] Input/output error
[Errno 5] Input/output error
[Errno 5] Input/output error
[Errno 5] Input/output error
[Errno 5] Input/output error
[Errno 5] Input/output error
[Errno 5] Input/output error
17
17
17
17
17
[Errno 5] Input/output error
[Errno 5] Input/output error
17
17

What I'm trying to figure out is why at some random points in time I2C returns error instead of returning data? No hardware changes, nothing else is running on RPi, literally nothing is changing, but I2C stops working.

Any ideas?

Have you solved the problem?

I had the same problem also when configuring Raspberry as a master. I think they are due to the missing of synchronization between writing and reading operations. Ie it my happen that reading can be attempted when writing is being done. Unfortunately raspberry does always something also if you think the contrary. this is because raspberry is a multithread platform and only one bus is available(at my knowledge).

I solved the problem adding a synchronization between raspberry and picaxe by using a GPIO pin on raspberry and one on picaxe. In this way reading (as well as writing) my happen only when there is an ok signal from the other system.

I hope this delay can be useful also with such a delay.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM