PIC18 SPI slave ADC (for RPi)

Discussion in 'Embedded Systems and Microcontrollers' started by nsaspook, Oct 14, 2012.

  1. nsaspook

    Thread Starter AAC Fanatic!

    Aug 27, 2009
    2,910
    2,172
    In the process of creating a Linux module for the Raspberry PI I wrote this slave/master loop SPI tester for it using a F8722 and F25K22. The ADC slave module is independent so it could be used as the basis for a SPI based ADC IO expander.

    https://github.com/nsaspook/daq_gert/tree/master/RPi_PIC

    It works well as a signal generator but YMMV...
     
  2. nsaspook

    Thread Starter AAC Fanatic!

    Aug 27, 2009
    2,910
    2,172
    This is a clip of the Raspberry PI using a PIC18 (Powered at 3.3 Volts from the PI) as a 12 channel external ADC via SPI. The RPi program just reads all 12 channels and displays the data. The hex byte 4C is a configuration byte sent from the PIC that tells the host it's ADC configuration.

    Code ( (Unknown Language)):
    1. /*
    2.  * bit 7 high for command
    3.  * bit 6 0 send lower or 1 send upper byte ADC result first
    4.  * bits 3..0 port address
    5.  *
    6.  * bit 7 low  for config data sent in CMD_DUMMY per uC type
    7.  * bits 6 config bit code always 1
    8.  * bit    5 0=ADC ref VDD, 1=ADC rec FVR=2.048
    9.  * bit  4 0=10bit adc, 1=12bit adc
    10.  * bits 3..0 number of ADC channels
    11.  *
    12.  */
    Channel 0 is connected to a pot for input voltage adjustment, channel 1 is connected to VDD , and the rest are just floating. It's a mess of wiring with breakouts so the signals can be easily monitored. :D

    http://flic.kr/p/dmagUX

    PIC firmware C program:
    https://github.com/nsaspook/daq_gert/blob/master/RPi_PIC/SlaveO.c
    RPi testing C program:
    https://github.com/nsaspook/daq_gert/blob/master/pictest.c
     
    Last edited: Oct 20, 2012
  3. nsaspook

    Thread Starter AAC Fanatic!

    Aug 27, 2009
    2,910
    2,172
  4. nickelflipper

    Active Member

    Jun 2, 2010
    280
    35
    This is good stuff, thanks for your work and bringing it to our attention. I finally got a Pi to play with this winter, so will be diving in sometime in the near future. I am a real newbie on the microprocessor and linux end of things.

    I took a brief look at your code and had a few general questions that I'm sure is obvious to others.

    1) Is the SPI library for the Pi using a hardware SPI module on the BRCM chip, or is it software?

    2) Can the Pi use SPI or other hardware interrupts like the UART?

    3) Do you have links to how a beginner can compile project code and the toolchain involved?

    4) Is there a link to the repository of libraries and library commands associated with low level type of microcontroller functions (UART, I2C, PWM etc.)?
     
  5. nsaspook

    Thread Starter AAC Fanatic!

    Aug 27, 2009
    2,910
    2,172
    1. The SPI is using the hardware on the chip, the BCM2835 has a SPI master.
    2. The Linux kernel separates direct SPI hardware manipulation from the Userland via a set of message and process queues so only at the direct kernel driver level (like my module) do you have full access to them. It's not something a beginner should mess without knowing the danger because there is little to stop you from causing massive disk corruption from overwriting kernel memory.

    This is the basic information needed to understand the structure: http://lwn.net/Kernel/LDD3/
    A HOW-TO to install my module in a RPi system: https://github.com/nsaspook/daq_gert/blob/master/RPi_Comedi_HOWTO.txt

    3. User programs that use SPI (or any IO) device open and operate them with the IOCTL interface (files) or a device library (Comedi, etc) that abstracts that device into a standard C program interface. So programming something on the RPi is like any normal system that has a C (gcc) compiler. I use Netbeans on a x86 Linux system to write the software and then compile it on the RPi using a NFS mounted drive they share.

    4. This should be a good starting place for hardware interface software. https://projects.drogon.net/raspberry-pi/
     
    Last edited: Oct 27, 2012
  6. nickelflipper

    Active Member

    Jun 2, 2010
    280
    35
    Thanks for the links and info, looks like I have plenty of reading to do now too :). Time to load some software and dig in. I had a go at OpenWRT with a PIC, but only got as far as ECHO xx to the ...../USB0 (or something like that)....lol.
     
Loading...