本帖最后由 IFELSERETURN 于 2023-8-28 19:16 编辑
int32_t ist_protocol_receive(uint8_t *buf)
{
uint32_t err_count = 0;
int32_t length;
int32_t old_count = 0;
uint8_t file_name[65];
uint8_t file_size[17];
int32_t size_num = 0;
uint8_t packet_data[1024] = {0};
enTransferMachine Machine = TRAN_NAME;
uint32_t FlashDestination = ApplicationBakAddress;
uint32_t EraseCounter = 0x0;
uint32_t NbrOfPage = 0;
fmc_state_enum FLASHStatus = FMC_READY;
uint32_t RamSource;
uint8_t ack[] = {0x01, 0x06};
uint8_t ask[] = {0x01, 0x43};
uint8_t abo[] = {0x01, 0x44};
boot_cmd_to_arm(ask);
while (1) {
int ret = serial1_recv_packet(buf, &length);
// printf("<%s : %d> ... dataLen: %d \r\n", __func__, __LINE__, length);
printf("serial1_recv_packet\r\n");
if (0 > ret) {
err_count++;
boot_cmd_to_arm(ask);
printf("<%s : %d> ... receive failed count: %d err_num: %d\r\n", __func__, __LINE__, err_count, ret);
if (err_count > 5) {
return -1;
}
} else {
err_count = 0;
printf("<%s : %d> ... receive success Len:%d %d\r\n", __func__, __LINE__, length, ret);
switch (Machine) {
case TRAN_NAME: {
if(length >= 128) {
int i = 0;
int j = 0;
while (buf && i < 64) {
file_name = buf;
i++;
}
file_name = '\0';
i++;
while (buf && j < 16) {
file_size[j] = buf;
i++;
j++;
}
file_size[j] = '\0';
Str2Int(file_size, &size_num);
if (size_num > 0) {
fmc_write_words(ConfigStartAddress, 4, (uint8_t *)&size_num, 4);
NbrOfPage = FLASH_PagesMask(size_num);
/* Erase the FLASH pages */
fmc_unlock(); /* Flash unlock */
for(EraseCounter = 0; (EraseCounter < NbrOfPage) && (FLASHStatus == FMC_READY); EraseCounter++)
{
FLASHStatus = fmc_page_erase(FlashDestination + (FMC_PAGE_SIZE * EraseCounter));
fmc_flag_clear(FMC_FLAG_END);
fmc_flag_clear(FMC_FLAG_WPERR);
fmc_flag_clear(FMC_FLAG_PGERR);
}
fmc_lock(); /* Flash lock */
Machine = TRAN_DATA;
}
printf("<%s : %d> ... name: %s size: %s \r\n", __func__, __LINE__, file_name, file_size);
}
} break;
case TRAN_DATA: {
printf("<%s : %d> ... dataLen: %d \r\n", __func__, __LINE__, length);
if(length >= 128) {
if (2 > (ret - old_count)) {
old_count = ret;
int32_t j;
memcpy(packet_data, buf, length);
RamSource = (uint32_t)packet_data;
printf("<%s : %d> ... RamSource: 0x%08x \r\n", __func__, __LINE__, RamSource);
fmc_unlock(); /* Flash unlock */
for (j = 0;(j < length) && (FlashDestination < ApplicationBakAddress + size_num);j += 4)
{
/* Program the data received into STM32F10x Flash */
fmc_word_program(FlashDestination, *(uint32_t*)RamSource);
fmc_flag_clear(FMC_FLAG_END);
fmc_flag_clear(FMC_FLAG_WPERR);
fmc_flag_clear(FMC_FLAG_PGERR);
if (*(uint32_t*)FlashDestination != *(uint32_t*)RamSource)
{
/* End session */
boot_cmd_to_arm(abo);
return -2;
}
FlashDestination += 4;
RamSource += 4;
}
fmc_lock(); /* Flash lock */
boot_cmd_to_arm(ack);
} else {
boot_cmd_to_arm(ask);
}
} else {
if ((0x01 == buf[0]) && (0x04== buf[1])) {
Machine = TRAN_END;
}
boot_cmd_to_arm(ask);
}
} break;
case TRAN_END: {
if ((0x01 == buf[0]) && (0x04== buf[1])) {
boot_cmd_to_arm(ack);
} else if ((length >= 128) && (is_zero_buf(buf, length))) {
boot_cmd_to_arm(ack);
return size_num;
} else {
boot_cmd_to_arm(ask);
}
} break;
}
}
}
}
int32_t serial1_recv_packet(uint8_t *data, int32_t *length)
{
uint16_t i;
uint16_t rev_flag = 0;
uint8_t dataLen;
uint8_t chekSum;
uint8_t count;
int32_t data_count;
uint8_t recv_char;
uint8_t rev_data[USART_RX_SIZE];
int32_t save_index;
enPacketMachine Machine = FRAME_HEAD0;
printf("first serial1_recv_packet running...\n");
// SET_TIMER_OUT();
while (!rev_flag) {
printf("serial1_recv_packet running...\r\n");
if(usart0_rx.rx_idle == 1){
usart0_rx.rx_idle = 0;
memcpy(rev_data, usart0_rx.data, USART_RX_SIZE);
save_index = usart0_rx.save_index;
for(i=0;i<save_index;i++){
printf("0x%x, ",rev_data);
recv_char = rev_data;
switch(Machine) {
case FRAME_HEAD0: {
if (HEAD0 == recv_char) {
Machine = FRAME_HEAD1;
} else {
printf("<%s : %d> ... %d\r\n", __func__, __LINE__, Machine);
}
} break;
case FRAME_HEAD1: {
if (HEAD1 == recv_char) {
Machine = FRAME_DATA_LEN;
} else {
printf("<%s : %d> ... %d\r\n", __func__, __LINE__, Machine);
Machine = FRAME_HEAD0;
}
} break;
case FRAME_DATA_LEN: {
dataLen = recv_char - 4;
chekSum = recv_char;
Machine = FRAME_DEV_ID;
} break;
case FRAME_DEV_ID: {
if (IMAGE_ID == recv_char) {
chekSum ^= recv_char;
Machine = FRAME_DATA_CNT;
} else {
printf("<%s : %d> ... %d\r\n", __func__, __LINE__, Machine);
Machine = FRAME_HEAD0;
}
} break;
case FRAME_DATA_CNT: {
count = recv_char;
chekSum ^= recv_char;
data_count = 0;
Machine = FRAME_DATA;
} break;
case FRAME_DATA: {
data[data_count] = recv_char;
printf("data[%d]=%x\r\n", data_count, data[data_count]);
chekSum ^= recv_char;
data_count++;
if ((uint8_t)(data_count & 0xff) >= dataLen) {
Machine = FRAME_CKECK_SUM;
}
} break;
case FRAME_CKECK_SUM: {
if (chekSum == recv_char) {
Machine = FRAME_TAIL0;
} else {
printf("<%s : %d> ... %d check[0x%02x 0x%02x]\r\n", __func__, __LINE__, Machine, chekSum, recv_char);
Machine = FRAME_HEAD0;
}
} break;
case FRAME_TAIL0: {
if (TAIL0 == recv_char) {
Machine = FRAME_TAIL1;
} else {
printf("<%s : %d> ... %d\r\n", __func__, __LINE__, Machine);
Machine = FRAME_HEAD0;
}
} break;
case FRAME_TAIL1: {
if (TAIL1 == recv_char) {
*length = data_count;
printf("FRAME_TAIL1 count %d\r\n", data_count);
rev_flag = 1;
// return ((int32_t)count) & 0xff;
}
printf("<%s : %d> ... %d\r\n", __func__, __LINE__, Machine);
Machine = FRAME_HEAD0;
} break;
}
// if (0U == get_timer_count()) {
// return -1;
// }
}
}
}
printf("serial1_recv_packet off...\r\n");
return 0;
}
入口是从这段代码
if (UpdateFlag == *((uint32_t *)ConfigStartAddress)) {
printf("update from soc \r\n");
FMC_flash_write_word(0x800FC00, 0xff);
int32_t ret = ist_protocol_receive(&tab_1024[0]);
if (ret > 0) {
printf("update success from soc image size: %d \r\n", ret);
CopyApp();
JumpApp();
}
}
程序执行到了serial1_recv_packet函数中的最后两句
printf("serial1_recv_packet off...\r\n");
return 0;这两句,但是没有返回继续执行,很奇怪,求大佬们帮忙看看 |
|