nvm implementation for rp2040

This commit is contained in:
microDev 2021-02-11 12:48:36 +05:30
parent 206109763f
commit 205837c47b
No known key found for this signature in database
GPG Key ID: 2C0867BE60967730
5 changed files with 117 additions and 5 deletions

View File

@ -0,0 +1,72 @@
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2020 microDev
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include "common-hal/nvm/ByteArray.h"
#include <string.h>
#include "py/runtime.h"
#include "src/rp2_common/hardware_flash/include/hardware/flash.h"
uint32_t common_hal_nvm_bytearray_get_length(nvm_bytearray_obj_t* self) {
return self->len;
}
static void write_page(uint32_t page_addr, uint32_t offset, uint32_t len, uint8_t* bytes) {
// Write a whole page to flash, buffering it first and then erasing and rewriting
// it since we can only clear a whole page at a time.
if (offset == 0 && len == FLASH_PAGE_SIZE) {
flash_range_program(page_addr - 0x10000000, bytes, FLASH_PAGE_SIZE);
} else {
uint8_t buffer[FLASH_PAGE_SIZE];
memcpy(buffer, (uint8_t*) page_addr, FLASH_PAGE_SIZE);
memcpy(buffer + offset, bytes, len);
flash_range_program(page_addr - 0x10000000, buffer, FLASH_PAGE_SIZE);
}
}
bool common_hal_nvm_bytearray_set_bytes(nvm_bytearray_obj_t* self,
uint32_t start_index, uint8_t* values, uint32_t len) {
uint32_t address = (uint32_t) self->start_address + start_index;
uint32_t offset = address % FLASH_PAGE_SIZE;
uint32_t page_addr = address - offset;
while (len) {
uint32_t write_len = MIN(len, FLASH_PAGE_SIZE - offset);
write_page(page_addr, offset, write_len, values);
len -= write_len;
values += write_len;
page_addr += FLASH_PAGE_SIZE;
offset = 0;
}
return true;
}
void common_hal_nvm_bytearray_get_bytes(nvm_bytearray_obj_t* self,
uint32_t start_index, uint32_t len, uint8_t* values) {
memcpy(values, self->start_address + start_index, len);
}

View File

@ -0,0 +1,38 @@
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2020 microDev
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#ifndef MICROPY_INCLUDED_RASPBERRYPI_COMMON_HAL_NVM_BYTEARRAY_H
#define MICROPY_INCLUDED_RASPBERRYPI_COMMON_HAL_NVM_BYTEARRAY_H
#include "py/obj.h"
typedef struct {
mp_obj_base_t base;
uint8_t * start_address;
uint32_t len;
} nvm_bytearray_obj_t;
#endif // MICROPY_INCLUDED_RASPBERRYPI_COMMON_HAL_NVM_BYTEARRAY_H

View File

@ -0,0 +1 @@
// No nvm module functions.

View File

@ -27,15 +27,16 @@
#ifndef __INCLUDED_MPCONFIGPORT_H
#define __INCLUDED_MPCONFIGPORT_H
#define MICROPY_PY_SYS_PLATFORM "RP2040"
#define MICROPY_PY_SYS_PLATFORM "RP2040"
#define CIRCUITPY_INTERNAL_NVM_SIZE 0
#define CIRCUITPY_INTERNAL_NVM_SIZE (4*1024)
#define CIRCUITPY_INTERNAL_NVM_START_ADDR (0x100FF000)
#define CIRCUITPY_DEFAULT_STACK_SIZE (24*1024)
#define CIRCUITPY_DEFAULT_STACK_SIZE (24*1024)
#define MICROPY_USE_INTERNAL_PRINTF (1)
#define CIRCUITPY_PROCESSOR_COUNT (2)
#define CIRCUITPY_PROCESSOR_COUNT (2)
// This also includes mpconfigboard.h.
#include "py/circuitpy_mpconfig.h"

View File

@ -32,7 +32,7 @@ CIRCUITPY_AUDIOMP3 = 0
CIRCUITPY_COUNTIO = 0 # Use PWM interally
CIRCUITPY_FREQUENCYIO = 0 # Use PWM interally
CIRCUITPY_I2CPERIPHERAL = 0
CIRCUITPY_NVM = 0
CIRCUITPY_NVM = 1
CIRCUITPY_PULSEIO = 0 # Use PIO interally
CIRCUITPY_ROTARYIO = 0 # Use PIO interally
CIRCUITPY_WATCHDOG = 1