From befae166f61622f80bcb0fb7e810dac2b275fdbc Mon Sep 17 00:00:00 2001 From: Stefan Reinauer Date: Fri, 20 Nov 2020 20:21:57 -0800 Subject: [PATCH] clang-format -i -style=WebKit re-format the code to be consistent and have correct indentation. Signed-off-by: Stefan Reinauer --- ChipInfoDb.h | 14 +- FlashCommand.c | 2 +- FlashCommand.h | 20 +- IntelHexFile.c | 3 +- IntelHexFile.h | 6 +- Macro.h | 304 +++--- MotorolaFile.c | 9 +- MotorolaFile.h | 4 +- SerialFlash.c | 2697 ++++++++++++++++++++++++------------------------ SerialFlash.h | 112 +- board.h | 36 +- dpcmd.c | 1654 +++++++++++++++-------------- dpcmd.h | 32 +- project.c | 40 +- usbdriver.c | 592 +++++------ usbdriver.h | 66 +- 16 files changed, 2807 insertions(+), 2784 deletions(-) diff --git a/ChipInfoDb.h b/ChipInfoDb.h index 34199a5..3fd3390 100644 --- a/ChipInfoDb.h +++ b/ChipInfoDb.h @@ -5,15 +5,15 @@ #include "Macro.h" -#define NUMBER_OF_SUPPORTING_CHIPS 3 +#define NUMBER_OF_SUPPORTING_CHIPS 3 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); - int (*m_code_api_doProgram)(void); - int (*m_code_api_doRead)(char *name); - int (*m_code_api_doSegmentErase)(void); + 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); + int (*m_code_api_doProgram)(void); + int (*m_code_api_doRead)(char* name); + int (*m_code_api_doSegmentErase)(void); }; bool Dedi_List_AllChip(void); diff --git a/FlashCommand.c b/FlashCommand.c index 7bedadc..9a6cecf 100644 --- a/FlashCommand.c +++ b/FlashCommand.c @@ -1,7 +1,7 @@ /// CFlashCommand Implementations -#include "Macro.h" #include "FlashCommand.h" +#include "Macro.h" #include "project.h" #include "usbdriver.h" #define SerialFlash_FALSE -1 diff --git a/FlashCommand.h b/FlashCommand.h index cd0e88e..2190a24 100644 --- a/FlashCommand.h +++ b/FlashCommand.h @@ -5,23 +5,23 @@ #include "Macro.h" -#define FlashCommand_TRUE 1 -#define FlashCommand_FALSE 0 +#define FlashCommand_TRUE 1 +#define FlashCommand_FALSE 0 -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); -int FlashCommand_TransceiveIn(unsigned char *v, int len,int Index); +int FlashCommand_TransceiveIn(unsigned char* v, int len, int Index); -int FlashCommand_SendCommand_OutOnlyInstruction(unsigned char *v, int len, int Index); +int FlashCommand_SendCommand_OutOnlyInstruction(unsigned char* v, int len, int Index); -int FlashCommand_SendCommand_OutInstructionWithCS(unsigned char *v,int len, int Index); +int FlashCommand_SendCommand_OutInstructionWithCS(unsigned char* v, int len, int 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); -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); -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); -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); #endif //FLASHCOMMANDS diff --git a/IntelHexFile.c b/IntelHexFile.c index 9e501a8..d0a60a5 100644 --- a/IntelHexFile.c +++ b/IntelHexFile.c @@ -168,7 +168,8 @@ bool HexFileToBin(const char* filePath, unsigned char* vOutData, unsigned long* return false; // 0x0a != Line[0] && NULL != Line[0]) return false; } - if (Line[i] == '\n') Line[i] = '\0'; + 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 diff --git a/IntelHexFile.h b/IntelHexFile.h index 9ab16eb..9278e81 100644 --- a/IntelHexFile.h +++ b/IntelHexFile.h @@ -130,16 +130,16 @@ typedef struct { unsigned char intel_lg_data; unsigned short intel_adr; unsigned char intel_type; - unsigned char intel_data [LL_MAX_LINE]; + unsigned char intel_data[LL_MAX_LINE]; unsigned char intel_lrc; } t_one_line; #define INTEL_DATA_TYPE 0 //save binary data in vBuffer to file in Intel Hex format -bool BinToHexFile(const char* filePath, unsigned char* vBuffer, unsigned long FileSize) ; +bool BinToHexFile(const char* filePath, unsigned char* vBuffer, unsigned long FileSize); // read binary data from Intel Hex file -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); #endif // end of header file diff --git a/Macro.h b/Macro.h index d7302e2..e7a8ae1 100644 --- a/Macro.h +++ b/Macro.h @@ -3,25 +3,25 @@ #ifndef _MACRO_H #define _MACRO_H -#include #include #include +#include #include // new defined macros //programmer info RQ -#define PROGINFO_REQUEST 0x08 -#define SET_VCC 0x09 ///< set VCC -#define SET_VPP 0x03 ///< set VPP -#define SET_CS 0x14 -#define SET_IOMODE 0x15 -#define SET_SPICLK 0x61 -#define SET_HOLD 0x1D -#define SET_SA 0x0A +#define PROGINFO_REQUEST 0x08 +#define SET_VCC 0x09 ///< set VCC +#define SET_VPP 0x03 ///< set VPP +#define SET_CS 0x14 +#define SET_IOMODE 0x15 +#define SET_SPICLK 0x61 +#define SET_HOLD 0x1D +#define SET_SA 0x0A //first field of RQ -#define OUT_REQUEST 0x42 -#define IN_REQUEST 0xC2 +#define OUT_REQUEST 0x42 +#define IN_REQUEST 0xC2 //second field of RQ in case of bulk transfer //#define TRANSCEIVE 0x01 @@ -31,29 +31,29 @@ //#define WRITE_EEPROM 0x06 // values of Request Field of a setup packet typedef struct FirmwareInfo { - char Programmer[20]; - char Version[10]; - char FPGAVersion[10]; - unsigned int dwSignature; - unsigned int Rev[3]; - unsigned int FirstIndex; - unsigned int FirstSize; - unsigned int SecondIndex; - unsigned int SecondSize; -}FW_INFO; + char Programmer[20]; + char Version[10]; + char FPGAVersion[10]; + unsigned int dwSignature; + unsigned int Rev[3]; + unsigned int FirstIndex; + unsigned int FirstSize; + unsigned int SecondIndex; + unsigned int SecondSize; +} FW_INFO; typedef enum { - TRANSCEIVE = 0x01, + TRANSCEIVE = 0x01, - DTC_READ = 0x20, - WRITE = 0x30, + DTC_READ = 0x20, + WRITE = 0x30, - ATMEL45_WRITE = 0x31, + ATMEL45_WRITE = 0x31, - READ_EEPROM = 0x05, - WRITE_EEPROM = 0x06, - GET_BUTTON_STATUS = 0x11, -}USB_CMD; + READ_EEPROM = 0x05, + WRITE_EEPROM = 0x06, + GET_BUTTON_STATUS = 0x11, +} USB_CMD; typedef struct ChipInfo { char TypeName[100]; @@ -87,63 +87,63 @@ typedef struct ChipInfo { size_t RDIDCommand; size_t Timeout; size_t VoltageInMv; -}CHIP_INFO; +} CHIP_INFO; //third field of RQ -#define CTRL_TIMEOUT 3000 ///< time out for control pipe or for firmwire +#define CTRL_TIMEOUT 3000 ///< time out for control pipe or for firmwire // fourth field of RQ -#define RESULT_IN 0x01 ///< result in or not -#define NO_RESULT_IN 0x00 -#define GET_REGISTER 0x01 ///< get register value or not -#define NO_REGISTER 0x00 -#define RFU 0x00 +#define RESULT_IN 0x01 ///< result in or not +#define NO_RESULT_IN 0x00 +#define GET_REGISTER 0x01 ///< get register value or not +#define NO_REGISTER 0x00 +#define RFU 0x00 // bulk write mode -#define PAGE_PROGRAM 0x01 ///< pp via bulk pipes -#define PAGE_WRITE 0x02 ///< pw via bulk pipes -#define AAI_1_BYTE 0x03 -#define AAI_2_BYTE 0x04 -#define PP_128BYTE 0x05 -#define PP_AT26DF041 0x06 ///< PP AT26DF041 -#define PP_SB_FPGA 0x07 ///< Silicon Blue FPGA -#define MODE_NUMONYX_PCM 0x08 ///< Mode_Numonyx_pcm -#define PP_4ADR_256BYTE 0x09 -#define PP_32BYTE 0x0A +#define PAGE_PROGRAM 0x01 ///< pp via bulk pipes +#define PAGE_WRITE 0x02 ///< pw via bulk pipes +#define AAI_1_BYTE 0x03 +#define AAI_2_BYTE 0x04 +#define PP_128BYTE 0x05 +#define PP_AT26DF041 0x06 ///< PP AT26DF041 +#define PP_SB_FPGA 0x07 ///< Silicon Blue FPGA +#define MODE_NUMONYX_PCM 0x08 ///< Mode_Numonyx_pcm +#define PP_4ADR_256BYTE 0x09 +#define PP_32BYTE 0x0A #define PP_4ADDR_256BYTE_12 0x0B #define PP_4ADDR_256BYTE_MICROM 0x0C #define PP_4ADDR_256BYTE_S70FS01GS 0x0D // bulk read mode -#define BULK_NORM_READ 0x01 ///< read via bulk pipes -#define BULK_FAST_READ 0x02 ///< fast read via bulk pipes -#define BULK_AT45xx_READ 0x03 ///< fast read via bulk pipes -#define BULK_4BYTE_FAST_READ 0x04 ///< For size is bigger than 128Mb -#define BULK_4BYTE_FAST_READ_MICRON 0x05 +#define BULK_NORM_READ 0x01 ///< read via bulk pipes +#define BULK_FAST_READ 0x02 ///< fast read via bulk pipes +#define BULK_AT45xx_READ 0x03 ///< fast read via bulk pipes +#define BULK_4BYTE_FAST_READ 0x04 ///< For size is bigger than 128Mb +#define BULK_4BYTE_FAST_READ_MICRON 0x05 //for flash card -#define POLLING 0x02 ///< polling -#define SET_VPP 0x03 ///< set vpp -#define VPP_OFF 0x00 ///< vpp value -#define VPP_9V 0x01 -#define VPP_12V 0x02 +#define POLLING 0x02 ///< polling +#define SET_VPP 0x03 ///< set vpp +#define VPP_OFF 0x00 ///< vpp value +#define VPP_9V 0x01 +#define VPP_12V 0x02 -#define SET_TARGET_FLASH 0x04 ///< set target FLASH_TRAY -#define APPLICATION_MEMORY_1 0x00 ///< application memory chip 1 -#define FLASH_CARD 0x01 ///< flash card -#define APPLICATION_MEMORY_2 0x02 ///< application memory chip 2 +#define SET_TARGET_FLASH 0x04 ///< set target FLASH_TRAY +#define APPLICATION_MEMORY_1 0x00 ///< application memory chip 1 +#define FLASH_CARD 0x01 ///< flash card +#define APPLICATION_MEMORY_2 0x02 ///< application memory chip 2 //for io & LED -#define SET_IO 0x07 ///< request +#define SET_IO 0x07 ///< request //IO number -#define IO_1 0x01 -#define IO_2 0x02 -#define IO_3 0x04 -#define IO_4 0x08 +#define IO_1 0x01 +#define IO_2 0x02 +#define IO_3 0x04 +#define IO_4 0x08 //led number -#define LED_RED 0x01 -#define LED_GREEN 0x02 -#define LED_ORANGE 0x04 +#define LED_RED 0x01 +#define LED_GREEN 0x02 +#define LED_ORANGE 0x04 #define SUPPORT_ST #define SUPPORT_SST @@ -167,111 +167,111 @@ typedef struct ChipInfo { // memory support list #ifdef SUPPORT_NANTRONICS - #define SUPPORT_NANTRONICS_N25Sxx "N25Sxx" +#define SUPPORT_NANTRONICS_N25Sxx "N25Sxx" #endif #ifdef SUPPORT_ATO - #define SUPPORT_ATO_ATO25Qxx "ATO25Qxx" +#define SUPPORT_ATO_ATO25Qxx "ATO25Qxx" #endif #ifdef SUPPORT_ST - #define SUPPORT_ST_M25PExx "M25PExx" - #define SUPPORT_ST_M25Pxx "M25Pxx" - #define SUPPORT_ST_M45PExx "M45PExx" - #define SUPPORT_NUMONYX_Alverstone "Alverstone" - #define SUPPORT_NUMONYX_N25Qxxx_Large "N25Qxxx_Large" - #define SUPPORT_NUMONYX_N25Qxxx_Large_2Die "N25Qxxx_Large_2Die" - #define SUPPORT_NUMONYX_N25Qxxx_Large_4Die "N25Qxxx_Large_4Die" +#define SUPPORT_ST_M25PExx "M25PExx" +#define SUPPORT_ST_M25Pxx "M25Pxx" +#define SUPPORT_ST_M45PExx "M45PExx" +#define SUPPORT_NUMONYX_Alverstone "Alverstone" +#define SUPPORT_NUMONYX_N25Qxxx_Large "N25Qxxx_Large" +#define SUPPORT_NUMONYX_N25Qxxx_Large_2Die "N25Qxxx_Large_2Die" +#define SUPPORT_NUMONYX_N25Qxxx_Large_4Die "N25Qxxx_Large_4Die" #endif #ifdef SUPPORT_SST - #define SUPPORT_SST_25xFxxA "25xFxxA" - #define SUPPORT_SST_25xFxxB "25xFxxB" - #define SUPPORT_SST_25xFxxC "25xFxxC" - #define SUPPORT_SST_25xFxx "25xFxx" - #define SUPPORT_SST_26xFxxC "26VFxxC" +#define SUPPORT_SST_25xFxxA "25xFxxA" +#define SUPPORT_SST_25xFxxB "25xFxxB" +#define SUPPORT_SST_25xFxxC "25xFxxC" +#define SUPPORT_SST_25xFxx "25xFxx" +#define SUPPORT_SST_26xFxxC "26VFxxC" #endif #ifdef SUPPORT_WINBOND - #define SUPPORT_WINBOND_W25Bxx "W25Bxx" - #define SUPPORT_WINBOND_W25Pxx "W25Pxx" - #define SUPPORT_WINBOND_W25Pxx_Large "W25Pxx_Large" - #define SUPPORT_WINBOND_W25Xxx "W25Xxx" +#define SUPPORT_WINBOND_W25Bxx "W25Bxx" +#define SUPPORT_WINBOND_W25Pxx "W25Pxx" +#define SUPPORT_WINBOND_W25Pxx_Large "W25Pxx_Large" +#define SUPPORT_WINBOND_W25Xxx "W25Xxx" #endif #ifdef SUPPORT_PMC - #define SUPPORT_PMC_PM25LVxxx "PM25LVxxx" - #define SUPPORT_PMC_PM25Wxxx "PM25Wxxx" +#define SUPPORT_PMC_PM25LVxxx "PM25LVxxx" +#define SUPPORT_PMC_PM25Wxxx "PM25Wxxx" #endif #ifdef SUPPORT_SPANSION - #define SUPPORT_SPANSION_S25FLxx "S25FLxxx" - #define SUPPORT_SPANSION_S25FLxx_Large "S25FLxxx_Large" - #define SUPPORT_SPANSION_S70FSxx_Large "S70FSxxx_Large" +#define SUPPORT_SPANSION_S25FLxx "S25FLxxx" +#define SUPPORT_SPANSION_S25FLxx_Large "S25FLxxx_Large" +#define SUPPORT_SPANSION_S70FSxx_Large "S70FSxxx_Large" #endif #ifdef SUPPORT_MACRONIX - #define SUPPORT_MACRONIX_MX25Lxxx "MX25Lxxx" - #define SUPPORT_MACRONIX_MX25Lxxx_Large "MX25Lxxx_Large" - #define SUPPORT_MACRONIX_MX25Lxxx_PP32 "MX25Lxxx_PP32" +#define SUPPORT_MACRONIX_MX25Lxxx "MX25Lxxx" +#define SUPPORT_MACRONIX_MX25Lxxx_Large "MX25Lxxx_Large" +#define SUPPORT_MACRONIX_MX25Lxxx_PP32 "MX25Lxxx_PP32" #endif #ifdef SUPPORT_EON - #define SUPPORT_EON_EN25Xxx "EN25Xxx" - #define SUPPORT_EON_EN25QHxx_Large "EN25QHxx_Large" +#define SUPPORT_EON_EN25Xxx "EN25Xxx" +#define SUPPORT_EON_EN25QHxx_Large "EN25QHxx_Large" #endif #ifdef SUPPORT_ATMEL - #define SUPPORT_ATMEL_AT26xxx "AT26xxx" - #define SUPPORT_ATMEL_AT25Fxxx "AT25Fxxx" - #define SUPPORT_ATMEL_AT25FSxxx "AT25FSxxx" - #define SUPPORT_ATMEL_45DBxxxD "AT45DBxxxD" - #define SUPPORT_ATMEL_45DBxxxB "AT45DBxxxB" +#define SUPPORT_ATMEL_AT26xxx "AT26xxx" +#define SUPPORT_ATMEL_AT25Fxxx "AT25Fxxx" +#define SUPPORT_ATMEL_AT25FSxxx "AT25FSxxx" +#define SUPPORT_ATMEL_45DBxxxD "AT45DBxxxD" +#define SUPPORT_ATMEL_45DBxxxB "AT45DBxxxB" #endif #ifdef SUPPORT_AMIC - #define SUPPORT_AMIC_A25Lxxx "A25Lxxx" - #define SUPPORT_AMIC_A25LQxxx "A25LQxxx" +#define SUPPORT_AMIC_A25Lxxx "A25Lxxx" +#define SUPPORT_AMIC_A25LQxxx "A25LQxxx" #endif #ifdef SUPPORT_ESMT - #define SUPPORT_ESMT_F25Lxx "F25Lxx" +#define SUPPORT_ESMT_F25Lxx "F25Lxx" #endif #ifdef SUPPORT_INTEL - #define SUPPORT_INTEL_S33 "S33" +#define SUPPORT_INTEL_S33 "S33" #endif #ifdef SUPPORT_FREESCALE - #define SUPPORT_FREESCALE_MCF "MCF" +#define SUPPORT_FREESCALE_MCF "MCF" #endif #ifdef SUPPORT_SANYO - #define SUPPORT_SANYO_LE25FWxxx "LE25FWxxx" +#define SUPPORT_SANYO_LE25FWxxx "LE25FWxxx" #endif #ifdef SUPPORT_TSI - #define SUPPORT_TSI_TS25Lxx "TS25Lxx" - #define SUPPORT_TSI_TS25Lxx_0A "TS25Lxx_0A" +#define SUPPORT_TSI_TS25Lxx "TS25Lxx" +#define SUPPORT_TSI_TS25Lxx_0A "TS25Lxx_0A" #endif #ifdef SUPPORT_SILICONBLUE - #define SUPPORT_SILICONBLUE_iCE65 "iCE65" +#define SUPPORT_SILICONBLUE_iCE65 "iCE65" #endif #ifdef SUPPORT_FIDELIX - #define SUPPORT_FIDELIX_FM25Qxx "FM25Qxx" +#define SUPPORT_FIDELIX_FM25Qxx "FM25Qxx" #endif #ifdef SUPPORT_FUDAN - #define SUPPORT_FUDAN_FM25Fxx "FM25Fxx" +#define SUPPORT_FUDAN_FM25Fxx "FM25Fxx" #endif // for usb -#define USB_TIMEOUT 800000 ///< time out value for usb EP2 +#define USB_TIMEOUT 800000 ///< time out value for usb EP2 // for SR reads -#define MAX_TRIALS 0x80000 +#define MAX_TRIALS 0x80000 #define BIT0 0x01 #define BIT1 0x02 @@ -312,43 +312,43 @@ struct CAddressRange { }; struct memory_id { - char TypeName[20]; - size_t UniqueID; - char Class[20]; - char Description[20]; - - char Manufacturer[20]; - char ManufactureUrl[20]; - char Voltage[20]; - char Clock[20]; - char ProgramIOMethod[20]; - - size_t ManufactureID; - size_t JedecDeviceID; - size_t AlternativeID; - - size_t ChipSizeInByte; - size_t SectorSizeInByte; - size_t PageSizeInByte; - size_t BlockSizeInByte; - - size_t MaxErasableSegmentInByte; - - size_t AddrWidth; - - bool DualID; - size_t VppSupport; - bool MXIC_WPmode; - size_t IDNumber; - size_t RDIDCommand; - size_t Timeout; + char TypeName[20]; + size_t UniqueID; + char Class[20]; + char Description[20]; + + char Manufacturer[20]; + char ManufactureUrl[20]; + char Voltage[20]; + char Clock[20]; + char ProgramIOMethod[20]; + + size_t ManufactureID; + size_t JedecDeviceID; + size_t AlternativeID; + + size_t ChipSizeInByte; + size_t SectorSizeInByte; + size_t PageSizeInByte; + size_t BlockSizeInByte; + + size_t MaxErasableSegmentInByte; + + size_t AddrWidth; + + bool DualID; + size_t VppSupport; + bool MXIC_WPmode; + size_t IDNumber; + size_t RDIDCommand; + size_t Timeout; }; enum { - SITE_NORMAL=0, - SITE_BUSY, - SITE_ERROR, - SITE_OK, + SITE_NORMAL = 0, + SITE_BUSY, + SITE_ERROR, + SITE_OK, }; enum { @@ -359,12 +359,12 @@ enum { typedef enum { vccPOWEROFF = 0x00, - vcc3_5V = 0x10, - vcc2_5V = 0x11, - vcc1_8V = 0x12, + vcc3_5V = 0x10, + vcc2_5V = 0x11, + vcc1_8V = 0x12, vccdo_nothing = 0xFF, } VCC_VALUE; -#endif //_MACRO_H +#endif //_MACRO_H diff --git a/MotorolaFile.c b/MotorolaFile.c index fa934f5..b276b36 100644 --- a/MotorolaFile.c +++ b/MotorolaFile.c @@ -177,10 +177,12 @@ bool S19FileToBin(const char* filePath, unsigned char* vData, unsigned long* Fil /* Remove carriage return/line feed at the end of line. */ i = strlen(Line) - 1; - if (Line[i] == '\n') Line[i] = '\0'; + 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' */ @@ -326,7 +328,6 @@ bool S19FileToBin(const char* filePath, unsigned char* vData, unsigned long* Fil default: break; - } if (Starting_Address_Setted) { @@ -400,7 +401,7 @@ bool BinToS19File(const char* filePath, unsigned char* vData, unsigned long File chksum += (addr >> 16) & 0xff; chksum += (addr >> 24) & 0xff; - fprintf(FileOut, "S3"); /* record header preamble */ + fprintf(FileOut, "S3"); /* record header preamble */ fprintf(FileOut, "%02X%08X", total + 5, addr); memset(s19buf, 0, sizeof(s19buf)); diff --git a/MotorolaFile.h b/MotorolaFile.h index b40f99b..a811b2b 100644 --- a/MotorolaFile.h +++ b/MotorolaFile.h @@ -5,7 +5,7 @@ #include -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); bool BinToS19File(const char* filePath, unsigned char* vData, unsigned long FileSize); -#endif //_MOTOROLA_FILE_H +#endif //_MOTOROLA_FILE_H diff --git a/SerialFlash.c b/SerialFlash.c index 46a6387..c74bf5d 100644 --- a/SerialFlash.c +++ b/SerialFlash.c @@ -3,8 +3,8 @@ #include "FlashCommand.h" #include "project.h" #include "usbdriver.h" -#include #include +#include extern int m_isCanceled; extern int m_bProtectAfterWritenErase; @@ -14,62 +14,62 @@ extern CHIP_INFO Chip_Info; extern volatile bool g_bIsSF600[16]; extern bool Is_NewUSBCommand(int Index); -unsigned char mcode_WRSR=0x01; -unsigned char mcode_WRDI=0x04; -unsigned char mcode_RDSR=0x05; -unsigned char mcode_WREN=0x06; -unsigned char mcode_SegmentErase=SE; -unsigned char mcode_ChipErase=CHIP_ERASE; -unsigned char mcode_Program=PAGE_PROGRAM; -unsigned char mcode_ProgramCode_4Adr=0x02; -unsigned char mcode_ProgramCode_4Adr_S70FSxxx=0x12; -unsigned char mcode_Read=BULK_FAST_READ; -unsigned char mcode_ReadCode=0x0B; -unsigned char mcode_ReadCode_S70FSxxx=0x0C; -unsigned int g_AT45_PageSize=0; -unsigned int g_AT45_PageSizeMask=0; -size_t AT45ChipSize=0; -size_t AT45PageSize=0; +unsigned char mcode_WRSR = 0x01; +unsigned char mcode_WRDI = 0x04; +unsigned char mcode_RDSR = 0x05; +unsigned char mcode_WREN = 0x06; +unsigned char mcode_SegmentErase = SE; +unsigned char mcode_ChipErase = CHIP_ERASE; +unsigned char mcode_Program = PAGE_PROGRAM; +unsigned char mcode_ProgramCode_4Adr = 0x02; +unsigned char mcode_ProgramCode_4Adr_S70FSxxx = 0x12; +unsigned char mcode_Read = BULK_FAST_READ; +unsigned char mcode_ReadCode = 0x0B; +unsigned char mcode_ReadCode_S70FSxxx = 0x0C; +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 ; - unsigned char vInstruction; //size 1 + CNTRPIPE_RQ rq; + unsigned char vInstruction; //size 1 // first control packet vInstruction = 0xD7; - rq.Function = URB_FUNCTION_VENDOR_ENDPOINT ; - rq.Direction = VENDOR_DIRECTION_OUT ; - rq.Request = TRANSCEIVE ; + rq.Function = URB_FUNCTION_VENDOR_ENDPOINT; + rq.Direction = VENDOR_DIRECTION_OUT; + rq.Request = TRANSCEIVE; if (Is_NewUSBCommand(Index)) { - rq.Value = RESULT_IN; - rq.Index = RFU; + rq.Value = RESULT_IN; + rq.Index = RFU; } else { - rq.Value = RFU ; - rq.Index = RESULT_IN ; - } - rq.Length = (unsigned long) 1 ; + rq.Value = RFU; + rq.Index = RESULT_IN; + } + rq.Length = (unsigned long)1; - if(OutCtrlRequest(&rq, &vInstruction, 1, Index) == SerialFlash_FALSE) - return 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 ; + 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)) { - rq.Value = 1; - rq.Index = 0; + rq.Value = 1; + rq.Index = 0; } else { - rq.Value = CTRL_TIMEOUT; - rq.Index = NO_REGISTER; - } - rq.Length = (unsigned long) 1; + rq.Value = CTRL_TIMEOUT; + rq.Index = NO_REGISTER; + } + rq.Length = (unsigned long)1; - if(InCtrlRequest(&rq, &vBuffer, 1, Index) == SerialFlash_FALSE) - return SerialFlash_FALSE ; + if (InCtrlRequest(&rq, &vBuffer, 1, Index) == SerialFlash_FALSE) + return SerialFlash_FALSE; *cSR = vBuffer; @@ -78,17 +78,18 @@ bool AT45doRDSR(unsigned char* cSR, int Index) bool AT45WaitForWIP(int USBIndex) { - unsigned char cSR ; - size_t i = Chip_Info.Timeout*100; - if(i==0) - i=0x10000; + unsigned char cSR; + size_t i = Chip_Info.Timeout * 100; + if (i == 0) + i = 0x10000; // wait until WIP = 0 - do{ - AT45doRDSR(&cSR,USBIndex) ; + do { + AT45doRDSR(&cSR, USBIndex); Sleep(10); - }while((!(cSR & 0x80)) && (i-- > 0)) ; // poll bit 7 + } while ((!(cSR & 0x80)) && (i-- > 0)); // poll bit 7 - if(i<=0) return false; + if (i <= 0) + return false; return true; } @@ -96,37 +97,37 @@ bool AT45WaitForWIP(int USBIndex) unsigned char getWriteMode(int USBIndex) { unsigned char cMode; - unsigned char cSR ; - AT45doRDSR(&cSR,USBIndex); + unsigned char cSR; + AT45doRDSR(&cSR, USBIndex); bool powerOfTwo = (1 == (cSR & 0x1)); -// typedef enum + // typedef enum enum { - AT45DB011D = 0x1F22, - AT45DB021D = 0x1F23, - AT45DB041D = 0x1F24, - AT45DB081D = 0x1F25, - AT45DB161D = 0x1F26, - AT45DB321D = 0x1F27, - AT45DB642D = 0x1F28, - }; + AT45DB011D = 0x1F22, + AT45DB021D = 0x1F23, + AT45DB041D = 0x1F24, + AT45DB081D = 0x1F25, + AT45DB161D = 0x1F26, + AT45DB321D = 0x1F27, + AT45DB642D = 0x1F28, + }; switch (Chip_Info.UniqueID) { - case AT45DB011D: - case AT45DB021D: - case AT45DB041D: - case AT45DB081D: - cMode = powerOfTwo ? 1 : 2; //256 : 264; - break; - case AT45DB161D: - case AT45DB321D: - cMode = powerOfTwo ? 3 : 4; //512 : 528; - break; - case AT45DB642D: - cMode = powerOfTwo ? 5 : 6; //1024 : 1056; - break; - default: - cMode = powerOfTwo ? 1 : 2; //256 : 264; + case AT45DB011D: + case AT45DB021D: + case AT45DB041D: + case AT45DB081D: + cMode = powerOfTwo ? 1 : 2; //256 : 264; + break; + case AT45DB161D: + case AT45DB321D: + cMode = powerOfTwo ? 3 : 4; //512 : 528; + break; + case AT45DB642D: + cMode = powerOfTwo ? 5 : 6; //1024 : 1056; + break; + default: + cMode = powerOfTwo ? 1 : 2; //256 : 264; } return cMode; } @@ -135,18 +136,18 @@ void SetPageSize(CHIP_INFO* mem, int USBIndex) { unsigned char writeMode; writeMode = getWriteMode(USBIndex); - mcode_Program=writeMode; - size_t pageSize[7] = {0, 256, 264, 512, 528, 1024, 1056}; -// size_t pageSizeMask[7] = {0, (1<<8)-1, (1<<9)-1, (1<<9)-1, (1<<10)-1, (1<<10)-1, (1<<11)-1}; - mem->PageSizeInByte=pageSize[writeMode]; + mcode_Program = writeMode; + size_t pageSize[7] = { 0, 256, 264, 512, 528, 1024, 1056 }; + // size_t pageSizeMask[7] = {0, (1<<8)-1, (1<<9)-1, (1<<9)-1, (1<<10)-1, (1<<10)-1, (1<<11)-1}; + mem->PageSizeInByte = pageSize[writeMode]; - if(! (writeMode & 0x1) ) // for AT45DB:0x1F2200 - 0x1F2800 - mem->ChipSizeInByte=Chip_Info.ChipSizeInByte / 256 * 8+Chip_Info.ChipSizeInByte; + if (!(writeMode & 0x1)) // for AT45DB:0x1F2200 - 0x1F2800 + mem->ChipSizeInByte = Chip_Info.ChipSizeInByte / 256 * 8 + Chip_Info.ChipSizeInByte; else - mem->ChipSizeInByte=Chip_Info.ChipSizeInByte ; + mem->ChipSizeInByte = Chip_Info.ChipSizeInByte; - AT45ChipSize=mem->ChipSizeInByte; - AT45PageSize=mem->PageSizeInByte; + AT45ChipSize = mem->ChipSizeInByte; + AT45PageSize = mem->PageSizeInByte; } size_t GetChipSize(void) @@ -159,97 +160,97 @@ size_t GetPageSize(void) return AT45PageSize; } -bool AT45xxx_protectBlock(int bProtect,int Index) +bool AT45xxx_protectBlock(int bProtect, int Index) { - CNTRPIPE_RQ rq ; - unsigned char vInstruction[4]; //size 1 + CNTRPIPE_RQ rq; + unsigned char vInstruction[4]; //size 1 - rq.Function = URB_FUNCTION_VENDOR_ENDPOINT ; - rq.Direction = VENDOR_DIRECTION_OUT ; - rq.Request = TRANSCEIVE ; + rq.Function = URB_FUNCTION_VENDOR_ENDPOINT; + rq.Direction = VENDOR_DIRECTION_OUT; + rq.Request = TRANSCEIVE; if (Is_NewUSBCommand(Index)) { - rq.Value = RESULT_IN; - rq.Index = RFU; + rq.Value = RESULT_IN; + rq.Index = RFU; } else { - rq.Value = RFU ; - rq.Index = RESULT_IN ; - } - rq.Length = (unsigned long) 4 ; + rq.Value = RFU; + rq.Index = RESULT_IN; + } + rq.Length = (unsigned long)4; // protect block ,set BP1 BP0 to 1 if (bProtect) { - vInstruction[0]=0x3D; - vInstruction[1]=0x2A; - vInstruction[2]=0x7F; - vInstruction[3]=0xA9; + vInstruction[0] = 0x3D; + vInstruction[1] = 0x2A; + vInstruction[2] = 0x7F; + vInstruction[3] = 0xA9; } else { - vInstruction[0]=0x3D; - vInstruction[1]=0x2A; - vInstruction[2]=0x7F; - vInstruction[3]=0x9A; + vInstruction[0] = 0x3D; + vInstruction[1] = 0x2A; + vInstruction[2] = 0x7F; + vInstruction[3] = 0x9A; } - if(OutCtrlRequest(&rq, vInstruction, 4, Index) == SerialFlash_FALSE) - return false ; + if (OutCtrlRequest(&rq, vInstruction, 4, Index) == SerialFlash_FALSE) + return false; return true; } -bool AT45rangeProgram(struct CAddressRange* AddrRange, unsigned char *vData, unsigned char modeWrite, unsigned char WriteCom, int Index) +bool AT45rangeProgram(struct CAddressRange* AddrRange, unsigned char* vData, unsigned char modeWrite, unsigned char WriteCom, int Index) { - CHIP_INFO mem_id; + CHIP_INFO mem_id; - SetPageSize(&mem_id,Index); - modeWrite=getWriteMode(Index); - size_t packageNum = (AddrRange->end - AddrRange->start + mem_id.PageSizeInByte- 1) / mem_id.PageSizeInByte; - size_t pageSize[2] = { 264, 256}; + SetPageSize(&mem_id, Index); + modeWrite = getWriteMode(Index); + size_t packageNum = (AddrRange->end - AddrRange->start + mem_id.PageSizeInByte - 1) / mem_id.PageSizeInByte; + size_t pageSize[2] = { 264, 256 }; unsigned char idx = modeWrite & 0x1; - size_t itrCnt = packageNum * mem_id.PageSizeInByte / pageSize[idx]; + size_t itrCnt = packageNum * mem_id.PageSizeInByte / pageSize[idx]; unsigned char* itr_begin; size_t i; -// AT45xxx_protectBlock(false,Index); + // AT45xxx_protectBlock(false,Index); - FlashCommand_SendCommand_SetupPacketForAT45DBBulkWrite(AddrRange, modeWrite,WriteCom,Index); + FlashCommand_SendCommand_SetupPacketForAT45DBBulkWrite(AddrRange, modeWrite, WriteCom, Index); itr_begin = vData; for (i = 0; i < itrCnt; ++i) { - bool b = BulkPipeWrite(itr_begin + (i * pageSize[idx]), pageSize[idx], USB_TIMEOUT,Index); + bool b = BulkPipeWrite(itr_begin + (i * pageSize[idx]), pageSize[idx], USB_TIMEOUT, Index); if ((!b) || m_isCanceled) { - return false ; + return false; } } -// Sleep(10) ; - return true ; + // Sleep(10) ; + return true; } -int AT45rangSectorErase(size_t sectionSize, struct CAddressRange AddrRange,int USBIndex) +int AT45rangSectorErase(size_t sectionSize, struct CAddressRange AddrRange, int USBIndex) { // send request - CNTRPIPE_RQ rq ; - unsigned char vInstruction[5]={0x7C,0x50,0x50,0x50,0x50} ; + CNTRPIPE_RQ rq; + unsigned char vInstruction[5] = { 0x7C, 0x50, 0x50, 0x50, 0x50 }; // instrcution format - rq.Function = URB_FUNCTION_VENDOR_ENDPOINT ; - rq.Direction = VENDOR_DIRECTION_OUT ; - rq.Request = TRANSCEIVE ; + rq.Function = URB_FUNCTION_VENDOR_ENDPOINT; + rq.Direction = VENDOR_DIRECTION_OUT; + rq.Request = TRANSCEIVE; if (Is_NewUSBCommand(USBIndex)) { - rq.Value = RESULT_IN; - rq.Index = RFU; + rq.Value = RESULT_IN; + rq.Index = RFU; } else { - rq.Value = RFU ; - rq.Index = RESULT_IN ; - } - rq.Length = 4 ; + rq.Value = RFU; + rq.Index = RESULT_IN; + } + rq.Length = 4; - size_t sectorNum = (AddrRange.end - AddrRange.start + sectionSize - 1) / sectionSize ; + size_t sectorNum = (AddrRange.end - AddrRange.start + sectionSize - 1) / sectionSize; size_t 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 + 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); + int b = OutCtrlRequest(&rq, vInstruction, 4, USBIndex); if ((b == SerialFlash_FALSE) || m_isCanceled) return false; @@ -257,293 +258,295 @@ int AT45rangSectorErase(size_t sectionSize, struct CAddressRange AddrRange,int U return false; } - return true ; + return true; } -bool AT45batchErase(size_t* vAddrs,size_t AddrSize,int USBIndex) +bool AT45batchErase(size_t* vAddrs, size_t AddrSize, int USBIndex) { CHIP_INFO mem_id; int i; - SetPageSize(&mem_id,USBIndex); + SetPageSize(&mem_id, USBIndex); if (strcmp(Chip_Info.Class, SUPPORT_ATMEL_45DBxxxB) == 0) { - mem_id.PageSizeInByte=Chip_Info.PageSizeInByte; - mem_id.ChipSizeInByte=Chip_Info.ChipSizeInByte; + mem_id.PageSizeInByte = Chip_Info.PageSizeInByte; + mem_id.ChipSizeInByte = Chip_Info.ChipSizeInByte; } - mem_id.SectorSizeInByte=Chip_Info.SectorSizeInByte; + mem_id.SectorSizeInByte = Chip_Info.SectorSizeInByte; struct CAddressRange range; for (i = 0; i < AddrSize; i++) { switch (mem_id.ChipSizeInByte) { - case (1<<17): // 1Mbit + case (1 << 17): // 1Mbit if (vAddrs[i] < (1 << 11)) { - range.start=vAddrs[i]; - range.end=vAddrs[i]+(1<<11); - AT45rangSectorErase(1<<11, range,USBIndex); + range.start = vAddrs[i]; + range.end = vAddrs[i] + (1 << 11); + AT45rangSectorErase(1 << 11, range, USBIndex); } else { - range.start=vAddrs[i]; - range.end=vAddrs[i]+(1<<15); - AT45rangSectorErase(1<<15, range,USBIndex); - } - break; - case (1<<17)*264/256: // 1Mbit + range.start = vAddrs[i]; + range.end = vAddrs[i] + (1 << 15); + AT45rangSectorErase(1 << 15, range, USBIndex); + } + break; + case (1 << 17) * 264 / 256: // 1Mbit if (vAddrs[i] < (1 << 12)) { - range.start=vAddrs[i]; - range.end=vAddrs[i]+(1<<12); - AT45rangSectorErase( 1<<12, range,USBIndex); + range.start = vAddrs[i]; + range.end = vAddrs[i] + (1 << 12); + AT45rangSectorErase(1 << 12, range, USBIndex); } else { - range.start=vAddrs[i]; - range.end=vAddrs[i]+(1<<16); - AT45rangSectorErase( 1<<16, range,USBIndex); - } - break; - case (1<<18): // 2Mbit - range.start=0; - range.end=1<<12; - AT45rangSectorErase( 1<<11, range,USBIndex); - range.start=1<<16; - range.end=mem_id.ChipSizeInByte; - AT45rangSectorErase( 1<<15, range,USBIndex); + range.start = vAddrs[i]; + range.end = vAddrs[i] + (1 << 16); + AT45rangSectorErase(1 << 16, range, USBIndex); + } break; - case (1<<18)*264/256: // 2Mbit - range.start=0; - range.end=1<<13; - AT45rangSectorErase( 1<<12, range,USBIndex); - range.start=1<<16; - range.end=mem_id.ChipSizeInByte<<1; - AT45rangSectorErase( 1<<16, range,USBIndex); + case (1 << 18): // 2Mbit + range.start = 0; + range.end = 1 << 12; + AT45rangSectorErase(1 << 11, range, USBIndex); + range.start = 1 << 16; + range.end = mem_id.ChipSizeInByte; + AT45rangSectorErase(1 << 15, range, USBIndex); break; - case (1<<19): // 4Mbit - range.start=0; - range.end=1<<12; - AT45rangSectorErase( 1<<11, range,USBIndex); - range.start=1<<16; - range.end=mem_id.ChipSizeInByte; - AT45rangSectorErase( 1<<16, range,USBIndex); + case (1 << 18) * 264 / 256: // 2Mbit + range.start = 0; + range.end = 1 << 13; + AT45rangSectorErase(1 << 12, range, USBIndex); + range.start = 1 << 16; + range.end = mem_id.ChipSizeInByte << 1; + AT45rangSectorErase(1 << 16, range, USBIndex); break; - case (1<<19)*264/256: // 4Mbit - range.start=0; - range.end=1<<13; - AT45rangSectorErase( 1<<12, range,USBIndex); - range.start=1<<17; - range.end=mem_id.ChipSizeInByte<<1; - AT45rangSectorErase( 1<<17, range,USBIndex); + case (1 << 19): // 4Mbit + range.start = 0; + range.end = 1 << 12; + AT45rangSectorErase(1 << 11, range, USBIndex); + range.start = 1 << 16; + range.end = mem_id.ChipSizeInByte; + AT45rangSectorErase(1 << 16, range, USBIndex); break; - case (1<<20): // 8Mbit - range.start=0; - range.end=1<<12; - AT45rangSectorErase( 1<<11, range,USBIndex); - range.start=1<<16; - range.end=mem_id.ChipSizeInByte; - AT45rangSectorErase( 1<<16, range,USBIndex); + case (1 << 19) * 264 / 256: // 4Mbit + range.start = 0; + range.end = 1 << 13; + AT45rangSectorErase(1 << 12, range, USBIndex); + range.start = 1 << 17; + range.end = mem_id.ChipSizeInByte << 1; + AT45rangSectorErase(1 << 17, range, USBIndex); break; - case (1<<20)*264/256: // 8Mbit - range.start=0; - range.end=1<<13; - AT45rangSectorErase( 1<<12, range,USBIndex); - range.start=1<<17; - range.end=mem_id.ChipSizeInByte<<1; - AT45rangSectorErase( 1<<17, range,USBIndex); + case (1 << 20): // 8Mbit + range.start = 0; + range.end = 1 << 12; + AT45rangSectorErase(1 << 11, range, USBIndex); + range.start = 1 << 16; + range.end = mem_id.ChipSizeInByte; + AT45rangSectorErase(1 << 16, range, USBIndex); break; - case (1<<21): // 16Mbit + case (1 << 20) * 264 / 256: // 8Mbit + range.start = 0; + range.end = 1 << 13; + AT45rangSectorErase(1 << 12, range, USBIndex); + range.start = 1 << 17; + range.end = mem_id.ChipSizeInByte << 1; + AT45rangSectorErase(1 << 17, range, USBIndex); + break; + case (1 << 21): // 16Mbit if (vAddrs[i] < (1 << 13)) { - range.start=vAddrs[i]; - range.end=vAddrs[i]+(1<<12); - AT45rangSectorErase( 1<<12, range,USBIndex); + range.start = vAddrs[i]; + range.end = vAddrs[i] + (1 << 12); + AT45rangSectorErase(1 << 12, range, USBIndex); } else { - range.start=vAddrs[i]; - range.end=vAddrs[i]+(1<<17); - AT45rangSectorErase( 1<<17, range,USBIndex); + range.start = vAddrs[i]; + range.end = vAddrs[i] + (1 << 17); + AT45rangSectorErase(1 << 17, range, USBIndex); } break; - case (1<<21)*264/256: // 16Mbit + case (1 << 21) * 264 / 256: // 16Mbit if (vAddrs[i] < (1 << 14)) { // printf("Addr=0x%X\n",vAddrs[i]); - range.start=vAddrs[i]; - range.end=vAddrs[i]+(1<<13); - AT45rangSectorErase( 1<<13, range,USBIndex); + range.start = vAddrs[i]; + range.end = vAddrs[i] + (1 << 13); + AT45rangSectorErase(1 << 13, range, USBIndex); } else { // printf("Addr=0x%X\n",vAddrs[i]); - range.start=vAddrs[i]; - range.end=vAddrs[i]+(1<<18); - AT45rangSectorErase( 1<<18, range,USBIndex); + range.start = vAddrs[i]; + range.end = vAddrs[i] + (1 << 18); + AT45rangSectorErase(1 << 18, range, USBIndex); } break; - case (1<<22): // 32Mbit - range.start=0; - range.end=1<<13; - AT45rangSectorErase( 1<<12, range,USBIndex); - range.start=1<<18; - range.end=mem_id.ChipSizeInByte; - AT45rangSectorErase( 1<<18, range,USBIndex); + case (1 << 22): // 32Mbit + range.start = 0; + range.end = 1 << 13; + AT45rangSectorErase(1 << 12, range, USBIndex); + range.start = 1 << 18; + range.end = mem_id.ChipSizeInByte; + AT45rangSectorErase(1 << 18, range, USBIndex); break; - case (1<<22)/256*264: // 32Mbit - range.start=0; - range.end=1<<14; - AT45rangSectorErase( 1<<13, range,USBIndex); - range.start=1<<17; - range.end=mem_id.ChipSizeInByte<<1; - AT45rangSectorErase( 1<<17, range,USBIndex); + case (1 << 22) / 256 * 264: // 32Mbit + range.start = 0; + range.end = 1 << 14; + AT45rangSectorErase(1 << 13, range, USBIndex); + range.start = 1 << 17; + range.end = mem_id.ChipSizeInByte << 1; + AT45rangSectorErase(1 << 17, range, USBIndex); break; - case (1<<23): // 64Mbit - range.start=0; - range.end=1<<14; - AT45rangSectorErase( 1<<13, range,USBIndex); - range.start=1<<18; - range.end=mem_id.ChipSizeInByte; - AT45rangSectorErase( 1<<18, range,USBIndex); + case (1 << 23): // 64Mbit + range.start = 0; + range.end = 1 << 14; + AT45rangSectorErase(1 << 13, range, USBIndex); + range.start = 1 << 18; + range.end = mem_id.ChipSizeInByte; + AT45rangSectorErase(1 << 18, range, USBIndex); break; - case (1<<23)/256*264: // 64Mbit - range.start=0; - range.end=1<<15; - AT45rangSectorErase( 1<<14, range,USBIndex); - range.start=1<<19; - range.end=mem_id.ChipSizeInByte<<1; - AT45rangSectorErase( 1<<19, range,USBIndex); + case (1 << 23) / 256 * 264: // 64Mbit + range.start = 0; + range.end = 1 << 15; + AT45rangSectorErase(1 << 14, range, USBIndex); + range.start = 1 << 19; + range.end = mem_id.ChipSizeInByte << 1; + AT45rangSectorErase(1 << 19, range, USBIndex); break; default: - // CSerialFlash::chipErase(); + // CSerialFlash::chipErase(); break; - }; - } + }; + } return true; } -bool AT45chipErase(unsigned int Addr,unsigned int Length,int USBIndex) +bool AT45chipErase(unsigned int Addr, unsigned int Length, int USBIndex) { - unsigned char com[4]={0xC7,0x94,0x80,0x9A}; - int b = FlashCommand_SendCommand_OutOnlyInstruction(com,4,USBIndex); - if(b==SerialFlash_FALSE) return false ; + unsigned char com[4] = { 0xC7, 0x94, 0x80, 0x9A }; + int b = FlashCommand_SendCommand_OutOnlyInstruction(com, 4, USBIndex); + if (b == SerialFlash_FALSE) + return false; - if(AT45WaitForWIP(USBIndex)==false) return false; + if (AT45WaitForWIP(USBIndex) == false) + return false; return true; CHIP_INFO mem_id; - SetPageSize(&mem_id,USBIndex); + SetPageSize(&mem_id, USBIndex); if (strcmp(Chip_Info.Class, SUPPORT_ATMEL_45DBxxxB) == 0) { - mem_id.PageSizeInByte=Chip_Info.PageSizeInByte; - mem_id.ChipSizeInByte=Chip_Info.ChipSizeInByte; + mem_id.PageSizeInByte = Chip_Info.PageSizeInByte; + mem_id.ChipSizeInByte = Chip_Info.ChipSizeInByte; } - mem_id.SectorSizeInByte=Chip_Info.SectorSizeInByte; -// AT45xxx_protectBlock(false,USBIndex); + mem_id.SectorSizeInByte = Chip_Info.SectorSizeInByte; + // AT45xxx_protectBlock(false,USBIndex); struct CAddressRange range; switch (mem_id.ChipSizeInByte) { - case (1<<17): // 1Mbit - range.start=0; - range.end=1<<12; - AT45rangSectorErase(1<<11, range,USBIndex); - range.start=1<<15; - range.end=mem_id.ChipSizeInByte; - AT45rangSectorErase(1<<15, range,USBIndex); - break; - case (1<<17)*264/256: // 1Mbit - range.start=0; - range.end=1<<13; - AT45rangSectorErase( 1<<12, range,USBIndex); - range.start=1<<16; - range.end=mem_id.ChipSizeInByte<<1; - AT45rangSectorErase( 1<<16, range,USBIndex); - break; - case (1<<18): // 2Mbit - range.start=0; - range.end=1<<12; - AT45rangSectorErase( 1<<11, range,USBIndex); - range.start=1<<16; - range.end=mem_id.ChipSizeInByte; - AT45rangSectorErase( 1<<15, range,USBIndex); - break; - case (1<<18)*264/256: // 2Mbit - range.start=0; - range.end=1<<13; - AT45rangSectorErase( 1<<12, range,USBIndex); - range.start=1<<16; - range.end=mem_id.ChipSizeInByte<<1; - AT45rangSectorErase( 1<<16, range,USBIndex); - break; - case (1<<19): // 4Mbit - range.start=0; - range.end=1<<12; - AT45rangSectorErase( 1<<11, range,USBIndex); - range.start=1<<16; - range.end=mem_id.ChipSizeInByte; - AT45rangSectorErase( 1<<16, range,USBIndex); - break; - case (1<<19)*264/256: // 4Mbit - range.start=0; - range.end=1<<13; - AT45rangSectorErase( 1<<12, range,USBIndex); - range.start=1<<17; - range.end=mem_id.ChipSizeInByte<<1; - AT45rangSectorErase( 1<<17, range,USBIndex); - break; - case (1<<20): // 8Mbit - range.start=0; - range.end=1<<12; - AT45rangSectorErase( 1<<11, range,USBIndex); - range.start=1<<16; - range.end=mem_id.ChipSizeInByte; - AT45rangSectorErase( 1<<16, range,USBIndex); - break; - case (1<<20)*264/256: // 8Mbit - range.start=0; - range.end=1<<13; - AT45rangSectorErase( 1<<12, range,USBIndex); - range.start=1<<17; - range.end=mem_id.ChipSizeInByte<<1; - AT45rangSectorErase( 1<<17, range,USBIndex); - break; - case (1<<21): // 16Mbit - range.start=0; - range.end=1<<13; - AT45rangSectorErase( 1<<12, range,USBIndex); - range.start=1<<17; - range.end=mem_id.ChipSizeInByte; - AT45rangSectorErase( 1<<17, range,USBIndex); - break; - case (1<<21)*264/256: // 16Mbit - range.start=0; - range.end=1<<14; - AT45rangSectorErase( 1<<13, range,USBIndex); - range.start=1<<18; - range.end=mem_id.ChipSizeInByte<<1; - AT45rangSectorErase( 1<<18, range,USBIndex); - break; - case (1<<22): // 32Mbit - range.start=0; - range.end=1<<13; - AT45rangSectorErase( 1<<12, range,USBIndex); - range.start=1<<18; - range.end=mem_id.ChipSizeInByte; - AT45rangSectorErase( 1<<18, range,USBIndex); - break; - case (1<<22)/256*264: // 32Mbit - range.start=0; - range.end=1<<14; - AT45rangSectorErase( 1<<13, range,USBIndex); - range.start=1<<17; - range.end=mem_id.ChipSizeInByte<<1; - AT45rangSectorErase( 1<<17, range,USBIndex); - break; - case (1<<23): // 64Mbit - range.start=0; - range.end=1<<14; - AT45rangSectorErase( 1<<13, range,USBIndex); - range.start=1<<18; - range.end=mem_id.ChipSizeInByte; - AT45rangSectorErase( 1<<18, range,USBIndex); - break; - case (1<<23)/256*264: // 64Mbit - range.start=0; - range.end=1<<15; - AT45rangSectorErase( 1<<14, range,USBIndex); - range.start=1<<19; - range.end=mem_id.ChipSizeInByte<<1; - AT45rangSectorErase( 1<<19, range,USBIndex); - break; - default: - // CSerialFlash::chipErase(); + case (1 << 17): // 1Mbit + range.start = 0; + range.end = 1 << 12; + AT45rangSectorErase(1 << 11, range, USBIndex); + range.start = 1 << 15; + range.end = mem_id.ChipSizeInByte; + AT45rangSectorErase(1 << 15, range, USBIndex); + break; + case (1 << 17) * 264 / 256: // 1Mbit + range.start = 0; + range.end = 1 << 13; + AT45rangSectorErase(1 << 12, range, USBIndex); + range.start = 1 << 16; + range.end = mem_id.ChipSizeInByte << 1; + AT45rangSectorErase(1 << 16, range, USBIndex); + break; + case (1 << 18): // 2Mbit + range.start = 0; + range.end = 1 << 12; + AT45rangSectorErase(1 << 11, range, USBIndex); + range.start = 1 << 16; + range.end = mem_id.ChipSizeInByte; + AT45rangSectorErase(1 << 15, range, USBIndex); + break; + case (1 << 18) * 264 / 256: // 2Mbit + range.start = 0; + range.end = 1 << 13; + AT45rangSectorErase(1 << 12, range, USBIndex); + range.start = 1 << 16; + range.end = mem_id.ChipSizeInByte << 1; + AT45rangSectorErase(1 << 16, range, USBIndex); + break; + case (1 << 19): // 4Mbit + range.start = 0; + range.end = 1 << 12; + AT45rangSectorErase(1 << 11, range, USBIndex); + range.start = 1 << 16; + range.end = mem_id.ChipSizeInByte; + AT45rangSectorErase(1 << 16, range, USBIndex); + break; + case (1 << 19) * 264 / 256: // 4Mbit + range.start = 0; + range.end = 1 << 13; + AT45rangSectorErase(1 << 12, range, USBIndex); + range.start = 1 << 17; + range.end = mem_id.ChipSizeInByte << 1; + AT45rangSectorErase(1 << 17, range, USBIndex); + break; + case (1 << 20): // 8Mbit + range.start = 0; + range.end = 1 << 12; + AT45rangSectorErase(1 << 11, range, USBIndex); + range.start = 1 << 16; + range.end = mem_id.ChipSizeInByte; + AT45rangSectorErase(1 << 16, range, USBIndex); + break; + case (1 << 20) * 264 / 256: // 8Mbit + range.start = 0; + range.end = 1 << 13; + AT45rangSectorErase(1 << 12, range, USBIndex); + range.start = 1 << 17; + range.end = mem_id.ChipSizeInByte << 1; + AT45rangSectorErase(1 << 17, range, USBIndex); + break; + case (1 << 21): // 16Mbit + range.start = 0; + range.end = 1 << 13; + AT45rangSectorErase(1 << 12, range, USBIndex); + range.start = 1 << 17; + range.end = mem_id.ChipSizeInByte; + AT45rangSectorErase(1 << 17, range, USBIndex); + break; + case (1 << 21) * 264 / 256: // 16Mbit + range.start = 0; + range.end = 1 << 14; + AT45rangSectorErase(1 << 13, range, USBIndex); + range.start = 1 << 18; + range.end = mem_id.ChipSizeInByte << 1; + AT45rangSectorErase(1 << 18, range, USBIndex); + break; + case (1 << 22): // 32Mbit + range.start = 0; + range.end = 1 << 13; + AT45rangSectorErase(1 << 12, range, USBIndex); + range.start = 1 << 18; + range.end = mem_id.ChipSizeInByte; + AT45rangSectorErase(1 << 18, range, USBIndex); + break; + case (1 << 22) / 256 * 264: // 32Mbit + range.start = 0; + range.end = 1 << 14; + AT45rangSectorErase(1 << 13, range, USBIndex); + range.start = 1 << 17; + range.end = mem_id.ChipSizeInByte << 1; + AT45rangSectorErase(1 << 17, range, USBIndex); + break; + case (1 << 23): // 64Mbit + range.start = 0; + range.end = 1 << 14; + AT45rangSectorErase(1 << 13, range, USBIndex); + range.start = 1 << 18; + range.end = mem_id.ChipSizeInByte; + AT45rangSectorErase(1 << 18, range, USBIndex); + break; + case (1 << 23) / 256 * 264: // 64Mbit + range.start = 0; + range.end = 1 << 15; + AT45rangSectorErase(1 << 14, range, USBIndex); + range.start = 1 << 19; + range.end = mem_id.ChipSizeInByte << 1; + AT45rangSectorErase(1 << 19, range, USBIndex); + break; + default: + // CSerialFlash::chipErase(); break; }; return true; @@ -551,324 +554,324 @@ bool AT45chipErase(unsigned int Addr,unsigned int Length,int USBIndex) // write status register , just 1 bytes // WRSR only is effects on SRWD BP2 BP1 BP0 when WEL = 1 -int SerialFlash_doWRSR(unsigned char cSR,int Index) +int SerialFlash_doWRSR(unsigned char cSR, int Index) { -//simon if(! m_usb.is_open()) -//Simon return false ; + //simon if(! m_usb.is_open()) + //Simon return false ; - // wait until WIP = 0 - SerialFlash_waitForWIP(Index) ; + // wait until WIP = 0 + SerialFlash_waitForWIP(Index); - // wait until WEL = 1 - SerialFlash_waitForWEL(Index) ; + // wait until WEL = 1 + SerialFlash_waitForWEL(Index); - // send request - unsigned char vInstruction[2]; + // send request + unsigned char vInstruction[2]; - vInstruction[0] = mcode_WRSR; - vInstruction[1] = cSR; + vInstruction[0] = mcode_WRSR; + vInstruction[1] = cSR; - return FlashCommand_SendCommand_OutOnlyInstruction(vInstruction, 2,Index); + return FlashCommand_SendCommand_OutOnlyInstruction(vInstruction, 2, Index); } -int SerialFlash_doRDSR(unsigned char *cSR,int Index) +int SerialFlash_doRDSR(unsigned char* cSR, int Index) { - // read status -//Simon if(! m_usb.is_open() ) -//Simon return false ; + // read status + //Simon if(! m_usb.is_open() ) + //Simon return false ; - // send request - CNTRPIPE_RQ rq ; - unsigned char vInstruction; //size 1 + // send request + CNTRPIPE_RQ rq; + unsigned char vInstruction; //size 1 - // first control packet - vInstruction = mcode_RDSR; + // first control packet + vInstruction = mcode_RDSR; - rq.Function = URB_FUNCTION_VENDOR_ENDPOINT ; - rq.Direction = VENDOR_DIRECTION_OUT ; - rq.Request = TRANSCEIVE ; + rq.Function = URB_FUNCTION_VENDOR_ENDPOINT; + rq.Direction = VENDOR_DIRECTION_OUT; + rq.Request = TRANSCEIVE; if (Is_NewUSBCommand(Index)) { - rq.Value = RESULT_IN ; - rq.Index = RFU ; + rq.Value = RESULT_IN; + rq.Index = RFU; } else { - rq.Value = RFU ; - rq.Index = RESULT_IN ; - } - rq.Length = 1;//(unsigned long) 1 ; - - 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 ; + rq.Value = RFU; + rq.Index = RESULT_IN; + } + rq.Length = 1; //(unsigned long) 1 ; + + 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)) { - rq.Value = 1; - rq.Index = 0; + rq.Value = 1; + rq.Index = 0; } else { - rq.Value = CTRL_TIMEOUT ; - rq.Index = NO_REGISTER ; - } - rq.Length = 1;//(unsigned long) 1; + rq.Value = CTRL_TIMEOUT; + rq.Index = NO_REGISTER; + } + rq.Length = 1; //(unsigned long) 1; - if(InCtrlRequest(&rq, &vBuffer, 1, Index) == SerialFlash_FALSE) - return SerialFlash_FALSE ; + if (InCtrlRequest(&rq, &vBuffer, 1, Index) == SerialFlash_FALSE) + return SerialFlash_FALSE; - *cSR = vBuffer; - return SerialFlash_TRUE; + *cSR = vBuffer; + return SerialFlash_TRUE; } -int S70FSxxx_Large_doRDSR1V(bool die1, unsigned char *cSR,int Index) +int S70FSxxx_Large_doRDSR1V(bool die1, unsigned char* cSR, int Index) { - CNTRPIPE_RQ rq ; - unsigned char vInstruction[7]={0}; //size 1 + CNTRPIPE_RQ rq; + unsigned char vInstruction[7] = { 0 }; //size 1 - // first control packet - vInstruction[0] = 0x65; - vInstruction[1] = ((0x800000>>24)|(die1?0:(0x4000000>>24))); + // first control packet + vInstruction[0] = 0x65; + vInstruction[1] = ((0x800000 >> 24) | (die1 ? 0 : (0x4000000 >> 24))); 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; + vInstruction[5] = 0; + vInstruction[6] = 0; - rq.Function = URB_FUNCTION_VENDOR_ENDPOINT ; - rq.Direction = VENDOR_DIRECTION_OUT ; - rq.Request = TRANSCEIVE ; + rq.Function = URB_FUNCTION_VENDOR_ENDPOINT; + rq.Direction = VENDOR_DIRECTION_OUT; + rq.Request = TRANSCEIVE; if (Is_NewUSBCommand(Index)) { - rq.Value = RESULT_IN ; - rq.Index = RFU ; + rq.Value = RESULT_IN; + rq.Index = RFU; } else { - rq.Value = RFU ; - rq.Index = RESULT_IN ; - } - rq.Length = 7;//(unsigned long) 1 ; + rq.Value = RFU; + rq.Index = RESULT_IN; + } + rq.Length = 7; //(unsigned long) 1 ; if (OutCtrlRequest(&rq, vInstruction, 7, Index) == SerialFlash_FALSE) - return 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 ; + // 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)) { - rq.Value = 1; - rq.Index = 0; + rq.Value = 1; + rq.Index = 0; } else { - rq.Value = CTRL_TIMEOUT ; - rq.Index = NO_REGISTER ; - } - rq.Length = 1;//(unsigned long) 1; + rq.Value = CTRL_TIMEOUT; + rq.Index = NO_REGISTER; + } + rq.Length = 1; //(unsigned long) 1; - if(InCtrlRequest(&rq, &vBuffer, 1, Index) == SerialFlash_FALSE) - return SerialFlash_FALSE ; + if (InCtrlRequest(&rq, &vBuffer, 1, Index) == SerialFlash_FALSE) + return SerialFlash_FALSE; - *cSR = vBuffer; + *cSR = vBuffer; - return SerialFlash_TRUE; + return SerialFlash_TRUE; } -int S70FSxxx_Large_doRDCR2V(bool die1, unsigned char *cSR,int Index) +int S70FSxxx_Large_doRDCR2V(bool die1, unsigned char* cSR, int Index) { - CNTRPIPE_RQ rq ; - unsigned char vInstruction[7]={0}; //size 1 + CNTRPIPE_RQ rq; + unsigned char vInstruction[7] = { 0 }; //size 1 - // first control packet - vInstruction[0] = 0x65; - vInstruction[1] = ((0x800003>>24)|(die1?0:(0x4000000>>24))); + // first control packet + vInstruction[0] = 0x65; + vInstruction[1] = ((0x800003 >> 24) | (die1 ? 0 : (0x4000000 >> 24))); 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; + vInstruction[5] = 0; + vInstruction[6] = 0; - rq.Function = URB_FUNCTION_VENDOR_ENDPOINT ; - rq.Direction = VENDOR_DIRECTION_OUT ; - rq.Request = TRANSCEIVE ; + rq.Function = URB_FUNCTION_VENDOR_ENDPOINT; + rq.Direction = VENDOR_DIRECTION_OUT; + rq.Request = TRANSCEIVE; if (Is_NewUSBCommand(Index)) { - rq.Value = RESULT_IN ; - rq.Index = RFU ; + rq.Value = RESULT_IN; + rq.Index = RFU; } else { - rq.Value = RFU ; - rq.Index = RESULT_IN ; - } - rq.Length = 7;//(unsigned long) 1 ; + rq.Value = RFU; + rq.Index = RESULT_IN; + } + rq.Length = 7; //(unsigned long) 1 ; if (OutCtrlRequest(&rq, vInstruction, 7, Index) == SerialFlash_FALSE) - return 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 ; + // 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)) { - rq.Value = 1; - rq.Index = 0; + rq.Value = 1; + rq.Index = 0; } else { - rq.Value = CTRL_TIMEOUT ; - rq.Index = NO_REGISTER ; - } - rq.Length = 1;//(unsigned long) 1; + rq.Value = CTRL_TIMEOUT; + rq.Index = NO_REGISTER; + } + rq.Length = 1; //(unsigned long) 1; - if(InCtrlRequest(&rq, &vBuffer, 1, Index) == SerialFlash_FALSE) - return SerialFlash_FALSE ; + if (InCtrlRequest(&rq, &vBuffer, 1, Index) == SerialFlash_FALSE) + return SerialFlash_FALSE; - *cSR = vBuffer; + *cSR = vBuffer; - return SerialFlash_TRUE; + return SerialFlash_TRUE; } -int MX25Lxxx_Large_doRDCR(unsigned char *cSR,int Index) +int MX25Lxxx_Large_doRDCR(unsigned char* cSR, int Index) { - CNTRPIPE_RQ rq ; - unsigned char vInstruction[1]={0}; //size 1 + CNTRPIPE_RQ rq; + unsigned char vInstruction[1] = { 0 }; //size 1 - // first control packet - vInstruction[0] = 0x15; + // first control packet + vInstruction[0] = 0x15; - rq.Function = URB_FUNCTION_VENDOR_ENDPOINT ; - rq.Direction = VENDOR_DIRECTION_OUT ; - rq.Request = TRANSCEIVE ; + rq.Function = URB_FUNCTION_VENDOR_ENDPOINT; + rq.Direction = VENDOR_DIRECTION_OUT; + rq.Request = TRANSCEIVE; if (Is_NewUSBCommand(Index)) { - rq.Value = RESULT_IN ; - rq.Index = RFU ; + rq.Value = RESULT_IN; + rq.Index = RFU; } else { - rq.Value = RFU ; - rq.Index = RESULT_IN ; - } - rq.Length = 1;//(unsigned long) 1 ; + rq.Value = RFU; + rq.Index = RESULT_IN; + } + rq.Length = 1; //(unsigned long) 1 ; if (OutCtrlRequest(&rq, vInstruction, 1, Index) == SerialFlash_FALSE) - return 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 ; + // 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)) { - rq.Value = 1; - rq.Index = 0; + rq.Value = 1; + rq.Index = 0; } else { - rq.Value = CTRL_TIMEOUT ; - rq.Index = NO_REGISTER ; - } - rq.Length = 1;//(unsigned long) 1; + rq.Value = CTRL_TIMEOUT; + rq.Index = NO_REGISTER; + } + rq.Length = 1; //(unsigned long) 1; - if(InCtrlRequest(&rq, &vBuffer, 1, Index) == SerialFlash_FALSE) - return SerialFlash_FALSE ; + if (InCtrlRequest(&rq, &vBuffer, 1, Index) == SerialFlash_FALSE) + return SerialFlash_FALSE; - *cSR = vBuffer; + *cSR = vBuffer; - return SerialFlash_TRUE; + return SerialFlash_TRUE; } -int MX25Lxxx_Large_doRDSR(unsigned char *cSR,int Index) +int MX25Lxxx_Large_doRDSR(unsigned char* cSR, int Index) { - CNTRPIPE_RQ rq ; - unsigned char vInstruction[1]={0}; //size 1 + CNTRPIPE_RQ rq; + unsigned char vInstruction[1] = { 0 }; //size 1 - // first control packet - vInstruction[0] = 0x05; + // first control packet + vInstruction[0] = 0x05; - rq.Function = URB_FUNCTION_VENDOR_ENDPOINT ; - rq.Direction = VENDOR_DIRECTION_OUT ; - rq.Request = TRANSCEIVE ; + rq.Function = URB_FUNCTION_VENDOR_ENDPOINT; + rq.Direction = VENDOR_DIRECTION_OUT; + rq.Request = TRANSCEIVE; if (Is_NewUSBCommand(Index)) { - rq.Value = RESULT_IN ; - rq.Index = RFU ; + rq.Value = RESULT_IN; + rq.Index = RFU; } else { - rq.Value = RFU ; - rq.Index = RESULT_IN ; - } - rq.Length = 1;//(unsigned long) 1 ; + rq.Value = RFU; + rq.Index = RESULT_IN; + } + rq.Length = 1; //(unsigned long) 1 ; if (OutCtrlRequest(&rq, vInstruction, 1, Index) == SerialFlash_FALSE) - return 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 ; + // 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)) { - rq.Value = 1; - rq.Index = 0; + rq.Value = 1; + rq.Index = 0; } else { - rq.Value = CTRL_TIMEOUT ; - rq.Index = NO_REGISTER ; - } - rq.Length = 1;//(unsigned long) 1; + rq.Value = CTRL_TIMEOUT; + rq.Index = NO_REGISTER; + } + rq.Length = 1; //(unsigned long) 1; - if(InCtrlRequest(&rq, &vBuffer, 1, Index) == SerialFlash_FALSE) - return SerialFlash_FALSE ; + if (InCtrlRequest(&rq, &vBuffer, 1, Index) == SerialFlash_FALSE) + return SerialFlash_FALSE; - *cSR = vBuffer; + *cSR = vBuffer; - return SerialFlash_TRUE; + return SerialFlash_TRUE; } -int MX25Lxxx_Large_doWRCR(unsigned char cCR,int Index) +int MX25Lxxx_Large_doWRCR(unsigned char cCR, int Index) { - unsigned char cSR; + unsigned char cSR; - CNTRPIPE_RQ rq ; - unsigned char vInstruction[1]={0}; //size 1 + CNTRPIPE_RQ rq; + unsigned char vInstruction[1] = { 0 }; //size 1 - // first control packet - vInstruction[0] = 0x05; + // first control packet + vInstruction[0] = 0x05; - rq.Function = URB_FUNCTION_VENDOR_ENDPOINT ; - rq.Direction = VENDOR_DIRECTION_OUT ; - rq.Request = TRANSCEIVE ; + rq.Function = URB_FUNCTION_VENDOR_ENDPOINT; + rq.Direction = VENDOR_DIRECTION_OUT; + rq.Request = TRANSCEIVE; if (Is_NewUSBCommand(Index)) { - rq.Value = RESULT_IN ; - rq.Index = RFU ; + rq.Value = RESULT_IN; + rq.Index = RFU; } else { - rq.Value = RFU ; - rq.Index = RESULT_IN ; - } - rq.Length = 1;//(unsigned long) 1 ; + rq.Value = RFU; + rq.Index = RESULT_IN; + } + rq.Length = 1; //(unsigned long) 1 ; if (OutCtrlRequest(&rq, vInstruction, 1, Index) == SerialFlash_FALSE) - return 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 ; + // 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)) { - rq.Value = 1; - rq.Index = 0; + rq.Value = 1; + rq.Index = 0; } else { - rq.Value = CTRL_TIMEOUT ; - rq.Index = NO_REGISTER ; - } - rq.Length = 1;//(unsigned long) 1; + rq.Value = CTRL_TIMEOUT; + rq.Index = NO_REGISTER; + } + rq.Length = 1; //(unsigned long) 1; - if(InCtrlRequest(&rq, &vBuffer, 1, Index) == SerialFlash_FALSE) - return SerialFlash_FALSE ; + if (InCtrlRequest(&rq, &vBuffer, 1, Index) == SerialFlash_FALSE) + return SerialFlash_FALSE; - cSR = vBuffer; + cSR = vBuffer; - // wait until WIP = 0 - SerialFlash_waitForWIP(Index) ; + // wait until WIP = 0 + SerialFlash_waitForWIP(Index); - // wait until WEL = 1 - SerialFlash_waitForWEL(Index) ; - // send request - unsigned char vInstruction2[3]; + // wait until WEL = 1 + SerialFlash_waitForWEL(Index); + // send request + unsigned char vInstruction2[3]; - vInstruction2[0] = 0x01; - vInstruction2[1] = cSR; - vInstruction2[2] = cCR; + vInstruction2[0] = 0x01; + vInstruction2[1] = cSR; + vInstruction2[2] = cCR; - return FlashCommand_SendCommand_OutOnlyInstruction(vInstruction2, 3,Index); + return FlashCommand_SendCommand_OutOnlyInstruction(vInstruction2, 3, Index); } // /** @@ -886,28 +889,28 @@ int MX25Lxxx_Large_doWRCR(unsigned char cCR,int Index) */ void SerialFlash_waitForWEL(int Index) { - unsigned char cSR ; - size_t i = 10; - // wait until WIP = 0 and WEL = 1 - do{ - SerialFlash_doWREN(Index) ; - - // read SR to check WEL until WEL = 1 - SerialFlash_doRDSR(&cSR,Index) ; - }while(((cSR & 0x02) == 0) && (i-- > 0)) ; + unsigned char cSR; + size_t i = 10; + // wait until WIP = 0 and WEL = 1 + do { + SerialFlash_doWREN(Index); + + // read SR to check WEL until WEL = 1 + SerialFlash_doRDSR(&cSR, Index); + } while (((cSR & 0x02) == 0) && (i-- > 0)); } -void S70FSxxx_Large_waitForWEL(bool die1,int Index) +void S70FSxxx_Large_waitForWEL(bool die1, int Index) { - unsigned char cSR=0; - size_t i = 10; + unsigned char cSR = 0; + size_t i = 10; - do{// wait until WIP = 0 and WEL = 1 - SerialFlash_doWREN(Index) ; + do { // wait until WIP = 0 and WEL = 1 + SerialFlash_doWREN(Index); - // read SR to check WEL until WEL = 1 - S70FSxxx_Large_doRDSR1V(die1,&cSR,Index) ; - }while(((cSR & 0x02) == 0) && (i-- > 0)) ; + // read SR to check WEL until WEL = 1 + S70FSxxx_Large_doRDSR1V(die1, &cSR, Index); + } while (((cSR & 0x02) == 0) && (i-- > 0)); } /** @@ -923,34 +926,34 @@ void S70FSxxx_Large_waitForWEL(bool die1,int Index) * SRWD 0 0 BP2 BP1 BP0 WEL WIP * */ -bool S70FSxxx_Large_waitForWIP(bool die1,int Index) +bool S70FSxxx_Large_waitForWIP(bool die1, int Index) { - unsigned char cSR ; - size_t i = Chip_Info.Timeout*100; - if(i==0) - i=MAX_TRIALS; - // wait until WIP = 0 - do{ - S70FSxxx_Large_doRDSR1V(die1,&cSR,Index) ; + unsigned char cSR; + size_t i = Chip_Info.Timeout * 100; + if (i == 0) + i = MAX_TRIALS; + // wait until WIP = 0 + do { + S70FSxxx_Large_doRDSR1V(die1, &cSR, Index); Sleep(5); - }while((cSR & 0x01) && (i-- > 0)) ; - if(i<=0) + } while ((cSR & 0x01) && (i-- > 0)); + if (i <= 0) return false; return true; } bool SerialFlash_waitForWIP(int Index) { - unsigned char cSR ; - size_t i = Chip_Info.Timeout*100; - if(i==0) - i=MAX_TRIALS; - // wait until WIP = 0 - do{ - SerialFlash_doRDSR(&cSR,Index) ; + unsigned char cSR; + size_t i = Chip_Info.Timeout * 100; + if (i == 0) + i = MAX_TRIALS; + // wait until WIP = 0 + do { + SerialFlash_doRDSR(&cSR, Index); Sleep(5); - }while((cSR & 0x01) && (i-- > 0)) ; - if(i<=0) + } while ((cSR & 0x01) && (i-- > 0)); + if (i <= 0) return false; return true; } @@ -958,148 +961,149 @@ bool SerialFlash_waitForWIP(int Index) int SerialFlash_doWREN(int Index) { unsigned char v = mcode_WREN; - return FlashCommand_SendCommand_OutOnlyInstruction(&v, 1,Index); + return FlashCommand_SendCommand_OutOnlyInstruction(&v, 1, Index); } int SerialFlash_doWRDI(int Index) { - unsigned char v=mcode_WRDI; - return FlashCommand_SendCommand_OutOnlyInstruction(&v, 1,Index); + unsigned char v = mcode_WRDI; + return FlashCommand_SendCommand_OutOnlyInstruction(&v, 1, Index); } -bool SST25xFxx_protectBlock(int bProtect,int Index) +bool SST25xFxx_protectBlock(int bProtect, int Index) { - bool result = false ; - unsigned char tmpSRVal; - unsigned char dstSRVal ; + bool result = false; + unsigned char tmpSRVal; + unsigned char dstSRVal; SerialFlash_waitForWIP(Index); // un-protect block ,set BP1 BP0 to 0 - dstSRVal = 0x00 ; + dstSRVal = 0x00; // protect block ,set BP1 BP0 to 1 - if(bProtect){ - dstSRVal |= 0x8C ; // 8C : 1000 1100 + if (bProtect) { + dstSRVal |= 0x8C; // 8C : 1000 1100 } - unsigned char vInstruction[2] ; - vInstruction[0] = 0x50 ; //Write enable - FlashCommand_SendCommand_OutOnlyInstruction(vInstruction,1,Index); + unsigned char vInstruction[2]; + vInstruction[0] = 0x50; //Write enable + FlashCommand_SendCommand_OutOnlyInstruction(vInstruction, 1, Index); - int numOfRetry = 1000 ; - vInstruction[0] = 0x01 ; //Write register - vInstruction[1] = dstSRVal; - FlashCommand_SendCommand_OutOnlyInstruction(vInstruction,2,Index); + int numOfRetry = 1000; + vInstruction[0] = 0x01; //Write register + vInstruction[1] = dstSRVal; + FlashCommand_SendCommand_OutOnlyInstruction(vInstruction, 2, Index); - result = SerialFlash_doRDSR(&tmpSRVal,Index) ; - while( (tmpSRVal & 0x01) && numOfRetry > 0) // WIP = TRUE; + result = SerialFlash_doRDSR(&tmpSRVal, Index); + while ((tmpSRVal & 0x01) && numOfRetry > 0) // WIP = TRUE; { // read SR - result = SerialFlash_doRDSR(&tmpSRVal,Index) ; + result = SerialFlash_doRDSR(&tmpSRVal, Index); if (!result) return false; - numOfRetry -- ; + numOfRetry--; }; - return ((tmpSRVal ^ dstSRVal)& 0x0C ) ? false:true; + return ((tmpSRVal ^ dstSRVal) & 0x0C) ? false : true; } -bool SST25xFxxA_protectBlock(int bProtect,int Index) +bool SST25xFxxA_protectBlock(int bProtect, int Index) { - bool result = false ; - unsigned char tmpSRVal; - unsigned char dstSRVal ; + bool result = false; + unsigned char tmpSRVal; + unsigned char dstSRVal; // un-protect block ,set BP1 BP0 to 0 - dstSRVal = 0x00 ; + dstSRVal = 0x00; // protect block ,set BP1 BP0 to 1 - if(bProtect){ - dstSRVal |= 0x0 ; // 8C : 1000 1100 + if (bProtect) { + dstSRVal |= 0x0; // 8C : 1000 1100 } - int numOfRetry = 1000 ; - result = SerialFlash_doWRSR(dstSRVal,Index) ; - result = SerialFlash_doRDSR(&tmpSRVal,Index) ; - while( (tmpSRVal & 0x01) && numOfRetry > 0) // WIP = TRUE; + int numOfRetry = 1000; + result = SerialFlash_doWRSR(dstSRVal, Index); + result = SerialFlash_doRDSR(&tmpSRVal, Index); + while ((tmpSRVal & 0x01) && numOfRetry > 0) // WIP = TRUE; { // read SR - result = SerialFlash_doRDSR(&tmpSRVal,Index) ; + result = SerialFlash_doRDSR(&tmpSRVal, Index); if (!result) return false; - numOfRetry -- ; + numOfRetry--; }; - return ((tmpSRVal ^ dstSRVal)& 0x0C ) ? false:true; + return ((tmpSRVal ^ dstSRVal) & 0x0C) ? false : true; } -bool AT25FSxxx_protectBlock(int bProtect,int Index) +bool AT25FSxxx_protectBlock(int bProtect, int Index) { - bool result = false ; - unsigned char tmpSRVal; - unsigned char dstSRVal ; + bool result = false; + unsigned char tmpSRVal; + unsigned char dstSRVal; // un-protect block ,set BP1 BP0 to 0 - dstSRVal = 0x00 ; + dstSRVal = 0x00; // protect block ,set BP1 BP0 to 1 - if(bProtect){ - dstSRVal |= 0xFF ; // 8C : 1000 1100 + if (bProtect) { + dstSRVal |= 0xFF; // 8C : 1000 1100 } - int numOfRetry = 1000 ; - result = SerialFlash_doWRSR(dstSRVal,Index) ; - result = SerialFlash_doRDSR(&tmpSRVal,Index) ; - while( (tmpSRVal & 0x01) && numOfRetry > 0) // WIP = TRUE; + int numOfRetry = 1000; + result = SerialFlash_doWRSR(dstSRVal, Index); + result = SerialFlash_doRDSR(&tmpSRVal, Index); + while ((tmpSRVal & 0x01) && numOfRetry > 0) // WIP = TRUE; { // read SR - result = SerialFlash_doRDSR(&tmpSRVal,Index) ; + result = SerialFlash_doRDSR(&tmpSRVal, Index); if (!result) return false; - numOfRetry -- ; + numOfRetry--; }; - return ((tmpSRVal ^ dstSRVal)& 0x0C ) ? false:true; + return ((tmpSRVal ^ dstSRVal) & 0x0C) ? false : true; } -bool AT26Fxxx_protectBlock(int bProtect,int Index) +bool AT26Fxxx_protectBlock(int bProtect, int Index) { const unsigned int AT26DF041 = 0x1F4400; const unsigned int AT26DF004 = 0x1F0400; const unsigned int AT26DF081A = 0x1F4501; enum { - PROTECTSECTOR = 0x36, // Write Enable - UNPROTECTSECTOR = 0x39, // Write Disable - READPROTECTIONREGISTER = 0x3C, // Write Disable + 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 - bool result = false ; + bool result = false; - int numOfRetry = 1000 ; + int numOfRetry = 1000; - result = SerialFlash_doWRSR(0,Index) ; + result = SerialFlash_doWRSR(0, Index); - unsigned char tmpSRVal; + unsigned char tmpSRVal; do { - result = SerialFlash_doRDSR(&tmpSRVal,Index) ; - numOfRetry -- ; + result = SerialFlash_doRDSR(&tmpSRVal, Index); + numOfRetry--; - }while( (tmpSRVal & 0x01) && numOfRetry > 0 && result); + } while ((tmpSRVal & 0x01) && numOfRetry > 0 && result); - if (tmpSRVal & 0x80 ) return false; ///< enable SPRL + if (tmpSRVal & 0x80) + return false; ///< enable SPRL // send request - CNTRPIPE_RQ rq ; - unsigned char vInstruction[4] ={0}; + CNTRPIPE_RQ rq; + unsigned char vInstruction[4] = { 0 }; - vInstruction[0] = bProtect ? PROTECTSECTOR : UNPROTECTSECTOR ; + vInstruction[0] = bProtect ? PROTECTSECTOR : UNPROTECTSECTOR; - size_t iUniformSectorSize = 0x10000; // always regarded as 64K + size_t iUniformSectorSize = 0x10000; // always regarded as 64K size_t cnt = Chip_Info.ChipSizeInByte / iUniformSectorSize; size_t i; for (i = 0; i < cnt; ++i) { @@ -1107,290 +1111,294 @@ bool AT26Fxxx_protectBlock(int bProtect,int Index) vInstruction[1] = (unsigned char)i; - rq.Function = URB_FUNCTION_VENDOR_ENDPOINT ; - rq.Direction = VENDOR_DIRECTION_OUT ; - rq.Request = TRANSCEIVE ; + rq.Function = URB_FUNCTION_VENDOR_ENDPOINT; + rq.Direction = VENDOR_DIRECTION_OUT; + rq.Request = TRANSCEIVE; if (Is_NewUSBCommand(Index)) { - rq.Value = RESULT_IN; - rq.Index = RFU; + rq.Value = RESULT_IN; + rq.Index = RFU; } else { - rq.Value = RFU ; - rq.Index = RESULT_IN ; - } + rq.Value = RFU; + rq.Index = RESULT_IN; + } rq.Length = 4; - if(OutCtrlRequest(&rq, vInstruction,4,Index)==SerialFlash_FALSE) - return false ; + if (OutCtrlRequest(&rq, vInstruction, 4, Index) == SerialFlash_FALSE) + return false; } - if(AT26DF081A == Chip_Info.UniqueID || AT26DF004 == Chip_Info.UniqueID) // 8K each for the last 64K + 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) { SerialFlash_doWREN(Index); - vInstruction[2] = (unsigned char)(i<<5); + vInstruction[2] = (unsigned char)(i << 5); - rq.Function = URB_FUNCTION_VENDOR_ENDPOINT ; - rq.Direction = VENDOR_DIRECTION_OUT ; - rq.Request = TRANSCEIVE ; + rq.Function = URB_FUNCTION_VENDOR_ENDPOINT; + rq.Direction = VENDOR_DIRECTION_OUT; + rq.Request = TRANSCEIVE; if (Is_NewUSBCommand(Index)) { - rq.Value = RESULT_IN; - rq.Index = RFU; + rq.Value = RESULT_IN; + rq.Index = RFU; } else { - rq.Value = RFU ; - rq.Index = RESULT_IN ; - } + rq.Value = RFU; + rq.Index = RESULT_IN; + } rq.Length = 4; - if(OutCtrlRequest(&rq, vInstruction,4,Index) == SerialFlash_FALSE) - return false ; + if (OutCtrlRequest(&rq, vInstruction, 4, Index) == SerialFlash_FALSE) + return false; } } return true; } -bool CMX25LxxxdoRDSCUR(unsigned char* cSR,int Index) +bool CMX25LxxxdoRDSCUR(unsigned char* cSR, int Index) { - // read status - // send request - CNTRPIPE_RQ rq ; - unsigned char vInstruction=RDSCUR ; //size 1 - - rq.Function = URB_FUNCTION_VENDOR_ENDPOINT ; - rq.Direction = VENDOR_DIRECTION_OUT ; - rq.Request = TRANSCEIVE ; + // read status + // send request + CNTRPIPE_RQ rq; + unsigned char vInstruction = RDSCUR; //size 1 + + rq.Function = URB_FUNCTION_VENDOR_ENDPOINT; + rq.Direction = VENDOR_DIRECTION_OUT; + rq.Request = TRANSCEIVE; if (Is_NewUSBCommand(Index)) { - rq.Value = RESULT_IN; - rq.Index = RFU; + rq.Value = RESULT_IN; + rq.Index = RFU; } else { - rq.Value = RFU ; - rq.Index = RESULT_IN ; - } - rq.Length = 1; - - 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 ; + rq.Value = RFU; + rq.Index = RESULT_IN; + } + rq.Length = 1; + + 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)) { - rq.Value = 1; - rq.Index = RFU; + rq.Value = 1; + rq.Index = RFU; } else { - rq.Value = CTRL_TIMEOUT; - rq.Index = NO_REGISTER; - } - rq.Length = 1; + rq.Value = CTRL_TIMEOUT; + rq.Index = NO_REGISTER; + } + rq.Length = 1; - if(InCtrlRequest(&rq, &vBuffer,1,Index)==SerialFlash_FALSE) - return SerialFlash_FALSE ; + if (InCtrlRequest(&rq, &vBuffer, 1, Index) == SerialFlash_FALSE) + return SerialFlash_FALSE; - *cSR = vBuffer; + *cSR = vBuffer; - return SerialFlash_TRUE; + return SerialFlash_TRUE; } -bool CS25FLxxx_LargedoUnlockDYB(unsigned int cSR, int Index) +bool CS25FLxxx_LargedoUnlockDYB(unsigned int cSR, int Index) { - // wait until WIP = 0 - if(SerialFlash_waitForWIP(Index)==false) return false; + // wait until WIP = 0 + if (SerialFlash_waitForWIP(Index) == false) + return false; - unsigned char vInstruction[15] ; + unsigned char vInstruction[15]; int i; - unsigned int topend,bottomstart,end; + unsigned int topend, bottomstart, end; - if(strstr(Chip_Info.Class,SUPPORT_SPANSION_S25FLxx_Large) != NULL)//256 + if (strstr(Chip_Info.Class, SUPPORT_SPANSION_S25FLxx_Large) != NULL) //256 { - topend=0x20000; - bottomstart=0x1fe0000; - end=0x2000000; + topend = 0x20000; + bottomstart = 0x1fe0000; + end = 0x2000000; } else { - topend=0x20000; - bottomstart=0xfe0000; - end=0x1000000; + topend = 0x20000; + bottomstart = 0xfe0000; + end = 0x1000000; } - for( i=0;i>24); - vInstruction[2]=(unsigned char)(i>>16); - vInstruction[3]=(unsigned char)(i>>8); - vInstruction[4]=(unsigned char)(i); - vInstruction[5]=0xff; - FlashCommand_SendCommand_OutOnlyInstruction(vInstruction,6,Index); + SerialFlash_waitForWEL(Index); + vInstruction[0] = 0xE1; //write DYB + vInstruction[1] = (unsigned char)(i >> 24); + vInstruction[2] = (unsigned char)(i >> 16); + vInstruction[3] = (unsigned char)(i >> 8); + vInstruction[4] = (unsigned char)(i); + vInstruction[5] = 0xff; + FlashCommand_SendCommand_OutOnlyInstruction(vInstruction, 6, Index); SerialFlash_waitForWIP(Index); } - for( i=topend;i>24); - vInstruction[2]=(unsigned char)(i>>16); - vInstruction[3]=(unsigned char)(i>>8); - vInstruction[4]=(unsigned char)(i); - vInstruction[5]=0xff; - FlashCommand_SendCommand_OutOnlyInstruction(vInstruction,6,Index); + SerialFlash_waitForWEL(Index); + vInstruction[0] = 0xE1; //write DYB + vInstruction[1] = (unsigned char)(i >> 24); + vInstruction[2] = (unsigned char)(i >> 16); + vInstruction[3] = (unsigned char)(i >> 8); + vInstruction[4] = (unsigned char)(i); + vInstruction[5] = 0xff; + FlashCommand_SendCommand_OutOnlyInstruction(vInstruction, 6, Index); SerialFlash_waitForWIP(Index); } - for(i=bottomstart;i>24); - vInstruction[2]=(unsigned char)(i>>16); - vInstruction[3]=(unsigned char)(i>>8); - vInstruction[4]=(unsigned char)(i); - vInstruction[5]=0xff; - FlashCommand_SendCommand_OutOnlyInstruction(vInstruction,6,Index); + SerialFlash_waitForWEL(Index); + vInstruction[0] = 0xE1; //write DYB + vInstruction[1] = (unsigned char)(i >> 24); + vInstruction[2] = (unsigned char)(i >> 16); + vInstruction[3] = (unsigned char)(i >> 8); + vInstruction[4] = (unsigned char)(i); + vInstruction[5] = 0xff; + FlashCommand_SendCommand_OutOnlyInstruction(vInstruction, 6, Index); SerialFlash_waitForWIP(Index); } - return true; + return true; } -int SerialFlash_protectBlock(int bProtect,int Index) +int SerialFlash_protectBlock(int bProtect, int Index) { - if(strcmp(Chip_Info.Class,SUPPORT_SST_25xFxx)==0 || strstr(Chip_Info.Class,SUPPORT_SST_25xFxxB)!=NULL)// || strstr(Chip_Info.Class,SUPPORT_SST_25xFxxC)!=NULL) - return SST25xFxx_protectBlock( bProtect, Index); - else if(strstr(Chip_Info.Class,SUPPORT_SST_25xFxxA)!=NULL) - return SST25xFxxA_protectBlock( bProtect, Index); - else if(strstr(Chip_Info.Class,SUPPORT_ATMEL_AT25FSxxx)!=NULL || strstr(Chip_Info.Class,SUPPORT_ATMEL_AT25Fxxx)!=NULL) - return AT25FSxxx_protectBlock( bProtect, Index); - else if(strstr(Chip_Info.Class,SUPPORT_ATMEL_AT26xxx)!=NULL) - return AT26Fxxx_protectBlock( bProtect, Index); + if (strcmp(Chip_Info.Class, SUPPORT_SST_25xFxx) == 0 || strstr(Chip_Info.Class, SUPPORT_SST_25xFxxB) != NULL) // || strstr(Chip_Info.Class,SUPPORT_SST_25xFxxC)!=NULL) + return SST25xFxx_protectBlock(bProtect, Index); + else if (strstr(Chip_Info.Class, SUPPORT_SST_25xFxxA) != NULL) + return SST25xFxxA_protectBlock(bProtect, Index); + else if (strstr(Chip_Info.Class, SUPPORT_ATMEL_AT25FSxxx) != NULL || strstr(Chip_Info.Class, SUPPORT_ATMEL_AT25Fxxx) != NULL) + 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) { - unsigned char tmpSRVal; + unsigned char tmpSRVal; bool result; - result=CMX25LxxxdoRDSCUR(&tmpSRVal,Index); + result = CMX25LxxxdoRDSCUR(&tmpSRVal, Index); if (result == true && (tmpSRVal & 0x80) && Chip_Info.MXIC_WPmode == true) { - if(bProtect != false) return true; - SerialFlash_doWREN(Index) ; + if (bProtect != false) + return true; + SerialFlash_doWREN(Index); - unsigned char v=GBULK; - return FlashCommand_SendCommand_OutOnlyInstruction(&v, 1, 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) { CS25FLxxx_LargedoUnlockDYB(0, Index); } } else if (strstr(Chip_Info.Class, SUPPORT_SST_26xFxxC) != NULL) { - unsigned char v=0x98; - SerialFlash_waitForWEL(Index) ; - FlashCommand_SendCommand_OutOnlyInstruction(&v,1,Index); - } - if(SerialFlash_is_protectbits_set(Index)==bProtect) return 1; - - bool result = false ; - unsigned char tmpSRVal; - unsigned char dstSRVal ; + unsigned char v = 0x98; + SerialFlash_waitForWEL(Index); + FlashCommand_SendCommand_OutOnlyInstruction(&v, 1, Index); + } + if (SerialFlash_is_protectbits_set(Index) == bProtect) + return 1; + + bool result = false; + unsigned char tmpSRVal; + unsigned char dstSRVal; //int numOfRetry = 3 ; // un-protect block ,set BP2 BP1 BP2 to 0 - dstSRVal = 0x00 ; + dstSRVal = 0x00; // protect block ,set BP2 BP1 BP2 to 1 - if(bProtect){ - dstSRVal += 0x9C ; // 9C : 9001 1100 + if (bProtect) { + dstSRVal += 0x9C; // 9C : 9001 1100 } - int numOfRetry = 1000 ; - result = SerialFlash_doWRSR(dstSRVal,Index) ; - result = SerialFlash_doRDSR(&tmpSRVal,Index) ; - while( (tmpSRVal & 0x01) && numOfRetry > 0) // WIP = TRUE; + int numOfRetry = 1000; + result = SerialFlash_doWRSR(dstSRVal, Index); + result = SerialFlash_doRDSR(&tmpSRVal, Index); + while ((tmpSRVal & 0x01) && numOfRetry > 0) // WIP = TRUE; { // read SR - result = SerialFlash_doRDSR(&tmpSRVal,Index) ; + result = SerialFlash_doRDSR(&tmpSRVal, Index); - if(! result) return false; + if (!result) + return false; - numOfRetry -- ; + numOfRetry--; }; - return ((tmpSRVal ^ dstSRVal)& 0x0C ) ? 0:1; + return ((tmpSRVal ^ dstSRVal) & 0x0C) ? 0 : 1; } -int SerialFlash_EnableQuadIO(int bEnable,int boRW,int Index) +int SerialFlash_EnableQuadIO(int bEnable, int boRW, int Index) { m_boEnWriteQuadIO = (bEnable & boRW); //Simon: ported done??? m_boEnReadQuadIO = (bEnable & boRW); //Simon: ported done??? - return SerialFlash_TRUE; + return SerialFlash_TRUE; } -bool CEN25QHxx_LargeEnable4ByteAddrMode(bool Enable4Byte,int Index) +bool CEN25QHxx_LargeEnable4ByteAddrMode(bool Enable4Byte, int Index) { -// SerialFlash_doWREN(Index); + // SerialFlash_doWREN(Index); if (Enable4Byte) { - unsigned char v= EN4B; - if( FlashCommand_TransceiveOut(&v,1,false,Index)==SerialFlash_FALSE) + unsigned char v = EN4B; + if (FlashCommand_TransceiveOut(&v, 1, false, Index) == SerialFlash_FALSE) return false; } else { - unsigned char v= EXIT4B; - if(FlashCommand_TransceiveOut(&v,1,false,Index)==SerialFlash_FALSE) + unsigned char v = EXIT4B; + if (FlashCommand_TransceiveOut(&v, 1, false, Index) == SerialFlash_FALSE) return false; } - return true; + return true; } -bool CN25Qxxx_LargeRDFSR(unsigned char *cSR, int Index) +bool CN25Qxxx_LargeRDFSR(unsigned char* cSR, int Index) { // send request - CNTRPIPE_RQ rq ; - unsigned char vInstruction=0x70 ; //size 1 + CNTRPIPE_RQ rq; + unsigned char vInstruction = 0x70; //size 1 - rq.Function = URB_FUNCTION_VENDOR_ENDPOINT ; - rq.Direction = VENDOR_DIRECTION_OUT ; - rq.Request = TRANSCEIVE ; + rq.Function = URB_FUNCTION_VENDOR_ENDPOINT; + rq.Direction = VENDOR_DIRECTION_OUT; + rq.Request = TRANSCEIVE; if (Is_NewUSBCommand(Index)) { - rq.Value = RESULT_IN; - rq.Index = RFU; + rq.Value = RESULT_IN; + rq.Index = RFU; } else { - rq.Value = RFU ; - rq.Index = RESULT_IN ; - } + rq.Value = RFU; + rq.Index = RESULT_IN; + } rq.Length = 1; - if(OutCtrlRequest(&rq, &vInstruction,1,Index)==SerialFlash_FALSE) - return 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 ; + 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)) { - rq.Value = 1; - rq.Index = 0; + rq.Value = 1; + rq.Index = 0; } else { - rq.Value = CTRL_TIMEOUT ; - rq.Index = NO_REGISTER ; - } - rq.Length = 1;//(unsigned long) 1; + rq.Value = CTRL_TIMEOUT; + rq.Index = NO_REGISTER; + } + rq.Length = 1; //(unsigned long) 1; - if(InCtrlRequest(&rq, &vBuffer, 1, Index) == SerialFlash_FALSE) - return SerialFlash_FALSE ; + if (InCtrlRequest(&rq, &vBuffer, 1, Index) == SerialFlash_FALSE) + return SerialFlash_FALSE; - *cSR = vBuffer; - return SerialFlash_TRUE; + *cSR = vBuffer; + return SerialFlash_TRUE; } -bool CN25Qxxx_Large_doWRVCR(unsigned char ucVCR,int Index) +bool CN25Qxxx_Large_doWRVCR(unsigned char ucVCR, int Index) { - unsigned char vInstruction[2]; //size 1 + unsigned char vInstruction[2]; //size 1 - SerialFlash_waitForWIP(Index); - SerialFlash_waitForWEL(Index); - vInstruction[0]=0x81; - vInstruction[1]=ucVCR&0xFF; + 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; @@ -1404,60 +1412,60 @@ bool CN25Qxxx_Large_doWRVCR(unsigned char ucVCR,int Index) if(numOfRetry<=0) return false; #endif - return true; + return true; } -bool CN25Qxxx_Large_doRDENVCR(unsigned char *ucENVCR,int Index) +bool CN25Qxxx_Large_doRDENVCR(unsigned char* ucENVCR, int Index) { - CNTRPIPE_RQ rq ; - unsigned char vInstruction; //size 1 - vInstruction=0x65; + CNTRPIPE_RQ rq; + unsigned char vInstruction; //size 1 + vInstruction = 0x65; - rq.Function = URB_FUNCTION_VENDOR_ENDPOINT ; - rq.Direction = VENDOR_DIRECTION_OUT ; - rq.Request = TRANSCEIVE ; + rq.Function = URB_FUNCTION_VENDOR_ENDPOINT; + rq.Direction = VENDOR_DIRECTION_OUT; + rq.Request = TRANSCEIVE; if (Is_NewUSBCommand(Index)) { - rq.Value = RESULT_IN ; - rq.Index = RFU ; + rq.Value = RESULT_IN; + rq.Index = RFU; } else { - rq.Value = RFU ; - rq.Index = RESULT_IN ; - } - rq.Length = 1;//(unsigned long) 1 ; - - if(OutCtrlRequest(&rq, &vInstruction, 1, Index) == SerialFlash_FALSE) - return SerialFlash_FALSE ; - - // second control packet : fetch data - unsigned char vBuffer; - rq.Function = URB_FUNCTION_VENDOR_ENDPOINT ; - rq.Direction = VENDOR_DIRECTION_IN ; - rq.Request = TRANSCEIVE ; + rq.Value = RFU; + rq.Index = RESULT_IN; + } + rq.Length = 1; //(unsigned long) 1 ; + + if (OutCtrlRequest(&rq, &vInstruction, 1, Index) == SerialFlash_FALSE) + return SerialFlash_FALSE; + + // second control packet : fetch data + unsigned char vBuffer; + rq.Function = URB_FUNCTION_VENDOR_ENDPOINT; + rq.Direction = VENDOR_DIRECTION_IN; + rq.Request = TRANSCEIVE; if (Is_NewUSBCommand(Index)) { - rq.Value = 1; - rq.Index = 0; + rq.Value = 1; + rq.Index = 0; } else { - rq.Value = CTRL_TIMEOUT ; - rq.Index = NO_REGISTER ; - } - rq.Length = 1; - - if(InCtrlRequest(&rq, &vBuffer, 1, Index) == SerialFlash_FALSE) - return SerialFlash_FALSE ; - *ucENVCR = vBuffer; - return SerialFlash_TRUE; + rq.Value = CTRL_TIMEOUT; + rq.Index = NO_REGISTER; + } + rq.Length = 1; + + if (InCtrlRequest(&rq, &vBuffer, 1, Index) == SerialFlash_FALSE) + return SerialFlash_FALSE; + *ucENVCR = vBuffer; + return SerialFlash_TRUE; } -bool CN25Qxxx_Large_doWRENVCR(unsigned char ucENVCR,int Index) +bool CN25Qxxx_Large_doWRENVCR(unsigned char ucENVCR, int Index) { - unsigned char vInstruction[2]; //size 1 + unsigned char vInstruction[2]; //size 1 - SerialFlash_waitForWIP(Index); - SerialFlash_waitForWEL(Index); - vInstruction[0]=0x61; - vInstruction[1]=ucENVCR&0xFF; + 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); @@ -1473,135 +1481,135 @@ bool CN25Qxxx_Large_doWRENVCR(unsigned char ucENVCR,int Index) if(numOfRetry<=0) return false; #endif - return true; + return true; } -bool CN25Qxxx_Large_doRDVCR(unsigned char *ucVCR,int Index) +bool CN25Qxxx_Large_doRDVCR(unsigned char* ucVCR, int Index) { - CNTRPIPE_RQ rq ; - unsigned char vInstruction; //size 1 - vInstruction=0x85; + CNTRPIPE_RQ rq; + unsigned char vInstruction; //size 1 + vInstruction = 0x85; - rq.Function = URB_FUNCTION_VENDOR_ENDPOINT ; - rq.Direction = VENDOR_DIRECTION_OUT ; - rq.Request = TRANSCEIVE ; + rq.Function = URB_FUNCTION_VENDOR_ENDPOINT; + rq.Direction = VENDOR_DIRECTION_OUT; + rq.Request = TRANSCEIVE; if (Is_NewUSBCommand(Index)) { - rq.Value = RESULT_IN ; - rq.Index = RFU ; + rq.Value = RESULT_IN; + rq.Index = RFU; } else { - rq.Value = RFU ; - rq.Index = RESULT_IN ; - } - rq.Length = 1;//(unsigned long) 1 ; - - if(OutCtrlRequest(&rq, &vInstruction, 1, Index) == SerialFlash_FALSE) - return SerialFlash_FALSE ; - - // second control packet : fetch data - unsigned char vBuffer; - rq.Function = URB_FUNCTION_VENDOR_ENDPOINT ; - rq.Direction = VENDOR_DIRECTION_IN ; - rq.Request = TRANSCEIVE ; + rq.Value = RFU; + rq.Index = RESULT_IN; + } + rq.Length = 1; //(unsigned long) 1 ; + + if (OutCtrlRequest(&rq, &vInstruction, 1, Index) == SerialFlash_FALSE) + return SerialFlash_FALSE; + + // second control packet : fetch data + unsigned char vBuffer; + rq.Function = URB_FUNCTION_VENDOR_ENDPOINT; + rq.Direction = VENDOR_DIRECTION_IN; + rq.Request = TRANSCEIVE; if (Is_NewUSBCommand(Index)) { - rq.Value = 1; - rq.Index = 0; + rq.Value = 1; + rq.Index = 0; } else { - rq.Value = CTRL_TIMEOUT ; - rq.Index = NO_REGISTER ; - } - rq.Length = 1; - - if(InCtrlRequest(&rq, &vBuffer, 1, Index) == SerialFlash_FALSE) - return SerialFlash_FALSE ; - *ucVCR = vBuffer; - return SerialFlash_TRUE; + rq.Value = CTRL_TIMEOUT; + rq.Index = NO_REGISTER; + } + rq.Length = 1; + + if (InCtrlRequest(&rq, &vBuffer, 1, Index) == SerialFlash_FALSE) + return SerialFlash_FALSE; + *ucVCR = vBuffer; + return SerialFlash_TRUE; } -bool CN25Qxxx_MutipleDIe_LargeRDEAR(unsigned char *cSR, int Index) -{// read status -//Simon if(! m_usb.is_open() ) -//Simon return false ; +bool CN25Qxxx_MutipleDIe_LargeRDEAR(unsigned char* cSR, int Index) +{ // read status + //Simon if(! m_usb.is_open() ) + //Simon return false ; - // send request - CNTRPIPE_RQ rq ; - unsigned char vInstruction; //size 1 + // send request + CNTRPIPE_RQ rq; + unsigned char vInstruction; //size 1 - // first control packet - vInstruction = 0xc8; + // first control packet + vInstruction = 0xc8; - rq.Function = URB_FUNCTION_VENDOR_ENDPOINT ; - rq.Direction = VENDOR_DIRECTION_OUT ; - rq.Request = TRANSCEIVE ; + rq.Function = URB_FUNCTION_VENDOR_ENDPOINT; + rq.Direction = VENDOR_DIRECTION_OUT; + rq.Request = TRANSCEIVE; if (Is_NewUSBCommand(Index)) { - rq.Value = RESULT_IN ; - rq.Index = RFU ; + rq.Value = RESULT_IN; + rq.Index = RFU; } else { - rq.Value = RFU ; - rq.Index = RESULT_IN ; - } - rq.Length = 1;//(unsigned long) 1 ; - - 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 ; + rq.Value = RFU; + rq.Index = RESULT_IN; + } + rq.Length = 1; //(unsigned long) 1 ; + + 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)) { - rq.Value = 1; - rq.Index = 0; + rq.Value = 1; + rq.Index = 0; } else { - rq.Value = CTRL_TIMEOUT ; - rq.Index = NO_REGISTER ; - } - rq.Length = 1;//(unsigned long) 1; + rq.Value = CTRL_TIMEOUT; + rq.Index = NO_REGISTER; + } + rq.Length = 1; //(unsigned long) 1; - if(InCtrlRequest(&rq, &vBuffer, 1, Index) == SerialFlash_FALSE) - return SerialFlash_FALSE ; + if (InCtrlRequest(&rq, &vBuffer, 1, Index) == SerialFlash_FALSE) + return SerialFlash_FALSE; - *cSR = vBuffer; - return SerialFlash_TRUE; + *cSR = vBuffer; + return SerialFlash_TRUE; } bool CN25Qxxx_MutipleDIe_LargeWREAR(unsigned char cSR, int Index) { unsigned char v[2]; - v[0]=0xc5; - v[1]=cSR; - FlashCommand_TransceiveOut(v,2,false,Index); - return true; + v[0] = 0xc5; + v[1] = cSR; + FlashCommand_TransceiveOut(v, 2, false, Index); + return true; } -bool CS25FLxx_LargeEnable4ByteAddrMode(bool Enable4Byte,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); + SerialFlash_waitForWEL(Index); if (Enable4Byte) { - unsigned char v[2]; - v[0]=0x17; - v[1]=0x80; - FlashCommand_TransceiveOut(v,2,false,Index); + 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); - } + 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); + unsigned char v = EN4B; + FlashCommand_TransceiveOut(&v, 1, false, Index); return true; } else { - unsigned char v= EXIT4B; - FlashCommand_TransceiveOut(&v,1,false,Index); - return true; - } - } - return true; + unsigned char v = EXIT4B; + FlashCommand_TransceiveOut(&v, 1, false, Index); + return true; + } + } + return true; } -bool CN25Qxxx_LargeEnable4ByteAddrMode(bool Enable4Byte,int Index) +bool CN25Qxxx_LargeEnable4ByteAddrMode(bool Enable4Byte, int Index) { #if 0 if(Enable4Byte) @@ -1614,85 +1622,85 @@ bool CN25Qxxx_LargeEnable4ByteAddrMode(bool Enable4Byte,int Index) return true; #else if (Enable4Byte) { - unsigned char v= EN4B; - int numOfRetry = 5 ; + unsigned char v = EN4B; + int numOfRetry = 5; unsigned char re; - do{ + do { SerialFlash_waitForWEL(Index); - FlashCommand_TransceiveOut(&v,1,false,Index); + FlashCommand_TransceiveOut(&v, 1, false, Index); Sleep(100); - CN25Qxxx_LargeRDFSR(&re,Index); - }while((re & 0x01)==0 && numOfRetry-- > 0); + CN25Qxxx_LargeRDFSR(&re, Index); + } while ((re & 0x01) == 0 && numOfRetry-- > 0); return true; } else { unsigned char re; -// CN25Qxxx_LargeRDFSR(&re,Index); - unsigned char v= EXIT4B; - int numOfRetry = 5 ; + // CN25Qxxx_LargeRDFSR(&re,Index); + unsigned char v = EXIT4B; + int numOfRetry = 5; - do{ - SerialFlash_waitForWEL(Index); - FlashCommand_TransceiveOut(&v,1,false,Index); + do { + SerialFlash_waitForWEL(Index); + FlashCommand_TransceiveOut(&v, 1, false, Index); Sleep(100); - CN25Qxxx_LargeRDFSR(&re,Index); - }while((re & 0x01)==1 && numOfRetry-- > 0); + CN25Qxxx_LargeRDFSR(&re, Index); + } while ((re & 0x01) == 1 && numOfRetry-- > 0); return true; } #endif } -int S70FSxxx_Large_Enable4ByteAddrMode(int Enable4Byte,int Index) +int S70FSxxx_Large_Enable4ByteAddrMode(int Enable4Byte, int Index) { if (Enable4Byte) { - unsigned char v= EN4B; - int numOfRetry = 5 ; + unsigned char v = EN4B; + int numOfRetry = 5; unsigned char re; - do{ - // SerialFlash_waitForWEL(Index); - FlashCommand_TransceiveOut(&v,1,false,Index); + do { + // SerialFlash_waitForWEL(Index); + FlashCommand_TransceiveOut(&v, 1, false, Index); Sleep(100); - S70FSxxx_Large_doRDCR2V(false, &re,Index); - }while((re & 0x80)==0 && numOfRetry-- > 0); - do{ - // SerialFlash_waitForWEL(Index); - FlashCommand_TransceiveOut(&v,1,false,Index); + S70FSxxx_Large_doRDCR2V(false, &re, Index); + } while ((re & 0x80) == 0 && numOfRetry-- > 0); + do { + // SerialFlash_waitForWEL(Index); + FlashCommand_TransceiveOut(&v, 1, false, Index); Sleep(100); - S70FSxxx_Large_doRDCR2V(true, &re,Index); + S70FSxxx_Large_doRDCR2V(true, &re, Index); - }while((re & 0x80)==0 && numOfRetry-- > 0); + } while ((re & 0x80) == 0 && numOfRetry-- > 0); return true; } else { unsigned char re; - unsigned char v= EXIT4B; - int numOfRetry = 5 ; + unsigned char v = EXIT4B; + int numOfRetry = 5; - do{ - // SerialFlash_waitForWEL(Index); - FlashCommand_TransceiveOut(&v,1,false,Index); + do { + // SerialFlash_waitForWEL(Index); + FlashCommand_TransceiveOut(&v, 1, false, Index); Sleep(100); - S70FSxxx_Large_doRDCR2V(false, &re,Index); - }while((re & 0x80)==0 && numOfRetry-- > 0); + S70FSxxx_Large_doRDCR2V(false, &re, Index); + } while ((re & 0x80) == 0 && numOfRetry-- > 0); - do{ - // SerialFlash_waitForWEL(Index); - FlashCommand_TransceiveOut(&v,1,false,Index); + do { + // SerialFlash_waitForWEL(Index); + FlashCommand_TransceiveOut(&v, 1, false, Index); Sleep(100); - S70FSxxx_Large_doRDCR2V(true, &re,Index); - }while((re & 0x80)==0 && numOfRetry-- > 0); + S70FSxxx_Large_doRDCR2V(true, &re, Index); + } while ((re & 0x80) == 0 && numOfRetry-- > 0); return true; } } //Simon: unused ??? -int SerialFlash_Enable4ByteAddrMode(int bEnable,int Index) +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) - return CEN25QHxx_LargeEnable4ByteAddrMode(bEnable,Index); - else if(strstr(Chip_Info.Class,SUPPORT_SPANSION_S70FSxx_Large)!= NULL) - return S70FSxxx_Large_Enable4ByteAddrMode(bEnable,Index); - else if(strstr(Chip_Info.Class,SUPPORT_NUMONYX_N25Qxxx_Large) != NULL) - return CN25Qxxx_LargeEnable4ByteAddrMode(bEnable, Index); - else if(strstr(Chip_Info.Class,SUPPORT_SPANSION_S25FLxx_Large)!=NULL) - return CS25FLxx_LargeEnable4ByteAddrMode(bEnable, Index); + return CEN25QHxx_LargeEnable4ByteAddrMode(bEnable, Index); + else if (strstr(Chip_Info.Class, SUPPORT_SPANSION_S70FSxx_Large) != NULL) + return S70FSxxx_Large_Enable4ByteAddrMode(bEnable, Index); + else if (strstr(Chip_Info.Class, SUPPORT_NUMONYX_N25Qxxx_Large) != NULL) + return CN25Qxxx_LargeEnable4ByteAddrMode(bEnable, Index); + else if (strstr(Chip_Info.Class, SUPPORT_SPANSION_S25FLxx_Large) != NULL) + return CS25FLxx_LargeEnable4ByteAddrMode(bEnable, Index); return SerialFlash_TRUE; } @@ -1703,9 +1711,9 @@ int SerialFlash_Enable4ByteAddrMode(int bEnable,int Index) * \returns * true, if blank; false otherwise */ -int SerialFlash_rangeBlankCheck(struct CAddressRange *Range,int Index) +int SerialFlash_rangeBlankCheck(struct CAddressRange* Range, int Index) { - unsigned char *vBuffer; + unsigned char* vBuffer; unsigned int i; Range->length = Range->end - Range->start; @@ -1715,17 +1723,17 @@ int SerialFlash_rangeBlankCheck(struct CAddressRange *Range,int Index) if (vBuffer != 0) { if (SerialFlash_rangeRead(Range, vBuffer, Index) == SerialFlash_FALSE) { free(vBuffer); - return false ; + return false; } 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; + return false; } } free(vBuffer); - return true ; + return true; } else { free(vBuffer); return SerialFlash_FALSE; @@ -1749,40 +1757,40 @@ int SerialFlash_rangeBlankCheck(struct CAddressRange *Range,int Index) * true, if successfull * false, if data size larger than memory size, or operation fails */ -int SerialFlash_rangeProgram(struct CAddressRange *AddrRange, unsigned char *vData,int Index) +int SerialFlash_rangeProgram(struct CAddressRange* AddrRange, unsigned char* vData, int Index) { - 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); + 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) { - if(g_bIsSF600[Index]==true) - return SerialFlash_bulkPipeProgram(AddrRange, vData, PP_4ADR_256BYTE,mcode_ProgramCode_4Adr,Index); + 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); + 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) { - return SerialFlash_bulkPipeProgram(AddrRange, vData, PP_4ADDR_256BYTE_S70FS01GS,mcode_ProgramCode_4Adr_S70FSxxx,Index); + return SerialFlash_bulkPipeProgram(AddrRange, vData, PP_4ADDR_256BYTE_S70FS01GS, mcode_ProgramCode_4Adr_S70FSxxx, Index); } else return false; } else - return SerialFlash_bulkPipeProgram(AddrRange, vData, mcode_Program, mcode_ProgramCode_4Adr,Index); + return SerialFlash_bulkPipeProgram(AddrRange, vData, mcode_Program, mcode_ProgramCode_4Adr, Index); } -int SerialFlash_rangeRead(struct CAddressRange *AddrRange, unsigned char *vData,int Index) +int SerialFlash_rangeRead(struct CAddressRange* AddrRange, unsigned char* vData, int Index) { 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 (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(g_bIsSF600[Index]==true) - return SerialFlash_bulkPipeRead(AddrRange, vData, BULK_4BYTE_FAST_READ,mcode_ReadCode_S70FSxxx,Index); + if (g_bIsSF600[Index] == true) + return SerialFlash_bulkPipeRead(AddrRange, vData, BULK_4BYTE_FAST_READ, mcode_ReadCode_S70FSxxx, Index); else - return false; + return false; } else { - return SerialFlash_bulkPipeRead(AddrRange, vData, (unsigned char) mcode_Read, (unsigned char) mcode_ReadCode, Index); - } + return SerialFlash_bulkPipeRead(AddrRange, vData, (unsigned char)mcode_Read, (unsigned char)mcode_ReadCode, Index); + } }; /** @@ -1801,20 +1809,20 @@ int SerialFlash_rangeRead(struct CAddressRange *AddrRange, unsigned char *vData, */ int SerialFlash_DoPolling(int Index) { - CNTRPIPE_RQ rq ; - unsigned char vDataPack[4]; + CNTRPIPE_RQ rq; + unsigned char vDataPack[4]; - rq.Function = URB_FUNCTION_VENDOR_ENDPOINT ; - rq.Direction = VENDOR_DIRECTION_IN ; - rq.Request = POLLING ; - rq.Value = (CTRL_TIMEOUT >> 16) & 0xffff ; - rq.Index = CTRL_TIMEOUT & 0xffff ; - rq.Length = (unsigned long)(4) ; + rq.Function = URB_FUNCTION_VENDOR_ENDPOINT; + rq.Direction = VENDOR_DIRECTION_IN; + rq.Request = POLLING; + rq.Value = (CTRL_TIMEOUT >> 16) & 0xffff; + rq.Index = CTRL_TIMEOUT & 0xffff; + rq.Length = (unsigned long)(4); - if(OutCtrlRequest(&rq, vDataPack, 4,Index) == SerialFlash_FALSE) - return SerialFlash_FALSE ; + if (OutCtrlRequest(&rq, vDataPack, 4, Index) == SerialFlash_FALSE) + return SerialFlash_FALSE; - return SerialFlash_TRUE ; + return SerialFlash_TRUE; } /** @@ -1832,117 +1840,117 @@ int SerialFlash_DoPolling(int Index) */ int SerialFlash_is_good() { -//Simon return ( m_info.is_good() && m_usb.is_open() ) ; - return SerialFlash_TRUE ; + //Simon return ( m_info.is_good() && m_usb.is_open() ) ; + return SerialFlash_TRUE; } -int SerialFlash_batchErase(uintptr_t* vAddrs,size_t AddrSize,int Index) +int SerialFlash_batchErase(uintptr_t* vAddrs, size_t AddrSize, int Index) { - if(!SerialFlash_StartofOperation(Index)) - return false; + if (!SerialFlash_StartofOperation(Index)) + return false; // if(strstr(Chip_Info.Class,SUPPORT_ATMEL_45DBxxxB) != NULL || // strstr(Chip_Info.Class,SUPPORT_ATMEL_45DBxxxD) != NULL) -// return AT45batchErase(vAddrs, AddrSize,Index); + // return AT45batchErase(vAddrs, AddrSize,Index); 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 ; + if (SerialFlash_protectBlock(false, Index) == SerialFlash_FALSE) + return false; SerialFlash_Enable4ByteAddrMode(true, Index); // send request - CNTRPIPE_RQ rq ; + CNTRPIPE_RQ rq; size_t i; - unsigned char vInstruction[5];//(5, mcode_SegmentErase) ; - vInstruction[0]=mcode_SegmentErase; + unsigned char vInstruction[5]; //(5, mcode_SegmentErase) ; + vInstruction[0] = mcode_SegmentErase; -// printf("mcode_SegmentErase=%x\n",mcode_SegmentErase); + // printf("mcode_SegmentErase=%x\n",mcode_SegmentErase); // instrcution format - rq.Function = URB_FUNCTION_VENDOR_ENDPOINT ; - rq.Direction = VENDOR_DIRECTION_OUT ; - rq.Request = TRANSCEIVE ; + rq.Function = URB_FUNCTION_VENDOR_ENDPOINT; + rq.Direction = VENDOR_DIRECTION_OUT; + rq.Request = TRANSCEIVE; if (Is_NewUSBCommand(Index)) { - rq.Value = NO_RESULT_IN; - rq.Index = RFU; + rq.Value = NO_RESULT_IN; + rq.Index = RFU; } else { - rq.Value = RFU ; - rq.Index = NO_RESULT_IN ; - } + rq.Value = RFU; + rq.Index = NO_RESULT_IN; + } rq.Length = 5; for (i = 0; i < AddrSize; i++) { - SerialFlash_waitForWEL(Index) ; + SerialFlash_waitForWEL(Index); 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 + 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 { // MSB~ LSB (23...0) - vInstruction[1] = (unsigned char)((vAddrs[i] >> 16) & 0xff) ; //MSB - vInstruction[2] = (unsigned char)((vAddrs[i] >> 8) & 0xff) ; //M - vInstruction[3] = (unsigned char)(vAddrs[i] & 0xff) ; //LSB + vInstruction[1] = (unsigned char)((vAddrs[i] >> 16) & 0xff); //MSB + vInstruction[2] = (unsigned char)((vAddrs[i] >> 8) & 0xff); //M + vInstruction[3] = (unsigned char)(vAddrs[i] & 0xff); //LSB rq.Length = 4; } - OutCtrlRequest(&rq, vInstruction,rq.Length,Index); + OutCtrlRequest(&rq, vInstruction, rq.Length, Index); SerialFlash_waitForWIP(Index); } SerialFlash_Enable4ByteAddrMode(false, Index); - if(!SerialFlash_EndofOperation(Index)) - return false; - return true ; + if (!SerialFlash_EndofOperation(Index)) + return false; + return true; } -int SerialFlash_rangeErase(unsigned char cmd, size_t sectionSize, struct CAddressRange *AddrRange,int Index) +int SerialFlash_rangeErase(unsigned char cmd, size_t sectionSize, struct CAddressRange* AddrRange, int Index) { - if(strstr(Chip_Info.Class,SUPPORT_ATMEL_45DBxxxB) != NULL || strstr(Chip_Info.Class,SUPPORT_ATMEL_45DBxxxD) != NULL) - return AT45rangSectorErase(sectionSize, *AddrRange,Index); + if (strstr(Chip_Info.Class, SUPPORT_ATMEL_45DBxxxB) != NULL || strstr(Chip_Info.Class, SUPPORT_ATMEL_45DBxxxD) != NULL) + return AT45rangSectorErase(sectionSize, *AddrRange, Index); - if(SerialFlash_protectBlock(false,Index) == SerialFlash_FALSE) - return SerialFlash_FALSE ; + if (SerialFlash_protectBlock(false, Index) == SerialFlash_FALSE) + return SerialFlash_FALSE; SerialFlash_Enable4ByteAddrMode(true, Index); // send request - CNTRPIPE_RQ rq ; + CNTRPIPE_RQ rq; unsigned char vInstruction[5]; vInstruction[0] = cmd; // instrcution format - rq.Function = URB_FUNCTION_VENDOR_ENDPOINT ; - rq.Direction = VENDOR_DIRECTION_OUT ; - rq.Request = TRANSCEIVE ; + rq.Function = URB_FUNCTION_VENDOR_ENDPOINT; + rq.Direction = VENDOR_DIRECTION_OUT; + rq.Request = TRANSCEIVE; if (Is_NewUSBCommand(Index)) { - rq.Value = RESULT_IN; - rq.Index = RFU; + rq.Value = RESULT_IN; + rq.Index = RFU; } else { - rq.Value = RFU ; - rq.Index = RESULT_IN ; - } - rq.Length = (unsigned long)(1) ; + rq.Value = RFU; + rq.Index = RESULT_IN; + } + rq.Length = (unsigned long)(1); - size_t sectorNum = (AddrRange->end - AddrRange->start + sectionSize - 1) / sectionSize ; + size_t sectorNum = (AddrRange->end - AddrRange->start + sectionSize - 1) / sectionSize; size_t i; for (i = 0; i < sectorNum; ++i) { - SerialFlash_waitForWEL(Index) ; + SerialFlash_waitForWEL(Index); // MSB~ LSB (23...0) - size_t addr = (AddrRange->start + i * sectionSize) ; + size_t addr = (AddrRange->start + i * sectionSize); 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) ; + 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 { // MSB~ LSB (23...0) - vInstruction[1] = (unsigned char)((addr >> 16) & 0xff) ; //MSB - vInstruction[2] = (unsigned char)((addr >> 8) & 0xff) ; //M - vInstruction[3] = (unsigned char)(addr & 0xff) ; //LSB + vInstruction[1] = (unsigned char)((addr >> 16) & 0xff); //MSB + vInstruction[2] = (unsigned char)((addr >> 8) & 0xff); //M + vInstruction[3] = (unsigned char)(addr & 0xff); //LSB rq.Length = (unsigned long)(4); } @@ -1950,69 +1958,69 @@ int SerialFlash_rangeErase(unsigned char cmd, size_t sectionSize, struct CAddres if ((b == SerialFlash_FALSE) || m_isCanceled) return false; - SerialFlash_waitForWIP(Index) ; + SerialFlash_waitForWIP(Index); } SerialFlash_Enable4ByteAddrMode(false, Index); - return true ; + return true; } -bool S70FSxxx_Large_chipErase(unsigned int Addr,unsigned int Length,int USBIndex) +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 - S70FSxxx_Large_waitForWEL(true,USBIndex) ; - unsigned char vInstruction[5]={0}; - vInstruction[0]=0xFE; - vInstruction[1]=0x00; - vInstruction[2]=0x00; - vInstruction[3]=0x00; - vInstruction[4]=0x00; + S70FSxxx_Large_Enable4ByteAddrMode(true, USBIndex); + + // wait until WIP = 0 + S70FSxxx_Large_waitForWIP(true, USBIndex); + // wait until WEL = 1 + S70FSxxx_Large_waitForWEL(true, USBIndex); + unsigned char vInstruction[5] = { 0 }; + vInstruction[0] = 0xFE; + vInstruction[1] = 0x00; + vInstruction[2] = 0x00; + vInstruction[3] = 0x00; + vInstruction[4] = 0x00; FlashCommand_SendCommand_OutOnlyInstruction(vInstruction, 5, USBIndex); - S70FSxxx_Large_waitForWIP(true,USBIndex) ; //check die 1 + S70FSxxx_Large_waitForWIP(true, USBIndex); //check die 1 - S70FSxxx_Large_Enable4ByteAddrMode(false, USBIndex); - S70FSxxx_Large_waitForWIP(false,USBIndex) ; + S70FSxxx_Large_Enable4ByteAddrMode(false, USBIndex); + S70FSxxx_Large_waitForWIP(false, USBIndex); - // wait until WEL = 1 - S70FSxxx_Large_waitForWEL(false,USBIndex) ; - vInstruction[0]=0xFE; - vInstruction[1]=0x04; - vInstruction[2]=0x00; - vInstruction[3]=0x00; - vInstruction[4]=0x00; + // wait until WEL = 1 + S70FSxxx_Large_waitForWEL(false, USBIndex); + vInstruction[0] = 0xFE; + vInstruction[1] = 0x04; + vInstruction[2] = 0x00; + vInstruction[3] = 0x00; + vInstruction[4] = 0x00; FlashCommand_SendCommand_OutOnlyInstruction(vInstruction, 5, USBIndex); - S70FSxxx_Large_waitForWIP(false,USBIndex) ; //check die 2 + S70FSxxx_Large_waitForWIP(false, USBIndex); //check die 2 return true; } /// chip erase bool SerialFlash_chipErase(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 (!SerialFlash_StartofOperation(Index)) + return false; + if (strstr(Chip_Info.Class, SUPPORT_ATMEL_45DBxxxB) != NULL || strstr(Chip_Info.Class, SUPPORT_ATMEL_45DBxxxD) != NULL) return AT45chipErase(0, Chip_Info.ChipSizeInByte, Index); - if(strstr(Chip_Info.Class,SUPPORT_SPANSION_S70FSxx_Large) != NULL) - return S70FSxxx_Large_chipErase(0, Chip_Info.ChipSizeInByte, 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; - SerialFlash_waitForWEL(Index) ; + SerialFlash_waitForWEL(Index); unsigned char v = mcode_ChipErase; - FlashCommand_SendCommand_OutOnlyInstruction(&v,1,Index); - SerialFlash_waitForWIP(Index) ; + FlashCommand_SendCommand_OutOnlyInstruction(&v, 1, Index); + SerialFlash_waitForWIP(Index); // if( SerialFlash_protectBlock(m_bProtectAfterWritenErase,Index) == // SerialFlash_FALSE) return false ; - if(!SerialFlash_EndofOperation(Index)) - return false; - return true ; + if (!SerialFlash_EndofOperation(Index)) + return false; + return true; } /// die erase @@ -2023,68 +2031,73 @@ int SerialFlash_DieErase(int Index) SerialFlash_Enable4ByteAddrMode(true, Index); // printf("Die erase!\n"); - // send request - unsigned char vInstruction[5]; - int numOfRetry = 5 ; - unsigned char re; - vInstruction[0] = mcode_ChipErase; + // send request + unsigned char vInstruction[5]; + int numOfRetry = 5; + unsigned char re; + vInstruction[0] = 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; + 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++) { - SerialFlash_waitForWEL(Index) ; - - // MSB~ LSB (23...0) - size_t addr = (i * die_size) ; - // 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 - - int b = FlashCommand_SendCommand_OutOnlyInstruction(vInstruction, 5, Index); - if((b==SerialFlash_FALSE)|| m_isCanceled) return false ; - - SerialFlash_waitForWIP(Index) ; - numOfRetry = 5 ; - do{ - CN25Qxxx_LargeRDFSR(&re,Index); - Sleep(5); - }while((re & 0x01)==0 && numOfRetry-- > 0); - } - SerialFlash_Enable4ByteAddrMode(false, Index); - return true ; + SerialFlash_waitForWEL(Index); + + // MSB~ LSB (23...0) + size_t addr = (i * die_size); + // 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 + + int b = FlashCommand_SendCommand_OutOnlyInstruction(vInstruction, 5, Index); + if ((b == SerialFlash_FALSE) || m_isCanceled) + return false; + + SerialFlash_waitForWIP(Index); + numOfRetry = 5; + do { + CN25Qxxx_LargeRDFSR(&re, Index); + Sleep(5); + } while ((re & 0x01) == 0 && numOfRetry-- > 0); + } + SerialFlash_Enable4ByteAddrMode(false, Index); + return true; } -int SerialFlash_bulkPipeProgram(struct CAddressRange *AddrRange, unsigned char *vData, unsigned char modeWrite, unsigned char WriteCom, int Index) +int SerialFlash_bulkPipeProgram(struct CAddressRange* AddrRange, unsigned char* vData, unsigned char modeWrite, unsigned char WriteCom, int Index) { - size_t i,j,divider; - unsigned char *itr_begin; + 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\n", modeWrite,WriteCom); itr_begin = vData; switch (modeWrite) { - //transfer how many data each time - case MODE_NUMONYX_PCM: - case PP_32BYTE: - divider=9; - break; - case PP_128BYTE: - divider=7; - break; - default: - divider=8; - break; + //transfer how many data each time + case MODE_NUMONYX_PCM: + case PP_32BYTE: + divider = 9; + break; + case PP_128BYTE: + divider = 7; + break; + default: + divider = 8; + break; } if ((AddrRange->end / 0x1000000) > (AddrRange->start / 0x1000000)) //(AddrRange.end>0x1000000 && AddrRange.start<0x1000000) @@ -2093,50 +2106,50 @@ int SerialFlash_bulkPipeProgram(struct CAddressRange *AddrRange, unsigned char * { struct CAddressRange down_range; struct CAddressRange range_temp; - range_temp.start= AddrRange->start&0xFF000000; - range_temp.end= AddrRange->end + ((AddrRange->end % 0x1000000)? (0x1000000-(AddrRange->end % 0x1000000)):0); + range_temp.start = AddrRange->start & 0xFF000000; + range_temp.end = AddrRange->end + ((AddrRange->end % 0x1000000) ? (0x1000000 - (AddrRange->end % 0x1000000)) : 0); down_range.start = AddrRange->start; down_range.end = AddrRange->end; size_t packageNum; - size_t loop=(range_temp.end-range_temp.start)/0x1000000; + size_t loop = (range_temp.end - range_temp.start) / 0x1000000; // printf("loop=%d \n",loop); -// printf("range_temp.end=%x,range_temp.start=%x\n\r",range_temp.end,range_temp.start); + // printf("range_temp.end=%x,range_temp.start=%x\n\r",range_temp.end,range_temp.start); for (j = 0; j < loop; j++) { - if(j==(loop-1)) - down_range.end=AddrRange->end; + if (j == (loop - 1)) + down_range.end = AddrRange->end; else - down_range.end=(AddrRange->start&0xFF000000)+(0x1000000*(j+1)); + down_range.end = (AddrRange->start & 0xFF000000) + (0x1000000 * (j + 1)); - if(j==0) - down_range.start=AddrRange->start; + if (j == 0) + down_range.start = AddrRange->start; else - down_range.start=(AddrRange->start&0xFF000000)+(0x1000000*j); + down_range.start = (AddrRange->start & 0xFF000000) + (0x1000000 * j); - down_range.length=down_range.end-down_range.start; - packageNum = down_range.length >> divider ; + down_range.length = down_range.end - down_range.start; + packageNum = down_range.length >> divider; // 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); + FlashCommand_SendCommand_SetupPacketForBulkWrite(&down_range, modeWrite, WriteCom, Index); 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); + size_t packageNum = (AddrRange->end - AddrRange->start) >> divider; + FlashCommand_SendCommand_SetupPacketForBulkWrite(AddrRange, modeWrite, WriteCom, Index); for (i = 0; i < packageNum; ++i) { - BulkPipeWrite((unsigned char *)((itr_begin + (i << divider))), 1<length = AddrRange->end - AddrRange->start; if (AddrRange->length <= 0) { return false; } // 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) + if ((AddrRange->end / 0x1000000) > (AddrRange->start / 0x1000000)) //(AddrRange.end>0x1000000 && AddrRange.start<0x1000000) { struct CAddressRange read_range; struct CAddressRange range_temp; - range_temp.start= AddrRange->start&0xFF000000; - range_temp.end= AddrRange->end + ((AddrRange->end % 0x1000000)? (0x1000000-(AddrRange->end % 0x1000000)):0); + range_temp.start = AddrRange->start & 0xFF000000; + range_temp.end = AddrRange->end + ((AddrRange->end % 0x1000000) ? (0x1000000 - (AddrRange->end % 0x1000000)) : 0); - read_range.start=AddrRange->start; - read_range.end=AddrRange->end; - loop=(range_temp.end-range_temp.start)/0x1000000; + read_range.start = AddrRange->start; + read_range.end = AddrRange->end; + loop = (range_temp.end - range_temp.start) / 0x1000000; for (j = 0; j < loop; j++) { 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)) // for sf100 - { - unsigned char re=0; - int numOfRetry = 5 ; - do{ - TurnOFFVcc(Index); - Sleep(100); - TurnONVcc(Index); - SerialFlash_waitForWEL(Index); - unsigned char v[2]={0xc5,j}; - FlashCommand_TransceiveOut(v,2,false,Index); - Sleep(100); - CN25Qxxx_MutipleDIe_LargeRDEAR(&re,Index); - }while(((re&j)!=j)&& numOfRetry-- > 0); - if(numOfRetry==0) - return false; - } - - if(j==(loop-1)) - read_range.end=AddrRange->end; + { + unsigned char re = 0; + int numOfRetry = 5; + do { + TurnOFFVcc(Index); + Sleep(100); + TurnONVcc(Index); + SerialFlash_waitForWEL(Index); + unsigned char v[2] = { 0xc5, j }; + FlashCommand_TransceiveOut(v, 2, false, Index); + Sleep(100); + CN25Qxxx_MutipleDIe_LargeRDEAR(&re, Index); + } while (((re & j) != j) && numOfRetry-- > 0); + if (numOfRetry == 0) + return false; + } + + if (j == (loop - 1)) + read_range.end = AddrRange->end; else - read_range.end=(AddrRange->start&0xFF000000)+(0x1000000*(j+1)); + read_range.end = (AddrRange->start & 0xFF000000) + (0x1000000 * (j + 1)); - if(j==0) - read_range.start=AddrRange->start; + if (j == 0) + read_range.start = AddrRange->start; else - read_range.start=((AddrRange->start&0xFF000000)+(0x1000000*j)); + read_range.start = ((AddrRange->start & 0xFF000000) + (0x1000000 * j)); - read_range.length=read_range.end-read_range.start; + read_range.length = read_range.end - read_range.start; - pageNum = read_range.length >> 9 ; - FlashCommand_SendCommand_SetupPacketForBulkRead(&read_range, modeRead,ReadCom,Index); + pageNum = read_range.length >> 9; + FlashCommand_SendCommand_SetupPacketForBulkRead(&read_range, modeRead, ReadCom, Index); for (i = 0; i < pageNum; ++i) { - ret = BulkPipeRead(vData + (BufferLocation+i)*512, USB_TIMEOUT,Index); + ret = BulkPipeRead(vData + (BufferLocation + i) * 512, USB_TIMEOUT, Index); if ((ret != 512) || m_isCanceled) return 0; //memcpy(vData + (BufferLocation+i)*512, v, 512); @@ -2221,98 +2234,98 @@ int SerialFlash_bulkPipeRead(struct CAddressRange *AddrRange, unsigned char *vDa BufferLocation += pageNum; } } else { - unsigned char EAR=(AddrRange->start*0x1000000)>>24; + 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)) { - unsigned char re=0; - int numOfRetry = 5 ; - do{ - TurnOFFVcc(Index); - Sleep(100); - TurnONVcc(Index); - SerialFlash_waitForWEL(Index); - unsigned char v[2]={0xc5,EAR}; - FlashCommand_TransceiveOut(v,2,false,Index); - Sleep(100); - CN25Qxxx_MutipleDIe_LargeRDEAR(&re,Index); - }while(((re&EAR)!=EAR)&& numOfRetry-- > 0); - if(numOfRetry==0) - return false; - } - pageNum = AddrRange->length >> 9 ; - FlashCommand_SendCommand_SetupPacketForBulkRead(AddrRange, modeRead,ReadCom,Index); + unsigned char re = 0; + int numOfRetry = 5; + do { + TurnOFFVcc(Index); + Sleep(100); + TurnONVcc(Index); + SerialFlash_waitForWEL(Index); + unsigned char v[2] = { 0xc5, EAR }; + FlashCommand_TransceiveOut(v, 2, false, Index); + Sleep(100); + CN25Qxxx_MutipleDIe_LargeRDEAR(&re, Index); + } while (((re & EAR) != EAR) && numOfRetry-- > 0); + if (numOfRetry == 0) + return false; + } + pageNum = AddrRange->length >> 9; + FlashCommand_SendCommand_SetupPacketForBulkRead(AddrRange, modeRead, ReadCom, Index); for (i = 0; i < pageNum; ++i) { - ret = BulkPipeRead(vData + i*ret, USB_TIMEOUT,Index); + ret = BulkPipeRead(vData + i * ret, USB_TIMEOUT, Index); if ((ret != 512) || m_isCanceled) { - return false ; + return false; } //memcpy(vData + i*ret, v, ret); } } - if(SerialFlash_EnableQuadIO(false,m_boEnReadQuadIO,Index) == SerialFlash_FALSE) + if (SerialFlash_EnableQuadIO(false, m_boEnReadQuadIO, Index) == SerialFlash_FALSE) return false; SerialFlash_Enable4ByteAddrMode(false, Index); - if(!SerialFlash_EndofOperation(Index)) - return false; + if (!SerialFlash_EndofOperation(Index)) + return false; - return true ; + return true; } void SerialFlash_SetCancelOperationFlag() { - m_isCanceled = SerialFlash_TRUE; + m_isCanceled = SerialFlash_TRUE; } void SerialFlash_ClearCancelOperationFlag() { - m_isCanceled = SerialFlash_FALSE; + m_isCanceled = SerialFlash_FALSE; } -int SerialFlash_readSR(unsigned char *cSR,int Index) +int SerialFlash_readSR(unsigned char* cSR, int Index) { - return SerialFlash_doRDSR(cSR,Index); + return SerialFlash_doRDSR(cSR, Index); } -int SerialFlash_writeSR(unsigned char cSR,int Index) +int SerialFlash_writeSR(unsigned char cSR, int Index) { - SerialFlash_doWREN(Index); - return SerialFlash_doWRSR(cSR,Index); + SerialFlash_doWREN(Index); + return SerialFlash_doWRSR(cSR, Index); } int SerialFlash_is_protectbits_set(int Index) { - unsigned char sr; - SerialFlash_doRDSR(&sr,Index) ; + unsigned char sr; + SerialFlash_doRDSR(&sr, Index); - return ((0 != (sr & 0x9C) )? 1: 0); + return ((0 != (sr & 0x9C)) ? 1 : 0); } 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(CN25Qxxx_Large_doWRVCR(0xFB,Index)==false) - return false; - if(CN25Qxxx_Large_doWRENVCR(0xFF,Index)==false) - return false; + if (CN25Qxxx_Large_doWRVCR(0xFB, Index) == false) + return false; + if (CN25Qxxx_Large_doWRENVCR(0xFF, Index) == false) + return false; - return true; + return true; } 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 (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(CN25Qxxx_Large_doWRVCR(0xFB,Index)==false) - return false; - if(CN25Qxxx_Large_doWRENVCR(0xF7,Index)==false) - return false; - } - } + 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)) { - MX25Lxxx_Large_doWRCR(0x70,Index); - } + MX25Lxxx_Large_doWRCR(0x70, Index); + } } return true; diff --git a/SerialFlash.h b/SerialFlash.h index db4dece..88fb7b1 100644 --- a/SerialFlash.h +++ b/SerialFlash.h @@ -6,94 +6,94 @@ #include "Macro.h" #include -#define SerialFlash_FALSE -1 -#define SerialFlash_TRUE 1 - - enum //list of all chip-specific instruction, for ST serial flash - { - WREN = 0x06, // Write Enable - WRDI = 0x04, // Write Disable - RDIDJ = 0x9F, //RDIDJ // Read Jedec ID , except 80 - RDSR = 0x05, // Read Status Register - WRSR = 0x01, // Write Status Register - READ = 0x03, // Byte Read - FREAD = 0x0B, // Fast Read - PP = 0x02, // Page Program - SE = 0xD8, // Sector Erase - CHIP_ERASE = 0xC7, //CHIP_ERASE // Bulk (or Chip) Erase - DP = 0xB9, // Deep Power Down - RDP = 0xAB, //RES // Release Deep Power Down - RES = 0xAB, //RES // RDP and read signature - RDSCUR = 0x2B, - GBULK = 0x98, - EN4B = 0xB7, - EXIT4B = 0xE9, - }; - -int SerialFlash_doWRSR(unsigned char cSR,int Index); - -int SerialFlash_doRDSR(unsigned char *cSR,int Index); - -void S70FSxxx_Large_waitForWEL(bool die1,int Index); -int S70FSxxx_Large_doRDSR1V(bool die1, unsigned char *cSR,int Index); -int S70FSxxx_Large_doRDCR2V(bool die1, unsigned char *cSR,int Index); +#define SerialFlash_FALSE -1 +#define SerialFlash_TRUE 1 + +enum //list of all chip-specific instruction, for ST serial flash +{ + WREN = 0x06, // Write Enable + WRDI = 0x04, // Write Disable + RDIDJ = 0x9F, //RDIDJ // Read Jedec ID , except 80 + RDSR = 0x05, // Read Status Register + WRSR = 0x01, // Write Status Register + READ = 0x03, // Byte Read + FREAD = 0x0B, // Fast Read + PP = 0x02, // Page Program + SE = 0xD8, // Sector Erase + CHIP_ERASE = 0xC7, //CHIP_ERASE // Bulk (or Chip) Erase + DP = 0xB9, // Deep Power Down + RDP = 0xAB, //RES // Release Deep Power Down + RES = 0xAB, //RES // RDP and read signature + RDSCUR = 0x2B, + GBULK = 0x98, + EN4B = 0xB7, + EXIT4B = 0xE9, +}; + +int SerialFlash_doWRSR(unsigned char cSR, int Index); + +int SerialFlash_doRDSR(unsigned char* cSR, int Index); + +void S70FSxxx_Large_waitForWEL(bool die1, int Index); +int S70FSxxx_Large_doRDSR1V(bool die1, unsigned char* cSR, int Index); +int S70FSxxx_Large_doRDCR2V(bool die1, unsigned char* cSR, int Index); void SerialFlash_waitForWEL(int Index); -bool S70FSxxx_Large_waitForWIP(bool die1,int Index); +bool S70FSxxx_Large_waitForWIP(bool die1, int Index); bool SerialFlash_waitForWIP(int Index); int SerialFlash_doWREN(int Index); int SerialFlash_doWRDI(int Index); -int SerialFlash_protectBlock(int bProtect,int Index); +int SerialFlash_protectBlock(int bProtect, int Index); -int SerialFlash_EnableQuadIO(int bEnable,int boRW,int Index); +int SerialFlash_EnableQuadIO(int bEnable, int boRW, int Index); -int SerialFlash_Enable4ByteAddrMode(int bEnable,int Index); +int SerialFlash_Enable4ByteAddrMode(int bEnable, int Index); -int SerialFlash_rangeBlankCheck(struct CAddressRange *Range,int Index); +int SerialFlash_rangeBlankCheck(struct CAddressRange* Range, int Index); -int SerialFlash_rangeProgram(struct CAddressRange *AddrRange, unsigned char *vData,int Index); +int SerialFlash_rangeProgram(struct CAddressRange* AddrRange, unsigned char* vData, int Index); -int SerialFlash_rangeRead(struct CAddressRange *AddrRange, unsigned char *vData,int Index); +int SerialFlash_rangeRead(struct CAddressRange* AddrRange, unsigned char* vData, int Index); int SerialFlash_DoPolling(int Index); int SerialFlash_is_good(); -int SerialFlash_batchErase(uintptr_t* vAddrs,size_t AddrSize,int Index); +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_rangeErase(unsigned char cmd, size_t sectionSize, struct CAddressRange* AddrRange, 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_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); +int SerialFlash_bulkPipeRead(struct CAddressRange* AddrRange, unsigned char* vData, unsigned char modeRead, unsigned char ReadCom, int Index); void SerialFlash_SetCancelOperationFlag(); void SerialFlash_ClearCancelOperationFlag(); -int SerialFlash_readSR(unsigned char *cSR,int Index); +int SerialFlash_readSR(unsigned char* cSR, int Index); -int SerialFlash_writeSR(unsigned char cSR,int Index); +int SerialFlash_writeSR(unsigned char cSR, int Index); int SerialFlash_is_protectbits_set(int Index); -bool SST25xFxxA_protectBlock(int bProtect,int Index); -bool SST25xFxx_protectBlock(int bProtect,int Index); -bool AT25FSxxx_protectBlock(int bProtect,int Index); -bool CEN25QHxx_LargeEnable4ByteAddrMode(bool Enable4Byte,int Index); -bool CN25Qxxx_LargeRDFSR(unsigned char *cSR, int Index); -bool CN25Qxxx_LargeEnable4ByteAddrMode(bool Enable4Byte,int Index); +bool SST25xFxxA_protectBlock(int bProtect, int Index); +bool SST25xFxx_protectBlock(int bProtect, int Index); +bool AT25FSxxx_protectBlock(int bProtect, int Index); +bool CEN25QHxx_LargeEnable4ByteAddrMode(bool Enable4Byte, int Index); +bool CN25Qxxx_LargeRDFSR(unsigned char* cSR, int Index); +bool CN25Qxxx_LargeEnable4ByteAddrMode(bool Enable4Byte, int Index); bool CN25Qxxx_MutipleDIe_LargeWREAR(unsigned char cSR, int Index); -bool CN25Qxxx_MutipleDIe_LargeRDEAR(unsigned char *cSR, int Index); -bool CN25Qxxx_Large_doRDVCR(unsigned char *ucVCR,int Index); -bool CN25Qxxx_Large_doWRVCR(unsigned char ucVCR,int Index); -bool CN25Qxxx_Large_doRDENVCR(unsigned char *ucENVCR,int Index); -bool CN25Qxxx_Large_doWRENVCR(unsigned char ucENVCR,int Index); -bool CS25FLxx_LargeEnable4ByteAddrMode(bool Enable4Byte,int Index); +bool CN25Qxxx_MutipleDIe_LargeRDEAR(unsigned char* cSR, int Index); +bool CN25Qxxx_Large_doRDVCR(unsigned char* ucVCR, int Index); +bool CN25Qxxx_Large_doWRVCR(unsigned char ucVCR, int Index); +bool CN25Qxxx_Large_doRDENVCR(unsigned char* ucENVCR, int Index); +bool CN25Qxxx_Large_doWRENVCR(unsigned char ucENVCR, int Index); +bool CS25FLxx_LargeEnable4ByteAddrMode(bool Enable4Byte, int Index); size_t GetChipSize(void); size_t GetPageSize(void); bool SerialFlash_StartofOperation(int Index); diff --git a/board.h b/board.h index 34eecbc..9176788 100644 --- a/board.h +++ b/board.h @@ -10,29 +10,29 @@ void SendFFSequence(int Index); void QueryBoard(int Index); unsigned char GetFPGAVersion(int Index); bool SetIO(unsigned char ioState, int Index); -bool SetTargetFlash(unsigned char StartupMode,int Index); -bool SetLEDProgBoard(size_t Clolor,int Index); -bool SetGreenLEDOn(bool boOn,int Index); +bool SetTargetFlash(unsigned char StartupMode, int Index); +bool SetLEDProgBoard(size_t Clolor, int Index); +bool SetGreenLEDOn(bool boOn, int Index); bool SetOrangeLEDOn(bool boOn, int Index); bool SetRedLEDOn(bool boOn, int Index); -bool SetLEDOnOff(size_t Color,int Index); -bool SetCS(size_t value,int Index); -bool SetIOModeToSF600(size_t value,int Index); -bool BlinkProgBoard(bool boIsV5,int Index); -bool LeaveSF600Standalone(bool Enable,int Index); -bool SetSPIClockValue(unsigned short v,int Index); +bool SetLEDOnOff(size_t Color, int Index); +bool SetCS(size_t value, int Index); +bool SetIOModeToSF600(size_t value, int Index); +bool BlinkProgBoard(bool boIsV5, int Index); +bool LeaveSF600Standalone(bool Enable, int Index); +bool SetSPIClockValue(unsigned short v, int Index); unsigned int ReadUID(int Index); bool SetSPIClockDefault(int Index); -bool EraseST7Sectors(bool bSect1,int Index); -bool ProgramSectors(const char* sFilePath, bool bSect1,int Index); +bool EraseST7Sectors(bool bSect1, int Index); +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); +bool WriteUID(unsigned int dwUID, int Index); +bool WriteManufacturerID(unsigned char ManuID, 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); +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 bbba2f7..a1ef1bc 100644 --- a/dpcmd.c +++ b/dpcmd.c @@ -8,14 +8,14 @@ #include #include #include -#include #include +#include #include #include #include #include -#define min(a,b) (a>b? b:a) +#define min(a, b) (a > b ? b : a) extern unsigned char* pBufferForLastReadData[16]; extern unsigned char* pBufferforLoadedFile; @@ -24,145 +24,143 @@ extern unsigned long g_ulFileSize; extern volatile bool g_bIsSF600[16]; extern unsigned char g_BatchIndex; -unsigned int g_Vcc=vcc3_5V; -unsigned int g_Vpp=9; -unsigned int g_uiAddr=0; -size_t g_uiLen=0; -unsigned int g_ucFill=0xFF; -unsigned int g_ucTarget=1; -unsigned int g_uiTimeout=1800; -unsigned int g_ucSPIClock=clk_12M; -unsigned int g_uiBlink=0; -unsigned int g_uiDevNum=0; -unsigned int g_uiDeviceID=0; -unsigned int g_IO1Select=0; -unsigned int g_IO4Select=1; - -bool g_bEnableVpp=false; -int g_StartupMode=STARTUP_APPLI_SF_1; -bool g_bStatus=true; +unsigned int g_Vcc = vcc3_5V; +unsigned int g_Vpp = 9; +unsigned int g_uiAddr = 0; +size_t g_uiLen = 0; +unsigned int g_ucFill = 0xFF; +unsigned int g_ucTarget = 1; +unsigned int g_uiTimeout = 1800; +unsigned int g_ucSPIClock = clk_12M; +unsigned int g_uiBlink = 0; +unsigned int g_uiDevNum = 0; +unsigned int g_uiDeviceID = 0; +unsigned int g_IO1Select = 0; +unsigned int g_IO4Select = 1; + +bool g_bEnableVpp = false; +int g_StartupMode = STARTUP_APPLI_SF_1; +bool g_bStatus = true; CHIP_INFO Chip_Info; -char *l_opt_arg; +char* l_opt_arg; struct CAddressRange DownloadAddrRange; struct CAddressRange UploadAddrRange; struct CAddressRange LockAddrrange; -unsigned int g_uiLockLen=0; -unsigned int g_uiLockStart=0; - -char* g_parameter_read="\0"; -char* g_parameter_program="\0"; -char* g_parameter_loadfile="\0"; -char* g_parameter_auto="\0"; -char* g_parameter_batch="\0"; -char* g_parameter_fsum="\0"; -char* g_parameter_raw="\0"; -char* g_parameter_raw_return="\0"; -char* g_parameter_addr="0"; -char* g_parameter_length="\0"; -char* g_parameter_fill="FF"; -char* g_parameter_type="\0"; -char* g_parameter_timeout="300"; -char* g_parameter_target="1"; -char* g_parameter_vcc="NO"; +unsigned int g_uiLockLen = 0; +unsigned int g_uiLockStart = 0; + +char* g_parameter_read = "\0"; +char* g_parameter_program = "\0"; +char* g_parameter_loadfile = "\0"; +char* g_parameter_auto = "\0"; +char* g_parameter_batch = "\0"; +char* g_parameter_fsum = "\0"; +char* g_parameter_raw = "\0"; +char* g_parameter_raw_return = "\0"; +char* g_parameter_addr = "0"; +char* g_parameter_length = "\0"; +char* g_parameter_fill = "FF"; +char* g_parameter_type = "\0"; +char* g_parameter_timeout = "300"; +char* g_parameter_target = "1"; +char* g_parameter_vcc = "NO"; //char* g_parameter_spi_clk="2"; -char* g_parameter_blink="0"; -char* g_parameter_fw="\0"; -char g_LogPath[512]={0}; +char* g_parameter_blink = "0"; +char* g_parameter_fw = "\0"; +char g_LogPath[512] = { 0 }; unsigned long g_ucOperation; struct memory_id g_ChipID; char g_board_type[8]; int g_firmversion; -int g_CurrentSeriase=Seriase_25; +int g_CurrentSeriase = Seriase_25; int m_isCanceled = 0; int m_bProtectAfterWritenErase = 0; int m_boEnReadQuadIO = 0; int m_boEnWriteQuadIO = 0; -volatile bool g_is_operation_on_going=false; -bool g_is_operation_successful[16]={false}; -bool g_bDisplayTimer=true; -bool isSendFFsequence=false; +volatile bool g_is_operation_on_going = false; +bool g_is_operation_successful[16] = { false }; +bool g_bDisplayTimer = true; +bool isSendFFsequence = false; /* only print timing progress info if this many seconds ellapsed * before the last time we did */ static float g_tv_display_delta_s = 0.5; - //char* const short_options = "?Ldber:p:u:sf:I:R:a:l:vx:T:S:N:B:D:F:V:t:g:c:POik:"; -static const char* msg_err_communication = "Error: USB communication error or configuration file missing.\n"; +static const char* msg_err_communication = "Error: USB communication error or configuration file missing.\n"; //static const char* msg_err_openfile = "Error: Failed to open file.\n"; -static const char* msg_err_identifychip = "Error: chip not identified.\n"; -static const char* msg_err_timeout_abortion = "\nError: Abort abnormally due to timeout.\n*** You might need to un-plug the USB device to recover the state. ***\n"; +static const char* msg_err_identifychip = "Error: chip not identified.\n"; +static const char* msg_err_timeout_abortion = "\nError: Abort abnormally due to timeout.\n*** You might need to un-plug the USB device to recover the state. ***\n"; //static const char* msg_err_lengthoverflow_abortion= "\nError: Length parameter exceeding file size, operation aborted\n"; //static const char* msg_err_addroverflow_abortion = "\nError: Exceeding chip size, operation aborted\n"; //static const char* msg_warning_optionignore_al = "\nWarning: either --addr or --length cannot be used with --auto, will be ignored\n"; -static const char* msg_info_loading = "\nLoading file, please wait ..."; -static const char* msg_info_checking = "\nChecking, please wait ..."; -static const char* msg_info_erasing = "\nErasing, please wait ..."; -static const char* msg_info_programming = "\nProgramming, please wait ..."; -static const char* msg_info_reading = "\nReading, please wait ..."; -static const char* msg_info_auto = "\nAuto Sequences, please wait ..."; -static const char* msg_info_verifying = "\nVerifying, please wait ..."; +static const char* msg_info_loading = "\nLoading file, please wait ..."; +static const char* msg_info_checking = "\nChecking, please wait ..."; +static const char* msg_info_erasing = "\nErasing, please wait ..."; +static const char* msg_info_programming = "\nProgramming, please wait ..."; +static const char* msg_info_reading = "\nReading, please wait ..."; +static const char* msg_info_auto = "\nAuto Sequences, please wait ..."; +static const char* msg_info_verifying = "\nVerifying, please wait ..."; //static const char* msg_info_unknownoption= "\nError: illegal option."; -static const char* msg_info_chipblank = "\nThe chip is blank"; -static const char* msg_info_chipnotblank = "\nThe chip is NOT blank"; -static const char* msg_info_eraseOK = "\nErase OK"; -static const char* msg_info_erasefail = "\nError: Erase Failed"; -static const char* msg_info_progOK = "\nProgram OK"; -static const char* msg_info_progfail = "\nError: Program Failed"; -static const char* msg_info_readOK = "\nRead OK"; -static const char* msg_info_readfail = "\nRead Failed"; -static const char* msg_info_autoOK = "\nAutomatic program OK"; -static const char* msg_info_autofail = "\nError: Automatic program Failed"; -static const char* msg_info_verifyOK = "\nVerify OK"; -static const char* msg_info_verifyfail = "\nError: Verify Failed"; -static const char* msg_info_firmwareupdate = "\nUpdating firmware, please wait..."; -static const char* msg_info_firmwareupdateOK = "\nUpdate firmware OK"; -static const char* msg_info_firmwareupdatefail = "\nError: Update firmware Failed"; - +static const char* msg_info_chipblank = "\nThe chip is blank"; +static const char* msg_info_chipnotblank = "\nThe chip is NOT blank"; +static const char* msg_info_eraseOK = "\nErase OK"; +static const char* msg_info_erasefail = "\nError: Erase Failed"; +static const char* msg_info_progOK = "\nProgram OK"; +static const char* msg_info_progfail = "\nError: Program Failed"; +static const char* msg_info_readOK = "\nRead OK"; +static const char* msg_info_readfail = "\nRead Failed"; +static const char* msg_info_autoOK = "\nAutomatic program OK"; +static const char* msg_info_autofail = "\nError: Automatic program Failed"; +static const char* msg_info_verifyOK = "\nVerify OK"; +static const char* msg_info_verifyfail = "\nError: Verify Failed"; +static const char* msg_info_firmwareupdate = "\nUpdating firmware, please wait..."; +static const char* msg_info_firmwareupdateOK = "\nUpdate firmware OK"; +static const char* msg_info_firmwareupdatefail = "\nError: Update firmware Failed"; char* const short_options = "?Ldber:p:u:z:sf:I:R:a:l:vx:T:S:N:B:t:g:c:PO:ik:1:4:U:E:"; struct option long_options[] = { - { "help", 0, NULL, '?' }, - { "list", 0, NULL, 'L' }, - { "detect", 0, NULL, 'd' }, - { "blank", 0, NULL, 'b' }, - { "erase", 0, NULL, 'e' }, - { "read", 1, NULL, 'r' }, - { "prog", 1, NULL, 'p' }, - { "auto", 1, NULL, 'u' }, - { "batch", 1, NULL, 'z' }, - { "sum", 0, NULL, 's' }, - { "fsum", 1, NULL, 'f' }, - { "raw-instruction", 1, NULL, 'I' }, - { "raw-require-return", 1, NULL, 'R' }, - { "addr", 1, NULL, 'a' }, - { "length", 1, NULL, 'l' }, - { "verify", 0, NULL, 'v' }, - { "fill", 1, NULL, 'x' }, - { "type", 1, NULL, 'T' }, - { "lock-start", 1, NULL, 'S' }, - { "lock-length", 1, NULL, 'N' }, - { "blink", 1, NULL, 'B' }, - { "device", 1, NULL, 'D' }, -// { "fix-device", 1, NULL, 'F' }, - { "list-device-id", 1, NULL, 'V' }, - { "load-file", 1, NULL, 'F' }, - { "timeout", 1, NULL, 't' }, - { "target", 1, NULL, 'g' }, - { "vcc", 1, NULL, 'c' }, - { "vpp", 0, NULL, 'P' }, - { "log", 1, NULL, 'O' }, - { "silent", 0, NULL, 'i' }, - { "spi-clk", 1, NULL, 'k' }, - { "set-io1", 1, NULL, '1' }, - { "set-io4", 1, NULL, '4' }, - { "update-fw", 1, NULL, 'U' }, - { "display-delta", 1, NULL, 'E' }, - { 0, 0, 0, 0}, + { "help", 0, NULL, '?' }, + { "list", 0, NULL, 'L' }, + { "detect", 0, NULL, 'd' }, + { "blank", 0, NULL, 'b' }, + { "erase", 0, NULL, 'e' }, + { "read", 1, NULL, 'r' }, + { "prog", 1, NULL, 'p' }, + { "auto", 1, NULL, 'u' }, + { "batch", 1, NULL, 'z' }, + { "sum", 0, NULL, 's' }, + { "fsum", 1, NULL, 'f' }, + { "raw-instruction", 1, NULL, 'I' }, + { "raw-require-return", 1, NULL, 'R' }, + { "addr", 1, NULL, 'a' }, + { "length", 1, NULL, 'l' }, + { "verify", 0, NULL, 'v' }, + { "fill", 1, NULL, 'x' }, + { "type", 1, NULL, 'T' }, + { "lock-start", 1, NULL, 'S' }, + { "lock-length", 1, NULL, 'N' }, + { "blink", 1, NULL, 'B' }, + { "device", 1, NULL, 'D' }, + // { "fix-device", 1, NULL, 'F' }, + { "list-device-id", 1, NULL, 'V' }, + { "load-file", 1, NULL, 'F' }, + { "timeout", 1, NULL, 't' }, + { "target", 1, NULL, 'g' }, + { "vcc", 1, NULL, 'c' }, + { "vpp", 0, NULL, 'P' }, + { "log", 1, NULL, 'O' }, + { "silent", 0, NULL, 'i' }, + { "spi-clk", 1, NULL, 'k' }, + { "set-io1", 1, NULL, '1' }, + { "set-io4", 1, NULL, '4' }, + { "update-fw", 1, NULL, 'U' }, + { "display-delta", 1, NULL, 'E' }, + { 0, 0, 0, 0 }, }; int OpenUSB(void); @@ -319,460 +317,460 @@ int Check(int argc, char *argv[]) int Sequence() { // *** the calling order in the following block must be kept as is *** - bool boResult=true; + bool boResult = true; boResult &= BlankCheck(); if (boResult == false) { - if(!(g_ucOperation&ERASE)) + if (!(g_ucOperation & ERASE)) return EXCODE_FAIL_BLANK; } if ((!((g_ucOperation & BLANK) && (boResult == true)) || (!(g_ucOperation & BLANK)))) { boResult = Erase(); - if(boResult==false) + if (boResult == false) return EXCODE_FAIL_ERASE; } boResult &= Program(); - if(boResult==false) + if (boResult == false) return EXCODE_FAIL_PROG; boResult &= Read(); - if(boResult==false) + if (boResult == false) return EXCODE_FAIL_READ; boResult &= Auto(); - if(boResult==false) + if (boResult == false) return EXCODE_FAIL_BATCH; boResult &= CalChecksum(); if (boResult == true) { if (g_ucOperation & CSUM) { if (g_uiLen == 0) { - if(g_uiDevNum==0) + if (g_uiDevNum == 0) return CRC32(pBufferForLastReadData[0], Chip_Info.ChipSizeInByte); - else - return CRC32(pBufferForLastReadData[g_uiDevNum-1], Chip_Info.ChipSizeInByte); + else + return CRC32(pBufferForLastReadData[g_uiDevNum - 1], Chip_Info.ChipSizeInByte); } else { - if(g_uiDevNum==0) + if (g_uiDevNum == 0) return CRC32(pBufferForLastReadData[0], g_uiLen); - else - return CRC32(pBufferForLastReadData[g_uiDevNum-1], g_uiLen); - } + else + return CRC32(pBufferForLastReadData[g_uiDevNum - 1], g_uiLen); + } } } else return EXCODE_FAIL_CHKSUM; boResult &= Verify(); - if(boResult==false) + if (boResult == false) return EXCODE_FAIL_VERIFY; return EXCODE_PASS; } -void print_message_function( void *ptr ) +void print_message_function(void* ptr) { - char *message; - message = (char *) ptr; - printf("%s \n", message); + char* message; + message = (char*)ptr; + printf("%s \n", message); } void GetLogPath(char* pBuf) { - memset(pBuf,0,512); + memset(pBuf, 0, 512); if (readlink("/proc/self/exe", pBuf, 512) != -1) { - if(strlen(pBuf)<(511-sizeof("/log.txt"))) - strcat(pBuf,"/log.txt"); + if (strlen(pBuf) < (511 - sizeof("/log.txt"))) + strcat(pBuf, "/log.txt"); // printf("%s\n",pBuf); } } void EnterStandaloneMode(int Index) { - if(g_bIsSF600[Index]==true) - LeaveSF600Standalone(false,Index); + if (g_bIsSF600[Index] == true) + LeaveSF600Standalone(false, Index); } void LeaveStandaloneMode(int Index) { - if(g_bIsSF600[Index]==true) - LeaveSF600Standalone(true,Index); + if (g_bIsSF600[Index] == true) + LeaveSF600Standalone(true, Index); } void WriteLog(int ErrorCode, bool Init) { - FILE *FileOut=NULL ; /* output files */ - time_t rawtime; - struct tm * timeinfo; - char buffer [512]={0}; - char* Result[]={ - " PASS\n", - " FAIL\n", - " Erase Fail\n", - " Program Fail\n", - " Verify Fail\n", - " Read Fail\n", - " Blank Check Fail\n", - " Auto Batch Fail\n", - " Checksum Fail\n", - " Identify Fail\n", - " Unknow Fail\n" - }; - - time (&rawtime); - timeinfo = localtime (&rawtime); - - strftime (buffer,80,"%Y-%b-%d %T",timeinfo); + FILE* FileOut = NULL; /* output files */ + time_t rawtime; + struct tm* timeinfo; + char buffer[512] = { 0 }; + char* Result[] = { + " PASS\n", + " FAIL\n", + " Erase Fail\n", + " Program Fail\n", + " Verify Fail\n", + " Read Fail\n", + " Blank Check Fail\n", + " Auto Batch Fail\n", + " Checksum Fail\n", + " Identify Fail\n", + " Unknow Fail\n" + }; + + time(&rawtime); + timeinfo = localtime(&rawtime); + + strftime(buffer, 80, "%Y-%b-%d %T", timeinfo); if (Init) { - FileOut = fopen(g_LogPath,"wt") ; - if(FileOut != NULL) - fprintf(FileOut, "%s%s%s",buffer," USB communication =",Result[ErrorCode]); + FileOut = fopen(g_LogPath, "wt"); + if (FileOut != NULL) + fprintf(FileOut, "%s%s%s", buffer, " USB communication =", Result[ErrorCode]); } else { - FileOut = fopen(g_LogPath,"a+t") ; - if(FileOut != NULL) - fprintf(FileOut, "%s%s",buffer,Result[ErrorCode]); - } + FileOut = fopen(g_LogPath, "a+t"); + if (FileOut != NULL) + fprintf(FileOut, "%s%s", buffer, Result[ErrorCode]); + } - if(FileOut != NULL) - fclose(FileOut); + if (FileOut != NULL) + fclose(FileOut); } int GetConfigVer() { - char file_line_buf[512]; - char test[80]; - char *pch,*tok; - bool bFound=0; - int Ver=0; - FILE* fp; /*Declare file pointer variable*/ + char file_line_buf[512]; + char test[80]; + char *pch, *tok; + bool bFound = 0; + int Ver = 0; + FILE* fp; /*Declare file pointer variable*/ if ((fp = openChipInfoDb()) == NULL) - return 1; + return 1; while (fgets(file_line_buf, 512, fp) != NULL) { - if(strstr(file_line_buf,"DediProgChipDatabase") && bFound==0) - bFound=1; + if (strstr(file_line_buf, "DediProgChipDatabase") && bFound == 0) + bFound = 1; - pch=strstr(file_line_buf, "Ver"); + pch = strstr(file_line_buf, "Ver"); if (bFound == 1 && pch != NULL) { - memset(test, '\0', 80); - strcpy(test,pch+strlen("Ver")); - tok = strtok(test,"\"= \t"); - Ver = strtol(tok,NULL,10); - break; - } - } - fclose(fp); - return Ver; + memset(test, '\0', 80); + strcpy(test, pch + strlen("Ver")); + tok = strtok(test, "\"= \t"); + Ver = strtol(tok, NULL, 10); + break; + } + } + fclose(fp); + return Ver; } -int main(int argc, char *argv[]) +int main(int argc, char* argv[]) { - #if 0 //Check all USB device capability +#if 0 //Check all USB device capability Check(argc,argv); return; - #endif - int c; - int iExitCode=EXCODE_PASS; - bool bDetect=false; - bool bDevice=false; +#endif + int c; + int iExitCode = EXCODE_PASS; + bool bDetect = false; + bool bDevice = false; 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); + g_ucOperation = 0; + GetLogPath(g_LogPath); if (argc == 1) { - cli_classic_usage(false); - return 0; - } + cli_classic_usage(false); + return 0; + } - if(OpenUSB()==0) - iExitCode=EXCODE_FAIL_USB; + if (OpenUSB() == 0) + iExitCode = EXCODE_FAIL_USB; - LeaveStandaloneMode(0); - QueryBoard(0); + LeaveStandaloneMode(0); + QueryBoard(0); while ((c = getopt_long(argc, argv, short_options, long_options, NULL)) != -1) { switch (c) { - case '?': - cli_classic_usage(true); - CloseProject(); - ExitProgram(); - return EXCODE_PASS; - case 'L': - g_ucOperation |= LIST_TYPE; - break; - case 'd': - bDetect=true; - break; - case 'b': - g_ucOperation |= BLANK; - break; - case 'e': - g_ucOperation |= ERASE; - break; - case 'r': - g_parameter_read = optarg; - g_ucOperation |= READ_TO_FILE; - break; - case 'p': - g_parameter_program= optarg; - g_ucOperation |= PROGRAM; - break; - case 'u': - g_parameter_auto = optarg; - g_ucOperation |= BATCH; - g_BatchIndex=2; - break; - case 'z': - g_parameter_batch = optarg; - g_ucOperation |= BATCH; - g_BatchIndex=1; - break; - case 's': // display chip checksum - g_ucOperation |= CSUM; - break; - case 'f': //display file checksum - g_parameter_fsum = optarg; - g_ucOperation |= FSUM; - break; - case 'I': - g_parameter_raw = optarg; - break; - case 'R': //raw-require-return - g_parameter_raw_return = optarg; - break; - /* Optional Switches that add fine-tune ability to Basic Switches: */ - case 'a': - g_parameter_addr = optarg; - g_uiAddr=0; - sscanf(optarg,"%x",&g_uiAddr); - break; - case 'l': - g_uiLen=0; + case '?': + cli_classic_usage(true); + CloseProject(); + ExitProgram(); + return EXCODE_PASS; + case 'L': + g_ucOperation |= LIST_TYPE; + break; + case 'd': + bDetect = true; + break; + case 'b': + g_ucOperation |= BLANK; + break; + case 'e': + g_ucOperation |= ERASE; + break; + case 'r': + g_parameter_read = optarg; + g_ucOperation |= READ_TO_FILE; + break; + case 'p': + g_parameter_program = optarg; + g_ucOperation |= PROGRAM; + break; + case 'u': + g_parameter_auto = optarg; + g_ucOperation |= BATCH; + g_BatchIndex = 2; + break; + case 'z': + g_parameter_batch = optarg; + g_ucOperation |= BATCH; + g_BatchIndex = 1; + break; + case 's': // display chip checksum + g_ucOperation |= CSUM; + break; + case 'f': //display file checksum + g_parameter_fsum = optarg; + g_ucOperation |= FSUM; + break; + case 'I': + g_parameter_raw = optarg; + break; + case 'R': //raw-require-return + g_parameter_raw_return = optarg; + break; + /* Optional Switches that add fine-tune ability to Basic Switches: */ + case 'a': + g_parameter_addr = optarg; + g_uiAddr = 0; + sscanf(optarg, "%x", &g_uiAddr); + break; + case 'l': + g_uiLen = 0; if (strstr(optarg, "0x")) - sscanf(optarg,"%zx",&g_uiLen); + sscanf(optarg, "%zx", &g_uiLen); else sscanf(optarg, "%zu", &g_uiLen); - break; - case 'v': - g_ucOperation |= VERIFY; - break; - case 'x': - g_ucFill=0xFF; - sscanf(optarg,"%x",&g_ucFill); - break; - case 'T': - g_parameter_type= optarg; //type - break; - case 'S': //lock start - sscanf(optarg,"%x",&g_uiLockStart); - // l_opt_arg = optarg; -// printf("hexadecimal starting address (with arg: %s)\n", l_opt_arg); - break; - case 'N': //lock length - sscanf(optarg,"%x",&g_uiLockLen); -// l_opt_arg = optarg; - // printf("hexadecimal length of area that will be kept unchanged while updating (with arg: %s)\n", l_opt_arg); - break; - case 'B': - sscanf(optarg,"%d",&g_uiBlink); - g_ucOperation |= BLINK; - break; - case 'D': // device - //l_opt_arg = optarg; - //printf("activate only the programmer connected to USBx (with arg: %s)\n", l_opt_arg); - - bDevice = true; - sscanf(optarg,"%x",&g_uiDevNum); - //devpath = optarg; - break; - case 'F': - // l_opt_arg = optarg; - // printf("Fix programmer serial number with programmer sequence. (with arg: %s)\n", l_opt_arg); - // break; - g_parameter_loadfile= optarg; - g_ucOperation |= LOADFILE; - break; - case 'V': - sscanf(optarg,"%d",&g_uiDeviceID); - g_ucOperation |= DEVICE_ID; //list device id - break; - /* Miscellaneous options: */ - case 't': - l_opt_arg = optarg; - sscanf(l_opt_arg,"%d",&g_uiTimeout); - break; - case 'g': - g_parameter_target=optarg; - sscanf(optarg,"%d",&g_ucTarget); - break; - case 'c': - g_parameter_vcc=optarg; - sscanf(optarg,"%d",&g_Vcc); - break; - case 'P': - g_bEnableVpp=true; - break; - case 'O': //Log file - strcpy(g_LogPath,optarg); + break; + case 'v': + g_ucOperation |= VERIFY; + break; + case 'x': + g_ucFill = 0xFF; + sscanf(optarg, "%x", &g_ucFill); + break; + case 'T': + g_parameter_type = optarg; //type + break; + case 'S': //lock start + sscanf(optarg, "%x", &g_uiLockStart); + // l_opt_arg = optarg; + // printf("hexadecimal starting address (with arg: %s)\n", l_opt_arg); + break; + case 'N': //lock length + sscanf(optarg, "%x", &g_uiLockLen); + // l_opt_arg = optarg; + // printf("hexadecimal length of area that will be kept unchanged while updating (with arg: %s)\n", l_opt_arg); + break; + case 'B': + sscanf(optarg, "%d", &g_uiBlink); + g_ucOperation |= BLINK; + break; + case 'D': // device + //l_opt_arg = optarg; + //printf("activate only the programmer connected to USBx (with arg: %s)\n", l_opt_arg); + + bDevice = true; + sscanf(optarg, "%x", &g_uiDevNum); + //devpath = optarg; + break; + case 'F': + // l_opt_arg = optarg; + // printf("Fix programmer serial number with programmer sequence. (with arg: %s)\n", l_opt_arg); + // break; + g_parameter_loadfile = optarg; + g_ucOperation |= LOADFILE; + break; + case 'V': + sscanf(optarg, "%d", &g_uiDeviceID); + g_ucOperation |= DEVICE_ID; //list device id + break; + /* Miscellaneous options: */ + case 't': + l_opt_arg = optarg; + sscanf(l_opt_arg, "%d", &g_uiTimeout); + break; + case 'g': + g_parameter_target = optarg; + sscanf(optarg, "%d", &g_ucTarget); + break; + case 'c': + g_parameter_vcc = optarg; + sscanf(optarg, "%d", &g_Vcc); + break; + case 'P': + g_bEnableVpp = true; + break; + case 'O': //Log file + strcpy(g_LogPath, optarg); // printf("%s\n",g_LogPath); - break; - case 'i': - g_bDisplayTimer=false; - break; - case 'k': - l_opt_arg = optarg; - sscanf(l_opt_arg,"%d",&g_ucSPIClock); - break; - case '1': - l_opt_arg = optarg; - sscanf(l_opt_arg,"%d",&g_IO1Select); - break; - case '4': - l_opt_arg = optarg; - sscanf(l_opt_arg,"%d",&g_IO4Select); - break; - case 'U': - g_parameter_fw=optarg; - iExitCode=FirmwareUpdate(); - goto Exit; - break; - case 'E': - g_tv_display_delta_s = atof(optarg); - if (g_tv_display_delta_s <= 0) { - fprintf(stderr, "E: invalid number" - " of seconds for display delta" - " (%f); expect larger than" - " zero\n", - g_tv_display_delta_s); - return 1; - } - break; - default: - break; - } + break; + case 'i': + g_bDisplayTimer = false; + break; + case 'k': + l_opt_arg = optarg; + sscanf(l_opt_arg, "%d", &g_ucSPIClock); + break; + case '1': + l_opt_arg = optarg; + sscanf(l_opt_arg, "%d", &g_IO1Select); + break; + case '4': + l_opt_arg = optarg; + sscanf(l_opt_arg, "%d", &g_IO4Select); + break; + case 'U': + g_parameter_fw = optarg; + iExitCode = FirmwareUpdate(); + goto Exit; + break; + case 'E': + g_tv_display_delta_s = atof(optarg); + if (g_tv_display_delta_s <= 0) { + fprintf(stderr, "E: invalid number" + " of seconds for display delta" + " (%f); expect larger than" + " zero\n", + g_tv_display_delta_s); + return 1; + } + break; + default: + break; + } } - int dev_cnt=get_usb_dev_cnt(); + int dev_cnt = get_usb_dev_cnt(); if (bDetect == true) { if (bDevice == false) { for (int i = 0; i < dev_cnt; i++) { // //printf("%s\n",g_LogPath); - int dwUID=ReadUID(i); + int dwUID = ReadUID(i); 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); + } + WriteLog(iExitCode, true); + Chip_Info = GetFirstDetectionMatch(i); 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; - } - } + printf("%s", msg_err_identifychip); + iExitCode = EXCODE_FAIL_IDENTIFY; + } + } } else if (g_uiDevNum != 0) { - WriteLog(iExitCode, true); + WriteLog(iExitCode, true); printf("%d,\tdetecting chip\n", g_uiDevNum); - Chip_Info=GetFirstDetectionMatch(g_uiDevNum-1); + Chip_Info = GetFirstDetectionMatch(g_uiDevNum - 1); 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; - } + printf("%s", msg_err_identifychip); + iExitCode = EXCODE_FAIL_IDENTIFY; + } } else { - printf("The number of programmer is not defined!\n"); - iExitCode=EXCODE_FAIL_IDENTIFY; - } - goto Exit; + printf("The number of programmer is not defined!\n"); + iExitCode = EXCODE_FAIL_IDENTIFY; + } + goto Exit; } else - WriteLog(iExitCode, true); + WriteLog(iExitCode, true); if (iExitCode != EXCODE_PASS) goto Exit; - iExitCode=Handler(); + iExitCode = Handler(); Exit: - printf("\n\n"); - CloseProject(); - ExitProgram(); - WriteLog(iExitCode,false); - return iExitCode; + printf("\n\n"); + CloseProject(); + ExitProgram(); + WriteLog(iExitCode, false); + return iExitCode; } void ExitProgram(void) { for (int i = 0; i < get_usb_dev_cnt(); i++) { - if(pBufferForLastReadData[i] != NULL) + if (pBufferForLastReadData[i] != NULL) free(pBufferForLastReadData[i]); } - if(pBufferforLoadedFile != NULL) - free(pBufferforLoadedFile); + if (pBufferforLoadedFile != NULL) + free(pBufferforLoadedFile); } int FirmwareUpdate() { - FW_INFO Data; - FILE * pFile; + FW_INFO Data; + FILE* pFile; size_t result, filesize; - int fwVer; -// unsigned char vBuffer[8] ; - int fw[3]; + int fwVer; + // unsigned char vBuffer[8] ; + int fw[3]; - pFile = fopen( g_parameter_fw , "rb" ); + pFile = fopen(g_parameter_fw, "rb"); if (pFile == NULL) { - printf("Can not open %s \n",g_parameter_fw); + printf("Can not open %s \n", g_parameter_fw); return EXCODE_FAIL_UPDATE_FW; } // obtain file size: - filesize = fseek (pFile , 0 , SEEK_END); + filesize = fseek(pFile, 0, SEEK_END); if (filesize < sizeof(FW_INFO)) { printf("File %s too small.\n", g_parameter_fw); - return -1; - } - fseek (pFile , 0 , SEEK_SET); -// rewind (pFile); + return -1; + } + fseek(pFile, 0, SEEK_SET); + // rewind (pFile); // copy the file into the buffer: - result = fread ((unsigned char*)&Data,1,sizeof(FW_INFO),pFile); + result = fread((unsigned char*)&Data, 1, sizeof(FW_INFO), pFile); if (result != sizeof(FW_INFO)) { printf("File %s read problem.\n", g_parameter_fw); - return -1; - } - fclose (pFile); + return -1; + } + fclose(pFile); if (Data.dwSignature != 0x44504657) { printf("This file is not DediProg firmware format.\n"); - return EXCODE_FAIL_UPDATE_FW; - } - sscanf(&Data.Version[0],"%d.%d.%d",&fw[0],&fw[1],&fw[2]); - fwVer=((fw[0]<<16) | (fw[1]<<8) | fw[2]); + return EXCODE_FAIL_UPDATE_FW; + } + sscanf(&Data.Version[0], "%d.%d.%d", &fw[0], &fw[1], &fw[2]); + fwVer = ((fw[0] << 16) | (fw[1] << 8) | fw[2]); printf("Current Firmware is %x, New Firmware Version is %x\n", g_firmversion, fwVer); - if(strstr(g_board_type,Data.Programmer)==NULL) + if (strstr(g_board_type, Data.Programmer) == NULL) printf("Error: This firmware for %s!\n", Data.Programmer); if ((strstr(g_board_type, Data.Programmer) == NULL) || ((fwVer & 0xff0000) != (g_firmversion & 0xff0000))) { printf("Error: This firmware can not be updated on this programmer!\n"); - return EXCODE_FAIL_UPDATE_FW; - } + return EXCODE_FAIL_UPDATE_FW; + } - printf("%s",msg_info_firmwareupdate); - Run(UPDATE_FIRMWARE,g_uiDevNum); + printf("%s", msg_info_firmwareupdate); + Run(UPDATE_FIRMWARE, g_uiDevNum); - if( Wait( msg_info_firmwareupdateOK, msg_info_firmwareupdatefail) ) - return 0; + if (Wait(msg_info_firmwareupdateOK, msg_info_firmwareupdatefail)) + return 0; - return 0; + return 0; } void cli_classic_usage(bool IsShowExample) @@ -780,29 +778,29 @@ void cli_classic_usage(bool IsShowExample) printf("Basic Usages:\n"); printf("./dpcmd -uxxx\n"); printf("./dpcmd --auto=xxx\n"); - if(IsShowExample) + if (IsShowExample) printf("Usage Examples:\n" - "1. dpcmd -r\"f:\\file.bin\", \n reads the chip and save it into a file \"file.bin\"\n" - "2. dpcmd -rSTDOUT -a0x100 -l0x23, \n reads 0x23 bytes starting from 0x100 and display it on the screen\n" - "3. dpcmd -ufile.bin, \n Update the whole chip with file.bin\n" - "4. Dpcmd -ufile.bin -a0x100, \n Replace the serial flash contents for the size of file.bin with file.bin.\n" - " (contents outside the replace area will be kept unchanged)\n" - "5. dpcmd -pfile.bin -a0x100, \n program file.bin into the serial flash starting from address 0x100\n" - " (the target area is assumed to be blank already)\n" - "6. dpcmd -pfile.bin -x0xaa, \n programs file.bin into the serial flash and fill the rest area with 0xaa\n" - " (the target area is assumed to be blank already)\n" - "Remark: -a, -l only works with -p, -r, -s and -u\n" - "Remark: -x only works with -p"); + "1. dpcmd -r\"f:\\file.bin\", \n reads the chip and save it into a file \"file.bin\"\n" + "2. dpcmd -rSTDOUT -a0x100 -l0x23, \n reads 0x23 bytes starting from 0x100 and display it on the screen\n" + "3. dpcmd -ufile.bin, \n Update the whole chip with file.bin\n" + "4. Dpcmd -ufile.bin -a0x100, \n Replace the serial flash contents for the size of file.bin with file.bin.\n" + " (contents outside the replace area will be kept unchanged)\n" + "5. dpcmd -pfile.bin -a0x100, \n program file.bin into the serial flash starting from address 0x100\n" + " (the target area is assumed to be blank already)\n" + "6. dpcmd -pfile.bin -x0xaa, \n programs file.bin into the serial flash and fill the rest area with 0xaa\n" + " (the target area is assumed to be blank already)\n" + "Remark: -a, -l only works with -p, -r, -s and -u\n" + "Remark: -x only works with -p"); printf("(space is not needed between the switches and parameters. E.g. dpcmd -ubio.bin)\n"); printf(" -? [ --help ] show this help message\n" - " --list print supported chip list\n" - " -d [ --detect ] detect chip\n" - " -b [ --blank ] blank check\n" - " -e [ --erase ] erase entire chip\n" - " -r [ --read ] arg read chip contents and save to a bin/hex/s19 file\n" - " -p [ --prog ] arg program chip without erase\n" - " -u [ --auto ] arg automatically run the following sequence:\n" + " --list print supported chip list\n" + " -d [ --detect ] detect chip\n" + " -b [ --blank ] blank check\n" + " -e [ --erase ] erase entire chip\n" + " -r [ --read ] arg read chip contents and save to a bin/hex/s19 file\n" + " -p [ --prog ] arg program chip without erase\n" + " -u [ --auto ] arg automatically run the following sequence:\n" " - Read the memory content\n" " - Compare the memory content\n" " - Erase only the sectors with some differences\n" @@ -811,126 +809,125 @@ void cli_classic_usage(bool IsShowExample) " - check if the chip is blank or not;\n" " - erase the entire chip(if not blank);\n" " - program a whole file starting from address 0\n" - " -s [ --sum ] display chip content checksum\n" - " -f [ --fsum ] arg display the file checksum\n" - " --raw-instruction arg issue raw serial flash instructions.\n" - " - use spaces(" ") to delimite bytes.\n" - " - instructions must be enclosed in double quotation marks(\"\")\n" - " Example:\n" - " dpcmd --raw-instruction \"03 FF 00 12\"\n" - " --raw-require-return arg (=0) decimal bytes of result to return in decimal\n" - " after issuing raw instructions.\n" - " - used along with --raw-instruction only.\n" - " Example:\n" - " dpcmd --raw-instruction \"03 FF 00 12\" \n" - " --raw-require-return 1\n" - " dpcmd --raw-instruction \"06|01 0C|05\" \n" - " --raw-require-return \"0|0|1\" \n" - "\n" - "Optional Switches that add fine-tune ability to Basic Switches:\n" - " -a [ --addr ] arg hexadecimal starting address hexadecimal(e.g.\n" - " 0x1000),\n" - " - works with --prog/read/sum/auto only\n" - " - defaults to 0, if omitted.\n" + " -s [ --sum ] display chip content checksum\n" + " -f [ --fsum ] arg display the file checksum\n" + " --raw-instruction arg issue raw serial flash instructions.\n" + " - use spaces(" + ") to delimite bytes.\n" + " - instructions must be enclosed in double quotation marks(\"\")\n" + " Example:\n" + " dpcmd --raw-instruction \"03 FF 00 12\"\n" + " --raw-require-return arg (=0) decimal bytes of result to return in decimal\n" + " after issuing raw instructions.\n" + " - used along with --raw-instruction only.\n" + " Example:\n" + " dpcmd --raw-instruction \"03 FF 00 12\" \n" + " --raw-require-return 1\n" + " dpcmd --raw-instruction \"06|01 0C|05\" \n" + " --raw-require-return \"0|0|1\" \n" + "\n" + "Optional Switches that add fine-tune ability to Basic Switches:\n" + " -a [ --addr ] arg hexadecimal starting address hexadecimal(e.g.\n" + " 0x1000),\n" + " - works with --prog/read/sum/auto only\n" + " - defaults to 0, if omitted.\n" " -l [ --length ] arg decimal/hexadecimal length to read/program in bytes,\n" - " - works with --prog/read/sum/auto only\n" - " - defaults to whole file if omitted\n" - " -v [ --verify ] verify checksum file and chip\n" - " - works with --prog/auto only\n" - " -x [ --fill ] arg (=FF) fill spare space with an hex value(e.g.FF),\n" - " - works with --prog, --auto only\n" - " --type arg Specify a type to override auto detection\n" - " - use --list arguement to look up supported type.\n" - " --lock-start arg hexadecimal starting address(e.g. 0x1000),\n" - " - works with --auto/lock-length\n" - " - defaults to 0, if omitted.\n" - " --lock-length arg hexadecimal length of area that will be kept\n" - " unchanged while updating\n" - " - used along with --auto/lock-start only.\n" - " --blink arg \n" -// " - 0 : Blink green LED 3 times from USB1 to USBn\n" -// " (Default)\n" -// " note: the sequence is assigned by OS during USB plug-in\n" - " - 1: Blink the programmer connected to USB1 3 times.\n" -// " - n: Blink the programmer connected to USBn 3 times.\n" - " --device arg (work with all Basic Switchs)\n" -// " - 1: activate only the programmer connected to USB1\n" -// " - n: activate only the programmer connected to USBn\n" -// " note: if '--device' is not used, the command will\n" -// " be executed on all connected programmer.\n" -// " --fix-device arg Fix programmer serial number with programmer sequence.\n" -// " - instructions must be enclosed in double quotation marks(\"\")\n" -// " Example:\n" -// " dpcmd --fix-device \"1 DP000001\"\n" - " --list-device-id arg \n" -// " - 0 : List all ID of programmers from USB1 to USBn (Default)\n" -// " note: the sequence is assigned by OS during USB plug-in.\n" - " - 1: Prompt the device ID of programmer connected to USB1.\n" -// " - n: Prompt the device ID of programmer connected to USBn.\n" - " --load-file arg -Load a bin/hex/s19 file and compare with the memory conten.\n" - " -work with --verify only" - "\n" - "Miscellaneous options:\n" - " -t [ --timeout ] arg (=300) Timeout value in seconds\n" - " -g [ --target ] arg (=1) Target Options\n" - " Available values:\n" - " 1, Chip 1(Default)\n" - " 2, Chip 2\n" - " 3, Socket\n" - " 0, reference card\n" - " --vcc arg (=0) specify vcc\n" - " 0, 3.5V\n" - " 1, 2.5V\n" - " 2, 1.8V\n" - " 1800 ~ 3800, 1.8V ~ 3.8V\n" - " (minimum step 100mV)(For SF600/SF600Plus only)\n" - " --vpp apply vpp when the memory chip supports it\n" - " --log arg Record the operation result in given/appointed .txt file\n" - " Example:\n" - " ./dpcmd --log /tmp/log.txt\n" - " Note: If user didn't use this command, \n" - " the operation result will be recorded in \"log.txt\"\n" - " - work with --prog and --erase.\n" - " -i [ --silent ] supress the display of real-time timer counting\n" - " - used when integrating with 3rd-party tools(e.g. IDE)\n" - " --spi-clk arg (=2) specify SPI clock:\n" - " 2, 12 MHz(Default)\n" - " 0, 24 MHz\n" - " 1, 8 MHz\n" - " 3, 3 MHz\n" - " 4, 2.18 MHz\n" - " 5, 1.5 MHz\n" - " 6, 750 KHz\n" - " 7, 375 KHz\n" - " --set-io1 arg (=0) specify Level of IO1(SF100) or GPIO1(SF600/SF600Plus):\n" - " 0, Low(Default)\n" - " 1, High\n" - " --set-io4 arg (=1) specify Level of IO4(SF100) or GPIO2(SF600/SF600Plus):\n" - " 0, Low\n" - " 1, High(Default)\n" - " -e|--display-delta SECONDS (=0.5) wait this many seconds\n" - " before refreshing the screen\n" - "\n\n\n"); + " - works with --prog/read/sum/auto only\n" + " - defaults to whole file if omitted\n" + " -v [ --verify ] verify checksum file and chip\n" + " - works with --prog/auto only\n" + " -x [ --fill ] arg (=FF) fill spare space with an hex value(e.g.FF),\n" + " - works with --prog, --auto only\n" + " --type arg Specify a type to override auto detection\n" + " - use --list arguement to look up supported type.\n" + " --lock-start arg hexadecimal starting address(e.g. 0x1000),\n" + " - works with --auto/lock-length\n" + " - defaults to 0, if omitted.\n" + " --lock-length arg hexadecimal length of area that will be kept\n" + " unchanged while updating\n" + " - used along with --auto/lock-start only.\n" + " --blink arg \n" + // " - 0 : Blink green LED 3 times from USB1 to USBn\n" + // " (Default)\n" + // " note: the sequence is assigned by OS during USB plug-in\n" + " - 1: Blink the programmer connected to USB1 3 times.\n" + // " - n: Blink the programmer connected to USBn 3 times.\n" + " --device arg (work with all Basic Switchs)\n" + // " - 1: activate only the programmer connected to USB1\n" + // " - n: activate only the programmer connected to USBn\n" + // " note: if '--device' is not used, the command will\n" + // " be executed on all connected programmer.\n" + // " --fix-device arg Fix programmer serial number with programmer sequence.\n" + // " - instructions must be enclosed in double quotation marks(\"\")\n" + // " Example:\n" + // " dpcmd --fix-device \"1 DP000001\"\n" + " --list-device-id arg \n" + // " - 0 : List all ID of programmers from USB1 to USBn (Default)\n" + // " note: the sequence is assigned by OS during USB plug-in.\n" + " - 1: Prompt the device ID of programmer connected to USB1.\n" + // " - n: Prompt the device ID of programmer connected to USBn.\n" + " --load-file arg -Load a bin/hex/s19 file and compare with the memory conten.\n" + " -work with --verify only" + "\n" + "Miscellaneous options:\n" + " -t [ --timeout ] arg (=300) Timeout value in seconds\n" + " -g [ --target ] arg (=1) Target Options\n" + " Available values:\n" + " 1, Chip 1(Default)\n" + " 2, Chip 2\n" + " 3, Socket\n" + " 0, reference card\n" + " --vcc arg (=0) specify vcc\n" + " 0, 3.5V\n" + " 1, 2.5V\n" + " 2, 1.8V\n" + " 1800 ~ 3800, 1.8V ~ 3.8V\n" + " (minimum step 100mV)(For SF600/SF600Plus only)\n" + " --vpp apply vpp when the memory chip supports it\n" + " --log arg Record the operation result in given/appointed .txt file\n" + " Example:\n" + " ./dpcmd --log /tmp/log.txt\n" + " Note: If user didn't use this command, \n" + " the operation result will be recorded in \"log.txt\"\n" + " - work with --prog and --erase.\n" + " -i [ --silent ] supress the display of real-time timer counting\n" + " - used when integrating with 3rd-party tools(e.g. IDE)\n" + " --spi-clk arg (=2) specify SPI clock:\n" + " 2, 12 MHz(Default)\n" + " 0, 24 MHz\n" + " 1, 8 MHz\n" + " 3, 3 MHz\n" + " 4, 2.18 MHz\n" + " 5, 1.5 MHz\n" + " 6, 750 KHz\n" + " 7, 375 KHz\n" + " --set-io1 arg (=0) specify Level of IO1(SF100) or GPIO1(SF600/SF600Plus):\n" + " 0, Low(Default)\n" + " 1, High\n" + " --set-io4 arg (=1) specify Level of IO4(SF100) or GPIO2(SF600/SF600Plus):\n" + " 0, Low\n" + " 1, High(Default)\n" + " -e|--display-delta SECONDS (=0.5) wait this many seconds\n" + " before refreshing the screen\n" + "\n\n\n"); } int OpenUSB(void) { - return usb_driver_init(); + return usb_driver_init(); } - void sin_handler(int sig) { - if(sig==SIGINT) - { - ; - } + if (sig == SIGINT) { + ; + } } int Handler(void) { if (Is_usbworking(0) == true) { - #if 0 +#if 0 if(m_vm.count("fix-device")) { FixProgrammer(); @@ -938,7 +935,7 @@ int Handler(void) } AssignedDevice(); - #endif +#endif if ((g_ucOperation & BLINK) == BLINK) { BlinkProgrammer(); @@ -963,18 +960,18 @@ int Handler(void) return EXCODE_PASS; if (strlen(g_parameter_type) > 0) { - int dev_cnt=get_usb_dev_cnt(); + int dev_cnt = get_usb_dev_cnt(); if (g_uiDevNum == 0) { for (int i = 0; i < dev_cnt; i++) { - RawInstructions(i); - } + RawInstructions(i); + } } else if (g_uiDevNum <= dev_cnt) { - RawInstructions(g_uiDevNum-1); + RawInstructions(g_uiDevNum - 1); } else { printf("Error: Did not find the programmer.\n"); - return EXCODE_FAIL_OTHERS; - } + return EXCODE_FAIL_OTHERS; + } return Sequence(); } else if (DetectChip()) { return Sequence(); @@ -986,7 +983,7 @@ int Handler(void) bool InitProject(void) { - int dev_cnt=get_usb_dev_cnt(); + int dev_cnt = get_usb_dev_cnt(); if (Is_usbworking(0)) { int targets[4] = { STARTUP_APPLI_CARD, @@ -997,50 +994,50 @@ bool InitProject(void) if (g_uiDevNum == 0) { for (int i = 0; i < dev_cnt; i++) { - g_StartupMode=targets[g_ucTarget]; - SetTargetFlash((unsigned char)targets[g_ucTarget], i); - SetSPIClock(i); - SetVcc(i); + g_StartupMode = targets[g_ucTarget]; + SetTargetFlash((unsigned char)targets[g_ucTarget], i); + SetSPIClock(i); + SetVcc(i); 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); + printf("%s chip size is %zd bytes.\n\n", Chip_Info.TypeName, Chip_Info.ChipSizeInByte); + ProjectInitWithID(Chip_Info, i); if (!strcmp(Chip_Info.Class, "N25Qxxx_Large")) - isSendFFsequence=true; + isSendFFsequence = true; } else { printf("Chip Type Unknow is applied manually.\n"); - return false; - } + return false; + } } else { - ProjectInit(i); - } - } + ProjectInit(i); + } + } } 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); + 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 (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("%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.\n"); - return false; - } + return false; + } } else { - ProjectInit(g_uiDevNum-1); - } + ProjectInit(g_uiDevNum - 1); + } } else printf("Error: Did not find the programmer.\n"); - return true; + return true; } return false; } @@ -1055,43 +1052,43 @@ void CloseProject(void) bool DetectChip(void) { - int dev_cnt=get_usb_dev_cnt(); - Chip_Info=GetFirstDetectionMatch(0); + int dev_cnt = get_usb_dev_cnt(); + Chip_Info = GetFirstDetectionMatch(0); if (g_uiDevNum == 0) { for (int i = 0; i < dev_cnt; i++) { if (!Is_usbworking(i)) { - printf("%s",msg_err_communication); - return false; - } + printf("%s", msg_err_communication); + return false; + } if (0 == Chip_Info.UniqueID) { - printf("%s",msg_err_identifychip); - return false; - } - printf("By reading the chip ID, the chip applies to [ %s ]\n",Chip_Info.TypeName); - printf("%s parameters to be applied by default\n",Chip_Info.TypeName); - printf("%s chip size is %zd bytes.\n\n",Chip_Info.TypeName,Chip_Info.ChipSizeInByte); - - RawInstructions(i); - } + printf("%s", msg_err_identifychip); + return false; + } + printf("By reading the chip ID, the chip applies to [ %s ]\n", Chip_Info.TypeName); + printf("%s parameters to be applied by default\n", Chip_Info.TypeName); + printf("%s chip size is %zd bytes.\n\n", Chip_Info.TypeName, Chip_Info.ChipSizeInByte); + + RawInstructions(i); + } } else if (g_uiDevNum <= dev_cnt) { if (!Is_usbworking(g_uiDevNum - 1)) { - printf("%s",msg_err_communication); - return false; - } + printf("%s", msg_err_communication); + return false; + } if (0 == Chip_Info.UniqueID) { - printf("%s",msg_err_identifychip); - return false; - } - printf("By reading the chip ID, the chip applies to [ %s ]\n",Chip_Info.TypeName); - printf("%s parameters to be applied by default\n",Chip_Info.TypeName); - printf("%s chip size is %zd bytes.\n\n",Chip_Info.TypeName,Chip_Info.ChipSizeInByte); + printf("%s", msg_err_identifychip); + return false; + } + printf("By reading the chip ID, the chip applies to [ %s ]\n", Chip_Info.TypeName); + printf("%s parameters to be applied by default\n", Chip_Info.TypeName); + printf("%s chip size is %zd bytes.\n\n", Chip_Info.TypeName, Chip_Info.ChipSizeInByte); - RawInstructions(g_uiDevNum-1); + RawInstructions(g_uiDevNum - 1); } else printf("Error: Did not find the programmer.\n"); - return true; + return true; } /* Simon: add later */ @@ -1101,38 +1098,38 @@ void SetVpp(int Index) void SetSPIClock(int Index) { -// sscanf(g_parameter_spi_clk,"%d",&g_ucSPIClock); - SetSPIClockValue(g_ucSPIClock,Index); + // sscanf(g_parameter_spi_clk,"%d",&g_ucSPIClock); + SetSPIClockValue(g_ucSPIClock, Index); } void SetVcc(int Index) { -// sscanf(g_parameter_vcc,"%d",&g_Vcc); + // sscanf(g_parameter_vcc,"%d",&g_Vcc); if (!(g_Vcc <= 3800 && g_Vcc >= 1800 && g_bIsSF600[Index] == true)) - g_Vcc= (0x10 | (g_Vcc&0x03)); + g_Vcc = (0x10 | (g_Vcc & 0x03)); } int do_loadFile(void) { char* filename = NULL; - if(g_ucOperation & PROGRAM) - filename=g_parameter_program; - else if(g_ucOperation & LOADFILE) - filename=g_parameter_loadfile; + 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) { - case 1: - filename=g_parameter_batch; - break; - case 2: - default: - filename=g_parameter_auto; - break; - } + case 1: + filename = g_parameter_batch; + break; + case 2: + default: + filename = g_parameter_auto; + break; + } } else if (g_ucOperation & FSUM) - filename=g_parameter_fsum; - printf("%s",msg_info_loading); - printf("(%s)",filename); + filename = g_parameter_fsum; + printf("%s", msg_info_loading); + printf("(%s)", filename); return LoadFile(filename); } @@ -1145,72 +1142,72 @@ bool ListTypes(void) void BlinkProgrammer(void) { - bool IsV5=is_BoardVersionGreaterThan_5_0_0(0); + bool IsV5 = is_BoardVersionGreaterThan_5_0_0(0); if (IsV5 == false) return; - int dev_cnt=get_usb_dev_cnt(); + int dev_cnt = get_usb_dev_cnt(); if (g_uiBlink == 0) { for (int i = 0; i < dev_cnt; i++) { printf("Blink LED on USB device %d \n", i); - BlinkProgBoard(IsV5,i); + BlinkProgBoard(IsV5, i); Sleep(500); - BlinkProgBoard(IsV5,i); + BlinkProgBoard(IsV5, i); Sleep(500); - BlinkProgBoard(IsV5,i); - } + BlinkProgBoard(IsV5, i); + } } else if (g_uiBlink <= dev_cnt) { printf("Blink LED on USB device %d \n", g_uiBlink); - BlinkProgBoard(IsV5,g_uiBlink-1); + BlinkProgBoard(IsV5, g_uiBlink - 1); Sleep(500); - BlinkProgBoard(IsV5,g_uiBlink-1); + BlinkProgBoard(IsV5, g_uiBlink - 1); Sleep(500); - BlinkProgBoard(IsV5,g_uiBlink-1); + BlinkProgBoard(IsV5, g_uiBlink - 1); } else printf("Error: Did not find the programmer.\n"); } void ListSFSerialID(void) { - unsigned int dev_cnt=0; + unsigned int dev_cnt = 0; dev_cnt = get_usb_dev_cnt(); - unsigned int dwUID=0; + unsigned int dwUID = 0; if (g_uiDeviceID < 1) { for (unsigned int i = 0; i < dev_cnt; i++) { - dwUID=ReadUID(i); - if((dwUID / 600000)==0) + dwUID = ReadUID(i); + if ((dwUID / 600000) == 0) printf("%d,\tDP%06d\n", i + 1, dwUID); - else + else printf("%d,\tSF%06d\n", i + 1, dwUID); - } + } } else { - if(g_uiDeviceID>dev_cnt) + if (g_uiDeviceID > dev_cnt) printf("The number of programmer is not defined!\n"); else { - dwUID=ReadUID(g_uiDeviceID-1); - if((dwUID / 600000)==0) + dwUID = ReadUID(g_uiDeviceID - 1); + if ((dwUID / 600000) == 0) printf("%d,\tDP%06d\n", g_uiDeviceID, dwUID); - else + else printf("%d,\tSF%06d\n", g_uiDeviceID, dwUID); - } + } } } void do_BlankCheck(void) { - printf("%s\n",msg_info_checking); - Run(BLANKCHECK_WHOLE_CHIP,g_uiDevNum); + printf("%s\n", msg_info_checking); + Run(BLANKCHECK_WHOLE_CHIP, g_uiDevNum); - Wait ( msg_info_chipblank, msg_info_chipnotblank ); + Wait(msg_info_chipblank, msg_info_chipnotblank); } void do_Erase(void) { - printf("%s \n",msg_info_erasing); - Run(ERASE_WHOLE_CHIP,g_uiDevNum); + printf("%s \n", msg_info_erasing); + Run(ERASE_WHOLE_CHIP, g_uiDevNum); - Wait( msg_info_eraseOK, msg_info_erasefail) ; + Wait(msg_info_eraseOK, msg_info_erasefail); } void do_Program(void) @@ -1219,82 +1216,82 @@ void do_Program(void) return; SaveProgContextChanges(); - printf("%s\n",msg_info_programming); - Run(PROGRAM_CHIP,g_uiDevNum); + printf("%s\n", msg_info_programming); + Run(PROGRAM_CHIP, g_uiDevNum); 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))); + printf("\nChecksum(whole file): %08X", g_uiFileChecksum); + printf("\nChecksum(Written part of file): %08X\n", CRC32(pBufferforLoadedFile, min(DownloadAddrRange.end - DownloadAddrRange.start, g_ulFileSize))); } } void do_Read(void) { - DownloadAddrRange.start=g_uiAddr; - DownloadAddrRange.length=g_uiLen; - DownloadAddrRange.end=g_uiAddr+g_uiLen; + DownloadAddrRange.start = g_uiAddr; + DownloadAddrRange.length = g_uiLen; + DownloadAddrRange.end = g_uiAddr + g_uiLen; - printf("%s\n",msg_info_reading); - Run(READ_ANY_BY_PREFERENCE_CONFIGURATION,g_uiDevNum); - Wait(msg_info_readOK,msg_info_readfail); + printf("%s\n", msg_info_reading); + Run(READ_ANY_BY_PREFERENCE_CONFIGURATION, g_uiDevNum); + Wait(msg_info_readOK, msg_info_readfail); } void do_DisplayOrSave(void) { - int dev_cnt=get_usb_dev_cnt(); + int dev_cnt = get_usb_dev_cnt(); if (g_uiDevNum == 0) { for (int icnt = 0; icnt < dev_cnt; icnt++) { if (strcmp(g_parameter_read, "STDOUT") == 0) { - int i,Len=UploadAddrRange.end-UploadAddrRange.start; - printf("\n"); - printf("Hex value display (starting from 0x%02X, 0x%02zX bytes in total): \n",g_uiAddr,g_uiLen); + int i, Len = UploadAddrRange.end - UploadAddrRange.start; + printf("\n"); + printf("Hex value display (starting from 0x%02X, 0x%02zX bytes in total): \n", g_uiAddr, g_uiLen); for (i = 0; i < Len; i++) { if ((i % 16 == 0) && i != 0) { - printf("\n"); - } - printf("%02X ",*(pBufferForLastReadData[icnt]+i)); - } - printf("\n\n"); + printf("\n"); + } + printf("%02X ", *(pBufferForLastReadData[icnt] + i)); + } + printf("\n\n"); } else { - UploadAddrRange.length=UploadAddrRange.end-UploadAddrRange.start; - char str[64]; - sprintf(str, "%d_", icnt+1); - strcat(str,g_parameter_read); - - if(WriteFile((const char*)str, pBufferForLastReadData[icnt], UploadAddrRange.length)==1) - printf("\nSuccessfully saved into file:%s \n",str); - else - printf("\nFailed to save into file: %s\n",str); - } - } + UploadAddrRange.length = UploadAddrRange.end - UploadAddrRange.start; + char str[64]; + sprintf(str, "%d_", icnt + 1); + strcat(str, g_parameter_read); + + if (WriteFile((const char*)str, pBufferForLastReadData[icnt], UploadAddrRange.length) == 1) + printf("\nSuccessfully saved into file:%s \n", str); + else + printf("\nFailed to save into file: %s\n", str); + } + } } else if (g_uiDevNum != 0) { if (strcmp(g_parameter_read, "STDOUT") == 0) { - int i,Len=UploadAddrRange.end-UploadAddrRange.start; + int i, Len = UploadAddrRange.end - UploadAddrRange.start; printf("\n"); - printf("Hex value display (starting from 0x%02X, 0x%02zX bytes in total): \n",g_uiAddr,g_uiLen); + printf("Hex value display (starting from 0x%02X, 0x%02zX bytes in total): \n", g_uiAddr, g_uiLen); for (i = 0; i < Len; i++) { - if((i%16==0) && i != 0) - printf("\n"); - printf("%02X ",*(pBufferForLastReadData[g_uiDevNum-1]+i)); + if ((i % 16 == 0) && i != 0) + printf("\n"); + printf("%02X ", *(pBufferForLastReadData[g_uiDevNum - 1] + i)); } printf("\n\n"); } else { - UploadAddrRange.length=UploadAddrRange.end-UploadAddrRange.start; - if(WriteFile((const char*)g_parameter_read, pBufferForLastReadData[g_uiDevNum-1], UploadAddrRange.length)==1) - printf("\nSuccessfully saved into file:%s \n",g_parameter_read); + UploadAddrRange.length = UploadAddrRange.end - UploadAddrRange.start; + if (WriteFile((const char*)g_parameter_read, pBufferForLastReadData[g_uiDevNum - 1], UploadAddrRange.length) == 1) + printf("\nSuccessfully saved into file:%s \n", g_parameter_read); else - printf("\nFailed to save into file: %s\n",g_parameter_read); + printf("\nFailed to save into file: %s\n", g_parameter_read); } } else - printf("The number of programmer is not defined!\n"); + printf("The number of programmer is not defined!\n"); } void SaveProgContextChanges(void) { - LockAddrrange.start=g_uiLockStart; - LockAddrrange.length=g_uiLockLen; - LockAddrrange.end=g_uiLockStart+g_uiLockLen; + LockAddrrange.start = g_uiLockStart; + LockAddrrange.length = g_uiLockLen; + LockAddrrange.end = g_uiLockStart + g_uiLockLen; #if 0 using numeric_conversion::hexstring_to_size_t; @@ -1312,66 +1309,66 @@ void do_Auto(void) return; SaveProgContextChanges(); - printf("%s",msg_info_auto); - Run(AUTO,g_uiDevNum); + printf("%s", msg_info_auto); + Run(AUTO, g_uiDevNum); if (Wait(msg_info_autoOK, msg_info_autofail)) { - printf("\nChecksum(whole file): %08X",g_uiFileChecksum); - printf("\nChecksum(Written part of file): %08X\n",CRC32(pBufferforLoadedFile, DownloadAddrRange.end-DownloadAddrRange.start)); + printf("\nChecksum(whole file): %08X", g_uiFileChecksum); + printf("\nChecksum(Written part of file): %08X\n", CRC32(pBufferforLoadedFile, DownloadAddrRange.end - DownloadAddrRange.start)); } } void do_Verify(void) { - if(g_ucOperation & LOADFILE) + if (g_ucOperation & LOADFILE) do_loadFile(); - printf("%s",msg_info_verifying); - Run(VERIFY_CONTENT,g_uiDevNum); + printf("%s", msg_info_verifying); + Run(VERIFY_CONTENT, g_uiDevNum); - Wait(msg_info_verifyOK,msg_info_verifyfail); + Wait(msg_info_verifyOK, msg_info_verifyfail); } void do_ReadSR(int Index) { - unsigned char rdsr=0x05; + unsigned char rdsr = 0x05; unsigned char cSR; - if(FlashCommand_SendCommand_OneOutOneIn(&rdsr,1,&cSR,1,Index)==0) + if (FlashCommand_SendCommand_OneOutOneIn(&rdsr, 1, &cSR, 1, Index) == 0) printf("SR unavailable\n"); else - printf("SR=0x%02X\n",cSR); + printf("SR=0x%02X\n", cSR); } void do_RawInstructinos_2(int outDLen, char* para, int Index) { - unsigned char vOut[512]; - unsigned char vIn[512]={0xFF,0xFF,0xFF,0}; - int i=0; - char* pch; - - pch = strtok (para, " ,;-"); - while (pch != NULL) { - sscanf(pch,"%02hhx",&vOut[i]); - i++; - pch = strtok (NULL, " ,;-"); - } - - // if(strlen(g_parameter_raw_return)>0) - // sscanf(g_parameter_raw_return,"%hhu",&Length); - - if(outDLen>0) - FlashCommand_SendCommand_OneOutOneIn(vOut,i,vIn,outDLen,Index); - else - FlashCommand_SendCommand_OutOnlyInstruction(vOut,i,Index); + unsigned char vOut[512]; + unsigned char vIn[512] = { 0xFF, 0xFF, 0xFF, 0 }; + int i = 0; + char* pch; + + pch = strtok(para, " ,;-"); + while (pch != NULL) { + sscanf(pch, "%02hhx", &vOut[i]); + i++; + pch = strtok(NULL, " ,;-"); + } + + // if(strlen(g_parameter_raw_return)>0) + // sscanf(g_parameter_raw_return,"%hhu",&Length); + + if (outDLen > 0) + FlashCommand_SendCommand_OneOutOneIn(vOut, i, vIn, outDLen, Index); + else + FlashCommand_SendCommand_OutOnlyInstruction(vOut, i, Index); if (outDLen > 0) { - printf("issuing raw instruction \"%s\" returns %d bytes as required:\n",g_parameter_raw,outDLen); - for(i=0; i 0) { - sscanf(g_parameter_raw_return,"%hhu",&Length); - strcpy(retrunParameter,g_parameter_raw_return); + sscanf(g_parameter_raw_return, "%hhu", &Length); + strcpy(retrunParameter, g_parameter_raw_return); - pchR = strtok (retrunParameter,"|"); + pchR = strtok(retrunParameter, "|"); while (pchR != NULL) { - sscanf(pchR,"%02hhx",&pchReturn[iReturn]); - iReturn++; - pchR = strtok (NULL,"|"); - } + sscanf(pchR, "%02hhx", &pchReturn[iReturn]); + iReturn++; + pchR = strtok(NULL, "|"); + } if (iReturn != i) { - printf("\nError: value of --raw-require-return failed. No return value!\n\n"); - memset(pchReturn,0,30); - } + printf("\nError: value of --raw-require-return failed. No return value!\n\n"); + memset(pchReturn, 0, 30); + } } for (int j = 0; j < i; j++) { - do_RawInstructinos_2(pchReturn[j], pch[j],Index); + do_RawInstructinos_2(pchReturn[j], pch[j], Index); } } void RawInstructions(int Index) { if (strlen(g_parameter_raw) > 0) { - printf("\nDevice %d:\n",Index+1); + printf("\nDevice %d:\n", Index + 1); do_RawInstructions(Index); } } bool BlankCheck(void) { - if(g_ucOperation&BLANK) + if (g_ucOperation & BLANK) do_BlankCheck(); return g_bStatus; @@ -1432,14 +1429,14 @@ bool BlankCheck(void) bool Erase(void) { - if(g_ucOperation&ERASE) + if (g_ucOperation & ERASE) do_Erase(); return g_bStatus; } bool Program(void) { - if(g_ucOperation&PROGRAM) + if (g_ucOperation & PROGRAM) do_Program(); return g_bStatus; @@ -1456,14 +1453,14 @@ bool Read(void) bool Auto(void) { - if(g_ucOperation & BATCH) + if (g_ucOperation & BATCH) do_Auto(); return g_bStatus; } bool Verify(void) { - if(g_ucOperation & VERIFY) + if (g_ucOperation & VERIFY) do_Verify(); return g_bStatus; } @@ -1473,193 +1470,194 @@ bool CalChecksum(void) int dev_cnt = get_usb_dev_cnt(); if (g_ucOperation & FSUM) { do_loadFile(); - printf("\nChecksum(file): 0x%08X\n",g_uiFileChecksum); + printf("\nChecksum(file): 0x%08X\n", g_uiFileChecksum); } if (g_ucOperation & CSUM) { do_Read(); if (g_uiDevNum == 0) { for (int i = 0; i < dev_cnt; i++) { - int dwUID=ReadUID(i); + int dwUID = ReadUID(i); if (g_uiAddr == 0 && g_uiLen == 0) { - if((dwUID / 600000)==0) - printf("\nDevice %d (DP%06d):",i+1,dwUID); - else - printf("\nDevice %d (SF%06d):",i+1,dwUID); + if ((dwUID / 600000) == 0) + printf("\nDevice %d (DP%06d):", i + 1, dwUID); + else + printf("\nDevice %d (SF%06d):", i + 1, dwUID); printf("Checksum of the whole chip(address starting from: 0x%X, 0x%zX bytes in total): %08X\n", g_uiAddr, Chip_Info.ChipSizeInByte, CRC32(pBufferForLastReadData[i], Chip_Info.ChipSizeInByte)); } else { - if((dwUID / 600000)==0) - printf("\nDevice %d (DP%06d):",i+1,ReadUID(i)); - else - printf("\nDevice %d (SF%06d):",i+1,ReadUID(i)); - - printf("Checksum(address starting from: 0x%X, 0x%zX bytes in total): %08X\n",g_uiAddr,g_uiLen,CRC32(pBufferForLastReadData[i], min(g_uiLen,Chip_Info.ChipSizeInByte))); - } - } + if ((dwUID / 600000) == 0) + printf("\nDevice %d (DP%06d):", i + 1, ReadUID(i)); + else + printf("\nDevice %d (SF%06d):", i + 1, ReadUID(i)); + + printf("Checksum(address starting from: 0x%X, 0x%zX bytes in total): %08X\n", g_uiAddr, g_uiLen, CRC32(pBufferForLastReadData[i], min(g_uiLen, Chip_Info.ChipSizeInByte))); + } + } } else if (g_uiDevNum != 0) { - int dwUID=ReadUID(g_uiDevNum-1); + int dwUID = ReadUID(g_uiDevNum - 1); if (g_uiAddr == 0 && g_uiLen == 0) { - if((dwUID / 600000)==0) - printf("\nDevice %d (DP%06d):",g_uiDevNum,ReadUID(g_uiDevNum-1)); - else - printf("\nDevice %d (SF%06d):",g_uiDevNum,ReadUID(g_uiDevNum-1)); + if ((dwUID / 600000) == 0) + printf("\nDevice %d (DP%06d):", g_uiDevNum, ReadUID(g_uiDevNum - 1)); + else + printf("\nDevice %d (SF%06d):", g_uiDevNum, ReadUID(g_uiDevNum - 1)); printf("Checksum of the whole chip(address starting from: 0x%X, 0x%zX bytes in total): %08X\n", g_uiAddr, Chip_Info.ChipSizeInByte, CRC32(pBufferForLastReadData[g_uiDevNum - 1], Chip_Info.ChipSizeInByte)); } else { - if((dwUID / 600000)==0) - printf("\nDevice %d (DP%06d):",g_uiDevNum,ReadUID(g_uiDevNum-1)); - else - printf("\nDevice %d (SF%06d):",g_uiDevNum,ReadUID(g_uiDevNum-1)); - printf("Checksum(address starting from: 0x%X, 0x%zX bytes in total): %08X\n",g_uiAddr,g_uiLen,CRC32(pBufferForLastReadData[g_uiDevNum-1], min(g_uiLen,Chip_Info.ChipSizeInByte))); - } + if ((dwUID / 600000) == 0) + printf("\nDevice %d (DP%06d):", g_uiDevNum, ReadUID(g_uiDevNum - 1)); + else + printf("\nDevice %d (SF%06d):", g_uiDevNum, ReadUID(g_uiDevNum - 1)); + printf("Checksum(address starting from: 0x%X, 0x%zX bytes in total): %08X\n", g_uiAddr, g_uiLen, CRC32(pBufferForLastReadData[g_uiDevNum - 1], min(g_uiLen, Chip_Info.ChipSizeInByte))); + } } else - printf("The number of programmer is not defined!\n"); + printf("The number of programmer is not defined!\n"); } return g_bStatus; } -bool Wait(const char* strOK,const char* strFail) +bool Wait(const char* strOK, const char* strFail) { - size_t timeOut = g_uiTimeout; - struct timeval tv,basetv,diff; - int dev_cnt=get_usb_dev_cnt(); + size_t timeOut = g_uiTimeout; + struct timeval tv, basetv, diff; + int dev_cnt = get_usb_dev_cnt(); gettimeofday(&basetv, NULL); - Sleep(100); // wait till the new thread starts .... + Sleep(100); // wait till the new thread starts .... printf("\n"); while (g_is_operation_on_going == true) { - for(volatile unsigned int dly = 100000; dly; dly--) __asm("nop"); - gettimeofday (&tv , NULL); + for (volatile unsigned int dly = 100000; dly; dly--) + __asm("nop"); + gettimeofday(&tv, NULL); 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; + printf("%s", msg_err_timeout_abortion); + g_is_operation_on_going = false; + g_bStatus = false; return false; } if (g_bDisplayTimer == true) { - timersub(&tv, &basetv, &diff); + timersub(&tv, &basetv, &diff); printf("\r%0.1fs elapsed", diff.tv_sec + 0.000001 * diff.tv_usec); Sleep(500); - } + } } - printf("\n"); - if(g_uiDevNum==0)//mutiple + printf("\n"); + if (g_uiDevNum == 0) //mutiple { for (int i = 0; i < dev_cnt; i++) { - int dwUID=ReadUID(i); - if((dwUID / 600000)==0) - printf("\nDevice %d (DP%06d):",i+1,dwUID); - else - printf("\nDevice %d (SF%06d):",i+1,dwUID); - printf("%s\n",g_is_operation_successful[i]? strOK : strFail); - g_bStatus=g_is_operation_successful[i]; - } + int dwUID = ReadUID(i); + if ((dwUID / 600000) == 0) + printf("\nDevice %d (DP%06d):", i + 1, dwUID); + else + printf("\nDevice %d (SF%06d):", i + 1, dwUID); + printf("%s\n", g_is_operation_successful[i] ? strOK : strFail); + g_bStatus = g_is_operation_successful[i]; + } } else if (g_uiDevNum <= dev_cnt) { - int dwUID=ReadUID(g_uiDevNum-1); - if((dwUID / 600000)==0) - printf("\nDevice %d (DP%06d):",g_uiDevNum,dwUID); - else - printf("\nDevice %d (SF%06d):",g_uiDevNum,dwUID); - - printf("%s\n",g_is_operation_successful[g_uiDevNum-1]? strOK : strFail); - g_bStatus=g_is_operation_successful[g_uiDevNum-1]; + int dwUID = ReadUID(g_uiDevNum - 1); + if ((dwUID / 600000) == 0) + printf("\nDevice %d (DP%06d):", g_uiDevNum, dwUID); + else + printf("\nDevice %d (SF%06d):", g_uiDevNum, dwUID); + + printf("%s\n", g_is_operation_successful[g_uiDevNum - 1] ? strOK : strFail); + g_bStatus = g_is_operation_successful[g_uiDevNum - 1]; } return g_bStatus; } -int FlashIdentifier(CHIP_INFO*Chip_Info, int search_all,int Index) +int FlashIdentifier(CHIP_INFO* Chip_Info, int search_all, int Index) { long UniqueID = 0; int rc = 0; for (int c = 0; c < 2; c++) { - if(c==1) - SendFFSequence(Index); + if (c == 1) + SendFFSequence(Index); UniqueID = flash_ReadId(0x9f, 4, Index); - if (UniqueID != 0){ + if (UniqueID != 0) { rc = Dedi_Search_Chip_Db(0x9f, UniqueID, Chip_Info, search_all); - if(rc && (search_all == 0)){ - if(c==1) - isSendFFsequence=true; + if (rc && (search_all == 0)) { + if (c == 1) + isSendFFsequence = true; return rc; - } + } } UniqueID = 0; rc = 0; UniqueID = flash_ReadId(0x9f, 3, Index); - if (UniqueID != 0){ + if (UniqueID != 0) { rc = Dedi_Search_Chip_Db(0x9f, UniqueID, Chip_Info, search_all); - if(rc && (search_all == 0)){ - if(c==1) - isSendFFsequence=true; + if (rc && (search_all == 0)) { + if (c == 1) + isSendFFsequence = true; return rc; - } + } } UniqueID = 0; rc = 0; UniqueID = flash_ReadId(0x9f, 2, Index); - if(UniqueID != 0){ + if (UniqueID != 0) { rc = Dedi_Search_Chip_Db(0x9f, UniqueID, Chip_Info, search_all); - if(rc && (search_all == 0)){ - if(c==1) - isSendFFsequence=true; + if (rc && (search_all == 0)) { + if (c == 1) + isSendFFsequence = true; return rc; - } + } } } UniqueID = 0; rc = 0; UniqueID = flash_ReadId(0x15, 2, Index); - if (UniqueID != 0){ + if (UniqueID != 0) { rc = Dedi_Search_Chip_Db(0x15, UniqueID, Chip_Info, search_all); - if(rc && (search_all == 0)) + if (rc && (search_all == 0)) return rc; } UniqueID = 0; rc = 0; UniqueID = flash_ReadId(0xab, 3, Index); - if (UniqueID != 0){ + if (UniqueID != 0) { rc = Dedi_Search_Chip_Db(0xab, UniqueID, Chip_Info, search_all); - if(rc && (search_all == 0)) + if (rc && (search_all == 0)) return rc; } UniqueID = 0; rc = 0; UniqueID = flash_ReadId(0xab, 2, Index); - if (UniqueID != 0){ + if (UniqueID != 0) { rc = Dedi_Search_Chip_Db(0xab, UniqueID, Chip_Info, search_all); - if(rc && (search_all == 0)) + if (rc && (search_all == 0)) return rc; } UniqueID = 0; rc = 0; UniqueID = flash_ReadId(0x90, 3, Index); - if (UniqueID != 0){ + if (UniqueID != 0) { rc = Dedi_Search_Chip_Db(0x90, UniqueID, Chip_Info, search_all); - if(rc && (search_all == 0)) + if (rc && (search_all == 0)) return rc; } UniqueID = 0; rc = 0; UniqueID = flash_ReadId(0x90, 2, Index); - if (UniqueID != 0){ + if (UniqueID != 0) { rc = Dedi_Search_Chip_Db(0x90, UniqueID, Chip_Info, search_all); - if(rc && (search_all == 0)) + if (rc && (search_all == 0)) return rc; } UniqueID = 0; rc = 0; UniqueID = flash_ReadId(0x90, 5, Index); // for 25LF020 - if (UniqueID != 0){ - UniqueID = UniqueID - 0xFFFF0000; + if (UniqueID != 0) { + UniqueID = UniqueID - 0xFFFF0000; rc = Dedi_Search_Chip_Db(0x90, UniqueID, Chip_Info, search_all); - if(rc && (search_all == 0)) + if (rc && (search_all == 0)) return rc; } return rc; diff --git a/dpcmd.h b/dpcmd.h index 7b0da1a..2d3c327 100644 --- a/dpcmd.h +++ b/dpcmd.h @@ -6,18 +6,18 @@ #include enum ErrorCode { - EXCODE_PASS, - EXCODE_FAIL_USB, - EXCODE_FAIL_ERASE, - EXCODE_FAIL_PROG, - EXCODE_FAIL_VERIFY, - EXCODE_FAIL_READ, - EXCODE_FAIL_BLANK, // 5 - EXCODE_FAIL_BATCH, - EXCODE_FAIL_CHKSUM, - EXCODE_FAIL_IDENTIFY, - EXCODE_FAIL_UPDATE_FW, - EXCODE_FAIL_OTHERS, + EXCODE_PASS, + EXCODE_FAIL_USB, + EXCODE_FAIL_ERASE, + EXCODE_FAIL_PROG, + EXCODE_FAIL_VERIFY, + EXCODE_FAIL_READ, + EXCODE_FAIL_BLANK, // 5 + EXCODE_FAIL_BATCH, + EXCODE_FAIL_CHKSUM, + EXCODE_FAIL_IDENTIFY, + EXCODE_FAIL_UPDATE_FW, + EXCODE_FAIL_OTHERS, }; enum { // value dedicated by the spec @@ -28,7 +28,7 @@ enum { // value dedicated by the spec STARTUP_SPECIFY_LATER = 0xFE, STARTUP_PREVIOUS = 0xFF -} ; +}; enum { clk_24M = 0x00, @@ -39,12 +39,12 @@ enum { clk_1500K = 0x05, clk_750K = 0x06, clk_375K = 0x07, -} ; +}; int Sequence(); void cli_classic_usage(bool IsShowExample); bool InitProject(void); -void CloseProject(void); +void CloseProject(void); bool DetectChip(void); void SetVpp(int Index); void SetSPIClock(int Index); @@ -74,7 +74,7 @@ bool CalChecksum(void); int Handler(); bool ListTypes(void); void GetLogPath(char* path); -bool Wait(const char* strOK,const char* strFail); +bool Wait(const char* strOK, const char* strFail); void ExitProgram(void); int FirmwareUpdate(); void sin_handler(int sig); diff --git a/project.c b/project.c index db0670f..9b6f8c8 100644 --- a/project.c +++ b/project.c @@ -1,15 +1,15 @@ +#include "project.h" #include "IntelHexFile.h" #include "MotorolaFile.h" #include "SerialFlash.h" #include "board.h" #include "dpcmd.h" -#include "project.h" #include "usbdriver.h" #include #include #include -#include #include +#include #include #define min(a, b) (a > b ? b : a) @@ -421,18 +421,18 @@ bool threadBlankCheck(int Index) bool threadEraseWholeChip(int Index) { - bool result = false; + 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); + // 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 { @@ -815,7 +815,8 @@ bool RangeUpdateThruChipErase(int Index) DownloadAddrRange.length = DownloadAddrRange.end - DownloadAddrRange.start; SetIOMode(false, Index); - if (!threadReadChip(Index)) return false; + if (!threadReadChip(Index)) + return false; memcpy(vc, pBufferForLastReadData, Chip_Info.ChipSizeInByte); @@ -826,7 +827,8 @@ bool RangeUpdateThruChipErase(int Index) } } if (boIsBlank == false) { - if (threadEraseWholeChip(Index) == false) return false; + if (threadEraseWholeChip(Index) == false) + return false; if (strstr(Chip_Info.Class, SUPPORT_MACRONIX_MX25Lxxx) != NULL || strstr(Chip_Info.Class, SUPPORT_ATMEL_45DBxxxD) != NULL) { TurnOFFVcc(Index); @@ -867,14 +869,16 @@ 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; + 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 @@ -1023,7 +1027,8 @@ void SetIOMode(bool isProg, int Index) m_boEnReadQuadIO = 0; m_boEnWriteQuadIO = 0; - if (g_bIsSF600[Index] == false) return; + if (g_bIsSF600[Index] == false) + return; SetIOModeToSF600(IOValue, Index); return; @@ -1173,7 +1178,8 @@ CHIP_INFO GetFirstDetectionMatch(int Index) Loop = 1; for (i = 0; i < Loop; i++) { - if (Found == 1) break; + if (Found == 1) + break; if (Loop == 1) g_Vcc = vcc3_5V; else diff --git a/usbdriver.c b/usbdriver.c index 235fe69..883988c 100644 --- a/usbdriver.c +++ b/usbdriver.c @@ -1,11 +1,11 @@ #include "usbdriver.h" #include "FlashCommand.h" #include "project.h" -#include #include +#include -unsigned int m_nbDeviceDetected = 0; -unsigned char DevIndex = 0; +unsigned int m_nbDeviceDetected = 0; +unsigned char DevIndex = 0; extern volatile bool g_bIsSF600[16]; extern int g_CurrentSeriase; extern char g_board_type[8]; @@ -14,35 +14,35 @@ extern CHIP_INFO Chip_Info; extern unsigned int g_uiDevNum; extern bool isSendFFsequence; -#define SerialFlash_FALSE -1 -#define SerialFlash_TRUE 1 +#define SerialFlash_FALSE -1 +#define SerialFlash_TRUE 1 static int dev_index; -static usb_device_entry_t usb_device_entry[MAX_Dev_Index]; -static libusb_device_handle *dediprog_handle[MAX_Dev_Index]; +static usb_device_entry_t usb_device_entry[MAX_Dev_Index]; +static libusb_device_handle* dediprog_handle[MAX_Dev_Index]; -static libusb_context *ctx = NULL; +static libusb_context* ctx = NULL; bool Is_NewUSBCommand(int Index) { - if(is_SF100nBoardVersionGreaterThan_5_5_0(Index) || is_SF600nBoardVersionGreaterThan_6_9_0(Index)) - return true; - return false; + if (is_SF100nBoardVersionGreaterThan_5_5_0(Index) || is_SF600nBoardVersionGreaterThan_6_9_0(Index)) + return true; + return false; } extern unsigned char GetFPGAVersion(int Index); void usb_dev_init(void) { - int r; - r = libusb_init(&ctx); - if (r < 0) { - printf("initialization failed!"); - return; - } + int r; + r = libusb_init(&ctx); + if (r < 0) { + printf("initialization failed!"); + return; + } #if LIBUSB_API_VERSION < 0x01000106 - libusb_set_debug(ctx, LIBUSB_LOG_LEVEL_INFO); + libusb_set_debug(ctx, LIBUSB_LOG_LEVEL_INFO); #else - libusb_set_option(ctx, LIBUSB_OPTION_LOG_LEVEL, LIBUSB_LOG_LEVEL_INFO); + libusb_set_option(ctx, LIBUSB_OPTION_LOG_LEVEL, LIBUSB_LOG_LEVEL_INFO); #endif } @@ -51,43 +51,44 @@ void usb_db_init(void) int i; for (i = 0; i < MAX_Dev_Index; i++) { usb_device_entry[i].valid = 0; - dediprog_handle[i] = NULL; - } + dediprog_handle[i] = NULL; + } } void IsSF600(int Index) { - if( Index==-1 ) Index = DevIndex; + if (Index == -1) + Index = DevIndex; - CNTRPIPE_RQ rq ; - unsigned char vBuffer[16] ; - int fw[3]; + CNTRPIPE_RQ rq; + unsigned char vBuffer[16]; + int fw[3]; memset(vBuffer, '\0', 16); - rq.Function = URB_FUNCTION_VENDOR_ENDPOINT ; - rq.Direction = VENDOR_DIRECTION_IN ; - rq.Request = 0x08 ; - rq.Value = 0 ; - rq.Index = 0 ; + rq.Function = URB_FUNCTION_VENDOR_ENDPOINT; + rq.Direction = VENDOR_DIRECTION_IN; + rq.Request = 0x08; + rq.Value = 0; + rq.Index = 0; rq.Length = 16; - if(InCtrlRequest(&rq, vBuffer,16,Index)==SerialFlash_FALSE) - return ; + if (InCtrlRequest(&rq, vBuffer, 16, Index) == SerialFlash_FALSE) + return; - memcpy(g_board_type,&vBuffer[0],8); - sscanf((char*)&vBuffer[8],"V:%d.%d.%d",&fw[0],&fw[1],&fw[2]); - g_firmversion=((fw[0]<<16) | (fw[1]<<8) | fw[2]); + memcpy(g_board_type, &vBuffer[0], 8); + sscanf((char*)&vBuffer[8], "V:%d.%d.%d", &fw[0], &fw[1], &fw[2]); + g_firmversion = ((fw[0] << 16) | (fw[1] << 8) | fw[2]); // printf("g_firmversion=%x\n",g_firmversion); // printf("g_board_type=%s\n",g_board_type); - if(strstr(g_board_type,"SF600") != NULL) - g_bIsSF600[Index]=true; + if (strstr(g_board_type, "SF600") != NULL) + g_bIsSF600[Index] = true; else - g_bIsSF600[Index]=false; + g_bIsSF600[Index] = false; GetFPGAVersion(Index); } int get_usb_dev_cnt(void) -{ +{ return dev_index; } @@ -96,37 +97,39 @@ static int FindUSBDevice(void) { unsigned int vid = 0x0483; unsigned int pid = 0xdada; - usb_db_init(); - + usb_db_init(); + ssize_t count; - libusb_device **devs; + libusb_device** devs; count = libusb_get_device_list(ctx, &devs); for (size_t idx = 0; idx < count; ++idx) { - libusb_device *device = devs[idx]; - struct libusb_device_descriptor desc = {0}; + libusb_device* device = devs[idx]; + struct libusb_device_descriptor desc = { 0 }; int rc = libusb_get_device_descriptor(device, &desc); if (rc < 0) { printf("Error getting device descriptor\n"); break; } - if (desc.idVendor == vid && desc.idProduct == pid) { + if (desc.idVendor == vid && desc.idProduct == pid) { usb_device_entry[dev_index].usb_device = device; usb_device_entry[dev_index].valid = 1; dev_index++; } } - printf(" \n"); + printf(" \n"); return dev_index; } -int OutCtrlRequest( CNTRPIPE_RQ *rq, unsigned char *buf, unsigned long buf_size ,int Index ) +int OutCtrlRequest(CNTRPIPE_RQ* rq, unsigned char* buf, unsigned long buf_size, int Index) { - int requesttype; - int ret = 0; + 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; + if ((rq->Function != URB_FUNCTION_VENDOR_ENDPOINT) && (g_bIsSF600[Index] == true)) + return true; requesttype = 0x00; @@ -141,13 +144,13 @@ int OutCtrlRequest( CNTRPIPE_RQ *rq, unsigned char *buf, unsigned long buf_size if (rq->Function == URB_FUNCTION_VENDOR_OTHER) requesttype |= 0x43; - if (dediprog_handle[Index] ) { + if (dediprog_handle[Index]) { ret = libusb_control_transfer(dediprog_handle[Index], requesttype, rq->Request, rq->Value, rq->Index, buf, buf_size, DEFAULT_TIMEOUT); } else { printf("no device"); } if (ret != buf_size) { - #if 0 +#if 0 printf("Control Pipe output error!\n"); printf("rq->Direction=%X\n",rq->Direction); printf("rq->Function=%X\n",rq->Function); @@ -158,24 +161,24 @@ int OutCtrlRequest( CNTRPIPE_RQ *rq, unsigned char *buf, unsigned long buf_size printf("buf_size=%X\n",buf_size); printf("buf[0]=%X\n",buf[0]); printf("g_bIsSF600=%d\n",g_bIsSF600); - #endif +#endif printf("Error: %s\n", libusb_strerror(ret)); return -1; } return ret; } -int InCtrlRequest( CNTRPIPE_RQ *rq, unsigned char *buf, unsigned long buf_size, int Index ) -{ - int requesttype; - int ret = 0; +int InCtrlRequest(CNTRPIPE_RQ* rq, unsigned char* buf, unsigned long buf_size, int Index) +{ + int requesttype; + int ret = 0; if ((rq->Function != URB_FUNCTION_VENDOR_ENDPOINT) && (g_bIsSF600[Index] == true)) return true; - if( Index==-1 ) + if (Index == -1) Index = DevIndex; - if(sizeof(buf) == 0) + if (sizeof(buf) == 0) return 0; requesttype = 0x00; @@ -191,7 +194,7 @@ int InCtrlRequest( CNTRPIPE_RQ *rq, unsigned char *buf, unsigned long buf_size, if (rq->Function == URB_FUNCTION_VENDOR_OTHER) requesttype |= 0x43; - if (dediprog_handle[Index] ) { + if (dediprog_handle[Index]) { ret = libusb_control_transfer(dediprog_handle[Index], requesttype, rq->Request, rq->Value, rq->Index, buf, buf_size, DEFAULT_TIMEOUT); } else { printf("no device"); @@ -201,7 +204,7 @@ int InCtrlRequest( CNTRPIPE_RQ *rq, unsigned char *buf, unsigned long buf_size, printf("Control Pipe input error!\n"); return -1; } - + return ret; } @@ -209,20 +212,20 @@ int InCtrlRequest( CNTRPIPE_RQ *rq, unsigned char *buf, unsigned long buf_size, // should be called after usb successfully opens pipes. int dediprog_start_appli(int Index) { - //IsSF600(Index); - CNTRPIPE_RQ rq ; - int ret; + //IsSF600(Index); + CNTRPIPE_RQ rq; + int ret; unsigned char vInstruction; // special instruction - vInstruction = 0 ; + vInstruction = 0; - rq.Function = URB_FUNCTION_VENDOR_OTHER ; - rq.Direction = VENDOR_DIRECTION_IN ; - rq.Request = 0xb ; - rq.Value = 0x00 ; - rq.Index = 0x00 ; - rq.Length = 0x01 ; + rq.Function = URB_FUNCTION_VENDOR_OTHER; + rq.Direction = VENDOR_DIRECTION_IN; + rq.Request = 0xb; + rq.Value = 0x00; + rq.Index = 0x00; + rq.Length = 0x01; ret = OutCtrlRequest(&rq, &vInstruction, 1, Index); @@ -231,36 +234,36 @@ int dediprog_start_appli(int Index) int dediprog_get_chipid(int Index) { - //IsSF600(Index); - CNTRPIPE_RQ rq ; - int ret; + //IsSF600(Index); + CNTRPIPE_RQ rq; + int ret; unsigned char vInstruction[3]; memset(vInstruction, 0, 3); - rq.Function = URB_FUNCTION_VENDOR_ENDPOINT ; - rq.Direction = VENDOR_DIRECTION_OUT ; - rq.Request = 0x1 ; - rq.Value = 0xff ; - if(Is_NewUSBCommand(Index)) - rq.Index = 0 ; - else - rq.Index = 0x1 ; + rq.Function = URB_FUNCTION_VENDOR_ENDPOINT; + rq.Direction = VENDOR_DIRECTION_OUT; + rq.Request = 0x1; + rq.Value = 0xff; + if (Is_NewUSBCommand(Index)) + rq.Index = 0; + else + rq.Index = 0x1; - rq.Length = 0x1 ; + rq.Length = 0x1; - vInstruction[0]= 0x9f; + vInstruction[0] = 0x9f; ret = OutCtrlRequest(&rq, vInstruction, 1, Index); // special instruction memset(vInstruction, 0, 3); - rq.Function = URB_FUNCTION_VENDOR_ENDPOINT ; - rq.Direction = VENDOR_DIRECTION_IN ; - rq.Request = 0x1 ; - rq.Value = 0x0 ; - rq.Index = 0x00 ; - rq.Length = 0x03 ; + rq.Function = URB_FUNCTION_VENDOR_ENDPOINT; + rq.Direction = VENDOR_DIRECTION_IN; + rq.Request = 0x1; + rq.Value = 0x0; + rq.Index = 0x00; + rq.Length = 0x03; ret = InCtrlRequest(&rq, vInstruction, 3, Index); return ret; @@ -269,176 +272,176 @@ int dediprog_get_chipid(int Index) // return size read // if fail , return -1 //long CUSB::BulkPipeRead(PBYTE pBuff, UINT sz, UINT timeOut) const -int BulkPipeRead(unsigned char *pBuff, unsigned int timeOut, int Index) +int BulkPipeRead(unsigned char* pBuff, unsigned int timeOut, int Index) { int ret, actual_length; if (Index == -1) Index = DevIndex; unsigned long cnRead = 512; - ret = libusb_bulk_transfer(dediprog_handle[Index], 2 | LIBUSB_ENDPOINT_IN, pBuff,cnRead, &actual_length, DEFAULT_TIMEOUT); + ret = libusb_bulk_transfer(dediprog_handle[Index], 2 | LIBUSB_ENDPOINT_IN, pBuff, cnRead, &actual_length, DEFAULT_TIMEOUT); cnRead = ret; - return cnRead ; + 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) +int BulkPipeWrite(unsigned char* pBuff, unsigned int size, unsigned int timeOut, int Index) { int ret, actual_length; - int nWrite = 512 ; + int nWrite = 512; unsigned char pData[512]; - memset(pData, 0xFF, 512); // fill buffer with 0xFF + memset(pData, 0xFF, 512); // fill buffer with 0xFF - memcpy(pData, pBuff , size); + memcpy(pData, pBuff, size); if (Index == -1) Index = DevIndex; - ret = libusb_bulk_transfer(dediprog_handle[Index], (g_bIsSF600[Index]==true)? 0x01:0x02,pData, nWrite, &actual_length, DEFAULT_TIMEOUT); + ret = libusb_bulk_transfer(dediprog_handle[Index], (g_bIsSF600[Index] == true) ? 0x01 : 0x02, pData, nWrite, &actual_length, DEFAULT_TIMEOUT); nWrite = ret; return nWrite; } -int dediprog_set_spi_voltage(int v,int Index) +int dediprog_set_spi_voltage(int v, int Index) { int ret = 0; -// int voltage_selector; - CNTRPIPE_RQ rq ; -// unsigned char vBuffer[12]; + // int voltage_selector; + CNTRPIPE_RQ rq; + // unsigned char vBuffer[12]; if (0 == v) Sleep(200); - rq.Function = URB_FUNCTION_VENDOR_ENDPOINT ; - rq.Direction = VENDOR_DIRECTION_OUT ; - rq.Request = SET_VCC ; - rq.Length = 0 ; - + rq.Function = URB_FUNCTION_VENDOR_ENDPOINT; + rq.Direction = VENDOR_DIRECTION_OUT; + rq.Request = SET_VCC; + rq.Length = 0; + if (Is_NewUSBCommand(Index)) { - rq.Value = v ; - rq.Index = 0; + rq.Value = v; + rq.Index = 0; } else { - rq.Value = v ; - rq.Index = 0x04 | g_CurrentSeriase; // ID detect mode - } - ret = OutCtrlRequest(&rq, NULL, 0, Index); + rq.Value = v; + rq.Index = 0x04 | g_CurrentSeriase; // ID detect mode + } + ret = OutCtrlRequest(&rq, NULL, 0, Index); if (0 != v) { - Sleep(200); + Sleep(200); if (isSendFFsequence) { - 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); + } + } - return ret; + return ret; } -int dediprog_set_vpp_voltage(int volt,int Index) +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. */ - voltage_selector = 0x0; - break; - case 9: - voltage_selector = 0x1; - break; - case 12: - voltage_selector = 0x2; - break; - default: - return 1; - } - - rq.Function = URB_FUNCTION_VENDOR_ENDPOINT ; - rq.Direction = VENDOR_DIRECTION_OUT ; - rq.Request = 0x03 ; - rq.Value = voltage_selector ; - rq.Index = 0 ; - rq.Length = 0x0 ; - - ret = OutCtrlRequest(&rq, NULL, 0, Index); - - if (ret == SerialFlash_FALSE) { -// printf("Command Set VPP Voltage 0x%x failed!\n", voltage_selector); - return 1; - } - return 0; + int ret; + int voltage_selector; + CNTRPIPE_RQ rq; + + switch (volt) { + case 0: + /* Admittedly this one is an assumption. */ + voltage_selector = 0x0; + break; + case 9: + voltage_selector = 0x1; + break; + case 12: + voltage_selector = 0x2; + break; + default: + return 1; + } + + rq.Function = URB_FUNCTION_VENDOR_ENDPOINT; + rq.Direction = VENDOR_DIRECTION_OUT; + rq.Request = 0x03; + rq.Value = voltage_selector; + rq.Index = 0; + rq.Length = 0x0; + + ret = OutCtrlRequest(&rq, NULL, 0, Index); + + if (ret == SerialFlash_FALSE) { + // printf("Command Set VPP Voltage 0x%x failed!\n", voltage_selector); + return 1; + } + return 0; } -int dediprog_set_spi_clk(int khz,int Index) +int dediprog_set_spi_clk(int khz, int Index) { return 0; - int ret; - int hz_selector; - //IsSF600(Index); - CNTRPIPE_RQ rq ; - - switch (khz) { - case 24000: - /* Admittedly this one is an assumption. */ - hz_selector = 0x0; - break; - case 8000: - hz_selector = 0x1; - break; - case 12000: - hz_selector = 0x2; - break; - case 3000: - hz_selector = 0x3; - break; - case 2180: - hz_selector = 0x4; - break; - case 1500: - hz_selector = 0x5; - break; - case 750: - hz_selector = 0x6; - break; - case 375: - hz_selector = 0x7; - break; - default: - printf("Unknown clk %i KHz! Aborting.\n", khz); - return 1; - } -// printf("Setting SPI clk to %u.%03u MHz\n", khz / 1000, -// khz % 1000); - - rq.Function = URB_FUNCTION_VENDOR_ENDPOINT ; - rq.Direction = VENDOR_DIRECTION_OUT ; - rq.Request = 0x61 ; - rq.Value = hz_selector ; - rq.Index = 0 ; - rq.Length = 0x0 ; - - ret = OutCtrlRequest(&rq, NULL, 0, Index); - - if (ret == SerialFlash_FALSE) { -// printf("Command Set SPI clk 0x%x failed!\n", hz_selector); - return 1; - } - return 0; + int ret; + int hz_selector; + //IsSF600(Index); + CNTRPIPE_RQ rq; + + switch (khz) { + case 24000: + /* Admittedly this one is an assumption. */ + hz_selector = 0x0; + break; + case 8000: + hz_selector = 0x1; + break; + case 12000: + hz_selector = 0x2; + break; + case 3000: + hz_selector = 0x3; + break; + case 2180: + hz_selector = 0x4; + break; + case 1500: + hz_selector = 0x5; + break; + case 750: + hz_selector = 0x6; + break; + case 375: + hz_selector = 0x7; + break; + default: + printf("Unknown clk %i KHz! Aborting.\n", khz); + return 1; + } + // printf("Setting SPI clk to %u.%03u MHz\n", khz / 1000, + // khz % 1000); + + rq.Function = URB_FUNCTION_VENDOR_ENDPOINT; + rq.Direction = VENDOR_DIRECTION_OUT; + rq.Request = 0x61; + rq.Value = hz_selector; + rq.Index = 0; + rq.Length = 0x0; + + ret = OutCtrlRequest(&rq, NULL, 0, Index); + + if (ret == SerialFlash_FALSE) { + // printf("Command Set SPI clk 0x%x failed!\n", hz_selector); + return 1; + } + return 0; } int usb_driver_init(void) { - // struct usb_bus *bus; - // struct usb_device *dev; + // struct usb_bus *bus; + // struct usb_device *dev; bool result = false; int device_cnt = 0; - int ret; + int ret; for (int i = 0; i < MAX_Dev_Index; i++) { - dediprog_handle[i]=NULL; + dediprog_handle[i] = NULL; } usb_dev_init(); @@ -447,75 +450,76 @@ int usb_driver_init(void) if (g_uiDevNum == 0) { for (int i = 0; i < device_cnt; i++) { if (usb_device_entry[i].valid == 0) { - printf("Error: Programmers are not connected.\n"); - return 0; - } - - ret = libusb_open(usb_device_entry[i].usb_device, &dediprog_handle[i]); + printf("Error: Programmers are not connected.\n"); + return 0; + } + + ret = libusb_open(usb_device_entry[i].usb_device, &dediprog_handle[i]); if (dediprog_handle[i] == NULL) { - printf("Error: Programmers are not connected.\n"); - return 0; - } - ret = libusb_set_configuration(dediprog_handle[i], 1); - + printf("Error: Programmers are not connected.\n"); + return 0; + } + ret = libusb_set_configuration(dediprog_handle[i], 1); + if (ret) { - printf("Error: Programmers USB set configuration: 0x%x.\n",ret); - return 0; - } - ret = libusb_claim_interface(dediprog_handle[i], 0); + printf("Error: Programmers USB set configuration: 0x%x.\n", ret); + return 0; + } + ret = libusb_claim_interface(dediprog_handle[i], 0); if (ret) { - printf("Error: Programmers USB claim interface: 0x%x.\n",ret); - return 0; - } - g_bIsSF600[i]=false; - - IsSF600(i); - dediprog_start_appli(i); - IsSF600(i); - result = (dediprog_handle[i] != NULL); - } + printf("Error: Programmers USB claim interface: 0x%x.\n", ret); + return 0; + } + g_bIsSF600[i] = false; + + IsSF600(i); + dediprog_start_appli(i); + IsSF600(i); + result = (dediprog_handle[i] != NULL); + } } else { if (usb_device_entry[g_uiDevNum - 1].valid == 0) { - printf("Error: Programmers are not connected.\n"); - return 0; - } - - ret = libusb_open(usb_device_entry[g_uiDevNum-1].usb_device, &dediprog_handle[g_uiDevNum-1]); + printf("Error: Programmers are not connected.\n"); + return 0; + } + + ret = libusb_open(usb_device_entry[g_uiDevNum - 1].usb_device, &dediprog_handle[g_uiDevNum - 1]); if (dediprog_handle[g_uiDevNum - 1] == NULL) { - printf("Error: Programmers are not connected.\n"); - return 0; - } + printf("Error: Programmers are not connected.\n"); + return 0; + } printf("dediprog_handle[%d]=%p\n", g_uiDevNum - 1, dediprog_handle[g_uiDevNum - 1]); - ret = libusb_set_configuration(dediprog_handle[g_uiDevNum-1], 1); - + ret = libusb_set_configuration(dediprog_handle[g_uiDevNum - 1], 1); + if (ret) { - printf("Error: Programmers USB set configuration: 0x%x.\n",ret); - return 0; - } - ret = libusb_claim_interface(dediprog_handle[g_uiDevNum-1], 0); + printf("Error: Programmers USB set configuration: 0x%x.\n", ret); + return 0; + } + ret = libusb_claim_interface(dediprog_handle[g_uiDevNum - 1], 0); if (ret) { - printf("Error: Programmers USB claim interface: 0x%x.\n",ret); - return 0; - } - g_bIsSF600[g_uiDevNum-1]=false; - - IsSF600(g_uiDevNum-1); - dediprog_start_appli(g_uiDevNum-1); - IsSF600(g_uiDevNum-1); - result = (dediprog_handle[g_uiDevNum-1] != NULL); -} - - return result;//((dediprog_handle[i] != NULL)? 1:0); + printf("Error: Programmers USB claim interface: 0x%x.\n", ret); + return 0; + } + g_bIsSF600[g_uiDevNum - 1] = false; + + IsSF600(g_uiDevNum - 1); + dediprog_start_appli(g_uiDevNum - 1); + IsSF600(g_uiDevNum - 1); + result = (dediprog_handle[g_uiDevNum - 1] != NULL); + } + + return result; //((dediprog_handle[i] != NULL)? 1:0); } int usb_driver_release(void) { - for (int i = 0; i < dev_index; i++) { - if(dediprog_handle[i]==NULL) return 0; - libusb_release_interface(dediprog_handle[i], 0); - libusb_close (dediprog_handle[i]); - } - return 0; + for (int i = 0; i < dev_index; i++) { + if (dediprog_handle[i] == NULL) + return 0; + libusb_release_interface(dediprog_handle[i], 0); + libusb_close(dediprog_handle[i]); + } + return 0; } #if 0 //Simon: unit test code @@ -555,61 +559,61 @@ int usb_driver_test(void) bool Is_usbworking(int Index) { usleep(1000); // unknow reson - return ((dediprog_handle[Index] != NULL)? true:false); + return ((dediprog_handle[Index] != NULL) ? true : false); } //long long flash_ReadId(boost::tuple command,int Index) -long flash_ReadId(unsigned int read_id_code, unsigned int out_data_size ,int Index) +long flash_ReadId(unsigned int read_id_code, unsigned int out_data_size, int Index) { // read status -// if(! m_usb.is_open() ) -// return 0 ; + // if(! m_usb.is_open() ) + // return 0 ; // RDID is not decoded when in DP mode // so , first release from Deep Power Down mode or read signature // DoRDP() ; // send request - CNTRPIPE_RQ rq ; + CNTRPIPE_RQ rq; unsigned char vInstruction[8]; unsigned long rc = 0; int i; // first control packet vInstruction[0] = read_id_code; - rq.Function = URB_FUNCTION_VENDOR_ENDPOINT ; - rq.Direction = VENDOR_DIRECTION_OUT ; - rq.Request = TRANSCEIVE ; + rq.Function = URB_FUNCTION_VENDOR_ENDPOINT; + rq.Direction = VENDOR_DIRECTION_OUT; + rq.Request = TRANSCEIVE; if (Is_NewUSBCommand(Index)) { - rq.Value = RESULT_IN ; - rq.Index = 0 ; + rq.Value = RESULT_IN; + rq.Index = 0; } else { - rq.Value = RFU ; - rq.Index = RESULT_IN ; - } + rq.Value = RFU; + rq.Index = RESULT_IN; + } rq.Length = 1; - if(OutCtrlRequest( &rq, vInstruction, (unsigned long)1 , Index ) == SerialFlash_FALSE ) - return rc; //OutCtrlRequest() return error + if (OutCtrlRequest(&rq, vInstruction, (unsigned long)1, Index) == SerialFlash_FALSE) + return rc; //OutCtrlRequest() return error // second control packet : fetch data memset(vInstruction, 0, sizeof(vInstruction)); - rq.Function = URB_FUNCTION_VENDOR_ENDPOINT ; - rq.Direction = VENDOR_DIRECTION_IN ; - rq.Request = TRANSCEIVE ; + rq.Function = URB_FUNCTION_VENDOR_ENDPOINT; + rq.Direction = VENDOR_DIRECTION_IN; + rq.Request = TRANSCEIVE; if (Is_NewUSBCommand(Index)) { - rq.Value = 0x01 ; - rq.Index = 0 ; + rq.Value = 0x01; + rq.Index = 0; } else { - rq.Value = CTRL_TIMEOUT ; - rq.Index = NO_REGISTER ; - } - rq.Length = out_data_size ; + rq.Value = CTRL_TIMEOUT; + rq.Index = NO_REGISTER; + } + rq.Length = out_data_size; - if(InCtrlRequest(&rq, vInstruction, (unsigned long)out_data_size, Index)==SerialFlash_FALSE) + if (InCtrlRequest(&rq, vInstruction, (unsigned long)out_data_size, Index) == SerialFlash_FALSE) return rc; - for(i=0; i -#include #include #include +#include +#include + +#define URB_FUNCTION_VENDOR_DEVICE 0x0017 +#define URB_FUNCTION_VENDOR_INTERFACE 0x0018 +#define URB_FUNCTION_VENDOR_ENDPOINT 0x0019 +#define URB_FUNCTION_VENDOR_OTHER 0x0020 -#define URB_FUNCTION_VENDOR_DEVICE 0x0017 -#define URB_FUNCTION_VENDOR_INTERFACE 0x0018 -#define URB_FUNCTION_VENDOR_ENDPOINT 0x0019 -#define URB_FUNCTION_VENDOR_OTHER 0x0020 +#define URB_FUNCTION_CLASS_DEVICE 0x001A +#define URB_FUNCTION_CLASS_INTERFACE 0x001B +#define URB_FUNCTION_CLASS_ENDPOINT 0x001C +#define URB_FUNCTION_CLASS_OTHER 0x001F -#define URB_FUNCTION_CLASS_DEVICE 0x001A -#define URB_FUNCTION_CLASS_INTERFACE 0x001B -#define URB_FUNCTION_CLASS_ENDPOINT 0x001C -#define URB_FUNCTION_CLASS_OTHER 0x001F +#define PIPE_RESET 1 +#define ABORT_TRANSFER 0 -#define PIPE_RESET 1 -#define ABORT_TRANSFER 0 +#define VENDOR_DIRECTION_IN 1 +#define VENDOR_DIRECTION_OUT 0 -#define VENDOR_DIRECTION_IN 1 -#define VENDOR_DIRECTION_OUT 0 +#define DEFAULT_TIMEOUT 30000 -#define DEFAULT_TIMEOUT 30000 - -#define MAX_Dev_Index 16 +#define MAX_Dev_Index 16 bool Is_usbworking(int Index); typedef struct usb_device_entry { - libusb_device *usb_device; + libusb_device* usb_device; int valid; -}usb_device_entry_t; +} usb_device_entry_t; typedef struct { unsigned short Function; @@ -45,28 +45,28 @@ typedef struct { } CNTRPIPE_RQ, *PCNTRPIPE_RQ; /* Set/clear LEDs on dediprog */ -#define PASS_ON (0 << 0) -#define PASS_OFF (1 << 0) -#define BUSY_ON (0 << 1) -#define BUSY_OFF (1 << 1) -#define ERROR_ON (0 << 2) -#define ERROR_OFF (1 << 2) - -int usb_driver_init(void); +#define PASS_ON (0 << 0) +#define PASS_OFF (1 << 0) +#define BUSY_ON (0 << 1) +#define BUSY_OFF (1 << 1) +#define ERROR_ON (0 << 2) +#define ERROR_OFF (1 << 2) + +int usb_driver_init(void); int get_usb_dev_cnt(void); int usb_driver_release(void); -int OutCtrlRequest( CNTRPIPE_RQ *rq, unsigned char *buf, unsigned long buf_size ,int Index ); +int OutCtrlRequest(CNTRPIPE_RQ* rq, unsigned char* buf, unsigned long buf_size, int Index); -int InCtrlRequest( CNTRPIPE_RQ *rq, unsigned char *buf, unsigned long buf_size, int Index ); +int InCtrlRequest(CNTRPIPE_RQ* rq, unsigned char* buf, unsigned long buf_size, int Index); -int BulkPipeRead(unsigned char *pBuff, unsigned int timeOut, int Index); +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); +int dediprog_set_vpp_voltage(int volt, int Index); -long flash_ReadId(unsigned int read_id_code, unsigned int out_data_size ,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); +int BulkPipeWrite(unsigned char* pBuff, unsigned int size, unsigned int timeOut, int Index); #endif //DEDI_USB_DRIVER