Subversion Repositories svnkaklik

Rev

Details | Last modification | View Log

Rev Author Line No. Line
507 kaklik 1
/*----------------------------------------------------------------------*/
2
/* FAT file system sample project for FatFs R0.06  (C)ChaN, 2008        */
3
/*----------------------------------------------------------------------*/
4
 
5
 
6
#include <avr/io.h>
7
#include <avr/pgmspace.h>
8
#include <avr/interrupt.h>
9
#include <string.h>
10
#include "uart.h"
11
#include "xitoa.h"
12
#include "ff.h"
13
#include "diskio.h"
14
#include "rtc.h"
15
 
16
#include "mmc.c"
17
 
18
 
19
 
20
DWORD acc_size;				/* Work register for fs command */
21
WORD acc_files, acc_dirs;
22
FILINFO finfo;
23
 
24
BYTE line[120];				/* Console input buffer */
25
 
26
FATFS fatfs[2];				/* File system object for each logical drive */
27
BYTE Buff[1024];			/* Working buffer */
28
 
29
 
30
volatile WORD Timer;		/* 100Hz increment timer */
31
 
32
 
33
 
34
#if _MULTI_PARTITION != 0
35
const PARTITION Drives[] = { {0,0}, {0,1} };
36
#endif
37
 
38
/*---------------------------------------------------------*/
39
/* 100Hz timer interrupt generated by OC2                  */
40
/*---------------------------------------------------------*/
41
 
42
 
43
ISR(TIMER2_COMP_vect)
44
{
45
	Timer++;			/* Performance counter for this module */
46
	disk_timerproc();	/* Drive timer procedure of low level disk I/O module */
47
}
48
 
49
 
50
 
51
/*---------------------------------------------------------*/
52
/* User Provided Timer Function for FatFs module           */
53
/*---------------------------------------------------------*/
54
/* This is a real time clock service to be called from     */
55
/* FatFs module. Any valid time must be returned even if   */
56
/* the system does not support a real time clock.          */
57
/* This is not required in read-only configuration.        */
58
 
59
 
60
DWORD get_fattime ()
61
{
62
	RTC rtc;
63
 
64
 
65
	rtc_gettime(&rtc);
66
 
67
	return	  ((DWORD)(rtc.year - 1980) << 25)
68
			| ((DWORD)rtc.month << 21)
69
			| ((DWORD)rtc.mday << 16)
70
			| ((DWORD)rtc.hour << 11)
71
			| ((DWORD)rtc.min << 5)
72
			| ((DWORD)rtc.sec >> 1);
73
}
74
 
75
 
76
/*--------------------------------------------------------------------------*/
77
/* Monitor                                                                  */
78
 
79
 
80
static
81
void put_dump (const BYTE *buff, uint32_t ofs, BYTE cnt)
82
{
83
	BYTE n;
84
 
85
 
86
	xprintf(PSTR("%08lX "), ofs);
87
	for(n = 0; n < cnt; n++)
88
		xprintf(PSTR(" %02X"), buff[n]);
89
	xputc(' ');
90
	for(n = 0; n < cnt; n++) {
91
		if ((buff[n] < 0x20)||(buff[n] >= 0x7F))
92
			xputc('.');
93
		else
94
			xputc(buff[n]);
95
	}
96
	xputc('\n');
97
}
98
 
99
 
100
static
101
void get_line (char *buff, int len)
102
{
103
	char c;
104
	int idx = 0;
105
 
106
 
107
	for (;;) {
108
		c = uart_get();
109
		if (c == '\r') break;
110
		if ((c == '\b') && idx) {
111
			idx--; uart_put(c);
112
		}
113
		if (((BYTE)c >= ' ') && (idx < len - 1)) {
114
				buff[idx++] = c; uart_put(c);
115
		}
116
	}
117
	buff[idx] = 0;
118
	uart_put(c);
119
	uart_put('\n');
120
}
121
 
122
 
