|
| 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 "py/runtime.h" |
| 30 | + |
| 31 | +#include "nvs_flash.h" |
| 32 | + |
| 33 | +uint32_t common_hal_nvm_bytearray_get_length(nvm_bytearray_obj_t *self) { |
| 34 | + return self->len; |
| 35 | +} |
| 36 | + |
| 37 | +static nvs_handle get_nvs_handle(void) { |
| 38 | + // Initialize NVS |
| 39 | + esp_err_t err = nvs_flash_init(); |
| 40 | + if (err == ESP_ERR_NVS_NO_FREE_PAGES || err == ESP_ERR_NVS_NEW_VERSION_FOUND) { |
| 41 | + // NVS partition was truncated and needs to be erased |
| 42 | + // Retry nvs_flash_init |
| 43 | + ESP_ERROR_CHECK(nvs_flash_erase()); |
| 44 | + err = nvs_flash_init(); |
| 45 | + } |
| 46 | + ESP_ERROR_CHECK(err); |
| 47 | + |
| 48 | + // Open NVS handle |
| 49 | + nvs_handle nvs_handle; |
| 50 | + if (nvs_open("CPY", NVS_READWRITE, &nvs_handle) != ESP_OK) { |
| 51 | + mp_raise_RuntimeError(translate("NVS Error")); |
| 52 | + } |
| 53 | + return nvs_handle; |
| 54 | +} |
| 55 | + |
| 56 | +bool common_hal_nvm_bytearray_set_bytes(nvm_bytearray_obj_t *self, |
| 57 | + uint32_t start_index, uint8_t* values, uint32_t len) { |
| 58 | + char index[9]; |
| 59 | + sprintf(index, "%i", start_index); |
| 60 | + // start nvs |
| 61 | + nvs_handle handle = get_nvs_handle(); |
| 62 | + bool status = ((nvs_set_u8(handle, (const char *)index, *values) == ESP_OK) && (nvs_commit(handle) == ESP_OK)); |
| 63 | + // close nvs |
| 64 | + nvs_close(handle); |
| 65 | + return status; |
| 66 | +} |
| 67 | + |
| 68 | +// NVM memory is memory mapped so reading it is easy. |
| 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 | + char index[9]; |
| 72 | + sprintf(index, "%i", start_index); |
| 73 | + // start nvs |
| 74 | + nvs_handle handle = get_nvs_handle(); |
| 75 | + if (nvs_get_u8(handle, (const char *)index, values) != ESP_OK) { |
| 76 | + mp_raise_RuntimeError(translate("NVS Error")); |
| 77 | + } |
| 78 | + // close nvs |
| 79 | + nvs_close(handle); |
| 80 | +} |
0 commit comments