From 97acaa2402ffe3be296befeff8b739139bcc06b4 Mon Sep 17 00:00:00 2001 From: peabody124 Date: Tue, 1 Feb 2011 02:18:12 +0000 Subject: [PATCH] CC-15 Gets settings saved to flash chip on CC using crude sector based storage. Sector based on Object ID so could overlap :-(. git-svn-id: svn://svn.openpilot.org/OpenPilot/trunk@2662 ebee16cc-31ac-478f-84a7-5cbb03baadba --- flight/CopterControl/Makefile | 1 - flight/CopterControl/System/inc/pios_config.h | 5 +- flight/CopterControl/System/pios_board.c | 1 + flight/PiOS/Common/pios_flash_w25x.c | 7 +- flight/PiOS/inc/pios_flash_w25x.h | 1 + flight/UAVObjects/uavobjectmanager.c | 64 +++++++++++++++++++ 6 files changed, 73 insertions(+), 6 deletions(-) diff --git a/flight/CopterControl/Makefile b/flight/CopterControl/Makefile index 5c8af2a14..a6359047d 100644 --- a/flight/CopterControl/Makefile +++ b/flight/CopterControl/Makefile @@ -183,7 +183,6 @@ SRC += $(OPUAVSYNTHDIR)/ratedesired.c SRC += $(OPUAVSYNTHDIR)/manualcontrolsettings.c SRC += $(OPUAVSYNTHDIR)/mixersettings.c SRC += $(OPUAVSYNTHDIR)/mixerstatus.c -SRC += $(OPUAVSYNTHDIR)/ahrssettings.c SRC += $(OPUAVSYNTHDIR)/firmwareiapobj.c SRC += $(OPUAVSYNTHDIR)/uavobjectsinit.c #${wildcard ${OBJ}/$(shell echo $(VAR) | tr A-Z a-z)/*.c} diff --git a/flight/CopterControl/System/inc/pios_config.h b/flight/CopterControl/System/inc/pios_config.h index a13a8fce7..c735d53f6 100644 --- a/flight/CopterControl/System/inc/pios_config.h +++ b/flight/CopterControl/System/inc/pios_config.h @@ -55,12 +55,15 @@ #define PIOS_INCLUDE_FREERTOS #define PIOS_INCLUDE_GPIO #define PIOS_INCLUDE_EXTI -#define PIOS_INCLUDE_WDG +//#define PIOS_INCLUDE_WDG #define PIOS_INCLUDE_I2C_ESC #define PIOS_INCLUDE_ADXL345 #define PIOS_INCLUDE_FLASH +/* A really shitty setting saving implementation */ +#define PIOS_INCLUDE_FLASH_SECTOR_SETTINGS + /* Defaults for Logging */ #define LOG_FILENAME "PIOS.LOG" #define STARTUP_LOG_ENABLED 1 diff --git a/flight/CopterControl/System/pios_board.c b/flight/CopterControl/System/pios_board.c index 633e73083..ab842b4be 100644 --- a/flight/CopterControl/System/pios_board.c +++ b/flight/CopterControl/System/pios_board.c @@ -47,6 +47,7 @@ void PIOS_Board_Init(void) { /* SPI Init */ PIOS_SPI_Init(); + PIOS_Flash_W25X_Init(); #if defined(PIOS_INCLUDE_SPEKTRUM) /* SPEKTRUM init must come before comms */ diff --git a/flight/PiOS/Common/pios_flash_w25x.c b/flight/PiOS/Common/pios_flash_w25x.c index 680c906cb..a37f758b8 100644 --- a/flight/PiOS/Common/pios_flash_w25x.c +++ b/flight/PiOS/Common/pios_flash_w25x.c @@ -38,7 +38,6 @@ static int8_t PIOS_Flash_W25X_ClaimBus(); static void PIOS_Flash_W25X_ReleaseBus(); static uint8_t PIOS_Flash_W25X_WriteEnable(); static uint8_t PIOS_Flash_W25X_Busy() ; -//static uint8_t PIOS_Flash_W25X_EraseChip(); /** * @brief Claim the SPI bus for flash use and assert CS pin @@ -129,7 +128,7 @@ uint8_t PIOS_Flash_W25X_ReadID() int8_t PIOS_Flash_W25X_EraseSector(uint32_t addr) { uint8_t ret; - uint8_t out[] = {W25X_SECTOR_ERASE, (addr >> 16) && 0xff, (addr >> 8) && 0xff , addr && 0xff}; + uint8_t out[] = {W25X_SECTOR_ERASE, (addr >> 16) & 0xff, (addr >> 8) & 0xff , addr & 0xff}; if((ret = PIOS_Flash_W25X_WriteEnable()) != 0) return ret; @@ -153,7 +152,7 @@ int8_t PIOS_Flash_W25X_EraseSector(uint32_t addr) * @brief Execute the whole chip * @returns 0 if successful, -1 if unable to claim bus */ -/*static uint8_t PIOS_Flash_W25X_EraseChip() +int8_t PIOS_Flash_W25X_EraseChip() { uint8_t ret; uint8_t out[] = {W25X_CHIP_ERASE}; @@ -172,7 +171,7 @@ int8_t PIOS_Flash_W25X_EraseSector(uint32_t addr) #endif } return 0; -}*/ +} /** diff --git a/flight/PiOS/inc/pios_flash_w25x.h b/flight/PiOS/inc/pios_flash_w25x.h index e9a8715be..9d6fd49a5 100644 --- a/flight/PiOS/inc/pios_flash_w25x.h +++ b/flight/PiOS/inc/pios_flash_w25x.h @@ -10,6 +10,7 @@ int8_t PIOS_Flash_W25X_Init(); uint8_t PIOS_Flash_W25X_ReadStatus(); uint8_t PIOS_Flash_W25X_ReadID(); +int8_t PIOS_Flash_W25X_EraseChip(); int8_t PIOS_Flash_W25X_EraseSector(uint32_t add); int8_t PIOS_Flash_W25X_WriteData(uint32_t addr, uint8_t * data, uint16_t len); int8_t PIOS_Flash_W25X_ReadData(uint32_t addr, uint8_t * data, uint16_t len); diff --git a/flight/UAVObjects/uavobjectmanager.c b/flight/UAVObjects/uavobjectmanager.c index 529ea2479..ac6a8445f 100644 --- a/flight/UAVObjects/uavobjectmanager.c +++ b/flight/UAVObjects/uavobjectmanager.c @@ -562,6 +562,14 @@ int32_t UAVObjSaveToFile(UAVObjHandle obj, uint16_t instId, FILEINFO* file) return 0; } +struct fileHeader { + uint32_t id; + uint16_t instId; + uint16_t size; +} __attribute__((packed)); + +#define FLASH_MASK 0x001ff000 /* Select a sector */ + /** * Save the data of the specified object to the file system (SD card). * If the object contains multiple instances, all of them will be saved. @@ -574,6 +582,32 @@ int32_t UAVObjSaveToFile(UAVObjHandle obj, uint16_t instId, FILEINFO* file) */ int32_t UAVObjSave(UAVObjHandle obj, uint16_t instId) { +#if defined(PIOS_INCLUDE_FLASH_SECTOR_SETTINGS) + ObjectList* objEntry = (ObjectList*)obj; + + if(objEntry == NULL) + return -1; + + ObjectInstList* instEntry = getInstance(objEntry, instId); + + if(instEntry == NULL) + return -1; + + if(instEntry->data == NULL) + return -1; + + + struct fileHeader header = { + .id = objEntry->id, + .instId = instId, + .size = objEntry->numBytes + }; + + uint32_t addr = (objEntry->id & FLASH_MASK); + PIOS_Flash_W25X_EraseSector(addr); + PIOS_Flash_W25X_WriteData(addr, (uint8_t *) &header, sizeof(header)); + PIOS_Flash_W25X_WriteData(addr + sizeof(header), instEntry->data,objEntry->numBytes); +#endif #if defined(PIOS_INCLUDE_SDCARD) FILEINFO file; ObjectList* objEntry; @@ -710,6 +744,36 @@ UAVObjHandle UAVObjLoadFromFile(FILEINFO* file) */ int32_t UAVObjLoad(UAVObjHandle obj, uint16_t instId) { +#if defined(PIOS_INCLUDE_FLASH_SECTOR_SETTINGS) + ObjectList* objEntry = (ObjectList*)obj; + + if(objEntry == NULL) + return -1; + + ObjectInstList* instEntry = getInstance(objEntry, instId); + + if(instEntry == NULL) + return -1; + + if(instEntry->data == NULL) + return -1; + + struct fileHeader header; + uint32_t addr = (objEntry->id & FLASH_MASK); + + PIOS_Flash_W25X_ReadData(addr, (uint8_t *) &header, sizeof(header)); + + if(header.id != objEntry->id) + return -1; + + // Read the instance data + if (PIOS_Flash_W25X_ReadData(addr + sizeof(header) ,instEntry->data, objEntry->numBytes) != 0) + return -1; + + // Fire event + sendEvent(objEntry, instId, EV_UNPACKED); +#endif + #if defined(PIOS_INCLUDE_SDCARD) FILEINFO file; ObjectList* objEntry;