[feat] add uart process flash function

This commit is contained in:
zhji 2025-05-04 21:16:41 +08:00
parent 5470f0d5c3
commit cf55c5cc2a
2 changed files with 69 additions and 10 deletions

View File

@ -76,6 +76,66 @@ struct uart_cfg_s uart_cfg = {
.rx_fifo_level = UART_FIFO_LEVEL_1_2, .rx_fifo_level = UART_FIFO_LEVEL_1_2,
}; };
void process_return_ok(void)
{
uart_write_block(UART_ID_0, (uint8_t *)"OKAY", 4);
}
void process_return_fail(void)
{
uart_write_block(UART_ID_0, (uint8_t *)"FAIL", 4);
}
void process_flash_erase(uint32_t addr, uint32_t length)
{
addr = addr & (~(FLASH_ERASE_SIZE - 1));
length = (length + FLASH_ERASE_SIZE - 1) / FLASH_ERASE_SIZE;
for (int i = 0; i < length; i++) {
flash_erase(addr + i * FLASH_ERASE_SIZE);
}
}
void process_flash_write(uint32_t addr, uint8_t *data, uint32_t length)
{
uint32_t offset;
uint32_t chunk_size;
offset = addr & (FLASH_WRITE_SIZE - 1);
if (offset) {
chunk_size = FLASH_WRITE_SIZE - offset;
if (chunk_size > length) {
chunk_size = length;
}
flash_write(addr, data, chunk_size);
addr += chunk_size;
data += chunk_size;
length -= chunk_size;
}
while (length >= FLASH_WRITE_SIZE) {
flash_write(addr, data, FLASH_WRITE_SIZE);
addr += FLASH_WRITE_SIZE;
data += FLASH_WRITE_SIZE;
length -= FLASH_WRITE_SIZE;
}
if (length > 0) {
flash_write(addr, data, length);
}
}
void process_flash_read(uint32_t addr, uint8_t *data, uint32_t length)
{
while (length >= FLASH_READ_SIZE) {
flash_read(addr, data, FLASH_READ_SIZE);
uart_write_block(UART_ID_0, data, FLASH_READ_SIZE);
addr += FLASH_READ_SIZE;
length -= FLASH_READ_SIZE;
}
if (length > 0) {
flash_read(addr, data, length);
uart_write_block(UART_ID_0, data, length);
}
}
void uart_process(uint16_t code, uint16_t length) void uart_process(uint16_t code, uint16_t length)
{ {
uint8_t *p; uint8_t *p;
@ -100,18 +160,17 @@ void uart_process(uint16_t code, uint16_t length)
} }
} }
v1 = ((uint32_t)p[0] << 24) | ((uint32_t)p[1] << 16) | ((uint32_t)p[2] << 8) | ((uint32_t)p[3] << 0);
v2 = ((uint32_t)p[4] << 24) | ((uint32_t)p[5] << 16) | ((uint32_t)p[6] << 8) | ((uint32_t)p[7] << 0);
if (code == 0x0001) { if (code == 0x0001) {
v1 = ((uint32_t)p[0] << 24) | ((uint32_t)p[1] << 16) | ((uint32_t)p[2] << 8) | ((uint32_t)p[3] << 0); process_flash_erase(v1, v2);
v2 = ((uint32_t)p[4] << 24) | ((uint32_t)p[5] << 16) | ((uint32_t)p[6] << 8) | ((uint32_t)p[7] << 0); process_return_ok();
printf("erase, addr=0x%08lX, length = %ld\r\n", v1, v2);
} else if (code == 0x0002) { } else if (code == 0x0002) {
v1 = ((uint32_t)p[0] << 24) | ((uint32_t)p[1] << 16) | ((uint32_t)p[2] << 8) | ((uint32_t)p[3] << 0); process_flash_write(v1, p + 8, v2);
v2 = ((uint32_t)p[4] << 24) | ((uint32_t)p[5] << 16) | ((uint32_t)p[6] << 8) | ((uint32_t)p[7] << 0); process_return_ok();
printf("write, addr=0x%08lX, length = %ld\r\n", v1, v2);
} else if (code == 0x0003) { } else if (code == 0x0003) {
v1 = ((uint32_t)p[0] << 24) | ((uint32_t)p[1] << 16) | ((uint32_t)p[2] << 8) | ((uint32_t)p[3] << 0); process_return_ok();
v2 = ((uint32_t)p[4] << 24) | ((uint32_t)p[5] << 16) | ((uint32_t)p[6] << 8) | ((uint32_t)p[7] << 0); process_flash_read(v1, uart_tx_buffer, v2);
printf("read, addr=0x%08lX, length = %ld\r\n", v1, v2);
} }
} }
@ -147,7 +206,6 @@ void uart_state_machine(uint8_t id)
uart_rx_length = 0; uart_rx_length = 0;
return; return;
} }
printf("%02X,", uart_rx_buffer[uart_rx_length-1]);
} }
} }

View File

@ -12,6 +12,7 @@
#define FLASHCMD_READ_JEDEC_ID (0x9F) #define FLASHCMD_READ_JEDEC_ID (0x9F)
#define FLASH_WRITE_SIZE (256) #define FLASH_WRITE_SIZE (256)
#define FLASH_READ_SIZE (256)
#define FLASH_ERASE_SIZE (4096) #define FLASH_ERASE_SIZE (4096)
#ifdef __cplusplus #ifdef __cplusplus