123
static
124
FRESULT scan_files (char* path)
125
{
126
	DIR dirs;
127
	FRESULT res;
128
	int i;
129
 
130
 
131
	if ((res = f_opendir(&dirs, path)) == FR_OK) {
132
		i = strlen(path);
133
		while (((res = f_readdir(&dirs, &finfo)) == FR_OK) && finfo.fname[0]) {
134
			if (finfo.fattrib & AM_DIR) {
135
				acc_dirs++;
136
				*(path+i) = '/'; strcpy(path+i+1, &finfo.fname[0]);
137
				res = scan_files(path);
138
				*(path+i) = '\0';
139
				if (res != FR_OK) break;
140
			} else {
141
				acc_files++;
142
				acc_size += finfo.fsize;
143
			}
144
		}
145
	}
146
 
147
	return res;
148
}
149
 
150
 
151
 
152
static
153
void put_rc (FRESULT rc)
154
{
155
	const prog_char *p;
156
	static const prog_char str[] =
157
		"OK\0" "NOT_READY\0" "NO_FILE\0" "NO_PATH\0" "INVALID_NAME\0" "INVALID_DRIVE\0"
158
		"DENIED\0" "EXIST\0" "RW_ERROR\0" "WRITE_PROTECTED\0" "NOT_ENABLED\0"
159
		"NO_FILESYSTEM\0" "INVALID_OBJECT\0" "MKFS_ABORTED\0";
160
	FRESULT i;
161
 
162
	for (p = str, i = 0; i != rc && pgm_read_byte_near(p); i++) {
163
		while(pgm_read_byte_near(p++));
164
	}
165
	xprintf(PSTR("rc=%u FR_%S\n"), (WORD)rc, p);
166
}
167
 
168
 
169
 
170
 
171
static
172
void IoInit ()
173
{
174
	PORTA = 0b11111111;	// Port A
175
 
176
	PORTB = 0b10110000; // Port B
177
	DDRB  = 0b11000000;
178
 
179
	PORTC = 0b11111111;	// Port C
180
 
181
	PORTD = 0b11111111; // Port D
182
 
183
	PORTE = 0b11110010; // Port E
184
	DDRE  = 0b10000010;
185
 
186
	PORTF = 0b11111111;	// Port F
187
 
188
	PORTG = 0b11111; 	// Port G
189
 
190
	uart_init();		// Initialize UART driver
191
 
192
/*
193
	OCR1A = 51;			// Timer1: LCD bias generator (OC1B)
194
	OCR1B = 51;
195
	TCCR1A = 0b00010000;
196
	TCCR1B = 0b00001010;
197
*/
198
	OCR2 = 90-1;		// Timer2: 100Hz interval (OC2)
199
	TCCR2 = 0b00001101;
200
 
201
	TIMSK = 0b10000000;	// Enable TC2.oc, interrupt
202
 
203
	rtc_init();			// Initialize RTC
204
 
205
	sei();
206
}
207
 
208
 
209
 
210
/*-----------------------------------------------------------------------*/
211
/* Main                                                                  */
212
 
213
 
