赞
踩
历程中:使用Ymode 向flash 中"download"写入下载文件。
rt_err_t rym_recv_on_device(
struct rym_ctx *ctx,
rt_device_t dev,
rt_uint16_t oflag,
rym_callback on_begin,
rym_callback on_data,
rym_callback on_end,
int handshake_timeout) 实现了文件的传输:
struct rym_ctx *ctx :
ymodem_update.c 【struct rym_ctx rctx】 调用ymode.c 【 struct rym_ctx *ctx】 ,
rctx 去耦合,放在了顶层函数中,底层依靠指针调用该结构提。具体功能不深究。
dev:
rt_console_get_device()获取的结果就是serial1设备 ,也可改为其他serial设备。
oflag :
RT_DEVICE_OFLAG_RDWR | RT_DEVICE_FLAG_INT_RX 权限。
on_begin:
--》ymodem_on_begin
on_data:
-》ymodem_on_data
on_end:
-》NULL,
handshake_timeout:
-》RT_TICK_PER_SECOND
- rt_err_t rym_recv_on_device(
- struct rym_ctx *ctx,
- rt_device_t dev,
- rt_uint16_t oflag,
- rym_callback on_begin,
- rym_callback on_data,
- rym_callback on_end,
- int handshake_timeout)
- {
- rt_err_t res;
- rt_err_t (*odev_rx_ind)(rt_device_t dev, rt_size_t size);
- rt_uint16_t odev_flag;
- int int_lvl;
-
- RT_ASSERT(_rym_the_ctx == 0);
- _rym_the_ctx = ctx;
-
- ctx->on_begin = on_begin;
- ctx->on_data = on_data;
- ctx->on_end = on_end;
- ctx->dev = dev;
- rt_sem_init(&ctx->sem, "rymsem", 0, RT_IPC_FLAG_FIFO);
-
- odev_rx_ind = dev->rx_indicate;
- /* no data should be received before the device has been fully setted up.
- */
- int_lvl = rt_hw_interrupt_disable();
- rt_device_set_rx_indicate(dev, _rym_rx_ind);
-
- odev_flag = dev->flag;
- /* make sure the device don't change the content. */
- dev->flag &= ~RT_DEVICE_FLAG_STREAM;
- rt_hw_interrupt_enable(int_lvl);
- res = rt_device_open(dev, oflag);
- if (res != RT_EOK)
- goto __exit;
- res = _rym_do_recv(ctx, handshake_timeout);
- rt_device_close(dev);
- __exit:
- /* no rx_ind should be called before the callback has been fully detached.
- */
- int_lvl = rt_hw_interrupt_disable();
- rt_sem_detach(&ctx->sem);
- dev->flag = odev_flag;
- rt_device_set_rx_indicate(dev, odev_rx_ind);
- rt_hw_interrupt_enable(int_lvl);
- rt_free(ctx->buf);
- _rym_the_ctx = RT_NULL;
- return res;
- }

如果像更改下载的磁盘:更改这里。增加下载工厂模式的文件。
if ((dl_part = fal_partition_find("download")) == RT_NULL)
- #define DBG_ENABLE
- #define DBG_SECTION_NAME "ymodem"
- #ifdef YMODEM_DEBUG
- #define DBG_LEVEL DBG_LOG
- #else
- #define DBG_LEVEL DBG_INFO
- #endif
- #define DBG_COLOR
- #include <rtdbg.h>
-
- static size_t update_file_total_size, update_file_cur_size;
-
- static const struct fal_partition * dl_part = RT_NULL;
-
- static enum rym_code ymodem_on_begin(struct rym_ctx *ctx, rt_uint8_t *buf, rt_size_t len)
- {
- char *file_name, *file_size;
-
- /* calculate and store file size */
- file_name = (char *)&buf[0];
- file_size = (char *)&buf[rt_strlen(file_name) + 1];
- update_file_total_size = atol(file_size);
-
- update_file_cur_size = 0;
-
- /* Get download partition information and erase download partition data */
- if ((dl_part = fal_partition_find("download")) == RT_NULL)
- {
- LOG_E("Firmware download failed! Partition (%s) find error!", "download");
- return RYM_CODE_CAN;
- }
-
- if (update_file_total_size > dl_part->len)
- {
- LOG_E("Firmware is too large! File size (%d), '%s' partition size (%d)", update_file_total_size, "download", dl_part->len);
- return RYM_CODE_CAN;
- }
-
- LOG_I("Start erase. Size (%d)", update_file_total_size);
-
- /* erase DL section */
- if (fal_partition_erase(dl_part, 0, update_file_total_size) < 0)
- {
- LOG_E("Firmware download failed! Partition (%s) erase error!", dl_part->name);
- return RYM_CODE_CAN;
- }
-
- return RYM_CODE_ACK;
- }
-
- static enum rym_code ymodem_on_data(struct rym_ctx *ctx, rt_uint8_t *buf, rt_size_t len)
- {
- /* write data of application to DL partition */
- if (fal_partition_write(dl_part, update_file_cur_size, buf, len) < 0)
- {
- LOG_E("Firmware download failed! Partition (%s) write data error!", dl_part->name);
- return RYM_CODE_CAN;
- }
-
- update_file_cur_size += len;
-
- return RYM_CODE_ACK;
- }
-
- void update(uint8_t argc, char **argv)
- {
- struct rym_ctx rctx;
-
- rt_kprintf("Waring: Ymodem has started! This operator will not recovery.\n");
- rt_kprintf("Please select the ota firmware file and use Ymodem to send.\n");
-
- if (!rym_recv_on_device(&rctx, rt_console_get_device(), RT_DEVICE_OFLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
- ymodem_on_begin, ymodem_on_data, NULL, RT_TICK_PER_SECOND))
- {
- rt_kprintf("Download firmware to flash success.\n");
- rt_kprintf("System now will restart...\r\n");
-
- /* wait some time for terminal response finish */
- rt_thread_delay(rt_tick_from_millisecond(200));
-
- /* Reset the device, Start new firmware */
- extern void rt_hw_cpu_reset(void);
- rt_hw_cpu_reset();
- /* wait some time for terminal response finish */
- rt_thread_delay(rt_tick_from_millisecond(200));
- }
- else
- {
- /* wait some time for terminal response finish */
- rt_thread_delay(RT_TICK_PER_SECOND);
- rt_kprintf("Update firmware fail.\n");
- }
-
- return;
- }
- MSH_CMD_EXPORT_ALIAS(update, ymodem_start, Update user application firmware);

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。