8000 Turn on nvm in 3.0. · rbarraud/circuitpython@812fe0c · GitHub
[go: up one dir, main page]

Skip to content

Commit 812fe0c

Browse files
committed
Turn on nvm in 3.0.
Its 256b on M0 and 8k on M4 to match flash erase sizes. Fixes adafruit#758
1 parent 10eabf6 commit 812fe0c

File tree

24 files changed

+55
-125
lines changed

24 files changed

+55
-125
lines changed

ports/atmel-samd/Makefile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,8 @@ SRC_COMMON_HAL = \
285285
analogio/__init__.c \
286286
analogio/AnalogIn.c \
287287
analogio/AnalogOut.c \
288+
nvm/__init__.c \
289+
nvm/ByteArray.c \
288290
pulseio/__init__.c \
289291
pulseio/PulseIn.c B41A \
290292
pulseio/PulseOut.c \
@@ -293,9 +295,7 @@ SRC_COMMON_HAL = \
293295
usb_hid/Device.c \
294296
audioio/__init__.c \
295297
audioio/AudioOut.c \
296-
# nvm/__init__.c \
297-
audiobusio/PDMIn.c \
298-
nvm/ByteArray.c \
298+
# audiobusio/PDMIn.c \
299299
touchio/__init__.c \
300300
touchio/TouchIn.c \
301301

ports/atmel-samd/boards/circuitplayground_express/mpconfigboard.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,7 @@
4242

4343
// If you change this, then make sure to update the linker scripts as well to
4444
// make sure you don't overwrite code.
45-
// #define CIRCUITPY_INTERNAL_NVM_SIZE 256
46-
#define CIRCUITPY_INTERNAL_NVM_SIZE 0
45+
#define CIRCUITPY_INTERNAL_NVM_SIZE 256
4746

4847
#define BOARD_FLASH_SIZE (0x00040000 - 0x2000 - CIRCUITPY_INTERNAL_NVM_SIZE)
4948

ports/atmel-samd/boards/feather_m0_express/mpconfigboard.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,7 @@
3535

3636
// If you change this, then make sure to update the linker scripts as well to
3737
// make sure you don't overwrite code.
38-
// #define CIRCUITPY_INTERNAL_NVM_SIZE 256
39-
#define CIRCUITPY_INTERNAL_NVM_SIZE 0
38+
#define CIRCUITPY_INTERNAL_NVM_SIZE 256
4039

4140
#define BOARD_FLASH_SIZE (0x00040000 - 0x2000 - CIRCUITPY_INTERNAL_NVM_SIZE)
4241

ports/atmel-samd/boards/feather_m0_supersized/mpconfigboard.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,7 @@
3737

3838
// If you change this, then make sure to update the linker scripts as well to
3939
// make sure you don't overwrite code.
40-
// #define CIRCUITPY_INTERNAL_NVM_SIZE 256
41-
#define CIRCUITPY_INTERNAL_NVM_SIZE 0
40+
#define CIRCUITPY_INTERNAL_NVM_SIZE 256
4241

4342
#define BOARD_FLASH_SIZE (0x00040000 - 0x2000 - CIRCUITPY_INTERNAL_NVM_SIZE)
4443

ports/atmel-samd/boards/feather_m4_express/mpconfigboard.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@
1919

2020
// If you change this, then make sure to update the linker scripts as well to
2121
// make sure you don't overwrite code
22-
// #define CIRCUITPY_INTERNAL_NVM_SIZE 256
23-
#define CIRCUITPY_INTERNAL_NVM_SIZE 0
22+
#define CIRCUITPY_INTERNAL_NVM_SIZE 8192
2423

2524
#define BOARD_FLASH_SIZE (FLASH_SIZE - 0x4000 - CIRCUITPY_INTERNAL_NVM_SIZE)
2625

ports/atmel-samd/boards/itsybitsy_m0_express/mpconfigboard.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@
3535

