CARPLAY版本整理
This commit is contained in:
@ -0,0 +1,449 @@
|
||||
/*
|
||||
* FreeRTOS+FAT V2.3.3
|
||||
* Copyright (C) 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
* this software and associated documentation files (the "Software"), to deal in
|
||||
* the Software without restriction, including without limitation the rights to
|
||||
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
||||
* the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
* subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
||||
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*
|
||||
* https://www.FreeRTOS.org
|
||||
* https://github.com/FreeRTOS
|
||||
*
|
||||
*/
|
||||
|
||||
/* Standard includes. */
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdio.h>
|
||||
|
||||
/* Scheduler include files. */
|
||||
#include "FreeRTOS.h"
|
||||
#include "task.h"
|
||||
#include "semphr.h"
|
||||
#include "portmacro.h"
|
||||
|
||||
/* FreeRTOS+FAT includes. */
|
||||
#include "ff_headers.h"
|
||||
#include "ff_ramdisk.h"
|
||||
#include "ff_sys.h"
|
||||
|
||||
#define ramHIDDEN_SECTOR_COUNT 8
|
||||
#define ramPRIMARY_PARTITIONS 1
|
||||
#define ramHUNDRED_64_BIT 100ULL
|
||||
#define ramSECTOR_SIZE 512UL
|
||||
#define ramPARTITION_NUMBER 0 /* Only a single partition is used. */
|
||||
#define ramBYTES_PER_KB ( 1024ull )
|
||||
#define ramSECTORS_PER_KB ( ramBYTES_PER_KB / 512ull )
|
||||
|
||||
/* Used as a magic number to indicate that an FF_Disk_t structure is a RAM
|
||||
* disk. */
|
||||
#define ramSIGNATURE 0x41404342
|
||||
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
/*
|
||||
* The function that writes to the media - as this is implementing a RAM disk
|
||||
* the media is just a RAM buffer.
|
||||
*/
|
||||
static int32_t prvWriteRAM( uint8_t * pucBuffer,
|
||||
uint32_t ulSectorNumber,
|
||||
uint32_t ulSectorCount,
|
||||
FF_Disk_t * pxDisk );
|
||||
|
||||
/*
|
||||
* The function that reads from the media - as this is implementing a RAM disk
|
||||
* the media is just a RAM buffer.
|
||||
*/
|
||||
static int32_t prvReadRAM( uint8_t * pucBuffer,
|
||||
uint32_t ulSectorNumber,
|
||||
uint32_t ulSectorCount,
|
||||
FF_Disk_t * pxDisk );
|
||||
|
||||
#ifndef RAMDISK_ALREADY_PARTITIONED
|
||||
/*
|
||||
* This is the driver for a RAM disk. Unlike most media types, RAM disks are
|
||||
* volatile so are created anew each time the system is booted. As the disk is
|
||||
* new and just created, it must also be partitioned and formatted.
|
||||
*/
|
||||
static FF_Error_t prvPartitionAndFormatDisk( FF_Disk_t * pxDisk );
|
||||
#endif
|
||||
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
/* This is the prototype of the function used to initialise the RAM disk driver.
|
||||
* Other media drivers do not have to have the same prototype.
|
||||
*
|
||||
* In this example:
|
||||
+ pcName is the name to give the disk within FreeRTOS+FAT's virtual file system.
|
||||
+ pucDataBuffer is the start of the RAM to use as the disk.
|
||||
+ ulSectorCount is effectively the size of the disk, each sector is 512 bytes.
|
||||
+ xIOManagerCacheSize is the size of the IO manager's cache, which must be a
|
||||
+ multiple of the sector size, and at least twice as big as the sector size.
|
||||
*/
|
||||
FF_Disk_t * FF_RAMDiskInit( char * pcName,
|
||||
uint8_t * pucDataBuffer,
|
||||
uint32_t ulSectorCount,
|
||||
size_t xIOManagerCacheSize )
|
||||
{
|
||||
FF_Error_t xError;
|
||||
FF_Disk_t * pxDisk = NULL;
|
||||
FF_CreationParameters_t xParameters;
|
||||
|
||||
/* Check the validity of the xIOManagerCacheSize parameter. */
|
||||
configASSERT( ( xIOManagerCacheSize % ramSECTOR_SIZE ) == 0 );
|
||||
configASSERT( ( xIOManagerCacheSize >= ( 2 * ramSECTOR_SIZE ) ) );
|
||||
|
||||
/* Attempt to allocated the FF_Disk_t structure. */
|
||||
pxDisk = ( FF_Disk_t * ) pvPortMalloc( sizeof( FF_Disk_t ) );
|
||||
|
||||
if( pxDisk != NULL )
|
||||
{
|
||||
/* Start with every member of the structure set to zero. */
|
||||
memset( pxDisk, '\0', sizeof( FF_Disk_t ) );
|
||||
|
||||
/* Clear the entire space. */
|
||||
#ifndef RAMDISK_ALREADY_PARTITIONED
|
||||
memset( pucDataBuffer, '\0', ulSectorCount * ramSECTOR_SIZE );
|
||||
#endif
|
||||
|
||||
/* The pvTag member of the FF_Disk_t structure allows the structure to be
|
||||
* extended to also include media specific parameters. The only media
|
||||
* specific data that needs to be stored in the FF_Disk_t structure for a
|
||||
* RAM disk is the location of the RAM buffer itself - so this is stored
|
||||
* directly in the FF_Disk_t's pvTag member. */
|
||||
pxDisk->pvTag = ( void * ) pucDataBuffer;
|
||||
|
||||
/* The signature is used by the disk read and disk write functions to
|
||||
* ensure the disk being accessed is a RAM disk. */
|
||||
pxDisk->ulSignature = ramSIGNATURE;
|
||||
|
||||
/* The number of sectors is recorded for bounds checking in the read and
|
||||
* write functions. */
|
||||
pxDisk->ulNumberOfSectors = ulSectorCount;
|
||||
|
||||
/* Create the IO manager that will be used to control the RAM disk. */
|
||||
memset( &xParameters, '\0', sizeof( xParameters ) );
|
||||
xParameters.pucCacheMemory = NULL;
|
||||
xParameters.ulMemorySize = xIOManagerCacheSize;
|
||||
xParameters.ulSectorSize = ramSECTOR_SIZE;
|
||||
xParameters.fnWriteBlocks = prvWriteRAM;
|
||||
xParameters.fnReadBlocks = prvReadRAM;
|
||||
xParameters.pxDisk = pxDisk;
|
||||
|
||||
/* Driver is reentrant so xBlockDeviceIsReentrant can be set to pdTRUE.
|
||||
* In this case the semaphore is only used to protect FAT data
|
||||
* structures. */
|
||||
xParameters.pvSemaphore = ( void * ) xSemaphoreCreateRecursiveMutex();
|
||||
xParameters.xBlockDeviceIsReentrant = pdFALSE;
|
||||
|
||||
pxDisk->pxIOManager = FF_CreateIOManger( &xParameters, &xError );
|
||||
|
||||
if( ( pxDisk->pxIOManager != NULL ) && ( FF_isERR( xError ) == pdFALSE ) )
|
||||
{
|
||||
/* Record that the RAM disk has been initialised. */
|
||||
pxDisk->xStatus.bIsInitialised = pdTRUE;
|
||||
|
||||
#ifdef RAMDISK_ALREADY_PARTITIONED
|
||||
xError = FF_ERR_NONE;
|
||||
#else
|
||||
/* Create a partition on the RAM disk. NOTE! The disk is only
|
||||
* being partitioned here because it is a new RAM disk. It is
|
||||
* known that the disk has not been used before, and cannot already
|
||||
* contain any partitions. Most media drivers will not perform
|
||||
* this step because the media will have already been partitioned. */
|
||||
xError = prvPartitionAndFormatDisk( pxDisk );
|
||||
#endif
|
||||
|
||||
if( FF_isERR( xError ) == pdFALSE )
|
||||
{
|
||||
/* Record the partition number the FF_Disk_t structure is, then
|
||||
* mount the partition. */
|
||||
pxDisk->xStatus.bPartitionNumber = ramPARTITION_NUMBER;
|
||||
|
||||
/* Mount the partition. */
|
||||
xError = FF_Mount( pxDisk, ramPARTITION_NUMBER );
|
||||
FF_PRINTF( "FF_RAMDiskInit: FF_Mount: %s\n", ( const char * ) FF_GetErrMessage( xError ) );
|
||||
}
|
||||
|
||||
if( FF_isERR( xError ) == pdFALSE )
|
||||
{
|
||||
/* The partition mounted successfully, add it to the virtual
|
||||
* file system - where it will appear as a directory off the file
|
||||
* system's root directory. */
|
||||
FF_FS_Add( pcName, pxDisk );
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
FF_PRINTF( "FF_RAMDiskInit: FF_CreateIOManger: %s\n", ( const char * ) FF_GetErrMessage( xError ) );
|
||||
|
||||
/* The disk structure was allocated, but the disk's IO manager could
|
||||
* not be allocated, so free the disk again. */
|
||||
FF_RAMDiskDelete( pxDisk );
|
||||
pxDisk = NULL;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
FF_PRINTF( "FF_RAMDiskInit: Malloc failed\n" );
|
||||
}
|
||||
|
||||
return pxDisk;
|
||||
}
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
BaseType_t FF_RAMDiskDelete( FF_Disk_t * pxDisk )
|
||||
{
|
||||
if( pxDisk != NULL )
|
||||
{
|
||||
pxDisk->ulSignature = 0;
|
||||
pxDisk->xStatus.bIsInitialised = 0;
|
||||
|
||||
if( pxDisk->pxIOManager != NULL )
|
||||
{
|
||||
FF_DeleteIOManager( pxDisk->pxIOManager );
|
||||
}
|
||||
|
||||
vPortFree( pxDisk );
|
||||
}
|
||||
|
||||
return pdPASS;
|
||||
}
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
static int32_t prvReadRAM( uint8_t * pucDestination,
|
||||
uint32_t ulSectorNumber,
|
||||
uint32_t ulSectorCount,
|
||||
FF_Disk_t * pxDisk )
|
||||
{
|
||||
int32_t lReturn;
|
||||
uint8_t * pucSource;
|
||||
|
||||
if( pxDisk != NULL )
|
||||
{
|
||||
if( pxDisk->ulSignature != ramSIGNATURE )
|
||||
{
|
||||
/* The disk structure is not valid because it doesn't contain a
|
||||
* magic number written to the disk when it was created. */
|
||||
lReturn = FF_ERR_IOMAN_DRIVER_FATAL_ERROR | FF_ERRFLAG;
|
||||
}
|
||||
else if( pxDisk->xStatus.bIsInitialised == pdFALSE )
|
||||
{
|
||||
/* The disk has not been initialised. */
|
||||
lReturn = FF_ERR_IOMAN_OUT_OF_BOUNDS_WRITE | FF_ERRFLAG;
|
||||
}
|
||||
else if( ulSectorNumber >= pxDisk->ulNumberOfSectors )
|
||||
{
|
||||
/* The start sector is not within the bounds of the disk. */
|
||||
lReturn = ( FF_ERR_IOMAN_OUT_OF_BOUNDS_WRITE | FF_ERRFLAG );
|
||||
}
|
||||
else if( ( pxDisk->ulNumberOfSectors - ulSectorNumber ) < ulSectorCount )
|
||||
{
|
||||
/* The end sector is not within the bounds of the disk. */
|
||||
lReturn = ( FF_ERR_IOMAN_OUT_OF_BOUNDS_WRITE | FF_ERRFLAG );
|
||||
}
|
||||
else
|
||||
{
|
||||
/* Obtain the pointer to the RAM buffer being used as the disk. */
|
||||
pucSource = ( uint8_t * ) pxDisk->pvTag;
|
||||
|
||||
/* Move to the start of the sector being read. */
|
||||
pucSource += ( ramSECTOR_SIZE * ulSectorNumber );
|
||||
|
||||
/* Copy the data from the disk. As this is a RAM disk this can be
|
||||
* done using memcpy(). */
|
||||
memcpy( ( void * ) pucDestination,
|
||||
( void * ) pucSource,
|
||||
( size_t ) ( ulSectorCount * ramSECTOR_SIZE ) );
|
||||
|
||||
lReturn = FF_ERR_NONE;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
lReturn = FF_ERR_NULL_POINTER | FF_ERRFLAG;
|
||||
}
|
||||
|
||||
return lReturn;
|
||||
}
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
static int32_t prvWriteRAM( uint8_t * pucSource,
|
||||
uint32_t ulSectorNumber,
|
||||
uint32_t ulSectorCount,
|
||||
FF_Disk_t * pxDisk )
|
||||
{
|
||||
int32_t lReturn = FF_ERR_NONE;
|
||||
uint8_t * pucDestination;
|
||||
|
||||
if( pxDisk != NULL )
|
||||
{
|
||||
if( pxDisk->ulSignature != ramSIGNATURE )
|
||||
{
|
||||
/* The disk structure is not valid because it doesn't contain a
|
||||
* magic number written to the disk when it was created. */
|
||||
lReturn = FF_ERR_IOMAN_DRIVER_FATAL_ERROR | FF_ERRFLAG;
|
||||
}
|
||||
else if( pxDisk->xStatus.bIsInitialised == pdFALSE )
|
||||
{
|
||||
/* The disk has not been initialised. */
|
||||
lReturn = FF_ERR_IOMAN_OUT_OF_BOUNDS_WRITE | FF_ERRFLAG;
|
||||
}
|
||||
else if( ulSectorNumber >= pxDisk->ulNumberOfSectors )
|
||||
{
|
||||
/* The start sector is not within the bounds of the disk. */
|
||||
lReturn = ( FF_ERR_IOMAN_OUT_OF_BOUNDS_WRITE | FF_ERRFLAG );
|
||||
}
|
||||
else if( ( pxDisk->ulNumberOfSectors - ulSectorNumber ) < ulSectorCount )
|
||||
{
|
||||
/* The end sector is not within the bounds of the disk. */
|
||||
lReturn = ( FF_ERR_IOMAN_OUT_OF_BOUNDS_WRITE | FF_ERRFLAG );
|
||||
}
|
||||
else
|
||||
{
|
||||
/* Obtain the location of the RAM being used as the disk. */
|
||||
pucDestination = ( uint8_t * ) pxDisk->pvTag;
|
||||
|
||||
/* Move to the sector being written to. */
|
||||
pucDestination += ( ramSECTOR_SIZE * ulSectorNumber );
|
||||
|
||||
/* Write to the disk. As this is a RAM disk the write can use a
|
||||
* memcpy(). */
|
||||
memcpy( ( void * ) pucDestination,
|
||||
( void * ) pucSource,
|
||||
( size_t ) ulSectorCount * ( size_t ) ramSECTOR_SIZE );
|
||||
|
||||
lReturn = FF_ERR_NONE;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
lReturn = FF_ERR_NULL_POINTER | FF_ERRFLAG;
|
||||
}
|
||||
|
||||
return lReturn;
|
||||
}
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
#ifndef RAMDISK_ALREADY_PARTITIONED
|
||||
static FF_Error_t prvPartitionAndFormatDisk( FF_Disk_t * pxDisk )
|
||||
{
|
||||
FF_PartitionParameters_t xPartition;
|
||||
FF_Error_t xError;
|
||||
|
||||
/* Create a single partition that fills all available space on the disk. */
|
||||
memset( &xPartition, '\0', sizeof( xPartition ) );
|
||||
xPartition.ulSectorCount = pxDisk->ulNumberOfSectors;
|
||||
xPartition.ulHiddenSectors = ramHIDDEN_SECTOR_COUNT;
|
||||
xPartition.xPrimaryCount = ramPRIMARY_PARTITIONS;
|
||||
xPartition.eSizeType = eSizeIsQuota;
|
||||
|
||||
/* Partition the disk */
|
||||
xError = FF_Partition( pxDisk, &xPartition );
|
||||
FF_PRINTF( "FF_Partition: %s\n", ( const char * ) FF_GetErrMessage( xError ) );
|
||||
|
||||
if( FF_isERR( xError ) == pdFALSE )
|
||||
{
|
||||
/* Format the partition. */
|
||||
xError = FF_Format( pxDisk, ramPARTITION_NUMBER, pdTRUE, pdTRUE );
|
||||
FF_PRINTF( "FF_RAMDiskInit: FF_Format: %s\n", ( const char * ) FF_GetErrMessage( xError ) );
|
||||
}
|
||||
|
||||
return xError;
|
||||
}
|
||||
/*-----------------------------------------------------------*/
|
||||
#endif
|
||||
|
||||
BaseType_t FF_RAMDiskShowPartition( FF_Disk_t * pxDisk )
|
||||
{
|
||||
FF_Error_t xError;
|
||||
uint64_t ullFreeSectors;
|
||||
uint32_t ulTotalSizeKB, ulFreeSizeKB;
|
||||
int iPercentageFree;
|
||||
FF_IOManager_t * pxIOManager;
|
||||
const char * pcTypeName = "unknown type";
|
||||
BaseType_t xReturn = pdPASS;
|
||||
|
||||
if( pxDisk == NULL )
|
||||
{
|
||||
xReturn = pdFAIL;
|
||||
}
|
||||
else
|
||||
{
|
||||
pxIOManager = pxDisk->pxIOManager;
|
||||
|
||||
FF_PRINTF( "Reading FAT and calculating Free Space\n" );
|
||||
|
||||
switch( pxIOManager->xPartition.ucType )
|
||||
{
|
||||
case FF_T_FAT12:
|
||||
pcTypeName = "FAT12";
|
||||
break;
|
||||
|
||||
case FF_T_FAT16:
|
||||
pcTypeName = "FAT16";
|
||||
break;
|
||||
|
||||
case FF_T_FAT32:
|
||||
pcTypeName = "FAT32";
|
||||
break;
|
||||
|
||||
default:
|
||||
pcTypeName = "UNKOWN";
|
||||
break;
|
||||
}
|
||||
|
||||
FF_GetFreeSize( pxIOManager, &xError );
|
||||
|
||||
ullFreeSectors = pxIOManager->xPartition.ulFreeClusterCount * pxIOManager->xPartition.ulSectorsPerCluster;
|
||||
|
||||
if( pxIOManager->xPartition.ulDataSectors == ( uint32_t ) 0 )
|
||||
{
|
||||
iPercentageFree = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
iPercentageFree = ( int ) ( ( ramHUNDRED_64_BIT * ullFreeSectors + pxIOManager->xPartition.ulDataSectors / 2 ) /
|
||||
( ( uint64_t ) pxIOManager->xPartition.ulDataSectors ) );
|
||||
}
|
||||
|
||||
ulTotalSizeKB = pxIOManager->xPartition.ulDataSectors / ramSECTORS_PER_KB;
|
||||
ulFreeSizeKB = ( uint32_t ) ( ullFreeSectors / ramSECTORS_PER_KB );
|
||||
|
||||
/* It is better not to use the 64-bit format such as %Lu because it
|
||||
* might not be implemented. */
|
||||
FF_PRINTF( "Partition Nr %8u\n", pxDisk->xStatus.bPartitionNumber );
|
||||
FF_PRINTF( "Type %8u (%s)\n", pxIOManager->xPartition.ucType, pcTypeName );
|
||||
FF_PRINTF( "VolLabel '%8s' \n", pxIOManager->xPartition.pcVolumeLabel );
|
||||
FF_PRINTF( "TotalSectors %8lu\n", pxIOManager->xPartition.ulTotalSectors );
|
||||
FF_PRINTF( "SecsPerCluster %8lu\n", pxIOManager->xPartition.ulSectorsPerCluster );
|
||||
FF_PRINTF( "Size %8lu KB\n", ulTotalSizeKB );
|
||||
FF_PRINTF( "FreeSize %8lu KB ( %d perc free )\n", ulFreeSizeKB, iPercentageFree );
|
||||
}
|
||||
|
||||
return xReturn;
|
||||
}
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
void FF_RAMDiskFlush( FF_Disk_t * pxDisk )
|
||||
{
|
||||
if( ( pxDisk != NULL ) && ( pxDisk->xStatus.bIsInitialised != 0 ) && ( pxDisk->pxIOManager != NULL ) )
|
||||
{
|
||||
FF_FlushCache( pxDisk->pxIOManager );
|
||||
}
|
||||
}
|
||||
/*-----------------------------------------------------------*/
|
@ -0,0 +1,55 @@
|
||||
/*
|
||||
* FreeRTOS+FAT V2.3.3
|
||||
* Copyright (C) 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
* this software and associated documentation files (the "Software"), to deal in
|
||||
* the Software without restriction, including without limitation the rights to
|
||||
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
||||
* the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
* subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
||||
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*
|
||||
* https://www.FreeRTOS.org
|
||||
* https://github.com/FreeRTOS
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef __RAMDISK_H__
|
||||
|
||||
#define __RAMDISK_H__
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include "ff_headers.h"
|
||||
|
||||
//#define RAMDISK_ALREADY_PARTITIONED
|
||||
|
||||
/* Create a RAM disk, supplying enough memory to hold N sectors of 512 bytes each */
|
||||
FF_Disk_t * FF_RAMDiskInit( char * pcName,
|
||||
uint8_t * pucDataBuffer,
|
||||
uint32_t ulSectorCount,
|
||||
size_t xIOManagerCacheSize );
|
||||
|
||||
/* Release all resources */
|
||||
BaseType_t FF_RAMDiskDelete( FF_Disk_t * pxDisk );
|
||||
|
||||
/* Show some partition information */
|
||||
BaseType_t FF_RAMDiskShowPartition( FF_Disk_t * pxDisk );
|
||||
|
||||
#ifdef __cplusplus
|
||||
} /* extern "C" */
|
||||
#endif
|
||||
|
||||
#endif /* __RAMDISK_H__ */
|
@ -0,0 +1,80 @@
|
||||
/*
|
||||
* FreeRTOS+FAT V2.3.3
|
||||
* Copyright (C) 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
* this software and associated documentation files (the "Software"), to deal in
|
||||
* the Software without restriction, including without limitation the rights to
|
||||
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
||||
* the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
* subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
||||
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*
|
||||
* https://www.FreeRTOS.org
|
||||
* https://github.com/FreeRTOS
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef __SDDISK_H__
|
||||
|
||||
#define __SDDISK_H__
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include "ff_headers.h"
|
||||
|
||||
|
||||
/* Return non-zero if the SD-card is present.
|
||||
* The parameter 'pxDisk' may be null, unless device locking is necessary. */
|
||||
BaseType_t FF_SDDiskDetect( FF_Disk_t * pxDisk );
|
||||
|
||||
/* Create a RAM disk, supplying enough memory to hold N sectors of 512 bytes each */
|
||||
FF_Disk_t * FF_SDDiskInit( const char * pcName );
|
||||
|
||||
BaseType_t FF_SDDiskReinit( FF_Disk_t * pxDisk );
|
||||
|
||||
/* Unmount the volume */
|
||||
BaseType_t FF_SDDiskUnmount( FF_Disk_t * pDisk );
|
||||
|
||||
/* Mount the volume */
|
||||
BaseType_t FF_SDDiskMount( FF_Disk_t * pDisk );
|
||||
|
||||
/* Release all resources */
|
||||
BaseType_t FF_SDDiskDelete( FF_Disk_t * pDisk );
|
||||
|
||||
/* Show some partition information */
|
||||
BaseType_t FF_SDDiskShowPartition( FF_Disk_t * pDisk );
|
||||
|
||||
/* Flush changes from the driver's buf to disk */
|
||||
void FF_SDDiskFlush( FF_Disk_t * pDisk );
|
||||
|
||||
/* Format a given partition on an SD-card. */
|
||||
BaseType_t FF_SDDiskFormat( FF_Disk_t * pxDisk,
|
||||
BaseType_t aPart );
|
||||
|
||||
/* Format a given partition by name on an SD-card. */
|
||||
BaseType_t FF_SDDiskFormatRemount( FF_Disk_t * pxDisk,
|
||||
const char *pcName );
|
||||
/* Return non-zero if an SD-card is detected in a given slot. */
|
||||
BaseType_t FF_SDDiskInserted( BaseType_t xDriveNr );
|
||||
|
||||
/* _RB_ Temporary function - ideally the application would not need the IO
|
||||
* manageer structure, just a handle to a disk. */
|
||||
FF_IOManager_t * sddisk_ioman( FF_Disk_t * pxDisk );
|
||||
|
||||
#ifdef __cplusplus
|
||||
} /* extern "C" */
|
||||
#endif
|
||||
|
||||
#endif /* __SDDISK_H__ */
|
@ -0,0 +1,73 @@
|
||||
/*
|
||||
* FreeRTOS+FAT V2.3.3
|
||||
* Copyright (C) 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
* this software and associated documentation files (the "Software"), to deal in
|
||||
* the Software without restriction, including without limitation the rights to
|
||||
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
||||
* the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
* subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
||||
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*
|
||||
* https://www.FreeRTOS.org
|
||||
* https://github.com/FreeRTOS
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef __SFDISK_H__
|
||||
|
||||
#define __SFDISK_H__
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include "ff_headers.h"
|
||||
|
||||
|
||||
/* Return non-zero if the spi-flash is present.
|
||||
* The parameter 'pxDisk' may be null, unless device locking is necessary. */
|
||||
BaseType_t FF_SFDiskDetect( FF_Disk_t * pxDisk );
|
||||
|
||||
/* Create a RAM disk, supplying enough memory to hold N sectors of 512 bytes each */
|
||||
FF_Disk_t * FF_SFDiskInit( const char * pcName );
|
||||
|
||||
BaseType_t FF_SFDiskReinit( FF_Disk_t * pxDisk );
|
||||
|
||||
/* Unmount the volume */
|
||||
BaseType_t FF_SFDiskUnmount( FF_Disk_t * pDisk );
|
||||
|
||||
/* Mount the volume */
|
||||
BaseType_t FF_SFDiskMount( FF_Disk_t * pDisk );
|
||||
|
||||
/* Release all resources */
|
||||
BaseType_t FF_SFDiskDelete( FF_Disk_t * pDisk );
|
||||
|
||||
/* Show some partition information */
|
||||
BaseType_t FF_SFDiskShowPartition( FF_Disk_t * pDisk );
|
||||
|
||||
/* Flush changes from the driver's buf to disk */
|
||||
void FF_SFDiskFlush( FF_Disk_t * pDisk );
|
||||
|
||||
/* Format a given partition on an spi-flash. */
|
||||
BaseType_t FF_SFDiskFormat( FF_Disk_t * pxDisk,
|
||||
BaseType_t aPart );
|
||||
|
||||
/* Return non-zero if an spi-flash is detected in a given slot. */
|
||||
BaseType_t FF_SFDiskInserted( BaseType_t xDriveNr );
|
||||
|
||||
#ifdef __cplusplus
|
||||
} /* extern "C" */
|
||||
#endif
|
||||
|
||||
#endif /* __SFDISK_H__ */
|
@ -0,0 +1,77 @@
|
||||
/*
|
||||
* FreeRTOS+FAT V2.3.3
|
||||
* Copyright (C) 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
* this software and associated documentation files (the "Software"), to deal in
|
||||
* the Software without restriction, including without limitation the rights to
|
||||
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
||||
* the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
* subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
||||
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*
|
||||
* https://www.FreeRTOS.org
|
||||
* https://github.com/FreeRTOS
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef __USBDISK_H__
|
||||
|
||||
#define __USBDISK_H__
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include "ff_headers.h"
|
||||
|
||||
|
||||
/* Return non-zero if the SD-card is present.
|
||||
* The parameter 'pxDisk' may be null, unless device locking is necessary. */
|
||||
BaseType_t FF_USBDiskDetect( FF_Disk_t * pxDisk );
|
||||
|
||||
/* Create a RAM disk, supplying enough memory to hold N sectors of 512 bytes each */
|
||||
FF_Disk_t * FF_USBDiskInit( const char * pcName );
|
||||
|
||||
BaseType_t FF_USBDiskReinit( FF_Disk_t * pxDisk );
|
||||
|
||||
/* Unmount the volume */
|
||||
BaseType_t FF_USBDiskUnmount( FF_Disk_t * pDisk );
|
||||
|
||||
/* Mount the volume */
|
||||
BaseType_t FF_USBDiskMount( FF_Disk_t * pDisk );
|
||||
|
||||
/* Release all resources */
|
||||
BaseType_t FF_USBDiskDelete( FF_Disk_t * pDisk );
|
||||
|
||||
/* Show some partition information */
|
||||
BaseType_t FF_USBDiskShowPartition( FF_Disk_t * pDisk );
|
||||
|
||||
/* Flush changes from the driver's buf to disk */
|
||||
void FF_USBDiskFlush( FF_Disk_t * pDisk );
|
||||
|
||||
/* Format a given partition on an SD-card. */
|
||||
BaseType_t FF_USBDiskFormat( FF_Disk_t * pxDisk,
|
||||
BaseType_t aPart );
|
||||
|
||||
/* Return non-zero if an SD-card is detected in a given slot. */
|
||||
BaseType_t FF_USBDiskInserted( BaseType_t xDriveNr );
|
||||
|
||||
/* _RB_ Temporary function - ideally the application would not need the IO
|
||||
* manageer structure, just a handle to a disk. */
|
||||
FF_IOManager_t * usbdisk_ioman( FF_Disk_t * pxDisk );
|
||||
|
||||
#ifdef __cplusplus
|
||||
} /* extern "C" */
|
||||
#endif
|
||||
|
||||
#endif /* __USBDISK_H__ */
|
@ -0,0 +1,710 @@
|
||||
/*
|
||||
* FreeRTOS+FAT build 191128 - Note: FreeRTOS+FAT is still in the lab!
|
||||
* Copyright (C) 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
||||
* Authors include James Walmsley, Hein Tibosch and Richard Barry
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
* this software and associated documentation files (the "Software"), to deal in
|
||||
* the Software without restriction, including without limitation the rights to
|
||||
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
||||
* the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
* subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
||||
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*
|
||||
* https://www.FreeRTOS.org
|
||||
*
|
||||
*/
|
||||
|
||||
/* Standard includes. */
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdio.h>
|
||||
|
||||
/* LPC18xx includes. */
|
||||
#include "chip.h"
|
||||
#include "board.h"
|
||||
|
||||
/* FreeRTOS includes. */
|
||||
#include "FreeRTOS.h"
|
||||
#include "task.h"
|
||||
#include "semphr.h"
|
||||
#include "portmacro.h"
|
||||
|
||||
/* FreeRTOS+FAT includes. */
|
||||
#include "ff_sddisk.h"
|
||||
#include "ff_sys.h"
|
||||
|
||||
#include "mmcsd_core.h"
|
||||
|
||||
/* Misc definitions. */
|
||||
#define sdSIGNATURE 0x41404342UL
|
||||
#define sdHUNDRED_64_BIT ( 100ull )
|
||||
#define sdBYTES_PER_MB ( 1024ull * 1024ull )
|
||||
#define sdSECTORS_PER_MB ( sdBYTES_PER_MB / 512ull )
|
||||
#define sdIOMAN_MEM_SIZE 4096
|
||||
#define sdAligned( pvAddress ) ( ( ( ( size_t ) ( pvAddress ) ) & ( sizeof( size_t ) - 1 ) ) == 0 )
|
||||
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
/*_RB_ Functions require comment blocks. */
|
||||
static int32_t prvSDMMC_Init( void );
|
||||
static int32_t prvFFRead( uint8_t *pucBuffer, uint32_t ulSectorNumber, uint32_t ulSectorCount, FF_Disk_t *pxDisk );
|
||||
static int32_t prvFFWrite( uint8_t *pucBuffer, uint32_t ulSectorNumber, uint32_t ulSectorCount, FF_Disk_t *pxDisk );
|
||||
|
||||
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
/*_RB_ Variables require a comment block where appropriate. */
|
||||
static struct mmcsd_card *xCardInfo;
|
||||
static BaseType_t xSDCardStatus;
|
||||
static SemaphoreHandle_t xPlusFATMutex;
|
||||
|
||||
/*-----------------------------------------------------------*/
|
||||
static int32_t prvFFRead( uint8_t *pucBuffer, uint32_t ulSectorNumber, uint32_t ulSectorCount, FF_Disk_t *pxDisk )
|
||||
{
|
||||
int32_t iReturn;
|
||||
|
||||
/*_RB_ Many of the comments in this file apply to other functions in the file. */
|
||||
|
||||
if( ( pxDisk != NULL ) &&
|
||||
( xSDCardStatus == pdPASS ) &&
|
||||
( pxDisk->ulSignature == sdSIGNATURE ) &&
|
||||
( pxDisk->xStatus.bIsInitialised != pdFALSE ) &&
|
||||
( ulSectorNumber < pxDisk->ulNumberOfSectors ) &&
|
||||
( ( pxDisk->ulNumberOfSectors - ulSectorNumber ) >= ulSectorCount ) )
|
||||
{
|
||||
#if DEVICE_TYPE_SELECT == EMMC_FLASH
|
||||
iReturn = mmcsd_req_blk( xCardInfo, ulSectorNumber + OTA_MEDIA_OFFSET / 512, pucBuffer, ulSectorCount, 0 );
|
||||
#else
|
||||
iReturn = mmcsd_req_blk( xCardInfo, ulSectorNumber, pucBuffer, ulSectorCount, 0 );
|
||||
#endif
|
||||
|
||||
/*_RB_ I'm guessing 512 is a sector size, but that needs to be clear.
|
||||
Is it defined in a header somewhere? If so we can do a search and
|
||||
replace in files on it as it seems to be used everywhere. */
|
||||
if( iReturn == 0 ) /*_RB_ Signed/unsigned mismatch (twice!) */
|
||||
{
|
||||
iReturn = FF_ERR_NONE;
|
||||
}
|
||||
else
|
||||
{
|
||||
/*_RB_ Signed number used to return bitmap (again below). */
|
||||
iReturn = ( FF_ERR_IOMAN_OUT_OF_BOUNDS_READ | FF_ERRFLAG );
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
memset( ( void * ) pucBuffer, '\0', ulSectorCount * 512 );
|
||||
|
||||
if( pxDisk->xStatus.bIsInitialised != 0 )
|
||||
{
|
||||
FF_PRINTF( "prvFFRead: warning: %lu + %lu > %lu\n", ulSectorNumber, ulSectorCount, pxDisk->ulNumberOfSectors );
|
||||
}
|
||||
|
||||
iReturn = ( FF_ERR_IOMAN_OUT_OF_BOUNDS_READ | FF_ERRFLAG );
|
||||
}
|
||||
|
||||
return iReturn;
|
||||
}
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
static int32_t prvFFWrite( uint8_t *pucBuffer, uint32_t ulSectorNumber, uint32_t ulSectorCount, FF_Disk_t *pxDisk )
|
||||
{
|
||||
int32_t iReturn;
|
||||
|
||||
if( ( pxDisk != NULL ) &&
|
||||
( xSDCardStatus == pdPASS ) &&
|
||||
( pxDisk->ulSignature == sdSIGNATURE ) &&
|
||||
( pxDisk->xStatus.bIsInitialised != pdFALSE ) &&
|
||||
( ulSectorNumber < pxDisk->ulNumberOfSectors ) &&
|
||||
( ( pxDisk->ulNumberOfSectors - ulSectorNumber ) >= ulSectorCount ) )
|
||||
{
|
||||
#if DEVICE_TYPE_SELECT == EMMC_FLASH
|
||||
iReturn = mmcsd_req_blk( xCardInfo, ulSectorNumber + OTA_MEDIA_OFFSET / 512, pucBuffer, ulSectorCount, 1 );
|
||||
#else
|
||||
iReturn = mmcsd_req_blk( xCardInfo, ulSectorNumber, pucBuffer, ulSectorCount, 1 );
|
||||
#endif
|
||||
|
||||
if( iReturn == 0 ) /*_RB_ Signed/unsigned mismatch (twice!) */
|
||||
{
|
||||
iReturn = FF_ERR_NONE;
|
||||
}
|
||||
else
|
||||
{
|
||||
iReturn = ( FF_ERR_IOMAN_OUT_OF_BOUNDS_WRITE | FF_ERRFLAG );
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
memset( ( void * ) pucBuffer, '\0', ulSectorCount * 512 );
|
||||
if( pxDisk->xStatus.bIsInitialised )
|
||||
{
|
||||
FF_PRINTF( "prvFFWrite: warning: %lu + %lu > %lu\n", ulSectorNumber, ulSectorCount, pxDisk->ulNumberOfSectors );
|
||||
}
|
||||
|
||||
iReturn = ( FF_ERR_IOMAN_OUT_OF_BOUNDS_WRITE | FF_ERRFLAG );
|
||||
}
|
||||
|
||||
return iReturn;
|
||||
}
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
void FF_SDDiskFlush( FF_Disk_t *pxDisk )
|
||||
{
|
||||
if( ( pxDisk != NULL ) &&
|
||||
( pxDisk->xStatus.bIsInitialised != pdFALSE ) &&
|
||||
( pxDisk->pxIOManager != NULL ) )
|
||||
{
|
||||
FF_FlushCache( pxDisk->pxIOManager );
|
||||
}
|
||||
}
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
#if DEVICE_TYPE_SELECT == EMMC_FLASH
|
||||
#define emmcSECTOR_SIZE 512
|
||||
#define emmcHIDDEN_SECTOR_COUNT 8
|
||||
#define emmcPRIMARY_PARTITIONS 1
|
||||
#define emmcPARTITION_NUMBER 0 /* Only a single partition is used. */
|
||||
static FF_Error_t PartitionAndFormatEmmcDisk( FF_Disk_t * pxDisk )
|
||||
{
|
||||
FF_PartitionParameters_t xPartition;
|
||||
FF_Error_t xError;
|
||||
|
||||
/* Create a single partition that fills all available space on the disk. */
|
||||
memset( &xPartition, '\0', sizeof( xPartition ) );
|
||||
xPartition.ulSectorCount = OTA_MEDIA_SIZE / emmcSECTOR_SIZE;
|
||||
xPartition.ulHiddenSectors = emmcHIDDEN_SECTOR_COUNT;
|
||||
xPartition.xPrimaryCount = emmcPRIMARY_PARTITIONS;
|
||||
xPartition.eSizeType = eSizeIsQuota;
|
||||
|
||||
/* Partition the disk */
|
||||
xError = FF_Partition( pxDisk, &xPartition );
|
||||
FF_PRINTF( "FF_Partition: %s\n", ( const char * ) FF_GetErrMessage( xError ) );
|
||||
|
||||
if( FF_isERR( xError ) == pdFALSE )
|
||||
{
|
||||
/* Format the partition. */
|
||||
xError = FF_Format( pxDisk, emmcPARTITION_NUMBER, pdFALSE, pdFALSE );
|
||||
FF_PRINTF( "FF_Format: %s\n", ( const char * ) FF_GetErrMessage( xError ) );
|
||||
if ( FF_isERR( xError ) == pdFALSE )
|
||||
{
|
||||
xError = FF_Mount( pxDisk, 0 );
|
||||
FF_PRINTF( "FF_Mount: %s\n", ( const char * ) FF_GetErrMessage( xError ) );
|
||||
FF_SDDiskShowPartition( pxDisk );
|
||||
}
|
||||
}
|
||||
|
||||
return xError;
|
||||
}
|
||||
#endif
|
||||
|
||||
/* Initialise the SDIO driver and mount an SD card */
|
||||
FF_Disk_t *FF_SDDiskInit( const char *pcName )
|
||||
{
|
||||
FF_Error_t xFFError;
|
||||
BaseType_t xPartitionNumber = 0;
|
||||
FF_CreationParameters_t xParameters;
|
||||
FF_Disk_t * pxDisk;
|
||||
|
||||
xSDCardStatus = prvSDMMC_Init();
|
||||
if( xSDCardStatus == pdPASS )
|
||||
{
|
||||
pxDisk = ( FF_Disk_t * ) pvPortMalloc( sizeof( *pxDisk ) );
|
||||
|
||||
if( pxDisk != NULL )
|
||||
{
|
||||
|
||||
/* Initialise the created disk structure. */
|
||||
memset( pxDisk, '\0', sizeof( *pxDisk ) );
|
||||
|
||||
|
||||
if( xPlusFATMutex == NULL)
|
||||
{
|
||||
xPlusFATMutex = xSemaphoreCreateRecursiveMutex();
|
||||
}
|
||||
pxDisk->ulNumberOfSectors = xCardInfo->card_blknr;
|
||||
pxDisk->ulSignature = sdSIGNATURE;
|
||||
|
||||
if( xPlusFATMutex != NULL)
|
||||
{
|
||||
memset( &xParameters, '\0', sizeof( xParameters ) );
|
||||
xParameters.ulMemorySize = sdIOMAN_MEM_SIZE;
|
||||
xParameters.ulSectorSize = 512;
|
||||
xParameters.fnWriteBlocks = prvFFWrite;
|
||||
xParameters.fnReadBlocks = prvFFRead;
|
||||
xParameters.pxDisk = pxDisk;
|
||||
|
||||
/* prvFFRead()/prvFFWrite() are not re-entrant and must be
|
||||
protected with the use of a semaphore. */
|
||||
xParameters.xBlockDeviceIsReentrant = pdFALSE;
|
||||
|
||||
/* The semaphore will be used to protect critical sections in
|
||||
the +FAT driver, and also to avoid concurrent calls to
|
||||
prvFFRead()/prvFFWrite() from different tasks. */
|
||||
xParameters.pvSemaphore = ( void * ) xPlusFATMutex;
|
||||
|
||||
pxDisk->pxIOManager = FF_CreateIOManger( &xParameters, &xFFError );
|
||||
|
||||
if( pxDisk->pxIOManager == NULL )
|
||||
{
|
||||
FF_PRINTF( "FF_SDDiskInit: FF_CreateIOManger: %s\n", ( const char * ) FF_GetErrMessage( xFFError ) );
|
||||
FF_SDDiskDelete( pxDisk );
|
||||
pxDisk = NULL;
|
||||
}
|
||||
else
|
||||
{
|
||||
pxDisk->xStatus.bIsInitialised = pdTRUE;
|
||||
pxDisk->xStatus.bPartitionNumber = xPartitionNumber;
|
||||
if( FF_SDDiskMount( pxDisk ) == 0 )
|
||||
{
|
||||
#if DEVICE_TYPE_SELECT == EMMC_FLASH
|
||||
if ( PartitionAndFormatEmmcDisk(pxDisk) != FF_ERR_NONE )
|
||||
{
|
||||
FF_PRINTF( "FF_SDDiskInit: Mounted SD-card/emmc fail.\n");
|
||||
FF_SDDiskDelete( pxDisk );
|
||||
pxDisk = NULL;
|
||||
}
|
||||
else
|
||||
{
|
||||
if( pcName == NULL )
|
||||
{
|
||||
pcName = "/";
|
||||
}
|
||||
FF_FS_Add( pcName, pxDisk );
|
||||
FF_PRINTF( "FF_SDDiskInit: Mount SD-card/emmc as root \"%s\"\n", pcName );
|
||||
}
|
||||
#else
|
||||
FF_PRINTF( "FF_SDDiskInit: Mounted SD-card/emmc fail.\n");
|
||||
FF_SDDiskDelete( pxDisk );
|
||||
pxDisk = NULL;
|
||||
#endif
|
||||
}
|
||||
else
|
||||
{
|
||||
if( pcName == NULL )
|
||||
{
|
||||
pcName = "/";
|
||||
}
|
||||
FF_FS_Add( pcName, pxDisk );
|
||||
FF_PRINTF( "FF_SDDiskInit: Mounted SD-card as root \"%s\"\n", pcName );
|
||||
}
|
||||
} /* if( pxDisk->pxIOManager != NULL ) */
|
||||
} /* if( xPlusFATMutex != NULL) */
|
||||
} /* if( pxDisk != NULL ) */
|
||||
else
|
||||
{
|
||||
FF_PRINTF( "FF_SDDiskInit: Malloc failed\n" );
|
||||
}
|
||||
} /* if( xSDCardStatus == pdPASS ) */
|
||||
else
|
||||
{
|
||||
FF_PRINTF( "FF_SDDiskInit: prvSDMMC_Init failed\n" );
|
||||
pxDisk = NULL;
|
||||
}
|
||||
|
||||
return pxDisk;
|
||||
}
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
BaseType_t FF_SDDiskFormat( FF_Disk_t *pxDisk, BaseType_t xPartitionNumber )
|
||||
{
|
||||
FF_Error_t xError;
|
||||
BaseType_t xReturn = pdFAIL;
|
||||
|
||||
xError = FF_Unmount( pxDisk );
|
||||
|
||||
if( FF_isERR( xError ) != pdFALSE )
|
||||
{
|
||||
FF_PRINTF( "FF_SDDiskFormat: unmount fails: %08x\n", ( unsigned ) xError );
|
||||
}
|
||||
else
|
||||
{
|
||||
/* Format the drive - try FAT32 with large clusters. */
|
||||
xError = FF_Format( pxDisk, xPartitionNumber, pdFALSE, pdFALSE);
|
||||
|
||||
if( FF_isERR( xError ) )
|
||||
{
|
||||
FF_PRINTF( "FF_SDDiskFormat: %s\n", (const char*)FF_GetErrMessage( xError ) );
|
||||
}
|
||||
else
|
||||
{
|
||||
FF_PRINTF( "FF_SDDiskFormat: OK, now remounting\n" );
|
||||
pxDisk->xStatus.bPartitionNumber = xPartitionNumber;
|
||||
xError = FF_SDDiskMount( pxDisk );
|
||||
FF_PRINTF( "FF_SDDiskFormat: rc %08x\n", ( unsigned )xError );
|
||||
if( FF_isERR( xError ) == pdFALSE )
|
||||
{
|
||||
xReturn = pdPASS;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return xReturn;
|
||||
}
|
||||
BaseType_t FF_SDDiskFormatRemount( FF_Disk_t *pxDisk, const char *pcName )
|
||||
{
|
||||
FF_Error_t xError;
|
||||
BaseType_t xReturn = pdFAIL;
|
||||
|
||||
xError = FF_Unmount( pxDisk );
|
||||
|
||||
if( FF_isERR( xError ) != pdFALSE )
|
||||
{
|
||||
FF_PRINTF( "FF_SDDiskFormat: unmount fails: %08x\n", ( unsigned ) xError );
|
||||
}
|
||||
else
|
||||
{
|
||||
#if DEVICE_TYPE_SELECT == EMMC_FLASH
|
||||
if ( PartitionAndFormatEmmcDisk(pxDisk) != FF_ERR_NONE )
|
||||
{
|
||||
FF_PRINTF( "FF_SDDiskFormatRemount: PartitionAndFormatEmmcDisk fail.\n");
|
||||
FF_SDDiskDelete( pxDisk );
|
||||
pxDisk = NULL;
|
||||
}
|
||||
else
|
||||
{
|
||||
if( pcName == NULL )
|
||||
{
|
||||
pcName = "/";
|
||||
}
|
||||
FF_FS_Add( pcName, pxDisk );
|
||||
FF_PRINTF( "FF_SDDiskFormatRemount: Mount SD-card/emmc as root \"%s\"\n", pcName );
|
||||
}
|
||||
#else
|
||||
FF_PRINTF( "FF_SDDiskInit: Mounted SD-card/emmc fail.\n");
|
||||
FF_SDDiskDelete( pxDisk );
|
||||
pxDisk = NULL;
|
||||
#endif
|
||||
}
|
||||
|
||||
return xReturn;
|
||||
}
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
/* Get a pointer to IOMAN, which can be used for all FreeRTOS+FAT functions */
|
||||
BaseType_t FF_SDDiskMount( FF_Disk_t *pxDisk )
|
||||
{
|
||||
FF_Error_t xFFError;
|
||||
BaseType_t xReturn;
|
||||
|
||||
/* Mount the partition */
|
||||
xFFError = FF_Mount( pxDisk, pxDisk->xStatus.bPartitionNumber );
|
||||
|
||||
if( FF_isERR( xFFError ) )
|
||||
{
|
||||
FF_PRINTF( "FF_SDDiskMount: %08lX\n", xFFError );
|
||||
xReturn = pdFAIL;
|
||||
}
|
||||
else
|
||||
{
|
||||
pxDisk->xStatus.bIsMounted = pdTRUE;
|
||||
FF_PRINTF( "****** FreeRTOS+FAT initialized %lu sectors\n", pxDisk->pxIOManager->xPartition.ulTotalSectors );
|
||||
FF_SDDiskShowPartition( pxDisk );
|
||||
xReturn = pdPASS;
|
||||
}
|
||||
|
||||
return xReturn;
|
||||
}
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
FF_IOManager_t *sddisk_ioman( FF_Disk_t *pxDisk )
|
||||
{
|
||||
FF_IOManager_t *pxReturn;
|
||||
|
||||
if( ( pxDisk != NULL ) && ( pxDisk->xStatus.bIsInitialised != pdFALSE ) )
|
||||
{
|
||||
pxReturn = pxDisk->pxIOManager;
|
||||
}
|
||||
else
|
||||
{
|
||||
pxReturn = NULL;
|
||||
}
|
||||
return pxReturn;
|
||||
}
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
/* Release all resources */
|
||||
BaseType_t FF_SDDiskDelete( FF_Disk_t *pxDisk )
|
||||
{
|
||||
if( pxDisk != NULL )
|
||||
{
|
||||
pxDisk->ulSignature = 0;
|
||||
pxDisk->xStatus.bIsInitialised = 0;
|
||||
if( pxDisk->pxIOManager != NULL )
|
||||
{
|
||||
if( FF_Mounted( pxDisk->pxIOManager ) != pdFALSE )
|
||||
{
|
||||
FF_Unmount( pxDisk );
|
||||
}
|
||||
FF_DeleteIOManager( pxDisk->pxIOManager );
|
||||
}
|
||||
|
||||
vPortFree( pxDisk );
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
BaseType_t FF_SDDiskShowPartition( FF_Disk_t *pxDisk )
|
||||
{
|
||||
FF_Error_t xError;
|
||||
uint64_t ullFreeSectors;
|
||||
uint32_t ulTotalSizeMB, ulFreeSizeMB;
|
||||
int iPercentageFree;
|
||||
FF_IOManager_t *pxIOManager;
|
||||
const char *pcTypeName = "unknown type";
|
||||
BaseType_t xReturn = pdPASS;
|
||||
|
||||
if( pxDisk == NULL )
|
||||
{
|
||||
xReturn = pdFAIL;
|
||||
}
|
||||
else
|
||||
{
|
||||
pxIOManager = pxDisk->pxIOManager;
|
||||
|
||||
FF_PRINTF( "Reading FAT and calculating Free Space\n" );
|
||||
|
||||
switch( pxIOManager->xPartition.ucType )
|
||||
{
|
||||
case FF_T_FAT12:
|
||||
pcTypeName = "FAT12";
|
||||
break;
|
||||
|
||||
case FF_T_FAT16:
|
||||
pcTypeName = "FAT16";
|
||||
break;
|
||||
|
||||
case FF_T_FAT32:
|
||||
pcTypeName = "FAT32";
|
||||
break;
|
||||
|
||||
default:
|
||||
pcTypeName = "UNKOWN";
|
||||
break;
|
||||
}
|
||||
|
||||
FF_GetFreeSize( pxIOManager, &xError );
|
||||
|
||||
ullFreeSectors = pxIOManager->xPartition.ulFreeClusterCount * pxIOManager->xPartition.ulSectorsPerCluster;
|
||||
iPercentageFree = ( int ) ( ( sdHUNDRED_64_BIT * ullFreeSectors + pxIOManager->xPartition.ulDataSectors / 2 ) /
|
||||
( ( uint64_t )pxIOManager->xPartition.ulDataSectors ) );
|
||||
|
||||
ulTotalSizeMB = pxIOManager->xPartition.ulDataSectors / sdSECTORS_PER_MB;
|
||||
ulFreeSizeMB = ( uint32_t ) ( ullFreeSectors / sdSECTORS_PER_MB );
|
||||
|
||||
/* It is better not to use the 64-bit format such as %Lu because it
|
||||
might not be implemented. */
|
||||
FF_PRINTF( "Partition Nr %8u\n", pxDisk->xStatus.bPartitionNumber );
|
||||
FF_PRINTF( "Type %8u (%s)\n", pxIOManager->xPartition.ucType, pcTypeName );
|
||||
FF_PRINTF( "VolLabel '%8s' \n", pxIOManager->xPartition.pcVolumeLabel );
|
||||
FF_PRINTF( "TotalSectors %8lu\n", pxIOManager->xPartition.ulTotalSectors );
|
||||
FF_PRINTF( "SecsPerCluster %8lu\n", pxIOManager->xPartition.ulSectorsPerCluster );
|
||||
FF_PRINTF( "Size %8lu MB\n", ulTotalSizeMB );
|
||||
FF_PRINTF( "FreeSize %8lu MB ( %d perc free )\n", ulFreeSizeMB, iPercentageFree );
|
||||
}
|
||||
|
||||
return xReturn;
|
||||
}
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
static int32_t prvSDMMC_Init( void )
|
||||
{
|
||||
if (!xCardInfo) {
|
||||
xCardInfo = mmcsd_get_sdmmc_card_info();
|
||||
if (!xCardInfo)
|
||||
return pdFAIL;
|
||||
}
|
||||
|
||||
mmcsd_set_blksize(xCardInfo);
|
||||
|
||||
return pdPASS;
|
||||
}
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
#if DEVICE_TYPE_SELECT == EMMC_FLASH
|
||||
static uint32_t cached_sector = 0xffffffff;
|
||||
static uint8_t cached_data[emmcSECTOR_SIZE];
|
||||
|
||||
int32_t phyRead( uint8_t *pucBuffer, uint32_t ulSectorNumber, uint32_t ulSectorCount)
|
||||
{
|
||||
int32_t iReturn;
|
||||
/*_RB_ Many of the comments in this file apply to other functions in the file. */
|
||||
iReturn = mmcsd_req_blk( xCardInfo, ulSectorNumber, pucBuffer, ulSectorCount, 0 );
|
||||
|
||||
/*_RB_ I'm guessing 512 is a sector size, but that needs to be clear.
|
||||
Is it defined in a header somewhere? If so we can do a search and
|
||||
replace in files on it as it seems to be used everywhere. */
|
||||
if( iReturn == 0 ) /*_RB_ Signed/unsigned mismatch (twice!) */
|
||||
{
|
||||
iReturn = FF_ERR_NONE;
|
||||
}
|
||||
else
|
||||
{
|
||||
/*_RB_ Signed number used to return bitmap (again below). */
|
||||
iReturn = ( FF_ERR_IOMAN_OUT_OF_BOUNDS_READ | FF_ERRFLAG );
|
||||
}
|
||||
return iReturn;
|
||||
}
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
int32_t phyWrite( uint8_t *pucBuffer, uint32_t ulSectorNumber, uint32_t ulSectorCount)
|
||||
{
|
||||
int32_t iReturn;
|
||||
xSDCardStatus = prvSDMMC_Init();
|
||||
iReturn = mmcsd_req_blk( xCardInfo, ulSectorNumber, pucBuffer, ulSectorCount, 1 );
|
||||
if( iReturn == 0 ) /*_RB_ Signed/unsigned mismatch (twice!) */
|
||||
{
|
||||
iReturn = FF_ERR_NONE;
|
||||
}
|
||||
else
|
||||
{
|
||||
iReturn = ( FF_ERR_IOMAN_OUT_OF_BOUNDS_WRITE | FF_ERRFLAG );
|
||||
}
|
||||
return iReturn;
|
||||
}
|
||||
|
||||
#define EMMC_RW_MAX_SIZE 0x100000
|
||||
static int raw_emmc_read(uint32_t offset, size_t size, uint8_t *data)
|
||||
{
|
||||
unsigned int blkcnt = 0;
|
||||
unsigned int blkstart = 0;
|
||||
unsigned char *ptembuf = NULL;
|
||||
unsigned int inoffset = 0;
|
||||
unsigned int readsize = 0;
|
||||
int ret = 0;
|
||||
|
||||
xSDCardStatus = prvSDMMC_Init();
|
||||
|
||||
blkstart = offset / emmcSECTOR_SIZE;
|
||||
inoffset = offset - blkstart * emmcSECTOR_SIZE;
|
||||
readsize = size + inoffset;
|
||||
blkcnt = (readsize + emmcSECTOR_SIZE - 1) / emmcSECTOR_SIZE;
|
||||
|
||||
ptembuf = (uint8_t *)pvPortMalloc(blkcnt * emmcSECTOR_SIZE);
|
||||
if(!ptembuf)
|
||||
printf("malloc Error!!1\n");
|
||||
|
||||
ret = phyRead(ptembuf, blkstart, blkcnt);
|
||||
memcpy(data, ptembuf + inoffset, size);
|
||||
if(ptembuf) {
|
||||
free(ptembuf);
|
||||
ptembuf = NULL;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
static int raw_emmc_write(uint32_t offset, size_t size, uint8_t *data)
|
||||
{
|
||||
unsigned int blkcnt = 0;
|
||||
unsigned int blkstart = 0, blkend = 0;
|
||||
unsigned char *ptembuf = NULL;
|
||||
unsigned int inoffset = 0;
|
||||
unsigned int endoffset = 0;
|
||||
unsigned int wsize = 0;
|
||||
int ret = 0;
|
||||
|
||||
xSDCardStatus = prvSDMMC_Init();
|
||||
|
||||
blkstart = offset / emmcSECTOR_SIZE;
|
||||
inoffset = offset % emmcSECTOR_SIZE;
|
||||
wsize = size + inoffset;
|
||||
blkcnt = (wsize + emmcSECTOR_SIZE - 1) / emmcSECTOR_SIZE;
|
||||
blkend = blkstart + blkcnt - 1;
|
||||
endoffset = wsize % emmcSECTOR_SIZE;
|
||||
|
||||
ptembuf = (uint8_t *)pvPortMalloc(blkcnt * emmcSECTOR_SIZE);
|
||||
if(ptembuf) {
|
||||
if (inoffset) {
|
||||
if (blkstart != cached_sector) {
|
||||
phyRead(cached_data, blkstart, 1);
|
||||
cached_sector = blkstart;
|
||||
}
|
||||
memcpy(ptembuf, cached_data, inoffset);
|
||||
}
|
||||
|
||||
if (data)
|
||||
memcpy(ptembuf + inoffset, data, size);
|
||||
else
|
||||
memset(ptembuf + inoffset, 0xff, size);
|
||||
|
||||
if (cached_sector == blkstart) {
|
||||
if (size > emmcSECTOR_SIZE - inoffset)
|
||||
memcpy(cached_data + inoffset, ptembuf + inoffset, emmcSECTOR_SIZE - inoffset);
|
||||
else
|
||||
memcpy(cached_data + inoffset, ptembuf + inoffset, size);
|
||||
}
|
||||
|
||||
if (endoffset) {
|
||||
if (blkend != cached_sector) {
|
||||
phyRead(cached_data, blkend, 1);
|
||||
cached_sector = blkend;
|
||||
}
|
||||
memcpy(ptembuf + wsize, cached_data + endoffset, emmcSECTOR_SIZE - endoffset);
|
||||
memcpy(cached_data, ptembuf + wsize - endoffset, endoffset);
|
||||
}
|
||||
|
||||
ret = phyWrite(ptembuf,blkstart,blkcnt);
|
||||
if(ptembuf) {
|
||||
free(ptembuf);
|
||||
ptembuf = NULL;
|
||||
}
|
||||
} else
|
||||
printf("emmc_write malloc Error!!1\n");
|
||||
return ret;
|
||||
}
|
||||
|
||||
int emmc_read(uint32_t offset, size_t size, uint8_t *data)
|
||||
{
|
||||
int32_t leftsize = size;
|
||||
int32_t off = offset;
|
||||
uint8_t *buf = data;
|
||||
uint32_t rsize;
|
||||
int ret;
|
||||
|
||||
while (leftsize > 0) {
|
||||
rsize = leftsize > EMMC_RW_MAX_SIZE ? EMMC_RW_MAX_SIZE : leftsize;
|
||||
ret = raw_emmc_read(off, rsize, buf);
|
||||
if (ret)
|
||||
return ret;
|
||||
leftsize -= rsize;
|
||||
off += rsize;
|
||||
buf += rsize;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int emmc_write(uint32_t offset, size_t size, uint8_t *data)
|
||||
{
|
||||
int32_t leftsize = size;
|
||||
int32_t off = offset;
|
||||
uint8_t *buf = data;
|
||||
uint32_t wsize;
|
||||
int ret;
|
||||
|
||||
while (leftsize > 0) {
|
||||
wsize = leftsize > EMMC_RW_MAX_SIZE ? EMMC_RW_MAX_SIZE : leftsize;
|
||||
ret = raw_emmc_write(off, wsize, buf);
|
||||
if (ret)
|
||||
return ret;
|
||||
leftsize -= wsize;
|
||||
off += wsize;
|
||||
if (buf)
|
||||
buf += wsize;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
#endif
|
@ -0,0 +1,451 @@
|
||||
/*
|
||||
* FreeRTOS+FAT build 191128 - Note: FreeRTOS+FAT is still in the lab!
|
||||
* Copyright (C) 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
||||
* Authors include James Walmsley, Hein Tibosch and Richard Barry
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
* this software and associated documentation files (the "Software"), to deal in
|
||||
* the Software without restriction, including without limitation the rights to
|
||||
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
||||
* the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
* subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
||||
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*
|
||||
* https://www.FreeRTOS.org
|
||||
*
|
||||
*/
|
||||
|
||||
/* Standard includes. */
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdio.h>
|
||||
|
||||
/* LPC18xx includes. */
|
||||
#include "chip.h"
|
||||
#include "board.h"
|
||||
|
||||
/* FreeRTOS includes. */
|
||||
#include "FreeRTOS.h"
|
||||
#include "task.h"
|
||||
#include "semphr.h"
|
||||
#include "portmacro.h"
|
||||
|
||||
/* FreeRTOS+FAT includes. */
|
||||
#include "ff_sfdisk.h"
|
||||
#include "ff_sys.h"
|
||||
|
||||
#include "sfud.h"
|
||||
|
||||
/* Misc definitions. */
|
||||
#define sfSIGNATURE 0x41404342UL
|
||||
#define sfHUNDRED_64_BIT ( 100ull )
|
||||
#define sfBYTES_PER_MB ( 1024ull * 1024ull )
|
||||
#define sfSECTOR_SIZE ( 4096ull )
|
||||
#define sfSECTORS_PER_MB ( sfBYTES_PER_MB / sfSECTOR_SIZE )
|
||||
#define sfIOMAN_MEM_SIZE 16384
|
||||
#define sfDISK_MEDIA_OFFSET OTA_MEDIA_OFFSET
|
||||
#define sfAligned( pvAddress ) ( ( ( ( size_t ) ( pvAddress ) ) & ( sizeof( size_t ) - 1 ) ) == 0 )
|
||||
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
/*_RB_ Functions require comment blocks. */
|
||||
static int32_t prvFFRead( uint8_t *pucBuffer, uint32_t ulSectorNumber, uint32_t ulSectorCount, FF_Disk_t *pxDisk );
|
||||
static int32_t prvFFWrite( uint8_t *pucBuffer, uint32_t ulSectorNumber, uint32_t ulSectorCount, FF_Disk_t *pxDisk );
|
||||
|
||||
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
/*_RB_ Variables require a comment block where appropriate. */
|
||||
static sfud_flash *sflash;
|
||||
static SemaphoreHandle_t xPlusFATMutex;
|
||||
|
||||
/*-----------------------------------------------------------*/
|
||||
static int32_t prvFFRead( uint8_t *pucBuffer, uint32_t ulSectorNumber, uint32_t ulSectorCount, FF_Disk_t *pxDisk )
|
||||
{
|
||||
int32_t iReturn;
|
||||
|
||||
/*_RB_ Many of the comments in this file apply to other functions in the file. */
|
||||
|
||||
if( ( pxDisk != NULL ) &&
|
||||
( sflash != NULL ) &&
|
||||
( pxDisk->ulSignature == sfSIGNATURE ) &&
|
||||
( pxDisk->xStatus.bIsInitialised != pdFALSE ) &&
|
||||
( ulSectorNumber < pxDisk->ulNumberOfSectors ) &&
|
||||
( ( pxDisk->ulNumberOfSectors - ulSectorNumber ) >= ulSectorCount ) )
|
||||
{
|
||||
iReturn = sfud_read(sflash, sfDISK_MEDIA_OFFSET + ulSectorNumber * sfSECTOR_SIZE,
|
||||
ulSectorCount * sfSECTOR_SIZE, pucBuffer);
|
||||
|
||||
/*_RB_ I'm guessing 4096 is a sector size, but that needs to be clear.
|
||||
Is it defined in a header somewhere? If so we can do a search and
|
||||
replace in files on it as it seems to be used everywhere. */
|
||||
if( iReturn == 0 ) /*_RB_ Signed/unsigned mismatch (twice!) */
|
||||
{
|
||||
iReturn = FF_ERR_NONE;
|
||||
}
|
||||
else
|
||||
{
|
||||
/*_RB_ Signed number used to return bitmap (again below). */
|
||||
iReturn = ( FF_ERR_IOMAN_OUT_OF_BOUNDS_READ | FF_ERRFLAG );
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
memset( ( void * ) pucBuffer, '\0', ulSectorCount * 512 );
|
||||
|
||||
if( pxDisk->xStatus.bIsInitialised != 0 )
|
||||
{
|
||||
FF_PRINTF( "prvFFRead: warning: %lu + %lu > %lu\n", ulSectorNumber, ulSectorCount, pxDisk->ulNumberOfSectors );
|
||||
}
|
||||
|
||||
iReturn = ( FF_ERR_IOMAN_OUT_OF_BOUNDS_READ | FF_ERRFLAG );
|
||||
}
|
||||
|
||||
return iReturn;
|
||||
}
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
static int32_t prvFFWrite( uint8_t *pucBuffer, uint32_t ulSectorNumber, uint32_t ulSectorCount, FF_Disk_t *pxDisk )
|
||||
{
|
||||
int32_t iReturn;
|
||||
|
||||
if( ( pxDisk != NULL ) &&
|
||||
( sflash != NULL ) &&
|
||||
( pxDisk->ulSignature == sfSIGNATURE ) &&
|
||||
( pxDisk->xStatus.bIsInitialised != pdFALSE ) &&
|
||||
( ulSectorNumber < pxDisk->ulNumberOfSectors ) &&
|
||||
( ( pxDisk->ulNumberOfSectors - ulSectorNumber ) >= ulSectorCount ) )
|
||||
{
|
||||
iReturn = sfud_erase_write(sflash, sfDISK_MEDIA_OFFSET + ulSectorNumber * sfSECTOR_SIZE,
|
||||
ulSectorCount * sfSECTOR_SIZE, pucBuffer);
|
||||
|
||||
if( iReturn == 0 ) /*_RB_ Signed/unsigned mismatch (twice!) */
|
||||
{
|
||||
iReturn = FF_ERR_NONE;
|
||||
}
|
||||
else
|
||||
{
|
||||
iReturn = ( FF_ERR_IOMAN_OUT_OF_BOUNDS_WRITE | FF_ERRFLAG );
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
memset( ( void * ) pucBuffer, '\0', ulSectorCount * 512 );
|
||||
if( pxDisk->xStatus.bIsInitialised )
|
||||
{
|
||||
FF_PRINTF( "prvFFWrite: warning: %lu + %lu > %lu\n", ulSectorNumber, ulSectorCount, pxDisk->ulNumberOfSectors );
|
||||
}
|
||||
|
||||
iReturn = ( FF_ERR_IOMAN_OUT_OF_BOUNDS_WRITE | FF_ERRFLAG );
|
||||
}
|
||||
|
||||
return iReturn;
|
||||
}
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
void FF_SFDiskFlush( FF_Disk_t *pxDisk )
|
||||
{
|
||||
if( ( pxDisk != NULL ) &&
|
||||
( pxDisk->xStatus.bIsInitialised != pdFALSE ) &&
|
||||
( pxDisk->pxIOManager != NULL ) )
|
||||
{
|
||||
FF_FlushCache( pxDisk->pxIOManager );
|
||||
}
|
||||
}
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
#define sfHIDDEN_SECTOR_COUNT 8
|
||||
#define sfPRIMARY_PARTITIONS 1
|
||||
#define sfPARTITION_NUMBER 0 /* Only a single partition is used. */
|
||||
static FF_Error_t PartitionAndFormatSFDisk( FF_Disk_t * pxDisk )
|
||||
{
|
||||
FF_PartitionParameters_t xPartition;
|
||||
FF_Error_t xError;
|
||||
|
||||
/* Create a single partition that fills all available space on the disk. */
|
||||
memset( &xPartition, '\0', sizeof( xPartition ) );
|
||||
xPartition.ulSectorCount = OTA_MEDIA_SIZE / sfSECTOR_SIZE;
|
||||
xPartition.ulHiddenSectors = sfHIDDEN_SECTOR_COUNT;
|
||||
xPartition.xPrimaryCount = sfPRIMARY_PARTITIONS;
|
||||
xPartition.eSizeType = eSizeIsQuota;
|
||||
|
||||
/* Partition the disk */
|
||||
xError = FF_Partition( pxDisk, &xPartition );
|
||||
FF_PRINTF( "FF_Partition: %s\n", ( const char * ) FF_GetErrMessage( xError ) );
|
||||
|
||||
if( FF_isERR( xError ) == pdFALSE )
|
||||
{
|
||||
/* Format the partition. */
|
||||
xError = FF_Format( pxDisk, sfPARTITION_NUMBER, pdFALSE, pdFALSE );
|
||||
FF_PRINTF( "FF_Format: %s\n", ( const char * ) FF_GetErrMessage( xError ) );
|
||||
if ( FF_isERR( xError ) == pdFALSE )
|
||||
{
|
||||
xError = FF_Mount( pxDisk, 0 );
|
||||
FF_PRINTF( "FF_Mount: %s\n", ( const char * ) FF_GetErrMessage( xError ) );
|
||||
FF_SFDiskShowPartition( pxDisk );
|
||||
}
|
||||
}
|
||||
|
||||
return xError;
|
||||
}
|
||||
|
||||
/* mount an spi-flash partition */
|
||||
FF_Disk_t *FF_SFDiskInit( const char *pcName )
|
||||
{
|
||||
FF_Error_t xFFError;
|
||||
BaseType_t xPartitionNumber = 0;
|
||||
FF_CreationParameters_t xParameters;
|
||||
FF_Disk_t * pxDisk;
|
||||
|
||||
sflash = sfud_get_device(0);
|
||||
if( sflash != NULL )
|
||||
{
|
||||
pxDisk = ( FF_Disk_t * ) pvPortMalloc( sizeof( *pxDisk ) );
|
||||
|
||||
if( pxDisk != NULL )
|
||||
{
|
||||
|
||||
/* Initialise the created disk structure. */
|
||||
memset( pxDisk, '\0', sizeof( *pxDisk ) );
|
||||
|
||||
|
||||
if( xPlusFATMutex == NULL)
|
||||
{
|
||||
xPlusFATMutex = xSemaphoreCreateRecursiveMutex();
|
||||
}
|
||||
//pxDisk->ulNumberOfSectors = sflash->chip.capacity / sfSECTOR_SIZE;
|
||||
pxDisk->ulNumberOfSectors = OTA_MEDIA_SIZE / sfSECTOR_SIZE;
|
||||
pxDisk->ulSignature = sfSIGNATURE;
|
||||
|
||||
if( xPlusFATMutex != NULL)
|
||||
{
|
||||
memset( &xParameters, '\0', sizeof( xParameters ) );
|
||||
xParameters.ulMemorySize = sfIOMAN_MEM_SIZE;
|
||||
xParameters.ulSectorSize = sfSECTOR_SIZE;
|
||||
xParameters.fnWriteBlocks = prvFFWrite;
|
||||
xParameters.fnReadBlocks = prvFFRead;
|
||||
xParameters.pxDisk = pxDisk;
|
||||
|
||||
/* prvFFRead()/prvFFWrite() are not re-entrant and must be
|
||||
protected with the use of a semaphore. */
|
||||
xParameters.xBlockDeviceIsReentrant = pdFALSE;
|
||||
|
||||
/* The semaphore will be used to protect critical sections in
|
||||
the +FAT driver, and also to avoid concurrent calls to
|
||||
prvFFRead()/prvFFWrite() from different tasks. */
|
||||
xParameters.pvSemaphore = ( void * ) xPlusFATMutex;
|
||||
|
||||
pxDisk->pxIOManager = FF_CreateIOManger( &xParameters, &xFFError );
|
||||
|
||||
if( pxDisk->pxIOManager == NULL )
|
||||
{
|
||||
FF_PRINTF( "FF_SFDiskInit: FF_CreateIOManger: %s\n", ( const char * ) FF_GetErrMessage( xFFError ) );
|
||||
FF_SFDiskDelete( pxDisk );
|
||||
pxDisk = NULL;
|
||||
}
|
||||
else
|
||||
{
|
||||
pxDisk->xStatus.bIsInitialised = pdTRUE;
|
||||
pxDisk->xStatus.bPartitionNumber = xPartitionNumber;
|
||||
if( FF_SFDiskMount( pxDisk ) == 0 )
|
||||
{
|
||||
if ( PartitionAndFormatSFDisk(pxDisk) != FF_ERR_NONE )
|
||||
{
|
||||
FF_PRINTF( "FF_SFDiskInit: Mount spi-flash fail.\n");
|
||||
FF_SFDiskDelete( pxDisk );
|
||||
pxDisk = NULL;
|
||||
}
|
||||
else
|
||||
{
|
||||
if( pcName == NULL )
|
||||
{
|
||||
pcName = "/";
|
||||
}
|
||||
FF_FS_Add( pcName, pxDisk );
|
||||
FF_PRINTF( "FF_SFDiskInit: Mounted spi-flash as root \"%s\"\n", pcName );
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if( pcName == NULL )
|
||||
{
|
||||
pcName = "/";
|
||||
}
|
||||
FF_FS_Add( pcName, pxDisk );
|
||||
FF_PRINTF( "FF_SFDiskInit: Mounted spi-flash as root \"%s\"\n", pcName );
|
||||
}
|
||||
} /* if( pxDisk->pxIOManager != NULL ) */
|
||||
} /* if( xPlusFATMutex != NULL) */
|
||||
} /* if( pxDisk != NULL ) */
|
||||
else
|
||||
{
|
||||
FF_PRINTF( "FF_SFDiskInit: Malloc failed\n" );
|
||||
}
|
||||
} /* if( sflash != NULL ) */
|
||||
else
|
||||
{
|
||||
FF_PRINTF( "FF_SFDiskInit: sfud_get_device failed\n" );
|
||||
pxDisk = NULL;
|
||||
}
|
||||
|
||||
return pxDisk;
|
||||
}
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
BaseType_t FF_SFDiskFormat( FF_Disk_t *pxDisk, BaseType_t xPartitionNumber )
|
||||
{
|
||||
FF_Error_t xError;
|
||||
BaseType_t xReturn = pdFAIL;
|
||||
|
||||
xError = FF_Unmount( pxDisk );
|
||||
|
||||
if( FF_isERR( xError ) != pdFALSE )
|
||||
{
|
||||
FF_PRINTF( "FF_SFDiskFormat: unmount fails: %08x\n", ( unsigned ) xError );
|
||||
}
|
||||
else
|
||||
{
|
||||
/* Format the drive - try FAT32 with large clusters. */
|
||||
xError = FF_Format( pxDisk, xPartitionNumber, pdFALSE, pdFALSE);
|
||||
|
||||
if( FF_isERR( xError ) )
|
||||
{
|
||||
FF_PRINTF( "FF_SFDiskFormat: %s\n", (const char*)FF_GetErrMessage( xError ) );
|
||||
}
|
||||
else
|
||||
{
|
||||
FF_PRINTF( "FF_SFDiskFormat: OK, now remounting\n" );
|
||||
pxDisk->xStatus.bPartitionNumber = xPartitionNumber;
|
||||
xError = FF_SFDiskMount( pxDisk );
|
||||
FF_PRINTF( "FF_SFDiskFormat: rc %08x\n", ( unsigned )xError );
|
||||
if( FF_isERR( xError ) == pdFALSE )
|
||||
{
|
||||
xReturn = pdPASS;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return xReturn;
|
||||
}
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
/* Get a pointer to IOMAN, which can be used for all FreeRTOS+FAT functions */
|
||||
BaseType_t FF_SFDiskMount( FF_Disk_t *pxDisk )
|
||||
{
|
||||
FF_Error_t xFFError;
|
||||
BaseType_t xReturn;
|
||||
|
||||
/* Mount the partition */
|
||||
xFFError = FF_Mount( pxDisk, pxDisk->xStatus.bPartitionNumber );
|
||||
|
||||
if( FF_isERR( xFFError ) )
|
||||
{
|
||||
FF_PRINTF( "FF_SFDiskMount: %08lX\n", xFFError );
|
||||
xReturn = pdFAIL;
|
||||
}
|
||||
else
|
||||
{
|
||||
pxDisk->xStatus.bIsMounted = pdTRUE;
|
||||
FF_PRINTF( "****** FreeRTOS+FAT initialized %lu sectors\n", pxDisk->pxIOManager->xPartition.ulTotalSectors );
|
||||
FF_SFDiskShowPartition( pxDisk );
|
||||
xReturn = pdPASS;
|
||||
}
|
||||
|
||||
return xReturn;
|
||||
}
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
/* Release all resources */
|
||||
BaseType_t FF_SFDiskDelete( FF_Disk_t *pxDisk )
|
||||
{
|
||||
if( pxDisk != NULL )
|
||||
{
|
||||
pxDisk->ulSignature = 0;
|
||||
pxDisk->xStatus.bIsInitialised = 0;
|
||||
if( pxDisk->pxIOManager != NULL )
|
||||
{
|
||||
if( FF_Mounted( pxDisk->pxIOManager ) != pdFALSE )
|
||||
{
|
||||
FF_Unmount( pxDisk );
|
||||
}
|
||||
FF_DeleteIOManager( pxDisk->pxIOManager );
|
||||
}
|
||||
|
||||
vPortFree( pxDisk );
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
BaseType_t FF_SFDiskShowPartition( FF_Disk_t *pxDisk )
|
||||
{
|
||||
FF_Error_t xError;
|
||||
uint64_t ullFreeSectors;
|
||||
uint32_t ulTotalSizeMB, ulFreeSizeMB;
|
||||
int iPercentageFree;
|
||||
FF_IOManager_t *pxIOManager;
|
||||
const char *pcTypeName = "unknown type";
|
||||
BaseType_t xReturn = pdPASS;
|
||||
|
||||
if( pxDisk == NULL )
|
||||
{
|
||||
xReturn = pdFAIL;
|
||||
}
|
||||
else
|
||||
{
|
||||
pxIOManager = pxDisk->pxIOManager;
|
||||
|
||||
FF_PRINTF( "Reading FAT and calculating Free Space\n" );
|
||||
|
||||
switch( pxIOManager->xPartition.ucType )
|
||||
{
|
||||
case FF_T_FAT12:
|
||||
pcTypeName = "FAT12";
|
||||
break;
|
||||
|
||||
case FF_T_FAT16:
|
||||
pcTypeName = "FAT16";
|
||||
break;
|
||||
|
||||
case FF_T_FAT32:
|
||||
pcTypeName = "FAT32";
|
||||
break;
|
||||
|
||||
default:
|
||||
pcTypeName = "UNKOWN";
|
||||
break;
|
||||
}
|
||||
|
||||
FF_GetFreeSize( pxIOManager, &xError );
|
||||
|
||||
ullFreeSectors = pxIOManager->xPartition.ulFreeClusterCount * pxIOManager->xPartition.ulSectorsPerCluster;
|
||||
iPercentageFree = ( int ) ( ( sfHUNDRED_64_BIT * ullFreeSectors + pxIOManager->xPartition.ulDataSectors / 2 ) /
|
||||
( ( uint64_t )pxIOManager->xPartition.ulDataSectors ) );
|
||||
|
||||
ulTotalSizeMB = pxIOManager->xPartition.ulDataSectors / sfSECTORS_PER_MB;
|
||||
ulFreeSizeMB = ( uint32_t ) ( ullFreeSectors / sfSECTORS_PER_MB );
|
||||
|
||||
/* It is better not to use the 64-bit format such as %Lu because it
|
||||
might not be implemented. */
|
||||
FF_PRINTF( "Partition Nr %8u\n", pxDisk->xStatus.bPartitionNumber );
|
||||
FF_PRINTF( "Type %8u (%s)\n", pxIOManager->xPartition.ucType, pcTypeName );
|
||||
FF_PRINTF( "VolLabel '%8s' \n", pxIOManager->xPartition.pcVolumeLabel );
|
||||
FF_PRINTF( "TotalSectors %8lu\n", pxIOManager->xPartition.ulTotalSectors );
|
||||
FF_PRINTF( "SecsPerCluster %8lu\n", pxIOManager->xPartition.ulSectorsPerCluster );
|
||||
FF_PRINTF( "Size %8lu MB\n", ulTotalSizeMB );
|
||||
FF_PRINTF( "FreeSize %8lu MB ( %d perc free )\n", ulFreeSizeMB, iPercentageFree );
|
||||
}
|
||||
|
||||
return xReturn;
|
||||
}
|
||||
/*-----------------------------------------------------------*/
|
@ -0,0 +1,424 @@
|
||||
/*
|
||||
* FreeRTOS+FAT build 191128 - Note: FreeRTOS+FAT is still in the lab!
|
||||
* Copyright (C) 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
||||
* Authors include James Walmsley, Hein Tibosch and Richard Barry
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
* this software and associated documentation files (the "Software"), to deal in
|
||||
* the Software without restriction, including without limitation the rights to
|
||||
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
||||
* the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
* subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
||||
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*
|
||||
* https://www.FreeRTOS.org
|
||||
*
|
||||
*/
|
||||
|
||||
/* Standard includes. */
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdio.h>
|
||||
|
||||
/* LPC18xx includes. */
|
||||
#include "chip.h"
|
||||
#include "board.h"
|
||||
|
||||
/* FreeRTOS includes. */
|
||||
#include "FreeRTOS.h"
|
||||
#include "task.h"
|
||||
#include "semphr.h"
|
||||
#include "portmacro.h"
|
||||
|
||||
/* FreeRTOS+FAT includes. */
|
||||
#include "ff_usbdisk.h"
|
||||
#include "usb_massstorage.h"
|
||||
#include "ff_sys.h"
|
||||
|
||||
|
||||
/* Misc definitions. */
|
||||
#define usbSIGNATURE 0x41404342UL
|
||||
#define usbHUNDRED_64_BIT ( 100ull )
|
||||
#define usbBYTES_PER_MB ( 1024ull * 1024ull )
|
||||
#define usbSECTORS_PER_MB ( usbBYTES_PER_MB / 512ull )
|
||||
#define usbIOMAN_MEM_SIZE 4096
|
||||
#define usbAligned( pvAddress ) ( ( ( ( size_t ) ( pvAddress ) ) & ( sizeof( size_t ) - 1 ) ) == 0 )
|
||||
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
/*_RB_ Functions require comment blocks. */
|
||||
static int32_t prvUSBDisk_Init( void );
|
||||
static int32_t prvFFUSBDiskRead( uint8_t *pucBuffer, uint32_t ulSectorNumber, uint32_t ulSectorCount, FF_Disk_t *pxDisk );
|
||||
static int32_t prvFFUSBDiskWrite( uint8_t *pucBuffer, uint32_t ulSectorNumber, uint32_t ulSectorCount, FF_Disk_t *pxDisk );
|
||||
|
||||
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
/*_RB_ Variables require a comment block where appropriate. */
|
||||
static struct blk_desc *xUSBInfo;
|
||||
static BaseType_t xUSBDiskStatus;
|
||||
static SemaphoreHandle_t xPlusFATMutex;
|
||||
|
||||
/*-----------------------------------------------------------*/
|
||||
static int32_t prvFFUSBDiskRead( uint8_t *pucBuffer, uint32_t ulSectorNumber, uint32_t ulSectorCount, FF_Disk_t *pxDisk )
|
||||
{
|
||||
int32_t iReturn;
|
||||
|
||||
/*_RB_ Many of the comments in this file apply to other functions in the file. */
|
||||
if( ( pxDisk != NULL ) && NULL != xUSBInfo->block_read &&
|
||||
( xUSBDiskStatus == pdPASS ) &&
|
||||
( pxDisk->ulSignature == usbSIGNATURE ) &&
|
||||
( pxDisk->xStatus.bIsInitialised != pdFALSE ) &&
|
||||
( ulSectorNumber < pxDisk->ulNumberOfSectors ) &&
|
||||
( ( pxDisk->ulNumberOfSectors - ulSectorNumber ) >= ulSectorCount ) )
|
||||
{
|
||||
iReturn = xUSBInfo->block_read(xUSBInfo, ulSectorNumber, ulSectorCount, (void *)pucBuffer);
|
||||
|
||||
/*_RB_ I'm guessing 512 is a sector size, but that needs to be clear.
|
||||
Is it defined in a header somewhere? If so we can do a search and
|
||||
replace in files on it as it seems to be used everywhere. */
|
||||
if( iReturn == 0 ) /*_RB_ Signed/unsigned mismatch (twice!) */
|
||||
{
|
||||
iReturn = FF_ERR_NONE;
|
||||
}
|
||||
else
|
||||
{
|
||||
/*_RB_ Signed number used to return bitmap (again below). */
|
||||
iReturn = ( FF_ERR_IOMAN_OUT_OF_BOUNDS_READ | FF_ERRFLAG );
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
memset( ( void * ) pucBuffer, '\0', ulSectorCount * 512 );
|
||||
|
||||
if( pxDisk->xStatus.bIsInitialised != 0 )
|
||||
{
|
||||
FF_PRINTF( "prvFFRead: warning: %lu + %lu > %lu\n", ulSectorNumber, ulSectorCount, pxDisk->ulNumberOfSectors );
|
||||
}
|
||||
|
||||
iReturn = ( FF_ERR_IOMAN_OUT_OF_BOUNDS_READ | FF_ERRFLAG );
|
||||
}
|
||||
|
||||
return iReturn;
|
||||
}
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
static int32_t prvFFUSBDiskWrite( uint8_t *pucBuffer, uint32_t ulSectorNumber, uint32_t ulSectorCount, FF_Disk_t *pxDisk )
|
||||
{
|
||||
int32_t iReturn;
|
||||
|
||||
if( ( pxDisk != NULL ) && NULL != xUSBInfo->block_write &&
|
||||
( xUSBDiskStatus == pdPASS ) &&
|
||||
( pxDisk->ulSignature == usbSIGNATURE ) &&
|
||||
( pxDisk->xStatus.bIsInitialised != pdFALSE ) &&
|
||||
( ulSectorNumber < pxDisk->ulNumberOfSectors ) &&
|
||||
( ( pxDisk->ulNumberOfSectors - ulSectorNumber ) >= ulSectorCount ) )
|
||||
{
|
||||
iReturn = xUSBInfo->block_write(xUSBInfo, ulSectorNumber, ulSectorCount, (const void *)pucBuffer);
|
||||
|
||||
if( iReturn == 0 ) /*_RB_ Signed/unsigned mismatch (twice!) */
|
||||
{
|
||||
iReturn = FF_ERR_NONE;
|
||||
}
|
||||
else
|
||||
{
|
||||
iReturn = ( FF_ERR_IOMAN_OUT_OF_BOUNDS_WRITE | FF_ERRFLAG );
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
memset( ( void * ) pucBuffer, '\0', ulSectorCount * 512 );
|
||||
if( pxDisk->xStatus.bIsInitialised )
|
||||
{
|
||||
FF_PRINTF( "prvFFWrite: warning: %lu + %lu > %lu\n", ulSectorNumber, ulSectorCount, pxDisk->ulNumberOfSectors );
|
||||
}
|
||||
|
||||
iReturn = ( FF_ERR_IOMAN_OUT_OF_BOUNDS_WRITE | FF_ERRFLAG );
|
||||
}
|
||||
|
||||
return iReturn;
|
||||
}
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
void FF_USBDiskFlush( FF_Disk_t *pxDisk )
|
||||
{
|
||||
if( ( pxDisk != NULL ) &&
|
||||
( pxDisk->xStatus.bIsInitialised != pdFALSE ) &&
|
||||
( pxDisk->pxIOManager != NULL ) )
|
||||
{
|
||||
FF_FlushCache( pxDisk->pxIOManager );
|
||||
}
|
||||
}
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
/* Initialise the USB driver and mount an Udisk */
|
||||
FF_Disk_t *FF_USBDiskInit( const char *pcName )
|
||||
{
|
||||
FF_Error_t xFFError;
|
||||
BaseType_t xPartitionNumber = 0;
|
||||
FF_CreationParameters_t xParameters;
|
||||
FF_Disk_t * pxDisk;
|
||||
|
||||
xUSBDiskStatus = prvUSBDisk_Init();
|
||||
if( xUSBDiskStatus == pdPASS )
|
||||
{
|
||||
pxDisk = ( FF_Disk_t * ) pvPortMalloc( sizeof( *pxDisk ) );
|
||||
|
||||
if( pxDisk != NULL )
|
||||
{
|
||||
|
||||
/* Initialise the created disk structure. */
|
||||
memset( pxDisk, '\0', sizeof( *pxDisk ) );
|
||||
|
||||
|
||||
if( xPlusFATMutex == NULL)
|
||||
{
|
||||
xPlusFATMutex = xSemaphoreCreateRecursiveMutex();
|
||||
}
|
||||
pxDisk->ulNumberOfSectors = xUSBInfo->lba;
|
||||
pxDisk->ulSignature = usbSIGNATURE;
|
||||
|
||||
if( xPlusFATMutex != NULL)
|
||||
{
|
||||
memset( &xParameters, '\0', sizeof( xParameters ) );
|
||||
xParameters.ulMemorySize = usbIOMAN_MEM_SIZE;
|
||||
xParameters.ulSectorSize = 512;
|
||||
xParameters.fnWriteBlocks = prvFFUSBDiskWrite;
|
||||
xParameters.fnReadBlocks = prvFFUSBDiskRead;
|
||||
xParameters.pxDisk = pxDisk;
|
||||
|
||||
/* prvFFRead()/prvFFWrite() are not re-entrant and must be
|
||||
protected with the use of a semaphore. */
|
||||
xParameters.xBlockDeviceIsReentrant = pdFALSE;
|
||||
|
||||
/* The semaphore will be used to protect critical sections in
|
||||
the +FAT driver, and also to avoid concurrent calls to
|
||||
prvFFRead()/prvFFWrite() from different tasks. */
|
||||
xParameters.pvSemaphore = ( void * ) xPlusFATMutex;
|
||||
|
||||
pxDisk->pxIOManager = FF_CreateIOManger( &xParameters, &xFFError );
|
||||
|
||||
if( pxDisk->pxIOManager == NULL )
|
||||
{
|
||||
FF_PRINTF( "FF_USBDiskInit: FF_CreateIOManger: %s\n", ( const char * ) FF_GetErrMessage( xFFError ) );
|
||||
FF_USBDiskDelete( pxDisk );
|
||||
pxDisk = NULL;
|
||||
}
|
||||
else
|
||||
{
|
||||
pxDisk->xStatus.bIsInitialised = pdTRUE;
|
||||
pxDisk->xStatus.bPartitionNumber = xPartitionNumber;
|
||||
if( FF_USBDiskMount( pxDisk ) == 0 )
|
||||
{
|
||||
FF_USBDiskDelete( pxDisk );
|
||||
pxDisk = NULL;
|
||||
}
|
||||
else
|
||||
{
|
||||
if( pcName == NULL )
|
||||
{
|
||||
pcName = "/";
|
||||
}
|
||||
FF_FS_Add( pcName, pxDisk );
|
||||
FF_PRINTF( "FF_USBDiskInit: Mounted Udisk as root \"%s\"\n", pcName );
|
||||
FF_USBDiskShowPartition( pxDisk );
|
||||
}
|
||||
} /* if( pxDisk->pxIOManager != NULL ) */
|
||||
} /* if( xPlusFATMutex != NULL) */
|
||||
} /* if( pxDisk != NULL ) */
|
||||
else
|
||||
{
|
||||
FF_PRINTF( "FF_USBDiskInit: Malloc failed\n" );
|
||||
}
|
||||
} /* if( xUSBDiskStatus == pdPASS ) */
|
||||
else
|
||||
{
|
||||
FF_PRINTF( "FF_USBDiskInit: prvUSBDisk_Init failed\n" );
|
||||
pxDisk = NULL;
|
||||
}
|
||||
|
||||
return pxDisk;
|
||||
}
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
BaseType_t FF_USBDiskFormat( FF_Disk_t *pxDisk, BaseType_t xPartitionNumber )
|
||||
{
|
||||
FF_Error_t xError;
|
||||
BaseType_t xReturn = pdFAIL;
|
||||
|
||||
xError = FF_Unmount( pxDisk );
|
||||
|
||||
if( FF_isERR( xError ) != pdFALSE )
|
||||
{
|
||||
FF_PRINTF( "FF_USBDiskFormat: unmount fails: %08x\n", ( unsigned ) xError );
|
||||
}
|
||||
else
|
||||
{
|
||||
/* Format the drive - try FAT32 with large clusters. */
|
||||
xError = FF_Format( pxDisk, xPartitionNumber, pdFALSE, pdFALSE);
|
||||
|
||||
if( FF_isERR( xError ) )
|
||||
{
|
||||
FF_PRINTF( "FF_USBDiskFormat: %s\n", (const char*)FF_GetErrMessage( xError ) );
|
||||
}
|
||||
else
|
||||
{
|
||||
FF_PRINTF( "FF_USBDiskFormat: OK, now remounting\n" );
|
||||
pxDisk->xStatus.bPartitionNumber = xPartitionNumber;
|
||||
xError = FF_USBDiskMount( pxDisk );
|
||||
FF_PRINTF( "FF_USBDiskFormat: rc %08x\n", ( unsigned )xError );
|
||||
if( FF_isERR( xError ) == pdFALSE )
|
||||
{
|
||||
xReturn = pdPASS;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return xReturn;
|
||||
}
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
/* Get a pointer to IOMAN, which can be used for all FreeRTOS+FAT functions */
|
||||
BaseType_t FF_USBDiskMount( FF_Disk_t *pxDisk )
|
||||
{
|
||||
FF_Error_t xFFError;
|
||||
BaseType_t xReturn;
|
||||
|
||||
/* Mount the partition */
|
||||
xFFError = FF_Mount( pxDisk, pxDisk->xStatus.bPartitionNumber );
|
||||
|
||||
if( FF_isERR( xFFError ) )
|
||||
{
|
||||
FF_PRINTF( "FF_USBDiskMount: %08lX\n", xFFError );
|
||||
xReturn = pdFAIL;
|
||||
}
|
||||
else
|
||||
{
|
||||
pxDisk->xStatus.bIsMounted = pdTRUE;
|
||||
FF_PRINTF( "****** FreeRTOS+FAT initialized %lu sectors\n", pxDisk->pxIOManager->xPartition.ulTotalSectors );
|
||||
FF_USBDiskShowPartition( pxDisk );
|
||||
xReturn = pdPASS;
|
||||
}
|
||||
|
||||
return xReturn;
|
||||
}
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
FF_IOManager_t *usbdisk_ioman( FF_Disk_t *pxDisk )
|
||||
{
|
||||
FF_IOManager_t *pxReturn;
|
||||
|
||||
if( ( pxDisk != NULL ) && ( pxDisk->xStatus.bIsInitialised != pdFALSE ) )
|
||||
{
|
||||
pxReturn = pxDisk->pxIOManager;
|
||||
}
|
||||
else
|
||||
{
|
||||
pxReturn = NULL;
|
||||
}
|
||||
return pxReturn;
|
||||
}
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
/* Release all resources */
|
||||
BaseType_t FF_USBDiskDelete( FF_Disk_t *pxDisk )
|
||||
{
|
||||
if( pxDisk != NULL )
|
||||
{
|
||||
pxDisk->ulSignature = 0;
|
||||
pxDisk->xStatus.bIsInitialised = 0;
|
||||
if( pxDisk->pxIOManager != NULL )
|
||||
{
|
||||
if( FF_Mounted( pxDisk->pxIOManager ) != pdFALSE )
|
||||
{
|
||||
FF_Unmount( pxDisk );
|
||||
}
|
||||
FF_DeleteIOManager( pxDisk->pxIOManager );
|
||||
}
|
||||
|
||||
vPortFree( pxDisk );
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
BaseType_t FF_USBDiskShowPartition( FF_Disk_t *pxDisk )
|
||||
{
|
||||
FF_Error_t xError;
|
||||
uint64_t ullFreeSectors;
|
||||
uint32_t ulTotalSizeMB, ulFreeSizeMB;
|
||||
int iPercentageFree;
|
||||
FF_IOManager_t *pxIOManager;
|
||||
const char *pcTypeName = "unknown type";
|
||||
BaseType_t xReturn = pdPASS;
|
||||
|
||||
if( pxDisk == NULL )
|
||||
{
|
||||
xReturn = pdFAIL;
|
||||
}
|
||||
else
|
||||
{
|
||||
pxIOManager = pxDisk->pxIOManager;
|
||||
|
||||
FF_PRINTF( "Reading FAT and calculating Free Space\n" );
|
||||
|
||||
switch( pxIOManager->xPartition.ucType )
|
||||
{
|
||||
case FF_T_FAT12:
|
||||
pcTypeName = "FAT12";
|
||||
break;
|
||||
|
||||
case FF_T_FAT16:
|
||||
pcTypeName = "FAT16";
|
||||
break;
|
||||
|
||||
case FF_T_FAT32:
|
||||
pcTypeName = "FAT32";
|
||||
break;
|
||||
|
||||
default:
|
||||
pcTypeName = "UNKOWN";
|
||||
break;
|
||||
}
|
||||
|
||||
FF_GetFreeSize( pxIOManager, &xError );
|
||||
|
||||
ullFreeSectors = pxIOManager->xPartition.ulFreeClusterCount * pxIOManager->xPartition.ulSectorsPerCluster;
|
||||
iPercentageFree = ( int ) ( ( usbHUNDRED_64_BIT * ullFreeSectors + pxIOManager->xPartition.ulDataSectors / 2 ) /
|
||||
( ( uint64_t )pxIOManager->xPartition.ulDataSectors ) );
|
||||
|
||||
ulTotalSizeMB = pxIOManager->xPartition.ulDataSectors / usbSECTORS_PER_MB;
|
||||
ulFreeSizeMB = ( uint32_t ) ( ullFreeSectors / usbSECTORS_PER_MB );
|
||||
|
||||
/* It is better not to use the 64-bit format such as %Lu because it
|
||||
might not be implemented. */
|
||||
FF_PRINTF( "Partition Nr %8u\n", pxDisk->xStatus.bPartitionNumber );
|
||||
FF_PRINTF( "Type %8u (%s)\n", pxIOManager->xPartition.ucType, pcTypeName );
|
||||
FF_PRINTF( "VolLabel '%8s' \n", pxIOManager->xPartition.pcVolumeLabel );
|
||||
FF_PRINTF( "TotalSectors %8lu\n", pxIOManager->xPartition.ulTotalSectors );
|
||||
FF_PRINTF( "SecsPerCluster %8lu\n", pxIOManager->xPartition.ulSectorsPerCluster );
|
||||
FF_PRINTF( "Size %8lu MB\n", ulTotalSizeMB );
|
||||
FF_PRINTF( "FreeSize %8lu MB ( %d perc free )\n", ulFreeSizeMB, iPercentageFree );
|
||||
}
|
||||
|
||||
return xReturn;
|
||||
}
|
||||
/*-----------------------------------------------------------*/
|
||||
|
||||
static int32_t prvUSBDisk_Init( void )
|
||||
{
|
||||
xUSBInfo = &usb_dev_desc[0];//use the first device
|
||||
return pdPASS;
|
||||
}
|
||||
/*-----------------------------------------------------------*/
|
||||
|
Reference in New Issue
Block a user