From 72d2309674d4ec45f38a4be6cf9a35de68720db9 Mon Sep 17 00:00:00 2001 From: surenyi Date: Sat, 18 May 2019 12:00:40 +0800 Subject: [PATCH] add rx to lib/commands.c Signed-off-by: surenyi --- lib/commands.c | 47 +++++++++++++++++++++++++++++++ lib/serial.c | 2 +- targets/stm32f429-disc/retarget.c | 42 --------------------------- targets/vocoder/retarget.c | 43 ---------------------------- 4 files changed, 48 insertions(+), 86 deletions(-) diff --git a/lib/commands.c b/lib/commands.c index 9dcfb34..170ce31 100644 --- a/lib/commands.c +++ b/lib/commands.c @@ -4,6 +4,8 @@ #include #include "stm32f4xx.h" #include "bkp_sram.h" +#include "flash_if.h" +#include "ymodem.h" #include "cmd.h" static int do_md(cmd_tbl_t s, int argc, char *argv[]) @@ -69,3 +71,48 @@ static int do_upgrade(cmd_tbl_t s, int argc, char *argv[]) CON_CMD(upgrade, 2, "update firmware", do_upgrade); +struct fileinfo { + unsigned int start_address; + unsigned int total; + unsigned int received; +}; + +static int on_info(void *ud, const char *name, unsigned int size) +{ + struct fileinfo *fi = ud; + + (void)name; + fi->total = size; + return flash_if_erase(fi->start_address, fi->total); +} + +static int on_data(void *ud, const void *data, unsigned int len) +{ + struct fileinfo *fi = ud; + int err; + + if (fi->received + len > fi->total) { + len = fi->total - fi->received; + } + + err = flash_if_write(fi->start_address + fi->received, data, len); + fi->received += len; + return err; +} + +static int do_rx(cmd_tbl_t s, int argc, char *argv[]) +{ + struct fileinfo fi; + int err = -1; + + if (argc >= 2) { + fi.start_address = strtoul(argv[1], NULL, 0); + fi.total = 0; + fi.received = 0; + err = stdio_ymodem_recv(&fi, on_info, on_data); + } + return (err); +} + +CON_CMD(rx, 2, "receive file over ymodem", do_rx); + diff --git a/lib/serial.c b/lib/serial.c index 28811f3..a5f2326 100644 --- a/lib/serial.c +++ b/lib/serial.c @@ -748,7 +748,7 @@ void __attribute__((weak)) target_usart6_setup() static void usart6_setup(struct serial *uart) { RCC_APB2PeriphClockCmd(uart->rcc_uart, ENABLE); - target_usart7_setup(); + target_usart6_setup(); } static volatile uint8_t __usart6_rx_buffer[TARGET_USART6_RX_BUFFER_SIZE] __attribute__((section(".rxdma.usart6,\"aw\",%nobits@"))); diff --git a/targets/stm32f429-disc/retarget.c b/targets/stm32f429-disc/retarget.c index 901240b..616cabf 100644 --- a/targets/stm32f429-disc/retarget.c +++ b/targets/stm32f429-disc/retarget.c @@ -51,48 +51,6 @@ static int do_sr2(cmd_tbl_t s, int argc, char *argv[]) CON_CMD(sr2, 2, "read stmpe811 register 2", do_sr2); -struct fileinfo { - char name[256]; - unsigned int total; - unsigned int received; -}; - -static int __on_info(void *user, const char *file, unsigned int _size) -{ - struct fileinfo *fi = user; - - snprintf(fi->name, sizeof fi->name - 1, "%s", file); - fi->total = _size; - fi->received = 0; - return 0; -} - -static int __on_data(void *user, const void *ptr, unsigned int len) -{ - struct fileinfo *fi = user; - - memcpy((void *)(0xd0000000 + fi->received), ptr, len); - - iis_send_blocking(&i2s0, ptr, len); - - fi->received += len; - return 0; -} - -static int do_ymodem_recv(cmd_tbl_t s, int argc, char *argv[]) -{ - struct fileinfo fi; - - memset(&fi, 0, sizeof fi); - - serial_ymodem_recv(&serial0, &fi, __on_info, __on_data); - - printf("name: %s, size: %d, recv: %d\r\n", fi.name, fi.total, fi.received); - return 0; -} - -CON_CMD(rx, 1, "recv from ymodem", do_ymodem_recv); - static int do_play(cmd_tbl_t s, int argc, char *argv[]) { volatile uint8_t *addr = (volatile uint8_t *)0xd0000000; diff --git a/targets/vocoder/retarget.c b/targets/vocoder/retarget.c index bc57714..e94cfbe 100644 --- a/targets/vocoder/retarget.c +++ b/targets/vocoder/retarget.c @@ -71,49 +71,6 @@ static int do_rom_read(cmd_tbl_t s, int argc, char *argv[]) CON_CMD(rr, 3, "rom read", do_rom_read); -struct fileinfo { - char name[256]; - unsigned int total; - unsigned int received; -}; - -static int __on_info(void *user, const char *file, unsigned int _size) -{ - struct fileinfo *fi = user; - - snprintf(fi->name, sizeof fi->name - 1, "%s", file); - fi->total = _size; - fi->received = 0; - return 0; -} - -static int __on_data(void *user, const void *ptr, unsigned int len) -{ - struct fileinfo *fi = user; - - memcpy((void *)(0xc0000000 + fi->received), ptr, len); - fi->received += len; - return 0; -} - -static int do_ymodem_recv(cmd_tbl_t s, int argc, char *argv[]) -{ - struct fileinfo fi; - - memset(&fi, 0, sizeof fi); - - iwdg_set_period_ms(60000); - - serial_ymodem_recv(&serial0, &fi, __on_info, __on_data); - - iwdg_set_period_ms(1000); - - printf("name: %s, size: %d, recv: %d\r\n", fi.name, fi.total, fi.received); - return 0; -} - -CON_CMD(rx, 1, "recv from ymodem", do_ymodem_recv); - static int do_eer(cmd_tbl_t s, int argc, char *argv[]) { uint16_t addr;