2 // jtstflash.c, Andrea Michelotti amichelotti@atmel.com
3 // simple flash software.
5 // gcc -DLINUX jtstflash.c -o jtstflash (linux)
6 // gcc jtstflash.c -o jtstflash (cygwin)
10 Redistribution and use in source and binary forms, with or without
11 modification, are permitted provided that the following conditions are
14 * Redistributions of source code must retain the above copyright
15 notice, this list of conditions and the following disclaimer.
17 * Redistributions in binary form must reproduce the above
18 copyright notice, this list of conditions and the following
19 disclaimer in the documentation and/or other materials provided
20 with the distribution.
22 * Neither the name of [original copyright holder] nor the names of
23 its contributors may be used to endorse or promote products
24 derived from this software without specific prior written
27 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
30 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
31 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
32 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
33 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
34 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
35 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
36 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
37 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
44 #include <sys/types.h>
48 #define PRG_FLASH 0x4392014
51 #define BUFFER_SIZE 1024
53 #define FLASH_SIZE 1024*1024
54 #define PROTOK 0xc1a0c1a0
80 void check_error(int a){
84 printf("## verify error\n");
87 printf("## flash timeout error\n");
90 printf("## product id mismatch\n");
93 printf("## device id mismatch\n");
96 printf("## flash write protected\n");
99 printf("## PROTOCOL ERROR check serial connection 0x%x (%d)\n",a,a);
105 unsigned num2baud(unsigned baud_rate){
111 printf("%% baud rate %d not supported\n",baud_rate);
168 unsigned check_ack(int fd){
173 // printf("## check ack :0x%x\n",data);
179 unsigned ask(int fd,unsigned cmd, unsigned address,unsigned len){
185 // printf("%% sending command\n");
187 write(fd,&data.cmd,sizeof(data));
190 /* if((res=check_ack(fd)) ==0){
191 // printf("* command OK\n");
194 printf("## ACK check sending command failed 0x%x\n",res);
203 main(int argc,char**argv){
205 struct termios uparam,uparam_old;
209 unsigned baud_rate=115200;
211 unsigned STOPBITS = CSTOPB;
213 unsigned DATABITS = CS8;
214 unsigned PARITYON = 0;
217 char* s_dev="/dev/ttyS1";
219 #warning "USING WIN32 SETTINGS"
220 char* s_dev="/dev/com2";
225 unsigned address=0x500000,len=0,tlen;
228 char buffer[BUFFER_SIZE];
229 unsigned ok=0xc1a0c1a0;
231 if(!strcmp(argv[i],"-d")){
237 if(!strcmp(argv[i],"-a")){
242 /* if(!strcmp(argv[i],"-reset")){
243 operation = CMD_RESET;
247 if(!strcmp(argv[i],"-h")){
248 printf("usage is %s <bin> [-a <flash address>]\n",argv[0]);
252 // printf("name -> %s",s_fname);
256 mybin=fopen(s_fname,"rb");
258 printf("## you must specify a valid filename\n");
262 printf("## you must specify a valid filename\n");
266 address=strtoul(s_add,0,0);
268 BAUD=num2baud(baud_rate);
269 fd=open(s_dev,O_RDWR |O_NOCTTY| O_NDELAY);
271 printf("## cannot open serial %s\n",s_dev);
274 if(tcgetattr(fd,&uparam)<0){
275 perror("## cannot get serial paramenters \n");
280 //bzero(&uparam, sizeof(uparam));
282 uparam.c_cflag = BAUD | CS8 |CLOCAL|CREAD;
284 uparam.c_iflag = 0; //raw device
285 uparam.c_oflag = 0; // raw device
286 uparam.c_lflag = 0;//ICANON;
287 //uparam.c_cc[VMIN]=1;
288 //uparam.c_cc[VEOF]=4;/*ctrld*/
289 //uparam.c_cc[VTIME]=0;
290 fcntl(fd, F_SETFL, 0);
292 if(tcflush(fd, TCIFLUSH)<0){
293 printf("## cannot flush serial\n");
296 if(tcsetattr(fd,TCSANOW,&uparam)<0){
297 printf("## cannot set serial paramenters\n");
300 printf("* using serial %s, %d, no parity, 8b, 1stop\n",s_dev,baud_rate);
303 if(tcflush(fd, TCIFLUSH)<0){
304 printf("## cannot flush serial paramenters\n");
308 printf("* press return\n");
313 lstat(s_fname,&info);
316 printf("* binary len %d bytes..\n",len);
317 if(ask(fd,PRG_FLASH,address,len)!=0) {
323 if((res=check_ack(fd))!=0){
324 printf("## flash id check failed 0x%x\n",res);
328 printf("* flash id check ok\n");
330 if((res=check_ack(fd))!=0){
331 printf("## flash id check failed 0x%x\n",res);
335 printf("* erasing space address 0x%x... please wait\n",address);
337 if((res=check_ack(fd))!=0){
338 printf("## verify erase failed 0x%x\n",res);
342 printf("* flash erase check ok\n");
343 printf("* start programming %d bytes.\n",len);
346 int ll=(len<BUFFER_SIZE)?len:BUFFER_SIZE;
348 fread(buffer,1,ll,mybin);
351 printf("* downloading %.3f (rem:%d bytes)\r",(tlen-len)*100.0/tlen,tot);
352 if((res=check_ack(fd))!=0){
353 printf("## programming failed base add 0x%x\n",res);
361 printf("\n* 100%% end programming\n");