首页 | 新闻 | 新品 | 文库 | 方案 | 视频 | 下载 | 商城 | 开发板 | 数据中心 | 座谈新版 | 培训 | 工具 | 博客 | 论坛 | 百科 | GEC | 活动 | 主题月 | 电子展
返回列表 回复 发帖

STM32使用FSMC控制NAND flash 例程(2)

STM32使用FSMC控制NAND flash 例程(2)

  • void NAND_ReadID(NAND_IDTypeDef* NAND_ID)
  • {
  •   uint32_t data = 0;
  •   /*!< Send Command to the command area */
  •   *(__IO uint8_t *)(Bank_NAND_ADDR | CMD_AREA) = 0x90;
  •   *(__IO uint8_t *)(Bank_NAND_ADDR | ADDR_AREA) = 0x00;
  •    /*!< Sequence to read ID from NAND flash */
  •    data = *(__IO uint32_t *)(Bank_NAND_ADDR | DATA_AREA);
  •    NAND_ID->Maker_ID   = ADDR_1st_CYCLE (data);//四个周期读取四个ID
  •    NAND_ID->Device_ID  = ADDR_2nd_CYCLE (data);
  •    NAND_ID->Third_ID   = ADDR_3rd_CYCLE (data);
  •    NAND_ID->Fourth_ID  = ADDR_4th_CYCLE (data);
  • }

复制代码
  • uint32_t NAND_WriteSmallPage(uint8_t *pBuffer, NAND_ADDRESS Address, uint32_t NumPageToWrite)
  • {//传入参数:写入数据,写入初始地址,要写几页
  •   uint32_t index = 0x00, numpagewritten = 0x00, addressstatus = NAND_VALID_ADDRESS;
  •   uint32_t status = NAND_READY, size = 0x00;
  •   while((NumPageToWrite != 0x00) && (addressstatus == NAND_VALID_ADDRESS) && (status == NAND_READY))
  •   {
  •     /*!< Page write command and address */
  •     *(__IO uint8_t *)(Bank_NAND_ADDR | CMD_AREA) = NAND_CMD_AREA_A;
  •     *(__IO uint8_t *)(Bank_NAND_ADDR | CMD_AREA) = NAND_CMD_WRITE0;
  •     *(__IO uint8_t *)(Bank_NAND_ADDR | ADDR_AREA) = 0x00;
  •     *(__IO uint8_t *)(Bank_NAND_ADDR | ADDR_AREA) = 0x00;//添加此句
  •     *(__IO uint8_t *)(Bank_NAND_ADDR | ADDR_AREA) = ADDR_1st_CYCLE(ROW_ADDRESS);
  •     *(__IO uint8_t *)(Bank_NAND_ADDR | ADDR_AREA) = ADDR_2nd_CYCLE(ROW_ADDRESS);
  • //    *(__IO uint8_t *)(Bank_NAND_ADDR | ADDR_AREA) = ADDR_3rd_CYCLE(ROW_ADDRESS);//原版有此句
  •     /*!< Calculate the size */
  •     size = NAND_PAGE_SIZE + (NAND_PAGE_SIZE * numpagewritten);//统计写入数目
  •     /*!< Write data */
  •     for(; index < size; index++)
  •     {
  •       *(__IO uint8_t *)(Bank_NAND_ADDR | DATA_AREA) = pBuffer[index];
  •     }
  •     *(__IO uint8_t *)(Bank_NAND_ADDR | CMD_AREA) = NAND_CMD_WRITE_TRUE1;
  •     /*!< Check status for successful operation */
  •     status = NAND_GetStatus();
  •     if(status == NAND_READY)
  •     {
  •       numpagewritten++;
  •       NumPageToWrite--;
  •       /*!< Calculate Next small page Address */
  •       addressstatus = NAND_AddressIncrement(&Address);
  •     }
  •   }
  •   return (status | addressstatus);
  • }

复制代码

读取函数同理修改
  • uint32_t NAND_EraseBlock(NAND_ADDRESS Address)
  • {
  •   *(__IO uint8_t *)(Bank_NAND_ADDR | CMD_AREA) = NAND_CMD_ERASE0;
  •   *(__IO uint8_t *)(Bank_NAND_ADDR | ADDR_AREA) = ADDR_1st_CYCLE(ROW_ADDRESS);
  •   *(__IO uint8_t *)(Bank_NAND_ADDR | ADDR_AREA) = ADDR_2nd_CYCLE(ROW_ADDRESS);
  • //  *(__IO uint8_t *)(Bank_NAND_ADDR | ADDR_AREA) = ADDR_3rd_CYCLE(ROW_ADDRESS);//两次即可
  •   *(__IO uint8_t *)(Bank_NAND_ADDR | CMD_AREA) = NAND_CMD_ERASE1;
  •   return (NAND_GetStatus());
  • }

复制代码

fsmc_nand.h文件:
  • #define NAND_PAGE_SIZE             ((uint16_t)0x0800) /* 512 bytes per page w/o Spare Area *///每页2K
  • #define NAND_BLOCK_SIZE            ((uint16_t)0x0040) /* 32x512 bytes pages per block *///64个页
  • #define NAND_ZONE_SIZE             ((uint16_t)0x0400) /* 1024 Block per zone *///1024个快
  • #define NAND_SPARE_AREA_SIZE       ((uint16_t)0x0040) /* last 16 bytes as spare area */
  • #define NAND_MAX_ZONE              ((uint16_t)0x0001) /* 4 zones of 1024 block */

复制代码

修改完即可实现512B至2K每页的变更
继承事业,薪火相传
返回列表