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

📄 main.c

📁 基于pic24的fat系统,支持fat16,fat32
💻 C
📖 第 1 页 / 共 2 页
字号:
/*---------------------------------------------------------------*/
/* FAT file system module test program R0.06      (C)ChaN, 2008  */
/*---------------------------------------------------------------*/
/* http://elm-chan.org/fsw/ff/00index_e.html */

/*
******************************************************************************
* This program has been modified from FatFs (FAT File System Module) to run on
* the hardware platform PIC24-Eval-B2 Rev B for PICmicro PIC24FJ128GA010.
* 
* The original source code and features of FatFs can be obtained from the Internet
* web site at http://elm-chan.org/fsw/ff/00index_e.html
*
* To use this program, one must use HyperTerminal at 19200bps, no parity, 
* 1-stop bit, no handshake. Connect a straight cable from PC's COM PORT to
* RS232 connector onboard (J2B) for serial communication.
*
* Format a SD card and insert it to J1B (the SD Card socket) onboard.
*
* A reset action will send an ASCII message to HyperTerminal as
* <FatFs module test monitor for PIC24F> on PC
*
* Experiment a bit with the following command from hyperterminal:
* 
* di <Enter>	: This command initializes the SD Card.
* ds <Enter> 	: This command shows the sector information your SD card
* fi <Enter>	: Initialize the logical drive
* fl <Enter>	: It is the directory listing command
*
* Learn more about this FatFs from elm-chan web page as above. Look at the main()
* to see what else function is supported.
* 
* Author : John Leung
* Company: TechToys Company
* www.TechToys.com.hk
* Date: 9th Oct 2008
******************************************************************************
*/

#include <string.h>
//#include <p24FJ64GA002.h>
#include <p24FJ128GA010.h>
#include "pic24f.h"
#include "comm.h"
#include "monitor.h"
#include "diskio.h"
#include "ff.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_PRIPLL & FCKSM_CSDCMD & OSCIOFNC_OFF & IOL1WAY_OFF & I2C1SEL_PRI & POSCMOD_HS)

// Configuration bits
// Configure fast RC oscillator with PLL and RC15 pin for Fosc/2 function	
_CONFIG2(FNOSC_FRCPLL & OSCIOFNC_OFF)
_CONFIG1(JTAGEN_OFF & FWDTEN_OFF)   // JTAG off, watchdog timer off

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 */
	//AD1PCFG = 0x1FFF;
	//LATB =  0xD00C;
	//TRISB = 0x1C08;
	//LATA =  0x0001;
	//TRISA = 0x0000;
	//_CN15PUE = 1;
	//_CN16PUE = 1;

	/* Attach UART1 module to I/O pads */
	//No I/O padding required for high pin-count micro
	//RPOR1 = 0x0003;	/* U1TX -- RP2 */
	//RPINR18 = 0x1F03;	/* U1RX -- RP3 */

	/* Attach SPI1 module to I/O pads */
	//No I/O padding required for high pin-count micro
	//RPINR20 = 0x1F0C;	/* SDI1 -- RP12 */
	//RPOR6 = 0x0800;	/* SCK1OUT -- RP13 */
	//RPOR7 = 0x0007;	/* SDO1 -- RP14 */

	/* Start Timer1 in interval time of 1ms */
	// 1 tick period = (1/FCY)*8
	// Now we want a 1ms period, therefore the number of ticks is calculated as
	// (1/FCY)*8*PR1 = 1/1000; thus, PR1 = FCY/8/1000

	CLKDIV = 0x0000;					//FRC postscaler divided by 1 (8MHz from internal RC)

	PR1 = FCY / 8 / 1000;
	_TCKPS0 = 1;	/* Select prescaler Fcy/8 */
	_TON = 1;		/* Start Timer1 */
	_T1IE = 1;		/* Enable Timer1 interrupt */


	_EI();
}



/*-----------------------------------------------------------------------*/
/* 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 */
	spiInit();			// low level SPI port initialization

	//There is no LED onboard!
	//_LATA0 = 0;		/* LED ON */

	xputs("\nFatFs module test monitor for PIC24F\n");

	for (;;) {
		xputc('>');
		ptr = linebuf;
		get_line(ptr, sizeof(linebuf));

		switch (*ptr++) {

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

			case 'i' :	/* di - Initialize physical drive */
				xprintf("rc=%d\n", (WORD)disk_initialize(0));
				break;

			case 's' :	/* ds - Show disk status */
				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)

⌨️ 快捷键说明

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