-
Notifications
You must be signed in to change notification settings - Fork 0
/
pk3_gun.bat
executable file
·170 lines (139 loc) · 3.81 KB
/
pk3_gun.bat
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
@echo off
rem -------------------------------------------------
rem pk3_gun uses PICkit 3 Command Line Interface.
rem This batch script is a pic firmware programmer that can load more than one hex file and protect the device from the duplicated writing.
rem -------------------------------------------------
setlocal ENABLEDELAYEDEXPANSION
set TARGET_CMD="pk3cmd.exe"
set USED_FIRMWARE_LIST=used_list.txt
rem ********* main routine *********
rem call :clear_arr
call :get_arg %1
if %errorlevel%==-1 exit /b 0
call :cmd_search
if %errorlevel%==-1 exit /b 0
call :read_used_list
for /F "usebackq delims=" %%j in (`dir /A-D /s /b %target_dir%\*.hex`) do (
rem call :start_message
rem pause
call :is_used_firmware "%%j"
if !errorlevel!==1 (
rem --- continue ---
echo continue..
) else (
rem --- execute writing ---
call :start_message
pause
echo writing "%%j"
call :write_firmware "%%j"
if !errorlevel!==-1 (
echo Device error : !errorlevel!
exit /b 0
)
call :append_used_firmware "%%j"
call :end_message
)
)
call :clear_arr
echo ---- All processes done ----
exit /b 0
rem ----- main end -----
rem ********* sub routines *********
rem ----- Get argument -----
:get_arg (
if "%1"=="" (
echo ERROR : Specify the path to the hex files.
exit /b -1
)
set target_dir=%~f1
exit /b 0
)
rem ----- Search for mplab_ipe cli -----
:cmd_search (
pushd C:\
for /F "usebackq delims=" %%i in (`dir /A-D /s /b %TARGET_CMD%`) do (
set cli_cmd="%%i"
)
if "%cli_cmd%" EQU "" (
echo %TARGET_CMD% is not found.
pushd %0\..
exit /b -1
)
echo %cli_cmd% was found.
pushd %0\..
exit /b 0
)
rem ----- read the used firmware list -----
:read_used_list (
if Not exist %~dp0%USED_FIRMWARE_LIST% (
echo %~dp0%USED_FIRMWARE_LIST% is not found.
exit /b 0
)
rem -- extract list to memory --
set /a cnt=0
for /f "delims=" %%k in (%~dp0%USED_FIRMWARE_LIST%) do (
set /a cnt+=1
set list[!cnt!]=%%k
)
set /a max_cnt=%cnt%
exit /b 0
)
rem ----- check if the hex file is used -----
:is_used_firmware (
if "%list[1]%" EQU "" (
echo No used file detected.
exit /b 0
)
for /L %%l in (1,1,%max_cnt%) do (
if "!%~nx1"=="!list[%%l]!" (
echo %~nx1 was already used. Skip.
exit /b 1
)
)
exit /b 0
)
rem ------ Append the used firmware to the list ------
:append_used_firmware (
if Not exist %~dp0%USED_FIRMWARE_LIST% (
echo %~nx1> %~dp0%USED_FIRMWARE_LIST%
exit /b 0
)
echo %~nx1>> %~dp0%USED_FIRMWARE_LIST%
exit /b 0
)
rem ----- Program writing routine -----
:write_firmware (
rem %cli_cmd% -P16LF1938 -E
%cli_cmd% -P16LF1938 -F%1 -M -V3.3
if Not !errorlevel!==0 exit /b -1
exit /b 0
)
rem ----- Start Message -----
:start_message (
echo;
echo;
echo #######################################################
echo set a pic device you are about to write the firmware in.
echo #######################################################
exit /b 0
)
rem ----- End Message -----
:end_message (
echo;
echo #######################################################
echo Writing the firmware is done. Replace this pic device.
echo #######################################################
exit /b 0
)
:check (
for /L %%i in (1,1,%max_cnt%) do (
echo %%i : !list[%%i]!
)
exit /b 0
)
:clear_arr (
for /L %%l in (1,1,%max_cnt%) do (
set list[%%l]=
)
exit /b 0
)