iap 优化,避免 v8 升级失败

Signed-off-by: a1012112796 <1012112796@qq.com>
This commit is contained in:
2023-12-08 08:42:05 +08:00
parent 1830fad2a7
commit 32d8112577
8 changed files with 171 additions and 64 deletions

View File

@@ -1,5 +1,5 @@
/*
* COPYRIGHT (C) 2011-2021, Real-Thread Information Technology Ltd
* COPYRIGHT (C) 2011-2023, Real-Thread Information Technology Ltd
* All rights reserved
*
* SPDX-License-Identifier: Apache-2.0
@@ -121,7 +121,7 @@ static enum rym_code _rym_read_code(
}
/* the caller should at least alloc _RYM_STX_PKG_SZ buffer */
static rt_size_t _rym_read_data(
static rt_ssize_t _rym_read_data(
struct rym_ctx *ctx,
rt_size_t len)
{
@@ -149,31 +149,44 @@ static rt_err_t _rym_send_packet(
rt_uint16_t send_crc;
rt_uint8_t index_inv = ~index;
rt_size_t writelen = 0;
rt_size_t packetlen = 0;
send_crc = CRC16(ctx->buf + 3, _RYM_SOH_PKG_SZ - 5);
switch(code)
{
case RYM_CODE_SOH:
packetlen = _RYM_SOH_PKG_SZ;
break;
case RYM_CODE_STX:
packetlen = _RYM_STX_PKG_SZ;
break;
default:
return -RT_ERROR;
}
send_crc = CRC16(ctx->buf + 3, packetlen - 5);
ctx->buf[0] = code;
ctx->buf[1] = index;
ctx->buf[2] = index_inv;
ctx->buf[131] = (rt_uint8_t)(send_crc >> 8);
ctx->buf[132] = (rt_uint8_t)send_crc & 0xff;
ctx->buf[packetlen - 2] = (rt_uint8_t)(send_crc >> 8);
ctx->buf[packetlen - 1] = (rt_uint8_t)send_crc & 0xff;
do
{
writelen += rt_device_write(ctx->dev, 0, ctx->buf + writelen,
_RYM_SOH_PKG_SZ - writelen);
packetlen - writelen);
}
while (writelen < _RYM_SOH_PKG_SZ);
while (writelen < packetlen);
return RT_EOK;
}
static rt_size_t _rym_putchar(struct rym_ctx *ctx, rt_uint8_t code)
static rt_ssize_t _rym_putchar(struct rym_ctx *ctx, rt_uint8_t code)
{
rt_device_write(ctx->dev, 0, &code, sizeof(code));
return 1;
}
static rt_size_t _rym_getchar(struct rym_ctx *ctx)
static rt_ssize_t _rym_getchar(struct rym_ctx *ctx)
{
rt_uint8_t getc_ack;
@@ -276,7 +289,9 @@ static rt_err_t _rym_do_send_handshake(
/* congratulations, check passed. */
if (ctx->on_begin && ctx->on_begin(ctx, ctx->buf + 3, data_sz - 5) != RYM_CODE_SOH)
{
return -RYM_ERR_CODE;
}
code = RYM_CODE_SOH;
_rym_send_packet(ctx, code, index);
@@ -349,6 +364,7 @@ static rt_err_t _rym_do_trans(struct rym_ctx *ctx)
_rym_putchar(ctx, RYM_CODE_ACK);
_rym_putchar(ctx, RYM_CODE_C);
ctx->stage = RYM_STAGE_ESTABLISHED;
rt_size_t errors = 0;
while (1)
{
@@ -360,36 +376,61 @@ static rt_err_t _rym_do_trans(struct rym_ctx *ctx)
RYM_WAIT_PKG_TICK);
switch (code)
{
case RYM_CODE_SOH:
data_sz = 128;
break;
case RYM_CODE_STX:
data_sz = 1024;
break;
case RYM_CODE_EOT:
return RT_EOK;
default:
return -RYM_ERR_CODE;
case RYM_CODE_SOH:
data_sz = 128;
break;
case RYM_CODE_STX:
data_sz = 1024;
break;
case RYM_CODE_EOT:
return RT_EOK;
default:
errors++;
if(errors > RYM_MAX_ERRORS)
{
return -RYM_ERR_CODE;/* Abort communication */
}
else
{
_rym_putchar(ctx, RYM_CODE_NAK);/* Ask for a packet */
continue;
}
};
err = _rym_trans_data(ctx, data_sz, &code);
if (err != RT_EOK)
return err;
{
errors++;
if(errors > RYM_MAX_ERRORS)
{
return err;/* Abort communication */
}
else
{
_rym_putchar(ctx, RYM_CODE_NAK);/* Ask for a packet */
continue;
}
}
else
{
errors = 0;
}
switch (code)
{
case RYM_CODE_CAN:
/* the spec require multiple CAN */
for (i = 0; i < RYM_END_SESSION_SEND_CAN_NUM; i++)
{
_rym_putchar(ctx, RYM_CODE_CAN);
}
return -RYM_ERR_CAN;
case RYM_CODE_ACK:
_rym_putchar(ctx, RYM_CODE_ACK);
break;
default:
// wrong code
break;
case RYM_CODE_CAN:
/* the spec require multiple CAN */
for (i = 0; i < RYM_END_SESSION_SEND_CAN_NUM; i++)
{
_rym_putchar(ctx, RYM_CODE_CAN);
}
return -RYM_ERR_CAN;
case RYM_CODE_ACK:
_rym_putchar(ctx, RYM_CODE_ACK);
break;
default:
// wrong code
break;
};
}
}
@@ -402,21 +443,20 @@ static rt_err_t _rym_do_send_trans(struct rym_ctx *ctx)
rt_uint32_t index = 1;
rt_uint8_t getc_ack;
data_sz = _RYM_SOH_PKG_SZ;
data_sz = _RYM_STX_PKG_SZ;
while (1)
{
if (ctx->on_data && ctx->on_data(ctx, ctx->buf + 3, data_sz - 5) != RYM_CODE_SOH)
if (!ctx->on_data)
{
return -RYM_ERR_CODE;
code = RYM_CODE_SOH;
}
code = ctx->on_data(ctx, ctx->buf + 3, data_sz - 5);
_rym_send_packet(ctx, code, index);
index++;
rt_device_set_rx_indicate(ctx->dev, _rym_rx_ind);
getc_ack = _rym_getchar(ctx);
if (getc_ack != RYM_CODE_ACK)
{
return -RYM_ERR_ACK;
@@ -429,6 +469,8 @@ static rt_err_t _rym_do_send_trans(struct rym_ctx *ctx)
return RT_EOK;
}
// 发送EOT飞控回复ACK和'C'然后发送SOH空数据然后回复ACK然后等待飞控下发B5,5B,03,BB表示升级成功
static rt_err_t _rym_do_fin(struct rym_ctx *ctx)
{
enum rym_code code;
@@ -442,10 +484,15 @@ static rt_err_t _rym_do_fin(struct rym_ctx *ctx)
if (ctx->on_end)
ctx->on_end(ctx, ctx->buf + 3, 128);
_rym_putchar(ctx, RYM_CODE_NAK);
code = _rym_read_code(ctx, RYM_WAIT_PKG_TICK);
if (code != RYM_CODE_EOT)
return -RYM_ERR_CODE;
if (!ctx->v5_iap_adapt)
{
_rym_putchar(ctx, RYM_CODE_NAK);
code = _rym_read_code(ctx, RYM_WAIT_PKG_TICK);
if (code != RYM_CODE_EOT)
{
return -RYM_ERR_CODE;
}
}
_rym_putchar(ctx, RYM_CODE_ACK);
_rym_putchar(ctx, RYM_CODE_C);
@@ -460,7 +507,9 @@ static rt_err_t _rym_do_fin(struct rym_ctx *ctx)
data_sz = _RYM_STX_PKG_SZ;
}
else
{
return -RYM_ERR_CODE;
}
i = _rym_read_data(ctx, _RYM_SOH_PKG_SZ - 1);
if (i != (_RYM_SOH_PKG_SZ - 1))
@@ -558,11 +607,6 @@ static rt_err_t _rym_do_recv(
while (1)
{
err = _rym_do_trans(ctx);
if (err != RT_EOK)
{
rt_free(ctx->buf);
return err;
}
err = _rym_do_fin(ctx);
if (err != RT_EOK)
@@ -669,6 +713,11 @@ __exit:
_rym_the_ctx = RT_NULL;
if (res != RT_EOK)
{
rt_kprintf("rym_recv_on_device err: %d\n", res);
}
return res;
}