微波EDA网,见证研发工程师的成长!
首页 > 硬件设计 > 嵌入式设计 > NANDFlashd的读写(基于s3c2440)

NANDFlashd的读写(基于s3c2440)

时间:11-20 来源:互联网 点击:

  1. #ifndef__TEST_H__
  2. #define__TEST_H__
  3. #include"def.h"
  4. #defineMAX_NAND_BLOCK2048//一共2048块
  5. #defineNAND_PAGE_SIZE2048//每块main区2k字节=2048
  6. typedefstructnand_id_info//芯片的ID信息
  7. {
  8. U8IDm;//厂商ID
  9. U8IDd;//设备ID
  10. U8ID3rd;
  11. U8ID4th;
  12. U8ID5th;
  13. }nand_id_info;
  14. typedefstructbad_block_info//登记坏块
  15. {
  16. U8area[MAX_NAND_BLOCK];//0表示非坏块,1表示坏块
  17. U32sum;//坏块的总数
  18. }bad_block_info;
  19. //NAND操作指令
  20. #defineNAND_CMD_READ_1st0x00
  21. #defineNAND_CMD_READ_2st0x30
  22. #defineNAND_CMD_RANDOM_WRITE0x85//随机写
  23. #defineNAND_CMD_RANDOM_READ_1st0x05
  24. #defineNAND_CMD_RANDOM_READ_2st0xe0
  25. #defineNAND_CMD_READ_CB_1st0x00//将NAND里一块内容写进另一块
  26. #defineNAND_CMD_READ_CB_2st0x35
  27. #defineNAND_CMD_READ_ID0x90
  28. #defineNAND_CMD_RES0xff//复位命令
  29. #defineNAND_CMD_WRITE_PAGE_1st0x80
  30. #defineNAND_CMD_WRITE_PAGE_2st0x10
  31. #defineNAND_CMD_BLOCK_ERASE_1st0x60//擦除命令
  32. #defineNAND_CMD_BLOCK_ERASE_2st0xd0
  33. #defineNAND_CMD_READ_STATUS0x70
  34. //NAND中断向量
  35. #defineINT_NFCON24
  36. //NFCONFHCLK=100MHZ
  37. #defineS3C2440_NFCONF_TACLS_init(1<12)
  38. #defineS3C2440_NFCONF_TWRPH0_init(4<8)
  39. #defineS3C2440_NFCONF_TWRPH1_init(0<4)
  40. #defineS3C2440_NFCONF_BusWidth_init(0)
  41. #defineS3C2440_NFCONF_init()(rNFCONF=S3C2440_NFCONF_TACLS_init|/
  42. S3C2440_NFCONF_TWRPH0_init|/
  43. S3C2440_NFCONF_TWRPH1_init|/
  44. S3C2440_NFCONF_BusWidth_init)
  45. //NFCONT
  46. #defineS3C2440_NFCONT_LockTight_init(0<13)
  47. #defineS3C2440_NFCONT_SoftLock_init(0<12)
  48. #defineS3C2440_NFCONT_EnbIllegalAccINT_init(1<10)
  49. #defineS3C2440_NFCONT_EnbRnBINT_init(0<9)
  50. #defineS3C2440_NFCONT_RnB_TransMode_init(0<8)
  51. #defineS3C2440_NFCONT_SpareECCLock_init(1<6)
  52. #defineS3C2440_NFCONT_MainECCLock_init(1<5)
  53. #defineS3C2440_NFCONT_InitECC_init(1<4)
  54. #defineS3C2440_NFCONT_Reg_nCE_init(1<1)//初始配置片选无效
  55. #defineS3C2440_NFCONT_MODE_init(0)
  56. #defineS3C2440_NFCONT_init()(rNFCONT=S3C2440_NFCONT_LockTight_init|/
  57. S3C2440_NFCONT_SoftLock_init|/
  58. S3C2440_NFCONT_EnbIllegalAccINT_init|/
  59. S3C2440_NFCONT_EnbRnBINT_init|/
  60. S3C2440_NFCONT_RnB_TransMode_init|/
  61. S3C2440_NFCONT_SpareECCLock_init|/
  62. S3C2440_NFCONT_MainECCLock_init|/
  63. S3C2440_NFCONT_InitECC_init|/
  64. S3C2440_NFCONT_Reg_nCE_init|/
  65. S3C2440_NFCONT_MODE_init)
  66. //NFSTAT
  67. #defineS3C2440_NFSTAT_init()(rNFSTAT&=0x3)
  68. //NFESTAT0
  69. #defineS3C2440_NFESTAT0_init()(rNFESTAT0=0)
  70. //NFESTAT1
  71. #defineS3C2440_NFESTAT1_init()(rNFESTAT1=0)
  72. //
  73. #defineselect_nand()(rNFCONT&=~(1<1))
  74. #definedis_select_nand()(rNFCONT|=1<1)
  75. #definecontroller_enable()(rNFCONT|=1)
  76. #definecontroller_disable()(rNFCONT&=~1)
  77. //
  78. voidnand_flash_init(void);//初始化
  79. voidnand_read_id(void);
  80. externintnand_block_erase(U32num);//num要删除的块号
  81. externintnand_page_write(U32addr,U8*buffer,U32size);//addr要写的起始页地址,buffer要写的缓存,size要写的字节大小最大为4G
  82. externintnand_page_read(U32addr,U8*buffer,U32size);//addr开始页地址,从每页00地址开始读
  83. externintnand_random_read(U32paddr,U32offset,U8*data);//随机读数据paddr页地址,offset页内偏移地址
  84. externintnand_random_write(U32paddr,U32offset,U8data);//随机写,paddr页地址,offset页内区最后一个地偏移地址
  85. externvoidnand_test_bad_block(void);//测试坏块函数,并标记在nand_bbi变量里和spare里(如果非0xff则为坏块)
  86. #endif
  87. #include"2440addr.h"
  88. #include"test.h"
  89. #include"def.h"
  90. #defineNAND_DEBUG1//打印一些串口调试信息
  91. #defineUSE_ECC1//使用ECC验证
  92. nand_id_infonand_id;//定义登记芯片ID的全局变量
  93. bad_block_infonand_bbi;//定义来登记坏的全局变量
  94. voidinit_nand_bbi(void)//初始化变量
  95. {
  96. U32i;
  97. nand_bbi.sum=0;
  98. for(i=0;i
  99. nand_bbi.area[i]=0;//全部初始化为0
  100. }
  101. voidnand_mask_bad_block(U32n)//标志坏块,n是坏块的块号
  102. {
  103. #ifdefNAND_DEBUG
  104. Uart_Printf("NANDfoundandmaskabadblock=%d.",n);
  105. #endif
  106. if(nand_bbi.area[n]!=1)
  107. {
  108. nand_bbi.area[n]=1;
  109. nand_bbi.sum++;
  110. nand_random_write(n*64,2048+64-1,0);//每块的第一个spare的最后一个字节,标志本块是否为坏块,非0xff为坏块
  111. }
  112. }
  113. intdetect_nand_busy(void)//检测是否忙
  114. {
  115. U32a;
  116. a=0;
  117. while(!(rNFSTAT&(1<2)))
  118. {
  119. a++;
  120. if(a==5000000)//等待超时
  121. {
  122. Uart_Prin

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top