aboutsummaryrefslogtreecommitdiffstats
path: root/meta-steppeeagle/recipes-applications/spi-test/files/spirom-test.c
blob: 1c48f7c48a423a1edbcdbd1a83fbeb2118676672 (plain)
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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
/*****************************************************************************
*
* Copyright (c) 2014, Advanced Micro Devices, Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*     * Redistributions of source code must retain the above copyright
*       notice, this list of conditions and the following disclaimer.
*     * Redistributions in binary form must reproduce the above copyright
*       notice, this list of conditions and the following disclaimer in the
*       documentation and/or other materials provided with the distribution.
*     * Neither the name of Advanced Micro Devices, Inc. nor the names of
*       its contributors may be used to endorse or promote products derived
*       from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL ADVANCED MICRO DEVICES, INC. BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*
***************************************************************************/
#include <stdint.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
#include <fcntl.h>
#include <string.h>
#include <sys/ioctl.h>
#include <linux/types.h>

#include "spirom.h"

#define SPI_APP_VERSION		"0.1"

#define WREN		0x06
#define WRDI		0x04
#define RDSR		0x05
#define RDID		0x9F
#define CHIP_ERASE	0x60
#define SECTOR_ERASE	0x20
#define BLOCK_ERASE	0xD8
#define READ		0x03
#define WRITE		0x02

static void pabort(const char *s)
{
	perror(s);
	abort();
}

static const char *device = "/dev/spirom0.0";
static char command[20];
static int inputfile_fd;
static int outfile_fd;;
static unsigned long address;
static unsigned int num_bytes;

void show_license(void)
{
	printf("/*****************************************************************************\n"
	       "*\n"
	       "* Copyright (c) 2014, Advanced Micro Devices, Inc.\n"
	       "* All rights reserved.\n"
	       "*\n"
	       "* Redistribution and use in source and binary forms, with or without\n"
	       "* modification, are permitted provided that the following conditions are met:\n"
	       "*     * Redistributions of source code must retain the above copyright\n"
	       "*       notice, this list of conditions and the following disclaimer.\n"
	       "*     * Redistributions in binary form must reproduce the above copyright\n"
	       "*       notice, this list of conditions and the following disclaimer in the\n"
	       "*       documentation and/or other materials provided with the distribution.\n"
	       "*     * Neither the name of Advanced Micro Devices, Inc. nor the names of\n"
	       "*       its contributors may be used to endorse or promote products derived\n"
	       "*       from this software without specific prior written permission.\n"
	       "*\n"
	       "* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\" AND\n"
	       "* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\n"
	       "* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE\n"
	       "* DISCLAIMED. IN NO EVENT SHALL ADVANCED MICRO DEVICES, INC. BE LIABLE FOR ANY\n"
	       "* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\n"
	       "* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\n"
	       "* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\n"
	       "* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n"
	       "* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\n"
	       "* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n"
	       "*\n"
	       "*\n"
	       "***************************************************************************/\n");
}

