1
0
Fork 0
mirror of https://github.com/betaflight/betaflight.git synced 2025-07-18 22:05:17 +03:00

More detailed afatfs state reporting for CLI

This commit is contained in:
Nicholas Sherlock 2015-11-26 16:30:04 +13:00 committed by borisbstyle
parent 9389cc4a29
commit 4700bb638c
4 changed files with 76 additions and 26 deletions

View file

@ -119,31 +119,23 @@ void sdcardInsertionDetectInit(void)
} }
/** /**
* @brief Detect if SD card is correctly plugged in the memory slot. * Detect if a SD card is physically present in the memory slot.
* @param None
* @retval Return if SD is detected or not
*/ */
bool sdcard_isInserted(void) bool sdcard_isInserted(void)
{ {
bool result = true;
#ifdef SDCARD_DETECT_PIN #ifdef SDCARD_DETECT_PIN
/*!< Check GPIO to detect SD */
if ((GPIO_ReadInputData(SDCARD_DETECT_GPIO_PORT) & SDCARD_DETECT_PIN) != 0) result = (GPIO_ReadInputData(SDCARD_DETECT_GPIO_PORT) & SDCARD_DETECT_PIN) != 0;
{
#ifdef SD_DETECT_INVERTED #ifdef SDCARD_DETECT_INVERTED
return false; result = !result;
#else
return true;
#endif #endif
} else {
#ifdef SD_DETECT_INVERTED
return true;
#else
return false;
#endif
}
#else
return true;
#endif #endif
return result;
} }
static void sdcard_select() static void sdcard_select()
@ -247,7 +239,7 @@ static uint8_t sdcard_sendAppCommand(uint8_t commandCode, uint32_t commandArgume
/** /**
* Sends an IF_COND message to the card to check its version and validate its voltage requirements. Sets the global * Sends an IF_COND message to the card to check its version and validate its voltage requirements. Sets the global
* sdCardVersion with the detected version (0, 1, or 2) and returns true if the card is compatbile. * sdCardVersion with the detected version (0, 1, or 2) and returns true if the card is compatible.
*/ */
static bool sdcard_validateInterfaceCondition() static bool sdcard_validateInterfaceCondition()
{ {

View file

@ -437,6 +437,8 @@ typedef struct afatfs_t {
afatfsFile_t freeFile; afatfsFile_t freeFile;
#endif #endif
afatfsError_e lastError;
bool filesystemFull; bool filesystemFull;
// The current working directory: // The current working directory:
@ -500,6 +502,9 @@ static bool isPowerOfTwo(unsigned int x)
static bool afatfs_assert(bool condition) static bool afatfs_assert(bool condition)
{ {
if (!condition) { if (!condition) {
if (afatfs.lastError == AFATFS_ERROR_NONE) {
afatfs.lastError = AFATFS_ERROR_GENERIC;
}
afatfs.filesystemState = AFATFS_FILESYSTEM_STATE_FATAL; afatfs.filesystemState = AFATFS_FILESYSTEM_STATE_FATAL;
#ifdef AFATFS_DEBUG #ifdef AFATFS_DEBUG
raise(SIGTRAP); raise(SIGTRAP);
@ -942,6 +947,8 @@ static bool afatfs_parseVolumeID(const uint8_t *sector)
{ {
fatVolumeID_t *volume = (fatVolumeID_t *) sector; fatVolumeID_t *volume = (fatVolumeID_t *) sector;
afatfs.filesystemType = FAT_FILESYSTEM_TYPE_INVALID;
if (volume->bytesPerSector != AFATFS_SECTOR_SIZE || volume->numFATs != AFATFS_NUM_FATS if (volume->bytesPerSector != AFATFS_SECTOR_SIZE || volume->numFATs != AFATFS_NUM_FATS
|| sector[510] != FAT_VOLUME_ID_SIGNATURE_1 || sector[511] != FAT_VOLUME_ID_SIGNATURE_2) { || sector[510] != FAT_VOLUME_ID_SIGNATURE_1 || sector[511] != FAT_VOLUME_ID_SIGNATURE_2) {
return false; return false;
@ -967,7 +974,6 @@ static bool afatfs_parseVolumeID(const uint8_t *sector)
if (afatfs.numClusters <= FAT12_MAX_CLUSTERS) { if (afatfs.numClusters <= FAT12_MAX_CLUSTERS) {
afatfs.filesystemType = FAT_FILESYSTEM_TYPE_FAT12; afatfs.filesystemType = FAT_FILESYSTEM_TYPE_FAT12;
afatfs.filesystemState = AFATFS_FILESYSTEM_STATE_FATAL;
return false; // FAT12 is not a supported filesystem return false; // FAT12 is not a supported filesystem
} else if (afatfs.numClusters <= FAT16_MAX_CLUSTERS) { } else if (afatfs.numClusters <= FAT16_MAX_CLUSTERS) {
@ -987,8 +993,6 @@ static bool afatfs_parseVolumeID(const uint8_t *sector)
afatfs.clusterStartSector = endOfFATs + afatfs.rootDirectorySectors; afatfs.clusterStartSector = endOfFATs + afatfs.rootDirectorySectors;
afatfs_chdir(NULL);
return true; return true;
} }
@ -3245,6 +3249,7 @@ static void afatfs_freeFileCreated(afatfsFile_t *file)
} }
} else { } else {
// Failed to allocate an entry // Failed to allocate an entry
afatfs.lastError = AFATFS_ERROR_GENERIC;
afatfs.filesystemState = AFATFS_FILESYSTEM_STATE_FATAL; afatfs.filesystemState = AFATFS_FILESYSTEM_STATE_FATAL;
} }
} }
@ -3268,6 +3273,7 @@ static void afatfs_initContinue()
afatfs.initPhase = AFATFS_INITIALIZATION_READ_VOLUME_ID; afatfs.initPhase = AFATFS_INITIALIZATION_READ_VOLUME_ID;
goto doMore; goto doMore;
} else { } else {
afatfs.lastError = AFATFS_ERROR_BAD_MBR;
afatfs.filesystemState = AFATFS_FILESYSTEM_STATE_FATAL; afatfs.filesystemState = AFATFS_FILESYSTEM_STATE_FATAL;
} }
} }
@ -3286,6 +3292,7 @@ static void afatfs_initContinue()
afatfs.filesystemState = AFATFS_FILESYSTEM_STATE_READY; afatfs.filesystemState = AFATFS_FILESYSTEM_STATE_READY;
#endif #endif
} else { } else {
afatfs.lastError = AFATFS_ERROR_BAD_FILESYSTEM_HEADER;
afatfs.filesystemState = AFATFS_FILESYSTEM_STATE_FATAL; afatfs.filesystemState = AFATFS_FILESYSTEM_STATE_FATAL;
} }
} }
@ -3339,6 +3346,7 @@ static void afatfs_initContinue()
goto doMore; goto doMore;
} else if (status == AFATFS_OPERATION_FAILURE) { } else if (status == AFATFS_OPERATION_FAILURE) {
afatfs.lastError = AFATFS_ERROR_GENERIC;
afatfs.filesystemState = AFATFS_FILESYSTEM_STATE_FATAL; afatfs.filesystemState = AFATFS_FILESYSTEM_STATE_FATAL;
} }
break; break;
@ -3348,6 +3356,7 @@ static void afatfs_initContinue()
if (status == AFATFS_OPERATION_SUCCESS) { if (status == AFATFS_OPERATION_SUCCESS) {
afatfs.filesystemState = AFATFS_FILESYSTEM_STATE_READY; afatfs.filesystemState = AFATFS_FILESYSTEM_STATE_READY;
} else if (status == AFATFS_OPERATION_FAILURE) { } else if (status == AFATFS_OPERATION_FAILURE) {
afatfs.lastError = AFATFS_ERROR_GENERIC;
afatfs.filesystemState = AFATFS_FILESYSTEM_STATE_FATAL; afatfs.filesystemState = AFATFS_FILESYSTEM_STATE_FATAL;
} }
break; break;
@ -3382,6 +3391,11 @@ afatfsFilesystemState_e afatfs_getFilesystemState()
return afatfs.filesystemState; return afatfs.filesystemState;
} }
afatfsError_e afatfs_getLastError()
{
return afatfs.lastError;
}
void afatfs_init() void afatfs_init()
{ {
afatfs.filesystemState = AFATFS_FILESYSTEM_STATE_INITIALIZATION; afatfs.filesystemState = AFATFS_FILESYSTEM_STATE_INITIALIZATION;

View file

@ -37,6 +37,13 @@ typedef enum {
AFATFS_OPERATION_FAILURE, AFATFS_OPERATION_FAILURE,
} afatfsOperationStatus_e; } afatfsOperationStatus_e;
typedef enum {
AFATFS_ERROR_NONE = 0,
AFATFS_ERROR_GENERIC = 1,
AFATFS_ERROR_BAD_MBR = 2,
AFATFS_ERROR_BAD_FILESYSTEM_HEADER = 3
} afatfsError_e;
typedef struct afatfsDirEntryPointer_t { typedef struct afatfsDirEntryPointer_t {
uint32_t sectorNumberPhysical; uint32_t sectorNumberPhysical;
int16_t entryIndex; int16_t entryIndex;
@ -82,3 +89,4 @@ uint32_t afatfs_getContiguousFreeSpace();
bool afatfs_isFull(); bool afatfs_isFull();
afatfsFilesystemState_e afatfs_getFilesystemState(); afatfsFilesystemState_e afatfs_getFilesystemState();
afatfsError_e afatfs_getLastError();

View file

@ -57,6 +57,7 @@
#include "io/ledstrip.h" #include "io/ledstrip.h"
#include "io/flashfs.h" #include "io/flashfs.h"
#include "io/beeper.h" #include "io/beeper.h"
#include "io/asyncfatfs/asyncfatfs.h"
#include "rx/rx.h" #include "rx/rx.h"
#include "rx/spektrum.h" #include "rx/spektrum.h"
@ -1508,14 +1509,21 @@ static void cliWriteBytes(const uint8_t *buffer, int count)
static void cliSdInfo(char *cmdline) { static void cliSdInfo(char *cmdline) {
UNUSED(cmdline); UNUSED(cmdline);
cliPrint("SD card: ");
if (!sdcard_isInserted()) {
cliPrint("None inserted\r\n");
return;
}
if (!sdcard_isInitialized()) { if (!sdcard_isInitialized()) {
cliPrint("sd card not present\r\n"); cliPrint("Startup failed\r\n");
return; return;
} }
const sdcardMetadata_t *metadata = sdcard_getMetadata(); const sdcardMetadata_t *metadata = sdcard_getMetadata();
cliPrintf("manufacturer 0x%x, capacity: %ukB, %02d/%04d, v%d.%d, '", cliPrintf("Manufacturer 0x%x, %ukB, %02d/%04d, v%d.%d, '",
metadata->manufacturerID, metadata->manufacturerID,
metadata->numBlocks / 2, /* One block is half a kB */ metadata->numBlocks / 2, /* One block is half a kB */
metadata->productionMonth, metadata->productionMonth,
@ -1526,7 +1534,35 @@ static void cliSdInfo(char *cmdline) {
cliWriteBytes((uint8_t*)metadata->productName, sizeof(metadata->productName)); cliWriteBytes((uint8_t*)metadata->productName, sizeof(metadata->productName));
cliPrint("'\r\n"); cliPrint("'\r\n" "Filesystem: ");
switch (afatfs_getFilesystemState()) {
case AFATFS_FILESYSTEM_STATE_READY:
cliPrint("Ready");
break;
case AFATFS_FILESYSTEM_STATE_INITIALIZATION:
cliPrint("Initializing");
break;
case AFATFS_FILESYSTEM_STATE_UNKNOWN:
case AFATFS_FILESYSTEM_STATE_FATAL:
cliPrint("Fatal");
switch (afatfs_getLastError()) {
case AFATFS_ERROR_BAD_MBR:
cliPrint(" - no FAT MBR partitions");
break;
case AFATFS_ERROR_BAD_FILESYSTEM_HEADER:
cliPrint(" - bad FAT header");
break;
case AFATFS_ERROR_GENERIC:
case AFATFS_ERROR_NONE:
; // Nothing more detailed to print
break;
}
cliPrint("\r\n");
break;
}
} }
#endif #endif