|
| 1 | +/* |
| 2 | + * This file is part of the Silicon Labs Arduino Core |
| 3 | + * |
| 4 | + * The MIT License (MIT) |
| 5 | + * |
| 6 | + * Copyright 2024 Silicon Laboratories Inc. www.silabs.com |
| 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 "silabs_eeprom.h" |
| 28 | + |
| 29 | +#include "nvm3.h" |
| 30 | +#include "nvm3_hal_flash.h" |
| 31 | +#include "nvm3_default_config.h" |
| 32 | + |
| 33 | +// The NVM3 key space is divided into several domains, the usable user domain is 0x00000 - 0x0FFFF |
| 34 | +#define NVM3_USER_KEY_SPACE_END 0x0FFFFu |
| 35 | + |
| 36 | +#define NVM3_OBJECT_SIZE (NVM3_DEFAULT_MAX_OBJECT_SIZE) |
| 37 | +#define NVM3_MAX_SIZE 10240u |
| 38 | + |
| 39 | +uint8_t eeprom_read_byte(uint32_t addr) |
| 40 | +{ |
| 41 | + if (addr >= NVM3_MAX_SIZE) { |
| 42 | + return 0xFFu; |
| 43 | + } |
| 44 | + |
| 45 | + uint32_t object_idx = addr / NVM3_OBJECT_SIZE; |
| 46 | + uint32_t data_idx = addr % NVM3_OBJECT_SIZE; |
| 47 | + |
| 48 | + if (object_idx > NVM3_USER_KEY_SPACE_END) { |
| 49 | + return 0xFFu; |
| 50 | + } |
| 51 | + |
| 52 | + uint8_t object_cache[NVM3_OBJECT_SIZE]; |
| 53 | + Ecode_t status = nvm3_readData(nvm3_defaultHandle, object_idx, (void*)object_cache, NVM3_OBJECT_SIZE); |
| 54 | + |
| 55 | + if (status != ECODE_NVM3_OK) { |
| 56 | + return 0xFFu; |
| 57 | + } |
| 58 | + return object_cache[data_idx]; |
| 59 | +} |
| 60 | + |
| 61 | +void eeprom_write_byte(uint32_t addr, uint8_t value) |
| 62 | +{ |
| 63 | + if (addr >= NVM3_MAX_SIZE) { |
| 64 | + return; |
| 65 | + } |
| 66 | + |
| 67 | + uint32_t object_idx = addr / NVM3_OBJECT_SIZE; |
| 68 | + uint32_t data_idx = addr % NVM3_OBJECT_SIZE; |
| 69 | + |
| 70 | + if (object_idx > NVM3_USER_KEY_SPACE_END) { |
| 71 | + return; |
| 72 | + } |
| 73 | + |
| 74 | + // Read the object from NVM3 |
| 75 | + uint8_t object_cache[NVM3_OBJECT_SIZE]; |
| 76 | + Ecode_t status = nvm3_readData(nvm3_defaultHandle, object_idx, (void*)object_cache, NVM3_OBJECT_SIZE); |
| 77 | + if (status != ECODE_NVM3_OK && status != ECODE_NVM3_ERR_KEY_NOT_FOUND) { |
| 78 | + return; |
| 79 | + } |
| 80 | + |
| 81 | + // Initialize the object if it's currently non-existent |
| 82 | + if (status == ECODE_NVM3_ERR_KEY_NOT_FOUND) { |
| 83 | + memset(object_cache, 0xFFu, NVM3_OBJECT_SIZE); |
| 84 | + } |
| 85 | + |
| 86 | + // Update the requested value |
| 87 | + object_cache[data_idx] = value; |
| 88 | + |
| 89 | + // Write the object back to NVM3 |
| 90 | + nvm3_writeData(nvm3_defaultHandle, object_idx, object_cache, NVM3_OBJECT_SIZE); |
| 91 | + |
| 92 | + // Do repacking if needed |
| 93 | + if (nvm3_repackNeeded(nvm3_defaultHandle)) { |
| 94 | + nvm3_repack(nvm3_defaultHandle); |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +uint16_t eeprom_get_length() |
| 99 | +{ |
| 100 | + return NVM3_MAX_SIZE; |
| 101 | +} |
0 commit comments