void parse_command(int fd)
{
	uint8_t cmd_byte;
	struct spi_ioc_transfer tr;
	unsigned int bytes_chunks;
	unsigned int remaining_bytes;
	int ret;

	/* Zero initialize spi_ioc_transfer */
	memset(&tr, 0, sizeof(struct spi_ioc_transfer));
	if ((strncmp(command, "WREN", 4) == 0) ||
	    (strncmp(command, "wren", 4) == 0)) {
		/* Command without data */
		tr.buf[0] = WREN;
		tr.direction = 0;
		tr.len = 0;
		tr.addr_present = 0;

		ret = ioctl(fd, SPI_IOC_MESSAGE(1), &tr);
		if (ret < 1)
			pabort("can't send spi message");
	} else if ((strncmp(command, "WRDI", 4) == 0) ||
		   (strncmp(command, "wrdi", 4) == 0)) {
		/* Command without data */
		tr.buf[0] = WRDI;
		tr.direction = 0;
		tr.len = 0;
		tr.addr_present = 0;

		ret = ioctl(fd, SPI_IOC_MESSAGE(1), &tr);
		if (ret < 1)
			pabort("can't send spi message");
	} else if ((strncmp(command, "CHIPERASE", 4) == 0) ||
		   (strncmp(command, "chiperase", 4) == 0)) {

		tr.buf[0] = RDSR;
		tr.direction = RECEIVE;
		tr.addr_present = 0;
		tr.len = 1;

		ret = ioctl(fd, SPI_IOC_MESSAGE(1), &tr);
		if (ret < 1)
			pabort("can't send spi message");
		else if ((tr.buf[1] & 0x02) == 0x00) {
			printf("cannot execute command, write is disabled\n");
			exit(1);
		}

		/* Command without data */
		tr.buf[0] = CHIP_ERASE;
		tr.direction = 0;
		tr.len = 0;
		tr.addr_present = 0;
		ret = ioctl(fd, SPI_IOC_MESSAGE(1), &tr);
		if (ret < 1)
			pabort("can't send spi message");

		/* Make sure WIP has been reset */
		while (1) {
			memset(&tr, 0, sizeof(struct spi_ioc_transfer));
			tr.buf[0] = RDSR;
			tr.direction = RECEIVE;
			tr.addr_present = 0;
			tr.len = 1;

			ret = ioctl(fd, SPI_IOC_MESSAGE(1), &tr);
			if (ret < 1)
				pabort("can't send spi message");

			if ((tr.buf[1] & 0x01) == 0x00)
				break;
		}
	} else if ((strncmp(command, "RDSR", 4) == 0) ||
		   (strncmp(command, "rdsr", 4) == 0)) {
		/* Command with response */
		tr.buf[0] = RDSR;
		tr.direction = RECEIVE;
		tr.addr_present = 0;
		tr.len = 1;

		ret = ioctl(fd, SPI_IOC_MESSAGE(1), &tr);
		if (ret < 1)
			pabort("can't send spi message");

		/*
		 * The 1-byte response will be stored in tr.buf,
		 * so print it out
		 */
		printf("command: 0x%.2x response: 0x%.2x\n", tr.buf[0],
			tr.buf[1]);
	} else if ((strncmp(command, "RDID", 4) == 0) ||
		   (strncmp(command, "rdid", 4) == 0)) {
		/* Command with response */
		tr.buf[0] = RDID;
		tr.direction = RECEIVE;
		tr.addr_present = 0;
		tr.len = 3;

		ret = ioctl(fd, SPI_IOC_MESSAGE(1), &tr);
		if (ret < 1)
			pabort("can't send spi message");

		/*
		 * The 3-bytes response will be stored in tr.buf,
		 * so print it out
		 */
		printf("command: 0x%.2x response: 0x%.2x%.2x%.2x\n", tr.buf[0],
			tr.buf[1], tr.buf[2], tr.buf[3]);
	} else if ((strncmp(command, "SECTORERASE", 6) ==0) ||
		   (strncmp(command, "sectorerase", 6) ==0)) {
		int i;

		tr.buf[0] = RDSR;
		tr.direction = RECEIVE;
		tr.addr_present = 0;
		tr.len = 1;

		ret = ioctl(fd, SPI_IOC_MESSAGE(1), &tr);
		if (ret < 1)
			pabort("can't send spi message");
		else if ((tr.buf[1] & 0x02) == 0x00) {
			printf("cannot execute command, write is disabled\n");
			exit(1);
		}

		/*
		 * num_bytes here is a little bit of misnomer, it indicates the
		 * number of sectors to be erased, rather than the number of
		 * bytes to be erased.
		 */
		for (i = 0; i < num_bytes; i++) {
			/* Write Enable before Sector Erase */
			tr.buf[0] = WREN;
			tr.direction = 0;
			tr.len = 0;
			tr.addr_present = 0;
			ret = ioctl(fd, SPI_IOC_MESSAGE(1), &tr);
			if (ret < 1)
				pabort("can't send spi message");

			/* Command with address but no data */
			memset(&tr, 0, sizeof(struct spi_ioc_transfer));
			tr.buf[0] = SECTOR_ERASE;
			tr.buf[3] = address & 0xff;
			tr.buf[2] = (address >> 8) & 0xff;
			tr.buf[1] = (address >> 16) & 0xff;
			tr.addr_present = 1;
			tr.direction = 0;
			tr.len = 0;

			ret = ioctl(fd, SPI_IOC_MESSAGE(1), &tr);
			if (ret < 1)
				pabort("can't send spi message");

			/* point to the next 4k block */
			address += 4 * 1024;

			/*
			 * Before the next loop, we need to make sure that WIP
			 * bit in the output of RDSR has been reset.
			 */
			while (1) {
				memset(&tr, 0, sizeof(struct spi_ioc_transfer));
				tr.buf[0] = RDSR;
				tr.direction = RECEIVE;
				tr.addr_present = 0;
				tr.len = 1;

				ret = ioctl(fd, SPI_IOC_MESSAGE(1), &tr);
				if (ret < 1)
					pabort("can't send spi message");

				if ((tr.buf[1] & 0x01) == 0x00)
					break;
			}
		}
	} else if ((strncmp(command, "BLOCKERASE", 5) == 0) ||
		   (strncmp(command, "blockerase", 5) == 0)) {
		int i;

		tr.buf[0] = RDSR;
		tr.direction = RECEIVE;
		tr.addr_present = 0;
		tr.len = 1;

		ret = ioctl(fd, SPI_IOC_MESSAGE(1), &tr);
		if (ret < 1)
			pabort("can't send spi message");
		else if ((tr.buf[1] & 0x02) == 0x00) {
			printf("cannot execute command, write is disabled\n");
			exit(1);
		}

		/*
		 * num_bytes indicates the number of blocks to be erased,
		 * rather than the number of bytes to be erased.
		 */
		for (i = 0; i < num_bytes; i++) {
			/* Write Enable before Block Erase */
			tr.buf[0] = WREN;
			tr.direction = 0;
			tr.len = 0;
			tr.addr_present = 0;
			ret = ioctl(fd, SPI_IOC_MESSAGE(1), &tr);
			if (ret < 1)
				pabort("can't send spi message");

			/* Command with address but no data */
			memset(&tr, 0, sizeof(struct spi_ioc_transfer));
			tr.buf[0] = BLOCK_ERASE;
			tr.buf[3] = address & 0xff;
			tr.buf[2] = (address >> 8) & 0xff;
			tr.buf[1] = (address >> 16) & 0xff;
			tr.addr_present = 1;
			tr.direction = 0;
			tr.len = 0;

			ret = ioctl(fd, SPI_IOC_MESSAGE(1), &tr);
			if (ret < 1)
				pabort("can't send spi message");

			/* point to the next 64k block */
			address += 64 * 1024;

			/*
			 * Before the next loop, we need to make sure that WIP
			 * bit in the output of RDSR has been reset.
			 */
			while (1) {
				memset(&tr, 0, sizeof(struct spi_ioc_transfer));
				tr.buf[0] = RDSR;
				tr.direction = RECEIVE;
				tr.addr_present = 0;
				tr.len = 1;

				ret = ioctl(fd, SPI_IOC_MESSAGE(1), &tr);
				if (ret < 1)
					pabort("can't send spi message");

				if ((tr.buf[1] & 0x01) == 0x00)
					break;
			}
		}
	} else if ((strncmp(command, "READ", 4) == 0) ||
		   (strncmp(command, "read", 4) ==0)) {
		int i;

		/*
		 * We will break down the bytes to be received in chunks of
		 * of 64-bytes. Data might not be a even multiple of 64. So
		 * in that case, we will have some remaining bytes <64. We
		 * handle that separately.
		 */
		bytes_chunks = num_bytes / 64;
		remaining_bytes = num_bytes % 64;

		for (i = 0; i < bytes_chunks; i++) {
			/* Command with address and data */
			memset(&tr, 0, sizeof(struct spi_ioc_transfer));
			tr.buf[0] = READ;
			tr.direction = RECEIVE;
			/*
			 * We will store the address into the buffer in little
			 * endian order.
			 */
			tr.buf[3] = address & 0xff;
			tr.buf[2] = (address >> 8) & 0xff;
			tr.buf[1] = (address >> 16) & 0xff;
			tr.len = 64;
			tr.addr_present = 1;

			ret = ioctl(fd, SPI_IOC_MESSAGE(1), &tr);
			if (ret < 1)
				pabort("can't send spi message");

			/* Write the data read to output file */
			if (write(outfile_fd, &tr.buf[4], tr.len) < 0) {
				perror("write error");
				exit(1);
			}
			address += 64;
		}

		if (remaining_bytes) {
			memset(&tr, 0, sizeof(struct spi_ioc_transfer));
			tr.buf[0] = READ;
			tr.direction = RECEIVE;
			tr.buf[3] = address & 0xff;
			tr.buf[2] = (address >> 8) & 0xff;
			tr.buf[1] = (address >> 16) & 0xff;
			tr.len = remaining_bytes;
			tr.addr_present = 1;

			ret = ioctl(fd, SPI_IOC_MESSAGE(1), &tr);
			if (ret < 1)
				pabort("can't send spi message");

			if (write(outfile_fd, &tr.buf[4], tr.len) < 0) {
				perror("write error");
				exit(1);
			}
		}
	} else if ((strncmp(command, "WRITE", 5) == 0) ||
		   (strncmp(command, "write", 5) ==0)) {
		int i;

		/*
		 * We will break down the bytes to be transmitted in chunks of
		 * of 64-bytes. Like for read, we might not have data in an
		 * even multiple of 64 bytes. So we will handle the remaining
		 * bytes in the end.
		 */

		tr.buf[0] = RDSR;
		tr.direction = RECEIVE;
		tr.addr_present = 0;
		tr.len = 1;

		ret = ioctl(fd, SPI_IOC_MESSAGE(1), &tr);
		if (ret < 1)
			pabort("can't send spi message");
		else if ((tr.buf[1] & 0x02) == 0x00) {
			printf("cannot execute command, write is disabled\n");
			exit(1);
		}

		bytes_chunks = num_bytes / 64;
		remaining_bytes = num_bytes % 64;

		for (i = 0; i < bytes_chunks; i++) {
			tr.buf[0] = WREN;
			tr.direction = 0;
			tr.len = 0;
			tr.addr_present = 0;
			ret = ioctl(fd, SPI_IOC_MESSAGE(1), &tr);
			if (ret < 1)
				pabort("can't send spi message");

			/* Command with data and address */
			memset(&tr, 0, sizeof(struct spi_ioc_transfer));
			tr.buf[0] = WRITE;
			tr.direction = TRANSMIT;
			/*
			 * We will store the address into the buffer in little
			 * endian order.
			 */
			tr.buf[3] = address & 0xff;
			tr.buf[2] = (address >> 8) & 0xff;
			tr.buf[1] = (address >> 16) & 0xff;
			tr.len = 64;
			tr.addr_present = 1;

			/* Read 64 bytes from inputfile to buffer */
			if (read(inputfile_fd, &tr.buf[4], tr.len) < 0) {
				perror("read error");
				exit(1);
			}
			ret = ioctl(fd, SPI_IOC_MESSAGE(1), &tr);
			if (ret < 1)
				pabort("can't send spi message");

			address += 64;

			/*
			 * Before the next loop, we need to make sure that WIP
			 * bit in the output of RDSR has been reset.
			 */
			while (1) {
				memset(&tr, 0, sizeof(struct spi_ioc_transfer));
				tr.buf[0] = RDSR;
				tr.direction = RECEIVE;
				tr.addr_present = 0;
				tr.len = 1;

				ret = ioctl(fd, SPI_IOC_MESSAGE(1), &tr);
				if (ret < 1)
					pabort("can't send spi message");

				if ((tr.buf[1] & 0x01) == 0x00)
					break;
			}
		}

		if (remaining_bytes) {
			tr.buf[0] = WREN;
			tr.direction = 0;
			tr.len = 0;
			tr.addr_present = 0;
			ret = ioctl(fd, SPI_IOC_MESSAGE(1), &tr);
			if (ret < 1)
				pabort("can't send spi message");

			memset(&tr, 0, sizeof(struct spi_ioc_transfer));
			tr.buf[0] = WRITE;
			tr.direction = TRANSMIT;
			tr.buf[3] = address & 0xff;
			tr.buf[2] = (address >> 8) & 0xff;
			tr.buf[1] = (address >> 16) & 0xff;
			tr.len = remaining_bytes;
			tr.addr_present = 1;

			if (read(inputfile_fd, &tr.buf[4], tr.len) < 0) {
				perror("read error");
				exit(1);
			}
			ret = ioctl(fd, SPI_IOC_MESSAGE(1), &tr);
			if (ret < 1)
				pabort("can't send spi message");

			while (1) {
				memset(&tr, 0, sizeof(struct spi_ioc_transfer));
				tr.buf[0] = RDSR;
				tr.direction = RECEIVE;
				tr.addr_present = 0;
				tr.len = 1;

				ret = ioctl(fd, SPI_IOC_MESSAGE(1), &tr);
				if (ret < 1)
					pabort("can't send spi message");

				if ((tr.buf[1] & 0x01) == 0x00)
					break;
			}
		}
	} else
		pabort("Unrecognized command, please try again.\n");
}

