Commit a2ec2372 authored by Theodor-Adrian Stana's avatar Theodor-Adrian Stana

Store settings to flash

parent 1e7f7e98
This diff is collapsed.
/**************************************************************************//**
* @file eeprom_emulation.h
* @brief EEPROM Emulation Demo Application header file
* @author Silicon Labs
* @version 1.10
******************************************************************************
* @section License
* <b>(C) Copyright 2014 Silicon Labs, http://www.silabs.com</b>
*******************************************************************************
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
*
* DISCLAIMER OF WARRANTY/LIMITATION OF REMEDIES: Silicon Labs has no
* obligation to support this Software. Silicon Labs is providing the
* Software "AS IS", with no express or implied warranties of any kind,
* including, but not limited to, any implied warranties of merchantability
* or fitness for any particular purpose or warranties against infringement
* of any proprietary rights of a third party.
*
* Silicon Labs will not be liable for any consequential, incidental, or
* special damages, or any other relief, or for any claim by any third party,
* arising from your use of this Software.
*
******************************************************************************/
#ifndef __EE_H
#define __EE_H
#include <stdbool.h>
#include "em_device.h"
#include "em_msc.h"
#ifdef __cplusplus
extern "C" {
#endif
/*******************************************************************************
****************************** DEFINES ************************************
******************************************************************************/
#if defined (_EFM32_GECKO_FAMILY) || defined(_EFM32_TINY_FAMILY)
#define PAGE_SIZE 0x200
#elif defined(_EFM32_GIANT_FAMILY)
#define PAGE_SIZE ((FLASH_SIZE >= 0x80000) ? 0x1000 : 0x800)
#else
#pragma error "Unknown page size"
#endif
#define DEFAULT_NUMBER_OF_PAGES 2
#define MAX_NUMBER_OF_PAGES (FLASH_SIZE/PAGE_SIZE)
#define SIZE_OF_DATA 2 /* 2 bytes */
#define SIZE_OF_VIRTUAL_ADDRESS 2 /* 2 bytes */
#define SIZE_OF_VARIABLE (SIZE_OF_DATA + SIZE_OF_VIRTUAL_ADDRESS) /* 4 bytes */
#define MAX_ACTIVE_VARIABLES (PAGE_SIZE / SIZE_OF_VARIABLE)
#define PAGE_STATUS_ERASED 0xFF
#define PAGE_STATUS_RECEIVING 0xAA
#define PAGE_STATUS_ACTIVE 0x00
/*******************************************************************************
*************************** GLOBAL VARIABLES ******************************
******************************************************************************/
/* Since the data to be written to flash must be read from ram, the data used to
* set the pages' status, is explicitly written to the ram beforehand. */
static uint32_t pageStatusActiveValue = ((uint32_t) PAGE_STATUS_ACTIVE << 24) | 0x00FFFFFF;
static uint32_t pageStatusReceivingValue = ((uint32_t) PAGE_STATUS_RECEIVING << 24) | 0x00FFFFFF;
/*******************************************************************************
******************************* ENUMS *************************************
******************************************************************************/
typedef enum
{
eePageStatusErased = PAGE_STATUS_ERASED,
eePageStatusReceiving = PAGE_STATUS_RECEIVING,
eePageStatusActive = PAGE_STATUS_ACTIVE,
} EE_PageStatus_TypeDef;
/*******************************************************************************
****************************** STRUCTS ************************************
******************************************************************************/
typedef struct
{
/* Each variable is assigned a unique virtual address automatically when first
* written to, or when using the declare function. */
uint16_t virtualAddress;
} EE_Variable_TypeDef;
typedef struct
{
uint32_t *startAddress;
uint32_t *endAddress;
} EE_Page_TypeDef;
/*******************************************************************************
***************************** PROTOTYPES **********************************
******************************************************************************/
bool EE_Init(uint32_t);
bool EE_Format(uint32_t);
bool EE_Read(EE_Variable_TypeDef *var, uint16_t *readData);
void EE_Write(EE_Variable_TypeDef *var, uint16_t writeData);
bool EE_DeclareVariable(EE_Variable_TypeDef *var);
void EE_DeleteVariable(EE_Variable_TypeDef *var);
uint32_t EE_GetEraseCount(void);
/***************************************************************************//**
* @brief
* Returns the page status of the given page.
*
* @param[in] page
* Pointer to the page whose status to be returned.
*
* @return
* Returns the status of the given page.
******************************************************************************/
__STATIC_INLINE EE_PageStatus_TypeDef EE_getPageStatus(EE_Page_TypeDef *page)
{
return (EE_PageStatus_TypeDef)((*(page->startAddress) >> 24) & 0xFF);
}
/***************************************************************************//**
* @brief
* Sets status of the given page to active.
*
* @param[in] page
* Pointer to the page whose status to be altered.
*
* @return
* Returns the status of the flash operation.
******************************************************************************/
__STATIC_INLINE msc_Return_TypeDef EE_setPageStatusActive(EE_Page_TypeDef *page)
{
return MSC_WriteWord(page->startAddress, &pageStatusActiveValue, SIZE_OF_VARIABLE);
}
/***************************************************************************//**
* @brief
* Sets status of the given page to receiving.
*
* @param[in] page
* Pointer to the page whose status to be altered.
*
* @return
* Returns the status of the flash operation.
******************************************************************************/
__STATIC_INLINE msc_Return_TypeDef EE_setPageStatusReceiving(EE_Page_TypeDef *page)
{
return MSC_WriteWord(page->startAddress, &pageStatusReceivingValue, SIZE_OF_VARIABLE);
}
#ifdef __cplusplus
}
#endif
#endif /* __EE_H */
......@@ -144,6 +144,7 @@ C_SRC += \
../common/emlib/src/em_timer.c \
../common/emlib/src/em_usart.c \
../common/emlib/src/em_leuart.c \
../common/emlib/src/em_msc.c \
../common/emdrv/sleep/src/sleep.c \
../common/drivers/dmactrl.c \
../common/drivers/lcd.c \
......@@ -172,6 +173,7 @@ C_SRC += \
../common/gfx/gfx.c \
../common/gfx/ui.c \
../common/sincos.c \
../common/eeprom_emulation.c \
../common/usb/src/em_usbd.c \
../common/usb/src/em_usbdch9.c \
../common/usb/src/em_usbhal.c \
......
......@@ -27,31 +27,77 @@
#include <task.h>
setting_t setting_gps_on = { "GPS on", 1, 2 };
setting_t setting_coord_style = { "Coord style", 1, 3 };
setting_t setting_gps_sets_time = { "GPS sets time", 1, 2 };
#include <eeprom_emulation.h>
setting_t setting_gps_on = { "GPS on", 1, 2, 0 };
setting_t setting_coord_style = { "Coord style", 1, 3, 0 };
setting_t setting_gps_sets_time = { "GPS sets time", 1, 2, 0 };
/* Settings with nrvals == 0 do not wrap around, they are set externally */
setting_t setting_gmt_ofs_hr = { "GMT offset hours" , 0, 0 };
setting_t setting_gmt_ofs_min = { "GMT offset minutes", 0, 0 };
setting_t setting_gmt_ofs_hr = { "GMT ofs hrs", 0, 0, 0 };
setting_t setting_gmt_ofs_min = { "GMT ofs mins", 0, 0, 0 };
static EE_Variable_TypeDef gps_on, coord_style, gps_sets_time,
gmt_ofs_hr, gmt_ofs_min;
void setting_init()
{
/* Start adding virtual addresses to allocated variables */
EE_DeclareVariable(&gps_on);
EE_DeclareVariable(&coord_style);
EE_DeclareVariable(&gps_sets_time);
EE_DeclareVariable(&gmt_ofs_hr);
EE_DeclareVariable(&gmt_ofs_min);
/*
* Place these virtual addresses to the RAM-stored structs, so we can pass
* them to setting_apply() when called
*/
setting_gps_on.fladdr = gps_on.virtualAddress;
setting_coord_style.fladdr = coord_style.virtualAddress;
setting_gps_sets_time.fladdr = gps_sets_time.virtualAddress;
setting_gmt_ofs_hr.fladdr = gmt_ofs_hr.virtualAddress;
setting_gmt_ofs_min.fladdr = gmt_ofs_min.virtualAddress;
/*
* EE_Init() fails if the virtual addresses are not found in the flash. If
* it fails, we write the default setting values. Otherwise, we apply the
* settings in the flash to the setting values in RAM
*/
if (!EE_Init(2)) {
EE_Format(2);
EE_Write(&gps_on, setting_gps_on.val);
EE_Write(&coord_style, setting_coord_style.val);
EE_Write(&gps_sets_time, setting_gps_sets_time.val);
EE_Write(&gmt_ofs_hr, setting_gmt_ofs_hr.val);
EE_Write(&gmt_ofs_min, setting_gmt_ofs_min.val);
} else {
EE_Read(&gps_on, &setting_gps_on.val);
EE_Read(&coord_style, &setting_coord_style.val);
EE_Read(&gps_sets_time, &setting_gps_sets_time.val);
EE_Read(&gmt_ofs_hr, &setting_gmt_ofs_hr.val);
EE_Read(&gmt_ofs_min, &setting_gmt_ofs_min.val);
}
}
void setting_change(setting_t *setting)
{
int v = setting->val;
char s[16];
v++;
v %= setting->nrvals;
sprintf(s, ": %d", v);
setting_apply(setting, v);
}
void setting_apply(setting_t *setting, int val)
{
EE_Variable_TypeDef eevar;
eevar.virtualAddress = setting->fladdr;
taskENTER_CRITICAL();
setting->val = val;
EE_Write(&eevar, val);
taskEXIT_CRITICAL();
}
......
......@@ -30,10 +30,13 @@
#include "application.h"
#include <stdint.h>
typedef struct setting {
char name[16];
int val;
int nrvals;
uint16_t val; // uint16_t to comply to eeprom_emulation code
uint16_t nrvals;
uint16_t fladdr;
} setting_t;
extern application set_time;
......@@ -47,6 +50,7 @@ extern setting_t setting_gmt_ofs_hr;
extern setting_t setting_gmt_ofs_min;
extern setting_t setting_gps_sets_time;
void setting_init();
void setting_change(setting_t *setting);
void setting_apply(setting_t *setting, int val);
int setting_get(setting_t *setting);
......
......@@ -27,6 +27,7 @@
#include <em_chip.h>
#include <em_gpio.h>
#include <em_cmu.h>
#include <em_msc.h>
#include <sleep.h>
#include <drivers/i2cdrv.h>
......@@ -40,6 +41,7 @@
#include <gfx/ui.h>
#include <apps/app_list.h>
#include "settings/settings.h"
#include "battery_task.h"
#include "blight_task.h"
#include "state.h"
......@@ -55,13 +57,16 @@ int main(void)
CHIP_Init();
// Enable clocks
//CMU_ClockSelectSet(cmuClock_HF, cmuSelect_HFXO);
CMU_ClockSelectSet(cmuClock_HF, cmuSelect_HFXO);
CMU_ClockEnable(cmuClock_HFPER, true);
CMU_ClockEnable(cmuClock_GPIO, true);
I2C_Init_TypeDef i2cInit = I2C_INIT_DEFAULT;
I2CDRV_Init(&i2cInit);
MSC_Init();
setting_init();
#ifdef DEBUG
usbdbg_init();
#endif
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment