uboot 下nor flash 读写命令使用和验证方法

本文详细介绍了在UBoot环境下使用sf命令对SPINORFlash进行读、写和擦除的操作,包括sfprobe、sfread、sfwrite和sferase等命令的用法。同时强调了验证读写结果的正确性,以及在处理过程中需注意的内存地址有效性和数据格式问题。
摘要由CSDN通过智能技术生成

1 uboot下sf命令

1.1 uboot下sf命令集合

# sf
sf - SPI flash sub-system

Usage:
sf probe [[bus:]cs] [hz] [mode] - init flash device on given SPI bus
                                  and chip select
sf read addr offset|partition len       - read `len' bytes starting at
                                          `offset' or from start of mtd
                                          `partition'to memory at `addr'
sf write addr offset|partition len      - write `len' bytes from memory
                                          at `addr' to flash at `offset'
                                          or to start of mtd `partition'
sf erase offset|partition [+]len        - erase `len' bytes from `offset'
                                          or from start of mtd `partition'
                                         `+len' round up `len' to block size
sf update addr offset|partition len     - erase and write `len' bytes from memory
                                          at `addr' to flash at `offset'
                                          or to start of mtd `partition'
sf protect lock/unlock sector len       - protect/unprotect 'len' bytes starting
                                          at address 'sector'

sf test offset len              - run a very basic destructive test
#

1.2 sf probe

sf probe是用来探测flash设备的,在使用的时候可以直接使用sf probe后面不加参数也可以。

sf probe [[bus:]cs] [hz] [mode] - init flash device on given SPI bus

1.3 sf read

sf read是将从flash offset地址处的flash数据存放到addr内存地址处,所以此处addr要求为一个有效的内存地址,读取的数据长度为len,此处的len被识别为是16进制数据

sf read addr offset|partition len       - read `len' bytes starting at
                                          `offset' or from start of mtd
                                          `partition'to memory at `addr'

1.4 sf write

sf read是将从内存addr地址处的内存数据写到flash offset地址处,所以此处addr要求为一个有效的内存地址,写入的数据长度为len,此处的len被识别为是16进制数据

sf write addr offset|partition len      - write `len' bytes from memory
                                          at `addr' to flash at `offset'
                                          or to start of mtd `partition'

1.5 sf erase

该命令是将从flash offset地址开始,长度为len的flash空间擦除,需要注意的地方是该处的len需要是erase_size的整数倍才被允许执行擦除动作。

sf erase offset|partition [+]len        - erase `len' bytes from `offset'
                                          or from start of mtd `partition'
                                         `+len' round up `len' to block size

2 验证nor flash读写结果正确性

需要注意的一点是nor flash在执行写之前需要先执行擦除操作,否则会提示数据写入失败。

/* 2.1 sf probe 探测设备 */
sf probe
/* 2.2 将从0x10000000内存地址处开始的0x2000的长度的存储空间都写为ab */
mw.b 10000000 ab 2000
/* 2.3 sf erase 擦除nor flash空间 */
sf erase 0 2000		/* 从nor flash 0地址开始的0x2000长度的空间都执行擦除处理 */
/* 2.4 sf write 写nor flash */
sf write 10000000 0 2000 /* 将0x10000000 内存地址处的长度为0x2000的内存数据写入到nor flash 0地址处 */
/* 2.5 sf read 读nor flash */
sf read 12000000 0 2000 /* 将nor flash地址0处,长度为0x2000的内容读出来放到内存0x12000000地址处 */
/* 2.6 比对写入的数据和读出来的数据是否一致 */
cmp 0x10000000 0x12000000 0x20000

3 在处理的时候需要注意的点

3.1 sf read/write addr

在执行sf read/write的时候,addr必须是一个有效的内存地址,否则在执行读写的时候会触发系统的异常。

3.2 len/addr 数据格式

sf 命令的数据默认是按照16进制去处理的,即使在写的len以及addr不加十六进制的标识也会被识别为十六进制,这点也是需要注意的。

以下是在 U-Boot读写 NOR Flash 的示例代码: 1. NOR Flash 初始化 ```c #include <common.h> #include <command.h> #include <asm/io.h> #include <asm/nor.h> int do_nor_init(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[]) { nor_info_t nor; /* 初始化 NOR Flash */ nor_init(&nor); /* 设置 NOR Flash 为 16-bit 模式 */ nor_set_16bit(&nor); /* 打印 NOR Flash 的信息 */ print_nor_flash_info(&nor); return 0; } U_BOOT_CMD(nor_init, 1, 1, do_nor_init, "Initialize NOR Flash", ""); ``` 2. NOR Flash 读取 ```c #include <common.h> #include <command.h> #include <asm/io.h> #include <asm/nor.h> int do_nor_read(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[]) { nor_info_t nor; ulong addr; int len; uchar *buf; /* 解析参数 */ if (argc != 4) return CMD_RET_USAGE; addr = simple_strtoul(argv[1], NULL, 16); len = simple_strtoul(argv[2], NULL, 10); buf = (uchar *)simple_strtoul(argv[3], NULL, 16); /* 初始化 NOR Flash */ nor_init(&nor); /* 读取 NOR Flash */ nor_read(&nor, addr, len, buf); /* 打印读取的数据 */ print_buffer(addr, buf, 16, len / 16 + 1); return 0; } U_BOOT_CMD(nor_read, 4, 1, do_nor_read, "Read data from NOR Flash", ""); ``` 3. NOR Flash 写入 ```c #include <common.h> #include <command.h> #include <asm/io.h> #include <asm/nor.h> int do_nor_write(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[]) { nor_info_t nor; ulong addr; int len; uchar *buf; /* 解析参数 */ if (argc != 4) return CMD_RET_USAGE; addr = simple_strtoul(argv[1], NULL, 16); len = simple_strtoul(argv[2], NULL, 10); buf = (uchar *)simple_strtoul(argv[3], NULL, 16); /* 初始化 NOR Flash */ nor_init(&nor); /* 写入 NOR Flash */ nor_write(&nor, addr, len, buf); /* 打印写入的数据 */ print_buffer(addr, buf, 16, len / 16 + 1); return 0; } U_BOOT_CMD(nor_write, 4, 1, do_nor_write, "Write data to NOR Flash", ""); ``` 以上代码示例中,假设 NOR Flash 的驱动程序已经在 U-Boot 中实现,并且已经包含在头文件 asm/nor.h 中。nor_info_t 是 NOR Flash 的信息结构体,包含 NOR Flash 的大小、扇区大小、页大小等信息。nor_init() 函数用于初始化 NOR Flash,nor_set_16bit() 函数用于设置 NOR Flash 的模式为 16-bit 模式。nor_read() 函数用于从 NOR Flash 中读取数据,nor_write() 函数用于向 NOR Flash 中写入数据。print_buffer() 函数用于打印读取或写入的数据。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值