Skip to content

Commit 266be30

Browse files
tannewtdhalbert
authored andcommitted
atmel-samd: Introduce a nvm module for non-volatile byte-level memory access. (adafruit#203)
* atmel-samd: Introduce a nvm module for non-volatile byte-level memory access. This allows for persisting small configuration values even when the file system is read-only from CircuitPython. Fixes adafruit#160 * Review feedback: * Add tests. * Fix non-zero index. * Fix len()
1 parent dd1c4fc commit 266be30

32 files changed

+575
-25
lines changed

atmel-samd/Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,8 @@ SRC_COMMON_HAL = \
252252
microcontroller/__init__.c \
253253
microcontroller/Pin.c \
254254
neopixel_write/__init__.c \
255+
nvm/__init__.c \
256+
nvm/ByteArray.c \
255257
os/__init__.c \
256258
pulseio/__init__.c \
257259
pulseio/PulseIn.c \

atmel-samd/boards/arduino_zero/mpconfigboard.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,6 @@
1212

1313
#include "internal_flash.h"
1414

15+
#define CIRCUITPY_INTERNAL_NVM_SIZE 0
16+
1517
#define BOARD_FLASH_SIZE (0x00040000 - 0x2000 - 0x010000)

atmel-samd/boards/circuitplayground_express/mpconfigboard.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,11 @@
2525

2626
#include "spi_flash.h"
2727

28-
#define BOARD_FLASH_SIZE (0x00040000 - 0x2000)
28+
// If you change this, then make sure to update the linker scripts as well to
29+
// make sure you don't overwrite code.
30+
#define CIRCUITPY_INTERNAL_NVM_SIZE 256
31+
32+
#define BOARD_FLASH_SIZE (0x00040000 - 0x2000 - CIRCUITPY_INTERNAL_NVM_SIZE)
2933

3034
#include "flash_S25FL216K.h"
3135
#include "flash_GD25Q16C.h"

atmel-samd/boards/feather_m0_adalogger/mpconfigboard.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,6 @@
1111

1212
#include "internal_flash.h"
1313

14+
#define CIRCUITPY_INTERNAL_NVM_SIZE 0
15+
1416
#define BOARD_FLASH_SIZE (0x00040000 - 0x2000 - 0x010000)

atmel-samd/boards/feather_m0_basic/mpconfigboard.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,6 @@
1111

1212
#include "internal_flash.h"
1313

14+
#define CIRCUITPY_INTERNAL_NVM_SIZE 0
15+
1416
#define BOARD_FLASH_SIZE (0x00040000 - 0x2000 - 0x010000)

atmel-samd/boards/feather_m0_express/mpconfigboard.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,11 @@
2323

2424
#include "spi_flash.h"
2525

26-
#define BOARD_FLASH_SIZE (0x00040000 - 0x2000)
26+
// If you change this, then make sure to update the linker scripts as well to
27+
// make sure you don't overwrite code.
28+
#define CIRCUITPY_INTERNAL_NVM_SIZE 256
29+
30+
#define BOARD_FLASH_SIZE (0x00040000 - 0x2000 - CIRCUITPY_INTERNAL_NVM_SIZE)
2731

2832
#include "flash_S25FL216K.h"
2933
#include "flash_GD25Q16C.h"

atmel-samd/boards/gemma_m0/mpconfigboard.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
#define MICROPY_PORT_A (PORT_PA00 | PORT_PA01 | PORT_PA24 | PORT_PA25)
1212
#define MICROPY_PORT_B (0)
1313

14+
#define CIRCUITPY_INTERNAL_NVM_SIZE 0
15+
1416
#include "internal_flash.h"
1517

1618
#define BOARD_FLASH_SIZE (0x00040000 - 0x2000 - 0x010000)

atmel-samd/boards/metro_m0_express/mpconfigboard.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,11 @@
2525

2626
#include "spi_flash.h"
2727

28-
#define BOARD_FLASH_SIZE (0x00040000 - 0x2000)
28+
// If you change this, then make sure to update the linker scripts as well to
29+
// make sure you don't overwrite code.
30+
#define CIRCUITPY_INTERNAL_NVM_SIZE 256
31+
32+
#define BOARD_FLASH_SIZE (0x00040000 - 0x2000 - CIRCUITPY_INTERNAL_NVM_SIZE)
2933

3034
#include "flash_S25FL216K.h"
3135
#include "flash_GD25Q16C.h"

atmel-samd/boards/samd21x18-bootloader-crystalless.ld

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
/* Specify the memory areas */
66
MEMORY
77
{
8-
/* Leave 8KiB for the bootloader, 256b for persistent config (clock), and 64k for the flash file system. */
9-
FLASH (rx) : ORIGIN = 0x00000000+0x2000, LENGTH = 0x00040000 - 0x2000 - 0x100 - 0x010000
8+
/* Leave 8KiB for the bootloader, 256b for persistent config (clock), 64k for the flash file system and 256b for the user config. */
9+
FLASH (rx) : ORIGIN = 0x00000000+0x2000, LENGTH = 0x00040000 - 0x2000 - 0x100 - 0x010000 - 0x100
1010
RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 0x008000 /* 32 KiB */
1111
}
1212

atmel-samd/boards/samd21x18-bootloader-external-flash-crystalless.ld

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
/* Specify the memory areas */
66
MEMORY
77
{
8-
FLASH (rx) : ORIGIN = 0x00000000 + 0x2000, LENGTH = 0x00040000 - 0x2000 - 0x100 /* Leave 8KiB for the bootloader and 256b for config. */
8+
/* Leave 8KiB for the bootloader, 256b for internal config and 256b for user config. */
9+
FLASH (rx) : ORIGIN = 0x00000000 + 0x2000, LENGTH = 0x00040000 - 0x2000 - 0x100 - 0x100
910
RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 0x008000 /* 32 KiB */
1011
}
1112

atmel-samd/boards/samd21x18-bootloader-external-flash.ld

Lines changed: 1 addition & 1 deletion
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 + 0x2000, LENGTH = 0x00040000 - 0x2000 /* Leave 8KiB for the bootloader. */
8+
FLASH (rx) : ORIGIN = 0x00000000 + 0x2000, LENGTH = 0x00040000 - 0x2000 - 0x100 /* Leave 8KiB for the bootloader and 256b for user config. */
99
RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 0x008000 /* 32 KiB */
1010
}
1111

