diff --git a/PIC16F1-USB-Bootloader.mcp b/PIC16F1-USB-Bootloader.mcp new file mode 100644 index 0000000..1a98e0a --- /dev/null +++ b/PIC16F1-USB-Bootloader.mcp @@ -0,0 +1,69 @@ +[HEADER] +magic_cookie={66E99B07-E706-4689-9E80-9B2582898A13} +file_version=1.0 +device=PIC16F1455 +[PATH_INFO] +BuildDirPolicy=BuildDirIsProjectDir +dir_src= +dir_bin=C:\Users\User\Documents\GitHub\PIC16F1-USB-Bootloader\output +dir_tmp= +dir_sin= +dir_inc= +dir_lib= +dir_lkr= +[CAT_FILTERS] +filter_src=*.asm +filter_inc=*.h;*.inc +filter_obj=*.o +filter_lib=*.lib +filter_lkr=*.lkr +[CAT_SUBFOLDERS] +subfolder_src= +subfolder_inc= +subfolder_obj= +subfolder_lib= +subfolder_lkr= +[FILE_SUBFOLDERS] +file_000=. +file_001=. +file_002=. +file_003=. +file_004=. +file_005=. +[GENERATED_FILES] +file_000=no +file_001=no +file_002=no +file_003=no +file_004=no +file_005=no +[OTHER_FILES] +file_000=no +file_001=no +file_002=no +file_003=no +file_004=no +file_005=no +[FILE_INFO] +file_000=bootloader.asm +file_001=bdt.inc +file_002=log_macros.inc +file_003=macros.inc +file_004=protocol_constants.inc +file_005=usb.inc +[SUITE_INFO] +suite_guid={6B3DAA78-59C1-46DD-B6AA-DBDAE4E06484} +suite_state=generate-absolute-code +[TOOL_SETTINGS] +TS{DD2213A8-6310-47B1-8376-9430CDFC013F}= +TS{BFD27FBA-4A02-4C0E-A5E5-B812F3E7707C}=/m"$(BINDIR_)$(TARGETBASE).map" /w /o"$(BINDIR_)$(TARGETBASE).cof" +TS{ADE93A55-C7C7-4D4D-A4BA-59305F7D0391}= +[INSTRUMENTED_TRACE] +enable=0 +transport=0 +format=0 +[CUSTOM_BUILD] +Pre-Build= +Pre-BuildEnabled=1 +Post-Build= +Post-BuildEnabled=1 diff --git a/PIC16F1-USB-Bootloader.mcs b/PIC16F1-USB-Bootloader.mcs new file mode 100644 index 0000000..b8b99a5 --- /dev/null +++ b/PIC16F1-USB-Bootloader.mcs @@ -0,0 +1,6 @@ +[Header] +MagicCookie={0b13fe8c-dfe0-40eb-8900-6712719559a7} +Version=1.0 +[TOOL_LOC_STAMPS] +tool_loc{49D3CA3F-D9A3-4518-9943-226A347E8CC7}=C:\Program Files (x86)\Microchip\MPASM Suite\MPASMWIN.exe +tool_loc{96C98149-AA1B-4CF9-B967-FAE79CAB663C}=C:\Program Files (x86)\Microchip\MPASM Suite\mplink.exe diff --git a/PIC16F1-USB-Bootloader.mcw b/PIC16F1-USB-Bootloader.mcw new file mode 100644 index 0000000..0a81779 Binary files /dev/null and b/PIC16F1-USB-Bootloader.mcw differ diff --git a/bootloader.asm b/bootloader.asm index 43314e2..94b0ac0 100644 --- a/bootloader.asm +++ b/bootloader.asm @@ -49,9 +49,14 @@ ; With logging enabled, the bootloader will not fit in 512 words. ; Use this only for debugging! ; For more info, see log_macros.inc and log.asm. -LOGGING_ENABLED equ 0 - +LOGGING_ENABLED equ 0 +; Overdrive data in descriptor by app +ENABLE_POWER_CONFIG equ 0 +; Bootloader switch definition RA3 +USE_RA3_SWITCH equ 0 +; Bootloader switch definition RC3 (external pull-up need for this pin) +USE_RC3_SWITCH equ 1 radix dec list n=0,st=off @@ -100,6 +105,7 @@ ALL_DESCS_TOTAL_LEN equ DEVICE_DESC_LEN+CONFIG_DESC_TOTAL_LEN+SERIAL_NUM_DESC_LE EP0_BUF_SIZE equ 8 ; endpoint 0 buffer size EP1_OUT_BUF_SIZE equ 64 ; endpoint 1 OUT (CDC data) buffer size EP1_IN_BUF_SIZE equ 1 ; endpoint 1 IN (CDC data) buffer size (only need 1 byte to return status codes) +EP2_IN_BUF_SIZE equ 1 ; endpoint 2 IN (CDC data) buffer size ; Since we're only using 5 endpoints, use the BDT area for buffers, ; and use the 4 bytes normally occupied by the EP2 OUT buffer descriptor for variables. @@ -122,6 +128,9 @@ BANKED_EP1IN_BUF equ BANKED_EP0IN_BUF+EP0_BUF_SIZE+EXTRA_VARS_LEN EP1OUT_BUF equ EP1IN_BUF+EP1_IN_BUF_SIZE ; only use 1 byte for EP1 IN BANKED_EP1OUT_BUF equ BANKED_EP1IN_BUF+EP1_IN_BUF_SIZE +EP2IN_BUF equ EP1OUT_BUF+EP1_OUT_BUF_SIZE ; only use 1 byte for EP2 IN +BANKED_EP2IN_BUF equ BANKED_EP1OUT_BUF+EP1_OUT_BUF_SIZE + ; High byte of all endpoint buffers. EPBUF_ADRH equ (EP0OUT_BUF>>8) if ((EP0IN_BUF>>8) != (EP0OUT_BUF>>8)) || ((EP1OUT_BUF>>8) != (EP0OUT_BUF>>8)) || ((EP1IN_BUF>>8) != (EP0OUT_BUF>>8)) @@ -153,78 +162,18 @@ DEVICE_CONFIGURED equ 2 ; the device is configured org 0x0000 RESET_VECT ; Enable weak pull-ups +#if USE_RA3_SWITCH banksel OPTION_REG bcf OPTION_REG,NOT_WPUEN +#endif banksel OSCCON goto bootloader_start ; to be continued further down in the file org 0x0004 INTERRUPT_VECT -; check the high byte of the return address (at the top of the stack) - banksel TOSH - if LOGGING_ENABLED -; for 4k-word mode: if TOSH < 0x10, we're in the bootloader -; if TOSH >= 0x10, jump to the application interrupt handler - movlw high BOOTLOADER_SIZE - subwf TOSH,w - bnc _bootloader_interrupt - else -; for 512-word mode: if TOSH == 0, we're in the bootloader -; if TOSH != 0, jump to the application interrupt handler - tstf TOSH - bz _bootloader_interrupt - endif - pagesel APP_INTERRUPT + movlp high APP_INTERRUPT ; XC8 *expects* this goto APP_INTERRUPT -; executing from the bootloader? it's a USB interrupt -_bootloader_interrupt - banksel UIR -; reset? - btfss UIR,URSTIF - goto _utrans ; not a reset? just start servicing transactions - call usb_init ; if so, reset the USB interface (clears interrupts) - banksel PIE2 - bsf PIE2,USBIE ; reenable USB interrupts - banksel UIR - bcf UIR,URSTIF ; clear the flag -; service transactions -_utrans banksel UIR - btfss UIR,TRNIF - goto _usdone - movfw USTAT ; stash the status in a temp register - movwf FSR1H - bcf UIR,TRNIF ; clear flag and advance USTAT fifo - banksel BANKED_EP0OUT_STAT - andlw b'01111000' ; check endpoint number - bnz _ucdc ; if not endpoint 0, it's a CDC message - call usb_service_ep0 ; handle the control message - goto _utrans -; clear USB interrupt -_usdone banksel PIR2 - bcf PIR2,USBIF - retfie -_ucdc call usb_service_cdc ; USTAT value is still in FSR1H - goto _utrans - - - -;;; Idle loop. In bootloader mode, the MCU just spins here, and all USB -;;; communication is interrupt-driven. -;;; This snippet is deliberately located within the first 256 words of program -;;; memory, so we can easily check in the interrupt handler if the interrupt -;;; occurred while executing application code or bootloader code. -;;; (TOSH will be 0x00 when executing bootloader code, i.e. this snippet) -bootloader_main_loop - bsf INTCON,GIE ; enable interrupts -_loop - if LOGGING_ENABLED -; Print any pending characters in the log - call log_service - endif - goto _loop - - ;;; Handles a control transfer on endpoint 0. ;;; arguments: expects USTAT value in FSR1H @@ -247,6 +196,9 @@ _usb_ctrl_setup bcf USB_STATE,IS_CONTROL_WRITE ; get bmRequestType, but don't bother checking whether it's standard/class/vendor... ; the CDC and standard requests we'll receive have distinct bRequest numbers + bcf BANKED_EP0OUT_STAT,UOWN ; dearm the OUT endpoint + bcf BANKED_EP0IN_STAT,UOWN ; dearm the IN endpoint + movfw BANKED_EP0OUT_BUF+bmRequestType btfss BANKED_EP0OUT_BUF+bmRequestType,7 ; is this host->device? bsf USB_STATE,IS_CONTROL_WRITE ; if so, this is a control write @@ -344,7 +296,10 @@ _set_data_in_count_from_w movwf EP0_DATA_IN_COUNT ; the count needs to be set to the minimum of the descriptor's length (in W) ; and the requested length - subwf BANKED_EP0OUT_BUF+wLengthL,w ; just ignore high byte... + tstf BANKED_EP0OUT_BUF+wLengthH ; test high byte... + bnz _usb_ctrl_complete ; use length of descriptor + + subwf BANKED_EP0OUT_BUF+wLengthL,w bc _usb_ctrl_complete ; if W <= f, no need to adjust movfw BANKED_EP0OUT_BUF+wLengthL movwf EP0_DATA_IN_COUNT @@ -435,6 +390,8 @@ _bcopy sublw EP0_BUF_SIZE ; have we filled the buffer? ; write back the updated source pointer _bcdone movfw FSR0L movwf EP0_DATA_IN_PTR + +#if ENABLE_POWER_CONFIG ; if we're sending the configuration descriptor, we need to inject the app's ; values for bus power/self power and max current consumption _check_for_config_bmattributes @@ -455,6 +412,7 @@ _check_for_config_bmaxpower movfw APP_POWER_CONFIG bcf WREG,0 ; value is in the upper 7 bits movwf BANKED_EP0IN_BUF+0 +#endif return @@ -492,6 +450,7 @@ usb_service_cdc bz arm_ep1_out ; (just ignore them and rearm the OUT buffer) bcf BANKED_EP1IN_STAT,UOWN call bootloader_exec_cmd ; execute command; status returned in W + banksel BANKED_EP1IN_BUF movwf BANKED_EP1IN_BUF ; copy status to IN buffer movlw 1 @@ -668,6 +627,18 @@ _wosc movlw (1<=2048 - pagesel APP_ENTRY_POINT - endif +#endif + +#if USE_RC3_SWITCH + banksel ANSELC ;enable analog function on pin + bsf ANSELC,ANSC3 +#endif + + movlp high APP_ENTRY_POINT ; attempt to appease certain user apps goto APP_ENTRY_POINT + ; Not entering application code: initialize the USB interface and wait for commands. _bootloader_main ; Enable active clock tuning - banksel ACTCON - movlw (1< + Copyright (C) 19yy + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + +Also add information on how to contact you by electronic and paper mail. + +If the program is interactive, make it output a short notice like this +when it starts in an interactive mode: + + Gnomovision version 69, Copyright (C) 19yy name of author + Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. + This is free software, and you are welcome to redistribute it + under certain conditions; type `show c' for details. + +The hypothetical commands `show w' and `show c' should show the appropriate +parts of the General Public License. Of course, the commands you use may +be called something other than `show w' and `show c'; they could even be +mouse-clicks or menu items--whatever suits your program. + +You should also get your employer (if you work as a programmer) or your +school, if any, to sign a "copyright disclaimer" for the program, if +necessary. Here is a sample; alter the names: + + Yoyodyne, Inc., hereby disclaims all copyright interest in the program + `Gnomovision' (which makes passes at compilers) written by James Hacker. + + , 1 April 1989 + Ty Coon, President of Vice + +This General Public License does not permit incorporating your program into +proprietary programs. If your program is a subroutine library, you may +consider it more useful to permit linking proprietary applications with the +library. If this is what you want to do, use the GNU Library General +Public License instead of this License. diff --git a/win32/USBSERNT.INF b/win32/USBSERNT.INF new file mode 100644 index 0000000..8ba1dc9 --- /dev/null +++ b/win32/USBSERNT.INF @@ -0,0 +1,51 @@ +; Windows 2000 and XP setup File for CDC + +[Version] +Signature="$Windows NT$" +Class=Ports +ClassGuid={4D36E978-E325-11CE-BFC1-08002BE10318} +Provider=%MICROSOFT% +DriverVer=10/15/1999,5.0.2153.1 + +[Manufacturer] +%MICROSOFT%=Device + + +[Device] +%USB_CDC%=Reader, USB\VID_1D50&PID_EEEE + +[Reader_Install.NTx86] +;Windows2000 + +[DestinationDirs] +DefaultDestDir=12 +Reader.NT.Copy=12 + +[Reader.NT] +CopyFiles=Reader.NT.Copy +AddReg=Reader.NT.AddReg + +[Reader.NT.Copy] +;usbser.sys + +[Reader.NT.AddReg] +HKR,,DevLoader,,*ntkern +HKR,,NTMPDriver,,usbser.sys +HKR,,EnumPropPages32,,"MsPorts.dll,SerialPortPropPageProvider" + +[Reader.NT.Services] +AddService = usbser, 0x00000002, Service_Inst + +[Service_Inst] +DisplayName = %Serial.SvcDesc% +ServiceType = 1 ; SERVICE_KERNEL_DRIVER +StartType = 3 ; SERVICE_DEMAND_START +ErrorControl = 1 ; SERVICE_ERROR_NORMAL +ServiceBinary = %12%\usbser.sys +LoadOrderGroup = Base + +[Strings] +MICROSOFT = "Microsoft, Inc." +USB_CDC = "CDC USB to UART" +Serial.SvcDesc = "USB Serial emulation driver" + diff --git a/win32/clean.bat b/win32/clean.bat new file mode 100644 index 0000000..be1878a --- /dev/null +++ b/win32/clean.bat @@ -0,0 +1,3 @@ +@echo off +if exist *.o del *.o +if exist *.exe del *.exe diff --git a/win32/compile.bat b/win32/compile.bat new file mode 100644 index 0000000..6790182 --- /dev/null +++ b/win32/compile.bat @@ -0,0 +1,6 @@ +@echo ************************** +@echo *** Use MinGW compiler *** +@echo ************************** +@set PATH=c:\mingw\bin;d:\mingw\bin;%PATH% +@set CC=gcc +mingw32-make.exe -f make-mingw diff --git a/win32/make-mingw b/win32/make-mingw new file mode 100644 index 0000000..5df24d4 --- /dev/null +++ b/win32/make-mingw @@ -0,0 +1,18 @@ +CC = gcc +SVNVERS = $(shell svnversion) +CFLAGS = -Wall -g -O -DMINGW32 -Ilibusb-win32 -Ihidapi -DSVNVERSION='"$(SVNVERS)"' +LDFLAGS = -s + + + +PROG_OBJS = usb16f1prog.o sp.o + +all: usb16f1prog.exe + +usb16f1prog.exe:$(PROG_OBJS) + $(CC) $(LDFLAGS) -o $@ $(PROG_OBJS) $(LIBS) + + +### +#pic32prog.o: pic32prog.c target.h localize.h +#target.o: target.c target.h adapter.h localize.h pic32.h diff --git a/win32/sp.c b/win32/sp.c new file mode 100644 index 0000000..1b718ce --- /dev/null +++ b/win32/sp.c @@ -0,0 +1,91 @@ +#include "windows.h" + + +HANDLE sp_open(LPSTR ComPort, DWORD BitRate, DWORD ReadTimeOut) +{ + HANDLE hComm; + DCB dcb = {0}; + COMMTIMEOUTS CommTmOut = {0}; + + // Get a handle to the port. + hComm = CreateFile(ComPort, + GENERIC_READ | GENERIC_WRITE, + 0, + NULL, + OPEN_EXISTING, + FILE_ATTRIBUTE_NORMAL, + 0); + if (hComm == INVALID_HANDLE_VALUE) + return INVALID_HANDLE_VALUE; + +/* + // Get the current state of the port + FillMemory(&dcb, sizeof(dcb), 0); + + + + if (!GetCommState(hComm, &dcb)) + { + return INVALID_HANDLE_VALUE; + } + else + { + dcb.BaudRate = BitRate; + dcb.Parity = NOPARITY; + dcb.StopBits = ONESTOPBIT; + dcb.fDtrControl = DTR_CONTROL_DISABLE; + dcb.fRtsControl = RTS_CONTROL_DISABLE; + dcb.fOutX = FALSE; + dcb.fInX = FALSE; + dcb.fOutxCtsFlow = FALSE; + dcb.fOutxDsrFlow = FALSE; + dcb.ByteSize = 8; + } + + if (!SetCommState(hComm, &dcb)) + return INVALID_HANDLE_VALUE; + +*/ + // Set the timeout conditions + if (!GetCommTimeouts(hComm, &CommTmOut)) + { + return INVALID_HANDLE_VALUE; + } + else + { + CommTmOut.ReadIntervalTimeout = 110; + CommTmOut.ReadTotalTimeoutMultiplier = 0; + CommTmOut.ReadTotalTimeoutConstant = 100; + CommTmOut.WriteTotalTimeoutMultiplier = 0; + CommTmOut.WriteTotalTimeoutConstant = 100; + } + + if (!SetCommTimeouts(hComm, &CommTmOut)) + return INVALID_HANDLE_VALUE; + + PurgeComm(hComm,PURGE_TXABORT|PURGE_RXABORT|PURGE_TXCLEAR|PURGE_RXCLEAR); + + return hComm; +} + +INT sp_close(HANDLE hComPort) +{ + return CloseHandle(hComPort); +} + +int sp_write(HANDLE hComPort, unsigned char *Data, unsigned short Len) +{ + DWORD BytesWritten; + + if (!WriteFile(hComPort, Data, Len, &BytesWritten, NULL)) return -1; + return BytesWritten; +} + +int sp_read(HANDLE hComPort, unsigned char *Data, unsigned short Len) +{ + DWORD BytesRead; + + if (!ReadFile(hComPort, Data, Len, &BytesRead, NULL)) return -1; + return BytesRead; +} + diff --git a/win32/sp.h b/win32/sp.h new file mode 100644 index 0000000..1c32c4c --- /dev/null +++ b/win32/sp.h @@ -0,0 +1,19 @@ +#ifndef __SP_H +#define __SP_H + +#ifdef __cplusplus +extern "C" +{ +#endif + +HANDLE sp_open(LPSTR ComPort, DWORD BitRate, DWORD ReadTimeOut); +INT sp_close(HANDLE hComPort); +int sp_write(HANDLE hComPort, unsigned char *Data, unsigned short Len); +int sp_read(HANDLE hComPort, unsigned char *Data, unsigned short Len); + +#ifdef __cplusplus +} +#endif + +#endif + diff --git a/win32/usb16f1prog.c b/win32/usb16f1prog.c new file mode 100644 index 0000000..155a94d --- /dev/null +++ b/win32/usb16f1prog.c @@ -0,0 +1,419 @@ +// +// usb16f1prog +// Copyright (c) 2017, David Pribyl +// v1.0, 28.8.2017 +// +// This file is distributed under the terms of the GNU +// General Public License (GPL). See the accompanying file "COPYING" +// for more details. +// +// Uploads firmware to a PIC16F1xxx microcontroller programmed with +// Matt Sarnoff's USB bootloader. +// +// Accepts 16-bit Intel HEX files as input. +// Can only be used to write to program memory; the bootloader does not support +// writing the configuration words or user ID words, so values at those +// addresses in the input file are ignored. +// +// The programming protocol is very simplistic. There are three requests, +// distinguished by their length: Set Parameters (4 bytes), Write (64 bytes), +// and Reset (1 byte). After a Set Parameters or Write command, the host (e.g. +// this script) must wait for a 1-byte status response. If the response byte is +// 0x01, the operation succeeded. Otherwise, the host should abort. (See below +// for possible error values.) +// +// Set Parameters is 4 bytes long: +// - addressLowByte +// - addressHighByte +// - expectedChecksum +// - shouldErase +// addressLowByte/HighByte is the 16-bit word address, aligned to a 32-word +// boundary, where data should be written. +// expectedChecksum is the 8-bit checksum of the 32 words to be written at the +// specified address. (This is the 2's complement of the byte-wise sum mod 256 +// of the upcoming 32 words.) +// If the shouldErase byte is 0x45 ('E'), the flash row at that address is +// erased. An erase is mandatory before a Write command. +// +// Write is 64 bytes long: +// - dataWord0LowByte +// - dataWord0HighByte +// ... +// - dataWord31LowByte +// - dataWord31HighByte +// In other words, exactly 32 words, little-endian. If less than 32 words are +// to be written, the sequence should be padded out with 0x3FFF (0xFF, 0x3F). +// The device may return an error if the checksum of the data does not match +// the value sent in the last Set Parameters command, or if the values in flash +// do not match the supplied data after the write. (The latter may happen if you +// attempt to write to an address outside the device's ROM space, or to the +// bootloader region.) +// +// Reset is 1 byte long. If it is 0x52 ('R'), the device is reset, and no status +// is returned. + +#include +#include +#include +#include +#include "sp.h" + + +//#define TEST 1 + +#define HE_EOF -1 /* unexpected EOF */ +#define HE_DEX -2 /* hex digit required */ +#define HE_CEX -3 /* missing ':' */ +#define HE_CHK -4 /* checksum error */ +//#define HE_IGN 1 /* warning that some records were ignored */ + +#define STATUS_OK 1 +#define STATUS_INVALID_COMMAND 2 +#define STATUS_INVALID_CHECKSUM 3 +#define STATUS_VERIFY_FAILED 4 + +#define BCMD_ERASE 0x45 +#define BCMD_RESET 0x52 + + +#define MAXPROG 8192*2 + + +unsigned char progmem[MAXPROG]; + +static int check; +static int fail; + +/*****************************************************************************************/ +static unsigned hexdigit(FILE *fp) +{ + int c; + + if ( fail ) + return 0; + + if ( (c=getc(fp)) == EOF ) { + fail = HE_EOF; /* unexpected EOF */ + return 0; + } + + c -= c>'9'? 'A'-10: '0'; + if ( c<0 || c>0xF ) + fail = HE_DEX; /* hex digit expected */ + return c; +} + +/*****************************************************************************************/ + +static unsigned hexbyte(FILE *fp) +{ + unsigned b; + + b = hexdigit(fp); + b = (b<<4) + hexdigit(fp); + check += b; + return b; +} + +/*****************************************************************************************/ + +static unsigned hexword(FILE *fp) +{ + unsigned w; + + w = hexbyte(fp); + w = (w<<8) + hexbyte(fp); + return w; +} + +/*****************************************************************************************/ + +#define HEXBYTE() hexbyte(fp); if (fail) return fail +#define HEXWORD() hexword(fp); if (fail) return fail + + +/*****************************************************************************************/ + +int loadhex(FILE *fp) +{ + int address; + int adrhi=0; + int type; + int linelen; + int i; + unsigned char b; + + fail = 0; + + memset(progmem,0xff,MAXPROG); + + type = 0; + while ( type != 1 ) { + if ( getc(fp) != ':' ) return HE_CEX; /* expected ':' */ + check = 0; + linelen = HEXBYTE(); + address = HEXWORD(); + type = HEXBYTE(); + + if (type==0) { //Data + if (adrhi==0) { //progmem + for ( i=0; i>8)&0xFF; + Buff[2]=checksum; + Buff[3]=BCMD_ERASE; + +#if TEST + Sleep(10); + Back=0; +#else + ret=sp_write(hComPort,Buff,sizeof(Buff)); + + if (ret==sizeof(Buff)) { + ret=sp_read(hComPort,&Status,1); + if (ret==1) { + if (Status==STATUS_OK) Back=0; + else device_error(Status); + + } + } +#endif + return Back; +} + + +int device_write(HANDLE hComPort, unsigned char * Row) { + unsigned char Buff[64]; + unsigned char Status; + int Back=1; //Error + int i,ret; + + + for(i=0;i<64;i+=2) { + Buff[i]=Row[i]; + Buff[i+1]=Row[i+1]&0x3F; + + } +#if TEST + Sleep(50); + Back=0; +#else + ret=sp_write(hComPort,Buff,sizeof(Buff)); + + if (ret==sizeof(Buff)) { //64 + ret=sp_read(hComPort,&Status,1); + if (ret==1) { + if (Status==STATUS_OK) Back=0; + else device_error(Status); + + } + } +#endif + return Back; +} + +int device_reset(HANDLE hComPort) { + unsigned char Cmd; + int Back=1; //Error + int ret; + + Cmd=BCMD_RESET; + ret=sp_write(hComPort,&Cmd,1); + + if (ret==1) Back=0; + + return Back; +} + + + +/*****************************************************************************************/ +unsigned char ProgLine[64+3]; + +void PrepareProgressLine(void) { + ProgLine[0]='['; + memset(ProgLine+1,'B',4); + memset(ProgLine+1+4,'-',64-4); + ProgLine[65]=']'; + ProgLine[66]=0; + +} +void ShowProgressLine(int Addr,char Action) { + int Pos=Addr/(32*4); + ProgLine[Pos+1]=Action; + printf("\r %s",ProgLine); +} + +int main(int argc, char *argv[]) { + + int ret=-1; + FILE *fp; + int startaddr=0; + int endaddr=0; + int i; + HANDLE ComPort; + + if (argc > 2) { + printf ("usb16f1prog:\n"); + + printf (" Loading hex file ...\n"); + fp=fopen(argv[2],"r"); + if (fp!=NULL) { + ret=loadhex(fp); + fclose(fp); + } + + if (ret!=0) { + printf("\nError - unable to read '%s' file\n",argv[2]); + + } else { //Analize progmem + for (i=0;i2 + printf ("\nUsage:\n"); + printf (" usb16f1prog com1 file.hex\n"); + printf (" com1 .. bootloader serial port\n"); + printf (" file.hex .. program to write\n"); + printf ("\n"); + } + return 0; +} + + + diff --git a/win32/usb16f1prog.dsp b/win32/usb16f1prog.dsp new file mode 100644 index 0000000..0a3e8a9e --- /dev/null +++ b/win32/usb16f1prog.dsp @@ -0,0 +1,108 @@ +# Microsoft Developer Studio Project File - Name="usb16f1prog" - Package Owner=<4> +# Microsoft Developer Studio Generated Build File, Format Version 6.00 +# ** DO NOT EDIT ** + +# TARGTYPE "Win32 (x86) Console Application" 0x0103 + +CFG=usb16f1prog - Win32 Debug +!MESSAGE This is not a valid makefile. To build this project using NMAKE, +!MESSAGE use the Export Makefile command and run +!MESSAGE +!MESSAGE NMAKE /f "usb16f1prog.mak". +!MESSAGE +!MESSAGE You can specify a configuration when running NMAKE +!MESSAGE by defining the macro CFG on the command line. For example: +!MESSAGE +!MESSAGE NMAKE /f "usb16f1prog.mak" CFG="usb16f1prog - Win32 Debug" +!MESSAGE +!MESSAGE Possible choices for configuration are: +!MESSAGE +!MESSAGE "usb16f1prog - Win32 Release" (based on "Win32 (x86) Console Application") +!MESSAGE "usb16f1prog - Win32 Debug" (based on "Win32 (x86) Console Application") +!MESSAGE + +# Begin Project +# PROP AllowPerConfigDependencies 0 +# PROP Scc_ProjName "" +# PROP Scc_LocalPath "" +CPP=cl.exe +RSC=rc.exe + +!IF "$(CFG)" == "usb16f1prog - Win32 Release" + +# PROP BASE Use_MFC 0 +# PROP BASE Use_Debug_Libraries 0 +# PROP BASE Output_Dir "Release" +# PROP BASE Intermediate_Dir "Release" +# PROP BASE Target_Dir "" +# PROP Use_MFC 0 +# PROP Use_Debug_Libraries 0 +# PROP Output_Dir "Release" +# PROP Intermediate_Dir "Release" +# PROP Target_Dir "" +# ADD BASE CPP /nologo /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /c +# ADD CPP /nologo /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /c +# ADD BASE RSC /l 0x405 /d "NDEBUG" +# ADD RSC /l 0x405 /d "NDEBUG" +BSC32=bscmake.exe +# ADD BASE BSC32 /nologo +# ADD BSC32 /nologo +LINK32=link.exe +# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /machine:I386 +# ADD LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /machine:I386 + +!ELSEIF "$(CFG)" == "usb16f1prog - Win32 Debug" + +# PROP BASE Use_MFC 0 +# PROP BASE Use_Debug_Libraries 1 +# PROP BASE Output_Dir "Debug" +# PROP BASE Intermediate_Dir "Debug" +# PROP BASE Target_Dir "" +# PROP Use_MFC 0 +# PROP Use_Debug_Libraries 1 +# PROP Output_Dir "Debug" +# PROP Intermediate_Dir "Debug" +# PROP Target_Dir "" +# ADD BASE CPP /nologo /W3 /Gm /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /GZ /c +# ADD CPP /nologo /W3 /Gm /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /GZ /c +# ADD BASE RSC /l 0x405 /d "_DEBUG" +# ADD RSC /l 0x405 /d "_DEBUG" +BSC32=bscmake.exe +# ADD BASE BSC32 /nologo +# ADD BSC32 /nologo +LINK32=link.exe +# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /debug /machine:I386 /pdbtype:sept +# ADD LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /debug /machine:I386 /pdbtype:sept + +!ENDIF + +# Begin Target + +# Name "usb16f1prog - Win32 Release" +# Name "usb16f1prog - Win32 Debug" +# Begin Group "Source Files" + +# PROP Default_Filter "cpp;c;cxx;rc;def;r;odl;idl;hpj;bat" +# Begin Source File + +SOURCE=.\sp.c +# End Source File +# Begin Source File + +SOURCE=.\usb16f1prog.c +# End Source File +# End Group +# Begin Group "Header Files" + +# PROP Default_Filter "h;hpp;hxx;hm;inl" +# Begin Source File + +SOURCE=.\sp.h +# End Source File +# End Group +# Begin Group "Resource Files" + +# PROP Default_Filter "ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe" +# End Group +# End Target +# End Project diff --git a/win32/usb16f1prog.dsw b/win32/usb16f1prog.dsw new file mode 100644 index 0000000..dbbedbc --- /dev/null +++ b/win32/usb16f1prog.dsw @@ -0,0 +1,29 @@ +Microsoft Developer Studio Workspace File, Format Version 6.00 +# WARNING: DO NOT EDIT OR DELETE THIS WORKSPACE FILE! + +############################################################################### + +Project: "usb16f1prog"=.\usb16f1prog.dsp - Package Owner=<4> + +Package=<5> +{{{ +}}} + +Package=<4> +{{{ +}}} + +############################################################################### + +Global: + +Package=<5> +{{{ +}}} + +Package=<3> +{{{ +}}} + +############################################################################### + diff --git a/win32/usb16f1prog.exe b/win32/usb16f1prog.exe new file mode 100644 index 0000000..fbfecd1 Binary files /dev/null and b/win32/usb16f1prog.exe differ