fix: auto_erase/verif?

This commit is contained in:
2024-12-14 13:46:49 +06:00
parent 0071f68cb6
commit a0072d3d3a
5 changed files with 110 additions and 48 deletions

View File

@@ -1,4 +1,7 @@
source [find target/swj-dp.tcl]
source [find interface/jlink.cfg]
adapter driver jlink
transport select swd
if { [info exists CHIPNAME] } {
set _CHIPNAME $CHIPNAME
@@ -33,11 +36,8 @@ target create $_TARGETNAME cortex_m -endian $_ENDIAN -dap $_CHIPNAME.dap
# todo: get work area from makefile
$_TARGETNAME configure -work-area-phys 0x10001500 -work-area-size $_WORKAREASIZE -work-area-backup 0
adapter speed 4000
adapter speed 3000
adapter srst delay 100
if {![using_hla]} {
cortex_m reset_config sysresetreq
}
cortex_m reset_config vectreset
$_TARGETNAME configure -event reset-init {amebaz_init}

View File

@@ -46,6 +46,7 @@ int __attribute__((section(".vectors"))) main(void) {
/* clear block protection */
FLASH_SetStatusBits(0x1c, 0);
printf("[FLASH] Block protection cleared\n");
// uint32_t erase_sector_addr = 0;
while (1) {
FLASH_CONTROL->start = 0;
@@ -81,6 +82,13 @@ int __attribute__((section(".vectors"))) main(void) {
for (uint32_t offset = 0; offset < FLASH_CONTROL->len; offset += 4) {
uint32_t write_addr = FLASH_CONTROL->offset + offset;
uint32_t sector_addr1 = write_addr & 0xFFFFF000;
// if (sector_addr1 >=
// erase_sector_addr) { // relate to rtl8710_flasher_auto_erase
// FLASH_Erase(2, sector_addr1);
// erase_sector_addr =
// sector_addr1 + 0x1000; // next sector we should erase
// }
FLASH_TxData12B(write_addr, 4,
(uint8_t *)&FLASH_CONTROL->data[offset]);
}
@@ -88,24 +96,39 @@ int __attribute__((section(".vectors"))) main(void) {
printf("[FLASHER] Write completed successfully\n");
break;
case CMD_VERIFY:
for (uint32_t i = 0; i < FLASH_CONTROL->len; i += VERIFY_CHUNK_SIZE) {
size_t bytes_to_compare = MIN(4, FLASH_CONTROL->len - i);
const void *flash_addr =
(const void *)(SPI_FLASH_BASE + FLASH_CONTROL->offset + i);
case CMD_VERIFY: {
uint8_t tbuf[4];
printf("Verify: FLASH_CONTROL->data = %p\n", FLASH_CONTROL->data);
printf("Verify: Reading from flash base %p\n", (void *)SPI_FLASH_BASE);
printf("Verify: With offset %x\n", FLASH_CONTROL->offset);
if (memcmp(flash_addr, (const void *)&FLASH_CONTROL->data[i],
bytes_to_compare) != 0) {
for (uint32_t i = 0; i < FLASH_CONTROL->len; i += 4) {
volatile uint32_t *flash_addr =
(volatile uint32_t *)(SPI_FLASH_BASE + FLASH_CONTROL->offset + i);
*((uint32_t *)tbuf) = *flash_addr;
size_t l = MIN(4, FLASH_CONTROL->len - i);
size_t k;
for (k = 0; k < l; k++) {
if (tbuf[k] != FLASH_CONTROL->data[i + k]) {
printf("Verify mismatch at offset %x: flash=%02x buf=%02x\n",
i + k, tbuf[k], FLASH_CONTROL->data[i + k]);
break;
}
}
if (k < l) {
FLASH_CONTROL->status = 1;
FLASH_CONTROL->param = i;
break;
}
}
break;
}
default:
FLASH_CONTROL->status = 1;
break;
}
}
}
}

View File

@@ -10,8 +10,11 @@
#define MIN(a, b) ((a) < (b) ? (a) : (b))
#define BIT(x) (1 << (x))
#define READ_CHUNK_SIZE 32
#define VERIFY_CHUNK_SIZE 32
#define READ_CHUNK_SIZE 4
#define FLASH_WORD_SIZE 4
#define VERIFY_CHUNK_SIZE 4
#define VERIFY_WORDS (VERIFY_CHUNK_SIZE / FLASH_WORD_SIZE)
#define WRITE_CHUNK_SIZE 256
#define SPI_FLASH_BASE 0x08000000
@@ -129,4 +132,4 @@ __always_long_call void FLASH_StructInit_GD(void *FLASH_InitStruct);
__always_long_call uint8_t FLASH_Init(uint8_t SpicBitMode);
__always_long_call uint32_t FLASH_ClockDiv(uint8_t Div);
#endif /* _RTL8710_FLASHER_H_ */
#endif // _RTL8710_FLASHER_H_

View File

@@ -11,7 +11,7 @@ set rtl8710_flasher_ready 0
set rtl8710_flasher_capacity 0
set rtl8710_flasher_auto_erase 0
set rtl8710_flasher_auto_verify 0
set rtl8710_flasher_auto_erase_sector 0xFFFFFFFF
set rtl8710_flasher_auto_erase_sector 0
proc rtl8710_flasher_init {} {
global rtl8710_flasher_ready rtl8710_flasher_firmware_ptr
@@ -52,10 +52,14 @@ proc rtl8710_flasher_wait {} {
while {[rtl8710_flasher_mrw $addr]} {}
}
proc rtl8710_flasher_load_block {local_filename offset length} {
# proc rtl8710_flasher_load_block {local_filename offset length} {
# global rtl8710_flasher_buffer
# set buffer_addr [expr {$rtl8710_flasher_buffer + 0x20}]
# load_image $local_filename [expr {$buffer_addr - $offset}] bin $buffer_addr $length
# }
proc rtl8710_flasher_load_block {local_filename offset len} {
global rtl8710_flasher_buffer
set buffer_addr [expr {$rtl8710_flasher_buffer + 0x20}]
load_image $local_filename [expr {$buffer_addr - $offset}] bin $buffer_addr $length
load_image $local_filename [expr {$rtl8710_flasher_buffer + 0x20 - $offset}] bin [expr {$rtl8710_flasher_buffer + 0x20}] $len
}
proc rtl8710_flasher_block {command offset len} {
@@ -95,8 +99,25 @@ proc rtl8710_flasher_write_block {offset len} {
rtl8710_flasher_block "write" $offset $len
}
# proc rtl8710_flasher_verify_block {offset len} {
# rtl8710_flasher_block "verify" $offset $len
# }
proc rtl8710_flasher_verify_block {offset len} {
rtl8710_flasher_block "verify" $offset $len
global rtl8710_flasher_buffer
global rtl8710_flasher_command_verify
mww [expr {$rtl8710_flasher_buffer + 0x04}] $rtl8710_flasher_command_verify
mww [expr {$rtl8710_flasher_buffer + 0x08}] 0x00000000
mww [expr {$rtl8710_flasher_buffer + 0x10}] $offset
mww [expr {$rtl8710_flasher_buffer + 0x14}] $len
mww [expr {$rtl8710_flasher_buffer + 0x00}] 0x00000001
rtl8710_flasher_wait
set status [rtl8710_flasher_mrw [expr {$rtl8710_flasher_buffer + 0x08}]]
if {[expr {$status > 0}]} {
set status [rtl8710_flasher_mrw [expr {$rtl8710_flasher_buffer + 0x0C}]]
set status [expr {$status + $offset}]
error "verify error, offset $status"
}
}
proc rtl8710_flash_read_id {} {
@@ -170,27 +191,24 @@ proc rtl8710_flash_write {local_filename loc} {
global rtl8710_flasher_buffer_size rtl8710_flasher_sector_size
global rtl8710_flasher_auto_erase rtl8710_flasher_auto_verify
global rtl8710_flasher_auto_erase_sector
rtl8710_flasher_init
set size [file size $local_filename]
for {set offset 0} {$offset < $size} {set offset [expr {$offset + $rtl8710_flasher_buffer_size}]} {
set len [expr {($size - $offset) < $rtl8710_flasher_buffer_size ? ($size - $offset) : $rtl8710_flasher_buffer_size}]
set flash_offset [expr {$loc + $offset}]
echo "write offset $flash_offset"
rtl8710_flasher_load_block $local_filename $offset $len
if {$rtl8710_flasher_auto_erase} {
set start_sector [expr {$flash_offset / $rtl8710_flasher_sector_size}]
set end_sector [expr {($flash_offset + $len - 1) / $rtl8710_flasher_sector_size}]
# erase any new sectors we encounter
for {set sector $start_sector} {$sector <= $end_sector} {incr sector} {
if {$rtl8710_flasher_auto_erase_sector != $sector} {
echo "erase sector $sector"
rtl8710_flash_sector_erase [expr {$sector * $rtl8710_flasher_sector_size}]
set rtl8710_flasher_auto_erase_sector $sector
set sector_addr [expr {$sector * $rtl8710_flasher_sector_size}]
if {$sector_addr >= $rtl8710_flasher_auto_erase_sector} {
rtl8710_flash_sector_erase $sector_addr
set rtl8710_flasher_auto_erase_sector [expr {$sector_addr + $rtl8710_flasher_sector_size}]
}
}
}

View File

@@ -1,4 +1,7 @@
source [find target/swj-dp.tcl]
source [find interface/jlink.cfg]
adapter driver jlink
transport select swd
if { [info exists CHIPNAME] } {
set _CHIPNAME $CHIPNAME
@@ -33,12 +36,9 @@ target create $_TARGETNAME cortex_m -endian $_ENDIAN -dap $_CHIPNAME.dap
# todo: get work area from makefile
$_TARGETNAME configure -work-area-phys 0x10001500 -work-area-size $_WORKAREASIZE -work-area-backup 0
adapter speed 4000
adapter speed 3000
adapter srst delay 100
if {![using_hla]} {
cortex_m reset_config sysresetreq
}
cortex_m reset_config vectreset
$_TARGETNAME configure -event reset-init {amebaz_init}
set rtl8710_flasher_firmware_ptr 0x10001500
@@ -46,7 +46,7 @@ set rtl8710_flasher_buffer 0x10008000
set rtl8710_flasher_buffer_size 204800
set rtl8710_flasher_sector_size 4096
set rtl8710_flasher_code [list 0x489fe92d 0x2300b673 0xf04f9303 0x4e604380 0x3210f8d3 0x0f10f013 0x485ed001 0x4c5e47b0 0x4f5f4d5e 0xf44f2200 0x20107180 0x4b5d47a0 0x47982000 0xf44f2201 0x20107180 0x220147a0 0x4b592100 0x200a4c59 0x4b594798 0x47984628 0x20024b58 0xf8954798 0xaa030049 0x47a02103 0x300ef89d 0x200df89d 0x100cf89d 0x47b04852 0x48526829 0x68ab47b0 0x7929796a 0x47b04850 0x7f6a7feb 0x93007f29 0x7fab484e 0x4b4e47b0 0x201c2100 0x484d4798 0x46a147b0 0xf04f4c4c 0xf8c40a00 0x6823a000 0x6863b193 0xd86c2b05 0xf852a201 0xbf00f023 0x100015d5 0x100015e5 0x100015f3 0x10001619 0x1000161d 0x10001685 0xe7e8bf00 0x0049f895 0x2104aa03 0x9b0347c8 0xe7de60e3 0x46082100 0x47984b3a 0x47b0483a 0x6921e7d7 0x20024b37 0xe7d24798 0xf1026922 0x58d16200 0x5280f103 0x4200f502 0x62113304 0x429a6962 0xe7c4d8f2 0xe7f92300 0x482f6961 0xf8df6922 0x47b0b0bc 0x0800f04f 0x45436963 0x482cd801 0x6920e7db 0x0207eb08 0x21044440 0xf10847d8 0xe7f10804 0x0820f108 0x45436963 0x6963d9a7 0x0308eba3 0xbf982b04 0x69206962 0x6000f100 0xeba2bf94 0x22040208 0x0107eb08 0xf0004440 0x2800f839 0x2301d0e6 0xf8c460a3 0xe78e800c 0x0800f04f 0x2301e7e0 0xe78860a3 0x000004dd 0x10001744 0x00009b65 0x10000dec 0x10008020 0x00007d15 0x000042fd 0x00007465 0x00007b81 0x00007ca1 0x10001774 0x10001797 0x100017af 0x100017e2 0x0000784d 0x1000181a 0x10008000 0x00007755 0x1000183c 0x10001861 0x00007661 0x10001898 0xb4302a03 0xea41d912 0x07a40400 0x460b4684 0x4619d121 0xf8534660 0xf85c4b04 0x42a55b04 0x3a04d119 0xd8f42a03 0x46194660 0xb17a1e54 0x39011e43 0xe0011902 0xd009429a 0x0f01f813 0xcf01f811 0xd0f74560 0x000ceba0 0x4770bc30 0xbc302000 0x1e544770 0xbf00e7ea 0x414c465b 0x52454853 0x6c46205d 0x20687361 0x69726570 0x72656870 0x61206c61 0x6165726c 0x65207964 0x6c62616e 0x2e2e6465 0x000a3f2e 0x414c465b 0x205d4853 0x69766544 0x49206563 0x25203a44 0x20583230 0x58323025 0x32302520 0x5b000a58 0x53414c46 0x56205d48 0x6f646e65 0x30203a72 0x38302578 0x5b000a58 0x53414c46 0x43205d48 0x69666e6f 0x4d203a67 0x3d65646f 0x202c6425 0x636f6c43 0x64253d6b 0x6552202c 0x6d436461 0x78303d64 0x58323025 0x465b000a 0x4853414c 0x6954205d 0x676e696d 0x6153203a 0x656c706d 0x616c6544 0x64253d79 0x7544202c 0x43796d6d 0x656c6379 0x255b3d73 0x64252c64 0x5d64252c 0x465b000a 0x4853414c 0x6c42205d 0x206b636f 0x746f7270 0x69746365 0x63206e6f 0x7261656c 0x000a6465 0x414c465b 0x52454853 0x7546205d 0x63206c6c 0x20706968 0x73617265 0x6f632065 0x656c706d 0x0a646574 0x4c465b00 0x45485341 0x53205d52 0x74726174 0x20676e69 0x74697277 0x666f2065 0x20642520 0x65747962 0x74612073 0x66666f20 0x20746573 0x30257830 0x000a5838 0x414c465b 0x52454853 0x7257205d 0x20657469 0x706d6f63 0x6574656c 0x75732064 0x73656363 0x6c756673 0x000a796c ]
set rtl8710_flasher_code [list 0xb673b58f 0x93022300 0x4380f04f 0xf8d34d67 0xf0133210 0xd0010f10 0x47a84865 0x4e664c65 0xf44f2200 0x20107180 0x4b6447a0 0x47982000 0xf44f2201 0x20107180 0x220147a0 0x4b602100 0x200a4c60 0x4b604798 0x47984630 0x20024b5f 0xf8964798 0xaa020049 0x47a02103 0x300af89d 0x2009f89d 0x1008f89d 0x47a84859 0x48596831 0x68b347a8 0x79317972 0x47a84857 0x7f727ff3 0x93007f31 0x7fb34855 0x4b5547a8 0x201c2100 0x48544798 0x46a047a8 0x23004c53 0x68236023 0x6863b193 0xd87e2b05 0xf852a201 0xbf00f023 0x100015cd 0x100015dd 0x100015eb 0x10001611 0x10001615 0x1000163f 0xe7e8bf00 0x0049f896 0x2104aa02 0x9b0247c0 0xe7de60e3 0x46082100 0x47984b42 0x47a84842 0x6921e7d7 0x20024b3f 0xe7d24798 0xf1026922 0x58d16200 0x5280f103 0x4200f502 0x62113304 0x429a6962 0xe7c4d8f2 0xe7f92300 0x48376961 0xf8df6922 0x47a890dc 0x27004b36 0x42ba6962 0x4835d801 0x6920e7db 0x443818fa 0x47c82104 0x4b303704 0x492fe7f2 0x47a84830 0x6100f04f 0x47a8482f 0x482f6921 0x270047a8 0x42bb6963 0x6923d99f 0x6300f103 0x0c0cf10d 0x930359db 0x1bdb6963 0xbf962b04 0x23046963 0x20001bdb 0xd1014298 0xe7e93704 0xeb041839 0xf81c0e01 0xf89e2b01 0x454a9020 0xf89ed007 0x481e3020 0x230147a8 0x60e760a3 0x3001e77b 0x2301e7e8 0xe77660a3 0x000004dd 0x10001714 0x00009b65 0x10000dec 0x00007d15 0x000042fd 0x00007465 0x00007b81 0x00007ca1 0x10001744 0x10001767 0x1000177f 0x100017b2 0x0000784d 0x100017ea 0x10008000 0x00007755 0x1000180c 0x10001831 0x00007661 0x10008020 0x10001868 0x10001890 0x100018b2 0x100018d6 0x100018ee 0x414c465b 0x52454853 0x6c46205d 0x20687361 0x69726570 0x72656870 0x61206c61 0x6165726c 0x65207964 0x6c62616e 0x2e2e6465 0x000a3f2e 0x414c465b 0x205d4853 0x69766544 0x49206563 0x25203a44 0x20583230 0x58323025 0x32302520 0x5b000a58 0x53414c46 0x56205d48 0x6f646e65 0x30203a72 0x38302578 0x5b000a58 0x53414c46 0x43205d48 0x69666e6f 0x4d203a67 0x3d65646f 0x202c6425 0x636f6c43 0x64253d6b 0x6552202c 0x6d436461 0x78303d64 0x58323025 0x465b000a 0x4853414c 0x6954205d 0x676e696d 0x6153203a 0x656c706d 0x616c6544 0x64253d79 0x7544202c 0x43796d6d 0x656c6379 0x255b3d73 0x64252c64 0x5d64252c 0x465b000a 0x4853414c 0x6c42205d 0x206b636f 0x746f7270 0x69746365 0x63206e6f 0x7261656c 0x000a6465 0x414c465b 0x52454853 0x7546205d 0x63206c6c 0x20706968 0x73617265 0x6f632065 0x656c706d 0x0a646574 0x4c465b00 0x45485341 0x53205d52 0x74726174 0x20676e69 0x74697277 0x666f2065 0x20642520 0x65747962 0x74612073 0x66666f20 0x20746573 0x30257830 0x000a5838 0x414c465b 0x52454853 0x7257205d 0x20657469 0x706d6f63 0x6574656c 0x75732064 0x73656363 0x6c756673 0x000a796c 0x69726556 0x203a7966 0x53414c46 0x4f435f48 0x4f52544e 0x643e2d4c 0x20617461 0x7025203d 0x6556000a 0x79666972 0x6552203a 0x6e696461 0x72662067 0x66206d6f 0x6873616c 0x73616220 0x70252065 0x6556000a 0x79666972 0x6957203a 0x6f206874 0x65736666 0x78252074 0x6556000a 0x79666972 0x73696d20 0x6374616d 0x74612068 0x66666f20 0x20746573 0x203a7825 0x73616c66 0x30253d68 0x62207832 0x253d6675 0x0a783230 0x00000000 ]
set rtl8710_flasher_command_read_id 0
set rtl8710_flasher_command_mass_erase 1
@@ -61,7 +61,7 @@ set rtl8710_flasher_ready 0
set rtl8710_flasher_capacity 0
set rtl8710_flasher_auto_erase 0
set rtl8710_flasher_auto_verify 0
set rtl8710_flasher_auto_erase_sector 0xFFFFFFFF
set rtl8710_flasher_auto_erase_sector 0
proc rtl8710_flasher_init {} {
global rtl8710_flasher_ready rtl8710_flasher_firmware_ptr
@@ -102,10 +102,14 @@ proc rtl8710_flasher_wait {} {
while {[rtl8710_flasher_mrw $addr]} {}
}
proc rtl8710_flasher_load_block {local_filename offset length} {
# proc rtl8710_flasher_load_block {local_filename offset length} {
# global rtl8710_flasher_buffer
# set buffer_addr [expr {$rtl8710_flasher_buffer + 0x20}]
# load_image $local_filename [expr {$buffer_addr - $offset}] bin $buffer_addr $length
# }
proc rtl8710_flasher_load_block {local_filename offset len} {
global rtl8710_flasher_buffer
set buffer_addr [expr {$rtl8710_flasher_buffer + 0x20}]
load_image $local_filename [expr {$buffer_addr - $offset}] bin $buffer_addr $length
load_image $local_filename [expr {$rtl8710_flasher_buffer + 0x20 - $offset}] bin [expr {$rtl8710_flasher_buffer + 0x20}] $len
}
proc rtl8710_flasher_block {command offset len} {
@@ -145,8 +149,25 @@ proc rtl8710_flasher_write_block {offset len} {
rtl8710_flasher_block "write" $offset $len
}
# proc rtl8710_flasher_verify_block {offset len} {
# rtl8710_flasher_block "verify" $offset $len
# }
proc rtl8710_flasher_verify_block {offset len} {
rtl8710_flasher_block "verify" $offset $len
global rtl8710_flasher_buffer
global rtl8710_flasher_command_verify
mww [expr {$rtl8710_flasher_buffer + 0x04}] $rtl8710_flasher_command_verify
mww [expr {$rtl8710_flasher_buffer + 0x08}] 0x00000000
mww [expr {$rtl8710_flasher_buffer + 0x10}] $offset
mww [expr {$rtl8710_flasher_buffer + 0x14}] $len
mww [expr {$rtl8710_flasher_buffer + 0x00}] 0x00000001
rtl8710_flasher_wait
set status [rtl8710_flasher_mrw [expr {$rtl8710_flasher_buffer + 0x08}]]
if {[expr {$status > 0}]} {
set status [rtl8710_flasher_mrw [expr {$rtl8710_flasher_buffer + 0x0C}]]
set status [expr {$status + $offset}]
error "verify error, offset $status"
}
}
proc rtl8710_flash_read_id {} {
@@ -220,27 +241,24 @@ proc rtl8710_flash_write {local_filename loc} {
global rtl8710_flasher_buffer_size rtl8710_flasher_sector_size
global rtl8710_flasher_auto_erase rtl8710_flasher_auto_verify
global rtl8710_flasher_auto_erase_sector
rtl8710_flasher_init
set size [file size $local_filename]
for {set offset 0} {$offset < $size} {set offset [expr {$offset + $rtl8710_flasher_buffer_size}]} {
set len [expr {($size - $offset) < $rtl8710_flasher_buffer_size ? ($size - $offset) : $rtl8710_flasher_buffer_size}]
set flash_offset [expr {$loc + $offset}]
echo "write offset $flash_offset"
rtl8710_flasher_load_block $local_filename $offset $len
if {$rtl8710_flasher_auto_erase} {
set start_sector [expr {$flash_offset / $rtl8710_flasher_sector_size}]
set end_sector [expr {($flash_offset + $len - 1) / $rtl8710_flasher_sector_size}]
# erase any new sectors we encounter
for {set sector $start_sector} {$sector <= $end_sector} {incr sector} {
if {$rtl8710_flasher_auto_erase_sector != $sector} {
echo "erase sector $sector"
rtl8710_flash_sector_erase [expr {$sector * $rtl8710_flasher_sector_size}]
set rtl8710_flasher_auto_erase_sector $sector
set sector_addr [expr {$sector * $rtl8710_flasher_sector_size}]
if {$sector_addr >= $rtl8710_flasher_auto_erase_sector} {
rtl8710_flash_sector_erase $sector_addr
set rtl8710_flasher_auto_erase_sector [expr {$sector_addr + $rtl8710_flasher_sector_size}]
}
}
}