forked from GraniteDevices/SimpleMotionV2
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsmtest.c
361 lines (293 loc) · 10.7 KB
/
smtest.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
#include "simplemotion.h"
#include <stdio.h>
#include <stdlib.h>
#define ADDR 255
#include "vsd_cmd.h"
//gcc -o testi *.c
//#include "D:\gd\vsdr\vsdr-stm32\src\simplemotion_defs.h"
#include "/home/tero/shr/svn/granited/firmware_design/eclipseworkspace/vsdr-stm32/src/simplemotion_defs.h"
smbus h;
smint32 ret;
SM_STATUS stat=0;
void test()
{
//printf( "stat %d\n", smRawCommand( h, ADDR, 2, 0, &ret ) );
//printf("ret=%08x\n",ret);
//printf( "stat %d\n", smRawCommand( h, 2, 2, 0, &ret ) );
//printf("ret=%08x\n",ret);
//stat|=smAppendSetParamCommandToQueue(h,SMP_DIGITAL_OUT_VALUE_1,151);
// stat|=smAppendSetParamCommandToQueue(h,SMP_INPUT_MULTIPLIER,11);
stat|=smExecuteCommandQueue( h, ADDR );
stat|=smGetQueuedSetParamReturnValue(h,&ret);
printf("setstat=%d val=%d\n",stat,ret);
stat=0;
stat|=smAppendGetParamCommandToQueue(h,SMP_BUS_SPEED);
stat|=smAppendGetParamCommandToQueue(h,SMP_SM_VERSION_COMPAT);
stat|=smAppendGetParamCommandToQueue(h,SMP_DIGITAL_OUT_VALUE_1);
stat|=smExecuteCommandQueue( h, ADDR );
stat|=smGetQueuedGetParamReturnValue(h,&ret);
printf("stat=%d val=%d\n",stat,ret);
stat|=smGetQueuedGetParamReturnValue(h,&ret);
printf("stat=%d val=%d\n",stat,ret);
stat|=smGetQueuedGetParamReturnValue(h,&ret);
printf("stat=%d val=%d\n",stat,ret);
/* smAppendSMCommandToQueue( h, SMPCMD_SETPARAMADDR, SMP_RETURN_PARAM_LEN );
smAppendSMCommandToQueue( h, SMPCMD_24B, SMPRET_32B );
smAppendSMCommandToQueue( h, SMPCMD_SETPARAMADDR, SMP_RETURN_PARAM_ADDR );
smAppendSMCommandToQueue( h, SMPCMD_32B, SMP_BUS_SPEED );
smAppendSMCommandToQueue( h, SMPCMD_32B,SMP_INPUT_MULTIPLIER );
smAppendSMCommandToQueue( h, SMPCMD_SETPARAMADDR, SMP_INPUT_MULTIPLIER );
smAppendSMCommandToQueue( h, SMPCMD_24B, 100 );
// SMP_INPUT_MULTIPLIER
smExecuteCommandQueue( h, ADDR );
printf("",smGetQueuedSMCommandReturnValue( h ));
printf("",smGetQueuedSMCommandReturnValue( h ));
printf("",smGetQueuedSMCommandReturnValue( h ));
printf("",smGetQueuedSMCommandReturnValue( h ));
printf("",smGetQueuedSMCommandReturnValue( h ));
printf("",smGetQueuedSMCommandReturnValue( h ));
printf("",smGetQueuedSMCommandReturnValue( h ));
// printf("%d\n",smGetQueuedSMCommandReturnValue( h ));*/
/* smAppendCommandToQueue( h, CMD_GET_PARAM, RUNTIME_FAULTBITS );
smAppendCommandToQueue( h, CMD_GET_PARAM, RUNTIME_STATUSBITS );
smAppendCommandToQueue( h, CMD_GET_PARAM, RUNTIME_FIRMWARE_VERSION );
smExecuteCommandQueue( h, ADDR );
printf("%d\n",smGetQueuedCommandReturnValue( h, 0 ));
printf("%d\n",smGetQueuedCommandReturnValue( h, 1 ));
printf("%d\n",smGetQueuedCommandReturnValue( h, 2 ));
*/
}
void tests();
/*BL functions started by writing a value to this param:
* 1:mass erase and reset write pos counter to 0
* 2:write block
* 3:verify flash (update BOOTLOADER_STAT_FLASH_VERIFIED_OK status)
* 4:start main app
*/
#define SMP_BOOTLOADER_FUNCTION 191
//upload 16bits of data to buffer. max buffer length 4096 bytes before it must be written with "write block" function
//total uploaded amount must be multiple of 4 bytes before issuing write function
#define SMP_BOOTLOADER_UPLOAD 192
//bootloaded status
#define SMP_BOOTLOADER_STAT 193
#define BOOTLOADER_STAT_FLASH_VERIFIED_OK BV(0)
#define SMP_FIRMWARE_VERSION 6010
#define SMP_BACKWARDS_COMP_VERSION 6012
#define SUPPORTED_BL_VERSION_MIN 10000
#define SUPPORTED_BL_VERSION_MAX 10010
//load app rom from file. args:
//pass pointer to "data". this func will allocate the memory
//return number of 16bit words or -1 if load failed
int loadBLfile( const char *filename, smuint16 **data )
{
int bytes;
int i;
FILE *f;
smuint16 *buf;
//open file
f=fopen(filename,"rb");
if(f==NULL)
return -1;
//find file size
fseek(f, 0L, SEEK_END);
bytes = ftell(f);
fseek(f, 0L, SEEK_SET);
printf("size=%d\n",bytes);
//allocate
buf=(smuint16*)malloc((bytes+1)/2);//round upwards
if(buf==NULL)
return -1;
//load
for(i=0;i<bytes;i++)
{
int get=fgetc(f);
if(get==EOF)
return -1;
//treat data as byte array
((smuint8*)buf)[i]=get;
}
printf(".\n");
//fclose(f);
printf(".\n");
*data=buf;
return ((bytes+1)/2);
}
#define BL_CHUNK_LEN 32
int uploadGCfile(const char *fname)
{
smint32 blversion,blcompversion;
stat=0;
smuint16 *bin;
int i,c;
int size=loadBLfile(fname,&bin);
if(size<0)
{
printf("load failed\n");
return;
}
printf("loaded %d words\n",size);
//get possible node detected faults in communications
stat|=smAppendSMCommandToQueue( h, SMPCMD_SETPARAMADDR, SMP_RETURN_PARAM_ADDR );
stat|=smAppendSMCommandToQueue( h, SMPCMD_24B, SMP_CUMULATIVE_STATUS );
//set return param type and command type
stat|=smAppendSMCommandToQueue( h, SMPCMD_SETPARAMADDR, SMP_RETURN_PARAM_LEN );
stat|=smAppendSMCommandToQueue( h, SMPCMD_32B, SMPRET_CMD_STATUS );
stat|=smAppendSMCommandToQueue( h, SMPCMD_SETPARAMADDR, SMP_BOOTLOADER_UPLOAD );
stat|=smExecuteCommandQueue( h, ADDR );
stat|=smGetQueuedSMCommandReturnValue( h,&ret );printf("stat=%d val=%d\n",stat,ret);
stat|=smGetQueuedSMCommandReturnValue( h,&ret );printf("stat=%d val=%d\n",stat,ret);
int smnodestats=ret;
stat|=smGetQueuedSMCommandReturnValue( h,&ret );printf("stat=%d val=%d\n",stat,ret);
stat|=smGetQueuedSMCommandReturnValue( h,&ret );printf("stat=%d val=%d\n",stat,ret);
stat|=smGetQueuedSMCommandReturnValue( h,&ret );printf("stat=%d val=%d\n",stat,ret);
if(stat!=SM_OK || smnodestats!=SMP_CMD_STATUS_ACK)
{
printf("SM errors occurred:%d\n",smnodestats);
exit(5);
}
//upload data in 32=BL_CHUNK_LEN word chunks
for(i=0;i<size;)
{
printf("pos %d\n",i);
//getchar();
stat|=smAppendSMCommandToQueue( h, SMPCMD_SETPARAMADDR, SMP_BOOTLOADER_UPLOAD );
for(c=0;c<BL_CHUNK_LEN;c++)
{
smuint16 upword;
//pad end of file with constant
if(i>=size)
upword=0xeeee;
else
upword=bin[i];
printf("i=%d\n",i);
stat|=smAppendSMCommandToQueue( h, SMPCMD_24B, upword );
i++;
}
// stat|=smAppendSMCommandToQueue( h, SMPCMD_SETPARAMADDR, SMP_BOOTLOADER_FUNCTION );
// stat|=smAppendSMCommandToQueue( h, SMPCMD_24B, 2);//do write
stat|=smExecuteCommandQueue( h, ADDR );
for(c=0;c<BL_CHUNK_LEN+1;c++)
{
stat|=smGetQueuedSMCommandReturnValue( h,&ret );
if(stat!=SM_OK)
{
printf("error stat=%d\n",stat);
exit(6);
}
}
}
printf("done. enter to flash\n");
getchar();
stat|=smAppendSMCommandToQueue( h, SMPCMD_SETPARAMADDR, SMP_BOOTLOADER_FUNCTION );
stat|=smAppendSMCommandToQueue( h, SMPCMD_24B, 5);//flash gc
stat|=smExecuteCommandQueue( h, ADDR );
}
void blcommtest(const char *fname)
{
smint32 blversion,blcompversion;
stat=0;
smuint16 *bin;
int i,c;
int size=loadBLfile(fname,&bin);
if(size<0)
{
printf("load failed\n");
return;
}
printf("loaded %d words\n",size);
//read bl variables
stat|=smAppendSMCommandToQueue( h, SMPCMD_SETPARAMADDR, SMP_RETURN_PARAM_LEN );
stat|=smAppendSMCommandToQueue( h, SMPCMD_32B, SMPRET_32B );
stat|=smAppendSMCommandToQueue( h, SMPCMD_SETPARAMADDR, SMP_RETURN_PARAM_ADDR );
stat|=smAppendSMCommandToQueue( h, SMPCMD_32B, SMP_FIRMWARE_VERSION );
stat|=smAppendSMCommandToQueue( h, SMPCMD_32B, SMP_BACKWARDS_COMP_VERSION );
stat|=smExecuteCommandQueue( h, ADDR );
stat|=smGetQueuedSMCommandReturnValue( h,&ret );printf("stat=%d val=%d\n",stat,ret);
stat|=smGetQueuedSMCommandReturnValue( h,&ret );printf("stat=%d val=%d\n",stat,ret);
stat|=smGetQueuedSMCommandReturnValue( h,&ret );printf("stat=%d val=%d\n",stat,ret);
stat|=smGetQueuedSMCommandReturnValue( h,&ret );printf("stat=%d val=%d\n",stat,ret);
blversion=ret;
stat|=smGetQueuedSMCommandReturnValue( h,&ret );printf("stat=%d val=%d\n",stat,ret);
blcompversion=ret;
//if version ok
//if(blversion
//mass erase flash
stat|=smAppendSMCommandToQueue( h, SMPCMD_SETPARAMADDR, SMP_BOOTLOADER_FUNCTION );
stat|=smAppendSMCommandToQueue( h, SMPCMD_32B, 1 );//do mass erase
stat|=smExecuteCommandQueue( h, ADDR );
stat|=smGetQueuedSMCommandReturnValue( h,&ret );printf("stat=%d val=%d\n",stat,ret);
stat|=smGetQueuedSMCommandReturnValue( h,&ret );printf("stat=%d val=%d\n",stat,ret);
usleep(1000000);
//flash
//get possible node detected faults in communications
stat|=smAppendSMCommandToQueue( h, SMPCMD_SETPARAMADDR, SMP_RETURN_PARAM_ADDR );
stat|=smAppendSMCommandToQueue( h, SMPCMD_24B, SMP_CUMULATIVE_STATUS );
//set return param type and command type
stat|=smAppendSMCommandToQueue( h, SMPCMD_SETPARAMADDR, SMP_RETURN_PARAM_LEN );
stat|=smAppendSMCommandToQueue( h, SMPCMD_32B, SMPRET_CMD_STATUS );
stat|=smAppendSMCommandToQueue( h, SMPCMD_SETPARAMADDR, SMP_BOOTLOADER_UPLOAD );
stat|=smExecuteCommandQueue( h, ADDR );
stat|=smGetQueuedSMCommandReturnValue( h,&ret );printf("stat=%d val=%d\n",stat,ret);
stat|=smGetQueuedSMCommandReturnValue( h,&ret );printf("stat=%d val=%d\n",stat,ret);
int smnodestats=ret;
stat|=smGetQueuedSMCommandReturnValue( h,&ret );printf("stat=%d val=%d\n",stat,ret);
stat|=smGetQueuedSMCommandReturnValue( h,&ret );printf("stat=%d val=%d\n",stat,ret);
stat|=smGetQueuedSMCommandReturnValue( h,&ret );printf("stat=%d val=%d\n",stat,ret);
if(stat!=SM_OK || smnodestats!=SMP_CMD_STATUS_ACK)
{
printf("SM errors occurred:%d\n",smnodestats);
exit(5);
}
//upload data in 32=BL_CHUNK_LEN word chunks
for(i=0;i<size;)
{
printf("pos %d\n",i);
//getchar();
stat|=smAppendSMCommandToQueue( h, SMPCMD_SETPARAMADDR, SMP_BOOTLOADER_UPLOAD );
for(c=0;c<BL_CHUNK_LEN;c++)
{
smuint16 upword;
//pad end of file with constant
if(i>=size)
upword=0xeeee;
else
upword=bin[i];
printf("i=%d\n",i);
stat|=smAppendSMCommandToQueue( h, SMPCMD_24B, upword );
i++;
}
stat|=smAppendSMCommandToQueue( h, SMPCMD_SETPARAMADDR, SMP_BOOTLOADER_FUNCTION );
stat|=smAppendSMCommandToQueue( h, SMPCMD_24B, 2);//do write
stat|=smExecuteCommandQueue( h, ADDR );
for(c=0;c<BL_CHUNK_LEN+3;c++)
{
stat|=smGetQueuedSMCommandReturnValue( h,&ret );
if(stat!=SM_OK)
{
printf("error stat=%d\n",stat);
exit(6);
}
}
}
printf("done. enter to run\n");
getchar();
stat|=smAppendSMCommandToQueue( h, SMPCMD_SETPARAMADDR, SMP_BOOTLOADER_FUNCTION );
stat|=smAppendSMCommandToQueue( h, SMPCMD_24B, 4);//launch
stat|=smExecuteCommandQueue( h, ADDR );
}
int main(int args, const char *argv[])
{
if(args<2)
{
printf("args: rom bin file name\n");
return(0);
}
smSetDebugOutput(High,stderr);
smSetTimeout(1500);
h=smOpenBus("COM17"); //17=USB1 16=0
printf("handle=%d\n",h);
if(h==-1) return 3;
uploadGCfile(argv[1]);
//blcommtest(argv[1]);
smCloseBus(h);
}