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

📄 main.c

📁 FAT16 Microchip Application not for PIC24F
💻 C
字号:
/*---------------------------------------------------------------*/
/* FAT file system module test program R0.06      (C)ChaN, 2008  */
/*---------------------------------------------------------------*/


#include <p24fj128ga106.h>


_CONFIG1(JTAGEN_OFF & GCP_OFF & GWRP_OFF & BKBUG_OFF & COE_OFF & ICS_PGx1 & FWDTEN_OFF & WINDIS_OFF & FWPSA_PR32 & WDTPS_PS32768)
_CONFIG2(IESO_OFF & FNOSC_FRCPLL & FCKSM_CSDCMD & OSCIOFNC_ON & IOL1WAY_OFF & POSCMOD_NONE)

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>  
#include <stdio.h> 
#include <errno.h> 
#include "HardwareProfile.h"
#include "Delay.h"
#include "comm.h"
#include "monitor.h"
#include "diskio.h"
#include "ff.h"


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

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

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


volatile UINT Timer;		/* 1kHz increment timer */

volatile BYTE rtcYear = 108, rtcMon = 4, rtcMday = 1, rtcHour, rtcMin, rtcSec;




/*---------------------------------------------------------*/
/* 1000Hz timer interrupt generated by Timer1              */
/*---------------------------------------------------------*/


void __attribute__((interrupt, auto_psv)) _T1Interrupt (void)
{
	static const BYTE samurai[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
	static UINT div1k;
	BYTE n;


	_T1IF = 0;		/* Clear irq flag */
	Timer++;			/* Performance counter for this module */
	disk_timerproc();	/* Drive timer procedure of low level disk I/O module */

	/* Real Time Clock */
	if (++div1k >= 1000) {
		div1k = 0;
		if (++rtcSec >= 60) {
			rtcSec = 0;
			if (++rtcMin >= 60) {
				rtcMin = 0;
				if (++rtcHour >= 24) {
					rtcHour = 0;
					n = samurai[rtcMon - 1];
					if ((n == 28) && !(rtcYear & 3)) n++;
					if (++rtcMday > n) {
						rtcMday = 1;
						if (++rtcMon > 12) {
							rtcMon = 1;
							rtcYear++;
						}
					}
				}
			}
		}
	}
}



/*----------------------------------------------------------*/
/* User Provided RTC Function for FatFs module              */
/*----------------------------------------------------------*/
/* This is a real time clock service to be called from      */
/* FatFs module. Any value as a valid time must be returned */
/* even if the system does not support a real time clock.   */
/* This is not required in read-only configuration.         */


DWORD get_fattime (void)
{
	DWORD tmr;


	_DI();
	tmr =	  (((DWORD)rtcYear - 80) << 25)
			| ((DWORD)rtcMon << 21)
			| ((DWORD)rtcMday << 16)
			| (WORD)(rtcHour << 11)
			| (WORD)(rtcMin << 5)
			| (WORD)(rtcSec >> 1);
	_EI();

	return tmr;
}




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


static
void put_rc (FRESULT rc)
{
	const char *p;
	static const char str[] =
		"OK\0" "NOT_READY\0" "NO_FILE\0" "FR_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 && *p; i++) {
		while(*p++);
	}
	xprintf("rc=%u FR_%s\n", (UINT)rc, p);
}



static
FRESULT scan_files (char* path)
{
	DIR dirs;
	FRESULT res;
	BYTE 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;
			}
		}
	}
	if (res) put_rc(res);
		return res;
}



static
void IoInit ()
{
	/* Initialize GPIO ports */
	_DOZEN = 0;
	_RCDIV2 = 0;
	_RCDIV1 = 0;
	_RCDIV0 = 0;


	asm volatile ( "MOV #OSCCON, w1 \n"
"MOV #0x46, w2 \n"
"MOV #0x57, w3 \n"
"MOV.b w2, [w1] \n"
"MOV.b w3, [w1] \n"
"BCLR OSCCON,#6");

	AD1PCFGL = 0xffff;
	REDLED = 0;
	REDLED_TRIS = 0;
	GREENLED = 0;
	GREENLED_TRIS = 0;
	SDCS = 1;
	SDCS_TRIS = 0;

	RPINR18bits.U1RXR = 10;
	RPOR8bits.RP17R = 3;


		/* Attach SPI2 module to I/O pads */
	RPINR22bits.SDI2R = 26;			//SDI2 -- RP19	
	RPOR10bits.RP21R = SCK2OUT_IO; 	//SCK1OUT -- RP21
	RPOR9bits.RP19R = SDO2_IO;		//RP26 = SDO2

//	RPINR20 = 0x1F0C;	/* SDI1 -- RP12 */
//	RPOR6 = 0x0800;	/* SCK1OUT -- RP13 */
//	RPOR7 = 0x0007;	/* SDO1 -- RP14 */

// Lock Registers
asm volatile ( "MOV #OSCCON, w1 \n"
"MOV #0x46, w2 \n"
"MOV #0x57, w3 \n"
"MOV.b w2, [w1] \n"
"MOV.b w3, [w1] \n"
"BSET OSCCON, #6" );

	



	/* Start Timer1 in interval time of 1ms */
	PR1 = FCY / 8 / 1000;
	_TCKPS0 = 1;	/* Select prescaler Fcy/8 */
	_TON = 1;	/* Start Timer1 */
	_T1IE = 1;	/* Enable Timer1 interrupt */


	_EI();
}







