Skip to main content
Topic: Adding extra SPI devices? (Read 5296 times) previous topic - next topic

Adding extra SPI devices?

Hi all,

I'm trying to add extra SPI devices by using the EEPROM header to get SPI bus, plus spare I/O pins on the I/O header for Chip Select function.

Anyone tried this yet? I'm having trouble getting the SPI bus on the EEPROM heaer to function - I'm modifying the 'hello world' demo app from the dangerous prototypes dsPIC tutorial.

See attached C and header files.
Nothing seems to happen on the SPI bus, and the code gets stuck in a loop in the write_EEPROM_SPI() function.

Time to get Bus Pirate logic analyzer up and running!

Thanks in advance - it would be good to get a template together for this, as adding extra SPI chips would be a very useful feature - ADC, DAC, Memory, Temperature sensors etc.

Will

Re: Adding extra SPI devices?

Reply #1
Hey Will - Check in the PIC datasheet IO section under Peripheral Pin Select, or look through the init function of some of the server demos. The SPI functions are not fixed to any pin, they are assigned using the PPS function. I didn't see the setup in your code (maybe I missed it), but unless you assign pins to each SPI function they won't input or output anywhere.
Got a question? Please ask in the forum for the fastest answers.

Re: Adding extra SPI devices?

Reply #2
Adding... You can assign SPI to any of the IO header pins too, they all have PPS capability.
Got a question? Please ask in the forum for the fastest answers.

Re: Adding extra SPI devices?

Reply #3
Ahh I see. Ok, so I added the following after the UART PPS assignments:

   //assign SPI2 pins to EEPROM pins - see web Platform schematic
   RP1_O=SDO2_O;
   RP2_O=SCK2OUT_O;
   SDI2R_I=4;

I assume that RP1 = Physical pin 1?

Still no difference - processing hangs when I issue an 'r' to the terminal.
Do I also need to assign the EEPROM CS to a pin so I can force that high to ensure the eeprom does not conflict with the ADC?

Oh, and aren't these line sin HardwareProfile.h already assigning the pins?
//EEPROM setup
// 25LC I/O pins
#define EEPROM_CS_TRIS      (TRISCbits.TRISC7)
#define EEPROM_CS_IO      (PORTCbits.RC7)//could be LAT for output...
#define EEPROM_SCK_TRIS      (TRISCbits.TRISC6)
#define EEPROM_SDI_TRIS      (TRISBbits.TRISB9)
#define EEPROM_SDO_TRIS      (TRISCbits.TRISC8)


Thanks,

Will

Re: Adding extra SPI devices?

Reply #4
Take a look at your disassembly listing of your code.
You read from EEPROM_SSPBUF into temp, but temp is not used anywhere in the function.
The compiler *may* have optimised the assignment out of your code - as a result the buffer isn't read, and the SPIROV may be set, locking up the SPI module. There must be something like this going on because if the module is configured correctly, there should be nothing stopping it from sending a byte ( as in, it doesn't care if anything is actually listening in master mode - or even if the correct pins are assigned to the module )

Try making temp "volatile":
   volatile short temp;

...that should stop the compiler optimising it out (if that's the problem at all)

Tony

Re: Adding extra SPI devices?

Reply #5
Yes, you will need to hold the CD of the EEPROM high to de-select it. That's just a regular I/O though, and not a special peripheral.

The defines are just macros to use in your code, they don't do anything. The RP1 is reprogrammable pin 1 which you can find by looking at the datasheet or schematic, it's probably not pin 1. You can read more about macros and PPS in the intro to ds33 post:
http://dangerousprototypes.com/2010/02/ ... ogramming/
Got a question? Please ask in the forum for the fastest answers.

Re: Adding extra SPI devices?

Reply #6
Found the problems - the example I followed was enabling the SPI port before it was configured - swapped that around has fixed it.
Now have included STDIO and can printf the ADC value to the UART - web platform can now react to light level.

More details to follow when I've tidied up the demo & hardware.

BTW - are any of the PIC Analog inputs wired to any of the I/O pins? Could save using an external ADC chip.

Re: Adding extra SPI devices?

Reply #7
See attached code for example use of SPI interface from web platform to MAX147 ADC chip.

Screenshot of Terminal, printing value of MSB of ADC.
Light Dependant Resistor plus Variable resistor wired as potential divider across channel 7 of the ADC.

Next I will integrate the code back into the main (uIP) web platform code so I can read Light level via Telnet.

Cheers,

Will

Re: Adding extra SPI devices?

Reply #8
Nice work. Thanks for the PIC.

Quote
BTW - are any of the PIC Analog inputs wired to any of the I/O pins? Could save using an external ADC chip.

I think the ICSP header pins are ADC pins.
Got a question? Please ask in the forum for the fastest answers.