2

I am attempting to increase the frequency at which I read samples from the I2C dedicated pins. I have attempted this through the wiringPi library (C code), from which I reached just about 900Hz. As well as with python, I was able to reach roughly the same figures. I did some research and was lead to multi-byte reads via ioctl and read(), write() functions which as far as I understand are more low level, direct access of the I2C bus as opposed to the wiringPi library and any other python library I could've used prior. Though after some code testing, I am stagnating at just about 1.1KHz for a 14 byte read (9 seconds for 10K samples). I am unsure what I am missing here or how I can improve this as I would like to hit around 2KHz.

Reason being is I would like to thread in between and around the I2C communication. Also if anyone has any pointers as how this should be approached that would be appreciated.

Code for reading data, file printing adds 2 seconds, without any of the commented out code the time for 10000 samples is roughly 7 seconds.

while(counter<10000){
    buf[0] = 0x3B;
    write(fd,buf,1);
    read(fd,buf,14);

    // if ((write(fd, buf, 1)) != 1){
    // // Send the register to read from
    // fprintf(stderr, "Error writing to MPU6050\n");
    // }

    // if (read(fd, buf, 14) != 14){
    // fprintf(stderr, "Error reading from MPU6050\n");
    // }else{}

    // for(int i=0;i<14;i++){
    //   fprintf(new_file,"%.0f,",(float)buf[i]);
    // }
    // fprintf(new_file, "%s\n","");
    counter++;
  }
theeddible
  • 23
  • 3
  • Ah, let me see. Rpi3B+ can only do 100kHz flat. Rpi4B buster 2020feb can do up to at least 1MHz. If you are using Rpi4 buster, I can show you my experiment results. – tlfong01 Mar 25 '20 at 02:09
  • 1
    @tlfong01 I am using the pi zero, with buster lite on a 400KHz clock. – theeddible Mar 25 '20 at 02:17
  • 1
    Hi @theeddible, thank you for your testing config. My sad I2C experience was only with Rpi3B+, so I am not sure if RpiZero I2C can go faster than 100kHz. The web link below is my Rpi4B I2C 1MHz experiment results, with a full listing of /boot/config.txt and python test program. The python program is a complete copy-paste-and-run program. You just run it on RpiZ and if no luck, try Rpi4B to prove that RpiZ can't do. Good luck and cheers.. Reference: (1) "Max i2c speed of Rpi4?": https://raspberrypi.stackexchange.com/questions/108896/what-is-the-max-i2c-speed-of-the-raspberry-pi-4. – tlfong01 Mar 25 '20 at 02:41

1 Answers1

2

I can see no way to speed up the code. You are already doing the minimum number of transactions: one to select the register, one to read the data.

To transfer 15 bytes of data (1 write, 14 read) would take 153 (17 bytes + ack bits) bits.

That gives a maximum of 400000/153 or 2614 readings per second. In practice you have found the limit.

You could try to increase the bus speed beyond 400 kbps but I reckon any improvement will be marginal.

joan
  • 71,024
  • 5
  • 73
  • 106
  • Thanks @joan. I thought this was this was the issue but was hoping for some work room. Anyways, one last question if you could answer please. I would like to drop a second device on the pi and currently I am paralleling it with my sensor, which the question is for above. I can only see my sampling rate decrease in this case, so could I bit-bang two separate pins as I2C and increase my bandwidth that way in order to have smooth communication between the devices. The second device doesn't need to be fast, it just needs to not impede the performance of my sensor. – theeddible Mar 25 '20 at 15:45
  • By the way @joan, I am sure you know more than me on this subject and was hoping you could explain some things. I read my signal on a scope and the way it looks is first there's the write (2 bytes) then a small delay and finally a read (15 bytes), I took the time of just the read (15 bytes) and it is roughly 55ms, which would mean 135 bits in this time, equating to roughly a 250KHz clock pulsing. Which I quite don't understand, why is my clock running slow. – theeddible Mar 25 '20 at 16:13
  • You can add one or more software I2C buses (the limit is the number of spare GPIO). See /boot/overlays/README for details. That will not interfere with the hardware bus. The I2C bus speed may depend on the core clock speed (it may go low when the system isn't busy). Search for setting the core clock speed and you should find related info. – joan Mar 25 '20 at 18:17