cancel
Showing results for 
Search instead for 
Did you mean: 

BSPI1 and M2510AV Communication

rickysonp
Associate II
Posted on December 20, 2007 at 02:20

BSPI1 and M2510AV Communication

6 REPLIES 6
rickysonp
Associate II
Posted on May 05, 2006 at 19:25

Hi guys! I'm trying to write and after read some data from Serial Flash memory. I use STR710FZ2T6.

My problem is: I can't neither read nor write data in flash.

Well, I will post my source. I'll appreciate any answer or advices.

Sorry for my bad english!

// Stop flash for configuration

// Everytime you wanna to send a command to flash you must call the Spi_Flash_SSN_Low()

Spi_Flash_SSN_High ();

// Initialize Spi-Flash (configure pin and set BSPI1 as master)

Spi_Flash_Init();

// Init and configure BSPI1

Bspi_Disable ( BSPI1 );

BSPI1->CSR1 = ( BSPI_MASTER_SELECT | BSPI_8_BIT_WORD_LENGTH );

BSPI1->CSR2 = BSPI_RESET_FIFO;

Bspi_Clockivider (BSPI1, 8);

//BSPI1->CSR1 |= BSPI_10_WORD_Rx_FIFO_ENABLE;

//BSPI1->CSR2 |= BSPI_10_WORD_Tx_FIFO_ENABLE;

Bspi_Enable ( BSPI1 );

/*****************************************************************************/

// Start communication

// Write enable instruction

Spi_Flash_SSN_Low ();

Spi_Flash_SendInstruction ( M25_WREN );

Spi_Flash_SSN_High ();

// Erase sector before record data in memory

Spi_Flash_SectorErase ( M25_Sector3 );

// Send a Read Status register instruction in order to see if the erase already was done

Spi_Flash_WaitBusy ();

// Write enable instruction

Spi_Flash_SSN_Low ();

Spi_Flash_SendInstruction ( M25_WREN );

Spi_Flash_SSN_High ();

// Start writing

Spi_Flash_SSN_Low ();

Spi_Flash_SendInstruction ( M25_PP );

Spi_Flash_SendAddress ( 0x00000 );

for (int i=0; i

Spi_Flash_SendData(0xAA);

Spi_Flash_SSN_High ();

// Wait instruction to be completed

Spi_Flash_WaitBusy ();

// Write Enable

Spi_Flash_SSN_Low ();

Spi_Flash_SendInstruction ( M25_WREN );

Spi_Flash_SSN_High ();

//Spi_Flash_WriteStatusRegister ( 0x0C );

//Spi_Flash_WaitBusy ();

//bb = Spi_Flash_ReadStatusRegister();

// Read data to see if it was correctly recorded into spi-flash memory

Spi_Flash_SSN_Low ();

Spi_Flash_SendInstruction ( M25_READ );

Spi_Flash_SendAddress ( 0x00030 );

// Receive all the data sent in reverse order

for (int i=9; i>=0; i++)

Buffer[i] = Spi_Flash_ReceiveData();

Spi_Flash_SSN_High ();

while(1); // infinite loop

inline void Spi_Flash_Init ()

{

// Configure GPI00 on mode Alternate function Push Pull

GPIO_Config ( GPIO0, BSPI1_MISO | BSPI1_MOSI | BSPI1_SCLK, GPIO_AF_PP );

// Configure SCLK & SSN clock and data lines control

GPIO_Config ( GPIO0, M25_SSN | BSPI1_MC | BSPI1_SSN, GPIO_OUT_PP );

// Set the BSPI1_MC pin to high level to configure the BSPI1 as master in the STR710 eval board

GPIO0->PD |= 0x0002;

}

// Everytime you wanna to send a command to flash you must call the Spi_Flash_Enable()

// that keep the !SSN M2510 pin low

inline void Spi_Flash_SSN_Low ()

{

GPIO0->PD &= ~0x0008;

}

// And after any access you gotta set high !SSN pin calling Spi_Flash_Disable

inline void Spi_Flash_SSN_High ()

{

GPIO0->PD |= 0x0008;

}

u8 BPSI_DataSendReceive ( u8 data )

{

u8 ret_value;

while( BSPI1->CSR2 & BSPI_Rx_FIFO_NOT_EMPTY )

ret_value = BSPI1->RXR; // Clear Rx FIFO

// Wait until the Transmit FIFO is empty

while( !( BSPI1->CSR2 & BSPI_Tx_FIFO_EMPTY ) );

// Send data to Transmit buffer

BSPI1->TXR = (data<

// Wait until the Transmit FIFO is empty

while( !( BSPI1->CSR2 & BSPI_Tx_FIFO_EMPTY ) );

// Wait until the end of transmission

while( BSPI1->CSR2 & BSPI_Rx_FIFO_NOT_EMPTY )

ret_value = BSPI1->RXR; // Clear Rx

ret_value = (BSPI1->RXR)>>8; // Read the received data

return ret_value;

}

inline void Spi_Flash_SendAddress ( u32 data )

{

Spi_Flash_SendData ( (data & 0x00FF0000)>>16 );

Spi_Flash_SendData ( (data & 0x0000FF00)>>8 );

Spi_Flash_SendData ( (data & 0x000000FF) );

}

inline u8 Spi_Flash_ReadStatusRegister()

{

u8 ret_value;

// Select device

Spi_Flash_SSN_Low();

// Send instruction to Transmit buffer

ret_value = BPSI_DataSendReceive ( M25_RDSR );

// Deselect device

Spi_Flash_SSN_High ();

return ret_value;

}

inline void Spi_Flash_WriteStatusRegister ( u8 value )

{

// Select device

Spi_Flash_SSN_Low();

// Send instruction to Transmit buffer

BPSI_DataSendReceive ( M25_WRSR );

BPSI_DataSendReceive ( value );

// Deselect device

Spi_Flash_SSN_High ();

}

inline void Spi_Flash_SectorErase ( Spi_Flash_Sector_TypeDef Sector )

{

// Select device

Spi_Flash_SSN_Low();

// Send Sector Erase Instruction

BPSI_DataSendReceive ( M25_SE );

// Send sector address

Spi_Flash_SendAddress ( Sector );

// Deselect device

Spi_Flash_SSN_High ();

}

inline void Spi_Flash_WaitBusy()

{

while ( ( Spi_Flash_ReadStatusRegister() & M25_WIP ) );

}

Thanks!! ;)

rickysonp
Associate II
Posted on May 08, 2006 at 16:41

Hi!

I can read any memory position, including the Eletronic Signature through an instruction. But my writes appear don't have effect. I also erased all memory using Erase Bulk.

So, I just can read 0xFF.

I use the same routine for read and write:

u8 BPSI_DataSendReceive ( u8 data )

{

u8 ret_value;

while( BSPI1->CSR2 & BSPI_Rx_FIFO_NOT_EMPTY )

ret_value = BSPI1->RXR; // Clear Rx FIFO

// Wait until the Transmit FIFO is empty

while( !( BSPI1->CSR2 & BSPI_Tx_FIFO_EMPTY ) );

// Send data to Transmit buffer

BSPI1->TXR = (data<

// Wait until the Transmit FIFO is empty

while( !( BSPI1->CSR2 & BSPI_Tx_FIFO_EMPTY ) );

// Wait until the end of transmission

while( !(BSPI1->CSR2 & BSPI_Rx_FIFO_FULL) );

ret_value = (BSPI1->RXR)>>8; // Read the received data

return ret_value;

}

And SW7 and SW8 are OK! Memory is not hold neither hardware protected against write.

That's it! Tks!

rickysonp
Associate II
Posted on May 08, 2006 at 18:33

I solved the problem! Tks! ;)

rickysonp
Associate II
Posted on May 09, 2006 at 09:58

Sure! Actually my source is a little different now! But the main reason is that I was verifying the WIP (Write in progress) bit of serial flash in a wrong way. Because I forgot to discard some dummy byte after the Read Status register command. The correct way to verify is:

void Spi_Flash_WaitBusy()

{

u8 status = (Spi_Flash_ReadStatusRegister() | M25_WIP);

while ( (status & M25_WIP) )

status = Spi_Flash_ReadStatusRegister();

}

u8 Spi_Flash_ReadStatusRegister()

{

u8 ret_value;

// Select device

Spi_Flash_SSN_Low();

// Send instruction to Transmit buffer and receive Status Register

Spi_Flash_SendInstruction ( M25_RDSR );

ret_value = Spi_Flash_ReceiveData();

// Deselect device

Spi_Flash_SSN_High ();

return ret_value;

}

That's it RISC! Tks!

sany_as
Associate II
Posted on October 05, 2006 at 05:58

Hello Rickyson!!!

Can you please give me your source code for using the spi flash...I also am using the spi flash M25P32.Is there any difference???

I would be thankfull to you.

Thanks in advance.

Sany

rajendrask
Associate II
Posted on December 20, 2007 at 02:20

Hello,

I am also struggling to read and write into M25P64 flash through SPI0. Please send me sample source code if you have,,,,

rajendrask@hcl.in