diff --git a/iap/iap.c b/iap/iap.c index 3d296f1..c4c2523 100644 --- a/iap/iap.c +++ b/iap/iap.c @@ -6,6 +6,7 @@ #define IAP_FLASH_APP_SEC "app" #define IAP_FLASH_BOOT_SEC "boot" +static rt_device_t console_dev; struct rt_completion _wait; #define IS_AF(c) ((c >= 'A') && (c <= 'F')) @@ -316,56 +317,44 @@ static const rt_uint8_t *_pack_cmd(enum iap_v5_cmd cmd) return _cmd_line; } -#define _response_cmd(console_dev, cmd) \ +#define _response_cmd(cmd) \ rt_device_write(console_dev, 0, _pack_cmd(cmd), IAP_V5_CMD_SIZE) -static void jump_to_app(void); -extern const uint32_t IapAppAddr; - -#define PORTS_NUM (10) // uart1 - uart10 - -static rt_device_t _read_ports(rt_device_t ports[], rt_uint8_t *pch) +static rt_bool_t iap_v5_req_char(rt_device_t console_dev, + struct rt_completion *_wait, + rt_uint8_t want, + rt_tick_t wait) { - for (int i = 0; i < PORTS_NUM; i++) + rt_uint8_t ch; + rt_tick_t start = rt_tick_get(); + rt_bool_t rt = RT_FALSE; + + while (rt_tick_get() - start < wait) { - while (ports[i]) + if (rt_device_read(console_dev, 0, &ch, 1) == 1) { - if (rt_device_read(ports[i], 0, pch, 1) != 1) + if (ch == want) { + rt = RT_TRUE; break; } - if (*pch == '1' || *pch == 'q') - { - return ports[i]; - } + continue; } - } - return RT_NULL; -} - -static void _print_ports(rt_device_t ports[], const char *fmt, ...) -{ - va_list args; - char line_buf[128] = { 0 }; - rt_size_t real; - - va_start(args, fmt); - real = rt_vsnprintf(line_buf, sizeof(line_buf), fmt, args); - va_end(args); - - for (int i = 0; i < PORTS_NUM; i++) - { - if (ports[i] == RT_NULL) + if (rt_completion_wait(_wait, wait - (rt_tick_get() - start)) + != RT_EOK) { break; } - - rt_device_write(ports[i], 0, line_buf, real); } + + return rt; } +static void jump_to_app(void); +extern const uint32_t IapAppAddr; + static int _iap_entry(rt_bool_t is_boot); int iap_main_entry(void) @@ -387,123 +376,31 @@ static int _iap_entry(rt_bool_t is_boot) finsh_set_prompt("msh (IAP)"); } + rt_err_t (*odev_rx_ind)(rt_device_t dev, rt_size_t size); rt_err_t err = RT_EOK; struct custom_ctx *ctx = RT_NULL; - rt_device_t console = rt_console_get_device(); - RT_ASSERT(console); - rt_device_t ports[10] = {0}; - char name[RT_NAME_MAX] = {0}; + console_dev = rt_console_get_device(); + RT_ASSERT(console_dev); rt_completion_init(&_wait); + odev_rx_ind = console_dev->rx_indicate; + rt_device_set_rx_indicate(console_dev, port_rx_ind); - rt_bool_t _wait_mode = RT_FALSE; - rt_bool_t _continue_run = RT_FALSE; - rt_err_t (*odev_rx_ind)(rt_device_t dev, rt_size_t size); - - for (int i = 0; i < 10 && is_boot; i++) + if (!iap_v5_req_char(console_dev, &_wait, '1', is_boot? 1000: 100000)) { - rt_snprintf(name, RT_NAME_MAX, "uart%d", i+1); - ports[i] = rt_device_find(name); - RT_ASSERT(ports[i]); - - struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT; - config.baud_rate = BAUD_RATE_460800; - rt_device_control(ports[i], RT_DEVICE_CTRL_CONFIG, &config); - - rt_device_set_rx_indicate(ports[i], port_rx_ind); - rt_device_open(ports[i], RT_DEVICE_OFLAG_RDWR | RT_DEVICE_FLAG_INT_RX); + goto _exit; } + _response_cmd(IAP_V5_CMD_ONE_REPLY); - if (!is_boot) + rt_thread_mdelay(200); + if (!iap_v5_req_char(console_dev, &_wait, 'c', 1000)) { - ports[0] = rt_console_get_device(); - RT_ASSERT(ports[0]); - - odev_rx_ind = ports[0]->rx_indicate; - rt_device_set_rx_indicate(ports[0], port_rx_ind); - - struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT; - config.baud_rate = BAUD_RATE_460800; - rt_device_control(ports[0], RT_DEVICE_CTRL_CONFIG, &config); - - rt_device_open(ports[0], RT_DEVICE_OFLAG_RDWR | RT_DEVICE_FLAG_INT_RX); - } - - rt_device_t get_port = RT_NULL; - rt_uint8_t wait_cnt = 3; - rt_uint8_t ch; - - _print_ports(ports, "iap loop ..."); - - while (wait_cnt > 0 || !is_boot) - { - get_port = _read_ports(ports, &ch); - - if (get_port != RT_NULL) - { - if (ch == 'q') - { - _continue_run = RT_TRUE; - break; - } - - if (ch == '1') - { - _wait_mode = RT_TRUE; - _response_cmd(get_port, IAP_V5_CMD_ONE_REPLY); - break; - } - } - else - { - wait_cnt--; - } - - rt_completion_wait(&_wait, rt_tick_from_millisecond(100)); - } - - if (get_port == RT_NULL) - { - get_port = console; - } - - for (int i = 0; i < PORTS_NUM && is_boot; i++) - { - if (ports[i] == get_port) - { - continue; - } - - rt_device_set_rx_indicate(ports[i], RT_NULL); - rt_device_close(ports[i]); - } - - if (get_port != console) - { - rt_console_set_device(get_port->parent.name); - } - - if (_continue_run) - { - rt_device_close(get_port); - if (!is_boot) - { - rt_device_set_rx_indicate(ports[0], odev_rx_ind); - } - return 0; - } - - if (!_wait_mode) - { - err = RT_EOK; + err = RT_ENOSYS; goto _exit; } - rt_device_set_rx_indicate(get_port, port_rx_ind); - - rt_thread_mdelay(200); - _response_cmd(get_port, IAP_V5_CMD_C_REPLY); + _response_cmd(IAP_V5_CMD_C_REPLY); ctx = rt_calloc(1, sizeof(*ctx)); if (!ctx) @@ -522,7 +419,7 @@ static int _iap_entry(rt_bool_t is_boot) goto _exit; } - err = rym_recv_on_device(&ctx->parent, get_port, RT_DEVICE_OFLAG_RDWR | RT_DEVICE_FLAG_INT_RX, + err = rym_recv_on_device(&ctx->parent, console_dev, RT_DEVICE_OFLAG_RDWR | RT_DEVICE_FLAG_INT_RX, _rym_recv_begin, _rym_recv_data, _rym_recv_end, 1000); rt_free(ctx); @@ -530,22 +427,11 @@ static int _iap_entry(rt_bool_t is_boot) if (err == RT_EOK) { - rt_device_close(get_port); - _response_cmd(get_port, IAP_V5_CMD_UPDATE_SUCCEED); - if (!is_boot) - { - rt_device_set_rx_indicate(ports[0], odev_rx_ind); - } + _response_cmd(IAP_V5_CMD_UPDATE_SUCCEED); return 0; } _exit: - rt_device_close(get_port); - if (!is_boot) - { - rt_device_set_rx_indicate(ports[0], odev_rx_ind); - } - if (err == RT_EOK && is_boot) { rt_kprintf("jump to app ...\n"); @@ -553,9 +439,10 @@ _exit: return 0; } - _response_cmd(get_port, IAP_V5_CMD_UPDATE_FAILED); - _response_cmd(get_port, IAP_V5_CMD_POWER_ON_AGAIN); + _response_cmd(IAP_V5_CMD_UPDATE_FAILED); + _response_cmd(IAP_V5_CMD_POWER_ON_AGAIN); + rt_device_set_rx_indicate(console_dev, odev_rx_ind); return 0; } diff --git a/project.uvoptx b/project.uvoptx index 0d12d8b..5f27151 100644 --- a/project.uvoptx +++ b/project.uvoptx @@ -604,7 +604,7 @@ Drivers - 0 + 1 0 0 0 @@ -744,7 +744,7 @@ Fal - 1 + 0 0 0 0 @@ -856,7 +856,7 @@ IAP - 1 + 0 0 0 0 @@ -1404,7 +1404,7 @@ Utilities - 0 + 1 0 0 0