void file_list(char *ptr)
///////////////////////////////////////////////////////////////////////////
// void file_list(char *ptr)
//
//  Lists the contents of a text file
///////////////////////////////////////////////////////////////////////////
    {
    FRESULT result;      // FatFs function common result code
    FIL fsrc;
	char mesg[32];
    result = f_open(&fsrc, ptr, FA_OPEN_EXISTING | FA_READ);

    // display the contents of the file
    if (result == FR_OK)
        {
        WORD i, br;

        // Display the file's FIL data structure
        // f_show_FIL_structure(&fsrc);

        do
            {
            result = f_read(&fsrc, mesg, sizeof(mesg), &br);
            for (i = 0; i < br; i++)
                uart_put(mesg[i]);
                //COM1_putc(mesg[i]);
            } while ((result == FR_OK) && br);

        // Display the file's FIL data structure
        // f_show_FIL_structure(&fsrc);
        if (result != FR_OK)
            {
            xprintf("TYPE command ERROR\r\n");
            f_get_error_mesg(result,mesg);
            xprintf("FILE SYSTEM ERROR - %s\r\n",mesg);
            }

        // Close all files
        f_close(&fsrc);
        xprintf("\r\n");
        }
    else
        {
        f_get_error_mesg(result,mesg);
        xprintf("FILE SYSTEM ERROR - %s\r\n",mesg);
        }
    }


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


int main ()
{

	char *ptr, *ptr2;
	long p1, p2, p3;
	BYTE res, b1;
	WORD w1;
	UINT s1, s2, cnt;
	DWORD ofs = 0, sect = 0;
	FATFS *fs;				/* Pointer to file system object */
	DIR dir;				/* Directory object */
	FIL file1, file2;		/* File objects */


	IoInit();
	uart_init();		/* Initialize UART driver */
	
	
	xputs("\nFatFs module test monitor for PIC24F\n");

	xprintf("rc=%d\n", (WORD)disk_initialize(0));

/*	if (disk_ioctl(0, GET_SECTOR_COUNT, &p2) == RES_OK)
		{ xprintf("Drive size: %lu sectors\n", p2); }
	if (disk_ioctl(0, GET_SECTOR_SIZE, &w1) == RES_OK)
		{ xprintf("Sector size: %u\n", w1); }
	if (disk_ioctl(0, GET_BLOCK_SIZE, &p2) == RES_OK)
		{ xprintf("Erase block size: %lu sectors\n", p2); }
	if (disk_ioctl(0, MMC_GET_TYPE, &b1) == RES_OK)
		{ xprintf("MMC/SDC type: %u\n", b1); }
	if (disk_ioctl(0, MMC_GET_CSD, Buff) == RES_OK)
		{ xputs("CSD:\n"); put_dump(Buff, 0, 16); }
	if (disk_ioctl(0, MMC_GET_CID, Buff) == RES_OK)
		{ xputs("CID:\n"); put_dump(Buff, 0, 16); }
	if (disk_ioctl(0, MMC_GET_OCR, Buff) == RES_OK)
		{ xputs("OCR:\n"); put_dump(Buff, 0, 4); }
	if (disk_ioctl(0, MMC_GET_SDSTAT, Buff) == RES_OK) {
		xputs("SD Status:\n");
		for (s1 = 0; s1 < 64; s1 += 16) put_dump(Buff+s1, s1, 16);
	}*/
	
	put_rc(f_mount(0, &fatfs[0]));
	
	res = f_opendir(&dir, ptr);
	if (res) { put_rc(res);}
	p1 = s1 = s2 = 0;
	for(;;) {
		res = f_readdir(&dir, &finfo);
		if ((res != FR_OK) || !finfo.fname[0]) break;
		if (finfo.fattrib & AM_DIR) {
			s2++;
		} else {
			s1++; p1 += finfo.fsize;
		}
		xprintf("%c%c%c%c%c %u/%02u/%02u %02u:%02u %9lu  %s\n", 
				(finfo.fattrib & AM_DIR) ? 'D' : '-',
				(finfo.fattrib & AM_RDO) ? 'R' : '-',
				(finfo.fattrib & AM_HID) ? 'H' : '-',
				(finfo.fattrib & AM_SYS) ? 'S' : '-',
				(finfo.fattrib & AM_ARC) ? 'A' : '-',
				(finfo.fdate >> 9) + 1980, (finfo.fdate >> 5) & 15, finfo.fdate & 31,
				(finfo.ftime >> 11), (finfo.ftime >> 5) & 63,
				finfo.fsize, &(finfo.fname[0]));
	}
	xprintf("%4u File(s),%10lu bytes total\n%4u Dir(s)", s1, p1, s2);
	if (f_getfree(ptr, (DWORD*)&p1, &fs) == FR_OK)
		xprintf(", %10lu bytes free\n", p1 * fs->csize * 512);
	
	file_list("NIMISH.TXT");
	xputs("\nEnd\n");
	while(1);


}

⌨️ 快捷键说明

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