openocd-flash-program

介绍通过openocd来烧写flash。

编译openocd

修改src/flash/nor/spi.c,以增加相应flash model的支持。

1
2
3
4
/* name, read_cmd, qread_cmd, pprog_cmd, erase_cmd, chip_erase_cmd, device_id,
* pagesize, sectorsize, size_in_bytes */
// add by francis
FLASH_ID("mac 66u1g45g", 0x03, 0x8b, 0x02, 0x20, 0xc7, 0x003b25c2, 0x100, 0x1000, 0x8000000),

编译openocd

1
2
3
4
5
> ./bootstrap
> ./configure --prefix=/home/francis.zheng/openocd/ --enable-remote-bitbang
> make
> make install
> openocd -v

配置openocd

在openocd配置中增加flash相关信息

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
adapter speed 29000

source [find interface/ftdi/olimex-arm-usb-tiny-h.cfg]

set _CHIPNAME riscv
jtag newtap $_CHIPNAME cpu -irlen 5 -expected-id 0x00000cfd

set _TARGETNAME $_CHIPNAME.cpu
target create $_TARGETNAME riscv -chain-position $_TARGETNAME -rtos hwthread

riscv set_mem_access progbuf

echo "configure work area..."
$_TARGETNAME configure -work-area-phys 0x80000000 -work-area-size 0x80000000 -work-area-backup 0
echo "configure work area complete"

flash bank onboard_spi_flash fespi 0x20000000 0 0 0 $_TARGETNAME 0x10010000
echo "configure flash bank complete"

init
reset init
halt
echo "Ready for Remote Connections"

执行openocd

1
$ openocd -f demo.cfg

执行telnet

这里必须用telnet,而不能用gdb

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
$ telnet 127.0.0.1 4444
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
Open On-Chip Debugger
> flash probe 0
Found flash device 'mac 66u1g45g' (ID 0x003b25c2)
flash 'fespi' found at 0x20000000

> flash write_image erase demo.cfg 0x21000000 bin
Disabling abstract command writes to CSRs.
auto erase enabled
wrote 4096 bytes from file demo.cfg in 11.039106s (0.362 KiB/s)

> flash verify_bank 0 demo.cfg 0x0

另外,好像openocd和olimex如果跑在虚拟机上,下载速度会很慢,如果在local跑,会快不少。