Fixed nrf52 hal for unaligned byte write operations

pull/1/head
Daniele Lacamera 2018-11-22 18:52:13 +01:00
parent 861181df30
commit 24f8c091ac
3 changed files with 38 additions and 11 deletions

View File

@ -57,11 +57,6 @@ ifeq ($(VTOR),0)
CFLAGS+=-DNO_VTOR
endif
ifeq ($(SWAP),0)
CFLAGS+=-DWOLFBOOT_OVERWRITE_ONLY
endif
LDFLAGS:=-T $(LSCRIPT) -Wl,-gc-sections -Wl,-Map=wolfboot.map -ffreestanding -nostartfiles -mcpu=cortex-m3 -mthumb -nostdlib
all: factory.bin

View File

@ -54,14 +54,46 @@ static void flash_wait_complete(void)
int hal_flash_write(uint32_t address, const uint8_t *data, int len)
{
int i;
int words = (len + 3) / 4;
uint32_t *src = (uint32_t *)data;
uint32_t *dst = (uint32_t *)address;
for (i = 0; i < words; i ++) {
uint8_t off = address & 0x03;
int words, align;
if (off != 0) {
uint32_t first = *((uint32_t *)(address - off));
uint8_t *firstbytes = (uint8_t *)(&first);
for (i = 0; i < 4 - off; i++) {
firstbytes[i + off] = data[i];
}
NVMC_CONFIG = NVMC_CONFIG_WEN;
flash_wait_complete();
dst[i] = src[i];
*((uint32_t *)(address - off)) = first;
flash_wait_complete();
address += 4 - off;
data += off;
}
if (len > 3) {
uint32_t *src = (uint32_t *)data;
uint32_t *dst = (uint32_t *)address;
len -= off;
words = len / 4;
align = words * 4;
for (i = 0; i < words; i ++) {
NVMC_CONFIG = NVMC_CONFIG_WEN;
flash_wait_complete();
dst[i] = src[i];
flash_wait_complete();
}
if (len > align) {
uint32_t last = dst[words];
uint8_t *lastbytes = (uint8_t *)(&last);
for (i = off; i < 4; i++) {
if (i < len - align)
lastbytes[3 - i] = data[align + i];
}
NVMC_CONFIG = NVMC_CONFIG_WEN;
flash_wait_complete();
dst[words] = last;
flash_wait_complete();
}
}
return 0;
}

@ -1 +1 @@
Subproject commit 1ffc1108a6f351054548ff4e9a1791d0118aa7fb
Subproject commit 84fb23cfabd00a3ba2ae8f53665526c9f3844a48