Skip to content

Commit ce70b95

Browse files
authored
Merge pull request #4177 from microDev1/nvm-rp
RP2040: Support for NVM
2 parents 9f6ebf3 + b029031 commit ce70b95

File tree

6 files changed

+148
-6
lines changed

6 files changed

+148
-6
lines changed
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2020 microDev
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 <string.h>
30+
31+
#include "py/runtime.h"
32+
#include "src/rp2_common/hardware_flash/include/hardware/flash.h"
33+
34+
extern uint32_t __flash_binary_start;
35+
static const uint32_t flash_binary_start = (uint32_t) &__flash_binary_start;
36+
37+
#define RMV_OFFSET(addr) addr - flash_binary_start
38+
39+
uint32_t common_hal_nvm_bytearray_get_length(nvm_bytearray_obj_t* self) {
40+
return self->len;
41+
}
42+
43+
static void write_page(uint32_t page_addr, uint32_t offset, uint32_t len, uint8_t* bytes) {
44+
// Write a whole page to flash, buffering it first and then erasing and rewriting it
45+
// since we can only write a whole page at a time.
46+
if (offset == 0 && len == FLASH_PAGE_SIZE) {
47+
flash_range_program(RMV_OFFSET(page_addr), bytes, FLASH_PAGE_SIZE);
48+
} else {
49+
uint8_t buffer[FLASH_PAGE_SIZE];
50+
memcpy(buffer, (uint8_t*) page_addr, FLASH_PAGE_SIZE);
51+
memcpy(buffer + offset, bytes, len);
52+
flash_range_program(RMV_OFFSET(page_addr), buffer, FLASH_PAGE_SIZE);
53+
}
54+
}
55+
56+
static void erase_and_write_sector(uint32_t address, uint32_t len, uint8_t* bytes) {
57+
// Write a whole sector to flash, buffering it first and then erasing and rewriting it
58+
// since we can only erase a whole sector at a time.
59+
uint8_t buffer[FLASH_SECTOR_SIZE];
60+
memcpy(buffer, (uint8_t*) CIRCUITPY_INTERNAL_NVM_START_ADDR, FLASH_SECTOR_SIZE);
61+
memcpy(buffer + address, bytes, len);
62+
flash_range_erase(RMV_OFFSET(CIRCUITPY_INTERNAL_NVM_START_ADDR), FLASH_SECTOR_SIZE);
63+
flash_range_program(RMV_OFFSET(CIRCUITPY_INTERNAL_NVM_START_ADDR), buffer, FLASH_SECTOR_SIZE);
64+
}
65+
66+
void common_hal_nvm_bytearray_get_bytes(nvm_bytearray_obj_t* self,
67+
uint32_t start_index, uint32_t len, uint8_t* values) {
68+
memcpy(values, self->start_address + start_index, len);
69+
}
70+
71+
bool common_hal_nvm_bytearray_set_bytes(nvm_bytearray_obj_t* self,
72+
uint32_t start_index, uint8_t* values, uint32_t len) {
73+
uint8_t values_in[len];
74+
common_hal_nvm_bytearray_get_bytes(self, start_index, len, values_in);
75+
76+
bool all_ones = true;
77+
for (uint32_t i = 0; i < len; i++) {
78+
if (values_in[i] != UINT8_MAX) {
79+
all_ones = false;
80+
break;
81+
}
82+
}
83+
84+
if (all_ones) {
85+
uint32_t address = (uint32_t) self->start_address + start_index;
86+
uint32_t offset = address % FLASH_PAGE_SIZE;
87+
uint32_t page_addr = address - offset;
88+
89+
while (len) {
90+
uint32_t write_len = MIN(len, FLASH_PAGE_SIZE - offset);
91+
write_page(page_addr, offset, write_len, values);
92+
len -= write_len;
93+
values += write_len;
94+
page_addr += FLASH_PAGE_SIZE;
95+
offset = 0;
96+
}
97+
} else {
98+
erase_and_write_sector(start_index, len, values);
99+
}
100+
101+
return true;
102+
}
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) 2020 microDev
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_RASPBERRYPI_COMMON_HAL_NVM_BYTEARRAY_H
28+
#define MICROPY_INCLUDED_RASPBERRYPI_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_RASPBERRYPI_COMMON_HAL_NVM_BYTEARRAY_H
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
// No nvm module functions.

ports/raspberrypi/link.ld

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

2424
MEMORY
2525
{
26-
FLASH_FIRMWARE (rx) : ORIGIN = 0x10000000, LENGTH = 1024k
26+
FLASH_FIRMWARE (rx) : ORIGIN = 0x10000000, LENGTH = 1020k
2727
RAM (rwx) : ORIGIN = 0x20000000, LENGTH = 256k
2828
SCRATCH_X (rwx) : ORIGIN = 0x20040000, LENGTH = 4k
2929
SCRATCH_Y (rwx) : ORIGIN = 0x20041000, LENGTH = 4k

ports/raspberrypi/mpconfigport.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,16 @@
2929

3030
#include "src/rp2040/hardware_regs/include/hardware/platform_defs.h"
3131

32-
#define MICROPY_PY_SYS_PLATFORM "RP2040"
32+
#define MICROPY_PY_SYS_PLATFORM "RP2040"
3333

34-
#define CIRCUITPY_INTERNAL_NVM_SIZE 0
34+
#define CIRCUITPY_INTERNAL_NVM_SIZE (4*1024)
35+
#define CIRCUITPY_INTERNAL_NVM_START_ADDR (0x100FF000)
3536

36-
#define CIRCUITPY_DEFAULT_STACK_SIZE (24*1024)
37+
#define CIRCUITPY_DEFAULT_STACK_SIZE (24*1024)
3738

3839
#define MICROPY_USE_INTERNAL_PRINTF (1)
3940

40-
#define CIRCUITPY_PROCESSOR_COUNT (2)
41+
#define CIRCUITPY_PROCESSOR_COUNT (2)
4142

4243
// This also includes mpconfigboard.h.
4344
#include "py/circuitpy_mpconfig.h"

ports/raspberrypi/mpconfigport.mk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ CIRCUITPY_PWMIO = 1
3131
CIRCUITPY_COUNTIO = 0 # Use PWM interally
3232
CIRCUITPY_FREQUENCYIO = 0 # Use PWM interally
3333
CIRCUITPY_I2CPERIPHERAL = 0
34-
CIRCUITPY_NVM = 0
34+
CIRCUITPY_NVM = 1
3535
CIRCUITPY_PULSEIO = 0 # Use PIO interally
3636
CIRCUITPY_ROTARYIO = 0 # Use PIO interally
3737
CIRCUITPY_WATCHDOG = 1

0 commit comments

Comments
 (0)