📄 test-1__.c
字号:
** BUTTON, OR BY INSTALLING, COPYING, RUNNING, OR OTHERWISE USING THE PRIIO
** SOFTWARE, YOU AGREE TO BE BOUND BY THE TERMS OF THIS LICENSE AGREEMENT. IF
** YOU DO NOT AGREE TO THE TERMS OF THIS LICENSE AGREEMENT, PLEASE CLICK THE
** "CANCEL" BUTTON, AND DO NOT INSTALL, RUN, COPY, OR OTHERWISE USE THE PRIIO
** SOFTWARE.
**
*****************************************************************************
**
** VERSION HISTORY:
** ----------------
** Version: 3.0
** Date: March 29, 2006
** Revised by: Erick M. Higa
** Description:
** - See "FILE_SYS.C" file for any chages up to this point.
**
** Version: 3.0.1
** Date: April 7, 2006
** Revised by: Erick M. Higa
** Description:
** - Fixed initialize_media() so that the active partition entry
** (reguardless if it is the first entry) will be used.
**
****************************************************************************/
#define _SD_CMD_C_SRC
/*****************************************************************************
**
** MODULES USED
**
*****************************************************************************/
#include "..\flash\sd_cmd.h"
#include "..\flash\file_sys.h"
#ifdef _RTC_ON_
#include "..\flash\twi.h"
#endif
/*****************************************************************************
**
** PROTOTYPES OF LOCAL FUNCTIONS
**
*****************************************************************************/
void _FF_spi_init(void);
void _SD_send_cmd(uint8 command, uint32 argument);
void clear_sd_buff(void);
int8 reset_sd(void);
int8 init_sd(void);
/*****************************************************************************
**
** EXPORTED VARIABLES
**
*****************************************************************************/
uint8 _FF_Buff[512];
#ifndef _BYTES_PER_SEC_512_
HiLo16Union BPB_BytsPerSec;
#endif
uint8 BPB_SecPerClus;
HiLo16Union BPB_RsvdSecCnt;
HiLo16Union BPB_RootEntCnt;
HiLo16Union BPB_FATSz16;
uint8 BPB_FATType;
#ifndef _BIG_ENDIAN_
uint32 BS_VolSerial;
#else
HiLo32Union BS_VolSerial;
#endif
int8 BS_VolLab[12];
uint16 _FF_PartitionAddr;
uint32 _FF_RootAddr;
#ifdef _DIRECTORIES_SUPPORTED_
uint32 _FF_DirAddr;
#endif
uint16 _FF_Fat1Addr;
uint32 _FF_Fat2Addr;
uint16 FirstDataSector;
uint8 _FF_error;
uint32 _FF_BuffAddr;
uint16 Clus0Counter;
int8 _FF_FullPath[_FF_PATH_LENGTH];
uint16 DataClusTot;
#if defined(_SD_BLOCK_WRITE_) && !defined(_READ_ONLY_)
uint32 SDBlockWriteBlockCnt;
#endif
/*****************************************************************************
**
** GLOBAL VARIABLES
**
*****************************************************************************/
#ifdef _DEBUG_ON_
flash int8 _FF_VersionInfoStr[] = "\r\nFlashFile ";
flash int8 _FF_BSecStr[] = "\r\nBoot_Sec:\t[%X %X %X] [%X] [%X]";
flash int8 _FF_BootSecPartAddrStr[] = "\r\nPart Address:\t%lX";
flash int8 _FF_BPB_BPSStr[] = "\r\nBPB_BytsPerSec:\t%X";
flash int8 _FF_BPB_SPCStr[] = "\r\nBPB_SecPerClus:\t%X";
flash int8 _FF_BPB_RSCStr[] = "\r\nBPB_RsvdSecCnt:\t%X";
flash int8 _FF_BPB_NFATStr[] = "\r\nBPB_NumFATs:\t%X";
flash int8 _FF_BPB_RECStr[] = "\r\nBPB_RootEntCnt:\t%X";
flash int8 _FF_BPB_Fz16Str[] = "\r\nBPB_FATSz16:\t%X";
flash int8 _FF_BPB_TS16Str[] = "\r\nBPB_TotSec16:\t%lX";
flash int8 _FF_BPB_FTStr[] = "\r\nBPB_FATType:\tFAT";
flash int8 _FF_1CStr[] = "1%c";
flash int8 _FF_ERRStr[] = " ERROR!!";
flash int8 _FF_BPB_FTEStr[] = "\r\nBPB_FATType:\tFAT ERROR!!!";
flash int8 _FF_CCntStr[] = "\r\nClusterCnt:\t%lX";
flash int8 _FF_RAddrStr[] = "\r\nROOT_ADDR:\t%lX";
flash int8 _FF_F2AddrStr[] = "\r\nFAT2_ADDR:\t%lX";
flash int8 _FF_RDSecStr[] = "\r\nRootDirSectors:\t%X";
flash int8 _FF_FDSecStr[] = "\r\nFirstDataSector:\t%X";
flash int8 _FF_RErrStr[] = "\r\nRead ERROR!!!";
flash int8 _FF_InvParTblStr[] = "\r\nInvalid Partition Table Error";
flash int8 _FF_InvBootSecStr[] = "\r\nInvalid Boot Sector";
flash int8 _FF_ResetCMDStr[] = "\r\nReset CMD: ";
flash int8 _FF_OkStr[] = "OK!!!";
flash int8 _FF_Err02Str[] = "ERROR-%02X";
flash int8 _FF_InitStr[] = "\r\nInitialization: ";
flash int8 _FF_CRLFStr[] = "\r\n";
flash int8 _FF_02XStr[] = "%02X ";
#endif
#ifdef _FF_READ_DEBUG_
flash int8 _read_addrStr[] = "\r\nReadADDR[0x%lX]";
#endif
#ifdef _DEBUG_MULTI_BLOCK_
uint32 _BLOCK_ADDRESS_;
flash int8 _FF_StartBlockWriteStr[] = "\r\nStart Block Write";
flash int8 _FF_StartMultiErrStr[] = " - MultiStartError%d-%02X";
flash int8 _FF_MultiWriteStr[] = "\r\nBlock Write @ 0x%lX";
flash int8 _FF_Multi02Str[] = "-%02X-";
flash int8 _FF_MultiEndStr[] = "[MultiEndErr-%d]";
flash int8 _FF_WriteBlockStartStr[] = "\r\nStart Block Write";
flash int8 _FF_WriteBlock02XStr[] = "[%02X-%d]";
flash int8 _FF_WriteMultiStartStr[] = " - MultiStartError%d-%02X";
#endif
#ifdef _FF_WRITE_DEBUG_
flash int8 _FF_WriteAddr_ldXStr[] = "\r\nWriteADDR[0x%lX]";
flash int8 _FF_WriteAddrErrStr[] = "\tWriteErr[%d]";
#endif
/*****************************************************************************
**
** EXPORTED FUNCTIONS
**
*****************************************************************************/
#ifdef _DEBUG_ON_
/****************************************************************************
**
** Debug funtion that gets one character from the UART
**
** Parameters: NONE
**
** Returns: Charater received
**
****************************************************************************/
int16 _FF_getchar(void)
{
while(!(UCSR0A & 0x80))
;
return(UDR0); /* grab character */
}
/****************************************************************************
**
** Debug funtion that sends one character to the UART
**
** Parameters: send_char, character to send
**
** Returns: Sent character
**
****************************************************************************/
int16 _FF_putchar(int16 send_char)
{
while (!(UCSR0A & 0x20))
; /* wait xmit ready */
UDR0 = (uint8) send_char;
return(send_char);
}
/****************************************************************************
**
** Clears the SD/MMC's SPI bus so commands can be sent cleanly.
**
** Parameters: None
**
** Returns: None
**
****************************************************************************/
#if defined(_CVAVR_) || defined(_ICCAVR_) || defined(_ROWLEY_CWAVR_)
void _FF_printf(flash int8 *pstr, ...)
#elif defined(_IAR_EWAVR_)
void _FF_printf(PGM_P pstr, ...)
#endif /*defined(_CVAVR_) || defined(_ICCAVR_)*/
{
va_list arglist;
#ifdef _CVAVR_
int8 temp_buff[_FF_MAX_FPRINTF], *fp;
va_start(arglist, pstr);
vsprintf(temp_buff, pstr, arglist);
#else
int8 temp_buff[_FF_MAX_FPRINTF], *fp;
int8 pstr_sram[_FF_MAX_FPRINTF];
int16 cntr;
cntr = 0;
while((pstr[cntr]) && (cntr <= _FF_MAX_FPRINTF))
{
pstr_sram[cntr] = pstr[cntr];
cntr++;
}
pstr_sram[cntr] = 0;
va_start(arglist, pstr);
vsprintf(temp_buff, pstr_sram, arglist);
#endif
va_end(arglist);
fp = temp_buff;
while(*fp)
{
#ifdef _IAR_EWAVR_
if(_FF_putchar(*fp) == (int16) EOF)
return;
fp++;
#else
if(_FF_putchar(*fp++) == (int16) EOF)
return;
#endif
}
}
#endif /*_DEBUG_ON_*/
/****************************************************************************
**
** Sends one charater while receiving one character on the SPI bus
**
** Parameters: mydata, character to send
**
** Returns: Received character
**
****************************************************************************/
uint8 _FF_spi(uint8 mydata)
{
SPDR = mydata; /* byte 1 */
while((SPSR&0x80) == 0)
;
return(SPDR);
}
/****************************************************************************
**
** This function is required to initialize the Flash card so reading and
** writing to the card is possible. This function also reads the card's
** partition table and boot sector, storing the card information needed to
** handle the file system. No data can be read from or written to the card
** without this function being run first.
**
** Parameters: NONE
**
** Returns: 1 - Card initialized
** 0 - Initialization failed
**
****************************************************************************/
uint8 initialize_media(void)
{
int8 n;
uint32 root_dir_sectors;
#ifndef _BIG_ENDIAN_
uint16 part_sector_start;
#else
HiLo16Union part_sector_start;
#endif
HiLo32Union bpb_tot_sectors;
FAT16BootSectStruct *bpb;
_FF_spi_init();
if(set_SD_mode(SD_SPI)) /* Reset and initialize the card to SPI mode */
return(0);
#ifndef _BYTES_PER_SEC_512_
BPB_BytsPerSec.uval16 = 512; /* Initialize sector size to 512 (all SD cards have a 512 sector size) */
#endif
if(_FF_read(0x0, _FF_Buff))
{
#ifdef _DEBUG_ON_
_FF_printf(_FF_RErrStr);
#endif
_FF_error = INIT_ERR;
return(0);
}
bpb = (FAT16BootSectStruct *) _FF_Buff;
if(bpb->SigWord != SIGNATURE_WORD)
{
/* Location Has to have respective values to be a valid Boot or Partition Sector */
#ifdef _DEBUG_ON_
_FF_printf(_FF_InvParTblStr);
#endif
_FF_error = INV_PARTTABLE_ERR;
return(0);
}
if(((_FF_Buff[0] == 0xEB) && (_FF_Buff[2] == 0x90)) || (_FF_Buff[0] == 0xE9))
#ifndef _BIG_ENDIAN_
part_sector_start = 0; /* Valid Jump, must be Drive */
#else
part_sector_start.uval16 = 0; /* Valid Jump, must be Drive */
#endif
else
{
/* Find valid Partition Entry */
for(n = 0; n < 4; n++)
{
if(bpb->PartEnt[n].Starting_Head)
{
#ifndef _BIG_ENDIAN_
part_sector_start = bpb->PartEnt[n].Starting_Sector.uval16.lo;
#else
part_sector_start.uval8.hi = bpb->PartEnt[n].Starting_Sector.uval8.mh;
part_sector_start.uval8.lo = bpb->PartEnt[n].Starting_Sector.uval8.hi;
#endif
n = 0x10;
}
}
if(!(n & 0x10))
{
#ifndef _BIG_ENDIAN_
part_sector_start = bpb->PartEnt[0].Starting_Sector.uval16.lo;
#else
part_sector_start.uval8.hi = bpb->PartEnt[0].Starting_Sector.uval8.mh;
part_sector_start.uval8.lo = bpb->PartEnt[0].Starting_Sector.uval8.hi;
#endif
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -