Skip to content

Commit 8eb8374

Browse files
committed
Added NVM_FLASH_WRITEONCE compile option. Optimized write in kinetis HAL.
1 parent 472cdd0 commit 8eb8374

6 files changed

Lines changed: 54 additions & 24 deletions

File tree

Makefile

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ CORTEX_M0?=0
1717
NO_ASM?=0
1818
EXT_FLASH?=0
1919
ALLOW_DOWNGRADE?=0
20+
NVM_FLASH_WRITEONCE?=0
2021

2122
LSCRIPT:=hal/$(TARGET).ld
2223

@@ -76,16 +77,20 @@ CFLAGS+=-mthumb -Wall -Wextra -Wno-main -Wstack-usage=1024 -ffreestanding -Wno-u
7677
-DPLATFORM_$(TARGET)
7778

7879
ifeq ($(TARGET),kinetis)
79-
CFLAGS+=-I$(KINETIS)/drivers -I$(KINETIS) -DCPU_MK64FN1M0VLL12 -I$(KINETIS_CMSIS)/Include -DDEBUG_CONSOLE_ASSERT_DISABLE=1
80-
OBJS+=$(KINETIS)/drivers/fsl_clock.o $(KINETIS)/drivers/fsl_ftfx_flash.o $(KINETIS)/drivers/fsl_ftfx_cache.o $(KINETIS)/drivers/fsl_ftfx_controller.o
80+
CFLAGS+= -I$(KINETIS)/drivers -I$(KINETIS) -DCPU_MK64FN1M0VLL12 -I$(KINETIS_CMSIS)/Include -DDEBUG_CONSOLE_ASSERT_DISABLE=1
81+
OBJS+= $(KINETIS)/drivers/fsl_clock.o $(KINETIS)/drivers/fsl_ftfx_flash.o $(KINETIS)/drivers/fsl_ftfx_cache.o $(KINETIS)/drivers/fsl_ftfx_controller.o
8182
endif
8283

8384
ifeq ($(EXT_FLASH),1)
84-
CFLAGS+=-DEXT_FLASH=1 -DPART_UPDATE_EXT=1 -DPART_SWAP_EXT=1
85+
CFLAGS+= -DEXT_FLASH=1 -DPART_UPDATE_EXT=1 -DPART_SWAP_EXT=1
8586
endif
8687

8788
ifeq ($(ALLOW_DOWNGRADE),1)
88-
CFLAGS+=-DALLOW_DOWNGRADE
89+
CFLAGS+= -DALLOW_DOWNGRADE
90+
endif
91+
92+
ifeq ($(NVM_FLASH_WRITEONCE),1)
93+
CFLAGS+= -DNVM_FLASH_WRITEONCE
8994
endif
9095

9196
LDFLAGS:=-T $(LSCRIPT) -Wl,-gc-sections -Wl,-Map=wolfboot.map -ffreestanding -nostartfiles -mcpu=cortex-m3 -mthumb

hal/kinetis.c

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,16 @@
2424
#include "fsl_common.h"
2525
#include "fsl_flash.h"
2626
#include "fsl_ftfx_cache.h"
27+
#include "fsl_sysmpu.h"
2728

2829
static flash_config_t pflash;
2930
static ftfx_cache_config_t pcache;
3031
static int flash_init = 0;
3132

33+
#ifndef NVM_FLASH_WRITEONCE
34+
# error "wolfBoot Kinetis HAL: no WRITEONCE support detected. Please define NVM_FLASH_WRITEONCE"
35+
#endif
36+
3237
#ifdef __WOLFBOOT
3338
#define CPU_CORE_CLOCK 120000000U
3439

@@ -124,13 +129,8 @@ const osc_config_t oscConfig_BOARD_BootClockRUN =
124129

