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

📄 file_sys.c~

📁 This fat 16 can be used for logging function. The user can use it for logger device.
💻 C~
📖 第 1 页 / 共 5 页
字号:
    #ifdef _BYTES_PER_SEC_512_
        memset(&_FF_Buff[0x40], 0, 448);
    #else
        memset(&_FF_Buff[0x40], 0, (BPB_BytsPerSec.uval16 - 0x40));
    #endif

    path_addr_temp = clust_to_addr(clus_new.uval16);

    _FF_DirAddr = addr_temp;    /* reset dir addr */
    if(_FF_write(path_addr_temp, _FF_Buff))
    {
        #ifdef _DEBUG_MKDIR_
            _FF_printf(_FF_MkDirDebugStr, 8);
        #endif
        return((int16) EOF);
    }

    memset(_FF_Buff, 0, 0x40);

    for(c = 1; c < BPB_SecPerClus; c++)
    {
        /* Clear the rest of the Directory */
        if(_FF_write(path_addr_temp+c, _FF_Buff))
        {
            #ifdef _DEBUG_MKDIR_
                _FF_printf(_FF_MkDirDebugStr, 9);
            #endif
            return((int16) EOF);
        }
    }
    return(0);
}
#endif  /*defined(_DIRECTORIES_SUPPORTED_) && !defined(_READ_ONLY_)*/


#if defined(_DIRECTORIES_SUPPORTED_) && !defined(_READ_ONLY_)
/****************************************************************************
**
** Removes a directory on the card if it is empty.
**
** Parameters:  *f_path, pointer to the string of the directory name/path
**
** Returns:  0 - Working directory changed successfully
**          -1 - an error occured
**
****************************************************************************/
int16 rmdir(int8 *f_path)
{
    int8 *sp;
    int8 fpath[14];
    uint16 n, i;
    HiLo16Union clus_temp;
    FileDirEntryStruct *dep;
    uint32 addr_temp, path_addr_temp, scan_addr_temp;
    uint16 ent_max, ent_cntr, dir_clus;
    #ifndef _BYTES_PER_SEC_512_
        uint16 EntPerSec;

        EntPerSec = BPB_BytsPerSec.uval16 >> 5;        /* same as divide by 32 */
    #endif

    addr_temp = _FF_DirAddr;    /* save local dir addr */
    if(_FF_checkdir(f_path, fpath))
    {
        _FF_DirAddr = addr_temp;
        return((int16) EOF);
    }
    if(fpath[0]==0)
    {
        _FF_DirAddr = addr_temp;
        return((int16) EOF);
    }

    path_addr_temp = _FF_DirAddr;    /* save addr for later */

    if(_FF_chdir(fpath))    /* Change the dir_addr to dir to the one that we are trying to delete */
    {
        /* if Failed, this is not a directory */
        _FF_DirAddr = addr_temp;
        return((int16) EOF);
    }
    if((_FF_DirAddr == _FF_RootAddr) || (_FF_DirAddr == addr_temp))
    {
        /* if trying to delete root, or current dir error */
        _FF_DirAddr = addr_temp;
        return((int16) EOF);
    }

    /* sub-directory */
    dir_clus = addr_to_clust(_FF_DirAddr);     /* find the cluster number of this address */
    if(dir_clus == 0)
    {
        _FF_DirAddr = addr_temp;
        return((int16) EOF);
    }
    #ifdef _BYTES_PER_SEC_512_
        /* ent_max should be (BytsPerSec/BytsPerEntry)*(SecPerClus) */
        ent_max = (uint16) BPB_SecPerClus << 4;
    #else
        /* ent_max should be (BytsPerSec/BytsPerEntry)*(SecPerClus) */
        ent_max = (BPB_BytsPerSec.uval16 / EntPerSec) * (uint16) BPB_SecPerClus;
    #endif

    scan_addr_temp = _FF_DirAddr;
    ent_cntr = 0;
    while(ent_cntr < ent_max)
    {
        if(_FF_read(scan_addr_temp, _FF_Buff))
        {
            /* read error */
            _FF_DirAddr = addr_temp;
            return(0);
        }
        dep = (FileDirEntryStruct *) _FF_Buff;      /* set directory entry pointer to the beginning of the entry buffer */

        #ifdef _BYTES_PER_SEC_512_
            for(n = 0; n < 16; n++)
        #else
            for(n = 0; n < EntPerSec; n++)
        #endif
            {
                sp = ((dep + n)->name_entry);
                if(*sp == 0)
                {
                    ent_cntr = ent_max;
                    break;
                }
                while(valid_file_char(*sp) != 0)
                {
                    /* character is a valid file char */
                    sp++;
                    if(sp == (int8 *) &(dep->attr))
                    {
                        /* a valid file or folder found */
                        _FF_DirAddr = addr_temp;
                        return((int16) EOF);
                    }
                }
            }

        scan_addr_temp++;
        if(ent_cntr == ent_max)
        {
            i = next_cluster(dir_clus, SINGLE);     /* find the next cluster of this folder */
            if(i==0)
            {
                /* this cluster should never point to cluster 0 */
                _FF_DirAddr = addr_temp;
                return(0);
            }
            else if(i >= 0xFFF5)
            {
                /* points to EOF */
                break;
            }
            else
            {
                /* another dir cluster exists, set address to it and restart*/
                scan_addr_temp = clust_to_addr(i);
                if(scan_addr_temp == 0)
                    return((int16) EOF);
                dir_clus = i;               /* set new dir_clus */
                ent_cntr = 0;               /* reset entry count */
                i = 0;                      /* reset sector count */
            }
        }
    }

    /* directory empty, delete dir */
    _FF_DirAddr = path_addr_temp;    /* go back to previous directory */

    dep = scan_directory(fpath, SCAN_DIR_R);        /* Find directory */

    _FF_DirAddr = addr_temp;    /* reset address */

    /* no more entries, An Error Occurred            /* No file found */
    if(dep == 0)
        return((int16) EOF);
    if(dep->attr & ATTR_READ_ONLY)
        return((int16) EOF);

    #ifndef _BIG_ENDIAN_
        clus_temp.uval16 = dep->start_clus_lo;
    #else
        clus_temp.uval8.hi = dep->start_clus_lo.uval8.lo;
        clus_temp.uval8.lo = dep->start_clus_lo.uval8.hi;
    #endif
    dep->name_entry[0] = 0xE5;

    if(_FF_write(path_addr_temp, _FF_Buff))
        return((int16) EOF);
    if(erase_clus_chain(clus_temp.uval16))
        return((int16) EOF);

    return(0);
}
#endif  /*defined(_DIRECTORIES_SUPPORTED_) && !defined(_READ_ONLY_)*/