static void print_usage(const char *prog)
{
	printf("\nUsage: sudo %s [-DCAnio] [arguments]\n\n", prog);
	puts("  -D --device        SPI ROM device to use\n"
	     "                    (default /dev/spirom0.0)\n\n"
	     "  -C --command       command to send to the device\n"
	     "                    (WREN/WRDI/RDSR/RDID/CHIPERASE/SECTORERASE/\n"
	     "                     BLOCKERASE/READ/WRITE)\n\n"
	     "  -A --address       offset in decimal, into the device to read\n"
	     "                     from or write to. For a ROM size of 8MB,\n"
	     "                     address can go from 0 to 8388608. Negative\n"
	     "                     offsets are not valid, but the program\n"
	     "                     won't complain and convert it to its\n"
	     "                     unsigned equivalent.\n\n"
	     "  -n --num-bytes     number of bytes to be read from or written\n"
	     "                     to. Depending on the address, this can\n"
	     "                     take values from 0 to 8388608 for a ROM\n"
	     "                     size of 8MB.\n\n"
	     "                     In case of SECTORERASE and BLOCKERASE\n"
	     "                     commands, num-bytes actually takes the\n"
	     "                     number of sectors and blocks to be erased\n"
	     "                     respectively, rather than the number of\n"
	     "                     bytes.\n\n"
	     "  -i --input-file    file to be used as input.\n\n"
	     "  -o --output-file   file to be used for output. Remember that if\n"
	     "                     an existing filename is given, its contents\n"
	     "                     will be overwritten.\n"
	     "  -l --license       displays the terms of LICENSE for this application\n\n");
	exit(1);
}

