diff --git a/iap/iap.c b/iap/iap.c
index 8fc2e1d..c6041c2 100644
--- a/iap/iap.c
+++ b/iap/iap.c
@@ -5,7 +5,6 @@
#include "fal.h"
#define IAP_FLASH_SEC "app"
-static rt_device_t console_dev;
struct rt_completion _wait;
#define IS_AF(c) ((c >= 'A') && (c <= 'F'))
@@ -185,7 +184,7 @@ static enum rym_code _rym_recv_data(
RT_ASSERT(cctx);
RT_ASSERT(len <= RT_BUF_SIZE);
- if (cctx->w_index >= cctx->file_size)
+ if (cctx->w_index >= cctx->file_size)
{
return RYM_CODE_ACK;
}
@@ -252,44 +251,51 @@ static const rt_uint8_t *_pack_cmd(enum iap_v5_cmd cmd)
return _cmd_line;
}
-#define _response_cmd(cmd) \
+#define _response_cmd(console_dev, cmd) \
rt_device_write(console_dev, 0, _pack_cmd(cmd), IAP_V5_CMD_SIZE)
-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)
-{
- rt_uint8_t ch;
- rt_tick_t start = rt_tick_get();
- rt_bool_t rt = RT_FALSE;
-
- while (rt_tick_get() - start < wait)
- {
- if (rt_device_read(console_dev, 0, &ch, 1) == 1)
- {
- if (ch == want)
- {
- rt = RT_TRUE;
- break;
- }
-
- continue;
- }
-
- if (rt_completion_wait(_wait, wait - (rt_tick_get() - start))
- != RT_EOK)
- {
- break;
- }
- }
-
- return rt;
-}
-
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)
+{
+ for (int i = 0; i < PORTS_NUM; i++)
+ {
+ while (1)
+ {
+ if (rt_device_read(ports[i], 0, pch, 1) != 1)
+ {
+ break;
+ }
+
+ if (*pch == '1' || *pch == 'q')
+ {
+ return ports[i];
+ }
+ }
+ }
+
+ 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++)
+ {
+ rt_device_write(ports[i], 0, line_buf, real);
+ }
+}
+
int iap_main_entry(void)
{
if (IapAppAddr)
@@ -297,31 +303,101 @@ int iap_main_entry(void)
return 0;
}
- 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);
- console_dev = rt_console_get_device();
- RT_ASSERT(console_dev);
+ rt_device_t ports[10] = {0};
+ char name[RT_NAME_MAX] = {0};
rt_completion_init(&_wait);
- odev_rx_ind = console_dev->rx_indicate;
- rt_device_set_rx_indicate(console_dev, port_rx_ind);
- if (!iap_v5_req_char(console_dev, &_wait, '1', 1000))
+ rt_bool_t _wait_mode = RT_FALSE;
+ rt_bool_t _continue_run = RT_FALSE;
+
+ for (int i = 0; i < 10; i++)
{
+ rt_snprintf(name, RT_NAME_MAX, "uart%d", i+1);
+ ports[i] = rt_device_find(name);
+ RT_ASSERT(ports[i]);
+
+ rt_device_set_rx_indicate(ports[i], port_rx_ind);
+ rt_device_open(ports[i], 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)
+ {
+ 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; 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);
+ return 0;
+ }
+
+ if (!_wait_mode)
+ {
+ rt_device_close(get_port);
+
+ err = RT_EOK;
goto _exit;
}
- _response_cmd(IAP_V5_CMD_ONE_REPLY);
+
+ rt_device_set_rx_indicate(get_port, port_rx_ind);
rt_thread_mdelay(200);
- if (!iap_v5_req_char(console_dev, &_wait, 'c', 1000))
- {
- err = RT_ENOSYS;
- goto _exit;
- }
-
- _response_cmd(IAP_V5_CMD_C_REPLY);
+ _response_cmd(get_port, IAP_V5_CMD_C_REPLY);
ctx = rt_calloc(1, sizeof(*ctx));
if (!ctx)
@@ -340,7 +416,7 @@ int iap_main_entry(void)
goto _exit;
}
- err = rym_recv_on_device(&ctx->parent, console_dev, RT_DEVICE_OFLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
+ err = rym_recv_on_device(&ctx->parent, get_port, RT_DEVICE_OFLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
_rym_recv_begin, _rym_recv_data, _rym_recv_end, 1000);
rt_free(ctx);
@@ -348,11 +424,14 @@ int iap_main_entry(void)
if (err == RT_EOK)
{
- _response_cmd(IAP_V5_CMD_UPDATE_SUCCEED);
+ rt_device_close(get_port);
+ _response_cmd(get_port, IAP_V5_CMD_UPDATE_SUCCEED);
return 0;
}
_exit:
+ rt_device_close(get_port);
+
if (err == RT_EOK)
{
rt_kprintf("jump to app ...\n");
@@ -360,10 +439,9 @@ _exit:
return 0;
}
- _response_cmd(IAP_V5_CMD_UPDATE_FAILED);
- _response_cmd(IAP_V5_CMD_POWER_ON_AGAIN);
+ _response_cmd(get_port, IAP_V5_CMD_UPDATE_FAILED);
+ _response_cmd(get_port, 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 18fcc41..bfebf24 100644
--- a/project.uvoptx
+++ b/project.uvoptx
@@ -588,7 +588,7 @@
5
31
1
- 0
+ 1
0
0
board\board.c
@@ -784,7 +784,7 @@
IAP
- 0
+ 1
0
0
0