nonvolatile data

This commit is contained in:
Conor Patrick 2018-10-26 00:04:10 -04:00
parent d383ce67bf
commit 528e62173d
3 changed files with 53 additions and 19 deletions

View File

@ -35,6 +35,6 @@ void hw_init(void);
#define SOLO_BUTTON_PORT GPIOA #define SOLO_BUTTON_PORT GPIOA
#define SOLO_BUTTON_PIN LL_GPIO_PIN_0 #define SOLO_BUTTON_PIN LL_GPIO_PIN_0
#define SKIP_BUTTON_CHECK 0 #define SKIP_BUTTON_CHECK 1
#endif #endif

View File

@ -16,6 +16,23 @@
#include "fifo.h" #include "fifo.h"
#include "log.h" #include "log.h"
#define PAGE_SIZE 2048
#define PAGES 128
// Pages 120-127 are data
#define COUNTER_PAGE (PAGES - 3)
#define STATE2_PAGE (PAGES - 2)
#define STATE1_PAGE (PAGES - 1)
#define APPLICATION_START_PAGE (0)
#define APPLICATION_START_ADDR flash_addr(APPLICATION_START_PAGE)
#define APPLICATION_END_PAGE ((PAGES - 8)) // 120 is NOT included in application
#define APPLICATION_END_ADDR (flash_addr(APPLICATION_END_PAGE)-4) // NOT included in application
#define AUTH_WORD_ADDR (flash_addr(APPLICATION_END_PAGE)-4)
uint32_t __65_seconds = 0; uint32_t __65_seconds = 0;
extern PCD_HandleTypeDef hpcd; extern PCD_HandleTypeDef hpcd;
@ -128,24 +145,43 @@ void heartbeat()
void authenticator_read_state(AuthenticatorState * a) void authenticator_read_state(AuthenticatorState * a)
{ {
uint32_t * ptr = (uint32_t *)flash_addr(STATE1_PAGE);
memmove(a,ptr,sizeof(AuthenticatorState));
} }
void authenticator_read_backup_state(AuthenticatorState * a) void authenticator_read_backup_state(AuthenticatorState * a)
{ {
uint32_t * ptr = (uint32_t *)flash_addr(STATE2_PAGE);
memmove(a,ptr,sizeof(AuthenticatorState));
} }
// Return 1 yes backup is init'd, else 0 // Return 1 yes backup is init'd, else 0
//void authenticator_initialize()
int authenticator_is_backup_initialized() int authenticator_is_backup_initialized()
{ {
return 0; uint8_t header[16];
uint32_t * ptr = (uint32_t *)flash_addr(STATE2_PAGE);
memmove(header,ptr,16);
AuthenticatorState * state = (AuthenticatorState*)header;
return state->is_initialized == INITIALIZED_MARKER;
} }
void authenticator_write_state(AuthenticatorState * a, int backup) void authenticator_write_state(AuthenticatorState * a, int backup)
{ {
uint32_t * ptr;
if (! backup)
{
ptr = (uint32_t *)(PAGE_SIZE*STATE1_PAGE);
flash_erase_page(STATE1_PAGE);
flash_write(flash_addr(STATE1_PAGE), (uint8_t*)a, sizeof(AuthenticatorState));
}
else
{
ptr = (uint32_t *)(PAGE_SIZE*STATE2_PAGE);
flash_erase_page(STATE2_PAGE);
flash_write(flash_addr(STATE2_PAGE), (uint8_t*)a, sizeof(AuthenticatorState));
}
} }
void device_manage() void device_manage()

View File

@ -41,7 +41,7 @@ _Min_Stack_Size = 0x400; /* required amount of stack */
/* Specify the memory areas */ /* Specify the memory areas */
MEMORY MEMORY
{ {
FLASH (rx) : ORIGIN = 0x08000000, LENGTH = 256K FLASH (rx) : ORIGIN = 0x08000000, LENGTH = 240K /* Leave out 16 Kb for data */
RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 48K RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 48K
SRAM2 (rw) : ORIGIN = 0x10000000, LENGTH = 16K SRAM2 (rw) : ORIGIN = 0x10000000, LENGTH = 16K
} }
@ -83,8 +83,8 @@ SECTIONS
. = ALIGN(8); . = ALIGN(8);
} >FLASH } >FLASH
.ARM.extab : .ARM.extab :
{ {
. = ALIGN(8); . = ALIGN(8);
*(.ARM.extab* .gnu.linkonce.armextab.*) *(.ARM.extab* .gnu.linkonce.armextab.*)
. = ALIGN(8); . = ALIGN(8);
@ -105,7 +105,7 @@ SECTIONS
PROVIDE_HIDDEN (__preinit_array_end = .); PROVIDE_HIDDEN (__preinit_array_end = .);
. = ALIGN(8); . = ALIGN(8);
} >FLASH } >FLASH
.init_array : .init_array :
{ {
. = ALIGN(8); . = ALIGN(8);
@ -129,7 +129,7 @@ SECTIONS
_sidata = LOADADDR(.data); _sidata = LOADADDR(.data);
/* Initialized data sections goes into RAM, load LMA copy after code */ /* Initialized data sections goes into RAM, load LMA copy after code */
.data : .data :
{ {
. = ALIGN(8); . = ALIGN(8);
_sdata = .; /* create a global symbol at data start */ _sdata = .; /* create a global symbol at data start */
@ -142,11 +142,11 @@ SECTIONS
_sisram2 = LOADADDR(.sram2); _sisram2 = LOADADDR(.sram2);
/* CCM-RAM section /* CCM-RAM section
* *
* IMPORTANT NOTE! * IMPORTANT NOTE!
* If initialized variables will be placed in this section, * If initialized variables will be placed in this section,
* the startup code needs to be modified to copy the init-values. * the startup code needs to be modified to copy the init-values.
*/ */
.sram2 : .sram2 :
{ {
@ -154,12 +154,12 @@ SECTIONS
_ssram2 = .; /* create a global symbol at sram2 start */ _ssram2 = .; /* create a global symbol at sram2 start */
*(.sram2) *(.sram2)
*(.sram2*) *(.sram2*)
. = ALIGN(8); . = ALIGN(8);
_esram2 = .; /* create a global symbol at sram2 end */ _esram2 = .; /* create a global symbol at sram2 end */
} >SRAM2 AT> FLASH } >SRAM2 AT> FLASH
/* Uninitialized data section */ /* Uninitialized data section */
. = ALIGN(4); . = ALIGN(4);
.bss : .bss :
@ -187,7 +187,7 @@ SECTIONS
. = ALIGN(8); . = ALIGN(8);
} >RAM } >RAM
/* Remove information from the standard libraries */ /* Remove information from the standard libraries */
/DISCARD/ : /DISCARD/ :
@ -199,5 +199,3 @@ SECTIONS
.ARM.attributes 0 : { *(.ARM.attributes) } .ARM.attributes 0 : { *(.ARM.attributes) }
} }