📄 c2195.c
字号:
/**************** STFL-I based Serial Flash Memory Driver **********************
Filename: c2195.c
Description: Library routines for the M25PE10, M25PE20, M25PE40, M25PE80
Serial Flash Devices.
Version: V1.1
Date: 10-17-2005
Authors: Zhi Tan, Da Gang Zhou, STMicroelectronics, Shanghai (China)
Copyright (c) 2005 STMicroelectronics.
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.
********************************************************************************
Version History.
Ver. Date Comments
1.0 07/07/2005 M25PE10, M25PE20 and M25PE40 support
1.1 10/17/2005 Add M25PE80 support by Da Gang Zhou
********************************************************************************
This source file provides library C code for the M25PE10, M25PE20, M25PE40,
and M25PE80 Serial Flash devices.
The following functions are available in this library:
Flash(WriteEnable, 0) to disable write protect of the Flash memory
Flash(WriteDisable, 0) to enable write protect of the Flash memory
Flash(ReadDeviceIdentification, ParameterType) to get the Device Identification from the device
Flash(ReadManufacturerIdentification, ParameterType) to get the manufacturer Identification from the device
Flash(ReadStatusRegister, ParameterType) to get the value in the Status Register from the device
Flash(Read, ParameterType) to read from the Flash device
Flash(FastRead, ParameterType) to read from the Flash device in a faster way
Flash(PageWrite, ParameterType) to write an array of elements within one page
Flash(PageProgram, ParameterType) to program an array of elements within one page
Flash(PageErase, ParameterType) to erase a whole page
Flash(SectorErase, ParameterType) to erase a whole sector
Flash(BulkErase, 0) to erase the whole chip
Flash(DeepPowerDown, 0) to put the memory in a low power consumption mode
Flash(ReleaseFromDeepPowerDown, 0) to wake up the memory from the low power consumption mode
Flash(Write, ParameterType) to write an array of elements
Flash(Program, ParameterType) to program an array of elements
Flash(ReadLockRegister, ParameterType) to get the lock register value
Flash(WriteLockRegister, ParameterType) to set the lock register value
FlashErrorStr() to return an error description (define VERBOSE)
Note that data Bytes will be referred to as elements throughout the document unless otherwise specified.
For further information consult the related Datasheets (M25PE10, M25PE20, M25PE40 and M25PE80)
and Application Note (ANXXXX).
The Application Note gives information about how to modify this code for
a specific application.
The hardware specific functions which may need to be modified by the user are:
FlashWrite() for writing an element (uCPUBusType) to the Flash memory
FlashRead() for reading an element (uCPUBusType) from the Flash memory
FlashTimeOut() to return after the function has timed out
A list of the error conditions can be found at the end of the header file.
*******************************************************************************/
#include "Serialize.h" /* Header file with SPI master abstract prototypes */
#include "c2195.h" /* Header file with global prototypes */
#ifdef TIME_H_EXISTS
#include <time.h>
#endif
#ifdef SYNCHRONOUS_IO
#define WAIT_TILL_INSTRUCTION_EXECUTION_COMPLETE(x) \
FlashTimeOut(0); \
while(IsFlashBusy()) \
{ \
if(Flash_OperationTimeOut == FlashTimeOut(x)) return Flash_OperationTimeOut; \
};
#else
// do nothing
#endif
/******************************************************************************
Global variables: none
*******************************************************************************/
/*******************************************************************************
Function: ReturnType Flash( InstructionType insInstruction, ParameterType *fp )
Arguments: insInstruction is an enum which contains all the available function
instructions of the SW driver.
fp is a (union) parameter struct for all Flash memory instruction parameters
Return Value: The function returns the following conditions:
Flash_AddressInvalid,
Flash_MemoryOverflow,
Flash_PageEraseFailed,
Flash_PageNrInvalid,
Flash_SectorNrInvalid,
Flash_FunctionNotSupported,
Flash_NoInformationAvailable,
Flash_OperationOngoing,
Flash_OperationTimeOut,
Flash_ProgramFailed,
Flash_Success,
Flash_WrongType
Description: This function is used to access all functions provided with the
current Flash device.
Pseudo Code:
Step 1: Select the right action using the insInstruction parameter
Step 2: Execute the Flash memory Function
Step 3: Return the Error Code
*******************************************************************************/
ReturnType Flash( InstructionType insInstruction, ParameterType *fp ) {
ReturnType rRetVal;
ST_uint8 ucManufacturerIdentification, ucStatusRegister;
ST_uint16 ucDeviceIdentification;
switch (insInstruction) {
case WriteEnable:
rRetVal = FlashWriteEnable( );
break;
case WriteDisable:
rRetVal = FlashWriteDisable( );
break;
case ReadDeviceIdentification:
rRetVal = FlashReadDeviceIdentification(&ucDeviceIdentification);
(*fp).ReadDeviceIdentification.ucDeviceIdentification = ucDeviceIdentification;
break;
case ReadManufacturerIdentification:
rRetVal = FlashReadManufacturerIdentification(&ucManufacturerIdentification);
(*fp).ReadManufacturerIdentification.ucManufacturerIdentification = ucManufacturerIdentification;
break;
case ReadStatusRegister:
rRetVal = FlashReadStatusRegister(&ucStatusRegister);
(*fp).ReadStatusRegister.ucStatusRegister = ucStatusRegister;
break;
case Read:
rRetVal = FlashRead( (*fp).Read.udAddr,
(*fp).Read.pArray,
(*fp).Read.udNrOfElementsToRead
);
break;
case FastRead:
rRetVal = FlashFastRead( (*fp).Read.udAddr,
(*fp).Read.pArray,
(*fp).Read.udNrOfElementsToRead
);
break;
case PageWrite:
rRetVal = FlashWrite( (*fp).PageWrite.udAddr,
(*fp).PageWrite.pArray,
(*fp).PageWrite.udNrOfElementsInArray
);
break;
case PageProgram:
rRetVal = FlashProgram( (*fp).PageProgram.udAddr,
(*fp).PageProgram.pArray,
(*fp).PageProgram.udNrOfElementsInArray
);
break;
case PageErase:
rRetVal = FlashPageErase( (*fp).PageErase.upgPageNr );
break;
case SectorErase:
rRetVal = FlashSectorErase((*fp).SectorErase.ustSectorNr );
break;
#if defined( USE_M25PE80 )
case BulkErase:
rRetVal = FlashBulkErase( );
break;
#endif
case DeepPowerDown:
rRetVal = FlashDeepPowerDown( );
break;
case ReleaseFromDeepPowerDown:
rRetVal = FlashReleaseFromDeepPowerDown( );
break;
case Write:
rRetVal = FlashWrite( (*fp).Write.udAddr,
(*fp).Write.pArray,
(*fp).Write.udNrOfElementsInArray);
break;
case Program:
rRetVal = FlashProgram( (*fp).Program.udAddr,
(*fp).Program.pArray,
(*fp).Program.udNrOfElementsInArray);
break;
#if defined( USE_M25PE80 )
case ReadLockRegister:
rRetVal = FlashReadLockRegister( (*fp).ReadLockRegister.udAddr,
&ucStatusRegister);
(*fp).ReadLockRegister.ucpLockRegister = ucStatusRegister;
break;
case WriteLockRegister:
rRetVal = FlashWriteLockRegister( (*fp).WriteLockRegister.udAddr,
(*fp).WriteLockRegister.ucLockRegister);
break;
#endif
default:
rRetVal = Flash_FunctionNotSupported;
break;
} /* EndSwitch */
return rRetVal;
} /* EndFunction Flash */
/*******************************************************************************
Function: FlashWriteEnable( void )
Arguments: void
Return Value:
Flash_Success
Description: This function sets the Write Enable Latch(WEL)
by sending a WREN instruction.
Pseudo Code:
Step 1: Initialize the data (i.e. instruction) packet to be sent serially
Step 2: Send the packet serially
*******************************************************************************/
ReturnType FlashWriteEnable( void )
{
CharStream char_stream_send;
ST_uint8 cWREN = SPI_FLASH_INS_WREN;
// Step 1: Initialize the data (i.e. instruction) packet to be sent serially
char_stream_send.length = 1;
char_stream_send.pChar = &cWREN;
// Step 2: Send the packet serially
Serialize(&char_stream_send,
ptrNull,
enumEnableTransOnly_SelectSlave,
enumDisableTransOnly_DeSelectSlave
);
return Flash_Success;
} /* EndFunction FlashWriteEnable */
/*******************************************************************************
Function: FlashWriteDisable( void )
Arguments: void
Return Value:
Flash_Success
Description: This function resets the Write Enable Latch(WEL)
by sending a WRDI instruction.
Pseudo Code:
Step 1: Initialize the data (i.e. instruction) packet to be sent serially
Step 2: Send the packet serially
*******************************************************************************/
ReturnType FlashWriteDisable( void )
{
CharStream char_stream_send;
ST_uint8 cWRDI = SPI_FLASH_INS_WRDI;
// Step 1: Initialize the data (i.e. instruction) packet to be sent serially
char_stream_send.length = 1;
char_stream_send.pChar = &cWRDI;
// Step 2: Send the packet serially
Serialize(&char_stream_send,
ptrNull,
enumEnableTransOnly_SelectSlave,
enumDisableTransOnly_DeSelectSlave
);
return Flash_Success;
} /* EndFunction FlashWriteDisable */
/*******************************************************************************
Function: FlashReadDeviceIdentification( ST_uint16 *uwpDeviceIdentification)
Arguments: uwpDeviceIdentification, 16-bit buffer to hold the DeviceIdentification read from the
memory, with the memory type residing in the higher 8-bit nibble, and the
memory capacity in the lower 8-bit nibble
Return Value:
Flash_Success
Flash_WrongType
Description: This function returns the Device Identification (memory type + memory capacity)
by sending an SPI_FLASH_INS_RDID instruction.
After retrieving the Device Identification, the routine checks if the device is
the expected device (defined by EXPECTED_DEVICE). If not,
Flash_WrongType is returned.
Pseudo Code:
Step 1: Initialize the data (i.e. instruction) packet to be sent serially
Step 2: Send the packet serially
Step 3: get returned Device Identification (memory type + memory capacity)
*******************************************************************************/
ReturnType FlashReadDeviceIdentification( ST_uint16 *uwpDeviceIdentification)
{
CharStream char_stream_send;
CharStream char_stream_recv;
ST_uint8 cRDID = SPI_FLASH_INS_RDID;
ST_uint8 pID[3];
// Step 1: Initialize the data (i.e. instruction) packet to be sent serially
char_stream_send.length = 1;
char_stream_send.pChar = &cRDID;
char_stream_recv.length = 3;
char_stream_recv.pChar = &pID[0];
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -