📄 main.c
字号:
/******************** (C) COPYRIGHT 2008 STMicroelectronics ********************
* File Name : main.c
* Author : MCD Application Team
* Version : V2.0.3
* Date : 09/22/2008
* Description : Main program body
********************************************************************************
* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME.
* AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT,
* INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE
* CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING
* INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
*******************************************************************************/
/* Includes ------------------------------------------------------------------*/
#include "stm32f10x_lib.h"
#include "sdcard.h"
//#include "dosfs.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "integer.h"
#include "ff.h"
#include "diskio.h"
/* Private typedef -----------------------------------------------------------*/
typedef enum {FAILED = 0, PASSED = !FAILED} TestStatus;
/* Private define ------------------------------------------------------------*/
////////////////////////////////////////////////////////////////////
// Redirect fputc to the serial port, so printf() outputs to USART1.
#ifdef __GNUC__
/* With GCC/RAISONANCE, small printf (option LD Linker->Libraries->Small printf
set to 'Yes') calls __io_putchar() */
#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif /* __GNUC__ */
///////////////////////////////////////////////////////////////////
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
SD_CardInfo SDCardInfo;
SD_Error Status = SD_OK;
ErrorStatus HSEStartUpStatus;
/* Private function prototypes -----------------------------------------------*/
u8 USART_Scanf(u32 value);
void Serial_Init(void);
void RCC_Configuration(void);
void NVIC_Configuration(void);
/* Private functions ---------------------------------------------------------*/
FATFS fs; // Work area (file system object) for logical drive
FIL fsrc, fdst; // file objects
BYTE buffer[51]; // file copy buffer
FRESULT res; // FatFs function common result code
UINT br, bw; // File R/W count
static
FRESULT scan_files (char* path)
{
DWORD acc_size; /* Work register for fs command */
WORD acc_files, acc_dirs;
FILINFO finfo;
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;
}
}
}
return res;
}
void OutPutFile(void)
{
FRESULT res;
FILINFO finfo;
DIR dirs;
int i;
char *fn;
char path[50]={""};
char name[]={"WVO.TXT"};
printf("\n file system starting! \n");
disk_initialize(0);
f_mount(0, &fs);
/*
res = f_opendir(&dir, path);
if (res == FR_OK)
{
i = strlen(path);
for (;;)
{
res = f_readdir(&dir, &fno);
if (res != FR_OK || fno.fname[0] == 0) break;
fn = fno.fname;
if (fno.fattrib & AM_DIR)
{
sprintf(&path[i], "/%s", fn);
res = scan_files(path);
if (res != FR_OK) break;
path[i] = 0;
}
else
{
printf("%s/%s\n\n", path, fn);
}
}
}
*/
if (f_opendir(&dirs, path) == FR_OK)
{
while (f_readdir(&dirs, &finfo) == FR_OK)
{
if (finfo.fattrib & AM_ARC)
{
if(!finfo.fname[0])
break;
printf("\n file name is:\n %s\n",finfo.fname);
res = f_open(&fsrc, finfo.fname, FA_OPEN_EXISTING | FA_READ);
res = f_read(&fsrc, &buffer, 50, &br);
printf("\n file contex is:\n %s\n",buffer);
f_close(&fsrc);
}
}
}
while(1);
}
/*******************************************************************************
* Function Name : main
* Description : Main program.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
int main(void)
{
#ifdef DEBUG
debug();
#endif
/*
static uint8_t sector[SECTOR_SIZE];
static uint8_t transferbuffer[SECTOR_SIZE];
uint32_t pstart, psize, i;
uint8_t pactive, ptype;
VOLINFO vi;
DIRINFO di;
FILEINFO fi;
DIRENT de;
uint32_t cache;
*/
// Clock Config: HSE 72 MHz
RCC_Configuration();
// Interrupt Config
NVIC_Configuration();
// USART Config : 115200,8,n,1
Serial_Init();
/////////////////////////////////////////////////////////////////////
//////// SDCARD Initialisation //////////////////////////////////////
/////////////////Section adapted from ST example/////////////////////
/*-------------------------- SD Init ----------------------------- */
Status = SD_Init();
if (Status == SD_OK)
{
/*----------------- Read CSD/CID MSD registers ------------------*/
Status = SD_GetCardInfo(&SDCardInfo);
}
if (Status == SD_OK)
{
/*----------------- Select Card --------------------------------*/
Status = SD_SelectDeselect((u32) (SDCardInfo.RCA << 16));
}
if (Status == SD_OK)
{
Status = SD_EnableWideBusOperation(SDIO_BusWide_4b);
}
/* Set Device Transfer Mode to DMA */
if (Status == SD_OK)
{
Status = SD_SetDeviceMode(SD_DMA_MODE);//oet
// Status = SD_SetDeviceMode(SD_POLLING_MODE);
// Status = SD_SetDeviceMode(SD_INTERRUPT_MODE);
}
OutPutFile();
/////////////////////////////////////////////////////////////////////
//////// DOSFS Volume Acess /////////////////////////////////////////
/////////////////////////////////////////////////////////////////////
/*
if (DFS_ReadSector(0,sector,0,1))
{
printf("Cannot read media!\n");
while(1);
return -1;
}
if ((sector[0x1FE] == 0x55) & (sector[0x1FF] == 0xAA))
{
printf("The end of the last read sector contains a FAT or MBR end delimiter\n");
if ((sector[0x36] == 'F') & (sector[0x37] == 'A') & (sector[0x38] == 'T'))
{
printf("Well, this is not a MBR, but a FAT12/16 header!\n");
pstart = 0;
pactive = 0x80;
ptype = 0x06;
psize = 0xFFFFFFFF;
}
else if ((sector[0x52] == 'F') & (sector[0x53] == 'A') & (sector[0x54] == 'T'))
{
printf("Well, this is not a MBR, but a FAT32 header!\n");
pstart = 0;
pactive = 0x80;
ptype = 0x06;
psize = 0xFFFFFFFF;
}
else
{
pstart = DFS_GetPtnStart(0, sector, 0, &pactive, &ptype, &psize);
if (pstart == 0xffffffff)
{
printf("Cannot find first partition\n");
return -1;
}
}
}
printf("Partition 0 start sector 0x%-08.8lX active %-02.2hX type %-02.2hX size %-08.8lX\n", pstart, pactive, ptype, psize);
if (DFS_GetVolInfo(0, sector, pstart, &vi))
{
printf("Error getting volume information\n");
return -1;
}
printf("Volume label '%-11.11s'\n", vi.label);
printf("%d sector/s per cluster, %d reserved sector/s, volume total %d sectors.\n", vi.secperclus, vi.reservedsecs, vi.numsecs);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -