summaryrefslogtreecommitdiff
path: root/components/dfs/filesystems/uffs/src/example
diff options
context:
space:
mode:
author[email protected] <[email protected]>2011-01-16 13:30:54 +0000
committer[email protected] <[email protected]>2011-01-16 13:30:54 +0000
commit0030a2d396187a9fb9e665fa2f535eeeedd601a5 (patch)
treeebf490f8ea93620a60217ed14d9f8c7d52d2afa1 /components/dfs/filesystems/uffs/src/example
parentdede5414decc94bcdf8730c44a855be042fadbde (diff)
add support uffs interface
git-svn-id: https://rt-thread.googlecode.com/svn/trunk@1243 bbd45198-f89e-11dd-88c7-29a3b14d5316
Diffstat (limited to 'components/dfs/filesystems/uffs/src/example')
-rw-r--r--components/dfs/filesystems/uffs/src/example/CMakeLists.txt24
-rw-r--r--components/dfs/filesystems/uffs/src/example/flash-interface-example.c712
-rw-r--r--components/dfs/filesystems/uffs/src/example/static-mem-allocate.c316
3 files changed, 569 insertions, 483 deletions
diff --git a/components/dfs/filesystems/uffs/src/example/CMakeLists.txt b/components/dfs/filesystems/uffs/src/example/CMakeLists.txt
index 24e9aecfba..e60bf636d0 100644
--- a/components/dfs/filesystems/uffs/src/example/CMakeLists.txt
+++ b/components/dfs/filesystems/uffs/src/example/CMakeLists.txt
@@ -1,12 +1,12 @@
-INCLUDE_DIRECTORIES(${uffs_SOURCE_DIR}/src/inc)
-INCLUDE_DIRECTORIES(${uffs_SOURCE_DIR}/src/emu)
-
-LINK_DIRECTORIES(${uffs_BINARY_DIR}/src/emu)
-LINK_DIRECTORIES(${uffs_BINARY_DIR}/src/uffs)
-
-SET(static_mem_SRCS static-mem-allocate.c)
-SET(flash_if_SRCS flash-interface-example.c)
-ADD_EXECUTABLE(static-mem-example ${static_mem_SRCS})
-ADD_EXECUTABLE(flash-if-example ${flash_if_SRCS})
-TARGET_LINK_LIBRARIES(static-mem-example emu uffs emu)
-TARGET_LINK_LIBRARIES(flash-if-example emu uffs emu)
+INCLUDE_DIRECTORIES(${uffs_SOURCE_DIR}/src/inc)
+INCLUDE_DIRECTORIES(${uffs_SOURCE_DIR}/src/emu)
+
+LINK_DIRECTORIES(${uffs_BINARY_DIR}/src/emu)
+LINK_DIRECTORIES(${uffs_BINARY_DIR}/src/uffs)
+
+SET(static_mem_SRCS static-mem-allocate.c)
+SET(flash_if_SRCS flash-interface-example.c)
+ADD_EXECUTABLE(static-mem-example ${static_mem_SRCS})
+ADD_EXECUTABLE(flash-if-example ${flash_if_SRCS})
+TARGET_LINK_LIBRARIES(static-mem-example emu uffs emu)
+TARGET_LINK_LIBRARIES(flash-if-example emu uffs emu)
diff --git a/components/dfs/filesystems/uffs/src/example/flash-interface-example.c b/components/dfs/filesystems/uffs/src/example/flash-interface-example.c
index b7427e2921..7130f6fc5a 100644
--- a/components/dfs/filesystems/uffs/src/example/flash-interface-example.c
+++ b/components/dfs/filesystems/uffs/src/example/flash-interface-example.c
@@ -1,310 +1,402 @@
-/*
- This file is part of UFFS, the Ultra-low-cost Flash File System.
-
- Copyright (C) 2005-2009 Ricky Zheng <[email protected]>
-
- UFFS is free software; you can redistribute it and/or modify it under
- the GNU Library General Public License as published by the Free Software
- Foundation; either version 2 of the License, or (at your option) any
- later version.
-
- UFFS is distributed in the hope that it will be useful, but WITHOUT
- ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
- FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
- or GNU Library General Public License, as applicable, for more details.
-
- You should have received a copy of the GNU General Public License
- and GNU Library General Public License along with UFFS; if not, write
- to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
- Boston, MA 02110-1301, USA.
-
- As a special exception, if other files instantiate templates or use
- macros or inline functions from this file, or you compile this file
- and link it with other works to produce a work based on this file,
- this file does not by itself cause the resulting work to be covered
- by the GNU General Public License. However the source code for this
- file must still be made available in accordance with section (3) of
- the GNU General Public License v2.my_application_main_entry
-
- This exception does not invalidate any other reasons why a work based
- on this file might be covered by the GNU General Public License.
-*/
-/**
- * \file flash-interface-example.c
- * \brief example for using flash driver and multiple partitions, with static memory allocator.
- * \author Ricky Zheng, created at 27 Nov, 2007
- */
-
-#include <string.h>
-
-#include "uffs/uffs_device.h"
-#include "uffs/uffs_flash.h"
-#include "uffs/uffs_mtb.h"
-#include "uffs/uffs_fs.h"
-
-#define PFX "nand-drv:"
-
-#if CONFIG_USE_STATIC_MEMORY_ALLOCATOR == 0
-int main()
-{
- uffs_Perror(UFFS_ERR_NORMAL, "This example need CONFIG_USE_STATIC_MEMORY_ALLOCATOR = 1");
- return 0;
-}
-#else
-
-
-#define USE_SINGLE_WRITE_FUN
-
-
-#ifdef USE_SINGLE_WRITE_FUN
-static int nand_write_full_page(uffs_Device *dev, u32 block, u32 pageNum, const u8 *page, int len, const u8 *tag, int tag_len, const u8 *ecc);
-#else
-static int nand_write_page_data(uffs_Device *dev, u32 block, u32 pageNum, const u8 *page, int len, u8 *ecc);
-static int nand_write_page_spare(uffs_Device *dev, u32 block, u32 pageNum, const u8 *spare, int ofs, int len, UBOOL eod);
-#endif
-
-static int nand_read_page_data(uffs_Device *dev, u32 block, u32 pageNum, u8 *page, int len, u8 *ecc);
-static int nand_read_page_spare(uffs_Device *dev, u32 block, u32 pageNum, u8 *spare, int ofs, int len);
-
-static int nand_erase_block(uffs_Device *dev, u32 blockNumber);
-
-static URET nand_init_device(uffs_Device *dev);
-
-
-#ifdef USE_SINGLE_WRITE_FUN
-// if you want to optimize nand flash driver, or use special nand hardware controller,
-// or use other NAND driver (for example, eCos NAND lib), you shoud do layout in nand driver.
-static int nand_write_full_page(uffs_Device *dev, u32 block, u32 pageNum, const u8 *page, int len, const u8 *tag, int tag_len, const u8 *ecc)
-{
-#define SPOOL(dev) &((dev)->mem.spare_pool)
-
- u8 *spare_buf = NULL;
-
- spare_buf = (u8 *) uffs_PoolGet(SPOOL(dev)); // alloc a spare buffer
-
- // ... START WRITE COMMAND ...
- // ...
-
- if (page) {
- // WRITE page data
- // ....
- if (dev->attr->ecc_opt == UFFS_ECC_HW) {
- // read ECC from hardware controller to ecc buf,
- // ...
- }
- }
-
- if (tag && tag_len > 0) {
-
- // now, you can use UFFS's layout function
- uffs_FlashMakeSpare(dev, (uffs_TagStore *)tag, ecc, spare_buf);
- // or, do your own layout
- // ....
-
- // WRITE spare_buf to page spare ...
- // ...
- }
-
- // FINISH write command ...
- // ...
- // read program status ...
- // ...
-
- if (page)
- dev->st.page_write_count++;
- if (tag)
- dev->st.spare_write_count++;
-
- if (spare_buf)
- uffs_PoolPut(SPOOL(dev), spare_buf); // release spare buffer
-
- return UFFS_FLASH_NO_ERR;
-}
-
-#else
-
-static int nand_write_page_data(uffs_Device *dev, u32 block, u32 pageNum, const u8 *page, int len, u8 *ecc)
-{
- // send WRITE command
-
- // ... transfer data ...
-
- dev->st.page_write_count++;
- return UFFS_FLASH_NO_ERR;
-}
-
-
-static int nand_write_page_spare(uffs_Device *dev, u32 block, u32 pageNum, const u8 *spare, int ofs, int len, UBOOL eod)
-{
- if (eod == U_FALSE) {
- // send WRITE command
- }
- else {
- // do not need to send WRITE command if eod == U_FALSE because 'nand_write_page_data' is called before.
- }
-
- // ... transfer data ...
-
- // send COMMIT command
-
- // read STATUS
-
- dev->st.spare_write_count++;
- return UFFS_FLASH_NO_ERR;
-}
-
-#endif
-
-
-static int nand_read_page_data(uffs_Device *dev, u32 block, u32 pageNum, u8 *page, int len, u8 *ecc)
-{
- // send READ command
-
- // ... transfer data ...
-
- // read STATUS
-
- dev->st.page_read_count++;
- return UFFS_FLASH_NO_ERR;
-}
-
-static int nand_read_page_spare(uffs_Device *dev, u32 block, u32 pageNum, u8 *spare, int ofs, int len)
-{
- // send READ command
-
- // ... transfer data ...
-
- // read STATUS
-
- dev->st.spare_read_count++;
- return UFFS_FLASH_NO_ERR;
-}
-
-
-static int nand_erase_block(uffs_Device *dev, u32 blockNumber)
-{
- // insert your nand driver codes here ...
-
- dev->st.block_erase_count++;
- return UFFS_FLASH_NO_ERR;
-}
-
-
-/////////////////////////////////////////////////////////////////////////////////
-
-static struct uffs_FlashOpsSt my_nand_driver_ops = {
- nand_read_page_data, //ReadPageData
- nand_read_page_spare, //ReadPageSpare
- NULL, //ReadPageSpareWithLayout
-#ifdef USE_SINGLE_WRITE_FUN
- NULL,
- NULL,
- nand_write_full_page, //WriteFullPages
-#else
- nand_write_page_data, //WritePageData
- nand_write_page_spare, //WritePageSpare
- NULL,
-#endif
- NULL, //IsBadBlock
- NULL, //MarkBadBlock
- nand_erase_block, //EraseBlock
-};
-
-// change these parameters to fit your nand flash specification
-#define MAN_ID MAN_ID_SAMSUNG // simulate Samsung's NAND flash
-
-#define TOTAL_BLOCKS 1024
-#define PAGE_DATA_SIZE 512
-#define PAGE_SPARE_SIZE 16
-#define PAGES_PER_BLOCK 32
-#define PAGE_SIZE (PAGE_DATA_SIZE + PAGE_SPARE_SIZE)
-#define BLOCK_DATA_SIZE (PAGE_DATA_SIZE * PAGES_PER_BLOCK)
-
-#define NR_PARTITION 2 /* total partitions */
-#define PAR_1_BLOCKS 100 /* partition 1 */
-#define PAR_2_BLOCKS (TOTAL_BLOCKS - PAR_1_BLOCKS) /* partition 2 */
-
-static struct uffs_StorageAttrSt flash_storage = {0};
-
-/* static alloc the memory for each partition */
-
-static int static_buffer_par1[UFFS_STATIC_BUFF_SIZE(PAGES_PER_BLOCK, PAGE_SIZE, PAR_1_BLOCKS) / sizeof(int)];
-static int static_buffer_par2[UFFS_STATIC_BUFF_SIZE(PAGES_PER_BLOCK, PAGE_SIZE, PAR_2_BLOCKS) / sizeof(int)];;
-
-
-static void setup_flash_storage(struct uffs_StorageAttrSt *attr)
-{
- memset(attr, 0, sizeof(struct uffs_StorageAttrSt));
-
- attr->total_blocks = TOTAL_BLOCKS; /* total blocks */
- attr->page_data_size = PAGE_DATA_SIZE; /* page data size */
- attr->pages_per_block = PAGES_PER_BLOCK; /* pages per block */
- attr->spare_size = PAGE_SPARE_SIZE; /* page spare size */
- attr->block_status_offs = 4; /* block status offset is 5th byte in spare */
- attr->ecc_opt = UFFS_ECC_SOFT; /* ecc option */
- attr->layout_opt = UFFS_LAYOUT_UFFS; /* let UFFS do the spare layout */
-}
-
-
-static URET my_initDevice(uffs_Device *dev)
-{
- dev->ops = &my_nand_driver_ops;
-
- return U_SUCC;
-}
-
-static URET my_releaseDevice(uffs_Device *dev)
-{
- return U_SUCC;
-}
-
-/* define mount table */
-static uffs_Device demo_device_1 = {0};
-static uffs_Device demo_device_2 = {0};
-
-static uffs_MountTable demo_mount_table[] = {
- { &demo_device_1, 0, PAR_1_BLOCKS - 1, "/data/" },
- { &demo_device_2, PAR_1_BLOCKS, PAR_1_BLOCKS + PAR_2_BLOCKS - 1, "/" },
- { NULL, 0, 0, NULL }
-};
-
-static int my_init_filesystem(void)
-{
- uffs_MountTable *mtbl = &(demo_mount_table[0]);
-
- /* setup nand storage attributes */
- setup_flash_storage(&flash_storage);
-
- /* setup memory allocator */
- uffs_MemSetupStaticAllocator(&demo_device_1.mem, static_buffer_par1, sizeof(static_buffer_par1));
- uffs_MemSetupStaticAllocator(&demo_device_2.mem, static_buffer_par2, sizeof(static_buffer_par2));
-
- /* register mount table */
- while(mtbl->dev) {
- mtbl->dev->Init = my_initDevice;
- mtbl->dev->Release = my_releaseDevice;
- mtbl->dev->attr = &flash_storage;
- uffs_RegisterMountTable(mtbl);
- mtbl++;
- }
-
- return uffs_InitMountTable() == U_SUCC ? 0 : -1;
-}
-
-/* application entry */
-int main()
-{
- my_init_filesystem();
-
- // ... my application codes ....
- // read/write/create/delete files ...
-
- uffs_ReleaseMountTable();
-
- return 0;
-}
-
-#endif
-
-
-/////////////////////////////////////////////////////////////////////////////////
+/*
+ This file is part of UFFS, the Ultra-low-cost Flash File System.
+
+ Copyright (C) 2005-2009 Ricky Zheng <[email protected]>
+
+ UFFS is free software; you can redistribute it and/or modify it under
+ the GNU Library General Public License as published by the Free Software
+ Foundation; either version 2 of the License, or (at your option) any
+ later version.
+
+ UFFS is distributed in the hope that it will be useful, but WITHOUT
+ ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ or GNU Library General Public License, as applicable, for more details.
+
+ You should have received a copy of the GNU General Public License
+ and GNU Library General Public License along with UFFS; if not, write
+ to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ Boston, MA 02110-1301, USA.
+
+ As a special exception, if other files instantiate templates or use
+ macros or inline functions from this file, or you compile this file
+ and link it with other works to produce a work based on this file,
+ this file does not by itself cause the resulting work to be covered
+ by the GNU General Public License. However the source code for this
+ file must still be made available in accordance with section (3) of
+ the GNU General Public License v2.my_application_main_entry
+
+ This exception does not invalidate any other reasons why a work based
+ on this file might be covered by the GNU General Public License.
+*/
+/**
+ * \file flash-interface-example.c
+ * \brief example for using flash driver and multiple partitions, with static memory allocator.
+ * \author Ricky Zheng, created at 27 Nov, 2007
+ */
+
+#include <string.h>
+
+#include "uffs/uffs_device.h"
+#include "uffs/uffs_flash.h"
+#include "uffs/uffs_mtb.h"
+#include "uffs/uffs_fs.h"
+
+#define PFX "nand-drv:"
+
+struct my_nand_chip {
+ void *IOR_ADDR;
+ void *IOW_ADDR;
+ UBOOL inited;
+ // ...
+};
+
+/*
+ * Standard NAND flash commands
+ */
+#define NAND_CMD_READ0 0
+#define NAND_CMD_READ1 1
+#define NAND_CMD_RNDOUT 5
+#define NAND_CMD_PAGEPROG 0x10
+#define NAND_CMD_READOOB 0x50
+#define NAND_CMD_ERASE1 0x60
+#define NAND_CMD_STATUS 0x70
+#define NAND_CMD_STATUS_MULTI 0x71
+#define NAND_CMD_SEQIN 0x80
+#define NAND_CMD_RNDIN 0x85
+#define NAND_CMD_READID 0x90
+#define NAND_CMD_ERASE2 0xd0
+#define NAND_CMD_RESET 0xff
+
+
+/* impelent the following functions for your NAND flash */
+#define CHIP_SET_CLE(chip) {}
+#define CHIP_CLR_CLE(chip) {}
+#define CHIP_SET_ALE(chip) {}
+#define CHIP_CLR_ALE(chip) {}
+#define CHIP_SET_NCS(chip) {}
+#define CHIP_CLR_NCS(chip) {}
+#define CHIP_BUSY(chip) {}
+#define CHIP_READY(chip) {}
+#define WRITE_COMMAND(chip, cmd) {}
+#define WRITE_DATA_ADDR(chip, block, page, offset) {}
+#define WRITE_ERASE_ADDR(chip, block) {}
+#define WRITE_DATA(chip, data, len) {}
+#define READ_DATA(chip, data, len) {}
+
+#define PARSE_STATUS(v) (UFFS_FLASH_NO_ERR) // parse status to UFFS_FLASH_NO_ERR or UFFS_FLASH_BAD_BLK
+
+
+
+#if CONFIG_USE_STATIC_MEMORY_ALLOCATOR == 0
+int main()
+{
+ uffs_Perror(UFFS_ERR_NORMAL, "This example need CONFIG_USE_STATIC_MEMORY_ALLOCATOR = 1");
+ return 0;
+}
+#else
+
+
+static int nand_read_page(uffs_Device *dev, u32 block, u32 page, u8 *data, int data_len, u8 *ecc,
+ u8 *spare, int spare_len)
+{
+ u8 val = 0;
+ int ret = UFFS_FLASH_NO_ERR;
+ struct my_nand_chip *chip = (struct my_nand_chip *) dev->attr->_private;
+
+ CHIP_CLR_NCS(chip);
+ if (data && data_len > 0) {
+ CHIP_SET_CLE(chip);
+ WRITE_COMMAND(chip, NAND_CMD_READ0);
+ CHIP_CLR_CLE(chip);
+ CHIP_SET_ALE(chip);
+ WRITE_DATA_ADDR(chip, block, page, 0);
+ CHIP_CLR_ALE(chip);
+ READ_DATA(chip, data, data_len);
+
+ // for now, we return all 0xFF to pass UFFS mount, you should remove this at your driver
+ memset(data, 0xFF, data_len);
+ }
+
+ if (spare && spare_len > 0) {
+ CHIP_SET_CLE(chip);
+ WRITE_COMMAND(chip, NAND_CMD_READOOB);
+ CHIP_CLR_CLE(chip);
+ CHIP_SET_ALE(chip);
+ WRITE_DATA_ADDR(chip, block, page, dev->attr->page_data_size);
+ CHIP_CLR_ALE(chip);
+ READ_DATA(chip, spare, spare_len);
+
+ // for now, we return all 0xFF to pass UFFS mount, you should remove this at your driver
+ memset(spare, 0xFF, spare_len);
+ }
+
+ if (data == NULL && spare == NULL) {
+ // read bad block mark
+ CHIP_SET_CLE(chip);
+ WRITE_COMMAND(chip, NAND_CMD_READOOB);
+ CHIP_CLR_CLE(chip);
+ CHIP_SET_ALE(chip);
+ WRITE_DATA_ADDR(chip, block, page, dev->attr->page_data_size + attr->block_status_offs);
+ CHIP_CLR_ALE(chip);
+ READ_DATA(chip, &val, 1);
+ ret = (val == 0xFF ? UFFS_FLASH_NO_ERR : UFFS_FLASH_BAD_BLK);
+
+ // for now, we return UFFS_FLASH_NO_ERR to pass UFFS mount, you should remove this at your driver
+ ret = UFFS_FLASH_NO_ERR;
+ }
+
+ CHIP_SET_NCS(chip);
+
+ return ret;
+}
+
+static int nand_write_page(uffs_Device *dev, u32 block, u32 page,
+ const u8 *data, int data_len, const u8 *spare, int spare_len)
+{
+ u8 val = 0;
+ int ret = UFFS_FLASH_NO_ERR;
+ UBOOL fall_through = FALSE;
+ struct my_nand_chip *chip = (struct my_nand_chip *) dev->attr->_private;
+
+ CHIP_CLR_NCS(chip);
+
+ if (data && data_len > 0) {
+ CHIP_SET_CLE(chip);
+ WRITE_COMMAND(chip, NAND_CMD_READ0);
+ WRITE_COMMAND(chip, NAND_CMD_SEQIN);
+ CHIP_CLR_CLE(chip);
+ CHIP_SET_ALE(chip);
+ WRITE_DATA_ADDR(chip, block, page, 0);
+ CHIP_CLR_ALE(chip);
+ CHIP_BUSY(chip);
+ WRITE_DATA(chip, data, data_len);
+ if (data_len == dev->attr->page_data_size)
+ fall_through = U_TRUE;
+ else {
+ CHIP_SET_CLE(chip);
+ WRITE_COMMAND(chip, NAND_CMD_PAGEPROG);
+ WRITE_COMMAND(chip, NAND_CMD_STATUS);
+ CHIP_CLR_CLE(chip);
+ CHIP_READY(chip);
+ READ_DATA(chip, &val, 1);
+ ret = PARSE_STATUS(val);
+ }
+ }
+
+ if (ret != UFFS_FLASH_NO_ERR)
+ goto ext;
+
+ if (spare && spare_len > 0) {
+ if (!fall_through) {
+ CHIP_SET_CLE(chip);
+ WRITE_COMMAND(chip, NAND_CMD_READOOB);
+ WRITE_COMMAND(chip, NAND_CMD_SEQIN);
+ CHIP_CLR_CLE(chip);
+ CHIP_SET_ALE(chip);
+ WRITE_DATA_ADDR(chip, block, page, dev->attr->page_data_size);
+ CHIP_CLR_ALE(chip);
+ CHIP_BUSY(chip);
+ }
+ WRITE_DATA(chip, spare, spare_len);
+ CHIP_SET_CLE(chip);
+ WRITE_COMMAND(chip, NAND_CMD_PAGEPROG);
+ WRITE_COMMAND(chip, NAND_CMD_STATUS);
+ CHIP_CLR_CLE(chip);
+ CHIP_READY(chip);
+ READ_DATA(chip, &val, 1);
+ ret = PARSE_STATUS(val);
+ }
+
+ if (data == NULL && spare == NULL) {
+ // mark bad block
+ CHIP_SET_CLE(chip);
+ WRITE_COMMAND(chip, NAND_CMD_READOOB);
+ WRITE_COMMAND(chip, NAND_CMD_SEQIN);
+ CHIP_CLR_CLE(chip);
+ CHIP_SET_ALE(chip);
+ WRITE_DATA_ADDR(chip, block, page, dev->attr->page_data_size + attr->block_status_offs);
+ CHIP_CLR_ALE(chip);
+ CHIP_BUSY(chip);
+ val = 0;
+ WRITE_DATA(chip, &val, 1);
+ CHIP_SET_CLE(chip);
+ WRITE_COMMAND(chip, NAND_CMD_PAGEPROG);
+ WRITE_COMMAND(chip, NAND_CMD_STATUS);
+ CHIP_CLR_CLE(chip);
+ CHIP_READY(chip);
+ READ_DATA(chip, &val, 1);
+ ret = PARSE_STATUS(val);
+ }
+
+ext:
+ CHIP_SET_NCS(chip);
+
+ return ret;
+}
+
+static int nand_erase_block(uffs_Device *dev, u32 block)
+{
+ u8 val;
+ struct my_nand_chip *chip = (struct my_nand_chip *) dev->attr->_private;
+
+ CHIP_CLR_NCS(chip);
+
+ CHIP_SET_CLE(chip);
+ WRITE_COMMAND(chip, NAND_CMD_ERASE1);
+ CHIP_CLR_CLE(chip);
+ CHIP_SET_ALE(chip);
+ WRITE_ERASE_ADDR(chip, blcok);
+ CHIP_CLR_ALE(chip);
+ CHIP_SET_CLE(chip);
+ WRITE_COMMAND(chip, NAND_CMD_ERASE2);
+ WRITE_COMMAND(chip, NAND_CMD_STATUS);
+ CHIP_CLR_CLE(chip);
+ CHIP_READY(chip);
+ READ_DATA(chip, &val, 1);
+
+ CHIP_SET_NCS(chip);
+
+ return PARSE_STATUS(val);
+}
+
+
+static int nand_init_flash(uffs_Device *dev)
+{
+ // initialize your hardware here ...
+ struct my_nand_chip *chip = (struct my_nand_chip *) dev->attr->_private;
+
+ if (!chip->inited) {
+ // setup chip I/O address, setup NAND flash controller ... etc.
+ // chip->IOR_ADDR = 0xF0000000
+ // chip->IOW_ADDR = 0xF0000000
+ chip->inited = U_TRUE;
+ }
+ return 0;
+}
+
+static int nand_release_flash(uffs_Device *dev)
+{
+ // release your hardware here
+ struct my_nand_chip *chip = (struct my_nand_chip *) dev->attr->_private;
+
+ return 0;
+}
+
+static uffs_FlashOps g_my_nand_ops = {
+ nand_init_flash, // InitFlash()
+ nand_release_flash, // ReleaseFlash()
+ nand_read_page, // ReadPage()
+ NULL, // ReadPageWithLayout
+ nand_write_page, // WritePage()
+ NULL, // WirtePageWithLayout
+ NULL, // IsBadBlock(), let UFFS take care of it.
+ NULL, // MarkBadBlock(), let UFFS take care of it.
+ nand_erase_block, // EraseBlock()
+};
+
+/////////////////////////////////////////////////////////////////////////////////
+
+// change these parameters to fit your nand flash specification
+
+#define TOTAL_BLOCKS 1024
+#define PAGE_DATA_SIZE 512
+#define PAGE_SPARE_SIZE 16
+#define PAGES_PER_BLOCK 32
+#define PAGE_SIZE (PAGE_DATA_SIZE + PAGE_SPARE_SIZE)
+#define BLOCK_DATA_SIZE (PAGE_DATA_SIZE * PAGES_PER_BLOCK)
+
+#define NR_PARTITION 2 /* total partitions */
+#define PAR_1_BLOCKS 100 /* partition 1 */
+#define PAR_2_BLOCKS (TOTAL_BLOCKS - PAR_1_BLOCKS) /* partition 2 */
+
+struct my_nand_chip g_nand_chip = {0};
+static struct uffs_StorageAttrSt g_my_flash_storage = {0};
+
+/* define mount table */
+static uffs_Device demo_device_1 = {0};
+static uffs_Device demo_device_2 = {0};
+
+static uffs_MountTable demo_mount_table[] = {
+ { &demo_device_1, 0, PAR_1_BLOCKS - 1, "/data/" },
+ { &demo_device_2, PAR_1_BLOCKS, PAR_1_BLOCKS + PAR_2_BLOCKS - 1, "/" },
+ { NULL, 0, 0, NULL }
+};
+
+/* static alloc the memory for each partition */
+static int static_buffer_par1[UFFS_STATIC_BUFF_SIZE(PAGES_PER_BLOCK, PAGE_SIZE, PAR_1_BLOCKS) / sizeof(int)];
+static int static_buffer_par2[UFFS_STATIC_BUFF_SIZE(PAGES_PER_BLOCK, PAGE_SIZE, PAR_2_BLOCKS) / sizeof(int)];;
+
+static void init_nand_chip(struct my_nand_chip *chip)
+{
+ // init chip IO address, etc.
+}
+
+static void setup_flash_storage(struct uffs_StorageAttrSt *attr)
+{
+ memset(attr, 0, sizeof(struct uffs_StorageAttrSt));
+
+ attr->total_blocks = TOTAL_BLOCKS; /* total blocks */
+ attr->page_data_size = PAGE_DATA_SIZE; /* page data size */
+ attr->pages_per_block = PAGES_PER_BLOCK; /* pages per block */
+ attr->spare_size = PAGE_SPARE_SIZE; /* page spare size */
+ attr->block_status_offs = 4; /* block status offset is 5th byte in spare */
+ attr->ecc_opt = UFFS_ECC_SOFT; /* ecc option */
+ attr->layout_opt = UFFS_LAYOUT_UFFS; /* let UFFS do the spare layout */
+}
+
+static URET my_InitDevice(uffs_Device *dev)
+{
+ dev->attr = &g_my_flash_storage;
+ dev->attr->_private = (void *) &g_nand_chip; // hook nand_chip data structure to attr->_private
+ dev->ops = &g_my_nand_ops;
+
+ return U_SUCC;
+}
+
+static URET my_ReleaseDevice(uffs_Device *dev)
+{
+ return U_SUCC;
+}
+
+
+static int my_init_filesystem(void)
+{
+ uffs_MountTable *mtbl = &(demo_mount_table[0]);
+
+ /* setup nand storage attributes */
+ setup_flash_storage(&g_my_flash_storage);
+
+ /* setup memory allocator */
+ uffs_MemSetupStaticAllocator(&demo_device_1.mem, static_buffer_par1, sizeof(static_buffer_par1));
+ uffs_MemSetupStaticAllocator(&demo_device_2.mem, static_buffer_par2, sizeof(static_buffer_par2));
+
+ /* register mount table */
+ while(mtbl->dev) {
+ // setup device init/release entry
+ mtbl->dev->Init = my_InitDevice;
+ mtbl->dev->Release = my_ReleaseDevice;
+ uffs_RegisterMountTable(mtbl);
+ mtbl++;
+ }
+
+ return uffs_InitMountTable() == U_SUCC ? 0 : -1;
+}
+
+/* application entry */
+int main()
+{
+ my_init_filesystem();
+
+ // ... my application codes ....
+ // read/write/create/delete files ...
+
+ uffs_ReleaseMountTable();
+
+ return 0;
+}
+
+#endif
+
+
+/////////////////////////////////////////////////////////////////////////////////
diff --git a/components/dfs/filesystems/uffs/src/example/static-mem-allocate.c b/components/dfs/filesystems/uffs/src/example/static-mem-allocate.c
index c0f7d510ea..45e293fcc7 100644
--- a/components/dfs/filesystems/uffs/src/example/static-mem-allocate.c
+++ b/components/dfs/filesystems/uffs/src/example/static-mem-allocate.c
@@ -1,161 +1,155 @@
-/*
- This file is part of UFFS, the Ultra-low-cost Flash File System.
-
- Copyright (C) 2005-2009 Ricky Zheng <[email protected]>
-
- UFFS is free software; you can redistribute it and/or modify it under
- the GNU Library General Public License as published by the Free Software
- Foundation; either version 2 of the License, or (at your option) any
- later version.
-
- UFFS is distributed in the hope that it will be useful, but WITHOUT
- ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
- FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
- or GNU Library General Public License, as applicable, for more details.
-
- You should have received a copy of the GNU General Public License
- and GNU Library General Public License along with UFFS; if not, write
- to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
- Boston, MA 02110-1301, USA.
-
- As a special exception, if other files instantiate templates or use
- macros or inline functions from this file, or you compile this file
- and link it with other works to produce a work based on this file,
- this file does not by itself cause the resulting work to be covered
- by the GNU General Public License. However the source code for this
- file must still be made available in accordance with section (3) of
- the GNU General Public License v2.
-
- This exception does not invalidate any other reasons why a work based
- on this file might be covered by the GNU General Public License.
-*/
-
-/**
- * \file static-mem-allocate.c
- * \brief demostrate how to use static memory allocation. This example use
- * file emulated NAND flash.
- * \author Ricky Zheng
- */
-
-#include <stdio.h>
-#include <string.h>
-#include <stdlib.h>
-#include "uffs/uffs_config.h"
-#include "uffs/uffs_public.h"
-#include "uffs/uffs_fs.h"
-#include "uffs/uffs_utils.h"
-#include "uffs/uffs_core.h"
-#include "uffs/uffs_mtb.h"
-#include "cmdline.h"
-#include "uffs_fileem.h"
-
-#define PFX "static-example: "
-
-#if CONFIG_USE_STATIC_MEMORY_ALLOCATOR == 0
-int main()
-{
- uffs_Perror(UFFS_ERR_NORMAL, "This example need CONFIG_USE_STATIC_MEMORY_ALLOCATOR = 1");
- return 0;
-}
-#else
-
-extern struct cli_commandset * get_helper_cmds(void);
-
-#define DEFAULT_EMU_FILENAME "uffsemfile.bin"
-
-#define PAGE_DATA_SIZE 512
-#define PAGE_SPARE_SIZE 16
-#define PAGES_PER_BLOCK 32
-#define TOTAL_BLOCKS 128
-
-#define PAGE_SIZE (PAGE_DATA_SIZE + PAGE_SPARE_SIZE)
-#define BLOCK_DATA_SIZE (PAGES_PER_BLOCK * PAGE_DATA_SIZE)
-#define TOTAL_DATA_SIZE (TOTAL_BLOCKS * BLOCK_DATA_SIZE)
-#define BLOCK_SIZE (PAGES_PER_BLOCK * PAGE_SIZE)
-#define TOTAL_SIZE (BLOCK_SIZE * TOTAL_BLOCKS)
-
-#define MAX_MOUNT_TABLES 10
-#define MAX_MOUNT_POINT_NAME 32
-
-static uffs_Device demo_device = {0};
-static struct uffs_MountTableEntrySt demo_mount = {
- &demo_device,
- 0, /* start from block 0 */
- -1, /* use whole chip */
- "/", /* mount point */
- NULL
-};
-
-static struct uffs_StorageAttrSt emu_storage = {0};
-static struct uffs_FileEmuSt emu_private = {0};
-
-/* static alloc the memory */
-static int static_buffer_pool[UFFS_STATIC_BUFF_SIZE(PAGES_PER_BLOCK, PAGE_SIZE, TOTAL_BLOCKS) / sizeof(int)];
-
-
-static void setup_emu_storage(struct uffs_StorageAttrSt *attr)
-{
- attr->total_blocks = TOTAL_BLOCKS; /* total blocks */
- attr->page_data_size = PAGE_DATA_SIZE; /* page data size */
- attr->spare_size = PAGE_SPARE_SIZE; /* page spare size */
- attr->pages_per_block = PAGES_PER_BLOCK; /* pages per block */
- attr->block_status_offs = 4; /* block status offset is 5th byte in spare */
- attr->ecc_opt = UFFS_ECC_SOFT; /* ecc option */
- attr->layout_opt = UFFS_LAYOUT_UFFS; /* let UFFS do the spare layout */
-
-}
-
-static void setup_emu_private(uffs_FileEmu *emu)
-{
- memset(emu, 0, sizeof(uffs_FileEmu));
- emu->emu_filename = DEFAULT_EMU_FILENAME;
-}
-
-static int init_uffs_fs(void)
-{
- struct uffs_MountTableEntrySt *mtbl = &demo_mount;
-
- /* setup emu storage */
- setup_emu_storage(&emu_storage);
- setup_emu_private(&emu_private);
- emu_storage._private = &emu_private;
- mtbl->dev->attr = &emu_storage;
-
- /* setup memory allocator */
- uffs_MemSetupStaticAllocator(&mtbl->dev->mem, static_buffer_pool, sizeof(static_buffer_pool));
-
- /* setup device */
- uffs_fileem_setup_device(mtbl->dev);
-
- /* register mount table */
- uffs_RegisterMountTable(mtbl);
-
- return uffs_InitMountTable() == U_SUCC ? 0 : -1;
-}
-
-static int release_uffs_fs(void)
-{
- return uffs_ReleaseMountTable();
-}
-
-int main(int argc, char *argv[])
-{
- int ret;
-
- ret = init_uffs_fs();
-
- if (ret != 0) {
- printf ("Init file system fail: %d\n", ret);
- return -1;
- }
-
- cli_add_commandset(get_helper_cmds());
- cliMain();
-
- release_uffs_fs();
-
- return 0;
-}
-
-#endif
-
+/*
+ This file is part of UFFS, the Ultra-low-cost Flash File System.
+
+ Copyright (C) 2005-2009 Ricky Zheng <[email protected]>
+
+ UFFS is free software; you can redistribute it and/or modify it under
+ the GNU Library General Public License as published by the Free Software
+ Foundation; either version 2 of the License, or (at your option) any
+ later version.
+
+ UFFS is distributed in the hope that it will be useful, but WITHOUT
+ ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ or GNU Library General Public License, as applicable, for more details.
+
+ You should have received a copy of the GNU General Public License
+ and GNU Library General Public License along with UFFS; if not, write
+ to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ Boston, MA 02110-1301, USA.
+
+ As a special exception, if other files instantiate templates or use
+ macros or inline functions from this file, or you compile this file
+ and link it with other works to produce a work based on this file,
+ this file does not by itself cause the resulting work to be covered
+ by the GNU General Public License. However the source code for this
+ file must still be made available in accordance with section (3) of
+ the GNU General Public License v2.
+
+ This exception does not invalidate any other reasons why a work based
+ on this file might be covered by the GNU General Public License.
+*/
+
+/**
+ * \file static-mem-allocate.c
+ * \brief demostrate how to use static memory allocation. This example use
+ * file emulated NAND flash, one partition only.
+ * \author Ricky Zheng
+ */
+
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include "uffs/uffs_config.h"
+#include "uffs/uffs_public.h"
+#include "uffs/uffs_fs.h"
+#include "uffs/uffs_utils.h"
+#include "uffs/uffs_core.h"
+#include "uffs/uffs_mtb.h"
+#include "cmdline.h"
+#include "uffs_fileem.h"
+
+#define PFX "static-example: "
+
+#if CONFIG_USE_STATIC_MEMORY_ALLOCATOR == 0
+int main()
+{
+ uffs_Perror(UFFS_ERR_NORMAL, "This example need CONFIG_USE_STATIC_MEMORY_ALLOCATOR = 1");
+ return 0;
+}
+#else
+
+extern struct cli_commandset * get_helper_cmds(void);
+
+#define PAGE_DATA_SIZE 512
+#define PAGE_SPARE_SIZE 16
+#define PAGES_PER_BLOCK 32
+#define TOTAL_BLOCKS 128
+
+#define PAGE_SIZE (PAGE_DATA_SIZE + PAGE_SPARE_SIZE)
+#define BLOCK_DATA_SIZE (PAGES_PER_BLOCK * PAGE_DATA_SIZE)
+#define TOTAL_DATA_SIZE (TOTAL_BLOCKS * BLOCK_DATA_SIZE)
+#define BLOCK_SIZE (PAGES_PER_BLOCK * PAGE_SIZE)
+#define TOTAL_SIZE (BLOCK_SIZE * TOTAL_BLOCKS)
+
+#define MAX_MOUNT_TABLES 10
+#define MAX_MOUNT_POINT_NAME 32
+
+static uffs_Device demo_device = {0};
+static struct uffs_MountTableEntrySt demo_mount = {
+ &demo_device,
+ 0, /* start from block 0 */
+ -1, /* use whole chip */
+ "/", /* mount point */
+ NULL
+};
+
+/* static alloc the memory */
+static int static_buffer_pool[UFFS_STATIC_BUFF_SIZE(PAGES_PER_BLOCK, PAGE_SIZE, TOTAL_BLOCKS) / sizeof(int)];
+
+
+static void setup_storage(struct uffs_StorageAttrSt *attr)
+{
+ attr->total_blocks = TOTAL_BLOCKS; /* total blocks */
+ attr->page_data_size = PAGE_DATA_SIZE; /* page data size */
+ attr->spare_size = PAGE_SPARE_SIZE; /* page spare size */
+ attr->pages_per_block = PAGES_PER_BLOCK; /* pages per block */
+ attr->block_status_offs = 4; /* block status offset is 5th byte in spare */
+ attr->ecc_opt = UFFS_ECC_SOFT; /* use UFFS software ecc */
+ attr->layout_opt = UFFS_LAYOUT_UFFS; /* let UFFS do the spare layout */
+}
+
+static void setup_device(uffs_Device *dev)
+{
+ // using file emulator device
+ dev->Init = femu_InitDevice;
+ dev->Release = femu_ReleaseDevice;
+ dev->attr = femu_GetStorage();
+}
+
+static int init_uffs_fs(void)
+{
+ struct uffs_MountTableEntrySt *mtbl = &demo_mount;
+
+ /* setup flash storage attributes */
+ setup_storage(femu_GetStorage());
+
+ /* setup memory allocator */
+ uffs_MemSetupStaticAllocator(&mtbl->dev->mem, static_buffer_pool, sizeof(static_buffer_pool));
+
+ /* setup device: init, release, attr */
+ setup_device(mtbl->dev);
+
+ /* register mount table */
+ uffs_RegisterMountTable(mtbl);
+
+ return uffs_InitMountTable() == U_SUCC ? 0 : -1;
+}
+
+static int release_uffs_fs(void)
+{
+ return uffs_ReleaseMountTable();
+}
+
+int main(int argc, char *argv[])
+{
+ int ret;
+
+ ret = init_uffs_fs();
+
+ if (ret != 0) {
+ printf ("Init file system fail: %d\n", ret);
+ return -1;
+ }
+
+ cli_add_commandset(get_helper_cmds());
+ cli_add_commandset(get_test_cmds());
+ cliMain();
+
+ release_uffs_fs();
+
+ return 0;
+}
+
+#endif
+