#if defined(_DIRECTORIES_SUPPORTED_)
/****************************************************************************
**
** This function changes the working directory path for the File System.  
** _FF_DirAddr is changed to how the input string directs it.  A NULL is 
** returned upon success, or an EOF with no changes for a Failure. 
**
** Parameters:  *f_path, pointer to the string of the directory name/path
**
** Returns:  0 - Working directory changed successfully
**          -1 - an error occured
**
****************************************************************************/
#if defined(_CVAVR_) || defined(_ICCAVR_) || defined(_ROWLEY_CWAVR_)
int16 chdirc(flash int8 *f_path)
#elif defined(_IAR_EWAVR_)
int16 chdirc(PGM_P f_path)
#endif
{
    int8 temp_path[_FF_PATH_LENGTH];

    _FF_strcpyf(temp_path, f_path);

    return(chdir(temp_path));
}

/****************************************************************************
**
** This function changes the working directory path for the File System.  
** _FF_DirAddr is changed to how the input string directs it.  A NULL is 
** returned upon success, or an EOF with no changes for a Failure. 
**
** Parameters:  *f_path, pointer to the string of the directory name/path
**
** Returns:  0 - Working directory changed successfully
**          -1 - an error occured
**
****************************************************************************/
int16 chdir(int8 *f_path)
{
    int8 *qp, *sp, fpath[14];
    #ifdef _CVAVR_
        int8 c;
    #endif
    #ifdef _DEBUG_CHDIR_
        uint8 cnt;
    #endif
    uint32 addr_temp;

    addr_temp = _FF_DirAddr;    /* save initial dir addr */

    if(_FF_checkdir(f_path, fpath))
    {
        _FF_DirAddr = addr_temp;
        #ifdef _DEBUG_CHDIR_
            _FF_printf(_FF_ChDirDebugStr, 1);
        #endif
        return((int16) EOF);
    }
    if(fpath[0])
    {
        if(_FF_chdir(fpath))    /* Change the dir_addr to dir to the one that we are trying to delete */
        {
            /* if Failed, this is not a directory */
            _FF_DirAddr = addr_temp;
            #ifdef _DEBUG_CHDIR_
                _FF_printf(_FF_ChDirDebugStr, 2);
            #endif
            return((int16) EOF);
        }
    }

    sp = f_path;
    if(*sp == '\\')
    {
        _FF_FullPath[1] = 0;
        sp++;
    }

    #ifdef _DEBUG_CHDIR_
        cnt = 0;
    #endif
    while(*sp)
    {
        #ifdef _DEBUG_CHDIR_
            _FF_printf(_FF_ChDirDebugStr, 5 + (cnt++));
        #endif
        qp = &_FF_FullPath[_FF_strlen(_FF_FullPath)];
        if((sp[0] == '.') && (sp[1] == '.'))
        {
            /* go back a directory */
            #if defined(_ICCAVR_) || defined(_IAR_EWAVR_) || defined(_ROWLEY_CWAVR_)
                qp = _FF_strrchr(_FF_FullPath, '\\');
                if(qp == NULL)
                {
                    _FF_DirAddr = addr_temp;
                  #ifdef _DEBUG_CHDIR_
                    _FF_printf(_FF_ChDirDebugStr, 3);
                  #endif
                    return((int16) EOF);
                }
                *qp = 0;        /* take off the '\' so nex reverse compare works */
                qp = _FF_strrchr(_FF_FullPath, '\\') + 1;
                if(qp == NULL)
                {
                    _FF_DirAddr = addr_temp;
                  #ifdef _DEBUG_CHDIR_
                    _FF_printf(_FF_ChDirDebugStr, 4);
                  #endif
                    return((int16) EOF);
                }
                *qp = 0;        /* new termination */
            #elif defined(_CVAVR_)
                #pragma warn-
                c = strrpos(_FF_FullPath, '\\');
                if(c<0)
                {
                    _FF_DirAddr = addr_temp;
                    #ifdef _DEBUG_CHDIR_
                        _FF_printf(_FF_ChDirDebugStr, 5);
                    #endif
                    return((int16) EOF);
                }
                _FF_FullPath[c] = 0;
                c = strrpos(_FF_FullPath, '\\');
                if(c < 0)
                {
                    _FF_DirAddr = addr_temp;
                    #ifdef _DEBUG_CHDIR_
                        _FF_printf(_FF_ChDirDebugStr, 6);
                    #endif
                    return((int16) EOF);
                }
                _FF_FullPath[c+1] = 0;
                #pragma warn+
            #endif  /*defined(_CVAVR_)*/
            sp += 2;
        }
        else
        {

⌨️ 快捷键说明

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