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

Weird problems with SPI on PIC24H

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



Joined: 01 Oct 2003
Posts: 172
Location: Punta Gorda, Florida USA

View user's profile Send private message Send e-mail

Weird problems with SPI on PIC24H
PostPosted: Sun Jul 29, 2012 10:21 am     Reply with quote

I seem to have some very weird issues, besides the fact that I spent almost one day trying to debug new firmware with what appears to be a faulty ICD-U64 (would not read any of the program variable values, everything read as zero) Once I replaced the debugger and went to the ICD3 I am now seeing some normality and I can read program variables. In any case that is not why I'm here asking for help. I am working with a PIC24HJ256GP206 which I have used many times before with the CCS PCD compiler. On this project I am using the SPI2 port running on mode 0. In the past I have used the following line to set-up my hardware spi port:
Code:

 setup_spi2(SPI_MASTER | SPI_L_TO_H | SPI_XMIT_L_TO_H |SPI_CLK_DIV_16 | SPI_MODE_8B); // set up hardware spi for mode

and never had an issue however, now I find that the devices I am talking to in spi are not working correctly, so I attached my SPI logic analyzer and was able to see that everything was working correctly for the exception of my MOSI line always at zero??? the MISO, SCLK and chip select lines were working fine. I know my tris registers for any of these ports are set correctly, so there should be no problem there. Now if I use the following preprocessor line:
Code:

 #use SPI(FORCE_HW,SPI2,MASTER,DI=PIN_G7,DO=PIN_G8,MODE=0,BAUD=57600,BITS=8)

then my MOSI line works and my SPI bus seems to work fine????
I thought this may be a bug on the latest release of the compiler and tried various earlier versions with the same results.
Can any one tell me what is the difference between:
Code:

 setup_spi2(SPI_MASTER | SPI_L_TO_H | SPI_XMIT_L_TO_H |SPI_CLK_DIV_16 | SPI_MODE_8B);
 

and
Code:

 #use SPI(FORCE_HW,SPI2,MASTER,DI=PIN_G7,DO=PIN_G8,MODE=0,BAUD=57600,BITS=8)
 

I thought you can use either one of these, the latter will work with spi_xfer() which I never really used, I tried looking at the asm code for setup_spi() and it appears to be correct. I do not believe this issue is device specific as I have used the setup_spi() on the same device on previous projects without problems.

I'm missing something??????
jeremiah



Joined: 20 Jul 2010
Posts: 1354

View user's profile Send private message

PostPosted: Sun Jul 29, 2012 12:19 pm     Reply with quote

What were the values of SPI2STAT and SPI2CON when you read them while trouble shooting?
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Sun Jul 29, 2012 12:37 pm     Reply with quote

To allow others checking the generated code, you need to tell the PCD version.
cbarberis



Joined: 01 Oct 2003
Posts: 172
Location: Punta Gorda, Florida USA

View user's profile Send private message Send e-mail

PostPosted: Sun Jul 29, 2012 2:13 pm     Reply with quote

My compiler version of PCD is the latest 4.135

If I use the preprocessor spi directive (works fine)
SPI2STAT = 0xC001
SPI2CON1 = 0x0121
SPI2CON2 - 0x0000

now if I use the setup_spi2() ( MOSI goes away)
SPI2STAT = 0xC000
SPI2CON1 = 0x01BD
SPI2CON2 - 0x0000
cbarberis



Joined: 01 Oct 2003
Posts: 172
Location: Punta Gorda, Florida USA

View user's profile Send private message Send e-mail

PostPosted: Sun Jul 29, 2012 3:37 pm     Reply with quote

