Reading from analog GPIOs

Discussion in 'General Discussion' started by ImIronman, Apr 1, 2014.

  1. ImIronman

    ImIronman New Member

    Joined:
    Mar 31, 2014
    Messages:
    8
    Likes Received:
    0
    Hello,

    I'm a newbie at this tech and Linux, with some decent experience on working with the Arduino and in C Programming.

    I wanna obtain an analog signal in real-time from the GPIOs with ADC at, say- 10 KHz sample rate, and store it in the form of an array/vector. I am wondering what kind of programming tools are suitable for this task. I also would like to know if the CodeBlocks IDE or PureData will of any help!

    PS: I wanna do this on the iMX6 processor, and not using the Arduino/SAM3X processor.

    Thank you!
     
  2. peter247

    peter247 New Member

    Joined:
    Mar 10, 2014
    Messages:
    263
    Likes Received:
    2
    It may be a stupid question , but does the iMX6 processor have any ADC channels ?
    I guess that's the first thing to do you get access to them at any speed .
    or use a spi adc , I think the mcp32008 ?.
     
  3. mkopack

    mkopack Member

    Joined:
    Jun 14, 2013
    Messages:
    451
    Likes Received:
    21
    Yeah, going on what's in the "Getting Started" manual, there is no ADC functionality provided through GPIOs to the iMX6 chip. The SAM3 provides ADC internally in it's chip, which is why the Analog in pins work. They are not usable as Analog in by the iMX6 though. (See the diagram on page 29 of the manual found in the "Downloads" section, "Documentation" tab...)

    IF the SAM3 can do the sampling that fast, you could do the reading on that side, and pump the data across to the iMX6 via serial or some other interface internally. Otherwise, you'll need to use an external ADC chip and pump the digital value into the GPIO pins that the iMX6 can read.

    Good luck! Let us know how you make out.
     
  4. ImIronman

    ImIronman New Member

    Joined:
    Mar 31, 2014
    Messages:
    8
    Likes Received:
    0
    Thank you guys! I too checked out the iMX6 datasheet and figured it out!

    But my question is, how do I program the iMX6 processor to do it, i.e, recieve the serial input from SAM3X and store it in an array?? Like, if I write a C program to do this task, how do I compile it and dump the code into the processor? I know this might sound trivial, but sorry, am new to this stuff!

    Thank you!
     
  5. mkopack

    mkopack Member

    Joined:
    Jun 14, 2013
    Messages:
    451
    Likes Received:
    21
    Ah, I see what you're saying...

    This isn't like Arduino programming where you "dump the compiled code into the processor" - you run your program from the Linux command line, just like any other Linux program.

    Now, I haven't done the reading from the serial part (others here, possibly in the programming section should be able to help you).

    Basically, you'll need code loaded onto the SAM (standard Arduino style) that reads the values and writes them out to the serial interface. Then, you'll write a little C/C++ program for the iMX6 that reads from the Serial interface that is tied to the SAM3X and stores that or does whatever you want to do with that data. You didn't really say what you wanted to do with the data other than "store it in a vector/array" . I assume you want to do SOMETHING with that data... Are you wanting to do some sort of processing of it in real time? Or could you just dump that data to a file (CSV?) and use it afterwards?
     
  6. peter247

    peter247 New Member

    Joined:
    Mar 10, 2014
    Messages:
    263
    Likes Received:
    2
    I know how you would do that in python , but not in c++.
    But working on your example, is serial fast enough for 10 k , 16 bit samples per second ?.
    I guess the way to do it is , start slow and speed up !!!!.
     
  7. mkopack

    mkopack Member

    Joined:
    Jun 14, 2013
    Messages:
    451
    Likes Received:
    21
    Yeah, that's the concern I had as well. That's a LOT of data. But since you're talking 10000 16 bit ints a second, that's a minimum of 160kbps (don't forget handshaking and error correction overhead).

    Doing it in C++ isn't really all THAT hard, if you use a library like Boost to provide you easy access to the serial interface. But yeah, if possible I'd use Python which would be way simpler.

    Again, really depends on what you are trying to do with the data. If you don't need to process it in real time on the MX6 side, then you might be able to do some sort of data compression between the SAM3 and the iMX6 chip so there's less going across the serial. It just depends on what you're trying to achieve. More details would be helpful in us providing advice.
     
  8. ImIronman

    ImIronman New Member

    Joined:
    Mar 31, 2014
    Messages:
    8
    Likes Received:
    0
    Thanks again guys! And yes, I am working on a real-time-processing project in which I need to store data from the ADCs in vectors, and then execute some processing code on it.

    Now, I don't know python, so C/C++ is my only option for programming right now :| And is there any example code/project on Using boost library on UDOO?

    Also, is it possible to accomplish my task if I program and execute the code in the preinstalled Codeblocks IDE?
     
  9. mkopack

    mkopack Member

    Joined:
    Jun 14, 2013
    Messages:
    451
    Likes Received:
    21
    Never heard of codeblocks, sorry...

    I see from google that it's an IDE... Sorry, I'm mostly a Java guy so I'm more familiar with Netbeans or Eclipse.

    Look down in the "Programming" section. There's a few discussion in there on how to do interfacing between the two over serial.
     
  10. ImIronman

    ImIronman New Member

    Joined:
    Mar 31, 2014
    Messages:
    8
    Likes Received:
    0
    Oh its ok @mkopack! :) I actually did post the same query in the Programming section, but didn't get any response. This place seemed more active, so reposted over here. Anyways, thank you for your help !
     
  11. francescomm

    francescomm Member

    Joined:
    Dec 14, 2013
    Messages:
    80
    Likes Received:
    4
    I have been using the internal serial port at 115200, but I don't know if it can run faster, although I have seen code for Arduino specifying 230400.
    If all you need is getting data from Arduino to linux, I would minimize handshaking, error correction and the such, just print out all the values you get from the sensors to the serial, in a loop as fast as you can. Just get the linux side to read them fast enough. The sensor values you get from ADC on Arduino are 10 bit values, from 0 to 1024, so you may get 10000x10 bits / sec, so 100Kbps. Of course you must avoid sending them as 16 bits with 6 bits unused. Put 3x10bit values in a 32bit block (the remaining two bits may be used for control) and send it as 4 bytes, then decode at the other side.

    Just as an example, untested, if A,B and C are 10 bit sensor values:

    Code:
    long sendVal=(long)A<<20 & (long)B<<10 & (long)C; // move bits around
    
    char *asChars=(char*) &sendVal; // see the generated long as array of chars
    
    Serial.write(sendVal[0]); // send out the 4 chars
    Serial.write(sendVal[1]);
    Serial.write(sendVal[2]);
    Serial.write(sendVal[3]);
    
    Should send 4 bytes for 3 sensor values of 10 bit each (if I got it right).


    On the other side you collect 4 chars/bytes, build a long out of them, and then shift it to the right with >>10 a few times and & it with 1111111111 (10 bits, 1023) each time and you should (if I got it right..) get your values back. Of course you must keep Arduino and linux in phase (must know the first char is the first, etc..). If you loose one char in the middle, it will all get jammed.

    Just an idea...

    Have fun!
     
  12. mkopack

    mkopack Member

    Joined:
    Jun 14, 2013
    Messages:
    451
    Likes Received:
    21
    Nice approach!
     

Share This Page