📄 sif_m25p64.c
字号:
/*
*********************************************************************************************************
* Copyright (c) 2005 vBridge Microsystem, Inc.
* Unpublished & Not for Publication
* All Rights Reserved
*
* File :
*
* Description :
*
* Date : Aug 11, 2005
*********************************************************************************************************
*/
#ifdef SIF_FLASH
#include "endian.h"
#include "stddefs.h"
#include "genlib.h"
#include "cpu.h"
#include "flash.h"
#include "Sif.h"
#include "Sif_M25p64.h"
/* Manufacturer and device id... */
#define INTEL_DT28F128J5 0x00890018
#define INTEL_28F640 0x00890017
#define INTEL_DT28F640J5 0x00890015
#define ST_M58LW064D 0x00200017
#define ST_M25P64 0x00202017
/*
*********************************************************************************************
* SifInit
*
* Description: Initialize the serial flash interface.
*
* Arguments :
*
* Return :
*
* Note(s) :
*********************************************************************************************
*/
void SflashSifInit(void)
{
while(SifBusyRead()); // If a transaction doesn't end,
// the initialization cannot be started.
SifTimingRegSet(SIF_SI_TIMING_RISING, SIF_SO_TIMING_RISING, SIF_CLK_POL_IDLE1, \
SIF_CS_WIDTH_OFF_NONE, SIF_CS_DLY_2CLK, SIF_CS_OFF_2CLK, \
SIF_GAP_NONE, SIF_BGAP_1BIT, 1 /* clk_div */);
SifModeRegSet(SIF_CBUS_MODE, SIF_CS_n0, SIF_CTL_SEL_8BIT, SIF_ADD_SEL_3BYTE, \
SIF_READ, 0 /* sID */, 4 /* blen */);
SifIntrDis(); // Disable the SIF interrupt.
}
/*
*********************************************************************************************
* SflashReadByte
*
* Description: Read byte from the serial flash.
*
* Arguments : start_addr is the start address.
* data is the data pointer.
* number is the number to be read. Counted by bytes.
*
* Return : none.
*
* Note(s) :
*********************************************************************************************
*/
void SflashReadByte(int start_addr, char *data, int number)
{
int i;
SflashWaitTillReady(); // Wait till the internal Program,
// Erasure, & Status Register Write complete
SifModeRegSet(SIF_CBUS_MODE, SIF_CS_n0, SIF_CTL_SEL_8BIT, SIF_ADD_SEL_3BYTE, \
SIF_READ, 0 /* sID */, 1/* blen */);
SifCommandSet(SIF_INSTR_READ); // Write the read instruction into the register.
SifAddrSet(start_addr); // Write the start address into the register.
// Do not need set the address every time,
// because the adress register will add by blen automatically.
for(i = 0; i < number; i++) {
SifStart(); // Start SPI operation.
while(SifBusyRead());
*data++ = SifDataRead();
}
}
/*
*********************************************************************************************
* SflashReadWord
*
* Description: Read 4-byte from the serial flash.
*
* Arguments : start_addr is the start address.
* data is the data pointer.
* number is the number to be read. Counted by words.
*
* Return : none.
*
* Note(s) : Add on Feb 21, do not test
*********************************************************************************************
*/
void SflashReadWord(int start_addr, int *data, int number)
{
int i;
SflashWaitTillReady(); // Wait till the internal Program,
// Erasure, & Status Register Write complete
SifModeRegSet(SIF_CBUS_MODE, SIF_CS_n0, SIF_CTL_SEL_8BIT, SIF_ADD_SEL_3BYTE, \
SIF_READ, 0 /* sID */, 4/* blen */);
SifCommandSet(SIF_INSTR_READ); // Write the read instruction into the register.
SifAddrSet(start_addr); // Write the start address into the register.
// Do not need set the address every time,
// because the adress register will add by blen automatically.
for(i = 0; i < number; i++) {
SifStart(); // Start SPI operation.
while(SifBusyRead());
*data++ = SifDataRead();
}
}
/*
*********************************************************************************************
* SflashReadStatus
*
* Description: Read the status registers.
*
* Arguments :
*
* Return : none.
*
* Note(s) :
*********************************************************************************************
*/
char SflashReadStatus(void)
{
int i;
while(SifBusyRead()); // If a transaction doesn't end, new one cannot be started.
SifModeRegSet(SIF_CBUS_MODE, SIF_CS_n0, SIF_CTL_SEL_8BIT, SIF_ADD_SEL_NONE, \
SIF_READ, 0 /* sID */, 1 /* blen */);
SifCommandSet(SIF_INSTR_RDSR); // Write the read instruction into the register.
SifStart(); // Start SPI operation.
while(SifBusyRead());
return(SifDataRead());
}
/*
*********************************************************************************************
* SflashProgByte
*
* Description: Program the serial flash.
*
* Arguments : start_addr is the start address.
* data is the data pointer
* number is the number to be programmed. Counted by bytes.
*
* Return : none.
*
* Note(s) : number does not have limited.
*********************************************************************************************
*/
void SflashProgByte(int start_addr, char *data, int number)
{
int i;
while(SifBusyRead()); // If a transaction doesn't end, new one cannot be started.
for(i = 0; i < number; i++) {
SflashWriteEn();
SifModeRegSet(SIF_CBUS_MODE, SIF_CS_n0, SIF_CTL_SEL_8BIT, SIF_ADD_SEL_3BYTE, \
SIF_WRITE, 0 /* sID */, 1 /* blen */);
SifCommandSet(SIF_INSTR_PP);
SifAddrSet(start_addr++);
SifDataWrite(*data++);
SifStart(); // Start SPI operation.
while(SifBusyRead());
}
}
/*
*********************************************************************************************
* SflashProgWord
*
* Description: Program the serial flash by word.
*
* Arguments : start_addr is the start address.
* data is the data pointer
* number is the number to be programmed. Counted by words.
*
* Return : none.
*
* Note(s) : number does not have limited.
*********************************************************************************************
*/
void SflashProgWord(int start_addr, int *data, int number)
{
int i;
while(SifBusyRead()); // If a transaction doesn't end, new one cannot be started.
for(i = 0; i < number; i++) {
SflashWriteEn();
SifModeRegSet(SIF_CBUS_MODE, SIF_CS_n0, SIF_CTL_SEL_8BIT, SIF_ADD_SEL_3BYTE, \
SIF_WRITE, 0 /* sID */, 4 /* blen */);
SifCommandSet(SIF_INSTR_PP);
SifAddrSet(start_addr);
start_addr += 4;
SifDataWrite(*data++);
SifStart(); // Start SPI operation.
while(SifBusyRead());
}
}
/*
*********************************************************************************************
* SflashBulkErase
*
* Description: Erase the whole chip.
*
* Arguments : none.
*
* Return : none.
*
* Note(s) :
*********************************************************************************************
*/
void SflashBulkErase(void)
{
SflashWriteEn();
SifModeRegSet(SIF_CBUS_MODE, SIF_CS_n0, SIF_CTL_SEL_8BIT, SIF_ADD_SEL_NONE, \
SIF_WRITE, 0 /* sID */, 0 /* blen */);
SifCommandSet(SIF_INSTR_BE);
SifStart(); // Start SPI operation.
}
/*
*********************************************************************************************
* SflashWriteEn
*
* Description: set the Write Enable Latch bit.
*
* Arguments : none.
*
* Return : none.
*
* Note(s) : The Write Enable Latch (WEL) bit must be set prior to
* every Page Program (PP), Sector Erase(SE), Bulk Erase (BE) and
* Write Status Register(WRSR) instruction.
*********************************************************************************************
*/
void SflashWriteEn(void)
{
SflashWaitTillReady();
SifModeRegSet(SIF_CBUS_MODE, SIF_CS_n0, SIF_CTL_SEL_8BIT, SIF_ADD_SEL_NONE, \
SIF_WRITE, 0 /* sID */, 0 /* blen */);
SifCommandSet(SIF_INSTR_WREN);
SifStart(); // Start SPI operation.
while(!(SflashReadStatus() & 0x2)); // Wait till the WEL bit is set.
}
/*
*********************************************************************************************
* SflashWaitTillReady
*
* Description: wait till the serial flash is ready.
*
* Arguments : none.
*
* Return : none.
*
* Note(s) :
*********************************************************************************************
*/
void SflashWaitTillReady(void)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -