📄 norflash.c
字号:
/*****************************************************************************
* ex_norflash.c: External FLASH memory module file for NXP LPC23xx/24xx
* Family Microprocessors
*
* Copyright(C) 2006, NXP Semiconductor
* All rights reserved.
*
* History
* 2007.01.13 ver 1.00 Prelimnary version, first Release
*
******************************************************************************/
#include "LPC24xx.h" /* LPC23xx/24xx definitions */
#include "type.h"
#include "irq.h"
#include "target.h"
#include "timer.h"
#include "norflash.h"
/*****************************************************************************
** Function name: delay
**
** Descriptions: 延时
**
** parameters: delayCnt
**
** Returned value: None
**
*****************************************************************************/
void delay(DWORD delayCnt)
{
DWORD i;
for ( i = 0; i < delayCnt; i++ );
return;
}
/*****************************************************************************
** Function name: NORFLASHInit
**
** Descriptions: 初始化外部NOR FLASH存储器
**
** parameters: None
**
** Returned value: None
**
*****************************************************************************/
void NORFLASHInit( void )
{
/**************************************************************************
* 为NOR FLASH初始化EMC
**************************************************************************/
EMC_CTRL = 0x00000001;
PCONP |= 0x00000800; /* 打开 EMC PCLK */
PINSEL4 |= 0x50000000;
PINSEL5 |= 0x05050555;
PINSEL6 |= 0x55555555;
PINSEL8 |= 0x55555555;
PINSEL9 |= 0x50555555;
delayMs(1, 100); /* 利用定时器Timer 1 */
/* EMC_STA_CFG0 = 0x00000081;
EMC_STA_WAITWEN0 = 0x2;
EMC_STA_WAITOEN0 = 0x2;
EMC_STA_WAITRD0 = 0x1f;
EMC_STA_WAITPAGE0 = 0x1f;
EMC_STA_WAITWR0 = 0x1f;
EMC_STA_WAITTURN0 = 0xf;
*/
EMC_STA_CFG0 = 0x00000081;
EMC_STA_WAITWEN0 = 0x1;
EMC_STA_WAITOEN0 = 0x1;
EMC_STA_WAITRD0 = 0x1f;
EMC_STA_WAITPAGE0 = 0x1f;
EMC_STA_WAITWR0 = 0x1f;
EMC_STA_WAITTURN0 = 0xf;
delayMs(1, 10); /* 利用定时器Timer 1 */
return;
}
/*****************************************************************************
** Function name: ToggleBitCheck
**
** Descriptions: 如果数据有写过着擦出则触发改位检测
**
** parameters: Address and Expected data
**
** Returned value: done(TRUE) or timeout(FALSE)
**
*****************************************************************************/
DWORD ToggleBitCheck( DWORD Addr, WORD Data )
{
volatile WORD *ip;
WORD temp1, temp2;
DWORD TimeOut = PROGRAM_TIMEOUT;
while( TimeOut > 0 )
{
ip = GET_ADDR(Addr);
temp1 = *ip;
ip = GET_ADDR(Addr);
temp2 = *ip;
if ( (temp1 == temp2) && (temp1 == Data) )
{
return( TRUE );
}
TimeOut--;
}
return ( FALSE );
}
/*****************************************************************************
** Function name: NORFLASHCheckID
**
** Descriptions: 检查外部NOR FLASH存储器的ID号
** 具体参考相应NOR FLASH的数据手册
**
** parameters: None
**
** Returned value: Flash ID 读取不正确 False
**
*****************************************************************************/
DWORD NORFLASHCheckID( void )
{
volatile WORD *ip;
WORD SST_id1, SST_id2;
/* 确认对应AM29LV160的产品ID号 */
//autoselect mode (word)
ip = GET_ADDR(0x5555); //为什么是四个5?????????????
*ip = 0x00AA;
ip = GET_ADDR(0x2AAA);
*ip = 0x0055;
ip = GET_ADDR(0x5555);
*ip = 0x0090;
delay(10);
// ip=GET_ADDR(0x555);
// *ip=0x00AA;
// ip=GET_ADDR(0x2AA);
// *ip=0x0055;
// ip=GET_ADDR(0x555);
// *ip=0x0090;
// delay(10);
/* Read the product ID from 39VF160 */
/* 从AM29LV160读取产品ID号 */
ip = GET_ADDR(0x0000); //the fouth cycle is a read cycle
SST_id1 = *ip & 0x00FF;
ip = GET_ADDR(0x0001);
SST_id2 = *ip;
/* 确认AM29LV160的产品ID号,以便退出本函数 */
/* 进入读操作模式 */
/* ip = GET_ADDR(0x5555);
*ip = 0x00AA;
ip = GET_ADDR(0x2AAA);
*ip = 0x0055;
ip = GET_ADDR(0x5555);
*/
*ip = 0x00F0;
delay(10);
/* 校验 ID */
if ((SST_id1 == MANUFACTURER_ID) && (SST_id2 ==DEVICE_ID))
return( TRUE );
else
return( FALSE );
}
/*****************************************************************************
** Function name: NORFLASHErase
**
** Descriptions: 擦出外部NOR FLASH存储器
**
** parameters: None
**
** Returned value: None
**
*****************************************************************************/
void NORFLASHErase( void )
{
volatile WORD *ip;
ip = GET_ADDR(0x555);
*ip = 0x00AA;
ip = GET_ADDR(0x2AA);
*ip = 0x0055;
ip = GET_ADDR(0x555);
*ip = 0x0080;
ip = GET_ADDR(0x555);
*ip = 0x00AA;
ip = GET_ADDR(0x2AA);
*ip = 0x0055;
ip = GET_ADDR(0x555);
*ip = 0x0010;
delayMs(1, 30000); /* 利用定时器Timer 1 */
return;
}
void NORFLASHSectorErase(BYTE sector)
{
volatile WORD *ip;
if(sector >SECTOR_NUM_AMD) return ;
ip = GET_ADDR(0x555);
*ip = 0x00AA;
ip = GET_ADDR(0x2AA);
*ip = 0x0055;
ip = GET_ADDR(0x555);
*ip = 0x0080;
ip = GET_ADDR(0x555);
*ip = 0x00AA;
ip = GET_ADDR(0x2AA);
*ip = 0x0055;
ip = GET_ADDR(sector);
*ip = 0x0030;
delayMs(1, 2000); /* 利用定时器Timer 1 */
return;
}
/*****************************************************************************
** Function name: NORFLASHWriteWord
**
** Descriptions: 将一个16bit的数据写入外部NOR FLASH存储器
**
** parameters: DWORD address , WORD data
**
** Returned value: 写操作成功TRUE,失败FALSE
**
*****************************************************************************/
DWORD NORFLASHWriteWord( DWORD Addr, WORD Data )
{
volatile WORD *ip;
/*
ip = GET_ADDR(0x5555);
*ip = 0x00AA;
ip = GET_ADDR(0x2aaa);
*ip = 0x0055;
ip = GET_ADDR(0x5555);
*ip = 0x00A0;
*/
ip = GET_ADDR(0x555); //时序复用
*ip = 0x00AA;
ip = GET_ADDR(0x2aa);
*ip = 0x0055;
ip = GET_ADDR(0x555);
*ip = 0x00A0;
ip = GET_ADDR(Addr); /* 按照16bit字方式写 */
*ip = Data;
return ( ToggleBitCheck( Addr, Data ) );
}
/*********************************************************************************
** End Of File
*********************************************************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -