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 is not working with PCD v4.117

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



Joined: 26 Apr 2010
Posts: 56

View user's profile Send private message

SPI is not working with PCD v4.117
PostPosted: Fri Jan 14, 2011 9:44 pm     Reply with quote

Hi,

I am using PCD v4.117 and PIC24FJ256GB206. SPI device is a flash memory m25p128. There are two m25p128 connected together with different chip select IO from PIC. With old PCD 3.249, everything is working (18f452 to control spi) device).

m25p128 shall take 3-5 seconds to erase 16MB memory. During that time status register shall be 255. After erasing, the register should be 0.
With scope, clock and data in are perfect. But data from spi_read is 0 all the time.

Attached is my code. Please help.
Thanks.
Howard

Code:

#include <24FJ256GB206.h>
#fuses NOPR,HS,PROTECT,NOWDT

#use delay(clock=32M, crystal)
#zero_ram

//FRONT PANNEL
#define FP_RX         PIN_F4   //INTPUT
#define FP_TX         PIN_F5   //OUTPUT
#pin_select U1RX=FP_RX
#pin_select U1TX=FP_TX
#use rs232(UART1,baud=115200,bits=8, parity=N, stop=1, errors, stream=FP,restart_wdt)

//SPI pin
#define SPI_DATA_OUT      PIN_G7   //OUTPUT
#define SPI_DATA_IN      PIN_G8   //INPUT
#define SPI_CLK         PIN_G6   //INPUT
#define SPI_MEM_HOLD      PIN_E5   //OUTPUT - 0 IS ON HOLD
#define SPI_MEM_CS1      PIN_E6   //OUTPUT
#define SPI_MEM_CS2      PIN_G9   //OUTPUT

//SPI map
#pin_select SCK1OUT=SPI_CLK
#pin_select SDO1=SPI_DATA_OUT
#pin_select SS1OUT=SPI_DATA_IN



#define WREN_CMD        0x06  //Write Enable
#define WRDI_CMD        0x04  //Write Disable
#define RDSR_CMD        0x05  //Read Status Register
#define WRSR_CMD        0x01  //Write Status Register
#define READ_CMD        0x03  //Read Data Bytes
#define FAST_READ_CMD   0x0b  //Read Data Bytes at Higher Speed
#define PP_WRITE_CMD    0x02  //Page Program
#define SEC_ERASE_CMD   0xd8  //Sector Erase
#define ALL_ERASE_CMD   0xc7  //Bulk Erase
#define RDID_CMD          0xb9  //Read Identification


void main()
{
   unsigned int8 i, data = 1;
   
   output_high(SPI_MEM_CS1); // Start CS high
   output_high(SPI_MEM_CS2); // Start CS high
   output_low(SPI_MEM_HOLD);
   output_high(SPI_CLK); // Start CLK high
   
   
   // setup SPI with master and rising edge transmit
   setup_spi(SPI_MASTER | SPI_L_TO_H | SPI_XMIT_L_TO_H | SPI_CLK_DIV_16);
   

   enable_interrupts(INT_RDA);
   enable_interrupts(INTR_GLOBAL);
   output_high(SPI_MEM_HOLD);   //enable flash memory chip
   delay_ms(1000);

   fprintf(FP,"hello, ready to start now!!\r\n");
   output_low(SPI_MEM_CS1); // Drive CS low
   spi_write(WREN_CMD); // Write enable command
   spi_write(ALL_ERASE_CMD); // bulk erase command
   spi_write(RDSR_CMD); // SPI read status command
   data=spi_read(0); // Read first byte
   
   i = 0;
   while((data & 0x1)==1)
   {
      i++;
      delay_ms(1000); // Wait for WIP = 0 meaning
      data=spi_read(0); // Read first byte
      fprintf(FP,"waiting %u seconds\r\n", i);
     
   }

   output_high(SPI_MEM_CS1); // Drive CS high
   delay_ms(500); // 40s max wait
   fprintf(FP,"spi data is %u. Done\r\n", data);
   while(1);
   
}
Ttelmah



Joined: 11 Mar 2010
Posts: 19538

View user's profile Send private message

PostPosted: Sat Jan 15, 2011 3:11 am     Reply with quote

Bulk erase, won't start, till you raise the CS.

The sequence needs to be:
Drop chip select
Send erase command.
Raise chip select
Drop chip select
Send 'read status'
Read bytes till command finishes
Raise chip select

It is ignoring your command, because CS has not gone high after the bulk erase command.

Section 6.1 of the data sheet:
"Chip Select (S) must be driven High after the eighth bit of the instruction code has been latched in, otherwise the Bulk Erase instruction is not executed. As soon as Chip Select (S) is driven High, the self-timed Bulk Erase cycle (whose duration is tBE) is initiated."

Best Wishes
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Sat Jan 15, 2011 3:42 am     Reply with quote

PCD V4.117 hasn't been released by CCS. Ask there.
miro



