|
| 1 | +/* |
| 2 | + * This file is part of the MicroPython project, http://micropython.org/ |
| 3 | + * |
| 4 | + * The MIT License (MIT) |
| 5 | + * |
| 6 | + * Copyright (c) 2024 Daniel Campora on behalf of REMOTE TECH LTD |
| 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 <stdbool.h> |
| 28 | +#include <stdlib.h> |
| 29 | +#include <string.h> |
| 30 | +#include <stdint.h> |
| 31 | + |
| 32 | +#include <zephyr/kernel.h> |
| 33 | +#include <zephyr/sys/reboot.h> |
| 34 | +#include <zephyr/device.h> |
| 35 | +#include <zephyr/drivers/flash.h> |
| 36 | +#include <zephyr/storage/flash_map.h> |
| 37 | +#include <zephyr/fs/nvs.h> |
| 38 | + |
| 39 | +#include "nvmc.h" |
| 40 | + |
| 41 | + |
| 42 | +// declare private constants |
| 43 | + |
| 44 | +#define NVS_PARTITION storage_partition |
| 45 | +#define NVS_PARTITION_DEVICE FIXED_PARTITION_DEVICE(NVS_PARTITION) |
| 46 | +#define NVS_PARTITION_OFFSET FIXED_PARTITION_OFFSET(NVS_PARTITION) |
| 47 | + |
| 48 | + |
| 49 | +// declare private functions |
| 50 | + |
| 51 | + |
| 52 | +// declare private data |
| 53 | + |
| 54 | +static struct nvs_fs nvmc_fs; |
| 55 | + |
| 56 | + |
| 57 | +// define public functions |
| 58 | + |
| 59 | +void nvmc_init (void) { |
| 60 | + |
| 61 | + int rc = 0; |
| 62 | + struct flash_pages_info info; |
| 63 | + |
| 64 | + /* define the nvs file system by settings with: |
| 65 | + * sector_size equal to the pagesize, |
| 66 | + * 3 sectors |
| 67 | + * starting at NVS_PARTITION_OFFSET |
| 68 | + */ |
| 69 | + nvmc_fs.flash_device = NVS_PARTITION_DEVICE; |
| 70 | + if (!device_is_ready(nvmc_fs.flash_device)) { |
| 71 | + printk("Flash device %s is not ready\n", nvmc_fs.flash_device->name); |
| 72 | + return; |
| 73 | + } |
| 74 | + nvmc_fs.offset = NVS_PARTITION_OFFSET; |
| 75 | + rc = flash_get_page_info_by_offs(nvmc_fs.flash_device, nvmc_fs.offset, &info); |
| 76 | + if (rc) { |
| 77 | + printk("Unable to get page info, rc=%d\n", rc); |
| 78 | + return; |
| 79 | + } |
| 80 | + nvmc_fs.sector_size = info.size; |
| 81 | + nvmc_fs.sector_count = 3U; |
| 82 | + |
| 83 | + rc = nvs_mount(&nvmc_fs); |
| 84 | + if (rc) { |
| 85 | + printk("Flash Init failed, rc=%d\n", rc); |
| 86 | + return; |
| 87 | + } |
| 88 | + |
| 89 | + uint32_t signature = 0; |
| 90 | + if (!nvmc_get (E_NVM_SIGNATURE, &signature) || signature != NVMC_SIGNAUTRE_VALUE) { |
| 91 | + if (!nvmc_set (E_NVM_SIGNATURE, NVMC_SIGNAUTRE_VALUE)) { |
| 92 | + printk("NVM signature set failed!\n"); |
| 93 | + } |
| 94 | + |
| 95 | + if (!nvmc_set (E_NVM_CHARGE_COMPLETED, false)) { |
| 96 | + printk("NVM charge completed clear failed!\n"); |
| 97 | + } |
| 98 | + |
| 99 | + if (!nvmc_set (E_NVM_BATTERY_EMPTY, false)) { |
| 100 | + printk("NVM charge depleted clear failed!\n"); |
| 101 | + } |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +bool nvmc_set (teNvmElement element, uint32_t value) { |
| 106 | + uint16_t id = element + 1; |
| 107 | + |
| 108 | + if (nvs_write(&nvmc_fs, id, &value, sizeof(value)) == sizeof(value)) { |
| 109 | + return true; |
| 110 | + } |
| 111 | + return false; |
| 112 | +} |
| 113 | + |
| 114 | +bool nvmc_get (teNvmElement element, uint32_t *value) { |
| 115 | + uint16_t id = element + 1; |
| 116 | + |
| 117 | + if (nvs_read(&nvmc_fs, id, value, sizeof(*value)) > 0) { |
| 118 | + return true; |
| 119 | + } |
| 120 | + return false; |
| 121 | +} |
0 commit comments