3636
// If you change this, then make sure to update the linker scripts as well to
3737
// make sure you don't overwrite code.
38-
// #define CIRCUITPY_INTERNAL_NVM_SIZE 256
39-
#define CIRCUITPY_INTERNAL_NVM_SIZE 0
38+
#define CIRCUITPY_INTERNAL_NVM_SIZE 256
39+
4040
#define BOARD_FLASH_SIZE (0x00040000 - 0x2000 - CIRCUITPY_INTERNAL_NVM_SIZE)
4141

4242
#include "external_flash/devices.h"

ports/atmel-samd/boards/itsybitsy_m4_express/mpconfigboard.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@
1919

2020
// If you change this, then make sure to update the linker scripts as well to
2121
// make sure you don't overwrite code
22-
// #define CIRCUITPY_INTERNAL_NVM_SIZE 256
23-
#define CIRCUITPY_INTERNAL_NVM_SIZE 0
22+
#define CIRCUITPY_INTERNAL_NVM_SIZE 8192
2423

2524
#define BOARD_FLASH_SIZE (FLASH_SIZE - 0x4000 - CIRCUITPY_INTERNAL_NVM_SIZE)
2625

ports/atmel-samd/boards/metro_m0_express/mpconfigboard.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,7 @@
3636

3737
// If you change this, then make sure to update the linker scripts as well to
3838
// make sure you don't overwrite code.
39-
// #define CIRCUITPY_INTERNAL_NVM_SIZE 256
40-
#define CIRCUITPY_INTERNAL_NVM_SIZE 0
39+
#define CIRCUITPY_INTERNAL_NVM_SIZE 256
4140

4241
#define BOARD_FLASH_SIZE (0x00040000 - 0x2000 - CIRCUITPY_INTERNAL_NVM_SIZE)
4342

ports/atmel-samd/boards/metro_m4_express/mpconfigboard.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@
2222

2323
// If you change this, then make sure to update the linker scripts as well to
2424
// make sure you don't overwrite code
25-
// #define CIRCUITPY_INTERNAL_NVM_SIZE 256
26-
#define CIRCUITPY_INTERNAL_NVM_SIZE 0
25+
#define CIRCUITPY_INTERNAL_NVM_SIZE 8192
2726

2827
#define BOARD_FLASH_SIZE (FLASH_SIZE - 0x4000 - CIRCUITPY_INTERNAL_NVM_SIZE)
2928

ports/atmel-samd/boards/metro_m4_express_revb/mpconfigboard.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,7 @@
4949

5050
// If you change this, then make sure to update the linker scripts as well to
5151
// make sure you don't overwrite code
52-
// #define CIRCUITPY_INTERNAL_NVM_SIZE 256
53-
#define CIRCUITPY_INTERNAL_NVM_SIZE 0
52+
#define CIRCUITPY_INTERNAL_NVM_SIZE 8192
5453

5554
#define BOARD_FLASH_SIZE (FLASH_SIZE - 0x4000 - CIRCUITPY_INTERNAL_NVM_SIZE)
5655

ports/atmel-samd/boards/samd51x18-bootloader-external-flash.ld

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
/* Specify the memory areas */
66
MEMORY
77
{
8-
/* Leave 16KiB for the bootloader. */
9-
FLASH (rx) : ORIGIN = 0x00000000 + 16K, LENGTH = 256K - 16K
8+
/* Leave 16KiB for the bootloader. 8K for user data*/
9+
FLASH (rx) : ORIGIN = 0x00000000 + 16K, LENGTH = 256K - 16K - 8K
1010
RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 128K
1111
}
1212

@@ -76,7 +76,7 @@ SECTIONS
7676
.stack :
7777
{
7878
. = ALIGN(4);
79-
. = . + 2K; /* Reserve a minimum of 2K for the stack. */
79+
. = . + 10K; /* Reserve a minimum of 10K for the stack. nvm will temporarily store 8k on the stack when writing. */
8080
. = ALIGN(4);
8181
} >RAM
8282

ports/atmel-samd/boards/samd51x19-bootloader-external-flash.ld

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
/* Specify the memory areas */
66
MEMORY
77
{
8-
/* Leave 16KiB for the bootloader. */
9-
FLASH (rx) : ORIGIN = 0x00000000 + 16K, LENGTH = 512K - 16K
8+
/* Leave 16KiB for the bootloader. 8K for user data*/
9+
FLASH (rx) : ORIGIN = 0x00000000 + 16K, LENGTH = 512K - 16K - 8K
1010
RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 192K
1111
}
1212

@@ -76,7 +76,7 @@ SECTIONS
7676
.stack :
7777
{
7878
. = ALIGN(4);
79-
. = . + 2K; /* Reserve a minimum of 2K for the stack. */
79+
. = . + 10K; /* Reserve a minimum of 10K for the stack. nvm will temporarily store 8k on the stack when writing. */
8080
. = ALIGN(4);
8181
} >RAM
8282

ports/atmel-samd/boards/samd51x19-bootloader.ld

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
/* Specify the memory areas */
66
MEMORY
77
{
8-
/* Leave 16KiB for the bootloader and 256KiB for the internal file system. */
9-
FLASH (rx) : ORIGIN = 0x00000000 + 16K, LENGTH = 512K - 16K - 256K
8+
/* Leave 16KiB for the bootloader, 256KiB for the internal file system, and 8KiB for user binary data. */
9+
FLASH (rx) : ORIGIN = 0x00000000 + 16K, LENGTH = 512K - 16K - 256K - 8K
1010
RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 192K
1111
}
1212

@@ -76,7 +76,7 @@ SECTIONS
7676
.stack :
7777
{
7878
. = ALIGN(4);
79-
. = . + 2K; /* Reserve a minimum of 2K for the stack. */
79+
. = . + 10K; /* Reserve a minimum of 10K for the stack. nvm will temporarily store 8k on the stack when writing. */
8080
. = ALIGN(4);
8181
} >RAM
8282

ports/atmel-samd/boards/samd51x20-bootloader-external-flash.ld

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
MEMORY
77
{
88
/* Leave 16KiB for the bootloader. */
9-
FLASH (rx) : ORIGIN = 0x00000000 + 16K, LENGTH = 1M - 16K
9+
FLASH (rx) : ORIGIN = 0x00000000 + 16K, LENGTH = 1M - 16K - 8K
1010
RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 256K
1111
}
1212

@@ -76,7 +76,7 @@ SECTIONS
7676
.stack :
7777
{
7878
. = ALIGN(4);
79-
. = . + 2K; /* Reserve a minimum of 2K for the stack. */
79+
. = . + 10K; /* Reserve a minimum of 10K for the stack. nvm will temporarily store 8k on the stack when writing. */
8080
. = ALIGN(4);
8181
} >RAM
8282

ports/atmel-samd/boards/samd51x20-bootloader.ld

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
/* Specify the memory areas */
66
MEMORY
77
{
8-
/* Leave 16KiB for the bootloader and 512k for the filesystem. */
9-
FLASH (rx) : ORIGIN = 0x00000000 + 16K, LENGTH = 1M - 16K - 512K
8+
/* Leave 16KiB for the bootloader, 512k for the filesystem and 8k for user config data. */
9+
FLASH (rx) : ORIGIN = 0x00000000 + 16K, LENGTH = 1M - 16K - 512K - 8K
1010
RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 256K
1111
}
1212

@@ -76,7 +76,7 @@ SECTIONS
7676
.stack :
7777
{
7878
. = ALIGN(4);
79-
. = . + 2K; /* Reserve a minimum of 2K for the stack. */
79+
. = . + 10K; /* Reserve a minimum of 10K for the stack. nvm will temporarily store 8k on the stack when writing. */
8080
. = ALIGN(4);
8181
} >RAM
8282

ports/atmel-samd/boards/samd51x20-external-flash.ld

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
/* Specify the memory areas */
66
MEMORY
77
{
8-
FLASH (rx) : ORIGIN = 0x00000000, LENGTH = 1M
8+
FLASH (rx) : ORIGIN = 0x00000000, LENGTH = 1M - 8K
99
RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 256K
1010
}
1111

@@ -74,7 +74,7 @@ SECTIONS
7474
.stack :
7575
{
7676
. = ALIGN(4);
77-
. = . + 2K; /* Reserve a minimum of 2K for the stack. */
77+
. = . + 10K; /* Reserve a minimum of 10K for the stack. nvm will temporarily store 8k on the stack when writing. */
7878
. = ALIGN(4);
7979
} >RAM
8080

ports/atmel-samd/boards/samd51x20.ld

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
/* Specify the memory areas */
66
MEMORY
77
{
8-
/* 1024 KiB minus 512KiB for the internal file system. */
9-
FLASH (rx) : ORIGIN = 0x00000000, LENGTH = 1M - 512K
8+
/* 1024 KiB minus 512KiB for the internal file system and 8KiB for the user nvm. */
9+
FLASH (rx) : ORIGIN = 0x00000000, LENGTH = 1M - 512K - 8K
1010
RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 256K
1111
}
1212

@@ -75,7 +75,7 @@ SECTIONS
7575
.stack :
7676
{
7777
. = ALIGN(4);
78-
. = . + 2K; /* Reserve a minimum of 2K for the stack. */
78+
. = . + 10K; /* Reserve a minimum of 10K for the stack. nvm will temporarily store 8k on the stack when writing. */
7979
. = ALIGN(4);
8080
} >RAM
8181

ports/atmel-samd/boards/trinket_m0_haxpress/mpconfigboard.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@
4444

4545
// If you change this, then make sure to update the linker scripts as well to
4646
// make sure you don't overwrite code.
47-
//#define CIRCUITPY_INTERNAL_NVM_SIZE 256
48-
#define CIRCUITPY_INTERNAL_NVM_SIZE 0
47+
#define CIRCUITPY_INTERNAL_NVM_SIZE 256
48+
4949
#define BOARD_FLASH_SIZE (0x00040000 - 0x2000 - CIRCUITPY_INTERNAL_NVM_SIZE)
5050

5151
#include "external_flash/devices.h"

ports/atmel-samd/boards/ugame10/mpconfigboard.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@
3737

3838
// If you change this, then make sure to update the linker scripts as well to
3939
// make sure you don't overwrite code.
40-
//#define CIRCUITPY_INTERNAL_NVM_SIZE 256
41-
#define CIRCUITPY_INTERNAL_NVM_SIZE 0
40+
#define CIRCUITPY_INTERNAL_NVM_SIZE 256
41+
4242
#define BOARD_FLASH_SIZE (0x00040000 - 0x2000 - CIRCUITPY_INTERNAL_NVM_SIZE)
4343

4444
#include "external_flash/devices.h"

