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

H/W SPI on a pic24
Goto page 1, 2, 3  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
geokonst



Joined: 04 Apr 2009
Posts: 27

View user's profile Send private message

H/W SPI on a pic24
PostPosted: Tue Apr 07, 2009 4:16 pm     Reply with quote

Dear all,
Has anyone implemented a hardware spi on a pic24 before? Even better if using Peripheral Pin Select(Pin_select) ?

If yes could you please post the code because I am stuck for days (see my other post on SPI_READ) and about to give up.

Thank you in advance.


Last edited by geokonst on Tue Apr 07, 2009 4:50 pm; edited 1 time in total
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Tue Apr 07, 2009 4:31 pm     Reply with quote

I suppose it's working regularly. Of course, there may be some bugs as with other PCD built-in functions. Ask me a few days later.
Guest








PostPosted: Tue Apr 07, 2009 10:03 pm     Reply with quote

geokonst-

Any luck with your SPI module? I'm working with the PIC24FJ256GB106 and am also having problems getting it to function. Are you using the #use spi functionality? I've tried both that and writing directly to the control registers without any success.
geokonst



Joined: 04 Apr 2009
Posts: 27

View user's profile Send private message

PostPosted: Wed Apr 08, 2009 7:09 am     Reply with quote

Dear Guest,
I am afraid I haven't done any progress at all. Yes I've ended up using #use spi because ccs support recommended that. But still I cant read the spi repsonse. see also :
http://www.ccsinfo.com/forum/viewtopic.php?t=38457

It seems to me there is some weird bug with h/w spi for 24f. What exactly is your problem with spi?

Wish you luck.

FvM did you have a chance to look at it?
Guest








PostPosted: Wed Apr 08, 2009 10:13 am     Reply with quote

geokonst-

I noticed you other post after I responded to this one and it seems we are having nearly the same problems. i am unable to get so much as a clock out signal from my SPI bus, and the designated DO pin will not put out any data. I've tried different forms of configuration and checked the status and configuration registers to verify that they are being set correctly (which it appears they are). I've also viewed the the pin select configuration registers to check if the pins are being mapped correctly (which again they appear to be). At this point I'm at a loss as it seems to be a H/W problem like you mentioned. I've contacted Microchip's support center about it an am awaiting a reply. I'll post again if I find out anything useful.
geokonst



Joined: 04 Apr 2009
Posts: 27

View user's profile Send private message

PostPosted: Wed Apr 08, 2009 10:29 am     Reply with quote

Thank you.

In the meanwhile could you please post the version of your code that writes directly to the control registers to give it a go?

cheers
Guest








PostPosted: Wed Apr 08, 2009 12:20 pm     Reply with quote

The register locations are defined as follows:
Code:
#word SPI1STAT = 0x240    //SPI1  Status register
#word SPI1CON1 = 0x242    //SPI1 Control register 1
#word SPI1CON2 = 0x244    //SPI1 Control register 2

#word RPINR20 = 0x6A8
#word RPINR21 = 0x6AA
#word RPOR11  = 0x6D6

Remember that the RPINx and RPORx registers are based on which pins you define to be the input/output pins of the SPI register (see the datasheet section on the Peripheral Pin Select for more info).

To write to the register requires only an assignment statement such as
Code:
SPI1STAT = 0x8000;

Keep in mind that SPIxCON1 and SPIxCON2 have to be written to before SPIxSTAT since changes can only be made to the registers when the module is off (setting the 15th bit of SPIxSTAT high enables that module).

When checking the contents of the register I'm outputting the results to my Serial port screen using rs-232. This looks something like this:
Code:
printf("SPI1STAT = %4LX\r\n",SPI1STAT);
geokonst



Joined: 04 Apr 2009
Posts: 27

View user's profile Send private message

PostPosted: Wed Apr 08, 2009 1:06 pm     Reply with quote

So are you using SPI1BUF then to send something or spi_write? What are you trying to communicate with?

Have you tried the second code I posted? That doesn't doesn't even generate a clock?

I don't believe that something in the errata has anything to do with our issue, but maybe you might want have a look at it. There are three articles on SPI

http://ww1.microchip.com/downloads/en/DeviceDoc/80369d.pdf

Please let me know if have had any progress, because I really need to make this work ASAP.

Thanks
Guest








PostPosted: Wed Apr 08, 2009 1:08 pm     Reply with quote

I've tried both SPIBUF and spi_write without any success, I'll take a look at the second code you've posted and see if I can't make some changes on my own. I'm still awaiting a reply form Microchip as I am also needing to get this done as soon as possible.
geokonst



Joined: 04 Apr 2009
Posts: 27

View user's profile Send private message

PostPosted: Wed Apr 08, 2009 1:26 pm     Reply with quote

