View previous topic :: View next topic |
Author |
Message |
icesynth
Joined: 03 Sep 2007 Posts: 32 Location: Edmonton, Alberta
|
SPI_H_TO_L idling low? |
Posted: Fri Jan 29, 2010 1:22 am |
|
|
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
|
|
Posted: Fri Jan 29, 2010 2:19 am |
|
|
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
|
|
Posted: Fri Jan 29, 2010 9:15 am |
|
|
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
|
|
Posted: Sat Jan 30, 2010 12:39 am |
|
|
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
|
|
Posted: Sat Jan 30, 2010 3:18 am |
|
|
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
|
|
Posted: Sat Feb 06, 2010 12:08 pm |
|
|
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
|
|
Posted: Sat Feb 06, 2010 1:33 pm |
|
|
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
|
|
Posted: Sat Dec 11, 2010 11:49 am |
|
|
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. |
|
|
|