| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159 |
- #include <stdio.h>
- #include <stdint.h>
- #include <string.h>
- #include "ff_stdio.h"
- #include "FreeRTOS.h"
- #include "task.h"
- #include "chip.h"
- #include "crc.h"
- #include "ota_update.h"
- #include "mailbox_message.h"
- #include "mmcsd_core.h"
- #include "romfile.h"
- #include "sfud.h"
- #include "animation.h"
- #include "mailbox_update/mailbox_update_demo.h"
- static uint32_t upfile_total_size;
- static uint32_t upfile_size;
- static uint32_t upfile_offset;
- static int upfile_type;
- static int CanUpdateSendstatus(mb_msg_type_t msg_type, MailboxUpdateFrameRespondStatus status)
- {
- MailboxUpdateFrame frame = {0};
- frame.FrameRspStatus = status;
- return MailboxUpdateSendFrame(msg_type, &frame);
- }
- uint32_t MailboxUpdateCheck(void)
- {
- if (upfile_size != upfile_total_size) {
- printf("%s, data size rev err.\n", __func__);
- printf("upfile_size = %d, upfile_total_size = %d \n", upfile_size, upfile_total_size);
- return MUFRS_ACK_FAIL;
- }
- if (check_upfile_and_save(upfile_type, upfile_size, upfile_offset)) {
- printf("%s, filecheck fail.\n", __func__);
- return MUFRS_ACK_FAIL;
- } else {
- printf("%s update ok.\n", g_upfilename[upfile_type]);
- return MUFRS_ACK_OK;
- }
- }
- static uint32_t CanUpdateHandleData(MailboxUpdateFrame *frame)
- {
- int leftsize;
- uint32_t rwsize, rwoffset;
- uint8_t *buf = NULL;
- static uint32_t upfile_cur_offset;
- #if DEVICE_TYPE_SELECT != EMMC_FLASH
- sfud_flash *sflash = sfud_get_device(0);
- #endif
- leftsize = frame->DataSize;
- buf = (uint8_t *)frame->DataAddr;
- printf("Mailbox update rev frame dataaddr: 0x%x, datasize: 0x%x.\n", \
- (void *)frame->DataAddr, frame->DataSize);
- CP15_invalidate_dcache_for_dma(frame->DataAddr, frame->DataAddr + frame->DataSize); //使cache的数据无效 重新去ddr里面读
- rwoffset = upfile_cur_offset;
- if (frame->FrameStatus == MUFS_START) {
- uint32_t checksum;
- upfile_type = frame->FileType;
- if (upfile_type > UPFILE_TYPE_ROM){
- printf("%s, mailbox rev filetype error.\n", __func__);
- return MUFRS_ACK_FNOSUP;
- } else {
- printf("Mailbox update rev %s, file length %d.\n", g_upfilename[upfile_type], frame->TotalSize);
- if (frame->TotalSize > get_upfile_maxsize(upfile_type)) {
- printf("%s, filelength is too large, not enough space to save.\n", __func__);
- return MUFRS_ACK_FNOSPACE;
- }
- }
- if (upfile_type == UPFILE_TYPE_APP || upfile_type == UPFILE_TYPE_LDR) {
- checksum = *(uint32_t*)(buf + APPLDR_CHECKSUM_OFFSET);
- } else if (upfile_type == UPFILE_TYPE_ANIMATION) {
- BANIHEADER *header = (BANIHEADER *)buf;
- checksum = header->checksum;
- } else if (upfile_type == UPFILE_TYPE_ROM) {
- RomHeader *header = (RomHeader *)buf;
- checksum = header->checksum;
- }
- if (checksum == get_upfile_checksum(upfile_type, 0, 0)) {
- printf("checksum is the same as now, don't update.\n");
- return MUFRS_ACK_FSAME;
- }
- upfile_cur_offset = upfile_offset = get_upfile_offset(upfile_type, 1);
- upfile_size = 0;
- upfile_total_size = frame->TotalSize;
- } else if (frame->FrameStatus == MUFS_TFR) {
- while (leftsize > 0) {
- rwsize = leftsize > OTA_RW_SIZE ? OTA_RW_SIZE : leftsize;
- #if DEVICE_TYPE_SELECT == EMMC_FLASH
- if (emmc_write(rwoffset, rwsize, buf))
- #else
- if (sfud_erase_write(sflash, rwoffset, rwsize, buf) != SFUD_SUCCESS)
- #endif
- {
- printf("%s, burn data fail.\n", __func__);
- return MUFRS_ACK_FBURNE;
- }
- leftsize -= rwsize;
- rwoffset += rwsize;
- buf += rwsize;
- }
- upfile_size += frame->DataSize;
- printf("upfile_size = 0x%x.\n", upfile_size);
- upfile_cur_offset += frame->DataSize;
- printf("burn size: 0x%x.\n", frame->DataSize);
- } else {
- return MUFRS_ACK_FAIL;
- }
- return MUFRS_ACK_OK;
- }
- static void CanUpdateReceiveThread(void *para)
- {
- unsigned int status;
- mb_rxmsg_t msg;
- MailboxUpdateFrame *rx_frame;
- for (;;) {
- rx_frame = MailboxUpdateReceiveFrame(MB_MSG_T_CAN_UPDATE, &msg, portMAX_DELAY);
- if (!rx_frame)
- continue;
- //data analysis
- switch (rx_frame->FrameStatus) {
- case MUFS_START:
- case MUFS_TFR:
- CanUpdateSendstatus(MB_MSG_T_CAN_UPDATE, CanUpdateHandleData(rx_frame));
- break;
- case MUFS_END:
- CanUpdateSendstatus(MB_MSG_T_CAN_UPDATE, MailboxUpdateCheck());
- break;
- }
- }
- }
- /* 接收MCU Mailbox(CAN升级)信息, 升级CPU升级文件 */
- void CanUpdateReceiveDemo(void)
- {
- if (xTaskCreate(CanUpdateReceiveThread, "CanUpdateReceiveDemo", configMINIMAL_STACK_SIZE, NULL,
- 1, NULL) != pdPASS) {
- printf("create can update receive task fail.\n");
- }
- }
|