atmel-samd/boards/samd21x18-bootloader.ld

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
/* Specify the memory areas */
66
MEMORY
77
{
8-
/* Leave 8KiB for the bootloader and 64k for the flash file system. */
8+
/* Leave 8KiB for the bootloader, and 64k for the flash file system. */
99
FLASH (rx) : ORIGIN = 0x00000000+0x2000, LENGTH = 0x00040000 - 0x2000 - 0x010000
1010
RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 0x008000 /* 32 KiB */
1111
}

atmel-samd/boards/samd21x18-external-flash.ld

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
/* Specify the memory areas */
66
MEMORY
77
{
8-
FLASH (rx) : ORIGIN = 0x00000000, LENGTH = 0x00040000 - 0x100 /* 256 KiB but leave 256b for config */
8+
/* 256 KiB but leave 256b for internal config and 256b for user config (protected eeprom) */
9+
FLASH (rx) : ORIGIN = 0x00000000, LENGTH = 0x00040000 - 0x100 - 0x100
910
RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 0x008000 /* 32 KiB */
1011
}
1112

atmel-samd/boards/samd21x18.ld

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
/* Specify the memory areas */
66
MEMORY
77
{
8-
FLASH (rx) : ORIGIN = 0x00000000, LENGTH = 0x00040000 - 0x100 - 0x010000 /* Leave 256b for config and 64k for the flash file system. */
8+
/* Leave 256b for internal config, 64k for the flash file system and 256b for user config. */
9+
FLASH (rx) : ORIGIN = 0x00000000, LENGTH = 0x00040000 - 0x100 - 0x010000
910
RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 0x008000 /* 32 KiB */
1011
}
1112

atmel-samd/boards/trinket_m0/mpconfigboard.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,6 @@
1212

1313
#include "internal_flash.h"
1414

15+
#define CIRCUITPY_INTERNAL_NVM_SIZE 0
16+
1517
#define BOARD_FLASH_SIZE (0x00040000 - 0x2000 - 0x010000)

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,12 @@
2525
*/
2626

2727
#include "py/mphal.h"
28+
#include "py/obj.h"
2829

2930
#include "samd21_pins.h"
3031

32+
#include "shared-bindings/nvm/ByteArray.h"
33+
3134
void common_hal_mcu_delay_us(uint32_t delay) {
3235
mp_hal_delay_us(delay);
3336
}
@@ -47,6 +50,17 @@ void common_hal_mcu_enable_interrupts(void) {
4750
cpu_irq_restore(irq_flags);
4851
}
4952

53+
// NVM is only available on Express boards for now.
54+
#if CIRCUITPY_INTERNAL_NVM_SIZE > 0
55+
nvm_bytearray_obj_t common_hal_mcu_nvm_obj = {
56+
.base = {
57+
.type = &nvm_bytearray_type,
58+
},
59+
.len = NVMCTRL_ROW_SIZE,
60+
.start_address = (uint8_t*) (FLASH_SIZE - NVMCTRL_ROW_SIZE)
61+
};
62+
#endif
63+
5064
// This maps MCU pin names to pin objects.
5165
STATIC const mp_map_elem_t mcu_pin_global_dict_table[] = {
5266
// Pins in datasheet order.

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

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
* This file is part of the Micro Python project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2017 Scott Shawcroft for Adafruit Industries
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#include "common-hal/nvm/ByteArray.h"
28+
29+
#include "asf/sam0/drivers/nvm/nvm.h"
30+
31+
#include <stdint.h>
32+
#include <string.h>
33+
34+
uint32_t common_hal_nvm_bytearray_get_length(nvm_bytearray_obj_t *self) {
35+
return self->len;
36+
}
37+
38+
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+
}
81+
return true;
82+
}
83+
84+
// NVM memory is memory mapped so reading it is easy.
85+
void common_hal_nvm_bytearray_get_bytes(nvm_bytearray_obj_t *self,
86+
uint32_t start_index, uint32_t len, uint8_t* values) {
87+
memcpy(values, self->start_address + start_index, len);
88+
}

atmel-samd/common-hal/nvm/ByteArray.h

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2017 Scott Shawcroft for Adafruit Industries
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#ifndef __MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_NVM_BYTEARRAY_H__
28+
#define __MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_NVM_BYTEARRAY_H__
29+
30+
#include "py/obj.h"
31+
32+
typedef struct {
33+
mp_obj_base_t base;
34+
uint8_t* start_address;
35+
uint32_t len;
36+
} nvm_bytearray_obj_t;
37+
38+
#endif // __MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_NVM_BYTEARRAY_H__

atmel-samd/common-hal/nvm/__init__.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
// No nvm module functions.

atmel-samd/internal_flash.c

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,6 @@
4040

4141
#include "rgb_led_status.h"
4242

43-
#define TOTAL_INTERNAL_FLASH_SIZE 0x010000
44-
45-
#define INTERNAL_FLASH_MEM_SEG1_START_ADDR (0x00040000 - TOTAL_INTERNAL_FLASH_SIZE)
46-
#define INTERNAL_FLASH_PART1_START_BLOCK (0x1)
47-
#define INTERNAL_FLASH_PART1_NUM_BLOCKS (TOTAL_INTERNAL_FLASH_SIZE / FILESYSTEM_BLOCK_SIZE)
48-
4943
void internal_flash_init(void) {
5044
// Activity LED for flash writes.
5145
#ifdef MICROPY_HW_LED_MSC
@@ -120,7 +114,6 @@ static int32_t convert_block_to_flash_addr(uint32_t block) {
120114
}
121115

122116
bool internal_flash_read_block(uint8_t *dest, uint32_t block) {
123-
//printf("RD %u\n", block);
124117
if (block == 0) {
125118
// fake the MBR so we can decide on our own partition table
126119

atmel-samd/internal_flash.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@
3232

3333
#define FLASH_ROOT_POINTERS
3434

35+
#define TOTAL_INTERNAL_FLASH_SIZE 0x010000
36+
37+
#define INTERNAL_FLASH_MEM_SEG1_START_ADDR (0x00040000 - TOTAL_INTERNAL_FLASH_SIZE - CIRCUITPY_INTERNAL_NVM_SIZE)
38+
#define INTERNAL_FLASH_PART1_START_BLOCK (0x1)
39+
#define INTERNAL_FLASH_PART1_NUM_BLOCKS (TOTAL_INTERNAL_FLASH_SIZE / FILESYSTEM_BLOCK_SIZE)
40+
3541
#define INTERNAL_FLASH_SYSTICK_MASK (0x1ff) // 512ms
3642
#define INTERNAL_FLASH_IDLE_TICK(tick) (((tick) & INTERNAL_FLASH_SYSTICK_MASK) == 2)
3743

0 commit comments

Comments
 (0)