int32_t main(void) { uint32_t i; uint8_t * pt; uint8_t *paddr = (uint8_t *)TEST_SECTOR1; pt = (uint8_t *)u8TestData; SystemCoreClockUpdate(); #ifdef DEBUG_PRINT //Uart_Io_Init(); #endif #ifdef DEBUG_PRINT printf("==================================================\n"); printf("main flash Example Program Start \n"); printf("==================================================\n"); #endif delay(); #ifdef DEBUG_PRINT printf("==================================================\n"); printf("main flash erase \n"); printf("==================================================\n"); #endif delay(); MFlash_SectorErase((uint16_t*)TEST_SECTOR1); MFlash_SectorErase((uint16_t*)(TEST_SECTOR1+4)); for(i=0;i<TEST_SIZE;i++) { u8TestData[i] = i; } #ifdef DEBUG_PRINT printf("==================================================\n"); printf("main flash write 16bit data \n"); printf("==================================================\n"); #endif delay(); MFlash_WriteData16Bit((uint16_t*)TEST_SECTOR1, (uint16_t*)u8TestData, sizeof(u8TestData)/sizeof(uint16_t)); for(i=0;i<TEST_SIZE;i++) { u8TestData[i] = 0xff; } for(i=0;i<TEST_SIZE;i++) { *pt++ = *paddr++; } for(i=0;i<TEST_SIZE;i++) { if(i != u8TestData[i]) { #ifdef DEBUG_PRINT printf("main flash 16bit write data fail \n"); while(1); #endif } } #ifdef DEBUG_PRINT printf("main flash 16bit write data successfully \n"); #endif delay(); #ifdef DEBUG_PRINT printf("==================================================\n"); printf("main flash erase \n"); printf("==================================================\n"); #endif delay(); MFlash_SectorErase((uint16_t*)TEST_SECTOR1); MFlash_SectorErase((uint16_t*)(TEST_SECTOR1+4)); #ifdef DEBUG_PRINT printf("==================================================\n"); printf("main flash write 32bit data \n"); printf("==================================================\n"); #endif for(i=0;i<TEST_SIZE;i++) { u8TestData[i] = TEST_SIZE - i; } delay(); MFlash_WriteData32Bit((uint32_t*)TEST_SECTOR1, (uint32_t*)u8TestData, sizeof(u8TestData)/sizeof(uint32_t),TRUE); for(i=0;i<TEST_SIZE;i++) { u8TestData[i] = 0xff; } paddr = (uint8_t *)TEST_SECTOR1; pt = (uint8_t *)u8TestData; for(i=0;i<TEST_SIZE;i++) { *pt++ = *paddr++; } for(i=0;i<TEST_SIZE;i++) { if((TEST_SIZE - i) != u8TestData[i]) { #ifdef DEBUG_PRINT printf("main flash 32bit write data fail \n"); while(1); #endif } } #ifdef DEBUG_PRINT printf("main flash 32bit write data successfully \n"); printf("==================================================\n"); printf("main flash Example Program End \n"); printf("==================================================\n"); #endif while(1); }
/******************************************************************************* * 函数命名: Ymodem * 输入参数: - address of the first byte * 输出参数: - None * —返回值: - The size of the file * 函数功能: Receive a file using the ymodem protocol *******************************************************************************/ int32_t Ymodem_Receive (uint8_t *buf) { uint8_t packet_data[128 + 5], file_size[50], *file_ptr, *buf_ptr; int32_t i, packet_length, session_done, file_done, errors, session_begin, size = 0, packets_received = 0; uint32_t flashdestination, ramsource; /* Initialize flashdestination variable */ flashdestination = 0; flashdestination = APPLICATION_ADDRESS; //flashdestination = 0x1100; for (session_done = 0, errors = 0, session_begin = 0; ;) { for (packets_received = 0, file_done = 0, buf_ptr = buf; ;) { switch (Receive_Packet(packet_data, &packet_length, 0x10000))//纯数据缓存在 packet_data 中 0x1000 { case 0://正常传输 or 传输结束 errors = 0; switch (packet_length)//数据包长度 { case -1:/* Abort by sender */ Put_char(ACK); return 0; case 0:/* End of transmission */ Put_char(ACK); file_done = 1; // session_done = 1; //ybz 改变Ymodem协议 多个bin文件不可以连续传输。因为升级中掉电后,bin文件还存在在上位机的进程中,上电后再给一个bin文件升级时,其实是download了两个bin文件. 加上此句会造成上位机卡死。 break; default:/* Normal packet */ if ((packet_data[PACKET_SEQNO_INDEX] & 0xff) != (packets_received & 0xff)) { Put_char(NAK); } else { if (packets_received == 0) { /* Filename packet */ if ((packet_data[PACKET_HEADER] != 0) && (packet_data[4] != 0) && (packet_data[4] != 0x30)) { /* Filename packet has valid data */ for (i = 0, file_ptr = packet_data + PACKET_HEADER; (*file_ptr != 0) && (i < FILE_NAME_LENGTH);) { FileName[i++] = *file_ptr++; } FileName[i++] = '\0'; for (i = 0, file_ptr ++; (*file_ptr != ' ') && (i < FILE_SIZE_LENGTH); ) { file_size[i++] = *file_ptr++; } file_size[i++] = '\0'; Str2Int(file_size, &size); temp = size; /* Test the size of the image to be sent */ /* Image size is greater than Flash size */ if (size > USER_FLASH_SIZE) //USER_FLASH_SIZE + 1 { /* End session */ Put_char(CA); Put_char(CA); return -1; //写入bin文件的size 大于 Flash可以写入的size } /* erase user application area */ __disable_irq(); // MFlash_SectorErase ((uint16_t*)0x00001000);//擦除第2扇区 MFlash_SectorErase ((uint16_t*)0x00010000); //擦除第3扇区 Put_char(ACK); Put_char(CRC16); __NOP(); } /* Filename packet is empty, end session */ else { Put_char(ACK); file_done = 1; session_done = 1; break; } } /* Filename packet */ else /* Data packet */ { memcpy(buf_ptr, packet_data + PACKET_HEADER, packet_length); ramsource = (uint32_t)buf; /* Write received data in Flash */ // 要写的地址 数据首地址 数据长度 if (0 == Write(flashdestination, (uint32_t*)ramsource, (uint16_t)packet_length/4)) { Put_char(ACK); flashdestination = flashdestination + packet_length; } else /* An error occurred while writing to Flash memory */ { /* End session */ Put_char(CA); Put_char(CA); return -2; } } //end if(packets_received == 0) packets_received++; session_begin = 1; }//end if() /* Normal packet */ } break; //break case 0://正常传输 or 传输结束 case 1://abort by user Put_char(CA); Put_char(CA); return -3; default: //如果会话开始后发生错误 if (session_begin > 0) { errors++; } if (errors > MAX_ERRORS) //错误累积到一定程度之后,终止会话。 (5次) { Put_char(CA); Put_char(CA); return 0; } flashdestination = 0; //这两句修复了,升级过程中掉电。再上电后重新发送APP_bin第一次发生错误后,再次发送APP_bin写入flash地址错误 (正确的地址应为0x0000_1100) flashdestination = APPLICATION_ADDRESS; //重新发送C字符后, 重新初始化APP_address //flashdestination = 0x1100; Delay_ms(1000); //ybz Put_char(CRC16); //如果没有接收到字符 就一直向终端发送字符C break; //break switch } if (file_done != 0) //发送文件过程有错,退出文件传输 { break; //break for (file_done = 0) } } if (session_done != 0) //会话发生错误, 退出会话 { break; //break for (session_done = 0) } } return (int32_t)size; }