簡體   English   中英

樹莓派 I2C

[英]Raspberry Pi I2C

我有多個使用 I2C 來回通話的 arduino。 主機寫兩個字節,然后讀回 1 個字節的響應。 一切都很順利。 但現在我一直在努力將我的主人切換到 Raspberry Pi。 我編寫的代碼沒有問題,但每 200 次讀/寫中就有 1 次,它偶爾會返回錯誤讀取,這將對系統的可靠性造成巨大影響。 我附上了我的代碼,以防萬一有人看到我做錯了什么,或者其他人以前遇到過類似的問題。

RPi C++ 代碼:

#include <iostream>
#include <fstream>
#include <unistd.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <linux/i2c.h>
#include <linux/i2c-dev.h>
#include "../HaMiD_Lib/StopWatch.h"

using namespace std;

int file_i2c;
int length;
uint8_t buffer[2] = {0};

int timingLoopFreq = 500;       
int timingLoopMicroSeconds = 1000000 / timingLoopFreq;      //500 us
StopWatch loopTime(timingLoopMicroSeconds);                 // My own stopwatch livrary
uint8_t addr = 0x11;

using namespace std;

int main(void)
{
         //-------------- OPEN THE I2C BUS--------------------------
        char *filename = (char*)"/dev/i2c-1";
        if((file_i2c = open(filename, O_RDWR))< 0 ){
                //ERROR HANDLING: you can check errno to see what went wrong;
                cout << "Failed to open the i2c bus" << endl;
                return 0;
        }

        while(1){
                if (loopTime.check()) {
                        loopTime.reset();

                        if (ioctl(file_i2c, I2C_SLAVE, addr) < 0){
                                cout << "Failed to acquire bus access and/or talk to slave" << endl;
                                //ERROR HANDLING: you can check errno to see what went wrong;
                        }

                        // ------------- WRITE BYTES -------------
                        buffer[0] = 4;
                        buffer[1] = 0;
                        length = 2;                 //<<<<< Number of bytes to write
                        if (write(file_i2c, buffer, length) != length){                    // write() returns the number of bytes actually written, if it doesn't match then an error occurred (e.g. no response from the device)
                        // ERROR HANDLING: i2c transaction failed
                                cout << "Failed to write to the i2c bus " << endl;
                        } else {
                                    // ------------ READ BYTES -------
                                    length = 1;
                                    if (read(file_i2c, buffer, length) != length){              // read() returns the number of bytes actually read, if it doesn't match then an error occurred (e.g. no response from the device)
                                        //ERROR HANDLING: i2c transaction failed
                                        cout <<"Failed to read from the i2c bus" << endl;
                                    } else {
                                            cout << "Data read:" << buffer[0] << endl;
                                }
                        }
                }
        }
        cout << "exiting" << endl;
        return 0;
}

Arduino I2C 代碼段:

//I2C functions
void receiveEvent(int byteCount) {
    while (Wire.available()) {
        I2C_cmd_1st = Wire.read(); // 1 byte (maximum 256 commands)
        I2C_cmd_2nd = Wire.read(); // 1 byte (maximum 256 commands)
    }
}

void slavesRespond() {
    byte returnValue = 0;

    switch (I2C_cmd_1st) {
    case status_cmd:   // 40
        returnValue = module;
        if (module == DONE) {
            module = STOP;                  //reset the machine
        }
        break;
    case test_cmd:
        returnValue = ID;
        break;
    }
    Wire.write(returnValue);          // return response to last command
}

這是 cout 輸出的一小部分。 讀取的數據應始終返回 2,但有時(100 分之一)無法從 i2c 總線讀取或寫入,有時(500 分之一)返回錯誤值(在下面的示例中,它應返回 2,但有時返回 3偶然或可能是 97 等)。

Data read:2
Data read:3    //This is wrong!
Data read:2
Failed to read from the i2c bus
Failed to read from the i2c bus
Data read:3    //This is wrong!
Data read:2

任何幫助將不勝感激。 有沒有其他人看到過與 RPi 和 Arduino 類似的東西? 接線非常簡單,因為 RPi 是主控。

您是否嘗試在接收事件中僅將我的數據作為實際可用的數據讀取。 代碼考慮到了byteCount中的receiveEvent可以被 2 整除。

對於無法讀取 i2c 總線問題,請嘗試

dtoverlay=i2c-gpio,i2c_gpio_sda=2,i2c_gpio_scl=3,i2c_gpio_delay_us=2,bus=1

插入的

dtparam=i2c_arm=on

在 /boot/config.txt

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM