OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-3.0/] [packages/] [hal/] [arm/] [at91/] [jtst/] [current/] [support/] [jtstflash.cpp] - Blame information for rev 856

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 786 skrzyp
// jtstflash.cpp Andrea Michelotti amichelotti@atmel.com
2
// simple flash software. Compiled with MS Visual Studio 6
3
 
4
/*
5
Redistribution and use in source and binary forms, with or without
6
modification, are permitted provided that the following conditions are
7
met:
8
 
9
    * Redistributions of source code must retain the above copyright
10
      notice, this list of conditions and the following disclaimer.
11
 
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.
16
 
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
20
      permission.
21
 
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.
33
 
34
*/
35
#include "stdafx.h"
36
#include "windows.h"
37
#include <stdlib.h>
38
 
39
#include <sys/types.h>
40
#include <sys/stat.h>
41
#include <fcntl.h>
42
#include <io.h>
43
 
44
static HANDLE hComm;
45
 
46
#define PRG_FLASH 0x4392014
47
 
48
//
49
#define BUFFER_SIZE 1024
50
 
51
#define FLASH_SIZE 1024*1024
52
#define PROTOK 0xc1a0c1a0
53
typedef struct {
54
  unsigned cmd;
55
  unsigned add;
56
  unsigned len;
57
} command_t;
58
 
59
 
60
unsigned check_ack(){
61
  unsigned data;
62
  unsigned long len;
63
  //usleep(80000);
64
  //read(fd,&data,4);
65
  ReadFile(hComm,&data,4,&len,NULL);
66
  if(len!=4){
67
          printf("## (check_ack) protocol error byte read %d, expected 4\n",len);
68
          exit(1);
69
  }
70
  if((data==PROTOK)){
71
    //    printf("## check ack :0x%x\n",data);
72
    return 0;
73
  }
74
  return data;
75
}
76
 
77
unsigned ask(unsigned cmd, unsigned address,unsigned len){
78
  unsigned long rlen;
79
  command_t data;
80
 
81
  data.len = len;
82
  data.cmd = cmd;
83
  data.add = address;
84
  //   printf("%% sending command\n");
85
  //usleep(80000);
86
  WriteFile(hComm,&data.cmd,sizeof(data),&rlen,NULL);
87
 
88
  //  sleep(1);
89
  /*  if((res=check_ack(fd)) ==0){
90
    //    printf("* command OK\n");
91
    return 0;
92
  } else {
93
    printf("## ACK check sending command failed 0x%x\n",res);
94
    }*/
95
  //  sleep(1);
96
  return 0;
97
}
98
 
99
void initCom(char* com){
100
        DCB dcb;
101
        hComm = CreateFile (
102
                com,
103
        GENERIC_READ|GENERIC_WRITE,
104
        0,
105
        NULL,
106
        OPEN_EXISTING,
107
        0,
108
 
109
        );
110
 
111
        if(hComm==INVALID_HANDLE_VALUE){
112
                printf("##  cannot open serial port %s\n",com);
113
                exit(1);
114
        }
115
 
116
        if(GetCommState(hComm,&dcb)==0){
117
                printf("## cannot get serial status\n");
118
                exit(1);
119
        }
120
 
121
        dcb.BaudRate= CBR_115200;
122
        dcb.ByteSize = 8;
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");
130
                exit(1);
131
        }
132
 
133
}
134
 
135
 
136
 
137
int main(int argc, char* argv[])
138
{
139
 int res,i,tot=0;
140
  char* s_dev="COM2";
141
 
142
  char* s_fname=0;
143
  char* s_add=0;
144
  char* s_len=0;
145
  unsigned address=0x500000,len=0,tlen;
146
  FILE* mybin=0;
147
  struct stat info;
148
  char buffer[BUFFER_SIZE];
149
  unsigned ok=0xc1a0c1a0;
150
  unsigned long lenw;
151
  for(i=1;i<argc;i++){
152
    if(!strcmp(argv[i],"-d")){
153
      s_dev = argv[++i];
154
      continue;
155
    }
156
 
157
 
158
    if(!strcmp(argv[i],"-a")){
159
      s_add = argv[++i];
160
      continue;
161
    }
162
 
163
    /*    if(!strcmp(argv[i],"-reset")){
164
      operation = CMD_RESET;
165
      continue;
166
      }*/
167
 
168
    if(!strcmp(argv[i],"-h")){
169
      printf("usage is %s <bin> [-a <flash address>]\n",argv[0]);
170
      exit(0);
171
    }
172
    s_fname = argv[i];
173
    // printf("name  -> %s",s_fname);
174
  }
175
 
176
  if(s_fname){
177
    mybin=fopen(s_fname,"rb");
178
    if(mybin==0){
179
      printf("## you must specify a valid filename\n");
180
      exit(1);
181
    }
182
  } else {
183
     printf("## you must specify a valid filename\n");
184
     exit(1);
185
  }
186
  if(s_add){
187
    address=strtoul(s_add,0,0);
188
  }
189
 
190
        initCom(s_dev);
191
 
192
        {
193
                int fdf=fseek(mybin,0,SEEK_END );
194
                fdf = ftell(mybin);
195
                fseek(mybin,0,SEEK_SET );
196
                len = fdf- ftell(mybin);
197
        }
198
  printf("* binary len %d bytes flash add 0x%x..\n",len,address);
199
  if(ask(PRG_FLASH,address,len)!=0) {
200
    fclose(mybin);
201
    CloseHandle(hComm);
202
    return 0;
203
  }
204
  // check flsh id
205
  if((res=check_ack())!=0){
206
    printf("## flash id check failed 0x%x\n",res);
207
    CloseHandle(hComm);
208
    exit(1);
209
  }
210
  printf("* flash id check ok\n");
211
 
212
  WriteFile(hComm,&ok,4,&lenw,NULL);
213
  if((res=check_ack())!=0){
214
      printf("## flash id check failed 0x%x\n",res);
215
       CloseHandle(hComm);
216
      exit(1);
217
  }
218
  printf("* erasing space address 0x%x... please wait\n",address);
219
  // verify erase
220
  if((res=check_ack())!=0){
221
    printf("## verify erase failed 0x%x\n",res);
222
    CloseHandle(hComm);
223
    exit(1);
224
  }
225
  printf("* flash erase check ok\n");
226
  printf("* start programming %d bytes.\n",len);
227
  tlen=len;
228
  while(len>0){
229
 
230
    unsigned long ll=(len<BUFFER_SIZE)?len:BUFFER_SIZE;
231
    tot+=ll;
232
    fread(buffer,1,ll,mybin);
233
    //usleep(8000);
234
    //write(fd,buffer,ll);
235
        WriteFile(hComm,buffer,ll,&lenw,NULL);
236
        if(ll!=lenw){
237
                printf("## error writing byte write %d != byte to write %d\n",lenw,ll);
238
                exit(1);
239
        }
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);
243
      //      close(fd);
244
      //      exit(1);
245
    }
246
 
247
    len-=ll;
248
 
249
  }
250
        printf("\n* 100%% end programming\n");
251
        return 0;
252
}
253
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.