125130
void hal_init(void)
126131
{
127-
// Disable Watchdog
128-
// Write 0xC520 to watchdog unlock register
129-
*((volatile unsigned short *)0x4005200E) = 0xC520;
130-
// Followed by 0xD928 to complete the unlock
131-
*((volatile unsigned short *)0x4005200E) = 0xD928;
132-
// Now disable watchdog via STCTRLH register
133-
*((volatile unsigned short *)0x40052000) = 0x01D2u;
132+
/* Disable MPU */
133+
SYSMPU_Enable(SYSMPU, false);
134134

135135
/* Set the system clock dividers in SIM to safe value. */
136136
CLOCK_SetSimSafeDivs();
@@ -175,6 +175,7 @@ int hal_flash_write(uint32_t address, const uint8_t *data, int len)
175175
{
176176
int w = 0;
177177
int ret;
178+
const uint8_t empty_dword[8] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
178179
do_flash_init();
179180

180181
while (len > 0) {
@@ -186,9 +187,11 @@ int hal_flash_write(uint32_t address, const uint8_t *data, int len)
186187
memcpy(aligned_dword, address_align, 8);
187188
for (i = start_off; ((i < 8) && (i < len + start_off)); i++)
188189
aligned_dword[i] = data[w++];
189-
ret = FLASH_Program(&pflash, address_align, aligned_dword, 8);
190-
if (ret != kStatus_FTFx_Success)
191-
return -1;
190+
if (memcmp(aligned_dword, empty_dword, 8) != 0) {
191+
ret = FLASH_Program(&pflash, address_align, aligned_dword, 8);
192+
if (ret != kStatus_FTFx_Success)
193+
return -1;
194+
}
192195
address += i;
193196
len -= i;
194197
} else {

hal/kinetis.ld

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
ENTRY(isr_reset)
2-
31
MEMORY
42
{
53
FLASH (rx) : ORIGIN = 0x00000000, LENGTH = 0x000A000
6-
RAM (rwx) : ORIGIN = 0x20000000, LENGTH = 0x00010000
4+
RAM (rwx) : ORIGIN = 0x1FFF0000, LENGTH = 64K
75
}
86

97
SECTIONS

src/libwolfboot.c

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,24 @@ uint32_t ext_cache;
3535
#define PART_BOOT_ENDFLAGS ((WOLFBOOT_PARTITION_BOOT_ADDRESS + WOLFBOOT_PARTITION_SIZE) - TRAILER_SKIP)
3636
#define PART_UPDATE_ENDFLAGS ((WOLFBOOT_PARTITION_UPDATE_ADDRESS + WOLFBOOT_PARTITION_SIZE) - TRAILER_SKIP)
3737

38+
#ifdef NVM_FLASH_WRITEONCE
39+
static uint8_t NVM_CACHE[WOLFBOOT_SECTOR_SIZE];
40+
int hal_trailer_write(uint32_t addr, uint8_t val) {
41+
uint32_t addr_align = addr & (~(WOLFBOOT_SECTOR_SIZE - 1));
42+
uint32_t addr_off = addr & (WOLFBOOT_SECTOR_SIZE - 1);
43+
int ret = 0;
44+
memcpy(NVM_CACHE, (void *)addr_align, WOLFBOOT_SECTOR_SIZE);
45+
ret = hal_flash_erase(addr_align, WOLFBOOT_SECTOR_SIZE);
46+
if (ret != 0)
47+
return ret;
48+
NVM_CACHE[addr_off] = val;
49+
ret = hal_flash_write(addr_align, NVM_CACHE, WOLFBOOT_SECTOR_SIZE);
50+
return ret;
51+
}
52+
#else
53+
# define hal_trailer_write(addr, val) hal_flash_write(addr, (void *)&val, 1)
54+
#endif
55+
3856
#if defined PART_UPDATE_EXT
3957
static uint8_t *get_trailer_at(uint8_t part, uint32_t at)
4058
{
@@ -50,7 +68,7 @@ static uint8_t *get_trailer_at(uint8_t part, uint32_t at)
5068
static void set_trailer_at(uint8_t part, uint32_t at, uint8_t val)
5169
{
5270
if (part == PART_BOOT) {
53-
hal_flash_write(PART_BOOT_ENDFLAGS - (sizeof(uint32_t) + at), (void *)&val, 1);
71+
hal_trailer_write(PART_BOOT_ENDFLAGS - (sizeof(uint32_t) + at), val);
5472
}
5573
else if (part == PART_UPDATE) {
5674
ext_flash_write(PART_UPDATE_ENDFLAGS - (sizeof(uint32_t) + at), (void *)&val, 1);
@@ -82,10 +100,10 @@ static uint8_t *get_trailer_at(uint8_t part, uint32_t at)
82100
static void set_trailer_at(uint8_t part, uint32_t at, uint8_t val)
83101
{
84102
if (part == PART_BOOT) {
85-
hal_flash_write(PART_BOOT_ENDFLAGS - (sizeof(uint32_t) + at), (void *)&val, 1);
103+
hal_trailer_write(PART_BOOT_ENDFLAGS - (sizeof(uint32_t) + at), val);
86104
}
87105
else if (part == PART_UPDATE) {
88-
hal_flash_write(PART_UPDATE_ENDFLAGS - (sizeof(uint32_t) + at), (void *)&val, 1);
106+
hal_trailer_write(PART_UPDATE_ENDFLAGS - (sizeof(uint32_t) + at), val);
89107
}
90108
}
91109

src/loader.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ extern void do_boot(const uint32_t *app_offset);
2727
#define FLASHBUFFER_SIZE 256
2828
static int wolfBoot_copy_sector(struct wolfBoot_image *src, struct wolfBoot_image *dst, uint32_t sector)
2929
{
30-
volatile uint32_t *orig, *copy;
3130
uint32_t pos = 0;
3231
uint32_t src_sector_offset = (sector * WOLFBOOT_SECTOR_SIZE);
3332
uint32_t dst_sector_offset = (sector * WOLFBOOT_SECTOR_SIZE);
@@ -52,7 +51,7 @@ static int wolfBoot_copy_sector(struct wolfBoot_image *src, struct wolfBoot_imag
5251
#endif
5352
wb_flash_erase(dst, dst_sector_offset, WOLFBOOT_SECTOR_SIZE);
5453
while (pos < WOLFBOOT_SECTOR_SIZE) {
55-
orig = (volatile uint32_t *)(src->hdr + src_sector_offset + pos);
54+
uint8_t *orig = (uint8_t*)(src->hdr + src_sector_offset + pos);
5655
wb_flash_write(dst, dst_sector_offset + pos, orig, FLASHBUFFER_SIZE);
5756
pos += FLASHBUFFER_SIZE;
5857
}

src/wolfboot.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,15 @@ extern uint32_t *END_STACK;
3232
extern void main(void);
3333

3434
void isr_reset(void) {
35-
unsigned int *src, *dst;
36-
35+
register unsigned int *src, *dst;
36+
#ifdef PLATFORM_kinetis
37+
/* Immediately disable Watchdog after boot */
38+
/* Write Keys to unlock register */
39+
*((volatile unsigned short *)0x4005200E) = 0xC520;
40+
*((volatile unsigned short *)0x4005200E) = 0xD928;
41+
/* disable watchdog via STCTRLH register */
42+
*((volatile unsigned short *)0x40052000) = 0x01D2u;
43+
#endif
3744
/* Copy the .data section from flash to RAM. */
3845
src = (unsigned int *) &_stored_data;
3946
dst = (unsigned int *) &_start_data;

0 commit comments

Comments
 (0)