[driver][mtd_nand] Formatting code.|格式化代码。

This commit is contained in:
guozhanxin 2019-06-19 17:14:44 +08:00
parent d8702aa568
commit c421757cc4
2 changed files with 166 additions and 166 deletions

View File

@ -45,71 +45,71 @@ struct rt_mtd_nand_device
rt_uint32_t block_end; /* The end of available block */ rt_uint32_t block_end; /* The end of available block */
/* operations interface */ /* operations interface */
const struct rt_mtd_nand_driver_ops* ops; const struct rt_mtd_nand_driver_ops *ops;
}; };
struct rt_mtd_nand_driver_ops struct rt_mtd_nand_driver_ops
{ {
rt_err_t (*read_id) (struct rt_mtd_nand_device* device); rt_err_t (*read_id)(struct rt_mtd_nand_device *device);
rt_err_t (*read_page)(struct rt_mtd_nand_device* device, rt_err_t (*read_page)(struct rt_mtd_nand_device *device,
rt_off_t page, rt_off_t page,
rt_uint8_t* data, rt_uint32_t data_len, rt_uint8_t *data, rt_uint32_t data_len,
rt_uint8_t * spare, rt_uint32_t spare_len); rt_uint8_t *spare, rt_uint32_t spare_len);
rt_err_t (*write_page)(struct rt_mtd_nand_device * device, rt_err_t (*write_page)(struct rt_mtd_nand_device *device,
rt_off_t page, rt_off_t page,
const rt_uint8_t * data, rt_uint32_t data_len, const rt_uint8_t *data, rt_uint32_t data_len,
const rt_uint8_t * spare, rt_uint32_t spare_len); const rt_uint8_t *spare, rt_uint32_t spare_len);
rt_err_t (*move_page) (struct rt_mtd_nand_device *device, rt_off_t src_page, rt_off_t dst_page); rt_err_t (*move_page)(struct rt_mtd_nand_device *device, rt_off_t src_page, rt_off_t dst_page);
rt_err_t (*erase_block)(struct rt_mtd_nand_device* device, rt_uint32_t block); rt_err_t (*erase_block)(struct rt_mtd_nand_device *device, rt_uint32_t block);
rt_err_t (*check_block)(struct rt_mtd_nand_device* device, rt_uint32_t block); rt_err_t (*check_block)(struct rt_mtd_nand_device *device, rt_uint32_t block);
rt_err_t (*mark_badblock)(struct rt_mtd_nand_device* device, rt_uint32_t block); rt_err_t (*mark_badblock)(struct rt_mtd_nand_device *device, rt_uint32_t block);
}; };
rt_err_t rt_mtd_nand_register_device(const char* name, struct rt_mtd_nand_device* device); rt_err_t rt_mtd_nand_register_device(const char *name, struct rt_mtd_nand_device *device);
rt_inline rt_uint32_t rt_mtd_nand_read_id(struct rt_mtd_nand_device* device) rt_inline rt_uint32_t rt_mtd_nand_read_id(struct rt_mtd_nand_device *device)
{ {
return device->ops->read_id(device); return device->ops->read_id(device);
} }
rt_inline rt_err_t rt_mtd_nand_read( rt_inline rt_err_t rt_mtd_nand_read(
struct rt_mtd_nand_device* device, struct rt_mtd_nand_device *device,
rt_off_t page, rt_off_t page,
rt_uint8_t* data, rt_uint32_t data_len, rt_uint8_t *data, rt_uint32_t data_len,
rt_uint8_t * spare, rt_uint32_t spare_len) rt_uint8_t *spare, rt_uint32_t spare_len)
{ {
return device->ops->read_page(device, page, data, data_len, spare, spare_len); return device->ops->read_page(device, page, data, data_len, spare, spare_len);
} }
rt_inline rt_err_t rt_mtd_nand_write( rt_inline rt_err_t rt_mtd_nand_write(
struct rt_mtd_nand_device* device, struct rt_mtd_nand_device *device,
rt_off_t page, rt_off_t page,
const rt_uint8_t* data, rt_uint32_t data_len, const rt_uint8_t *data, rt_uint32_t data_len,
const rt_uint8_t * spare, rt_uint32_t spare_len) const rt_uint8_t *spare, rt_uint32_t spare_len)
{ {
return device->ops->write_page(device, page, data, data_len, spare, spare_len); return device->ops->write_page(device, page, data, data_len, spare, spare_len);
} }
rt_inline rt_err_t rt_mtd_nand_move_page(struct rt_mtd_nand_device* device, rt_inline rt_err_t rt_mtd_nand_move_page(struct rt_mtd_nand_device *device,
rt_off_t src_page, rt_off_t dst_page) rt_off_t src_page, rt_off_t dst_page)
{ {
return device->ops->move_page(device, src_page, dst_page); return device->ops->move_page(device, src_page, dst_page);
} }
rt_inline rt_err_t rt_mtd_nand_erase_block(struct rt_mtd_nand_device* device, rt_uint32_t block) rt_inline rt_err_t rt_mtd_nand_erase_block(struct rt_mtd_nand_device *device, rt_uint32_t block)
{ {
return device->ops->erase_block(device, block); return device->ops->erase_block(device, block);
} }
rt_inline rt_err_t rt_mtd_nand_check_block(struct rt_mtd_nand_device* device, rt_uint32_t block) rt_inline rt_err_t rt_mtd_nand_check_block(struct rt_mtd_nand_device *device, rt_uint32_t block)
{ {
return device->ops->check_block(device, block); return device->ops->check_block(device, block);
} }
rt_inline rt_err_t rt_mtd_nand_mark_badblock(struct rt_mtd_nand_device* device, rt_uint32_t block) rt_inline rt_err_t rt_mtd_nand_mark_badblock(struct rt_mtd_nand_device *device, rt_uint32_t block)
{ {
return device->ops->mark_badblock(device, block); return device->ops->mark_badblock(device, block);
} }

