|
| 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 | +} |
0 commit comments