CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

#USE SPI for multiple streams

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
bwhiten



Joined: 26 Nov 2003
Posts: 151
Location: Grayson, GA

View user's profile Send private message

#USE SPI for multiple streams
PostPosted: Fri Jun 13, 2008 7:05 am     Reply with quote

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

View user's profile Send private message

Re: #USE SPI for multiple streams
PostPosted: Fri Jun 13, 2008 7:20 am     Reply with quote

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

View user's profile Send private message

PostPosted: Fri Jun 13, 2008 8:11 pm     Reply with quote

RLS, You may over-estimate my programming abilities Smile 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

View user's profile Send private message

PostPosted: Sat Jun 14, 2008 4:55 am     Reply with quote

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

View user's profile Send private message

PostPosted: Sat Jun 14, 2008 8:59 am     Reply with quote

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

View user's profile Send private message

PostPosted: Sat Jun 14, 2008 7:10 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Sun Jun 15, 2008 8:04 am     Reply with quote

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

View user's profile Send private message

Thanks for the knowledge
PostPosted: Sun Jun 15, 2008 9:26 am     Reply with quote

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.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group