View File

@ -56,7 +56,7 @@ static rt_err_t _mtd_control(rt_device_t dev, int cmd, void *args)
} }
#ifdef RT_USING_DEVICE_OPS #ifdef RT_USING_DEVICE_OPS
const static struct rt_device_ops mtd_nand_ops = const static struct rt_device_ops mtd_nand_ops =
{ {
_mtd_init, _mtd_init,
_mtd_open, _mtd_open,
@ -78,7 +78,7 @@ rt_err_t rt_mtd_nand_register_device(const char *name,
/* set device class and generic device interface */ /* set device class and generic device interface */
dev->type = RT_Device_Class_MTD; dev->type = RT_Device_Class_MTD;
#ifdef RT_USING_DEVICE_OPS #ifdef RT_USING_DEVICE_OPS
dev->ops = &mtd_nand_ops; dev->ops = &mtd_nand_ops;
#else #else
dev->init = _mtd_init; dev->init = _mtd_init;
dev->open = _mtd_open; dev->open = _mtd_open;
@ -101,186 +101,186 @@ rt_err_t rt_mtd_nand_register_device(const char *name,
static void mtd_dump_hex(const rt_uint8_t *ptr, rt_size_t buflen) static void mtd_dump_hex(const rt_uint8_t *ptr, rt_size_t buflen)
{ {
unsigned char *buf = (unsigned char*)ptr; unsigned char *buf = (unsigned char *)ptr;
int i, j; int i, j;
for (i=0; i<buflen; i+=16) for (i = 0; i < buflen; i += 16)
{ {
rt_kprintf("%06x: ", i); rt_kprintf("%06x: ", i);
for (j=0; j<16; j++) for (j = 0; j < 16; j++)
if (i+j < buflen) if (i + j < buflen)
rt_kprintf("%02x ", buf[i+j]); rt_kprintf("%02x ", buf[i + j]);
else else
rt_kprintf(" "); rt_kprintf(" ");
rt_kprintf(" "); rt_kprintf(" ");
for (j=0; j<16; j++) for (j = 0; j < 16; j++)
if (i+j < buflen) if (i + j < buflen)
rt_kprintf("%c", __is_print(buf[i+j]) ? buf[i+j] : '.'); rt_kprintf("%c", __is_print(buf[i + j]) ? buf[i + j] : '.');
rt_kprintf("\n"); rt_kprintf("\n");
} }
} }
int mtd_nandid(const char* name) int mtd_nandid(const char *name)
{ {
struct rt_mtd_nand_device *nand; struct rt_mtd_nand_device *nand;
nand = RT_MTD_NAND_DEVICE(rt_device_find(name)); nand = RT_MTD_NAND_DEVICE(rt_device_find(name));
if (nand == RT_NULL) if (nand == RT_NULL)
{ {
rt_kprintf("no nand device found!\n"); rt_kprintf("no nand device found!\n");
return -RT_ERROR; return -RT_ERROR;
} }
return rt_mtd_nand_read_id(nand); return rt_mtd_nand_read_id(nand);
} }
FINSH_FUNCTION_EXPORT_ALIAS(mtd_nandid, nand_id, read ID - nandid(name)); FINSH_FUNCTION_EXPORT_ALIAS(mtd_nandid, nand_id, read ID - nandid(name));
int mtd_nand_read(const char* name, int block, int page) int mtd_nand_read(const char *name, int block, int page)
{ {
rt_err_t result; rt_err_t result;
rt_uint8_t *page_ptr; rt_uint8_t *page_ptr;
rt_uint8_t *oob_ptr; rt_uint8_t *oob_ptr;
struct rt_mtd_nand_device *nand; struct rt_mtd_nand_device *nand;
nand = RT_MTD_NAND_DEVICE(rt_device_find(name)); nand = RT_MTD_NAND_DEVICE(rt_device_find(name));
if (nand == RT_NULL) if (nand == RT_NULL)
{ {
rt_kprintf("no nand device found!\n"); rt_kprintf("no nand device found!\n");
return -RT_ERROR; return -RT_ERROR;
} }
page_ptr = rt_malloc(nand->page_size + nand->oob_size); page_ptr = rt_malloc(nand->page_size + nand->oob_size);
if (page_ptr == RT_NULL) if (page_ptr == RT_NULL)
{ {
rt_kprintf("out of memory!\n"); rt_kprintf("out of memory!\n");
return -RT_ENOMEM; return -RT_ENOMEM;
} }
oob_ptr = page_ptr + nand->page_size; oob_ptr = page_ptr + nand->page_size;
rt_memset(page_ptr, 0xff, nand->page_size + nand->oob_size); rt_memset(page_ptr, 0xff, nand->page_size + nand->oob_size);
/* calculate the page number */ /* calculate the page number */
page = block * nand->pages_per_block + page; page = block * nand->pages_per_block + page;
result = rt_mtd_nand_read(nand, page, page_ptr, nand->page_size, result = rt_mtd_nand_read(nand, page, page_ptr, nand->page_size,
oob_ptr, nand->oob_size); oob_ptr, nand->oob_size);
rt_kprintf("read page, rc=%d\n", result); rt_kprintf("read page, rc=%d\n", result);
mtd_dump_hex(page_ptr, nand->page_size); mtd_dump_hex(page_ptr, nand->page_size);
mtd_dump_hex(oob_ptr, nand->oob_size); mtd_dump_hex(oob_ptr, nand->oob_size);
rt_free(page_ptr); rt_free(page_ptr);
return 0; return 0;
} }
FINSH_FUNCTION_EXPORT_ALIAS(mtd_nand_read, nand_read, read page in nand - nand_read(name, block, page)); 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) int mtd_nand_readoob(const char *name, int block, int page)
{ {
struct rt_mtd_nand_device *nand; struct rt_mtd_nand_device *nand;
rt_uint8_t *oob_ptr; rt_uint8_t *oob_ptr;
nand = RT_MTD_NAND_DEVICE(rt_device_find(name)); nand = RT_MTD_NAND_DEVICE(rt_device_find(name));
if (nand == RT_NULL) if (nand == RT_NULL)
{ {
rt_kprintf("no nand device found!\n"); rt_kprintf("no nand device found!\n");
return -RT_ERROR; return -RT_ERROR;
} }
oob_ptr = rt_malloc(nand->oob_size); oob_ptr = rt_malloc(nand->oob_size);
if (oob_ptr == RT_NULL) if (oob_ptr == RT_NULL)
{ {
rt_kprintf("out of memory!\n"); rt_kprintf("out of memory!\n");
return -RT_ENOMEM; return -RT_ENOMEM;
} }
/* calculate the page number */ /* calculate the page number */
page = block * nand->pages_per_block + page; page = block * nand->pages_per_block + page;
rt_mtd_nand_read(nand, page, RT_NULL, nand->page_size, rt_mtd_nand_read(nand, page, RT_NULL, nand->page_size,
oob_ptr, nand->oob_size); oob_ptr, nand->oob_size);
mtd_dump_hex(oob_ptr, nand->oob_size); mtd_dump_hex(oob_ptr, nand->oob_size);
rt_free(oob_ptr); rt_free(oob_ptr);
return 0; return 0;
} }
FINSH_FUNCTION_EXPORT_ALIAS(mtd_nand_readoob, nand_readoob, read spare data in nand - nand_readoob(name, block, page)); 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) int mtd_nand_write(const char *name, int block, int page)
{ {
rt_err_t result; rt_err_t result;
rt_uint8_t *page_ptr; rt_uint8_t *page_ptr;
rt_uint8_t *oob_ptr; rt_uint8_t *oob_ptr;
rt_uint32_t index; rt_uint32_t index;
struct rt_mtd_nand_device *nand; struct rt_mtd_nand_device *nand;
nand = RT_MTD_NAND_DEVICE(rt_device_find(name)); nand = RT_MTD_NAND_DEVICE(rt_device_find(name));
if (nand == RT_NULL) if (nand == RT_NULL)
{ {
rt_kprintf("no nand device found!\n"); rt_kprintf("no nand device found!\n");
return -RT_ERROR; return -RT_ERROR;
} }
page_ptr = rt_malloc(nand->page_size + nand->oob_size); page_ptr = rt_malloc(nand->page_size + nand->oob_size);
if (page_ptr == RT_NULL) if (page_ptr == RT_NULL)
{ {
rt_kprintf("out of memory!\n"); rt_kprintf("out of memory!\n");
return -RT_ENOMEM; return -RT_ENOMEM;
} }
oob_ptr = page_ptr + nand->page_size; oob_ptr = page_ptr + nand->page_size;
/* prepare page data */ /* prepare page data */
for (index = 0; index < nand->page_size; index ++) for (index = 0; index < nand->page_size; index ++)
{ {
page_ptr[index] = index & 0xff; page_ptr[index] = index & 0xff;
} }
/* prepare oob data */ /* prepare oob data */
for (index = 0; index < nand->oob_size; index ++) for (index = 0; index < nand->oob_size; index ++)
{ {
oob_ptr[index] = index & 0xff; oob_ptr[index] = index & 0xff;
} }
/* calculate the page number */ /* calculate the page number */
page = block * nand->pages_per_block + page; page = block * nand->pages_per_block + page;
result = rt_mtd_nand_write(nand, page, page_ptr, nand->page_size, result = rt_mtd_nand_write(nand, page, page_ptr, nand->page_size,
oob_ptr, nand->oob_size); oob_ptr, nand->oob_size);
if (result != RT_MTD_EOK) if (result != RT_MTD_EOK)
{ {
rt_kprintf("write page failed!, rc=%d\n", result); rt_kprintf("write page failed!, rc=%d\n", result);
} }
rt_free(page_ptr); rt_free(page_ptr);
return 0; return 0;
} }
FINSH_FUNCTION_EXPORT_ALIAS(mtd_nand_write, nand_write, write dump data to nand - nand_write(name, block, page)); 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) int mtd_nand_erase(const char *name, int block)
{ {
struct rt_mtd_nand_device *nand; struct rt_mtd_nand_device *nand;
nand = RT_MTD_NAND_DEVICE(rt_device_find(name)); nand = RT_MTD_NAND_DEVICE(rt_device_find(name));
if (nand == RT_NULL) if (nand == RT_NULL)
{ {
rt_kprintf("no nand device found!\n"); rt_kprintf("no nand device found!\n");
return -RT_ERROR; return -RT_ERROR;
} }
return rt_mtd_nand_erase_block(nand, block); return rt_mtd_nand_erase_block(nand, block);
} }
FINSH_FUNCTION_EXPORT_ALIAS(mtd_nand_erase, nand_erase, nand_erase(name, block)); FINSH_FUNCTION_EXPORT_ALIAS(mtd_nand_erase, nand_erase, nand_erase(name, block));
int mtd_nand_erase_all(const char* name) int mtd_nand_erase_all(const char *name)
{ {
rt_uint32_t index = 0; rt_uint32_t index = 0;
struct rt_mtd_nand_device *nand; 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 = 0; index < (nand->block_end - nand->block_start); index ++) nand = RT_MTD_NAND_DEVICE(rt_device_find(name));
{ if (nand == RT_NULL)
rt_mtd_nand_erase_block(nand, index); {
} rt_kprintf("no nand device found!\n");
return -RT_ERROR;
return 0; }
for (index = 0; index < (nand->block_end - nand->block_start); 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)); FINSH_FUNCTION_EXPORT_ALIAS(mtd_nand_erase_all, nand_erase_all, erase all of nand device - nand_erase_all(name, block));
#endif #endif