?? ssp_flash_driver.c
字號:
/******************** (C) COPYRIGHT 2007 STMicroelectronics ********************
* File Name : ssp_flash_driver.h
* Author : MCD Application Team
* Version : V2.0
* Date : 12/07/2007
* Description : Driver containing c functions to access to M25P64 ST
* Serial Flash Memory available on MB460 eval board
********************************************************************************
* 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.
*******************************************************************************/
/* Includes ------------------------------------------------------------------*/
#include "ssp_flash_driver.h"
/* Private typedef -----------------------------------------------------------*/
#define SSP_FLASH_PageSize 256
#define WRITE 0x02 /* Write to Memory instruction */
#define WRSR 0x01 /* Write Status Register instruction */
#define WREN 0x06 /* Write enable instruction */
#define READ 0x03 /* Read from Memory instruction */
#define RDSR 0x05 /* Read Status Register instruction */
#define SE 0xD8 /* Sector Erase instruction */
#define BE 0xC7 /* Bulk Erase instruction */
#define Low 0x00 /* ChipSelect line low */
#define High 0x01 /* ChipSelect line high */
#define WIP_Flag 0x01 /* Write In Progress (WIP) flag */
#define Dummy_Byte 0xA5
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
static void SSP_FLASH_ChipSelect(u8 State);
static u8 SSP_FLASH_SendByte(u8 byte);
static void SSP_FLASH_WriteEnable(void);
static void SSP_FLASH_WaitForWriteEnd(void);
/* Private functions ---------------------------------------------------------*/
/*******************************************************************************
* Function Name : SSP_FLASH_Init
* Description : Initializes the peripherals used by the SPI FLASH driver.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void SSP_FLASH_Init(void)
{
SSP_InitTypeDef SSP_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_DeInit(GPIO5);
/* Gonfigure SSP0_CLK, SSP0_MOSI */
GPIO_InitStructure.GPIO_Direction = GPIO_PinOutput;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5;
GPIO_InitStructure.GPIO_Type = GPIO_Type_PushPull;
GPIO_InitStructure.GPIO_Alternate = GPIO_OutputAlt2;
GPIO_Init(GPIO5, &GPIO_InitStructure);
/* Gonfigure SSP0_NSS pins */
GPIO_InitStructure.GPIO_Direction = GPIO_PinOutput;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
GPIO_InitStructure.GPIO_Type = GPIO_Type_OpenCollector;
GPIO_InitStructure.GPIO_Alternate = GPIO_OutputAlt1;
GPIO_Init(GPIO5, &GPIO_InitStructure);
/* Gonfigure SSP0_MISO pin GPIO5.6 */
GPIO_InitStructure.GPIO_Direction = GPIO_PinInput;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
GPIO_InitStructure.GPIO_Type = GPIO_Type_PushPull;
GPIO_InitStructure.GPIO_IPInputConnected = GPIO_IPInputConnected_Enable;
GPIO_InitStructure.GPIO_Alternate = GPIO_InputAlt1 ;
GPIO_Init(GPIO5, &GPIO_InitStructure);
/* SSP0 configuration */
SSP_DeInit(SSP0);
SSP_InitStructure.SSP_FrameFormat = SSP_FrameFormat_Motorola;
SSP_InitStructure.SSP_Mode = SSP_Mode_Master;
SSP_InitStructure.SSP_CPOL = SSP_CPOL_High;
SSP_InitStructure.SSP_CPHA = SSP_CPHA_2Edge;
SSP_InitStructure.SSP_DataSize = SSP_DataSize_8b;
SSP_InitStructure.SSP_ClockRate = 0;
SSP_InitStructure.SSP_ClockPrescaler = 2;
SSP_Init(SSP0, &SSP_InitStructure);
/* SSP0 enable */
SSP_Cmd(SSP0, ENABLE);
}
/*******************************************************************************
* Function Name : SSP_FLASH_SectorErase
* Description : Erases the specified FLASH sector.
* Input : SectorAddr: address of the sector to erase.
* Output : None
* Return : None
*******************************************************************************/
void SSP_FLASH_SectorErase(u32 SectorAddr)
{
/* Send write enable instruction */
SSP_FLASH_WriteEnable();
/* Sector Erase */
/* Select the FLASH: Chip Select low */
SSP_FLASH_ChipSelect(Low);
/* Send Sector Erase instruction */
SSP_FLASH_SendByte(SE);
/* Send SectorAddr high nibble address byte */
SSP_FLASH_SendByte((SectorAddr & 0xFF0000) >> 16);
/* Send SectorAddr medium nibble address byte */
SSP_FLASH_SendByte((SectorAddr & 0xFF00) >> 8);
/* Send SectorAddr low nibble address byte */
SSP_FLASH_SendByte(SectorAddr & 0xFF);
/* Deselect the FLASH: Chip Select high */
SSP_FLASH_ChipSelect(High);
/* Wait the end of Flash writing */
SSP_FLASH_WaitForWriteEnd();
}
/*******************************************************************************
* Function Name : SSP_FLASH_BulkErase
* Description : Erases the entire FLASH.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void SSP_FLASH_BulkErase(void)
{
/* Send write enable instruction */
SSP_FLASH_WriteEnable();
/* Bulk Erase */
/* Select the FLASH: Chip Select low */
SSP_FLASH_ChipSelect(Low);
/* Send Bulk Erase instruction */
SSP_FLASH_SendByte(BE);
/* Deselect the FLASH: Chip Select high */
SSP_FLASH_ChipSelect(High);
/* Wait the end of Flash writing */
SSP_FLASH_WaitForWriteEnd();
}
/*******************************************************************************
* Function Name : SSP_FLASH_PageWrite
* Description : Writes more than one byte to the FLASH with a single WRITE
* cycle(Page WRITE sequence). The number of byte can't exceed
* the FLASH page size.
* Input : - pBuffer : pointer to the buffer containing the data to be
* written to the FLASH.
* - WriteAddr : FLASH's internal address to write to.
* - NumByteToWrite : number of bytes to write to the FLASH,
* must be equal or less than "SSP_FLASH_PageSize" value.
* Output : None
* Return : None
*******************************************************************************/
void SSP_FLASH_PageWrite(u8* pBuffer, u32 WriteAddr, u16 NumByteToWrite)
{
/* Enable the write access to the FLASH */
SSP_FLASH_WriteEnable();
/* Select the FLASH: Chip Select low */
SSP_FLASH_ChipSelect(Low);
/* Send "Write to Memory " instruction */
SSP_FLASH_SendByte(WRITE);
/* Send WriteAddr high nibble address byte to write to */
SSP_FLASH_SendByte((WriteAddr & 0xFF0000) >> 16);
/* Send WriteAddr medium nibble address byte to write to */
SSP_FLASH_SendByte((WriteAddr & 0xFF00) >> 8);
/* Send WriteAddr low nibble address byte to write to */
SSP_FLASH_SendByte(WriteAddr & 0xFF);
/* while there is data to be written on the FLASH */
while(NumByteToWrite--)
{
/* Send the current byte */
SSP_FLASH_SendByte(*pBuffer);
/* Point on the next byte to be written */
pBuffer++;
}
/* Deselect the FLASH: Chip Select high */
SSP_FLASH_ChipSelect(High);
/* Wait the end of Flash writing */
SSP_FLASH_WaitForWriteEnd();
}
/*******************************************************************************
* Function Name : SSP_FLASH_BlockWrite
* Description : Writes block of data to the FLASH. In this function, each
* byte is written using Byte WRITE sequence.
* Input : - pBuffer : pointer to the buffer containing the data to be
* written to the FLASH.
* - WriteAddr : FLASH's internal address to write to.
* - NumByteToWrite : number of bytes to write to the FLASH.
* Output : None
* Return : None
*******************************************************************************/
void SSP_FLASH_BlockWrite(u8* pBuffer, u32 WriteAddr, u16 NumByteToWrite)
{
/* while there is data to be written */
while(NumByteToWrite--)
{
/* Enable the write access to the FLASH */
SSP_FLASH_WriteEnable();
/* Select the FLASH: Chip Select low */
SSP_FLASH_ChipSelect(Low);
/* Send "Write to Memory " instruction */
SSP_FLASH_SendByte(WRITE);
/* Send WriteAddr high nibble address byte to write to */
SSP_FLASH_SendByte((WriteAddr & 0xFF0000) >> 16);
/* Send WriteAddr medium nibble address byte to write to */
SSP_FLASH_SendByte((WriteAddr& 0xFF00) >> 8);
/* Send WriteAddr low nibble address byte to write to */
SSP_FLASH_SendByte(WriteAddr & 0xFF);
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -