From 14d0fa4556e81763fd2a1e8fe4870a746186595e Mon Sep 17 00:00:00 2001 From: Attila Veghelyi Date: Wed, 16 Oct 2024 16:09:02 +0200 Subject: Use common functions from one file - not works yet --- Makefile_op | 1 + Makefile_opgui | 1 + common_functions.c | 820 +++++++++++++++++++++++++++++++++++++++++++++++++++++ common_functions.h | 17 ++ op.c | 795 +-------------------------------------------------- opgui.c | 792 +-------------------------------------------------- 6 files changed, 854 insertions(+), 1572 deletions(-) create mode 100644 common_functions.c create mode 100644 common_functions.h diff --git a/Makefile_op b/Makefile_op index 5fff89a..792653f 100644 --- a/Makefile_op +++ b/Makefile_op @@ -5,6 +5,7 @@ CFLAGS = '-DVERSION="$(VERSION)"' -w -Os -s #size #CFLAGS = -w -O3 -s #CFLAGS = -w -g #debug SOURCES = op.c \ + common_functions.c \ progP12.c \ progP16.c \ progP18.c \ diff --git a/Makefile_opgui b/Makefile_opgui index cd0f5a3..3ec07f3 100644 --- a/Makefile_opgui +++ b/Makefile_opgui @@ -12,6 +12,7 @@ CFLAGS += -Os -s #size LDLAGS = `pkg-config --libs gtk+-3.0` SOURCES = opgui.c \ + common_functions.c \ deviceRW.c \ progP12.c \ progP16.c \ diff --git a/common_functions.c b/common_functions.c new file mode 100644 index 0000000..388b4b0 --- /dev/null +++ b/common_functions.c @@ -0,0 +1,820 @@ +#include "common.h" +#include "common_functions.h" +#include "I2CSPI.h" +#include "deviceRW.h" +#include "fileIO.h" +#include "progAVR.h" + + +#define FALSE 0 +#define TRUE (!0) +#define MinDly 0 + + +void ProgID(); +int CheckS1(); +char** strings; //!localized strings +int saveLog=0,programID=0,load_osccal=0,load_BKosccal=0; +int use_osccal=1,use_BKosccal=0; +int load_calibword=0,max_err=200; +int AVRlock=0x100,AVRfuse=0x100,AVRfuse_h=0x100,AVRfuse_x=0x100; +int ICDenable=0,ICDaddr=0x1FF0; +int FWVersion=0,HwID=0; +FILE* logfile=0; +char LogFileName[512]=""; +char loadfile[512]="",savefile[512]=""; +char CoffFileName[512]=""; +WORD *memCODE_W=0; +int size=0,sizeW=0,sizeEE=0,sizeCONFIG=0,sizeUSERID=0; +unsigned char *memCODE=0,*memEE=0,memID[64],memCONFIG[48],memUSERID[8]; +double hvreg=0; +int RWstop=0; +int forceConfig=0; +char str[4096]; + +void printMsg(const char* msg) +{ +#ifdef OPGUI + MsgBox(msg); +#else + PrintMessage(msg); + fflush(stdout); + getchar(); +#endif +} + +#if !defined _WIN32 && !defined __CYGWIN__ //Linux + int fd = -1; + #if defined hiddevIO && defined OPGUI + struct hiddev_report_info rep_info_i,rep_info_u; + struct hiddev_usage_ref_multi ref_multi_i,ref_multi_u; + #endif + unsigned char bufferU[128],bufferI[128]; +#else //Windows + unsigned char bufferU0[128],bufferI0[128]; + unsigned char *bufferU,*bufferI; + DWORD NumberOfBytesRead,BytesWritten; + ULONG Result; + HANDLE WriteHandle,ReadHandle; + OVERLAPPED HIDOverlapped; + HANDLE hEventObject; +#endif + +/// +///Start HV regulator +int StartHVReg(double V){ + int j=0,z; + int vreg=(int)(V*10.0); + if(saveLog&&logfile) fprintf(logfile,"StartHVReg(%.2f)\n",V); + DWORD t0,t; + if(V==-1){ + bufferU[j++]=VREG_DIS; //disable HV regulator + bufferU[j++]=FLUSH; + PacketIO(5); + msDelay(40); + return -1; + } + t=t0=GetTickCount(); + bufferU[j++]=VREG_EN; //enable HV regulator + bufferU[j++]=SET_VPP; + bufferU[j++]=vreg; + bufferU[j++]=SET_PARAMETER; + bufferU[j++]=SET_T3; + bufferU[j++]=2000>>8; + bufferU[j++]=2000&0xff; + bufferU[j++]=WAIT_T3; + bufferU[j++]=READ_ADC; + bufferU[j++]=FLUSH; + for(;j(vreg/10.0+1)*G)&&t>=2; //if 12 bit ADC +// PrintMessage2("v=%d=%fV\n",v,v/G); + } + if(v>(vreg/10.0+1)*G){ + PrintMessage(strings[S_HiVPP]); //"Attenzione: tensione regolatore troppo alta\r\n\r\n" + return 0; + } + else if(v<(vreg/10.0-1)*G){ + PrintMessage(strings[S_LowVPP]); //"Attenzione: tensione regolatore troppo bassa\r\n\r\n" + return 0; + } + else if(v==0){ + PrintMessage(strings[S_lowUsbV]); //"Tensione USB troppo bassa (VUSB<4.5V)\r\n" + return 0; + } + else{ + PrintMessage2(strings[S_reg],t-t0,v/G); //"Regolatore avviato e funzionante dopo T=%d ms VPP=%.1f\r\n\r\n" + if(saveLog&&logfile) fprintf(logfile,strings[S_reg],t-t0,v/G); + return vreg; + } +} + +/// +///Read programmer ID +void ProgID() +{ + if(DeviceDetected!=1) return; + int j=0; + bufferU[j++]=PROG_RST; + bufferU[j++]=FLUSH; + for(;j E3=1 (active low) + } +} + +/// +///Execute hardware test +#ifdef OPGUI +void TestHw(GtkWidget *widget,GtkWindow* parent) +#else +void TestHw(void) +#endif +{ +#ifndef DEBUG + if(DeviceDetected!=1) return; +#endif + char str[256]; + StartHVReg(13); + int j=0; + printMsg(strings[I_TestHW]); //"Test hardware ..." + bufferU[j++]=SET_CK_D; + bufferU[j++]=0x0; + bufferU[j++]=EN_VPP_VCC; //VDD+VPP + bufferU[j++]=0x5; + bufferU[j++]=FLUSH; + for(;j=0x900){ //IO test + int j=0,i,x,r; + strcpy(str,"0000000000000"); + PrintMessage("IO test\nRC|RA|--RB--|\n"); + for(i=0;i<13;i++){ + x=1<>5)&0xFF; //PORTA-C + bufferU[j++]=READ_B; + bufferU[j++]=READ_AC; + bufferU[j++]=FLUSH; + for(;jMinDly?x:MinDly); +#else +// Sleep((long)ceil(delay)>MinDly?(long)ceil(delay):MinDly); + __int64 stop,freq,timeout; + QueryPerformanceCounter((LARGE_INTEGER *)&stop); + QueryPerformanceFrequency((LARGE_INTEGER *)&freq); + timeout=stop+delay*freq/1000.0; + while(stop0){ //try with this first + sprintf(path,"/dev/openprogrammer"); + ioctl(fd, HIDIOCGRAWINFO, &device_info); + if(device_info.vendor==vid&&device_info.product==pid) i=0; + else{ + close(fd); + i=-1; + } + } + if(i){ + for(i=0;i<16;i++){ + sprintf(path,"/dev/hidraw%d",i); + if((fd = open(path, O_RDWR|O_NONBLOCK))>0){ + ioctl(fd, HIDIOCGRAWINFO, &device_info); + if(device_info.vendor==vid&&device_info.product==pid) break; + else close(fd); + } + } + } + if(i==16){ + PrintMessage(strings[S_noprog]); + path[0]=0; + return 0; + } + } + else{ //user supplied path + if((fd = open(path, O_RDWR|O_NONBLOCK)) < 0) { + PrintMessage1(strings[S_DevPermission],path); //"cannot open %s, make sure you have read permission on it",path); + return 0; + } + ioctl(fd, HIDIOCGRAWINFO, &device_info); + if(device_info.vendor!=vid||device_info.product!=pid){ + PrintMessage(strings[S_noprog]); + return 0; + } + } + printf(strings[S_progDev],path); + return 1; + #else //use hiddev device (old method) + struct hiddev_devinfo device_info; + int i=-1; + if(path[0]==0){ //search all devices + if((fd = open("/dev/openprogrammer", O_RDONLY ))>0){ //try with this first + ioctl(fd, HIDIOCGDEVINFO, &device_info); + if(device_info.vendor==vid&&device_info.product==pid) i=0; + else{ + close(fd); + i=-1; + } + } + if(i){ + for(i=0;i<16;i++){ + sprintf(path,"/dev/usb/hiddev%d",i); + if((fd = open(path, O_RDONLY ))>0){ + ioctl(fd, HIDIOCGDEVINFO, &device_info); + if(device_info.vendor==vid&&device_info.product==pid) break; + else close(fd); + } + } + } + if(i==16){ + PrintMessage(strings[S_noprog]); + path[0]=0; + return 0; + } + } + else{ //user supplied path + if ((fd = open(path, O_RDONLY )) < 0) { + PrintMessage1(strings[S_DevPermission],path); //"cannot open %s, make sure you have read permission on it",path); + return 0; + } + ioctl(fd, HIDIOCGDEVINFO, &device_info); + if(device_info.vendor!=vid||device_info.product!=pid){ + PrintMessage(strings[S_noprog]); + return 0; + } + } + #ifndef OPGUI + printf(strings[S_progDev],path); + #endif + MyDeviceDetected = TRUE; + rep_info_u.report_type=HID_REPORT_TYPE_OUTPUT; + rep_info_i.report_type=HID_REPORT_TYPE_INPUT; + rep_info_u.report_id=rep_info_i.report_id=HID_REPORT_ID_FIRST; + rep_info_u.num_fields=rep_info_i.num_fields=1; + ref_multi_u.uref.report_type=HID_REPORT_TYPE_OUTPUT; + ref_multi_i.uref.report_type=HID_REPORT_TYPE_INPUT; + ref_multi_u.uref.report_id=ref_multi_i.uref.report_id=HID_REPORT_ID_FIRST; + ref_multi_u.uref.field_index=ref_multi_i.uref.field_index=0; + ref_multi_u.uref.usage_index=ref_multi_i.uref.usage_index=0; + ref_multi_u.num_values=ref_multi_i.num_values=DIMBUF; + #endif +#else //Windows + PSP_DEVICE_INTERFACE_DETAIL_DATA detailData; + HANDLE DeviceHandle; + HANDLE hDevInfo; + GUID HidGuid; + char MyDevicePathName[1024]; + ULONG Length; + ULONG Required; + typedef struct _HIDD_ATTRIBUTES { + ULONG Size; + USHORT VendorID; + USHORT ProductID; + USHORT VersionNumber; + } HIDD_ATTRIBUTES, *PHIDD_ATTRIBUTES; + typedef void (__stdcall*GETHIDGUID) (OUT LPGUID HidGuid); + typedef BOOLEAN (__stdcall*GETATTRIBUTES)(IN HANDLE HidDeviceObject,OUT PHIDD_ATTRIBUTES Attributes); + typedef BOOLEAN (__stdcall*SETNUMINPUTBUFFERS)(IN HANDLE HidDeviceObject,OUT ULONG NumberBuffers); + typedef BOOLEAN (__stdcall*GETNUMINPUTBUFFERS)(IN HANDLE HidDeviceObject,OUT PULONG NumberBuffers); + typedef BOOLEAN (__stdcall*GETFEATURE) (IN HANDLE HidDeviceObject, OUT PVOID ReportBuffer, IN ULONG ReportBufferLength); + typedef BOOLEAN (__stdcall*SETFEATURE) (IN HANDLE HidDeviceObject, IN PVOID ReportBuffer, IN ULONG ReportBufferLength); + typedef BOOLEAN (__stdcall*GETREPORT) (IN HANDLE HidDeviceObject, OUT PVOID ReportBuffer, IN ULONG ReportBufferLength); + typedef BOOLEAN (__stdcall*SETREPORT) (IN HANDLE HidDeviceObject, IN PVOID ReportBuffer, IN ULONG ReportBufferLength); + typedef BOOLEAN (__stdcall*GETMANUFACTURERSTRING) (IN HANDLE HidDeviceObject, OUT PVOID ReportBuffer, IN ULONG ReportBufferLength); + typedef BOOLEAN (__stdcall*GETPRODUCTSTRING) (IN HANDLE HidDeviceObject, OUT PVOID ReportBuffer, IN ULONG ReportBufferLength); + typedef BOOLEAN (__stdcall*GETINDEXEDSTRING) (IN HANDLE HidDeviceObject, IN ULONG StringIndex, OUT PVOID ReportBuffer, IN ULONG ReportBufferLength); + HIDD_ATTRIBUTES Attributes; + SP_DEVICE_INTERFACE_DATA devInfoData; + int LastDevice = FALSE; + int MemberIndex = 0; + LONG Result; + Length=0; + detailData=NULL; + DeviceHandle=NULL; + HMODULE hHID=0; + GETHIDGUID HidD_GetHidGuid=0; + GETATTRIBUTES HidD_GetAttributes=0; + SETNUMINPUTBUFFERS HidD_SetNumInputBuffers=0; + GETNUMINPUTBUFFERS HidD_GetNumInputBuffers=0; + GETFEATURE HidD_GetFeature=0; + SETFEATURE HidD_SetFeature=0; + GETREPORT HidD_GetInputReport=0; + SETREPORT HidD_SetOutputReport=0; + GETMANUFACTURERSTRING HidD_GetManufacturerString=0; + GETPRODUCTSTRING HidD_GetProductString=0; + hHID = LoadLibrary("hid.dll"); + if(!hHID){ + PrintMessage("Can't find hid.dll"); + return 0; + } + HidD_GetHidGuid=(GETHIDGUID)GetProcAddress(hHID,"HidD_GetHidGuid"); + HidD_GetAttributes=(GETATTRIBUTES)GetProcAddress(hHID,"HidD_GetAttributes"); + HidD_SetNumInputBuffers=(SETNUMINPUTBUFFERS)GetProcAddress(hHID,"HidD_SetNumInputBuffers"); + HidD_GetNumInputBuffers=(GETNUMINPUTBUFFERS)GetProcAddress(hHID,"HidD_GetNumInputBuffers"); + HidD_GetFeature=(GETFEATURE)GetProcAddress(hHID,"HidD_GetFeature"); + HidD_SetFeature=(SETFEATURE)GetProcAddress(hHID,"HidD_SetFeature"); + HidD_GetInputReport=(GETREPORT)GetProcAddress(hHID,"HidD_GetInputReport"); + HidD_SetOutputReport=(SETREPORT)GetProcAddress(hHID,"HidD_SetOutputReport"); + HidD_GetManufacturerString=(GETMANUFACTURERSTRING)GetProcAddress(hHID,"HidD_GetManufacturerString"); + HidD_GetProductString=(GETPRODUCTSTRING)GetProcAddress(hHID,"HidD_GetProductString"); + if(HidD_GetHidGuid==NULL\ + ||HidD_GetAttributes==NULL\ + ||HidD_GetFeature==NULL\ + ||HidD_SetFeature==NULL\ + ||HidD_GetInputReport==NULL\ + ||HidD_SetOutputReport==NULL\ + ||HidD_GetManufacturerString==NULL\ + ||HidD_GetProductString==NULL\ + ||HidD_SetNumInputBuffers==NULL\ + ||HidD_GetNumInputBuffers==NULL) return 0; + HMODULE hSAPI=0; + hSAPI = LoadLibrary("setupapi.dll"); + if(!hSAPI){ + PrintMessage("Can't find setupapi.dll"); + return 0; + } + typedef HDEVINFO (WINAPI* SETUPDIGETCLASSDEVS) (CONST GUID*,PCSTR,HWND,DWORD); + typedef BOOL (WINAPI* SETUPDIENUMDEVICEINTERFACES) (HDEVINFO,PSP_DEVINFO_DATA,CONST GUID*,DWORD,PSP_DEVICE_INTERFACE_DATA); + typedef BOOL (WINAPI* SETUPDIGETDEVICEINTERFACEDETAIL) (HDEVINFO,PSP_DEVICE_INTERFACE_DATA,PSP_DEVICE_INTERFACE_DETAIL_DATA_A,DWORD,PDWORD,PSP_DEVINFO_DATA); + typedef BOOL (WINAPI* SETUPDIDESTROYDEVICEINFOLIST) (HDEVINFO); + SETUPDIGETCLASSDEVS SetupDiGetClassDevsA=0; + SETUPDIENUMDEVICEINTERFACES SetupDiEnumDeviceInterfaces=0; + SETUPDIGETDEVICEINTERFACEDETAIL SetupDiGetDeviceInterfaceDetailA=0; + SETUPDIDESTROYDEVICEINFOLIST SetupDiDestroyDeviceInfoList=0; + SetupDiGetClassDevsA=(SETUPDIGETCLASSDEVS) GetProcAddress(hSAPI,"SetupDiGetClassDevsA"); + SetupDiEnumDeviceInterfaces=(SETUPDIENUMDEVICEINTERFACES) GetProcAddress(hSAPI,"SetupDiEnumDeviceInterfaces"); + SetupDiGetDeviceInterfaceDetailA=(SETUPDIGETDEVICEINTERFACEDETAIL) GetProcAddress(hSAPI,"SetupDiGetDeviceInterfaceDetailA"); + SetupDiDestroyDeviceInfoList=(SETUPDIDESTROYDEVICEINFOLIST) GetProcAddress(hSAPI,"SetupDiDestroyDeviceInfoList"); + if(SetupDiGetClassDevsA==NULL\ + ||SetupDiEnumDeviceInterfaces==NULL\ + ||SetupDiDestroyDeviceInfoList==NULL\ + ||SetupDiGetDeviceInterfaceDetailA==NULL) return 0; + /* + The following code is adapted from Usbhidio_vc6 application example by Jan Axelson + for more information see see http://www.lvr.com/hidpage.htm + */ + /* + API function: HidD_GetHidGuid + Get the GUID for all system HIDs. + Returns: the GUID in HidGuid. + */ + HidD_GetHidGuid(&HidGuid); + /* + API function: SetupDiGetClassDevs + Returns: a handle to a device information set for all installed devices. + Requires: the GUID returned by GetHidGuid. + */ + hDevInfo=SetupDiGetClassDevs(&HidGuid,NULL,NULL,DIGCF_PRESENT|DIGCF_INTERFACEDEVICE); + devInfoData.cbSize = sizeof(devInfoData); + //Step through the available devices looking for the one we want. + //Quit on detecting the desired device or checking all available devices without success. + MemberIndex = 0; + LastDevice = FALSE; + do + { + /* + API function: SetupDiEnumDeviceInterfaces + On return, MyDeviceInterfaceData contains the handle to a + SP_DEVICE_INTERFACE_DATA structure for a detected device. + Requires: + The DeviceInfoSet returned in SetupDiGetClassDevs. + The HidGuid returned in GetHidGuid. + An index to specify a device. + */ + Result=SetupDiEnumDeviceInterfaces (hDevInfo, 0, &HidGuid, MemberIndex, &devInfoData); + if (Result != 0) + { + //A device has been detected, so get more information about it. + /* + API function: SetupDiGetDeviceInterfaceDetail + Returns: an SP_DEVICE_INTERFACE_DETAIL_DATA structure + containing information about a device. + To retrieve the information, call this function twice. + The first time returns the size of the structure in Length. + The second time returns a pointer to the data in DeviceInfoSet. + Requires: + A DeviceInfoSet returned by SetupDiGetClassDevs + The SP_DEVICE_INTERFACE_DATA structure returned by SetupDiEnumDeviceInterfaces. + The final parameter is an optional pointer to an SP_DEV_INFO_DATA structure. + This application doesn't retrieve or use the structure. + If retrieving the structure, set + MyDeviceInfoData.cbSize = length of MyDeviceInfoData. + and pass the structure's address. + */ + //Get the Length value. + //The call will return with a "buffer too small" error which can be ignored. + Result = SetupDiGetDeviceInterfaceDetail(hDevInfo, &devInfoData, NULL, 0, &Length, NULL); + //Allocate memory for the hDevInfo structure, using the returned Length. + detailData = (PSP_DEVICE_INTERFACE_DETAIL_DATA)malloc(Length); + //Set cbSize in the detailData structure. + detailData -> cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA); + //Call the function again, this time passing it the returned buffer size. + Result = SetupDiGetDeviceInterfaceDetail(hDevInfo, &devInfoData, detailData, Length,&Required, NULL); + // Open a handle to the device. + // To enable retrieving information about a system mouse or keyboard, + // don't request Read or Write access for this handle. + /* + API function: CreateFile + Returns: a handle that enables reading and writing to the device. + Requires: + The DevicePath in the detailData structure + returned by SetupDiGetDeviceInterfaceDetail. + */ + DeviceHandle=CreateFile(detailData->DevicePath, + 0, FILE_SHARE_READ|FILE_SHARE_WRITE, + (LPSECURITY_ATTRIBUTES)NULL,OPEN_EXISTING, 0, NULL); + /* + API function: HidD_GetAttributes + Requests information from the device. + Requires: the handle returned by CreateFile. + Returns: a HIDD_ATTRIBUTES structure containing + the Vendor ID, Product ID, and Product Version Number. + Use this information to decide if the detected device is + the one we're looking for. + */ + //Set the Size to the number of bytes in the structure. + Attributes.Size = sizeof(Attributes); + Result = HidD_GetAttributes(DeviceHandle,&Attributes); + //Is it the desired device? + MyDeviceDetected = FALSE; + if (Attributes.VendorID == vid) + { + if (Attributes.ProductID == pid) + { + //Both the Vendor ID and Product ID match. + MyDeviceDetected = TRUE; + strcpy(MyDevicePathName,detailData->DevicePath); + // Get a handle for writing Output reports. + WriteHandle=CreateFile(detailData->DevicePath, + GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE, + (LPSECURITY_ATTRIBUTES)NULL,OPEN_EXISTING,0,NULL); + //Get a handle to the device for the overlapped ReadFiles. + ReadHandle=CreateFile(detailData->DevicePath, + GENERIC_READ,FILE_SHARE_READ|FILE_SHARE_WRITE,(LPSECURITY_ATTRIBUTES)NULL, + OPEN_EXISTING,FILE_FLAG_OVERLAPPED,NULL); + if (hEventObject) CloseHandle(hEventObject); + hEventObject = CreateEvent(NULL,TRUE,TRUE,""); + //Set the members of the overlapped structure. + HIDOverlapped.hEvent = hEventObject; + HIDOverlapped.Offset = 0; + HIDOverlapped.OffsetHigh = 0; + Result=HidD_SetNumInputBuffers(DeviceHandle,64); + } + else + //The Product ID doesn't match. + CloseHandle(DeviceHandle); + } + else + //The Vendor ID doesn't match. + CloseHandle(DeviceHandle); + //Free the memory used by the detailData structure (no longer needed). + free(detailData); + } + else + //SetupDiEnumDeviceInterfaces returned 0, so there are no more devices to check. + LastDevice=TRUE; + //If we haven't found the device yet, and haven't tried every available device, + //try the next one. + MemberIndex = MemberIndex + 1; + } //do + while ((LastDevice == FALSE) && (MyDeviceDetected == FALSE)); + //Free the memory reserved for hDevInfo by SetupDiClassDevs. + SetupDiDestroyDeviceInfoList(hDevInfo); + + #ifndef OPGUI + if(info&&MyDeviceDetected == TRUE){ + char string[1024]; + PrintMessage3("Device detected: vid=0x%04X pid=0x%04X\nPath: %s\n",vid,pid,MyDevicePathName); + if(HidD_GetManufacturerString(DeviceHandle,string,sizeof(string))==TRUE) wprintf(L"Manufacturer string: %s\n",string); + if(HidD_GetProductString(DeviceHandle,string,sizeof(string))==TRUE) wprintf(L"Product string: %s\n",string); + } + #endif +#endif + if (MyDeviceDetected == FALSE){ + PrintMessage(strings[S_noprog]); //"Programmer not detected\r\n" + //gtk_statusbar_push(status_bar,statusID,strings[S_noprog]); + } + else{ + PrintMessage(strings[S_prog]); //"Programmer detected\r\n"); +#ifdef OPGUI + PrintMessage2("VID=0x%04X PID=0x%04X\r\n",vid,pid); +#endif + //gtk_statusbar_push(status_bar,statusID,strings[S_prog]); + } + return MyDeviceDetected; +} + diff --git a/common_functions.h b/common_functions.h new file mode 100644 index 0000000..8521dd8 --- /dev/null +++ b/common_functions.h @@ -0,0 +1,17 @@ +#ifndef COMMON_FUNCTIONS_H +#define COMMON_FUNCTIONS_H + +extern int skipV33check=0; +extern char loadfileEE[512]="",savefileEE[512]=""; +extern int vid=0x1209,pid=0x5432; +extern int DeviceDetected=0; +extern int new_vid=0x1209,new_pid=0x5432; +extern int old_vid=0x04D8,old_pid=0x0100; +#if !defined _WIN32 && !defined __CYGWIN__ //Linux + extern char path[512]=""; +#endif +#ifdef OPGUI + extern GtkWidget * b_V33check; +#endif + +#endif // COMMON_FUNCTIONS_H diff --git a/op.c b/op.c index 8c39cec..bc32f94 100644 --- a/op.c +++ b/op.c @@ -22,6 +22,7 @@ #include "common.h" +#include "common_functions.h" #include "I2CSPI.h" #include "deviceRW.h" #include "fileIO.h" @@ -45,63 +46,17 @@ int kbhit() #else #include "conio.h" #endif -#define MinDly 0 + +int info=0; +int devType=0x10000; +char dev[64]=""; void msDelay(double delay); void TestHw(); int StartHVReg(double V); -void ProgID(); + void DisplayEE(); int FindDevice(); -int CheckS1(); - -char** strings; -int saveLog=0,programID=0,load_osccal=0,load_BKosccal=0; -int use_osccal=1,use_BKosccal=0; -int load_calibword=0,max_err=200; -int AVRlock=0x100,AVRfuse=0x100,AVRfuse_h=0x100,AVRfuse_x=0x100; -int ICDenable=0,ICDaddr=0x1FF0; -int FWVersion=0,HwID=0; - -FILE* logfile=0; -char LogFileName[512]=""; -char loadfile[512]="",savefile[512]=""; -char loadfileEE[512]="",savefileEE[512]=""; -int info=0; -int vid=0x1209,pid=0x5432; -int new_vid=0x1209,new_pid=0x5432; -int old_vid=0x04D8,old_pid=0x0100; - -WORD *memCODE_W=0; -int size=0,sizeW=0,sizeEE=0,sizeCONFIG=0,sizeUSERID=0; -unsigned char *memCODE=0,*memEE=0,memID[64],memCONFIG[48],memUSERID[8]; -double hvreg=0; -int DeviceDetected=0; -int skipV33check=0; -int progress=0; -int RWstop=0; -int forceConfig=0; -char dev[64]=""; -int devType=0x10000; -char str[4096]; - -#if !defined _WIN32 && !defined __CYGWIN__ //Linux - int fd = -1; - struct hiddev_report_info rep_info_i,rep_info_u; - struct hiddev_usage_ref_multi ref_multi_i,ref_multi_u; - char path[512]=""; - unsigned char bufferU[128],bufferI[128]; -#else //Windows - unsigned char bufferU0[128],bufferI0[128]; - unsigned char *bufferU,*bufferI; - DWORD NumberOfBytesRead,BytesWritten; - ULONG Result; - HANDLE WriteHandle,ReadHandle; - OVERLAPPED HIDOverlapped; - HANDLE hEventObject; -#endif - - int main (int argc, char **argv) { @@ -622,741 +577,3 @@ void DisplayEE(){ } if(empty) PrintMessage(strings[S_Empty]); //empty } - -/// -///Start HV regulator -int StartHVReg(double V){ - int j=0,z; - int vreg=(int)(V*10.0); - if(saveLog&&logfile) fprintf(logfile,"StartHVReg(%.2f)\n",V); - DWORD t0,t; - if(V==-1){ - bufferU[j++]=VREG_DIS; //disable HV regulator - bufferU[j++]=FLUSH; - PacketIO(5); - msDelay(40); - return -1; - } - t=t0=GetTickCount(); - bufferU[j++]=VREG_EN; //enable HV regulator - bufferU[j++]=SET_VPP; - bufferU[j++]=vreg; - bufferU[j++]=SET_PARAMETER; - bufferU[j++]=SET_T3; - bufferU[j++]=2000>>8; - bufferU[j++]=2000&0xff; - bufferU[j++]=WAIT_T3; - bufferU[j++]=READ_ADC; - bufferU[j++]=FLUSH; - for(;j(vreg/10.0+1)*G)&&t>=2; //if 12 bit ADC -// PrintMessage2("v=%d=%fV\n",v,v/G); - } - if(v>(vreg/10.0+1)*G){ - PrintMessage(strings[S_HiVPP]); //"Attenzione: tensione regolatore troppo alta\r\n\r\n" - return 0; - } - else if(v<(vreg/10.0-1)*G){ - PrintMessage(strings[S_LowVPP]); //"Attenzione: tensione regolatore troppo bassa\r\n\r\n" - return 0; - } - else if(v==0){ - PrintMessage(strings[S_lowUsbV]); //"Tensione USB troppo bassa (VUSB<4.5V)\r\n" - return 0; - } - else{ - PrintMessage2(strings[S_reg],t-t0,v/G); //"Regolatore avviato e funzionante dopo T=%d ms VPP=%.1f\r\n\r\n" - if(saveLog&&logfile) fprintf(logfile,strings[S_reg],t-t0,v/G); - return vreg; - } -} - -/// -///Read programmer ID -void ProgID() -{ - if(DeviceDetected!=1) return; - int j=0; - bufferU[j++]=PROG_RST; - bufferU[j++]=FLUSH; - for(;j E3=1 -} - -/// -///Execute hardware test -void TestHw() -{ -#ifndef DEBUG - if(DeviceDetected!=1) return; -#endif - char str[256]; - StartHVReg(13); - int j=0; - PrintMessage(strings[I_TestHW]); //"Test hardware ..." - fflush(stdout); - getchar(); - bufferU[j++]=SET_CK_D; - bufferU[j++]=0x0; - bufferU[j++]=EN_VPP_VCC; //VDD+VPP - bufferU[j++]=0x5; - bufferU[j++]=FLUSH; - for(;j=0x900){ //IO test - int j=0,i,x,r; - strcpy(str,"0000000000000"); - PrintMessage("IO test\nRC|RA|--RB--|\n"); - for(i=0;i<13;i++){ - x=1<>5)&0xFF; //PORTA-C - bufferU[j++]=READ_B; - bufferU[j++]=READ_AC; - bufferU[j++]=FLUSH; - for(;jMinDly?x:MinDly); -#else -// Sleep((long)ceil(delay)>MinDly?(long)ceil(delay):MinDly); - __int64 stop,freq,timeout; - QueryPerformanceCounter((LARGE_INTEGER *)&stop); - QueryPerformanceFrequency((LARGE_INTEGER *)&freq); - timeout=stop+delay*freq/1000.0; - while(stop0){ //try with this first - sprintf(path,"/dev/openprogrammer"); - ioctl(fd, HIDIOCGRAWINFO, &device_info); - if(device_info.vendor==vid&&device_info.product==pid) i=0; - else{ - close(fd); - i=-1; - } - } - if(i){ - for(i=0;i<16;i++){ - sprintf(path,"/dev/hidraw%d",i); - if((fd = open(path, O_RDWR|O_NONBLOCK))>0){ - ioctl(fd, HIDIOCGRAWINFO, &device_info); - if(device_info.vendor==vid&&device_info.product==pid) break; - else close(fd); - } - } - } - if(i==16){ - PrintMessage(strings[S_noprog]); - path[0]=0; - return 0; - } - } - else{ //user supplied path - if((fd = open(path, O_RDWR|O_NONBLOCK)) < 0) { - PrintMessage1(strings[S_DevPermission],path); //"cannot open %s, make sure you have read permission on it",path); - return 0; - } - ioctl(fd, HIDIOCGRAWINFO, &device_info); - if(device_info.vendor!=vid||device_info.product!=pid){ - PrintMessage(strings[S_noprog]); - return 0; - } - } - printf(strings[S_progDev],path); - return 1; - #else //use hiddev device (old method) - struct hiddev_devinfo device_info; - int i=-1; - if(path[0]==0){ //search all devices - if((fd = open("/dev/openprogrammer", O_RDONLY ))>0){ //try with this first - ioctl(fd, HIDIOCGDEVINFO, &device_info); - if(device_info.vendor==vid&&device_info.product==pid) i=0; - else{ - close(fd); - i=-1; - } - } - if(i){ - for(i=0;i<16;i++){ - sprintf(path,"/dev/usb/hiddev%d",i); - if((fd = open(path, O_RDONLY ))>0){ - ioctl(fd, HIDIOCGDEVINFO, &device_info); - if(device_info.vendor==vid&&device_info.product==pid) break; - else close(fd); - } - } - } - if(i==16){ - PrintMessage(strings[S_noprog]); - path[0]=0; - return 0; - } - } - else{ //user supplied path - if ((fd = open(path, O_RDONLY )) < 0) { - PrintMessage1(strings[S_DevPermission],path); //"cannot open %s, make sure you have read permission on it",path); - return 0; - } - ioctl(fd, HIDIOCGDEVINFO, &device_info); - if(device_info.vendor!=vid||device_info.product!=pid){ - PrintMessage(strings[S_noprog]); - return 0; - } - } - printf(strings[S_progDev],path); - MyDeviceDetected = TRUE; - rep_info_u.report_type=HID_REPORT_TYPE_OUTPUT; - rep_info_i.report_type=HID_REPORT_TYPE_INPUT; - rep_info_u.report_id=rep_info_i.report_id=HID_REPORT_ID_FIRST; - rep_info_u.num_fields=rep_info_i.num_fields=1; - ref_multi_u.uref.report_type=HID_REPORT_TYPE_OUTPUT; - ref_multi_i.uref.report_type=HID_REPORT_TYPE_INPUT; - ref_multi_u.uref.report_id=ref_multi_i.uref.report_id=HID_REPORT_ID_FIRST; - ref_multi_u.uref.field_index=ref_multi_i.uref.field_index=0; - ref_multi_u.uref.usage_index=ref_multi_i.uref.usage_index=0; - ref_multi_u.num_values=ref_multi_i.num_values=DIMBUF; - #endif -#else //Windows - PSP_DEVICE_INTERFACE_DETAIL_DATA detailData; - HANDLE DeviceHandle; - HANDLE hDevInfo; - GUID HidGuid; - char MyDevicePathName[1024]; - ULONG Length; - ULONG Required; - typedef struct _HIDD_ATTRIBUTES { - ULONG Size; - USHORT VendorID; - USHORT ProductID; - USHORT VersionNumber; - } HIDD_ATTRIBUTES, *PHIDD_ATTRIBUTES; - typedef void (__stdcall*GETHIDGUID) (OUT LPGUID HidGuid); - typedef BOOLEAN (__stdcall*GETATTRIBUTES)(IN HANDLE HidDeviceObject,OUT PHIDD_ATTRIBUTES Attributes); - typedef BOOLEAN (__stdcall*SETNUMINPUTBUFFERS)(IN HANDLE HidDeviceObject,OUT ULONG NumberBuffers); - typedef BOOLEAN (__stdcall*GETNUMINPUTBUFFERS)(IN HANDLE HidDeviceObject,OUT PULONG NumberBuffers); - typedef BOOLEAN (__stdcall*GETFEATURE) (IN HANDLE HidDeviceObject, OUT PVOID ReportBuffer, IN ULONG ReportBufferLength); - typedef BOOLEAN (__stdcall*SETFEATURE) (IN HANDLE HidDeviceObject, IN PVOID ReportBuffer, IN ULONG ReportBufferLength); - typedef BOOLEAN (__stdcall*GETREPORT) (IN HANDLE HidDeviceObject, OUT PVOID ReportBuffer, IN ULONG ReportBufferLength); - typedef BOOLEAN (__stdcall*SETREPORT) (IN HANDLE HidDeviceObject, IN PVOID ReportBuffer, IN ULONG ReportBufferLength); - typedef BOOLEAN (__stdcall*GETMANUFACTURERSTRING) (IN HANDLE HidDeviceObject, OUT PVOID ReportBuffer, IN ULONG ReportBufferLength); - typedef BOOLEAN (__stdcall*GETPRODUCTSTRING) (IN HANDLE HidDeviceObject, OUT PVOID ReportBuffer, IN ULONG ReportBufferLength); - typedef BOOLEAN (__stdcall*GETINDEXEDSTRING) (IN HANDLE HidDeviceObject, IN ULONG StringIndex, OUT PVOID ReportBuffer, IN ULONG ReportBufferLength); - HIDD_ATTRIBUTES Attributes; - SP_DEVICE_INTERFACE_DATA devInfoData; - int LastDevice = FALSE; - int MemberIndex = 0; - LONG Result; - Length=0; - detailData=NULL; - DeviceHandle=NULL; - HMODULE hHID=0; - GETHIDGUID HidD_GetHidGuid=0; - GETATTRIBUTES HidD_GetAttributes=0; - SETNUMINPUTBUFFERS HidD_SetNumInputBuffers=0; - GETNUMINPUTBUFFERS HidD_GetNumInputBuffers=0; - GETFEATURE HidD_GetFeature=0; - SETFEATURE HidD_SetFeature=0; - GETREPORT HidD_GetInputReport=0; - SETREPORT HidD_SetOutputReport=0; - GETMANUFACTURERSTRING HidD_GetManufacturerString=0; - GETPRODUCTSTRING HidD_GetProductString=0; - hHID = LoadLibrary("hid.dll"); - if(!hHID){ - PrintMessage("Can't find hid.dll"); - return 0; - } - HidD_GetHidGuid=(GETHIDGUID)GetProcAddress(hHID,"HidD_GetHidGuid"); - HidD_GetAttributes=(GETATTRIBUTES)GetProcAddress(hHID,"HidD_GetAttributes"); - HidD_SetNumInputBuffers=(SETNUMINPUTBUFFERS)GetProcAddress(hHID,"HidD_SetNumInputBuffers"); - HidD_GetNumInputBuffers=(GETNUMINPUTBUFFERS)GetProcAddress(hHID,"HidD_GetNumInputBuffers"); - HidD_GetFeature=(GETFEATURE)GetProcAddress(hHID,"HidD_GetFeature"); - HidD_SetFeature=(SETFEATURE)GetProcAddress(hHID,"HidD_SetFeature"); - HidD_GetInputReport=(GETREPORT)GetProcAddress(hHID,"HidD_GetInputReport"); - HidD_SetOutputReport=(SETREPORT)GetProcAddress(hHID,"HidD_SetOutputReport"); - HidD_GetManufacturerString=(GETMANUFACTURERSTRING)GetProcAddress(hHID,"HidD_GetManufacturerString"); - HidD_GetProductString=(GETPRODUCTSTRING)GetProcAddress(hHID,"HidD_GetProductString"); - if(HidD_GetHidGuid==NULL\ - ||HidD_GetAttributes==NULL\ - ||HidD_GetFeature==NULL\ - ||HidD_SetFeature==NULL\ - ||HidD_GetInputReport==NULL\ - ||HidD_SetOutputReport==NULL\ - ||HidD_GetManufacturerString==NULL\ - ||HidD_GetProductString==NULL\ - ||HidD_SetNumInputBuffers==NULL\ - ||HidD_GetNumInputBuffers==NULL) return 0; - HMODULE hSAPI=0; - hSAPI = LoadLibrary("setupapi.dll"); - if(!hSAPI){ - PrintMessage("Can't find setupapi.dll"); - return 0; - } - typedef HDEVINFO (WINAPI* SETUPDIGETCLASSDEVS) (CONST GUID*,PCSTR,HWND,DWORD); - typedef BOOL (WINAPI* SETUPDIENUMDEVICEINTERFACES) (HDEVINFO,PSP_DEVINFO_DATA,CONST GUID*,DWORD,PSP_DEVICE_INTERFACE_DATA); - typedef BOOL (WINAPI* SETUPDIGETDEVICEINTERFACEDETAIL) (HDEVINFO,PSP_DEVICE_INTERFACE_DATA,PSP_DEVICE_INTERFACE_DETAIL_DATA_A,DWORD,PDWORD,PSP_DEVINFO_DATA); - typedef BOOL (WINAPI* SETUPDIDESTROYDEVICEINFOLIST) (HDEVINFO); - SETUPDIGETCLASSDEVS SetupDiGetClassDevsA=0; - SETUPDIENUMDEVICEINTERFACES SetupDiEnumDeviceInterfaces=0; - SETUPDIGETDEVICEINTERFACEDETAIL SetupDiGetDeviceInterfaceDetailA=0; - SETUPDIDESTROYDEVICEINFOLIST SetupDiDestroyDeviceInfoList=0; - SetupDiGetClassDevsA=(SETUPDIGETCLASSDEVS) GetProcAddress(hSAPI,"SetupDiGetClassDevsA"); - SetupDiEnumDeviceInterfaces=(SETUPDIENUMDEVICEINTERFACES) GetProcAddress(hSAPI,"SetupDiEnumDeviceInterfaces"); - SetupDiGetDeviceInterfaceDetailA=(SETUPDIGETDEVICEINTERFACEDETAIL) GetProcAddress(hSAPI,"SetupDiGetDeviceInterfaceDetailA"); - SetupDiDestroyDeviceInfoList=(SETUPDIDESTROYDEVICEINFOLIST) GetProcAddress(hSAPI,"SetupDiDestroyDeviceInfoList"); - if(SetupDiGetClassDevsA==NULL\ - ||SetupDiEnumDeviceInterfaces==NULL\ - ||SetupDiDestroyDeviceInfoList==NULL\ - ||SetupDiGetDeviceInterfaceDetailA==NULL) return 0; - /* - The following code is adapted from Usbhidio_vc6 application example by Jan Axelson - for more information see see http://www.lvr.com/hidpage.htm - */ - /* - API function: HidD_GetHidGuid - Get the GUID for all system HIDs. - Returns: the GUID in HidGuid. - */ - HidD_GetHidGuid(&HidGuid); - /* - API function: SetupDiGetClassDevs - Returns: a handle to a device information set for all installed devices. - Requires: the GUID returned by GetHidGuid. - */ - hDevInfo=SetupDiGetClassDevs(&HidGuid,NULL,NULL,DIGCF_PRESENT|DIGCF_INTERFACEDEVICE); - devInfoData.cbSize = sizeof(devInfoData); - //Step through the available devices looking for the one we want. - //Quit on detecting the desired device or checking all available devices without success. - MemberIndex = 0; - LastDevice = FALSE; - do - { - /* - API function: SetupDiEnumDeviceInterfaces - On return, MyDeviceInterfaceData contains the handle to a - SP_DEVICE_INTERFACE_DATA structure for a detected device. - Requires: - The DeviceInfoSet returned in SetupDiGetClassDevs. - The HidGuid returned in GetHidGuid. - An index to specify a device. - */ - Result=SetupDiEnumDeviceInterfaces (hDevInfo, 0, &HidGuid, MemberIndex, &devInfoData); - if (Result != 0) - { - //A device has been detected, so get more information about it. - /* - API function: SetupDiGetDeviceInterfaceDetail - Returns: an SP_DEVICE_INTERFACE_DETAIL_DATA structure - containing information about a device. - To retrieve the information, call this function twice. - The first time returns the size of the structure in Length. - The second time returns a pointer to the data in DeviceInfoSet. - Requires: - A DeviceInfoSet returned by SetupDiGetClassDevs - The SP_DEVICE_INTERFACE_DATA structure returned by SetupDiEnumDeviceInterfaces. - The final parameter is an optional pointer to an SP_DEV_INFO_DATA structure. - This application doesn't retrieve or use the structure. - If retrieving the structure, set - MyDeviceInfoData.cbSize = length of MyDeviceInfoData. - and pass the structure's address. - */ - //Get the Length value. - //The call will return with a "buffer too small" error which can be ignored. - Result = SetupDiGetDeviceInterfaceDetail(hDevInfo, &devInfoData, NULL, 0, &Length, NULL); - //Allocate memory for the hDevInfo structure, using the returned Length. - detailData = (PSP_DEVICE_INTERFACE_DETAIL_DATA)malloc(Length); - //Set cbSize in the detailData structure. - detailData -> cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA); - //Call the function again, this time passing it the returned buffer size. - Result = SetupDiGetDeviceInterfaceDetail(hDevInfo, &devInfoData, detailData, Length,&Required, NULL); - // Open a handle to the device. - // To enable retrieving information about a system mouse or keyboard, - // don't request Read or Write access for this handle. - /* - API function: CreateFile - Returns: a handle that enables reading and writing to the device. - Requires: - The DevicePath in the detailData structure - returned by SetupDiGetDeviceInterfaceDetail. - */ - DeviceHandle=CreateFile(detailData->DevicePath, - 0, FILE_SHARE_READ|FILE_SHARE_WRITE, - (LPSECURITY_ATTRIBUTES)NULL,OPEN_EXISTING, 0, NULL); - /* - API function: HidD_GetAttributes - Requests information from the device. - Requires: the handle returned by CreateFile. - Returns: a HIDD_ATTRIBUTES structure containing - the Vendor ID, Product ID, and Product Version Number. - Use this information to decide if the detected device is - the one we're looking for. - */ - //Set the Size to the number of bytes in the structure. - Attributes.Size = sizeof(Attributes); - Result = HidD_GetAttributes(DeviceHandle,&Attributes); - //Is it the desired device? - MyDeviceDetected = FALSE; - if (Attributes.VendorID == vid) - { - if (Attributes.ProductID == pid) - { - //Both the Vendor ID and Product ID match. - MyDeviceDetected = TRUE; - strcpy(MyDevicePathName,detailData->DevicePath); - // Get a handle for writing Output reports. - WriteHandle=CreateFile(detailData->DevicePath, - GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE, - (LPSECURITY_ATTRIBUTES)NULL,OPEN_EXISTING,0,NULL); - //Get a handle to the device for the overlapped ReadFiles. - ReadHandle=CreateFile(detailData->DevicePath, - GENERIC_READ,FILE_SHARE_READ|FILE_SHARE_WRITE,(LPSECURITY_ATTRIBUTES)NULL, - OPEN_EXISTING,FILE_FLAG_OVERLAPPED,NULL); - if (hEventObject) CloseHandle(hEventObject); - hEventObject = CreateEvent(NULL,TRUE,TRUE,""); - //Set the members of the overlapped structure. - HIDOverlapped.hEvent = hEventObject; - HIDOverlapped.Offset = 0; - HIDOverlapped.OffsetHigh = 0; - Result=HidD_SetNumInputBuffers(DeviceHandle,64); - } - else - //The Product ID doesn't match. - CloseHandle(DeviceHandle); - } - else - //The Vendor ID doesn't match. - CloseHandle(DeviceHandle); - //Free the memory used by the detailData structure (no longer needed). - free(detailData); - } - else - //SetupDiEnumDeviceInterfaces returned 0, so there are no more devices to check. - LastDevice=TRUE; - //If we haven't found the device yet, and haven't tried every available device, - //try the next one. - MemberIndex = MemberIndex + 1; - } //do - while ((LastDevice == FALSE) && (MyDeviceDetected == FALSE)); - //Free the memory reserved for hDevInfo by SetupDiClassDevs. - SetupDiDestroyDeviceInfoList(hDevInfo); - - if(info&&MyDeviceDetected == TRUE){ - char string[1024]; - PrintMessage3("Device detected: vid=0x%04X pid=0x%04X\nPath: %s\n",vid,pid,MyDevicePathName); - if(HidD_GetManufacturerString(DeviceHandle,string,sizeof(string))==TRUE) wprintf(L"Manufacturer string: %s\n",string); - if(HidD_GetProductString(DeviceHandle,string,sizeof(string))==TRUE) wprintf(L"Product string: %s\n",string); - } -#endif - if (MyDeviceDetected == FALSE){ - PrintMessage(strings[S_noprog]); //"Programmer not detected\r\n" - //gtk_statusbar_push(status_bar,statusID,strings[S_noprog]); - } - else{ - PrintMessage(strings[S_prog]); //"Programmer detected\r\n"); - //gtk_statusbar_push(status_bar,statusID,strings[S_prog]); - } - return MyDeviceDetected; -} diff --git a/opgui.c b/opgui.c index a6c550e..cf8255f 100644 --- a/opgui.c +++ b/opgui.c @@ -22,6 +22,7 @@ * or see */ #include "common.h" +#include "common_functions.h" #include "I2CSPI.h" #include "coff.h" #include "icd.h" @@ -33,44 +34,17 @@ #define MAXLINES 600 #define CONFIG_FILE "opgui.ini" #define CONFIG_DIR ".opgui" -#define MinDly 0 + void Connect(GtkWidget *widget,GtkWidget *window); void Quit(GtkWidget *widget,GtkWidget *window) { gtk_main_quit(); } void I2cspiR(); void I2cspiS(); -void ProgID(); + void PrintMessageI2C(const char *msg); void ShowContext(); int FindDevice(int vid,int pid); void TestHw(); -int CheckS1(); -char** strings; //!localized strings -int cmdline=0; -int saveLog=0,programID=0,load_osccal=0,load_BKosccal=0; -int use_osccal=1,use_BKosccal=0; -int load_calibword=0,max_err=200; -int AVRlock=0x100,AVRfuse=0x100,AVRfuse_h=0x100,AVRfuse_x=0x100; -int ICDenable=0,ICDaddr=0x1FF0; -int FWVersion=0,HwID=0; -FILE* logfile=0; -char LogFileName[512]=""; -char loadfile[512]="",savefile[512]=""; -char loadfileEE[512]="",savefileEE[512]=""; -char CoffFileName[512]=""; -int vid=0x1209,pid=0x5432; -int new_vid=0x1209,new_pid=0x5432; -int old_vid=0x04D8,old_pid=0x0100; -WORD *memCODE_W=0; -int size=0,sizeW=0,sizeEE=0,sizeCONFIG=0,sizeUSERID=0; -unsigned char *memCODE=0,*memEE=0,memID[64],memCONFIG[48],memUSERID[8]; -double hvreg=0; -int DeviceDetected=0; -int IOTimer=0; -int skipV33check=0; -int waitS1=0,waitingS1=0; -int progress=0; -int RWstop=0; -int forceConfig=0; + #ifdef DEBUG int addrDebug=0; unsigned short dataDebug=0; @@ -131,7 +105,6 @@ GtkWidget * VDD_ON; GtkWidget * b_io_active; GtkWidget * commandSend; GtkWidget * commandTransfer; -GtkWidget * b_V33check; GtkWidget * Hex_entry; GtkWidget * Address_entry; GtkWidget * Data_entry; @@ -203,23 +176,11 @@ char *groupNames[NUM_GROUPS] = { }; char *GROUP_ALL="*"; -#if !defined _WIN32 && !defined __CYGWIN__ //Linux - int fd = -1; -#ifdef hiddevIO - struct hiddev_report_info rep_info_i,rep_info_u; - struct hiddev_usage_ref_multi ref_multi_i,ref_multi_u; -#endif - char path[512]=""; - unsigned char bufferU[128],bufferI[128]; -#else //Windows - unsigned char bufferU0[128],bufferI0[128]; - unsigned char *bufferU,*bufferI; - DWORD NumberOfBytesRead,BytesWritten; - ULONG Result; - HANDLE WriteHandle,ReadHandle; - OVERLAPPED HIDOverlapped; - HANDLE hEventObject; -#endif +int progress=0; +int waitS1=0,waitingS1=0; +int IOTimer=0; +int cmdline=0; + /// ///Exit gint delete_event( GtkWidget *widget,GdkEvent *event,gpointer data ) @@ -1298,403 +1259,6 @@ void DisplayEE(){ } free(aux); } -/// -///Start HV regulator -int StartHVReg(double V){ - int j=0,z; - int vreg=(int)(V*10.0); - if(saveLog&&logfile) fprintf(logfile,"StartHVReg(%.2f)\n",V); - DWORD t0,t; - if(V==-1){ - bufferU[j++]=VREG_DIS; //disable HV regulator - bufferU[j++]=FLUSH; - PacketIO(5); - msDelay(40); - return -1; - } - t=t0=GetTickCount(); - bufferU[j++]=VREG_EN; //enable HV regulator - bufferU[j++]=SET_VPP; - bufferU[j++]=vreg; - bufferU[j++]=SET_PARAMETER; - bufferU[j++]=SET_T3; - bufferU[j++]=2000>>8; - bufferU[j++]=2000&0xff; - bufferU[j++]=WAIT_T3; - bufferU[j++]=READ_ADC; - bufferU[j++]=FLUSH; - for(;j(vreg/10.0+1)*G)&&t>=2; //if 12 bit ADC -// PrintMessage2("v=%d=%fV\n",v,v/G); - } - if(v>(vreg/10.0+1)*G){ - PrintMessage(strings[S_HiVPP]); //"Attenzione: tensione regolatore troppo alta\r\n\r\n" - return 0; - } - else if(v<(vreg/10.0-1)*G){ - PrintMessage(strings[S_LowVPP]); //"Attenzione: tensione regolatore troppo bassa\r\n\r\n" - return 0; - } - else if(v==0){ - PrintMessage(strings[S_lowUsbV]); //"Tensione USB troppo bassa (VUSB<4.5V)\r\n" - return 0; - } - else{ - PrintMessage2(strings[S_reg],t-t0,v/G); //"Regolatore avviato e funzionante dopo T=%d ms VPP=%.1f\r\n\r\n" - if(saveLog&&logfile) fprintf(logfile,strings[S_reg],t-t0,v/G); - return vreg; - } -} -/// -///Read programmer ID -void ProgID() -{ - if(DeviceDetected!=1) return; - int j=0; - bufferU[j++]=PROG_RST; - bufferU[j++]=FLUSH; - for(;j E3=1 (active low) - } -} -/// -///Execute hardware test -void TestHw(GtkWidget *widget,GtkWindow* parent) -{ -#ifndef DEBUG - if(DeviceDetected!=1) return; -#endif - char str[256]; - StartHVReg(13); - int j=0; - MsgBox(strings[I_TestHW]); //"Test hardware ..." - bufferU[j++]=SET_CK_D; - bufferU[j++]=0x0; - bufferU[j++]=EN_VPP_VCC; //VDD+VPP - bufferU[j++]=0x5; - bufferU[j++]=FLUSH; - for(;j=0x900){ //IO test - int j=0,i,x,r; - strcpy(str,"0000000000000"); - PrintMessage("IO test\nRC|RA|--RB--|\n"); - for(i=0;i<13;i++){ - x=1<>5)&0xFF; //PORTA-C - bufferU[j++]=READ_B; - bufferU[j++]=READ_AC; - bufferU[j++]=FLUSH; - for(;jMinDly?x:MinDly); -#else -// Sleep((long)ceil(delay)>MinDly?(long)ceil(delay):MinDly); - __int64 stop,freq,timeout; - QueryPerformanceCounter((LARGE_INTEGER *)&stop); - QueryPerformanceFrequency((LARGE_INTEGER *)&freq); - timeout=stop+delay*freq/1000.0; - while(stop0){ //try with this first - sprintf(path,"/dev/openprogrammer"); - ioctl(fd, HIDIOCGRAWINFO, &device_info); - if(device_info.vendor==vid&&device_info.product==pid) i=0; - else{ - close(fd); - i=-1; - } - } - if(i){ - for(i=0;i<16;i++){ - sprintf(path,"/dev/hidraw%d",i); - if((fd = open(path, O_RDWR|O_NONBLOCK))>0){ - ioctl(fd, HIDIOCGRAWINFO, &device_info); - if(device_info.vendor==vid&&device_info.product==pid) break; - else close(fd); - } - } - } - if(i==16){ - PrintMessage(strings[S_noprog]); - path[0]=0; - return 0; - } - } - else{ //user supplied path - if((fd = open(path, O_RDWR|O_NONBLOCK)) < 0) { - PrintMessage1(strings[S_DevPermission],path); //"cannot open %s, make sure you have read permission on it",path); - return 0; - } - ioctl(fd, HIDIOCGRAWINFO, &device_info); - if(device_info.vendor!=vid||device_info.product!=pid){ - PrintMessage(strings[S_noprog]); - return 0; - } - } - printf(strings[S_progDev],path); - return 1; - #else //use hiddev device (old method) - struct hiddev_devinfo device_info; - int i=-1; - if(path[0]==0){ //search all devices - if((fd = open("/dev/openprogrammer", O_RDONLY ))>0){ //try with this first - ioctl(fd, HIDIOCGDEVINFO, &device_info); - if(device_info.vendor==vid&&device_info.product==pid) i=0; - else{ - close(fd); - i=-1; - } - } - if(i){ - for(i=0;i<16;i++){ - sprintf(path,"/dev/usb/hiddev%d",i); - if((fd = open(path, O_RDONLY ))>0){ - ioctl(fd, HIDIOCGDEVINFO, &device_info); - if(device_info.vendor==vid&&device_info.product==pid) break; - else close(fd); - } - } - } - if(i==16){ - PrintMessage(strings[S_noprog]); - path[0]=0; - return 0; - } - } - else{ //user supplied path - if ((fd = open(path, O_RDONLY )) < 0) { - PrintMessage1(strings[S_DevPermission],path); //"cannot open %s, make sure you have read permission on it",path); - return 0; - } - ioctl(fd, HIDIOCGDEVINFO, &device_info); - if(device_info.vendor!=vid||device_info.product!=pid){ - PrintMessage(strings[S_noprog]); - return 0; - } - } - MyDeviceDetected = TRUE; - rep_info_u.report_type=HID_REPORT_TYPE_OUTPUT; - rep_info_i.report_type=HID_REPORT_TYPE_INPUT; - rep_info_u.report_id=rep_info_i.report_id=HID_REPORT_ID_FIRST; - rep_info_u.num_fields=rep_info_i.num_fields=1; - ref_multi_u.uref.report_type=HID_REPORT_TYPE_OUTPUT; - ref_multi_i.uref.report_type=HID_REPORT_TYPE_INPUT; - ref_multi_u.uref.report_id=ref_multi_i.uref.report_id=HID_REPORT_ID_FIRST; - ref_multi_u.uref.field_index=ref_multi_i.uref.field_index=0; - ref_multi_u.uref.usage_index=ref_multi_i.uref.usage_index=0; - ref_multi_u.num_values=ref_multi_i.num_values=DIMBUF; - #endif -#else //Windows - PSP_DEVICE_INTERFACE_DETAIL_DATA detailData; - HANDLE DeviceHandle; - HANDLE hDevInfo; - GUID HidGuid; - char MyDevicePathName[1024]; - ULONG Length; - ULONG Required; - typedef struct _HIDD_ATTRIBUTES { - ULONG Size; - USHORT VendorID; - USHORT ProductID; - USHORT VersionNumber; - } HIDD_ATTRIBUTES, *PHIDD_ATTRIBUTES; - typedef void (__stdcall*GETHIDGUID) (OUT LPGUID HidGuid); - typedef BOOLEAN (__stdcall*GETATTRIBUTES)(IN HANDLE HidDeviceObject,OUT PHIDD_ATTRIBUTES Attributes); - typedef BOOLEAN (__stdcall*SETNUMINPUTBUFFERS)(IN HANDLE HidDeviceObject,OUT ULONG NumberBuffers); - typedef BOOLEAN (__stdcall*GETNUMINPUTBUFFERS)(IN HANDLE HidDeviceObject,OUT PULONG NumberBuffers); - typedef BOOLEAN (__stdcall*GETFEATURE) (IN HANDLE HidDeviceObject, OUT PVOID ReportBuffer, IN ULONG ReportBufferLength); - typedef BOOLEAN (__stdcall*SETFEATURE) (IN HANDLE HidDeviceObject, IN PVOID ReportBuffer, IN ULONG ReportBufferLength); - typedef BOOLEAN (__stdcall*GETREPORT) (IN HANDLE HidDeviceObject, OUT PVOID ReportBuffer, IN ULONG ReportBufferLength); - typedef BOOLEAN (__stdcall*SETREPORT) (IN HANDLE HidDeviceObject, IN PVOID ReportBuffer, IN ULONG ReportBufferLength); - typedef BOOLEAN (__stdcall*GETMANUFACTURERSTRING) (IN HANDLE HidDeviceObject, OUT PVOID ReportBuffer, IN ULONG ReportBufferLength); - typedef BOOLEAN (__stdcall*GETPRODUCTSTRING) (IN HANDLE HidDeviceObject, OUT PVOID ReportBuffer, IN ULONG ReportBufferLength); -// typedef BOOLEAN (__stdcall*GETINDEXEDSTRING) (IN HANDLE HidDeviceObject, IN ULONG StringIndex, OUT PVOID ReportBuffer, IN ULONG ReportBufferLength); - HIDD_ATTRIBUTES Attributes; - SP_DEVICE_INTERFACE_DATA devInfoData; - int LastDevice = FALSE; - int MemberIndex = 0; - LONG Result; - Length=0; - detailData=NULL; - DeviceHandle=NULL; - HMODULE hHID=0; - GETHIDGUID HidD_GetHidGuid=0; - GETATTRIBUTES HidD_GetAttributes=0; - SETNUMINPUTBUFFERS HidD_SetNumInputBuffers=0; - GETNUMINPUTBUFFERS HidD_GetNumInputBuffers=0; - GETFEATURE HidD_GetFeature=0; - SETFEATURE HidD_SetFeature=0; - GETREPORT HidD_GetInputReport=0; - SETREPORT HidD_SetOutputReport=0; - GETMANUFACTURERSTRING HidD_GetManufacturerString=0; - GETPRODUCTSTRING HidD_GetProductString=0; - hHID = LoadLibrary("hid.dll"); - if(!hHID){ - PrintMessage("Can't find hid.dll"); - return 0; - } - HidD_GetHidGuid=(GETHIDGUID)GetProcAddress(hHID,"HidD_GetHidGuid"); - HidD_GetAttributes=(GETATTRIBUTES)GetProcAddress(hHID,"HidD_GetAttributes"); - HidD_SetNumInputBuffers=(SETNUMINPUTBUFFERS)GetProcAddress(hHID,"HidD_SetNumInputBuffers"); - HidD_GetNumInputBuffers=(GETNUMINPUTBUFFERS)GetProcAddress(hHID,"HidD_GetNumInputBuffers"); - HidD_GetFeature=(GETFEATURE)GetProcAddress(hHID,"HidD_GetFeature"); - HidD_SetFeature=(SETFEATURE)GetProcAddress(hHID,"HidD_SetFeature"); - HidD_GetInputReport=(GETREPORT)GetProcAddress(hHID,"HidD_GetInputReport"); - HidD_SetOutputReport=(SETREPORT)GetProcAddress(hHID,"HidD_SetOutputReport"); - HidD_GetManufacturerString=(GETMANUFACTURERSTRING)GetProcAddress(hHID,"HidD_GetManufacturerString"); - HidD_GetProductString=(GETPRODUCTSTRING)GetProcAddress(hHID,"HidD_GetProductString"); - if(HidD_GetHidGuid==NULL\ - ||HidD_GetAttributes==NULL\ - ||HidD_GetFeature==NULL\ - ||HidD_SetFeature==NULL\ - ||HidD_GetInputReport==NULL\ - ||HidD_SetOutputReport==NULL\ - ||HidD_GetManufacturerString==NULL\ - ||HidD_GetProductString==NULL\ - ||HidD_SetNumInputBuffers==NULL\ - ||HidD_GetNumInputBuffers==NULL) return 0; - HMODULE hSAPI=0; - hSAPI = LoadLibrary("setupapi.dll"); - if(!hSAPI){ - PrintMessage("Can't find setupapi.dll"); - return 0; - } - typedef HDEVINFO (WINAPI* SETUPDIGETCLASSDEVS) (CONST GUID*,PCSTR,HWND,DWORD); - typedef BOOL (WINAPI* SETUPDIENUMDEVICEINTERFACES) (HDEVINFO,PSP_DEVINFO_DATA,CONST GUID*,DWORD,PSP_DEVICE_INTERFACE_DATA); - typedef BOOL (WINAPI* SETUPDIGETDEVICEINTERFACEDETAIL) (HDEVINFO,PSP_DEVICE_INTERFACE_DATA,PSP_DEVICE_INTERFACE_DETAIL_DATA_A,DWORD,PDWORD,PSP_DEVINFO_DATA); - typedef BOOL (WINAPI* SETUPDIDESTROYDEVICEINFOLIST) (HDEVINFO); - SETUPDIGETCLASSDEVS SetupDiGetClassDevsA=0; - SETUPDIENUMDEVICEINTERFACES SetupDiEnumDeviceInterfaces=0; - SETUPDIGETDEVICEINTERFACEDETAIL SetupDiGetDeviceInterfaceDetailA=0; - SETUPDIDESTROYDEVICEINFOLIST SetupDiDestroyDeviceInfoList=0; - SetupDiGetClassDevsA=(SETUPDIGETCLASSDEVS) GetProcAddress(hSAPI,"SetupDiGetClassDevsA"); - SetupDiEnumDeviceInterfaces=(SETUPDIENUMDEVICEINTERFACES) GetProcAddress(hSAPI,"SetupDiEnumDeviceInterfaces"); - SetupDiGetDeviceInterfaceDetailA=(SETUPDIGETDEVICEINTERFACEDETAIL) GetProcAddress(hSAPI,"SetupDiGetDeviceInterfaceDetailA"); - SetupDiDestroyDeviceInfoList=(SETUPDIDESTROYDEVICEINFOLIST) GetProcAddress(hSAPI,"SetupDiDestroyDeviceInfoList"); - if(SetupDiGetClassDevsA==NULL\ - ||SetupDiEnumDeviceInterfaces==NULL\ - ||SetupDiDestroyDeviceInfoList==NULL\ - ||SetupDiGetDeviceInterfaceDetailA==NULL) return 0; - /* - The following code is adapted from Usbhidio_vc6 application example by Jan Axelson - for more information see see http://www.lvr.com/hidpage.htm - */ - /* - API function: HidD_GetHidGuid - Get the GUID for all system HIDs. - Returns: the GUID in HidGuid. - */ - HidD_GetHidGuid(&HidGuid); - /* - API function: SetupDiGetClassDevs - Returns: a handle to a device information set for all installed devices. - Requires: the GUID returned by GetHidGuid. - */ - hDevInfo=SetupDiGetClassDevs(&HidGuid,NULL,NULL,DIGCF_PRESENT|DIGCF_INTERFACEDEVICE); - devInfoData.cbSize = sizeof(devInfoData); - //Step through the available devices looking for the one we want. - //Quit on detecting the desired device or checking all available devices without success. - MemberIndex = 0; - LastDevice = FALSE; - do - { - /* - API function: SetupDiEnumDeviceInterfaces - On return, MyDeviceInterfaceData contains the handle to a - SP_DEVICE_INTERFACE_DATA structure for a detected device. - Requires: - The DeviceInfoSet returned in SetupDiGetClassDevs. - The HidGuid returned in GetHidGuid. - An index to specify a device. - */ - Result=SetupDiEnumDeviceInterfaces (hDevInfo, 0, &HidGuid, MemberIndex, &devInfoData); - if (Result != 0) - { - //A device has been detected, so get more information about it. - /* - API function: SetupDiGetDeviceInterfaceDetail - Returns: an SP_DEVICE_INTERFACE_DETAIL_DATA structure - containing information about a device. - To retrieve the information, call this function twice. - The first time returns the size of the structure in Length. - The second time returns a pointer to the data in DeviceInfoSet. - Requires: - A DeviceInfoSet returned by SetupDiGetClassDevs - The SP_DEVICE_INTERFACE_DATA structure returned by SetupDiEnumDeviceInterfaces. - The final parameter is an optional pointer to an SP_DEV_INFO_DATA structure. - This application doesn't retrieve or use the structure. - If retrieving the structure, set - MyDeviceInfoData.cbSize = length of MyDeviceInfoData. - and pass the structure's address. - */ - //Get the Length value. - //The call will return with a "buffer too small" error which can be ignored. - Result = SetupDiGetDeviceInterfaceDetail(hDevInfo, &devInfoData, NULL, 0, &Length, NULL); - //Allocate memory for the hDevInfo structure, using the returned Length. - detailData = (PSP_DEVICE_INTERFACE_DETAIL_DATA)malloc(Length); - //Set cbSize in the detailData structure. - detailData -> cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA); - //Call the function again, this time passing it the returned buffer size. - Result = SetupDiGetDeviceInterfaceDetail(hDevInfo, &devInfoData, detailData, Length,&Required, NULL); - // Open a handle to the device. - // To enable retrieving information about a system mouse or keyboard, - // don't request Read or Write access for this handle. - /* - API function: CreateFile - Returns: a handle that enables reading and writing to the device. - Requires: - The DevicePath in the detailData structure - returned by SetupDiGetDeviceInterfaceDetail. - */ - DeviceHandle=CreateFile(detailData->DevicePath, - 0, FILE_SHARE_READ|FILE_SHARE_WRITE, - (LPSECURITY_ATTRIBUTES)NULL,OPEN_EXISTING, 0, NULL); - /* - API function: HidD_GetAttributes - Requests information from the device. - Requires: the handle returned by CreateFile. - Returns: a HIDD_ATTRIBUTES structure containing - the Vendor ID, Product ID, and Product Version Number. - Use this information to decide if the detected device is - the one we're looking for. - */ - //Set the Size to the number of bytes in the structure. - Attributes.Size = sizeof(Attributes); - Result = HidD_GetAttributes(DeviceHandle,&Attributes); - //Is it the desired device? - MyDeviceDetected = FALSE; - if (Attributes.VendorID == vid) - { - if (Attributes.ProductID == pid) - { - //Both the Vendor ID and Product ID match. - MyDeviceDetected = TRUE; - strcpy(MyDevicePathName,detailData->DevicePath); - // Get a handle for writing Output reports. - WriteHandle=CreateFile(detailData->DevicePath, - GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE, - (LPSECURITY_ATTRIBUTES)NULL,OPEN_EXISTING,0,NULL); - //Get a handle to the device for the overlapped ReadFiles. - ReadHandle=CreateFile(detailData->DevicePath, - GENERIC_READ,FILE_SHARE_READ|FILE_SHARE_WRITE,(LPSECURITY_ATTRIBUTES)NULL, - OPEN_EXISTING,FILE_FLAG_OVERLAPPED,NULL); - if (hEventObject) CloseHandle(hEventObject); - hEventObject = CreateEvent(NULL,TRUE,TRUE,""); - //Set the members of the overlapped structure. - HIDOverlapped.hEvent = hEventObject; - HIDOverlapped.Offset = 0; - HIDOverlapped.OffsetHigh = 0; - Result=HidD_SetNumInputBuffers(DeviceHandle,64); - } - else - //The Product ID doesn't match. - CloseHandle(DeviceHandle); - } - else - //The Vendor ID doesn't match. - CloseHandle(DeviceHandle); - //Free the memory used by the detailData structure (no longer needed). - free(detailData); - } - else - //SetupDiEnumDeviceInterfaces returned 0, so there are no more devices to check. - LastDevice=TRUE; - //If we haven't found the device yet, and haven't tried every available device, - //try the next one. - MemberIndex = MemberIndex + 1; - } //do - while ((LastDevice == FALSE) && (MyDeviceDetected == FALSE)); - //Free the memory reserved for hDevInfo by SetupDiClassDevs. - SetupDiDestroyDeviceInfoList(hDevInfo); -/* if(info){ - PrintMessage3("Device detected: vid=0x%04X pid=0x%04X\nPath: %s\n",vid,pid,MyDevicePathName); - if(HidD_GetManufacturerString(DeviceHandle,string,sizeof(string))==TRUE) wprintf(L"Manufacturer string: %s\n",string); - if(HidD_GetProductString(DeviceHandle,string,sizeof(string))==TRUE) wprintf(L"Product string: %s\n",string); - }*/ -#endif - if (MyDeviceDetected == FALSE){ - PrintMessage(strings[S_noprog]); //"Programmer not detected\r\n" - //gtk_statusbar_push(status_bar,statusID,strings[S_noprog]); - } - else{ - PrintMessage(strings[S_prog]); //"Programmer detected\r\n"); - PrintMessage2("VID=0x%04X PID=0x%04X\r\n",vid,pid); - //gtk_statusbar_push(status_bar,statusID,strings[S_prog]); - } - return MyDeviceDetected; -} -- cgit v1.2.3-54-g00ecf