#include "Uart_Vcom.h" extern u8 W25Q256; //extern USBCCommonReg* gUSBC_ComReg; UINT8 Erase_Whole_Flash; UINT8 baud_index; UINT8 vcom_out_index; UINT8 vcom_buf_ID; vcom_rep vcom_rp[2]; UINT16 uart_rx_number; UINT16 uart_rx_endIndex; UINT8 uart_buf[4096+512]; UINT8 uart_act_index; UINT8 vcom_buf[2][4096+512]; //1024*8 UINT8 gu8EP3_OUT_Buffer[4096+512]; //UINT32 CRC32_Table[256]; UINT32 CRC_sAddr,CRC_Len,CRC_Old_Val,CRC_New_Val; UINT32 block_write_len,wring_block_Addr; UINT32 flh_rdPos,flh_Len,flh_Addr; UINT32 flh_sAddr,flh_eAddr; UINT8 flash_Buf[4096+512]; //UINT8 flash_rdBuf0[4096]; UINT8 flash_rdBuf1[4096]; UINT8 runSTEP; UINT32 cnCRC0,cnCRC1,cnCRC2; UINT8 inBuf[64]; UINT8 Flag_Loop=0; UINT8 count_Loop=0; UINT8 count_100ms=0; UINT8 FLag_Pos=0; const UINT32 bdRate[11]={ 9600, 19200, 38400, 57600, 115200, 230400, 256000, 460800, 921600, 1382400, 3000000 }; //const u8 mcuID[4] __attribute__((at(0x08001000))) = {0x90, 0x26, 0x71, 0x19}; //const u8 mcuID[4] __attribute__((at(0x08001000))) = {0x89, 0x76, 0x71, 0x19}; //crcH //crcL const u8 mcuID[4] = {0x89, 0x76, 0x71, 0x19}; //const u8 cfgID[6] __attribute__((at(0x08001200))) = {0x50, 0x1B, 0x4F, 0xFF,0x54,0x4C}; const u8 cfgID[6] = {0x50, 0x1B, 0x4F, 0xFF,0x54,0x4C}; //const u8 mkDATE[4] __attribute__((at(0x08001400))) = {0x22,0x05,0x03,0x01}; const u8 mkDATE[4] = {0x22,0x05,0x03,0x01}; UINT32 NandFlash_new_CheckCRC(UINT32 addr,UINT32 len); //iapfun jump2app; //动态显示的定时器 //#define PIT1_Val 3450*2 //1ms //#define PIT1_Val 3515 //1ms #define PIT1_Val 2812 //1ms void PIT1_reload(void) { PIT1->PMR = PIT1_Val; PIT1->PCSR |= PCSR_EN; } void PIT1_Init_Uart(void) { NVIC_Init(1, 0, PIT1_IRQn, 2); PIT1->PCSR &= (~PCSR_EN); PIT1->PCSR = (5<<8)|PCSR_OVW|PCSR_PIE|PCSR_RLD|PCSR_PDBG; //60Mhz/32 中断模式 PIT1->PMR = PIT1_Val; PIT1->PCSR &= ~PCSR_EN; } /**************************************************/ void PIT1_IRQHandler(void) { int kk; PIT1->PCSR &= ~PCSR_EN; //Disable PIT1 PIT1->PCSR |= PCSR_PIF; //clear PIF interrupt flag uart_rx_endIndex = 1; uart_act_index = 1; gu8EP3_OUT_Buffer[0] = uart_buf[0]; // printf("Pit\r\n"); if(gu8EP3_OUT_Buffer[0] == 0x6C) { /* id = 0;//uart_buf[5]; tUN.u8[0] = uart_buf[1]; tUN.u8[1] = uart_buf[2]; tUN.u8[2] = uart_buf[3]; tUN.u8[3] = uart_buf[4]; vcom_rp[id].wrAddr = tUN.u32; tUN.u8[0] = uart_buf[6]; tUN.u8[1] = uart_buf[7]; tUN.u8[2] = 0x00; tUN.u8[3] = 0x00; vcom_rp[id].wrLen = tUN.u32; for(kk=0;kkPCSR &= (~PCSR_EN); PIT2->PCSR = (7<<8)|PCSR_OVW|PCSR_PIE|PCSR_RLD|PCSR_PDBG; //60Mhz/128 中断模式 PIT2->PMR = 46875; PIT2->PCSR &= ~PCSR_EN; NVIC_Init(1, 0, PIT2_IRQn, 2); } void PIT2_IRQHandler(void) { PIT2->PCSR |= PCSR_PIF; //clear PIF interrupt flag count_100ms++; if(count_100ms>=5) { count_100ms=0; Flag_Loop=1; } } void Uart_Init(UINT32 pclk,UINT32 bound) { UINT32 band_rate=0; //设置波特率:115200 SCI1->SCIBRDF=(((pclk*8/bound)+1)/2)&0x003f; band_rate =(pclk*4/bound)>>6; SCI1->SCIBDH =(UINT8)((band_rate>>8)&0x00ff); SCI1->SCIBDL =(UINT8)(band_rate&0x00ff); SCI1->SCICR2 |= (1<<2)|(1<<3)|(1<<5); NVIC_Init(0, 0, SCI1_IRQn, 2); } //void SCI1_IRQHandler(void) //{ //// u8 cjc=0; // if((SCI1->SCISR1 & 0x20)== 0x20) // { //// cjc = SCI1->SCIDRL; //// LT_SingleByteToPc_uart3(cjc); // uart_buf[uart_rx_number++] = SCI1->SCIDRL; // // // if(uart_rx_number>(4096+512)) // uart_rx_number =0 ; // PIT1_reload(); // } //} void SerialOutChar(char c) { if (c == '\n') SerialOutChar('\r'); //while((SCI1->SCISR1 & 0x80) == 0x00); SCI1->SCIDRL = c; while((SCI1->SCISR1 & 0x40) == 0x00); } void SerialOutString(char *c) { char *p = c; while(*p) SerialOutChar(*p++); } void SendData(UINT16 data) { SCI1->SCIDRL =(UINT8)data; } void LT_UsartRxDisable(void) { SCI1->SCICR2 |= 0x20; } void LT_UsartRxEnable(void) { SCI1->SCICR2 &=~(0x20); } void LT_SingleByteToPc(UINT8 val) { while ((SCI1->SCISR1 & 0x40) == 0x00){}; SCI1->SCIDRL =val; } void LT_SingleByteToPc_uart3(UINT8 val) { SCI3->SCIDRL =val; while ((SCI3->SCISR1 & 0x40) == 0x00){}; } void uart_send(UINT8 *pCH,UINT32 len) { UINT16 i; gu8EP3_OUT_Buffer[0] = 0x00; for(i=0;i> 8) ^ CRC32_Table[(Crc&0xFF)^(*pch)]; // pch++; // } // return Crc; //} #define BLOCK_CRC 1 //U16 Find_A_NewGood_Block(void) //{ // BBM bbm[20]; // U16 ii,jj; // U16 EnNum = 0; // U16 block_id; // U16 newBlock; // // if((W25N01GV_ReadSR(0xC0) & 0x40) == 0x40) // 替换列表已经满了 // { // return 10000; // } // // W25N01GV_Read_BBM_LUT(bbm); // // for(ii=0;ii<20;ii++) // { //// if(bbm[ii].LBA&0x8000) // if((bbm[ii].LBA&0xc000) == 0x8000) // { // EnNum++; // //printf("bbm %d: 0x%x 0x%x\n",ii,bbm[ii].LBA,bbm[ii].PBA); // } // } // // if(EnNum == 20) return 10000; // // newBlock = 1023 - EnNum; // // //printf("newBlock = %d\n",newBlock); // // if(EnNum>0) // { // jj = EnNum-1; // block_id = bbm[jj].PBA&0x3FF; // if(newBlock >= block_id) // { // newBlock = block_id-1; // } // } // // //printf("newBlock = %d\n",newBlock); // // return newBlock; //} u8 test_count = 0; UINT32 Uart_RP_to_Flash(UINT8 id) { UINT32 i,l0,l1; UINT32 newBlock,newBlock_Addr; UINT32 pageAddr;// = vcom_rp[id].wrAddr/2048; //计算页地址; UINT32 crc1,crc2; UINT8 find_index=0; UINT8 *pWrBuf; // printf("Flash_type:%d\r\n",Flash_type); if(Flash_type<2) { if(Erase_Whole_Flash==0) { // printf("NorAddr: 0x%x %d\n",vcom_rp[id].wrAddr,id); if((vcom_rp[id].wrAddr%(1024*64))==0) { W25QXX_BlockErase64KB(vcom_rp[id].wrAddr/65536); } } l0 = vcom_rp[id].wrLen/256; l1 = vcom_rp[id].wrLen%256; if(l1) l0++; for(i=0;i0)||(block_write_len == 131072)) //每个Blcok最后一帧数据 { crc1 = GetCrc32((UINT8 *)0x20010000,block_write_len,0); crc2 = NandFlash_new_CheckCRC(wring_block_Addr,block_write_len); printf("***last one***\r\n"); // newBlock = Find_A_NewGood_Block(); // printf("newBlock=%d\n",newBlock); //printf("bkcrc1: 0x%x 0x%x\n",crc1,crc2); if(crc1 != crc2) //写入数据异常 { printf("data erro!!\r\n"); W25N01GV_Erase_Block(wring_block_Addr/131072);//擦除块,按页地址输入 l0 = block_write_len/2048; l1 = block_write_len%2048; if(l1) l0++; for(i=0;i> 8) ^ CRC32_Table[(cnCRC2&0xFF)^(gu8EP3_OUT_Buffer[i])]; // if(i==11) cnCRC1 = cnCRC2; // } // inBuf[0] = CMD_CONNECT_VCOM; // tUN.u32 = cnCRC2; // inBuf[1] = tUN.u8[0]; // inBuf[2] = tUN.u8[1]; // inBuf[3] = tUN.u8[2]; // inBuf[4] = tUN.u8[3]; // tUN.u32 = cnCRC1; // inBuf[5] = tUN.u8[0]; // inBuf[6] = tUN.u8[1]; // inBuf[7] = tUN.u8[2]; // inBuf[8] = tUN.u8[3]; // //tUN.u32 = makeDATE[0]; // inBuf[9] = mkDATE[3];//tUN.u8[0]; // inBuf[10] = mkDATE[2];//tUN.u8[1]; // inBuf[11] = mkDATE[1];//tUN.u8[2]; // inBuf[12] = mkDATE[0];//tUN.u8[3]; // // inBuf[13] = 0x81; // inBuf[14] = 0x00; // inBuf[15] = 0xBA; // inBuf[16] = 0xEE; //} unsigned char app_flag =0; void do_CMD_WR_MCU_PAGE(void) { // UN_u32Data tUN; // UINT32 *pSrc; // UINT8 rdByte; // UINT32 i,tCRC,cCRC; // pSrc = (UINT32 *)&gu8EP3_OUT_Buffer[8]; // // tUN.u8[0] = gu8EP3_OUT_Buffer[2]; // tUN.u8[1] = gu8EP3_OUT_Buffer[3]; // tUN.u8[2] = gu8EP3_OUT_Buffer[4]; // tUN.u8[3] = gu8EP3_OUT_Buffer[5]; // flh_Addr = 1024*256;//1024*39;//tUN.u32; //abs address // tUN.u8[0] = gu8EP3_OUT_Buffer[6]; // tUN.u8[1] = gu8EP3_OUT_Buffer[7]; // tUN.u8[2] = 0x00; // tUN.u8[3] = 0x00; // flh_Len = tUN.u32; // // tCRC = pSrc[6]; // cCRC = GetCrc32_Uart(gu8EP3_OUT_Buffer,flh_Len+8); // // if(tCRC == cCRC) // { // if((flh_Addr >= 0x7C00)&&(flh_Addr<0x7F000)) // { // EFLASH_Init(g_sys_clk/1000); // EFLASH_SetWritePermission(); // // for(i=0;i= 0x08008000 ) { if(tCRC == cCRC)//Datas crc ok { #if Display_Progress FLag_Pos=0; PIT2->PCSR |= PCSR_EN; #endif for(i=0;i0) tLen++; EFLASH_Init(g_sys_clk/1000); EFLASH_SetWritePermission(); printf("flh_Addr=%x\r\n",flh_Addr); EFLASH_Write(flh_Addr,flash_Buf,flh_rdPos); EFLASH_ClrWritePermission(); inBuf[1] = 0; } else inBuf[1] = 1; } else inBuf[1] = 2; inBuf[0] = CMD_UPDATE_FLASH; inBuf[2] = gu8EP3_OUT_Buffer[2]; inBuf[3] = gu8EP3_OUT_Buffer[3]; inBuf[4] = gu8EP3_OUT_Buffer[4]; inBuf[5] = gu8EP3_OUT_Buffer[5]; flh_rdPos = 0; } else if(gu8EP3_OUT_Buffer[0] == CMD_WR_MCU_PAGE)// { printf("enter CMD_WR_MCU_PAGE\r\n"); do_CMD_WR_MCU_PAGE(); } else if(gu8EP3_OUT_Buffer[0] == CMD_RESET) // { printf("enter CMD_RESET\r\n"); ttUN.u8[0] = gu8EP3_OUT_Buffer[1]; ttUN.u8[1] = gu8EP3_OUT_Buffer[2]; ttUN.u8[2] = gu8EP3_OUT_Buffer[3]; ttUN.u8[3] = gu8EP3_OUT_Buffer[4]; laddr = ttUN.u32; if((laddr == 0x8008000)||(laddr == 0x800A000)) { laddr=APP_ADDR; // inBuf[1] = 0; // inBuf[0] = CMD_RESET; // uart_send((UINT8*)inBuf, 63); //if(((laddr>=0x8000000)&&(laddr<=0x807F000))||((laddr>=0x20000000)&&(laddr<=0x20038000))) //防止运行非Flash或SRAM地址 { Disable_Interrupts; SPI_Cmd(SPI2,DISABLE); // gUSBC_ComReg->UCSR = 0x0; //DCACHE_Init(cacheOff, cacheOff, cacheOff, cacheOff); //ICACHE_Init(cacheOff, cacheOff, cacheOff, cacheOff); Run_Boot_ID=0x00000000; // jump2app=(iapfun)*(vu32*)(laddr + 4); // jump2app(); Set_POR_Reset(); while(1); } } else { inBuf[1] = 1; inBuf[0] = CMD_RESET; } } else if(gu8EP3_OUT_Buffer[0] == CMD_ERASE_FLASH) { printf("enter CMD_ERASE_FLASH\r\n"); ttUN.u8[0] = gu8EP3_OUT_Buffer[1]; ttUN.u8[1] = gu8EP3_OUT_Buffer[2]; ttUN.u8[2] = gu8EP3_OUT_Buffer[3]; ttUN.u8[3] = gu8EP3_OUT_Buffer[4]; flh_sAddr = DATA_FIELDS_BEGIN_ADDR + ttUN.u32; ttUN.u8[0] = gu8EP3_OUT_Buffer[5]; ttUN.u8[1] = gu8EP3_OUT_Buffer[6]; ttUN.u8[2] = gu8EP3_OUT_Buffer[7]; ttUN.u8[3] = gu8EP3_OUT_Buffer[8]; flh_eAddr = DATA_FIELDS_BEGIN_ADDR + ttUN.u32; ttUN.u8[0] = gu8EP3_OUT_Buffer[9]; ttUN.u8[1] = gu8EP3_OUT_Buffer[10]; ttUN.u8[2] = gu8EP3_OUT_Buffer[11]; ttUN.u8[3] = gu8EP3_OUT_Buffer[12]; tCRC = ttUN.u32; cCRC = GetCrc32(gu8EP3_OUT_Buffer,9,0); if(flh_sAddr >= 0x08008000) { if(tCRC == cCRC)//Datas crc ok { #if 0 EFLASH_Init(g_sys_clk/1000); EFLASH_SetWritePermission(); EFLASH_PageErase(0x8009E00);//测试:擦除Flash前将标志位清除 for(i=flh_sAddr;i0)&&(((i+1)%16) == 0)) printf("\n"); // cCRC = (cCRC >> 8) ^ CRC32_Table[(cCRC&0xFF)^(rdByte)]; // } // } // else cCRC = 0x0; // //cCRC = GetCrc32(Flash_SADDR,pSrc[1])&0xFFFF; //计算Flash内容的CRC // response[0] = CMD_CRC_FLASH; // // ttUN.u32 = cCRC; // response[1] = ttUN.u8[0]; // response[2] = ttUN.u8[1]; // response[3] = ttUN.u8[2]; // response[4] = ttUN.u8[3]; // // ttUN.u32 = tLen; // response[5] = ttUN.u8[0]; // response[6] = ttUN.u8[1]; // response[7] = ttUN.u8[2]; // response[8] = ttUN.u8[3]; // //Enable_Cache(); // //HID_IntInCallback(response); pSrc = (UINT32 *)&gu8EP3_OUT_Buffer; ttUN.u8[0] = gu8EP3_OUT_Buffer[16]; ttUN.u8[1] = gu8EP3_OUT_Buffer[17]; ttUN.u8[2] = gu8EP3_OUT_Buffer[18]; ttUN.u8[3] = gu8EP3_OUT_Buffer[19]; tCRC = ttUN.u32; cCRC = GetCrc32(gu8EP3_OUT_Buffer,16,0); if(tCRC == cCRC)//Datas crc ok { app_flag=1; //计算Flash CRC32 Value tLen = pSrc[1]; tCRC = pSrc[2]; enterAddr = DATA_FIELDS_BEGIN_ADDR+pSrc[3]; cCRC = 0x0; //EPORT1->EPDR |= 0x80; for(i=0;i> 8) ^ CRC32_Table[(cCRC&0xFF)^(rdByte)]; } //EPORT1->EPDR &= ~0x80; // cCRC = tCRC; // printf("\r\n\r\ntCRC:%x cCRC:%x tLen:%x\r\n\r\n",tCRC,cCRC,tLen); // cCRC = tCRC; if(tCRC==cCRC) { EFLASH_Init(g_sys_clk/1000); EFLASH_SetWritePermission(); tCRC = 0x21436587; EFLASH_PageErase(0x8009E00);//测试:擦除Flash前将标志位清除 EFLASH_WordsProg(0x8009E00,&tCRC,1); //测试:校准成功后将标志位写入0x21436587 EFLASH_ClrWritePermission(); Run_Boot_ID=0x00000000; #if Display_Progress PIT2->PCSR &= ~PCSR_EN; Flag_Loop=0; count_Loop=0; count_100ms=0; // LT768_Print_Internal_Font_String(202,120,Red,Blue2," "); LT768_Print_Internal_Font_String(322,120,Red,Blue2,"Check OK"); #endif } #if Display_Progress else LT768_Print_Internal_Font_String(322,120,Red,Blue2,"Check NG"); #endif } else cCRC = 0x0; inBuf[0] = CMD_CRC_FLASH; ttUN.u32 = cCRC; inBuf[1] = ttUN.u8[0]; inBuf[2] = ttUN.u8[1]; inBuf[3] = ttUN.u8[2]; inBuf[4] = ttUN.u8[3]; ttUN.u32 = tLen; inBuf[5] = ttUN.u8[0]; inBuf[6] = ttUN.u8[1]; inBuf[7] = ttUN.u8[2]; inBuf[8] = ttUN.u8[3]; } else { inBuf[0] = CMD_ERROR_PACKET; //HID_IntInCallback(response); } uart_send((UINT8*)inBuf, 63); // printf("***updata OK !!***\r\n"); //UINT32 enterAddr; // UINT32 *pSrc; // UINT32 i,laddr; // UINT32 tmpIER; // UINT32 tCRC,cCRC,tLen; // UINT8 rdByte; // //UINT8 response[64]; // UN_u32Data ttUN; // for(i=0;i<64;i++) inBuf[i] = 0x00; // if(gu8EP3_OUT_Buffer[0] == CMD_UPDATE_FLASH) // { // flh_rdPos = 0; // ttUN.u8[0] = gu8EP3_OUT_Buffer[2]; // ttUN.u8[1] = gu8EP3_OUT_Buffer[3]; // ttUN.u8[2] = gu8EP3_OUT_Buffer[4]; // ttUN.u8[3] = gu8EP3_OUT_Buffer[5]; // if(APP_ADDR > 0x8008000) // flh_Addr = APP_ADDR + ttUN.u32; // else flh_Addr = APP_ADDR + ttUN.u32; // ttUN.u8[0] = gu8EP3_OUT_Buffer[6]; // ttUN.u8[1] = gu8EP3_OUT_Buffer[7]; // ttUN.u8[2] = 0x00; // ttUN.u8[3] = 0x00; // flh_Len = ttUN.u32; // // for(i=0;i0) tLen++; // EFLASH_WordsProg(flh_Addr,pSrc,tLen); // // inBuf[1] = 0; // inBuf[0] = CMD_UPDATE_FLASH; // // inBuf[2] = gu8EP3_OUT_Buffer[2]; // inBuf[3] = gu8EP3_OUT_Buffer[3]; // inBuf[4] = gu8EP3_OUT_Buffer[4]; // inBuf[5] = gu8EP3_OUT_Buffer[5]; // // flh_rdPos = 0; // // } // else if(gu8EP3_OUT_Buffer[0] == CMD_WR_MCU_PAGE)// // { // do_CMD_WR_MCU_PAGE(); // } // else if(gu8EP3_OUT_Buffer[0] == CMD_UPDATE_SDRAM) //数据写入内存指定位置 // { //// do_CMD_UPDATE_SDRAM(); // return ; // } // else if(gu8EP3_OUT_Buffer[0] == CMD_RESET) // // { // ttUN.u8[0] = gu8EP3_OUT_Buffer[1]; // ttUN.u8[1] = gu8EP3_OUT_Buffer[2]; // ttUN.u8[2] = gu8EP3_OUT_Buffer[3]; // ttUN.u8[3] = gu8EP3_OUT_Buffer[4]; // laddr = APP_ADDR;//ttUN.u32; // // // if(((laddr>=0x8000000)&&(laddr<=0x807F000))||((laddr>=0x20000000)&&(laddr<=0x20038000))) //防止运行非Flash或SRAM地址 // { // Disable_Interrupts; // ///SPI_Cmd(SPI2,DISABLE); // PIT1->PCSR &= ~PCSR_EN; //// gUSBC_ComReg->UCSR = 0x0; //// jump2app=(iapfun)*(vu32*)(laddr + 4); //// jump2app(); //// while(1); // } // } // else if(gu8EP3_OUT_Buffer[0] == CMD_ERASE_FLASH) // { // ttUN.u8[0] = gu8EP3_OUT_Buffer[1]; // ttUN.u8[1] = gu8EP3_OUT_Buffer[2]; // ttUN.u8[2] = gu8EP3_OUT_Buffer[3]; // ttUN.u8[3] = gu8EP3_OUT_Buffer[4]; // // if(APP_ADDR > 0x8008000) // flh_sAddr = APP_ADDR + ttUN.u32; // else flh_sAddr = APP_ADDR + ttUN.u32; // // ttUN.u8[0] = gu8EP3_OUT_Buffer[5]; // ttUN.u8[1] = gu8EP3_OUT_Buffer[6]; // ttUN.u8[2] = gu8EP3_OUT_Buffer[7]; // ttUN.u8[3] = gu8EP3_OUT_Buffer[8]; // // if(APP_ADDR > 0x8008000) // flh_eAddr = APP_ADDR + ttUN.u32; // else flh_eAddr = APP_ADDR + ttUN.u32; // // //printf("\nStart to erase mcu flash 0x%x 0x%x\n",flh_sAddr,flh_eAddr); // // //sAddr,eAddr // // EFLASH_Init(g_sys_clk/1000); // EFLASH_SetWritePermission(); // // for(i=flh_sAddr;i 0x8008000) // enterAddr = APP_ADDR + pSrc[3]; // else enterAddr = APP_ADDR+pSrc[3]; // //计算Flash CRC32 Value // cCRC = 0x0; // // //printf("\nflh_sAddr=0x%x len=%d\n",flh_sAddr,tLen); // // for(i=0;i0)&&(((i+1)%16) == 0)) printf("\n"); //// cCRC = (cCRC >> 8) ^ CRC32_Table[(cCRC&0xFF)^(rdByte)]; // rdByte = EFLASH_ByteRead(enterAddr+i); // cCRC = (cCRC >> 8) ^ CRC32_Table[(cCRC&0xFF)^(rdByte)]; // } // printf("\r\n\r\ntCRC:%x cCRC:%x tLen:%x\r\n\r\n",tCRC,cCRC,tLen); // // printf("\n"); // //LTPrintf("cCRC:0x%x\n",cCRC); // //cCRC = GetCrc32(Flash_SADDR,pSrc[1])&0xFFFF; //计算Flash内容的CRC // inBuf[0] = CMD_CRC_FLASH; // // ttUN.u32 = cCRC; // inBuf[1] = ttUN.u8[0]; // inBuf[2] = ttUN.u8[1]; // inBuf[3] = ttUN.u8[2]; // inBuf[4] = ttUN.u8[3]; // // ttUN.u32 = tLen; // inBuf[5] = ttUN.u8[0]; // inBuf[6] = ttUN.u8[1]; // inBuf[7] = ttUN.u8[2]; // inBuf[8] = ttUN.u8[3]; // //Enable_Cache(); // //HID_IntInCallback(response); // } // else // { // inBuf[0] = CMD_ERROR_PACKET; // //HID_IntInCallback(response); // } // uart_send((UINT8*)inBuf, 63); } UINT32 Nor_Flash_CheckCRC(UINT32 addr,UINT32 len) { #if 0 UINT8 inBuf[64]; UINT8 rdByte; UINT32 i=0,j=0,k,l0,l1; UINT32 newCRC; UN_u32Data tUN; UINT32 addrbyteNum; volatile unsigned char temp; int txnum,rxnum; SSI_TypeDef *SSIx=SSI1; //计算Flash CRC32 Value newCRC = 0x0; txnum=len; SSIx->SSI_DR = W25X_ReadData; if (Flash_type==1) { SSIx->SSI_DR = (addr>>24)&0xff; addrbyteNum = 5; } else addrbyteNum = 4; SSIx->SSI_DR = (addr>>16)&0xff; SSIx->SSI_DR = (addr>>8)&0xff; SSIx->SSI_DR = (addr>>0)&0xff; __asm("nop"); __asm("nop"); __asm("nop"); while(len>0) { if( (SSIx->SSI_SR&SR_TFNF)&&(txnum>0) ) { SSIx->SSI_DR=DUMMY_BYTE; txnum--; } if(SSIx->SSI_SR&SR_RFNE) { if(j < addrbyteNum) { temp=SSIx->SSI_DR; j++; } else { rdByte = SSIx->SSI_DR; newCRC = (newCRC >> 8) ^ CRC32_Table[(newCRC&0xFF)^(rdByte)]; i++; len--; } } if(i>0) { if((i%0x100000) == 0) { inBuf[0] = 0x7E; inBuf[1] = 0x30; tUN.u32 = i; inBuf[2] = tUN.u8[0]; inBuf[3] = tUN.u8[1]; inBuf[4] = tUN.u8[2]; inBuf[5] = tUN.u8[3]; //EP_IN_Transfer(EP2,gu8EP2_IN_Buffer,EP2_SIZE,inBuf,EP2_SIZE); if(uart_act_index == 1) { uart_send((UINT8*)inBuf, 63); } else usb_cdc_send(INDEX_EP1, (UINT8*)inBuf, 63); //printf("0x%x\r\n",i); } } } while(SSIx->SSI_SR&SR_BUSY){;} #else UINT8 inBuf[64]; UINT8 rdByte; UINT32 i=0,j=0,k,l0,l1; UINT32 newCRC; UN_u32Data tUN; UINT32 addrRead; volatile unsigned char temp; //计算Flash CRC32 Value newCRC = 0x0; l0 = len/0x100000; l1 = len%0x100000; addrRead = addr; //printf("l0 = %d,l1 = %d\r\n",addrRead,len); for(i=0;i> 8) ^ CRC32_Table[(newCRC&0xFF)^(rdByte)]; } } inBuf[0] = 0x7E; inBuf[1] = 0x30; tUN.u32 = 1024*1024*(i+1); inBuf[2] = tUN.u8[0]; inBuf[3] = tUN.u8[1]; inBuf[4] = tUN.u8[2]; inBuf[5] = tUN.u8[3]; tUN.u32 = newCRC; inBuf[6] = tUN.u8[0]; inBuf[7] = tUN.u8[1]; inBuf[8] = tUN.u8[2]; inBuf[9] = tUN.u8[3]; if(uart_act_index == 1) { uart_send((UINT8*)inBuf, 63); } //printf("0x%x\r\n",1024*1024*(i+1)); } l0 = l1/2048; l1 = l1%2048; //printf("l0 = %d,l1 = %d\r\n",l0,l1); for(i=0;i> 8) ^ CRC32_Table[(newCRC&0xFF)^(rdByte)]; } } if(l1>0) { W25QXX_Read(flash_rdBuf1 ,addrRead , 2048); addrRead += 2048; for(j=0;j> 8)^CRC32_Table[(newCRC&0xFF)^(rdByte)]; } } #endif return newCRC; } UINT32 NandFlash_CheckCRC(UINT32 addr,UINT32 len) { UINT8 inBuf[64]; UINT8 rdByte; UINT32 i,j,k,l0,l1; UINT32 newCRC; UN_u32Data tUN; UINT32 pageID=addr/2048; volatile unsigned char temp; #if 1 newCRC = 0x0; l0 = len/0x100000; l1 = len%0x100000; for(i=0;i> 8) ^ CRC32_Table[(newCRC&0xFF)^(rdByte)]; } } inBuf[0] = 0x7E; inBuf[1] = 0x30; tUN.u32 = 1024*1024*(i+1); inBuf[2] = tUN.u8[0]; inBuf[3] = tUN.u8[1]; inBuf[4] = tUN.u8[2]; inBuf[5] = tUN.u8[3]; if(uart_act_index == 1) { uart_send((UINT8*)inBuf, 63); } } l0 = l1/2048; l1 = l1%2048; for(i=0;i> 8) ^ CRC32_Table[(newCRC&0xFF)^(rdByte)]; } } if(l1>0) { W25N01GV_ReadPageAddr_Data(flash_rdBuf1,0x0000,pageID++,2048); for(j=0;j> 8)^CRC32_Table[(newCRC&0xFF)^(rdByte)]; } } #else newCRC = 0x0; l0 = len/0x100000; l1 = len%0x100000; //printf("l0 = %d,l1 = %d\r\n",l0,l1); for(i=0;i> 8) ^ CRC32_Table[(newCRC&0xFF)^(rdByte)]; } } inBuf[0] = 0x7E; inBuf[1] = 0x30; tUN.u32 = 1024*1024*(i+1); inBuf[2] = tUN.u8[0]; inBuf[3] = tUN.u8[1]; inBuf[4] = tUN.u8[2]; inBuf[5] = tUN.u8[3]; //EP_IN_Transfer(EP2,gu8EP2_IN_Buffer,EP2_SIZE,inBuf,EP2_SIZE); if(uart_act_index == 1) { uart_send((UINT8*)inBuf, 63); } else usb_cdc_send(INDEX_EP1, (UINT8*)inBuf, 63); //printf("0x%x\r\n",1024*1024*(i+1)); } l0 = l1/2048; l1 = l1%2048; //printf("l0 = %d,l1 = %d\r\n",l0,l1); for(i=0;i> 8) ^ CRC32_Table[(newCRC&0xFF)^(rdByte)]; } } if(l1>0) { LT_W25N01GV_Load_PageData_toBuf(pageID++); LT_W25N01GV_Read(flash_rdBuf1,0x0000,2048); for(j=0;j> 8)^CRC32_Table[(newCRC&0xFF)^(rdByte)]; } } //printf("newCRC = 0x%x\r\n",newCRC); #endif return newCRC; } UINT32 NandFlash_new_CheckCRC(UINT32 addr,UINT32 len) { // UINT8 inBuf[64]; UINT8 rdByte; UINT32 i,j,k,l0,l1; UINT32 newCRC; // UN_u32Data tUN; UINT32 pageID=addr/2048; volatile unsigned char temp; newCRC = 0x0; l0 = len/0x100000; l1 = len%0x100000; for(i=0;i> 8) ^ CRC32_Table[(newCRC&0xFF)^(rdByte)]; } } // inBuf[0] = 0x7E; // inBuf[1] = 0x30; // tUN.u32 = 1024*1024*(i+1); // inBuf[2] = tUN.u8[0]; // inBuf[3] = tUN.u8[1]; // inBuf[4] = tUN.u8[2]; // inBuf[5] = tUN.u8[3]; // if(uart_act_index == 1) // { // uart_send((UINT8*)inBuf, 63); // } } l0 = l1/2048; l1 = l1%2048; for(i=0;i> 8) ^ CRC32_Table[(newCRC&0xFF)^(rdByte)]; } } if(l1>0) { W25N01GV_ReadPageAddr_Data(flash_rdBuf1,0x0000,pageID++,2048); for(j=0;j> 8)^CRC32_Table[(newCRC&0xFF)^(rdByte)]; } } return newCRC; } void do_all_uart(void) { int id,kk; // UINT8 tmpU8; UINT8 inBuf[64]; UN_u32Data tUN; UINT32 tmpU32; UINT32 oldCRC,newCRC; UINT32 *pCU32; UINT32 i; int pos=0; // printf("enter do_all_uart\r\n"); if(uart_rx_endIndex == 1)//Uart有数据接收 { uart_rx_endIndex = 0; // printf("gu8=%x\r\n",gu8EP3_OUT_Buffer[0]); if(gu8EP3_OUT_Buffer[0]&0x20) { // printf("enter gu8EP3_OUT_Buffer[0]&0x20\r\n"); if(gu8EP3_OUT_Buffer[0] == 0x6C) { // printf("enter gu8EP3_OUT_Buffer[0] == 0x6C\r\n"); id = uart_buf[5]; printf("id=%d\r\n",id); tUN.u8[0] = uart_buf[1]; tUN.u8[1] = uart_buf[2]; tUN.u8[2] = uart_buf[3]; tUN.u8[3] = uart_buf[4]; vcom_rp[id].wrAddr = tUN.u32; tUN.u8[0] = uart_buf[6]; tUN.u8[1] = uart_buf[7]; tUN.u8[2] = 0x00; tUN.u8[3] = 0x00; vcom_rp[id].wrLen = tUN.u32; //Datas to write buffer for(kk=0;kkPCSR |= PCSR_EN; #endif vcom_rp[id].fullIndex = 1; uart_rx_number =0 ; if(vcom_rp[0].fullIndex == 1) { tmpU32 = Uart_RP_to_Flash(0); vcom_rp[0].fullIndex = 0; inBuf[0] = 0x7C; if(tmpU32==0) inBuf[1] = 0; else inBuf[1] = 2; uart_send((UINT8*)inBuf, 63); } if(vcom_rp[1].fullIndex == 1) { tmpU32 = Uart_RP_to_Flash(1); vcom_rp[1].fullIndex = 0; inBuf[0] = 0x7C; if(tmpU32==0) inBuf[1] = 1; else inBuf[1] = 2; uart_send((UINT8*)inBuf, 63); } } else//Datas crc error { inBuf[0] = 0x7C; inBuf[1] = 2; uart_send((UINT8*)inBuf, 63); } } //else else { printf("enter gu8EP3_OUT_Buffer[0]&0x20 else\r\n"); if(uart_buf[0]==0x6D) //读取Flash ID { printf("enter 0x6D\r\n"); vcom_out_index = 0; Erase_Whole_Flash = 0; if(gu8EP3_OUT_Buffer[2] == 1) tUN.u32 = W25N01GV_ReadID(); else tUN.u32 = W25QXX_ReadID_9F(); inBuf[0] = 0x7D; inBuf[1] = tUN.u8[0]; inBuf[2] = tUN.u8[1]; inBuf[3] = tUN.u8[2]; inBuf[4] = tUN.u8[3]; uart_send((UINT8*)inBuf, 63); #if Display_Progress LT768_Print_Internal_Font_String(70,170,Red,Blue2,"Update Flash"); #endif } if(gu8EP3_OUT_Buffer[0]==0x6F) //设置Addr_32bits= 1 { printf("enter 0x6F\r\n"); vcom_out_index = 0; Flash_type = gu8EP3_OUT_Buffer[1]; // printf("type_6F:%d",Flash_type); if(Flash_type==1) { W25Q256 = 1; W25QXX_Enter_4Byte_AddressMode(); } inBuf[0] = 0x7F; inBuf[1] = 0; uart_send((UINT8*)inBuf, 63); } if(gu8EP3_OUT_Buffer[0]==0x68) //擦除整个Flash { printf("enter 0x68\r\n"); vcom_out_index = 0; if(Flash_type<2) { Erase_Whole_Flash = 1; W25QXX_Erase_Chip(); } inBuf[0] = 0x78; inBuf[1] = 0; uart_send((UINT8*)inBuf, 63); } if(gu8EP3_OUT_Buffer[0]==0x6E) //计算Flash CRC { printf("enter 0x6E\r\n"); vcom_out_index = 0; tUN.u8[0] = gu8EP3_OUT_Buffer[1]; tUN.u8[1] = gu8EP3_OUT_Buffer[2]; tUN.u8[2] = gu8EP3_OUT_Buffer[3]; tUN.u8[3] = gu8EP3_OUT_Buffer[4]; CRC_sAddr = tUN.u32; //校验开始地址 tUN.u8[0] = gu8EP3_OUT_Buffer[5]; tUN.u8[1] = gu8EP3_OUT_Buffer[6]; tUN.u8[2] = gu8EP3_OUT_Buffer[7]; tUN.u8[3] = gu8EP3_OUT_Buffer[8]; CRC_Len = tUN.u32; //校验数据长度 tUN.u8[0] = gu8EP3_OUT_Buffer[9]; tUN.u8[1] = gu8EP3_OUT_Buffer[10]; tUN.u8[2] = gu8EP3_OUT_Buffer[11]; tUN.u8[3] = gu8EP3_OUT_Buffer[12]; CRC_Old_Val = tUN.u32; tUN.u8[0] = gu8EP3_OUT_Buffer[13]; tUN.u8[1] = gu8EP3_OUT_Buffer[14]; tUN.u8[2] = gu8EP3_OUT_Buffer[15]; tUN.u8[3] = gu8EP3_OUT_Buffer[16]; oldCRC = tUN.u32; newCRC = GetCrc32(gu8EP3_OUT_Buffer,13,0); if(oldCRC == newCRC)//Datas crc ok { // for(i = 0;i < 3;i++) // { // if(Flash_type<2) // CRC_New_Val = Nor_Flash_CheckCRC(CRC_sAddr,CRC_Len); // else CRC_New_Val = NandFlash_CheckCRC(CRC_sAddr,CRC_Len); // if(CRC_Old_Val == CRC_New_Val) // { // inBuf[1] = 0x31; // break; // } // else inBuf[1] = 0x32; // } if(Flash_type<2) CRC_New_Val = Nor_Flash_CheckCRC(CRC_sAddr,CRC_Len); else CRC_New_Val = NandFlash_CheckCRC(CRC_sAddr,CRC_Len); if(CRC_Old_Val == CRC_New_Val) inBuf[1] = 0x31; else inBuf[1] = 0x32; } else inBuf[1] = 0x32; #if Display_Progress if(inBuf[1]==0x31) { PIT2->PCSR &= ~PCSR_EN; Flag_Loop=0; count_Loop=0; count_100ms=0; // LT768_Print_Internal_Font_String(226,170,Red,Blue2," "); LT768_Print_Internal_Font_String(322,170,Red,Blue2,"Check OK"); } else { // LT768_Print_Internal_Font_String(226,170,Red,Blue2," "); LT768_Print_Internal_Font_String(322,170,Red,Blue2,"Check NG"); } #endif //SS_Inactive; DelayMS(250);//DelayMS(8); inBuf[0] = 0x7E; tUN.u32 = CRC_New_Val; inBuf[2] = tUN.u8[0]; inBuf[3] = tUN.u8[1]; inBuf[4] = tUN.u8[2]; inBuf[5] = tUN.u8[3]; tUN.u32 = CRC_Len; inBuf[6] = tUN.u8[0]; inBuf[7] = tUN.u8[1]; inBuf[8] = tUN.u8[2]; inBuf[9] = tUN.u8[3]; tUN.u32 = CRC_sAddr; inBuf[10] = tUN.u8[0]; inBuf[11] = tUN.u8[1]; inBuf[12] = tUN.u8[2]; inBuf[13] = tUN.u8[3]; uart_send((UINT8*)inBuf, 63); } } } else { if(gu8EP3_OUT_Buffer[0] == CMD_CONNECT_VCOM) { printf("CMD_CONNECT_VCOM=%x\r\n",gu8EP3_OUT_Buffer[0]); pCU32 = (UINT32 *)&gu8EP3_OUT_Buffer; cnCRC0 = pCU32[2]; // get_connect_info(); gu8EP3_OUT_Buffer[8] = mcuID[0];//0x8C; gu8EP3_OUT_Buffer[9] = mcuID[1];//0x26; gu8EP3_OUT_Buffer[10] = mcuID[2];//0x71; gu8EP3_OUT_Buffer[11] = mcuID[3];//0x19; gu8EP3_OUT_Buffer[12] = cfgID[0];//0x50; gu8EP3_OUT_Buffer[13] = cfgID[2];//0x4F; gu8EP3_OUT_Buffer[14] = cfgID[4];//0x54; gu8EP3_OUT_Buffer[15] = cfgID[5];//0x4C; cnCRC2 = 0x0; for(i=0;i<16;i++) { cnCRC2 = (cnCRC2 >> 8) ^ CRC32_Table[(cnCRC2&0xFF)^(gu8EP3_OUT_Buffer[i])]; if(i==11) cnCRC1 = cnCRC2; } inBuf[0] = CMD_CONNECT_VCOM; tUN.u32 = cnCRC2; inBuf[1] = tUN.u8[0]; inBuf[2] = tUN.u8[1]; inBuf[3] = tUN.u8[2]; inBuf[4] = tUN.u8[3]; tUN.u32 = cnCRC1; inBuf[5] = tUN.u8[0]; inBuf[6] = tUN.u8[1]; inBuf[7] = tUN.u8[2]; inBuf[8] = tUN.u8[3]; //tUN.u32 = makeDATE[0]; inBuf[9] = mkDATE[3];//tUN.u8[0]; inBuf[10] = mkDATE[2];//tUN.u8[1]; inBuf[11] = mkDATE[1];//tUN.u8[2]; inBuf[12] = mkDATE[0];//tUN.u8[3]; inBuf[13] = 0x81; inBuf[14] = 0x00; inBuf[15] = 0xBA; inBuf[16] = 0xEE; inBuf[17] = gu8EP3_OUT_Buffer[12]; inBuf[18] = gu8EP3_OUT_Buffer[13]; if(cnCRC0 == cnCRC1) { baud_index = gu8EP3_OUT_Buffer[16]; } // printf("uart_act_index:%d\r\n\r\n",uart_act_index); uart_send((UINT8*)inBuf, 63); } else if(gu8EP3_OUT_Buffer[0] == CMD_GET_INFO) { printf("CMD_GET_INFO=%x\r\n",gu8EP3_OUT_Buffer[0]); pCU32 = (UINT32 *)&gu8EP3_OUT_Buffer; // get_device_info(); pos=0; inBuf[pos++] = CMD_GET_INFO; inBuf[pos++] = gu8EP3_OUT_Buffer[1]; inBuf[pos++] = gu8EP3_OUT_Buffer[2]; inBuf[pos++] = gu8EP3_OUT_Buffer[3]; inBuf[pos++] = mcuID[3];//4 inBuf[pos++] = mcuID[2];//5 inBuf[pos++] = mcuID[1];//6 inBuf[pos++] = mcuID[0];//7 inBuf[pos++] = mkDATE[3];//8 inBuf[pos++] = mkDATE[2];//9 inBuf[pos++] = mkDATE[1];//10 inBuf[pos++] = mkDATE[0];//11 tUN.u32 = W25QXX_ReadID_9F(); inBuf[pos++] = tUN.u8[0];//12 inBuf[pos++] = tUN.u8[1];//13 inBuf[pos++] = tUN.u8[2];//14 inBuf[pos++] = tUN.u8[3];//15 tUN.u32 = W25N01GV_ReadID(); inBuf[pos++] = tUN.u8[0];//16 inBuf[pos++] = tUN.u8[1];//17 inBuf[pos++] = tUN.u8[2];//18 inBuf[pos++] = tUN.u8[3];//19 cnCRC0 = GetCrc32(inBuf,pos,0); tUN.u32 = cnCRC0; inBuf[pos++] = tUN.u8[0]; inBuf[pos++] = tUN.u8[1]; inBuf[pos++] = tUN.u8[2]; inBuf[pos++] = tUN.u8[3]; cnCRC0 = pCU32[2]; cnCRC1 = GetCrc32(gu8EP3_OUT_Buffer,8,0); if(cnCRC0 == cnCRC1) { // if((gu8EP3_OUT_Buffer[1] == 0xE8)&&(gu8EP3_OUT_Buffer[2] == 0x17)) // uart_checkCRC_index = 1; // else uart_checkCRC_index = 0; baud_index = gu8EP3_OUT_Buffer[3]; } else { inBuf[1] = 0x01; inBuf[2] = 0x00; //inBuf[3] = ; } uart_send((UINT8*)inBuf, 63); } //CMD_SET_SWD else if(gu8EP3_OUT_Buffer[0] == CMD_SET_SWD) { printf("enter CMD_SET_SWD"); if(uart_buf[1] == 0xAA) { SWD_Enable_Cmd(TRUE); //SWD enable } else { SWD_Enable_Cmd(FALSE); //SWD disable } inBuf[0] = CMD_SET_SWD; uart_send((UINT8*)inBuf, 63); } else { Uart_ParseCmd();//ParseCmd(); //MCU Update } } } }