| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164 |
- #include <string.h>
- #include "app/iap.h"
- #include "app/nv_storage.h"
- #include "bsp/fmc_flash.h"
- #include "bsp/shark_rtc.h"
- #include "libs/logger.h"
- static int iap_write_image(uint8_t *data, int len);
- static int iap_check_image(uint8_t *data, int len);
- void _reboot_timer_handler(shark_timer_t *t);
- void iap_read_string(can_frame_t *frame);
- static u8 _write_success = 0;
- static int _write_position = 0;
- static shark_timer_t _reboot_timer = {.handler = _reboot_timer_handler,};
- void iap_read_chip_id(can_frame_t *frame)
- {
- u8 buff[24];
- u8 len;
- buff[0] = buff[1] = 0x00;
- len = shark_read_chip_id(buff + 2) + 2;
- protocol_send_bms_info(frame->head.can_addr, frame->key, buff, len);
- }
- void process_iap_message(can_frame_t *frame, int len){
- uint8_t *data = NULL;
- int data_len = 0;
- switch(frame->key) {
- case CAN_KEY_IAP_ENTER:
- if (gd32_flash_size() < 128 * 1024){
- wdog_reload();
- fmc_iap_write_magic(0xFFFFFFFF);
- shark_rtc_set_backup(0x3000);
- nv_save_all_soc();
- NVIC_SystemReset();
- while(1);
- }
- protocol_send_ack(frame->head.can_addr, frame->key, 0);
- break;
- case CAN_KEY_IAP_BEGIN:
- if (gd32_flash_size() < 128 * 1024){
- protocol_send_ack(frame->head.can_addr, frame->key, 1);
- break;
- }
- fmc_write_image_begin();
- _write_position = 0;
- _write_success = 0;
- protocol_send_ack(frame->head.can_addr, frame->key, 0);
- break;
- case CAN_KEY_IAP_WRITE:
- _write_success = 0;
- data_len = iap_write_image(frame->data, len);
- data = frame->data;
- break;
- case CAN_KEY_IAP_CHECK:
- fmc_write_image_end();
- data_len = iap_check_image(frame->data, len);
- data = frame->data;
- break;
- case CAN_KEY_IAP_BOOT:
- if (_write_success) {
- nv_save_all_soc();
- NVIC_SystemReset();
- while(1);
- } else {
- protocol_send_ack(frame->head.can_addr, frame->key, 0);
- }
- break;
- case CAN_KEY_IAP_STAT:
- if (len > 0 && frame->data[0] == 0x01) {
- iap_read_chip_id(frame);
- } else {
- protocol_send_ack(frame->head.can_addr, frame->key, 0);
- }
- break;
- case CAN_EEY_IAP_READ_STRING:
- iap_read_string(frame);
- break;
- case CAN_KEY_REBOOT:
- if (len == 4 && shark_decode_u32(frame->data) == SHARK_IAP_MAGIC_SUCCESS) {
- fmc_iap_write_magic(0xFFFFFFFF);
- }
- shark_rtc_set_backup(0x3003);
- shark_timer_post(&_reboot_timer, 100);
- protocol_send_ack(frame->head.can_addr, frame->key, 1);
- break;
- case CAN_KET_ERASE_NV:
- shark_rtc_set_backup(0x3002);
- if (len == 0) {
- nv_erase_all_soc(1);
- }else {
- if (frame->data[0] == 1) {//erase and clear cycle
- nv_erase_all_soc(0);
- }else {
- nv_erase_all_soc(1);
- }
- }
- shark_timer_post(&_reboot_timer, 100);
- protocol_send_ack(frame->head.can_addr, frame->key, 1);
- break;
- }
- if (data != NULL && data_len > 0){
- protocol_send_bms_info(frame->head.can_addr, frame->key, frame->data, data_len);
- }
- }
- void _reboot_timer_handler(shark_timer_t *t){
- nv_save_all_soc();
- NVIC_SystemReset();
- }
- static int iap_write_image(uint8_t *data, int len){
- int w_pos = shark_decode_u24(data);
- if (w_pos == _write_position && len > 3) {
- fmc_write_image_continue(data + 3, len - 3);
- _write_position += len - 3;
- }
- data[0] = 0;
- shark_encode_u24(data+1, _write_position);
- return 4;
- }
- void iap_read_string(can_frame_t *frame)
- {
- uint8_t buff[64];
- const char *text;
- uint32_t addr;
- uint32_t len;
- addr = ((uint32_t) frame->data[2]) << 16 | ((uint32_t) frame->data[1]) << 8 | frame->data[0];
- text = (const char *) (0x08000000 + addr);
- len = strlen(text);
- if (len > (sizeof(buff)-4)) {
- len = sizeof(buff) - 4;
- }
- buff[0] = 0;
- memcpy(buff+1, frame->data, 3);
- memcpy(buff + 4, text, len);
- protocol_send_bms_info(frame->head.can_addr, frame->key, buff, len + 4);
- }
- static int iap_check_image(uint8_t *data, int len) {
- uint32_t size, checksum;
- size = shark_decode_u24(data);
- checksum = shark_decode_u32(data + 3);
- uint32_t d_checksum = shark_iap_checksum_init();
- d_checksum = shark_iap_checksum_put(d_checksum, (const u8 *)fmc_iap_image_addr(), size);
- d_checksum = shark_iap_checksum_finish(d_checksum);
- if (checksum != d_checksum) {
- data[0] = 1;
- return 1;
- }
- fmc_write_magic(size, checksum, SHARK_IAP_MAGIC_FLASH);
- _write_success = 1;
- data[0] = 0;
- return 1;
- }
|