|
| 1 | +// This file is part of the CircuitPython project: https://circuitpython.org |
| 2 | +// |
| 3 | +// SPDX-FileCopyrightText: Copyright (c) 2025 Scott Shawcroft for Adafruit Industries |
| 4 | +// |
| 5 | +// SPDX-License-Identifier: MIT |
| 6 | + |
| 7 | +#include "py/runtime.h" |
| 8 | +#include "common-hal/nvm/ByteArray.h" |
| 9 | +#include "shared-bindings/nvm/ByteArray.h" |
| 10 | + |
| 11 | +#include <string.h> |
| 12 | + |
| 13 | +#include <zephyr/drivers/flash.h> |
| 14 | +#include <zephyr/storage/flash_map.h> |
| 15 | + |
| 16 | +#define NVM_PARTITION nvm_partition |
| 17 | + |
| 18 | +#if FIXED_PARTITION_EXISTS(NVM_PARTITION) |
| 19 | + |
| 20 | +static const struct flash_area *nvm_area = NULL; |
| 21 | +static size_t nvm_erase_size = 0; |
| 22 | + |
| 23 | +static bool ensure_nvm_open(void) { |
| 24 | + if (nvm_area != NULL) { |
| 25 | + return true; |
| 26 | + } |
| 27 | + int rc = flash_area_open(FIXED_PARTITION_ID(NVM_PARTITION), &nvm_area); |
| 28 | + if (rc != 0) { |
| 29 | + return false; |
| 30 | + } |
| 31 | + |
| 32 | + const struct device *dev = flash_area_get_device(nvm_area); |
| 33 | + struct flash_pages_info info; |
| 34 | + flash_get_page_info_by_offs(dev, nvm_area->fa_off, &info); |
| 35 | + nvm_erase_size = info.size; |
| 36 | + |
| 37 | + return true; |
| 38 | +} |
| 39 | + |
| 40 | +uint32_t common_hal_nvm_bytearray_get_length(const nvm_bytearray_obj_t *self) { |
| 41 | + if (!ensure_nvm_open()) { |
| 42 | + return 0; |
| 43 | + } |
| 44 | + return nvm_area->fa_size; |
| 45 | +} |
| 46 | + |
| 47 | +bool common_hal_nvm_bytearray_set_bytes(const nvm_bytearray_obj_t *self, |
| 48 | + uint32_t start_index, uint8_t *values, uint32_t len) { |
| 49 | + if (!ensure_nvm_open()) { |
| 50 | + return false; |
| 51 | + } |
| 52 | + |
| 53 | + uint32_t address = start_index; |
| 54 | + while (len > 0) { |
| 55 | + uint32_t page_offset = address % nvm_erase_size; |
| 56 | + uint32_t page_start = address - page_offset; |
| 57 | + uint32_t write_len = MIN(len, nvm_erase_size - page_offset); |
| 58 | + |
| 59 | + uint8_t *buffer = m_malloc(nvm_erase_size); |
| 60 | + if (buffer == NULL) { |
| 61 | + return false; |
| 62 | + } |
| 63 | + |
| 64 | + // Read the full erase page. |
| 65 | + int rc = flash_area_read(nvm_area, page_start, buffer, nvm_erase_size); |
| 66 | + if (rc != 0) { |
| 67 | + m_free(buffer); |
| 68 | + return false; |
| 69 | + } |
| 70 | + |
| 71 | + // Modify the relevant bytes. |
| 72 | + memcpy(buffer + page_offset, values, write_len); |
| 73 | + |
| 74 | + // Erase the page. |
| 75 | + rc = flash_area_erase(nvm_area, page_start, nvm_erase_size); |
| 76 | + if (rc != 0) { |
| 77 | + m_free(buffer); |
| 78 | + return false; |
| 79 | + } |
| 80 | + |
| 81 | + // Write the page back. |
| 82 | + rc = flash_area_write(nvm_area, page_start, buffer, nvm_erase_size); |
| 83 | + m_free(buffer); |
| 84 | + if (rc != 0) { |
| 85 | + return false; |
| 86 | + } |
| 87 | + |
| 88 | + address += write_len; |
| 89 | + values += write_len; |
| 90 | + len -= write_len; |
| 91 | + } |
| 92 | + return true; |
| 93 | +} |
| 94 | + |
| 95 | +void common_hal_nvm_bytearray_get_bytes(const nvm_bytearray_obj_t *self, |
| 96 | + uint32_t start_index, uint32_t len, uint8_t *values) { |
| 97 | + if (!ensure_nvm_open()) { |
| 98 | + return; |
| 99 | + } |
| 100 | + flash_area_read(nvm_area, start_index, values, len); |
| 101 | +} |
| 102 | + |
| 103 | +#endif // FIXED_PARTITION_EXISTS(NVM_PARTITION) |
0 commit comments