From b24866225a6301d3a663f874725e83c012dc25d3 Mon Sep 17 00:00:00 2001 From: Florent Duchon Date: Wed, 26 Dec 2012 17:36:00 +0100 Subject: digital/beacon: add bitcloud stack into common directory digital/zigbit --- .../stack/Components/PersistDataServer/Makefile | 73 ++++++ .../PersistDataServer/include/pdsDataServer.h | 99 +++++++ .../include/private/pdsAuxService.h | 80 ++++++ .../include/private/pdsCrcService.h | 73 ++++++ .../PersistDataServer/include/private/pdsDbg.h | 37 +++ .../include/private/pdsMemAbstract.h | 70 +++++ .../include/private/pdsWriteData.h | 84 ++++++ .../PersistDataServer/src/pdsCrcService.c | 217 ++++++++++++++++ .../PersistDataServer/src/pdsDataServer.c | 206 +++++++++++++++ .../PersistDataServer/src/pdsWriteData.c | 286 +++++++++++++++++++++ 10 files changed, 1225 insertions(+) create mode 100644 digital/zigbit/bitcloud/stack/Components/PersistDataServer/Makefile create mode 100644 digital/zigbit/bitcloud/stack/Components/PersistDataServer/include/pdsDataServer.h create mode 100644 digital/zigbit/bitcloud/stack/Components/PersistDataServer/include/private/pdsAuxService.h create mode 100644 digital/zigbit/bitcloud/stack/Components/PersistDataServer/include/private/pdsCrcService.h create mode 100644 digital/zigbit/bitcloud/stack/Components/PersistDataServer/include/private/pdsDbg.h create mode 100644 digital/zigbit/bitcloud/stack/Components/PersistDataServer/include/private/pdsMemAbstract.h create mode 100644 digital/zigbit/bitcloud/stack/Components/PersistDataServer/include/private/pdsWriteData.h create mode 100644 digital/zigbit/bitcloud/stack/Components/PersistDataServer/src/pdsCrcService.c create mode 100644 digital/zigbit/bitcloud/stack/Components/PersistDataServer/src/pdsDataServer.c create mode 100644 digital/zigbit/bitcloud/stack/Components/PersistDataServer/src/pdsWriteData.c (limited to 'digital/zigbit/bitcloud/stack/Components/PersistDataServer') diff --git a/digital/zigbit/bitcloud/stack/Components/PersistDataServer/Makefile b/digital/zigbit/bitcloud/stack/Components/PersistDataServer/Makefile new file mode 100644 index 00000000..1983e28b --- /dev/null +++ b/digital/zigbit/bitcloud/stack/Components/PersistDataServer/Makefile @@ -0,0 +1,73 @@ +COMPONENTS_PATH = .. +include $(MAKERULES) + +# Compiler flags extending with Confiuration server parameters. +CFLAGS += $(CFLAGS_CS) + +# Compiler flags extending with "debug info" compiler flag. +CFLAGS += $(CFLAGS_DBG) + +# Hardware flags. +CFLAGS += $(CFLAGS_HW) + +##### PATHS FLAGS OF INCLUDES ######### +CFLAGS += -I$(PDS_PATH)/include +CFLAGS += -I$(PDS_PATH)/include/private +CFLAGS += -I$(CS_PATH)/include +CFLAGS += -I$(BSP_PATH)/include +CFLAGS += -I$(SE_PATH)/include +CFLAGS += -I$(ZDO_PATH)/include +CFLAGS += -I$(NWK_PATH)/include +CFLAGS += -I$(APS_PATH)/include +CFLAGS += -I$(APS_PATH)/include/private +CFLAGS += -I$(MAC_PHY_PATH)/include +CFLAGS += -I$(HAL_PATH)/include +CFLAGS += -I$(HAL_HWD_COMMON_PATH)/include +CFLAGS += -I$(SSP_PATH)/include +CFLAGS += -I$(CS_PATH)/include/private +CFLAGS += -I$(MAC_PHY_PATH)/MAC_ENV/include +CFLAGS += -I$(MAC_PHY_PATH)/MAC_HWD_PHY/include +CFLAGS += -I$(MAC_PHY_PATH)/MAC_HWI/include + +#We take address of packed structure members in pdsWriteData.c, IAR for ARM don't like this. +#Suppress this warning, code is correct. +ifeq ($(COMPILER_AND_MICRO_TYPE), IAR_ARM) + CFLAGS += --diag_suppress Pa039 +endif + +###### LIB ######### +BUILDDIR = . +LIBDIR = $(BUILDDIR)/lib +LISTDIR = $(BUILDDIR)/list +LDFLAGS = -L$(LIBDIR) + +objects = \ +$(BUILDDIR)/objs/pdsDataServer.o \ +$(BUILDDIR)/objs/pdsWriteData.o \ +$(BUILDDIR)/objs/pdsCrcService.o +###### TARGETS ################ +all: component_label LIB_PERSIST_DATA_SERVER +component_label: + @echo + @echo ---------------------------------------------------- + @echo Persist Data Server library creation. + @echo ---------------------------------------------------- + +################ +$(objects): + $(CC) $(CFLAGS) $^ -o $@ +$(BUILDDIR)/objs/pdsDataServer.o: $(BUILDDIR)/src/pdsDataServer.c +$(BUILDDIR)/objs/pdsWriteData.o: $(BUILDDIR)/src/pdsWriteData.c +$(BUILDDIR)/objs/pdsCrcService.o: $(BUILDDIR)/src/pdsCrcService.c +################ +LIB_PERSIST_DATA_SERVER : $(objects) + $(AR) $(AR_KEYS) $(LIBDIR)/lib$(PDS_LIB).a $(objects) + $(SIZE) -td $(LIBDIR)/lib$(PDS_LIB).a +################ + +clean: + @echo + @echo ---------------------------------------------------- + @echo Persist Data Server component cleaning. + @echo ---------------------------------------------------- + rm -f $(objects) $(LIBDIR)/lib$(PDS_LIB).a $(LISTDIR)/*.* diff --git a/digital/zigbit/bitcloud/stack/Components/PersistDataServer/include/pdsDataServer.h b/digital/zigbit/bitcloud/stack/Components/PersistDataServer/include/pdsDataServer.h new file mode 100644 index 00000000..67855a66 --- /dev/null +++ b/digital/zigbit/bitcloud/stack/Components/PersistDataServer/include/pdsDataServer.h @@ -0,0 +1,99 @@ +/***************************************************************************//** + \file pdsDataServer.h + + \brief The header file describes the Persistence Data Server interface + + \author + Atmel Corporation: http://www.atmel.com \n + Support email: avr@atmel.com + + Copyright (c) 2008-2011, Atmel Corporation. All rights reserved. + Licensed under Atmel's Limited License Agreement (BitCloudTM). + + \internal + History: + 28/09/07 A. Khromykh - Created + 01/11/10 A. Razinkov - Modified +*****************************************************************************/ + +#ifndef _PERSISTDATASERVER_H +#define _PERSISTDATASERVER_H + +/****************************************************************************** + Includes section +******************************************************************************/ +#include + +/****************************************************************************** + Types section +******************************************************************************/ +//! PDS state result after a command call +typedef enum +{ + PDS_SUCCESS, //!< - Command completed successfully + PDS_EEPROM_ERROR, //!< - EEPROM error occurred + PDS_CRC_ERROR, //!< - Wrong CRC + PDS_COMMIT_IN_PROGRESS //!< - Commit to external memory started +} PDS_DataServerState_t; + +#ifdef _COMMISSIONING_ +//! \cond internal +/***************************************************************************//** +\brief Reads all parameters from EEPROM to Config Server memory. + +Reads all parameters from EEPROM to Config Server memory and also checks CRC of +EEPROM stack parameters. In case of wrong CRC ConfigServer should reset EEPROM stack +parameters to default values. +*******************************************************************************/ +void PDS_Init(void); +//! \endcond + +/*****************************************************************************//** +\brief Writes default values of parameters to EEPROM + +\return - PDS state as a result of setting parameters to default state +*******************************************************************************/ +PDS_DataServerState_t PDS_SetToDefault(void); + +/***************************************************************************//** +\brief Must be called from the ::APL_TaskHandler() function only.\n +The function forces writing data from ConfigServer to EEPROM. After returning from the function +all ConfigServer data is stored in EEPROM. +*******************************************************************************/ +void PDS_FlushData(void); + +/***************************************************************************//** +\brief Must be called from the ::APL_TaskHandler() function only.\n +Stops Persist Data Server. +*******************************************************************************/ +void PDS_Stop(void); +#endif /* _COMMISSIONING_ */ + +/*****************************************************************************//** +\brief Must be called from the ::APL_TaskHandler() function only.\n +Reads data from the user area of EEPROM. + +\param[in] offset - data offset +\param[in] data - pointer to user data area +\param[in] length - data length +\param[in] callback - pointer to a callback function; if callback is NULL, then data will be read syncronously + +\return - PDS state as a result of data reading operation +*******************************************************************************/ +PDS_DataServerState_t PDS_ReadUserData(uint16_t offset, uint8_t *data, uint16_t length, void (*callback)(void)); + +/****************************************************************************//** +\brief Must be called only from ::APL_TaskHandler() function.\n +Writes data to user area of EEPROM. + +\param[in] offset - data offset +\param[in] data - pointer to user data area +\param[in] length - data length +\param[in] callback - pointer to a callback function; if callback is NULL, then data will be written syncronously + +\return - PDS state as a result of data writing operation +*******************************************************************************/ +PDS_DataServerState_t PDS_WriteUserData(uint16_t offset, uint8_t *data, uint16_t length, void (*callback)(void)); + +#endif //#ifndef _PERSISTDATASERVER_H + diff --git a/digital/zigbit/bitcloud/stack/Components/PersistDataServer/include/private/pdsAuxService.h b/digital/zigbit/bitcloud/stack/Components/PersistDataServer/include/private/pdsAuxService.h new file mode 100644 index 00000000..3735afaa --- /dev/null +++ b/digital/zigbit/bitcloud/stack/Components/PersistDataServer/include/private/pdsAuxService.h @@ -0,0 +1,80 @@ +/***************************************************************************//** + \file pdsAuxService.h + + \brief The header file describes the auxiliary service of Persistence Data Server + + \author + Atmel Corporation: http://www.atmel.com \n + Support email: avr@atmel.com + + Copyright (c) 2008-2011, Atmel Corporation. All rights reserved. + Licensed under Atmel's Limited License Agreement (BitCloudTM). + + \internal + + History: + 22/01/08 A. Khromykh - Created + 01/11/10 A. Razinkov - Modified +*****************************************************************************/ + +#ifndef _PDSAUXSERVICE_H +#define _PDSAUXSERVICE_H + +/****************************************************************************** + Includes section +******************************************************************************/ +#ifndef _MAC2_ + #include + #include + #include +#else // _MAC2_ + #include +#endif // _MAC2_ + +/****************************************************************************** + Define(s) section +******************************************************************************/ +/* define for eeprom write highlight */ +//#define USE_LED + +// crc area +#define PDS_CRC_AREA 10 +// user area +#define USER_BASE_EEPROM_ADDRESS (csPersistentMemorySize + PDS_CRC_AREA) + +#define PDS_CS_PARAMETER 0 +#define PDS_CS_MEMORY 1 +#define PDS_STOP_SAVE 2 + +#define EEPROM_BUSY -2 +#define EEPROM_ERROR -1 + +#define STORE_TIMER_TIMEOUT 300000ul // 5 minutes +#define SHORT_TIMEOUT 50ul // 50 msec +#define PDS_LONG_INTERVAL 0 +#define PDS_SHORT_INTERVAL 1 +#define SAVE_IS_STARTED 1 +#define SAVE_IS_STOPED 0 + +#define MAX_CS_PDS_VARIABLE_SIZE 16 + +/****************************************************************************** + Types section +******************************************************************************/ +// crc property +typedef struct +{ + uint8_t crc; + uint16_t position; + PDS_DataServerState_t eepromState; +} PDS_ServiceCrc_t; + +/****************************************************************************** + External global variables section +******************************************************************************/ +extern HAL_AppTimer_t pdsEepromSaveServiceTimer; +extern uint8_t savingIsStarted; +extern const uint8_t csPersistentItemsAmount; +extern const uint16_t csPersistentMemorySize; + +#endif /*_PDSAUXSERVICE_H*/ diff --git a/digital/zigbit/bitcloud/stack/Components/PersistDataServer/include/private/pdsCrcService.h b/digital/zigbit/bitcloud/stack/Components/PersistDataServer/include/private/pdsCrcService.h new file mode 100644 index 00000000..cf00f9f4 --- /dev/null +++ b/digital/zigbit/bitcloud/stack/Components/PersistDataServer/include/private/pdsCrcService.h @@ -0,0 +1,73 @@ +/***************************************************************************//** + \file pdsCrcService.h + + \brief The header file describes the interface of crc counting + + \author + Atmel Corporation: http://www.atmel.com \n + Support email: avr@atmel.com + + Copyright (c) 2008-2011, Atmel Corporation. All rights reserved. + Licensed under Atmel's Limited License Agreement (BitCloudTM). + + \internal + History: + 22/01/08 A. Khromykh - Created + 01/11/10 A. Razinkov - Modified +*****************************************************************************/ + +#ifndef _PDSCRCSERVICE_H +#define _PDSCRCSERVICE_H + +/****************************************************************************** + Includes section +******************************************************************************/ +#include +#include + +#ifdef _COMMISSIONING_ +/****************************************************************************** + Prototypes section +******************************************************************************/ +/****************************************************************************** +\brief Calculate CRC of persistent data stored in persist memory +\ +\param[out] crcStation - pointer to CRC service structure +******************************************************************************/ +void pdsCalculateCrc(PDS_ServiceCrc_t *crcStation); + +/****************************************************************************** +\brief Read CRC of stored data from persist memory +\ +\param[out] crcStation - pointer to CRC service structure +******************************************************************************/ +void pdsReadCrc(PDS_ServiceCrc_t *crcStation); + +/****************************************************************************** +\brief Write CRC of stored data to persist memory. The ring buffer used +\ to increase persist memory life cycle +\ +\param[out] crcStation - pointer to CRC service structure +******************************************************************************/ +PDS_DataServerState_t pdsWriteCrc(void); + +/****************************************************************************** +\brief Clears whole CRC area in persist memory +\ +\return operation result +******************************************************************************/ +PDS_DataServerState_t pdsClearCrcArea(void); + +/****************************************************************************** +\brief Check if any valid data exists in persist memory +\ +\return operation result +******************************************************************************/ +PDS_DataServerState_t pdsCheckPersistMemory(void); + +#ifdef __DBG_PDS__ + void pdsDbgReadAllEeprom(void); +#endif + +#endif /* _COMMISSIONING_ */ +#endif /*_PDSCRCSERVICE_H*/ diff --git a/digital/zigbit/bitcloud/stack/Components/PersistDataServer/include/private/pdsDbg.h b/digital/zigbit/bitcloud/stack/Components/PersistDataServer/include/private/pdsDbg.h new file mode 100644 index 00000000..b336095f --- /dev/null +++ b/digital/zigbit/bitcloud/stack/Components/PersistDataServer/include/private/pdsDbg.h @@ -0,0 +1,37 @@ +/****************************************************************************** + \file csDbg.h + + \brief + Persistent Data Server debug info + + \author + Atmel Corporation: http://www.atmel.com \n + Support email: avr@atmel.com + + Copyright (c) 2010 , Atmel Corporation. All rights reserved. + Licensed under Atmel's Limited License Agreement (BitCloudTM). + + \internal + History: + 29.10.10 A. Razinkov - Created. +******************************************************************************/ + +#ifndef _PDSDBG_H_ +#define _PDSDBG_H_ + +/****************************************************************************** + Includes section +******************************************************************************/ +#include + +/****************************************************************************** + Types section +******************************************************************************/ + +typedef enum +{ + PDS_PDSPREPAREMEMORYACCESS0, + PDS_PDSPREPAREMEMORYACCESS1 +} PDS_DbgCodeId_t; + +#endif /* CSDBG_H_ */ diff --git a/digital/zigbit/bitcloud/stack/Components/PersistDataServer/include/private/pdsMemAbstract.h b/digital/zigbit/bitcloud/stack/Components/PersistDataServer/include/private/pdsMemAbstract.h new file mode 100644 index 00000000..ca95f064 --- /dev/null +++ b/digital/zigbit/bitcloud/stack/Components/PersistDataServer/include/private/pdsMemAbstract.h @@ -0,0 +1,70 @@ +/***************************************************************************//** + \file pdsMemAbstract.h + + \brief Memory abstract header + + \author + Atmel Corporation: http://www.atmel.com \n + Support email: avr@atmel.com + + Copyright (c) 2008-2011, Atmel Corporation. All rights reserved. + Licensed under Atmel's Limited License Agreement (BitCloudTM). + + \internal + History: + 6/11/08 A. Khromykh - Created +*****************************************************************************/ +#ifndef _PDSMEMABSTRACT_H +#define _PDSMEMABSTRACT_H + +/* Type of usage memory */ +#define USE_EEPROM + +/* This header file performs persistence memory API redefinition. + * The goal is to achieve memory type independence for persist data server (PDS). + * + * + * MEMORY_DESCRIPTOR - persistence memory descriptor. It is a structure type, which + * includes the following variables: + * uint16_t address - internal persistence memory address to be accessed. + * uint8_t *data - pointer to the variable in RAM to be stored (or to be filled + * by read value from persistence). + * uint16_t length - "data" variable length (bytes). Equal to the number of bytes + * to be exchanged between persistence memory and RAM. + * + * + * WRITE_MEMORY, READ_MEMORY - persistence memory access functions. These functions + * should have the following form: + * int write/read(MEMORY_DESCRIPTOR *memoryDescr, void (*callback)(void)); + * Parameters: + * memoryDescr - pointer to memory descriptor + * callback - pointer to callback function. Callback function will be called + * after read or write persistence memory operation is completed. + * Returns: + * 0 - successful access; + * -1 - the number of bytes to read (write) is too large. + * Persistence memory access functions should perform memory arbitration also, i.e. + * memory busy return status is illegal. + * + * + * IS_MEMORY_BUSY - persistence memory access function, should have the following form: + * bool isMemoryBusy(void); + * Parameters: + * none. + * Returns: + * true - memory is busy + * false - memory is free; +*/ + +#if defined(USE_EEPROM) + +#include + +#define WRITE_MEMORY HAL_WriteEeprom +#define READ_MEMORY HAL_ReadEeprom +#define IS_MEMORY_BUSY HAL_IsEepromBusy +#define MEMORY_DESCRIPTOR HAL_EepromParams_t + +#endif + +#endif /* _PDSMEMABSTRACT_H */ diff --git a/digital/zigbit/bitcloud/stack/Components/PersistDataServer/include/private/pdsWriteData.h b/digital/zigbit/bitcloud/stack/Components/PersistDataServer/include/private/pdsWriteData.h new file mode 100644 index 00000000..17cedbc4 --- /dev/null +++ b/digital/zigbit/bitcloud/stack/Components/PersistDataServer/include/private/pdsWriteData.h @@ -0,0 +1,84 @@ +/****************************************************************************** + \file csDbg.h + + \brief + Persistent Periodic data save implementation header + + \author + Atmel Corporation: http://www.atmel.com \n + Support email: avr@atmel.com + + Copyright (c) 2010 , Atmel Corporation. All rights reserved. + Licensed under Atmel's Limited License Agreement (BitCloudTM). + + \internal + History: + 29.10.10 A. Razinkov - Created. +******************************************************************************/ + +#ifndef _PDSWRITEDATA_H_ +#define _PDSWRITEDATA_H_ + +/****************************************************************************** + Includes section +******************************************************************************/ +#include + +#ifdef _COMMISSIONING_ +/****************************************************************************** + Functions prototypes section +******************************************************************************/ +/****************************************************************************** +\brief Update persistent items values in Configuration Server from persist memory +\ +\return Operation result +******************************************************************************/ +PDS_DataServerState_t pdsUpdate(void); + +/****************************************************************************** +\brief Locates parameter by it's index in persistent memory table. And prepares +\ memory descriptor to store parameter's value in EEPROM +\ +\param[in] index - index of the parameter in persistent memory table +\param[out]descriptor - memory descriptor to store parameter value +******************************************************************************/ +void pdsPrepareMemoryAccess(uint8_t index, MEMORY_DESCRIPTOR* descriptor); + +/****************************************************************************** +\brief Timer callback, initiates the commit process. +******************************************************************************/ +void pdsOnTimerSave(void); + +/******************************************************************************* +\brief Start server work +*******************************************************************************/ +void pdsStartPersistServer(void); +#endif /* _COMMISSIONING_ */ + +/****************************************************************************** +\brief Writes data to persist memory +\ +\param[in] descriptor - memory descriptor to store parameter value +\param[out]callback - callback to write-finidhed event handler +******************************************************************************/ +PDS_DataServerState_t pdsWrite(MEMORY_DESCRIPTOR* descriptor, void (*callback)(void)); + +/****************************************************************************** +\brief Read data from persist memory +\ +\param[in] descriptor - memory descriptor to read parameter value +\param[out]callback - callback to read-finished event handler +******************************************************************************/ +PDS_DataServerState_t pdsRead(MEMORY_DESCRIPTOR* descriptor, void (*callback)(void)); + +/****************************************************************************** +\brief Wait until memory be ready for transaction +******************************************************************************/ +void pdsWaitMemoryFree(void); + +/******************************************************************************* +\brief Dummy callback +*******************************************************************************/ +void pdsDummyCallback(void); + +#endif /* _PDSWRITEDATA_H_ */ diff --git a/digital/zigbit/bitcloud/stack/Components/PersistDataServer/src/pdsCrcService.c b/digital/zigbit/bitcloud/stack/Components/PersistDataServer/src/pdsCrcService.c new file mode 100644 index 00000000..de9631dc --- /dev/null +++ b/digital/zigbit/bitcloud/stack/Components/PersistDataServer/src/pdsCrcService.c @@ -0,0 +1,217 @@ +/***************************************************************************//** + \file pdsCrcService.c + + \brief Persistence Data Server implementation + + \author + Atmel Corporation: http://www.atmel.com \n + Support email: avr@atmel.com + + Copyright (c) 2008-2011, Atmel Corporation. All rights reserved. + Licensed under Atmel's Limited License Agreement (BitCloudTM). + + \internal + History: + 22/01/08 A. Khromykh - Created + 01/11/10 A. Razinkov - Modified +*****************************************************************************/ + +#ifdef _COMMISSIONING_ +/****************************************************************************** + Includes section +******************************************************************************/ +#include +#include +#include +#include +#include +#include + +/****************************************************************************** +\brief Calculate CRC of persistent data stored in persist memory +\ +\param[out] crcStation - pointer to CRC service structure +******************************************************************************/ +void pdsCalculateCrc(PDS_ServiceCrc_t *crcStation) +{ + uint8_t tdata = 0; + MEMORY_DESCRIPTOR descriptor; + + if (NULL == crcStation) + return; + + crcStation->crc = 0; + descriptor.address = SYSTEM_BASE_EEPROM_ADDRESS; + descriptor.length = 1; + while (descriptor.address < csPersistentMemorySize) + { + descriptor.data = &tdata; + + pdsWaitMemoryFree(); + + if (EEPROM_ERROR == READ_MEMORY(&descriptor, NULL)) + { + crcStation->eepromState = PDS_EEPROM_ERROR; + return; + } + + pdsWaitMemoryFree(); + + crcStation->crc += tdata; + descriptor.address++; + } + crcStation->eepromState = PDS_SUCCESS; +} + +/****************************************************************************** +\brief Read CRC of stored data from persist memory +\ +\param[out] crcStation - pointer to CRC service structure +******************************************************************************/ +void pdsReadCrc(PDS_ServiceCrc_t *crcStation) +{ + MEMORY_DESCRIPTOR descriptor; + + if (NULL == crcStation) + return; + + descriptor.address = csPersistentMemorySize; + descriptor.length = 1; + while (descriptor.address < USER_BASE_EEPROM_ADDRESS) + { + descriptor.data = &(crcStation->crc); + + pdsWaitMemoryFree(); + + if (EEPROM_ERROR == READ_MEMORY(&descriptor, NULL)) + { + crcStation->eepromState = PDS_EEPROM_ERROR; + return; + } + + pdsWaitMemoryFree(); + + if (0xFF != crcStation->crc) + { + crcStation->position = descriptor.address; + crcStation->eepromState = PDS_SUCCESS; + return; + } + descriptor.address++; + } + crcStation->position = csPersistentMemorySize; + crcStation->eepromState = PDS_SUCCESS; +} + +/****************************************************************************** +\brief Clears whole CRC area in persist memory +\ +\return operation result +******************************************************************************/ +PDS_DataServerState_t pdsClearCrcArea(void) +{ + uint8_t value = 0xFF; + MEMORY_DESCRIPTOR descriptor; + uint8_t status; + + descriptor.data = &value; + descriptor.address = csPersistentMemorySize; + descriptor.length = 1; + + for (uint8_t i = 0; i < PDS_CRC_AREA; i++) + { + status = pdsWrite(&descriptor, pdsDummyCallback); + if (status != PDS_SUCCESS) + return status; + + descriptor.address++; + } + return PDS_SUCCESS; +} + +/****************************************************************************** +\brief Write CRC of stored data to persist memory. The ring buffer used +\ to increase persist memory life cycle +\ +\param[out] crcStation - pointer to CRC service structure +******************************************************************************/ +PDS_DataServerState_t pdsWriteCrc(void) +{ + PDS_ServiceCrc_t crcRead, crcCalc; + MEMORY_DESCRIPTOR descriptor; + uint8_t datadelete = 0xFF; + PDS_DataServerState_t status; + + pdsReadCrc(&crcRead); + if (PDS_EEPROM_ERROR == crcRead.eepromState) + return PDS_EEPROM_ERROR; + + pdsCalculateCrc(&crcCalc); + if (PDS_EEPROM_ERROR == crcCalc.eepromState) + return PDS_EEPROM_ERROR; + + if (0 == (uint8_t)(crcCalc.crc + crcRead.crc)) + return PDS_SUCCESS; + + crcCalc.crc = 0 - crcCalc.crc; + descriptor.address = crcRead.position; + descriptor.data = &datadelete; + descriptor.length = 1; + + status = pdsWrite(&descriptor, pdsDummyCallback); + if (status != PDS_SUCCESS) + return status; + + if (++descriptor.address >= USER_BASE_EEPROM_ADDRESS) + descriptor.address = csPersistentMemorySize; + descriptor.data = &(crcCalc.crc); + + status = pdsWrite(&descriptor, pdsDummyCallback); + if (status != PDS_SUCCESS) + return status; + + return PDS_SUCCESS; +} + +/****************************************************************************** +\brief Check if any valid data exists in persist memory +\ +\return operation result +******************************************************************************/ +PDS_DataServerState_t pdsCheckPersistMemory(void) +{ + PDS_ServiceCrc_t crcRead, crcCalc; + + pdsCalculateCrc(&crcCalc); + if (PDS_EEPROM_ERROR == crcCalc.eepromState) + return PDS_EEPROM_ERROR; + + pdsReadCrc(&crcRead); + if (PDS_EEPROM_ERROR == crcRead.eepromState) + return PDS_EEPROM_ERROR; + + if (0x00 == (uint8_t)(crcCalc.crc + crcRead.crc)) + return PDS_SUCCESS; + + return PDS_CRC_ERROR; +} + +#ifdef __DBG_PDS__ + uint8_t eepromCopy[0x400u]; + + void pdsDbgReadAllEeprom(void) + { + PDS_DataServerState_t status; + MEMORY_DESCRIPTOR descriptor; + + descriptor.address = 0; + descriptor.data = eepromCopy; + descriptor.length = 0x400u; + + status = pdsRead(&descriptor, pdsDummyCallback); + if (status != PDS_SUCCESS) + return; + } +#endif +#endif /* _COMMISSIONING_ */ +// eof pdsDataServer.c diff --git a/digital/zigbit/bitcloud/stack/Components/PersistDataServer/src/pdsDataServer.c b/digital/zigbit/bitcloud/stack/Components/PersistDataServer/src/pdsDataServer.c new file mode 100644 index 00000000..530ac0a7 --- /dev/null +++ b/digital/zigbit/bitcloud/stack/Components/PersistDataServer/src/pdsDataServer.c @@ -0,0 +1,206 @@ +/***************************************************************************//** + \file pdsDataServer.c + + \brief Persistence Data Server implementation + + \author + Atmel Corporation: http://www.atmel.com \n + Support email: avr@atmel.com + + Copyright (c) 2008-2011, Atmel Corporation. All rights reserved. + Licensed under Atmel's Limited License Agreement (BitCloudTM). + + \internal + History: + 22/01/08 A. Khromykh - Created + 01/11/10 A. Razinkov - Modified +*****************************************************************************/ +/****************************************************************************** + Includes section +******************************************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#ifdef _COMMISSIONING_ +/****************************************************************************** + Prototypes section +******************************************************************************/ + +/****************************************************************************** + External variables section +******************************************************************************/ +/* Number of records in csPersistentMemTable*/ +extern const uint8_t csPersistentItemsAmount; + +/****************************************************************************** + Implementations section +******************************************************************************/ +/****************************************************************************** +\brief Persist Data Server initialization procedure +******************************************************************************/ +void PDS_Init(void) +{ + PDS_DataServerState_t status; + +#ifdef USE_LED + BSP_OpenLeds(); +#endif + + /* Check if any valid data exists in persist memory */ + status = pdsCheckPersistMemory(); + + /* Valid data exists - try to update from persist memory */ + if (PDS_SUCCESS == status) + { + if (PDS_EEPROM_ERROR != pdsUpdate()) + { + pdsStartPersistServer(); + } + } + + else if (PDS_EEPROM_ERROR != status) + { + pdsStartPersistServer(); + } +} + +/****************************************************************************** +\brief Set the default system area values and store persistent data to persist + memory + +\return operation result +******************************************************************************/ +PDS_DataServerState_t PDS_SetToDefault(void) +{ + MEMORY_DESCRIPTOR descriptor; + PDS_DataServerState_t status; + + CS_PdsDefaultValue(); + /* Store all persistent parameters */ + for (uint8_t index = 0; index < csPersistentItemsAmount; index++) + { + pdsPrepareMemoryAccess(index, &descriptor); + status = pdsWrite(&descriptor, pdsDummyCallback); + if (status != PDS_SUCCESS) + return status; + } + + /* Reset CRC area and rewrite CRC*/ + status = pdsClearCrcArea(); + if (status != PDS_SUCCESS) + return status; + status = pdsWriteCrc(); + if (status != PDS_SUCCESS) + return status; + + return PDS_SUCCESS; +} + +/****************************************************************************** +\brief On-demand data storing in persist memory +******************************************************************************/ +void PDS_FlushData(void) +{ + if (SAVE_IS_STOPED == savingIsStarted) + { + /* Force timer to finish - store data immediately */ + HAL_StopAppTimer(&pdsEepromSaveServiceTimer); + pdsOnTimerSave(); + } + + while (SAVE_IS_STARTED == savingIsStarted) + { + SYS_ForceRunTask(); + } +} + +/***************************************************************************//** +\brief Must be called only from ::APL_TaskHandler() function.\n + Stops persist data server. +*******************************************************************************/ +void PDS_Stop(void) +{ + while (SAVE_IS_STARTED == savingIsStarted) + { + SYS_ForceRunTask(); + } + + HAL_StopAppTimer(&pdsEepromSaveServiceTimer); +} +#endif /* _COMMISSIONING_ */ + +/****************************************************************************** +\brief Read data from user area in persist memory + +\param[in] offset - data offset in persist memory +\param[in] length - data lenght +\param[out] data - pointer to user data area in RAM +\param[out] callback - callback to read-finished event handler + +\return operation result +******************************************************************************/ +PDS_DataServerState_t PDS_ReadUserData(uint16_t offset, uint8_t *data, uint16_t length, void (*callback)(void)) +{ + MEMORY_DESCRIPTOR descriptor; + PDS_DataServerState_t status; + + descriptor.address = USER_BASE_EEPROM_ADDRESS + offset; + descriptor.length = length; + descriptor.data = data; + + while(SAVE_IS_STARTED == savingIsStarted) + { + SYS_ForceRunTask(); + } + + if (NULL == callback) + callback = pdsDummyCallback; + + status = pdsRead(&descriptor, callback); + if (status != PDS_SUCCESS) + return status; + + return PDS_SUCCESS; +} + +/****************************************************************************** +\brief Write data to user area in persist memory +\ +\param[in] offset - data offset in persist memory +\param[in] length - data lenght +\param[out] data - pointer to user data area +\param[out] callback - callback to read-finished event handler + +\return operation result +******************************************************************************/ +PDS_DataServerState_t PDS_WriteUserData(uint16_t offset, uint8_t *data, uint16_t length, void (*callback)(void)) +{ + MEMORY_DESCRIPTOR descriptor; + PDS_DataServerState_t status; + + descriptor.address = USER_BASE_EEPROM_ADDRESS + offset; + descriptor.length = length; + descriptor.data = data; + + while(SAVE_IS_STARTED == savingIsStarted) + { + SYS_ForceRunTask(); + } + + if (NULL == callback) + callback = pdsDummyCallback; + + status = pdsWrite(&descriptor, callback); + if (status != PDS_SUCCESS) + return status; + + return PDS_SUCCESS; +} +// eof pdsDataServer.c diff --git a/digital/zigbit/bitcloud/stack/Components/PersistDataServer/src/pdsWriteData.c b/digital/zigbit/bitcloud/stack/Components/PersistDataServer/src/pdsWriteData.c new file mode 100644 index 00000000..bb7f9859 --- /dev/null +++ b/digital/zigbit/bitcloud/stack/Components/PersistDataServer/src/pdsWriteData.c @@ -0,0 +1,286 @@ +/**************************************************************************//** + \file pdsWriteData.c + + \brief Periodic data save implementation + + \author + Atmel Corporation: http://www.atmel.com \n + Support email: avr@atmel.com + + Copyright (c) 2008-2011, Atmel Corporation. All rights reserved. + Licensed under Atmel's Limited License Agreement (BitCloudTM). + + \internal + History: + 22/01/08 A. Khromykh - Created + 01/11/10 A. Razinkov - Modified +*****************************************************************************/ +/****************************************************************************** + Includes section +******************************************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +/****************************************************************************** + Global variables section +******************************************************************************/ +uint8_t savingIsStarted = SAVE_IS_STOPED; + +#ifdef _COMMISSIONING_ +HAL_AppTimer_t pdsEepromSaveServiceTimer; + +/****************************************************************************** + External variables section +******************************************************************************/ +/* Configuration Server persistent memory table */ +extern const CS_PersistantData_t PROGMEM_DECLARE(csPersistentMemTable[]); + +/* Number of records in csPersistentMemTable*/ +extern const uint8_t csPersistentItemsAmount; + +/* Total size of memory occupied by persistent parameters */ +extern const uint16_t csPersistentMemorySize; + +/****************************************************************************** + Prototypes section +******************************************************************************/ +static PDS_DataServerState_t pdsCommit(void); +static bool pdsCommitStarted(MEMORY_DESCRIPTOR* descriptor); + +/****************************************************************************** + Implementation section +******************************************************************************/ +/****************************************************************************** +\brief Start internal PDS timer +\ +\param[in] typeInterval - set timer interval. Available intervals are: + PDS_LONG_INTERVAL - 5 minutes + PDS_SHORT_INTERVAL - 50 msec +\param[out] callback - callback on timer fired event + +******************************************************************************/ +void pdsStartPdsTimer(void (*callback)(void), uint8_t typeInterval) +{ + pdsEepromSaveServiceTimer.callback = callback; + if (PDS_LONG_INTERVAL == typeInterval) + { +#ifdef _POWER_FAILURE_ + uint32_t storingInterval; + + CS_ReadParameter(CS_PDS_STORING_INTERVAL_ID, &storingInterval); + pdsEepromSaveServiceTimer.interval = storingInterval; +#else + pdsEepromSaveServiceTimer.interval = STORE_TIMER_TIMEOUT; +#endif /* _POWER_FAILURE_ */ + } + else + pdsEepromSaveServiceTimer.interval = SHORT_TIMEOUT; + pdsEepromSaveServiceTimer.mode = TIMER_ONE_SHOT_MODE; + HAL_StartAppTimer(&pdsEepromSaveServiceTimer); +} + +/******************************************************************************* +\brief Start server work +*******************************************************************************/ +void pdsStartPersistServer(void) +{ + pdsStartPdsTimer(pdsOnTimerSave, PDS_LONG_INTERVAL); +} + +/****************************************************************************** +\brief Commit item value changes (if any) from Configuration Server to persist +\ memory +\ +\param[in] descriptor - memory descriptor to store parameter value + +\return true - if changes found and commit started; false - otherwise +******************************************************************************/ +static bool pdsCommitStarted(MEMORY_DESCRIPTOR* descriptor) +{ + uint8_t data; + MEMORY_DESCRIPTOR byteAccessDescriptor = + { + .data = &data, + .length = 1 + }; + + /* Check for any changes in byte-by-byte order */ + for (uint16_t i = 0; i < descriptor->length; i++) + { + byteAccessDescriptor.address = descriptor->address + i; + pdsRead(&byteAccessDescriptor, pdsDummyCallback); + /* Start rewrite out of date data in persist memory*/ + if (*byteAccessDescriptor.data != *((descriptor->data) + i)) + { + pdsWrite(descriptor, pdsOnTimerSave); + return true; + } + } + return false; +} + +/****************************************************************************** +\brief Update persistent items values in Configuration Server from persist memory +\ +\return Operation result +******************************************************************************/ +PDS_DataServerState_t pdsUpdate(void) +{ + PDS_DataServerState_t status; + MEMORY_DESCRIPTOR descriptor; + + for (uint8_t item = 0; item < csPersistentItemsAmount; item++) + { + pdsPrepareMemoryAccess(item, &descriptor); + status = pdsRead(&descriptor, pdsDummyCallback); + if (status != PDS_SUCCESS) + return status; + } + return PDS_SUCCESS; +} + +/****************************************************************************** +\brief Commit persistent memory changes (if any) from Configuration Server +\ to persist memory. Process comes iterative way - when some item stored, +\ this function will be called as a callback. +\ +\return Operation result +******************************************************************************/ +PDS_DataServerState_t pdsCommit(void) +{ + MEMORY_DESCRIPTOR descriptor; + static uint8_t item = 0; + + while (item < csPersistentItemsAmount) + { + pdsPrepareMemoryAccess(item, &descriptor); + if(pdsCommitStarted(&descriptor)) + return PDS_COMMIT_IN_PROGRESS; + + item++; + } + item = 0; + return PDS_SUCCESS; +} + +/****************************************************************************** +\brief Timer callback, initiates the commit process. +******************************************************************************/ +void pdsOnTimerSave(void) +{ +#ifdef USE_LED + BSP_OnLed(LED_YELLOW); +#endif + + savingIsStarted = SAVE_IS_STARTED; + + if (PDS_SUCCESS == pdsCommit()) + { + if (PDS_EEPROM_ERROR == pdsWriteCrc()) + { + pdsStartPdsTimer(pdsOnTimerSave, PDS_SHORT_INTERVAL); + } + else + { + pdsStartPersistServer(); +#ifdef USE_LED + BSP_OffLed(LED_YELLOW); +#endif + savingIsStarted = SAVE_IS_STOPED; + } + } +} + +/****************************************************************************** +\brief Locates parameter by it's index in persistent memory table. And prepares +\ memory descriptor to store parameter's value in EEPROM +\ +\param[in] index - index of the parameter in persistent memory table +\param[out]descriptor - memory descriptor to store parameter value +******************************************************************************/ +void pdsPrepareMemoryAccess(uint8_t index, MEMORY_DESCRIPTOR* descriptor) +{ + CS_PersistantData_t item; + + memcpy_P(&item, &csPersistentMemTable[index], sizeof(CS_PersistantData_t)); + + assert(item.value, PDS_PDSPREPAREMEMORYACCESS0); + descriptor->data = item.value; + descriptor->address = item.offset; + + /* Calculate item size using offsets */ + if (index == (csPersistentItemsAmount-1)) + { + descriptor->length = csPersistentMemorySize - item.offset; + } + else + { + CS_PersistantData_t nextItem; + memcpy_P(&nextItem, &csPersistentMemTable[index+1], sizeof(CS_PersistantData_t)); + assert(nextItem.value, PDS_PDSPREPAREMEMORYACCESS1); + descriptor->length = nextItem.offset - item.offset; + } +} +#endif /* _COMMISSIONING_ */ + +/****************************************************************************** +\brief Write data to persist memory +\ +\param[in] descriptor - memory descriptor to store parameter value +\param[out]callback - callback to write-finished event handler +******************************************************************************/ +PDS_DataServerState_t pdsWrite(MEMORY_DESCRIPTOR* descriptor, void (*callback)(void)) +{ + pdsWaitMemoryFree(); + if (EEPROM_ERROR == WRITE_MEMORY(descriptor, callback)) + return PDS_EEPROM_ERROR; + + if (callback == pdsDummyCallback) + pdsWaitMemoryFree(); + + return PDS_SUCCESS; +} + +/****************************************************************************** +\brief Read data from persist memory +\ +\param[in] descriptor - memory descriptor to read parameter value +\param[out]callback - callback to read-finished event handler +******************************************************************************/ +PDS_DataServerState_t pdsRead(MEMORY_DESCRIPTOR* descriptor, void (*callback)(void)) +{ + pdsWaitMemoryFree(); + if (EEPROM_ERROR == READ_MEMORY(descriptor, callback)) + return PDS_EEPROM_ERROR; + + if (callback == pdsDummyCallback) + pdsWaitMemoryFree(); + + return PDS_SUCCESS; +} + +/****************************************************************************** +\brief Wait until memory be ready for transaction +******************************************************************************/ +void pdsWaitMemoryFree(void) +{ + while(IS_MEMORY_BUSY()) + { + SYS_ForceRunTask(); + } +} + +/******************************************************************************* +\brief Dummy callback +*******************************************************************************/ +void pdsDummyCallback(void) +{} +// eof pdsDataServer.c -- cgit v1.2.3