📄 main.c
字号:
/******************** (C) COPYRIGHT 2003 STMicroelectronics ********************
* File Name : main.c
* Author : MCD Application Team
* Date First Issued : 03/07/2003
* Description : This file is used for the the STR71x BSPI COMMUNICATION WITH
* M25P10-A SERIAL FLASH application note V2.0(AN 1810).
********************************************************************************
* History:
* 03/07/2003 : First Version
* 07/12/2004 : V2.0
*******************************************************************************
THE PRESENT SOFTWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS WITH
CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME.
AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT, INDIRECT
OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE CONTENT
OF SUCH SOFTWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING INFORMATION
CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
*******************************************************************************/
#include "71x_lib.h"
/* STR71x pin definition */
#define BSPI1_MISO 0x0010 /* SPI1: Master in/Slave out data */
#define BSPI1_MOSI 0x0020 /* SPI1: Master out/Slave In data */
#define BSPI1_SCLK 0x0040 /* SPI1: Serial Clock */
#define BSPI1_SSN 0x0080 /* SPI1: SSN */
#define M25_SSN 0x0008 /* M25 Ship Select */
#define BSPI1_MC 0x0004 /* BSPI1 Master Config */
/* M25 instruction definition */
#define M25_WREN 0x06 /* Write Enable instruction */
#define M25_WRDI 0x04 /* Write Disable instrction */
#define M25_RDSR 0x05 /* Read Status Register instruction */
#define M25_WRSR 0x01 /* Write Status Register instruction */
#define M25_PP 0x02 /* Page Program instruction */
#define M25_SE 0xD8 /* Sector Erase instruction */
#define M25_BE 0xC7 /* Bulk Erase instruction */
#define M25_DP 0xB9 /* Deep Power-down instruction */
#define M25_RES 0xAB /* Release from Deep Power-down, and Read */
/* Electronic Signature */
#define M25_READ 0x03 /* Read Data Byte instruction */
#define M25_FASTREAD 0x0B /* Read Data Byte at Higher Speed */
/* Status Register Bit definition */
#define M25_WIP 0x01 /* Write In Progress bit, polling it to establish */
/* when the previous write cycle or erase cycle
is complete. */
#define M25_WEL 0x02 /* Write Enable Latch bit indicates the status
of the internal write enable latch. */
#define M52_BP0 0x04 /* Block Protect bits, they define the area to
be software */
#define M25_BP1 0x08 /* protected against program and erase
instructions. */
#define M25_SRWD 0x80 /* The Status Register Write Protect, */
#define M25_Sector0 0x0001 /* M25 sector0 address memory declaration */
#define M25_Sector1 0x0801 /* M25 sector1 address memory declaration */
#define M25_Sector2 0x1001 /* M25 sector2 address memory declaration */
#define M25_Sector3 0x1801 /* M25 sector3 address memory declaration */
#define M25_WriteAddress 0x0000
#define Dummy 0x0F
#define M25_ReadAddress M25_WriteAddress
#define M25_Chip_Select_ENABLE GPIO0->PD &= ~0x0008
#define M25_Chip_Select_DISABLE GPIO0->PD |= 0x0008
#define M25_InstructionSend(Instruction) BPSI_DataSendReceive (Instruction)
#define M25_DataSend(Data) BPSI_DataSendReceive (Data)
#define M25_DataReceive() BPSI_DataSendReceive (Dummy)
#define BSPI_TFE 1<<6
#define BSPI_RFF 1<<4
#define STRING "STR71x-BSPI/M25P10_A"
#define BufferSize (sizeof(STRING)-1)
void InitBSPIFlash (void);
void M25_AddressSend(u32 Address);
void M25_SectorErase ( u32 M25_Sector );
u8 BPSI_DataSendReceive(u8 DATA);
char SendBuffer [BufferSize]=STRING;
char ReceiveBuffer [BufferSize];
int main (void)
{
u8 i;
#ifdef DEBUG
debug();
#endif
/* ------------------------------------------------------------------------------- */
/* Initialize the STR71x BSPI1 & erase the corresponding sector memory to be used. */
/* ------------------------------------------------------------------------------- */
/* Initialize STR71x BSPI device */
InitBSPIFlash ();
M25_Chip_Select_DISABLE;
M25_Chip_Select_ENABLE;
/* Send a write enable instruction to the memory */
M25_InstructionSend(M25_WREN);
M25_Chip_Select_DISABLE;
M25_Chip_Select_ENABLE;
/* Erase sector0 from the memory */
M25_SectorErase(M25_Sector0);
M25_Chip_Select_DISABLE;
M25_Chip_Select_ENABLE;
/* Send Read status register instruction */
M25_InstructionSend(M25_RDSR);
/* wait until the sector is erased */
while((M25_DataReceive()&M25_WIP));
M25_Chip_Select_DISABLE;
/* ------------------------------------------------------------------------------- */
/* Write data to the memory and wait until all the data is porogrammed */
/* ------------------------------------------------------------------------------- */
M25_Chip_Select_ENABLE;
/* Send a write enable instruction to the memory */
M25_InstructionSend(M25_WREN);
M25_Chip_Select_DISABLE;
M25_Chip_Select_ENABLE;
/* Send page program instruction */
M25_InstructionSend(M25_PP);
/* send address memory to write to*/
M25_AddressSend(M25_WriteAddress);
for (i=0;i<BufferSize;i++)
/* Send data to the M25 memory */
M25_DataSend(SendBuffer [i]);
M25_Chip_Select_DISABLE;
M25_Chip_Select_ENABLE;
/* Send Read status register instruction */
M25_InstructionSend(M25_RDSR);
/* Waite until all the data sent is programmed */
while((M25_DataReceive()&M25_WIP));
M25_Chip_Select_DISABLE;
/* ------------------------------------------------------------------------------- */
/* Read from the flash memory the data alraidy porogrammed */
/* ------------------------------------------------------------------------------- */
M25_Chip_Select_ENABLE;
/* send a Read Data Byte instruction */
M25_InstructionSend(M25_READ);
/* send address memory to be read */
M25_AddressSend(M25_ReadAddress);
for (i=0;i<BufferSize;i++)
/* read all the data already written */
ReceiveBuffer[i] = M25_DataReceive();
M25_Chip_Select_DISABLE;
while(1);
}
/*******************************************************************************
* Function Name : InitBSPIFlash
* Description : Initialize the BSPIFlash.
* Input : None.
* Return : None
*******************************************************************************/
void InitBSPIFlash (void)
{
/* 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 */
GPIO_BitWrite(GPIO0, 0x2, 0x1);
/* Initialize BSPI1 device */
BSPI_Init ( BSPI1 );
/* Configure Baud rate Frequency: ---> APB1/6 */
BSPI_ClockDividerConfig (BSPI1, 6);
/* Enable BSPI1 */
BSPI_Enable ( BSPI1, ENABLE );
/* Configure BSPI1 as a Master */
BSPI_MasterEnable ( BSPI1, ENABLE );
/* Configure the clock to be active low */
BSPI_ClkActiveHigh ( BSPI1, DISABLE );
/* Enable capturing the first Data sample on the first edge of SCK */
BSPI_ClkFEdge ( BSPI1, DISABLE );
/* Set the word length to 16 bit */
BSPI_8bLEn ( BSPI1, ENABLE );
}
/*******************************************************************************
* Function Name : M25_AddressSend
* Description : Send the Target Address (24 bits).
* Input : Address memory to send.
* Return : None
*******************************************************************************/
void M25_AddressSend(u32 Address)
{
BPSI_DataSendReceive((Address & 0x00FF0000) >> 16 );
BPSI_DataSendReceive((Address & 0x0000FF00) >> 8 );
BPSI_DataSendReceive((Address & 0x000000FF) );
}
/*******************************************************************************
* Function Name : M25_SectorErase
* Description : This function erase a specifyed sector
* Input : The sector name to erase
* Return : None
*******************************************************************************/
void M25_SectorErase ( u32 M25_Sector )
{
/* Send write enable instruction */
BPSI_DataSendReceive(M25_SE);
/* Send the 24 bits Target Address */
M25_AddressSend(M25_Sector);
}
/*******************************************************************************
* Function Name : BPSI_DataSendReceive
* Description : wait until the end of transmit and receive data was accomplit
* Input : Data to dend
* Return : Received data
*******************************************************************************/
u8 BPSI_DataSendReceive(u8 DATA)
{
u8 temp;
while(BSPI1->CSR2&BSPI_RFNE)
temp = BSPI1->RXR;
while(!(BSPI1->CSR2&BSPI_TFE)); /* Wait until the Transmit FIFO is empty */
BSPI1->TXR = DATA<<8; /* Send data to Transmit buffer */
while(!(BSPI1->CSR2&BSPI_RFF)); /* Wait until the end of transmission */
temp = (BSPI1->RXR)>>8; /* Read the received data */
return temp;
}
/******************* (C) COPYRIGHT 2003 STMicroelectronics *****END OF FILE****/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -