⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.c

📁 这是一个很精巧的FAT文件系统的,它实现数据文件存储的FAT方式访问
💻 C
📖 第 1 页 / 共 2 页
字号:
/*----------------------------------------------------------------------*/
/* FAT file system sample project for FatFs R0.04a  (C)ChaN, 2007       */
/*----------------------------------------------------------------------*/


#include <avr/io.h>
#include <avr/pgmspace.h>
#include <avr/interrupt.h>
#include <string.h>
#include "uart.h"
#include "xitoa.h"
#include "stime.h"
#include "ff.h"
#include "diskio.h"



DWORD acc_size;				/* Work register for fs command */
WORD acc_files, acc_dirs;
FILINFO finfo;

BYTE linebuf[120];			/* Console input buffer */

FATFS fatfs[2];				/* File system object for each logical drive */
BYTE Buff[1024];			/* Working buffer */


volatile WORD Timer;		/* 100Hz increment timer */

time_t rtc = 1144713600;	/* Real Time Clock (start at 2006/4/11) */


#if _MULTI_PARTITION != 0
const PARTITION Drives[] = { {0,0}, {0,1} };
#endif

/*---------------------------------------------------------*/
/* 100Hz timer interrupt generated by OC2                  */
/*---------------------------------------------------------*/


ISR(TIMER2_COMP_vect)
{
	Timer++;			/* Performance counter for this module */
	disk_timerproc();	/* Drive timer procedure of low level disk I/O module */
}



/*---------------------------------------------------------*/
/* 1 Hz timer interrupt generated by OC0                   */
/*---------------------------------------------------------*/


ISR(TIMER0_COMP_vect)
{
	rtc++;
}



/*---------------------------------------------------------*/
/* User Provided Timer Function for FatFs module           */
/*---------------------------------------------------------*/
/* This is a real time clock service to be called from     */
/* FatFs module. Any valid time must be returned even if   */
/* the system does not support a real time clock.          */


DWORD get_fattime ()
{
	struct tm *tmr;
	time_t t;


	cli();
	t = rtc;
	sei();
	tmr = gmtime(&t);
	return	  ((DWORD)(tmr->tm_year - 80) << 25)
			| ((DWORD)(tmr->tm_mon + 1) << 21)
			| ((DWORD)tmr->tm_mday << 16)
			| (WORD)(tmr->tm_hour << 11)
			| (WORD)(tmr->tm_min << 5)
			| (WORD)(tmr->tm_sec >> 1);
/*
	return	((2006UL-1980) << 25)	// Year = 2006
			| (2UL << 21)			// Month = Feb
			| (9UL << 16)			// Day = 9
			| (22U << 11)			// Hour = 22
			| (30U << 5)			// Min = 30
			| (0U >> 1)				// Sec = 0
			;
*/
}


/*--------------------------------------------------------------------------*/
/* Monitor                                                                  */


static
void put_dump (uint8_t *buff, uint32_t ofs, uint8_t cnt)
{
	uint8_t n;


	xprintf(PSTR("\n%08lX "), ofs);
	for(n = 0; n < cnt; n++)
		xprintf(PSTR(" %02X"), buff[n]);
	xputc(' ');
	for(n = 0; n < cnt; n++) {
		if ((buff[n] < 0x20)||(buff[n] >= 0x7F))
			xputc('.');
		else
			xputc(buff[n]);
	}
}


static
void get_line (char *buff, int len)
{
	char c;
	int idx = 0;


	for (;;) {
		c = uart_get();
		if (c == '\r') break;
		if ((c == '\b') && idx) {
			idx--; uart_put(c);
		}
		if (((BYTE)c >= ' ') && (idx < len - 1)) {
				buff[idx++] = c; uart_put(c);
		}
	}
	buff[idx] = 0;
	uart_put(c);
}


static
FRESULT scan_files (char* path)
{
	DIR dirs;
	FRESULT res;
	int i;


	if ((res = f_opendir(&dirs, path)) == FR_OK) {
		i = strlen(path);
		while (((res = f_readdir(&dirs, &finfo)) == FR_OK) && finfo.fname[0]) {
			if (finfo.fattrib & AM_DIR) {
				acc_dirs++;
				*(path+i) = '/'; strcpy(path+i+1, &finfo.fname[0]);
				res = scan_files(path);
				*(path+i) = '\0';
				if (res != FR_OK) break;
			} else {
				acc_files++;
				acc_size += finfo.fsize;
			}
		}
	}

	return res;
}



static
void put_rc (FRESULT rc)
{
	const prog_char *p;
	static const prog_char str[] =
		"OK\0" "NOT_READY\0" "NO_FILE\0" "NO_PATH\0" "INVALID_NAME\0" "INVALID_DRIVE\0"
		"DENIED\0" "EXIST\0" "RW_ERROR\0" "WRITE_PROTECTED\0" "NOT_ENABLED\0"
		"NO_FILESYSTEM\0" "INVALID_OBJECT\0" "MKFS_ABORTED\0";
	FRESULT i;

	for (p = str, i = 0; i != rc && pgm_read_byte_near(p); i++) {
		while(pgm_read_byte_near(p++));
	}
	xprintf(PSTR("\nrc=%u FR_%S"), (WORD)rc, p);
}




static
void IoInit ()
{
	PORTA = 0b11111111;	// Port A

	PORTB = 0b10110000; // Port B
	DDRB  = 0b11000000;

	PORTC = 0b11111111;	// Port C

	PORTD = 0b11111111; // Port D

	PORTE = 0b11111110; // Port E
	DDRE  = 0b10000010;

	PORTF = 0b11111111;	// Port F

	PORTG = 0b00111; 	// Port G

	uart_init();		// Initialize UART driver

	ASSR = 0b00001000;	// Timer0: 1Hz async operation (OC0)
	OCR0 = 128-1;
	TCCR0 = 0b0001110;
/*
	OCR1A = 51;			// Timer1: LCD bias generator (OC1B)
	OCR1B = 51;
	TCCR1A = 0b00010000;
	TCCR1B = 0b00001010;
*/
	OCR2 = 90-1;		// Timer2: 100Hz interval (OC2)
	TCCR2 = 0b00001101;

	TIMSK = 0b10000010;	// Enable TC2.oc, TC0.oc interrupt

	sei();
}



/*-----------------------------------------------------------------------*/
/* Main                                                                  */


int main ()
{
	char *ptr, *ptr2;
	DWORD p1, p2, p3;
	BYTE res;
	WORD s1, s2, cnt, blen = sizeof(Buff);
	DWORD ofs, sect = 0;
	struct tm *tmr = gmtime(0);
	FATFS *fs;
	DIR dir;				/* Directory object */
	FIL file1, file2;		/* File object */


	IoInit();

	/* Join xitoa module to uart module */
	xfunc_out = (void (*)(char))uart_put;

	xputs(PSTR("\nFatFs module test monitor"));

	for (;;) {
		xputs(PSTR("\n>"));
		ptr = linebuf;
		get_line(ptr, sizeof(linebuf));

		switch (*ptr++) {

		case 'd' :
			switch (*ptr++) {
			case 'd' :	/* dd <phy_drv#> [<sector>] - Dump secrtor */
				if (!xatoi(&ptr, &p1)) break;
				if (!xatoi(&ptr, &p2)) p2 = sect;
				res = disk_read((BYTE)p1, Buff, p2, 1);
				if (res) { xprintf(PSTR("\nrc=%d"), (WORD)res); break; }
				sect = p2 + 1;
				xprintf(PSTR("\nSector:%lu"), p2);
				for (ptr=Buff, ofs = 0; ofs < 0x200; ptr+=16, ofs+=16)
					put_dump(ptr, ofs, 16);
				break;

			case 'i' :	/* di <phy_drv#> - Initialize disk */
				if (!xatoi(&ptr, &p1)) break;
				xprintf(PSTR("\nrc=%d"), (WORD)disk_initialize((BYTE)p1));
				break;

			case 's' :	/* ds <phy_drv#> - Show disk status */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -