View previous topic :: View next topic |
Author |
Message |
bwhiten
Joined: 26 Nov 2003 Posts: 151 Location: Grayson, GA
|
#USE SPI for multiple streams |
Posted: Fri Jun 13, 2008 7:05 am |
|
|
I'm looking for an example using the #USE SPI method rather than the setup_spi method. #USE SPI has the STREAM = option and I need to run two device speeds on the same SPI bus. One at 10MHz and the other at 5MHz. I have separate chip selects for these.
Seems when I search the forum for combinations of #USE and SPI I get the same 1034 posts with little relevance to my question. |
|
|
RLScott
Joined: 10 Jul 2007 Posts: 465
|
Re: #USE SPI for multiple streams |
Posted: Fri Jun 13, 2008 7:20 am |
|
|
You could just avoid the CCS library of SPI functions and write your own code by talking directly to the SPI registers. You could reconfigure the SPI clock speed before each transaction so as to customize the speed for the slave device. The SPI interface is fairly simple and is well-documented in the Microchip datasheets.
Robert Scott
Real-Time Specialties |
|
|
bwhiten
Joined: 26 Nov 2003 Posts: 151 Location: Grayson, GA
|
|
Posted: Fri Jun 13, 2008 8:11 pm |
|
|
RLS, You may over-estimate my programming abilities Mostly hardware design here until I began using PICs. I would hope, for my needs, the library functions would suffice. That is if they work. I can use the spi_setup just fine individually, and that code works, but when I use the #USE SPI version of setup things don't go so well. |
|
|
RLScott
Joined: 10 Jul 2007 Posts: 465
|
|
Posted: Sat Jun 14, 2008 4:55 am |
|
|
What, then, is the problem with using the spi_setup version of the code? _________________ Robert Scott
Real-Time Specialties
Embedded Systems Consulting |
|
|
bwhiten
Joined: 26 Nov 2003 Posts: 151 Location: Grayson, GA
|
|
Posted: Sat Jun 14, 2008 8:59 am |
|
|
I not sure how to switch speeds during run time using the setup_spi command. Is it possible to use it at the beginning of each access to a device? |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Sat Jun 14, 2008 7:10 pm |
|
|
In my code I have two SPI setup functions with different speed settings, like: Code: | //-----------------------------------------------------------------------------
// Setup the SPI bus with a slow clock speed (16MHz / 64 = 256kHz).
// RTC has a max. speed of 1MHz, but MMC and FRAM go to 20MHz and SD even to 25MHz.
//-----------------------------------------------------------------------------
void SPI_SlowClock()
{
setup_spi(SPI_MASTER | SPI_MODE_3 | SPI_CLK_DIV_64);
}
//-----------------------------------------------------------------------------
// Setup the SPI bus with a fast clock speed (16MHz / 4 = 4MHz).
// RTC has a max. speed of 1MHz, but MMC and FRAM go to 20MHz and SD even to 25MHz.
//-----------------------------------------------------------------------------
void SPI_FastClock()
{
setup_spi(SPI_MASTER | SPI_MODE_3 | SPI_CLK_DIV_4);
} |
My clock speed is 16MHz and depending on the selected device I call one of the two functions. Example: Code: | //-----------------------------------------------------------------------------
// Select the specified device on the SPI bus and set the SPI clock speed.
// Calling this function assures no two devices will be active at the same time.
//-----------------------------------------------------------------------------
void SPI_Select(spi_device Device)
{
// Disable all SPI devices
// Note that some devices need a high level and others a low level.
output_low(RTC_SELECT); // Active high
output_high(FRAM_SELECTinv); // Active low
output_high(MMC_SELECTinv); // Active low
switch (Device)
{
case SPI_NO_DEVICE:
// Deselect all devices.
// Nothing to do here, devices already deselected
break;
case SPI_RTC:
// Select Real Time Clock
SPI_SlowClock();
delay_us(5); // RTC is slow; at least 4 us required between selects
output_high(RTC_SELECT); // Active high
delay_us(5); // RTC is slow; at least 4 us after CE is activated
break;
case SPI_FRAM:
// Select FRAM
SPI_FastClock();
output_low(FRAM_SELECTinv); // Active low
break;
case SPI_MMC:
// Select MMC Card flash memory
SPI_FastClock();
spi_write(0xFF); // Allow 8 dummy clocks before asserting CS again
output_low(MMC_SELECTinv); // Active low
break;
default:
// fprintf(ToLog, "Error: Unknown spi device selected\n\r");
break;
}
} |
|
|
|
Ken Johnson
Joined: 23 Mar 2006 Posts: 197 Location: Lewisburg, WV
|
|
Posted: Sun Jun 15, 2008 8:04 am |
|
|
Look at the generated assembly code.
I believe that when you use the #use spi with the stream option, the spi is setup on each call to write_spi(). If you use setup_spi(), then that setting remains in effect until another setup_spi().
Look at the generated assembly code.
Ken |
|
|
bwhiten
Joined: 26 Nov 2003 Posts: 151 Location: Grayson, GA
|
Thanks for the knowledge |
Posted: Sun Jun 15, 2008 9:26 am |
|
|
I hadn't realized you could repeatedly call setup_spi with different parameters during runtime. I guess I was treating it like a preprocessor directive. I'll give that a try next.
Thanks again. |
|
|
|