From c689d979615af353afac26fe25f0608f535ff18a Mon Sep 17 00:00:00 2001 From: James Cotton Date: Sat, 14 May 2011 14:28:11 -0500 Subject: [PATCH] OP-216: Improved error handling --- flight/PiOS/Common/pios_flash_w25x.c | 8 ++++++++ flight/PiOS/Common/pios_flashfs_objlist.c | 22 ++++++++++++++-------- 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/flight/PiOS/Common/pios_flash_w25x.c b/flight/PiOS/Common/pios_flash_w25x.c index b1c602422..624f76ef8 100644 --- a/flight/PiOS/Common/pios_flash_w25x.c +++ b/flight/PiOS/Common/pios_flash_w25x.c @@ -222,6 +222,14 @@ int8_t PIOS_Flash_W25X_WriteData(uint32_t addr, uint8_t * data, uint16_t len) return 0; } +/** + * @brief Read data from a location in flash memory + * @param[in] addr Address in flash to write to + * @param[in] data Pointer to data to write from flash + * @param[in] len Length of data to write (max 256 bytes) + * @return Zero if success or error code + * @retval -1 Unable to claim SPI bus + */ int8_t PIOS_Flash_W25X_ReadData(uint32_t addr, uint8_t * data, uint16_t len) { if(PIOS_Flash_W25X_ClaimBus() == -1) diff --git a/flight/PiOS/Common/pios_flashfs_objlist.c b/flight/PiOS/Common/pios_flashfs_objlist.c index 5e403599b..4f39125db 100644 --- a/flight/PiOS/Common/pios_flashfs_objlist.c +++ b/flight/PiOS/Common/pios_flashfs_objlist.c @@ -208,14 +208,18 @@ int32_t PIOS_FLASHFS_ObjSave(UAVObjHandle obj, uint16_t instId, uint8_t * data) .size = UAVObjGetNumBytes(obj) }; - PIOS_Flash_W25X_EraseSector(addr); + if(PIOS_Flash_W25X_EraseSector(addr) != 0) + return -2; // Save header // This information IS redundant with the object table id. Oh well. Better safe than sorry. - PIOS_Flash_W25X_WriteData(addr, (uint8_t *) &header, sizeof(header)); + + if(PIOS_Flash_W25X_WriteData(addr, (uint8_t *) &header, sizeof(header)) != 0) + return -3; // Save data - PIOS_Flash_W25X_WriteData(addr + sizeof(header), data, UAVObjGetNumBytes(obj)); + if(PIOS_Flash_W25X_WriteData(addr + sizeof(header), data, UAVObjGetNumBytes(obj)) != 0) + return -4; return 0; } @@ -226,8 +230,9 @@ int32_t PIOS_FLASHFS_ObjSave(UAVObjHandle obj, uint16_t instId, uint8_t * data) * @param[in] instId The instance of the object to save * @return 0 if success or error code * @retval -1 if object not in file table - * @retval -2 if loaded data instId or objId don't match - * @retval -3 if unable to retrieve instance data + * @retval -2 if unable to retrieve object header + * @retval -3 if loaded data instId or objId don't match + * @retval -4 if unable to retrieve instance data * @note This uses one sector on the flash chip per object so that no buffering in ram * must be done when erasing the sector before a save */ @@ -245,14 +250,15 @@ int32_t PIOS_FLASHFS_ObjLoad(UAVObjHandle obj, uint16_t instId, uint8_t * data) // Load header // This information IS redundant with the object table id. Oh well. Better safe than sorry. - PIOS_Flash_W25X_ReadData(addr, (uint8_t *) &header, sizeof(header)); + if(PIOS_Flash_W25X_ReadData(addr, (uint8_t *) &header, sizeof(header)) != 0) + return -2; if((header.id != objId) || (header.instId != instId)) - return -2; + return -3; // Read the instance data if (PIOS_Flash_W25X_ReadData(addr + sizeof(header), data, UAVObjGetNumBytes(obj)) != 0) - return -3; + return -4; return 0; }