Shop OBEX P1 Docs P2 Docs Learn Events
C3 ADC PropGCC driver code — Parallax Forums

C3 ADC PropGCC driver code

I started messing around with the C3 again. I would like to use the ADC component on the board, but I have no idea as to where to start with C code for the driver. Anybody have a C driver already written, or could give me a good starting point for doing the driver?

I would like to stay away from doing a conversion, would like to do a scratch build, I guess a lot of hand holding on this one.

Thanks

Ray

Comments

  • Ray
    Not C, but Spin (maybe a starting point?)

    Not sure if you have this link, but here's the link to the FTP site for both the C3 and the Programming and Customizing the Propeller Book.

    "ftp://ftp.propeller-chip.com/"]ftp://ftp.propeller-chip.com/

    Follow the link; then go to the C3 directory and the Sources directory. There are a few analog to digital demos. In the docs folder there is a data sheet for an MCP3202, so I assume that is the ADC chip on the C3??

    I did write a C program that loads the PASM from the SPI object in the Prop Tool Library and C code that uses the MCP 3208. The library is in this post:
    https://forums.parallax.com/discussion/comment/1315065/#Comment_1315065

    I haven't tried on a C3 so I'm not sure using an SPI interface with the C3 is as direct as it is with the Activity Board or PPDB.

    Tom
  • Rsadeika wrote: »
    I started messing around with the C3 again. I would like to use the ADC component on the board, but I have no idea as to where to start with C code for the driver. Anybody have a C driver already written, or could give me a good starting point for doing the driver?

    I would like to stay away from doing a conversion, would like to do a scratch build, I guess a lot of hand holding on this one.

    Thanks

    Ray
    Sorry, I never know around here when people say "C code" whether or not that includes C++. Many people do include C++ in "C code." That being said, PropWare has a C++ MCP3xxx object that supports the MCP3202. See the docs here, source here, and an example here.
  • @DavidZemon, I must admit that I have not used PropWare, but I have looked at it. I like the simplicity of using SimpleIDE, but that does not mean that your software is in any way inferior, I have to applaud you in the excellent job that you have been doing in supporting the C/C++ initiative for the Propeller.

    Since there is a driver, written in C++, for the ADC being used on the C3, I am considering redoing my existing code, mainly in C format so I can incorporate the C++ ADC driver. Not sure what this would entail.

    As an experiment, for my existing C program, I changed the compiler type to C++, and tried too compile the program. It came up with an error for the cogrun() function and a numerous amount of warnings. My first guess would be that simpletools.h is lacking something that would allow it to be run with the C++ compiler, without warnings.

    I will be investigating what I can use, that PropWare offers, within a SimpleIDE environment. It would be nice if the libraries were interchangeable between the compiler types, maybe they are and I am just doing something incorrectly. Not sure if I want to go 100% C++, at this time.

    Ray
  • I am starting with a very simple program that is now designated as .cpp, to see what the initial problems might be. If I can get a run without any errors or warnings, then I might add the C++ ADC driver and see how it can be used.

    Below the program listing is the build status for the program, the error has me scratching my head, not sure what that means.

    Ray

    /**
    * test1.cpp
    *
    * December 14, 2016
    * 
    */
    #include "simpletools.h"
    #include <stdio.h>            // Recommended over iostream for saving space
    #include <propeller.h>        // Propeller-specific functions
    
    void irrem();
    
    
    int main(){
      // Add your code here
      cog_run(irrem,128);
      
      while(1)
      {
        pause(400);
      }    
    
      return 0;
    }
    
    void irrem()
    {
      while(1)
      {
        pause(500);
      }    
    }  
    
    Project Directory: E:/programs/simpleide/C++/test1/

    SimpleIDE Version 1.0.2
    E:/programs/simpleide/SimpleIDE/Learn/Simple Libraries/
    E:/programs/simpleide/SimpleIDE/ Updated on: 2016-09-01

    propeller-elf-gcc.exe -v GCC 4.6.1 (propellergcc_v1_0_0_2408)
    propeller-elf-c++ -I . -L . -I ./../../SimpleIDE/Learn/Simple Libraries/Utility/libsimpletools -L ./../../SimpleIDE/Learn/Simple Libraries/Utility/libsimpletools/cmm/ -I E:/programs/simpleide/SimpleIDE/Learn/Simple Libraries/TextDevices/libsimpletext -L E:/programs/simpleide/SimpleIDE/Learn/Simple Libraries/TextDevices/libsimpletext/cmm/ -I E:/programs/simpleide/SimpleIDE/Learn/Simple Libraries/Protocol/libsimplei2c -L E:/programs/simpleide/SimpleIDE/Learn/Simple Libraries/Protocol/libsimplei2c/cmm/ -o cmm/test1.elf -Os -mcmm -m32bit-doubles -fno-exceptions -fno-rtti test1.cpp -ltiny -lsimpletools -lsimpletext -lsimplei2c -ltiny -ltiny -lsimpletools -lsimpletext -lsimplei2c -ltiny -lsimpletools -lsimpletext -ltiny -lsimpletools -ltiny
    test1.cpp: In function 'int main()':
    test1.cpp:14:20: error: invalid conversion from 'void (*)()' to 'void (*)(void*)' [-fpermissive]
    ./../../SimpleIDE/Learn/Simple Libraries/Utility/libsimpletools/simpletools.h:1404:6: error: initializing argument 1 of 'int* cog_run(void (*)(void*), int)' [-fpermissive]
    Done. Build Failed!

    Click error or warning messages above to debug.
  • The program below now runs without any errors or warnings. This is a test run to see what it takes to start/run a COG.

    I tested this using a QuickStart Board+HIB, it has the built in IR stuff. So far it looks like intermixing the simpletools.h within a .cpp program has not caused any difficulties yet. I am at a complete loss as to how I should add the C++ ADC driver code, and then be able to use the function calls.

    I guess maybe at this point I should go ahead and try to see if I can install the latest PropGCC version, so I can maybe try and see how this stuff will run in XMMC mode.

    Ray
    /**
    * test1.cpp
    *
    * December 14, 2016
    * 
    */
    #include "simpletools.h"
    #include <stdio.h>            // Recommended over iostream for saving space
    #include <stdlib.h>
    #include <propeller.h>        // Propeller-specific functions
    #include "sirc.h"
    
    void irrem(void *arg);
    
    
    int main(){
      // Add your code here
    /* Code for starting a new COG.*/
      int pin = 8;  // IR on the QuickStart Board.
      int stacksize = sizeof(_thread_state_t)+sizeof(int)*25;
      int *stack = (int*) malloc(stacksize);
      int cog;
    
      cog = cogstart(irrem, (void*) pin , stack, stacksize);
    /*********************************************************/
      //cog_run(irrem,128);
      
      while(1)
      {
        pause(400);
      }    
    
      return 0;
    }
    
    /* Test run for the SIRC code.*/
    void irrem(void *arg)
    {
      int pin = (int) arg;
      while(1)
      {
        int button = sirc_button(pin);
        switch(button)
        {
          case 16:     // Ch+
            high(19);
            break;
          case 17:     // Ch-
            low(19);
            break;
        }      
      }    
    }  
    
  • I went ahead and updated to propellergcc-alpha_v1_9_0-gcc4-win32, and compiled the program below. Using the CMM setting, the program works as expected. The problem now is that simpletext.h, fdserial.h, and sirc.h are CMM libs, and not XMM libs, will not run in XMM setting.

    Not sure what the easiest way would be to correct this. I am thinking that I would need to find the library source, and then have it compiled in XMM mode? I tried to find the simpletools library source code, but I had no luck. You would think that there would be some compiled libraries for all the memory models residing somewhere, but I am probably wrong again.

    Now I am thinking that the available C3 C++ ADC code will not work with the XMM setting. This all seems to be very complicated if you have to compile the libraries, for the specific memory model first, before you can use it, and then deal with the multiple copies of the same library in different memory forms. This could be a very large nightmare.

    Ray
    /**
    * cr_c3.cpp
    *
    * December 14, 2016
    */
    #include "simpletools.h"
    #include <stdio.h>            // Recommended over iostream for saving space
    #include <propeller.h>        // Propeller-specific functions
    #include "simpletext.h"
    #include "fdserial.h"
    #include "sirc.h"
    
    serial *create;
    
    void irrem(void *arg);
    
    void CRsafe();
    void CRstart();
    
    
    int main(){
      // Add your code here
      /* Code for starting a new COG.*/
      int pin = 0;  // IR on the C3 Board.
      int stacksize = sizeof(_thread_state_t)+sizeof(int)*25;
      int *stack = (int*) malloc(stacksize);
      int cog;
    
      cog = cogstart(irrem, (void*) pin , stack, stacksize);
    /*********************************************************/
      create = fdserial_open(1, 2, 0, 115200);
    
      while(1)
      {
      }    
    
      return 0;
    }
    
    /* Test run for the SIRC code.*/
    void irrem(void *arg)
    {
      int pin = (int) arg;
      while(1)
      {
        int button = sirc_button(pin);
        switch(button)
        {
    //      case 16:     // Ch+
    //        high(19);
    //        break;
    //      case 17:     // Ch-
    //        low(19);
    //        break;
          case 27:    // << 
            CRsafe();
            break;
          case 26:    // > 
            CRstart();
            break;
    
        }      
      }    
    }  
    
    void CRsafe()
    {
      fdserial_txChar(create,128);
      pause(400);
      fdserial_txChar(create,132);  
    }
    
    void CRstart()
    {
      fdserial_txChar(create,128);
    }        
    
  • Ray, the C3 does not have the ADC just connected to SPI pins - there are several devices (Flash, SRAM,ADC, etc) connected to one SPI bus but with each device needing to have it's CS line pulled active low by a SPI selection decoder. The scheme sounds a whole lot more complex than it is but the C3 ADC will definitely need to be enabled by that SPI select mechanism in order to be used. Some very well commented SPIN code is found at ftp://ftp.propeller-chip.com/PropC3/Sources/c3_analog_digital_demo_sterm_010.spin.

    ftp://ftp.propeller-chip.com/PropC3/ is the top level documentation and sample code store I've been using successfully for a while. Hope this helps!

    -Mike
  • DavidZemonDavidZemon Posts: 2,973
    edited 2016-12-15 03:56
    Hi ray,

    Glad you're interested in trying it. I just confirmed that PropWare's MCP3xxx object, and indeed the entire MCP3xxx_Demo file (which includes the serial routines), works in xmmc mode on a Quickstart. Pre-compiled versions of PropWare can be used from within SimpleIDE by following the instructions at the top of this page: http://david.zemon.name/PropWare/#/download

    When you download PropWare, it also ships with all of the Simple libraries, including xmmc-compiled versions. However, so long as you're building from SimpleIDE, that should not be necessary. I don't have SimpleIDE installed on this machine at the moment, so I can't confirm, but I'm pretty sure SimpleIDE is supposed to automatically compile and link in any of the Simple libraries that are required by your application.

    Now, that all being said, I was able to get your code to work on my Quickstart board after a one change. I increased your stack size significantly, to this:
    int stacksize = EXTRA_STACK_LONGS+sizeof(int)*256;
    

    "EXTRA_STACK_LONGS" is a macro defined on line 120 of propeller.h, and it resolves to a much larger value than what you have.

    As for "compiler type" you want to leave that set to C, but make sure your files are named ".cpp". This will invoke gcc ("C++" compiler type invokes g++) and gcc will compile as C++ based on the file extension. Compiling with g++ just brings in automatic linkage of the C++ libraries, which you won't need. The link above, about downloading PropWare, explain all the right settings in SimpleIDE to get you going.
  • Because most of the I/O pins are used for the many onboard devices, the C3 has a special way of activating the SPI chip select (active low). Instead of simply connecting one CS line from a pin to a device it uses 2 pins feeding into a counter and decoder to allow 2 pins to control 16 SPI devices. It is explained in the book "Unleashing the Propeller C3", product number 32209.

    You can find that pdf by entering "C3" in the searchbox on the product page.

    There is a lot of detail and examples in the book, but a short explanation from the book is:
    "One signal would be used to clock a 4-bit counter (U11; 74LVC161A), the output of the counter would be fed into a 3-8 bit decoder (U7; 74LVC138A) then as the count increased the decoder would enable one of n outputs (active low). These outputs would then be directly connected to the active low chip select line of each and every SPI device on the bus. Finally, to reset the counter, the other remaining IO line feeds the active low reset of the counter (SPI_SEL_CLR) and this allows you to very quickly reset the SPI select to device channel 0."

    Tom
  • twm47099twm47099 Posts: 867
    edited 2016-12-24 02:33
    I decided to try porting the C3 ADC spin code to C. I used c3_analog_digital_demo_sterm_010.spin by Andre' LaMothe as the basis. The spin program is located on the ftp site (above post), and the book "Unleashing the Propeller C3" has the information on the ADC & SPI methods. The tricky parts are how the C3 selects the SPI device (instead of just dropping CS), and Andre' wrote a general SPI write/read routine that writes a bit to the device and then reads a bit from the device during the same SPI clock cycle. I'm more used to writing the entire command and then reading the data.

    I added a 400 ms delay between each set of readings to allow the printing to keep up with the data collection. Otherwise the printed values lagged far behind as I adjusted the voltage using the potentiometers.

    Here's the SimpleIDE C version. --

    EDIT 12/23 9:50 EST: sorry I had edited it in a text editor & and introduced some errors. I am reediting it in the IDE and will repost when fixed.

    Tom


  • Here's the fixed C program

    Tom
  • Thanks Tom, I will give it a test run when I get back to the C3 project.

    Ray
Sign In or Register to comment.