ports/atmel-samd/clocks.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,12 @@ bool gclk_enabled(uint8_t gclk) {
7474

7575
void disable_gclk(uint8_t gclk) {
7676
#ifdef SAMD51
77+
while ((GCLK->SYNCBUSY.vec.GENCTRL & (1 << gclk)) != 0) {}
7778
GCLK->GENCTRL[gclk].bit.GENEN = false;
7879
while ((GCLK->SYNCBUSY.vec.GENCTRL & (1 << gclk)) != 0) {}
7980
#endif
8081
#ifdef SAMD21
82+
while (GCLK->STATUS.bit.SYNCBUSY == 1) {}
8183
GCLK->GENCTRL.reg = GCLK_GENCTRL_ID(gclk);
8284
while (GCLK->STATUS.bit.SYNCBUSY == 1) {}
8385
#endif

ports/atmel-samd/common-hal/microcontroller/__init__.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -85,13 +85,13 @@ const mcu_processor_obj_t common_hal_mcu_processor_obj = {
8585
// NVM is only available on Express boards for now.
8686
#if CIRCUITPY_INTERNAL_NVM_SIZE > 0
8787
// The singleton nvm.ByteArray object.
88-
// const nvm_bytearray_obj_t common_hal_mcu_nvm_obj = {
89-
// .base = {
90-
// .type = &nvm_bytearray_type,
91-
// },
92-
// .len = NVMCTRL_ROW_SIZE,
93-
// .start_address = (uint8_t*) (FLASH_SIZE - NVMCTRL_ROW_SIZE)
94-
// };
88+
const nvm_bytearray_obj_t common_hal_mcu_nvm_obj = {
89+
.base = {
90+
.type = &nvm_bytearray_type,
91+
},
92+
.len = CIRCUITPY_INTERNAL_NVM_SIZE,
93+
.start_address = (uint8_t*) (FLASH_SIZE - CIRCUITPY_INTERNAL_NVM_SIZE)
94+
};
9595
#endif
9696

9797
// This maps MCU pin names to pin objects.

ports/atmel-samd/common-hal/nvm/ByteArray.c

Lines changed: 7 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626

2727
#include "common-hal/nvm/ByteArray.h"
2828

29-
#include "asf/sam0/drivers/nvm/nvm.h"
29+
#include "hal_flash.h"
3030

3131
#include <stdint.h>
3232
#include <string.h>
@@ -36,48 +36,12 @@ uint32_t common_hal_nvm_bytearray_get_length(nvm_bytearray_obj_t *self) {
3636
}
3737

3838
bool common_hal_nvm_bytearray_set_bytes(nvm_bytearray_obj_t *self,
39-
uint32_t start_index, uint8_t* values, uint32_t len) {
40-
uint32_t total_written = 0;
41-
for (uint32_t i = 0; i < self->len / NVMCTRL_ROW_SIZE; i++) {
42-
uint32_t row_start = NVMCTRL_ROW_SIZE * i;
43-
if (row_start + NVMCTRL_ROW_SIZE < start_index || start_index + len < row_start) {
44-
continue;
45-
}
46-
uint8_t temp_row[NVMCTRL_ROW_SIZE];
47-
memcpy(temp_row,
48-
self->start_address + row_start,
49-
NVMCTRL_ROW_SIZE);
50-
enum status_code error_code;
51-
do {
52-
error_code = nvm_erase_row((uint32_t) self->start_address + row_start);
53-
} while (error_code == STATUS_BUSY);
54-
if (error_code != STATUS_OK) {
55-
return false;
56-
}
57-
uint32_t data_start = 0;
58-
if (start_index > row_start) {
59-
data_start = start_index - row_start;
60-
}
61-
uint32_t data_len = len;
62-
uint32_t data_remaining = data_len - total_written;
63-
uint32_t row_remaining = NVMCTRL_ROW_SIZE - data_start;
64-
if (data_remaining > row_remaining) {
65-
data_len = row_remaining;
66-
}
67-
memcpy(temp_row + data_start,
68-
values + total_written,
69-
data_len);
70-
for (int page = 0; page < NVMCTRL_ROW_SIZE / NVMCTRL_PAGE_SIZE; page++) {
71-
do {
72-
error_code = nvm_write_buffer((uint32_t) self->start_address + row_start + page * NVMCTRL_PAGE_SIZE,
73-
temp_row + page * NVMCTRL_PAGE_SIZE,
74-
NVMCTRL_PAGE_SIZE);
75-
} while (error_code == STATUS_BUSY);
76-
if (error_code != STATUS_OK) {
77-
return false;
78-
}
79-
}
80-
}
39+
uint32_t start_index, uint8_t* values, uint32_t len) {
40+
// We don't use features that use any advanced NVMCTRL features so we can fake the descriptor
41+
// whenever we need it instead of storing it long term.
42+
struct flash_descriptor desc;
43+
desc.dev.hw = NVMCTRL;
44+
flash_write(&desc, (uint32_t) self->start_address + start_index, values, len);
8145
return true;
8246
}
8347

0 commit comments

Comments
 (0)
0