Are you working with an accelerometer too? I actually need to implement two spis (like one was not enough); one between the accelerometer and the gb106 one between the gb106 and another pic.

The good news is that I've already done software spi for the accelerometer (in case I can't make HW work). Unfortunately software spi will be too slow for comms between the two pics.

Tomorrow morning (UK time)I will try the registers approach and see what I get on the scope.
Please update me if you find anything.
Cheers!
Guest








PostPosted: Wed Apr 08, 2009 1:31 pm     Reply with quote

A few questions:

Is the spi.h file a user defined one or one that came with the compiler?

In your code you have an initial spi_read() and then another when you send 0xFF. Is the value based on your sensor's communication protocol or is there another reason for it?

I looked over the errata and agree, I don't think any of the listings are relevant to the problems we're having (although it may have to do with the 30 us delay you had to insert between read functions).
geokonst



Joined: 04 Apr 2009
Posts: 27

View user's profile Send private message

PostPosted: Wed Apr 08, 2009 2:34 pm     Reply with quote

Dear Guest,
I just happened to name my c file spi so spi.h was the file generated by the wizard.

The reason I am used two spi_read is that if I tried with one write and one read (as it would be the normal thing to expect) I would get only 8 clocks. What I found out later is that the two clocks for the two commands overlapped. That's where the delay comes in.
I have no idea why using two reads works when two writes or one write and one read won't (without the delay) .

The FF on the second read is just dummy data to keep the clock going which is I believe common practice. I chose FF instead of 00 because the mode is 3 on the acc. 8F is the accelerometers who is register.

Any news from microchip? have you tried ccsinfo customer support? They tend to answer within hours. They still have answered me though in my last email :(

cheers
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Wed Apr 08, 2009 3:45 pm     Reply with quote

As expected, the PIC24FJ128GA106 SPI is working regularly. As with other PIC24, it's recommended to read the datasheet thoroughly and to check, which PCD built-in functions are working correctly and which are apparently faulty. I used PCD V4.084. There may be SPI related changes towards V4.090, but rather unlikely.

I used this setup, with some additional definitions.
Code:
#define SPI_CLK_DIV_2    0x001B
#define SPI_CPHA_0       0x0100

#define SPI_MODE_0 (SPI_L_TO_H | SPI_CPHA_0)
#define SPI_MODE_1 (SPI_L_TO_H)
#define SPI_MODE_2 (SPI_H_TO_L | SPI_CPHA_0)
#define SPI_MODE_3 (SPI_H_TO_L)

setup_spi(SPI_MASTER | SPI_MODE_3 |SPI_CLK_DIV_1);

#pin_select preprocessor commands are generally functional, except for the RPI45 input pin that's used for SDI1 in my application. So I had to go back to manual register settings (not shown here).
Code:
#pin_select SDI1=PIN_xx
#pin_select SDO1=PIN_F3
#pin_select SCK1OUT=PIN_D9

From the read/write built-in functions, only spi_read(data) is working correctly, and it must not be used without an argument. As in other cases, PCD built-in functions apparently have never been tested at CCS.

Code:
SPI_nCS_LAT = 0;
spi_read(0xAB);
spi_read(0);
spi_read(0);
spi_read(0);
bID=spi_read(0);
SPI_nCS_LAT = 1;

The above code reads the ID byte from a serial flash memory, proving that the interface is operating correctly.

I also tried #use spi, but an error message informed me, that it isn't supporting hardware SPI.
geokonst



Joined: 04 Apr 2009
Posts: 27

View user's profile Send private message

PostPosted: Wed Apr 08, 2009 8:57 pm     Reply with quote

Dear Fvm,
Thank you for your reply.
Could you please clarify for me the following;
What exactly SPI_CPHA_0 is doing?
How did you find out that RPI45 was not working with pin_select? Did you get an error message? could this be causing my problem?
Finally why are you using four spi_reads? Do you need to send 3 bytes before the memory chip responds or is it some other reason?

Thanks for you help.
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Thu Apr 09, 2009 12:42 am     Reply with quote

Quote:
What exactly SPI_CPHA_0 is doing
It's used to set the clock phase (CPHA in the original Motorola SPI terminology). Without setting this bit, PIC24 SPI operation defaults to mode 1 and mode 3. With PCH, this option could be set by SPI_XMIT_L_TO_H. As said, you have to read the PIC24 datasheet. (And should know, which SPI modes are supported by the connected peripheral).

Code:
#pin_select SDI1=PIN_F6
results in an "Invalid Pin" error. (PIN_F6 is RPI45)

Quote:
Finally why are you using four spi_reads?
Sending 3 dummy bytes is simply required by the serial flash. Nothing specific to PIC24.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page 1, 2, 3  Next
Page 1 of 3

 
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