📄 m25p64.c
字号:
*
* 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)
{
while(SflashReadStatus() & 0x1); // Wait till the internal erase,
// program or Status Write ends.
}
/*
*********************************************************************************************
* Following functions will be used in serial testing!
*********************************************************************************************
*/
/*
*********************************************************************************************
* SflashProgData
*
* Description: Program the whole serial flash with the same word.
*
* Arguments : prog_data is the program data, 4-byte.
*
* Return : none.
*
* Note(s) : The routine is OK under revised 20, but NOT supported in image 33
*********************************************************************************************
*/
void SflashProgData(int prog_data)
{
int i,j;
int addr;
addr = 0;
while(SifBusyRead()); // If a transaction doesn't end, new one cannot be started.
printf("Sector ");
for(i = 0; i < SIF_SECTOR_NUMBER * SIF_PAGES_PER_SECTOR; i++) {
if(!(i & (SIF_PAGES_PER_SECTOR - 1)))
printf("%d, ", i/SIF_PAGES_PER_SECTOR);
for (j=0; j < SIF_PAGE_SIZE/SIF_MAX_ONCE_PROGRAM_BYTES; j++){
SflashWriteEn();
SifModeRegSet(SIF_CBUS_MODE, SIF_CS_n0, SIF_CTL_SEL_8BIT, SIF_ADD_SEL_3BYTE, \
SIF_WRITE, 0 /* sID */, SIF_MAX_ONCE_PROGRAM_BYTES/* blen */);
SifCommandSet(SIF_INSTR_PP);
SifAddrSet(addr);
addr += SIF_MAX_ONCE_PROGRAM_BYTES;
SifDataWrite(prog_data);
SifStart(); // Start SPI operation.
while(SifBusyRead());
}
}
printf("complete!\r\n");
}
/*
*********************************************************************************************
* SflashSectorVerify
*
* Description: Read the word from the sector one by one, compare with verify_data.
*
* Arguments : no_sector is the sector number.
* verify_data is the 4-byte compared.
*
* Return : SUCCESSFUL: all the byte are equal to verify_data.
* FAILED: at least one byte is not qeual to verify_data.
*
* Note(s) :
*********************************************************************************************
*/
int SflashSectorVerify(int no_sector, int verify_data)
{
int i, tmp;
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(no_sector * SIF_PAGES_PER_SECTOR * SIF_PAGE_SIZE);
for(i = 0; i < SIF_PAGES_PER_SECTOR * SIF_PAGE_SIZE; i = i + 4) {
SifStart(); // Start SPI operation.
while(SifBusyRead());
tmp = SifDataRead();
if (tmp != verify_data) {
printf("%x\r\n", tmp);
printf("%x\r\n", verify_data);
// WaitPressKey();
//return FAILED;
}
}
return SUCCESSFUL;
}
/*
*********************************************************************************************************
* TestSflashProgBiosBoot
*
* Description :
*
* Arguments : data a null pointer is passed to it when AppTaskStart starts excuting .
*
* Returns : None.
*
* Notes :
*********************************************************************************************************
*/
void SflashProgBiosBoot(void)
{
int k;
int *tmp, read_data[BIOS_SIZE], read_data1[BOOT_SIZE];
printf("\r\nProg bios & boot of the serial flash!\r\n");
SflashSifInit();
/* Sector 0 erase */
printf("\r\nSector 0 Erasing...\r\n");
SflashSecErase(0);
SflashWaitTillReady();
printf("Verifying...");
if(SflashSectorVerify(0, 0xffffffff))
printf("OK!\r\n");
else {
printf("failed!\r\n");
}
/* Prog the bios */
printf("Prog the bios of serial flash\r\n");
SflashProgWord(0 /* start_addr */, bios, BIOS_SIZE /* number */);
/* Verify */
// Verify bios area
printf("\r\nVerify bios area!\r\n");
SflashReadWord(0, read_data, BIOS_SIZE);
tmp = bios;
for(k = 0; k < BIOS_SIZE; k++) {
if(*tmp++ != read_data[k]) {
break;
} else
printf("No. %d: 0x%x, ", k, read_data[k]);
}
if(k == BIOS_SIZE)
printf("\r\nBios verify OK!\r\n");
else
printf("\r\nBios verify failed!\r\n");
/* Prog the boot */
printf("\r\nProg the boot of serial flash\r\n");
SflashProgWord(0x400 /* start_addr */, boot, BOOT_SIZE /* number */);
for(k=0;k<110;k++)
printf("boot'%d'] = 0x%x,",k,boot[k]);
/* Verify */
// Verify boot area
printf("\r\nVerify boot area!\r\n");
SflashReadWord(0x400, read_data1, BOOT_SIZE);
tmp = boot;
for(k = 0; k < BOOT_SIZE; k++) {
if(*tmp++ != read_data1[k]) {
printf("error data: 0x%x,right data is: 0x%x",read_data1[k],*tmp);
break;
} else
printf("No. %d: 0x%x, ", k, read_data1[k]);
}
if(k == BOOT_SIZE)
printf("\r\nBoot verify OK!\r\n");
else
printf("\r\nBoot verify failed!\r\n");
}
/*
*********************************************************************************************
* SflashProgSectorWord
*
* Description: Program the specified sector with the same word.
*
* Arguments : sector_no is the sector number.
* prog_data is the program data, 4-byte.
*
* Return : none.
*
* Note(s) : add on Feb 21
*********************************************************************************************
*/
void SflashProgSectorWord(int sector_no, int prog_data)
{
int i, addr;
addr = sector_no * SIF_BYTES_PER_SECTOR;
while(SifBusyRead()); // If a transaction doesn't end, new one cannot be started.
//printf("Sector ");
for(i = 0; i < SIF_PAGES_PER_SECTOR; i++) {
SflashWriteEn();
SifModeRegSet(SIF_CBUS_MODE, SIF_CS_n0, SIF_CTL_SEL_8BIT, SIF_ADD_SEL_3BYTE, \
SIF_WRITE, 0 /* sID */, SIF_PAGE_SIZE /* blen */);
SifCommandSet(SIF_INSTR_PP);
SifAddrSet(addr);
addr += SIF_PAGE_SIZE;
SifDataWrite(prog_data);
SifStart(); // Start SPI operation.
while(SifBusyRead());
}
//printf("complete!\r\n");
}
char *SflashHelp[] = {
"Sflash memory operations",
"{op} [args]",
"Ops...",
" erase rnge",
" id",
" write {dest} {src} {byte_cnt}",
" read {dest} {src} {byte_cnt}",
"",
" rnge = range of affected sectors",
" Range syntax examples: <1> <1-5> <1,3,7> <all>",
0,
};
/* Sflash():
* Code that handles the user interface. See SflashHelp[] below for usage.
*/
int
Sflash(int argc,char *argv[])
{
int snum, ret,rslt,i;
long dest,src, bytecnt;
if(argc<2)
return CMD_PARAM_ERROR;
if (strcmp(argv[1],"erase") == 0)
{
if (argc != 3) {
ret = CMD_PARAM_ERROR;
}
else {
for(snum=0;snum<SIF_SECTOR_NUMBER;snum++) {
if ((argv[2] == 0) || inRange(argv[2],snum)) {
printf("erase sector %d...\n",snum);
ticktock();
SflashSecErase(snum);
SflashWaitTillReady();
}
}
}
}
else if (strcmp(argv[1],"id") == 0) {
i = SflashReadID();
printf("Serial flash ID = 0x%x\n",i);
if (i == (M_ID | (MEM_TYPE<<8) | (MEM_CAP<<16)))
printf("It's ST serial flash, type is M25P64.\n");
else
printf("Unknown serial flash device.\n");
}
else if (strcmp(argv[1],"write") == 0) {
if (argc == 5) {
dest = strtoul(argv[2],(char **)0,0);
src = strtoul(argv[3],(char **)0,0);
bytecnt = (long)strtoul(argv[4],(char **)0,0);
SflashProgWord(dest, src, bytecnt/4+1);
SflashWaitTillReady();
printf("write sucessful\n");
}
else
ret = CMD_PARAM_ERROR;
}
else if (!strcmp(argv[1],"read")) {
if (argc == 5) {
dest = strtoul(argv[2],(char **)0,0);
src = strtoul(argv[3],(char **)0,0);
bytecnt = (long)strtoul(argv[4],(char **)0,0);
SflashReadWord(src, dest, bytecnt/4+1);
}
else
ret = CMD_PARAM_ERROR;
}
else if (!strcmp(argv[1],"bios")) {
SflashProgBiosBoot();
}
return(ret);
}
/* STM25p64_8x1_erase():
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -