Skip to content

Commit c5f644f

Browse files
committed
Fixed nrf52 flash access
1 parent 62fcc55 commit c5f644f

1 file changed

Lines changed: 13 additions & 15 deletions

File tree

hal/nrf52.c

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,13 @@ 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-
NVMC_CONFIG = NVMC_CONFIG_WEN;
58-
flash_wait_complete();
59-
/* Set 8-bit write */
60-
for (i = 0; i < len; i++) {
61-
((uint8_t *)(address))[i] = data[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 ++) {
61+
NVMC_CONFIG = NVMC_CONFIG_WEN;
62+
flash_wait_complete();
63+
dst[i] = src[i];
6264
flash_wait_complete();
6365
}
6466
return 0;
@@ -75,16 +77,12 @@ void hal_flash_lock(void)
7577

7678
int hal_flash_erase(uint32_t address, int len)
7779
{
78-
int start = -1, end = -1, i;
79-
uint32_t end_address = address + len;
80-
start = (address / FLASH_PAGE_SIZE);
81-
if (start == 0)
82-
return -1;
83-
end = end_address / FLASH_PAGE_SIZE;
84-
NVMC_CONFIG = NVMC_CONFIG_EEN;
85-
flash_wait_complete();
86-
for (i = start; i <= end; i++) {
87-
NVMC_ERASEPAGE = i;
80+
uint32_t end = address + len;
81+
uint32_t p;
82+
for (p = address; p <= end; p += FLASH_PAGE_SIZE) {
83+
NVMC_CONFIG = NVMC_CONFIG_EEN;
84+
flash_wait_complete();
85+
NVMC_ERASEPAGE = p;
8886
flash_wait_complete();
8987
}
9088
return 0;

0 commit comments

Comments
 (0)