214
int main ()
215
{
216
	char *ptr, *ptr2;
217
	DWORD p1, p2, p3;
218
	BYTE res, b1;
219
	WORD w1;
220
	UINT s1, s2, cnt;
221
	DWORD ofs, sect = 0;
222
	RTC rtc;
223
	FATFS *fs;
224
	DIR dir;				/* Directory object */
225
	FIL file1, file2;		/* File object */
226
 
227
 
228
	IoInit();
229
 
230
	/* Join xitoa module to uart module */
231
	xfunc_out = (void (*)(char))uart_put;
232
 
233
	xputs(PSTR("FatFs module test monitor\n"));
234
 
235
	for (;;) {
236
		xputc('>');
237
		get_line(line, sizeof(line));
238
		ptr = line;
239
 
240
		switch (*ptr++) {
241
 
242
		case 'd' :
243
			switch (*ptr++) {
244
			case 'd' :	/* dd <phy_drv#> [<sector>] - Dump secrtor */
245
				if (!xatoi(&ptr, &p1)) break;
246
				if (!xatoi(&ptr, &p2)) p2 = sect;
247
				res = disk_read((BYTE)p1, Buff, p2, 1);
248
				if (res) { xprintf(PSTR("rc=%d\n"), (WORD)res); break; }
249
				sect = p2 + 1;
250
				xprintf(PSTR("Sector:%lu\n"), p2);
251
				for (ptr=Buff, ofs = 0; ofs < 0x200; ptr+=16, ofs+=16)
252
					put_dump(ptr, ofs, 16);
253
				break;
254
 
255
			case 'i' :	/* di <phy_drv#> - Initialize disk */
256
				if (!xatoi(&ptr, &p1)) break;
257
				xprintf(PSTR("rc=%d\n"), (WORD)disk_initialize((BYTE)p1));
258
				break;
259
 
260
			case 's' :	/* ds <phy_drv#> - Show disk status */
261
				if (!xatoi(&ptr, &p1)) break;
262
				if (disk_ioctl((BYTE)p1, GET_SECTOR_COUNT, &p2) == RES_OK)
263
					{ xprintf(PSTR("Drive size: %lu sectors\n"), p2); }
264
				if (disk_ioctl((BYTE)p1, GET_SECTOR_SIZE, &w1) == RES_OK)
265
					{ xprintf(PSTR("Sector size: %u\n"), w1); }
266
				if (disk_ioctl((BYTE)p1, GET_BLOCK_SIZE, &p2) == RES_OK)
267
					{ xprintf(PSTR("Erase block size: %lu sectors\n"), p2); }
268
				if (disk_ioctl((BYTE)p1, MMC_GET_TYPE, &b1) == RES_OK)
269
					{ xprintf(PSTR("MMC/SDC type: %u\n"), b1); }
270
				if (disk_ioctl((BYTE)p1, MMC_GET_CSD, Buff) == RES_OK)
271
					{ xputs(PSTR("CSD:\n")); put_dump(Buff, 0, 16); }
272
				if (disk_ioctl((BYTE)p1, MMC_GET_CID, Buff) == RES_OK)
273
					{ xputs(PSTR("CID:\n")); put_dump(Buff, 0, 16); }
274
				if (disk_ioctl((BYTE)p1, MMC_GET_OCR, Buff) == RES_OK)
275
					{ xputs(PSTR("OCR:\n")); put_dump(Buff, 0, 4); }
276
				if (disk_ioctl((BYTE)p1, MMC_GET_SDSTAT, Buff) == RES_OK) {
277
					xputs(PSTR("SD Status:\n"));
278
					for (s1 = 0; s1 < 64; s1 += 16) put_dump(Buff+s1, s1, 16);
279
				}
280
				if (disk_ioctl((BYTE)p1, ATA_GET_MODEL, line) == RES_OK)
281
					{ line[40] = '\0'; xprintf(PSTR("Model: %s\n"), line); }
282
				if (disk_ioctl((BYTE)p1, ATA_GET_SN, line) == RES_OK)
283
					{ line[20] = '\0'; xprintf(PSTR("S/N: %s\n"), line); }
284
				break;
285
			}
286
			break;
287
 
288
		case 'b' :
289
			switch (*ptr++) {
290
			case 'd' :	/* bd <addr> - Dump R/W buffer */
291
				if (!xatoi(&ptr, &p1)) break;
292
				for (ptr=&Buff[p1], ofs = p1, cnt = 32; cnt; cnt--, ptr+=16, ofs+=16)
293
					put_dump(ptr, ofs, 16);
294
				break;
295
 
296
			case 'e' :	/* be <addr> [<data>] ... - Edit R/W buffer */
297
				if (!xatoi(&ptr, &p1)) break;
298
				if (xatoi(&ptr, &p2)) {
299
					do {
300
						Buff[p1++] = (BYTE)p2;
301
					} while (xatoi(&ptr, &p2));
302
					break;
303
				}
304
				for (;;) {
305
					xprintf(PSTR("%04X %02X-"), (WORD)(p1), (WORD)Buff[p1]);
306
					get_line(line, sizeof(line));
307
					ptr = line;
308
					if (*ptr == '.') break;
309
					if (*ptr < ' ') { p1++; continue; }
310
					if (xatoi(&ptr, &p2))
311
						Buff[p1++] = (BYTE)p2;
312
					else
313
						xputs(PSTR("???\n"));
314
				}
315
				break;
316
 
317
			case 'r' :	/* br <phy_drv#> <sector> [<n>] - Read disk into R/W buffer */
318
				if (!xatoi(&ptr, &p1)) break;
319
				if (!xatoi(&ptr, &p2)) break;
320
				if (!xatoi(&ptr, &p3)) p3 = 1;
321
				xprintf(PSTR("rc=%u\n"), (WORD)disk_read((BYTE)p1, Buff, p2, p3));
322
				break;
323
 
324
			case 'w' :	/* bw <phy_drv#> <sector> [<n>] - Write R/W buffer into disk */
325
				if (!xatoi(&ptr, &p1)) break;
326
				if (!xatoi(&ptr, &p2)) break;
327
				if (!xatoi(&ptr, &p3)) p3 = 1;
328
				xprintf(PSTR("rc=%u\n"), (WORD)disk_write((BYTE)p1, Buff, p2, p3));
329
				break;
330
 
331
			case 'f' :	/* bf <n> - Fill working buffer */
332
				if (!xatoi(&ptr, &p1)) break;
333
				memset(Buff, (BYTE)p1, sizeof(Buff));
334
				break;
335
 
336
			}
337
			break;
338
 
339
		case 'f' :
340
			switch (*ptr++) {
341
 
342
			case 'i' :	/* fi <log drv#> - Initialize logical drive */
343
				if (!xatoi(&ptr, &p1)) break;
344
				put_rc(f_mount((BYTE)p1, &fatfs[p1]));
345
				break;
346
 
347
			case 's' :	/* fs [<path>] - Show logical drive status */
348
				res = f_getfree(ptr, &p2, &fs);
349
				if (res) { put_rc(res); break; }
350
				xprintf(PSTR("FAT type = %u\nBytes/Cluster = %lu\nNumber of FATs = %u\n"
351
							 "Root DIR entries = %u\nSectors/FAT = %lu\nNumber of clusters = %lu\n"
352
							 "FAT start (lba) = %lu\nDIR start (lba,clustor) = %lu\nData start (lba) = %lu\n"),
353
						(WORD)fs->fs_type, (DWORD)fs->csize * 512, (WORD)fs->n_fats,
354
						fs->n_rootdir, (DWORD)fs->sects_fat, (DWORD)fs->max_clust - 2,
355
						fs->fatbase, fs->dirbase, fs->database
356
				);
357
				acc_size = acc_files = acc_dirs = 0;
358
				res = scan_files(ptr);
359
				if (res) { put_rc(res); break; }
360
				xprintf(PSTR("%u files, %lu bytes.\n%u folders.\n"
361
							 "%lu KB total disk space.\n%lu KB available.\n"),
362
						acc_files, acc_size, acc_dirs,
363
						(fs->max_clust - 2) * (fs->csize / 2), p2 * (fs->csize / 2)
364
				);
365
				break;
366
 
367
			case 'l' :	/* fl [<path>] - Directory listing */
368
				res = f_opendir(&dir, ptr);
369
				if (res) { put_rc(res); break; }
370
				p1 = s1 = s2 = 0;
371
				for(;;) {
372
					res = f_readdir(&dir, &finfo);
373
					if ((res != FR_OK) || !finfo.fname[0]) break;
374
					if (finfo.fattrib & AM_DIR) {
375
						s2++;
376
					} else {
377
						s1++; p1 += finfo.fsize;
378
					}
379
					xprintf(PSTR("%c%c%c%c%c %u/%02u/%02u %02u:%02u %9lu  %s\n"), 
380
								(finfo.fattrib & AM_DIR) ? 'D' : '-',
381
								(finfo.fattrib & AM_RDO) ? 'R' : '-',
382
								(finfo.fattrib & AM_HID) ? 'H' : '-',
383
								(finfo.fattrib & AM_SYS) ? 'S' : '-',
384
								(finfo.fattrib & AM_ARC) ? 'A' : '-',
385
								(finfo.fdate >> 9) + 1980, (finfo.fdate >> 5) & 15, finfo.fdate & 31,
386
								(finfo.ftime >> 11), (finfo.ftime >> 5) & 63,
387
								finfo.fsize, &(finfo.fname[0]));
388
				}
389
				xprintf(PSTR("%4u File(s),%10lu bytes total\n%4u Dir(s)"), s1, p1, s2);
390
				if (f_getfree(ptr, &p1, &fs) == FR_OK)
391
					xprintf(PSTR(", %10luK bytes free\n"), p1 * fs->csize / 2);
392
				break;
393
 
394
			case 'o' :	/* fo <mode> <name> - Open a file */
395
				if (!xatoi(&ptr, &p1)) break;
396
				put_rc(f_open(&file1, ptr, (BYTE)p1));
397
				break;
398
 
399
			case 'c' :	/* fc - Close a file */
400
				put_rc(f_close(&file1));
401
				break;
402
 
403
			case 'e' :	/* fe - Seek file pointer */
404
				if (!xatoi(&ptr, &p1)) break;
405
				res = f_lseek(&file1, p1);
406
				put_rc(res);
407
				if (res == FR_OK)
408
					xprintf(PSTR("fptr = %lu(0x%lX)\n"), file1.fptr, file1.fptr);
409
				break;
410
 
411
			case 'r' :	/* fr <len> - read file */
412
				if (!xatoi(&ptr, &p1)) break;
413
				p2 = 0;
414
				Timer = 0;
415
				while (p1) {
416
					if (p1 >= sizeof(Buff))	{ cnt = sizeof(Buff); p1 -= sizeof(Buff); }
417
					else 			{ cnt = (WORD)p1; p1 = 0; }
418
					res = f_read(&file1, Buff, cnt, &s2);
419
					if (res != FR_OK) { put_rc(res); break; }
420
					p2 += s2;
421
					if (cnt != s2) break;
422
				}
423
				s2 = Timer;
424
				xprintf(PSTR("%lu bytes read with %lu bytes/sec.\n"), p2, p2 * 100 / s2);
425
				break;
426
 
427
			case 'd' :	/* fd <len> - read and dump file from current fp */
428
				if (!xatoi(&ptr, &p1)) break;
429
				ofs = file1.fptr;
430
				while (p1) {
431
					if (p1 >= 16)	{ cnt = 16; p1 -= 16; }
432
					else 			{ cnt = (WORD)p1; p1 = 0; }
433
					res = f_read(&file1, Buff, cnt, &cnt);
434
					if (res != FR_OK) { put_rc(res); break; }
435
					if (!cnt) break;
436
					put_dump(Buff, ofs, cnt);
437
					ofs += 16;
438
				}
439
				break;
440
 
441
			case 'w' :	/* fw <len> <val> - write file */
442
				if (!xatoi(&ptr, &p1) || !xatoi(&ptr, &p2)) break;
443
				memset(Buff, (BYTE)p2, sizeof(Buff));
444
				p2 = 0;
445
				Timer = 0;
446
				while (p1) {
447
					if (p1 >= sizeof(Buff))	{ cnt = sizeof(Buff); p1 -= sizeof(Buff); }
448
					else 			{ cnt = (WORD)p1; p1 = 0; }
449
					res = f_write(&file1, Buff, cnt, &s2);
450
					if (res != FR_OK) { put_rc(res); break; }
451
					p2 += s2;
452
					if (cnt != s2) break;
453
				}
454
				s2 = Timer;
455
				xprintf(PSTR("%lu bytes written with %lu bytes/sec.\n"), p2, p2 * 100 / s2);
456
				break;
457
 
458
			case 'v' :	/* fv - Truncate file */
459
				put_rc(f_truncate(&file1));
460
				break;
461
 
462
			case 'n' :	/* fn <old_name> <new_name> - Change file/dir name */
463
				while (*ptr == ' ') ptr++;
464
				ptr2 = strchr(ptr, ' ');
465
				if (!ptr2) break;
466
				*ptr2++ = 0;
467
				while (*ptr2 == ' ') ptr2++;
468
				put_rc(f_rename(ptr, ptr2));
469
				break;
470
 
471
			case 'u' :	/* fu <name> - Unlink a file or dir */
472
				put_rc(f_unlink(ptr));
473
				break;
474
 
475
			case 'k' :	/* fk <name> - Create a directory */
476
				put_rc(f_mkdir(ptr));
477
				break;
478
 
479
			case 'a' :	/* fa <atrr> <mask> <name> - Change file/dir attribute */
480
				if (!xatoi(&ptr, &p1) || !xatoi(&ptr, &p2)) break;
481
				put_rc(f_chmod(ptr, p1, p2));
482
				break;
483
 
484
			case 't' :	/* ft <year> <month> <day> <hour> <min> <sec> <name> */
485
				if (!xatoi(&ptr, &p1) || !xatoi(&ptr, &p2) || !xatoi(&ptr, &p3)) break;
486
				finfo.fdate = ((p1 - 1980) << 9) | ((p2 & 15) << 5) | (p3 & 31);
487
				if (!xatoi(&ptr, &p1) || !xatoi(&ptr, &p2) || !xatoi(&ptr, &p3)) break;
488
				finfo.ftime = ((p1 & 31) << 11) | ((p1 & 63) << 5) | ((p1 >> 1) & 31);
489
				put_rc(f_utime(ptr, &finfo));
490
				break;
491
 
492
			case 'x' : /* fx <src_name> <dst_name> - Copy file */
493
				while (*ptr == ' ') ptr++;
494
				ptr2 = strchr(ptr, ' ');
495
				if (!ptr2) break;
496
				*ptr2++ = 0;
497
				xprintf(PSTR("Opening \"%s\""), ptr);
498
				res = f_open(&file1, ptr, FA_OPEN_EXISTING | FA_READ);
499
				if (res) {
500
					put_rc(res);
501
					break;
502
				}
503
				xprintf(PSTR("\nCreating \"%s\""), ptr2);
504
				res = f_open(&file2, ptr2, FA_CREATE_ALWAYS | FA_WRITE);
505
				if (res) {
506
					put_rc(res);
507
					f_close(&file1);
508
					break;
509
				}
510
				xprintf(PSTR("\nCopying..."));
511
				p1 = 0;
512
				for (;;) {
513
					res = f_read(&file1, Buff, sizeof(Buff), &s1);
514
					if (res || s1 == 0) break;   /* error or eof */
515
					res = f_write(&file2, Buff, s1, &s2);
516
					p1 += s2;
517
					if (res || s2 < s1) break;   /* error or disk full */
518
				}
519
				if (res) put_rc(res);
520
				xprintf(PSTR("\n%lu bytes copied.\n"), p1);
521
				f_close(&file1);
522
				f_close(&file2);
523
				break;
524
#if _USE_MKFS
525
			case 'm' :	/* fm <logi drv#> <part type> <bytes/clust> - Create file system */
526
				if (!xatoi(&ptr, &p1) || !xatoi(&ptr, &p2) || !xatoi(&ptr, &p3)) break;
527
				xprintf(PSTR("The drive %u will be formatted. Are you sure? (Y/n)="), (WORD)p1);
528
				get_line(ptr, sizeof(line));
529
				if (*ptr == 'Y') put_rc(f_mkfs((BYTE)p1, (BYTE)p2, (WORD)p3));
530
				break;
531
#endif
532
			}
533
			break;
534
 
535
		case 't' :	/* t [<year> <mon> <mday> <hour> <min> <sec>] */
536
			if (xatoi(&ptr, &p1)) {
537
				rtc.year = (WORD)p1;
538
				xatoi(&ptr, &p1); rtc.month = (BYTE)p1;
539
				xatoi(&ptr, &p1); rtc.mday = (BYTE)p1;
540
				xatoi(&ptr, &p1); rtc.hour = (BYTE)p1;
541
				xatoi(&ptr, &p1); rtc.min = (BYTE)p1;
542
				if (!xatoi(&ptr, &p1)) break;
543
				rtc.sec = (BYTE)p1;
544
				rtc_settime(&rtc);
545
			}
546
			rtc_gettime(&rtc);
547
			xprintf(PSTR("%u/%u/%u %02u:%02u:%02u\n"), rtc.year, rtc.month, rtc.mday, rtc.hour, rtc.min, rtc.sec);
548
			break;
549
		}
550
	}
551
 
552
}
553
 
554