diff --git a/cores/nRF5/nrf52_flash.c b/cores/nRF5/nrf52_flash.c index 0989bd8f..d303a5c2 100644 --- a/cores/nRF5/nrf52_flash.c +++ b/cores/nRF5/nrf52_flash.c @@ -34,6 +34,7 @@ */ /**************************************************************************/ +#include #include "Arduino.h" #include "nrf52_flash.h" @@ -69,6 +70,24 @@ static void print_write_after(uint32_t address, uint32_t count) #endif +static int nrf52_flash_sector_info(void *dev, int idx, uint32_t *address, uint32_t *sz); + +static const struct hal_flash_funcs nrf52k_flash_funcs = { + .hff_read = nrf52k_flash_read, + .hff_write = nrf52_flash_write, + .hff_erase_sector = nrf52_flash_erase_sector, + .hff_sector_info = nrf52_flash_sector_info, + .hff_init = nrf52_flash_init +}; + +const struct hal_flash nrf52k_flash_dev = { + .hf_itf = &nrf52k_flash_funcs, + .hf_base_addr = 0x00000000, + .hf_size = 512 * 1024, /* XXX read from factory info? */ + .hf_sector_cnt = 128, /* XXX read from factory info? */ + .hf_align = 1 +}; + static SemaphoreHandle_t _evt_sem = NULL; static volatile uint32_t _op_result; @@ -78,14 +97,16 @@ void hal_flash_event_cb(uint32_t event) xSemaphoreGive(_evt_sem); } -int nrf52_flash_init(void) +int nrf52_flash_init(const struct hal_flash *dev) { + (void) dev; _evt_sem = xSemaphoreCreateCounting(10, 0); return (_evt_sem != NULL) ? 0 : 1; } -int nrf52_flash_erase_sector(uint32_t sector_address) +int nrf52_flash_erase_sector(const struct hal_flash *dev, uint32_t sector_address) { + (void) dev; uint32_t err; #if CFG_DEBUG_NFFS @@ -124,8 +145,10 @@ static int write_and_wait(uint32_t addr, uint32_t const * const data, uint32_t s return (_op_result == NRF_EVT_FLASH_OPERATION_SUCCESS ) ? 0 : (-1); } -int nrf52_flash_write(uint32_t address, const void *src, uint32_t num_bytes) +int nrf52_flash_write(const struct hal_flash *dev, uint32_t address, const void *src, uint32_t num_bytes) { + (void) dev; + #if CFG_DEBUG_NFFS const uint32_t _num = num_bytes; const uint32_t _addr = address; @@ -235,3 +258,32 @@ int nrf52_flash_write(uint32_t address, const void *src, uint32_t num_bytes) return 0; } +int nrf52k_flash_read(const struct hal_flash *dev, uint32_t address, void *dst, uint32_t num_bytes) +{ + (void) dev; + memcpy(dst, (void *)address, num_bytes); + return 0; +} + +static int nrf52_flash_sector_info(void *dev, int idx, uint32_t *address, uint32_t *sz) +{ + (void) dev; + assert(idx < nrf52k_flash_dev.hf_sector_cnt); + *address = idx * NRF52K_FLASH_SECTOR_SZ; + *sz = NRF52K_FLASH_SECTOR_SZ; + return 0; +} + + +const struct hal_flash * +hal_bsp_flash_dev(uint8_t id) +{ + /* + * Internal flash mapped to id 0. + */ + if (id != 0) { + return NULL; + } + return &nrf52k_flash_dev; +} + diff --git a/cores/nRF5/nrf52_flash.h b/cores/nRF5/nrf52_flash.h index c5d305bc..cb4f56bc 100644 --- a/cores/nRF5/nrf52_flash.h +++ b/cores/nRF5/nrf52_flash.h @@ -37,6 +37,7 @@ #define NRF52_FLASH_H_ #include +#include #ifdef __cplusplus extern "C" { @@ -44,9 +45,13 @@ #define NRF52K_FLASH_SECTOR_SZ 4096 -int nrf52_flash_init(void); -int nrf52_flash_erase_sector(uint32_t sector_address); -int nrf52_flash_write(uint32_t address, const void *src, uint32_t num_bytes); +// const struct hal_flash* dev is required for nffs flash interface but not used +// by nrf52_flash, just simply pass NULL when invoked + +int nrf52_flash_init(const struct hal_flash *dev); +int nrf52_flash_erase_sector(const struct hal_flash *dev, uint32_t sector_address); +int nrf52_flash_write(const struct hal_flash *dev, uint32_t address, const void *src, uint32_t num_bytes); +int nrf52k_flash_read(const struct hal_flash *dev, uint32_t address, void *dst, uint32_t num_bytes); #ifdef __cplusplus } diff --git a/libraries/nffs/library.properties b/libraries/nffs/library.properties index 1346b5ed..c168d1eb 100644 --- a/libraries/nffs/library.properties +++ b/libraries/nffs/library.properties @@ -1,10 +1,10 @@ name=Newtron Flash Filesystem -version=0.1.0 +version=1.0.0 author=Apache Software Foundation maintainer=Adafruit sentence=Newtron Flash Filesystem paragraph=Newtron Flash Filesystem category=Communication -url=https://github.com/adafruit/Adafruit_nRF52_Arduino +url=http://mynewt.apache.org/latest/os/modules/fs/nffs/nffs architectures=* includes=NewtNffs.h diff --git a/libraries/nffs/src/Nffs.cpp b/libraries/nffs/src/Nffs.cpp index e5e54677..f01f568a 100644 --- a/libraries/nffs/src/Nffs.cpp +++ b/libraries/nffs/src/Nffs.cpp @@ -37,8 +37,7 @@ #include "syscfg/syscfg.h" #include "hal/hal_flash.h" - -#define NFFS_AREA_MAX 8 +#include "flash_map/flash_map.h" ApacheNffs Nffs; @@ -54,20 +53,21 @@ bool ApacheNffs::begin(void) if (_initialized) return true; _initialized = true; - // Init flash - nrf52_flash_init(); + // Init flash map (required before accessing flash API) + flash_map_init(); /* Initialize nffs's internal state. */ errnum = nffs_init(); VERIFY_STATUS( errnum, false ); /* Convert the set of flash blocks we intend to use for nffs into an array - * of nffs area descriptors. + * of nffs area descriptors. Number of descriptor must be larger than + * number of sectors for nffs a.k.a 7 */ - struct nffs_area_desc descs[NFFS_AREA_MAX + 1]; - int cnt = NFFS_AREA_MAX; + struct nffs_area_desc descs[NFFS_SECTOR_NUM + 1]; + int cnt = NFFS_SECTOR_NUM; - errnum = nffs_misc_desc_from_flash_area( MYNEWT_VAL(NFFS_FLASH_AREA), &cnt, descs); + errnum = nffs_misc_desc_from_flash_area( FLASH_AREA_NFFS, &cnt, descs); VERIFY_STATUS( errnum, false ); /* Attempt to restore an existing nffs file system from flash. */ @@ -78,18 +78,19 @@ bool ApacheNffs::begin(void) { PRINT_MESS("No FS detected, format"); errnum = nffs_format(descs); - VERIFY_STATUS( errnum, false ); } + VERIFY_STATUS( errnum, false ); + return true; } bool ApacheNffs::format(void) { - struct nffs_area_desc descs[NFFS_AREA_MAX + 1]; - int cnt = NFFS_AREA_MAX; + struct nffs_area_desc descs[NFFS_SECTOR_NUM + 1]; + int cnt = NFFS_SECTOR_NUM; - errnum = nffs_misc_desc_from_flash_area( MYNEWT_VAL(NFFS_FLASH_AREA), &cnt, descs); + errnum = nffs_misc_desc_from_flash_area( FLASH_AREA_NFFS, &cnt, descs); VERIFY_STATUS( errnum, false ); errnum = nffs_format(descs); diff --git a/libraries/nffs/src/fs/nffs/src/nffs.c b/libraries/nffs/src/fs/nffs/src/nffs.c index 2c74c1f8..9eeefd2f 100644 --- a/libraries/nffs/src/fs/nffs/src/nffs.c +++ b/libraries/nffs/src/fs/nffs/src/nffs.c @@ -26,7 +26,7 @@ //#include "sysflash/sysflash.h" //#include "bsp/bsp.h" #include "hal/hal_flash.h" -//#include "flash_map/flash_map.h" +#include "flash_map/flash_map.h" #include "os/os_mempool.h" #include "os/os_mutex.h" #include "os/os_malloc.h" diff --git a/libraries/nffs/src/fs/nffs/src/nffs_misc.c b/libraries/nffs/src/fs/nffs/src/nffs_misc.c index 95341fc2..8f6a167b 100644 --- a/libraries/nffs/src/fs/nffs/src/nffs_misc.c +++ b/libraries/nffs/src/fs/nffs/src/nffs_misc.c @@ -25,22 +25,6 @@ #include "nffs/nffs.h" #include "nffs_priv.h" -static const struct flash_area sysflash_map_dfl = -{ - .fa_id = 0, - .fa_device_id = 0, - .fa_off = 0x6D000, - .fa_size = 7*4096, /* 28 kB */ -}; - -static int -flash_area_open(uint8_t id, const struct flash_area **fap) -{ - (void) id; - *fap = &sysflash_map_dfl; - return 0; -} - /** * Determines if the file system contains a valid root directory. For the root * directory to be valid, it must be present and have the following traits: @@ -489,7 +473,7 @@ nffs_misc_desc_from_flash_area(int id, int *cnt, struct nffs_area_desc *nad) hf = hal_bsp_flash_dev(fa->fa_device_id); for (i = 0; i < hf->hf_sector_cnt; i++) { - hf->hf_itf->hff_sector_info(i, &start, &size); + hf->hf_itf->hff_sector_info(hf, i, &start, &size); if (start >= fa->fa_off && start < fa->fa_off + fa->fa_size) { if (first_idx == -1) { first_idx = i; @@ -507,7 +491,7 @@ nffs_misc_desc_from_flash_area(int id, int *cnt, struct nffs_area_desc *nad) move_on = 1; for (i = first_idx, j = 0; i < last_idx + 1; i++) { - hf->hf_itf->hff_sector_info(i, &start, &size); + hf->hf_itf->hff_sector_info(hf, i, &start, &size); if (move_on) { nad[j].nad_flash_id = fa->fa_device_id; nad[j].nad_offset = start; diff --git a/libraries/nffs/src/hw/hal/include/hal/hal_bsp.h b/libraries/nffs/src/hw/hal/include/hal/hal_bsp.h new file mode 100644 index 00000000..be8730fb --- /dev/null +++ b/libraries/nffs/src/hw/hal/include/hal/hal_bsp.h @@ -0,0 +1,83 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +#ifndef __HAL_BSP_H_ +#define __HAL_BSP_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +#include + +#if 0 +/* + * Initializes BSP; registers flash_map with the system. + */ +void hal_bsp_init(void); +#endif + +/* + * Return pointer to flash device structure, given BSP specific + * flash id. + */ +struct hal_flash; +const struct hal_flash *hal_bsp_flash_dev(uint8_t flash_id); + +#if 0 +/* + * Grows heap by given amount. XXX giving space back not implemented. + */ +void *_sbrk(int incr); + +/* + * Report which memory areas should be included inside a coredump. + */ +struct hal_bsp_mem_dump { + void *hbmd_start; + uint32_t hbmd_size; +}; + +const struct hal_bsp_mem_dump *hal_bsp_core_dump(int *area_cnt); + +/* + * Get unique HW identifier/serial number for platform. + * Returns the number of bytes filled in. + */ +#define HAL_BSP_MAX_ID_LEN 32 +int hal_bsp_hw_id(uint8_t *id, int max_len); + +#define HAL_BSP_POWER_ON (1) +#define HAL_BSP_POWER_WFI (2) +#define HAL_BSP_POWER_SLEEP (3) +#define HAL_BSP_POWER_DEEP_SLEEP (4) +#define HAL_BSP_POWER_OFF (5) +#define HAL_BSP_POWER_PERUSER (128) + +int hal_bsp_power_state(int state); + +/* Returns priority of given interrupt number */ +uint32_t hal_bsp_get_nvic_priority(int irq_num, uint32_t pri); +#endif + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/libraries/nffs/src/hw/hal/include/hal/hal_flash.h b/libraries/nffs/src/hw/hal/include/hal/hal_flash.h index 76c31703..11f5f1df 100644 --- a/libraries/nffs/src/hw/hal/include/hal/hal_flash.h +++ b/libraries/nffs/src/hw/hal/include/hal/hal_flash.h @@ -26,6 +26,7 @@ extern "C" { #include +int hal_flash_ioctl(uint8_t flash_id, uint32_t cmd, void *args); int hal_flash_read(uint8_t flash_id, uint32_t address, void *dst, uint32_t num_bytes); int hal_flash_write(uint8_t flash_id, uint32_t address, const void *src, @@ -33,10 +34,7 @@ int hal_flash_write(uint8_t flash_id, uint32_t address, const void *src, int hal_flash_erase_sector(uint8_t flash_id, uint32_t sector_address); int hal_flash_erase(uint8_t flash_id, uint32_t address, uint32_t num_bytes); uint8_t hal_flash_align(uint8_t flash_id); -int nrf52_flash_init(void); - -struct hal_flash; -const struct hal_flash *hal_bsp_flash_dev(uint8_t flash_id); +int hal_flash_init(void); #ifdef __cplusplus } diff --git a/libraries/nffs/src/hw/hal/include/hal/hal_flash_int.h b/libraries/nffs/src/hw/hal/include/hal/hal_flash_int.h index 125b0294..d424bb17 100644 --- a/libraries/nffs/src/hw/hal/include/hal/hal_flash_int.h +++ b/libraries/nffs/src/hw/hal/include/hal/hal_flash_int.h @@ -29,12 +29,18 @@ extern "C" { /* * API that flash driver has to implement. */ +struct hal_flash; + struct hal_flash_funcs { -// int (*hff_read)(uint32_t address, void *dst, uint32_t num_bytes); -// int (*hff_write)(uint32_t address, const void *src, uint32_t num_bytes); -// int (*hff_erase_sector)(uint32_t sector_address); - int (*hff_sector_info)(int idx, uint32_t *address, uint32_t *size); -// int (*hff_init)(void); + int (*hff_read)(const struct hal_flash *dev, uint32_t address, void *dst, + uint32_t num_bytes); + int (*hff_write)(const struct hal_flash *dev, uint32_t address, + const void *src, uint32_t num_bytes); + int (*hff_erase_sector)(const struct hal_flash *dev, + uint32_t sector_address); + int (*hff_sector_info)(const struct hal_flash *dev, int idx, + uint32_t *address, uint32_t *size); + int (*hff_init)(const struct hal_flash *dev); }; struct hal_flash { @@ -42,7 +48,7 @@ struct hal_flash { uint32_t hf_base_addr; uint32_t hf_size; int hf_sector_cnt; - int hf_align; /* Alignment requirement. 1 if unrestricted. */ + int hf_align; /* Alignment requirement. 1 if unrestricted. */ }; /* diff --git a/libraries/nffs/src/hw/hal/src/hal_flash.c b/libraries/nffs/src/hw/hal/src/hal_flash.c index b8275e30..403c7b26 100644 --- a/libraries/nffs/src/hw/hal/src/hal_flash.c +++ b/libraries/nffs/src/hw/hal/src/hal_flash.c @@ -6,7 +6,7 @@ * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, @@ -16,46 +16,59 @@ * specific language governing permissions and limitations * under the License. */ - -#include +#include #include +//#include -#include "nrf.h" -//#include "mcu/nrf52_hal.h" - -#include +#include "hal/hal_bsp.h" #include "hal/hal_flash.h" -#include "rtos.h" +#include "hal/hal_flash_int.h" -#include "common_func.h" -#include "verify.h" -#include "nrf52_flash.h" - -int hal_flash_sector_info(int idx, uint32_t *address, uint32_t *sz); - -static const struct hal_flash_funcs nrf52k_flash_funcs = { -// .hff_read = hal_flash_read, -// .hff_write = hal_flash_write, -// .hff_erase_sector = hal_flash_erase_sector, - .hff_sector_info = hal_flash_sector_info, -// .hff_init = hal_flash_init -}; - - -const struct hal_flash nrf52k_flash_dev = { - .hf_itf = &nrf52k_flash_funcs, - .hf_base_addr = 0x00000000, - .hf_size = 512 * 1024, /* XXX read from factory info? */ - .hf_sector_cnt = 128, /* XXX read from factory info? */ - .hf_align = 1 -}; - -uint8_t hal_flash_align(uint8_t flash_id) +int +hal_flash_init(void) { - return nrf52k_flash_dev.hf_align; + const struct hal_flash *hf; + uint8_t i; + int rc = 0; + + for (i = 0; ; i++) { + hf = hal_bsp_flash_dev(i); + if (!hf) { + break; + } + if (hf->hf_itf->hff_init(hf)) { + rc = -1; + } + } + return rc; } -static int hal_flash_check_addr(const struct hal_flash *hf, uint32_t addr) +uint8_t +hal_flash_align(uint8_t flash_id) +{ + const struct hal_flash *hf; + + hf = hal_bsp_flash_dev(flash_id); + if (!hf) { + return 1; + } + return hf->hf_align; +} + +uint32_t +hal_flash_sector_size(const struct hal_flash *hf, int sec_idx) +{ + uint32_t size; + uint32_t start; + + if (hf->hf_itf->hff_sector_info(hf, sec_idx, &start, &size)) { + return 0; + } + return size; +} + +static int +hal_flash_check_addr(const struct hal_flash *hf, uint32_t addr) { if (addr < hf->hf_base_addr || addr > hf->hf_base_addr + hf->hf_size) { return -1; @@ -63,32 +76,56 @@ static int hal_flash_check_addr(const struct hal_flash *hf, uint32_t addr) return 0; } -const struct hal_flash *hal_bsp_flash_dev(uint8_t flash_id) +int +hal_flash_read(uint8_t id, uint32_t address, void *dst, uint32_t num_bytes) { - return &nrf52k_flash_dev; + const struct hal_flash *hf; + + hf = hal_bsp_flash_dev(id); + if (!hf) { + return -1; + } + if (hal_flash_check_addr(hf, address) || + hal_flash_check_addr(hf, address + num_bytes)) { + return -1; + } + return hf->hf_itf->hff_read(hf, address, dst, num_bytes); } -int hal_flash_read(uint8_t id, uint32_t address, void *dst, uint32_t num_bytes) +int +hal_flash_write(uint8_t id, uint32_t address, const void *src, + uint32_t num_bytes) { - (void) id; - memcpy(dst, (void *)address, num_bytes); - return 0; + const struct hal_flash *hf; + + hf = hal_bsp_flash_dev(id); + if (!hf) { + return -1; + } + if (hal_flash_check_addr(hf, address) || + hal_flash_check_addr(hf, address + num_bytes)) { + return -1; + } + return hf->hf_itf->hff_write(hf, address, src, num_bytes); } -int hal_flash_write(uint8_t id, uint32_t address, const void *src, uint32_t num_bytes) +int +hal_flash_erase_sector(uint8_t id, uint32_t sector_address) { - (void) id; + const struct hal_flash *hf; - return nrf52_flash_write(address, src, num_bytes); + hf = hal_bsp_flash_dev(id); + if (!hf) { + return -1; + } + if (hal_flash_check_addr(hf, sector_address)) { + return -1; + } + return hf->hf_itf->hff_erase_sector(hf, sector_address); } -int hal_flash_erase_sector(uint8_t id, uint32_t sector_address) -{ - (void) id; - return nrf52_flash_erase_sector(sector_address); -} - -int hal_flash_erase(uint8_t id, uint32_t address, uint32_t num_bytes) +int +hal_flash_erase(uint8_t id, uint32_t address, uint32_t num_bytes) { const struct hal_flash *hf; uint32_t start, size; @@ -97,8 +134,10 @@ int hal_flash_erase(uint8_t id, uint32_t address, uint32_t num_bytes) int i; int rc; - hf = &nrf52k_flash_dev; - + hf = hal_bsp_flash_dev(id); + if (!hf) { + return -1; + } if (hal_flash_check_addr(hf, address) || hal_flash_check_addr(hf, address + num_bytes)) { return -1; @@ -113,7 +152,7 @@ int hal_flash_erase(uint8_t id, uint32_t address, uint32_t num_bytes) } for (i = 0; i < hf->hf_sector_cnt; i++) { - rc = hal_flash_sector_info(i, &start, &size); + rc = hf->hf_itf->hff_sector_info(hf, i, &start, &size); assert(rc == 0); end_area = start + size; if (address < end_area && end > start) { @@ -121,7 +160,7 @@ int hal_flash_erase(uint8_t id, uint32_t address, uint32_t num_bytes) * If some region of eraseable area falls inside sector, * erase the sector. */ - if (hal_flash_erase_sector(id, start)) { + if (hf->hf_itf->hff_erase_sector(hf, start)) { return -1; } } @@ -129,10 +168,8 @@ int hal_flash_erase(uint8_t id, uint32_t address, uint32_t num_bytes) return 0; } -int hal_flash_sector_info(int idx, uint32_t *address, uint32_t *sz) +int +hal_flash_ioctl(uint8_t id, uint32_t cmd, void *args) { - assert(idx < nrf52k_flash_dev.hf_sector_cnt); - *address = idx * NRF52K_FLASH_SECTOR_SZ; - *sz = NRF52K_FLASH_SECTOR_SZ; - return 0; + return 0; } diff --git a/libraries/nffs/src/sys/defs/include/defs/error.h b/libraries/nffs/src/sys/defs/include/defs/error.h new file mode 100644 index 00000000..7bc984db --- /dev/null +++ b/libraries/nffs/src/sys/defs/include/defs/error.h @@ -0,0 +1,46 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +#ifndef H_DEFS_ERROR_ +#define H_DEFS_ERROR_ + +#ifdef __cplusplus +extern "C" { +#endif + +#define SYS_EOK (0) +#define SYS_ENOMEM (-1) +#define SYS_EINVAL (-2) +#define SYS_ETIMEOUT (-3) +#define SYS_ENOENT (-4) +#define SYS_EIO (-5) +#define SYS_EAGAIN (-6) +#define SYS_EACCES (-7) +#define SYS_EBUSY (-8) +#define SYS_ENODEV (-9) +#define SYS_ERANGE (-10) + +#define SYS_EPERUSER (-65535) + +#ifdef __cplusplus +} +#endif + + +#endif /* H_DEFS_ERROR_ */ diff --git a/libraries/nffs/src/sys/flash_map/include/flash_map/flash_map.h b/libraries/nffs/src/sys/flash_map/include/flash_map/flash_map.h index acb0f29b..b4149ceb 100644 --- a/libraries/nffs/src/sys/flash_map/include/flash_map/flash_map.h +++ b/libraries/nffs/src/sys/flash_map/include/flash_map/flash_map.h @@ -24,6 +24,12 @@ extern "C" { #endif +// Adafruit +#define NFFS_SECTOR_NUM 7 +#define NFFS_START_ADDR 0x6D000 + +#define FLASH_AREA_NFFS 0 + /** * * Provides abstraction of flash regions for type of use. @@ -51,7 +57,6 @@ struct flash_area { uint32_t fa_size; }; -#if 0 extern const struct flash_area *flash_map; extern int flash_map_entries; @@ -82,6 +87,7 @@ int flash_area_erase(const struct flash_area *, uint32_t off, uint32_t len); */ uint8_t flash_area_align(const struct flash_area *); +#if 0 /* * Given flash map index, return info about sectors within the area. */ diff --git a/libraries/nffs/src/sys/flash_map/src/flash_map.c b/libraries/nffs/src/sys/flash_map/src/flash_map.c new file mode 100644 index 00000000..e44c707b --- /dev/null +++ b/libraries/nffs/src/sys/flash_map/src/flash_map.c @@ -0,0 +1,296 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +#include +#include +#include + +#include "syscfg/syscfg.h" +//#include "sysinit/sysinit.h" +//#include "sysflash/sysflash.h" +#include "defs/error.h" +#include "hal/hal_bsp.h" +#include "hal/hal_flash.h" +#include "hal/hal_flash_int.h" +//#include "mfg/mfg.h" +#include "flash_map/flash_map.h" + +// Adafruit: flash map with only NFFS area +const struct flash_area adafruit_flash_map[] = +{ + /* FLASH_AREA_NFFS */ + { + .fa_id = FLASH_AREA_NFFS, + .fa_device_id = 0, + .fa_off = NFFS_START_ADDR, + .fa_size = NFFS_SECTOR_NUM*4096, /* 28 kB */ + } +}; + +const struct flash_area *flash_map = adafruit_flash_map; +int flash_map_entries = sizeof(adafruit_flash_map) / sizeof (adafruit_flash_map[0]); + +int +flash_area_open(uint8_t id, const struct flash_area **fap) +{ + const struct flash_area *area; + int i; + + if (flash_map == NULL) { + return SYS_EACCES; + } + + for (i = 0; i < flash_map_entries; i++) { + area = flash_map + i; + if (area->fa_id == id) { + *fap = area; + return 0; + } + } + + return SYS_ENOENT; +} + +void +flash_area_close(const struct flash_area *fa) +{ + /* nothing to do for now */ +} + +#if 0 +int +flash_area_to_sectors(int id, int *cnt, struct flash_area *ret) +{ + const struct flash_area *fa; + const struct hal_flash *hf; + uint32_t start; + uint32_t size; + int rc; + int i; + + rc = flash_area_open(id, &fa); + if (rc != 0) { + return rc; + } + + *cnt = 0; + + hf = hal_bsp_flash_dev(fa->fa_device_id); + for (i = 0; i < hf->hf_sector_cnt; i++) { + hf->hf_itf->hff_sector_info(hf, i, &start, &size); + if (start >= fa->fa_off && start < fa->fa_off + fa->fa_size) { + if (ret) { + ret->fa_id = id; + ret->fa_device_id = fa->fa_device_id; + ret->fa_off = start; + ret->fa_size = size; + ret++; + } + (*cnt)++; + } + } + return 0; +} +#endif + +int +flash_area_read(const struct flash_area *fa, uint32_t off, void *dst, + uint32_t len) +{ + if (off > fa->fa_size || off + len > fa->fa_size) { + return -1; + } + return hal_flash_read(fa->fa_device_id, fa->fa_off + off, dst, len); +} + +int +flash_area_write(const struct flash_area *fa, uint32_t off, const void *src, + uint32_t len) +{ + if (off > fa->fa_size || off + len > fa->fa_size) { + return -1; + } + return hal_flash_write(fa->fa_device_id, fa->fa_off + off, + (void *)src, len); +} + +int +flash_area_erase(const struct flash_area *fa, uint32_t off, uint32_t len) +{ + if (off > fa->fa_size || off + len > fa->fa_size) { + return -1; + } + return hal_flash_erase(fa->fa_device_id, fa->fa_off + off, len); +} + +uint8_t +flash_area_align(const struct flash_area *fa) +{ + return hal_flash_align(fa->fa_device_id); +} + +#if 0 +/** + * Converts the specified image slot index to a flash area ID. If the + * specified value is not a valid image slot index (0 or 1), a crash is + * triggered. + */ +int +flash_area_id_from_image_slot(int slot) +{ + switch (slot) { + case 0: + return FLASH_AREA_IMAGE_0; + case 1: + return FLASH_AREA_IMAGE_1; + default: + assert(0); + return FLASH_AREA_IMAGE_0; + } +} + +/** + * Converts the specified flash area ID to an image slot index (0 or 1). If + * the area ID does not correspond to an image slot, -1 is returned. + */ +int +flash_area_id_to_image_slot(int area_id) +{ + switch (area_id) { + case FLASH_AREA_IMAGE_0: + return 0; + case FLASH_AREA_IMAGE_1: + return 1; + default: + return -1; + } +} + +/** + * Reads the flash map layout from the manufacturing meta region. This + * function requires that the flash map be populated with a + * FLASH_AREA_BOOTLOADER entry, as the meta region is stored at the end of the + * boot loader area. + * + * @param max_areas The maximum number of flash areas to read. If + * the count of flash area TLVs in the meta + * region is greater than this number, this + * function fails. + * @param out_areas (out) An array of flash areas. On success, the flash + * map stored in the meta region gets written + * here. + * @param out_num_areas (out) On success, the number of flash areas read gets + * written here. + * + * @return 0 on success; nonzero on failure. + */ +static int +flash_map_read_mfg(int max_areas, + struct flash_area *out_areas, int *out_num_areas) +{ + struct mfg_meta_flash_area meta_flash_area; + struct mfg_meta_tlv tlv; + struct flash_area *fap; + uint32_t off; + int rc; + + *out_num_areas = 0; + off = 0; + + /* Ensure manufacturing meta region has been located in flash. */ + rc = mfg_init(); + if (rc != 0) { + return rc; + } + + while (1) { + if (*out_num_areas >= max_areas) { + return -1; + } + + rc = mfg_next_tlv_with_type(&tlv, &off, MFG_META_TLV_TYPE_FLASH_AREA); + switch (rc) { + case 0: + break; + case MFG_EDONE: + return 0; + default: + return rc; + } + + rc = mfg_read_tlv_flash_area(&tlv, off, &meta_flash_area); + if (rc != 0) { + return rc; + } + + fap = out_areas + *out_num_areas; + fap->fa_id = meta_flash_area.area_id; + fap->fa_device_id = meta_flash_area.device_id; + fap->fa_off = meta_flash_area.offset; + fap->fa_size = meta_flash_area.size; + + (*out_num_areas)++; + } +} + +void +flash_map_init(void) +{ + static struct flash_area mfg_areas[MYNEWT_VAL(FLASH_MAP_MAX_AREAS)]; + + int num_areas; + int rc; + + /* Ensure this function only gets called by sysinit. */ + SYSINIT_ASSERT_ACTIVE(); + + rc = hal_flash_init(); + SYSINIT_PANIC_ASSERT(rc == 0); + + /* Use the hardcoded default flash map. This is done for two reasons: + * 1. A minimal flash map configuration is required to boot strap the + * process of reading the flash map from the manufacturing meta region. + * In particular, a FLASH_AREA_BOOTLOADER entry is required, as the meta + * region is located at the end of the boot loader area. + * 2. If we fail to read the flash map from the meta region, the system + * continues to use the default flash map. + */ + flash_map = sysflash_map_dflt; + flash_map_entries = sizeof sysflash_map_dflt / sizeof sysflash_map_dflt[0]; + + /* Attempt to read the flash map from the manufacturing meta region. On + * success, use the new flash map instead of the default hardcoded one. + */ + rc = flash_map_read_mfg(sizeof mfg_areas / sizeof mfg_areas[0], + mfg_areas, &num_areas); + if (rc == 0) { + flash_map = mfg_areas; + flash_map_entries = num_areas; + } +} + +#else + +void flash_map_init(void) +{ + hal_flash_init(); +} + +#endif + diff --git a/libraries/nffs/src/syscfg/syscfg.h b/libraries/nffs/src/syscfg/syscfg.h index c941140f..052c3de3 100644 --- a/libraries/nffs/src/syscfg/syscfg.h +++ b/libraries/nffs/src/syscfg/syscfg.h @@ -46,7 +46,6 @@ #define MYNEWT_VAL(x) MYNEWT_VAL_ ## x #define MYNEWT_VAL_FS_CLI (0) -#define MYNEWT_VAL_NFFS_FLASH_AREA 0 #define MYNEWT_VAL_NFFS_DETECT_FAIL (NFFS_DETECT_FAIL_FORMAT) #endif /* SYSCFG_H_ */