Skip to content

Commit 205837c

Browse files
committed
nvm implementation for rp2040
1 parent 2061097 commit 205837c

File tree

5 files changed

+117
-5
lines changed

5 files changed

+117
-5
lines changed
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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+
uint32_t common_hal_nvm_bytearray_get_length(nvm_bytearray_obj_t* self) {
35+
return self->len;
36+
}
37+
38+
static void write_page(uint32_t page_addr, uint32_t offset, uint32_t len, uint8_t* bytes) {
39+
// Write a whole page to flash, buffering it first and then erasing and rewriting
40+
// it since we can only clear a whole page at a time.
41+
if (offset == 0 && len == FLASH_PAGE_SIZE) {
42+
flash_range_program(page_addr - 0x10000000, bytes, FLASH_PAGE_SIZE);
43+
} else {
44+
uint8_t buffer[FLASH_PAGE_SIZE];
45+
memcpy(buffer, (uint8_t*) page_addr, FLASH_PAGE_SIZE);
46+
memcpy(buffer + offset, bytes, len);
47+
flash_range_program(page_addr - 0x10000000, buffer, FLASH_PAGE_SIZE);
48+
}
49+
}
50+
51+
bool common_hal_nvm_bytearray_set_bytes(nvm_bytearray_obj_t* self,
52+
uint32_t start_index, uint8_t* values, uint32_t len) {
53+
uint32_t address = (uint32_t) self->start_address + start_index;
54+
uint32_t offset = address % FLASH_PAGE_SIZE;
55+
uint32_t page_addr = address - offset;
56+
57+
while (len) {
58+
uint32_t write_len = MIN(len, FLASH_PAGE_SIZE - offset);
59+
write_page(page_addr, offset, write_len, values);
60+
len -= write_len;
61+
values += write_len;
62+
page_addr += FLASH_PAGE_SIZE;
63+
offset = 0;
64+
}
65+
66+
return true;
67+
}
68+
69+
void common_hal_nvm_bytearray_get_bytes(nvm_bytearray_obj_t* self,
70+
uint32_t start_index, uint32_t len, uint8_t* values) {
71+
memcpy(values, self->start_address + start_index, len);
72+
}
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/mpconfigport.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,16 @@
2727
#ifndef __INCLUDED_MPCONFIGPORT_H
2828
#define __INCLUDED_MPCONFIGPORT_H
2929

30-
#define MICROPY_PY_SYS_PLATFORM "RP2040"
30+
#define MICROPY_PY_SYS_PLATFORM "RP2040"
3131

32-
#define CIRCUITPY_INTERNAL_NVM_SIZE 0
32+
#define CIRCUITPY_INTERNAL_NVM_SIZE (4*1024)
33+
#define CIRCUITPY_INTERNAL_NVM_START_ADDR (0x100FF000)
3334

34-
#define CIRCUITPY_DEFAULT_STACK_SIZE (24*1024)
35+
#define CIRCUITPY_DEFAULT_STACK_SIZE (24*1024)
3536

3637
#define MICROPY_USE_INTERNAL_PRINTF (1)
3738

38-
#define CIRCUITPY_PROCESSOR_COUNT (2)
39+
#define CIRCUITPY_PROCESSOR_COUNT (2)
3940

4041
// This also includes mpconfigboard.h.
4142
#include "py/circuitpy_mpconfig.h"

ports/raspberrypi/mpconfigport.mk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ CIRCUITPY_AUDIOMP3 = 0
3232
CIRCUITPY_COUNTIO = 0 # Use PWM interally
3333
CIRCUITPY_FREQUENCYIO = 0 # Use PWM interally
3434
CIRCUITPY_I2CPERIPHERAL = 0
35-
CIRCUITPY_NVM = 0
35+
CIRCUITPY_NVM = 1
3636
CIRCUITPY_PULSEIO = 0 # Use PIO interally
3737
CIRCUITPY_ROTARYIO = 0 # Use PIO interally
3838
CIRCUITPY_WATCHDOG = 1

0 commit comments

Comments
 (0)