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

What is wrong for this SPI and M25P40 Flash Memory ???

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



Joined: 18 Oct 2003
Posts: 145

View user's profile Send private message

What is wrong for this SPI and M25P40 Flash Memory ???
PostPosted: Mon May 01, 2006 2:36 pm     Reply with quote

Hi,

I use the M25P40 Flash memory from ST in my project and I have this source code but the SPI hardware function NOT WORK!!, the bit banging WORK!!.

Can you tell me that is wrong in this code??.

The M25P40 work in mode 0,0 & 1,1 and The most significant bit is send first. The SPI_WRITE send first the MSB or LSB ?????

Thank you very much for help me!!

Code:

void SPIFLASH_Init( void )
{
   #ifdef SPIFLASH_SPI_FUNC
      setup_spi( SPI_MASTER | SPI_L_TO_H | SPI_XMIT_L_TO_H | SPI_CLK_DIV_4 );
   #endif

   output_high( SPIFLASH_SELECT_BANK0 );
   output_high( SPIFLASH_SELECT_BANK1 );
   output_low( SPIFLASH_DI );
   output_low( SPIFLASH_CLK );
}



#ifdef SPIFLASH_BIT_BANGING

   void SPIFLASH_Out( char *sData, int16 lLen )
   {
      int      iBit;

      int16   lPtr;
      char   cByte;

      for ( lPtr = 0 ; lPtr < lLen ; lPtr++ )
      {
         cByte = sData[ lPtr ];

         for( iBit = 0 ; iBit < 8; iBit++ )
         {
            output_bit( SPIFLASH_DI, shift_left( &cByte, 1, 0 ) );
            output_high( SPIFLASH_CLK );      //data latches
            output_low( SPIFLASH_CLK );         //back to idle
         }      
      }
   }



   void SPIFLASH_In( char *sData, int32 dLen )
   {
      int      iBit;
      int32   dPtr;
      char   cByte;

      for ( dPtr = 0 ; dPtr < dLen ; dPtr++ )
      {
         for( iBit = 0 ; iBit < 8; iBit++ )
         {
            output_high( SPIFLASH_CLK );                     //data latches
            shift_left( &cByte, 1, input( SPIFLASH_DO ) );
            output_low( SPIFLASH_CLK );                        //back to idle
         }
         sData[ dPtr ] = cByte;
      }
   }

#else

   void SPIFLASH_Out( char *sData, int16 lLen )
   {
      int      iBit;
      int16   lPtr;
      char   cByte;

      for ( lPtr = 0 ; lPtr < lLen ; lPtr++ )
      {
         for( iBit = 0 ; iBit < 8; iBit++ )
            shift_right( &cByte, 1, bit_test( sData[ lPtr ], iBit - 1 ) );

         spi_write( cByte );
      }
   }



   void SPIFLASH_In( char *sData, int32 dLen )
   {
      int      iBit;
      int32   dPtr;
      char   cByte;


      for ( dPtr = 0 ; dPtr < dLen ; dPtr++ )
      {
         cByte = spi_read( 0xFF );
         for( iBit = 0 ; iBit < 8; iBit++ )
            shift_right( &sData[ dPtr ], 1, shift_left( &cByte, 1, 0 ) );
      }
   }

#endif



int SPIFLASH_Status( void )
{
   int      iCmd, iStatus;

   iCmd = SPIFLASH_INST_RSR;

   output_low( SPIFLASH_SELECT_BANK0 );
   SPIFLASH_Out( &iCmd, 1 );
   SPIFLASH_In( &iStatus, 1 );
   output_high( SPIFLASH_SELECT_BANK0 );

   return iStatus;
}



PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue May 02, 2006 12:41 am     Reply with quote

I don't know why you're doing all the shifting stuff in the hardware
version. It should be something like this:
Code:

void SPIFLASH_Out(char *sData, int16 lLen )
{
int16 lPtr;

for(lPtr = 0 ; lPtr < lLen; lPtr++)
   {
    spi_write(sData[lPtr]);
   }

}

Also the SPIFLASH_In() routine needs to be fixed in a similar way.
cfernandez



Joined: 18 Oct 2003
Posts: 145

View user's profile Send private message

PostPosted: Tue May 02, 2006 6:54 am     Reply with quote

Dear PCM,

The SPI_Write send firs the Most Significant Bit (7) or LSB ?

Thank you very much!!!!!!
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue May 02, 2006 9:40 am     Reply with quote

Quote:
The SPI_Write send firs the Most Significant Bit (7) or LSB ?

Look in the data sheet for your PIC. You didn't say what PIC you're
using, but the section will probably be called:
"Master Synchronous Serial Port (MSSP)"

Then in that section, find the timing diagram labeled:
SPI MODE TIMING, MASTER MODE

That diagram shows the bits. The bit on the left side of the
timing diagram is the one that comes out first.

Read that section in the data sheet and you will learn the answer.
cfernandez



Joined: 18 Oct 2003
Posts: 145

View user's profile Send private message

PostPosted: Tue May 02, 2006 12:33 pm     Reply with quote

PCM

You are a MONSTER!!!!!!!!!

Thank you very much!!!!!!!!!!
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