It looks like when I use the setup_spi2() function there is a difference in the SPI2CON bits, looks like bit 7 is being set (SSEN) which activates the slave SPI select pin, this definitely should not happen!!! as it is set for master mode the other item is the SPRE bit being set to 1:1 (I don't think that matters) Now I wonder if MOSI is affected by the SSEN bit?? I don't think it should?
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Mon Jul 30, 2012 12:47 am     Reply with quote

Quote:
Now I wonder if MOSI is affected by the SSEN bit?? I don't think it should?

I agree it shouldn't, but I would try just in case. There may be a problem with other parts of the SPI code, can you post a minimized code example that reproduces the problem?
cbarberis



Joined: 01 Oct 2003
Posts: 172
Location: Punta Gorda, Florida USA

View user's profile Send private message Send e-mail

PostPosted: Mon Jul 30, 2012 6:13 am     Reply with quote

The only part of the SPI code is just the read and write functions as shown below:
void w5100_write_reg(int16 addr, unsigned int8 data);
unsigned int8 w5100_read_reg(int16 addr);


void w5100_write_reg(int16 addr, unsigned int8 data)
{
W5100_CHIPSEL;
delay_us(15);
spi_read2(W5100_WRITE_CMD);
spi_read2(addr >> 8); // Send address msb
spi_read2(addr); // Send address lsb
spi_read2(data); // Write data to W5100
delay_us(15);
W5100_CHIPNOSEL;
}


unsigned int8 w5100_read_reg(int16 addr)
{
int8 retval;

W5100_CHIPSEL;
delay_us(15);
spi_read2(W5100_READ_CMD);
spi_read2(addr >> 8); // Send address msb
spi_read2(addr); // Send address lsb
retval = spi_read2(1); // Read data from W5100
delay_us(15);
W5100_CHIPNOSEL;

return(retval); }
RF_Developer



Joined: 07 Feb 2011
Posts: 839

View user's profile Send private message

PostPosted: Mon Jul 30, 2012 7:08 am     Reply with quote

Quote:
Can any one tell me what is the difference between:


setup_spi2(SPI_MASTER | SPI_L_TO_H | SPI_XMIT_L_TO_H |SPI_CLK_DIV_16 | SPI_MODE_8B);


and

#use SPI(FORCE_HW,SPI2,MASTER,DI=PIN_G7,DO=PIN_G8,MODE=0,BAUD=57600,BITS=8)


setup_spi() is older and can only be used on PICs with SPI hardware. If setup_spi() is used, you must use spi_read(), spi_write() and spi_data_is_in().

#use spi is the newer, more flexible way of doing SPI stuff. It will give software SPI support when no hardware SPI device is available. You can also force it to impliment a software SPI device if required. #use spi is used with spi_xfer() and not the older spi_write() and spi_read(). It may work if you mix the two up, but there may be odd and unexpected results.

Your two SPI setups give quite different results. Your setup_spi() sets the 2nd SPI hardware port to be mode 0 clocked at 1/16 of your clock rate, or 3MHz. I do not know what "SPI_MODE_8B" means: it doesn't appear in any of the device .h files that I've got. Is it a PIC24 related setting? Your use spi directive sets the second SPI port to be mode two at the very much slower bit rate of 57600. That shows you are not clear about the difference between SPI and asynchronous serial comms. SPI rarely, if ever runs at any of the clock rates commonly seen for serial. Indeed SPI clock rates are generally in the order of MHz or at least high KHz, 57600 bps is very slow, and is not the same rate as your setup_spi. You then set it to use SPI2, are its default pins the same as you've set, if not then despite your FORCE_HW, you may be getting a software SPI. Defining bits as 8 *may* result in a software SPI, though it shouldn't. All the SPI hardware in all PICs is 8 bits. Certainly setting anything other than 8 bits will result, possibly unexpectedly, in a sofware SPI. I try to specify only what is required, and I've found that over-specifying SPI and serial settings leads to a software emulation rather than using the hardware. In your case I suspect its more than possible that your #use spi has given you a software SPI.

You must remember that the point of these support routines and directives is to hide device dependencies from you. setup_spi() won't generate the same code on all PICs, it will vary from PIC to PIC. It will be about the same, but not quite. So yes, it could be a device dependancy, or a compiler bug, or... well, most likely its programmer error. One thing to look for is contention between devices. In other words is something else, even something you haven't set up, already using the pins? Is there a conflict between your pins settings and the default for your SPI2? You mention TRIS settings, that means you are using fast_io. That can cause trouble, especially on these more complex PICs. Are you really, really sure you've got that set up correctly?

RF Developer
Ttelmah



Joined: 11 Mar 2010
Posts: 19539

View user's profile Send private message

PostPosted: Mon Jul 30, 2012 7:37 am     Reply with quote

Yes, SPI_MODE_8B, is specific to several of the 24 and 33 chips. They support 16bit hardware SPI, so you have settings:

SPI_MODE_8B
SPI_MODE_16B

available to enable this.

8B is the default on all these chips, and the define codes as '0', and does nothing. It is the 16B one that actually changes anything.....


Best Wishes
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Mon Jul 30, 2012 8:19 am     Reply with quote

I don't see any issues with the SPI handling of PCD V4.135.

You can correct the configuration by adding SPI_SS_DISABLED to setup_spi2().

For the SPI speed settings, you should use the respective SPI_CLK_xx configuration constants.

I don't agree with some points said in the above post, e.g. about more more flexible operation of #use spi. It does e.g. not support reconfiguration of SPI, which is essential for a number of application.

The dataseheet also clarifies that hardware spi overrides port tristate settings.
cbarberis



Joined: 01 Oct 2003
Posts: 172
Location: Punta Gorda, Florida USA

View user's profile Send private message Send e-mail

PostPosted: Mon Jul 30, 2012 4:28 pm     Reply with quote

In my case both setup_spi() #use spi are selecting the hardware functions of the PIC, not software spi and that can be easily seen by looking at the contents of SPI2STAT and SPI2CON. As far as selecting a Baud rate in the case of the #use api directive is because if you don't put in a baud rate it reverts to the highest speed available which may be not a good idea to use, so what else can you put in there when they are asking for a baud rate? I usually never treat the spi bus like the asynchronous serial port.
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Tue Jul 31, 2012 12:28 am     Reply with quote

Quote:
As far as selecting a Baud rate in the case of the #use api directive is because if you don't put in a baud rate it reverts to the highest speed available which may be not a good idea to use, so what else can you put in there when they are asking for a baud rate?

Yes, that's the intended way to specify the transmission speed. There's nothing wrong with the #use spi statement as far as I see. Usually you'll however want higher SPI clock rates in the MHz range.
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