static void parse_opts(int argc, char *argv[])
{
	if (argc == 1)
		print_usage(argv[0]);

	while (1) {
		static const struct option lopts[] = {
			{ "device",  1, 0, 'D' },
			{ "command", 1, 0, 'C' },
			{ "address", 1, 0, 'A' },
			{ "num-bytes", 1, 0, 'n' },
			{ "input-file", 1, 0, 'i' },
			{ "output-file", 1, 0, 'o' },
			{ "license", 0, 0, 'l' },
			{ NULL, 0, 0, 0 },
		};
		int c;

		c = getopt_long(argc, argv, "D:C:A:n:i:o:l", lopts, NULL);

		if (c == -1)
			break;

		switch (c) {
		case 'D':
			device = optarg;
			break;
		case 'C':
			memset(command, sizeof(command), 0);
			strncpy(command, optarg, sizeof(command));
			break;
		case 'A':
			address = atol(optarg);
			break;
		case 'n':
			num_bytes = atoi(optarg);
			break;
		case 'i':
			inputfile_fd = open(optarg, O_RDONLY);
			if (inputfile_fd < 0) {
				printf("Error opening %s\n", optarg);
				exit(1);
			}
			break;
		case 'o':
			outfile_fd = open(optarg, O_WRONLY | O_CREAT |
					O_TRUNC, 0644);
			if(outfile_fd < 0) {
				printf("Error opening %s\n", optarg);
				exit(1);
			}
			break;
		case 'l':
			show_license();
			exit(0);;
		default:
			print_usage(argv[0]);
			break;
		}
	}
}

int main(int argc, char *argv[])
{
	int ret = 0;
	int fd;

	printf("SPI sample application version: %s\n", SPI_APP_VERSION);
	printf("Copyright (c) 2014, Advanced Micro Devices, Inc.\n"
	       "This sample application comes with ABSOLUTELY NO WARRANTY;\n"
	       "This is free software, and you are welcome to redistribute it\n"
	       "under certain conditions; type `license' for details.\n\n");

	parse_opts(argc, argv);

	fd = open(device, O_RDWR);
	if (fd < 0)
		pabort("can't open device");

	parse_command(fd);

	return ret;
}