-
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
22aa9f4
commit 3fd6fac
Showing
5 changed files
with
230 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/* | ||
* Copyright (C) 2020 Dyne.org foundation | ||
* | ||
* This file is subject to the terms and conditions of the GNU | ||
* General Public License (GPL) version 2. See the file LICENSE | ||
* for more details. | ||
* | ||
*/ | ||
#include <stdint.h> | ||
#include "nvmc.h" | ||
|
||
/* Flash write/erase control */ | ||
#define NVMC_BASE (0x4001E000) | ||
#define NVMC_CONFIG *((volatile uint32_t *)(NVMC_BASE + 0x504)) | ||
#define NVMC_ERASEPAGE *((volatile uint32_t *)(NVMC_BASE + 0x508)) | ||
#define NVMC_READY *((volatile uint32_t *)(NVMC_BASE + 0x400)) | ||
#define NVMC_CONFIG_REN 0 | ||
#define NVMC_CONFIG_WEN 1 | ||
#define NVMC_CONFIG_EEN 2 | ||
|
||
|
||
|
||
static void flash_wait_complete(void) | ||
{ | ||
while (NVMC_READY == 0) | ||
; | ||
} | ||
|
||
int flash_write(uint32_t address, const uint8_t *data, int len) | ||
{ | ||
int i = 0; | ||
uint32_t *src, *dst; | ||
|
||
while (i < len) { | ||
if ((len - i > 3) && ((((address + i) & 0x03) == 0) && ((((uint32_t)data) + i) & 0x03) == 0)) { | ||
src = (uint32_t *)data; | ||
dst = (uint32_t *)address; | ||
NVMC_CONFIG = NVMC_CONFIG_WEN; | ||
flash_wait_complete(); | ||
dst[i >> 2] = src[i >> 2]; | ||
flash_wait_complete(); | ||
i+=4; | ||
} else { | ||
uint32_t val; | ||
uint8_t *vbytes = (uint8_t *)(&val); | ||
int off = (address + i) - (((address + i) >> 2) << 2); | ||
dst = (uint32_t *)(address - off); | ||
val = dst[i >> 2]; | ||
vbytes[off] = data[i]; | ||
NVMC_CONFIG = NVMC_CONFIG_WEN; | ||
flash_wait_complete(); | ||
dst[i >> 2] = val; | ||
flash_wait_complete(); | ||
i++; | ||
} | ||
} | ||
return 0; | ||
} | ||
|
||
|
||
int flash_erase(uint32_t address, int len) | ||
{ | ||
uint32_t end = address + len - 1; | ||
uint32_t p; | ||
for (p = address; p <= end; p += FLASH_PAGE_SIZE) { | ||
NVMC_CONFIG = NVMC_CONFIG_EEN; | ||
flash_wait_complete(); | ||
NVMC_ERASEPAGE = p; | ||
flash_wait_complete(); | ||
} | ||
return 0; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
#ifndef NVMC_H | ||
#define NVMC_H | ||
#define FLASH_PAGE_SIZE (4096) | ||
int flash_write(uint32_t address, const uint8_t *data, int len); | ||
int flash_erase(uint32_t address, int len); | ||
#endif |