📄 m25p64.c
字号:
/*
***************************************************************************
* Copyright (c) 2005 vBridge Microsystem, Inc.
* Unpublished & Not for Publication
* All Rights Reserved
*
* File :
*
* Description :
*
* Date : Aug 11, 2005
***************************************************************************
*/
#include "config.h"
#ifdef INCLUDE_SIF_FLASH
#ifdef INCLUDE_FLASH
#include "sif.h"
#include "m25p64.h"
#include "cli.h"
#include "boot.h"
#include "bios.h"
#include "flash.h"
#define INTEL_DT28F128J5 0x00890018
#define INTEL_28F640 0x00890017
#define INTEL_DT28F640J5 0x00890015
#define ST_M58LW064D 0x00200017
#define ST_M25P64 0x00172020
unsigned char printf (char *pcFmt,...);
typedef unsigned char uchar;
//char WaitPressKey(void);
/*
***************************************************************************
* 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_SI_TIMING_FALLING, SIF_CLK_POL_IDLE1, \
SIF_CS_WIDTH_OFF_NONE, SIF_CS_DLY_2CLK, SIF_CS_OFF_2CLK, \
SIF_GAP_NONE, SIF_BGAP_1BIT, 5 /* clk_div */);
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
*********************************************************************************************
*/
void SflashReadWord(int start_addr, int *data, int number)
{
int i,step,process;
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.
// step = number/100;
// process = step;
for(i = 0; i < number; i++) {
SifStart(); // Start SPI operation.
while(SifBusyRead());
*data++ = SifDataRead();
/*
if(i=process)
{
printf(".");
process += step;
}*/
}
}
/*
*********************************************************************************************
* 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());
}
/*
*********************************************************************************************
* SflashReadID
*
* Description: Read byte from the serial flash.
*
* Arguments :
*
* Return : none.
*
* Note(s) :
*********************************************************************************************
*/
int SflashReadID(void)
{
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_NONE, \
SIF_READ, 0 /* sID */, 3/* blen */);
SifCommandSet(SIF_INSTR_RDID); // 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,step,process;
step = number/100;
process = 0;
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());
if(i==process)
{
putchar('=');
process += step;
}
}
}
/*
*********************************************************************************************
* SflashSecErase
*
* Description: Erase a specific sector.
*
* Arguments : sector_no is the sector number to be erased.
*
* Return : none.
*
* Note(s) :
*********************************************************************************************
*/
void SflashSecErase(int sector_no)
{
SflashWriteEn();
SifModeRegSet(SIF_CBUS_MODE, SIF_CS_n0, SIF_CTL_SEL_8BIT, SIF_ADD_SEL_3BYTE, \
SIF_WRITE, 0 /* sID */, 0 /* blen */);
SifCommandSet(SIF_INSTR_SE);
SifAddrSet(sector_no * SIF_PAGES_PER_SECTOR * SIF_PAGE_SIZE); // Write the start address into the register.
SifStart(); // Start SPI operation.
}
/*
*********************************************************************************************
* 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
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -