2021-02-11 02:18:36 -05:00
|
|
|
/*
|
|
|
|
* 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"
|
2021-11-10 09:42:21 -05:00
|
|
|
#include "shared-bindings/nvm/ByteArray.h"
|
2021-02-11 02:18:36 -05:00
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "py/runtime.h"
|
|
|
|
#include "src/rp2_common/hardware_flash/include/hardware/flash.h"
|
2021-12-04 12:34:49 -05:00
|
|
|
#include "shared-bindings/microcontroller/__init__.h"
|
2021-02-11 02:18:36 -05:00
|
|
|
|
2021-02-12 07:58:48 -05:00
|
|
|
extern uint32_t __flash_binary_start;
|
2021-03-15 09:57:36 -04:00
|
|
|
static const uint32_t flash_binary_start = (uint32_t)&__flash_binary_start;
|
2021-02-18 08:12:00 -05:00
|
|
|
|
|
|
|
#define RMV_OFFSET(addr) addr - flash_binary_start
|
2021-02-12 07:58:48 -05:00
|
|
|
|
2021-11-10 09:42:21 -05:00
|
|
|
uint32_t common_hal_nvm_bytearray_get_length(const nvm_bytearray_obj_t *self) {
|
2021-02-11 02:18:36 -05:00
|
|
|
return self->len;
|
|
|
|
}
|
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
static void write_page(uint32_t page_addr, uint32_t offset, uint32_t len, uint8_t *bytes) {
|
2021-02-18 08:12:00 -05:00
|
|
|
// Write a whole page to flash, buffering it first and then erasing and rewriting it
|
|
|
|
// since we can only write a whole page at a time.
|
2021-02-11 02:18:36 -05:00
|
|
|
if (offset == 0 && len == FLASH_PAGE_SIZE) {
|
2021-12-04 15:14:23 -05:00
|
|
|
// disable interrupts to prevent core hang on rp2040
|
|
|
|
common_hal_mcu_disable_interrupts();
|
2021-02-18 08:12:00 -05:00
|
|
|
flash_range_program(RMV_OFFSET(page_addr), bytes, FLASH_PAGE_SIZE);
|
2021-12-04 15:14:23 -05:00
|
|
|
common_hal_mcu_enable_interrupts();
|
2021-02-11 02:18:36 -05:00
|
|
|
} else {
|
|
|
|
uint8_t buffer[FLASH_PAGE_SIZE];
|
2021-03-15 09:57:36 -04:00
|
|
|
memcpy(buffer, (uint8_t *)page_addr, FLASH_PAGE_SIZE);
|
2021-02-11 02:18:36 -05:00
|
|
|
memcpy(buffer + offset, bytes, len);
|
2021-12-04 15:14:23 -05:00
|
|
|
common_hal_mcu_disable_interrupts();
|
2021-02-18 08:12:00 -05:00
|
|
|
flash_range_program(RMV_OFFSET(page_addr), buffer, FLASH_PAGE_SIZE);
|
2021-12-04 15:14:23 -05:00
|
|
|
common_hal_mcu_enable_interrupts();
|
2021-02-11 02:18:36 -05:00
|
|
|
}
|
2021-12-04 15:14:23 -05:00
|
|
|
|
2021-02-11 02:18:36 -05:00
|
|
|
}
|
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
static void erase_and_write_sector(uint32_t address, uint32_t len, uint8_t *bytes) {
|
2021-02-18 08:12:00 -05:00
|
|
|
// Write a whole sector to flash, buffering it first and then erasing and rewriting it
|
|
|
|
// since we can only erase a whole sector at a time.
|
|
|
|
uint8_t buffer[FLASH_SECTOR_SIZE];
|
2021-12-22 15:00:19 -05:00
|
|
|
// TODO: Update this to a better workaround for GCC 11 when one is provided.
|
|
|
|
// See: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=99578#c20
|
|
|
|
#pragma GCC diagnostic push
|
|
|
|
#pragma GCC diagnostic ignored "-Warray-bounds"
|
|
|
|
#pragma GCC diagnostic ignored "-Wstringop-overread"
|
2021-03-15 09:57:36 -04:00
|
|
|
memcpy(buffer, (uint8_t *)CIRCUITPY_INTERNAL_NVM_START_ADDR, FLASH_SECTOR_SIZE);
|
2021-12-22 15:00:19 -05:00
|
|
|
#pragma GCC diagnostic pop
|
2021-02-18 08:12:00 -05:00
|
|
|
memcpy(buffer + address, bytes, len);
|
2021-12-04 15:14:23 -05:00
|
|
|
// disable interrupts to prevent core hang on rp2040
|
|
|
|
common_hal_mcu_disable_interrupts();
|
2021-02-18 08:12:00 -05:00
|
|
|
flash_range_erase(RMV_OFFSET(CIRCUITPY_INTERNAL_NVM_START_ADDR), FLASH_SECTOR_SIZE);
|
|
|
|
flash_range_program(RMV_OFFSET(CIRCUITPY_INTERNAL_NVM_START_ADDR), buffer, FLASH_SECTOR_SIZE);
|
2021-12-04 13:21:51 -05:00
|
|
|
common_hal_mcu_enable_interrupts();
|
2021-02-18 08:12:00 -05:00
|
|
|
}
|
|
|
|
|
2021-11-10 09:42:21 -05:00
|
|
|
void common_hal_nvm_bytearray_get_bytes(const nvm_bytearray_obj_t *self,
|
2021-03-15 09:57:36 -04:00
|
|
|
uint32_t start_index, uint32_t len, uint8_t *values) {
|
2021-02-18 08:12:00 -05:00
|
|
|
memcpy(values, self->start_address + start_index, len);
|
|
|
|
}
|
|
|
|
|
2021-11-10 09:42:21 -05:00
|
|
|
bool common_hal_nvm_bytearray_set_bytes(const nvm_bytearray_obj_t *self,
|
2021-03-15 09:57:36 -04:00
|
|
|
uint32_t start_index, uint8_t *values, uint32_t len) {
|
2021-02-18 08:12:00 -05:00
|
|
|
uint8_t values_in[len];
|
|
|
|
common_hal_nvm_bytearray_get_bytes(self, start_index, len, values_in);
|
|
|
|
|
2021-03-02 04:06:28 -05:00
|
|
|
bool all_ones = true;
|
2021-02-18 08:12:00 -05:00
|
|
|
for (uint32_t i = 0; i < len; i++) {
|
|
|
|
if (values_in[i] != UINT8_MAX) {
|
2021-03-02 04:06:28 -05:00
|
|
|
all_ones = false;
|
|
|
|
break;
|
2021-02-18 08:12:00 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-02 04:06:28 -05:00
|
|
|
if (all_ones) {
|
2021-03-15 09:57:36 -04:00
|
|
|
uint32_t address = (uint32_t)self->start_address + start_index;
|
2021-03-02 04:06:28 -05:00
|
|
|
uint32_t offset = address % FLASH_PAGE_SIZE;
|
|
|
|
uint32_t page_addr = address - offset;
|
2021-02-11 02:18:36 -05:00
|
|
|
|
2021-03-02 04:06:28 -05:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
erase_and_write_sector(start_index, len, values);
|
2021-02-11 02:18:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|