-
Notifications
You must be signed in to change notification settings - Fork 0
/
Flash_rw.c
75 lines (59 loc) · 1.9 KB
/
Flash_rw.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <stdbool.h>
#include "nrf.h"
#include "flash_rw.h"
uint32_t pg_size;
uint32_t pg_num;
patold = 0;
pg_size = NRF_FICR->CODEPAGESIZE;
pg_num = NRF_FICR->CODESIZE - 1; // Use last page in flash
/** @brief Function for erasing a page in flash.
*
* @param page_address Address of the first word in the page to be erased.
*/
static void flash_page_erase(uint32_t *page_address)
{
// Turn on flash erase enable and wait until the NVMC is ready:
NRF_NVMC->CONFIG = (NVMC_CONFIG_WEN_Een << NVMC_CONFIG_WEN_Pos);
while (NRF_NVMC->READY == NVMC_READY_READY_Busy)
{
// Do nothing.
}
// Erase page:
NRF_NVMC->ERASEPAGE = (uint32_t)page_address;
while (NRF_NVMC->READY == NVMC_READY_READY_Busy)
{
// Do nothing.
}
// Turn off flash erase enable and wait until the NVMC is ready:
NRF_NVMC->CONFIG = (NVMC_CONFIG_WEN_Ren << NVMC_CONFIG_WEN_Pos);
while (NRF_NVMC->READY == NVMC_READY_READY_Busy)
{
// Do nothing.
}
}
/** @brief Function for filling a page in flash with a value.
*
* @param[in] address Address of the first word in the page to be filled.
* @param[in] value Value to be written to flash.
*/
static void flash_word_write(uint32_t *address, uint32_t value)
{
// Turn on flash write enable and wait until the NVMC is ready:
NRF_NVMC->CONFIG = (NVMC_CONFIG_WEN_Wen << NVMC_CONFIG_WEN_Pos);
while (NRF_NVMC->READY == NVMC_READY_READY_Busy)
{
// Do nothing.
}
*address = value;
while (NRF_NVMC->READY == NVMC_READY_READY_Busy)
{
// Do nothing.
}
// Turn off flash write enable and wait until the NVMC is ready:
NRF_NVMC->CONFIG = (NVMC_CONFIG_WEN_Ren << NVMC_CONFIG_WEN_Pos);
while (NRF_NVMC->READY == NVMC_READY_READY_Busy)
{
// Do nothing.
}
}
/** @} */