From 36eb53236edaa9e1c6b11fc7e24b4414e60ce479 Mon Sep 17 00:00:00 2001 From: Benwang217 Date: Tue, 20 Oct 2020 14:36:04 +0800 Subject: [PATCH] V1.11.3 --- .gitignore | 3 +- ChipInfoDb.h | 26 +- FlashCommand.c | 348 +++++------ FlashCommand.h | 4 +- IntelHexFile.c | 335 +++++------ IntelHexFile.h | 5 +- Macro.h | 73 +-- Makefile | 25 +- MotorolaFile.c | 361 ++++++----- MotorolaFile.h | 5 +- SerialFlash.c | 720 ++++++++-------------- SerialFlash.h | 9 +- board.c | 1368 ++++++++++++++++++++--------------------- board.h | 12 +- dpcmd.c | 679 +++++++-------------- dpcmd.h | 30 +- parse.c | 1041 +++++++++++++++----------------- project.c | 1574 ++++++++++++++++++++++-------------------------- project.h | 62 +- usbdriver.c | 192 +++--- usbdriver.h | 9 +- 21 files changed, 3011 insertions(+), 3870 deletions(-) diff --git a/.gitignore b/.gitignore index a7526b2..ef281e1 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ +*~ *.o -/dpcmd +dpcmd diff --git a/ChipInfoDb.h b/ChipInfoDb.h index d524824..34199a5 100755 --- a/ChipInfoDb.h +++ b/ChipInfoDb.h @@ -1,12 +1,13 @@ #pragma once -#ifndef ChipInfo_H +#ifndef ChipInfo_H #define ChipInfo_H +#include "Macro.h" + #define NUMBER_OF_SUPPORTING_CHIPS 3 -struct m_code_api -{ +struct m_code_api { int (*m_code_api_doRDSR)(unsigned char *cSR, int Index); int (*m_code_api_doWRSR)(unsigned char cSR,int Index); int (*m_code_api_doChipErase)(int Index); @@ -15,23 +16,12 @@ struct m_code_api int (*m_code_api_doSegmentErase)(void); }; - - +bool Dedi_List_AllChip(void); int ChipInfoDbFindItem(CHIP_INFO ChipInfoDb[], int NumberOfItems, long JedecDeviceIDToFind); - void ChipInfoDump(long JedecDeviceIDToFind); - long ChipInfoDumpChipSizeInKByte(long Jedec); -int Dedi_Search_Chip_Db_ByTypeName(char* TypeName, CHIP_INFO *Chip_Info, int Index); -void getExecPath(char* Path); - +int Dedi_Search_Chip_Db(long RDIDCommand, long UniqueID, CHIP_INFO* Chip_Info, int search_all); +int Dedi_Search_Chip_Db_ByTypeName(char* TypeName, CHIP_INFO* Chip_Info); +FILE* openChipInfoDb(void); #endif - - - - - - - - diff --git a/FlashCommand.c b/FlashCommand.c index ea90595..7bedadc 100755 --- a/FlashCommand.c +++ b/FlashCommand.c @@ -1,29 +1,28 @@ + /// CFlashCommand Implementations -//#include "stdafx.h" #include "Macro.h" -#include "usbdriver.h" #include "FlashCommand.h" -#define SerialFlash_FALSE -1 -#define SerialFlash_TRUE 1 +#include "project.h" +#include "usbdriver.h" +#define SerialFlash_FALSE -1 +#define SerialFlash_TRUE 1 + extern bool Is_NewUSBCommand(int Index); -int FlashCommand_TransceiveOut(unsigned char *v, int len ,int has_result_in,int Index) +int FlashCommand_TransceiveOut(unsigned char* v, int len, int has_result_in, int Index) { - CNTRPIPE_RQ rq ; - - rq.Function = URB_FUNCTION_VENDOR_ENDPOINT ; - rq.Direction = VENDOR_DIRECTION_OUT ; - rq.Request = TRANSCEIVE ; - if(Is_NewUSBCommand(Index)) - { - rq.Value = ((has_result_in==1)? RESULT_IN : NO_RESULT_IN ); - rq.Index = RFU ; - } - else - { - rq.Value = RFU ; - rq.Index = ((has_result_in==1)? RESULT_IN : NO_RESULT_IN ); - } + CNTRPIPE_RQ rq; + + rq.Function = URB_FUNCTION_VENDOR_ENDPOINT; + rq.Direction = VENDOR_DIRECTION_OUT; + rq.Request = TRANSCEIVE; + if (Is_NewUSBCommand(Index)) { + rq.Value = ((has_result_in == 1) ? RESULT_IN : NO_RESULT_IN); + rq.Index = RFU; + } else { + rq.Value = RFU; + rq.Index = ((has_result_in == 1) ? RESULT_IN : NO_RESULT_IN); + } rq.Length = len; #if 0 printf("len = %d",len); @@ -34,209 +33,188 @@ int FlashCommand_TransceiveOut(unsigned char *v, int len ,int has_result_in,int return OutCtrlRequest(&rq, v, len, Index); } -int FlashCommand_TransceiveIn(unsigned char *v, int len, int Index) +int FlashCommand_TransceiveIn(unsigned char* v, int len, int Index) { - CNTRPIPE_RQ rq ; - - rq.Function = URB_FUNCTION_VENDOR_ENDPOINT ; - rq.Direction = VENDOR_DIRECTION_IN ; - rq.Request = TRANSCEIVE ; - if(Is_NewUSBCommand(Index)) - { - rq.Value = 0x01; - rq.Index = NO_REGISTER ; - } - else - { - rq.Value = CTRL_TIMEOUT ; - rq.Index = NO_REGISTER ; - } - rq.Length = len ; - - if(InCtrlRequest(&rq, v, (unsigned long)len, Index)==SerialFlash_FALSE) + CNTRPIPE_RQ rq; + + rq.Function = URB_FUNCTION_VENDOR_ENDPOINT; + rq.Direction = VENDOR_DIRECTION_IN; + rq.Request = TRANSCEIVE; + if (Is_NewUSBCommand(Index)) { + rq.Value = 0x01; + rq.Index = NO_REGISTER; + } else { + rq.Value = CTRL_TIMEOUT; + rq.Index = NO_REGISTER; + } + rq.Length = len; + + if (InCtrlRequest(&rq, v, (unsigned long)len, Index) == SerialFlash_FALSE) return SerialFlash_FALSE; return FlashCommand_TRUE; } - -int FlashCommand_SendCommand_OutOnlyInstruction(unsigned char *v, int len, int Index) -{ - return FlashCommand_TransceiveOut(v, len,NO_RESULT_IN, Index); +int FlashCommand_SendCommand_OutOnlyInstruction(unsigned char* v, int len, int Index) +{ + return FlashCommand_TransceiveOut(v, len, NO_RESULT_IN, Index); } -int FlashCommand_SendCommand_OutInstructionWithCS(unsigned char *v, int len, int Index) +int FlashCommand_SendCommand_OutInstructionWithCS(unsigned char* v, int len, int Index) { - CNTRPIPE_RQ rq ; - - rq.Function = URB_FUNCTION_VENDOR_ENDPOINT ; - rq.Direction = VENDOR_DIRECTION_OUT ; - rq.Request = TRANSCEIVE ; - if(Is_NewUSBCommand(Index)) - { - rq.Value = 0x02 ; - rq.Index = 0; - } - else - { - rq.Value = RFU ; - rq.Index = 0x02 ; - } - rq.Length = len ; - - return OutCtrlRequest(&rq, v, len, Index); + CNTRPIPE_RQ rq; + + rq.Function = URB_FUNCTION_VENDOR_ENDPOINT; + rq.Direction = VENDOR_DIRECTION_OUT; + rq.Request = TRANSCEIVE; + if (Is_NewUSBCommand(Index)) { + rq.Value = 0x02; + rq.Index = 0; + } else { + rq.Value = RFU; + rq.Index = 0x02; + } + rq.Length = len; + + return OutCtrlRequest(&rq, v, len, Index); } -int FlashCommand_SendCommand_OneOutOneIn(unsigned char *vOut, int out_len, unsigned char *vIn, int in_len, int Index ) +int FlashCommand_SendCommand_OneOutOneIn(unsigned char* vOut, int out_len, unsigned char* vIn, int in_len, int Index) { - if(FlashCommand_TransceiveOut(vOut, out_len, RESULT_IN,Index) == SerialFlash_FALSE) { - return SerialFlash_FALSE; - } - if(FlashCommand_TransceiveIn(vIn, in_len, Index) == SerialFlash_FALSE) { - return SerialFlash_FALSE; - } + if (FlashCommand_TransceiveOut(vOut, out_len, RESULT_IN, Index) == SerialFlash_FALSE) { + return SerialFlash_FALSE; + } + if (FlashCommand_TransceiveIn(vIn, in_len, Index) == SerialFlash_FALSE) { + return SerialFlash_FALSE; + } return FlashCommand_TRUE; } -int FlashCommand_SendCommand_SetupPacketForBulkWrite(struct CAddressRange *AddrRange, unsigned char modeWrite,unsigned char WriteCom,int Index) +int FlashCommand_SendCommand_SetupPacketForBulkWrite(struct CAddressRange* AddrRange, unsigned char modeWrite, unsigned char WriteCom, int Index) { - - unsigned char vInstruction[10] ; - CNTRPIPE_RQ rq ; + unsigned char vInstruction[10]; + CNTRPIPE_RQ rq; // length in terms of 256/128 bytes size_t divider; - rq.Function = URB_FUNCTION_VENDOR_ENDPOINT ; - rq.Direction = VENDOR_DIRECTION_OUT ; - rq.Request = WRITE ; - switch(modeWrite) - { - case MODE_NUMONYX_PCM:// 512 bytes - case PP_32BYTE: - divider=9; - break; - case PP_128BYTE: //128 bytes - divider=7; - break; - default: // 256 bytes - divider=8; - break; - } - - size_t pageNum = (AddrRange->end - AddrRange->start) >> divider ; - - vInstruction[0] = (unsigned char)(pageNum & 0xff) ; // lowest byte of length : page number - vInstruction[1] = (unsigned char)( (pageNum >> 8) & 0xff) ; // highest byte of length: page number - vInstruction[2] = (unsigned char)( (pageNum >> 16) & 0xff) ; // reserved - vInstruction[3] = modeWrite; // PAGE_PROGRAM, PAGE_WRITE, AAI_1_BYTE, AAI_2_BYTE, PP_128BYTE, PP_AT26DF041 + rq.Function = URB_FUNCTION_VENDOR_ENDPOINT; + rq.Direction = VENDOR_DIRECTION_OUT; + rq.Request = WRITE; + switch (modeWrite) { + case MODE_NUMONYX_PCM: // 512 bytes + case PP_32BYTE: + divider = 9; + break; + case PP_128BYTE: // 128 bytes + divider = 7; + break; + default: // 256 bytes + divider = 8; + break; + } + + size_t pageNum = (AddrRange->end - AddrRange->start) >> divider; + + vInstruction[0] = (unsigned char)(pageNum & 0xff); // lowest byte of length : page number + vInstruction[1] = (unsigned char)((pageNum >> 8) & 0xff); // highest byte of length: page number + vInstruction[2] = (unsigned char)((pageNum >> 16) & 0xff); // reserved + vInstruction[3] = modeWrite; // PAGE_PROGRAM, PAGE_WRITE, AAI_1_BYTE, AAI_2_BYTE, PP_128BYTE, PP_AT26DF041 vInstruction[4] = WriteCom; - if(Is_NewUSBCommand(Index)) - { - vInstruction[5] = 0; - vInstruction[6] = (AddrRange->start & 0xff); - vInstruction[7] = ((AddrRange->start >> 8) & 0xff); - vInstruction[8] = ((AddrRange->start >> 16) & 0xff); - vInstruction[9] = ((AddrRange->start >> 24) & 0xff); - rq.Value = 0; - rq.Index = 0; - rq.Length = (unsigned long)(10) ; - } - else - { - rq.Value = (unsigned short)(AddrRange->start & 0xffff) ; //16 bits LSB - rq.Index = (unsigned short)((AddrRange->start >> 16) & 0xffff) ; //16 bits MSB - rq.Length = (unsigned long)(5) ; - } + if (Is_NewUSBCommand(Index)) { + vInstruction[5] = 0; + vInstruction[6] = (AddrRange->start & 0xff); + vInstruction[7] = ((AddrRange->start >> 8) & 0xff); + vInstruction[8] = ((AddrRange->start >> 16) & 0xff); + vInstruction[9] = ((AddrRange->start >> 24) & 0xff); + rq.Value = 0; + rq.Index = 0; + rq.Length = (unsigned long)(10); + } else { + rq.Value = (unsigned short)(AddrRange->start & 0xffff); // 16 bits LSB + rq.Index = (unsigned short)((AddrRange->start >> 16) & 0xffff); // 16 bits MSB + rq.Length = (unsigned long)(5); + } // send rq via control pipe - return OutCtrlRequest(&rq, vInstruction, rq.Length,Index); + return OutCtrlRequest(&rq, vInstruction, rq.Length, Index); } - -int FlashCommand_SendCommand_SetupPacketForAT45DBBulkWrite(struct CAddressRange *AddrRange, unsigned char modeWrite,unsigned char WriteCom,int Index) +int FlashCommand_SendCommand_SetupPacketForAT45DBBulkWrite(struct CAddressRange* AddrRange, unsigned char modeWrite, unsigned char WriteCom, int Index) { /* modeWrite: - 1: page-size = 256 - 2: page-size = 264 - 3: page-size = 512 - 4: page-size = 528 - 5: page-size = 1024 - 6: page-size = 1056 - */ - size_t pageSize[7] = { 0, 256, 264, 512, 528, 1024, 1056}; - - size_t pageNum = ((AddrRange->end- AddrRange->start) + pageSize[modeWrite] - 1) / pageSize[modeWrite] ; -// printf("modeWrite=%d\r\n",modeWrite); -// printf("pageNum=%d\t \r\n",pageNum); - - unsigned char vInstruction[10] ; - vInstruction[0] = (unsigned char)(pageNum & 0xff) ; // lowest byte of length : page number - vInstruction[1] = (unsigned char)( (pageNum >> 8) & 0xff) ; // highest byte of length: page number - vInstruction[2] = (unsigned char)( (pageNum >> 16) & 0xff) ; // reserved + 1: page-size = 256 + 2: page-size = 264 + 3: page-size = 512 + 4: page-size = 528 + 5: page-size = 1024 + 6: page-size = 1056 + */ + size_t pageSize[7] = { 0, 256, 264, 512, 528, 1024, 1056 }; + + size_t pageNum = ((AddrRange->end - AddrRange->start) + pageSize[modeWrite] - 1) / pageSize[modeWrite]; + // printf("modeWrite = %hhu\n",modeWrite); + // printf("pageNum = %lu\n",pageNum); + + unsigned char vInstruction[10]; + vInstruction[0] = (unsigned char)(pageNum & 0xff); // lowest byte of length : page number + vInstruction[1] = (unsigned char)((pageNum >> 8) & 0xff); // highest byte of length: page number + vInstruction[2] = (unsigned char)((pageNum >> 16) & 0xff); // reserved vInstruction[3] = modeWrite; vInstruction[4] = WriteCom; - CNTRPIPE_RQ rq ; - rq.Function = URB_FUNCTION_VENDOR_ENDPOINT ; - rq.Direction = VENDOR_DIRECTION_OUT ; - rq.Request = ATMEL45_WRITE ; - - if(Is_NewUSBCommand(Index)) - { - vInstruction[5] = 0; - vInstruction[6] = (AddrRange->start & 0xff); - vInstruction[7] = ((AddrRange->start >> 8) & 0xff); - vInstruction[8] = ((AddrRange->start >> 16) & 0xff); - vInstruction[9] = ((AddrRange->start >> 24) & 0xff); - rq.Value = 0; - rq.Index = 0; - rq.Length = (unsigned long)(10) ; - } - else - { - rq.Value = (unsigned short)(AddrRange->start & 0xffff) ; //16 bits LSB - rq.Index = (unsigned short)((AddrRange->start >> 16) & 0xffff) ; //16 bits MSB - rq.Length = (unsigned long)(5) ; - } + CNTRPIPE_RQ rq; + rq.Function = URB_FUNCTION_VENDOR_ENDPOINT; + rq.Direction = VENDOR_DIRECTION_OUT; + rq.Request = ATMEL45_WRITE; + + if (Is_NewUSBCommand(Index)) { + vInstruction[5] = 0; + vInstruction[6] = (AddrRange->start & 0xff); + vInstruction[7] = ((AddrRange->start >> 8) & 0xff); + vInstruction[8] = ((AddrRange->start >> 16) & 0xff); + vInstruction[9] = ((AddrRange->start >> 24) & 0xff); + rq.Value = 0; + rq.Index = 0; + rq.Length = (unsigned long)(10); + } else { + rq.Value = (unsigned short)(AddrRange->start & 0xffff); // 16 bits LSB + rq.Index = (unsigned short)((AddrRange->start >> 16) & 0xffff); // 16 bits MSB + rq.Length = (unsigned long)(5); + } // send rq via control pipe return OutCtrlRequest(&rq, vInstruction, rq.Length, Index); } -int FlashCommand_SendCommand_SetupPacketForBulkRead(struct CAddressRange *AddrRange, unsigned char modeRead,unsigned char ReadCom,int Index) +int FlashCommand_SendCommand_SetupPacketForBulkRead(struct CAddressRange* AddrRange, unsigned char modeRead, unsigned char ReadCom, int Index) { - - unsigned char vInstruction[10] ; - CNTRPIPE_RQ rq ; - rq.Function = URB_FUNCTION_VENDOR_ENDPOINT ; - rq.Direction = VENDOR_DIRECTION_OUT ; - rq.Request = DTC_READ ; + unsigned char vInstruction[10]; + CNTRPIPE_RQ rq; + rq.Function = URB_FUNCTION_VENDOR_ENDPOINT; + rq.Direction = VENDOR_DIRECTION_OUT; + rq.Request = DTC_READ; // length in terms of 256 bytes - size_t pageNum = AddrRange->length >> 9 ; + size_t pageNum = AddrRange->length >> 9; - vInstruction[0] = (unsigned char)(pageNum & 0xff) ; // lowest byte of length : page number - vInstruction[1] = (unsigned char)( (pageNum >> 8) & 0xff) ; // highest byte of length: page number - vInstruction[2] = (unsigned char)( (pageNum >> 16) & 0xff) ; // reserved + vInstruction[0] = (unsigned char)(pageNum & 0xff); // lowest byte of length : page number + vInstruction[1] = (unsigned char)((pageNum >> 8) & 0xff); // highest byte of length: page number + vInstruction[2] = (unsigned char)((pageNum >> 16) & 0xff); // reserved vInstruction[3] = modeRead; // BULK_NORM_READ, BULK_FAST_READ vInstruction[4] = ReadCom; - if(Is_NewUSBCommand(Index)) - { - vInstruction[5] = 0; - vInstruction[6] = (AddrRange->start & 0xff); - vInstruction[7] = ((AddrRange->start >> 8) & 0xff); - vInstruction[8] = ((AddrRange->start >> 16) & 0xff); - vInstruction[9] = ((AddrRange->start >> 24) & 0xff); - rq.Value = 0; - rq.Index = 0; - rq.Length = (unsigned long)(10) ; - } - else - { - rq.Value = (unsigned short)(AddrRange->start & 0xffff) ; //16 bits LSB - rq.Index = (unsigned short)((AddrRange->start >> 16) & 0xffff) ; //16 bits MSB - rq.Length = (unsigned long)(5) ; - } + if (Is_NewUSBCommand(Index)) { + vInstruction[5] = 0; + vInstruction[6] = (AddrRange->start & 0xff); + vInstruction[7] = ((AddrRange->start >> 8) & 0xff); + vInstruction[8] = ((AddrRange->start >> 16) & 0xff); + vInstruction[9] = ((AddrRange->start >> 24) & 0xff); + rq.Value = 0; + rq.Index = 0; + rq.Length = (unsigned long)(10); + } else { + rq.Value = (unsigned short)(AddrRange->start & 0xffff); // 16 bits LSB + rq.Index = (unsigned short)((AddrRange->start >> 16) & 0xffff); // 16 bits MSB + rq.Length = (unsigned long)(5); + } // send rq via control pipe - return OutCtrlRequest(&rq, vInstruction,rq.Length,Index); + return OutCtrlRequest(&rq, vInstruction, rq.Length, Index); } - diff --git a/FlashCommand.h b/FlashCommand.h index ee5fd39..cd0e88e 100755 --- a/FlashCommand.h +++ b/FlashCommand.h @@ -1,13 +1,13 @@ #pragma once #ifndef FLASHCOMMANDS +#define FLASHCOMMANDS -#define FLASHCOMMANDS +#include "Macro.h" #define FlashCommand_TRUE 1 #define FlashCommand_FALSE 0 - int FlashCommand_TransceiveOut(unsigned char *v, int len,int has_result_in,int Index); int FlashCommand_TransceiveIn(unsigned char *v, int len,int Index); diff --git a/IntelHexFile.c b/IntelHexFile.c index b1b84c7..9e501a8 100755 --- a/IntelHexFile.c +++ b/IntelHexFile.c @@ -1,129 +1,126 @@ -#include +#include "IntelHexFile.h" +#include "project.h" #include #include -#include #include -#include "Macro.h" -#include "IntelHexFile.h" +#include extern unsigned char* pBufferforLoadedFile; extern unsigned long g_ulFileSize; /* -* the choice for the total length (16) of a line, but the specification -* can support an another value -*/ -bool BinToHexFile(const char* filePath, unsigned char* vBuffer, unsigned long FileSize) + * the choice for the total length (16) of a line, but the specification + * can support an another value + */ +bool BinToHexFile(const char* filePath, unsigned char* vBuffer, unsigned long FileSize) { -// +anderson_variable 06/10/11 /////////////////////////////////////// -// unsigned char unch = 0x12; -//int j=0; -//int str1_cnt=0; -//int str2_cnt=0; - -//CString str; -//CString str1 = L""; -//CString str2 = L""; -//CString str_tmp = L""; -// -anderson_variable 06/10/11 /////////////////////////////////////// - FILE *FileOut ; /* input files */ - FileOut = fopen(filePath,"wt") ; - if(FileOut==NULL) + // +anderson_variable 06/10/11 /////////////////////////////////////// + // unsigned char unch = 0x12; + // int j=0; + // int str1_cnt=0; + // int str2_cnt=0; + + // CString str; + // CString str1 = L""; + // CString str2 = L""; + // CString str_tmp = L""; + // -anderson_variable 06/10/11 /////////////////////////////////////// + FILE* FileOut; /* input files */ + FileOut = fopen(filePath, "wt"); + if (FileOut == NULL) return false; size_t i; - t_one_line one_record ; + t_one_line one_record; one_record.intel_adr = 0; one_record.intel_type = INTEL_DATA_TYPE; - char buf[256] = {0} ; + char buf[256] = { 0 }; int iULBA = (unsigned int)((FileSize >> 16) & 0xFFFF); unsigned int idx = 0; - do //for(unsigned int idx = 0; idx < iULBA; ++idx) + do // for(unsigned int idx = 0; idx < iULBA; ++idx) { - //Extended Linear Address Record (32-bit format only) - memset(buf, 0, sizeof(buf)) ; - fprintf(FileOut, ":02000004%04X%02X\n", idx, (unsigned char)(0x100 - 0x06 - (idx & 0xFF) - ((idx>>8) & 0xFF))); + // Extended Linear Address Record (32-bit format only) + memset(buf, 0, sizeof(buf)); + fprintf(FileOut, ":02000004%04X%02X\n", idx, (unsigned char)(0x100 - 0x06 - (idx & 0xFF) - ((idx >> 8) & 0xFF))); size_t len = iULBA ? 0x10000 : (FileSize & 0xFFFF); - while(len > 0) - { - one_record.intel_lg_data = ( LL_MAX_LINE > len ) ? len : LL_MAX_LINE ; - memcpy(one_record.intel_data,vBuffer + one_record.intel_adr + (idx<<16),one_record.intel_lg_data); -// copy(vBuffer + one_record.intel_adr + (idx<<16), -// vBuffer + one_record.intel_adr + (idx<<16) + one_record.intel_lg_data, -// one_record.intel_data) ; + while (len > 0) { + one_record.intel_lg_data = (LL_MAX_LINE > len) ? len : LL_MAX_LINE; + memcpy(one_record.intel_data, vBuffer + one_record.intel_adr + (idx << 16), one_record.intel_lg_data); + // copy(vBuffer + one_record.intel_adr + (idx<<16), + // vBuffer + one_record.intel_adr + (idx<<16) + one_record.intel_lg_data, + // one_record.intel_data) ; - len -= one_record.intel_lg_data ; + len -= one_record.intel_lg_data; one_record.intel_lrc = one_record.intel_lg_data; one_record.intel_lrc += ((one_record.intel_adr >> 8) & 0xff); - one_record.intel_lrc += (one_record.intel_adr &0xff); + one_record.intel_lrc += (one_record.intel_adr & 0xff); one_record.intel_lrc += one_record.intel_type; - memset(buf, 0, sizeof(buf)) ; - fprintf(FileOut, ":%02X%04X%02X", one_record.intel_lg_data, one_record.intel_adr, one_record.intel_type) ; + memset(buf, 0, sizeof(buf)); + fprintf(FileOut, ":%02X%04X%02X", one_record.intel_lg_data, one_record.intel_adr, one_record.intel_type); - for(i = 0; i < one_record.intel_lg_data ; ++ i) - { - memset(buf, 0, sizeof(buf)) ; - fprintf(FileOut, "%02X", (one_record.intel_data[i] & 0xff)) ; - one_record.intel_lrc += one_record.intel_data[i] ; - } + for (i = 0; i < one_record.intel_lg_data; ++i) { + memset(buf, 0, sizeof(buf)); + fprintf(FileOut, "%02X", (one_record.intel_data[i] & 0xff)); + one_record.intel_lrc += one_record.intel_data[i]; + } - memset(buf, 0, sizeof(buf)) ; + memset(buf, 0, sizeof(buf)); fprintf(FileOut, "%02X\n", ((0x100 - one_record.intel_lrc) & 0xff)); - one_record.intel_adr += one_record.intel_lg_data ; + one_record.intel_adr += one_record.intel_lg_data; } idx++; - } while( iULBA--); + } while (iULBA--); fprintf(FileOut, ":00000001FF\n"); - fclose (FileOut); + fclose(FileOut); - return true ; + return true; } -bool HexFileToBin(const char* filePath, unsigned char* vOutData,unsigned long* FileSize,unsigned char PaddingByte) +bool HexFileToBin(const char* filePath, unsigned char* vOutData, unsigned long* FileSize, unsigned char PaddingByte) { - FILE *Filin ; /* input files */ + FILE* Filin; /* input files */ /* size in bytes */ - const long MEMORY_SIZE = 16*1024*1024 ; - const long ADDRESS_MASK = 0x00FFFFFF ; - const int MAX_LINE_SIZE = 256 ; - const int NO_ADDRESS_TYPE_SELECTED = 0 ; - const int LINEAR_ADDRESS = 1 ; - const int SEGMENTED_ADDRESS = 2 ; + const long MEMORY_SIZE = 16 * 1024 * 1024; + const long ADDRESS_MASK = 0x00FFFFFF; + const int MAX_LINE_SIZE = 256; + const int NO_ADDRESS_TYPE_SELECTED = 0; + const int LINEAR_ADDRESS = 1; + const int SEGMENTED_ADDRESS = 2; - unsigned int Seg_Lin_Select = NO_ADDRESS_TYPE_SELECTED; + unsigned int Seg_Lin_Select = NO_ADDRESS_TYPE_SELECTED; - const int CKS_8 = 0 ; + const int CKS_8 = 0; /* line inputted from file */ char Line[MAX_LINE_SIZE]; /* flag that a file was read */ - bool Enable_Checksum_Error = false ; + bool Enable_Checksum_Error = false; /* cmd-line parameter # */ - unsigned char *p; + unsigned char* p; unsigned int i; /* Application specific */ - unsigned int Nb_Bytes; - unsigned int First_Word, Address, Segment, Upper_Address; - unsigned int Lowest_Address, Highest_Address, Starting_Address; - unsigned int Phys_Addr, hType; - unsigned int temp; + unsigned int Nb_Bytes; + unsigned int First_Word, Address, Segment, Upper_Address; + unsigned int Lowest_Address, Highest_Address, Starting_Address; + unsigned int Phys_Addr, hType; + unsigned int temp; bool Starting_Address_Setted = false; int temp2; - unsigned char Data_Str[MAX_LINE_SIZE]; - unsigned char Checksum = 0; + unsigned char Data_Str[MAX_LINE_SIZE]; + unsigned char Checksum = 0; unsigned short int wCKS; unsigned short int w; @@ -133,24 +130,24 @@ bool HexFileToBin(const char* filePath, unsigned char* vOutData,unsigned long* F bool Cks_Addr_set = false; /* Just a normal file name */ - Filin = fopen(filePath,"r") ; - if(Filin == 0L) - return false ; + Filin = fopen(filePath, "r"); + if (Filin == 0L) + return false; /* allocate a buffer */ - unsigned char* Memory_Block = (unsigned char*)malloc(MEMORY_SIZE) ; + unsigned char* Memory_Block = (unsigned char*)malloc(MEMORY_SIZE); /* To begin, assume the lowest address is at the end of the memory. - subsequent addresses will lower this number. At the end of the input - file, this value will be the lowest address. */ + subsequent addresses will lower this number. At the end of the input + file, this value will be the lowest address. */ Lowest_Address = MEMORY_SIZE - 1; Highest_Address = 0; - bool boEndofFile=false; + bool boEndofFile = false; /* To begin, assume the lowest address is at the end of the memory. - subsequent addresses will lower this number. At the end of the input - file, this value will be the lowest address. */ + subsequent addresses will lower this number. At the end of the input + file, this value will be the lowest address. */ Segment = 0; Upper_Address = 0; Lowest_Address = MEMORY_SIZE - 1; @@ -160,33 +157,31 @@ bool HexFileToBin(const char* filePath, unsigned char* vOutData,unsigned long* F do /* repeat until EOF(Filin) */ { /* Read a line from input file. */ - if(fgets(Line,MAX_LINE_SIZE,Filin)==NULL) - break; + if (fgets(Line, MAX_LINE_SIZE, Filin) == NULL) + break; /* Remove carriage return/line feed at the end of line. */ - i = strlen(Line)-1; + i = strlen(Line) - 1; - if(':' != Line[0] && boEndofFile ==false) - { - free(Memory_Block); - return false;//0x0a != Line[0] && NULL != Line[0]) return false; - } + if (':' != Line[0] && boEndofFile == false) { + free(Memory_Block); + return false; // 0x0a != Line[0] && NULL != Line[0]) return false; + } if (Line[i] == '\n') Line[i] = '\0'; /* Scan the first two bytes and nb of bytes. - The two bytes are read in First_Word since it's use depend on the - record type: if it's an extended address record or a data record. + The two bytes are read in First_Word since it's use depend on the + record type: if it's an extended address record or a data record. */ - sscanf (Line, ":%2x%4x%2x%s",&Nb_Bytes,&First_Word,&hType,Data_Str); + sscanf(Line, ":%2x%4x%2x%s", &Nb_Bytes, &First_Word, &hType, Data_Str); Checksum = Nb_Bytes + (First_Word >> 8) + (First_Word & 0xFF) + hType; p = Data_Str; /* If we're reading the last record, ignore it. */ - switch (hType) - { + switch (hType) { /* Data record */ case 0: Address = First_Word; @@ -195,54 +190,48 @@ bool HexFileToBin(const char* filePath, unsigned char* vOutData,unsigned long* F Phys_Addr = (Segment << 4) + Address; else /* LINEAR_ADDRESS or NO_ADDRESS_TYPE_SELECTED - Upper_Address = 0 as specified in the Intel spec. until an extended address - record is read. */ + Upper_Address = 0 as specified in the Intel spec. until an extended address + record is read. */ Phys_Addr = ((Upper_Address << 16) & ADDRESS_MASK) + Address; /* Check that the physical address stays in the buffer's range. */ - if ((Phys_Addr + Nb_Bytes) <= MEMORY_SIZE -1) - { + if ((Phys_Addr + Nb_Bytes) <= MEMORY_SIZE - 1) { /* Set the lowest address as base pointer. */ if (Phys_Addr < Lowest_Address) Lowest_Address = Phys_Addr; /* Same for the top address. */ - temp = Phys_Addr + Nb_Bytes -1; + temp = Phys_Addr + Nb_Bytes - 1; if (temp > Highest_Address) Highest_Address = temp; /* Read the Data bytes. */ /* Bytes are written in the Memory block even if checksum is wrong. */ - if(Nb_Bytes==0) - { + if (Nb_Bytes == 0) { free(Memory_Block); - return false; - } - for (i= Nb_Bytes; i > 0; i--) - { - sscanf ((const char *)(p), "%2x",&temp2); + return false; + } + for (i = Nb_Bytes; i > 0; i--) { + sscanf((const char*)(p), "%2x", &temp2); p += 2; Memory_Block[Phys_Addr++] = temp2; Checksum = (Checksum + temp2) & 0xFF; }; /* Read the Checksum value. */ - sscanf ((const char *)(p), "%2x",&temp2); + sscanf((const char*)(p), "%2x", &temp2); /* Verify Checksum value. */ Checksum = (Checksum + temp2) & 0xFF; - if ((Checksum != 0) && Enable_Checksum_Error) - { + if ((Checksum != 0) && Enable_Checksum_Error) { } - } - else - { + } else { if (Seg_Lin_Select == SEGMENTED_ADDRESS) - fprintf(stderr,"Data record skipped at %4X:%4X\n",Segment,Address); + fprintf(stderr, "Data record skipped at %4X:%4X\n", Segment, Address); else - fprintf(stderr,"Data record skipped at %8X\n",Phys_Addr); + fprintf(stderr, "Data record skipped at %8X\n", Phys_Addr); } break; @@ -250,22 +239,21 @@ bool HexFileToBin(const char* filePath, unsigned char* vOutData,unsigned long* F /* End of file record */ case 1: /* Simply ignore checksum errors in this line. */ - boEndofFile=true; + boEndofFile = true; break; /* Extended segment address record */ case 2: /* First_Word contains the offset. It's supposed to be 0000 so - we ignore it. */ + we ignore it. */ /* First extended segment address record ? */ if (Seg_Lin_Select == NO_ADDRESS_TYPE_SELECTED) Seg_Lin_Select = SEGMENTED_ADDRESS; /* Then ignore subsequent extended linear address records */ - if (Seg_Lin_Select == SEGMENTED_ADDRESS) - { - sscanf ((const char *)(p), "%4x%2x",&Segment,&temp2); + if (Seg_Lin_Select == SEGMENTED_ADDRESS) { + sscanf((const char*)(p), "%4x%2x", &Segment, &temp2); /* Update the current address. */ Phys_Addr = (Segment << 4) & ADDRESS_MASK; @@ -273,149 +261,136 @@ bool HexFileToBin(const char* filePath, unsigned char* vOutData,unsigned long* F /* Verify Checksum value. */ Checksum = (Checksum + (Segment >> 8) + (Segment & 0xFF) + temp2) & 0xFF; -// if ((Checksum != 0) && Enable_Checksum_Error) -// Status_Checksum_Error = true; + // if ((Checksum != 0) && Enable_Checksum_Error) + // Status_Checksum_Error = true; } break; /* Start segment address record */ case 3: /* Nothing to be done since it's for specifying the starting address for - execution of the binary code */ + execution of the binary code */ break; /* Extended linear address record */ case 4: /* First_Word contains the offset. It's supposed to be 0000 so - we ignore it. */ + we ignore it. */ /* First extended linear address record ? */ if (Seg_Lin_Select == NO_ADDRESS_TYPE_SELECTED) Seg_Lin_Select = LINEAR_ADDRESS; /* Then ignore subsequent extended segment address records */ - if (Seg_Lin_Select == LINEAR_ADDRESS) - { - sscanf ((const char *)(p), "%4x%2x",&Upper_Address,&temp2); + if (Seg_Lin_Select == LINEAR_ADDRESS) { + sscanf((const char*)(p), "%4x%2x", &Upper_Address, &temp2); /* Update the current address. */ Phys_Addr = Upper_Address << 4; /* Verify Checksum value. */ - Checksum = (Checksum + (Upper_Address >> 8) + (Upper_Address & 0xFF) + temp2) - & 0xFF; + Checksum = (Checksum + (Upper_Address >> 8) + (Upper_Address & 0xFF) + temp2) & 0xFF; -// if ((Checksum != 0) && Enable_Checksum_Error) -// Status_Checksum_Error = true; + // if ((Checksum != 0) && Enable_Checksum_Error) + // Status_Checksum_Error = true; } break; /* Start linear address record */ case 5: /* Nothing to be done since it's for specifying the starting address for - execution of the binary code */ + execution of the binary code */ break; default: break; // 20040617+ Added to remove GNU compiler warning about label at end of compound statement } - } while (!feof (Filin)); + } while (!feof(Filin)); /*-----------------------------------------------------------------------------*/ -// fprintf(stdout,"Lowest address = %08X\n",Lowest_Address); -// fprintf(stdout,"Highest address = %08X\n",Highest_Address); -// fprintf(stdout,"Pad Byte = %X\n", PadByte); + // fprintf(stdout,"Lowest address = %08X\n",Lowest_Address); + // fprintf(stdout,"Highest address = %08X\n",Highest_Address); + // fprintf(stdout,"Pad Byte = %X\n", PadByte); wCKS = 0; - if( !Cks_range_set ) - { + if (!Cks_range_set) { Cks_Start = Lowest_Address; Cks_End = Highest_Address; } - switch (Cks_Type) - { + switch (Cks_Type) { case 0: - for (i=Cks_Start; i<=Cks_End; i++) - { + for (i = Cks_Start; i <= Cks_End; i++) { wCKS += Memory_Block[i]; } - //fprintf(stdout,"8-bit Checksum = %02X\n",wCKS & 0xff); - if( Cks_Addr_set ) - { + // fprintf(stdout,"8-bit Checksum = %02X\n",wCKS & 0xff); + if (Cks_Addr_set) { wCKS = Cks_Value - (wCKS - Memory_Block[Cks_Addr]); Memory_Block[Cks_Addr] = (unsigned char)(wCKS & 0xff); - //fprintf(stdout,"Addr %08X set to %02X\n",Cks_Addr, wCKS & 0xff); + // fprintf(stdout,"Addr %08X set to %02X\n",Cks_Addr, wCKS & 0xff); } break; case 2: - for (i=Cks_Start; i<=Cks_End; i+=2) - { - w = Memory_Block[i+1] | ((unsigned short)Memory_Block[i] << 8); + for (i = Cks_Start; i <= Cks_End; i += 2) { + w = Memory_Block[i + 1] | ((unsigned short)Memory_Block[i] << 8); wCKS += w; } - //fprintf(stdout,"16-bit Checksum = %04X\n",wCKS); - if( Cks_Addr_set ) - { - w = Memory_Block[Cks_Addr+1] | ((unsigned short)Memory_Block[Cks_Addr] << 8); + // fprintf(stdout,"16-bit Checksum = %04X\n",wCKS); + if (Cks_Addr_set) { + w = Memory_Block[Cks_Addr + 1] | ((unsigned short)Memory_Block[Cks_Addr] << 8); wCKS = Cks_Value - (wCKS - w); Memory_Block[Cks_Addr] = (unsigned char)(wCKS >> 8); - Memory_Block[Cks_Addr+1] = (unsigned char)(wCKS & 0xff); - //fprintf(stdout,"Addr %08X set to %04X\n",Cks_Addr, wCKS); + Memory_Block[Cks_Addr + 1] = (unsigned char)(wCKS & 0xff); + // fprintf(stdout,"Addr %08X set to %04X\n",Cks_Addr, wCKS); } break; case 1: - for (i=Cks_Start; i<=Cks_End; i+=2) - { - w = Memory_Block[i] | ((unsigned short)Memory_Block[i+1] << 8); + for (i = Cks_Start; i <= Cks_End; i += 2) { + w = Memory_Block[i] | ((unsigned short)Memory_Block[i + 1] << 8); wCKS += w; } - //fprintf(stdout,"16-bit Checksum = %04X\n",wCKS); - if( Cks_Addr_set ) - { - w = Memory_Block[Cks_Addr] | ((unsigned short)Memory_Block[Cks_Addr+1] << 8); + // fprintf(stdout,"16-bit Checksum = %04X\n",wCKS); + if (Cks_Addr_set) { + w = Memory_Block[Cks_Addr] | ((unsigned short)Memory_Block[Cks_Addr + 1] << 8); wCKS = Cks_Value - (wCKS - w); - Memory_Block[Cks_Addr+1] = (unsigned char)(wCKS >> 8); + Memory_Block[Cks_Addr + 1] = (unsigned char)(wCKS >> 8); Memory_Block[Cks_Addr] = (unsigned char)(wCKS & 0xff); - //fprintf(stdout,"Addr %08X set to %04X\n",Cks_Addr, wCKS); + // fprintf(stdout,"Addr %08X set to %04X\n",Cks_Addr, wCKS); } - default: - ; + default:; } /* This starting address is for the binary file, - ex.: if the first record is :nn010000ddddd... - the data supposed to be stored at 0100 will start at 0000 in the binary file. + ex.: if the first record is :nn010000ddddd... + the data supposed to be stored at 0100 will start at 0000 in the binary file. - Specifying this starting address will put FF bytes in the binary file so that - the data supposed to be stored at 0100 will start at the same address in the - binary file. - */ + Specifying this starting address will put FF bytes in the binary file so that + the data supposed to be stored at 0100 will start at the same address in the + binary file. + */ - if(Starting_Address_Setted) - { + if (Starting_Address_Setted) { Lowest_Address = Starting_Address; } - if(pBufferforLoadedFile != NULL) + if (pBufferforLoadedFile != NULL) free(pBufferforLoadedFile); -// pBufferforLoadedFile=(unsigned char*)malloc(Highest_Address -Lowest_Address+ 1); - pBufferforLoadedFile=(unsigned char*)malloc(Highest_Address+ 1); - - if( Lowest_Address < Highest_Address) - { -// memcpy(pBufferforLoadedFile,Memory_Block + Lowest_Address, Highest_Address -Lowest_Address+ 1 ) ; -// g_ulFileSize=(Highest_Address -Lowest_Address+ 1); ; - memcpy(pBufferforLoadedFile,Memory_Block, Highest_Address+ 1 ) ; - g_ulFileSize=(Highest_Address+ 1); ; + // pBufferforLoadedFile=(unsigned char*)malloc(Highest_Address - Lowest_Address + 1); + pBufferforLoadedFile = (unsigned char*)malloc(Highest_Address + 1); + + if (Lowest_Address < Highest_Address) { + // memcpy(pBufferforLoadedFile,Memory_Block + Lowest_Address, Highest_Address - Lowest_Address + 1 ) ; + // g_ulFileSize=(Highest_Address -Lowest_Address+ 1); ; + memcpy(pBufferforLoadedFile, Memory_Block, Highest_Address + 1); + g_ulFileSize = (Highest_Address + 1); } free(Memory_Block); diff --git a/IntelHexFile.h b/IntelHexFile.h index d8c4210..9ab16eb 100755 --- a/IntelHexFile.h +++ b/IntelHexFile.h @@ -123,9 +123,10 @@ of file record, so that the LL field is 0 #ifndef INTELHEXFILE_H #define INTELHEXFILE_H +#include + #define LL_MAX_LINE 16 -typedef struct -{ +typedef struct { unsigned char intel_lg_data; unsigned short intel_adr; unsigned char intel_type; diff --git a/Macro.h b/Macro.h index 22a931c..d7302e2 100755 --- a/Macro.h +++ b/Macro.h @@ -2,9 +2,12 @@ #ifndef _MACRO_H #define _MACRO_H + +#include #include #include #include + // new defined macros //programmer info RQ #define PROGINFO_REQUEST 0x08 @@ -27,8 +30,7 @@ //#define READ_EEPROM 0x05 //#define WRITE_EEPROM 0x06 // values of Request Field of a setup packet -typedef struct FirmwareInfo -{ +typedef struct FirmwareInfo { char Programmer[20]; char Version[10]; char FPGAVersion[10]; @@ -40,8 +42,7 @@ typedef struct FirmwareInfo unsigned int SecondSize; }FW_INFO; -typedef enum -{ +typedef enum { TRANSCEIVE = 0x01, DTC_READ = 0x20, @@ -87,6 +88,7 @@ typedef struct ChipInfo { size_t Timeout; size_t VoltageInMv; }CHIP_INFO; + //third field of RQ #define CTRL_TIMEOUT 3000 ///< time out for control pipe or for firmwire @@ -265,7 +267,6 @@ typedef struct ChipInfo { #define SUPPORT_FUDAN_FM25Fxx "FM25Fxx" #endif - // for usb #define USB_TIMEOUT 800000 ///< time out value for usb EP2 @@ -304,16 +305,13 @@ typedef struct ChipInfo { #define LIST_TYPE BIT12 #define LOADFILE BIT13 -//#define size_t unsigned int -struct CAddressRange -{ +struct CAddressRange { size_t start; size_t end; size_t length; }; -struct memory_id -{ +struct memory_id { char TypeName[20]; size_t UniqueID; char Class[20]; @@ -346,23 +344,19 @@ struct memory_id size_t Timeout; }; - -enum -{ +enum { SITE_NORMAL=0, SITE_BUSY, SITE_ERROR, SITE_OK, }; -enum -{ +enum { Seriase_45 = 0x08, Seriase_25 = 0x00, }; -typedef enum -{ +typedef enum { vccPOWEROFF = 0x00, vcc3_5V = 0x10, @@ -373,49 +367,4 @@ typedef enum } VCC_VALUE; - static unsigned int crc32_tab[] = { - 0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f, - 0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988, - 0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91, 0x1db71064, 0x6ab020f2, - 0xf3b97148, 0x84be41de, 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7, - 0x136c9856, 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec, 0x14015c4f, 0x63066cd9, - 0xfa0f3d63, 0x8d080df5, 0x3b6e20c8, 0x4c69105e, 0xd56041e4, 0xa2677172, - 0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b, 0x35b5a8fa, 0x42b2986c, - 0xdbbbc9d6, 0xacbcf940, 0x32d86ce3, 0x45df5c75, 0xdcd60dcf, 0xabd13d59, - 0x26d930ac, 0x51de003a, 0xc8d75180, 0xbfd06116, 0x21b4f4b5, 0x56b3c423, - 0xcfba9599, 0xb8bda50f, 0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924, - 0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d, 0x76dc4190, 0x01db7106, - 0x98d220bc, 0xefd5102a, 0x71b18589, 0x06b6b51f, 0x9fbfe4a5, 0xe8b8d433, - 0x7807c9a2, 0x0f00f934, 0x9609a88e, 0xe10e9818, 0x7f6a0dbb, 0x086d3d2d, - 0x91646c97, 0xe6635c01, 0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e, - 0x6c0695ed, 0x1b01a57b, 0x8208f4c1, 0xf50fc457, 0x65b0d9c6, 0x12b7e950, - 0x8bbeb8ea, 0xfcb9887c, 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3, 0xfbd44c65, - 0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2, 0x4adfa541, 0x3dd895d7, - 0xa4d1c46d, 0xd3d6f4fb, 0x4369e96a, 0x346ed9fc, 0xad678846, 0xda60b8d0, - 0x44042d73, 0x33031de5, 0xaa0a4c5f, 0xdd0d7cc9, 0x5005713c, 0x270241aa, - 0xbe0b1010, 0xc90c2086, 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f, - 0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4, 0x59b33d17, 0x2eb40d81, - 0xb7bd5c3b, 0xc0ba6cad, 0xedb88320, 0x9abfb3b6, 0x03b6e20c, 0x74b1d29a, - 0xead54739, 0x9dd277af, 0x04db2615, 0x73dc1683, 0xe3630b12, 0x94643b84, - 0x0d6d6a3e, 0x7a6a5aa8, 0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1, - 0xf00f9344, 0x8708a3d2, 0x1e01f268, 0x6906c2fe, 0xf762575d, 0x806567cb, - 0x196c3671, 0x6e6b06e7, 0xfed41b76, 0x89d32be0, 0x10da7a5a, 0x67dd4acc, - 0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5, 0xd6d6a3e8, 0xa1d1937e, - 0x38d8c2c4, 0x4fdff252, 0xd1bb67f1, 0xa6bc5767, 0x3fb506dd, 0x48b2364b, - 0xd80d2bda, 0xaf0a1b4c, 0x36034af6, 0x41047a60, 0xdf60efc3, 0xa867df55, - 0x316e8eef, 0x4669be79, 0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236, - 0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f, 0xc5ba3bbe, 0xb2bd0b28, - 0x2bb45a92, 0x5cb36a04, 0xc2d7ffa7, 0xb5d0cf31, 0x2cd99e8b, 0x5bdeae1d, - 0x9b64c2b0, 0xec63f226, 0x756aa39c, 0x026d930a, 0x9c0906a9, 0xeb0e363f, - 0x72076785, 0x05005713, 0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38, - 0x92d28e9b, 0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21, 0x86d3d2d4, 0xf1d4e242, - 0x68ddb3f8, 0x1fda836e, 0x81be16cd, 0xf6b9265b, 0x6fb077e1, 0x18b74777, - 0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c, 0x8f659eff, 0xf862ae69, - 0x616bffd3, 0x166ccf45, 0xa00ae278, 0xd70dd2ee, 0x4e048354, 0x3903b3c2, - 0xa7672661, 0xd06016f7, 0x4969474d, 0x3e6e77db, 0xaed16a4a, 0xd9d65adc, - 0x40df0b66, 0x37d83bf0, 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9, - 0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605, 0xcdd70693, - 0x54de5729, 0x23d967bf, 0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94, - 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d - }; #endif //_MACRO_H diff --git a/Makefile b/Makefile index 872d0f3..3bc87eb 100755 --- a/Makefile +++ b/Makefile @@ -5,9 +5,10 @@ PROGRAM = dpcmd CC = gcc -CFLAGS = -Os -Wall -lpthread -std=gnu99 +CFLAGS = -O2 -Wall -lpthread -std=gnu99 +PREFIX ?= /usr/local -UNAME_OS := $(shell lsb_release -si) +UNAME_OS := $(shell which lsb_release 2>/dev/null && lsb_release -si) ifneq ($(UNAME_OS),Ubuntu) CFLAGS+=-D_NON_UBUNTU @@ -16,11 +17,21 @@ endif FEATURE_LIBS += -lusb -lpthread PROGRAMMER_OBJS += dpcmd.o usbdriver.o FlashCommand.o SerialFlash.o parse.o board.o project.o IntelHexFile.o MotorolaFile.o - -$(PROGRAM): $(PROGRAMMER_OBJS) +$(PROGRAM): $(PROGRAMMER_OBJS) *.h Makefile $(CC) $(CFLAGS) -o $(PROGRAM) $(PROGRAMMER_OBJS) $(FEATURE_LIBS) - clean: - rm -f $(PROGRAM) $(PROGRAM).exe *.o *.d *.c~ *.h~ *~ - + @rm -vf $(PROGRAM) $(PROGRAM).exe *.o *.d + +install: $(PROGRAM) + @[ $(shell id -u) -eq 0 ] || (echo "Error: install needs root privileges" && false) + @mkdir -vp $(PREFIX)/bin $(PREFIX)/share/DediProg + @echo -n "install: " && install -v -o 0 -g 0 -m 0755 $(PROGRAM) $(PREFIX)/bin/$(PROGRAM) + strip $(PREFIX)/bin/$(PROGRAM) + @echo -n "install: " && install -v -o 0 -g 0 -m 0644 ChipInfoDb.dedicfg $(PREFIX)/share/DediProg/ChipInfoDb.dedicfg + @echo -n "install: " && install -v -o 0 -g 0 -m 0644 60-dediprog.rules /etc/udev/rules.d/60-dediprog.rules + +uninstall: + @[ $(shell id -u) -eq 0 ] || (echo "Error: uninstall needs root privileges" && false) + @rm -vf $(PREFIX)/bin/$(PROGRAM) $(PREFIX)/share/DediProg/ChipInfoDb.dedicfg /etc/udev/rules.d/60-dediprog.rules + @[ -d "$(PREFIX)/share/DediProg" ] && rmdir -v $(PREFIX)/share/DediProg || true diff --git a/MotorolaFile.c b/MotorolaFile.c index 9ddcc61..fa934f5 100755 --- a/MotorolaFile.c +++ b/MotorolaFile.c @@ -1,123 +1,120 @@ - //-------------------------------------------------------------------------------- - //NAME - //srec - S-record file and record format - //DESCRIPTION +//-------------------------------------------------------------------------------- +//NAME +//srec - S-record file and record format +//DESCRIPTION - //An S-record file consists of a sequence of specially formatted ASCII character strings. An S-record will be less than or equal to 78 bytes in length. - //The order of S-records within a file is of no significance and no particular order may be assumed. +//An S-record file consists of a sequence of specially formatted ASCII character strings. An S-record will be less than or equal to 78 bytes in length. +//The order of S-records within a file is of no significance and no particular order may be assumed. - //The general format of an S-record follows: +//The general format of an S-record follows: - //+-------------------//------------------//-----------------------+ - //| type | count | address | data | checksum | - //+-------------------//------------------//-----------------------+ +//+-------------------//------------------//-----------------------+ +//| type | count | address | data | checksum | +//+-------------------//------------------//-----------------------+ - //type -- A char[2] field. These characters describe the type of record (S0, S1, S2, S3, S5, S7, S8, or S9). - //count -- A char[2] field. These characters when paired and interpreted as a hexadecimal value, display the count of remaining character pairs in the record. +//type -- A char[2] field. These characters describe the type of record (S0, S1, S2, S3, S5, S7, S8, or S9). +//count -- A char[2] field. These characters when paired and interpreted as a hexadecimal value, display the count of remaining character pairs in the record. - //address -- A char[4,6, or 8] field. These characters grouped and interpreted as a hexadecimal value, display the address at which the data field is to be loaded into memory. The length of the field depends on the number of bytes necessary to hold the address. A 2-byte address uses 4 characters, a 3-byte address uses 6 characters, and a 4-byte address uses 8 characters. +//address -- A char[4,6, or 8] field. These characters grouped and interpreted as a hexadecimal value, display the address at which the data field is to be loaded into memory. The length of the field depends on the number of bytes necessary to hold the address. A 2-byte address uses 4 characters, a 3-byte address uses 6 characters, and a 4-byte address uses 8 characters. - //data -- A char [0-64] field. These characters when paired and interpreted as hexadecimal values represent the memory loadable data or descriptive information. +//data -- A char [0-64] field. These characters when paired and interpreted as hexadecimal values represent the memory loadable data or descriptive information. - //checksum -- A char[2] field. These characters when paired and interpreted as a hexadecimal value display the least significant byte of the ones complement of the sum of the byte values represented by the pairs of characters making up the count, the address, and the data fields. +//checksum -- A char[2] field. These characters when paired and interpreted as a hexadecimal value display the least significant byte of the ones complement of the sum of the byte values represented by the pairs of characters making up the count, the address, and the data fields. - //Each record is terminated with a line feed. If any additional or different record terminator(s) or delay characters are needed during transmission to the target system it is the responsibility of the transmitting program to provide them. +//Each record is terminated with a line feed. If any additional or different record terminator(s) or delay characters are needed during transmission to the target system it is the responsibility of the transmitting program to provide them. - //S0 Record. The type of record is 'S0' (0x5330). The address field is unused and will be filled with zeros (0x0000). The header information within the data field is divided into the following subfields. +//S0 Record. The type of record is 'S0' (0x5330). The address field is unused and will be filled with zeros (0x0000). The header information within the data field is divided into the following subfields. +//mname is char[20] and is the module name. +//ver is char[2] and is the version number. +//rev is char[2] and is the revision number. +//description is char[0-36] and is a text comment. - //mname is char[20] and is the module name. - //ver is char[2] and is the version number. - //rev is char[2] and is the revision number. - //description is char[0-36] and is a text comment. +//Each of the subfields is composed of ASCII bytes whose associated characters, when paired, represent one byte hexadecimal values in the case of the version and revision numbers, or represent the hexadecimal values of the ASCII characters comprising the module name and description. +//S1 Record. The type of record field is 'S1' (0x5331). The address field is intrepreted as a 2-byte address. The data field is composed of memory loadable data. - //Each of the subfields is composed of ASCII bytes whose associated characters, when paired, represent one byte hexadecimal values in the case of the version and revision numbers, or represent the hexadecimal values of the ASCII characters comprising the module name and description. - //S1 Record. The type of record field is 'S1' (0x5331). The address field is intrepreted as a 2-byte address. The data field is composed of memory loadable data. +//S2 Record. The type of record field is 'S2' (0x5332). The address field is intrepreted as a 3-byte address. The data field is composed of memory loadable data. - //S2 Record. The type of record field is 'S2' (0x5332). The address field is intrepreted as a 3-byte address. The data field is composed of memory loadable data. +//S3 Record. The type of record field is 'S3' (0x5333). The address field is intrepreted as a 4-byte address. The data field is composed of memory loadable data. - //S3 Record. The type of record field is 'S3' (0x5333). The address field is intrepreted as a 4-byte address. The data field is composed of memory loadable data. +//S5 Record. The type of record field is 'S5' (0x5335). The address field is intrepreted as a 2-byte value and contains the count of S1, S2, and S3 records previously transmitted. There is no data field. - //S5 Record. The type of record field is 'S5' (0x5335). The address field is intrepreted as a 2-byte value and contains the count of S1, S2, and S3 records previously transmitted. There is no data field. +//S7 Record. The type of record field is 'S7' (0x5337). The address field contains the starting execution address and is intrepreted as 4-byte address. There is no data field. - //S7 Record. The type of record field is 'S7' (0x5337). The address field contains the starting execution address and is intrepreted as 4-byte address. There is no data field. +//S8 Record. The type of record field is 'S8' (0x5338). The address field contains the starting execution address and is intrepreted as 3-byte address. There is no data field. - //S8 Record. The type of record field is 'S8' (0x5338). The address field contains the starting execution address and is intrepreted as 3-byte address. There is no data field. +//S9 Record. The type of record field is 'S9' (0x5339). The address field contains the starting execution address and is intrepreted as 2-byte address. There is no data field. - //S9 Record. The type of record field is 'S9' (0x5339). The address field contains the starting execution address and is intrepreted as 2-byte address. There is no data field. +//EXAMPLE - //EXAMPLE +//Shown below is a typical S-record format file. - //Shown below is a typical S-record format file. +//S00600004844521B +//S1130000285F245F2212226A000424290008237C2A +//S11300100002000800082629001853812341001813 +//S113002041E900084E42234300182342000824A952 +//S107003000144ED492 +//S5030004F8 +//S9030000FC +//The file consists of one S0 record, four S1 records, one S5 record and an S9 record. - //S00600004844521B - //S1130000285F245F2212226A000424290008237C2A - //S11300100002000800082629001853812341001813 - //S113002041E900084E42234300182342000824A952 - //S107003000144ED492 - //S5030004F8 - //S9030000FC - //The file consists of one S0 record, four S1 records, one S5 record and an S9 record. +//The S0 record is comprised as follows: - //The S0 record is comprised as follows: +//S0 S-record type S0, indicating it is a header record. +//06 Hexadecimal 06 (decimal 6), indicating that six character pairs (or ASCII bytes) follow. +//00 00 Four character 2-byte address field, zeroes in this example. - //S0 S-record type S0, indicating it is a header record. - //06 Hexadecimal 06 (decimal 6), indicating that six character pairs (or ASCII bytes) follow. - //00 00 Four character 2-byte address field, zeroes in this example. +//48 44 52 ASCII H, D, and R - "HDR". +//1B The checksum. +//The first S1 record is comprised as follows: +//S1 S-record type S1, indicating it is a data record to be loaded at a 2-byte address. +//13 Hexadecimal 13 (decimal 19), indicating that nineteen character pairs, representing a 2 byte address, 16 bytes of binary data, and a 1 byte checksum, follow. +//00 00 Four character 2-byte address field; hexidecimal address 0x0000, where the data which follows is to be loaded. +//28 5F 24 5F 22 12 22 6A 00 04 24 29 00 08 23 7C Sixteen character pairs representing the actual binary data. +//2A The checksum. +//The second and third S1 records each contain 0x13 (19) character pairs and are ended with checksums of 13 and 52, respectively. The fourth S1 record contains 07 character pairs and has a checksum of 92. - //48 44 52 ASCII H, D, and R - "HDR". - //1B The checksum. - //The first S1 record is comprised as follows: - //S1 S-record type S1, indicating it is a data record to be loaded at a 2-byte address. - //13 Hexadecimal 13 (decimal 19), indicating that nineteen character pairs, representing a 2 byte address, 16 bytes of binary data, and a 1 byte checksum, follow. - //00 00 Four character 2-byte address field; hexidecimal address 0x0000, where the data which follows is to be loaded. - //28 5F 24 5F 22 12 22 6A 00 04 24 29 00 08 23 7C Sixteen character pairs representing the actual binary data. - //2A The checksum. - //The second and third S1 records each contain 0x13 (19) character pairs and are ended with checksums of 13 and 52, respectively. The fourth S1 record contains 07 character pairs and has a checksum of 92. +//The S5 record is comprised as follows: - //The S5 record is comprised as follows: +//S5 S-record type S5, indicating it is a count record indicating the number of S1 records +//03 Hexadecimal 03 (decimal 3), indicating that three character pairs follow. +//00 04 Hexadecimal 0004 (decimal 4), indicating that there are four data records previous to this record. +//F8 The checksum. +//The S9 record is comprised as follows: - //S5 S-record type S5, indicating it is a count record indicating the number of S1 records - //03 Hexadecimal 03 (decimal 3), indicating that three character pairs follow. - //00 04 Hexadecimal 0004 (decimal 4), indicating that there are four data records previous to this record. - //F8 The checksum. - //The S9 record is comprised as follows: +//S9 S-record type S9, indicating it is a termination record. +//03 Hexadecimal 03 (decimal 3), indicating that three character pairs follow. +//00 00 The address field, hexadecimal 0 (decimal 0) indicating the starting execution address. +//FC The checksum. - //S9 S-record type S9, indicating it is a termination record. - //03 Hexadecimal 03 (decimal 3), indicating that three character pairs follow. - //00 00 The address field, hexadecimal 0 (decimal 0) indicating the starting execution address. - //FC The checksum. +//-------------------------------------------------------------------------------- - - //-------------------------------------------------------------------------------- - - //Instructor Notes - //There isn't any evidence that Motorola ever has made use of the header information within the data field of the S0 record, as described above. This must have been used by some third party vendors. - //This is the only place that a 78-byte limit on total record length or 64-byte limit on data length is documented. These values shouldn't be trusted for the general case. - //The count field can have values in the range of 0x3 (2 bytes of address + 1 byte checksum = 3, a not very useful record) to 0xff; this is the count of remaining character pairs, including checksum. - //If you write code to convert S-Records, you should always assume that a record can be as long as 514 (decimal) characters in length (255 * 2 = 510, plus 4 characters for the type and count fields), - //plus any terminating character(s). That is, in establishing an input buffer in C, you would declare it to be an array of 515 chars, thus leaving room for the terminating null character. -#include +//Instructor Notes +//There isn't any evidence that Motorola ever has made use of the header information within the data field of the S0 record, as described above. This must have been used by some third party vendors. +//This is the only place that a 78-byte limit on total record length or 64-byte limit on data length is documented. These values shouldn't be trusted for the general case. +//The count field can have values in the range of 0x3 (2 bytes of address + 1 byte checksum = 3, a not very useful record) to 0xff; this is the count of remaining character pairs, including checksum. +//If you write code to convert S-Records, you should always assume that a record can be as long as 514 (decimal) characters in length (255 * 2 = 510, plus 4 characters for the type and count fields), +//plus any terminating character(s). That is, in establishing an input buffer in C, you would declare it to be an array of 515 chars, thus leaving room for the terminating null character. +#include "MotorolaFile.h" +#include "project.h" #include #include -#include #include -#include "Macro.h" -#include "MotorolaFile.h" +#include extern unsigned char* pBufferforLoadedFile; extern unsigned long g_ulFileSize; -bool S19FileToBin(const char* filePath, unsigned char* vData,unsigned long* FileSize, unsigned char PaddingByte) +bool S19FileToBin(const char* filePath, unsigned char* vData, unsigned long* FileSize, unsigned char PaddingByte) { - FILE *Filin ; /* input files */ + FILE* Filin; /* input files */ /* size in bytes */ - const long MEMORY_SIZE = 1024*1024 * 4; - const long ADDRESS_MASK = 0x003FFFFF ; - const int MAX_LINE_SIZE = 512 ; + const long MEMORY_SIZE = 1024 * 1024 * 4; + const long ADDRESS_MASK = 0x003FFFFF; + const int MAX_LINE_SIZE = 512; - const int CKS_8 = 0 ; + const int CKS_8 = 0; int PadByte = PaddingByte; @@ -125,25 +122,25 @@ bool S19FileToBin(const char* filePath, unsigned char* vData,unsigned long* File char Line[MAX_LINE_SIZE]; /* flag that a file was read */ - bool Enable_Checksum_Error = false ; + bool Enable_Checksum_Error = false; /* cmd-line parameter # */ - unsigned char *p; + unsigned char* p; unsigned int i; /* Application specific */ - unsigned int Nb_Bytes; - unsigned int Address, Lowest_Address, Highest_Address, Starting_Address; - unsigned int Phys_Addr, sType; - unsigned int temp; + unsigned int Nb_Bytes; + unsigned int Address, Lowest_Address, Highest_Address, Starting_Address; + unsigned int Phys_Addr, sType; + unsigned int temp; bool Starting_Address_Setted = false; int temp2; - unsigned char Data_Str[MAX_LINE_SIZE]; - unsigned char Checksum = 0; + unsigned char Data_Str[MAX_LINE_SIZE]; + unsigned char Checksum = 0; unsigned short int wCKS; unsigned short int w; @@ -153,13 +150,13 @@ bool S19FileToBin(const char* filePath, unsigned char* vData,unsigned long* File bool Cks_Addr_set = false; /* Just a normal file name */ - Filin = fopen(filePath,"rt") ; - if(Filin == 0L) - return false ; + Filin = fopen(filePath, "rt"); + if (Filin == 0L) + return false; /* allocate a buffer */ - unsigned char* Memory_Block=(unsigned char*)malloc(MEMORY_SIZE) ; - memset(Memory_Block,PadByte,MEMORY_SIZE); + unsigned char* Memory_Block = (unsigned char*)malloc(MEMORY_SIZE); + memset(Memory_Block, PadByte, MEMORY_SIZE); /* To begin, assume the lowest address is at the end of the memory. subsequent addresses will lower this number. At the end of the input @@ -167,31 +164,30 @@ bool S19FileToBin(const char* filePath, unsigned char* vData,unsigned long* File Lowest_Address = MEMORY_SIZE - 1; Highest_Address = 0; -// int readCnt = 0 ; -// int checkCnt = 0 ; + // int readCnt = 0 ; + // int checkCnt = 0 ; /* Now read the file & process the lines. */ do /* repeat until EOF(Filin) */ { /* Read a line from input file. */ - if(fgets(Line,MAX_LINE_SIZE,Filin)==NULL) - break; + if (fgets(Line, MAX_LINE_SIZE, Filin) == NULL) + break; /* Remove carriage return/line feed at the end of line. */ - i = strlen(Line)-1; + i = strlen(Line) - 1; if (Line[i] == '\n') Line[i] = '\0'; // check if this file is an S-file - if('S' != Line[0] && Line[0] != '\0') return false; + if ('S' != Line[0] && Line[0] != '\0') return false; /* Scan starting address and nb of bytes. */ /* Look at the record sType after the 'S' */ - switch(Line[1]) - { + switch (Line[1]) { /* 16 bits address */ case '1': - sscanf (Line,"S%1x%2x%4x%s",&sType,&Nb_Bytes,&Address,Data_Str); + sscanf(Line, "S%1x%2x%4x%s", &sType, &Nb_Bytes, &Address, Data_Str); Checksum = Nb_Bytes + (Address >> 8) + (Address & 0xFF); /* Adjust Nb_Bytes for the number of data bytes */ @@ -200,7 +196,7 @@ bool S19FileToBin(const char* filePath, unsigned char* vData,unsigned long* File /* 24 bits address */ case '2': - sscanf (Line,"S%1x%2x%6x%s",&sType,&Nb_Bytes,&Address,Data_Str); + sscanf(Line, "S%1x%2x%6x%s", &sType, &Nb_Bytes, &Address, Data_Str); Checksum = Nb_Bytes + (Address >> 16) + (Address >> 8) + (Address & 0xFF); /* Adjust Nb_Bytes for the number of data bytes */ @@ -209,7 +205,7 @@ bool S19FileToBin(const char* filePath, unsigned char* vData,unsigned long* File /* 32 bits address */ case '3': - sscanf (Line,"S%1x%2x%8x%s",&sType,&Nb_Bytes,&Address,Data_Str); + sscanf(Line, "S%1x%2x%8x%s", &sType, &Nb_Bytes, &Address, Data_Str); Checksum = Nb_Bytes + (Address >> 24) + (Address >> 16) + (Address >> 8) + (Address & 0xFF); /* Adjust Nb_Bytes for the number of data bytes */ @@ -223,8 +219,7 @@ bool S19FileToBin(const char* filePath, unsigned char* vData,unsigned long* File p = Data_Str; /* If we're reading the last record, ignore it. */ - switch (sType) - { + switch (sType) { /* Data record */ case 1: case 2: @@ -232,39 +227,34 @@ bool S19FileToBin(const char* filePath, unsigned char* vData,unsigned long* File Phys_Addr = Address & ADDRESS_MASK; /* Check that the physical address stays in the buffer's range. */ - if ((Phys_Addr + Nb_Bytes) <= MEMORY_SIZE -1) - { + if ((Phys_Addr + Nb_Bytes) <= MEMORY_SIZE - 1) { /* Set the lowest address as base pointer. */ if (Phys_Addr < Lowest_Address) Lowest_Address = Phys_Addr; /* Same for the top address. */ - temp = Phys_Addr + Nb_Bytes -1; + temp = Phys_Addr + Nb_Bytes - 1; if (temp > Highest_Address) Highest_Address = temp; /* Read the Data bytes. */ - for (i= Nb_Bytes; i > 0; i--) - { - sscanf ((const char *)(p), "%2x",&temp2); + for (i = Nb_Bytes; i > 0; i--) { + sscanf((const char*)(p), "%2x", &temp2); p += 2; Memory_Block[Phys_Addr++] = temp2; Checksum = (Checksum + temp2) & 0xFF; }; /* Read the Checksum value. */ - sscanf ((const char *)(p), "%2x",&temp2); + sscanf((const char*)(p), "%2x", &temp2); /* Verify Checksum value. */ Checksum = (0xFF - Checksum) & 0xFF; - if ((temp2 != Checksum) && Enable_Checksum_Error) - { + if ((temp2 != Checksum) && Enable_Checksum_Error) { } - } - else - { + } else { //fprintf(stderr,"Data record skipped at %8X\n",Phys_Addr); } break; @@ -273,32 +263,28 @@ bool S19FileToBin(const char* filePath, unsigned char* vData,unsigned long* File default: break; // 20040617+ Added to remove GNU compiler warning about label at end of compound statement } - } while (!feof (Filin)); - fclose (Filin); + } while (!feof(Filin)); + fclose(Filin); /*-----------------------------------------------------------------------------*/ -// fprintf(stdout,"Lowest address = %08X\n",Lowest_Address); -// fprintf(stdout,"Highest address = %08X\n",Highest_Address); -// fprintf(stdout,"Pad Byte = %X\n", PadByte); + // fprintf(stdout,"Lowest address = %08X\n",Lowest_Address); + // fprintf(stdout,"Highest address = %08X\n",Highest_Address); + // fprintf(stdout,"Pad Byte = %X\n", PadByte); wCKS = 0; - if( !Cks_range_set ) - { + if (!Cks_range_set) { Cks_Start = Lowest_Address; Cks_End = Highest_Address; } - switch (Cks_Type) - { + switch (Cks_Type) { case 0: - for (i=Cks_Start; i<=Cks_End; i++) - { + for (i = Cks_Start; i <= Cks_End; i++) { wCKS += Memory_Block[i]; } //fprintf(stdout,"8-bit Checksum = %02X\n",wCKS & 0xff); - if( Cks_Addr_set ) - { + if (Cks_Addr_set) { wCKS = Cks_Value - (wCKS - Memory_Block[Cks_Addr]); Memory_Block[Cks_Addr] = (unsigned char)(wCKS & 0xff); //fprintf(stdout,"Addr %08X set to %02X\n",Cks_Addr, wCKS & 0xff); @@ -307,102 +293,98 @@ bool S19FileToBin(const char* filePath, unsigned char* vData,unsigned long* File case 2: - for (i=Cks_Start; i<=Cks_End; i+=2) - { - w = Memory_Block[i+1] | ((unsigned short)Memory_Block[i] << 8); + for (i = Cks_Start; i <= Cks_End; i += 2) { + w = Memory_Block[i + 1] | ((unsigned short)Memory_Block[i] << 8); wCKS += w; } //fprintf(stdout,"16-bit Checksum = %04X\n",wCKS); - if( Cks_Addr_set ) - { - w = Memory_Block[Cks_Addr+1] | ((unsigned short)Memory_Block[Cks_Addr] << 8); + if (Cks_Addr_set) { + w = Memory_Block[Cks_Addr + 1] | ((unsigned short)Memory_Block[Cks_Addr] << 8); wCKS = Cks_Value - (wCKS - w); Memory_Block[Cks_Addr] = (unsigned char)(wCKS >> 8); - Memory_Block[Cks_Addr+1] = (unsigned char)(wCKS & 0xff); + Memory_Block[Cks_Addr + 1] = (unsigned char)(wCKS & 0xff); //fprintf(stdout,"Addr %08X set to %04X\n",Cks_Addr, wCKS); } break; case 1: - for (i=Cks_Start; i<=Cks_End; i+=2) - { - w = Memory_Block[i] | ((unsigned short)Memory_Block[i+1] << 8); + for (i = Cks_Start; i <= Cks_End; i += 2) { + w = Memory_Block[i] | ((unsigned short)Memory_Block[i + 1] << 8); wCKS += w; } //fprintf(stdout,"16-bit Checksum = %04X\n",wCKS); - if( Cks_Addr_set ) - { - w = Memory_Block[Cks_Addr] | ((unsigned short)Memory_Block[Cks_Addr+1] << 8); + if (Cks_Addr_set) { + w = Memory_Block[Cks_Addr] | ((unsigned short)Memory_Block[Cks_Addr + 1] << 8); wCKS = Cks_Value - (wCKS - w); - Memory_Block[Cks_Addr+1] = (unsigned char)(wCKS >> 8); + Memory_Block[Cks_Addr + 1] = (unsigned char)(wCKS >> 8); Memory_Block[Cks_Addr] = (unsigned char)(wCKS & 0xff); //fprintf(stdout,"Addr %08X set to %04X\n",Cks_Addr, wCKS); } default: - ; + break; + } - if(Starting_Address_Setted) - { + if (Starting_Address_Setted) { Lowest_Address = Starting_Address; } - if(pBufferforLoadedFile!= NULL) + if (pBufferforLoadedFile != NULL) free(pBufferforLoadedFile); -// pBufferforLoadedFile=(unsigned char*)malloc(Highest_Address-Lowest_Address+1); -// g_ulFileSize=Highest_Address -Lowest_Address+ 1; -// memcpy(pBufferforLoadedFile,Memory_Block+Lowest_Address,Highest_Address-Lowest_Address+1); - pBufferforLoadedFile=(unsigned char*)malloc(Highest_Address+1); - g_ulFileSize=Highest_Address + 1; - memcpy(pBufferforLoadedFile,Memory_Block,Highest_Address+1); + // pBufferforLoadedFile=(unsigned char*)malloc(Highest_Address-Lowest_Address+1); + // g_ulFileSize=Highest_Address -Lowest_Address+ 1; + // memcpy(pBufferforLoadedFile,Memory_Block+Lowest_Address,Highest_Address-Lowest_Address+1); + pBufferforLoadedFile = (unsigned char*)malloc(Highest_Address + 1); + g_ulFileSize = Highest_Address + 1; + memcpy(pBufferforLoadedFile, Memory_Block, Highest_Address + 1); free(Memory_Block); - return true ; + return true; } bool BinToS19File(const char* filePath, unsigned char* vData, unsigned long FileSize) { //ANSI only - FILE *FileOut ; /* input files */ - FileOut = fopen(filePath,"wt") ; - if(FileOut==NULL) + FILE* FileOut; /* input files */ + FileOut = fopen(filePath, "wt"); + if (FileOut == NULL) return false; - int i; - int chksum; - int total = 0 ; + int i; + int chksum; + int total = 0; int addr = 0x00000000; long l1total = FileSize; - unsigned char recbuf[32] ; - char s19buf[128] ; + unsigned char recbuf[32]; + char s19buf[128]; while (l1total >= 32) { // copy data total = 32; - memcpy(recbuf,vData+addr,total); + memcpy(recbuf, vData + addr, total); - chksum = total+5; /* total bytes in this record */ + chksum = total + 5; /* total bytes in this record */ chksum += addr & 0xff; - chksum += (addr>>8) & 0xff; - chksum += (addr>>16) & 0xff; - chksum += (addr>>24) & 0xff; + chksum += (addr >> 8) & 0xff; + chksum += (addr >> 16) & 0xff; + chksum += (addr >> 24) & 0xff; fprintf(FileOut, "S3"); - fprintf(FileOut, "%02X%08X", total+5, addr) ; + fprintf(FileOut, "%02X%08X", total + 5, addr); - memset(s19buf, 0, sizeof(s19buf)) ; - for(i=0;i>8) & 0xff; - chksum += (addr>>16) & 0xff; - chksum += (addr>>24) & 0xff; + chksum += (addr >> 8) & 0xff; + chksum += (addr >> 16) & 0xff; + chksum += (addr >> 24) & 0xff; - fprintf(FileOut, "S3") ; ; /* record header preamble */ - fprintf(FileOut, "%02X%08X", total+5, addr) ; + fprintf(FileOut, "S3"); /* record header preamble */ + fprintf(FileOut, "%02X%08X", total + 5, addr); - memset(s19buf, 0, sizeof(s19buf)) ; - for(i=0;i + bool S19FileToBin(const char* filePath, unsigned char* vData,unsigned long* FileSize,unsigned char PaddingByte) ; bool BinToS19File(const char* filePath, unsigned char* vData, unsigned long FileSize); - #endif //_MOTOROLA_FILE_H diff --git a/SerialFlash.c b/SerialFlash.c index a375956..bf2db8c 100755 --- a/SerialFlash.c +++ b/SerialFlash.c @@ -1,10 +1,9 @@ -#include "Macro.h" -#include "usbdriver.h" +#include "SerialFlash.h" #include "ChipInfoDb.h" #include "FlashCommand.h" -#include "SerialFlash.h" +#include "project.h" +#include "usbdriver.h" #include -#include extern int m_isCanceled; extern int m_bProtectAfterWritenErase; @@ -12,11 +11,8 @@ extern int m_boEnReadQuadIO; extern int m_boEnWriteQuadIO; extern CHIP_INFO Chip_Info; extern volatile bool g_bIsSF600[16]; -extern void Sleep(unsigned int ms); extern bool Is_NewUSBCommand(int Index); -unsigned char g_micron_usVCR=0xFFFF; - unsigned char mcode_WRSR=0x01; unsigned char mcode_WRDI=0x04; unsigned char mcode_RDSR=0x05; @@ -33,6 +29,7 @@ unsigned int g_AT45_PageSize=0; unsigned int g_AT45_PageSizeMask=0; size_t AT45ChipSize=0; size_t AT45PageSize=0; + bool AT45doRDSR(unsigned char* cSR, int Index) { CNTRPIPE_RQ rq ; @@ -44,13 +41,10 @@ bool AT45doRDSR(unsigned char* cSR, int Index) rq.Function = URB_FUNCTION_VENDOR_ENDPOINT ; rq.Direction = VENDOR_DIRECTION_OUT ; rq.Request = TRANSCEIVE ; - if(Is_NewUSBCommand(Index)) - { + if (Is_NewUSBCommand(Index)) { rq.Value = RESULT_IN; rq.Index = RFU; - } - else - { + } else { rq.Value = RFU ; rq.Index = RESULT_IN ; } @@ -64,13 +58,10 @@ bool AT45doRDSR(unsigned char* cSR, int Index) rq.Function = URB_FUNCTION_VENDOR_ENDPOINT ; rq.Direction = VENDOR_DIRECTION_IN ; rq.Request = TRANSCEIVE ; - if(Is_NewUSBCommand(Index)) - { + if (Is_NewUSBCommand(Index)) { rq.Value = 1; rq.Index = 0; - } - else - { + } else { rq.Value = CTRL_TIMEOUT; rq.Index = NO_REGISTER; } @@ -95,6 +86,7 @@ bool AT45WaitForWIP(int USBIndex) AT45doRDSR(&cSR,USBIndex) ; Sleep(10); }while((!(cSR & 0x80)) && (i-- > 0)) ; // poll bit 7 + if(i<=0) return false; return true; @@ -108,8 +100,7 @@ unsigned char getWriteMode(int USBIndex) bool powerOfTwo = (1 == (cSR & 0x1)); // typedef enum - enum - { + enum { AT45DB011D = 0x1F22, AT45DB021D = 0x1F23, AT45DB041D = 0x1F24, @@ -119,8 +110,7 @@ unsigned char getWriteMode(int USBIndex) AT45DB642D = 0x1F28, }; - switch(Chip_Info.UniqueID) - { + switch (Chip_Info.UniqueID) { case AT45DB011D: case AT45DB021D: case AT45DB041D: @@ -136,10 +126,8 @@ unsigned char getWriteMode(int USBIndex) break; default: cMode = powerOfTwo ? 1 : 2; //256 : 264; - ; } return cMode; - } void SetPageSize(CHIP_INFO* mem, int USBIndex) @@ -170,7 +158,6 @@ size_t GetPageSize(void) return AT45PageSize; } - bool AT45xxx_protectBlock(int bProtect,int Index) { CNTRPIPE_RQ rq ; @@ -179,27 +166,21 @@ bool AT45xxx_protectBlock(int bProtect,int Index) rq.Function = URB_FUNCTION_VENDOR_ENDPOINT ; rq.Direction = VENDOR_DIRECTION_OUT ; rq.Request = TRANSCEIVE ; - if(Is_NewUSBCommand(Index)) - { + if (Is_NewUSBCommand(Index)) { rq.Value = RESULT_IN; rq.Index = RFU; - } - else - { + } else { rq.Value = RFU ; rq.Index = RESULT_IN ; } rq.Length = (unsigned long) 4 ; // protect block ,set BP1 BP0 to 1 - if(bProtect) - { + if (bProtect) { vInstruction[0]=0x3D; vInstruction[1]=0x2A; vInstruction[2]=0x7F; vInstruction[3]=0xA9; - } - else - { + } else { vInstruction[0]=0x3D; vInstruction[1]=0x2A; vInstruction[2]=0x7F; @@ -229,17 +210,14 @@ bool AT45rangeProgram(struct CAddressRange* AddrRange, unsigned char *vData, uns itr_begin = vData; - for( i = 0; i < itrCnt; ++ i) - { + for (i = 0; i < itrCnt; ++i) { bool b = BulkPipeWrite(itr_begin + (i * pageSize[idx]), pageSize[idx], USB_TIMEOUT,Index); - if((!b) || m_isCanceled) - { + if ((!b) || m_isCanceled) { return false ; } } // Sleep(10) ; return true ; - } int AT45rangSectorErase(size_t sectionSize, struct CAddressRange AddrRange,int USBIndex) @@ -252,13 +230,10 @@ int AT45rangSectorErase(size_t sectionSize, struct CAddressRange AddrRange,int U rq.Function = URB_FUNCTION_VENDOR_ENDPOINT ; rq.Direction = VENDOR_DIRECTION_OUT ; rq.Request = TRANSCEIVE ; - if(Is_NewUSBCommand(USBIndex)) - { + if (Is_NewUSBCommand(USBIndex)) { rq.Value = RESULT_IN; rq.Index = RFU; - } - else - { + } else { rq.Value = RFU ; rq.Index = RESULT_IN ; } @@ -267,17 +242,18 @@ int AT45rangSectorErase(size_t sectionSize, struct CAddressRange AddrRange,int U size_t sectorNum = (AddrRange.end - AddrRange.start + sectionSize - 1) / sectionSize ; size_t i; - for( i = 0; i < sectorNum; ++ i) - { + for (i = 0; i < sectorNum; ++i) { size_t addr = (AddrRange.start + i * sectionSize) ; vInstruction[1] = (unsigned char)((addr >> 16) & 0xff) ; //MSB vInstruction[2] = (unsigned char)((addr >> 8) & 0xff) ; //M vInstruction[3] = (unsigned char)(addr & 0xff) ; //LSB int b = OutCtrlRequest(&rq, vInstruction,4,USBIndex); - if((b==SerialFlash_FALSE)|| m_isCanceled) return false ; + if ((b == SerialFlash_FALSE) || m_isCanceled) + return false; - if(AT45WaitForWIP(USBIndex)==false) return false; + if (AT45WaitForWIP(USBIndex) == false) + return false; } return true ; @@ -288,8 +264,7 @@ bool AT45batchErase(size_t* vAddrs,size_t AddrSize,int USBIndex) CHIP_INFO mem_id; int i; SetPageSize(&mem_id,USBIndex); - if(strcmp(Chip_Info.Class,SUPPORT_ATMEL_45DBxxxB)==0) - { + if (strcmp(Chip_Info.Class, SUPPORT_ATMEL_45DBxxxB) == 0) { mem_id.PageSizeInByte=Chip_Info.PageSizeInByte; mem_id.ChipSizeInByte=Chip_Info.ChipSizeInByte; } @@ -297,33 +272,25 @@ bool AT45batchErase(size_t* vAddrs,size_t AddrSize,int USBIndex) struct CAddressRange range; - for(i=0; i>24)|(die1?0:(0x4000000>>24))); - vInstruction[2] = 0x800000>>16; - vInstruction[3] = 0x800000>>8; - vInstruction[4] = 0x800000; + vInstruction[2] = (unsigned char)((0x800000 >> 16) & 0xff); + vInstruction[3] = (unsigned char)((0x800000 >> 8) & 0xff); + vInstruction[4] = (unsigned char)((0x800000) & 0xff); vInstruction[5] = 0; vInstruction[6] = 0; rq.Function = URB_FUNCTION_VENDOR_ENDPOINT ; rq.Direction = VENDOR_DIRECTION_OUT ; rq.Request = TRANSCEIVE ; - if(Is_NewUSBCommand(Index)) - { + if (Is_NewUSBCommand(Index)) { rq.Value = RESULT_IN ; rq.Index = RFU ; - } - else - { + } else { rq.Value = RFU ; rq.Index = RESULT_IN ; } rq.Length = 7;//(unsigned long) 1 ; - if(OutCtrlRequest(&rq, &vInstruction, 7, Index) == SerialFlash_FALSE) + if (OutCtrlRequest(&rq, vInstruction, 7, Index) == SerialFlash_FALSE) return SerialFlash_FALSE ; - // second control packet : fetch data unsigned char vBuffer ; //just read one bytes , in fact more bytes are also available rq.Function = URB_FUNCTION_VENDOR_ENDPOINT ; rq.Direction = VENDOR_DIRECTION_IN ; rq.Request = TRANSCEIVE ; - if(Is_NewUSBCommand(Index)) - { + if (Is_NewUSBCommand(Index)) { rq.Value = 1; rq.Index = 0; - } - else - { + } else { rq.Value = CTRL_TIMEOUT ; rq.Index = NO_REGISTER ; } @@ -732,43 +678,36 @@ int S70FSxxx_Large_doRDCR2V(bool die1, unsigned char *cSR,int Index) // first control packet vInstruction[0] = 0x65; vInstruction[1] = ((0x800003>>24)|(die1?0:(0x4000000>>24))); - vInstruction[2] = 0x800003>>16; - vInstruction[3] = 0x800003>>8; - vInstruction[4] = 0x800003; + vInstruction[2] = (unsigned char)((0x800003 >> 16) & 0xff); + vInstruction[3] = (unsigned char)((0x800003 >> 8) & 0xff); + vInstruction[4] = (unsigned char)((0x800003) & 0xff); vInstruction[5] = 0; vInstruction[6] = 0; rq.Function = URB_FUNCTION_VENDOR_ENDPOINT ; rq.Direction = VENDOR_DIRECTION_OUT ; rq.Request = TRANSCEIVE ; - if(Is_NewUSBCommand(Index)) - { + if (Is_NewUSBCommand(Index)) { rq.Value = RESULT_IN ; rq.Index = RFU ; - } - else - { + } else { rq.Value = RFU ; rq.Index = RESULT_IN ; } rq.Length = 7;//(unsigned long) 1 ; - if(OutCtrlRequest(&rq, &vInstruction, 7, Index) == SerialFlash_FALSE) + if (OutCtrlRequest(&rq, vInstruction, 7, Index) == SerialFlash_FALSE) return SerialFlash_FALSE ; - // second control packet : fetch data unsigned char vBuffer ; //just read one bytes , in fact more bytes are also available rq.Function = URB_FUNCTION_VENDOR_ENDPOINT ; rq.Direction = VENDOR_DIRECTION_IN ; rq.Request = TRANSCEIVE ; - if(Is_NewUSBCommand(Index)) - { + if (Is_NewUSBCommand(Index)) { rq.Value = 1; rq.Index = 0; - } - else - { + } else { rq.Value = CTRL_TIMEOUT ; rq.Index = NO_REGISTER ; } @@ -782,7 +721,6 @@ int S70FSxxx_Large_doRDCR2V(bool die1, unsigned char *cSR,int Index) return SerialFlash_TRUE; } - int MX25Lxxx_Large_doRDCR(unsigned char *cSR,int Index) { CNTRPIPE_RQ rq ; @@ -794,34 +732,27 @@ int MX25Lxxx_Large_doRDCR(unsigned char *cSR,int Index) rq.Function = URB_FUNCTION_VENDOR_ENDPOINT ; rq.Direction = VENDOR_DIRECTION_OUT ; rq.Request = TRANSCEIVE ; - if(Is_NewUSBCommand(Index)) - { + if (Is_NewUSBCommand(Index)) { rq.Value = RESULT_IN ; rq.Index = RFU ; - } - else - { + } else { rq.Value = RFU ; rq.Index = RESULT_IN ; } rq.Length = 1;//(unsigned long) 1 ; - if(OutCtrlRequest(&rq, &vInstruction, 1, Index) == SerialFlash_FALSE) + if (OutCtrlRequest(&rq, vInstruction, 1, Index) == SerialFlash_FALSE) return SerialFlash_FALSE ; - // second control packet : fetch data unsigned char vBuffer ; //just read one bytes , in fact more bytes are also available rq.Function = URB_FUNCTION_VENDOR_ENDPOINT ; rq.Direction = VENDOR_DIRECTION_IN ; rq.Request = TRANSCEIVE ; - if(Is_NewUSBCommand(Index)) - { + if (Is_NewUSBCommand(Index)) { rq.Value = 1; rq.Index = 0; - } - else - { + } else { rq.Value = CTRL_TIMEOUT ; rq.Index = NO_REGISTER ; } @@ -846,34 +777,27 @@ int MX25Lxxx_Large_doRDSR(unsigned char *cSR,int Index) rq.Function = URB_FUNCTION_VENDOR_ENDPOINT ; rq.Direction = VENDOR_DIRECTION_OUT ; rq.Request = TRANSCEIVE ; - if(Is_NewUSBCommand(Index)) - { + if (Is_NewUSBCommand(Index)) { rq.Value = RESULT_IN ; rq.Index = RFU ; - } - else - { + } else { rq.Value = RFU ; rq.Index = RESULT_IN ; } rq.Length = 1;//(unsigned long) 1 ; - if(OutCtrlRequest(&rq, &vInstruction, 1, Index) == SerialFlash_FALSE) + if (OutCtrlRequest(&rq, vInstruction, 1, Index) == SerialFlash_FALSE) return SerialFlash_FALSE ; - // second control packet : fetch data unsigned char vBuffer ; //just read one bytes , in fact more bytes are also available rq.Function = URB_FUNCTION_VENDOR_ENDPOINT ; rq.Direction = VENDOR_DIRECTION_IN ; rq.Request = TRANSCEIVE ; - if(Is_NewUSBCommand(Index)) - { + if (Is_NewUSBCommand(Index)) { rq.Value = 1; rq.Index = 0; - } - else - { + } else { rq.Value = CTRL_TIMEOUT ; rq.Index = NO_REGISTER ; } @@ -887,10 +811,8 @@ int MX25Lxxx_Large_doRDSR(unsigned char *cSR,int Index) return SerialFlash_TRUE; } - int MX25Lxxx_Large_doWRCR(unsigned char cCR,int Index) { - unsigned char cSR; CNTRPIPE_RQ rq ; @@ -902,34 +824,27 @@ int MX25Lxxx_Large_doWRCR(unsigned char cCR,int Index) rq.Function = URB_FUNCTION_VENDOR_ENDPOINT ; rq.Direction = VENDOR_DIRECTION_OUT ; rq.Request = TRANSCEIVE ; - if(Is_NewUSBCommand(Index)) - { + if (Is_NewUSBCommand(Index)) { rq.Value = RESULT_IN ; rq.Index = RFU ; - } - else - { + } else { rq.Value = RFU ; rq.Index = RESULT_IN ; } rq.Length = 1;//(unsigned long) 1 ; - if(OutCtrlRequest(&rq, &vInstruction, 1, Index) == SerialFlash_FALSE) + if (OutCtrlRequest(&rq, vInstruction, 1, Index) == SerialFlash_FALSE) return SerialFlash_FALSE ; - // second control packet : fetch data unsigned char vBuffer ; //just read one bytes , in fact more bytes are also available rq.Function = URB_FUNCTION_VENDOR_ENDPOINT ; rq.Direction = VENDOR_DIRECTION_IN ; rq.Request = TRANSCEIVE ; - if(Is_NewUSBCommand(Index)) - { + if (Is_NewUSBCommand(Index)) { rq.Value = 1; rq.Index = 0; - } - else - { + } else { rq.Value = CTRL_TIMEOUT ; rq.Index = NO_REGISTER ; } @@ -952,7 +867,6 @@ int MX25Lxxx_Large_doWRCR(unsigned char cCR,int Index) vInstruction2[1] = cSR; vInstruction2[2] = cCR; - return FlashCommand_SendCommand_OutOnlyInstruction(vInstruction2, 3,Index); } // @@ -995,7 +909,6 @@ void S70FSxxx_Large_waitForWEL(bool die1,int Index) }while(((cSR & 0x02) == 0) && (i-- > 0)) ; } - /** * \brief * wait Write In Process with a timeout. @@ -1043,17 +956,14 @@ bool SerialFlash_waitForWIP(int Index) int SerialFlash_doWREN(int Index) { - unsigned char v=mcode_WREN;; + unsigned char v = mcode_WREN; return FlashCommand_SendCommand_OutOnlyInstruction(&v, 1,Index); - } - int SerialFlash_doWRDI(int Index) { unsigned char v=mcode_WRDI; return FlashCommand_SendCommand_OutOnlyInstruction(&v, 1,Index); - } bool SST25xFxx_protectBlock(int bProtect,int Index) @@ -1070,7 +980,6 @@ bool SST25xFxx_protectBlock(int bProtect,int Index) dstSRVal |= 0x8C ; // 8C : 1000 1100 } - unsigned char vInstruction[2] ; vInstruction[0] = 0x50 ; //Write enable FlashCommand_SendCommand_OutOnlyInstruction(vInstruction,1,Index); @@ -1086,10 +995,10 @@ bool SST25xFxx_protectBlock(int bProtect,int Index) // read SR result = SerialFlash_doRDSR(&tmpSRVal,Index) ; - if(! result) return false; + if (!result) + return false; numOfRetry -- ; - }; return ((tmpSRVal ^ dstSRVal)& 0x0C ) ? false:true; @@ -1116,10 +1025,10 @@ bool SST25xFxxA_protectBlock(int bProtect,int Index) // read SR result = SerialFlash_doRDSR(&tmpSRVal,Index) ; - if(! result) return false; + if (!result) + return false; numOfRetry -- ; - }; return ((tmpSRVal ^ dstSRVal)& 0x0C ) ? false:true; @@ -1146,10 +1055,10 @@ bool AT25FSxxx_protectBlock(int bProtect,int Index) // read SR result = SerialFlash_doRDSR(&tmpSRVal,Index) ; - if(! result) return false; + if (!result) + return false; numOfRetry -- ; - }; return ((tmpSRVal ^ dstSRVal)& 0x0C ) ? false:true; @@ -1160,13 +1069,13 @@ bool AT26Fxxx_protectBlock(int bProtect,int Index) const unsigned int AT26DF041 = 0x1F4400; const unsigned int AT26DF004 = 0x1F0400; const unsigned int AT26DF081A = 0x1F4501; - enum - { + enum { PROTECTSECTOR = 0x36, // Write Enable UNPROTECTSECTOR = 0x39, // Write Disable READPROTECTIONREGISTER = 0x3C, // Write Disable }; - if(AT26DF041 == Chip_Info.UniqueID) return true; // feature is not supported on this chip + if (AT26DF041 == Chip_Info.UniqueID) + return true; // feature is not supported on this chip bool result = false ; @@ -1175,17 +1084,14 @@ bool AT26Fxxx_protectBlock(int bProtect,int Index) result = SerialFlash_doWRSR(0,Index) ; unsigned char tmpSRVal; - do - { + do { result = SerialFlash_doRDSR(&tmpSRVal,Index) ; numOfRetry -- ; }while( (tmpSRVal & 0x01) && numOfRetry > 0 && result); - if (tmpSRVal & 0x80 ) return false; ///< enable SPRL - // send request CNTRPIPE_RQ rq ; unsigned char vInstruction[4] ={0}; @@ -1195,8 +1101,7 @@ bool AT26Fxxx_protectBlock(int bProtect,int Index) size_t iUniformSectorSize = 0x10000; // always regarded as 64K size_t cnt = Chip_Info.ChipSizeInByte / iUniformSectorSize; size_t i; - for( i = 0; i < cnt; ++i) - { + for (i = 0; i < cnt; ++i) { SerialFlash_doWREN(Index); vInstruction[1] = (unsigned char)i; @@ -1204,13 +1109,10 @@ bool AT26Fxxx_protectBlock(int bProtect,int Index) rq.Function = URB_FUNCTION_VENDOR_ENDPOINT ; rq.Direction = VENDOR_DIRECTION_OUT ; rq.Request = TRANSCEIVE ; - if(Is_NewUSBCommand(Index)) - { + if (Is_NewUSBCommand(Index)) { rq.Value = RESULT_IN; rq.Index = RFU; - } - else - { + } else { rq.Value = RFU ; rq.Index = RESULT_IN ; } @@ -1223,8 +1125,7 @@ bool AT26Fxxx_protectBlock(int bProtect,int Index) if(AT26DF081A == Chip_Info.UniqueID || AT26DF004 == Chip_Info.UniqueID) // 8K each for the last 64K { vInstruction[1] = (unsigned char)(cnt - 1); - for( i = 1; i < 8; ++i) - { + for (i = 1; i < 8; ++i) { SerialFlash_doWREN(Index); vInstruction[2] = (unsigned char)(i<<5); @@ -1232,13 +1133,10 @@ bool AT26Fxxx_protectBlock(int bProtect,int Index) rq.Function = URB_FUNCTION_VENDOR_ENDPOINT ; rq.Direction = VENDOR_DIRECTION_OUT ; rq.Request = TRANSCEIVE ; - if(Is_NewUSBCommand(Index)) - { + if (Is_NewUSBCommand(Index)) { rq.Value = RESULT_IN; rq.Index = RFU; - } - else - { + } else { rq.Value = RFU ; rq.Index = RESULT_IN ; } @@ -1262,13 +1160,10 @@ bool CMX25LxxxdoRDSCUR(unsigned char* cSR,int Index) rq.Function = URB_FUNCTION_VENDOR_ENDPOINT ; rq.Direction = VENDOR_DIRECTION_OUT ; rq.Request = TRANSCEIVE ; - if(Is_NewUSBCommand(Index)) - { + if (Is_NewUSBCommand(Index)) { rq.Value = RESULT_IN; rq.Index = RFU; - } - else - { + } else { rq.Value = RFU ; rq.Index = RESULT_IN ; } @@ -1282,13 +1177,10 @@ bool CMX25LxxxdoRDSCUR(unsigned char* cSR,int Index) rq.Function = URB_FUNCTION_VENDOR_ENDPOINT ; rq.Direction = VENDOR_DIRECTION_IN ; rq.Request = TRANSCEIVE ; - if(Is_NewUSBCommand(Index)) - { + if (Is_NewUSBCommand(Index)) { rq.Value = 1; rq.Index = RFU; - } - else - { + } else { rq.Value = CTRL_TIMEOUT; rq.Index = NO_REGISTER; } @@ -1302,7 +1194,6 @@ bool CMX25LxxxdoRDSCUR(unsigned char* cSR,int Index) return SerialFlash_TRUE; } - bool CS25FLxxx_LargedoUnlockDYB(unsigned int cSR, int Index) { // wait until WIP = 0 @@ -1318,9 +1209,7 @@ bool CS25FLxxx_LargedoUnlockDYB(unsigned int cSR, int Index) topend=0x20000; bottomstart=0x1fe0000; end=0x2000000; - } - else - { + } else { topend=0x20000; bottomstart=0xfe0000; end=0x1000000; @@ -1377,29 +1266,22 @@ int SerialFlash_protectBlock(int bProtect,int Index) return AT25FSxxx_protectBlock( bProtect, Index); else if(strstr(Chip_Info.Class,SUPPORT_ATMEL_AT26xxx)!=NULL) return AT26Fxxx_protectBlock( bProtect, Index); - else if(strstr(Chip_Info.Class,SUPPORT_MACRONIX_MX25Lxxx)!=NULL || strstr(Chip_Info.Class,SUPPORT_MACRONIX_MX25Lxxx_Large)!=NULL) - { + else if (strstr(Chip_Info.Class, SUPPORT_MACRONIX_MX25Lxxx) != NULL || strstr(Chip_Info.Class, SUPPORT_MACRONIX_MX25Lxxx_Large) != NULL) { unsigned char tmpSRVal; bool result; result=CMX25LxxxdoRDSCUR(&tmpSRVal,Index); - if(result==true && (tmpSRVal&0x80) && Chip_Info.MXIC_WPmode==true) - { + if (result == true && (tmpSRVal & 0x80) && Chip_Info.MXIC_WPmode == true) { if(bProtect != false) return true; SerialFlash_doWREN(Index) ; unsigned char v=GBULK; return FlashCommand_SendCommand_OutOnlyInstruction(&v, 1, Index); } - } - else if(strstr(Chip_Info.Class,SUPPORT_SPANSION_S25FLxx) != NULL || strstr(Chip_Info.Class,SUPPORT_SPANSION_S25FLxx_Large) != NULL) - { - if(bProtect==false && strstr(Chip_Info.TypeName,"Secure") != NULL) - { + } else if (strstr(Chip_Info.Class, SUPPORT_SPANSION_S25FLxx) != NULL || strstr(Chip_Info.Class, SUPPORT_SPANSION_S25FLxx_Large) != NULL) { + if (bProtect == false && strstr(Chip_Info.TypeName, "Secure") != NULL) { CS25FLxxx_LargedoUnlockDYB(0, Index); } - } - else if(strstr(Chip_Info.Class,SUPPORT_SST_26xFxxC) != NULL) - { + } else if (strstr(Chip_Info.Class, SUPPORT_SST_26xFxxC) != NULL) { unsigned char v=0x98; SerialFlash_waitForWEL(Index) ; FlashCommand_SendCommand_OutOnlyInstruction(&v,1,Index); @@ -1428,13 +1310,11 @@ int SerialFlash_protectBlock(int bProtect,int Index) if(! result) return false; numOfRetry -- ; - }; return ((tmpSRVal ^ dstSRVal)& 0x0C ) ? 0:1; } - int SerialFlash_EnableQuadIO(int bEnable,int boRW,int Index) { m_boEnWriteQuadIO = (bEnable & boRW); //Simon: ported done??? @@ -1445,14 +1325,11 @@ int SerialFlash_EnableQuadIO(int bEnable,int boRW,int Index) bool CEN25QHxx_LargeEnable4ByteAddrMode(bool Enable4Byte,int Index) { // SerialFlash_doWREN(Index); - if(Enable4Byte) - { + if (Enable4Byte) { unsigned char v= EN4B; if( FlashCommand_TransceiveOut(&v,1,false,Index)==SerialFlash_FALSE) return false; - } - else - { + } else { unsigned char v= EXIT4B; if(FlashCommand_TransceiveOut(&v,1,false,Index)==SerialFlash_FALSE) return false; @@ -1469,13 +1346,10 @@ bool CN25Qxxx_LargeRDFSR(unsigned char *cSR, int Index) rq.Function = URB_FUNCTION_VENDOR_ENDPOINT ; rq.Direction = VENDOR_DIRECTION_OUT ; rq.Request = TRANSCEIVE ; - if(Is_NewUSBCommand(Index)) - { + if (Is_NewUSBCommand(Index)) { rq.Value = RESULT_IN; rq.Index = RFU; - } - else - { + } else { rq.Value = RFU ; rq.Index = RESULT_IN ; } @@ -1489,13 +1363,10 @@ bool CN25Qxxx_LargeRDFSR(unsigned char *cSR, int Index) rq.Function = URB_FUNCTION_VENDOR_ENDPOINT ; rq.Direction = VENDOR_DIRECTION_IN ; rq.Request = TRANSCEIVE ; - if(Is_NewUSBCommand(Index)) - { + if (Is_NewUSBCommand(Index)) { rq.Value = 1; rq.Index = 0; - } - else - { + } else { rq.Value = CTRL_TIMEOUT ; rq.Index = NO_REGISTER ; } @@ -1511,14 +1382,14 @@ bool CN25Qxxx_Large_doWRVCR(unsigned char ucVCR,int Index) { unsigned char vInstruction[2]; //size 1 - SerialFlash_waitForWIP(Index); - SerialFlash_waitForWEL(Index); + SerialFlash_waitForWIP(Index); + SerialFlash_waitForWEL(Index); vInstruction[0]=0x81; vInstruction[1]=ucVCR&0xFF; - FlashCommand_TransceiveOut(&vInstruction,2,false,Index); + FlashCommand_TransceiveOut(vInstruction,2,false,Index); - SerialFlash_waitForWIP(Index); + SerialFlash_waitForWIP(Index); #if 0 unsigned char ucRDVCR=0xFF; @@ -1535,7 +1406,6 @@ bool CN25Qxxx_Large_doWRVCR(unsigned char ucVCR,int Index) return true; } - bool CN25Qxxx_Large_doRDENVCR(unsigned char *ucENVCR,int Index) { CNTRPIPE_RQ rq ; @@ -1545,13 +1415,10 @@ bool CN25Qxxx_Large_doRDENVCR(unsigned char *ucENVCR,int Index) rq.Function = URB_FUNCTION_VENDOR_ENDPOINT ; rq.Direction = VENDOR_DIRECTION_OUT ; rq.Request = TRANSCEIVE ; - if(Is_NewUSBCommand(Index)) - { + if (Is_NewUSBCommand(Index)) { rq.Value = RESULT_IN ; rq.Index = RFU ; - } - else - { + } else { rq.Value = RFU ; rq.Index = RESULT_IN ; } @@ -1565,13 +1432,10 @@ bool CN25Qxxx_Large_doRDENVCR(unsigned char *ucENVCR,int Index) rq.Function = URB_FUNCTION_VENDOR_ENDPOINT ; rq.Direction = VENDOR_DIRECTION_IN ; rq.Request = TRANSCEIVE ; - if(Is_NewUSBCommand(Index)) - { + if (Is_NewUSBCommand(Index)) { rq.Value = 1; rq.Index = 0; - } - else - { + } else { rq.Value = CTRL_TIMEOUT ; rq.Index = NO_REGISTER ; } @@ -1581,20 +1445,18 @@ bool CN25Qxxx_Large_doRDENVCR(unsigned char *ucENVCR,int Index) return SerialFlash_FALSE ; *ucENVCR = vBuffer; return SerialFlash_TRUE; - } - bool CN25Qxxx_Large_doWRENVCR(unsigned char ucENVCR,int Index) { unsigned char vInstruction[2]; //size 1 - SerialFlash_waitForWIP(Index); - SerialFlash_waitForWEL(Index); + SerialFlash_waitForWIP(Index); + SerialFlash_waitForWEL(Index); vInstruction[0]=0x61; vInstruction[1]=ucENVCR&0xFF; - FlashCommand_TransceiveOut(&vInstruction,2,false,Index); + FlashCommand_TransceiveOut(vInstruction,2,false,Index); #if 0 SerialFlash_waitForWIP(Index); @@ -1613,7 +1475,6 @@ bool CN25Qxxx_Large_doWRENVCR(unsigned char ucENVCR,int Index) return true; } - bool CN25Qxxx_Large_doRDVCR(unsigned char *ucVCR,int Index) { CNTRPIPE_RQ rq ; @@ -1623,13 +1484,10 @@ bool CN25Qxxx_Large_doRDVCR(unsigned char *ucVCR,int Index) rq.Function = URB_FUNCTION_VENDOR_ENDPOINT ; rq.Direction = VENDOR_DIRECTION_OUT ; rq.Request = TRANSCEIVE ; - if(Is_NewUSBCommand(Index)) - { + if (Is_NewUSBCommand(Index)) { rq.Value = RESULT_IN ; rq.Index = RFU ; - } - else - { + } else { rq.Value = RFU ; rq.Index = RESULT_IN ; } @@ -1643,13 +1501,10 @@ bool CN25Qxxx_Large_doRDVCR(unsigned char *ucVCR,int Index) rq.Function = URB_FUNCTION_VENDOR_ENDPOINT ; rq.Direction = VENDOR_DIRECTION_IN ; rq.Request = TRANSCEIVE ; - if(Is_NewUSBCommand(Index)) - { + if (Is_NewUSBCommand(Index)) { rq.Value = 1; rq.Index = 0; - } - else - { + } else { rq.Value = CTRL_TIMEOUT ; rq.Index = NO_REGISTER ; } @@ -1659,7 +1514,6 @@ bool CN25Qxxx_Large_doRDVCR(unsigned char *ucVCR,int Index) return SerialFlash_FALSE ; *ucVCR = vBuffer; return SerialFlash_TRUE; - } bool CN25Qxxx_MutipleDIe_LargeRDEAR(unsigned char *cSR, int Index) {// read status @@ -1676,13 +1530,10 @@ bool CN25Qxxx_MutipleDIe_LargeRDEAR(unsigned char *cSR, int Index) rq.Function = URB_FUNCTION_VENDOR_ENDPOINT ; rq.Direction = VENDOR_DIRECTION_OUT ; rq.Request = TRANSCEIVE ; - if(Is_NewUSBCommand(Index)) - { + if (Is_NewUSBCommand(Index)) { rq.Value = RESULT_IN ; rq.Index = RFU ; - } - else - { + } else { rq.Value = RFU ; rq.Index = RESULT_IN ; } @@ -1696,13 +1547,10 @@ bool CN25Qxxx_MutipleDIe_LargeRDEAR(unsigned char *cSR, int Index) rq.Function = URB_FUNCTION_VENDOR_ENDPOINT ; rq.Direction = VENDOR_DIRECTION_IN ; rq.Request = TRANSCEIVE ; - if(Is_NewUSBCommand(Index)) - { + if (Is_NewUSBCommand(Index)) { rq.Value = 1; rq.Index = 0; - } - else - { + } else { rq.Value = CTRL_TIMEOUT ; rq.Index = NO_REGISTER ; } @@ -1715,7 +1563,6 @@ bool CN25Qxxx_MutipleDIe_LargeRDEAR(unsigned char *cSR, int Index) return SerialFlash_TRUE; } - bool CN25Qxxx_MutipleDIe_LargeWREAR(unsigned char cSR, int Index) { unsigned char v[2]; @@ -1727,42 +1574,31 @@ bool CN25Qxxx_MutipleDIe_LargeWREAR(unsigned char cSR, int Index) bool CS25FLxx_LargeEnable4ByteAddrMode(bool Enable4Byte,int Index) { - if((strstr(Chip_Info.TypeName,"S25FL512Sxxxxxx1x")!=NULL)|| - (strstr(Chip_Info.TypeName,"S25FL512Sxxxxxx1x(Secure)")!=NULL)) - { - SerialFlash_waitForWEL(Index); - if(Enable4Byte) - { - unsigned char v[2]; - v[0]=0x17; - v[1]=0x80; - FlashCommand_TransceiveOut(&v,2,false,Index); - } - else - { - unsigned char v[2]; - v[0]=0x17; - v[1]=0x00; - FlashCommand_TransceiveOut(&v,2,false,Index); - } - } - else - { - if(Enable4Byte) - { - unsigned char v= EN4B; + if ((strstr(Chip_Info.TypeName, "S25FL512Sxxxxxx1x") != NULL) || (strstr(Chip_Info.TypeName, "S25FL512Sxxxxxx1x(Secure)") != NULL)) { + SerialFlash_waitForWEL(Index); + if (Enable4Byte) { + unsigned char v[2]; + v[0]=0x17; + v[1]=0x80; + FlashCommand_TransceiveOut(v,2,false,Index); + } else { + unsigned char v[2]; + v[0]=0x17; + v[1]=0x00; + FlashCommand_TransceiveOut(v,2,false,Index); + } + } else { + if (Enable4Byte) { + unsigned char v= EN4B; FlashCommand_TransceiveOut(&v,1,false,Index); return true; - } - else - { + } else { unsigned char v= EXIT4B; FlashCommand_TransceiveOut(&v,1,false,Index); return true; } } return true; - } bool CN25Qxxx_LargeEnable4ByteAddrMode(bool Enable4Byte,int Index) { @@ -1776,8 +1612,7 @@ bool CN25Qxxx_LargeEnable4ByteAddrMode(bool Enable4Byte,int Index) } return true; #else - if(Enable4Byte) - { + if (Enable4Byte) { unsigned char v= EN4B; int numOfRetry = 5 ; unsigned char re; @@ -1788,9 +1623,7 @@ bool CN25Qxxx_LargeEnable4ByteAddrMode(bool Enable4Byte,int Index) CN25Qxxx_LargeRDFSR(&re,Index); }while((re & 0x01)==0 && numOfRetry-- > 0); return true; - } - else - { + } else { unsigned char re; // CN25Qxxx_LargeRDFSR(&re,Index); unsigned char v= EXIT4B; @@ -1809,8 +1642,7 @@ bool CN25Qxxx_LargeEnable4ByteAddrMode(bool Enable4Byte,int Index) int S70FSxxx_Large_Enable4ByteAddrMode(int Enable4Byte,int Index) { - if(Enable4Byte) - { + if (Enable4Byte) { unsigned char v= EN4B; int numOfRetry = 5 ; unsigned char re; @@ -1828,9 +1660,7 @@ int S70FSxxx_Large_Enable4ByteAddrMode(int Enable4Byte,int Index) }while((re & 0x80)==0 && numOfRetry-- > 0); return true; - } - else - { + } else { unsigned char re; unsigned char v= EXIT4B; int numOfRetry = 5 ; @@ -1854,9 +1684,7 @@ int S70FSxxx_Large_Enable4ByteAddrMode(int Enable4Byte,int Index) //Simon: unused ??? int SerialFlash_Enable4ByteAddrMode(int bEnable,int Index) { - if(strstr(Chip_Info.Class,SUPPORT_EON_EN25QHxx_Large) != NULL || - strstr(Chip_Info.Class,SUPPORT_MACRONIX_MX25Lxxx_Large) != NULL || - strstr(Chip_Info.Class,SUPPORT_WINBOND_W25Pxx_Large) != NULL) + if (strstr(Chip_Info.Class, SUPPORT_EON_EN25QHxx_Large) != NULL || strstr(Chip_Info.Class, SUPPORT_MACRONIX_MX25Lxxx_Large) != NULL || strstr(Chip_Info.Class, SUPPORT_WINBOND_W25Pxx_Large) != NULL) return CEN25QHxx_LargeEnable4ByteAddrMode(bEnable,Index); else if(strstr(Chip_Info.Class,SUPPORT_SPANSION_S70FSxx_Large)!= NULL) return S70FSxxx_Large_Enable4ByteAddrMode(bEnable,Index); @@ -1867,7 +1695,6 @@ int SerialFlash_Enable4ByteAddrMode(int bEnable,int Index) return SerialFlash_TRUE; } - /** * \brief * read data, check if 0xFF @@ -1884,38 +1711,32 @@ int SerialFlash_rangeBlankCheck(struct CAddressRange *Range,int Index) if (Range->length <= 0) return SerialFlash_FALSE; vBuffer = malloc(Range->length); - if (vBuffer != 0) - { - if(SerialFlash_rangeRead(Range, vBuffer,Index) == SerialFlash_FALSE) - { + if (vBuffer != 0) { + if (SerialFlash_rangeRead(Range, vBuffer, Index) == SerialFlash_FALSE) { free(vBuffer); return false ; } - for(i=0; ilength; i++) - { - if(vBuffer[i] != 0xFF) - { -// printf("not blank at %X(%d)=%X\r\n",i,i,vBuffer[i]); + for (i = 0; i < Range->length; i++) { + if (vBuffer[i] != 0xFF) { + // printf("not blank at %X(%d)=%X\n",i,i,vBuffer[i]); free(vBuffer); return false; } } free(vBuffer); return true ; - } - else - { + } else { free(vBuffer); return SerialFlash_FALSE; } } - /** * \brief * Write brief comment for Download here. * - * it first check the data size and then delegates the actual operation to BulkPipeWrite() + * it first check the data size and then delegates the actual operation to + * BulkPipeWrite() * * \param AddrRange * the flash memory starting(inclusive) and end address(exlusive) @@ -1931,50 +1752,38 @@ int SerialFlash_rangeProgram(struct CAddressRange *AddrRange, unsigned char *vDa { if(strstr(Chip_Info.Class,SUPPORT_ATMEL_45DBxxxB) != NULL || strstr(Chip_Info.Class,SUPPORT_ATMEL_45DBxxxD) != NULL) return AT45rangeProgram(AddrRange, vData,mcode_Program, mcode_ProgramCode_4Adr, Index); - else if(strstr(Chip_Info.Class,SUPPORT_SPANSION_S25FLxx_Large)!=NULL) - { + else if (strstr(Chip_Info.Class, SUPPORT_SPANSION_S25FLxx_Large) != NULL) { if(g_bIsSF600[Index]==true) return SerialFlash_bulkPipeProgram(AddrRange, vData, PP_4ADR_256BYTE,mcode_ProgramCode_4Adr,Index); else return SerialFlash_bulkPipeProgram(AddrRange, vData, PP_4ADDR_256BYTE_12,mcode_ProgramCode_4Adr,Index); - } - else if(strstr(Chip_Info.Class,SUPPORT_SPANSION_S70FSxx_Large)!=NULL) - { - if(g_bIsSF600[Index]==true) - { + } else if (strstr(Chip_Info.Class, SUPPORT_SPANSION_S70FSxx_Large) != NULL) { + if (g_bIsSF600[Index] == true) { return SerialFlash_bulkPipeProgram(AddrRange, vData, PP_4ADDR_256BYTE_S70FS01GS,mcode_ProgramCode_4Adr_S70FSxxx,Index); - } - else + } else return false; - } - else + } else return SerialFlash_bulkPipeProgram(AddrRange, vData, mcode_Program, mcode_ProgramCode_4Adr,Index); } int SerialFlash_rangeRead(struct CAddressRange *AddrRange, unsigned char *vData,int Index) { - - if(strstr(Chip_Info.Class,SUPPORT_SPANSION_S25FLxx_Large)!=NULL) - { + if (strstr(Chip_Info.Class, SUPPORT_SPANSION_S25FLxx_Large) != NULL) { if(g_bIsSF600[Index]==true) return SerialFlash_bulkPipeRead(AddrRange, vData, BULK_4BYTE_FAST_READ,mcode_ReadCode,Index); else return SerialFlash_bulkPipeRead(AddrRange, vData, BULK_4BYTE_FAST_READ_MICRON,mcode_ReadCode,Index); } - if(strstr(Chip_Info.Class,SUPPORT_SPANSION_S70FSxx_Large)!=NULL) - { + if (strstr(Chip_Info.Class, SUPPORT_SPANSION_S70FSxx_Large) != NULL) { if(g_bIsSF600[Index]==true) return SerialFlash_bulkPipeRead(AddrRange, vData, BULK_4BYTE_FAST_READ,mcode_ReadCode_S70FSxxx,Index); else return false; - } - else - { + } else { return SerialFlash_bulkPipeRead(AddrRange, vData, (unsigned char) mcode_Read, (unsigned char) mcode_ReadCode, Index); } }; - /** * \brief * poll the status register after Erase operation. @@ -1985,7 +1794,8 @@ int SerialFlash_rangeRead(struct CAddressRange *AddrRange, unsigned char *vData, * (not fully implemented or used now) * * \remarks - * A polling command can be used to poll the status register after Erase operation + * A polling command can be used to poll the status register after Erase + * operation * */ int SerialFlash_DoPolling(int Index) @@ -2004,20 +1814,19 @@ int SerialFlash_DoPolling(int Index) return SerialFlash_FALSE ; return SerialFlash_TRUE ; - } - /** * \brief * retrieves object wellness flag * - * before calling any other method, be sure to call is_good() to check the wellness - * which indicates whether the instance is constructed successfully or not + * before calling any other method, be sure to call is_good() to check the + * wellness which indicates whether the instance is constructed successfully or + * not * * \returns - * true only if all objects of chip info, chip itself, USB are correctly constructed - * false otherwise + * true only if all objects of chip info, chip itself, USB are correctly + * constructed false otherwise * */ int SerialFlash_is_good() @@ -2030,10 +1839,12 @@ int SerialFlash_batchErase(uintptr_t* vAddrs,size_t AddrSize,int Index) { if(!SerialFlash_StartofOperation(Index)) return false; -// if(strstr(Chip_Info.Class,SUPPORT_ATMEL_45DBxxxB) != NULL || strstr(Chip_Info.Class,SUPPORT_ATMEL_45DBxxxD) != NULL) + // if(strstr(Chip_Info.Class,SUPPORT_ATMEL_45DBxxxB) != NULL || + // strstr(Chip_Info.Class,SUPPORT_ATMEL_45DBxxxD) != NULL) // return AT45batchErase(vAddrs, AddrSize,Index); - if(0 == mcode_SegmentErase) return 1; // chipErase code not initialised or not supported, please check chip class ctor. + if (0 == mcode_SegmentErase) + return 1; // chipErase code not initialised or not supported, please check chip class ctor. if(SerialFlash_protectBlock(false,Index) == SerialFlash_FALSE) return false ; @@ -2050,31 +1861,24 @@ int SerialFlash_batchErase(uintptr_t* vAddrs,size_t AddrSize,int Index) rq.Function = URB_FUNCTION_VENDOR_ENDPOINT ; rq.Direction = VENDOR_DIRECTION_OUT ; rq.Request = TRANSCEIVE ; - if(Is_NewUSBCommand(Index)) - { + if (Is_NewUSBCommand(Index)) { rq.Value = NO_RESULT_IN; rq.Index = RFU; - } - else - { + } else { rq.Value = RFU ; rq.Index = NO_RESULT_IN ; } rq.Length = 5; - for(i=0; i0x1000000) - { + if (Chip_Info.ChipSizeInByte > 0x1000000) { // MSB~ LSB (31...0) vInstruction[1]=(unsigned char)((vAddrs[i] >> 24) & 0xff) ; //MSB vInstruction[2] = (unsigned char)((vAddrs[i] >> 16) & 0xff) ; //M vInstruction[3] = (unsigned char)((vAddrs[i] >> 8) & 0xff) ; //M vInstruction[4] = (unsigned char)(vAddrs[i] & 0xff) ; //LSB rq.Length = 5; - } - else - { + } else { // MSB~ LSB (23...0) vInstruction[1] = (unsigned char)((vAddrs[i] >> 16) & 0xff) ; //MSB vInstruction[2] = (unsigned char)((vAddrs[i] >> 8) & 0xff) ; //M @@ -2109,13 +1913,10 @@ int SerialFlash_rangeErase(unsigned char cmd, size_t sectionSize, struct CAddres rq.Function = URB_FUNCTION_VENDOR_ENDPOINT ; rq.Direction = VENDOR_DIRECTION_OUT ; rq.Request = TRANSCEIVE ; - if(Is_NewUSBCommand(Index)) - { + if (Is_NewUSBCommand(Index)) { rq.Value = RESULT_IN; rq.Index = RFU; - } - else - { + } else { rq.Value = RFU ; rq.Index = RESULT_IN ; } @@ -2123,24 +1924,20 @@ int SerialFlash_rangeErase(unsigned char cmd, size_t sectionSize, struct CAddres size_t sectorNum = (AddrRange->end - AddrRange->start + sectionSize - 1) / sectionSize ; size_t i; - for(i = 0; i < sectorNum; ++ i) - { + for (i = 0; i < sectorNum; ++i) { SerialFlash_waitForWEL(Index) ; // MSB~ LSB (23...0) size_t addr = (AddrRange->start + i * sectionSize) ; - if(Chip_Info.ChipSizeInByte>0x1000000) - { + if (Chip_Info.ChipSizeInByte > 0x1000000) { // MSB~ LSB (31...0) vInstruction[1] = (unsigned char)((addr >> 24) & 0xff) ; //MSB vInstruction[2] = (unsigned char)((addr >> 16) & 0xff) ; //M vInstruction[3] = (unsigned char)((addr >> 8) & 0xff) ; //M vInstruction[4] = (unsigned char)(addr & 0xff) ; //LSB rq.Length = (unsigned long)(5) ; - } - else - { + } else { // MSB~ LSB (23...0) vInstruction[1] = (unsigned char)((addr >> 16) & 0xff) ; //MSB vInstruction[2] = (unsigned char)((addr >> 8) & 0xff) ; //M @@ -2149,7 +1946,8 @@ int SerialFlash_rangeErase(unsigned char cmd, size_t sectionSize, struct CAddres } int b = OutCtrlRequest(&rq, vInstruction, rq.Length, Index); - if((b==SerialFlash_FALSE)|| m_isCanceled) return false ; + if ((b == SerialFlash_FALSE) || m_isCanceled) + return false; SerialFlash_waitForWIP(Index) ; } @@ -2162,7 +1960,6 @@ bool S70FSxxx_Large_chipErase(unsigned int Addr,unsigned int Length,int USBIndex { S70FSxxx_Large_Enable4ByteAddrMode(true, USBIndex); - // wait until WIP = 0 S70FSxxx_Large_waitForWIP(true,USBIndex) ; // wait until WEL = 1 @@ -2173,11 +1970,10 @@ bool S70FSxxx_Large_chipErase(unsigned int Addr,unsigned int Length,int USBIndex vInstruction[2]=0x00; vInstruction[3]=0x00; vInstruction[4]=0x00; - FlashCommand_SendCommand_OutOnlyInstruction(&vInstruction,5,USBIndex); + FlashCommand_SendCommand_OutOnlyInstruction(vInstruction, 5, USBIndex); S70FSxxx_Large_waitForWIP(true,USBIndex) ; //check die 1 - S70FSxxx_Large_Enable4ByteAddrMode(false, USBIndex); S70FSxxx_Large_waitForWIP(false,USBIndex) ; @@ -2188,13 +1984,14 @@ bool S70FSxxx_Large_chipErase(unsigned int Addr,unsigned int Length,int USBIndex vInstruction[2]=0x00; vInstruction[3]=0x00; vInstruction[4]=0x00; - FlashCommand_SendCommand_OutOnlyInstruction(&vInstruction,5,USBIndex); + FlashCommand_SendCommand_OutOnlyInstruction(vInstruction, 5, USBIndex); S70FSxxx_Large_waitForWIP(false,USBIndex) ; //check die 2 + return true; } /// chip erase -int SerialFlash_chipErase(int Index) +bool SerialFlash_chipErase(int Index) { if(!SerialFlash_StartofOperation(Index)) return false; @@ -2203,13 +2000,15 @@ int SerialFlash_chipErase(int Index) if(strstr(Chip_Info.Class,SUPPORT_SPANSION_S70FSxx_Large) != NULL) return S70FSxxx_Large_chipErase(0, Chip_Info.ChipSizeInByte, Index); - if( SerialFlash_protectBlock(false,Index) == SerialFlash_FALSE) return false ; + if (SerialFlash_protectBlock(false, Index) == SerialFlash_FALSE) + return false; SerialFlash_waitForWEL(Index) ; unsigned char v = mcode_ChipErase; FlashCommand_SendCommand_OutOnlyInstruction(&v,1,Index); SerialFlash_waitForWIP(Index) ; - //if( SerialFlash_protectBlock(m_bProtectAfterWritenErase,Index) == SerialFlash_FALSE) return false ; + // if( SerialFlash_protectBlock(m_bProtectAfterWritenErase,Index) == + // SerialFlash_FALSE) return false ; if(!SerialFlash_EndofOperation(Index)) return false; return true ; @@ -2218,23 +2017,22 @@ int SerialFlash_chipErase(int Index) /// die erase int SerialFlash_DieErase(int Index) { - if( SerialFlash_protectBlock(false,Index) == SerialFlash_FALSE) return false ; + if (SerialFlash_protectBlock(false, Index) == SerialFlash_FALSE) + return false; SerialFlash_Enable4ByteAddrMode(true, Index); -// printf("Die erase!\r\n"); + // printf("Die erase!\n"); // send request unsigned char vInstruction[5]; int numOfRetry = 5 ; unsigned char re; vInstruction[0] = mcode_ChipErase; -// printf("chip erase=0x%x\r\n",mcode_ChipErase); - + // printf("chip erase=0x%x\n",mcode_ChipErase); size_t dieNum = ((strstr(Chip_Info.Class,"2Die") != NULL)? 2:4); size_t die_size= Chip_Info.ChipSizeInByte/dieNum; size_t i; - for(i = 0; i < dieNum; i++) - { + for (i = 0; i < dieNum; i++) { SerialFlash_waitForWEL(Index) ; // MSB~ LSB (23...0) @@ -2264,22 +2062,17 @@ int SerialFlash_bulkPipeProgram(struct CAddressRange *AddrRange, unsigned char * size_t i,j,divider; unsigned char *itr_begin; - if(!SerialFlash_StartofOperation(Index)) - return false; - if(SerialFlash_protectBlock(false,Index) == SerialFlash_FALSE) - return false ; + if (!SerialFlash_StartofOperation(Index)) return false; + if (SerialFlash_protectBlock(false, Index) == SerialFlash_FALSE) return false; - if(modeWrite!= PP_SB_FPGA) - SerialFlash_waitForWEL(Index) ; + if (modeWrite != PP_SB_FPGA) SerialFlash_waitForWEL(Index); SerialFlash_Enable4ByteAddrMode(true, Index); - if(SerialFlash_EnableQuadIO(true,m_boEnWriteQuadIO,Index) == SerialFlash_FALSE) - return false; + if (SerialFlash_EnableQuadIO(true, m_boEnWriteQuadIO, Index) == SerialFlash_FALSE) return false; -// printf("WriteMode=%d, WriteCom=%02X\r\n", modeWrite,WriteCom); + // printf("WriteMode=%d, WriteCom=%02X\n", modeWrite,WriteCom); itr_begin = vData; - switch(modeWrite) - { + switch (modeWrite) { //transfer how many data each time case MODE_NUMONYX_PCM: case PP_32BYTE: @@ -2293,9 +2086,10 @@ int SerialFlash_bulkPipeProgram(struct CAddressRange *AddrRange, unsigned char * break; } - if((AddrRange->end/0x1000000)>(AddrRange->start/0x1000000))//(AddrRange.end>0x1000000 && AddrRange.start<0x1000000) ||(AddrRange.end>0x2000000 && AddrRange.start<0x2000000) || + if ((AddrRange->end / 0x1000000) > (AddrRange->start / 0x1000000)) //(AddrRange.end>0x1000000 && AddrRange.start<0x1000000) + //||(AddrRange.end>0x2000000 && AddrRange.start<0x2000000) + //|| { - struct CAddressRange down_range; struct CAddressRange range_temp; range_temp.start= AddrRange->start&0xFF000000; @@ -2305,11 +2099,10 @@ int SerialFlash_bulkPipeProgram(struct CAddressRange *AddrRange, unsigned char * down_range.end = AddrRange->end; size_t packageNum; size_t loop=(range_temp.end-range_temp.start)/0x1000000; -// printf("loop=%d \r\n",loop); + // printf("loop=%d \n",loop); // printf("range_temp.end=%x,range_temp.start=%x\n\r",range_temp.end,range_temp.start); - for(j=0; jend; else @@ -2322,23 +2115,20 @@ int SerialFlash_bulkPipeProgram(struct CAddressRange *AddrRange, unsigned char * down_range.length=down_range.end-down_range.start; packageNum = down_range.length >> divider ; -// printf("packageNum=%d \r\n",packageNum); -// printf("down_range.start=%X, down_range.end=%X \r\n",down_range.start, down_range.end); + // printf("packageNum=%d \n",packageNum); + // printf("down_range.start=%X, down_range.end=%X\n",down_range.start, down_range.end); FlashCommand_SendCommand_SetupPacketForBulkWrite(&down_range, modeWrite,WriteCom,Index); - for( i = 0; i < packageNum; ++ i) - { + for (i = 0; i < packageNum; ++i) { BulkPipeWrite((unsigned char *)(itr_begin + (i << divider)), 1<end - AddrRange->start) >> divider ; FlashCommand_SendCommand_SetupPacketForBulkWrite(AddrRange, modeWrite,WriteCom,Index); - for(i = 0; i < packageNum; ++ i) - { + for (i = 0; i < packageNum; ++i) { BulkPipeWrite((unsigned char *)((itr_begin + (i << divider))), 1<length <= 0) { return false; } -// printf("AddrRange->end=%x, AddrRange->start=%x\r\n",AddrRange->end,AddrRange->start); + // printf("AddrRange->end=%x, AddrRange->start=%x\n",AddrRange->end,AddrRange->start); if((AddrRange->end/0x1000000) > (AddrRange->start/0x1000000))//(AddrRange.end>0x1000000 && AddrRange.start<0x1000000) { struct CAddressRange read_range; @@ -2387,10 +2178,8 @@ int SerialFlash_bulkPipeRead(struct CAddressRange *AddrRange, unsigned char *vDa read_range.end=AddrRange->end; loop=(range_temp.end-range_temp.start)/0x1000000; - for(j=0; j> 9 ; FlashCommand_SendCommand_SetupPacketForBulkRead(&read_range, modeRead,ReadCom,Index); - for(i = 0; i < pageNum; ++ i) - { + for (i = 0; i < pageNum; ++i) { ret = BulkPipeRead(vData + (BufferLocation+i)*512, USB_TIMEOUT,Index); - if((ret!=512) || m_isCanceled) return 0 ; + if ((ret != 512) || m_isCanceled) + return 0; //memcpy(vData + (BufferLocation+i)*512, v, 512); } BufferLocation += pageNum; } - } - else - { + } else { unsigned char EAR=(AddrRange->start*0x1000000)>>24; - if(g_bIsSF600[Index]==false &&(strstr(Chip_Info.Class,SUPPORT_NUMONYX_N25Qxxx_Large_2Die) != NULL || - strstr(Chip_Info.Class,SUPPORT_NUMONYX_N25Qxxx_Large_4Die) != NULL)) - { + if (g_bIsSF600[Index] == false && (strstr(Chip_Info.Class, SUPPORT_NUMONYX_N25Qxxx_Large_2Die) != NULL || strstr(Chip_Info.Class, SUPPORT_NUMONYX_N25Qxxx_Large_4Die) != NULL)) { unsigned char re=0; int numOfRetry = 5 ; do{ @@ -2454,11 +2239,9 @@ int SerialFlash_bulkPipeRead(struct CAddressRange *AddrRange, unsigned char *vDa } pageNum = AddrRange->length >> 9 ; FlashCommand_SendCommand_SetupPacketForBulkRead(AddrRange, modeRead,ReadCom,Index); - for(i = 0; i < pageNum; ++ i) - { + for (i = 0; i < pageNum; ++i) { ret = BulkPipeRead(vData + i*ret, USB_TIMEOUT,Index); - if((ret != 512) || m_isCanceled) - { + if ((ret != 512) || m_isCanceled) { return false ; } //memcpy(vData + i*ret, v, ret); @@ -2472,10 +2255,8 @@ int SerialFlash_bulkPipeRead(struct CAddressRange *AddrRange, unsigned char *vDa return false; return true ; - } - void SerialFlash_SetCancelOperationFlag() { m_isCanceled = SerialFlash_TRUE; @@ -2506,52 +2287,37 @@ int SerialFlash_is_protectbits_set(int Index) } bool SerialFlash_StartofOperation(int Index) { - if(strstr(Chip_Info.Class,SUPPORT_NUMONYX_N25Qxxx_Large_2Die) != NULL || - strstr(Chip_Info.Class,SUPPORT_NUMONYX_N25Qxxx_Large_4Die) != NULL || - strstr(Chip_Info.Class,SUPPORT_NUMONYX_N25Qxxx_Large) != NULL) - { + if (strstr(Chip_Info.Class, SUPPORT_NUMONYX_N25Qxxx_Large_2Die) != NULL || strstr(Chip_Info.Class, SUPPORT_NUMONYX_N25Qxxx_Large_4Die) != NULL || strstr(Chip_Info.Class, SUPPORT_NUMONYX_N25Qxxx_Large) != NULL) { if(CN25Qxxx_Large_doWRVCR(0xFB,Index)==false) return false; if(CN25Qxxx_Large_doWRENVCR(0xFF,Index)==false) return false; return true; - } - else if(strstr(Chip_Info.Class,SUPPORT_ST_M25Pxx) != NULL) - { - if(strstr(Chip_Info.TypeName,"N25Q064")!=NULL ) - { + } else if (strstr(Chip_Info.Class, SUPPORT_ST_M25Pxx) != NULL) { + if (strstr(Chip_Info.TypeName, "N25Q064") != NULL) { if(CN25Qxxx_Large_doWRVCR(0xFB,Index)==false) return false; if(CN25Qxxx_Large_doWRENVCR(0xDF,Index)==false) return false; } - if(strstr(Chip_Info.TypeName,"N25Q128")!=NULL ) - { + if (strstr(Chip_Info.TypeName, "N25Q128") != NULL) { if(CN25Qxxx_Large_doWRVCR(0xFB,Index)==false) return false; if(CN25Qxxx_Large_doWRENVCR(0xF7,Index)==false) return false; } } - if(strstr(Chip_Info.Class,SUPPORT_MACRONIX_MX25Lxxx_Large) != NULL) - { - if((strstr(Chip_Info.TypeName,"MX66U1G45GXDJ54")!=NULL)|| - (strstr(Chip_Info.TypeName,"MX66U2G45GXRI54")!=NULL)) - { + if (strstr(Chip_Info.Class, SUPPORT_MACRONIX_MX25Lxxx_Large) != NULL) { + if ((strstr(Chip_Info.TypeName, "MX66U1G45GXDJ54") != NULL) || (strstr(Chip_Info.TypeName, "MX66U2G45GXRI54") != NULL)) { MX25Lxxx_Large_doWRCR(0x70,Index); - - - } } return true; } - bool SerialFlash_EndofOperation(int Index) { return true; } - diff --git a/SerialFlash.h b/SerialFlash.h index 555086b..12d0836 100755 --- a/SerialFlash.h +++ b/SerialFlash.h @@ -1,16 +1,16 @@ #pragma once #ifndef SERIALFLASHS - #define SERIALFLASHS +#include "Macro.h" + #define SerialFlash_FALSE -1 #define SerialFlash_TRUE 1 #ifdef _NON_UBUNTU typedef unsigned long uintptr_t; #endif -//#define size_t unsigned int enum //list of all chip-specific instruction, for ST serial flash { @@ -33,7 +33,6 @@ typedef unsigned long uintptr_t; EXIT4B = 0xE9, }; - int SerialFlash_doWRSR(unsigned char cSR,int Index); int SerialFlash_doRDSR(unsigned char *cSR,int Index); @@ -69,10 +68,9 @@ int SerialFlash_batchErase(uintptr_t* vAddrs,size_t AddrSize,int Index); int SerialFlash_rangeErase(unsigned char cmd, size_t sectionSize, struct CAddressRange *AddrRange,int Index); -int SerialFlash_chipErase(int Index); +bool SerialFlash_chipErase(int Index); int SerialFlash_DieErase(int Index); - int SerialFlash_bulkPipeProgram(struct CAddressRange *AddrRange, unsigned char *vData, unsigned char modeWrite, unsigned char WriteCom, int Index); int SerialFlash_bulkPipeRead(struct CAddressRange *AddrRange, unsigned char *vData, unsigned char modeRead,unsigned char ReadCom,int Index); @@ -104,5 +102,4 @@ size_t GetPageSize(void); bool SerialFlash_StartofOperation(int Index); bool SerialFlash_EndofOperation(int Index); - #endif //SERIALFLASHS diff --git a/board.c b/board.c index e7dc09b..7c6f5a5 100755 --- a/board.c +++ b/board.c @@ -1,51 +1,73 @@ -#include "Macro.h" -#include "usbdriver.h" #include "board.h" -#define SerialFlash_FALSE -1 -#define SerialFlash_TRUE 1 -#define min(a,b) (a>b? b:a) +#include "FlashCommand.h" +#include "project.h" +#include "usbdriver.h" +#define SerialFlash_FALSE -1 +#define SerialFlash_TRUE 1 +#define min(a, b) (a > b ? b : a) -volatile bool g_bIsSF600=false; +volatile bool g_bIsSF600 = false; extern char g_board_type[8]; extern int g_firmversion; extern unsigned int g_IO1Select; extern unsigned int g_IO4Select; extern unsigned int g_Vcc; -extern void Sleep(unsigned int ms); extern bool Is_NewUSBCommand(int Index); -//extern int is_SF100nBoardVersionGreaterThan_5_2_0(int Index); -//extern int is_SF600nBoardVersionGreaterThan_6_9_0(int Index); +// extern int is_SF100nBoardVersionGreaterThan_5_2_0(int Index); +// extern int is_SF600nBoardVersionGreaterThan_6_9_0(int Index); + +bool ReadOnBoardFlash(unsigned char* Data, bool ReadUID, int Index) +{ + CNTRPIPE_RQ rq; + unsigned char vBuffer[16]; + + rq.Function = URB_FUNCTION_VENDOR_ENDPOINT; + rq.Direction = VENDOR_DIRECTION_IN; + rq.Request = 0x05; + if (Is_NewUSBCommand(Index)) { + rq.Value = ReadUID; + rq.Index = 0; + } else { + rq.Value = 0x00; + rq.Index = ReadUID; + } + rq.Length = 16; + + if (InCtrlRequest(&rq, vBuffer, 16, Index) == SerialFlash_FALSE) { + return false; + } + memcpy(Data, vBuffer, 16); + return true; +} void QueryBoard(int Index) { -// printf("QueryBoard\r\n"); + // printf("QueryBoard\n"); return; - if(!Is_usbworking(Index)) - { - printf("Do not find SFxx programmer!!\n"); - return ; + if (!Is_usbworking(Index)) { + printf("Do not find SFxx programmer!!\n"); + return; } - CNTRPIPE_RQ rq ; + CNTRPIPE_RQ rq; unsigned char vBuffer[16]; - rq.Function = URB_FUNCTION_VENDOR_ENDPOINT ; - rq.Direction = VENDOR_DIRECTION_IN ; - rq.Request = PROGINFO_REQUEST ; - rq.Value = RFU ; - rq.Index = 0x00 ; + rq.Function = URB_FUNCTION_VENDOR_ENDPOINT; + rq.Direction = VENDOR_DIRECTION_IN; + rq.Request = PROGINFO_REQUEST; + rq.Value = RFU; + rq.Index = 0x00; rq.Length = (unsigned long)16; memset(vBuffer, '\0', 16); - if(InCtrlRequest(&rq, vBuffer,16,Index) == SerialFlash_FALSE) - { - printf("send fail\r\n"); - return ; + if (InCtrlRequest(&rq, vBuffer, 16, Index) == SerialFlash_FALSE) { + printf("send fail\n"); + return; } - memcpy(g_board_type,&vBuffer[0],8); + memcpy(g_board_type, &vBuffer[0], 8); // memcpy(g_firmversion,&vBuffer[10],8); -// printf("g_board_type=%s\r\n",g_board_type); +// printf("g_board_type=%s\n",g_board_type); // if(strstr(g_board_type,"SF600") != NULL) // g_bIsSF600=true; // else @@ -69,7 +91,7 @@ void QueryBoard(int Index) if(m_info.board_type==L"SF600") { - ReadOnBoardFlash(false,Index); + ReadOnBoardFlash(false,Index); m_info.dwUID=(DWORD)m_vUID[0]<<16 | (DWORD)m_vUID[1]<<8 | m_vUID[2]; } else @@ -79,20 +101,21 @@ void QueryBoard(int Index) unsigned char GetFPGAVersion(int Index) { - if(strstr(g_board_type,"SF600")==NULL) return -1; + if (strstr(g_board_type, "SF600") == NULL) + return -1; - CNTRPIPE_RQ rq ; - unsigned char vDataPack ; + CNTRPIPE_RQ rq; + unsigned char vDataPack; - rq.Function = URB_FUNCTION_VENDOR_ENDPOINT ; + rq.Function = URB_FUNCTION_VENDOR_ENDPOINT; rq.Direction = VENDOR_DIRECTION_IN; - rq.Request = 0x1C ; - rq.Value = RFU ; - rq.Index = 0 ; - rq.Length = 1 ; + rq.Request = 0x1C; + rq.Value = RFU; + rq.Index = 0; + rq.Length = 1; - if(InCtrlRequest(&rq,&vDataPack,1,Index)==SerialFlash_FALSE){ - return -1 ; + if (InCtrlRequest(&rq, &vDataPack, 1, Index) == SerialFlash_FALSE) { + return -1; } return vDataPack; @@ -100,894 +123,815 @@ unsigned char GetFPGAVersion(int Index) bool SetIO(unsigned char ioState, int Index) { - CNTRPIPE_RQ rq ; - unsigned char vDataPack ; // 1 bytes, in fact no needs + CNTRPIPE_RQ rq; + unsigned char vDataPack; // 1 bytes, in fact no needs - rq.Function = URB_FUNCTION_VENDOR_ENDPOINT ; - rq.Direction = VENDOR_DIRECTION_OUT ; - rq.Request = SET_IO ; + rq.Function = URB_FUNCTION_VENDOR_ENDPOINT; + rq.Direction = VENDOR_DIRECTION_OUT; + rq.Request = SET_IO; - if(Is_NewUSBCommand(Index)) - { - rq.Value = (ioState&0x0F)|0x70 ; - rq.Index = 0; - } - else - { - rq.Value = ioState ; - rq.Index = 0x07 ; - } - rq.Length = 0 ; + if (Is_NewUSBCommand(Index)) { + rq.Value = (ioState & 0x0F) | 0x70; + rq.Index = 0; + } else { + rq.Value = ioState; + rq.Index = 0x07; + } + rq.Length = 0; - if(OutCtrlRequest(&rq,&vDataPack,0,Index)==SerialFlash_FALSE){ - return false ; + if (OutCtrlRequest(&rq, &vDataPack, 0, Index) == SerialFlash_FALSE) { + return false; } - return true ; + return true; } -bool SetTargetFlash(unsigned char StartupMode,int Index) +bool SetTargetFlash(unsigned char StartupMode, int Index) { - CNTRPIPE_RQ rq ; - unsigned char vInstruction ; + CNTRPIPE_RQ rq; + unsigned char vInstruction; - rq.Function = URB_FUNCTION_VENDOR_ENDPOINT ; - rq.Direction = VENDOR_DIRECTION_OUT ; + rq.Function = URB_FUNCTION_VENDOR_ENDPOINT; + rq.Direction = VENDOR_DIRECTION_OUT; rq.Request = SET_TARGET_FLASH; rq.Value = StartupMode; - rq.Index = 0 ; - rq.Length = 0 ; + rq.Index = 0; + rq.Length = 0; - if(OutCtrlRequest(&rq,&vInstruction,0,Index)==SerialFlash_FALSE ) - { - return false ; + if (OutCtrlRequest(&rq, &vInstruction, 0, Index) == SerialFlash_FALSE) { + return false; } return true; } -bool SetLEDProgBoard(size_t Color,int Index) +bool SetLEDProgBoard(size_t Color, int Index) { - if(! Is_usbworking(Index)) - { + if (!Is_usbworking(Index)) { return false; } - CNTRPIPE_RQ rq ; + CNTRPIPE_RQ rq; unsigned char vBuffer[16]; - rq.Function = URB_FUNCTION_VENDOR_ENDPOINT ; - rq.Direction = VENDOR_DIRECTION_OUT ; - rq.Request = SET_IO ; - if(Is_NewUSBCommand(Index)) - { - rq.Value = (Color&0xFFF7) | (g_IO1Select<<1)|(g_IO4Select<<3); - rq.Index=0; - } - else - { - rq.Value = 0x09 ; - rq.Index = Color>>8 ;//LED 0:ON 1:OFF Bit0:Green Bit1:Orange Bit2:Red - } - rq.Length = 0 ; + rq.Function = URB_FUNCTION_VENDOR_ENDPOINT; + rq.Direction = VENDOR_DIRECTION_OUT; + rq.Request = SET_IO; + if (Is_NewUSBCommand(Index)) { + rq.Value = (Color & 0xFFF7) | (g_IO1Select << 1) | (g_IO4Select << 3); + rq.Index = 0; + } else { + rq.Value = 0x09; + rq.Index = Color >> 8; // LED 0:ON 1:OFF Bit0:Green Bit1:Orange Bit2:Red + } + rq.Length = 0; - if(OutCtrlRequest(&rq,vBuffer,0,Index)==SerialFlash_FALSE) - { + if (OutCtrlRequest(&rq, vBuffer, 0, Index) == SerialFlash_FALSE) { return false; } return true; } -bool SetGreenLEDOn(bool boOn,int Index) +bool SetGreenLEDOn(bool boOn, int Index) { - return SetLEDProgBoard(boOn?0x0609:0x0709,Index); + return SetLEDProgBoard(boOn ? 0x0609 : 0x0709, Index); } bool SetOrangeLEDOn(bool boOn, int Index) { - return SetLEDProgBoard(boOn?0x0509:0x0709,Index); + return SetLEDProgBoard(boOn ? 0x0509 : 0x0709, Index); } bool SetRedLEDOn(bool boOn, int Index) { - return SetLEDProgBoard(boOn?0x0309:0x0709,Index); -} - -bool SetLEDOnOff(size_t Color,int Index) -{ - bool result=true; - switch(Color) - { - case SITE_ERROR: - result&=SetRedLEDOn(true,Index); - break; - case SITE_BUSY: - result&=SetOrangeLEDOn(true,Index); - break; - case SITE_OK: - result&=SetGreenLEDOn(true,Index); - break; - case SITE_NORMAL: - result&=SetLEDProgBoard(0x0709,Index);// Turn off LED - break; - } + return SetLEDProgBoard(boOn ? 0x0309 : 0x0709, Index); +} + +bool SetLEDOnOff(size_t Color, int Index) +{ + bool result = true; + switch (Color) { + case SITE_ERROR: + result &= SetRedLEDOn(true, Index); + break; + case SITE_BUSY: + result &= SetOrangeLEDOn(true, Index); + break; + case SITE_OK: + result &= SetGreenLEDOn(true, Index); + break; + case SITE_NORMAL: + result &= SetLEDProgBoard(0x0709, Index); // Turn off LED + break; + } return result; } -bool SetCS(size_t value,int Index) +bool SetCS(size_t value, int Index) { - if(! Is_usbworking(Index)) - { + if (!Is_usbworking(Index)) { return false; } - CNTRPIPE_RQ rq ; - unsigned char vBuffer ; - rq.Function = URB_FUNCTION_VENDOR_ENDPOINT ; - rq.Direction = VENDOR_DIRECTION_OUT ; + CNTRPIPE_RQ rq; + unsigned char vBuffer; + rq.Function = URB_FUNCTION_VENDOR_ENDPOINT; + rq.Direction = VENDOR_DIRECTION_OUT; rq.Request = SET_CS; rq.Value = value; // 12MHz - rq.Index = RFU ; - rq.Length = 0 ; + rq.Index = RFU; + rq.Length = 0; - if(OutCtrlRequest(&rq, &vBuffer,0,Index)==SerialFlash_FALSE) - { - return false; + if (OutCtrlRequest(&rq, &vBuffer, 0, Index) == SerialFlash_FALSE) { + return false; } return true; } -bool SetIOModeToSF600(size_t value,int Index) +bool SetIOModeToSF600(size_t value, int Index) { - if(! Is_usbworking(Index)) - { + if (!Is_usbworking(Index)) { return false; } - CNTRPIPE_RQ rq ; - unsigned char vBuffer ; - rq.Function = URB_FUNCTION_VENDOR_ENDPOINT ; - rq.Direction = VENDOR_DIRECTION_OUT ; + CNTRPIPE_RQ rq; + unsigned char vBuffer; + rq.Function = URB_FUNCTION_VENDOR_ENDPOINT; + rq.Direction = VENDOR_DIRECTION_OUT; rq.Request = SET_IOMODE; rq.Value = value; - rq.Index = RFU ; - rq.Length = 0 ; + rq.Index = RFU; + rq.Length = 0; - if(OutCtrlRequest(&rq, &vBuffer,0,Index)==SerialFlash_FALSE) - { - return false; + if (OutCtrlRequest(&rq, &vBuffer, 0, Index) == SerialFlash_FALSE) { + return false; } return true; } -bool BlinkProgBoard(bool boIsV5,int Index) +bool BlinkProgBoard(bool boIsV5, int Index) { - if(! Is_usbworking(Index) ) - { + if (!Is_usbworking(Index)) { return false; } - SetGreenLEDOn(true,Index); + SetGreenLEDOn(true, Index); Sleep(500); - SetGreenLEDOn(false,Index); + SetGreenLEDOn(false, Index); return true; } -bool ReadOnBoardFlash(unsigned char* Data,bool ReadUID,int Index) -{ - CNTRPIPE_RQ rq ; - unsigned char vBuffer[16] ; - - rq.Function = URB_FUNCTION_VENDOR_ENDPOINT ; - rq.Direction = VENDOR_DIRECTION_IN ; - rq.Request = 0x05 ; - if(Is_NewUSBCommand(Index)) - { - rq.Value = ReadUID; - rq.Index = 0; - } - else - { - rq.Value = 0x00 ; - rq.Index = ReadUID; - } - rq.Length = 16 ; - - if(InCtrlRequest(&rq, vBuffer,16,Index)==SerialFlash_FALSE) - { - return false; - } - memcpy(Data,vBuffer,16); -} - -bool LeaveSF600Standalone(bool Enable,int Index) +bool LeaveSF600Standalone(bool Enable, int Index) { - if(! Is_usbworking(Index)) - { + if (!Is_usbworking(Index)) { return false; } - CNTRPIPE_RQ rq ; - unsigned char vBuffer ; - rq.Function = URB_FUNCTION_VENDOR_ENDPOINT ; - rq.Direction = VENDOR_DIRECTION_OUT ; + CNTRPIPE_RQ rq; + unsigned char vBuffer; + rq.Function = URB_FUNCTION_VENDOR_ENDPOINT; + rq.Direction = VENDOR_DIRECTION_OUT; rq.Request = SET_SA; rq.Value = Enable; - rq.Index = RFU ; - rq.Length = 0 ; + rq.Index = RFU; + rq.Length = 0; - if(OutCtrlRequest(&rq,&vBuffer,0,Index)==SerialFlash_FALSE) - { + if (OutCtrlRequest(&rq, &vBuffer, 0, Index) == SerialFlash_FALSE) { return false; } return true; } -bool SetSPIClockValue(unsigned short v,int Index) +bool SetSPIClockValue(unsigned short v, int Index) { - if(!Is_usbworking(Index) ) + if (!Is_usbworking(Index)) return false; // send request - CNTRPIPE_RQ rq ; + CNTRPIPE_RQ rq; unsigned char vBuffer; - rq.Function = URB_FUNCTION_VENDOR_ENDPOINT ; - rq.Direction = VENDOR_DIRECTION_OUT ; + rq.Function = URB_FUNCTION_VENDOR_ENDPOINT; + rq.Direction = VENDOR_DIRECTION_OUT; rq.Request = SET_SPICLK; - rq.Value = v ; - rq.Index = RFU ; - rq.Length = 0 ; + rq.Value = v; + rq.Index = RFU; + rq.Length = 0; - if(OutCtrlRequest(&rq, &vBuffer,0,Index)==SerialFlash_FALSE) - return false; + if (OutCtrlRequest(&rq, &vBuffer, 0, Index) == SerialFlash_FALSE) + return false; return true; } unsigned int ReadUID(int Index) { - if(! Is_usbworking(Index) ) - { + if (!Is_usbworking(Index)) { return false; } - unsigned int dwUID=0; + unsigned int dwUID = 0; - if(g_bIsSF600==true) - { + if (g_bIsSF600 == true) { unsigned char vUID[16]; - ReadOnBoardFlash(vUID,false,Index); - dwUID=(unsigned int)vUID[0]<<16 | (unsigned int)vUID[1]<<8 | vUID[2]; + if (ReadOnBoardFlash(vUID, false, Index) == false) + return false; + dwUID = (unsigned int)vUID[0] << 16 | (unsigned int)vUID[1] << 8 | vUID[2]; return dwUID; } - CNTRPIPE_RQ rq ; - unsigned char vBuffer[3] ; + CNTRPIPE_RQ rq; + unsigned char vBuffer[3]; // read - rq.Function = URB_FUNCTION_VENDOR_OTHER ; - rq.Direction = VENDOR_DIRECTION_IN ; - rq.Request = 0x7; - rq.Value = 0 ; - rq.Index = 0xEF00 ; - rq.Length = 3; + rq.Function = URB_FUNCTION_VENDOR_OTHER; + rq.Direction = VENDOR_DIRECTION_IN; + rq.Request = 0x7; + rq.Value = 0; + rq.Index = 0xEF00; + rq.Length = 3; - if( InCtrlRequest(&rq, vBuffer,3,Index)==SerialFlash_FALSE) - { - return false; + if (InCtrlRequest(&rq, vBuffer, 3, Index) == SerialFlash_FALSE) { + return false; } - dwUID=(unsigned int)vBuffer[0]<<16 | (unsigned int)vBuffer[1]<<8 | vBuffer[2]; + dwUID = (unsigned int)vBuffer[0] << 16 | (unsigned int)vBuffer[1] << 8 | vBuffer[2]; return dwUID; } bool SetSPIClockDefault(int Index) { - if(!Is_usbworking(Index) ) - return false; - // send request - CNTRPIPE_RQ rq ; - unsigned char vBuffer ; - rq.Function = URB_FUNCTION_VENDOR_ENDPOINT ; - rq.Direction = VENDOR_DIRECTION_OUT ; - rq.Request = SET_SPICLK; - rq.Value = 0x02; // 12MHz - rq.Index = 0 ; - rq.Length = 0 ; + if (!Is_usbworking(Index)) + return false; + // send request + CNTRPIPE_RQ rq; + unsigned char vBuffer; + rq.Function = URB_FUNCTION_VENDOR_ENDPOINT; + rq.Direction = VENDOR_DIRECTION_OUT; + rq.Request = SET_SPICLK; + rq.Value = 0x02; // 12MHz + rq.Index = 0; + rq.Length = 0; - if(OutCtrlRequest(&rq, &vBuffer,0,Index)==SerialFlash_FALSE) - { - printf("Set SPI clock error\r\n"); - return false; - } + if (OutCtrlRequest(&rq, &vBuffer, 0, Index) == SerialFlash_FALSE) { + printf("Set SPI clock error\n"); + return false; + } - return true; + return true; } -bool SetVpp4IAP(bool bOn,int Index) +bool SetVpp4IAP(bool bOn, int Index) { - if(!Is_usbworking(Index) ) - return false; - // send request - CNTRPIPE_RQ rq ; - unsigned char vBuffer ; + if (!Is_usbworking(Index)) + return false; + // send request + CNTRPIPE_RQ rq; + unsigned char vBuffer; - rq.Function = URB_FUNCTION_VENDOR_OTHER ; - rq.Direction = VENDOR_DIRECTION_IN ; - rq.Request = bOn ? 0x0 : 0x01 ; - rq.Value = 0 ; - rq.Index = 0 ; - rq.Length = 0x01 ; + rq.Function = URB_FUNCTION_VENDOR_OTHER; + rq.Direction = VENDOR_DIRECTION_IN; + rq.Request = bOn ? 0x0 : 0x01; + rq.Value = 0; + rq.Index = 0; + rq.Length = 0x01; - if(OutCtrlRequest(&rq, &vBuffer,1,Index)==SerialFlash_FALSE) - { - return false; - } + if (OutCtrlRequest(&rq, &vBuffer, 1, Index) == SerialFlash_FALSE) { + return false; + } - Sleep(200); - return true; + Sleep(200); + return true; } bool UnlockRASS(int Index) { - if(!Is_usbworking(Index) ) - return false; + if (!Is_usbworking(Index)) + return false; - // send request - CNTRPIPE_RQ rq ; - unsigned char vBuffer ; + // send request + CNTRPIPE_RQ rq; + unsigned char vBuffer; - rq.Function = URB_FUNCTION_VENDOR_OTHER ; - rq.Direction = VENDOR_DIRECTION_IN ; - rq.Request = 0x03 ; - rq.Value = 0 ; - rq.Index = 0 ; - rq.Length = 0x01 ; + rq.Function = URB_FUNCTION_VENDOR_OTHER; + rq.Direction = VENDOR_DIRECTION_IN; + rq.Request = 0x03; + rq.Value = 0; + rq.Index = 0; + rq.Length = 0x01; - if(OutCtrlRequest(&rq, &vBuffer,1,Index)==SerialFlash_FALSE) - { - return false; - } - return true; + if (OutCtrlRequest(&rq, &vBuffer, 1, Index) == SerialFlash_FALSE) { + return false; + } + return true; } unsigned char ReadManufacturerID(int Index) { - if(!Is_usbworking(Index) ) - return false; - - if(g_bIsSF600==true) - { - unsigned char vUID[16]; - ReadOnBoardFlash(vUID,false,Index); - return vUID[3]; - } - - CNTRPIPE_RQ rq ; - unsigned char vBuffer; - - // read - rq.Function = URB_FUNCTION_VENDOR_OTHER ; - rq.Direction = VENDOR_DIRECTION_IN ; - rq.Request = 0x7; - rq.Value = 0 ; - rq.Index = 0xEF03; - rq.Length = 1; - - if(InCtrlRequest(&rq, &vBuffer,1,Index)==SerialFlash_FALSE) - { - return false; - } - return vBuffer; - -} - -bool EraseST7Sectors(bool bSect1,int Index) -{ - if(!Is_usbworking(Index) ) - return false; - - // send request - CNTRPIPE_RQ rq ; - unsigned char vBuffer ; - - rq.Function = URB_FUNCTION_VENDOR_OTHER ; - rq.Direction = VENDOR_DIRECTION_IN ; - rq.Request = bSect1 ? 0x04 : 0x05 ; - rq.Value = 0 ; - rq.Index = 0 ; - rq.Length = 0x01 ; - - if(OutCtrlRequest(&rq, &vBuffer,1,Index)==SerialFlash_FALSE) - { - return false; - } - return true; -} - -bool ProgramSectors(const char* sFilePath, bool bSect1,int Index) -{ - const unsigned int iSect1StartAddr = 0xE000; - const unsigned int iSect2StartAddr = 0x8000; - const unsigned int iSect1Size = 0x1000; - const unsigned int iSect2Size = 0x6000; - - unsigned char* pBuffer=NULL; - unsigned char* pTmp=NULL; - FW_INFO fw_info; - FILE * pFile; - size_t lSize; - unsigned char Data; - - unsigned int iStartAddr = bSect1 ? iSect1StartAddr : iSect2StartAddr; - unsigned int Size,tmp; - unsigned int uiIndex=0; - - // prog sectors - CNTRPIPE_RQ rq ; - - pFile = fopen( sFilePath , "rb" ); - if (pFile==NULL) - { - printf("Can not open %s \n",sFilePath); - return false; - } - - fseek (pFile , 0 , SEEK_SET); - lSize=fread ((unsigned char*)&fw_info,1,sizeof(FW_INFO),pFile); - if(lSize != sizeof(FW_INFO)) - printf("Possible read length error %s\n", sFilePath); - - - if(bSect1==true) - { - Size=iSect1Size; - uiIndex=fw_info.FirstIndex; - } - else - { - Size=iSect2Size; - uiIndex=fw_info.SecondIndex; - } - - pBuffer = (unsigned char*)malloc(Size); - memset(pBuffer,0xFF,Size); - fseek (pFile , uiIndex, SEEK_SET); - lSize = fread(pBuffer,1,Size,pFile); - if(lSize != Size) - printf("Possible read length error %s\n", sFilePath); - - fclose (pFile); - pTmp=pBuffer; - - - while(Size) - { - tmp=min(Size,0x100); - /// receive page - rq.Function = URB_FUNCTION_VENDOR_OTHER; - rq.Direction = VENDOR_DIRECTION_OUT; - rq.Request = 0x1; - rq.Value = 0; - rq.Index = 0; - rq.Length = 0x100; /// plage size for ST7 Iap prog - - if(OutCtrlRequest(&rq,pTmp,tmp,Index)==SerialFlash_FALSE) - { - free(pBuffer); - return false; - } + if (!Is_usbworking(Index)) + return false; + + if (g_bIsSF600 == true) { + unsigned char vUID[16]; + if (ReadOnBoardFlash(vUID, false, Index) == false) + return false; + return vUID[3]; + } - /// program page - rq.Function = URB_FUNCTION_VENDOR_OTHER; - rq.Direction = VENDOR_DIRECTION_IN; - rq.Request = 0x8; - rq.Value = 0; - rq.Index = iStartAddr & 0xFFFF; ///< ConvLongToInt(lngST7address Mod &H10000) - rq.Length = 0x1; - iStartAddr += 0x100; + CNTRPIPE_RQ rq; + unsigned char vBuffer; - if(OutCtrlRequest(&rq,&Data,1,Index )==SerialFlash_FALSE) - { - free(pBuffer); - return false; - } - Size-=tmp; - pTmp+=tmp; - } + // read + rq.Function = URB_FUNCTION_VENDOR_OTHER; + rq.Direction = VENDOR_DIRECTION_IN; + rq.Request = 0x7; + rq.Value = 0; + rq.Index = 0xEF03; + rq.Length = 1; + + if (InCtrlRequest(&rq, &vBuffer, 1, Index) == SerialFlash_FALSE) { + return false; + } + return vBuffer; +} + +bool EraseST7Sectors(bool bSect1, int Index) +{ + if (!Is_usbworking(Index)) + return false; + + // send request + CNTRPIPE_RQ rq; + unsigned char vBuffer; + + rq.Function = URB_FUNCTION_VENDOR_OTHER; + rq.Direction = VENDOR_DIRECTION_IN; + rq.Request = bSect1 ? 0x04 : 0x05; + rq.Value = 0; + rq.Index = 0; + rq.Length = 0x01; + + if (OutCtrlRequest(&rq, &vBuffer, 1, Index) == SerialFlash_FALSE) { + return false; + } + return true; +} + +bool ProgramSectors(const char* sFilePath, bool bSect1, int Index) +{ + const unsigned int iSect1StartAddr = 0xE000; + const unsigned int iSect2StartAddr = 0x8000; + const unsigned int iSect1Size = 0x1000; + const unsigned int iSect2Size = 0x6000; + + unsigned char* pBuffer = NULL; + unsigned char* pTmp = NULL; + FW_INFO fw_info; + FILE* pFile; + size_t lSize; + unsigned char Data; + + unsigned int iStartAddr = bSect1 ? iSect1StartAddr : iSect2StartAddr; + unsigned int Size, tmp; + unsigned int uiIndex = 0; + + // prog sectors + CNTRPIPE_RQ rq; + + pFile = fopen(sFilePath, "rb"); + if (pFile == NULL) { + printf("Can not open %s \n", sFilePath); + return false; + } + + fseek(pFile, 0, SEEK_SET); + lSize = fread((unsigned char*)&fw_info, 1, sizeof(FW_INFO), pFile); + if (lSize != sizeof(FW_INFO)) + printf("Possible read length error %s\n", sFilePath); + + if (bSect1 == true) { + Size = iSect1Size; + uiIndex = fw_info.FirstIndex; + } else { + Size = iSect2Size; + uiIndex = fw_info.SecondIndex; + } + + pBuffer = (unsigned char*)malloc(Size); + memset(pBuffer, 0xFF, Size); + fseek(pFile, uiIndex, SEEK_SET); + lSize = fread(pBuffer, 1, Size, pFile); + if (lSize != Size) + printf("Possible read length error %s\n", sFilePath); + + fclose(pFile); + pTmp = pBuffer; + + while (Size) { + tmp = min(Size, 0x100); + /// receive page + rq.Function = URB_FUNCTION_VENDOR_OTHER; + rq.Direction = VENDOR_DIRECTION_OUT; + rq.Request = 0x1; + rq.Value = 0; + rq.Index = 0; + rq.Length = 0x100; /// plage size for ST7 Iap prog + + if (OutCtrlRequest(&rq, pTmp, tmp, Index) == SerialFlash_FALSE) { + free(pBuffer); + return false; + } + + /// program page + rq.Function = URB_FUNCTION_VENDOR_OTHER; + rq.Direction = VENDOR_DIRECTION_IN; + rq.Request = 0x8; + rq.Value = 0; + rq.Index = iStartAddr & 0xFFFF; ///< ConvLongToInt(lngST7address Mod &H10000) + rq.Length = 0x1; + iStartAddr += 0x100; + + if (OutCtrlRequest(&rq, &Data, 1, Index) == SerialFlash_FALSE) { + free(pBuffer); + return false; + } + Size -= tmp; + pTmp += tmp; + } - free(pBuffer); - return true; + free(pBuffer); + return true; } bool UpdateChkSum(int Index) { - if(!Is_usbworking(Index) ) - return false; + if (!Is_usbworking(Index)) + return false; - CNTRPIPE_RQ rq ; - unsigned char vBuffer[2] ; + CNTRPIPE_RQ rq; + unsigned char vBuffer[2]; - // send to calculate checksum - rq.Function = URB_FUNCTION_VENDOR_OTHER ; - rq.Direction = VENDOR_DIRECTION_IN ; - rq.Request = 0x9; - rq.Value = 0 ; - rq.Index = 0 ; - rq.Length = 2 ; + // send to calculate checksum + rq.Function = URB_FUNCTION_VENDOR_OTHER; + rq.Direction = VENDOR_DIRECTION_IN; + rq.Request = 0x9; + rq.Value = 0; + rq.Index = 0; + rq.Length = 2; - if(OutCtrlRequest(&rq, vBuffer,2,Index)==SerialFlash_FALSE) - { - return false; - } + if (OutCtrlRequest(&rq, vBuffer, 2, Index) == SerialFlash_FALSE) { + return false; + } - return true; + return true; } -bool WriteUID(unsigned int dwUID,int Index) +bool WriteUID(unsigned int dwUID, int Index) { - if(!Is_usbworking(Index) ) - return false; + if (!Is_usbworking(Index)) + return false; - if(g_bIsSF600) - return true; + if (g_bIsSF600) + return true; - CNTRPIPE_RQ rq; - unsigned char Data; + CNTRPIPE_RQ rq; + unsigned char Data; - // read - rq.Function = URB_FUNCTION_VENDOR_OTHER ; - rq.Direction = VENDOR_DIRECTION_IN ; - rq.Request = 0x6; - rq.Value = (unsigned char)(dwUID>>16) ; - rq.Index = 0xEF00 ; - rq.Length = 1; + // read + rq.Function = URB_FUNCTION_VENDOR_OTHER; + rq.Direction = VENDOR_DIRECTION_IN; + rq.Request = 0x6; + rq.Value = (unsigned char)(dwUID >> 16); + rq.Index = 0xEF00; + rq.Length = 1; - if(InCtrlRequest(&rq, &Data,1,Index)==SerialFlash_FALSE) - { - return false; - } + if (InCtrlRequest(&rq, &Data, 1, Index) == SerialFlash_FALSE) { + return false; + } - rq.Index = 0xEF01 ; - rq.Value = (unsigned char)(dwUID>>8) ; + rq.Index = 0xEF01; + rq.Value = (unsigned char)(dwUID >> 8); - if(InCtrlRequest(&rq, &Data,1,Index)==SerialFlash_FALSE) - { - return false; - } + if (InCtrlRequest(&rq, &Data, 1, Index) == SerialFlash_FALSE) { + return false; + } - rq.Index = 0xEF02 ; - rq.Value = (unsigned char)(dwUID) ; + rq.Index = 0xEF02; + rq.Value = (unsigned char)(dwUID); - if(!InCtrlRequest(&rq, &Data,1,Index)) - { - return false; - } + if (!InCtrlRequest(&rq, &Data, 1, Index)) { + return false; + } - return true; + return true; } -bool WriteManufacturerID(unsigned char ManuID,int Index) +bool WriteManufacturerID(unsigned char ManuID, int Index) { - if(!Is_usbworking(Index) ) - return false; + if (!Is_usbworking(Index)) + return false; - if(g_bIsSF600) - return true; + if (g_bIsSF600) + return true; - CNTRPIPE_RQ rq ; - unsigned char Data; + CNTRPIPE_RQ rq; + unsigned char Data; - // read - rq.Function = URB_FUNCTION_VENDOR_OTHER ; - rq.Direction = VENDOR_DIRECTION_IN ; - rq.Request = 0x6; - rq.Value = ManuID ; - rq.Index = 0xEF03 ; - rq.Length = 1; + // read + rq.Function = URB_FUNCTION_VENDOR_OTHER; + rq.Direction = VENDOR_DIRECTION_IN; + rq.Request = 0x6; + rq.Value = ManuID; + rq.Index = 0xEF03; + rq.Length = 1; - if(InCtrlRequest(&rq, &Data,1,Index)==SerialFlash_FALSE) - { - return false; - } + if (InCtrlRequest(&rq, &Data, 1, Index) == SerialFlash_FALSE) { + return false; + } - return true; + return true; } -bool ReadMemOnST7(unsigned int iAddr,int Index) +bool ReadMemOnST7(unsigned int iAddr, int Index) { - if(!Is_usbworking(Index) ) - return false; + if (!Is_usbworking(Index)) + return false; - CNTRPIPE_RQ rq ; - unsigned char vBuffer[2] ; + CNTRPIPE_RQ rq; + unsigned char vBuffer[2]; - // read - rq.Function = URB_FUNCTION_VENDOR_OTHER ; - rq.Direction = VENDOR_DIRECTION_IN ; - rq.Request = 0x7; - rq.Value = 0 ; - rq.Index = iAddr & 0xFFFF ; - rq.Length = 2; + // read + rq.Function = URB_FUNCTION_VENDOR_OTHER; + rq.Direction = VENDOR_DIRECTION_IN; + rq.Request = 0x7; + rq.Value = 0; + rq.Index = iAddr & 0xFFFF; + rq.Length = 2; - if(OutCtrlRequest(&rq, vBuffer,2,Index)==SerialFlash_FALSE) - { - return false; - } + if (OutCtrlRequest(&rq, vBuffer, 2, Index) == SerialFlash_FALSE) { + return false; + } - return true; + return true; } -bool UpdateSF600Firmware(const char* sFolder,int Index) +bool UpdateSF600Firmware(const char* sFolder, int Index) { - bool boResult=true; - unsigned char vUID[16]; - unsigned int dwUID; + bool boResult = true; + unsigned char vUID[16]; + unsigned int dwUID; - ReadOnBoardFlash(vUID,false,Index); - dwUID=(unsigned int)vUID[0]<<16 | (unsigned int)vUID[1]<<8 | vUID[2]; - boResult &= UpdateSF600Flash(sFolder,Index); - Sleep(200); - boResult &= UpdateSF600Flash_FPGA(sFolder,Index); - Sleep(1000); - WriteSF600UID(dwUID,vUID[3],Index); - return boResult; + if (ReadOnBoardFlash(vUID, false, Index) == false) + return false; + dwUID = (unsigned int)vUID[0] << 16 | (unsigned int)vUID[1] << 8 | vUID[2]; + boResult &= UpdateSF600Flash(sFolder, Index); + Sleep(200); + boResult &= UpdateSF600Flash_FPGA(sFolder, Index); + Sleep(1000); + WriteSF600UID(dwUID, vUID[3], Index); + return boResult; } -bool WriteSF600UID(unsigned int dwUID, unsigned char ManuID,int Index) +bool WriteSF600UID(unsigned int dwUID, unsigned char ManuID, int Index) { - CNTRPIPE_RQ rq ; - unsigned char vBuffer[16] ; + CNTRPIPE_RQ rq; + unsigned char vBuffer[16]; - // first control packet - rq.Function = URB_FUNCTION_VENDOR_ENDPOINT ; - rq.Direction = VENDOR_DIRECTION_OUT ; - rq.Request = WRITE_EEPROM ; - rq.Value = 0; - rq.Index = 0 ; - rq.Length = 16; + // first control packet + rq.Function = URB_FUNCTION_VENDOR_ENDPOINT; + rq.Direction = VENDOR_DIRECTION_OUT; + rq.Request = WRITE_EEPROM; + rq.Value = 0; + rq.Index = 0; + rq.Length = 16; - vBuffer[0]=(unsigned char)(dwUID>>16) ; - vBuffer[1]=(unsigned char)(dwUID>>8) ; - vBuffer[2]=(unsigned char)(dwUID) ; - vBuffer[3]=ManuID ; + vBuffer[0] = (unsigned char)(dwUID >> 16); + vBuffer[1] = (unsigned char)(dwUID >> 8); + vBuffer[2] = (unsigned char)(dwUID); + vBuffer[3] = ManuID; - if(!OutCtrlRequest(&rq, vBuffer,16,Index)) - return false ; + if (!OutCtrlRequest(&rq, vBuffer, 16, Index)) + return false; - return true ; + return true; } -void EncrypFirmware(unsigned char* vBuffer,unsigned int Size,int Index) +bool EncrypFirmware(unsigned char* vBuffer, unsigned int Size, int Index) { - unsigned char vUID[16]; - unsigned int i=0; - ReadOnBoardFlash(vUID,true,Index); - for(i=0; i<16; i++) - vBuffer[i]=vBuffer[i]^vUID[i]; + unsigned char vUID[16]; + unsigned int i = 0; + if (ReadOnBoardFlash(vUID, true, Index) == false) + return false; + for (i = 0; i < 16; i++) + vBuffer[i] = vBuffer[i] ^ vUID[i]; - for(; i> 8) & 0xff); + Package[4] = ((fw_info.FirstSize >> 16) & 0xff); + Package[5] = ((fw_info.FirstSize >> 24) & 0xff); + + if (!OutCtrlRequest(&rq, Package, 6, Index)) { + free(pBuffer); + return false; + } + } else { + rq.Function = URB_FUNCTION_VENDOR_ENDPOINT; + rq.Direction = VENDOR_DIRECTION_OUT; + rq.Request = 0x1A; + rq.Value = (unsigned short)(fw_info.FirstSize & 0xffff); + rq.Index = (unsigned short)((fw_info.FirstSize >> 16) & 0xffff); + rq.Length = 0; + + if (!OutCtrlRequest(&rq, pBuffer, 0, Index)) { + free(pBuffer); + return false; + } + } - pBuffer = (unsigned char*) malloc(fw_info.FirstSize); - fseek (pFile , fw_info.FirstIndex, SEEK_SET); - lSize = fread (pBuffer,1,fw_info.FirstSize,pFile); - if(lSize != fw_info.FirstSize) - printf("Possible read length error %s\n", sFilePath); - - fclose(pFile); - - EncrypFirmware(pBuffer,fw_info.FirstSize,Index); - - if(Is_NewUSBCommand(Index)) - {//for win8.1 - rq.Function = URB_FUNCTION_VENDOR_ENDPOINT ; - rq.Direction = VENDOR_DIRECTION_OUT ; - rq.Request = 0x1A; - rq.Value = 0; - rq.Index = 0; - rq.Length = 6 ; + pagenum = (fw_info.FirstSize >> 9) + ((fw_info.FirstSize % (1 << 9)) ? 1 : 0); + dwsize = fw_info.FirstSize; - //evy 6.0.4.25 for win 8.1 with new firmware(7.0.2 up) - unsigned char Package[6]; - Package[0]=pBuffer[0]; - Package[1]=pBuffer[1]; - Package[2]=(fw_info.FirstSize & 0xff); - Package[3]=((fw_info.FirstSize >>8) & 0xff); - Package[4]=((fw_info.FirstSize >> 16) & 0xff); - Package[5]=((fw_info.FirstSize >> 24) & 0xff); - - if(!OutCtrlRequest(&rq, Package,6,Index)) - { - free(pBuffer); - return false ; - } - } - else - { - rq.Function = URB_FUNCTION_VENDOR_ENDPOINT ; - rq.Direction = VENDOR_DIRECTION_OUT ; - rq.Request = 0x1A ; - rq.Value = (unsigned short)(fw_info.FirstSize & 0xffff) ; - rq.Index = (unsigned short)((fw_info.FirstSize >> 16) & 0xffff) ; - rq.Length = 0 ; - - - if(!OutCtrlRequest(&rq, pBuffer,0,Index) ) - { - free(pBuffer); - return false ; - } - } - - pagenum=(fw_info.FirstSize>>9)+((fw_info.FirstSize%(1<<9))? 1:0); - dwsize=fw_info.FirstSize; - - for( i=0; i(vBuffer.size() & 0xffff) ; - rq.Index = 0;//static_cast((vBuffer.size() >> 16) & 0xffff) ; - rq.Length = 4 ; - - //evy 6.0.4.25 for win 8.1 with new firmware(7.0.2 up) - unsigned char Package[4]; - Package[0]=(fw_info.SecondSize & 0xff); - Package[1]=((fw_info.SecondSize>>8) & 0xff); - Package[2]=((fw_info.SecondSize >> 16) & 0xff); - Package[3]=((fw_info.SecondSize >> 24) & 0xff); - - if(!OutCtrlRequest(&rq, Package,4,Index) ) - { - printf("Error: 1\r\n"); - free(pBuffer); - return false ; - } - } - else - { - rq.Function = URB_FUNCTION_VENDOR_ENDPOINT ; - rq.Direction = VENDOR_DIRECTION_OUT ; - rq.Request = 0x1B; - rq.Value = (unsigned short)(fw_info.SecondSize & 0xffff) ; - rq.Index = (unsigned short)((fw_info.SecondSize >> 16) & 0xffff) ; - rq.Length = 0 ; - - if(!OutCtrlRequest(&rq, pBuffer,0,Index) ) - { - printf("Error: 2\r\n"); - free(pBuffer); - return false ; - } - } + for (i = 0; i < pagenum; i++) { + BulkPipeWrite((pBuffer + (i << 9)), min(512, dwsize), USB_TIMEOUT, Index); + dwsize -= 512; + } + + free(pBuffer); + return true; +} + +bool UpdateSF600Flash_FPGA(const char* sFilePath, int Index) +{ + CNTRPIPE_RQ rq; + unsigned char* pBuffer; + int pagenum = 0; + unsigned int dwsize = 0; + FW_INFO fw_info; + FILE* pFile; + size_t lSize; + int i = 0; + + pFile = fopen(sFilePath, "rb"); + if (pFile == NULL) { + printf("Can not open %s \n", sFilePath); + return false; + } - pagenum=(fw_info.SecondSize>>9)+((fw_info.SecondSize%(1<<9))? 1:0); - dwsize=fw_info.SecondSize; + fseek(pFile, 0, SEEK_SET); + lSize = fread((unsigned char*)&fw_info, 1, sizeof(FW_INFO), pFile); + if (lSize != sizeof(FW_INFO)) + printf("Possible read length error %s\n", sFilePath); - for( i=0; i(vBuffer.size() & 0xffff) ; + rq.Index = 0; // static_cast((vBuffer.size() >> 16) & 0xffff) ; + rq.Length = 4; + + // evy 6.0.4.25 for win 8.1 with new firmware(7.0.2 up) + unsigned char Package[4]; + Package[0] = (fw_info.SecondSize & 0xff); + Package[1] = ((fw_info.SecondSize >> 8) & 0xff); + Package[2] = ((fw_info.SecondSize >> 16) & 0xff); + Package[3] = ((fw_info.SecondSize >> 24) & 0xff); + + if (!OutCtrlRequest(&rq, Package, 4, Index)) { + printf("Error: 1\n"); + free(pBuffer); + return false; + } + } else { + rq.Function = URB_FUNCTION_VENDOR_ENDPOINT; + rq.Direction = VENDOR_DIRECTION_OUT; + rq.Request = 0x1B; + rq.Value = (unsigned short)(fw_info.SecondSize & 0xffff); + rq.Index = (unsigned short)((fw_info.SecondSize >> 16) & 0xffff); + rq.Length = 0; + + if (!OutCtrlRequest(&rq, pBuffer, 0, Index)) { + printf("Error: 2\n"); + free(pBuffer); + return false; + } + } + + pagenum = (fw_info.SecondSize >> 9) + ((fw_info.SecondSize % (1 << 9)) ? 1 : 0); + dwsize = fw_info.SecondSize; + + for (i = 0; i < pagenum; i++) { + BulkPipeWrite(pBuffer + (i << 9), min(512, dwsize), USB_TIMEOUT, Index); + dwsize -= 512; + } + return true; } -bool UpdateFirmware(const char* sFolder,int Index) +bool UpdateFirmware(const char* sFolder, int Index) { - bool bResult = true; - unsigned int UID=0; - unsigned char ManID=0; - // read status - if(g_bIsSF600==true) - return UpdateSF600Firmware(sFolder,Index); + bool bResult = true; + unsigned int UID = 0; + unsigned char ManID = 0; + // read status + if (g_bIsSF600 == true) + return UpdateSF600Firmware(sFolder, Index); - dediprog_set_spi_voltage(g_Vcc,Index); + dediprog_set_spi_voltage(g_Vcc, Index); - if(g_firmversion > 0x040107) // 4.1.7 - bResult &= SetSPIClockDefault(Index); + if (g_firmversion > 0x040107) // 4.1.7 + bResult &= SetSPIClockDefault(Index); - bResult &= SetVpp4IAP(true,Index); - bResult &= UnlockRASS(Index); + bResult &= SetVpp4IAP(true, Index); + bResult &= UnlockRASS(Index); - UID = ReadUID(Index); - ManID = ReadManufacturerID(Index); + UID = ReadUID(Index); + ManID = ReadManufacturerID(Index); - // erase & program sector 1 - bResult &= EraseST7Sectors(true,Index); - bResult &= ProgramSectors(sFolder, true,Index); - // erase & program sector 2 - bResult &= EraseST7Sectors(false,Index); - bResult &= ProgramSectors(sFolder, false,Index); + // erase & program sector 1 + bResult &= EraseST7Sectors(true, Index); + bResult &= ProgramSectors(sFolder, true, Index); + // erase & program sector 2 + bResult &= EraseST7Sectors(false, Index); + bResult &= ProgramSectors(sFolder, false, Index); - // calculate and read back checksum - bResult &= UpdateChkSum(Index); + // calculate and read back checksum + bResult &= UpdateChkSum(Index); - bResult &= WriteUID(UID,Index); - bResult &= WriteManufacturerID(ManID,Index); - bResult &= SetVpp4IAP(false,Index); + bResult &= WriteUID(UID, Index); + bResult &= WriteManufacturerID(ManID, Index); + bResult &= SetVpp4IAP(false, Index); - // read back checksum - bResult &= ReadMemOnST7(0xEFFE,Index); - return bResult; + // read back checksum + bResult &= ReadMemOnST7(0xEFFE, Index); + return bResult; } void SendFFSequence(int Index) { - unsigned char v[4]={0xff,0xff,0xff,0xff}; - FlashCommand_SendCommand_OutOnlyInstruction(v,4,Index); + unsigned char v[4] = { 0xff, 0xff, 0xff, 0xff }; + FlashCommand_SendCommand_OutOnlyInstruction(v, 4, Index); } - diff --git a/board.h b/board.h index f651e7a..34eecbc 100755 --- a/board.h +++ b/board.h @@ -1,9 +1,12 @@ - #pragma once #ifndef _BOARD_H #define _BOARD_H +#include +#include + +void SendFFSequence(int Index); void QueryBoard(int Index); unsigned char GetFPGAVersion(int Index); bool SetIO(unsigned char ioState, int Index); @@ -25,16 +28,11 @@ bool ProgramSectors(const char* sFilePath, bool bSect1,int Index); bool UpdateChkSum(int Index); bool WriteUID(unsigned int dwUID,int Index); bool WriteManufacturerID(unsigned char ManuID,int Index); -void EncrypFirmware(unsigned char* vBuffer,unsigned int Size,int Index); +bool EncrypFirmware(unsigned char* vBuffer, unsigned int Size, int Index); bool UpdateSF600Flash(const char* sFilePath,int Index); bool WriteSF600UID(unsigned int dwUID, unsigned char ManuID,int Index); bool UpdateSF600Flash_FPGA(const char* sFilePath,int Index); bool UpdateSF600Firmware(const char* sFolder,int Index); bool UpdateFirmware(const char* sFolder,int Index); - - - - #endif - diff --git a/dpcmd.c b/dpcmd.c index 0325049..7a54676 100755 --- a/dpcmd.c +++ b/dpcmd.c @@ -1,28 +1,21 @@ -#if 1 -#include -#include -#include -#include "Macro.h" - -#include "usbdriver.h" +#include "dpcmd.h" #include "ChipInfoDb.h" +#include "FlashCommand.h" #include "SerialFlash.h" +#include "board.h" #include "project.h" -#include +#include "usbdriver.h" +#include +#include #include -#include +#include +#include #include -#include -#include #include +#include -#include "dpcmd.h" -#include "board.h" -#include "FlashCommand.h" #define min(a,b) (a>b? b:a) -#include - extern unsigned char* pBufferForLastReadData[16]; extern unsigned char* pBufferforLoadedFile; extern unsigned int g_uiFileChecksum; @@ -47,8 +40,6 @@ unsigned int g_IO4Select=1; bool g_bEnableVpp=false; int g_StartupMode=STARTUP_APPLI_SF_1; bool g_bStatus=true; -extern void Sleep(unsigned int ms); - CHIP_INFO Chip_Info; char *l_opt_arg; @@ -329,13 +320,11 @@ int Sequence() // *** the calling order in the following block must be kept as is *** bool boResult=true; boResult &= BlankCheck(); - if(boResult==false) - { + if (boResult == false) { if(!(g_ucOperation&ERASE)) return EXCODE_FAIL_BLANK; } - if((!((g_ucOperation&BLANK)&&(boResult==true))||(!(g_ucOperation&BLANK)))) - { + if ((!((g_ucOperation & BLANK) && (boResult == true)) || (!(g_ucOperation & BLANK)))) { boResult = Erase(); if(boResult==false) return EXCODE_FAIL_ERASE; @@ -353,29 +342,22 @@ int Sequence() if(boResult==false) return EXCODE_FAIL_BATCH; - boResult &= CalChecksum(); - if(boResult==true) - { - if(g_ucOperation & CSUM) - { - if(g_uiLen==0) - { + if (boResult == true) { + if (g_ucOperation & CSUM) { + if (g_uiLen == 0) { if(g_uiDevNum==0) return CRC32(pBufferForLastReadData[0], Chip_Info.ChipSizeInByte); else return CRC32(pBufferForLastReadData[g_uiDevNum-1], Chip_Info.ChipSizeInByte); - } - else - { + } else { if(g_uiDevNum==0) return CRC32(pBufferForLastReadData[0], g_uiLen); else return CRC32(pBufferForLastReadData[g_uiDevNum-1], g_uiLen); } } - } - else + } else return EXCODE_FAIL_CHKSUM; boResult &= Verify(); @@ -385,7 +367,6 @@ int Sequence() return EXCODE_PASS; } - void print_message_function( void *ptr ) { char *message; @@ -396,11 +377,10 @@ void print_message_function( void *ptr ) void GetLogPath(char* pBuf) { memset(pBuf,0,512); - if (readlink ("/proc/self/exe", pBuf, 512) != -1) - { + if (readlink("/proc/self/exe", pBuf, 512) != -1) { if(strlen(pBuf)<(511-sizeof("/log.txt"))) strcat(pBuf,"/log.txt"); -// printf("%s\r\n",pBuf); + // printf("%s\n",pBuf); } } @@ -440,14 +420,11 @@ void WriteLog(int ErrorCode, bool Init) timeinfo = localtime (&rawtime); strftime (buffer,80,"%Y-%b-%d %T",timeinfo); - if(Init) - { + if (Init) { FileOut = fopen(g_LogPath,"wt") ; if(FileOut != NULL) fprintf(FileOut, "%s%s%s",buffer," USB communication =",Result[ErrorCode]); - } - else - { + } else { FileOut = fopen(g_LogPath,"a+t") ; if(FileOut != NULL) fprintf(FileOut, "%s%s",buffer,Result[ErrorCode]); @@ -459,28 +436,22 @@ void WriteLog(int ErrorCode, bool Init) int GetConfigVer() { - char path[512],fname[256]; char file_line_buf[512]; char test[80]; char *pch,*tok; bool bFound=0; int Ver=0; FILE* fp; /*Declare file pointer variable*/ - getExecPath(path); - if ((fp = fopen(path,"rt")) == NULL) - { - fprintf(stderr,"Error opening file: %s\n",fname); + + if ((fp = openChipInfoDb()) == NULL) return 1; - } - while(fgets(file_line_buf, 512, fp) != NULL) - { + while (fgets(file_line_buf, 512, fp) != NULL) { if(strstr(file_line_buf,"DediProgChipDatabase") && bFound==0) bFound=1; pch=strstr(file_line_buf, "Ver"); - if(bFound==1 && pch != NULL) - { + if (bFound == 1 && pch != NULL) { memset(test, '\0', 80); strcpy(test,pch+strlen("Ver")); tok = strtok(test,"\"= \t"); @@ -490,11 +461,8 @@ int GetConfigVer() } fclose(fp); return Ver; - } -extern void func(int); - int main(int argc, char *argv[]) { #if 0 //Check all USB device capability @@ -506,32 +474,24 @@ int main(int argc, char *argv[]) bool bDetect=false; bool bDevice=false; - //signal(SIGINT, sin_handler); - - printf("\nDpCmd Linux 1.11.2.%02d Engine Version:\nLast Built on May 25 2018\n\n",GetConfigVer()); //1. new feature.bug.configS + printf("\nDpCmd Linux 1.11.2.%02d Engine Version:\nLast Built on May 25 2018\n\n", GetConfigVer()); // 1. new feature.bug.configS g_ucOperation=0; GetLogPath(g_LogPath); - if(argc==1) - { + if (argc == 1) { cli_classic_usage(false); return 0; } - if(OpenUSB()==0) iExitCode=EXCODE_FAIL_USB; LeaveStandaloneMode(0); QueryBoard(0); - - - while((c = getopt_long (argc, argv, short_options, long_options, NULL)) != -1) - { - switch (c) - { + while ((c = getopt_long(argc, argv, short_options, long_options, NULL)) != -1) { + switch (c) { case '?': cli_classic_usage(true); CloseProject(); @@ -588,7 +548,10 @@ int main(int argc, char *argv[]) break; case 'l': g_uiLen=0; + if (strstr(optarg, "0x")) sscanf(optarg,"%zx",&g_uiLen); + else + sscanf(optarg, "%zu", &g_uiLen); break; case 'v': g_ucOperation |= VERIFY; @@ -651,7 +614,7 @@ int main(int argc, char *argv[]) break; case 'O': //Log file strcpy(g_LogPath,optarg); -// printf("%s\r\n",g_LogPath); + // printf("%s\n",g_LogPath); break; case 'i': g_bDisplayTimer=false; @@ -689,66 +652,49 @@ int main(int argc, char *argv[]) } } - int dev_cnt=get_usb_dev_cnt(); - if(bDetect==true) - { - if(bDevice==false) - { - for(int i=0;i0) - { - printf("\nDevice %d (SF%06d):\tdetecting chip\r\n",i+1,dwUID); - } - else - { - printf("\nDevice %d (DP%06d):\tdetecting chip\r\n",i+1,dwUID); + if ((dwUID / 600000) > 0) { + printf("\nDevice %d (SF%06d):\tdetecting chip\n", i + 1, dwUID); + } else { + printf("\nDevice %d (DP%06d):\tdetecting chip\n", i + 1, dwUID); } WriteLog(iExitCode, true); Chip_Info=GetFirstDetectionMatch(i); - if(Chip_Info.UniqueID !=0 ) - { - printf("By reading the chip ID, the chip applies to [ %s ]\r\n", Chip_Info.TypeName); - printf("%s chip size is %zd bytes.\r\n",Chip_Info.TypeName,Chip_Info.ChipSizeInByte); - } - else - { + if (Chip_Info.UniqueID != 0) { + printf("By reading the chip ID, the chip applies to [ %s ]\n", Chip_Info.TypeName); + printf("%s chip size is %zd bytes.\n", Chip_Info.TypeName, Chip_Info.ChipSizeInByte); + } else { printf("%s",msg_err_identifychip); iExitCode=EXCODE_FAIL_IDENTIFY; } } - } - else if(g_uiDevNum!=0) - { + } else if (g_uiDevNum != 0) { WriteLog(iExitCode, true); - printf("%d,\tdetecting chip\r\n",g_uiDevNum); + printf("%d,\tdetecting chip\n", g_uiDevNum); Chip_Info=GetFirstDetectionMatch(g_uiDevNum-1); - if(Chip_Info.UniqueID !=0 ) - { - printf(" \tBy reading the chip ID, the chip applies to [ %s ]\r\n", Chip_Info.TypeName); - printf(" \t%s chip size is %zd bytes.\r\n",Chip_Info.TypeName,Chip_Info.ChipSizeInByte); - } - else - { + if (Chip_Info.UniqueID != 0) { + printf(" \tBy reading the chip ID, the chip applies to [ %s ]\n", Chip_Info.TypeName); + printf(" \t%s chip size is %zd bytes.\n", Chip_Info.TypeName, Chip_Info.ChipSizeInByte); + } else { printf("%s",msg_err_identifychip); iExitCode=EXCODE_FAIL_IDENTIFY; } - } - else - { + } else { printf("The number of programmer is not defined!\n"); iExitCode=EXCODE_FAIL_IDENTIFY; } goto Exit; - } - else + } else WriteLog(iExitCode, true); - if(iExitCode != EXCODE_PASS) goto Exit; + if (iExitCode != EXCODE_PASS) + goto Exit; iExitCode=Handler(); Exit: @@ -761,8 +707,7 @@ int main(int argc, char *argv[]) void ExitProgram(void) { - for(int i=0;i0) - { + if (strlen(g_parameter_type) > 0) { int dev_cnt=get_usb_dev_cnt(); - if(g_uiDevNum==0) - { - for(int i=0;i0) - { - if(Dedi_Search_Chip_Db_ByTypeName(g_parameter_type,&Chip_Info,i)) - { - printf("Chip Type %s is applied manually.\r\n",Chip_Info.TypeName); + if (strlen(g_parameter_type) > 0) { + if (Dedi_Search_Chip_Db_ByTypeName(g_parameter_type, &Chip_Info)) { + printf("Chip Type %s is applied manually.\n", Chip_Info.TypeName); printf("%s chip size is %zd bytes.\n\n",Chip_Info.TypeName,Chip_Info.ChipSizeInByte); ProjectInitWithID(Chip_Info,i); - if(Chip_Info.Class=="N25Qxxx_Large") + if (!strcmp(Chip_Info.Class, "N25Qxxx_Large")) isSendFFsequence=true; - } - else - { - printf("Chip Type Unknow is applied manually.\r\n"); + } else { + printf("Chip Type Unknow is applied manually.\n"); return false; } - } - else - { + } else { ProjectInit(i); } } - } - else if(g_uiDevNum<=dev_cnt) - { + } else if (g_uiDevNum <= dev_cnt) { g_StartupMode=targets[g_ucTarget]; SetTargetFlash((unsigned char)targets[g_ucTarget], g_uiDevNum-1); SetSPIClock(g_uiDevNum-1); SetVcc(g_uiDevNum-1); - if(strlen(g_parameter_type)>0) - { - if(Dedi_Search_Chip_Db_ByTypeName(g_parameter_type,&Chip_Info,g_uiDevNum-1)) - { - printf("Chip Type %s is applied manually.\r\n",Chip_Info.TypeName); + if(strlen(g_parameter_type)>0){ + if (Dedi_Search_Chip_Db_ByTypeName(g_parameter_type, &Chip_Info)) { + printf("Chip Type %s is applied manually.\n", Chip_Info.TypeName); printf("%s chip size is %zd bytes.\n\n",Chip_Info.TypeName,Chip_Info.ChipSizeInByte); ProjectInitWithID(Chip_Info,g_uiDevNum-1); - } - else - { - printf("Chip Type Unknow is applied manually.\r\n"); + } else { + printf("Chip Type Unknow is applied manually.\n"); return false; } - } - else - { + } else { ProjectInit(g_uiDevNum-1); } - } - else - printf("Error: Did not find the programmer.\r\n"); - + } else + printf("Error: Did not find the programmer.\n"); return true; } @@ -1150,8 +1046,7 @@ bool InitProject(void) void CloseProject(void) { - for(int i=0;i=1800 && g_bIsSF600[Index]==true) - ; - else + if (!(g_Vcc <= 3800 && g_Vcc >= 1800 && g_bIsSF600[Index] == true)) g_Vcc= (0x10 | (g_Vcc&0x03)); } int do_loadFile(void) { - char* filename; + char* filename = NULL; if(g_ucOperation & PROGRAM) filename=g_parameter_program; else if(g_ucOperation & LOADFILE) filename=g_parameter_loadfile; - else if(g_ucOperation & BATCH) - { - switch(g_BatchIndex) - { + else if (g_ucOperation & BATCH) { + switch (g_BatchIndex) { case 1: filename=g_parameter_batch; break; @@ -1248,8 +1128,7 @@ int do_loadFile(void) filename=g_parameter_auto; break; } - } - else if(g_ucOperation & FSUM) + } else if (g_ucOperation & FSUM) filename=g_parameter_fsum; printf("%s",msg_info_loading); printf("(%s)",filename); @@ -1258,41 +1137,36 @@ int do_loadFile(void) bool ListTypes(void) { - if((g_ucOperation&LIST_TYPE)!=LIST_TYPE) return false; - Dedi_List_AllChip(); - - return true; + if ((g_ucOperation & LIST_TYPE) != LIST_TYPE) + return false; + return Dedi_List_AllChip(); } void BlinkProgrammer(void) { bool IsV5=is_BoardVersionGreaterThan_5_0_0(0); - if(IsV5==false) return; + if (IsV5 == false) + return; int dev_cnt=get_usb_dev_cnt(); - if(g_uiBlink==0) - { - for(int i=0;idev_cnt) - printf("The number of programmer is not defined!\r\n"); - else - { + printf("The number of programmer is not defined!\n"); + else { dwUID=ReadUID(g_uiDeviceID-1); if((dwUID / 600000)==0) - printf("%d,\tDP%06d\r\n",g_uiDeviceID,dwUID); + printf("%d,\tDP%06d\n", g_uiDeviceID, dwUID); else - printf("%d,\tSF%06d\r\n",g_uiDeviceID,dwUID); + printf("%d,\tSF%06d\n", g_uiDeviceID, dwUID); } } - } void do_BlankCheck(void) @@ -1341,20 +1209,19 @@ void do_Erase(void) printf("%s \n",msg_info_erasing); Run(ERASE_WHOLE_CHIP,g_uiDevNum); - Wait( msg_info_eraseOK, msg_info_erasefail) ; } void do_Program(void) { - if(!do_loadFile()) return; + if (!do_loadFile()) + return; SaveProgContextChanges(); printf("%s\n",msg_info_programming); Run(PROGRAM_CHIP,g_uiDevNum); - if( Wait( msg_info_progOK, msg_info_progfail )) - { + if (Wait(msg_info_progOK, msg_info_progfail)) { printf("\nChecksum(whole file): %08X",g_uiFileChecksum); printf("\nChecksum(Written part of file): %08X\n",CRC32(pBufferforLoadedFile, min(DownloadAddrRange.end-DownloadAddrRange.start,g_ulFileSize))); } @@ -1375,28 +1242,20 @@ void do_DisplayOrSave(void) { int dev_cnt=get_usb_dev_cnt(); - if(g_uiDevNum==0) - { - for(int icnt=0;icnt0) // sscanf(g_parameter_raw_return,"%hhu",&Length); - if(outDLen>0) + if(outDLen>0) FlashCommand_SendCommand_OneOutOneIn(vOut,i,vIn,outDLen,Index); - else + else FlashCommand_SendCommand_OutOnlyInstruction(vOut,i,Index); - if(outDLen>0) - { + if (outDLen > 0) { printf("issuing raw instruction \"%s\" returns %d bytes as required:\n",g_parameter_raw,outDLen); for(i=0; i0) - { + if (strlen(g_parameter_raw_return) > 0) { sscanf(g_parameter_raw_return,"%hhu",&Length); strcpy(retrunParameter,g_parameter_raw_return); pchR = strtok (retrunParameter,"|"); - while(pchR!=NULL) - { + while (pchR != NULL) { sscanf(pchR,"%02hhx",&pchReturn[iReturn]); iReturn++; pchR = strtok (NULL,"|"); } - if(iReturn!=i) - { + if (iReturn != i) { printf("\nError: value of --raw-require-return failed. No return value!\n\n"); memset(pchReturn,0,30); } } - for(int j=0;j0) - { + if (strlen(g_parameter_raw) > 0) { printf("\nDevice %d:\n",Index+1); do_RawInstructions(Index); - } } @@ -1612,12 +1446,9 @@ bool Program(void) bool Read(void) { - int dev_cnt=get_usb_dev_cnt(); - if(g_ucOperation&READ_TO_FILE) - { + if (g_ucOperation & READ_TO_FILE) { do_Read(); do_DisplayOrSave(); - } return g_bStatus; } @@ -1631,40 +1462,31 @@ bool Auto(void) bool Verify(void) { - if(g_ucOperation & VERIFY) do_Verify(); return g_bStatus; } - bool CalChecksum(void) { int dev_cnt = get_usb_dev_cnt(); - if(g_ucOperation & FSUM) - { + if (g_ucOperation & FSUM) { do_loadFile(); printf("\nChecksum(file): 0x%08X\n",g_uiFileChecksum); } - if(g_ucOperation & CSUM) - { + if (g_ucOperation & CSUM) { do_Read(); - if(g_uiDevNum==0) - { - for(int i=0;i timeOut) - { - printf("%0.2f\t s elapsed\r",diff.tv_sec + 0.000001 * diff.tv_usec); + if (tv.tv_sec - basetv.tv_sec > timeOut) { + timersub(&tv, &basetv, &diff); + printf("\r%0.2fs elapsed", diff.tv_sec + 0.000001 * diff.tv_usec); printf("%s",msg_err_timeout_abortion); g_is_operation_on_going=false; g_bStatus=false; - timersub(&tv, &basetv, &diff); return false; } - if(g_bDisplayTimer==true) - { - float diff_s, diff_last_s; - timersub(&tv, &tv_display_last, &diff); - diff_s = (float)diff.tv_sec + 0.000001 * diff.tv_usec; - if (diff_s > g_tv_display_delta_s) - { - /* it's been more than X seconds since the last - * time we displayed, so let's display currently - * ellapsed time since we started */ + if (g_bDisplayTimer == true) { timersub(&tv, &basetv, &diff); - printf("%0.6f\t s elapsed\r", diff.tv_sec + 0.000001 * diff.tv_usec); - fflush(stdout); - memcpy(&tv_display_last, &tv, sizeof(tv_display_last)); - } + printf("\r%0.1fs elapsed", diff.tv_sec + 0.000001 * diff.tv_usec); } } printf("\n"); if(g_uiDevNum==0)//mutiple { - for(int i=0;i + +enum ErrorCode { EXCODE_PASS, EXCODE_FAIL_USB, EXCODE_FAIL_ERASE, @@ -19,25 +20,7 @@ enum ErrorCode EXCODE_FAIL_OTHERS, }; -typedef enum -{ - BLANKCHECK_WHOLE_CHIP, - PROGRAM_CHIP, - ERASE_WHOLE_CHIP, - READ_WHOLE_CHIP, - READ_ANY_BY_PREFERENCE_CONFIGURATION, - VERIFY_CONTENT, - AUTO, - - Download2Card, - // 07.03.2009 - UPDATE_FIRMWARE, - AUTO_UPDATE_FIRMWARE, - -} OPERATION_TYPE; - - enum -{ // value dedicated by the spec +enum { // value dedicated by the spec STARTUP_APPLI_SF_1 = 0, STARTUP_APPLI_CARD = 1, STARTUP_APPLI_SF_2 = 2, @@ -47,8 +30,7 @@ typedef enum STARTUP_PREVIOUS = 0xFF } ; -enum -{ +enum { clk_24M = 0x00, clk_8M = 0x01, clk_12M = 0x02, diff --git a/parse.c b/parse.c index bdaa6c7..bd165bb 100755 --- a/parse.c +++ b/parse.c @@ -1,267 +1,255 @@ -#include +#include "ChipInfoDb.h" +#include "project.h" +#include +#include #include #include -#include - -#include "Macro.h" -#include "ChipInfoDb.h" +#define testbufsize 256 #define linebufsize 512 -#define filebufsize 1024*1024 -#define min(a,b) (((a)>(b))? (b):(a)) -#define max(a,b) (((a) > (b)) ? (a) : (b)) +#define filebufsize 1024 * 1024 +#define min(a, b) (((a) > (b)) ? (b) : (a)) +#define max(a, b) (((a) > (b)) ? (a) : (b)) -void getExecPath(char* Path) +FILE* openChipInfoDb(void) { - memset(Path,0,512); - if (readlink ("/proc/self/exe", Path, 512) != -1) - { -// printf("%s\r\n",Path); - dirname (Path); -// printf("%s\r\n",Path); - strcat(Path,"/ChipInfoDb.dedicfg"); - // printf("%s\r\n",Path); -//// - } + FILE* fp = NULL; + char Path[linebufsize]; + + memset(Path, 0, linebufsize); + if (readlink("/proc/self/exe", Path, 512) != -1) { + dirname(Path); + strcat(Path, "/ChipInfoDb.dedicfg"); + // printf("%s\n",Path); + if ((fp = fopen(Path, "rt")) == NULL) { + // ChipInfoDb.dedicfg not in program directory + dirname(Path); + dirname(Path); + strcat(Path, "/share/DediProg/ChipInfoDb.dedicfg"); + // printf("%s\n",Path); + if ((fp = fopen(Path, "rt")) == NULL) + fprintf(stderr, "Error opening file: %s\n", Path); + } + } + return fp; } -long fsize(FILE *fp){ - long prev=ftell(fp); +long fsize(FILE* fp) +{ + long prev = ftell(fp); fseek(fp, 0L, SEEK_END); - long sz=ftell(fp); - fseek(fp,prev,SEEK_SET); //go back to where we were + long sz = ftell(fp); + fseek(fp, prev, SEEK_SET); // go back to where we were return sz; } - -int Dedi_Search_Chip_Db(long RDIDCommand, long UniqueID, CHIP_INFO *Chip_Info, int search_all) +int Dedi_Search_Chip_Db(long RDIDCommand, + long UniqueID, + CHIP_INFO* Chip_Info, + int search_all) { - FILE* fp; /*Declare file pointer variable*/ - int found_flag = 0; - char file_line_buf[linebufsize], *tok, fname[256], *file_buf, test[256]; - char* pch; - long sz=0; - char Path[512]; - getExecPath(Path); -// printf("%s\r\n",Path); - /*If file doesn't exist or filetype isn't allowed exit and*/ - /*error message & return (1) control to the OS*/ - if ((fp = fopen(Path,"rt")) == NULL) - { - fprintf(stderr,"Error opening file: %s\n",fname); - return 1; - } - sz=fsize(fp); - file_buf = (char*)malloc(sz); + FILE* fp; /*Declare file pointer variable*/ + int found_flag = 0; + char file_line_buf[linebufsize], *tok, *file_buf, test[testbufsize]; + char* pch; + long sz = 0; + + if ((fp = openChipInfoDb()) == NULL) + return 1; + sz = fsize(fp); + file_buf = (char*)malloc(sz); - memset(file_buf, '\0', sz); - /*Read into the buffer contents within thr file stream*/ + memset(file_buf, '\0', sz); + /*Read into the buffer contents within thr file stream*/ - while(fgets(file_line_buf, linebufsize, fp) != NULL) - { - pch=strstr(file_line_buf,"TypeName"); - if(pch!= NULL) - { - if(found_flag==1) - break; - memset(test, '\0', 100); - strcpy(test,pch+strlen("TypeName")); - tok = strtok(test,"\"= \t"); - Chip_Info->TypeName[0] = '\0'; - Chip_Info->Class[0] = '\0'; - Chip_Info->UniqueID = 0; - Chip_Info->Description[0] = '\0'; - Chip_Info->Manufacturer[0] = '\0'; - Chip_Info->ManufactureUrl[0] = '\0'; - Chip_Info->ProgramIOMethod[0] = '\0'; - Chip_Info->Voltage[0] = '\0'; - Chip_Info->Clock[0] = '\0'; - Chip_Info->ManufactureID = 0; - Chip_Info->JedecDeviceID = 0; - Chip_Info->AlternativeID = 0; - Chip_Info->ChipSizeInByte = 0; - Chip_Info->SectorSizeInByte = 0; - Chip_Info->BlockSizeInByte = 0; - Chip_Info->PageSizeInByte = 0; - Chip_Info->AddrWidth = 0; - Chip_Info->IDNumber = 0; - Chip_Info->RDIDCommand = 0; - Chip_Info->MaxErasableSegmentInByte=0; - Chip_Info->DualID=false;; - Chip_Info->VppSupport=0; - Chip_Info->MXIC_WPmode=false; - Chip_Info->Timeout=0; - // end of struct init - strcpy(Chip_Info->TypeName,tok); - continue; - } + while (fgets(file_line_buf, linebufsize, fp) != NULL) { + pch = strstr(file_line_buf, "TypeName"); + if (pch != NULL) { + if (found_flag == 1) + break; + memset(test, '\0', testbufsize); + strcpy(test, pch + strlen("TypeName")); + tok = strtok(test, "\"= \t"); + Chip_Info->TypeName[0] = '\0'; + Chip_Info->Class[0] = '\0'; + Chip_Info->UniqueID = 0; + Chip_Info->Description[0] = '\0'; + Chip_Info->Manufacturer[0] = '\0'; + Chip_Info->ManufactureUrl[0] = '\0'; + Chip_Info->ProgramIOMethod[0] = '\0'; + Chip_Info->Voltage[0] = '\0'; + Chip_Info->Clock[0] = '\0'; + Chip_Info->ManufactureID = 0; + Chip_Info->JedecDeviceID = 0; + Chip_Info->AlternativeID = 0; + Chip_Info->ChipSizeInByte = 0; + Chip_Info->SectorSizeInByte = 0; + Chip_Info->BlockSizeInByte = 0; + Chip_Info->PageSizeInByte = 0; + Chip_Info->AddrWidth = 0; + Chip_Info->IDNumber = 0; + Chip_Info->RDIDCommand = 0; + Chip_Info->MaxErasableSegmentInByte = 0; + Chip_Info->DualID = false; + Chip_Info->VppSupport = 0; + Chip_Info->MXIC_WPmode = false; + Chip_Info->Timeout = 0; + // end of struct init + strcpy(Chip_Info->TypeName, tok); + continue; + } - pch=strstr(file_line_buf,"Class"); - if(pch != NULL) - { - memset(test, '\0', 80); - strcpy(test,pch+strlen("Class")); - tok = strtok(test,"\"= \t"); -// printf("Class = %s\n",tok); - strcpy(Chip_Info->Class,tok); - continue; - } - pch=strstr(file_line_buf,"UniqueID"); - if(pch != NULL) - { - memset(test, '\0', 80); - strcpy(test,pch+strlen("UniqueID")); - tok = strtok(test,"\"= \t"); -// printf("UniqueID = 0x%lx\n",strtol(tok,NULL,16)); - Chip_Info->UniqueID = strtol(tok,NULL,16); - if((UniqueID == Chip_Info->UniqueID)) - found_flag = 1; - continue; - } - pch=strstr(file_line_buf,"Manufacturer"); - if(pch != NULL) - { - memset(test, '\0', 80); - strcpy(test,pch+strlen("Manufacturer")); - tok = strtok(test,"\"= \t"); - //printf("Manufacturer = %s\n",tok); - strcpy(Chip_Info->Manufacturer,tok); - continue; - } - pch=strstr(file_line_buf,"Voltage"); - if(pch != NULL) - { - memset(test, '\0', 80); - strcpy(test,pch+strlen("Voltage")); - tok = strtok(test,"\"= \t"); - //printf("Voltage = %s\n",tok); - if(strcmp(tok, "3.3V") == 0) - Chip_Info->VoltageInMv = 3300; - else if(strcmp(tok, "2.5V") == 0) - Chip_Info->VoltageInMv = 2500; - else if(strcmp(tok, "1.8V") == 0) - Chip_Info->VoltageInMv = 1800; - else - Chip_Info->VoltageInMv = 3300; - continue; - } - pch=strstr(file_line_buf,"JedecDeviceID"); - if(pch != NULL) - { - memset(test, '\0', 80); - strcpy(test,pch+strlen("JedecDeviceID")); - tok = strtok(test,"\"= \t"); - //printf("JedecDeviceID = 0x%lx\n",strtol(tok,NULL,16)); - Chip_Info->JedecDeviceID = strtol(tok,NULL,16); - if((UniqueID == Chip_Info->JedecDeviceID)) - found_flag = 1; - continue; - } - pch=strstr(file_line_buf,"ChipSizeInKByte"); - if(pch != NULL ) - { - memset(test, '\0', 80); - strcpy(test,pch+strlen("ChipSizeInKByte")); - tok = strtok(test,"\"= \t"); - //printf("ChipSizeInKByte = %ld\n",strtol(tok,NULL,10)); - Chip_Info->ChipSizeInByte = strtol(tok,NULL,10)*1024; - continue; - } - pch=strstr(file_line_buf,"SectorSizeInByte"); - if(pch != NULL ) - { - memset(test, '\0', 80); - strcpy(test,pch+strlen("SectorSizeInByte")); - tok = strtok(test,"\"= \t"); - //printf("SectorSizeInByte = %ld\n",strtol(tok,NULL,10)); - Chip_Info->SectorSizeInByte = strtol(tok,NULL,10); - continue; - } - pch=strstr(file_line_buf,"BlockSizeInByte"); - if(pch != NULL ) - { - memset(test, '\0', 80); - strcpy(test,pch+strlen("BlockSizeInByte")); - tok = strtok(test,"\"= \t"); - //printf("BlockSizeInByte = %ld\n",strtol(tok,NULL,10)); - Chip_Info->BlockSizeInByte = strtol(tok,NULL,10); - continue; - } - pch=strstr(file_line_buf,"PageSizeInByte"); - if(pch != NULL ) - { - memset(test, '\0', 80); - strcpy(test,pch+strlen("PageSizeInByte")); - tok = strtok(test,"\"= \t"); - //printf("PageSizeInByte = %ld\n",strtol(tok,NULL,10)); - Chip_Info->PageSizeInByte = strtol(tok,NULL,10); - continue; - } - pch=strstr(file_line_buf,"AddrWidth"); - if(pch != NULL ) - { - memset(test, '\0', 80); - strcpy(test,pch+strlen("AddrWidth")); - tok = strtok(test,"\"= \t"); - //printf("AddrWidth = %ld\n",strtol(tok,NULL,10)); - Chip_Info->AddrWidth = strtol(tok,NULL,10); - continue; - } - pch=strstr(file_line_buf,"IDNumber"); - if(pch != NULL ) - { - memset(test, '\0', 80); - strcpy(test,pch+strlen("IDNumber")); - tok = strtok(test,"\"= \t"); - //printf("IDNumber = %ld\n",strtol(tok,NULL,10)); - Chip_Info->IDNumber = strtol(tok,NULL,10); - continue; - } - pch=strstr(file_line_buf,"VppSupport"); - if(pch != NULL ) - { - memset(test, '\0', 80); - strcpy(test,pch+strlen("VppSupport")); - tok = strtok(test,"\"= \t"); - //printf("IDNumber = %ld\n",strtol(tok,NULL,10)); - Chip_Info->VppSupport = strtol(tok,NULL,10); - continue; - } - pch=strstr(file_line_buf,"RDIDCommand"); - if(pch != NULL ) - { - memset(test, '\0', 80); - strcpy(test,pch+strlen("RDIDCommand")); - tok = strtok(test,"\"= \t"); - Chip_Info->RDIDCommand = strtol(tok,NULL,16); - continue; - } - pch=strstr(file_line_buf,"Timeout"); - if(pch != NULL ) - { - memset(test, '\0', 80); - strcpy(test,pch+strlen("Timeout")); - tok = strtok(test,"\"= \t"); - Chip_Info->Timeout= strtol(tok,NULL,16); - continue; - } - pch=strstr(file_line_buf,"MXIC_WPmode"); - if(pch != NULL ) - { - memset(test, '\0', 80); - strcpy(test,pch+strlen("MXIC_WPmode")); - tok = strtok(test,"\"= \t"); - if(strstr(tok,"true") != NULL) - Chip_Info->MXIC_WPmode=true; - else - Chip_Info->MXIC_WPmode=false; - //starting checking input data - } + pch = strstr(file_line_buf, "Class"); + if (pch != NULL) { + memset(test, '\0', testbufsize); + strcpy(test, pch + strlen("Class")); + tok = strtok(test, "\"= \t"); + // printf("Class = %s\n",tok); + strcpy(Chip_Info->Class, tok); + continue; + } + pch = strstr(file_line_buf, "UniqueID"); + if (pch != NULL) { + memset(test, '\0', testbufsize); + strcpy(test, pch + strlen("UniqueID")); + tok = strtok(test, "\"= \t"); + // printf("UniqueID = 0x%lx\n",strtol(tok,NULL,16)); + Chip_Info->UniqueID = strtol(tok, NULL, 16); + if ((UniqueID == Chip_Info->UniqueID)) + found_flag = 1; + continue; + } + pch = strstr(file_line_buf, "Manufacturer"); + if (pch != NULL) { + memset(test, '\0', testbufsize); + strcpy(test, pch + strlen("Manufacturer")); + tok = strtok(test, "\"= \t"); + // printf("Manufacturer = %s\n",tok); + strcpy(Chip_Info->Manufacturer, tok); + continue; + } + pch = strstr(file_line_buf, "Voltage"); + if (pch != NULL) { + memset(test, '\0', testbufsize); + strcpy(test, pch + strlen("Voltage")); + tok = strtok(test, "\"= \t"); + // printf("Voltage = %s\n",tok); + if (strcmp(tok, "3.3V") == 0) + Chip_Info->VoltageInMv = 3300; + else if (strcmp(tok, "2.5V") == 0) + Chip_Info->VoltageInMv = 2500; + else if (strcmp(tok, "1.8V") == 0) + Chip_Info->VoltageInMv = 1800; + else + Chip_Info->VoltageInMv = 3300; + continue; + } + pch = strstr(file_line_buf, "JedecDeviceID"); + if (pch != NULL) { + memset(test, '\0', testbufsize); + strcpy(test, pch + strlen("JedecDeviceID")); + tok = strtok(test, "\"= \t"); + // printf("JedecDeviceID = 0x%lx\n",strtol(tok,NULL,16)); + Chip_Info->JedecDeviceID = strtol(tok, NULL, 16); + if ((UniqueID == Chip_Info->JedecDeviceID)) + found_flag = 1; + continue; + } + pch = strstr(file_line_buf, "ChipSizeInKByte"); + if (pch != NULL) { + memset(test, '\0', testbufsize); + strcpy(test, pch + strlen("ChipSizeInKByte")); + tok = strtok(test, "\"= \t"); + // printf("ChipSizeInKByte = %ld\n",strtol(tok,NULL,10)); + Chip_Info->ChipSizeInByte = strtol(tok, NULL, 10) * 1024; + continue; + } + pch = strstr(file_line_buf, "SectorSizeInByte"); + if (pch != NULL) { + memset(test, '\0', testbufsize); + strcpy(test, pch + strlen("SectorSizeInByte")); + tok = strtok(test, "\"= \t"); + // printf("SectorSizeInByte = %ld\n",strtol(tok,NULL,10)); + Chip_Info->SectorSizeInByte = strtol(tok, NULL, 10); + continue; + } + pch = strstr(file_line_buf, "BlockSizeInByte"); + if (pch != NULL) { + memset(test, '\0', testbufsize); + strcpy(test, pch + strlen("BlockSizeInByte")); + tok = strtok(test, "\"= \t"); + // printf("BlockSizeInByte = %ld\n",strtol(tok,NULL,10)); + Chip_Info->BlockSizeInByte = strtol(tok, NULL, 10); + continue; + } + pch = strstr(file_line_buf, "PageSizeInByte"); + if (pch != NULL) { + memset(test, '\0', testbufsize); + strcpy(test, pch + strlen("PageSizeInByte")); + tok = strtok(test, "\"= \t"); + // printf("PageSizeInByte = %ld\n",strtol(tok,NULL,10)); + Chip_Info->PageSizeInByte = strtol(tok, NULL, 10); + continue; + } + pch = strstr(file_line_buf, "AddrWidth"); + if (pch != NULL) { + memset(test, '\0', testbufsize); + strcpy(test, pch + strlen("AddrWidth")); + tok = strtok(test, "\"= \t"); + // printf("AddrWidth = %ld\n",strtol(tok,NULL,10)); + Chip_Info->AddrWidth = strtol(tok, NULL, 10); + continue; + } + pch = strstr(file_line_buf, "IDNumber"); + if (pch != NULL) { + memset(test, '\0', testbufsize); + strcpy(test, pch + strlen("IDNumber")); + tok = strtok(test, "\"= \t"); + // printf("IDNumber = %ld\n",strtol(tok,NULL,10)); + Chip_Info->IDNumber = strtol(tok, NULL, 10); + continue; + } + pch = strstr(file_line_buf, "VppSupport"); + if (pch != NULL) { + memset(test, '\0', testbufsize); + strcpy(test, pch + strlen("VppSupport")); + tok = strtok(test, "\"= \t"); + // printf("IDNumber = %ld\n",strtol(tok,NULL,10)); + Chip_Info->VppSupport = strtol(tok, NULL, 10); + continue; + } + pch = strstr(file_line_buf, "RDIDCommand"); + if (pch != NULL) { + memset(test, '\0', testbufsize); + strcpy(test, pch + strlen("RDIDCommand")); + tok = strtok(test, "\"= \t"); + Chip_Info->RDIDCommand = strtol(tok, NULL, 16); + continue; + } + pch = strstr(file_line_buf, "Timeout"); + if (pch != NULL) { + memset(test, '\0', testbufsize); + strcpy(test, pch + strlen("Timeout")); + tok = strtok(test, "\"= \t"); + Chip_Info->Timeout = strtol(tok, NULL, 16); + continue; + } + pch = strstr(file_line_buf, "MXIC_WPmode"); + if (pch != NULL) { + memset(test, '\0', testbufsize); + strcpy(test, pch + strlen("MXIC_WPmode")); + tok = strtok(test, "\"= \t"); + if (strstr(tok, "true") != NULL) + Chip_Info->MXIC_WPmode = true; + else + Chip_Info->MXIC_WPmode = false; + // starting checking input data + } #if 0 pch=strstr(file_line_buf,"Description="); if( pch != NULL) { - memset(test, '\0', 80); + memset(test, '\0', testbufsize); strcpy(test,pch+strlen("Description=")); tok = strtok(test,"\""); // printf("Description = %s\n",tok); @@ -271,7 +259,7 @@ int Dedi_Search_Chip_Db(long RDIDCommand, long UniqueID, CHIP_INFO *Chip_Info, i pch=strstr(file_line_buf,"ManufactureUrl="); if(pch != NULL) { - memset(test, '\0', 80); + memset(test, '\0', testbufsize); strcpy(test,pch+strlen("ManufactureUrl=")); tok = strtok(test,"\""); //printf("ManufactureUrl = %s\n",tok); @@ -281,7 +269,7 @@ int Dedi_Search_Chip_Db(long RDIDCommand, long UniqueID, CHIP_INFO *Chip_Info, i pch=strstr(file_line_buf,"Clock="); if(pch != NULL) { - memset(test, '\0', 80); + memset(test, '\0', testbufsize); strcpy(test,pch+strlen("Clock=")); tok = strtok(test,"\""); //printf("Clock = %s\n",tok); @@ -291,7 +279,7 @@ int Dedi_Search_Chip_Db(long RDIDCommand, long UniqueID, CHIP_INFO *Chip_Info, i pch=strstr(file_line_buf,"ManufactureID="); if(pch != NULL) { - memset(test, '\0', 80); + memset(test, '\0', ); strcpy(test,pch+strlen("ManufactureID=")); tok = strtok(test,"\""); printf("ManufactureID = 0x%lx\n",strtol(tok,NULL,16)); @@ -300,7 +288,7 @@ int Dedi_Search_Chip_Db(long RDIDCommand, long UniqueID, CHIP_INFO *Chip_Info, i pch=strstr(file_line_buf,"DeviceID="); if(pch != NULL) { - memset(test, '\0', 80); + memset(test, '\0', testbufsize); strcpy(test,pch+strlen("DeviceID=")); tok = strtok(test,"\""); //printf("DeviceID = 0x%lx\n",strtol(tok,NULL,16)); @@ -308,252 +296,228 @@ int Dedi_Search_Chip_Db(long RDIDCommand, long UniqueID, CHIP_INFO *Chip_Info, i } #endif - }/*Continue until EOF is encoutered*/ - fclose(fp); /*Close file*/ - Chip_Info->MaxErasableSegmentInByte = max(Chip_Info->SectorSizeInByte, Chip_Info->BlockSizeInByte); - free(file_buf); - return found_flag; /*Executed without errors*/ -}/*End main*/ + } /*Continue until EOF is encoutered*/ + fclose(fp); /*Close file*/ + Chip_Info->MaxErasableSegmentInByte = max(Chip_Info->SectorSizeInByte, Chip_Info->BlockSizeInByte); + free(file_buf); + return found_flag; /*Executed without errors*/ +} /*End main*/ -int Dedi_Search_Chip_Db_ByTypeName(char* TypeName, CHIP_INFO *Chip_Info, int Index) +int Dedi_Search_Chip_Db_ByTypeName(char* TypeName, CHIP_INFO* Chip_Info) { - FILE* fp; /*Declare file pointer variable*/ - int found_flag = 0,i; - char file_line_buf[linebufsize], *tok, fname[256], *file_buf, test[256]; - char* pch; - long sz=0; - char Type[256]={0}; - char Path[512]; - getExecPath(Path); + FILE* fp; /*Declare file pointer variable*/ + int found_flag = 0, i; + char file_line_buf[linebufsize], *tok, *file_buf, test[testbufsize]; + char* pch; + long sz = 0; + char Type[256] = { 0 }; - for(i=0; iTypeName[0] = '\0'; - Chip_Info->Class[0] = '\0'; - Chip_Info->UniqueID = 0; - Chip_Info->Description[0] = '\0'; - Chip_Info->Manufacturer[0] = '\0'; - Chip_Info->ManufactureUrl[0] = '\0'; - Chip_Info->ProgramIOMethod[0] = '\0'; - Chip_Info->Voltage[0] = '\0'; - Chip_Info->Clock[0] = '\0'; - Chip_Info->ManufactureID = 0; - Chip_Info->JedecDeviceID = 0; - Chip_Info->AlternativeID = 0; - Chip_Info->ChipSizeInByte = 0; - Chip_Info->SectorSizeInByte = 0; - Chip_Info->BlockSizeInByte = 0; - Chip_Info->PageSizeInByte = 0; - Chip_Info->AddrWidth = 0; - Chip_Info->IDNumber = 0; - Chip_Info->RDIDCommand = 0; - Chip_Info->MaxErasableSegmentInByte=0; - Chip_Info->DualID=false;; - Chip_Info->VppSupport=0; - Chip_Info->MXIC_WPmode=false; - Chip_Info->Timeout=0; - // end of struct init - strcpy(Chip_Info->TypeName,tok); - } - else - found_flag=0; - } + sz = fsize(fp); + file_buf = (char*)malloc(sz); + memset(file_buf, '\0', sz); + // data_temp = file_buf; + /*Read into the buffer contents within thr file stream*/ + while (fgets(file_line_buf, linebufsize, fp) != NULL) { + pch = strstr(file_line_buf, "TypeName"); + if (pch != NULL) { + if (found_flag == 1) + break; + memset(test, '\0', testbufsize); + strcpy(test, pch + strlen("TypeName")); + tok = strtok(test, "\"= \t"); + strcpy(Type, tok); + for (i = 0; i < strlen(Type); i++) + Type[i] = toupper(tok[i]); - if(found_flag==0) - continue; + if (strcmp(Type, TypeName) == 0) { + found_flag = 1; + Chip_Info->TypeName[0] = '\0'; + Chip_Info->Class[0] = '\0'; + Chip_Info->UniqueID = 0; + Chip_Info->Description[0] = '\0'; + Chip_Info->Manufacturer[0] = '\0'; + Chip_Info->ManufactureUrl[0] = '\0'; + Chip_Info->ProgramIOMethod[0] = '\0'; + Chip_Info->Voltage[0] = '\0'; + Chip_Info->Clock[0] = '\0'; + Chip_Info->ManufactureID = 0; + Chip_Info->JedecDeviceID = 0; + Chip_Info->AlternativeID = 0; + Chip_Info->ChipSizeInByte = 0; + Chip_Info->SectorSizeInByte = 0; + Chip_Info->BlockSizeInByte = 0; + Chip_Info->PageSizeInByte = 0; + Chip_Info->AddrWidth = 0; + Chip_Info->IDNumber = 0; + Chip_Info->RDIDCommand = 0; + Chip_Info->MaxErasableSegmentInByte = 0; + Chip_Info->DualID = false; + Chip_Info->VppSupport = 0; + Chip_Info->MXIC_WPmode = false; + Chip_Info->Timeout = 0; + // end of struct init + strcpy(Chip_Info->TypeName, tok); + } else + found_flag = 0; + } - pch=strstr(file_line_buf,"Class"); - if(pch != NULL) - { - memset(test, '\0', 80); - strcpy(test,pch+strlen("Class")); - tok = strtok(test,"\"= \t"); - //printf("Class = %s\n",tok); - strcpy(Chip_Info->Class,tok); - continue; - } - pch=strstr(file_line_buf,"UniqueID"); - if(pch != NULL) - { - memset(test, '\0', 80); - strcpy(test,pch+strlen("UniqueID")); - tok = strtok(test,"\"= \t"); - //printf("UniqueID = 0x%lx\n",strtol(tok,NULL,16)); - Chip_Info->UniqueID = strtol(tok,NULL,16); - continue; - } - pch=strstr(file_line_buf,"Manufacturer"); - if(pch != NULL) - { - memset(test, '\0', 80); - strcpy(test,pch+strlen("Manufacturer")); - tok = strtok(test,"\"= \t"); - //printf("Manufacturer = %s\n",tok); - strcpy(Chip_Info->Manufacturer,tok); - continue; - } - pch=strstr(file_line_buf,"Voltage"); - if(pch != NULL) - { - memset(test, '\0', 80); - strcpy(test,pch+strlen("Voltage")); - tok = strtok(test,"\"= \t"); - //printf("Voltage = %s\n",tok); - if(strcmp(tok, "3.3V") == 0) - Chip_Info->VoltageInMv = 3300; - else if(strcmp(tok, "2.5V") == 0) - Chip_Info->VoltageInMv = 2500; - else if(strcmp(tok, "1.8V") == 0) - Chip_Info->VoltageInMv = 1800; - else - Chip_Info->VoltageInMv = 3300; - continue; - } - pch=strstr(file_line_buf,"JedecDeviceID"); - if(pch != NULL) - { - memset(test, '\0', 80); - strcpy(test,pch+strlen("JedecDeviceID")); - tok = strtok(test,"\"= \t"); - //printf("JedecDeviceID = 0x%lx\n",strtol(tok,NULL,16)); - Chip_Info->JedecDeviceID = strtol(tok,NULL,16); - continue; - } - pch=strstr(file_line_buf,"ChipSizeInKByte"); - if(pch != NULL ) - { - memset(test, '\0', 80); - strcpy(test,pch+strlen("ChipSizeInKByte")); - tok = strtok(test,"\"= \t"); - //printf("ChipSizeInKByte = %ld\n",strtol(tok,NULL,10)); - Chip_Info->ChipSizeInByte = strtol(tok,NULL,10)*1024; - continue; - } - pch=strstr(file_line_buf,"SectorSizeInByte"); - if(pch != NULL ) - { - memset(test, '\0', 80); - strcpy(test,pch+strlen("SectorSizeInByte")); - tok = strtok(test,"\"= \t"); - //printf("SectorSizeInByte = %ld\n",strtol(tok,NULL,10)); - Chip_Info->SectorSizeInByte = strtol(tok,NULL,10); - continue; - } - pch=strstr(file_line_buf,"BlockSizeInByte"); - if(pch != NULL ) - { - memset(test, '\0', 80); - strcpy(test,pch+strlen("BlockSizeInByte")); - tok = strtok(test,"\"= \t"); - //printf("BlockSizeInByte = %ld\n",strtol(tok,NULL,10)); - Chip_Info->BlockSizeInByte = strtol(tok,NULL,10); - continue; - } - pch=strstr(file_line_buf,"PageSizeInByte"); - if(pch != NULL ) - { - memset(test, '\0', 80); - strcpy(test,pch+strlen("PageSizeInByte")); - tok = strtok(test,"\"= \t"); - //printf("PageSizeInByte = %ld\n",strtol(tok,NULL,10)); - Chip_Info->PageSizeInByte = strtol(tok,NULL,10); - continue; - } - pch=strstr(file_line_buf,"AddrWidth"); - if(pch != NULL ) - { - memset(test, '\0', 80); - strcpy(test,pch+strlen("AddrWidth")); - tok = strtok(test,"\"= \t"); - //printf("AddrWidth = %ld\n",strtol(tok,NULL,10)); - Chip_Info->AddrWidth = strtol(tok,NULL,10); - continue; - } - pch=strstr(file_line_buf,"IDNumber"); - if(pch != NULL ) - { - memset(test, '\0', 80); - strcpy(test,pch+strlen("IDNumber")); - tok = strtok(test,"\"= \t"); - //printf("IDNumber = %ld\n",strtol(tok,NULL,10)); - Chip_Info->IDNumber = strtol(tok,NULL,10); - continue; - } - pch=strstr(file_line_buf,"VppSupport"); - if(pch != NULL ) - { - memset(test, '\0', 80); - strcpy(test,pch+strlen("VppSupport")); - tok = strtok(test,"\"= \t"); - //printf("IDNumber = %ld\n",strtol(tok,NULL,10)); - Chip_Info->VppSupport = strtol(tok,NULL,10); - continue; - } - pch=strstr(file_line_buf,"RDIDCommand"); - if(pch != NULL ) - { - memset(test, '\0', 80); - strcpy(test,pch+strlen("RDIDCommand")); - tok = strtok(test,"\"= \t"); - Chip_Info->RDIDCommand = strtol(tok,NULL,16); - continue; - } - pch=strstr(file_line_buf,"Timeout"); - if(pch != NULL ) - { - memset(test, '\0', 80); - strcpy(test,pch+strlen("Timeout")); - tok = strtok(test,"\"= \t"); - Chip_Info->Timeout= strtol(tok,NULL,16); - continue; - } - pch=strstr(file_line_buf,"MXIC_WPmode"); - if(pch != NULL ) - { - memset(test, '\0', 80); - strcpy(test,pch+strlen("MXIC_WPmode")); - tok = strtok(test,"\"= \t"); - if(strstr(tok,"true") != NULL) - Chip_Info->MXIC_WPmode=true; - else - Chip_Info->MXIC_WPmode=false; - //starting checking input data - continue; - } + if (found_flag == 0) + continue; + + pch = strstr(file_line_buf, "Class"); + if (pch != NULL) { + memset(test, '\0', testbufsize); + strcpy(test, pch + strlen("Class")); + tok = strtok(test, "\"= \t"); + // printf("Class = %s\n",tok); + strcpy(Chip_Info->Class, tok); + continue; + } + pch = strstr(file_line_buf, "UniqueID"); + if (pch != NULL) { + memset(test, '\0', testbufsize); + strcpy(test, pch + strlen("UniqueID")); + tok = strtok(test, "\"= \t"); + // printf("UniqueID = 0x%lx\n",strtol(tok,NULL,16)); + Chip_Info->UniqueID = strtol(tok, NULL, 16); + continue; + } + pch = strstr(file_line_buf, "Manufacturer"); + if (pch != NULL) { + memset(test, '\0', testbufsize); + strcpy(test, pch + strlen("Manufacturer")); + tok = strtok(test, "\"= \t"); + // printf("Manufacturer = %s\n",tok); + strcpy(Chip_Info->Manufacturer, tok); + continue; + } + pch = strstr(file_line_buf, "Voltage"); + if (pch != NULL) { + memset(test, '\0', testbufsize); + strcpy(test, pch + strlen("Voltage")); + tok = strtok(test, "\"= \t"); + // printf("Voltage = %s\n",tok); + if (strcmp(tok, "3.3V") == 0) + Chip_Info->VoltageInMv = 3300; + else if (strcmp(tok, "2.5V") == 0) + Chip_Info->VoltageInMv = 2500; + else if (strcmp(tok, "1.8V") == 0) + Chip_Info->VoltageInMv = 1800; + else + Chip_Info->VoltageInMv = 3300; + continue; + } + pch = strstr(file_line_buf, "JedecDeviceID"); + if (pch != NULL) { + memset(test, '\0', testbufsize); + strcpy(test, pch + strlen("JedecDeviceID")); + tok = strtok(test, "\"= \t"); + // printf("JedecDeviceID = 0x%lx\n",strtol(tok,NULL,16)); + Chip_Info->JedecDeviceID = strtol(tok, NULL, 16); + continue; + } + pch = strstr(file_line_buf, "ChipSizeInKByte"); + if (pch != NULL) { + memset(test, '\0', testbufsize); + strcpy(test, pch + strlen("ChipSizeInKByte")); + tok = strtok(test, "\"= \t"); + // printf("ChipSizeInKByte = %ld\n",strtol(tok,NULL,10)); + Chip_Info->ChipSizeInByte = strtol(tok, NULL, 10) * 1024; + continue; + } + pch = strstr(file_line_buf, "SectorSizeInByte"); + if (pch != NULL) { + memset(test, '\0', testbufsize); + strcpy(test, pch + strlen("SectorSizeInByte")); + tok = strtok(test, "\"= \t"); + // printf("SectorSizeInByte = %ld\n",strtol(tok,NULL,10)); + Chip_Info->SectorSizeInByte = strtol(tok, NULL, 10); + continue; + } + pch = strstr(file_line_buf, "BlockSizeInByte"); + if (pch != NULL) { + memset(test, '\0', testbufsize); + strcpy(test, pch + strlen("BlockSizeInByte")); + tok = strtok(test, "\"= \t"); + // printf("BlockSizeInByte = %ld\n",strtol(tok,NULL,10)); + Chip_Info->BlockSizeInByte = strtol(tok, NULL, 10); + continue; + } + pch = strstr(file_line_buf, "PageSizeInByte"); + if (pch != NULL) { + memset(test, '\0', testbufsize); + strcpy(test, pch + strlen("PageSizeInByte")); + tok = strtok(test, "\"= \t"); + // printf("PageSizeInByte = %ld\n",strtol(tok,NULL,10)); + Chip_Info->PageSizeInByte = strtol(tok, NULL, 10); + continue; + } + pch = strstr(file_line_buf, "AddrWidth"); + if (pch != NULL) { + memset(test, '\0', testbufsize); + strcpy(test, pch + strlen("AddrWidth")); + tok = strtok(test, "\"= \t"); + // printf("AddrWidth = %ld\n",strtol(tok,NULL,10)); + Chip_Info->AddrWidth = strtol(tok, NULL, 10); + continue; + } + pch = strstr(file_line_buf, "IDNumber"); + if (pch != NULL) { + memset(test, '\0', testbufsize); + strcpy(test, pch + strlen("IDNumber")); + tok = strtok(test, "\"= \t"); + // printf("IDNumber = %ld\n",strtol(tok,NULL,10)); + Chip_Info->IDNumber = strtol(tok, NULL, 10); + continue; + } + pch = strstr(file_line_buf, "VppSupport"); + if (pch != NULL) { + memset(test, '\0', testbufsize); + strcpy(test, pch + strlen("VppSupport")); + tok = strtok(test, "\"= \t"); + // printf("IDNumber = %ld\n",strtol(tok,NULL,10)); + Chip_Info->VppSupport = strtol(tok, NULL, 10); + continue; + } + pch = strstr(file_line_buf, "RDIDCommand"); + if (pch != NULL) { + memset(test, '\0', testbufsize); + strcpy(test, pch + strlen("RDIDCommand")); + tok = strtok(test, "\"= \t"); + Chip_Info->RDIDCommand = strtol(tok, NULL, 16); + continue; + } + pch = strstr(file_line_buf, "Timeout"); + if (pch != NULL) { + memset(test, '\0', testbufsize); + strcpy(test, pch + strlen("Timeout")); + tok = strtok(test, "\"= \t"); + Chip_Info->Timeout = strtol(tok, NULL, 16); + continue; + } + pch = strstr(file_line_buf, "MXIC_WPmode"); + if (pch != NULL) { + memset(test, '\0', testbufsize); + strcpy(test, pch + strlen("MXIC_WPmode")); + tok = strtok(test, "\"= \t"); + if (strstr(tok, "true") != NULL) + Chip_Info->MXIC_WPmode = true; + else + Chip_Info->MXIC_WPmode = false; + // starting checking input data + continue; + } #if 0 pch=strstr(file_line_buf,"ManufactureUrl="); if(pch != NULL) { - memset(test, '\0', 80); + memset(test, '\0', testbufsize); strcpy(test,pch+strlen("ManufactureUrl=")); tok = strtok(test,"\""); //printf("ManufactureUrl = %s\n",tok); @@ -561,7 +525,7 @@ int Dedi_Search_Chip_Db_ByTypeName(char* TypeName, CHIP_INFO *Chip_Info, int Ind } pch=strstr(file_line_buf,"Description="); if( pch != NULL) { - memset(test, '\0', 80); + memset(test, '\0', testbufsize); strcpy(test,pch+strlen("Description=")); tok = strtok(test,"\""); //printf("Description = %s\n",tok); @@ -569,7 +533,7 @@ int Dedi_Search_Chip_Db_ByTypeName(char* TypeName, CHIP_INFO *Chip_Info, int Ind } pch=strstr(file_line_buf,"Clock="); if(pch != NULL) { - memset(test, '\0', 80); + memset(test, '\0', testbufsize); strcpy(test,pch+strlen("Clock=")); tok = strtok(test,"\""); //printf("Clock = %s\n",tok); @@ -578,7 +542,7 @@ int Dedi_Search_Chip_Db_ByTypeName(char* TypeName, CHIP_INFO *Chip_Info, int Ind pch=strstr(file_line_buf,"ManufactureID="); if(pch != NULL) { - memset(test, '\0', 80); + memset(test, '\0', testbufsize); strcpy(test,pch+strlen("ManufactureID=")); tok = strtok(test,"\""); //printf("ManufactureID = 0x%lx\n",strtol(tok,NULL,16)); @@ -587,74 +551,65 @@ int Dedi_Search_Chip_Db_ByTypeName(char* TypeName, CHIP_INFO *Chip_Info, int Ind pch=strstr(file_line_buf,"DeviceID="); if(pch != NULL) { - memset(test, '\0', 80); + memset(test, '\0', testbufsize); strcpy(test,pch+strlen("DeviceID=")); tok = strtok(test,"\""); //printf("DeviceID = 0x%lx\n",strtol(tok,NULL,16)); Chip_Info->AlternativeID= strtol(tok,NULL,16); } #endif - }/*Continue until EOF is encoutered*/ - fclose(fp); /*Close file*/ - Chip_Info->MaxErasableSegmentInByte = max(Chip_Info->SectorSizeInByte, Chip_Info->BlockSizeInByte); - if(found_flag==0) - { - Chip_Info->TypeName[0]=0; - Chip_Info->UniqueID=0; - } - free(file_buf); - return found_flag; /*Executed without errors*/ -}/*End main*/ + } /*Continue until EOF is encoutered*/ + fclose(fp); /*Close file*/ + Chip_Info->MaxErasableSegmentInByte = max(Chip_Info->SectorSizeInByte, Chip_Info->BlockSizeInByte); + if (found_flag == 0) { + Chip_Info->TypeName[0] = 0; + Chip_Info->UniqueID = 0; + } + free(file_buf); + return found_flag; /*Executed without errors*/ +} -void Dedi_List_AllChip() +bool Dedi_List_AllChip(void) { - FILE* fp; /*Declare file pointer variable*/ - char file_line_buf[linebufsize], *tok, fname[15], *file_buf, test[100];; - char* pch; - char TypeName[100],Manufacturer[100]; - long sz=0; - char Path[512]; - getExecPath(Path); + FILE* fp; /*Declare file pointer variable*/ + char file_line_buf[linebufsize], *tok, *file_buf, test[testbufsize]; + char* pch; + char TypeName[100], Manufacturer[100]; + long sz = 0; - /*If file doesn't exist or filetype isn't allowed exit and*/ - /*error message & return (1) control to the OS*/ - if ((fp = fopen(Path,"rt")) == NULL){ - fprintf(stderr,"Error opening file: %s\n",fname); - return 1; - } - sz=fsize(fp); - file_buf=(char*)malloc(sz); - memset(file_buf, '\0', sz); - /*Read into the buffer contents within thr file stream*/ - while(fgets(file_line_buf, linebufsize, fp) != NULL) - { - pch=strstr(file_line_buf,"TypeName"); - if(pch!= NULL) - { - memset(test, '\0', 100); - strcpy(test,pch+strlen("TypeName")); - tok = strtok(test,"\"= \t"); + if ((fp = openChipInfoDb()) == NULL) + return false; + sz = fsize(fp); + file_buf = (char*)malloc(sz); + memset(file_buf, '\0', sz); + /*Read into the buffer contents within thr file stream*/ + while (fgets(file_line_buf, linebufsize, fp) != NULL) { + pch = strstr(file_line_buf, "TypeName"); + if (pch != NULL) { + memset(test, '\0', testbufsize); + strcpy(test, pch + strlen("TypeName")); + tok = strtok(test, "\"= \t"); // end of struct init - TypeName[0]='\0'; - Manufacturer[0]='\0'; - strcpy(TypeName,tok); - continue; - } - pch=strstr(file_line_buf,"Manufacturer"); - if(pch != NULL) - { - memset(test, '\0', 100); - strcpy(test,pch+strlen("Manufacturer")); - tok = strtok(test,"\"= \t"); - //printf("Manufacturer = %s\n",tok); - strcpy(Manufacturer,tok); - if(TypeName[0] != '\0' && Manufacturer[0] != '\0') - printf("%s\t\tby %s\r\n",TypeName,Manufacturer); - } - }/*Continue until EOF is encoutered*/ - fclose(fp); /*Close file*/ -}/*End main*/ + TypeName[0] = '\0'; + Manufacturer[0] = '\0'; + strcpy(TypeName, tok); + continue; + } + pch = strstr(file_line_buf, "Manufacturer"); + if (pch != NULL) { + memset(test, '\0', testbufsize); + strcpy(test, pch + strlen("Manufacturer")); + tok = strtok(test, "\"= \t"); + // printf("Manufacturer = %s\n",tok); + strcpy(Manufacturer, tok); + if (TypeName[0] != '\0' && Manufacturer[0] != '\0') + printf("%s\t\tby %s\n", TypeName, Manufacturer); + } + } /*Continue until EOF is encoutered*/ + fclose(fp); /*Close file*/ + return true; +} /*End main*/ /***********************PARSE.TXT (CONTENT)**********************/ /* "Move each word to the next line" */ diff --git a/project.c b/project.c index 8741ad4..e4fb27b 100755 --- a/project.c +++ b/project.c @@ -1,29 +1,26 @@ -#include -#include -#include -#include -#include - -#include "Macro.h" -#include "project.h" #include "IntelHexFile.h" #include "MotorolaFile.h" -#include "dpcmd.h" #include "SerialFlash.h" -#include "usbdriver.h" #include "board.h" +#include "dpcmd.h" +#include "project.h" +#include "usbdriver.h" +#include +#include +#include +#include #include -#define min(a,b) (a>b? b:a) - -unsigned char* pBufferForLastReadData[16]={NULL}; -unsigned char* pBufferforLoadedFile=NULL; -unsigned long g_ulFileSize=0; -unsigned int g_uiFileChecksum=0; -unsigned char g_BatchIndex=2; -unsigned int CompleteCnt=0; -bool m_bOperationResult[16]; -bool bAuto[16]={false}; +#define min(a, b) (a > b ? b : a) + +unsigned char* pBufferForLastReadData[16] = { NULL }; +unsigned char* pBufferforLoadedFile = NULL; +unsigned long g_ulFileSize = 0; +unsigned int g_uiFileChecksum = 0; +unsigned char g_BatchIndex = 2; +unsigned int CompleteCnt = 0; +// bool m_bOperationResult[16]; +bool bAuto[16] = { false }; extern unsigned int g_ucFill; extern int m_boEnReadQuadIO; extern int m_boEnWriteQuadIO; @@ -32,8 +29,6 @@ extern char g_board_type[8]; extern int g_firmversion; extern char* g_parameter_vcc; extern char* g_parameter_fw; -//extern bool S19FileToBin(const char* filePath, unsigned char* vData,unsigned long* FileSize, unsigned char PaddingByte); -//extern bool HexFileToBin(const char* filePath, unsigned char* vOutData,unsigned long* FileSize,unsigned char PaddingByte); extern unsigned char mcode_WRSR; extern unsigned char mcode_WRDI; extern unsigned char mcode_RDSR; @@ -57,337 +52,349 @@ extern unsigned int g_Vpp; extern unsigned int g_uiAddr; extern size_t g_uiLen; extern bool g_bEnableVpp; -extern unsigned int g_uiDevNum; //evy add -extern int FlashIdentifier(CHIP_INFO * Chip_Info, int search_all,int Index); +extern unsigned int g_uiDevNum; // evy add +extern int FlashIdentifier(CHIP_INFO* Chip_Info, int search_all, int Index); pthread_mutex_t g_count_mutex; -#define FIRMWARE_VERSION(x,y,z) ((x<<16) | (y<<8) | z) - -enum FILE_FORMAT{ - BIN , - HEX, - S19, - } ; - -void Sleep(unsigned int mSec) -{ - usleep(mSec*1000); -} +#define FIRMWARE_VERSION(x, y, z) ((x << 16) | (y << 8) | z) + +enum FILE_FORMAT { + BIN, + HEX, + S19, +}; + +static unsigned int crc32_tab[] = { + 0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f, + 0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988, + 0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91, 0x1db71064, 0x6ab020f2, + 0xf3b97148, 0x84be41de, 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7, + 0x136c9856, 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec, 0x14015c4f, 0x63066cd9, + 0xfa0f3d63, 0x8d080df5, 0x3b6e20c8, 0x4c69105e, 0xd56041e4, 0xa2677172, + 0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b, 0x35b5a8fa, 0x42b2986c, + 0xdbbbc9d6, 0xacbcf940, 0x32d86ce3, 0x45df5c75, 0xdcd60dcf, 0xabd13d59, + 0x26d930ac, 0x51de003a, 0xc8d75180, 0xbfd06116, 0x21b4f4b5, 0x56b3c423, + 0xcfba9599, 0xb8bda50f, 0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924, + 0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d, 0x76dc4190, 0x01db7106, + 0x98d220bc, 0xefd5102a, 0x71b18589, 0x06b6b51f, 0x9fbfe4a5, 0xe8b8d433, + 0x7807c9a2, 0x0f00f934, 0x9609a88e, 0xe10e9818, 0x7f6a0dbb, 0x086d3d2d, + 0x91646c97, 0xe6635c01, 0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e, + 0x6c0695ed, 0x1b01a57b, 0x8208f4c1, 0xf50fc457, 0x65b0d9c6, 0x12b7e950, + 0x8bbeb8ea, 0xfcb9887c, 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3, 0xfbd44c65, + 0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2, 0x4adfa541, 0x3dd895d7, + 0xa4d1c46d, 0xd3d6f4fb, 0x4369e96a, 0x346ed9fc, 0xad678846, 0xda60b8d0, + 0x44042d73, 0x33031de5, 0xaa0a4c5f, 0xdd0d7cc9, 0x5005713c, 0x270241aa, + 0xbe0b1010, 0xc90c2086, 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f, + 0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4, 0x59b33d17, 0x2eb40d81, + 0xb7bd5c3b, 0xc0ba6cad, 0xedb88320, 0x9abfb3b6, 0x03b6e20c, 0x74b1d29a, + 0xead54739, 0x9dd277af, 0x04db2615, 0x73dc1683, 0xe3630b12, 0x94643b84, + 0x0d6d6a3e, 0x7a6a5aa8, 0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1, + 0xf00f9344, 0x8708a3d2, 0x1e01f268, 0x6906c2fe, 0xf762575d, 0x806567cb, + 0x196c3671, 0x6e6b06e7, 0xfed41b76, 0x89d32be0, 0x10da7a5a, 0x67dd4acc, + 0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5, 0xd6d6a3e8, 0xa1d1937e, + 0x38d8c2c4, 0x4fdff252, 0xd1bb67f1, 0xa6bc5767, 0x3fb506dd, 0x48b2364b, + 0xd80d2bda, 0xaf0a1b4c, 0x36034af6, 0x41047a60, 0xdf60efc3, 0xa867df55, + 0x316e8eef, 0x4669be79, 0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236, + 0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f, 0xc5ba3bbe, 0xb2bd0b28, + 0x2bb45a92, 0x5cb36a04, 0xc2d7ffa7, 0xb5d0cf31, 0x2cd99e8b, 0x5bdeae1d, + 0x9b64c2b0, 0xec63f226, 0x756aa39c, 0x026d930a, 0x9c0906a9, 0xeb0e363f, + 0x72076785, 0x05005713, 0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38, + 0x92d28e9b, 0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21, 0x86d3d2d4, 0xf1d4e242, + 0x68ddb3f8, 0x1fda836e, 0x81be16cd, 0xf6b9265b, 0x6fb077e1, 0x18b74777, + 0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c, 0x8f659eff, 0xf862ae69, + 0x616bffd3, 0x166ccf45, 0xa00ae278, 0xd70dd2ee, 0x4e048354, 0x3903b3c2, + 0xa7672661, 0xd06016f7, 0x4969474d, 0x3e6e77db, 0xaed16a4a, 0xd9d65adc, + 0x40df0b66, 0x37d83bf0, 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9, + 0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605, 0xcdd70693, + 0x54de5729, 0x23d967bf, 0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94, + 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d +}; void TurnONVpp(int Index) { - if(g_bEnableVpp==true) - dediprog_set_vpp_voltage(Chip_Info.VppSupport,Index); + if (g_bEnableVpp == true) + dediprog_set_vpp_voltage(Chip_Info.VppSupport, Index); } void TurnOFFVpp(int Index) { - if(g_bEnableVpp==true) - dediprog_set_vpp_voltage(0,Index); + if (g_bEnableVpp == true) + dediprog_set_vpp_voltage(0, Index); } void TurnONVcc(int Index) -{ - dediprog_set_spi_voltage(g_Vcc,Index); +{ + dediprog_set_spi_voltage(g_Vcc, Index); } void TurnOFFVcc(int Index) -{ - dediprog_set_spi_voltage(0,Index); +{ + dediprog_set_spi_voltage(0, Index); } - unsigned int CRC32(unsigned char* v, unsigned long size) { - unsigned int checksum = 0; unsigned int dwCrc32 = 0xFFFFFFFF; unsigned long i; - for(i=0;i> 8) ^ crc32_tab[(v[i]) ^ ((dwCrc32) & 0x000000FF)]; - //checksum += v[i]; - - dwCrc32 = ~dwCrc32; - return dwCrc32;// & 0xFFFF; + for (i = 0; i < size; i++) + dwCrc32 = ((dwCrc32) >> 8) ^ crc32_tab[(v[i]) ^ ((dwCrc32)&0x000000FF)]; + // checksum += v[i]; + dwCrc32 = ~dwCrc32; + return dwCrc32; // & 0xFFFF; } -int ReadBINFile(const char *filename,unsigned char *buf, unsigned long* size) +int ReadBINFile(const char* filename, unsigned char* buf, unsigned long* size) { - FILE * pFile; + FILE* pFile; long lSize; size_t result; - pFile = fopen( filename , "rb" ); - if (pFile==NULL) - { - printf("Open %s failed\n",filename); - fputs ("File error\n",stderr); + pFile = fopen(filename, "rb"); + if (pFile == NULL) { + printf("Open %s failed\n", filename); + fputs("File error\n", stderr); return 0; } // obtain file size: - fseek (pFile , 0 , SEEK_END); - lSize = ftell (pFile); - + fseek(pFile, 0, SEEK_END); + lSize = ftell(pFile); - rewind (pFile); + rewind(pFile); // allocate memory to contain the whole file: - if(pBufferforLoadedFile!=NULL) - free(pBufferforLoadedFile); + if (pBufferforLoadedFile != NULL) + free(pBufferforLoadedFile); - pBufferforLoadedFile = (unsigned char*) malloc (lSize); - + pBufferforLoadedFile = (unsigned char*)malloc(lSize); - if (pBufferforLoadedFile == NULL) - { - fputs ("Memory error\n",stderr); + if (pBufferforLoadedFile == NULL) { + fputs("Memory error\n", stderr); return 0; } // copy the file into the buffer: - result = fread (pBufferforLoadedFile,1,lSize,pFile); - if (result != lSize) - { - fputs ("Reading error\n",stderr); + result = fread(pBufferforLoadedFile, 1, lSize, pFile); + if (result != lSize) { + fputs("Reading error\n", stderr); return 0; } /* the whole file is now loaded in the memory buffer. */ // terminate - g_ulFileSize=lSize; + g_ulFileSize = lSize; - fclose (pFile); + fclose(pFile); return 1; } -int WriteBINFile(const char *filename,unsigned char *buf, unsigned long size) +int WriteBINFile(const char* filename, unsigned char* buf, unsigned long size) { - unsigned long numbytes; - FILE *image; + unsigned long numbytes; + FILE* image; - if (!filename) { - printf("No filename specified.\n"); - return 0; - } - if ((image = fopen(filename, "wb")) == NULL) { - perror(filename); - return 0; - } + if (!filename) { + printf("No filename specified.\n"); + return 0; + } + if ((image = fopen(filename, "wb")) == NULL) { + perror(filename); + return 0; + } - numbytes = fwrite(buf, 1, size, image); - fclose(image); - if (numbytes != size) { - printf("File %s could not be written completely.\n", - filename); - return 0; - } - return 1; + numbytes = fwrite(buf, 1, size, image); + fclose(image); + if (numbytes != size) { + printf("File %s could not be written completely.\n", filename); + return 0; + } + return 1; } - int GetFileFormatFromExt(const char* csPath) { - size_t length=strlen(csPath); - char fmt[5]={0}; - size_t i=length; - size_t j=0; + size_t length = strlen(csPath); + char fmt[5] = { 0 }; + size_t i = length; + size_t j = 0; - while(csPath[i] != '.') - { - if (i==0) { + while (csPath[i] != '.') { + if (i == 0) return BIN; - } - i--; } - if (length-i > 5) { + if (length - i > 5) return BIN; - } - for(j=i;j Chip_Info.ChipSizeInByte - 1) + if (DownloadAddrRange.start > Chip_Info.ChipSizeInByte - 1) return false; - + size_t size = DownloadAddrRange.length; - if(size > g_ulFileSize && g_ucFill==0xFF) + if (size > g_ulFileSize && g_ucFill == 0xFF) + return false; + + if (DownloadAddrRange.end > Chip_Info.ChipSizeInByte) return false; - - if(DownloadAddrRange.end > Chip_Info.ChipSizeInByte) - return false; return true; } bool ProgramChip(int Index) -{ +{ bool need_padding = (g_ucFill != 0xFF); unsigned char* vc; - struct CAddressRange real_addr[16]; - real_addr[Index].start=(DownloadAddrRange.start &(~(0x200 - 1))); - real_addr[Index].end=((DownloadAddrRange.end + (0x200 - 1)) & (~(0x200 - 1))); - real_addr[Index].length=real_addr[Index].end-real_addr[Index].start; - - vc=(unsigned char*)malloc(real_addr[Index].length); - memset(vc,g_ucFill,real_addr[Index].length); - - if(need_padding==true) - { - memcpy(vc+(DownloadAddrRange.start & 0x1FF),pBufferforLoadedFile,min(g_ulFileSize,DownloadAddrRange.length)); + struct CAddressRange real_addr[16]; + real_addr[Index].start = (DownloadAddrRange.start & (~(0x200 - 1))); + real_addr[Index].end = ((DownloadAddrRange.end + (0x200 - 1)) & (~(0x200 - 1))); + real_addr[Index].length = real_addr[Index].end - real_addr[Index].start; + + vc = (unsigned char*)malloc(real_addr[Index].length); + memset(vc, g_ucFill, real_addr[Index].length); + + if (need_padding == true) { + memcpy(vc + (DownloadAddrRange.start & 0x1FF), pBufferforLoadedFile, min(g_ulFileSize, DownloadAddrRange.length)); + } else { + memcpy(vc + (DownloadAddrRange.start & 0x1FF), pBufferforLoadedFile, DownloadAddrRange.length); } - else - { - memcpy(vc+(DownloadAddrRange.start & 0x1FF),pBufferforLoadedFile,DownloadAddrRange.length); - } - - bool result= SerialFlash_rangeProgram(&real_addr[Index], vc,Index); - return result; + bool result = SerialFlash_rangeProgram(&real_addr[Index], vc, Index); + return result; } -bool ReadChip(const struct CAddressRange range,int Index) +bool ReadChip(const struct CAddressRange range, int Index) { bool result = true; unsigned char* vc; size_t addrStart = range.start; -// size_t addrLeng = range.length; + // size_t addrLeng = range.length; struct CAddressRange addr; - addr.start= range.start &(~(0x200 - 1)); - addr.end=(range.end + (0x200 - 1)) & (~(0x200 - 1)); - addr.length=addr.end-addr.start; - vc=(unsigned char*)malloc(addr.length); + addr.start = range.start & (~(0x200 - 1)); + addr.end = (range.end + (0x200 - 1)) & (~(0x200 - 1)); + addr.length = addr.end - addr.start; + vc = (unsigned char*)malloc(addr.length); result = SerialFlash_rangeRead(&addr, vc, Index); - if(result) - { - if(pBufferForLastReadData[Index] != NULL) - { + if (result) { + if (pBufferForLastReadData[Index] != NULL) { free(pBufferForLastReadData[Index]); - } + } unsigned int offset = (addrStart & 0x1FF); - pBufferForLastReadData[Index]=(unsigned char*)malloc(range.length); - memcpy(pBufferForLastReadData[Index],vc+offset,range.length); - UploadAddrRange=range; + pBufferForLastReadData[Index] = (unsigned char*)malloc(range.length); + memcpy(pBufferForLastReadData[Index], vc + offset, range.length); + UploadAddrRange = range; } - if(vc != NULL) - free (vc); + if (vc != NULL) + free(vc); return result; - } bool threadBlankCheck(int Index) -{ - bool result = false; +{ + bool result = false; struct CAddressRange Addr; - Addr.start=0; - Addr.end=Chip_Info.ChipSizeInByte; - - SetIOMode(false,Index); + Addr.start = 0; + Addr.end = Chip_Info.ChipSizeInByte; + SetIOMode(false, Index); - result = SerialFlash_rangeBlankCheck(&Addr,Index) ; + result = SerialFlash_rangeBlankCheck(&Addr, Index); - m_bOperationResult[Index]=result? 1:RES_BLANK; +// m_bOperationResult[Index] = result ? 1 : RES_BLANK; #if 0 if( !bAuto[Index] ) //not batch @@ -406,26 +413,25 @@ bool threadBlankCheck(int Index) // SerialFlash_ClearCancelOperationFlag(); } -#endif - g_is_operation_successful[Index] = result; +#endif + g_is_operation_successful[Index] = result; return result; } bool threadEraseWholeChip(int Index) -{ - bool result = false; - - -// power::CAutoVccPower autopowerVcc(m_usb, m_context.power.vcc,Index); -// power::CAutoVppPower autopowerVpp(m_usb, SupportedVpp(),Index); - if(strstr(Chip_Info.Class,SUPPORT_NUMONYX_N25Qxxx_Large_2Die) != NULL || strstr(Chip_Info.Class,SUPPORT_NUMONYX_N25Qxxx_Large_4Die) != NULL) - result = SerialFlash_DieErase(Index); - else +{ + bool result = false; + + // power::CAutoVccPower autopowerVcc(m_usb, m_context.power.vcc,Index); + // power::CAutoVppPower autopowerVpp(m_usb, SupportedVpp(),Index); + if (strstr(Chip_Info.Class, SUPPORT_NUMONYX_N25Qxxx_Large_2Die) != NULL || strstr(Chip_Info.Class, SUPPORT_NUMONYX_N25Qxxx_Large_4Die) != NULL) + result = SerialFlash_DieErase(Index); + else result = SerialFlash_chipErase(Index); -// Log(result ? L"A whole chip erased" : L"Error: Failed to erase a whole chip"); + // Log(result ? L"A whole chip erased" : L"Error: Failed to erase a whole chip"); - m_bOperationResult[Index]=result? 1:RES_ERASE; + // m_bOperationResult[Index] = result ? 1 : RES_ERASE; #if 0 if( !bAuto[Index] ) //not batch { @@ -436,92 +442,83 @@ bool threadEraseWholeChip(int Index) g_is_operation_on_going=false; SerialFlash_ClearCancelOperationFlag(); - } + } } else { SerialFlash_ClearCancelOperationFlag(); } -#endif - g_is_operation_successful[Index] = result; - return result; +#endif + g_is_operation_successful[Index] = result; + return result; } -bool threadReadRangeChip(struct CAddressRange range,int Index) -{ +bool threadReadRangeChip(struct CAddressRange range, int Index) +{ bool result = true; - struct CAddressRange AddrRound; - - SetIOMode(false,Index); + struct CAddressRange AddrRound; - AddrRound.start= (range.start &(~(0x1FF))); - AddrRound.end=((range.end +0x1FF) & (~(0x1FF))); - AddrRound.length=AddrRound.end-AddrRound.start; + SetIOMode(false, Index); - unsigned char *pBuffer = malloc(AddrRound.length); + AddrRound.start = (range.start & (~(0x1FF))); + AddrRound.end = ((range.end + 0x1FF) & (~(0x1FF))); + AddrRound.length = AddrRound.end - AddrRound.start; - if(pBufferForLastReadData[Index]==NULL) - pBufferForLastReadData[Index]=malloc(range.end-range.start); - else - { + unsigned char* pBuffer = malloc(AddrRound.length); + + if (pBufferForLastReadData[Index] == NULL) + pBufferForLastReadData[Index] = malloc(range.end - range.start); + else { free(pBufferForLastReadData[Index]); - pBufferForLastReadData[Index]=malloc(range.end-range.start); + pBufferForLastReadData[Index] = malloc(range.end - range.start); } - if(pBufferForLastReadData[Index]==NULL) - { + if (pBufferForLastReadData[Index] == NULL) { printf("allocate memory fail.\n"); free(pBuffer); return false; } - result = SerialFlash_rangeRead(&AddrRound, pBuffer, Index); - - if(result) - { + result = SerialFlash_rangeRead(&AddrRound, pBuffer, Index); + + if (result) { UploadAddrRange.start = range.start; - UploadAddrRange.end= range.end; - UploadAddrRange.length = range.end-range.start; - memcpy(pBufferForLastReadData[Index],pBuffer+ (UploadAddrRange.start & 0x1FF),UploadAddrRange.length); - - } + UploadAddrRange.end = range.end; + UploadAddrRange.length = range.end - range.start; + memcpy(pBufferForLastReadData[Index], pBuffer + (UploadAddrRange.start & 0x1FF), UploadAddrRange.length); + } + + free(pBuffer); - free(pBuffer); - return result; } bool threadReadChip(int Index) { - bool result = false; + bool result = false; struct CAddressRange Addr; - Addr.start=0; - Addr.end=Chip_Info.ChipSizeInByte; - + Addr.start = 0; + Addr.end = Chip_Info.ChipSizeInByte; - SetIOMode(false,Index); + SetIOMode(false, Index); - if(pBufferForLastReadData[Index]==NULL) - pBufferForLastReadData[Index]=(unsigned char*)malloc(Chip_Info.ChipSizeInByte); - else - { + if (pBufferForLastReadData[Index] == NULL) + pBufferForLastReadData[Index] = (unsigned char*)malloc(Chip_Info.ChipSizeInByte); + else { free(pBufferForLastReadData[Index]); - pBufferForLastReadData[Index]=(unsigned char*)malloc(Chip_Info.ChipSizeInByte); + pBufferForLastReadData[Index] = (unsigned char*)malloc(Chip_Info.ChipSizeInByte); } - if(pBufferForLastReadData[Index]==NULL) - { + if (pBufferForLastReadData[Index] == NULL) { printf("allocate memory fail.\n"); return false; } - result = SerialFlash_rangeRead(&Addr, pBufferForLastReadData[Index],Index); + result = SerialFlash_rangeRead(&Addr, pBufferForLastReadData[Index], Index); - if(result) - { + if (result) { UploadAddrRange.start = 0; - UploadAddrRange.end=Chip_Info.ChipSizeInByte; - UploadAddrRange.length=Chip_Info.ChipSizeInByte; + UploadAddrRange.end = Chip_Info.ChipSizeInByte; + UploadAddrRange.length = Chip_Info.ChipSizeInByte; } - g_is_operation_successful[Index] = result; @@ -531,61 +528,50 @@ bool threadConfiguredReadChip(int Index) { bool result = true; struct CAddressRange addr; - addr.start= DownloadAddrRange.start; - addr.length= DownloadAddrRange.end-DownloadAddrRange.start; + addr.start = DownloadAddrRange.start; + addr.length = DownloadAddrRange.end - DownloadAddrRange.start; - - if(0 == addr.length && Chip_Info.ChipSizeInByte > addr.start) + if (0 == addr.length && Chip_Info.ChipSizeInByte > addr.start) addr.length = Chip_Info.ChipSizeInByte - addr.start; - addr.end=addr.length+addr.start; - if(addr.start >= Chip_Info.ChipSizeInByte) - { + addr.end = addr.length + addr.start; + if (addr.start >= Chip_Info.ChipSizeInByte) { result = false; - } - else if(0 == addr.start && (0 == addr.length)) - { - result = threadReadChip(Index); - } - else - { - result = threadReadRangeChip(addr,Index); + } else if (0 == addr.start && (0 == addr.length)) { + result = threadReadChip(Index); + } else { + result = threadReadRangeChip(addr, Index); } g_is_operation_successful[Index] = result; -// g_is_operation_on_going = false; + // g_is_operation_on_going = false; return result; } bool threadProgram(int Index) -{ - int pthread_mutex_init(pthread_mutex_t *restrict mutex, const pthread_mutexattr_t *restricattr); - - bool result = true; +{ + int pthread_mutex_init(pthread_mutex_t * restrict mutex, const pthread_mutexattr_t* restricattr); - SetIOMode(true,Index); + bool result = true; - pthread_mutex_lock(&g_count_mutex); - if(g_ulFileSize== 0) - { - result = false; + SetIOMode(true, Index); + + pthread_mutex_lock(&g_count_mutex); + if (g_ulFileSize == 0) { + result = false; } - if( result && (!ValidateProgramParameters(Index)) ) - { - result = false; - } + if (result && (!ValidateProgramParameters(Index))) { + result = false; + } pthread_mutex_unlock(&g_count_mutex); - int pthread_mutex_destroy(pthread_mutex_t *mutex); + int pthread_mutex_destroy(pthread_mutex_t * mutex); - if( result && ProgramChip(Index)) - { + if (result && ProgramChip(Index)) { result = true; - } - else - { - result = false; + } else { + result = false; } #if 0 @@ -608,54 +594,51 @@ bool threadProgram(int Index) { // SerialFlash_ClearCancelOperationFlag(); - }*/ -#endif - - + }*/ +#endif + g_is_operation_successful[Index] = result; return result; } bool threadCompareFileAndChip(int Index) -{ - bool result = true; +{ + bool result = true; - SetIOMode(false,Index); + SetIOMode(false, Index); - if(g_ulFileSize==0) + if (g_ulFileSize == 0) result = false; - if( result && (!ValidateProgramParameters(Index)) ) - result = false; - //refresh vcc before verify + if (result && (!ValidateProgramParameters(Index))) + result = false; + // refresh vcc before verify TurnOFFVcc(Index); - Sleep(100); - TurnONVcc(Index); - Sleep(100); + Sleep(100); + TurnONVcc(Index); + Sleep(100); + + if (IdentifyChipBeforeOperation(Index) == false) + result = false; - if(IdentifyChipBeforeOperation(Index)==false) - result = false; + if (result) { + ReadChip(DownloadAddrRange, Index); - if( result ) - { - - ReadChip(DownloadAddrRange,Index); - - size_t offset = min(DownloadAddrRange.length,g_ulFileSize); - unsigned int crcFile = CRC32(pBufferforLoadedFile,offset); - unsigned int crcChip = CRC32(pBufferForLastReadData[Index],offset); -// unsigned int i=0; - #if 0 + size_t offset = min(DownloadAddrRange.length, g_ulFileSize); + unsigned int crcFile = CRC32(pBufferforLoadedFile, offset); + unsigned int crcChip = CRC32(pBufferForLastReadData[Index], offset); + // unsigned int i=0; +#if 0 for(i=0; i<10; i++) { if(pBufferforLoadedFile[i] != pBufferForLastReadData[i]) - printf("Data deferent at %X, pBufferforLoadedFile[i]=%x,pBufferForLastReadData[i]=%x\r\n",i,pBufferforLoadedFile[i],pBufferForLastReadData[i]); + printf("Data deferent at %X, pBufferforLoadedFile[i]=%x,pBufferForLastReadData[i]=%x\n",i,pBufferforLoadedFile[i],pBufferForLastReadData[i]); } - #endif +#endif result = (crcChip == crcFile); } - + #if 0 if( !bAuto[Index] ) //not batch @@ -677,46 +660,41 @@ bool threadCompareFileAndChip(int Index) // SerialFlash_ClearCancelOperationFlag(); } -#endif +#endif g_is_operation_successful[Index] = result; return result; } -size_t GenerateDiff(uintptr_t* Addr,unsigned char* in1, unsigned long long size1, unsigned char* in2, unsigned long long size2, uintptr_t baseAddr, size_t step) +size_t GenerateDiff(uintptr_t* Addr, unsigned char* in1, unsigned long long size1, unsigned char* in2, unsigned long long size2, uintptr_t baseAddr, size_t step) { size_t upper = min(size1, size2); size_t realAddr; - uintptr_t i,j=0; - + uintptr_t i, j = 0; - for(i = 0; i < upper; ++i) - { - if(in1[i] != in2[i]) - { + for (i = 0; i < upper; ++i) { + if (in1[i] != in2[i]) { realAddr = i + baseAddr; - Addr[j++]=(realAddr & (~(step - 1))); + Addr[j++] = (realAddr & (~(step - 1))); - i += ((step - 1)|realAddr) - realAddr; - if(i >= upper) break; + i += ((step - 1) | realAddr) - realAddr; + if (i >= upper) + break; } } return j; } -size_t Condense(uintptr_t* out,unsigned char* vc, uintptr_t* addrs, size_t addrSize, uintptr_t baseAddr, size_t step) +size_t Condense(uintptr_t* out, unsigned char* vc, uintptr_t* addrs, size_t addrSize, uintptr_t baseAddr, size_t step) { -// size_t* itr, itr_end; - size_t i,j,outSize=0; -// itr_end=addrs+addrSize; - - for(j=0; j0) - { - if(LockAddrrange.startDownloadAddrRange.end) - down_with_lock_range.end=LockAddrrange.start+LockAddrrange.length; + struct CAddressRange down_with_lock_range; + down_with_lock_range.start = DownloadAddrRange.start; + down_with_lock_range.end = DownloadAddrRange.end; + down_with_lock_range.length = down_with_lock_range.end - down_with_lock_range.start; + DownloadAddrRange.length = down_with_lock_range.length; + + if (LockAddrrange.length > 0) { + if (LockAddrrange.start < DownloadAddrRange.start) + down_with_lock_range.start = LockAddrrange.start; + if ((LockAddrrange.start + LockAddrrange.length) > DownloadAddrRange.end) + down_with_lock_range.end = LockAddrrange.start + LockAddrrange.length; } - struct CAddressRange effectiveRange;//(addr_round.SectionRound(down_with_lock_range)); - effectiveRange.start=down_with_lock_range.start &(~(Chip_Info.MaxErasableSegmentInByte - 1)) ; - effectiveRange.end=(down_with_lock_range.end + (Chip_Info.MaxErasableSegmentInByte - 1)) & (~(Chip_Info.MaxErasableSegmentInByte - 1)); - effectiveRange.length=effectiveRange.end-effectiveRange.start; - - if(!threadReadRangeChip(effectiveRange,Index)) return false; + struct CAddressRange effectiveRange; //(addr_round.SectionRound(down_with_lock_range)); + effectiveRange.start = down_with_lock_range.start & (~(Chip_Info.MaxErasableSegmentInByte - 1)); + effectiveRange.end = (down_with_lock_range.end + (Chip_Info.MaxErasableSegmentInByte - 1)) & (~(Chip_Info.MaxErasableSegmentInByte - 1)); + effectiveRange.length = effectiveRange.end - effectiveRange.start; + + if (!threadReadRangeChip(effectiveRange, Index)) + return false; - unsigned int offsetOfRealStartAddrOffset = 0; + unsigned int offsetOfRealStartAddrOffset = 0; unsigned char* vc; unsigned char* vc2; - vc=(unsigned char*)malloc(effectiveRange.length); - vc2=(unsigned char*)malloc(effectiveRange.length); + vc = (unsigned char*)malloc(effectiveRange.length); + vc2 = (unsigned char*)malloc(effectiveRange.length); - uintptr_t* addrs=(size_t*)malloc(min(DownloadAddrRange.length,g_ulFileSize)); - size_t Leng=0; + uintptr_t* addrs = (size_t*)malloc(min(DownloadAddrRange.length, g_ulFileSize)); + size_t Leng = 0; - memcpy(vc,pBufferForLastReadData[Index],effectiveRange.length); //memory data + memcpy(vc, pBufferForLastReadData[Index], effectiveRange.length); //memory data - - - if(LockAddrrange.length>0) - { - unsigned int offsetOfRealStartAddrOffset = LockAddrrange.start-effectiveRange.start; //DownloadAddrRange.start - effectiveRange.start; - memcpy(pBufferforLoadedFile+offsetOfRealStartAddrOffset,pBufferForLastReadData[Index]+offsetOfRealStartAddrOffset,LockAddrrange.length); - Leng=GenerateDiff(addrs,vc,DownloadAddrRange.length,pBufferforLoadedFile,g_ulFileSize,DownloadAddrRange.start,Chip_Info.MaxErasableSegmentInByte); - } - else - { - unsigned int offsetOfRealStartAddrOffset = DownloadAddrRange.start - effectiveRange.start; - Leng=GenerateDiff(addrs,vc+offsetOfRealStartAddrOffset,DownloadAddrRange.length,pBufferforLoadedFile,g_ulFileSize,DownloadAddrRange.start,Chip_Info.MaxErasableSegmentInByte); + if (LockAddrrange.length > 0) { + unsigned int offsetOfRealStartAddrOffset = LockAddrrange.start - effectiveRange.start; // DownloadAddrRange.start - effectiveRange.start; + memcpy(pBufferforLoadedFile + offsetOfRealStartAddrOffset, pBufferForLastReadData[Index] + offsetOfRealStartAddrOffset, LockAddrrange.length); + Leng = GenerateDiff(addrs, vc, DownloadAddrRange.length, pBufferforLoadedFile, g_ulFileSize, DownloadAddrRange.start, Chip_Info.MaxErasableSegmentInByte); + } else { + unsigned int offsetOfRealStartAddrOffset = DownloadAddrRange.start - effectiveRange.start; + Leng = GenerateDiff(addrs, vc + offsetOfRealStartAddrOffset, DownloadAddrRange.length, pBufferforLoadedFile, g_ulFileSize, DownloadAddrRange.start, Chip_Info.MaxErasableSegmentInByte); } - - - if(Leng==0) // speed optimisation + + if (Leng == 0) // speed optimisation { return true; - } - else - { + } else { + uintptr_t* condensed_addr = (size_t*)malloc(min(DownloadAddrRange.length, g_ulFileSize)); + size_t condensed_size; + condensed_size = Condense(condensed_addr, vc, addrs, Leng, effectiveRange.start, Chip_Info.MaxErasableSegmentInByte); - uintptr_t* condensed_addr=(size_t*)malloc(min(DownloadAddrRange.length,g_ulFileSize)); - size_t condensed_size; - condensed_size=Condense(condensed_addr,vc, addrs, Leng, effectiveRange.start,Chip_Info.MaxErasableSegmentInByte); - - SerialFlash_batchErase(condensed_addr,condensed_size,Index); + SerialFlash_batchErase(condensed_addr, condensed_size, Index); - if(strstr(Chip_Info.Class,SUPPORT_MACRONIX_MX25Lxxx) != NULL) - { + if (strstr(Chip_Info.Class, SUPPORT_MACRONIX_MX25Lxxx) != NULL) { TurnOFFVcc(Index); Sleep(100); TurnONVcc(Index); - } - - memcpy(vc+offsetOfRealStartAddrOffset,pBufferforLoadedFile,DownloadAddrRange.length); + } + + memcpy(vc + offsetOfRealStartAddrOffset, pBufferforLoadedFile, DownloadAddrRange.length); size_t i = 0; - for(i=0; i (1<<16)); + bool fast_load = (Chip_Info.ChipSizeInByte > (1 << 16)); - return ((mcode_SegmentErase!=0) && fast_load) + return ((mcode_SegmentErase != 0) && fast_load) ? RangeUpdateThruSectorErase(Index) : RangeUpdateThruChipErase(Index); } bool ReplaceChipContentThruChipErase(int Index) -{ - if(!threadBlankCheck(Index))//not blank - { - if(!threadEraseWholeChip(Index)) - return false; - } +{ + if (!threadBlankCheck(Index)) // not blank + { + if (!threadEraseWholeChip(Index)) + return false; + } return threadProgram(Index); } @@ -907,191 +866,171 @@ bool threadPredefinedBatchSequences(int Index) { bool result = true; - if(g_ulFileSize==0) result = false; + if (g_ulFileSize == 0) result = false; -// size_t option=2; -// bool bVerifyAfterCompletion; - // 07.11.2009 -// bool bIdentifyBeforeOperation; - - if( result && (!ValidateProgramParameters(Index)) ) result = false; + // size_t option=2; + // bool bVerifyAfterCompletion; + // 07.11.2009 + // bool bIdentifyBeforeOperation; + + if (result && (!ValidateProgramParameters(Index))) result = false; #if 0 if(strstr(Chip_Info.Class,SUPPORT_MACRONIX_MX25Lxxx)!= NULL ||strstr(Chip_Info.Class,SUPPORT_MACRONIX_MX25Lxxx_Large) != NULL ||strstr(Chip_Info.Class,SUPPORT_MACRONIX_MX25Lxxx_PP32)!=NULL) Sleep(10); #endif - - if( result ) - { - switch(g_BatchIndex) - { - case 1: //-z - result=ReplaceChipContentThruChipErase(Index); - break; - case 2: - default: - result = RangeUpdate(Index); - break; - } - } - return result; + + if (result) { + switch (g_BatchIndex) { + case 1: //-z + result = ReplaceChipContentThruChipErase(Index); + break; + case 2: + default: + result = RangeUpdate(Index); + break; + } + } + return result; } void threadRun(void* Type) -{ - - THREAD_STRUCT* thread_data=(THREAD_STRUCT*)Type; - OPERATION_TYPE opType=thread_data->type; - int Index=thread_data->USBIndex; +{ + THREAD_STRUCT* thread_data = (THREAD_STRUCT*)Type; + OPERATION_TYPE opType = thread_data->type; + int Index = thread_data->USBIndex; g_is_operation_successful[Index] = true; bool is_greater_than_5_0_0 = is_BoardVersionGreaterThan_5_0_0(Index); - - int dwUID=ReadUID(Index); - if( g_uiAddr==0 && g_uiLen ==0) - { - if((dwUID / 600000)==0) - printf("\nDevice %d (DP%06d):",Index+1,dwUID); - else - printf("\nDevice %d (SF%06d):",Index+1,dwUID); + + int dwUID = ReadUID(Index); + if (g_uiAddr == 0 && g_uiLen == 0) { + if ((dwUID / 600000) == 0) + printf("\nDevice %d (DP%06d):", Index + 1, dwUID); + else + printf("\nDevice %d (SF%06d):", Index + 1, dwUID); } - - if( opType==UPDATE_FIRMWARE ) - { - g_is_operation_successful[Index]=UpdateFirmware(g_parameter_fw, Index); - g_is_operation_on_going=false; - free(Type); + + if (opType == UPDATE_FIRMWARE) { + g_is_operation_successful[Index] = UpdateFirmware(g_parameter_fw, Index); + g_is_operation_on_going = false; + free(Type); return; } - if(opType==AUTO_UPDATE_FIRMWARE) - { - free(Type); + if (opType == AUTO_UPDATE_FIRMWARE) { + free(Type); return; } - if(1)//is_good()) - { + if (1) // is_good()) + { TurnONVcc(Index); - if(is_greater_than_5_0_0) - { - SetLEDOnOff(SITE_BUSY,Index); - } - - if(IdentifyChipBeforeOperation(Index)==false) - { - printf("Warning: Failed to detect flash (%d).\r\n",Index); - g_is_operation_successful[Index] = false; - + if (is_greater_than_5_0_0) { + SetLEDOnOff(SITE_BUSY, Index); } - else - { - switch(opType) // operations - { - case BLANKCHECK_WHOLE_CHIP: - threadBlankCheck(Index); - break; - - case ERASE_WHOLE_CHIP: - threadEraseWholeChip(Index); - break; - case PROGRAM_CHIP: + if (IdentifyChipBeforeOperation(Index) == false) { + printf("Warning: Failed to detect flash (%d).\n", Index); + g_is_operation_successful[Index] = false; - TurnONVpp(Index); - threadProgram(Index); - TurnOFFVpp(Index); - break; + } else { + switch (opType) // operations + { + case BLANKCHECK_WHOLE_CHIP: + threadBlankCheck(Index); + break; - case READ_WHOLE_CHIP: - // m threadReadChip(Index); - break; + case ERASE_WHOLE_CHIP: + threadEraseWholeChip(Index); + break; - case READ_ANY_BY_PREFERENCE_CONFIGURATION: - threadConfiguredReadChip(Index); - break; + case PROGRAM_CHIP: - case VERIFY_CONTENT: - threadCompareFileAndChip(Index); - break; + TurnONVpp(Index); + threadProgram(Index); + TurnOFFVpp(Index); + break; - case AUTO: - TurnONVpp(Index); - bAuto[Index]=true; - threadPredefinedBatchSequences(Index) ; - TurnOFFVpp(Index); - break; + case READ_WHOLE_CHIP: + // m threadReadChip(Index); + break; - default: - break; - } - } - - } - - SetLEDOnOff(g_is_operation_successful[Index]? SITE_OK:SITE_ERROR,Index); + case READ_ANY_BY_PREFERENCE_CONFIGURATION: + threadConfiguredReadChip(Index); + break; - TurnOFFVcc(Index); - free(Type); + case VERIFY_CONTENT: + threadCompareFileAndChip(Index); + break; + + case AUTO: + TurnONVpp(Index); + bAuto[Index] = true; + threadPredefinedBatchSequences(Index); + TurnOFFVpp(Index); + break; + + default: + break; + } + } + } + + SetLEDOnOff(g_is_operation_successful[Index] ? SITE_OK : SITE_ERROR, Index); + + TurnOFFVcc(Index); + free(Type); CompleteCnt++; - if(g_uiDevNum!=0) - g_is_operation_on_going = false; - else if( CompleteCnt==get_usb_dev_cnt()) - g_is_operation_on_going = false; - + if (g_uiDevNum != 0) + g_is_operation_on_going = false; + else if (CompleteCnt == get_usb_dev_cnt()) + g_is_operation_on_going = false; } +void Run(OPERATION_TYPE type, int DevIndex) +{ + int dev_cnt = get_usb_dev_cnt(); + pthread_t id; + THREAD_STRUCT* thread_data[16]; + CompleteCnt = 0; -void Run(OPERATION_TYPE type,int DevIndex) -{ - int dev_cnt=get_usb_dev_cnt(); - pthread_t id; - THREAD_STRUCT *thread_data[16]; - CompleteCnt = 0; - g_is_operation_on_going = true; - if(DevIndex==0)//mutiple - { - for(int i=0;itype=type; - bAuto[i]=false; - thread_data[i]->USBIndex=i; - pthread_create(&id,NULL,(void *) threadRun,(void*)thread_data[i]); - - } - } - else if(DevIndex<=dev_cnt) - { - thread_data[DevIndex-1]=(THREAD_STRUCT*)malloc(sizeof(THREAD_STRUCT)); - thread_data[DevIndex-1]->type=type; - bAuto[DevIndex-1]=false; - thread_data[DevIndex-1]->USBIndex=DevIndex-1; //0; - pthread_create(&id,NULL,(void *) threadRun,(void*)thread_data[DevIndex-1]); - } - else - printf("Error: Did not find the programmer.\r\n"); - - + if (DevIndex == 0) // mutiple + { + for (int i = 0; i < dev_cnt; i++) { + thread_data[i] = (THREAD_STRUCT*)malloc(sizeof(THREAD_STRUCT)); + thread_data[i]->type = type; + bAuto[i] = false; + thread_data[i]->USBIndex = i; + pthread_create(&id, NULL, (void*)threadRun, (void*)thread_data[i]); + } + } else if (DevIndex <= dev_cnt) { + thread_data[DevIndex - 1] = (THREAD_STRUCT*)malloc(sizeof(THREAD_STRUCT)); + thread_data[DevIndex - 1]->type = type; + bAuto[DevIndex - 1] = false; + thread_data[DevIndex - 1]->USBIndex = DevIndex - 1; // 0; + pthread_create( + &id, NULL, (void*)threadRun, (void*)thread_data[DevIndex - 1]); + } else + printf("Error: Did not find the programmer.\n"); } -void SetIOMode(bool isProg,int Index) +void SetIOMode(bool isProg, int Index) { - size_t IOValue=0; - m_boEnReadQuadIO=0; - m_boEnWriteQuadIO=0; + size_t IOValue = 0; + m_boEnReadQuadIO = 0; + m_boEnWriteQuadIO = 0; - if(g_bIsSF600[Index]==false) return; + if (g_bIsSF600[Index] == false) return; SetIOModeToSF600(IOValue, Index); return; -// if(strlen(Chip_Info.ProgramIOMethod)==0) -// { -// SetIOModeToSF600(IOValue, Index); -// return; -// } + // if(strlen(Chip_Info.ProgramIOMethod)==0) + // { + // SetIOModeToSF600(IOValue, Index); + // return; + // } #if 0 if(isProg) @@ -1122,9 +1061,7 @@ void SetIOMode(bool isProg,int Index) IOValue = 4; break; } - } - else - { + } else { switch(m_context.chip.IO_Mode) { case Context::CChipContext::IO_Single: @@ -1184,114 +1121,101 @@ void SetIOMode(bool isProg,int Index) bool is_BoardVersionGreaterThan_5_0_0(int Index) { - if(g_firmversion < FIRMWARE_VERSION(5, 0, 0)) + if (g_firmversion < FIRMWARE_VERSION(5, 0, 0)) return false; return true; } bool is_SF100nBoardVersionGreaterThan_5_5_0(int Index) { - if((g_firmversion >= FIRMWARE_VERSION(5, 5, 0)) && strstr(g_board_type,"SF100") != NULL) + if ((g_firmversion >= FIRMWARE_VERSION(5, 5, 0)) && strstr(g_board_type, "SF100") != NULL) return true; return false; } bool is_SF600nBoardVersionGreaterThan_6_9_0(int Index) { -// printf("g_board_type=%s\r\n",g_board_type); - if(strstr(g_board_type,"SF600") != NULL) - { - if((g_firmversion > FIRMWARE_VERSION(7, 0, 1)) || (g_firmversion == FIRMWARE_VERSION(6, 9, 0))) - { -// printf("g_firmversion=%X\r\n",g_firmversion); - return true; - } - } + // printf("g_board_type=%s\n",g_board_type); + if (strstr(g_board_type, "SF600") != NULL) { + if ((g_firmversion > FIRMWARE_VERSION(7, 0, 1)) || (g_firmversion == FIRMWARE_VERSION(6, 9, 0))) { + // printf("g_firmversion=%X\n",g_firmversion); + return true; + } + } return false; } bool is_SF100nBoardVersionGreaterThan_5_2_0(int Index) { - if((g_firmversion >= FIRMWARE_VERSION(5, 2, 0)) && strstr(g_board_type,"SF100") != NULL) + if ((g_firmversion >= FIRMWARE_VERSION(5, 2, 0)) && strstr(g_board_type, "SF100") != NULL) return true; return false; } bool is_SF600nBoardVersionGreaterThan_7_0_1n6_7_0(int Index) { - if(strstr(g_board_type,"SF600") != NULL) - { - if((g_firmversion > FIRMWARE_VERSION(7, 0, 1)) || (g_firmversion == FIRMWARE_VERSION(6, 7, 0))) - return true; - } + if (strstr(g_board_type, "SF600") != NULL) { + if ((g_firmversion > FIRMWARE_VERSION(7, 0, 1)) || (g_firmversion == FIRMWARE_VERSION(6, 7, 0))) + return true; + } return false; } - CHIP_INFO GetFirstDetectionMatch(int Index) { - CHIP_INFO binfo; - binfo.UniqueID=0; - int Found=0; - int i=0; - int Loop=3; - if(strcmp(g_parameter_vcc,"NO") != 0) - Loop=1; - - for(i=0; i +#include "usbdriver.h" +#include "FlashCommand.h" +#include "project.h" #include #include -#include "Macro.h" -#include "usbdriver.h" unsigned int m_nbDeviceDetected = 0; unsigned char DevIndex = 0; @@ -10,20 +10,15 @@ extern volatile bool g_bIsSF600[16]; extern int g_CurrentSeriase; extern char g_board_type[8]; extern int g_firmversion; -extern void Sleep(unsigned int ms); extern CHIP_INFO Chip_Info; extern unsigned int g_uiDevNum; extern bool isSendFFsequence; - #define SerialFlash_FALSE -1 #define SerialFlash_TRUE 1 static usb_dev_handle *dediprog_handle[MAX_Dev_Index]; -extern int is_SF100nBoardVersionGreaterThan_5_5_0(int Inde); -extern int is_SF600nBoardVersionGreaterThan_6_9_0(int Inde); - bool Is_NewUSBCommand(int Index) { if(is_SF100nBoardVersionGreaterThan_5_5_0(Index) || is_SF600nBoardVersionGreaterThan_6_9_0(Index)) @@ -42,8 +37,7 @@ void usb_dev_init(void) void usb_db_init(void) { int i; - for(i=0; inext) - { - for (dev = bus->devices; dev; dev = dev->next) - { - if ((dev->descriptor.idVendor == vid) && (dev->descriptor.idProduct == pid)) - { + for (bus = usb_get_busses(); bus; bus = bus->next) { + for (dev = bus->devices; dev; dev = dev->next) { + if ((dev->descriptor.idVendor == vid) && (dev->descriptor.idProduct == pid)) { usb_device_entry[dev_index].usb_device_handler = *dev; usb_device_entry[dev_index].valid = 1; dev_index++; @@ -109,38 +98,38 @@ static int FindUSBDevice(void) } } - printf(" \n"); return dev_index; } - int OutCtrlRequest( CNTRPIPE_RQ *rq, unsigned char *buf, unsigned long buf_size ,int Index ) { -// unsigned long bytesWrite; int requesttype; int ret = 0; - if( Index==-1 ) - Index = DevIndex; + if (Index == -1) Index = DevIndex; if( (rq->Function!=URB_FUNCTION_VENDOR_ENDPOINT) && (g_bIsSF600[Index]==true)) return true; requesttype = 0x00; - if( rq->Direction==VENDOR_DIRECTION_IN ) requesttype |= 0x80; - if( rq->Function==URB_FUNCTION_VENDOR_DEVICE ) requesttype |= 0x40; - if( rq->Function==URB_FUNCTION_VENDOR_INTERFACE ) requesttype |= 0x41; - if( rq->Function==URB_FUNCTION_VENDOR_ENDPOINT ) requesttype |= 0x42; - if( rq->Function==URB_FUNCTION_VENDOR_OTHER ) requesttype |= 0x43; - + if (rq->Direction == VENDOR_DIRECTION_IN) + requesttype |= 0x80; + if (rq->Function == URB_FUNCTION_VENDOR_DEVICE) + requesttype |= 0x40; + if (rq->Function == URB_FUNCTION_VENDOR_INTERFACE) + requesttype |= 0x41; + if (rq->Function == URB_FUNCTION_VENDOR_ENDPOINT) + requesttype |= 0x42; + if (rq->Function == URB_FUNCTION_VENDOR_OTHER) + requesttype |= 0x43; if (dediprog_handle[Index] ) { ret = usb_control_msg(dediprog_handle[Index], requesttype, rq->Request, rq->Value, rq->Index, (char*)buf, buf_size, DEFAULT_TIMEOUT); - }// else - // printf("no device"); - if(ret != buf_size) - { + } else { + printf("no device"); + } + if (ret != buf_size) { #if 0 printf("Control Pipe output error!\n"); printf("rq->Direction=%X\n",rq->Direction); @@ -153,52 +142,52 @@ int OutCtrlRequest( CNTRPIPE_RQ *rq, unsigned char *buf, unsigned long buf_size printf("buf[0]=%X\n",buf[0]); printf("g_bIsSF600=%d\n",g_bIsSF600); #endif - //printf("Error=0x%x\r\n",usb_strerror()); + printf("Error: %s\n", usb_strerror()); return -1; } return ret; } - int InCtrlRequest( CNTRPIPE_RQ *rq, unsigned char *buf, unsigned long buf_size, int Index ) { - //boost::mutex::scoped_lock l(mutex); - -// unsigned long bytesRead; int requesttype; int ret = 0; - if( (rq->Function!=URB_FUNCTION_VENDOR_ENDPOINT) && (g_bIsSF600[Index]==true)) return true; + if ((rq->Function != URB_FUNCTION_VENDOR_ENDPOINT) && (g_bIsSF600[Index] == true)) + return true; if( Index==-1 ) Index = DevIndex; if(sizeof(buf) == 0) return 0; - requesttype = 0x00; - if( rq->Direction==VENDOR_DIRECTION_IN ) requesttype |= 0x80; - if( rq->Function==URB_FUNCTION_VENDOR_DEVICE ) requesttype |= 0x40; - if( rq->Function==URB_FUNCTION_VENDOR_INTERFACE ) requesttype |= 0x41; - if( rq->Function==URB_FUNCTION_VENDOR_ENDPOINT ) requesttype |= 0x42; - if( rq->Function==URB_FUNCTION_VENDOR_OTHER ) requesttype |= 0x43; + if (rq->Direction == VENDOR_DIRECTION_IN) + requesttype |= 0x80; + if (rq->Function == URB_FUNCTION_VENDOR_DEVICE) + requesttype |= 0x40; + if (rq->Function == URB_FUNCTION_VENDOR_INTERFACE) + requesttype |= 0x41; + if (rq->Function == URB_FUNCTION_VENDOR_ENDPOINT) + requesttype |= 0x42; + if (rq->Function == URB_FUNCTION_VENDOR_OTHER) + requesttype |= 0x43; if (dediprog_handle[Index] ) { ret = usb_control_msg(dediprog_handle[Index], requesttype, rq->Request, rq->Value, rq->Index, (char*)buf, buf_size, DEFAULT_TIMEOUT); - }// else - // printf("no device"); + } else { + printf("no device"); + } - if(ret != buf_size) - { -// printf("Control Pipe input error!\n"); + if (ret != buf_size) { + printf("Control Pipe input error!\n"); return -1; } return ret; } - // part of USB driver , open usb pipes for data transfor // should be called after usb successfully opens pipes. int dediprog_start_appli(int Index) @@ -246,7 +235,6 @@ int dediprog_get_chipid(int Index) ret = OutCtrlRequest(&rq, vInstruction, 1, Index); - // special instruction memset(vInstruction, 0, 3); @@ -267,7 +255,8 @@ int dediprog_get_chipid(int Index) int BulkPipeRead(unsigned char *pBuff, unsigned int timeOut, int Index) { int ret; - if( Index==-1 ) Index = DevIndex; + if (Index == -1) + Index = DevIndex; unsigned long cnRead = 512; ret = usb_bulk_read(dediprog_handle[Index], 0x82, (char*)pBuff, cnRead, DEFAULT_TIMEOUT); @@ -275,7 +264,6 @@ int BulkPipeRead(unsigned char *pBuff, unsigned int timeOut, int Index) return cnRead ; } - //long CUSB::BulkPipeWrite(PBYTE pBuff, UINT sz, UINT timeOut) const int BulkPipeWrite(unsigned char *pBuff, unsigned int size,unsigned int timeOut, int Index) { @@ -287,14 +275,14 @@ int BulkPipeWrite(unsigned char *pBuff, unsigned int size,unsigned int timeOut, memcpy(pData, pBuff , size); - if( Index==-1 ) Index = DevIndex; + if (Index == -1) + Index = DevIndex; ret = usb_bulk_write(dediprog_handle[Index], (g_bIsSF600[Index]==true)? 0x01:0x02,pData, nWrite, DEFAULT_TIMEOUT); nWrite = ret; return nWrite; } - int dediprog_set_spi_voltage(int v,int Index) { int ret = 0; @@ -302,31 +290,26 @@ int dediprog_set_spi_voltage(int v,int Index) CNTRPIPE_RQ rq ; // unsigned char vBuffer[12]; - if(0 == v) Sleep(200); + if (0 == v) + Sleep(200); rq.Function = URB_FUNCTION_VENDOR_ENDPOINT ; rq.Direction = VENDOR_DIRECTION_OUT ; rq.Request = SET_VCC ; rq.Length = 0 ; - if(Is_NewUSBCommand(Index)) - { + if (Is_NewUSBCommand(Index)) { rq.Value = v ; rq.Index = 0; - } - else - { + } else { rq.Value = v ; rq.Index = 0x04 | g_CurrentSeriase; // ID detect mode } ret = OutCtrlRequest(&rq, NULL, 0, Index); - - if(0 != v) - { + if (0 != v) { Sleep(200); - if(isSendFFsequence) - { + if (isSendFFsequence) { unsigned char v[4]={0xff,0xff,0xff,0xff}; FlashCommand_SendCommand_OutOnlyInstruction(v,4,Index); } @@ -335,14 +318,12 @@ int dediprog_set_spi_voltage(int v,int Index) return ret; } - int dediprog_set_vpp_voltage(int volt,int Index) { int ret; int voltage_selector; CNTRPIPE_RQ rq ; - switch (volt) { case 0: /* Admittedly this one is an assumption. */ @@ -382,7 +363,6 @@ int dediprog_set_spi_clk(int khz,int Index) //IsSF600(Index); CNTRPIPE_RQ rq ; - switch (khz) { case 24000: /* Admittedly this one is an assumption. */ @@ -432,7 +412,6 @@ int dediprog_set_spi_clk(int khz,int Index) return 0; } - int usb_driver_init(void) { // struct usb_bus *bus; @@ -441,40 +420,33 @@ int usb_driver_init(void) int device_cnt = 0; int ret; - for(int i=0;i #include #include - -#define DEDI_USB_DRIVER 1 +#include #define URB_FUNCTION_VENDOR_DEVICE 0x0017 #define URB_FUNCTION_VENDOR_INTERFACE 0x0018 @@ -44,10 +44,8 @@ typedef struct { unsigned long Length; } CNTRPIPE_RQ, *PCNTRPIPE_RQ; - usb_device_entry_t usb_device_entry[MAX_Dev_Index]; - /* Set/clear LEDs on dediprog */ #define PASS_ON (0 << 0) #define PASS_OFF (1 << 0) @@ -70,11 +68,8 @@ int BulkPipeRead(unsigned char *pBuff, unsigned int timeOut, int Index); int dediprog_set_spi_voltage(int millivolt, int Index); int dediprog_set_vpp_voltage(int volt,int Index); - long flash_ReadId(unsigned int read_id_code, unsigned int out_data_size ,int Index); - int BulkPipeWrite(unsigned char *pBuff, unsigned int size,unsigned int timeOut, int Index); #endif //DEDI_USB_DRIVER -