1 // jtstflash.cpp Andrea Michelotti amichelotti@atmel.com
2 // simple flash software. Compiled with MS Visual Studio 6
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are
9 * Redistributions of source code must retain the above copyright
10 notice, this list of conditions and the following disclaimer.
12 * Redistributions in binary form must reproduce the above
13 copyright notice, this list of conditions and the following
14 disclaimer in the documentation and/or other materials provided
15 with the distribution.
17 * Neither the name of [original copyright holder] nor the names of
18 its contributors may be used to endorse or promote products
19 derived from this software without specific prior written
22 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 #include <sys/types.h>
46 #define PRG_FLASH 0x4392014
49 #define BUFFER_SIZE 1024
51 #define FLASH_SIZE 1024*1024
52 #define PROTOK 0xc1a0c1a0
65 ReadFile(hComm,&data,4,&len,NULL);
67 printf("## (check_ack) protocol error byte read %d, expected 4\n",len);
71 // printf("## check ack :0x%x\n",data);
77 unsigned ask(unsigned cmd, unsigned address,unsigned len){
84 // printf("%% sending command\n");
86 WriteFile(hComm,&data.cmd,sizeof(data),&rlen,NULL);
89 /* if((res=check_ack(fd)) ==0){
90 // printf("* command OK\n");
93 printf("## ACK check sending command failed 0x%x\n",res);
99 void initCom(char* com){
103 GENERIC_READ|GENERIC_WRITE,
111 if(hComm==INVALID_HANDLE_VALUE){
112 printf("## cannot open serial port %s\n",com);
116 if(GetCommState(hComm,&dcb)==0){
117 printf("## cannot get serial status\n");
121 dcb.BaudRate= CBR_115200;
123 dcb.Parity = NOPARITY;
124 dcb.StopBits = ONESTOPBIT;
125 dcb.fDtrControl= DTR_CONTROL_DISABLE;
126 dcb.fRtsControl = RTS_CONTROL_DISABLE;
127 dcb.DCBlength = sizeof(dcb);
128 if(SetCommState(hComm, &dcb)==0){
129 printf("## cannot set serial status\n");
137 int main(int argc, char* argv[])
145 unsigned address=0x500000,len=0,tlen;
148 char buffer[BUFFER_SIZE];
149 unsigned ok=0xc1a0c1a0;
152 if(!strcmp(argv[i],"-d")){
158 if(!strcmp(argv[i],"-a")){
163 /* if(!strcmp(argv[i],"-reset")){
164 operation = CMD_RESET;
168 if(!strcmp(argv[i],"-h")){
169 printf("usage is %s <bin> [-a <flash address>]\n",argv[0]);
173 // printf("name -> %s",s_fname);
177 mybin=fopen(s_fname,"rb");
179 printf("## you must specify a valid filename\n");
183 printf("## you must specify a valid filename\n");
187 address=strtoul(s_add,0,0);
193 int fdf=fseek(mybin,0,SEEK_END );
195 fseek(mybin,0,SEEK_SET );
196 len = fdf- ftell(mybin);
198 printf("* binary len %d bytes flash add 0x%x..\n",len,address);
199 if(ask(PRG_FLASH,address,len)!=0) {
205 if((res=check_ack())!=0){
206 printf("## flash id check failed 0x%x\n",res);
210 printf("* flash id check ok\n");
212 WriteFile(hComm,&ok,4,&lenw,NULL);
213 if((res=check_ack())!=0){
214 printf("## flash id check failed 0x%x\n",res);
218 printf("* erasing space address 0x%x... please wait\n",address);
220 if((res=check_ack())!=0){
221 printf("## verify erase failed 0x%x\n",res);
225 printf("* flash erase check ok\n");
226 printf("* start programming %d bytes.\n",len);
230 unsigned long ll=(len<BUFFER_SIZE)?len:BUFFER_SIZE;
232 fread(buffer,1,ll,mybin);
234 //write(fd,buffer,ll);
235 WriteFile(hComm,buffer,ll,&lenw,NULL);
237 printf("## error writing byte write %d != byte to write %d\n",lenw,ll);
240 printf("* downloading %.3f (rem:%d bytes)\r",(tlen-len)*100.0/tlen,tot);
241 if((res=check_ack())!=0){
242 printf("## programming failed base add 0x%x\n",res);
250 printf("\n* 100%% end programming\n");