📄 sma_fat16_private.c
字号:
/***********************************************************************
* $Workfile: SMA_fat16_private.c $
* $Revision: 1.0 $
* $Author: WellsK $
* $Date: Aug 27 2002 08:43:34 $
*
* Project: FAT16 support functions
*
* Description:
* See the SMA_fat16_private.h header file for a description of this
* package.
*
* This package uses *malloc*. If you use this package and need dynamic
* memory allocation, be sure to replace malloc with your own function.
*
* Revision History:
* $Log: //smaicnt2/pvcs/VM/CHIPS/archives/SOC/Source/File Systems/FAT/SMA_fat16_private.c-arc $
*
* Rev 1.0 Aug 27 2002 08:43:34 WellsK
* Initial revision.
*
*
* SHARP MICROELECTRONICS OF THE AMERICAS MAKES NO REPRESENTATION
* OR WARRANTIES WITH RESPECT TO THE PERFORMANCE OF THIS SOFTWARE,
* AND SPECIFICALLY DISCLAIMS ANY RESPONSIBILITY FOR ANY DAMAGES,
* SPECIAL OR CONSEQUENTIAL, CONNECTED WITH THE USE OF THIS SOFTWARE.
*
* SHARP MICROELECTRONICS OF THE AMERICAS PROVIDES THIS SOFTWARE SOLELY
* FOR THE PURPOSE OF SOFTWARE DEVELOPMENT INCORPORATING THE USE OF A
* SHARP MICROCONTROLLER OR SYSTEM-ON-CHIP PRODUCT. USE OF THIS SOURCE
* FILE IMPLIES ACCEPTANCE OF THESE CONDITIONS.
*
* COPYRIGHT (C) 2002 SHARP MICROELECTRONICS OF THE AMERICAS, INC.
* CAMAS, WA
**********************************************************************/
#include "SMA_types.h"
#include "SMA_fat16_private.h"
#include <stdlib.h>
//**********************************************************************
// Private functions
//**********************************************************************
/***********************************************************************
*
* Function: fat16_moveto
*
* Purpose:
* Simple data movement routine.
*
* Processing:
* Move a number of bytes from the source to destination.
*
* Parameters:
* source : Source address
* dest : Destination address
* size : Number of bytes to move
*
* Outputs:
* Data pointed to by source will be updated.
*
* Returns:
* Nothing
*
* Notes:
* None
*
**********************************************************************/
void fat16_moveto (void *source, void *dest, INT_32 size)
{
UNS_8 *cdest, *csource;
cdest = (UNS_8 *) dest;
csource = (UNS_8 *) source;
while (size > 0)
{
*cdest = *csource;
cdest++;
csource++;
size--;
}
}
/***********************************************************************
*
* Function: fat16_compare
*
* Purpose:
* Simple data comparison routine.
*
* Processing:
* Two strings are compared in lowercase up to the number of
* characters set by 'size'.
*
* Parameters:
* source : Source address
* dest : Destination address
* size : Number of characters to compare
*
* Outputs:
* Nothing
*
* Returns:
* '1' if the strings are the same, '0' otherwise
*
* Notes:
* None
*
**********************************************************************/
INT_32 fat16_compare (CHAR *source, CHAR *dest, INT_32 size)
{
INT_32 index;
INT_32 match = -1;
CHAR sr, ds;
index = 0;
while (match == -1)
{
sr = source [index];
ds = dest [index];
// Convert to lowercase for the comparison
if ((sr >= 'A') && (sr <= 'Z'))
{
sr = sr - 'A' + 'a';
}
if ((ds >= 'A') && (ds <= 'Z'))
{
ds = ds - 'A' + 'a';
}
if (sr == ds)
{
// Look for string end
if (index == (size - 1))
{
// Comparison matches
match = 1;
}
else
{
index++;
}
}
else
{
// No match
match = 0;
}
}
return match;
}
/***********************************************************************
*
* Function: fat16_wait_busy
*
* Purpose:
* Wait for the device to go 'unbusy'.
*
* Processing:
* Check the status of the device busy function. If the device is
* busy, perform a small loop and check again until the device is no
* longer busy.
*
* Parameters:
* fat_data : Pointer to a device data structure.
*
* Outputs:
* None
*
* Returns:
* Nothing
*
* Notes:
* None
*
**********************************************************************/
void fat16_wait_busy (fat_device_type *fat_data)
{
INT_32 i;
while (fat_data->func.busy_ck_func () == 1)
{
// Loop to prevent excessive bus usage
for (i = 0; i < 1000; i++);
}
}
/***********************************************************************
*
* Function: fat16_translate_cluster_to_sector
*
* Purpose:
* Translate a cluster number to a (absolute) sector number.
*
* Processing:
* See function.
*
* Parameters:
* fat_data : Pointer to a device data structure
* cluster : Cluster number
*
* Outputs:
* None
*
* Returns:
* An absolute sector number.
*
* Notes:
* None
*
**********************************************************************/
UNS_32 fat16_translate_cluster_to_sector (fat_device_type *fat_data,
UNS_16 cluster)
{
return (UNS_32) ((UNS_32) fat_data->cfat.first_data_sector +
(cluster - CLUSTERU_MIN) *
(UNS_32) fat_data->pat_hdr.sectors_cluster);
}
/***********************************************************************
*
* Function: fat16_read_mbr
*
* Purpose:
* Reads the FAT MBR and puts the partition tables in the passed
* structure.
*
* Processing:
* Read CHS (0, 0, 1) from the device (this is always the MBR in a
* storage device). Copy the partition data from the device data
* into the partition data table. Set the selected active partition
* to (-1), indicating that a partition has not been selected.
*
* Parameters:
* fat_data : Pointer to a device data structure.
*
* Outputs:
* Data in fat_data will be updated.
*
* Returns:
* Nothing
*
* Notes:
* None
*
**********************************************************************/
void fat16_read_mbr (fat_device_type *fat_data)
{
UNS_8 data [PTAB_SIZE];
// Read head 0, cylinder 0, sector 1 (MBR sector 0 absolute)
fat_data->func.set_sector_func (0);
// Issue read command
fat_data->func.start_read_func ();
// Wait for the operation to complete
fat16_wait_busy (fat_data);
// Read the MBR data from the device (PTAB_SIZE bytes)
fat_data->func.read_func (data, PTAB_SIZE);
// Strip out the 4 partition records into the FAT structure
fat16_moveto (&data [0x1BE], fat_data->part,
(4 * sizeof (partition_type)));
// On a fresh MBR read, the active partition is invalidated
fat_data->act_part = -1;
}
/***********************************************************************
*
* Function: fat16_read_sectors
*
* Purpose:
* Reads a number of sectors from a device into a buffer.
*
* Processing:
* See function.
*
* Parameters:
* fat_data : Pointer to a device data structure
* data : Pointer to data buffer to fill
* first_sector : Starting absolute sector to read
* num_sectors : Number of sectors to read
*
* Outputs:
* None
*
* Returns:
* Nothing
*
* Notes:
* None
*
**********************************************************************/
void fat16_read_sectors (fat_device_type *fat_data, void *data,
UNS_32 first_sector, UNS_32 num_sectors)
{
INT_32 index = 0;
UNS_8 *data8 = (UNS_8 *) data;
while (num_sectors > 0)
{
// Set sector number in device
fat_data->func.set_sector_func (first_sector);
// Issue a read command to the device
fat_data->func.start_read_func ();
// Wait for operation to complete
fat16_wait_busy (fat_data);
// Read the sector data and update buffer index
index = index + fat_data->func.read_func (&data8 [index],
(INT_32) fat_data->pat_hdr.bytes_sector);
// Update counters
first_sector++;
num_sectors--;
}
}
/***********************************************************************
*
* Function: fat16_write_sectors
*
* Purpose:
* Writes a number of sectors from a buffer to a device.
*
* Processing:
* See function.
*
* Parameters:
* fat_data : Pointer to a device data structure
* data : Pointer to data buffer to copy from
* first_sector : Starting absolute sector to write
* num_sectors : Number of sectors to write
*
* Outputs:
* None
*
* Returns:
* Nothing
*
* Notes:
* None
*
**********************************************************************/
void fat16_write_sectors (fat_device_type *fat_data, void *data,
UNS_32 first_sector, UNS_32 num_sectors)
{
INT_32 index = 0;
UNS_8 *data8 = (UNS_8 *) data;
while (num_sectors > 0)
{
// Set sector number in device
fat_data->func.set_sector_func (first_sector);
// Issue write command
fat_data->func.start_write_func ();
// Wait for operation to complete before writing data
fat16_wait_busy (fat_data);
// Put data in the write buffer
index = index + fat_data->func.write_func (&data8 [index],
(INT_32) fat_data->pat_hdr.bytes_sector);
// Wait for write to complete
fat16_wait_busy (fat_data);
// Update counters
first_sector++;
num_sectors--;
}
}
/***********************************************************************
*
* Function: fat16_parse_path
*
* Purpose:
* Finds the next directory name in a path.
*
* Processing:
* See function.
*
* Parameters:
* path : a path name string
*
* Outputs:
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -