From 528e62173d4495c78911a92a9d3360fe57a2526d Mon Sep 17 00:00:00 2001 From: Conor Patrick Date: Fri, 26 Oct 2018 00:04:10 -0400 Subject: [PATCH] nonvolatile data --- targets/stm32l442/src/app.h | 2 +- targets/stm32l442/src/device.c | 44 +++++++++++++++++++++++++++++--- targets/stm32l442/stm32l432xx.ld | 26 +++++++++---------- 3 files changed, 53 insertions(+), 19 deletions(-) diff --git a/targets/stm32l442/src/app.h b/targets/stm32l442/src/app.h index c75a265..0db87d3 100644 --- a/targets/stm32l442/src/app.h +++ b/targets/stm32l442/src/app.h @@ -35,6 +35,6 @@ void hw_init(void); #define SOLO_BUTTON_PORT GPIOA #define SOLO_BUTTON_PIN LL_GPIO_PIN_0 -#define SKIP_BUTTON_CHECK 0 +#define SKIP_BUTTON_CHECK 1 #endif diff --git a/targets/stm32l442/src/device.c b/targets/stm32l442/src/device.c index 754da57..08a2d1b 100644 --- a/targets/stm32l442/src/device.c +++ b/targets/stm32l442/src/device.c @@ -16,6 +16,23 @@ #include "fifo.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; extern PCD_HandleTypeDef hpcd; @@ -128,24 +145,43 @@ void heartbeat() 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) { - + uint32_t * ptr = (uint32_t *)flash_addr(STATE2_PAGE); + memmove(a,ptr,sizeof(AuthenticatorState)); } // Return 1 yes backup is init'd, else 0 -//void authenticator_initialize() 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) { + 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() diff --git a/targets/stm32l442/stm32l432xx.ld b/targets/stm32l442/stm32l432xx.ld index 70f0efe..762abfe 100644 --- a/targets/stm32l442/stm32l432xx.ld +++ b/targets/stm32l442/stm32l432xx.ld @@ -41,7 +41,7 @@ _Min_Stack_Size = 0x400; /* required amount of stack */ /* Specify the memory areas */ 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 SRAM2 (rw) : ORIGIN = 0x10000000, LENGTH = 16K } @@ -83,8 +83,8 @@ SECTIONS . = ALIGN(8); } >FLASH - .ARM.extab : - { + .ARM.extab : + { . = ALIGN(8); *(.ARM.extab* .gnu.linkonce.armextab.*) . = ALIGN(8); @@ -105,7 +105,7 @@ SECTIONS PROVIDE_HIDDEN (__preinit_array_end = .); . = ALIGN(8); } >FLASH - + .init_array : { . = ALIGN(8); @@ -129,7 +129,7 @@ SECTIONS _sidata = LOADADDR(.data); /* Initialized data sections goes into RAM, load LMA copy after code */ - .data : + .data : { . = ALIGN(8); _sdata = .; /* create a global symbol at data start */ @@ -142,11 +142,11 @@ SECTIONS _sisram2 = LOADADDR(.sram2); - /* CCM-RAM section - * - * IMPORTANT NOTE! + /* CCM-RAM section + * + * IMPORTANT NOTE! * 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 : { @@ -154,12 +154,12 @@ SECTIONS _ssram2 = .; /* create a global symbol at sram2 start */ *(.sram2) *(.sram2*) - + . = ALIGN(8); _esram2 = .; /* create a global symbol at sram2 end */ } >SRAM2 AT> FLASH - + /* Uninitialized data section */ . = ALIGN(4); .bss : @@ -187,7 +187,7 @@ SECTIONS . = ALIGN(8); } >RAM - + /* Remove information from the standard libraries */ /DISCARD/ : @@ -199,5 +199,3 @@ SECTIONS .ARM.attributes 0 : { *(.ARM.attributes) } } - -