Skip to content

Commit 0042cd4

Browse files
d00616henrikekblad
authored andcommitted
NVM: consideration of SoftDevice memory layout (#977)
1 parent 2d5404d commit 0042cd4

File tree

3 files changed

+21
-10
lines changed

3 files changed

+21
-10
lines changed

drivers/NRF5/Flash.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
1818
*/
1919
#include "drivers/NVM/Flash.h"
20+
#include <nrf.h>
2021

2122
FlashClass Flash;
2223

@@ -49,6 +50,19 @@ uint32_t *FlashClass::page_address(size_t page)
4950
return (uint32_t *)(page << page_size_bits());
5051
}
5152

53+
uint32_t *FlashClass::top_app_page_address()
54+
{
55+
// Bootcode at the top of the flash memory?
56+
// https://infocenter.nordicsemi.com/index.jsp?topic=%2Fcom.nordic.infocenter.sdk5.v12.0.0%2Flib_bootloader.html
57+
if (NRF_UICR->NRFFW[0]<0xFFFFFFFF) {
58+
// Return pointer calculated by SoftDevice/bootloader
59+
return (uint32_t *)NRF_UICR->NRFFW[0];
60+
}
61+
62+
// Return flash length
63+
return (uint32_t *)(Flash.page_count() << Flash.page_size_bits());
64+
}
65+
5266
void FlashClass::erase(uint32_t *address, size_t size)
5367
{
5468
size_t end_address = (size_t)address + size;

drivers/NVM/Flash.h

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,6 @@
3030
#include <Arduino.h>
3131
#include <stdio.h> // for size_t
3232

33-
#ifdef __RFduino__
34-
#include <chip.h>
35-
#else
36-
#include <nrf.h>
37-
#endif
38-
3933
/*
4034
* Define characteristics of Flash
4135
*
@@ -133,6 +127,10 @@ class FlashClass
133127
* @return address of given page
134128
*/
135129
uint32_t *page_address(size_t page);
130+
/** Get top of available flash for application data
131+
* @return Last available address + 1
132+
*/
133+
uint32_t *top_app_page_address();
136134
//----------------------------------------------------------------------------
137135
/*
138136
* Accessing flash memory

drivers/NVM/VirtualPage.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ void VirtualPageClass::format()
300300

301301
uint32_t *VirtualPageClass::get_page_address(uint16_t page)
302302
{
303-
return (uint32_t *)((Flash.page_count() << Flash.page_size_bits()) -
303+
return (uint32_t *)(Flash.top_app_page_address() -
304304
((page + VNM_VIRTUAL_PAGE_SKIP_FROM_TOP)
305305
<< VNM_VIRTUAL_PAGE_SIZE_BITS));
306306
}
@@ -336,7 +336,6 @@ void VirtualPageClass::build_page(uint32_t *address, uint32_t magic)
336336
uint32_t VirtualPageClass::get_page_erase_cycles(uint32_t *address)
337337
{
338338
// Return number of cycles
339-
return ((uint32_t)address[OFFSET_ERASE_COUNTER] &
340-
(uint32_t)MASK_ERASE_COUNTER) +
341-
1;
339+
return ((((uint32_t)address[OFFSET_ERASE_COUNTER])+1) &
340+
(uint32_t)MASK_ERASE_COUNTER);
342341
}

0 commit comments

Comments
 (0)