Shop OBEX P1 Docs P2 Docs Learn Events
Trying to capture data from an I2C bus, semi-random results! — Parallax Forums

Trying to capture data from an I2C bus, semi-random results!

ganzuulganzuul Posts: 8
edited 2014-10-01 07:04 in Learn with BlocklyProp
This program is supposed to listen to two pins, driven by an 24LC64 EEPROM which belongs to a cheap USB oscilloscope. Each time the Propeller sees that pin 1 goes from low to high it reads pin 0 into an unsigned int and shifts it left. When it has done this 32 times it iterates in an array and fills up another int. I should be able to capture 8KiB this way.

This is my first uC project. Things have been going reasonably well and I seem to have gotten the hang of things, but I ran into a conundrum. If I set snort[] to longer than 26, the program fails to return data. While printing the same values to serial I got a lot more data than just 26 values and the program runs for about a second before data stops. - Have I run into some memory limit?

The data I get is random! e.g.
80180300
828b450
f822472a
1f15ba00
2e4c70b8
caa00482
9121100
489c5090
4000653a
f92226e
202839e8
234684a4
9f8d00
20c0200
148242
c1000000
4000008
25100
224ab70a
78e60120
43009040
5851996
106e40c0
41023f8e
5990ba02
2caa1570
0
When printing to serial I several times got the impression that 'if (b == 32)' misses, or triggers prematurely, or something. Electrically everything seems fine, and I only get data transmission while the I2C clock on pin 1 is running.

The program:
/*
  snorph.c

  Sniff I2C data from an external device.

*/

#include "simpletools.h"

int pin0,pin1;
int a,b = 0;
int max_mem = 26;
unsigned int last_state = 1;
unsigned int snort[26];

int main()
{
  while (a <= max_mem ){

    pin0 = input(0);
    pin1 = input(1);

    if (last_state < pin1){                   //Read on clock rise
      snort[a] = snort[a] | pin0;
      snort[a] = snort[a] << 1;
      b++;
    }
    last_state = pin1;

    if (b == 32){
      a++;
      b = 0;
    }
  }
                                // Done collecting data, now print
  a = 0;
  while (a <= max_mem){
    print("%x\n",snort[a]);
    a++;
  }
}

I've been working on this all night and my brain is fried. Any and all tips are welcome. =)

Comments

  • SRLMSRLM Posts: 5,045
    edited 2014-09-30 20:49
    Don't you want to define snort with a size ( [ 5 ] instead of [ ] )?

    Also, you should left shift << before the or |. The way you have it now all bits are one too far left, and bit 32 is lost.
  • ganzuulganzuul Posts: 8
    edited 2014-09-30 21:08
    I probably do! Thanks.

    I can make it [26] now before the same issue comes up and the 2nd and 3rd entries are not stuck at length 1, but the data I collect is still random...

    Updating the code to reflect changes...
  • SRLMSRLM Posts: 5,045
    edited 2014-10-01 05:32
    Well, besides the shift error that I mentioned in my last post you also have an off by 1 error in your loop. a <= max_mem should be a < max_mem. But that won't affect your results, generally.

    The output that you posted looks reasonable to me, but how do you know what you're expecting? If it's some internal data on the EEPROM of your USB oscilloscope then it can be all sorts of values, which will look random. If you're lucky you'll catch a device address, but that's not guaranteed. Also remember that you're reading 9 bit values.

    My suggestion would be to use another pair of Propeller pins and a cog to send out a specific pattern that you can use as a test input to your program.
  • ganzuulganzuul Posts: 8
    edited 2014-10-01 07:04
    I must have been really tired yesterday. I missed both of those. Thanks again! =)

    What I meant by random is that on one capture I get the first column, and on the second I get the next:

    48b80080 2c5c0000
    2d442114 94a2008a
    898b0c80 44c58640
    45224641 22902320
    53a1158f 29d08ac7
    6c82b21c 3641590e
    28a070 145038
    86a041c1 435020e0
    14548f06 a6a4781
    12941c93 94a0e49
    4548f219 a6a4790c
    a2a3c26c 5151e136
    a90f4b26 94878593
    541c2802 2a0e1401
    8f02581 c47812c0
    c1c192bb 60e0495d
    7038011 8381c009
    1c1af424 e057a16
    7164e868 38927424
    d34e163b e9a70b19
    1027050 80a13828
    3520e0a0 12907050
    29e868a3 36f42451
    d00ea007 60075003
    3495e81c ba4bf40e
    e5a6f0f0 f2d37878


    I can now see that there are patterns... As if some values are rotated while others are not. This might be an electrical issue after all. :innocent:

    I also got it to capture 10x more data now by cycling the power twice on the o-scope! VERY glad this was just a glitch. I'll definitely run up another cog to simulate the data I'm trying to capture. =)


    ED:
    This is the code I ended up with.
    /*
      snorph.c
    */
    
    #include "simpletools.h"                      
     
    unsigned int mask = 0x1;
    unsigned int pin0,a,b = 0;
    unsigned int c = 0xAABBCCDD;
    unsigned int max_mem = 50;
    unsigned int snort[50];
    void gen();
    
    int main()                                    
    { 
    //  int *cog = cog_run(&gen, 10);    // Uncomment to start simulation using pins 8 and 9
      while (a < max_mem ){
        waitpne((1 << mask), (1 << mask));
        waitpeq((1 << mask), (1 << mask));
        pin0 = input(0);
        snort[a] = snort[a] << 1;
        snort[a] = snort[a] | pin0;
        b++;
        
        if (b == 32){
          a++;
          b = 0;
        }    
      } 
      // Done collecting data, now print
      a = 0;
      while (a < max_mem){
        print("%x\n",snort[a]);
        a++;
      }
    }
    
    /*********
     GENERATOR
     *********/
    void gen(){
      while(1){
            
        if(c & 0x1){
          high(8);      
        }      
        else {
          low(8);
        }   
        c = (c << 1)|(c >> (32 - 1));    
        
        low(9);
        //pause(1);  //waitpxx() works with pause() commented out!
        high(9);
        //pause(1);
     }    
    }  
    
Sign In or Register to comment.