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 CCS Technical Support

SPI_H_TO_L idling low?

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



Joined: 03 Sep 2007
Posts: 32
Location: Edmonton, Alberta

View user's profile Send private message Visit poster's website

SPI_H_TO_L idling low?
PostPosted: Fri Jan 29, 2010 1:22 am     Reply with quote

I am having an issue with the idle clock level during the SPI setup, code used to initialize and send a character is below. When initialized, the idle clock level stays low.

Code:

//********************************************************************
// System fuses
//********************************************************************
#fuses XT, PR_PLL , NOCOE, NODEBUG, NOWRTB, NOPUT, NOWRTSS, NOWRT, NOPROTECT, NORSS, NOWDT, NOWINDIS
#use delay( clock=40000000, restart_wdt )

...


setup_spi2( SPI_MASTER | SPI_H_TO_L | SPI_CLK_DIV_64 );
delay_ms(1);
spi_write2( 0xCB );



Screenshot of issue...
Yellow is the clock...


Has anyone encountered this issue before? Any suggestions?

Compiler: 4.104
Device: 33FJ64GP310
_________________
Programming for the the real world.
--Chris Burchett
Sylver Technologies Inc.
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Fri Jan 29, 2010 2:19 am     Reply with quote

Yes, coding of SPI modes is apparently partly incorrect with PCD compiler. Because I dislike the CCS
mode designators generally, I'm using these constants with PCD in my code. They are working at least with PIC24.

Code:
#define SPI_CLK_DIV_2    0x001B
#define SPI_CKE          0x0100
#define SPI_CKP          0x0040
#define SPI_SMP          0x0200

#define SPI_MODE_0 (SPI_CKE)
#define SPI_MODE_1 (0)
#define SPI_MODE_2 (SPI_CKP | SPI_CKE)
#define SPI_MODE_3 (SPI_CKP)

The constants are directly copied to the SPICON register, when used with spi_setup().
icesynth



Joined: 03 Sep 2007
Posts: 32
Location: Edmonton, Alberta

View user's profile Send private message Visit poster's website

PostPosted: Fri Jan 29, 2010 9:15 am     Reply with quote

Thanks!

I'll give it a try here and let you know what happens.
_________________
Programming for the the real world.
--Chris Burchett
Sylver Technologies Inc.
icesynth



Joined: 03 Sep 2007
Posts: 32
Location: Edmonton, Alberta

View user's profile Send private message Visit poster's website

PostPosted: Sat Jan 30, 2010 12:39 am     Reply with quote

Hmm... Still no go even when configuring the registers directly. Weird thing is that it is working if changed to SPI1, but does not seem to have an effect on SPI2 no matter what configuration bits are changed, leading me to believe the registers might possibly be wrong, but according to the datasheet they are correct. Just wondering what's missing...

Code:

   #word SPI2STAT   = 0x0260   // SPI 2 Status Word
   #word SPI2CON1  = 0x0262   // SPI 2 Control Register 1
   #word SPI2CON2  = 0x0264   // SPI 2 Control Register 2
   #word SPI2BUF     = 0x0268   // SPI 2 TX / RX Buffer

   SPI2CON1 =  0b0000000001100000; // CKP Idle High Active Low using Master Mode
   SPI2STAT |= 0b1000000000000000; // Enable SPI

   // Write to the port for debugging.
   while(true)
   {
      spi_write2( 0xCB );
      
      // Wait for the transfer to complete...
      while(!spi_data_is_in2())
      {
      }
       spi_read2(); // Discard unused data from buffer
      delay_us(250);
   }

_________________
Programming for the the real world.
--Chris Burchett
Sylver Technologies Inc.
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Sat Jan 30, 2010 3:18 am     Reply with quote

You should better specify the intended SPI mode unequivocally. You're setting mode 3 in your code. Is that,
what you intend? In the classical CCS C SPI syntax for 8-Bit processors, SPI_H_TO_L designates mode 2.
Figure 20-3 in the dsPIC30 Family Reference Manual shows the relation of SPI configuration bits and waveforms.

I must admit, that I didn't yet use SPI with dsPIC33 and don't know about possible issues. But as far as I understand
the documentation, SPI operation is identical among all dsPIC/PIC24 devices.
icesynth



Joined: 03 Sep 2007
Posts: 32
Location: Edmonton, Alberta

View user's profile Send private message Visit poster's website

PostPosted: Sat Feb 06, 2010 12:08 pm     Reply with quote

Found a workaround,

It seems that as long as I do not call spi_setup() or spi_setup2() anywhere in the code then it works fine with manually setting the bits and enabling the hardware. The spi_read, spi_write functions still work fine, though.

Interesting that it would have an effect, but this has been confirmed by a couple of people here that there is the same problem with other similar devices as well.
_________________
Programming for the the real world.
--Chris Burchett
Sylver Technologies Inc.
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Sat Feb 06, 2010 1:33 pm     Reply with quote

I must admit, that I don't exactly understand what's not working correctly in your code. The wrong respectively unusual
coding of SPI mode with spi_setup() is obvious, but it can be simply overcome by using the above SPI_MODE_X
macros. I'm not aware of other issues with recent PCD versions and spi_setup() built-in function.
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Sat Dec 11, 2010 11:49 am     Reply with quote

Quote:
The constants are directly copied to the SPICON register, when used with spi_setup().

With PCD V4.109, this statement became wrong. I suggested the above SPI MODE constants, because the MODE definition in PCD PIC24 device files had been incorrect. CCS decided to fix the problem by changing spi_setup() operation instead of correcting the SPI mode definitions. So the below version dependant constant definition is necessary to get MODE 2 and MODE 3 coded correctly: (I don't have PCD V4.108, so I can't check if it's already "fixed").
Code:
#IF  getenv("VERSION")>=4.109
  #define SPI_MODE_0 0x100
  #define SPI_MODE_1 0
  #define SPI_MODE_2 0x40
  #define SPI_MODE_3 0x140
#ELSE
  #define SPI_MODE_0 0x100
  #define SPI_MODE_1 0
  #define SPI_MODE_2 0x140
  #define SPI_MODE_3 0x40
#ENDIF

I spare myself additional consideration about undocumented compiler changes and other bad habbits. This has been said already.
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