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

📄 file_sys.c~

📁 This fat 16 can be used for logging function. The user can use it for logger device.
💻 C~
📖 第 1 页 / 共 5 页
字号:
                        /* another dir cluster exists */
                        dir_addr = clust_to_addr(m);
                        dir_clus = m;
                        d = 0;
                        m = 0;
                    }
                    else
                        break;
                }
            }
        #endif
    }
    _FF_putchar('\r');
    _FF_putchar('\n');
}
#endif


/****************************************************************************
**
** This function prints the Flash media's serial number and volume label to 
** the serial port (UART/USART must be properly set up) of the processor.  
** (Note: The volume serial number and label can also be accessed through the 
** global variables located in the "sd_cmd.c" file once "initialize_media()" 
** is called.  The serial number is stored as the unsigned long BP_VolID, and 
** the volume label is stored in the character string BP_VolLabel[12])
**
** Parameters: None
**
** Returns: None
**
****************************************************************************/
void GetVolID(void)
{
    #ifndef _BIG_ENDIAN_
        _FF_printf(VolSerialStr, BS_VolSerial);
    #else
        _FF_printf(VolSerialStr, BS_VolSerial.uval32);
    #endif
    _FF_printf(VolLabelStr, BS_VolLab);
}


/****************************************************************************
**
** Function that tells you the number of available clusters left on the disk 
**
** Parameters: None
**
** Returns: The number of free clusters
**
****************************************************************************/
uint16 available_clusters(void)
{
    uint16 i, cnt;

    cnt = 0;

    for(i = 2; i < (DataClusTot); i++)
    {
        if(next_cluster(i, CHAIN_R) == 0)
            cnt++;
    }

    return(cnt);
}


#ifndef _READ_ONLY_
/****************************************************************************
**
** Quickformat of the media (erase cluster table and root directory)
**
** Parameters: None
**
** Returns:  0 - Media successfully formatted
**          -1 - an error occured
**
****************************************************************************/
int16 fquickformat(void)
{
    uint32 addr, addr_chk;

  #ifdef _BYTES_PER_SEC_512_
    memset(_FF_Buff, 0, 512);
  #else
    memset(_FF_Buff, 0, BPB_BytsPerSec.uval16);
  #endif

    addr = (uint32) (_FF_Fat1Addr + 1);
  #ifdef _BYTES_PER_SEC_512_
    addr_chk = _FF_RootAddr + (((uint32) BPB_RootEntCnt.uval16 + 15) / 16);
  #else
    addr_chk = _FF_RootAddr + ((((uint32) BPB_RootEntCnt.uval16 * BPB_BytsPerSec.uval16) + 31) / 0x20);
  #endif
    while(addr < addr_chk)
    {
        if(_FF_write(addr, _FF_Buff))
        {
            _FF_error = WRITE_ERR;
            return((int16) EOF);
        }
        addr++;
    }
    _FF_Buff[0] = 0xF8;
    _FF_Buff[1] = 0xFF;
    _FF_Buff[2] = 0xFF;
    if(BPB_FATType == 0x36)
        _FF_Buff[3] = 0xFF;
    if((_FF_write((uint32) _FF_Fat1Addr, _FF_Buff)) || (_FF_write(_FF_Fat2Addr, _FF_Buff)))
    {
        _FF_error = WRITE_ERR;
        return((int16) EOF);
    }
    if(initialize_media())
        return(0);

    return((int16) EOF);
}
#endif


#if defined(_DIRECTORIES_SUPPORTED_) && !defined(_READ_ONLY_)
/****************************************************************************
**
** Makes a directory
**
** Parameters:  *f_path, pointer to the string of the directory name/path
**
** Returns:  0 - Directory created successfully
**          -1 - an error occured
**
****************************************************************************/
int16 mkdir(int8 *f_path)
{
    uint8 c, c2;
    int8 fpath[14], fpath_new[14];
    HiLo16Union clus_new, clus_old;
    FileDirEntryStruct *dep;
    HiLo16Union calc_time, calc_date;
    uint32 addr_temp, path_addr_temp;

    addr_temp = _FF_DirAddr;    /* save local dir addr */
    if(_FF_checkdir(f_path, fpath))
    {
        _FF_DirAddr = addr_temp;
        #ifdef _DEBUG_MKDIR_
            _FF_printf(_FF_MkDirDebugStr, 1);
        #endif
        return((int16) EOF);
    }
    if(fpath[0] == 0)
    {
        _FF_error = NAME_ERR;
        _FF_DirAddr = addr_temp;
        #ifdef _DEBUG_MKDIR_
            _FF_printf(_FF_MkDirDebugStr, 2);
        #endif
        return(NULL);
    }

    dep = scan_directory(fpath, SCAN_DIR_W);        /* Find directory space */
    if(dep == 0)
    {
        /* Entry already exists or no space available */
        _FF_DirAddr = addr_temp;
        #ifdef _DEBUG_MKDIR_
            _FF_printf(_FF_MkDirDebugStr, 3);
        #endif
        return((int16) EOF);
    }

    /* Get the filename into a form we can use to compare */
    if(file_name_conversion(fpath, fpath_new))
    {
        _FF_error = NAME_ERR;
        _FF_DirAddr = addr_temp;
        #ifdef _DEBUG_MKDIR_
            _FF_printf(_FF_MkDirDebugStr, 4);
        #endif
        return((int16) EOF);
    }

    clus_new.uval16 = prev_cluster(0);          /* find the next avaliable cluster for new directory */
    if(_FF_read(_FF_ScanAddr, _FF_Buff))        /* refresh the buffer for making a dir */
    {
        _FF_DirAddr = addr_temp;
        #ifdef _DEBUG_MKDIR_
            _FF_printf(_FF_MkDirDebugStr, 5);
        #endif
        return((int16) EOF);
    }

    /* Write folder information in current directory */
    memcpy(dep->name_entry, fpath_new, 11);     /* Write Filename */
    dep->attr = ATTR_DIRECTORY;                 /* Attribute bit auto set to "DIRECTORY" */
    dep->reserved = 0;                          /* Reserved for WinNT */
    dep->crt_tenth_sec = 0;                     /* Mili-second stamp for create */
    #if !defined(_BIG_ENDIAN_)
        /* Little Endian */
        #if defined(_RTC_ON_)
            rtc_get_timeNdate(&RTCHour, &RTCMin, &RTCSec, &RTCDay, &RTCMonth, &RTCYear);
            dep->crt_time = RTCHour;
            dep->crt_time <<= 6;
            dep->crt_time |= RTCMin;
            dep->crt_time <<= 5;
            dep->crt_time |= (RTCSec >> 1);
            dep->crt_date = RTCYear - 1980;
            dep->crt_date <<= 4;
            dep->crt_date |= RTCMonth;
            dep->crt_date <<= 5;
            dep->crt_date |= RTCDay;
            dep->acc_date = dep->crt_date;                /* File access date */
            dep->mod_date = dep->crt_date;                /* File modify date */
            dep->mod_time = dep->crt_time;                /* File modify time */
        #else   /*defined(_RTC_ON_)*/
            /* Set to 01/01/1980 00:00:00 */
            /* date_word=0x0021         time_word=0x0000 */
            dep->crt_date = CREATE_DATE_VLAUE;  /* File create date */
            dep->crt_time = 0;                  /* File create time */
            dep->acc_date = CREATE_DATE_VLAUE;  /* File access date */
            dep->mod_date = CREATE_DATE_VLAUE;  /* File modify date */
            dep->mod_time = 0;                  /* File modify time */
        #endif  /*defined(_RTC_ON_)*/
        dep->start_clus_hi = 0;                 /* 0 for FAT12/16 (2 bytes) */
        dep->start_clus_lo = clus_new.uval16;   /* Starting cluster (2 bytes) */
        dep->file_size = 0;                     /* File length (0 for new) */
        calc_time.uval16 = dep->crt_time;       /* Save for dir entry, OK backwards for BIG_ENDIAN */
        calc_date.uval16 = dep->crt_date;       /* Save for dir entry, OK backwards for BIG_ENDIAN */
    #else   /*!defined(_BIG_ENDIAN_)*/
        /* Big Endian */
        #if defined(_RTC_ON_)
            rtc_get_timeNdate(&RTCHour, &RTCMin, &RTCSec, &RTCDay, &RTCMonth, &RTCYear);
            dep->crt_time.uval8.lo = RTCHour;
            dep->crt_time.uval16 <<= 6;
            dep->crt_time.uval8.lo |= RTCMin;
            dep->crt_time.uval16 <<= 5;
            dep->crt_time.uval8.lo |= (RTCSec >> 1);
            dep->crt_date.uval16 = RTCYear - 1980;
            dep->crt_date.uval16 <<= 4;
            dep->crt_date.uval8.lo |= RTCMonth;
            dep->crt_date.uval16 <<= 5;
            dep->crt_date.uval8.lo |= RTCDay;
            dep->mod_date.uval8.lo = dep->crt_date.uval8.hi;                /* File modify date */
            dep->mod_date.uval8.hi = dep->crt_date.uval8.lo;                /* File modify date */
            dep->mod_time.uval8.lo = dep->crt_time.uval8.hi;                /* File modify time */
            dep->mod_time.uval8.hi = dep->crt_time.uval8.lo;                /* File modify time */
            dep->acc_date.uval16 = dep->mod_date.uval16;
            dep->crt_date.uval16 = dep->mod_date.uval16;
            dep->crt_time.uval16 = dep->mod_time.uval16;
        #else   /*defined(_RTC_ON_)*/
            /* Set to 01/01/1980 00:00:00 */
            /* date_word=0x2100         time_word=0x0000 */
            dep->crt_date.uval16 = CREATE_DATE_VLAUE;       /* File create date */
            dep->crt_time.uval16 = 0;                       /* File create time */
            dep->acc_date.uval16 = CREATE_DATE_VLAUE;       /* File access date */
            dep->mod_date.uval16 = CREATE_DATE_VLAUE;       /* File modify date */
            dep->mod_time.uval16 = 0;                       /* File modify time */
        #endif  /*defined(_RTC_ON_)*/
        dep->start_clus_hi = 0;                             /* 0 for FAT12/16 (2 bytes) */
        dep->start_clus_lo.uval8.lo = clus_new.uval8.hi;    /* Starting cluster (2 bytes) */
        dep->start_clus_lo.uval8.hi = clus_new.uval8.lo;
        dep->file_size.uval32 = 0;                          /* File length (0 for new) */
        calc_time.uval16 = dep->crt_time.uval16;            /* Save for dir entry, OK backwards for BIG_ENDIAN */
        calc_date.uval16 = dep->crt_date.uval16;            /* Save for dir entry, OK backwards for BIG_ENDIAN */
    #endif

    if(_FF_write(_FF_ScanAddr, _FF_Buff))                   /* write entry to card */
    {
        _FF_DirAddr = addr_temp;
      #ifdef _DEBUG_MKDIR_
        _FF_printf(_FF_MkDirDebugStr, 6);
      #endif
        return((int16) EOF);
    }
    if(write_clus_table(clus_new.uval16, 0xFFFF, SINGLE))
    {
        _FF_DirAddr = addr_temp;
        #ifdef _DEBUG_MKDIR_
            _FF_printf(_FF_MkDirDebugStr, 7);
        #endif
        return((int16) EOF);
    }
    dep = (FileDirEntryStruct *) _FF_Buff;
    clus_old.uval16 = addr_to_clust(_FF_DirAddr);       /* find the cluster number of this address */
    for(c2 = 1; c2 < 3; c2++)
    {
        for(c = 0; c < c2; c++)
            dep->name_entry[c] = '.';                   /* set name */
        for(c = c2; c < 11; c++)
            dep->name_entry[c] = 0x20;                  /* set name */
        dep->attr = ATTR_DIRECTORY;                     /* set to a directory */
        dep->reserved = 0;                              /* set reserved to 0 */
        dep->crt_tenth_sec = 0;                         /* set milisec stamp to 0 */
        #if !defined(_BIG_ENDIAN_)
            dep->crt_time = calc_time.uval16;           /* set create time */
            dep->crt_date = calc_date.uval16;           /* set create date */
            dep->acc_date = calc_date.uval16;           /* set access date */
            dep->mod_time = calc_time.uval16;           /* set modify time */
            dep->mod_date = calc_date.uval16;           /* set modify date */
        #else
            dep->crt_time.uval16 = calc_time.uval16;    /* set create time */
            dep->crt_date.uval16 = calc_date.uval16;    /* set create date */
            dep->acc_date.uval16 = calc_date.uval16;    /* set access date */
            dep->mod_time.uval16 = calc_time.uval16;    /* set modify time */
            dep->mod_date.uval16 = calc_date.uval16;    /* set modify date */
        #endif
        dep->start_clus_hi = 0;                         /* 0 for FAT12/16 */
        if(c2 == 1)
        {
            #ifndef _BIG_ENDIAN_
                /* Starting cluster (2 bytes) */
                dep->start_clus_lo = clus_new.uval16;
            #else
                /* Starting cluster (2 bytes) */
                dep->start_clus_lo.uval8.lo = clus_new.uval8.hi;
                dep->start_clus_lo.uval8.hi = clus_new.uval8.lo;
            #endif
        }
        else
        {
            #ifndef _BIG_ENDIAN_
                /* Starting cluster (2 bytes) */
                dep->start_clus_lo = clus_old.uval16;
            #else
                /* Starting cluster (2 bytes) */
                dep->start_clus_lo.uval8.lo = clus_old.uval8.hi;
                dep->start_clus_lo.uval8.hi = clus_old.uval8.lo;
            #endif
        }
        #ifndef _BIG_ENDIAN_
            /* File length (0 for new) */
            dep->file_size = 0;
        #else
            /* File length (0 for new) */
            dep->file_size.uval32 = 0;
        #endif
        dep++;
    }

⌨️ 快捷键说明

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