Rev 2061 Rev 2062
1 /******************** (C) COPYRIGHT 2010 STMicroelectronics ******************** 1 /******************** (C) COPYRIGHT 2010 STMicroelectronics ********************
2 * File Name : STMFlashLoader.cpp 2 * File Name : STMFlashLoader.cpp
3 * Author : MCD Application Team 3 * Author : MCD Application Team
4 * Version : v2.2.0 4 * Version : v2.2.0
5 * Date : 05/03/2010 5 * Date : 05/03/2010
6 * Description : STM Flash Loader command line version 6 * Description : STM Flash Loader command line version
7 ******************************************************************************** 7 ********************************************************************************
8 * THE PRESENT SOFTWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS 8 * THE PRESENT SOFTWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
9 * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME. 9 * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME.
10 * AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT, 10 * AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT,
11 * INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE 11 * INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE
12 * CONTENT OF SUCH SOFTWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING 12 * CONTENT OF SUCH SOFTWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING
13 * INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. 13 * INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
14 *******************************************************************************/ 14 *******************************************************************************/
15   15  
16 #include "stdafx.h" 16 #include "stdafx.h"
17 #include "string.h" 17 #include "string.h"
18 #include <stdio.h> 18 #include <stdio.h>
19 #include <string.h> 19 #include <string.h>
20 #include <stdlib.h> 20 #include <stdlib.h>
21 #include <errno.h> 21 #include <errno.h>
22 #include <dos.h> 22 #include <dos.h>
23   23  
24 #include "../STBLLIB/STBLLIB.h" 24 #include "../STBLLIB/STBLLIB.h"
25 #include "../Files/Files.h" 25 #include "../Files/Files.h"
26 #include "ini.h" 26 #include "ini.h"
27   27  
28   28  
29 #define NONE = 0; 29 #define NONE = 0;
30 #define ODD = 1; 30 #define ODD = 1;
31 #define EVEN = 2; 31 #define EVEN = 2;
32   32  
33 typedef enum STATE {OK,KO}; 33 typedef enum STATE {OK,KO};
34   34  
35 char MapFile[256]; 35 char MapFile[256];
36 PMAPPING pmMapping; 36 PMAPPING pmMapping;
37   37  
38 int TimeBO = 100; 38 int TimeBO = 100;
39   39  
40 BOOL SHOW_OK = TRUE; // Set to TRUE/FALSE to show/hide OK status messages 40 BOOL SHOW_OK = TRUE; // Set to TRUE/FALSE to show/hide OK status messages
41 BOOL SHOW_KO = TRUE; // Set to TRUE/FALSE to show/hide KO status messages 41 BOOL SHOW_KO = TRUE; // Set to TRUE/FALSE to show/hide KO status messages
42   42  
43 /*******************************************************************************************/ 43 /*******************************************************************************************/
44 /* Function : FileExist */ 44 /* Function : FileExist */
45 /* IN : file name */ 45 /* IN : file name */
46 /* OUT : boolean */ 46 /* OUT : boolean */
47 /* Description : verify if the given file exists */ 47 /* Description : verify if the given file exists */
48 /*******************************************************************************************/ 48 /*******************************************************************************************/
49 BOOL FileExist(LPCTSTR filename) 49 BOOL FileExist(LPCTSTR filename)
50 { 50 {
51 // Data structure for FindFirstFile 51 // Data structure for FindFirstFile
52 WIN32_FIND_DATA findData; 52 WIN32_FIND_DATA findData;
53   53  
54 // Clear find structure 54 // Clear find structure
55 ZeroMemory(&findData, sizeof(findData)); 55 ZeroMemory(&findData, sizeof(findData));
56   56  
57 // Search the file 57 // Search the file
58 HANDLE hFind = FindFirstFile( filename, &findData ); 58 HANDLE hFind = FindFirstFile( filename, &findData );
59 if ( hFind == INVALID_HANDLE_VALUE ) 59 if ( hFind == INVALID_HANDLE_VALUE )
60 { 60 {
61 // File not found 61 // File not found
62 return false; 62 return false;
63 } 63 }
64   64  
65 // File found 65 // File found
66   66  
67 // Release find handle 67 // Release find handle
68 FindClose( hFind ); 68 FindClose( hFind );
69 hFind = NULL; 69 hFind = NULL;
70   70  
71 // The file exists 71 // The file exists
72 return true; 72 return true;
73 } 73 }
74   74  
75 /*******************************************************************************************/ 75 /*******************************************************************************************/
76 /* Function : void man() */ 76 /* Function : void man() */
77 /* IN : */ 77 /* IN : */
78 /* OUT : */ 78 /* OUT : */
79 /* Description : print the manual on the standard output */ 79 /* Description : print the manual on the standard output */
80 /*******************************************************************************************/ 80 /*******************************************************************************************/
81 void man() 81 void man()
82 { 82 {
83 printf("STMicroelectronics UART Flash Loader command line v2.2.0 \n\n"); 83 printf("STMicroelectronics UART Flash Loader command line v2.2.0 \n\n");
84 printf(" Usage : \n\n"); 84 printf(" Usage : \n\n");
85 printf(" STMFlashLoader.exe [options] [Agrument][[options] [Agrument]...] \n\n"); 85 printf(" STMFlashLoader.exe [options] [Agrument][[options] [Agrument]...] \n\n");
86 86
87 printf(" -? (Show this help) \n"); 87 printf(" -? (Show this help) \n");
88 printf(" -c (Establish connection to the COM port) \n"); 88 printf(" -c (Establish connection to the COM port) \n");
89 printf(" --pn port_nb : e.g: 1, 2 ..., default 1 \n"); 89 printf(" --pn port_nb : e.g: 1, 2 ..., default 1 \n");
90 printf(" --br baud_rate : e.g: 115200, 57600 ..., default 57600 \n"); 90 printf(" --br baud_rate : e.g: 115200, 57600 ..., default 57600 \n");
91 printf(" --db data_bits : value in {5,6,7,8} ..., default 8 \n"); 91 printf(" --db data_bits : value in {5,6,7,8} ..., default 8 \n");
92 printf(" --pr parity : value in {NONE,ODD,EVEN} ..., default EVEN \n"); 92 printf(" --pr parity : value in {NONE,ODD,EVEN} ..., default EVEN \n");
93 printf(" --sb stop_bits : value in {1,1.5,2} ..., default 1 \n"); 93 printf(" --sb stop_bits : value in {1,1.5,2} ..., default 1 \n");
94 printf(" --ec echo : value OFF or ECHO or LISTEN ..., default is OFF \n"); 94 printf(" --ec echo : value OFF or ECHO or LISTEN ..., default is OFF \n");
95 printf(" --co control : Enable or Disable RTS and DTR outputs control \n"); 95 printf(" --co control : Enable or Disable RTS and DTR outputs control \n");
96 printf(" : value OFF or ON ..., default is OFF \n"); 96 printf(" : value OFF or ON ..., default is OFF \n");
97 printf(" --to time_out : (ms) e.g 1000, 2000, 3000 ..., default 5000 \n"); 97 printf(" --to time_out : (ms) e.g 1000, 2000, 3000 ..., default 5000 \n");
98   98  
99 printf(" -Rts (set Rts line to Hi, Lo)\n"); 99 printf(" -Rts (set Rts line to Hi, Lo)\n");
100 printf(" --State : State in {Hi, Lo} \n"); 100 printf(" --State : State in {Hi, Lo} \n");
101 printf(" -Dtr (Set Rts line to Hi, Lo)\n"); 101 printf(" -Dtr (Set Rts line to Hi, Lo)\n");
102 printf(" --State : State in {Hi, Lo}\n"); 102 printf(" --State : State in {Hi, Lo}\n");
103 103
104 printf(" -i device_name (e.g STM32_Low-density_16K, [See the Map directory]) \n"); 104 printf(" -i device_name (e.g STM32_Low-density_16K, [See the Map directory]) \n");
105   105  
106 printf(" -e (erase flash pages\n"); 106 printf(" -e (erase flash pages\n");
107 printf(" --all all pages : erase all pages\n"); 107 printf(" --all all pages : erase all pages\n");
108 printf(" --sec number_of_pages_group pages_group_codes : erase specified group pages \n"); 108 printf(" --sec number_of_pages_group pages_group_codes : erase specified group pages \n");
109   109  
110 printf(" -u (Upload flash contents to a .bin, .hex or .s19 file )\n"); 110 printf(" -u (Upload flash contents to a .bin, .hex or .s19 file )\n");
111 printf(" --fn file_name : full path name of the file \n"); 111 printf(" --fn file_name : full path name of the file \n");
112   112  
113 printf(" -d (Download the content of a file into MCU flash) \n"); 113 printf(" -d (Download the content of a file into MCU flash) \n");
114 printf(" --a address(hex): start @ in hex ; ignored if it is not a binary file \n"); 114 printf(" --a address(hex): start @ in hex ; ignored if it is not a binary file \n");
115 printf(" --fn file_name : full path name (.bin, .hex or .s19 file) \n"); 115 printf(" --fn file_name : full path name (.bin, .hex or .s19 file) \n");
116 printf(" --v : verify after download \n"); 116 printf(" --v : verify after download \n");
117 printf(" --o : optimize; removes FFs data \n"); 117 printf(" --o : optimize; removes FFs data \n");
118   118  
119   119  
120 printf(" -r (Run the flash code at the specified address \n"); 120 printf(" -r (Run the flash code at the specified address \n");
121 printf(" --a address(hex) : address in hexadecimal) \n"); 121 printf(" --a address(hex) : address in hexadecimal) \n");
122   122  
123   123  
124 printf(" -p (Enable or Disable protections) \n"); 124 printf(" -p (Enable or Disable protections) \n");
125 printf(" --ewp : enable write protection for sector codes (e.g 1,2,etc.) \n"); 125 printf(" --ewp : enable write protection for sector codes (e.g 1,2,etc.) \n");
126 printf(" --dwp : disable write protection \n"); 126 printf(" --dwp : disable write protection \n");
127 printf(" --drp : disable read protection \n"); 127 printf(" --drp : disable read protection \n");
128 printf(" --erp : enable read protection, all arguments following this one will fail \n"); 128 printf(" --erp : enable read protection, all arguments following this one will fail \n");
129   129  
130   130  
131 printf(" -o (Get or Set STM32 option bytes: use -d command for STM8!) \n"); 131 printf(" -o (Get or Set STM32 option bytes: use -d command for STM8!) \n");
132 printf(" --get --fn file_name : get option bytes from the device \n"); 132 printf(" --get --fn file_name : get option bytes from the device \n");
133 printf(" and write it in the specified file \n"); 133 printf(" and write it in the specified file \n");
134 printf(" --set --fn file_name : load option bytes from the specified file \n"); 134 printf(" --set --fn file_name : load option bytes from the specified file \n");
135 printf(" and write it to the device \n"); 135 printf(" and write it to the device \n");
136 printf(" --set --vals --OPB hex_val : set the specified option byte; OPB in: User, \n"); 136 printf(" --set --vals --OPB hex_val : set the specified option byte; OPB in: User, \n");
137 printf(" RDP, Data0, Data1, WRP0, WRP1, WRP2, WRP3 \n"); 137 printf(" RDP, Data0, Data1, WRP0, WRP1, WRP2, WRP3 \n");
138   138  
139   139  
140 } 140 }
141   141  
142   142  
143   143  
144   144  
145   145  
146 /*******************************************************************************************/ 146 /*******************************************************************************************/
147 /* Function : ParityToInt */ 147 /* Function : ParityToInt */
148 /* IN : parity as string (NONE, ODD, EVEN) */ 148 /* IN : parity as string (NONE, ODD, EVEN) */
149 /* OUT : integer */ 149 /* OUT : integer */
150 /* Description : Get the integer representation of the given parity */ 150 /* Description : Get the integer representation of the given parity */
151 /*******************************************************************************************/ 151 /*******************************************************************************************/
152 int ParityToInt(char* parity) 152 int ParityToInt(char* parity)
153 { 153 {
154 if (strcmp(parity,"NONE")==0) return 0; 154 if (strcmp(parity,"NONE")==0) return 0;
155 else if(strcmp(parity,"ODD")==0) return 1; 155 else if(strcmp(parity,"ODD")==0) return 1;
156 else if(strcmp(parity,"EVEN")==0) return 2; 156 else if(strcmp(parity,"EVEN")==0) return 2;
157 else return 2; 157 else return 2;
158 } 158 }
159   159  
160   160  
161 /*******************************************************************************************/ 161 /*******************************************************************************************/
162 /* Function : ModeToInt */ 162 /* Function : ModeToInt */
163 /* IN : Mode as string (OFF, ECHO, LISTEN) */ 163 /* IN : Mode as string (OFF, ECHO, LISTEN) */
164 /* OUT : int */ 164 /* OUT : int */
165 /* Description : Get the int representation of the given string Mode */ 165 /* Description : Get the int representation of the given string Mode */
166 /*******************************************************************************************/ 166 /*******************************************************************************************/
167 int ModeToInt(char* status) 167 int ModeToInt(char* status)
168 { 168 {
169 if (strcmp(status,"OFF")==0) return 0; 169 if (strcmp(status,"OFF")==0) return 0;
170 else if(strcmp(status,"ECHO")==0) return 1; 170 else if(strcmp(status,"ECHO")==0) return 1;
171 else if(strcmp(status,"LISTEN")==0) return 2; 171 else if(strcmp(status,"LISTEN")==0) return 2;
172 else return 0; 172 else return 0;
173 } 173 }
174   174  
175 /*******************************************************************************************/ 175 /*******************************************************************************************/
176 /* Function : StatusToBool */ 176 /* Function : StatusToBool */
177 /* IN : Status as string (ON, OFF) */ 177 /* IN : Status as string (ON, OFF) */
178 /* OUT : Bool */ 178 /* OUT : Bool */
179 /* Description : Get the boolean representation of the given string ON/OFF */ 179 /* Description : Get the boolean representation of the given string ON/OFF */
180 /*******************************************************************************************/ 180 /*******************************************************************************************/
181 bool StatusToBool(char* status) 181 bool StatusToBool(char* status)
182 { 182 {
183 if (strcmp(status,"OFF")==0) return false; 183 if (strcmp(status,"OFF")==0) return false;
184 else if(strcmp(status,"ON")==0) return true; 184 else if(strcmp(status,"ON")==0) return true;
185 else return false; 185 else return false;
186 } 186 }
187   187  
188   188  
189 /*******************************************************************************************/ 189 /*******************************************************************************************/
190 /* Function : Is_Option */ 190 /* Function : Is_Option */
191 /* IN : option as string */ 191 /* IN : option as string */
192 /* OUT : boolean */ 192 /* OUT : boolean */
193 /* Description : Verify if the given string present an option */ 193 /* Description : Verify if the given string present an option */
194 /*******************************************************************************************/ 194 /*******************************************************************************************/
195 bool Is_Option(char* option) 195 bool Is_Option(char* option)
196 { 196 {
197 if (strcmp(option,"-?")==0) return true; 197 if (strcmp(option,"-?")==0) return true;
198 else if (strcmp(option,"-c")==0) return true; 198 else if (strcmp(option,"-c")==0) return true;
199 else if (strcmp(option,"-i")==0) return true; 199 else if (strcmp(option,"-i")==0) return true;
200 else if (strcmp(option,"-e")==0) return true; 200 else if (strcmp(option,"-e")==0) return true;
201 else if (strcmp(option,"-u")==0) return true; 201 else if (strcmp(option,"-u")==0) return true;
202 else if (strcmp(option,"-d")==0) return true; 202 else if (strcmp(option,"-d")==0) return true;
203 else if (strcmp(option,"-v")==0) return true; 203 else if (strcmp(option,"-v")==0) return true;
204 else if (strcmp(option,"-p")==0) return true; 204 else if (strcmp(option,"-p")==0) return true;
205 else if (strcmp(option,"-r")==0) return true; 205 else if (strcmp(option,"-r")==0) return true;
206 else if (strcmp(option,"-o")==0) return true; 206 else if (strcmp(option,"-o")==0) return true;
207 else if (strcmp(option,"-Rts")==0) return true; 207 else if (strcmp(option,"-Rts")==0) return true;
208 else if (strcmp(option,"-Dtr")==0) return true; 208 else if (strcmp(option,"-Dtr")==0) return true;
209 else if (strcmp(option,"-Auto")==0) return true; 209 else if (strcmp(option,"-Auto")==0) return true;
210 else return false; 210 else return false;
211 } 211 }
212   212  
213 /*******************************************************************************************/ 213 /*******************************************************************************************/
214 /* Function : Is_SubOption */ 214 /* Function : Is_SubOption */
215 /* IN : sub-option as string */ 215 /* IN : sub-option as string */
216 /* OUT : boolean */ 216 /* OUT : boolean */
217 /* Description : Verify if the given string present a sub-option */ 217 /* Description : Verify if the given string present a sub-option */
218 /*******************************************************************************************/ 218 /*******************************************************************************************/
219 bool Is_SubOption(char* suboption) 219 bool Is_SubOption(char* suboption)
220 { 220 {
221 if (strcmp(suboption,"--pn")==0) return true; 221 if (strcmp(suboption,"--pn")==0) return true;
222 else if (strcmp(suboption,"--br")==0) return true; 222 else if (strcmp(suboption,"--br")==0) return true;
223 else if (strcmp(suboption,"--db")==0) return true; 223 else if (strcmp(suboption,"--db")==0) return true;
224 else if (strcmp(suboption,"--pr")==0) return true; 224 else if (strcmp(suboption,"--pr")==0) return true;
225 else if (strcmp(suboption,"--sb")==0) return true; 225 else if (strcmp(suboption,"--sb")==0) return true;
226 else if (strcmp(suboption,"--ec")==0) return true; 226 else if (strcmp(suboption,"--ec")==0) return true;
227 else if (strcmp(suboption,"--co")==0) return true; 227 else if (strcmp(suboption,"--co")==0) return true;
228 else if (strcmp(suboption,"--to")==0) return true; 228 else if (strcmp(suboption,"--to")==0) return true;
229 else if (strcmp(suboption,"--lcs")==0) return true; 229 else if (strcmp(suboption,"--lcs")==0) return true;
230 else if (strcmp(suboption,"--all")==0) return true; 230 else if (strcmp(suboption,"--all")==0) return true;
231 else if (strcmp(suboption,"--sec")==0) return true; 231 else if (strcmp(suboption,"--sec")==0) return true;
232 else if (strcmp(suboption,"--a")==0) return true; 232 else if (strcmp(suboption,"--a")==0) return true;
233 else if (strcmp(suboption,"--s")==0) return true; 233 else if (strcmp(suboption,"--s")==0) return true;
234 else if (strcmp(suboption,"--fn")==0) return true; 234 else if (strcmp(suboption,"--fn")==0) return true;
235 else if (strcmp(suboption,"--v")==0) return true; 235 else if (strcmp(suboption,"--v")==0) return true;
236 else if (strcmp(suboption,"--o")==0) return true; 236 else if (strcmp(suboption,"--o")==0) return true;
237 else if (strcmp(suboption,"--erp")==0) return true; 237 else if (strcmp(suboption,"--erp")==0) return true;
238 else if (strcmp(suboption,"--drp")==0) return true; 238 else if (strcmp(suboption,"--drp")==0) return true;
239 else if (strcmp(suboption,"--ewp")==0) return true; 239 else if (strcmp(suboption,"--ewp")==0) return true;
240 else if (strcmp(suboption,"--dwp")==0) return true; 240 else if (strcmp(suboption,"--dwp")==0) return true;
241 else if (strcmp(suboption,"--get")==0) return true; 241 else if (strcmp(suboption,"--get")==0) return true;
242 else if (strcmp(suboption,"--set")==0) return true; 242 else if (strcmp(suboption,"--set")==0) return true;
243 else if (strcmp(suboption,"--vals")==0) return true; 243 else if (strcmp(suboption,"--vals")==0) return true;
244 else if (strcmp(suboption,"--RDP")==0) return true; 244 else if (strcmp(suboption,"--RDP")==0) return true;
245 else if (strcmp(suboption,"--User")==0) return true; 245 else if (strcmp(suboption,"--User")==0) return true;
246 else if (strcmp(suboption,"--Data0")==0) return true; 246 else if (strcmp(suboption,"--Data0")==0) return true;
247 else if (strcmp(suboption,"--Data1")==0) return true; 247 else if (strcmp(suboption,"--Data1")==0) return true;
248 else if (strcmp(suboption,"--WRP0")==0) return true; 248 else if (strcmp(suboption,"--WRP0")==0) return true;
249 else if (strcmp(suboption,"--WRP1")==0) return true; 249 else if (strcmp(suboption,"--WRP1")==0) return true;
250 else if (strcmp(suboption,"--WRP2")==0) return true; 250 else if (strcmp(suboption,"--WRP2")==0) return true;
251 else if (strcmp(suboption,"--WRP3")==0) return true; 251 else if (strcmp(suboption,"--WRP3")==0) return true;
252 else if (strcmp(suboption,"--Hi")==0) return true; 252 else if (strcmp(suboption,"--Hi")==0) return true;
253 else if (strcmp(suboption,"--Lo")==0) return true; 253 else if (strcmp(suboption,"--Lo")==0) return true;
254 else return false; 254 else return false;
255 } 255 }
256   256  
257 /*******************************************************************************************/ 257 /*******************************************************************************************/
258 /* Function : write_debug_info */ 258 /* Function : write_debug_info */
259 /* IN : */ 259 /* IN : */
260 /* OUT : */ 260 /* OUT : */
261 /* Description : print the output messages on the standart output */ 261 /* Description : print the output messages on the standart output */
262 /*******************************************************************************************/ 262 /*******************************************************************************************/
263 void write_debug_info(char *msg, int page, DWORD addr, float size, STATE status) 263 void write_debug_info(char *msg, int page, DWORD addr, float size, STATE status)
264 { 264 {
265 char d_info[256]; 265 char d_info[256];
266   266  
267 if((page==0) && (addr==0) && (size==0)) 267 if((page==0) && (addr==0) && (size==0))
268 { 268 {
269 if(status == OK) 269 if(status == OK)
270 sprintf(d_info, "%s \t\t\t\t [OK] \n", msg); 270 sprintf(d_info, "%s \t\t\t\t [OK] \n", msg);
271 else 271 else
272 sprintf(d_info, "%s \t\t\t\t [KO] \n", msg); 272 sprintf(d_info, "%s \t\t\t\t [KO] \n", msg);
273 } 273 }
274 else if(status == OK) 274 else if(status == OK)
275 sprintf(d_info, "%s \t page %i \t @0x %8X \t size %.2f(KB) \t [OK] \n", msg, page, addr, (float)size); 275 sprintf(d_info, "%s \t page %i \t @0x %8X \t size %.2f(KB) \t [OK] \n", msg, page, addr, (float)size);
276 else 276 else
277 sprintf(d_info, "%s \t page %i \t @0x %8X \t size %.2f(KB) \t [KO] \n", msg, page, addr, (float)size); 277 sprintf(d_info, "%s \t page %i \t @0x %8X \t size %.2f(KB) \t [KO] \n", msg, page, addr, (float)size);
278   278  
279 if((SHOW_OK && (status == OK)) || (SHOW_KO && (status == KO))) printf(d_info); 279 if((SHOW_OK && (status == OK)) || (SHOW_KO && (status == KO))) printf(d_info);
280 } 280 }
281   281  
282 /*******************************************************************************************/ 282 /*******************************************************************************************/
283 /* Function : main */ 283 /* Function : main */
284 /* IN : */ 284 /* IN : */
285 /* OUT : */ 285 /* OUT : */
286 /* Description : */ 286 /* Description : */
287 /*******************************************************************************************/ 287 /*******************************************************************************************/
288 int main(int argc, char* argv[]) 288 int main(int argc, char* argv[])
289 { 289 {
290 START: 290 START:
291   291  
292 BYTE Res = SUCCESS; 292 BYTE Res = SUCCESS;
293 BYTE User, RDP, Data0, Data1, WRP0, WRP1, WRP2, WRP3; 293 BYTE User, RDP, Data0, Data1, WRP0, WRP1, WRP2, WRP3;
294 bool WaitForMoreSubOpt = TRUE; 294 bool WaitForMoreSubOpt = TRUE;
295   295  
296 //Initializing default serial connection parameters 296 //Initializing default serial connection parameters
297 int portname = 1; 297 int portname = 1;
298 long BaudRate = 57600 ; 298 long BaudRate = 57600 ;
299 int DataBits = 8; 299 int DataBits = 8;
300 int parity = ParityToInt("EVEN"); 300 int parity = ParityToInt("EVEN");
301 double nbStopBit = 1; 301 double nbStopBit = 1;
302 int timeout = 5000; 302 int timeout = 5000;
303 bool control = false; 303 bool control = false;
304   304  
305 int nsec = 0; 305 int nsec = 0;
306 DWORD address = 0x00000000; 306 DWORD address = 0x00000000;
307 DWORD size = 0x00000000; 307 DWORD size = 0x00000000;
308 char* filename; 308 char* filename;
309 char devname[256] = "STM32_Low-density_32K.STmap"; 309 char devname[256] = "STM32_Low-density_32K.STmap";
310 bool Verify = FALSE; 310 bool Verify = FALSE;
311 bool optimize = FALSE; 311 bool optimize = FALSE;
312 int becho = 0; 312 int becho = 0;
313   313  
314 char Drive[3], Dir[256], Fname[256], Ext[256]; 314 char Drive[3], Dir[256], Fname[256], Ext[256];
315 char *ptr; 315 char *ptr;
316   316  
317 bool bAuto = false; 317 bool bAuto = false;
318   318  
319 if (argc == 1) // wrong parameters 319 if (argc == 1) // wrong parameters
320 man(); 320 man();
321 else 321 else
322 { 322 {
323 int arg_index = 1; 323 int arg_index = 1;
324   324  
325 while(arg_index < argc) 325 while(arg_index < argc)
326 { 326 {
327 if(!Is_Option(argv[arg_index])) 327 if(!Is_Option(argv[arg_index]))
328 { 328 {
329 if (arg_index < argc - 1) 329 if (arg_index < argc - 1)
330 printf("bad parameter [%s] \n", argv[arg_index]); 330 printf("bad parameter [%s] \n", argv[arg_index]);
331   331  
332 if(COM_is_Open()) 332 if(COM_is_Open())
333 COM_Close(); 333 COM_Close();
334   334  
335   335  
336 if (bAuto) 336 if (bAuto)
337 goto Done_Success; 337 goto Done_Success;
338 printf("\n Press any key to continue ..."); 338 printf("\n Press any key to continue ...");
339 getchar(); 339 getchar();
340 return 1; 340 return 1;
341 } 341 }
342   342  
343 //============================ Show the man ========================================= 343 //============================ Show the man =========================================
344 if (strcmp(argv[arg_index],"-?")==0) 344 if (strcmp(argv[arg_index],"-?")==0)
345 { 345 {
346 man(); 346 man();
347 return 0; 347 return 0;
348 } 348 }
349 //=============================== connect ============================================ 349 //=============================== connect ============================================
350 else if (strcmp(argv[arg_index],"-c")==0) 350 else if (strcmp(argv[arg_index],"-c")==0)
351 { 351 {
352 while(arg_index < argc) 352 while(arg_index < argc)
353 { 353 {
354 if (arg_index< argc-1) 354 if (arg_index< argc-1)
355 arg_index++; 355 arg_index++;
356 else 356 else
357 break; 357 break;
358   358  
359 if(Is_Option(argv[arg_index])) // Set default connection settings and continue with the next option 359 if(Is_Option(argv[arg_index])) // Set default connection settings and continue with the next option
360 break; 360 break;
361 361
362 else if(Is_SubOption(argv[arg_index])) // Get connection settings 362 else if(Is_SubOption(argv[arg_index])) // Get connection settings
363 { 363 {
364 if (arg_index< argc-1) 364 if (arg_index< argc-1)
365 arg_index++; 365 arg_index++;
366 else 366 else
367 break; 367 break;
368   368  
369 if (strcmp(argv[arg_index-1],"--pn")==0) portname = atoi(argv[arg_index]);//port name (e.g COM1, COM2 ..., default COM1) \n"); 369 if (strcmp(argv[arg_index-1],"--pn")==0) portname = atoi(argv[arg_index]);//port name (e.g COM1, COM2 ..., default COM1) \n");
370 else if (strcmp(argv[arg_index-1],"--br")==0) BaudRate = atoi(argv[arg_index]);//baud reate (e.g 115200, 128000 ..., default 57600) \n"); 370 else if (strcmp(argv[arg_index-1],"--br")==0) BaudRate = atoi(argv[arg_index]);//baud reate (e.g 115200, 128000 ..., default 57600) \n");
371 else if (strcmp(argv[arg_index-1],"--db")==0) DataBits = atoi(argv[arg_index]);//data bits (in {5,6,7,8} ..., default 8) \n"); 371 else if (strcmp(argv[arg_index-1],"--db")==0) DataBits = atoi(argv[arg_index]);//data bits (in {5,6,7,8} ..., default 8) \n");
372 else if (strcmp(argv[arg_index-1],"--pr")==0) parity = ParityToInt(argv[arg_index]); //parity (in {NONE,ODD,EVEN} ..., default EVEN) \n"); 372 else if (strcmp(argv[arg_index-1],"--pr")==0) parity = ParityToInt(argv[arg_index]); //parity (in {NONE,ODD,EVEN} ..., default EVEN) \n");
373 else if (strcmp(argv[arg_index-1],"--sb")==0) nbStopBit= atof(argv[arg_index]);//stop bits (in {1,1.5,2} ..., default 1) \n"); 373 else if (strcmp(argv[arg_index-1],"--sb")==0) nbStopBit= atof(argv[arg_index]);//stop bits (in {1,1.5,2} ..., default 1) \n");
374 else if (strcmp(argv[arg_index-1],"--to")==0) timeout = atoi(argv[arg_index]);//time out (e.g 1000, 2000, 3000 ..., default 5) \n"); 374 else if (strcmp(argv[arg_index-1],"--to")==0) timeout = atoi(argv[arg_index]);//time out (e.g 1000, 2000, 3000 ..., default 5) \n");
375 else if (strcmp(argv[arg_index-1],"--ec")==0) becho = ModeToInt(argv[arg_index]); // Echo back mode, default is OFF \n"); 375 else if (strcmp(argv[arg_index-1],"--ec")==0) becho = ModeToInt(argv[arg_index]); // Echo back mode, default is OFF \n");
376 else if (strcmp(argv[arg_index-1],"--co")==0) control = StatusToBool(argv[arg_index]); // Outputs Control ON/OFF, default is OFF \n"); 376 else if (strcmp(argv[arg_index-1],"--co")==0) control = StatusToBool(argv[arg_index]); // Outputs Control ON/OFF, default is OFF \n");
377 377
378 } 378 }
379 else 379 else
380 { 380 {
381 if (arg_index < argc - 1) 381 if (arg_index < argc - 1)
382 printf("bad parameter [%s] \n", argv[arg_index]); 382 printf("bad parameter [%s] \n", argv[arg_index]);
383   383  
384 if(COM_is_Open()) 384 if(COM_is_Open())
385 COM_Close(); 385 COM_Close();
386   386  
387 printf("\n Press any key to continue ... 2"); 387 printf("\n Press any key to continue ... 2");
388 getchar(); 388 getchar();
389 return 1; 389 return 1;
390 } 390 }
391 } 391 }
392   392  
393   393  
394   394  
395 // Apply serial connection settings 395 // Apply serial connection settings
396 TARGET_SetComIntType(0); 396 TARGET_SetComIntType(0);
397 SetCOMSettings(portname, BaudRate, DataBits, parity, nbStopBit); 397 SetCOMSettings(portname, BaudRate, DataBits, parity, nbStopBit);
398 STBL_SetFlowControl(control); 398 STBL_SetFlowControl(control);
399   399  
400   400  
401 // Opening serial connection 401 // Opening serial connection
402 Res = COM_Open(); 402 Res = COM_Open();
403 SetTimeOut(1000); 403 SetTimeOut(1000);
404   404  
405 if ((Res != SUCCESS) && (Res != COM_ALREADY_OPENED)) 405 if ((Res != SUCCESS) && (Res != COM_ALREADY_OPENED))
406 { 406 {
407 write_debug_info("Opening Port", 0 ,0, 0, KO); 407 write_debug_info("Opening Port", 0 ,0, 0, KO);
408 printf("Cannot open the com port, the port may \n be used by another application \n"); 408 printf("Cannot open the com port, the port may \n be used by another application \n");
409   409  
410 if(COM_is_Open()) 410 if(COM_is_Open())
411 COM_Close(); 411 COM_Close();
412   412  
413 printf("\n Press any key to continue ... 3"); 413 printf("\n Press any key to continue ... 3");
414 getchar(); 414 getchar();
415 return 1; 415 return 1;
416 } 416 }
417 else 417 else
418 write_debug_info("Opening Port", 0 ,0, 0, OK); 418 write_debug_info("Opening Port", 0 ,0, 0, OK);
419   419  
420   420  
421 421
422 422
423 STBL_SetEcho(becho); // Setting Echo back mode 423 STBL_SetEcho(becho); // Setting Echo back mode
424   424  
425   425  
426   426  
427   427  
428 } 428 }
429   429  
430   430  
431 //============================ Auto option ======================================= 431 //============================ Auto option =======================================
432 else if (strcmp(argv[arg_index],"-Auto")==0) 432 else if (strcmp(argv[arg_index],"-Auto")==0)
433 { 433 {
434 if (arg_index< argc) 434 if (arg_index< argc)
435 arg_index++; 435 arg_index++;
436 else 436 else
437 break; 437 break;
438   438  
439 bAuto = true; 439 bAuto = true;
440 440
441 // BOOT0 = High 441 // BOOT0 = High
442 STBL_SetDtr(TRUE); 442 STBL_SetDtr(TRUE);
443 Sleep(50); 443 Sleep(100);
444 // Reset = Low 444 // Reset = Low
445 STBL_SetRts(TRUE); 445 STBL_SetRts(TRUE);
446   446  
447 Sleep(50); 447 Sleep(100);
448   448  
449 // Reset = High 449 // Reset = High
450 STBL_SetRts(FALSE); 450 STBL_SetRts(FALSE);
-   451 Sleep(100);
-   452
451   453  
452 STBL_SetDtr(FALSE); 454 STBL_SetDtr(FALSE);
453 Sleep(100); 455 Sleep(100);
454 COM_Close(); 456 COM_Close();
455 COM_Open(); 457 COM_Open();
456   458  
457 STBL_SetDtr(TRUE); 459 STBL_SetDtr(TRUE);
458 Sleep(50); 460 Sleep(100);
459 // Reset = Low 461 // Reset = Low
460 STBL_SetRts(TRUE); 462 STBL_SetRts(TRUE);
461   463  
462 Sleep(50); 464 Sleep(100);
463   465  
464 // Reset = High 466 // Reset = High
465 STBL_SetRts(FALSE); 467 STBL_SetRts(FALSE);
-   468 Sleep(100);
466   469  
467 STBL_SetDtr(FALSE); 470 STBL_SetDtr(FALSE);
468 Sleep(500); 471 Sleep(500);
469 //Sleep(1000); -  
-   472  
470 write_debug_info("Setting device to BOOT0", 0 ,0, 0, OK); 473 write_debug_info("Setting device to BOOT0", 0 ,0, 0, OK);
471   474  
472 //printf("\n RTS set low. Press any key to continue ... 4"); 475 //printf("\n RTS set low. Press any key to continue ... 4");
473 //getchar(); 476 //getchar();
474 } 477 }
475   478  
476 //============================ command RTS pin ======================================= 479 //============================ command RTS pin =======================================
477 else if (strcmp(argv[arg_index],"-Rts")==0) 480 else if (strcmp(argv[arg_index],"-Rts")==0)
478 { 481 {
479 //_sleep(1000); 482 //_sleep(1000);
480 while(arg_index < argc) 483 while(arg_index < argc)
481 { 484 {
482 if (arg_index< argc-1) arg_index++; 485 if (arg_index< argc-1) arg_index++;
483 else break; 486 else break;
484   487  
485 if(Is_Option(argv[arg_index])) break; 488 if(Is_Option(argv[arg_index])) break;
486 else if(Is_SubOption(argv[arg_index])) 489 else if(Is_SubOption(argv[arg_index]))
487 { 490 {
488 491
489 492
490 493
491 if (strcmp(argv[arg_index],"--Hi")==0) 494 if (strcmp(argv[arg_index],"--Hi")==0)
492 { 495 {
493 write_debug_info("Set Rts line", 0 ,0, 0,OK); 496 write_debug_info("Set Rts line", 0 ,0, 0,OK);
494 STBL_SetRts(TRUE); 497 STBL_SetRts(TRUE);
495   498  
496   499  
497   500  
498 } 501 }
499 else if (strcmp(argv[arg_index],"--Lo")==0) 502 else if (strcmp(argv[arg_index],"--Lo")==0)
500 { 503 {
501 write_debug_info("Reset Rts line", 0 ,0, 0,OK); 504 write_debug_info("Reset Rts line", 0 ,0, 0,OK);
502 STBL_SetRts(FALSE); 505 STBL_SetRts(FALSE);
503 } 506 }
504 else 507 else
505 { 508 {
506 write_debug_info("bad parameter [Set Rts line] should be Hi or Lo ", 0 ,0, 0,KO); 509 write_debug_info("bad parameter [Set Rts line] should be Hi or Lo ", 0 ,0, 0,KO);
507   510  
508 if (arg_index < argc - 1) 511 if (arg_index < argc - 1)
509 printf("bad parameter [%s] \n", argv[arg_index]); 512 printf("bad parameter [%s] \n", argv[arg_index]);
510   513  
511 if(COM_is_Open()) 514 if(COM_is_Open())
512 COM_Close(); 515 COM_Close();
513   516  
514 printf("\n Press any key to continue ... 5"); 517 printf("\n Press any key to continue ... 5");
515 getchar(); 518 getchar();
516 return 1; 519 return 1;
517 } 520 }
518 } 521 }
519 else 522 else
520 { 523 {
521 if (arg_index < argc - 1) 524 if (arg_index < argc - 1)
522 printf("bad parameter [%s] \n", argv[arg_index]); 525 printf("bad parameter [%s] \n", argv[arg_index]);
523   526  
524 if(COM_is_Open()) 527 if(COM_is_Open())
525 COM_Close(); 528 COM_Close();
526   529  
527 printf("\n Press any key to continue ... 6"); 530 printf("\n Press any key to continue ... 6");
528 getchar(); 531 getchar();
529 return 1; 532 return 1;
530 } 533 }
531 } 534 }
532 } 535 }
533 //============================ command DTR pin ======================================= 536 //============================ command DTR pin =======================================
534 else if (strcmp(argv[arg_index],"-Dtr")==0) 537 else if (strcmp(argv[arg_index],"-Dtr")==0)
535 { 538 {
536 while(arg_index < argc) 539 while(arg_index < argc)
537 { 540 {
538 if (arg_index< argc-1) 541 if (arg_index< argc-1)
539 arg_index++; 542 arg_index++;
540 else 543 else
541 break; 544 break;
542   545  
543 if(Is_Option(argv[arg_index])) 546 if(Is_Option(argv[arg_index]))
544 break; 547 break;
545   548  
546 else if(Is_SubOption(argv[arg_index])) 549 else if(Is_SubOption(argv[arg_index]))
547 { 550 {
548 if (strcmp(argv[arg_index],"--Hi")==0) 551 if (strcmp(argv[arg_index],"--Hi")==0)
549 { 552 {
550 write_debug_info("Set Dtr line", 0 ,0, 0,OK); 553 write_debug_info("Set Dtr line", 0 ,0, 0,OK);
551 STBL_SetDtr(TRUE); 554 STBL_SetDtr(TRUE);
552   555  
553 } 556 }
554 else if (strcmp(argv[arg_index],"--Lo")==0) 557 else if (strcmp(argv[arg_index],"--Lo")==0)
555 { 558 {
556 write_debug_info("Reset Dtr line", 0 ,0, 0,OK); 559 write_debug_info("Reset Dtr line", 0 ,0, 0,OK);
557 STBL_SetDtr(FALSE); 560 STBL_SetDtr(FALSE);
558 } 561 }
559 else 562 else
560 { 563 {
561 write_debug_info("bad parameter [Set Dtr line] should be Hi or Lo ", 0 ,0, 0,KO); 564 write_debug_info("bad parameter [Set Dtr line] should be Hi or Lo ", 0 ,0, 0,KO);
562   565  
563 if (arg_index < argc - 1) 566 if (arg_index < argc - 1)
564 printf("bad parameter [%s] \n", argv[arg_index]); 567 printf("bad parameter [%s] \n", argv[arg_index]);
565   568  
566 if(COM_is_Open()) 569 if(COM_is_Open())
567 COM_Close(); 570 COM_Close();
568   571  
569 printf("\n Press any key to continue ... 7"); 572 printf("\n Press any key to continue ... 7");
570 getchar(); 573 getchar();
571 return 1; 574 return 1;
572 } 575 }
573 } 576 }
574 else 577 else
575 { 578 {
576 if (arg_index < argc - 1) printf("bad parameter [%s] \n", argv[arg_index]); 579 if (arg_index < argc - 1) printf("bad parameter [%s] \n", argv[arg_index]);
577 if (arg_index < argc - 1) 580 if (arg_index < argc - 1)
578 printf("bad parameter [%s] \n", argv[arg_index]); 581 printf("bad parameter [%s] \n", argv[arg_index]);
579   582  
580 if(COM_is_Open()) 583 if(COM_is_Open())
581 COM_Close(); 584 COM_Close();
582   585  
583 printf("\n Press any key to continue ... 8"); 586 printf("\n Press any key to continue ... 8");
584 getchar(); 587 getchar();
585 return 1; 588 return 1;
586 } 589 }
587 } 590 }
588 } 591 }
589 //============================ ERASE ================================================= 592 //============================ ERASE =================================================
590 else if (strcmp(argv[arg_index],"-e")==0) 593 else if (strcmp(argv[arg_index],"-e")==0)
591 { 594 {
592 595
593 while(arg_index < argc) 596 while(arg_index < argc)
594 { 597 {
595 if (!WaitForMoreSubOpt) 598 if (!WaitForMoreSubOpt)
596 break; 599 break;
597 600
598 if (arg_index< argc-1) 601 if (arg_index< argc-1)
599 arg_index++; 602 arg_index++;
600 else 603 else
601 break; 604 break;
602   605  
603 if(Is_Option(argv[arg_index])) 606 if(Is_Option(argv[arg_index]))
604 break; 607 break;
605   608  
606 else if(Is_SubOption(argv[arg_index])) 609 else if(Is_SubOption(argv[arg_index]))
607 { 610 {
608 if (arg_index< argc) 611 if (arg_index< argc)
609 arg_index++; 612 arg_index++;
610 else 613 else
611 break; 614 break;
612   615  
613 //******************** This section is only for STM8 boot loader ******************* 616 //******************** This section is only for STM8 boot loader *******************
614 BYTE Version; 617 BYTE Version;
615 Commands pCmds; 618 Commands pCmds;
616 CString m_Version; 619 CString m_Version;
617 if (STBL_GET(&Version, &pCmds) == SUCCESS) 620 if (STBL_GET(&Version, &pCmds) == SUCCESS)
618 { 621 {
619 m_Version.Format("%x.%x",Version/16, Version & 0x0F) ; 622 m_Version.Format("%x.%x",Version/16, Version & 0x0F) ;
620 } 623 }
621 CIni Ini((LPCSTR)MapFile); 624 CIni Ini((LPCSTR)MapFile);
622   625  
623 if(Ini.IsKeyExist((LPCTSTR)"Product",(LPCTSTR)m_Version)) 626 if(Ini.IsKeyExist((LPCTSTR)"Product",(LPCTSTR)m_Version))
624 { 627 {
625 CString E_W_ROUTINEs = Ini.GetString((LPCTSTR)"Product",(LPCTSTR)m_Version, ""); 628 CString E_W_ROUTINEs = Ini.GetString((LPCTSTR)"Product",(LPCTSTR)m_Version, "");
626 CString Path(*__argv); 629 CString Path(*__argv);
627   630  
628 char fullPath [MAX_PATH]; 631 char fullPath [MAX_PATH];
629   632  
630 GetModuleFileName(0, fullPath, (MAX_PATH)); 633 GetModuleFileName(0, fullPath, (MAX_PATH));
631   634  
632 Path=fullPath; 635 Path=fullPath;
633   636  
634 int j=Path.ReverseFind('\\')+1; 637 int j=Path.ReverseFind('\\')+1;
635 if(j) Path=Path.Left(j); 638 if(j) Path=Path.Left(j);
636   639  
637 CString ToFind; 640 CString ToFind;
638   641  
639 ToFind.Format("%s%s%s", Path, "STM8_Routines\\", E_W_ROUTINEs); 642 ToFind.Format("%s%s%s", Path, "STM8_Routines\\", E_W_ROUTINEs);
640   643  
641 if(!E_W_ROUTINEs.IsEmpty()) 644 if(!E_W_ROUTINEs.IsEmpty())
642 { 645 {
643 if(!FileExist((LPCTSTR)ToFind)) 646 if(!FileExist((LPCTSTR)ToFind))
644 { 647 {
645 printf("\n!WARNING the erase or download operation may fail \n EW routines file is missing [%s]\n", ToFind); 648 printf("\n!WARNING the erase or download operation may fail \n EW routines file is missing [%s]\n", ToFind);
646 } 649 }
647 else 650 else
648 { 651 {
649 HANDLE Image; 652 HANDLE Image;
650 if (FILES_ImageFromFile((LPSTR)(LPCSTR)ToFind,&Image, 1)== FILES_NOERROR) 653 if (FILES_ImageFromFile((LPSTR)(LPCSTR)ToFind,&Image, 1)== FILES_NOERROR)
651 { 654 {
652 FILES_SetImageName(Image,(LPSTR)(LPCSTR)ToFind); 655 FILES_SetImageName(Image,(LPSTR)(LPCSTR)ToFind);
653   656  
654 DWORD NbElements; 657 DWORD NbElements;
655 if (FILES_GetImageNbElement(Image, &NbElements) == FILES_NOERROR) 658 if (FILES_GetImageNbElement(Image, &NbElements) == FILES_NOERROR)
656 { 659 {
657 for (int el=0; el< (int)NbElements;el++) 660 for (int el=0; el< (int)NbElements;el++)
658 { 661 {
659 IMAGEELEMENT Element={0}; 662 IMAGEELEMENT Element={0};
660 if (FILES_GetImageElement(Image, el, &Element) == FILES_NOERROR) 663 if (FILES_GetImageElement(Image, el, &Element) == FILES_NOERROR)
661 { 664 {
662 Element.Data=new BYTE[Element.dwDataLength]; 665 Element.Data=new BYTE[Element.dwDataLength];
663 if (FILES_GetImageElement(Image, el, &Element) == FILES_NOERROR) 666 if (FILES_GetImageElement(Image, el, &Element) == FILES_NOERROR)
664 { 667 {
665 if (STBL_DNLOAD(Element.dwAddress, Element.Data, Element.dwDataLength, FALSE) != SUCCESS) 668 if (STBL_DNLOAD(Element.dwAddress, Element.Data, Element.dwDataLength, FALSE) != SUCCESS)
666 { 669 {
667 670
668 } 671 }
669 } 672 }
670 } 673 }
671 } 674 }
672   675  
673 // Verify writen data 676 // Verify writen data
674 BOOL VerifySuccess = TRUE; 677 BOOL VerifySuccess = TRUE;
675 _sleep(100);; 678 _sleep(100);;
676   679  
677 #ifndef _VS6_USED 680 //#ifndef _VS6_USED
678 int el; 681 int el;
679 #endif 682 //#endif
680 for (el=0; el< (int)NbElements;el++) 683 for (el=0; el< (int)NbElements;el++)
681 { 684 {
682 IMAGEELEMENT Element={0}; 685 IMAGEELEMENT Element={0};
683 if (FILES_GetImageElement(Image, el, &Element) == FILES_NOERROR) 686 if (FILES_GetImageElement(Image, el, &Element) == FILES_NOERROR)
684 { 687 {
685 Element.Data=new BYTE[Element.dwDataLength]; 688 Element.Data=new BYTE[Element.dwDataLength];
686 if (FILES_GetImageElement(Image, el, &Element) == FILES_NOERROR) 689 if (FILES_GetImageElement(Image, el, &Element) == FILES_NOERROR)
687 { 690 {
688 if (STBL_VERIFY(Element.dwAddress, Element.Data, Element.dwDataLength, FALSE) != SUCCESS) 691 if (STBL_VERIFY(Element.dwAddress, Element.Data, Element.dwDataLength, FALSE) != SUCCESS)
689 { 692 {
690 VerifySuccess = FALSE; 693 VerifySuccess = FALSE;
691 char str[255]; 694 char str[255];
692 sprintf(str, "%s at address :0x%X. \n%s \nPlease disable the write protection then try agin.", "Data not matching ", Element.dwAddress, "The page may be write protected."); 695 sprintf(str, "%s at address :0x%X. \n%s \nPlease disable the write protection then try agin.", "Data not matching ", Element.dwAddress, "The page may be write protected.");
693 AfxMessageBox(str, MB_OK|MB_ICONEXCLAMATION); 696 AfxMessageBox(str, MB_OK|MB_ICONEXCLAMATION);
694 return 1; 697 return 1;
695 } 698 }
696 } 699 }
697 } 700 }
698 } 701 }
699 } 702 }
700 } 703 }
701 else 704 else
702 { 705 {
703 AfxMessageBox("Unable to load data from this file " + ToFind + " ..."); 706 AfxMessageBox("Unable to load data from this file " + ToFind + " ...");
704 return -1; 707 return -1;
705 } 708 }
706 } 709 }
707 } 710 }
708 } 711 }
709 else 712 else
710 { 713 {
711 int family = Ini.GetInt((LPCTSTR)"Product",(LPCTSTR)"family", 0); 714 int family = Ini.GetInt((LPCTSTR)"Product",(LPCTSTR)"family", 0);
712 if(family == 3) 715 if(family == 3)
713 { 716 {
714 printf("\n!WARNING the erase or download operation may fail \n EW routines file is missing\n"); 717 printf("\n!WARNING the erase or download operation may fail \n EW routines file is missing\n");
715 } 718 }
716 } 719 }
717 //End****************** This section is only for STM8 boot loader ******************* 720 //End****************** This section is only for STM8 boot loader *******************
718   721  
719 //End****************** This section is only for STM8 boot loader ******************* 722 //End****************** This section is only for STM8 boot loader *******************
720   723  
721 printf("\n ERASING ... \n"); 724 printf("\n ERASING ... \n");
722 if (strcmp(argv[arg_index-1],"--all")==0) 725 if (strcmp(argv[arg_index-1],"--all")==0)
723 { 726 {
724 727
725 WaitForMoreSubOpt = false; 728 WaitForMoreSubOpt = false;
726 Res = STBL_ERASE(0xFFFF, NULL); 729 Res = STBL_ERASE(0xFFFF, NULL);
727   730  
728   731  
729   732  
730 if (Res != SUCCESS) 733 if (Res != SUCCESS)
731 write_debug_info("erasing all pages", 0 ,0, 0, KO); 734 write_debug_info("erasing all pages", 0 ,0, 0, KO);
732 else 735 else
733 write_debug_info("erasing all pages", 0 ,0, 0, OK); 736 write_debug_info("erasing all pages", 0 ,0, 0, OK);
734 } 737 }
735 else if (strcmp(argv[arg_index-1],"--sec")==0) 738 else if (strcmp(argv[arg_index-1],"--sec")==0)
736 { 739 {
737 WaitForMoreSubOpt = true; 740 WaitForMoreSubOpt = true;
738   741  
739 nsec = atoi(argv[arg_index]); 742 nsec = atoi(argv[arg_index]);
740 LPWORD sectors = (LPWORD)malloc(nsec *2 + 2); 743 LPWORD sectors = (LPWORD)malloc(nsec *2 + 2);
741   744  
742   745  
743 sectors[0] = 0; 746 sectors[0] = 0;
744 for(int i = 1; i<= nsec; i++) 747 for(int i = 1; i<= nsec; i++)
745 { 748 {
746 sectors[0]++; 749 sectors[0]++;
747 arg_index++; 750 arg_index++;
748 sectors[sectors[0]] = atoi(argv[arg_index]); 751 sectors[sectors[0]] = atoi(argv[arg_index]);
749 } 752 }
750   753  
751 WaitForMoreSubOpt = false; 754 WaitForMoreSubOpt = false;
752   755  
753 printf("\nerasing %i sectors : ", sectors[0]); 756 printf("\nerasing %i sectors : ", sectors[0]);
754   757  
755 #ifndef _VS6_USED 758 //#ifndef _VS6_USED
756 int i; 759 int i;
757 #endif 760 //#endif
758   761  
759 for(i = 1; i<= nsec; i++) 762 for(i = 1; i<= nsec; i++)
760 { 763 {
761 printf("<%i>", sectors[i]); 764 printf("<%i>", sectors[i]);
762 } 765 }
763 printf("\n"); 766 printf("\n");
764   767  
765 Res = STBL_ERASE(nsec, (LPBYTE)sectors+2); 768 Res = STBL_ERASE(nsec, (LPBYTE)sectors+2);
766 if (Res != SUCCESS) 769 if (Res != SUCCESS)
767 { 770 {
768 write_debug_info("erasing", 0 ,0, 0, KO); 771 write_debug_info("erasing", 0 ,0, 0, KO);
769 772
770 if(COM_is_Open()) 773 if(COM_is_Open())
771 COM_Close(); 774 COM_Close();
772 775
773 printf("\n Press any key to continue ... 9"); 776 printf("\n Press any key to continue ... 9");
774 getchar(); 777 getchar();
775 return 1; 778 return 1;
776 } 779 }
777 else 780 else
778 write_debug_info("erasing", 0 ,0, 0, OK); 781 write_debug_info("erasing", 0 ,0, 0, OK);
779   782  
780 arg_index++; 783 arg_index++;
781 } 784 }
782 } 785 }
783 else 786 else
784 { 787 {
785 if (arg_index < argc - 1) 788 if (arg_index < argc - 1)
786 printf("bad parameter [%s] \n", argv[arg_index]); 789 printf("bad parameter [%s] \n", argv[arg_index]);
787   790  
788 if(COM_is_Open()) 791 if(COM_is_Open())
789 COM_Close(); 792 COM_Close();
790   793  
791 printf("\n Press any key to continue ... 10"); 794 printf("\n Press any key to continue ... 10");
792 getchar(); 795 getchar();
793 return 1; 796 return 1;
794 } 797 }
795 } 798 }
796 } 799 }
797 //============================ UPLOAD =============================================== 800 //============================ UPLOAD ===============================================
798 else if (strcmp(argv[arg_index],"-u")==0) 801 else if (strcmp(argv[arg_index],"-u")==0)
799 { 802 {
800 while(arg_index < argc) 803 while(arg_index < argc)
801 { 804 {
802 if (arg_index< argc-1) 805 if (arg_index< argc-1)
803 arg_index++; 806 arg_index++;
804 else 807 else
805 break; 808 break;
806   809  
807 if(Is_Option(argv[arg_index])) 810 if(Is_Option(argv[arg_index]))
808 break; 811 break;
809   812  
810 else if(Is_SubOption(argv[arg_index])) 813 else if(Is_SubOption(argv[arg_index]))
811 { 814 {
812 if (arg_index< argc) 815 if (arg_index< argc)
813 arg_index++; 816 arg_index++;
814 else 817 else
815 break; 818 break;
816   819  
817 /*if (strcmp(argv[arg_index-1],"--a")==0) 820 /*if (strcmp(argv[arg_index-1],"--a")==0)
818 { 821 {
819 address = _tcstoul(argv[arg_index], 0, 16) ; 822 address = _tcstoul(argv[arg_index], 0, 16) ;
820 } 823 }
821 else if (strcmp(argv[arg_index-1],"--s")==0) 824 else if (strcmp(argv[arg_index-1],"--s")==0)
822 { 825 {
823 size = _tcstoul(argv[arg_index], 0, 16) ; 826 size = _tcstoul(argv[arg_index], 0, 16) ;
824 } 827 }
825 else */if (strcmp(argv[arg_index-1],"--fn")==0) 828 else */if (strcmp(argv[arg_index-1],"--fn")==0)
826 { 829 {
827 filename = argv[arg_index]; 830 filename = argv[arg_index];
828 } 831 }
829 } 832 }
830 else 833 else
831 { 834 {
832 if (arg_index < argc - 1) 835 if (arg_index < argc - 1)
833 printf("bad parameter [%s] \n", argv[arg_index]); 836 printf("bad parameter [%s] \n", argv[arg_index]);
834   837  
835 if(COM_is_Open()) 838 if(COM_is_Open())
836 COM_Close(); 839 COM_Close();
837   840  
838 printf("\n Press any key to continue ... 11"); 841 printf("\n Press any key to continue ... 11");
839 getchar(); 842 getchar();
840 return 1; 843 return 1;
841 } 844 }
842 } 845 }
843   846  
844 printf("\n UPLOADING ... \n\n"); 847 printf("\n UPLOADING ... \n\n");
845   848  
846 HANDLE Handle; 849 HANDLE Handle;
847 FILES_CreateImage(&Handle, 0); 850 FILES_CreateImage(&Handle, 0);
848   851  
849 FILES_CreateImageFromMapping(&Handle,pmMapping); 852 FILES_CreateImageFromMapping(&Handle,pmMapping);
850   853  
851 DWORD NbElements = 0; 854 DWORD NbElements = 0;
852 if (FILES_GetImageNbElement(Handle, &NbElements) == FILES_NOERROR) 855 if (FILES_GetImageNbElement(Handle, &NbElements) == FILES_NOERROR)
853 { 856 {
854 if (NbElements > 0) 857 if (NbElements > 0)
855 { 858 {
856 for(int i = 0; i< (int)NbElements; i++) 859 for(int i = 0; i< (int)NbElements; i++)
857 { 860 {
858 IMAGEELEMENT Element={0}; 861 IMAGEELEMENT Element={0};
859 // Get element data size 862 // Get element data size
860 if (FILES_GetImageElement(Handle, i, &Element) == FILES_NOERROR) 863 if (FILES_GetImageElement(Handle, i, &Element) == FILES_NOERROR)
861 { 864 {
862 //Upload element data 865 //Upload element data
863 Element.Data = (LPBYTE)malloc(Element.dwDataLength); 866 Element.Data = (LPBYTE)malloc(Element.dwDataLength);
864 if (STBL_UPLOAD(Element.dwAddress, Element.Data, Element.dwDataLength) == SUCCESS) 867 if (STBL_UPLOAD(Element.dwAddress, Element.Data, Element.dwDataLength) == SUCCESS)
865 { 868 {
866 //Insert elment in the Image 869 //Insert elment in the Image
867 write_debug_info("Uploading", i ,Element.dwAddress, (float)Element.dwDataLength/(float)1024, OK); 870 write_debug_info("Uploading", i ,Element.dwAddress, (float)Element.dwDataLength/(float)1024, OK);
868 FILES_SetImageElement(Handle,i,FALSE,Element); 871 FILES_SetImageElement(Handle,i,FALSE,Element);
869 } 872 }
870 else 873 else
871 { 874 {
872 write_debug_info("Uploading", i ,Element.dwAddress, (float)Element.dwDataLength/(float)1024, KO); 875 write_debug_info("Uploading", i ,Element.dwAddress, (float)Element.dwDataLength/(float)1024, KO);
873 876
874 if(COM_is_Open()) 877 if(COM_is_Open())
875 COM_Close(); 878 COM_Close();
876 879
877 printf("\n Press any key to continue ... 12"); 880 printf("\n Press any key to continue ... 12");
878 getchar(); 881 getchar();
879 return 1; 882 return 1;
880 } 883 }
881 } 884 }
882 } 885 }
883 } 886 }
884 } 887 }
885   888  
886 if(!FileExist((LPCTSTR)filename)) 889 if(!FileExist((LPCTSTR)filename))
887 { 890 {
888 printf( "file %s does not exist .. Creating file\n", filename); 891 printf( "file %s does not exist .. Creating file\n", filename);
889 FILE* fp = fopen((LPCTSTR)filename, "a+"); 892 FILE* fp = fopen((LPCTSTR)filename, "a+");
890 fclose(fp); 893 fclose(fp);
891 } 894 }
892   895  
893 printf( "Writing data ...\n"); 896 printf( "Writing data ...\n");
894   897  
895 if (FILES_ImageToFile((LPSTR)(LPCSTR)filename,Handle) != FILES_NOERROR) 898 if (FILES_ImageToFile((LPSTR)(LPCSTR)filename,Handle) != FILES_NOERROR)
896 { 899 {
897 printf( "cannot write to file %s \n", filename); 900 printf( "cannot write to file %s \n", filename);
898 901
899 if(COM_is_Open()) 902 if(COM_is_Open())
900 COM_Close(); 903 COM_Close();
901   904  
902 printf("\n Press any key to continue ... 13"); 905 printf("\n Press any key to continue ... 13");
903 getchar(); 906 getchar();
904 return 1; 907 return 1;
905 } 908 }
906 else 909 else
907 printf("\n Uploaded data is dumped on %s", filename); 910 printf("\n Uploaded data is dumped on %s", filename);
908 } 911 }
909   912  
910   913  
911   914  
912 //============================ Get Device map file name ============================== 915 //============================ Get Device map file name ==============================
913 else if (strcmp(argv[arg_index],"-i")==0) 916 else if (strcmp(argv[arg_index],"-i")==0)
914 { 917 {
915 if (arg_index< argc) 918 if (arg_index< argc)
916 arg_index++; 919 arg_index++;
917 else 920 else
918 break; 921 break;
919   922  
920   923  
921 sprintf(devname,"%s.STmap", argv[arg_index]); 924 sprintf(devname,"%s.STmap", argv[arg_index]);
922   925  
923 char Drive[3], Dir[256], Fname[256], Ext[256]; 926 char Drive[3], Dir[256], Fname[256], Ext[256];
924 _splitpath(argv[0],Drive,Dir,Fname,Ext); 927 _splitpath(argv[0],Drive,Dir,Fname,Ext);
925 928
926 sprintf(MapFile, "%s%s%s%s", Drive, Dir , "Map\\", devname); 929 sprintf(MapFile, "%s%s%s%s", Drive, Dir , "Map\\", devname);
927   930  
928 pmMapping = NULL; 931 pmMapping = NULL;
929 WORD size = 0; 932 WORD size = 0;
930   933  
931 WORD PacketSize = 0; 934 WORD PacketSize = 0;
932 pmMapping = NULL; 935 pmMapping = NULL;
933 WORD Size = 0; 936 WORD Size = 0;
934 char MapName[256]; 937 char MapName[256];
935 // Get the number of sectors in the flash target: pmMapping should be NULL 938 // Get the number of sectors in the flash target: pmMapping should be NULL
936 // number of sectors is returned in the Size value 939 // number of sectors is returned in the Size value
937 BYTE PagePerSector = 0; 940 BYTE PagePerSector = 0;
938   941  
939 if (!FileExist((LPCTSTR)MapFile)) 942 if (!FileExist((LPCTSTR)MapFile))
940 { 943 {
941 printf("This version is not intended to support the <%s> target\n", argv[arg_index]); 944 printf("This version is not intended to support the <%s> target\n", argv[arg_index]);
942 945
943 if(COM_is_Open()) 946 if(COM_is_Open())
944 COM_Close(); 947 COM_Close();
945   948  
946 printf("\n Press any key to continue ... 14"); 949 printf("\n Press any key to continue ... 14");
947 getchar(); 950 getchar();
948 return 1; 951 return 1;
949 } 952 }
950   953  
951 FILES_GetMemoryMapping((LPSTR)(LPCTSTR)MapFile, &Size, (LPSTR)MapName, &PacketSize, pmMapping, &PagePerSector); 954 FILES_GetMemoryMapping((LPSTR)(LPCTSTR)MapFile, &Size, (LPSTR)MapName, &PacketSize, pmMapping, &PagePerSector);
952 // Allocate the mapping structure memory 955 // Allocate the mapping structure memory
953 pmMapping = (PMAPPING)malloc(sizeof(MAPPING)); 956 pmMapping = (PMAPPING)malloc(sizeof(MAPPING));
954 pmMapping->NbSectors = 0; 957 pmMapping->NbSectors = 0;
955 pmMapping->pSectors = (PMAPPINGSECTOR) malloc((Size) * sizeof(MAPPINGSECTOR)); 958 pmMapping->pSectors = (PMAPPINGSECTOR) malloc((Size) * sizeof(MAPPINGSECTOR));
956   959  
957 // Get the mapping info 960 // Get the mapping info
958 FILES_GetMemoryMapping((LPSTR)(LPCTSTR)MapFile, &Size, (LPSTR)(LPCTSTR)MapName, &PacketSize, pmMapping, &PagePerSector); 961 FILES_GetMemoryMapping((LPSTR)(LPCTSTR)MapFile, &Size, (LPSTR)(LPCTSTR)MapName, &PacketSize, pmMapping, &PagePerSector);
959 962
960 SetPaketSize(PacketSize); 963 SetPaketSize(PacketSize);
961   964  
962   965  
963   966  
964 //sending BL config byte (0x7F) & identifing target 967 //sending BL config byte (0x7F) & identifing target
965   968  
966   969  
967   970  
968 Res = STBL_Init_BL(); 971 Res = STBL_Init_BL();
969   972  
970 if (Res == UNREOGNIZED_DEVICE) 973 if (Res == UNREOGNIZED_DEVICE)
971 { 974 {
972 write_debug_info("Activating device", 0 ,0, 0, KO); 975 write_debug_info("Activating device", 0 ,0, 0, KO);
973   976  
974 if(COM_is_Open()) 977 if(COM_is_Open())
975 COM_Close(); 978 COM_Close();
976   979  
977 printf("Unrecognized device... Please, reset your device then try again \n"); 980 printf("Unrecognized device... Please, reset your device then try again \n");
978   981  
979 if(COM_is_Open()) 982 if(COM_is_Open())
980 COM_Close(); 983 COM_Close();
981 984
982 printf("Please, reset your device then press any key to continue \n"); 985 printf("Please, reset your device then press any key to continue \n");
983 printf("\n Press any key to continue ... 15"); 986 printf("\n Press any key to continue ... 15");
984 getchar(); 987 getchar();
985 goto START; 988 goto START;
986 } 989 }
987 else if (Res != SUCCESS) 990 else if (Res != SUCCESS)
988 { 991 {
989 write_debug_info("Activating device", 0 ,0, 0, KO); 992 write_debug_info("Activating device", 0 ,0, 0, KO);
990 printf("No response from the target, the Boot loader can not be started. \nPlease, verify the boot mode configuration, reset your device then try again. \n"); 993 printf("No response from the target, the Boot loader can not be started. \nPlease, verify the boot mode configuration, reset your device then try again. \n");
991 994
992 if(COM_is_Open()) 995 if(COM_is_Open())
993 COM_Close(); 996 COM_Close();
994 997
995 printf("Please, reset your device then then press any key to continue \n"); 998 printf("Please, reset your device then then press any key to continue \n");
996 printf("\n Press any key to continue ... 16"); 999 printf("\n Press any key to continue ... 16");
997 getchar(); 1000 getchar();
998 goto START; 1001 goto START;
999 } 1002 }
1000   1003  
1001 _sleep(TimeBO); 1004 _sleep(TimeBO);
1002 1005
1003 write_debug_info("Activating device", 0 ,0, 0, OK); 1006 write_debug_info("Activating device", 0 ,0, 0, OK);
1004 //Getting Target informations (version, available commands) 1007 //Getting Target informations (version, available commands)
1005 BYTE Version ; 1008 BYTE Version ;
1006 Commands pCmds; 1009 Commands pCmds;
1007   1010  
1008 Res = STBL_GET(&Version, &pCmds); 1011 Res = STBL_GET(&Version, &pCmds);
1009 if (Res != SUCCESS) 1012 if (Res != SUCCESS)
1010 { 1013 {
1011 if(COM_is_Open()) 1014 if(COM_is_Open())
1012 COM_Close(); 1015 COM_Close();
1013   1016  
1014 printf("\n Press any key to continue ... 17"); 1017 printf("\n Press any key to continue ... 17");
1015 getchar(); 1018 getchar();
1016 return 1; 1019 return 1;
1017 } 1020 }
1018   1021  
1019 SetTimeOut(timeout); 1022 SetTimeOut(timeout);
1020   1023  
1021 if (arg_index< argc) 1024 if (arg_index< argc)
1022 arg_index++; 1025 arg_index++;
1023 else 1026 else
1024 break; 1027 break;
1025 } 1028 }
1026 //============================ DOWNLOAD ============================================== 1029 //============================ DOWNLOAD ==============================================
1027 else if (strcmp(argv[arg_index],"-d")==0) 1030 else if (strcmp(argv[arg_index],"-d")==0)
1028 { 1031 {
1029 while(arg_index < argc) 1032 while(arg_index < argc)
1030 { 1033 {
1031 if (arg_index< argc-1) 1034 if (arg_index< argc-1)
1032 arg_index++; 1035 arg_index++;
1033 else 1036 else
1034 break; 1037 break;
1035   1038  
1036 if(Is_Option(argv[arg_index])) 1039 if(Is_Option(argv[arg_index]))
1037 break; 1040 break;
1038   1041  
1039 else if(Is_SubOption(argv[arg_index])) 1042 else if(Is_SubOption(argv[arg_index]))
1040 { 1043 {
1041 if (arg_index< argc) 1044 if (arg_index< argc)
1042 arg_index++; 1045 arg_index++;
1043 else 1046 else
1044 break; 1047 break;
1045   1048  
1046 if (strcmp(argv[arg_index-1],"--a")==0) 1049 if (strcmp(argv[arg_index-1],"--a")==0)
1047 { 1050 {
1048 address = _tcstoul(argv[arg_index], 0, 16) ; 1051 address = _tcstoul(argv[arg_index], 0, 16) ;
1049 } 1052 }
1050 else if (strcmp(argv[arg_index-1],"--v")==0) 1053 else if (strcmp(argv[arg_index-1],"--v")==0)
1051 { 1054 {
1052 Verify = true; 1055 Verify = true;
1053 arg_index--; 1056 arg_index--;
1054 } 1057 }
1055 else if (strcmp(argv[arg_index-1],"--o")==0) 1058 else if (strcmp(argv[arg_index-1],"--o")==0)
1056 { 1059 {
1057 optimize = TRUE; 1060 optimize = TRUE;
1058 arg_index--; 1061 arg_index--;
1059 } 1062 }
1060 else if (strcmp(argv[arg_index-1],"--fn")==0) 1063 else if (strcmp(argv[arg_index-1],"--fn")==0)
1061 { 1064 {
1062 filename = argv[arg_index]; 1065 filename = argv[arg_index];
1063 _splitpath(filename,Drive,Dir,Fname,Ext); 1066 _splitpath(filename,Drive,Dir,Fname,Ext);
1064 ptr=strupr(Ext); 1067 ptr=strupr(Ext);
1065 strcpy(Ext, ptr); 1068 strcpy(Ext, ptr);
1066 } 1069 }
1067 } 1070 }
1068 else 1071 else
1069 { 1072 {
1070 if (arg_index < argc - 1) 1073 if (arg_index < argc - 1)
1071 printf("bad parameter [%s] \n", argv[arg_index]); 1074 printf("bad parameter [%s] \n", argv[arg_index]);
1072   1075  
1073 if(COM_is_Open()) 1076 if(COM_is_Open())
1074 COM_Close(); 1077 COM_Close();
1075   1078  
1076 printf("\n Press any key to continue ... 18"); 1079 printf("\n Press any key to continue ... 18");
1077 getchar(); 1080 getchar();
1078 return 1; 1081 return 1;
1079 } 1082 }
1080 } 1083 }
1081   1084  
1082 PMAPPINGSECTOR pSector = pmMapping->pSectors; 1085 PMAPPINGSECTOR pSector = pmMapping->pSectors;
1083 for(int i = 1; i<= (int)pmMapping->NbSectors; i++) 1086 for(int i = 1; i<= (int)pmMapping->NbSectors; i++)
1084 { 1087 {
1085 if ((strcmp(Ext, ".BIN")!=0) && (i==0)) 1088 if ((strcmp(Ext, ".BIN")!=0) && (i==0))
1086 address = pSector->dwStartAddress; 1089 address = pSector->dwStartAddress;
1087   1090  
1088 pSector->UseForOperation = TRUE; 1091 pSector->UseForOperation = TRUE;
1089 pSector++; 1092 pSector++;
1090 } 1093 }
1091   1094  
1092 if(!FileExist((LPCTSTR)filename)) 1095 if(!FileExist((LPCTSTR)filename))
1093 { 1096 {
1094 printf( "file does not exist %s \n", filename); 1097 printf( "file does not exist %s \n", filename);
1095   1098  
1096 if(COM_is_Open()) 1099 if(COM_is_Open())
1097 COM_Close(); 1100 COM_Close();
1098   1101  
1099 printf("\n Press any key to continue ... 19"); 1102 printf("\n Press any key to continue ... 19");
1100 getchar(); 1103 getchar();
1101 return 1; 1104 return 1;
1102 } 1105 }
1103 1106
1104 //****************** This section is only for STM8 boot loader ******************* 1107 //****************** This section is only for STM8 boot loader *******************
1105 BYTE Version; 1108 BYTE Version;
1106 Commands pCmds; 1109 Commands pCmds;
1107 CString m_Version; 1110 CString m_Version;
1108 if (STBL_GET(&Version, &pCmds) == SUCCESS) 1111 if (STBL_GET(&Version, &pCmds) == SUCCESS)
1109 { 1112 {
1110 m_Version.Format("%x.%x",Version/16, Version & 0x0F) ; 1113 m_Version.Format("%x.%x",Version/16, Version & 0x0F) ;
1111 } 1114 }
1112 CIni Ini((LPCSTR)MapFile); 1115 CIni Ini((LPCSTR)MapFile);
1113   1116  
1114 if(Ini.IsKeyExist((LPCTSTR)"Product",(LPCTSTR)m_Version)) 1117 if(Ini.IsKeyExist((LPCTSTR)"Product",(LPCTSTR)m_Version))
1115 { 1118 {
1116 CString E_W_ROUTINEs = Ini.GetString((LPCTSTR)"Product",(LPCTSTR)m_Version, ""); 1119 CString E_W_ROUTINEs = Ini.GetString((LPCTSTR)"Product",(LPCTSTR)m_Version, "");
1117 CString Path(*__argv); 1120 CString Path(*__argv);
1118 int j=Path.ReverseFind('\\')+1; 1121 int j=Path.ReverseFind('\\')+1;
1119 if(j) Path=Path.Left(j); 1122 if(j) Path=Path.Left(j);
1120   1123  
1121 CString ToFind; 1124 CString ToFind;
1122   1125  
1123 ToFind.Format("%s%s%s", Path, "STM8_Routines\\", E_W_ROUTINEs); 1126 ToFind.Format("%s%s%s", Path, "STM8_Routines\\", E_W_ROUTINEs);
1124   1127  
1125 if(!E_W_ROUTINEs.IsEmpty()) 1128 if(!E_W_ROUTINEs.IsEmpty())
1126 { 1129 {
1127 if(!FileExist((LPCTSTR)ToFind)) 1130 if(!FileExist((LPCTSTR)ToFind))
1128 { 1131 {
1129 printf("\n!WARNING the erase or download operation may fail \n EW routines file is missing [%s]\n", ToFind); 1132 printf("\n!WARNING the erase or download operation may fail \n EW routines file is missing [%s]\n", ToFind);
1130 } 1133 }
1131 else 1134 else
1132 { 1135 {
1133 HANDLE Image; 1136 HANDLE Image;
1134 if (FILES_ImageFromFile((LPSTR)(LPCSTR)ToFind,&Image, 1)== FILES_NOERROR) 1137 if (FILES_ImageFromFile((LPSTR)(LPCSTR)ToFind,&Image, 1)== FILES_NOERROR)
1135 { 1138 {
1136 FILES_SetImageName(Image,(LPSTR)(LPCSTR)ToFind); 1139 FILES_SetImageName(Image,(LPSTR)(LPCSTR)ToFind);
1137   1140  
1138 DWORD NbElements; 1141 DWORD NbElements;
1139 if (FILES_GetImageNbElement(Image, &NbElements) == FILES_NOERROR) 1142 if (FILES_GetImageNbElement(Image, &NbElements) == FILES_NOERROR)
1140 { 1143 {
1141 for (int el=0; el< (int)NbElements;el++) 1144 for (int el=0; el< (int)NbElements;el++)
1142 { 1145 {
1143 IMAGEELEMENT Element={0}; 1146 IMAGEELEMENT Element={0};
1144 if (FILES_GetImageElement(Image, el, &Element) == FILES_NOERROR) 1147 if (FILES_GetImageElement(Image, el, &Element) == FILES_NOERROR)
1145 { 1148 {
1146 Element.Data=new BYTE[Element.dwDataLength]; 1149 Element.Data=new BYTE[Element.dwDataLength];
1147 if (FILES_GetImageElement(Image, el, &Element) == FILES_NOERROR) 1150 if (FILES_GetImageElement(Image, el, &Element) == FILES_NOERROR)
1148 { 1151 {
1149 if (STBL_DNLOAD(Element.dwAddress, Element.Data, Element.dwDataLength, FALSE) != SUCCESS) 1152 if (STBL_DNLOAD(Element.dwAddress, Element.Data, Element.dwDataLength, FALSE) != SUCCESS)
1150 { 1153 {
1151 1154
1152 } 1155 }
1153 } 1156 }
1154 } 1157 }
1155 } 1158 }
1156   1159  
1157 // Verify writen data 1160 // Verify writen data
1158 BOOL VerifySuccess = TRUE; 1161 BOOL VerifySuccess = TRUE;
1159 _sleep(100); 1162 _sleep(100);
1160   1163  
1161 #ifndef _VS6_USED 1164 //#ifndef _VS6_USED
1162 int el; 1165 int el;
1163 #endif 1166 //#endif
1164   1167  
1165 for (el=0; el< (int)NbElements;el++) 1168 for (el=0; el< (int)NbElements;el++)
1166 { 1169 {
1167 IMAGEELEMENT Element={0}; 1170 IMAGEELEMENT Element={0};
1168 if (FILES_GetImageElement(Image, el, &Element) == FILES_NOERROR) 1171 if (FILES_GetImageElement(Image, el, &Element) == FILES_NOERROR)
1169 { 1172 {
1170 Element.Data=new BYTE[Element.dwDataLength]; 1173 Element.Data=new BYTE[Element.dwDataLength];
1171 if (FILES_GetImageElement(Image, el, &Element) == FILES_NOERROR) 1174 if (FILES_GetImageElement(Image, el, &Element) == FILES_NOERROR)
1172 { 1175 {
1173 if (STBL_VERIFY(Element.dwAddress, Element.Data, Element.dwDataLength, FALSE) != SUCCESS) 1176 if (STBL_VERIFY(Element.dwAddress, Element.Data, Element.dwDataLength, FALSE) != SUCCESS)
1174 { 1177 {
1175 VerifySuccess = FALSE; 1178 VerifySuccess = FALSE;
1176 char str[255]; 1179 char str[255];
1177 sprintf(str, "%s at address :0x%X. \n%s \nPlease disable the write protection then try agin.", "Data not matching ", Element.dwAddress, "The page may be write protected."); 1180 sprintf(str, "%s at address :0x%X. \n%s \nPlease disable the write protection then try agin.", "Data not matching ", Element.dwAddress, "The page may be write protected.");
1178 AfxMessageBox(str, MB_OK|MB_ICONEXCLAMATION); 1181 AfxMessageBox(str, MB_OK|MB_ICONEXCLAMATION);
1179 return 1; 1182 return 1;
1180 } 1183 }
1181 } 1184 }
1182 } 1185 }
1183 } 1186 }
1184 } 1187 }
1185 } 1188 }
1186 else 1189 else
1187 { 1190 {
1188 AfxMessageBox("Unable to load data from this file " + ToFind + " ..."); 1191 AfxMessageBox("Unable to load data from this file " + ToFind + " ...");
1189 return -1; 1192 return -1;
1190 } 1193 }
1191 } 1194 }
1192 } 1195 }
1193 } 1196 }
1194 else 1197 else
1195 { 1198 {
1196 int family = Ini.GetInt((LPCTSTR)"Product",(LPCTSTR)"family", 0); 1199 int family = Ini.GetInt((LPCTSTR)"Product",(LPCTSTR)"family", 0);
1197 if(family == 3) 1200 if(family == 3)
1198 { 1201 {
1199 printf("\n!WARNING the erase or download operation may fail \n EW routines file is missing\n"); 1202 printf("\n!WARNING the erase or download operation may fail \n EW routines file is missing\n");
1200 } 1203 }
1201 } 1204 }
1202 //End****************** This section is only for STM8 boot loader ******************* 1205 //End****************** This section is only for STM8 boot loader *******************
1203   1206  
1204 printf("\n DOWNLOADING ... \n\n"); 1207 printf("\n DOWNLOADING ... \n\n");
1205   1208  
1206 HANDLE Handle; 1209 HANDLE Handle;
1207 if (FILES_ImageFromFile((LPSTR)(LPCSTR)filename,&Handle, 1) == FILES_NOERROR) 1210 if (FILES_ImageFromFile((LPSTR)(LPCSTR)filename,&Handle, 1) == FILES_NOERROR)
1208 { 1211 {
1209 FILES_SetImageName(Handle,(LPSTR)(LPCSTR)filename); 1212 FILES_SetImageName(Handle,(LPSTR)(LPCSTR)filename);
1210   1213  
1211 DWORD NbElements = 0; 1214 DWORD NbElements = 0;
1212 if (FILES_GetImageNbElement(Handle, &NbElements) == FILES_NOERROR) 1215 if (FILES_GetImageNbElement(Handle, &NbElements) == FILES_NOERROR)
1213 { 1216 {
1214 if ( NbElements > 0 ) 1217 if ( NbElements > 0 )
1215 { // if binary file -> change the elemnts address 1218 { // if binary file -> change the elemnts address
1216 if (strcmp(Ext, ".BIN")==0) 1219 if (strcmp(Ext, ".BIN")==0)
1217 { 1220 {
1218 for (int i=0;i< (int)NbElements;i++) 1221 for (int i=0;i< (int)NbElements;i++)
1219 { 1222 {
1220 IMAGEELEMENT Element={0}; 1223 IMAGEELEMENT Element={0};
1221 if (FILES_GetImageElement(Handle, i, &Element) == FILES_NOERROR) 1224 if (FILES_GetImageElement(Handle, i, &Element) == FILES_NOERROR)
1222 { 1225 {
1223 Element.Data= (LPBYTE)malloc(Element.dwDataLength); 1226 Element.Data= (LPBYTE)malloc(Element.dwDataLength);
1224 if (FILES_GetImageElement(Handle, i, &Element) == FILES_NOERROR) 1227 if (FILES_GetImageElement(Handle, i, &Element) == FILES_NOERROR)
1225 { 1228 {
1226 Element.dwAddress = Element.dwAddress + address; 1229 Element.dwAddress = Element.dwAddress + address;
1227 FILES_SetImageElement(Handle, i, FALSE, Element); 1230 FILES_SetImageElement(Handle, i, FALSE, Element);
1228 } 1231 }
1229 } 1232 }
1230 } 1233 }
1231 } 1234 }
1232 } 1235 }
1233 } 1236 }
1234   1237  
1235 FILES_FilterImageForOperation(Handle, pmMapping, OPERATION_UPLOAD, optimize); 1238 FILES_FilterImageForOperation(Handle, pmMapping, OPERATION_UPLOAD, optimize);
1236 } 1239 }
1237 else 1240 else
1238 { 1241 {
1239 printf("cannot open file %s \n", filename); 1242 printf("cannot open file %s \n", filename);
1240   1243  
1241 if(COM_is_Open()) 1244 if(COM_is_Open())
1242 COM_Close(); 1245 COM_Close();
1243   1246  
1244 printf("\n Press any key to continue ... 20"); 1247 printf("\n Press any key to continue ... 20");
1245 getchar(); 1248 getchar();
1246 return 1; 1249 return 1;
1247 } 1250 }
1248   1251  
1249 DWORD NbElements = 0; 1252 DWORD NbElements = 0;
1250 if (FILES_GetImageNbElement(Handle, &NbElements) == FILES_NOERROR) 1253 if (FILES_GetImageNbElement(Handle, &NbElements) == FILES_NOERROR)
1251 { 1254 {
1252 for (int el=0; el< (int)NbElements;el++) 1255 for (int el=0; el< (int)NbElements;el++)
1253 { 1256 {
1254 IMAGEELEMENT Element={0}; 1257 IMAGEELEMENT Element={0};
1255 if (FILES_GetImageElement(Handle, el, &Element) == FILES_NOERROR) 1258 if (FILES_GetImageElement(Handle, el, &Element) == FILES_NOERROR)
1256 { 1259 {
1257 Element.Data= (LPBYTE)malloc(Element.dwDataLength); 1260 Element.Data= (LPBYTE)malloc(Element.dwDataLength);
1258 if (FILES_GetImageElement(Handle, el, &Element) == FILES_NOERROR) 1261 if (FILES_GetImageElement(Handle, el, &Element) == FILES_NOERROR)
1259 { 1262 {
1260 if ((strcmp(Ext, ".BIN")==0) && (el==0)) 1263 if ((strcmp(Ext, ".BIN")==0) && (el==0))
1261 Element.dwAddress = address; 1264 Element.dwAddress = address;
1262 1265
1263 if (STBL_DNLOAD(Element.dwAddress, Element.Data, Element.dwDataLength, optimize) != SUCCESS) 1266 if (STBL_DNLOAD(Element.dwAddress, Element.Data, Element.dwDataLength, optimize) != SUCCESS)
1264 { 1267 {
1265 write_debug_info( "downloading", el ,Element.dwAddress, (float)Element.dwDataLength/(float)1024, KO); 1268 write_debug_info( "downloading", el ,Element.dwAddress, (float)Element.dwDataLength/(float)1024, KO);
1266 write_debug_info("The flash may be read protected; use -p --drp to disable write protection." , 0, 0, 0, KO); 1269 write_debug_info("The flash may be read protected; use -p --drp to disable write protection." , 0, 0, 0, KO);
1267 1270
1268 if(COM_is_Open()) 1271 if(COM_is_Open())
1269 COM_Close(); 1272 COM_Close();
1270 1273
1271 printf("\n Press any key to continue ... 21"); 1274 printf("\n Press any key to continue ... 21");
1272 getchar(); 1275 getchar();
1273 return 1; 1276 return 1;
1274 } 1277 }
1275   1278  
1276 write_debug_info( "downloading", el ,Element.dwAddress, (float)Element.dwDataLength/(float)1024, OK); 1279 write_debug_info( "downloading", el ,Element.dwAddress, (float)Element.dwDataLength/(float)1024, OK);
1277 } 1280 }
1278 } 1281 }
1279 } 1282 }
1280 } 1283 }
1281   1284  
1282 bool VerifySuccess = true; 1285 bool VerifySuccess = true;
1283 if (Verify) 1286 if (Verify)
1284 { 1287 {
1285 printf("\n VERIFYING ... \n\n"); 1288 printf("\n VERIFYING ... \n\n");
1286   1289  
1287   1290  
1288 for (int el=0; el< (int)NbElements;el++) 1291 for (int el=0; el< (int)NbElements;el++)
1289 { 1292 {
1290 IMAGEELEMENT Element={0}; 1293 IMAGEELEMENT Element={0};
1291 if (FILES_GetImageElement(Handle, el, &Element) == FILES_NOERROR) 1294 if (FILES_GetImageElement(Handle, el, &Element) == FILES_NOERROR)
1292 { 1295 {
1293 Element.Data=(LPBYTE)malloc(Element.dwDataLength); 1296 Element.Data=(LPBYTE)malloc(Element.dwDataLength);
1294 if (FILES_GetImageElement(Handle, el, &Element) == FILES_NOERROR) 1297 if (FILES_GetImageElement(Handle, el, &Element) == FILES_NOERROR)
1295 { 1298 {
1296 if ((strcmp(Ext, ".BIN")==0) && (el==0)) 1299 if ((strcmp(Ext, ".BIN")==0) && (el==0))
1297 Element.dwAddress = address; 1300 Element.dwAddress = address;
1298   1301  
1299 if (STBL_VERIFY(Element.dwAddress, Element.Data, Element.dwDataLength, optimize) != SUCCESS) 1302 if (STBL_VERIFY(Element.dwAddress, Element.Data, Element.dwDataLength, optimize) != SUCCESS)
1300 { 1303 {
1301 VerifySuccess = false; 1304 VerifySuccess = false;
1302 write_debug_info("verifying" ,el ,Element.dwAddress, (float)Element.dwDataLength/(float)1024, KO); 1305 write_debug_info("verifying" ,el ,Element.dwAddress, (float)Element.dwDataLength/(float)1024, KO);
1303 write_debug_info("some pages may be write protected; use -p --dwp to disable write protection." , 0, 0, 0, KO); 1306 write_debug_info("some pages may be write protected; use -p --dwp to disable write protection." , 0, 0, 0, KO);
1304 1307
1305 if(COM_is_Open()) 1308 if(COM_is_Open())
1306 COM_Close(); 1309 COM_Close();
1307 1310
1308 printf("\n Press any key to continue ... 22"); 1311 printf("\n Press any key to continue ... 22");
1309 getchar(); 1312 getchar();
1310 return 1; 1313 return 1;
1311 } 1314 }
1312   1315  
1313 write_debug_info("verifying" ,el ,Element.dwAddress, (float)Element.dwDataLength/(float)1024, OK); 1316 write_debug_info("verifying" ,el ,Element.dwAddress, (float)Element.dwDataLength/(float)1024, OK);
1314 } 1317 }
1315 } 1318 }
1316 } 1319 }
1317 } 1320 }
1318   1321  
1319 } 1322 }
1320 //============================ VERIFY ================================================ 1323 //============================ VERIFY ================================================
1321 else if (strcmp(argv[arg_index],"-v")==0) 1324 else if (strcmp(argv[arg_index],"-v")==0)
1322 { 1325 {
1323 if (arg_index< argc) 1326 if (arg_index< argc)
1324 arg_index++; 1327 arg_index++;
1325 else 1328 else
1326 break; 1329 break;
1327 } 1330 }
1328 //============================ Program option bytes ================================== 1331 //============================ Program option bytes ==================================
1329 else if (strcmp(argv[arg_index],"-o")==0) 1332 else if (strcmp(argv[arg_index],"-o")==0)
1330 { 1333 {
1331 while(arg_index < argc) 1334 while(arg_index < argc)
1332 { 1335 {
1333 if (arg_index< argc-1) 1336 if (arg_index< argc-1)
1334 arg_index++; 1337 arg_index++;
1335 else 1338 else
1336 break; 1339 break;
1337   1340  
1338 if(Is_Option(argv[arg_index])) 1341 if(Is_Option(argv[arg_index]))
1339 break; 1342 break;
1340   1343  
1341 else if(Is_SubOption(argv[arg_index])) 1344 else if(Is_SubOption(argv[arg_index]))
1342 { 1345 {
1343 if (arg_index< argc) 1346 if (arg_index< argc)
1344 arg_index++; 1347 arg_index++;
1345 else 1348 else
1346 break; 1349 break;
1347   1350  
1348 if (strcmp(argv[arg_index-1],"--get")==0) 1351 if (strcmp(argv[arg_index-1],"--get")==0)
1349 { 1352 {
1350 if (arg_index< argc) 1353 if (arg_index< argc)
1351 arg_index++; 1354 arg_index++;
1352 else 1355 else
1353 break; 1356 break;
1354 1357
1355 if (strcmp(argv[arg_index-1],"--fn")==0) 1358 if (strcmp(argv[arg_index-1],"--fn")==0)
1356 filename = argv[arg_index]; 1359 filename = argv[arg_index];
1357 1360
1358 if(TARGET_GetSIFData(&User, &RDP, &Data0, &Data1, &WRP0, &WRP1, &WRP2, &WRP3) == SUCCESS) 1361 if(TARGET_GetSIFData(&User, &RDP, &Data0, &Data1, &WRP0, &WRP1, &WRP2, &WRP3) == SUCCESS)
1359 { 1362 {
1360 write_debug_info("Getting Option bytes data" ,0 ,0, 0, OK); 1363 write_debug_info("Getting Option bytes data" ,0 ,0, 0, OK);
1361   1364  
1362 HANDLE Image; 1365 HANDLE Image;
1363 if (FILES_CreateImage(&Image, 1) == FILES_NOERROR) 1366 if (FILES_CreateImage(&Image, 1) == FILES_NOERROR)
1364 { 1367 {
1365 IMAGEELEMENT Element={0}; 1368 IMAGEELEMENT Element={0};
1366 Element.dwAddress = 0x1FFFF800; 1369 Element.dwAddress = 0x1FFFF800;
1367 Element.dwDataLength = 16; 1370 Element.dwDataLength = 16;
1368 Element.Data = (LPBYTE)malloc(Element.dwDataLength); 1371 Element.Data = (LPBYTE)malloc(Element.dwDataLength);
1369   1372  
1370 { 1373 {
1371 Element.Data[0] = RDP; 1374 Element.Data[0] = RDP;
1372 Element.Data[1] = ~RDP; 1375 Element.Data[1] = ~RDP;
1373 Element.Data[2] = User; 1376 Element.Data[2] = User;
1374 Element.Data[3] = ~User; 1377 Element.Data[3] = ~User;
1375 Element.Data[4] = Data0; 1378 Element.Data[4] = Data0;
1376 Element.Data[5] = ~Data0; 1379 Element.Data[5] = ~Data0;
1377 Element.Data[6] = Data1; 1380 Element.Data[6] = Data1;
1378 Element.Data[7] = ~Data1; 1381 Element.Data[7] = ~Data1;
1379 Element.Data[8] = WRP0; 1382 Element.Data[8] = WRP0;
1380 Element.Data[9] = ~WRP0; 1383 Element.Data[9] = ~WRP0;
1381 Element.Data[10] = WRP1; 1384 Element.Data[10] = WRP1;
1382 Element.Data[11] = ~WRP1; 1385 Element.Data[11] = ~WRP1;
1383 Element.Data[12] = WRP2; 1386 Element.Data[12] = WRP2;
1384 Element.Data[13] = ~WRP2; 1387 Element.Data[13] = ~WRP2;
1385 Element.Data[14] = WRP3; 1388 Element.Data[14] = WRP3;
1386 Element.Data[15] = ~WRP3; 1389 Element.Data[15] = ~WRP3;
1387 } 1390 }
1388   1391  
1389 FILES_SetImageElement(Image,0,TRUE,Element); 1392 FILES_SetImageElement(Image,0,TRUE,Element);
1390 if (FILES_ImageToFile((LPSTR)(LPCSTR)filename,Image) != FILES_NOERROR) 1393 if (FILES_ImageToFile((LPSTR)(LPCSTR)filename,Image) != FILES_NOERROR)
1391 { 1394 {
1392 write_debug_info("Saving Option bytes data",0 ,0, 0, KO); 1395 write_debug_info("Saving Option bytes data",0 ,0, 0, KO);
1393 } 1396 }
1394 else write_debug_info("Saving Option bytes data",0 ,0, 0, OK); 1397 else write_debug_info("Saving Option bytes data",0 ,0, 0, OK);
1395 } 1398 }
1396 } 1399 }
1397 else write_debug_info("Getting Option bytes data" ,0 ,0, 0, KO); 1400 else write_debug_info("Getting Option bytes data" ,0 ,0, 0, KO);
1398 } 1401 }
1399 else if (strcmp(argv[arg_index-1],"--set")==0) 1402 else if (strcmp(argv[arg_index-1],"--set")==0)
1400 { 1403 {
1401 if (arg_index< argc) arg_index++; 1404 if (arg_index< argc) arg_index++;
1402 else break; 1405 else break;
1403 1406
1404 if (strcmp(argv[arg_index-1],"--fn")==0) 1407 if (strcmp(argv[arg_index-1],"--fn")==0)
1405 { 1408 {
1406 filename = argv[arg_index]; 1409 filename = argv[arg_index];
1407   1410  
1408 HANDLE OPBImage; 1411 HANDLE OPBImage;
1409   1412  
1410 if(!FileExist((LPCTSTR)filename)) 1413 if(!FileExist((LPCTSTR)filename))
1411 { 1414 {
1412 printf( "file does not exist %s \n", filename); 1415 printf( "file does not exist %s \n", filename);
1413 1416
1414 if(COM_is_Open()) 1417 if(COM_is_Open())
1415 COM_Close(); 1418 COM_Close();
1416 1419
1417 printf("\n Press any key to continue ... 23"); 1420 printf("\n Press any key to continue ... 23");
1418 getchar(); 1421 getchar();
1419 return 1; 1422 return 1;
1420 } 1423 }
1421   1424  
1422 if (FILES_ImageFromFile((LPSTR)(LPCSTR)filename, &OPBImage, 0) == FILES_NOERROR) 1425 if (FILES_ImageFromFile((LPSTR)(LPCSTR)filename, &OPBImage, 0) == FILES_NOERROR)
1423 { 1426 {
1424 DWORD NbElements = 0; 1427 DWORD NbElements = 0;
1425 if (FILES_GetImageNbElement(OPBImage, &NbElements) == FILES_NOERROR) 1428 if (FILES_GetImageNbElement(OPBImage, &NbElements) == FILES_NOERROR)
1426 { 1429 {
1427 if ( NbElements == 1 ) 1430 if ( NbElements == 1 )
1428 { 1431 {
1429 IMAGEELEMENT Element={0}; 1432 IMAGEELEMENT Element={0};
1430 if (FILES_GetImageElement(OPBImage, 0, &Element) == FILES_NOERROR) 1433 if (FILES_GetImageElement(OPBImage, 0, &Element) == FILES_NOERROR)
1431 { 1434 {
1432 Element.Data= (LPBYTE)malloc(Element.dwDataLength); 1435 Element.Data= (LPBYTE)malloc(Element.dwDataLength);
1433 if (FILES_GetImageElement(OPBImage, 0, &Element) == FILES_NOERROR) 1436 if (FILES_GetImageElement(OPBImage, 0, &Element) == FILES_NOERROR)
1434 { 1437 {
1435 RDP = Element.Data[0] ; 1438 RDP = Element.Data[0] ;
1436 User = Element.Data[2] ; 1439 User = Element.Data[2] ;
1437 Data0 = Element.Data[4] ; 1440 Data0 = Element.Data[4] ;
1438 Data1 = Element.Data[6] ; 1441 Data1 = Element.Data[6] ;
1439 WRP0 = Element.Data[8] ; 1442 WRP0 = Element.Data[8] ;
1440 WRP1 = Element.Data[10]; 1443 WRP1 = Element.Data[10];
1441 WRP2 = Element.Data[12]; 1444 WRP2 = Element.Data[12];
1442 WRP3 = Element.Data[14]; 1445 WRP3 = Element.Data[14];
1443   1446  
1444 1447
1445 if (TARGET_SetSIFData(User, RDP, Data0, Data1, WRP0, WRP1, WRP2, WRP3) == SUCCESS) 1448 if (TARGET_SetSIFData(User, RDP, Data0, Data1, WRP0, WRP1, WRP2, WRP3) == SUCCESS)
1446 { 1449 {
1447 write_debug_info("Setting Option bytes data" ,0 ,0, 0, OK); 1450 write_debug_info("Setting Option bytes data" ,0 ,0, 0, OK);
1448   1451  
1449 if(COM_is_Open()) 1452 if(COM_is_Open())
1450 COM_Close(); 1453 COM_Close();
1451   1454  
1452 COM_Open(); 1455 COM_Open();
1453   1456  
1454 if(STBL_Init_BL() != SUCCESS) 1457 if(STBL_Init_BL() != SUCCESS)
1455 write_debug_info("Resetting device" ,0 ,0, 0, KO); 1458 write_debug_info("Resetting device" ,0 ,0, 0, KO);
1456 else 1459 else
1457 write_debug_info("Resetting device" ,0 ,0, 0, OK); 1460 write_debug_info("Resetting device" ,0 ,0, 0, OK);
1458 } 1461 }
1459 else 1462 else
1460 write_debug_info("Setting Option bytes data" ,0 ,0, 0, KO); 1463 write_debug_info("Setting Option bytes data" ,0 ,0, 0, KO);
1461 } 1464 }
1462 } 1465 }
1463 } 1466 }
1464 } 1467 }
1465 } 1468 }
1466 } 1469 }
1467 else if (strcmp(argv[arg_index-1],"--vals")==0) 1470 else if (strcmp(argv[arg_index-1],"--vals")==0)
1468 { 1471 {
1469 TARGET_GetSIFData(&User, &RDP, &Data0, &Data1, &WRP0, &WRP1, &WRP2, &WRP3); 1472 TARGET_GetSIFData(&User, &RDP, &Data0, &Data1, &WRP0, &WRP1, &WRP2, &WRP3);
1470 1473
1471 while(arg_index< argc) 1474 while(arg_index< argc)
1472 { 1475 {
1473 if(Is_Option(argv[arg_index])) 1476 if(Is_Option(argv[arg_index]))
1474 break; 1477 break;
1475 else if(Is_SubOption(argv[arg_index])) 1478 else if(Is_SubOption(argv[arg_index]))
1476 { 1479 {
1477 arg_index++; 1480 arg_index++;
1478 if(strcmp(argv[arg_index-1],"--RDP")==0) { RDP = _tcstoul(argv[arg_index], 0, 16);arg_index++;} 1481 if(strcmp(argv[arg_index-1],"--RDP")==0) { RDP = _tcstoul(argv[arg_index], 0, 16);arg_index++;}
1479 else if(strcmp(argv[arg_index-1],"--User")==0) { User = _tcstoul(argv[arg_index], 0, 16);arg_index++;} 1482 else if(strcmp(argv[arg_index-1],"--User")==0) { User = _tcstoul(argv[arg_index], 0, 16);arg_index++;}
1480 else if(strcmp(argv[arg_index-1],"--data0")==0){ Data0 = _tcstoul(argv[arg_index], 0, 16);arg_index++;} 1483 else if(strcmp(argv[arg_index-1],"--data0")==0){ Data0 = _tcstoul(argv[arg_index], 0, 16);arg_index++;}
1481 else if(strcmp(argv[arg_index-1],"--data1")==0){ Data1 = _tcstoul(argv[arg_index], 0, 16);arg_index++;} 1484 else if(strcmp(argv[arg_index-1],"--data1")==0){ Data1 = _tcstoul(argv[arg_index], 0, 16);arg_index++;}
1482 else if(strcmp(argv[arg_index-1],"--WRP0")==0) { WRP0 = _tcstoul(argv[arg_index], 0, 16);arg_index++;} 1485 else if(strcmp(argv[arg_index-1],"--WRP0")==0) { WRP0 = _tcstoul(argv[arg_index], 0, 16);arg_index++;}
1483 else if(strcmp(argv[arg_index-1],"--WRP1")==0) { WRP1 = _tcstoul(argv[arg_index], 0, 16);arg_index++;} 1486 else if(strcmp(argv[arg_index-1],"--WRP1")==0) { WRP1 = _tcstoul(argv[arg_index], 0, 16);arg_index++;}
1484 else if(strcmp(argv[arg_index-1],"--WRP2")==0) { WRP2 = _tcstoul(argv[arg_index], 0, 16);arg_index++;} 1487 else if(strcmp(argv[arg_index-1],"--WRP2")==0) { WRP2 = _tcstoul(argv[arg_index], 0, 16);arg_index++;}
1485 else if(strcmp(argv[arg_index-1],"--WRP3")==0) { WRP3 = _tcstoul(argv[arg_index], 0, 16);arg_index++;} 1488 else if(strcmp(argv[arg_index-1],"--WRP3")==0) { WRP3 = _tcstoul(argv[arg_index], 0, 16);arg_index++;}
1486 } 1489 }
1487 } 1490 }
1488 if (TARGET_SetSIFData(User, RDP, Data0, Data1, WRP0, WRP1, WRP2, WRP3) != SUCCESS) 1491 if (TARGET_SetSIFData(User, RDP, Data0, Data1, WRP0, WRP1, WRP2, WRP3) != SUCCESS)
1489 write_debug_info("Setting Option bytes data" ,0 ,0, 0, KO); 1492 write_debug_info("Setting Option bytes data" ,0 ,0, 0, KO);
1490 else 1493 else
1491 { 1494 {
1492 write_debug_info("Setting Option bytes data" ,0 ,0, 0, OK); 1495 write_debug_info("Setting Option bytes data" ,0 ,0, 0, OK);
1493   1496  
1494 if(COM_is_Open()) 1497 if(COM_is_Open())
1495 COM_Close(); 1498 COM_Close();
1496   1499  
1497 COM_Open(); 1500 COM_Open();
1498   1501  
1499 if(STBL_Init_BL() != SUCCESS) 1502 if(STBL_Init_BL() != SUCCESS)
1500 write_debug_info("Resetting device" ,0 ,0, 0, KO); 1503 write_debug_info("Resetting device" ,0 ,0, 0, KO);
1501 else 1504 else
1502 write_debug_info("Resetting device" ,0 ,0, 0, OK); 1505 write_debug_info("Resetting device" ,0 ,0, 0, OK);
1503 } 1506 }
1504 arg_index--; 1507 arg_index--;
1505 } 1508 }
1506 } 1509 }
1507 } 1510 }
1508 else 1511 else
1509 { 1512 {
1510 if (arg_index < argc - 1) 1513 if (arg_index < argc - 1)
1511 printf("bad parameter [%s] \n", argv[arg_index]); 1514 printf("bad parameter [%s] \n", argv[arg_index]);
1512   1515  
1513 if(COM_is_Open()) 1516 if(COM_is_Open())
1514 COM_Close(); 1517 COM_Close();
1515   1518  
1516 printf("\n Press any key to continue ... 24"); 1519 printf("\n Press any key to continue ... 24");
1517 getchar(); 1520 getchar();
1518 return 1; 1521 return 1;
1519 } 1522 }
1520 } 1523 }
1521 } 1524 }
1522 //============================ Set/Unset R/W protection ========================== 1525 //============================ Set/Unset R/W protection ==========================
1523 else if (strcmp(argv[arg_index],"-p")==0) 1526 else if (strcmp(argv[arg_index],"-p")==0)
1524 { 1527 {
1525 while(arg_index < argc) 1528 while(arg_index < argc)
1526 { 1529 {
1527 if (arg_index< argc-1) 1530 if (arg_index< argc-1)
1528 arg_index++; 1531 arg_index++;
1529 else 1532 else
1530 break; 1533 break;
1531   1534  
1532 if(Is_Option(argv[arg_index])) 1535 if(Is_Option(argv[arg_index]))
1533 break; 1536 break;
1534   1537  
1535 else if(Is_SubOption(argv[arg_index])) 1538 else if(Is_SubOption(argv[arg_index]))
1536 { 1539 {
1537 if (arg_index< argc) 1540 if (arg_index< argc)
1538 arg_index++; 1541 arg_index++;
1539 else 1542 else
1540 break; 1543 break;
1541   1544  
1542 if (strcmp(argv[arg_index-1],"--erp")==0) 1545 if (strcmp(argv[arg_index-1],"--erp")==0)
1543 { 1546 {
1544 if(STBL_READOUT_PROTECT() != SUCCESS) 1547 if(STBL_READOUT_PROTECT() != SUCCESS)
1545 write_debug_info( "enabling read protection", 0 , 0, 0, KO); 1548 write_debug_info( "enabling read protection", 0 , 0, 0, KO);
1546 else 1549 else
1547 write_debug_info( "enabling read protection", 0 , 0, 0, OK); 1550 write_debug_info( "enabling read protection", 0 , 0, 0, OK);
1548 1551
1549 _sleep(TimeBO); 1552 _sleep(TimeBO);
1550   1553  
1551 if(STBL_Init_BL() != SUCCESS) 1554 if(STBL_Init_BL() != SUCCESS)
1552 write_debug_info( "reseting device", 0 , 0, 0, KO); 1555 write_debug_info( "reseting device", 0 , 0, 0, KO);
1553 else 1556 else
1554 write_debug_info( "reseting device", 0 , 0, 0, OK); 1557 write_debug_info( "reseting device", 0 , 0, 0, OK);
1555   1558  
1556 arg_index--; 1559 arg_index--;
1557 } 1560 }
1558 else if (strcmp(argv[arg_index-1],"--drp")==0) 1561 else if (strcmp(argv[arg_index-1],"--drp")==0)
1559 { 1562 {
1560 if(STBL_READOUT_PERM_UNPROTECT() == SUCCESS) 1563 if(STBL_READOUT_PERM_UNPROTECT() == SUCCESS)
1561 { 1564 {
1562 write_debug_info( "disabling read protection", 0 , 0, 0, OK); 1565 write_debug_info( "disabling read protection", 0 , 0, 0, OK);
1563   1566  
1564 _sleep(TimeBO); 1567 _sleep(TimeBO);
1565   1568  
1566 if(STBL_Init_BL() != SUCCESS) 1569 if(STBL_Init_BL() != SUCCESS)
1567 write_debug_info( "reseting device", 0 , 0, 0, KO); 1570 write_debug_info( "reseting device", 0 , 0, 0, KO);
1568 else 1571 else
1569 write_debug_info( "reseting device", 0 , 0, 0, OK); 1572 write_debug_info( "reseting device", 0 , 0, 0, OK);
1570 } 1573 }
1571 else 1574 else
1572 write_debug_info( "disabling read protection", 0 , 0, 0, KO); 1575 write_debug_info( "disabling read protection", 0 , 0, 0, KO);
1573 1576
1574 arg_index--; 1577 arg_index--;
1575 } 1578 }
1576 else if (strcmp(argv[arg_index-1],"--ewp")==0) 1579 else if (strcmp(argv[arg_index-1],"--ewp")==0)
1577 { 1580 {
1578 LPBYTE sectors; 1581 LPBYTE sectors;
1579 if(Is_Option(argv[arg_index])) break; 1582 if(Is_Option(argv[arg_index])) break;
1580   1583  
1581 nsec = atoi(argv[arg_index]); 1584 nsec = atoi(argv[arg_index]);
1582 sectors = (LPBYTE)malloc(nsec + 1); 1585 sectors = (LPBYTE)malloc(nsec + 1);
1583   1586  
1584   1587  
1585 sectors[0] = 0; 1588 sectors[0] = 0;
1586 for(int i = 1; i<= nsec; i++) 1589 for(int i = 1; i<= nsec; i++)
1587 { 1590 {
1588 sectors[0]++; 1591 sectors[0]++;
1589 arg_index++; 1592 arg_index++;
1590 sectors[sectors[0]] = atoi(argv[arg_index]); 1593 sectors[sectors[0]] = atoi(argv[arg_index]);
1591 } 1594 }
1592   1595  
1593   1596  
1594 printf("\nenabling write protection %i sectors : ", sectors[0]); 1597 printf("\nenabling write protection %i sectors : ", sectors[0]);
1595   1598  
1596 #ifndef _VS6_USED 1599 //#ifndef _VS6_USED
1597 int i; 1600 int i;
1598 #endif 1601 //#endif
1599   1602  
1600 for(i = 1; i<= nsec; i++) 1603 for(i = 1; i<= nsec; i++)
1601 { 1604 {
1602 printf("<%i>", sectors[i]); 1605 printf("<%i>", sectors[i]);
1603 } 1606 }
1604 printf("\n"); 1607 printf("\n");
1605   1608  
1606 if(STBL_WRITE_PROTECT(((LPBYTE)sectors)[0],&((LPBYTE)sectors)[1]) != SUCCESS) 1609 if(STBL_WRITE_PROTECT(((LPBYTE)sectors)[0],&((LPBYTE)sectors)[1]) != SUCCESS)
1607 write_debug_info( "enabling write protection", 0 , 0, 0, KO); 1610 write_debug_info( "enabling write protection", 0 , 0, 0, KO);
1608 else 1611 else
1609 write_debug_info( "enabling write protection", 0 , 0, 0, OK); 1612 write_debug_info( "enabling write protection", 0 , 0, 0, OK);
1610 1613
1611 _sleep(TimeBO); 1614 _sleep(TimeBO);
1612   1615  
1613 if(STBL_Init_BL() != SUCCESS) 1616 if(STBL_Init_BL() != SUCCESS)
1614 write_debug_info( "reseting device", 0 , 0, 0, KO); 1617 write_debug_info( "reseting device", 0 , 0, 0, KO);
1615 else 1618 else
1616 write_debug_info( "reseting device", 0 , 0, 0, OK); 1619 write_debug_info( "reseting device", 0 , 0, 0, OK);
1617 } 1620 }
1618 else if (strcmp(argv[arg_index-1],"--dwp")==0) 1621 else if (strcmp(argv[arg_index-1],"--dwp")==0)
1619 { 1622 {
1620 if(STBL_WRITE_PERM_UNPROTECT() != SUCCESS) 1623 if(STBL_WRITE_PERM_UNPROTECT() != SUCCESS)
1621 write_debug_info( "disabling write protection", 0 , 0, 0, KO); 1624 write_debug_info( "disabling write protection", 0 , 0, 0, KO);
1622 else 1625 else
1623 write_debug_info( "disabling write protection", 0 , 0, 0, OK); 1626 write_debug_info( "disabling write protection", 0 , 0, 0, OK);
1624 1627
1625 _sleep(TimeBO); 1628 _sleep(TimeBO);
1626   1629  
1627 if(STBL_Init_BL() != SUCCESS) 1630 if(STBL_Init_BL() != SUCCESS)
1628 write_debug_info( "reseting device", 0 , 0, 0, KO); 1631 write_debug_info( "reseting device", 0 , 0, 0, KO);
1629 else 1632 else
1630 write_debug_info( "reseting device", 0 , 0, 0, OK); 1633 write_debug_info( "reseting device", 0 , 0, 0, OK);
1631 arg_index--; 1634 arg_index--;
1632 } 1635 }
1633 } 1636 }
1634 else 1637 else
1635 { 1638 {
1636 if (arg_index < argc - 1) 1639 if (arg_index < argc - 1)
1637 printf("bad parameter [%s] \n", argv[arg_index]); 1640 printf("bad parameter [%s] \n", argv[arg_index]);
1638   1641  
1639 if(COM_is_Open()) 1642 if(COM_is_Open())
1640 COM_Close(); 1643 COM_Close();
1641   1644  
1642 printf("\n Press any key to continue ... 25"); 1645 printf("\n Press any key to continue ... 25");
1643 getchar(); 1646 getchar();
1644 return 1; 1647 return 1;
1645 } 1648 }
1646 } 1649 }
1647 } 1650 }
1648 //============================ Run at address ======================================== 1651 //============================ Run at address ========================================
1649 else if (strcmp(argv[arg_index],"-r")==0) 1652 else if (strcmp(argv[arg_index],"-r")==0)
1650 { 1653 {
1651 while(arg_index < argc) 1654 while(arg_index < argc)
1652 { 1655 {
1653 if (arg_index< argc-1) 1656 if (arg_index< argc-1)
1654 arg_index++; 1657 arg_index++;
1655 else 1658 else
1656 break; 1659 break;
1657   1660  
1658 if(Is_Option(argv[arg_index])) 1661 if(Is_Option(argv[arg_index]))
1659 break; 1662 break;
1660   1663  
1661 else if(Is_SubOption(argv[arg_index])) 1664 else if(Is_SubOption(argv[arg_index]))
1662 { 1665 {
1663 if (arg_index< argc) 1666 if (arg_index< argc)
1664 arg_index++; 1667 arg_index++;
1665 else 1668 else
1666 break; 1669 break;
1667   1670  
1668 PMAPPINGSECTOR pSector = pmMapping->pSectors; 1671 PMAPPINGSECTOR pSector = pmMapping->pSectors;
1669 address = pSector->dwStartAddress; 1672 address = pSector->dwStartAddress;
1670   1673  
1671 if (strcmp(argv[arg_index-1],"--a")==0) 1674 if (strcmp(argv[arg_index-1],"--a")==0)
1672 { 1675 {
1673 address = _tcstoul(argv[arg_index], 0, 16) ; 1676 address = _tcstoul(argv[arg_index], 0, 16) ;
1674 } 1677 }
1675 } 1678 }
1676 else 1679 else
1677 { 1680 {
1678 if (arg_index < argc - 1) 1681 if (arg_index < argc - 1)
1679 printf("bad parameter [%s] \n", argv[arg_index]); 1682 printf("bad parameter [%s] \n", argv[arg_index]);
1680   1683  
1681 if(COM_is_Open()) 1684 if(COM_is_Open())
1682 COM_Close(); 1685 COM_Close();
1683   1686  
1684 printf("\n Press any key to continue ... 26"); 1687 printf("\n Press any key to continue ... 26");
1685 getchar(); 1688 getchar();
1686 return 1; 1689 return 1;
1687 } 1690 }
1688   1691  
1689 if (STBL_GO(address) == SUCCESS) 1692 if (STBL_GO(address) == SUCCESS)
1690 { 1693 {
1691 printf("Your code is running...\n"); 1694 printf("Your code is running...\n");
1692 } 1695 }
1693 else 1696 else
1694 { 1697 {
1695 printf( "run fails \n"); 1698 printf( "run fails \n");
1696 } 1699 }
1697 } 1700 }
1698 } 1701 }
1699 else 1702 else
1700 { 1703 {
1701 if (arg_index < argc - 1) 1704 if (arg_index < argc - 1)
1702 printf("bad parameter [%s] \n", argv[arg_index]); 1705 printf("bad parameter [%s] \n", argv[arg_index]);
1703   1706  
1704 if(COM_is_Open()) 1707 if(COM_is_Open())
1705 COM_Close(); 1708 COM_Close();
1706   1709  
1707 printf("\n Press any key to continue ... 27"); 1710 printf("\n Press any key to continue ... 27");
1708 getchar(); 1711 getchar();
1709 return 1; 1712 return 1;
1710 } 1713 }
1711 } 1714 }
1712 } 1715 }
1713   1716  
1714 Done_Success: 1717 Done_Success:
1715 if (bAuto) 1718 if (bAuto)
1716 { 1719 {
1717 // commented 1720 // commented
1718 STBL_SetDtr(FALSE); 1721 STBL_SetDtr(FALSE);
1719 Sleep(50); 1722 Sleep(50);
1720   1723  
1721 if(COM_is_Open()) 1724 if(COM_is_Open())
1722 COM_Close(); 1725 COM_Close();
1723 COM_Open(); 1726 COM_Open();
1724   1727  
1725 // Reset = Low 1728 // Reset = Low
1726 STBL_SetRts(TRUE); 1729 STBL_SetRts(TRUE);
1727 Sleep(50); 1730 Sleep(50);
1728 write_debug_info("Unset BOOT0 & RESET ", 0 ,0, 0, OK); 1731 write_debug_info("Unset BOOT0 & RESET ", 0 ,0, 0, OK);
1729 // Reset = High 1732 // Reset = High
1730 STBL_SetRts(FALSE); 1733 STBL_SetRts(FALSE);
1731   1734  
1732 // printf("command executed succesfully, press any key to close the COM port and exit"); 1735 // printf("command executed succesfully, press any key to close the COM port and exit");
1733 // printf("\n RTS set high. Press any key to continue ... 28"); 1736 // printf("\n RTS set high. Press any key to continue ... 28");
1734 // getchar(); 1737 // getchar();
1735 } 1738 }
1736   1739  
1737 printf("\nFlashing done. Enjoy ... \n"); 1740 printf("\nFlashing done. Enjoy ... \n");
1738   1741  
1739 if(COM_is_Open()) 1742 if(COM_is_Open())
1740 COM_Close(); 1743 COM_Close();
1741   1744  
1742 return 0; 1745 return 0;
1743 } 1746 }
1744   1747  
1745   1748  
1746 /******************* (C) COPYRIGHT 2010 STMicroelectronics *****END OF FILE******/ 1749 /******************* (C) COPYRIGHT 2010 STMicroelectronics *****END OF FILE******/