From 42ae1c634d9e102091b82bc61ebfe05cccd7fcea Mon Sep 17 00:00:00 2001 From: bernard Date: Sun, 29 Dec 2013 22:27:33 +0800 Subject: [PATCH] [Drivers] Add more debug utilities in MTD nand --- components/drivers/mtd/mtd_nand.c | 190 ++++++++++++++++++++++++++++++ 1 file changed, 190 insertions(+) diff --git a/components/drivers/mtd/mtd_nand.c b/components/drivers/mtd/mtd_nand.c index ff2ec8e54c..e09be4e1c3 100644 --- a/components/drivers/mtd/mtd_nand.c +++ b/components/drivers/mtd/mtd_nand.c @@ -93,5 +93,195 @@ rt_err_t rt_mtd_nand_register_device(const char *name, return rt_device_register(dev, name, RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_STANDALONE); } +#if defined(RT_MTD_NAND_DEBUG) && defined(RT_USING_FINSH) +#include +#define __is_print(ch) ((unsigned int)((ch) - ' ') < 127u - ' ') + +static void mtd_dump_hex(const rt_uint8_t *ptr, rt_size_t buflen) +{ + unsigned char *buf = (unsigned char*)ptr; + int i, j; + for (i=0; ipage_size + nand->oob_size); + if (page_ptr == RT_NULL) + { + rt_kprintf("out of memory!\n"); + return -RT_ENOMEM; + } + + oob_ptr = page_ptr + nand->page_size; + rt_memset(page_ptr, 0xff, nand->page_size + nand->oob_size); + + page = (block + nand->block_start) * nand->pages_per_block + page; + result = rt_mtd_nand_read(nand, page, page_ptr, nand->page_size, + oob_ptr, nand->oob_size); + + rt_kprintf("read page, rc=%d\n", result); + mtd_dump_hex(page_ptr, nand->page_size); + mtd_dump_hex(oob_ptr, nand->oob_size); + + rt_free(page_ptr); + return 0; +} +FINSH_FUNCTION_EXPORT_ALIAS(mtd_nand_read, nand_read, read page in nand - nand_read(name, block, page)); + +int mtd_nand_readoob(const char* name, int block, int page) +{ + struct rt_mtd_nand_device *nand; + rt_uint8_t *oob_ptr; + + nand = RT_MTD_NAND_DEVICE(rt_device_find(name)); + if (nand == RT_NULL) + { + rt_kprintf("no nand device found!\n"); + return -RT_ERROR; + } + + oob_ptr = rt_malloc(nand->oob_size); + if (oob_ptr == RT_NULL) + { + rt_kprintf("out of memory!\n"); + return -RT_ENOMEM; + } + + page = (block + nand->block_start) * nand->pages_per_block + page; + rt_mtd_nand_read(nand, page, RT_NULL, nand->page_size, + oob_ptr, nand->oob_size); + mtd_dump_hex(oob_ptr, nand->oob_size); + + rt_free(oob_ptr); + return 0; +} +FINSH_FUNCTION_EXPORT_ALIAS(mtd_nand_readoob, nand_readoob, read spare data in nand - nand_readoob(name, block, page)); + +int mtd_nand_write(const char* name, int block, int page) +{ + rt_err_t result; + rt_uint8_t *page_ptr; + rt_uint8_t *oob_ptr; + rt_uint32_t index; + struct rt_mtd_nand_device *nand; + + nand = RT_MTD_NAND_DEVICE(rt_device_find(name)); + if (nand == RT_NULL) + { + rt_kprintf("no nand device found!\n"); + return -RT_ERROR; + } + + page_ptr = rt_malloc(nand->page_size + nand->oob_size); + if (page_ptr == RT_NULL) + { + rt_kprintf("out of memory!\n"); + return -RT_ENOMEM; + } + + oob_ptr = page_ptr + nand->page_size; + /* prepare page data */ + for (index = 0; index < nand->page_size; index ++) + { + page_ptr[index] = index & 0xff; + } + /* prepare oob data */ + for (index = 0; index < nand->oob_size; index ++) + { + oob_ptr[index] = index & 0xff; + } + + page = (block + nand->block_start) * nand->pages_per_block + page; + result = rt_mtd_nand_write(nand, page, page_ptr, nand->page_size, + oob_ptr, nand->oob_size); + if (result != RT_MTD_EOK) + { + rt_kprintf("write page failed!, rc=%d\n", result); + } + + rt_free(page_ptr); + return 0; +} +FINSH_FUNCTION_EXPORT_ALIAS(mtd_nand_write, nand_write, write dump data to nand - nand_write(name, block, page)); + +int mtd_nand_erase(const char* name, int block) +{ + struct rt_mtd_nand_device *nand; + nand = RT_MTD_NAND_DEVICE(rt_device_find(name)); + if (nand == RT_NULL) + { + rt_kprintf("no nand device found!\n"); + return -RT_ERROR; + } + + block += nand->block_start; + + return rt_mtd_nand_erase_block(nand, block); +} +FINSH_FUNCTION_EXPORT_ALIAS(mtd_nand_erase, nand_erase, nand_erase(name, block)); + +int mtd_nand_erase_all(const char* name) +{ + rt_uint32_t index = 0; + struct rt_mtd_nand_device *nand; + + nand = RT_MTD_NAND_DEVICE(rt_device_find(name)); + if (nand == RT_NULL) + { + rt_kprintf("no nand device found!\n"); + return -RT_ERROR; + } + + for (index = nand->block_start; index < nand->block_end; index ++) + { + rt_mtd_nand_erase_block(nand, index); + } + + return 0; +} +FINSH_FUNCTION_EXPORT_ALIAS(mtd_nand_erase_all, nand_erase_all, erase all of nand device - nand_erase_all(name, block)); #endif + +#endif +