iap bootloader 实现

Signed-off-by: a1012112796 <1012112796@qq.com>
This commit is contained in:
2022-12-26 14:47:07 +08:00
parent 0401a1f32f
commit 06dbb658a5
2 changed files with 52 additions and 12 deletions

View File

@@ -3,7 +3,8 @@
#include "ymodem.h"
#include "fal.h"
#define IAP_FLASH_SEC "app"
#define IAP_FLASH_APP_SEC "app"
#define IAP_FLASH_BOOT_SEC "boot"
struct rt_completion _wait;
@@ -263,7 +264,7 @@ static rt_device_t _read_ports(rt_device_t ports[], rt_uint8_t *pch)
{
for (int i = 0; i < PORTS_NUM; i++)
{
while (1)
while (ports[i])
{
if (rt_device_read(ports[i], 0, pch, 1) != 1)
{
@@ -292,13 +293,25 @@ static void _print_ports(rt_device_t ports[], const char *fmt, ...)
for (int i = 0; i < PORTS_NUM; i++)
{
if (ports[i] == RT_NULL)
{
break;
}
rt_device_write(ports[i], 0, line_buf, real);
}
}
static int _iap_entry(rt_bool_t is_boot);
int iap_main_entry(void)
{
if (IapAppAddr)
return _iap_entry(RT_TRUE);
}
static int _iap_entry(rt_bool_t is_boot)
{
if (IapAppAddr && is_boot)
{
return 0;
}
@@ -315,8 +328,9 @@ int iap_main_entry(void)
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; i++)
for (int i = 0; i < 10 && is_boot; i++)
{
rt_snprintf(name, RT_NAME_MAX, "uart%d", i+1);
ports[i] = rt_device_find(name);
@@ -326,13 +340,23 @@ int iap_main_entry(void)
rt_device_open(ports[i], RT_DEVICE_OFLAG_RDWR | RT_DEVICE_FLAG_INT_RX);
}
if (!is_boot)
{
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);
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)
while (wait_cnt > 0 || !is_boot)
{
get_port = _read_ports(ports, &ch);
@@ -364,7 +388,7 @@ int iap_main_entry(void)
get_port = console;
}
for (int i = 0; i < PORTS_NUM; i++)
for (int i = 0; i < PORTS_NUM && is_boot; i++)
{
if (ports[i] == get_port)
{
@@ -383,13 +407,15 @@ int iap_main_entry(void)
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)
{
rt_device_close(get_port);
err = RT_EOK;
goto _exit;
}
@@ -407,11 +433,11 @@ int iap_main_entry(void)
goto _exit;
}
ctx->flash = fal_partition_find(IAP_FLASH_SEC);
ctx->flash = fal_partition_find(IapAppAddr? IAP_FLASH_BOOT_SEC: IAP_FLASH_APP_SEC);
if (ctx->flash == RT_NULL)
{
err = RT_ENOSYS;
rt_kprintf("fal_partition_find %s failed: %d\n", IAP_FLASH_SEC, rt_get_errno());
rt_kprintf("fal_partition_find %s failed: %d\n", IapAppAddr? IAP_FLASH_BOOT_SEC: IAP_FLASH_APP_SEC, rt_get_errno());
rt_free(ctx);
goto _exit;
}
@@ -426,13 +452,21 @@ int iap_main_entry(void)
{
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);
}
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)
if (err == RT_EOK && is_boot)
{
rt_kprintf("jump to app ...\n");
jump_to_app();
@@ -475,3 +509,9 @@ static void jump_to_app(void)
JumpToApplication();
}
MSH_CMD_EXPORT(jump_to_app, jump_to_app);
static void _cmd_iap(void)
{
_iap_entry(RT_FALSE);
}
MSH_CMD_EXPORT_ALIAS(_cmd_iap, iap, iap bootloader / app start);

View File

@@ -568,7 +568,7 @@
<Group>
<GroupName>Drivers</GroupName>
<tvExp>1</tvExp>
<tvExp>0</tvExp>
<tvExpOptDlg>0</tvExpOptDlg>
<cbSel>0</cbSel>
<RteFlg>0</RteFlg>