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 Previous  1, 2, 3
 
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

PostPosted: Fri Apr 17, 2009 7:10 pm     Reply with quote

OK I really have no words to thank you enough FvM.
I've been pulling my hair trying to make this work for a month now. You did it man!
Oh the joy!!!!

Thank you so much!!!!!!
THANK YOU!
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Sat Apr 18, 2009 3:35 am     Reply with quote

I'm glad, that the problem could be solved finally.

Now I have supplement regarding changes in V4.091.

I previously suggested to use spi_read only because of a bug in status bit handling with spi_write().
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;


In V4.091, SPIxTBF is handled correctly, but my original code is still not working due to another non-obvious issue.
Code:
spi_write(0xAB);
spi_write(0);
spi_write0);
spi_write(0);
bID=spi_read(0);

The code is intended to write 4 bytes (a command and 3 dummy bytes) and then read one byte in the fifth cycle.

Code:
....................    spi_write(0);
05D1C:  CLR.B   248
05D1E:  BTSC.B  240.1
05D20:  BRA     5D1E
05D22:  BCLR.B  240.6
....................    bID=spi_read(0);
05D24:  CLR.B   248
05D26:  BTSS.B  240.0
05D28:  BRA     5D26
05D2A:  MOV.B   248,W0L
05D2C:  BCLR.B  240.6
05D2E:  MOV.B   W0L,17F1


In the above assembly listing of part of the code, bID doesn't return the result of the last SPI cycle as expected.

This happens because spi_write() resets SPIROV (240.6) before the first byte is received, spi_read(0) doesn't wait for the second cycle to finish but returns the read data from previous cycle immediately. The problem can be handled by inserting a dummy spi_read() before the spi_read(0), or by using spi_read(xx) only, as I did before.

I won't say that it's actually a bug, but a problem involved with the status bit handling in PCD built-in SPI functions. But it must be expected to confuse PCD users seriously.
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Mon Sep 14, 2009 2:34 pm     Reply with quote

As a late update, I realized that PCD is using the mask value SPI_H_TO_L in a different meaning than the 8-Bit compilers. Thus my above coding of SPI modes isn't correct. What I declared as SPI MODE_3 is actually setting mode 0. It's no problem with SPI flash, that supports both modes. This is a corrected mapping of SPI modes.
Code:
#define SPI_MODE_0_0 0x0100
#define SPI_MODE_0_1 0x0000
#define SPI_MODE_1_0 0x0140
#define SPI_MODE_1_1 0x0040   
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 Previous  1, 2, 3
Page 3 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