主要在继续在ARM9基础学习中的一文《3、NAND Flash控制器--s3c2440》,
通过其认识要s3c2440中的Nand Flash控制器,并对Nand Flash结构及操作原理
有了一些基本的了解,但只实现要了从Nand Flash控制器以及参数设置,接下将
完善Nand Flash读写及删除等功能。
过程先实现其最基本的nand_init()、nand_reset()、write_nand()、read_nand()、
erase_nand()等功能,并不涉及到K9F2G08UXA其它附加信息,如ECC校验码的相
关使用,然后再在此基础上将功能增加完全。
1、 nand_init()
设置NFCONF、NFCONT、NFSTAT寄存器相关可参考《3、NAND Flash控制器--s3c2440》
再复位nand flash。以下为一些宏设置。
- #define NF_MECC_UnLock() {NFCONT&=~(1<<5);}
- #define NF_MECC_Lock() {NFCONT|=(1<<5);}
- #define NF_CMD(cmd) {NFCMD=cmd;}
- #define NF_ADDR(addr) {NFADDR=addr;}
- #define NF_nFCE_L() {NFCONT&=~(1<<1);}
- #define NF_nFCE_H() {NFCONT|=(1<<1);}
- #define NF_RSTECC() {NFCONT|=(1<<4);}
- #define NF_RDDATA8() (NFDATA)
- #define NF_WAITRB() {while(!(NFSTAT&(1<<1)));}
- //wait tWB and check F_RNB pin.
- // RnB Signal
- #define NF_CLEAR_RB() {NFSTAT |= (1<<2);} // Have write '1' to clear this bit.
- #define NF_DETECT_RB() {while(!(NFSTAT&(1<<2)));}
- void nand_init(void) 初始化
- {
- NFCONF = (TACLS << 12) | (TWRPH0 << 8) | (TWRPH1 << 4) | (0 << 0);
- NFCONT =
- (0 << 13) | (0 << 12) | (0 << 10) | (0 << 9) | (0 << 8) | (0 << 6) |
- (0 << 5) | (1 << 4) | (1 << 1) | (1 << 0);
- NFSTAT = 0;
- nand_reset();
- }
2、 nand_reset() 芯片复位
- static inline void nand_reset(void)
- {
- NF_nFCE_L(); //片选nand flash芯片
- NF_CLEAR_RB();
- NF_CMD(0xFF); //reset command
- NF_DETECT_RB();
- NF_nFCE_H(); //disable nand flash chip select
- }
3、read_nand() 读数据
- void read_nand(char *buf, unsigned long addr, int size)
- {
- int i;
- nand_reset();
- NF_nFCE_L();
- NF_CLEAR_RB();
- NFCMD = 0x00;
- send_addr(addr);
- NFCMD = 0x30;
- while(!(NFSTAT & 0x1 ));
- //nandflash ready
- for(i = 0; i < (size); i++)
- {
- buf[i] = NFDATA;
- }
- NF_nFCE_H();
- Uart_SendString("read nand flash device finish\r\n");
- }
4、write_nand() 写数据
- void read_nand(char *buf, unsigned long addr, int size)
- {
- int i;
- nand_reset();
- NF_nFCE_L();
- NF_CLEAR_RB();
- NFCMD = 0x00;
- send_addr(addr);
- NFCMD = 0x30;
- while(!(NFSTAT & 0x1 ));
- //nandflash ready
- for(i = 0; i < (size); i++)
- {
- buf[i] = NFDATA;
- }
- NF_nFCE_H();
- Uart_SendString("read nand flash device finish\r\n");
- }
5、erase_nand()块擦删除
- void erase_nand(unsigned long addr)
- {
- unsigned int stat;
- nand_reset();
- NF_nFCE_L();
- //发送指令60h块擦除设置指令
- NFCMD = 0x60;
- //发送三次行地址
- NFADDR = (addr >> 11) & 0xff;
- NFADDR = (addr >> 19) & 0xff;
- NFADDR = (addr >> 27) & 0x1;
- //nandflash ready
- //发送d0擦除指令
- NFCMD = 0xd0;
- //等待忙状态
- while(!(NFSTAT & 0x1));
- // 发送70h读状态指令
- NFCMD = 0x70;
- //读取数据
- stat = NFDATA;
- //最后一位判断成功与否
- if(stat & 0x1){
- Uart_SendString("erase nand flash device err\r\n");
- NF_nFCE_H();
- return ;
- }
- Uart_SendString("erase nand flash device succes\r\n");
- NF_nFCE_H();
- }
6、Test_NAND_FLASH()测试以上函数
- void Test_NAND_FLASH()
- {
- int i;
- char buf[10];
- NandInit();
- for(i=0;i<10;i++)
- {
- buf[i] = 'd';
- }
- erase_nand(0x5000);
- write_nand(buf,0x5000,10);
- for(i=0;i<10;i++)
- {
- buf[i] = '0';
- }
- read_nand(buf,0x5000,10);
- for(i=0;i<10;i++)
- {
- printf(" %c ",buf[i]);
- }
- Uart_SendString("\r\n");
- erase_nand(0x5000);
- read_nand(buf,0x5000,10);
- for(i=0;i<10;i++)
- {
- printf(" %c ",buf[i]);
- }
- return ;
- }
7、测试结果
下载至Nand Flash起始处运行即可
all_ts.rar
阅读(1615) | 评论(0) | 转发(0) |