[342] | 1 | /* Copyright (c) 2006 Rice University */ |
---|
| 2 | /* All Rights Reserved */ |
---|
| 3 | /* This code is covered by the Rice-WARP license */ |
---|
| 4 | /* See http://warp.rice.edu/license/ for details */ |
---|
| 5 | |
---|
| 6 | |
---|
[79] | 7 | #include "warpsysace.h" |
---|
| 8 | #include <stdio.h> |
---|
| 9 | #include <stdlib.h> |
---|
| 10 | #include "xparameters.h" |
---|
| 11 | #include "xsysace_l.h" |
---|
| 12 | #include "xsysace.h" |
---|
| 13 | #include <string.h> |
---|
| 14 | #include "warplib.h" |
---|
[341] | 15 | #include "sysace_stdio.h" |
---|
[79] | 16 | |
---|
| 17 | |
---|
| 18 | |
---|
| 19 | |
---|
| 20 | /**************************************************************************/ |
---|
| 21 | |
---|
| 22 | |
---|
| 23 | #define LSIZE 2048 |
---|
| 24 | |
---|
| 25 | #ifdef XPAR_XSYSACE_NUM_INSTANCES |
---|
| 26 | XSysAce SA; |
---|
| 27 | #endif |
---|
| 28 | |
---|
| 29 | /**************************************************************************/ |
---|
| 30 | |
---|
| 31 | // This is a system for appending log files. The size of each log can be set by modifying LSIZE |
---|
| 32 | // It creates log files as well as has a master log name file to keep track of the current file |
---|
| 33 | // being appended |
---|
| 34 | |
---|
| 35 | #ifdef XPAR_XSYSACE_NUM_INSTANCES |
---|
| 36 | int WarpSysace_appendLog(char *buffer) { |
---|
| 37 | |
---|
| 38 | if (strlen(buffer) > LSIZE) { |
---|
| 39 | return -1; |
---|
| 40 | } |
---|
| 41 | else { |
---|
| 42 | |
---|
| 43 | // Define some constants and determine if the current-name-log-file exists |
---|
| 44 | |
---|
| 45 | char fname[20]; |
---|
| 46 | |
---|
| 47 | char filedata[LSIZE]=""; |
---|
| 48 | |
---|
| 49 | void *openfile = sysace_fopen("FNAMELOG.TXT", "r"); |
---|
| 50 | |
---|
| 51 | int filedatasize = sysace_fread(NULL, 1, 20, openfile); |
---|
| 52 | |
---|
| 53 | // print("Size established\r\n"); |
---|
| 54 | |
---|
| 55 | // putnum(filedatasize); |
---|
| 56 | |
---|
| 57 | // print("\r\n"); |
---|
| 58 | |
---|
| 59 | sysace_fclose(openfile); |
---|
| 60 | |
---|
| 61 | if (filedatasize <= 0) { // create the logname file if it doesn't exist |
---|
| 62 | |
---|
| 63 | void *openfile2 = sysace_fopen("FNAMELOG.TXT", "w"); |
---|
| 64 | |
---|
| 65 | strcat(fname, "LOG0000.TXT"); // if it doesn't exist we assume we're starting at the first log file |
---|
| 66 | |
---|
| 67 | sysace_fwrite(fname, 1, strlen(fname), openfile2); |
---|
| 68 | |
---|
| 69 | // puts(fname); |
---|
| 70 | |
---|
| 71 | // sysace_fclose(openfile2); |
---|
| 72 | |
---|
| 73 | // print("Main log file created\r\n"); |
---|
| 74 | |
---|
| 75 | } |
---|
| 76 | else { // otherwise, open the namelog file since it exists, and get the current log file to be modified |
---|
| 77 | |
---|
| 78 | void *openfile3 = sysace_fopen("FNAMELOG.TXT", "r"); |
---|
| 79 | |
---|
| 80 | sysace_fread(fname, 1, 20, openfile3); |
---|
| 81 | |
---|
| 82 | sysace_fclose(openfile3); |
---|
| 83 | |
---|
| 84 | // print("Main log file read\r\n"); |
---|
| 85 | |
---|
| 86 | } |
---|
| 87 | |
---|
| 88 | // the next step is to read in the amount of data as well as our string size |
---|
| 89 | // to determine whether to append this file or create a new one |
---|
| 90 | |
---|
| 91 | // puts(fname); |
---|
| 92 | |
---|
| 93 | void *datafile = sysace_fopen(fname, "r"); |
---|
| 94 | |
---|
| 95 | // putnum(datafile); |
---|
| 96 | |
---|
| 97 | int amtdata = sysace_fread(filedata, 1, LSIZE, datafile); // get the amount of data in the current log file |
---|
| 98 | |
---|
| 99 | sysace_fclose(datafile); |
---|
| 100 | |
---|
| 101 | // print("Size of data in log file is:"); |
---|
| 102 | |
---|
| 103 | // putnum(amtdata); |
---|
| 104 | |
---|
| 105 | // print("\r\n"); |
---|
| 106 | |
---|
| 107 | if ((amtdata + strlen(buffer)) <= LSIZE) { // if there is enough space, we simply append the log file by |
---|
| 108 | // concatination and overwriting |
---|
| 109 | void *datafile2 = sysace_fopen(fname, "w"); |
---|
| 110 | |
---|
| 111 | // putnum(datafile2); |
---|
| 112 | |
---|
| 113 | strcat(filedata, buffer); |
---|
| 114 | |
---|
| 115 | //puts(filedata); |
---|
| 116 | |
---|
| 117 | sysace_fwrite(filedata, 1, strlen(filedata), datafile2); |
---|
| 118 | |
---|
| 119 | sysace_fclose(datafile2); |
---|
| 120 | |
---|
| 121 | // print("Data written\r\n"); |
---|
| 122 | |
---|
| 123 | } |
---|
| 124 | else { // otherwise we go through a series of nested if/else statements to determine the new file |
---|
| 125 | // name to be created. It's obvius that it can be compressed into a simple recursive call |
---|
| 126 | // but this should hopefully make it easier to understand. We use our int-to-char and char-to-int |
---|
| 127 | // functions to draw from the current log-file name to determine which digits to increment. |
---|
| 128 | int curr3=WarpLib_ctoi(fname[6]); |
---|
| 129 | if(curr3<9) { |
---|
| 130 | curr3=curr3+1; |
---|
| 131 | fname[6]=WarpLib_itoc(curr3); |
---|
| 132 | } |
---|
| 133 | else { |
---|
| 134 | curr3=0; |
---|
| 135 | fname[6]=WarpLib_itoc(curr3); |
---|
| 136 | int curr2=WarpLib_ctoi(fname[5]); |
---|
| 137 | if(curr2<9) { |
---|
| 138 | curr2=curr2+1; |
---|
| 139 | fname[5]=WarpLib_itoc(curr2); |
---|
| 140 | } |
---|
| 141 | else { |
---|
| 142 | curr2=0; |
---|
| 143 | fname[5]=WarpLib_itoc(curr2); |
---|
| 144 | int curr1=WarpLib_ctoi(fname[4]); |
---|
| 145 | if(curr1<9) { |
---|
| 146 | curr1=curr1+1; |
---|
| 147 | fname[4]=WarpLib_itoc(curr1); |
---|
| 148 | } |
---|
| 149 | else { |
---|
| 150 | curr1=0; |
---|
| 151 | fname[4]=WarpLib_itoc(curr1); |
---|
| 152 | int curr0=WarpLib_ctoi(fname[3]); |
---|
| 153 | if(curr0<9) { |
---|
| 154 | curr0=curr0+1; |
---|
| 155 | fname[3]=WarpLib_itoc(curr0); |
---|
| 156 | } |
---|
| 157 | else { |
---|
| 158 | print("\r\n\r\nError: Out of Range\r\n\r\n"); |
---|
| 159 | } |
---|
| 160 | } |
---|
| 161 | } |
---|
| 162 | } |
---|
| 163 | |
---|
| 164 | // puts(fname); |
---|
| 165 | |
---|
| 166 | // now that the new file name has been determined, we create it and write the new data into it |
---|
| 167 | |
---|
| 168 | void *newdatafile = sysace_fopen(fname, "w"); |
---|
| 169 | // puts(buffer); |
---|
| 170 | sysace_fwrite(buffer, 1, strlen(buffer), newdatafile); |
---|
| 171 | sysace_fclose(newdatafile); |
---|
| 172 | |
---|
| 173 | // we now update the file-name-log with the new file-name |
---|
| 174 | |
---|
| 175 | void *mainfile = sysace_fopen("FNAMELOG.TXT", "w"); |
---|
| 176 | sysace_fwrite(fname, 1, strlen(fname), mainfile); |
---|
| 177 | sysace_fclose(mainfile); |
---|
| 178 | |
---|
| 179 | // print("Data written to new file"); |
---|
| 180 | |
---|
| 181 | |
---|
| 182 | } |
---|
| 183 | |
---|
| 184 | return 0; |
---|
| 185 | } |
---|
| 186 | |
---|
| 187 | |
---|
| 188 | } |
---|
| 189 | #endif |
---|
| 190 | |
---|
| 191 | |
---|
| 192 | #ifdef XPAR_XSYSACE_NUM_INSTANCES |
---|
[341] | 193 | void WarpSysace_reconfigure(unsigned int baseaddr, unsigned int newaddr) |
---|
| 194 | { |
---|
[79] | 195 | |
---|
[341] | 196 | // the last value in the XSysAce_mSetCfgAddr function call denotes the configuration |
---|
| 197 | // program to load as set in iMPACT. In the iMPACT setup tutorial, we set the bitstream |
---|
| 198 | // program to reconfigure the FPGA as configuration address 0, and this system as configuration |
---|
| 199 | // address 0 so that we may switch back. The iMPACT system allows for up to 8 reconfiguration |
---|
| 200 | // bitstreams (represented by 0-7). |
---|
| 201 | |
---|
| 202 | SA.BaseAddress = baseaddr; |
---|
| 203 | |
---|
[79] | 204 | XSysAce_mSetControlReg(baseaddr, 0x0); |
---|
| 205 | XSysAce_mSetCfgAddr(baseaddr, newaddr); |
---|
| 206 | XSysAce_mOrControlReg(baseaddr, 0x0008); |
---|
| 207 | XSysAce_ResetCfg(&SA); |
---|
[341] | 208 | //putnum(XSysAce_mGetControlReg(baseaddr)); // for debug |
---|
[79] | 209 | XSysAce_mOrControlReg(baseaddr, XSA_CR_CFGSTART_MASK); |
---|
[341] | 210 | XSysAce_Unlock(&SA); |
---|
[79] | 211 | |
---|
| 212 | } |
---|
| 213 | #endif |
---|
| 214 | |
---|
[341] | 215 | #ifdef XPAR_XSYSACE_NUM_INSTANCES |
---|
| 216 | void WarpSysace_GetLock(unsigned int baseaddr) |
---|
| 217 | { |
---|
[79] | 218 | |
---|
[341] | 219 | SA.BaseAddress = baseaddr; |
---|
| 220 | int a; |
---|
| 221 | XStatus stat; |
---|
| 222 | |
---|
| 223 | // Initialize the SysACE |
---|
| 224 | stat = XSysAce_Initialize(&SA, 0); |
---|
| 225 | |
---|
| 226 | do { |
---|
| 227 | // Get a lock on the SysACE. Need it for reading and writing to flash. |
---|
| 228 | stat = XSysAce_Lock(&SA, XFALSE); |
---|
| 229 | |
---|
| 230 | for(a = 0; a < 1000; a++) { |
---|
| 231 | } |
---|
| 232 | |
---|
| 233 | } while(stat == XST_DEVICE_BUSY); |
---|
| 234 | |
---|
| 235 | } |
---|
| 236 | #endif |
---|
| 237 | |
---|
| 238 | |
---|
[79] | 239 | #ifdef XPAR_XSYSACE_NUM_INSTANCES |
---|
| 240 | short WarpSysace_IChanDcOffset(unsigned int radio) { |
---|
| 241 | |
---|
| 242 | sysace_chdir("a:\\CAL"); |
---|
| 243 | |
---|
| 244 | short ival; |
---|
| 245 | char name[20]=""; |
---|
| 246 | |
---|
| 247 | switch(radio) { |
---|
| 248 | |
---|
| 249 | case(0x0001) : { |
---|
| 250 | strcat(name, "RAD1CAL.TXT"); |
---|
| 251 | break; |
---|
| 252 | } |
---|
| 253 | case(0x0002) : { |
---|
| 254 | strcat(name, "RAD2CAL.TXT"); |
---|
| 255 | break; |
---|
| 256 | } |
---|
| 257 | case(0x0004) : { |
---|
| 258 | strcat(name, "RAD3CAL.TXT"); |
---|
| 259 | break; |
---|
| 260 | } |
---|
| 261 | case(0x0008) : { |
---|
| 262 | strcat(name, "RAD4CAL.TXT"); |
---|
| 263 | break; |
---|
| 264 | } |
---|
| 265 | default : { |
---|
| 266 | return -2000; |
---|
| 267 | } |
---|
| 268 | } |
---|
| 269 | |
---|
| 270 | void *calfile = sysace_fopen(name, "r"); |
---|
| 271 | |
---|
| 272 | sysace_fread(&ival, 1, 2, calfile); |
---|
| 273 | |
---|
| 274 | sysace_fclose(calfile); |
---|
| 275 | |
---|
| 276 | return ival; |
---|
| 277 | |
---|
| 278 | } |
---|
| 279 | #endif |
---|
| 280 | |
---|
| 281 | |
---|
| 282 | #ifdef XPAR_XSYSACE_NUM_INSTANCES |
---|
| 283 | short WarpSysace_QChanDcOffset(unsigned int radio) { |
---|
| 284 | |
---|
| 285 | sysace_chdir("a:\\CAL"); |
---|
| 286 | |
---|
| 287 | short qval; |
---|
| 288 | char name[20]=""; |
---|
| 289 | |
---|
| 290 | switch(radio) { |
---|
| 291 | |
---|
| 292 | case(0x0001) : { |
---|
| 293 | strcat(name, "RAD1CAL.TXT"); |
---|
| 294 | break; |
---|
| 295 | } |
---|
| 296 | case(0x0002) : { |
---|
| 297 | strcat(name, "RAD2CAL.TXT"); |
---|
| 298 | break; |
---|
| 299 | } |
---|
| 300 | case(0x0004) : { |
---|
| 301 | strcat(name, "RAD3CAL.TXT"); |
---|
| 302 | break; |
---|
| 303 | } |
---|
| 304 | case(0x0008) : { |
---|
| 305 | strcat(name, "RAD4CAL.TXT"); |
---|
| 306 | break; |
---|
| 307 | } |
---|
| 308 | default : { |
---|
| 309 | return -2000; |
---|
| 310 | } |
---|
| 311 | } |
---|
| 312 | |
---|
| 313 | void *cal2file = sysace_fopen(name, "r"); |
---|
| 314 | |
---|
| 315 | sysace_fread(NULL, 1, 2, cal2file); |
---|
| 316 | |
---|
| 317 | sysace_fread(&qval, 1, 2, cal2file); |
---|
| 318 | |
---|
| 319 | sysace_fclose(cal2file); |
---|
| 320 | |
---|
| 321 | return qval; |
---|
| 322 | |
---|
| 323 | } |
---|
| 324 | #endif |
---|
| 325 | |
---|
| 326 | |
---|
| 327 | #ifdef XPAR_XSYSACE_NUM_INSTANCES |
---|
| 328 | short WarpSysace_UpdateDcOffset(short ival, short qval, unsigned int radio) { |
---|
| 329 | |
---|
| 330 | sysace_mkdir("a:\\CAL"); |
---|
| 331 | |
---|
| 332 | sysace_chdir("a:\\CAL"); |
---|
| 333 | |
---|
| 334 | char name[20]=""; |
---|
| 335 | |
---|
| 336 | switch(radio) { |
---|
| 337 | |
---|
| 338 | case(0x0001) : { |
---|
| 339 | strcat(name, "RAD1CAL.TXT"); |
---|
| 340 | break; |
---|
| 341 | } |
---|
| 342 | case(0x0002) : { |
---|
| 343 | strcat(name, "RAD2CAL.TXT"); |
---|
| 344 | break; |
---|
| 345 | } |
---|
| 346 | case(0x0004) : { |
---|
| 347 | strcat(name, "RAD3CAL.TXT"); |
---|
| 348 | break; |
---|
| 349 | } |
---|
| 350 | case(0x0008) : { |
---|
| 351 | strcat(name, "RAD4CAL.TXT"); |
---|
| 352 | break; |
---|
| 353 | } |
---|
| 354 | default : { |
---|
| 355 | return -2000; |
---|
| 356 | } |
---|
| 357 | } |
---|
| 358 | |
---|
| 359 | void *calfile = sysace_fopen(name, "r"); |
---|
| 360 | |
---|
| 361 | char filedatasize = sysace_fread(NULL, 1, 1000, calfile); |
---|
| 362 | |
---|
| 363 | sysace_fclose(calfile); |
---|
| 364 | |
---|
| 365 | if (filedatasize <= 0) { |
---|
| 366 | |
---|
| 367 | void *writefile = sysace_fopen(name, "w"); |
---|
| 368 | |
---|
| 369 | sysace_fwrite(&ival, 1, 2, writefile); |
---|
| 370 | |
---|
| 371 | sysace_fwrite(&qval, 1, 2, writefile); |
---|
| 372 | |
---|
| 373 | sysace_fclose(writefile); |
---|
| 374 | |
---|
| 375 | } |
---|
| 376 | else { |
---|
| 377 | char val[100]; |
---|
| 378 | |
---|
| 379 | void *cal2file = sysace_fopen(name, "r"); |
---|
| 380 | |
---|
| 381 | sysace_fread(NULL, 1, 4, cal2file); |
---|
| 382 | |
---|
| 383 | sysace_fread(&val, 1, 100, cal2file); |
---|
| 384 | |
---|
| 385 | sysace_fclose(cal2file); |
---|
| 386 | |
---|
| 387 | void *write2file = sysace_fopen(name, "w"); |
---|
| 388 | |
---|
| 389 | sysace_fwrite(&ival, 1, 2, write2file); |
---|
| 390 | |
---|
| 391 | sysace_fwrite(&qval, 1, 2, write2file); |
---|
| 392 | |
---|
| 393 | sysace_fwrite(&val, 1, filedatasize, write2file); |
---|
| 394 | |
---|
| 395 | sysace_fclose(write2file); |
---|
| 396 | |
---|
| 397 | } |
---|
| 398 | |
---|
| 399 | } |
---|
| 400 | #endif |
---|
| 401 | |
---|
| 402 | |
---|
| 403 | |
---|