Skip to content

Commit 24f8c09

Browse files
committed
Fixed nrf52 hal for unaligned byte write operations
1 parent 861181d commit 24f8c09

3 files changed

Lines changed: 38 additions & 11 deletions

File tree

Makefile

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,6 @@ ifeq ($(VTOR),0)
5757
CFLAGS+=-DNO_VTOR
5858
endif
5959

60-
ifeq ($(SWAP),0)
61-
CFLAGS+=-DWOLFBOOT_OVERWRITE_ONLY
62-
endif
63-
64-
6560
LDFLAGS:=-T $(LSCRIPT) -Wl,-gc-sections -Wl,-Map=wolfboot.map -ffreestanding -nostartfiles -mcpu=cortex-m3 -mthumb -nostdlib
6661

6762
all: factory.bin

hal/nrf52.c

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,46 @@ static void flash_wait_complete(void)
5454
int hal_flash_write(uint32_t address, const uint8_t *data, int len)
5555
{
5656
int i;
57-
int words = (len + 3) / 4;
58-
uint32_t *src = (uint32_t *)data;
59-
uint32_t *dst = (uint32_t *)address;
60-
for (i = 0; i < words; i ++) {
57+
uint8_t off = address & 0x03;
58+
int words, align;
59+
60+
if (off != 0) {
61+
uint32_t first = *((uint32_t *)(address - off));
62+
uint8_t *firstbytes = (uint8_t *)(&first);
63+
for (i = 0; i < 4 - off; i++) {
64+
firstbytes[i + off] = data[i];
65+
}
6166
NVMC_CONFIG = NVMC_CONFIG_WEN;
6267
flash_wait_complete();
63-
dst[i] = src[i];
68+
*((uint32_t *)(address - off)) = first;
6469
flash_wait_complete();
70+
address += 4 - off;
71+
data += off;
72+
}
73+
if (len > 3) {
74+
uint32_t *src = (uint32_t *)data;
75+
uint32_t *dst = (uint32_t *)address;
76+
len -= off;
77+
words = len / 4;
78+
align = words * 4;
79+
for (i = 0; i < words; i ++) {
80+
NVMC_CONFIG = NVMC_CONFIG_WEN;
81+
flash_wait_complete();
82+
dst[i] = src[i];
83+
flash_wait_complete();
84+
}
85+
if (len > align) {
86+
uint32_t last = dst[words];
87+
uint8_t *lastbytes = (uint8_t *)(&last);
88+
for (i = off; i < 4; i++) {
89+
if (i < len - align)
90+
lastbytes[3 - i] = data[align + i];
91+
}
92+
NVMC_CONFIG = NVMC_CONFIG_WEN;
93+
flash_wait_complete();
94+
dst[words] = last;
95+
flash_wait_complete();
96+
}
6597
}
6698
return 0;
6799
}

lib/wolfssl

Submodule wolfssl updated 52 files

0 commit comments

Comments
 (0)