Joined: 15 Jan 2011
Posts: 62

View user's profile Send private message

Re: SPI is not working with PCD v4.117
PostPosted: Sat Jan 15, 2011 9:46 am     Reply with quote

Howard,
The potential issue is the spi_write() does not wait until all bits are clocked out from the chip. When you toggle the /CS after a spi_write() high, you stop the spi transmition at the memory side too early. Try this (I've found that on the C30 forum) and it works fine in C30 and pic24 (8bit mode, SPI inter. disabled):
Code:

// for CCS
#BIT SPI1IF  =0x084.10
#BYTE SPI1BUF =0x248
#BIT SPITBF  =0x240.1
#BIT SPIROV  =0x240.6
#BIT SPIRBF  =0x240.0

void _spi_write(unsigned char data_out)
{       
SPI1IF = 0;                  //clear SPI IF
SPI1BUF = data_out & 0x00ff;     
while(SPITBF);     
while(SPI1IF == 0);       // wait on on SPI Interr. Flag
data_out = SPI1BUF;               
}


Miro
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Sat Jan 15, 2011 12:13 pm     Reply with quote

Quote:
The potential issue is the spi_write() does not wait until all bits are clocked out from the chip.

This has been the case with previous PCD versions, where it was recommended to use spi_read() instead of spi_write(). CCS has finally changed the code, now spi_write() is identical to spi_read(), waiting for RBF before returning from the command.

The only problem I can see is, that M25P128 is not operated according to the datasheet, that requires to release nCS after every command, as Ttelmah already mentioned. I must confess, that I never tried, if it possibly works keeping nCS asserted.
kongfu1



Joined: 26 Apr 2010
Posts: 56

View user's profile Send private message

Re: SPI is not working with PCD v4.117
PostPosted: Sat Jan 15, 2011 3:10 pm     Reply with quote

miro wrote:
Howard,
The potential issue is the spi_write() does not wait until all bits are clocked out from the chip. When you toggle the /CS after a spi_write() high, you stop the spi transmition at the memory side too early. Try this (I've found that on the C30 forum) and it works fine in C30 and pic24 (8bit mode, SPI inter. disabled):
Code:

// for CCS
#BIT SPI1IF  =0x084.10
#BYTE SPI1BUF =0x248
#BIT SPITBF  =0x240.1
#BIT SPIROV  =0x240.6
#BIT SPIRBF  =0x240.0

void _spi_write(unsigned char data_out)
{       
SPI1IF = 0;                  //clear SPI IF
SPI1BUF = data_out & 0x00ff;     
while(SPITBF);     
while(SPI1IF == 0);       // wait on on SPI Interr. Flag
data_out = SPI1BUF;               
}


Miro


Miro,

Thanks a lot.
Howard
kongfu1



Joined: 26 Apr 2010
Posts: 56

View user's profile Send private message

PostPosted: Sat Jan 15, 2011 3:12 pm     Reply with quote

Ttelmah wrote:
Bulk erase, won't start, till you raise the CS.

The sequence needs to be:
Drop chip select
Send erase command.
Raise chip select
Drop chip select
Send 'read status'
Read bytes till command finishes
Raise chip select

It is ignoring your command, because CS has not gone high after the bulk erase command.

Section 6.1 of the data sheet:
"Chip Select (S) must be driven High after the eighth bit of the instruction code has been latched in, otherwise the Bulk Erase instruction is not executed. As soon as Chip Select (S) is driven High, the self-timed Bulk Erase cycle (whose duration is tBE) is initiated."

Best Wishes


Good points. Those were missed when I try to put all codes together for the post. Thanks.
Howard
kongfu1



Joined: 26 Apr 2010
Posts: 56

View user's profile Send private message

PostPosted: Sat Jan 15, 2011 3:22 pm     Reply with quote

FvM wrote:
PCD V4.117 hasn't been released by CCS. Ask there.


Correct. 4.117 is not officially released yet. I only have two things, PCD.dll 4.117 fixed RTOS and a device head file to fix spi register error. There is a memory management bug for 24FJ256GB206 (maybe other PICs) that disabled malloc/calloc as well as structure array declaration that shall be fixed in final release of 4.117 soon.
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Sat Jan 15, 2011 4:12 pm     Reply with quote

Quote:
Those were missed when I try to put all codes together for the post.

Do you mean, your code operates nCS correctly but still doesn't work? Then post a real application rather than some fake code!

Quote:
Correct. 4.117 is not officially released yet. I only have two things, PCD.dll 4.117 fixed RTOS and a device head file to fix spi register error.

In other words, noone would be able to reproduce your results, because your PCD version uses different SPI registers? And there's a finite chance that it's just a newly introduced 4.117 bug.
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Sat Jan 15, 2011 4:20 pm     Reply with quote

Perhaps it's just another case of fake code, but if this line has been correctly copied from the real application, you don't need to search why the code fails:

#pin_select SS1OUT=SPI_DATA_IN
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