diff --git a/src/storage.c b/src/storage.c index 6d740b9..642241d 100644 --- a/src/storage.c +++ b/src/storage.c @@ -9,6 +9,20 @@ #include "sequencenumber.h" #include "storage.h" + +// Get external flash device +#if (CONFIG_SPI_NOR - 0) || \ + DT_NODE_HAS_STATUS(DT_INST(0, jedec_spi_nor), okay) +#define FLASH_DEVICE DT_LABEL(DT_INST(0, jedec_spi_nor)) +#define FLASH_NAME "JEDEC SPI-NOR" +#elif (CONFIG_NORDIC_QSPI_NOR - 0) || \ + DT_NODE_HAS_STATUS(DT_INST(0, nordic_qspi_nor), okay) +#define FLASH_DEVICE DT_LABEL(DT_INST(0, nordic_qspi_nor)) +#define FLASH_NAME "JEDEC QSPI-NOR" +#else +#error Unsupported flash driver +#endif + // Maybe use this as param for init function #define SEC_COUNT 8U @@ -67,7 +81,7 @@ int init_contact_storage(void) { struct flash_pages_info info; // define the nvs file system fs.offset = FLASH_AREA_OFFSET(storage); - rc = flash_get_page_info_by_offs(device_get_binding(DT_CHOSEN_ZEPHYR_FLASH_CONTROLLER_LABEL), fs.offset, &info); + rc = flash_get_page_info_by_offs(device_get_binding(FLASH_DEVICE), fs.offset, &info); if (rc) { // Error during retrieval of page information @@ -76,7 +90,7 @@ int init_contact_storage(void) { fs.sector_size = info.size; fs.sector_count = SEC_COUNT; - rc = nvs_init(&fs, DT_CHOSEN_ZEPHYR_FLASH_CONTROLLER_LABEL); + rc = nvs_init(&fs, FLASH_DEVICE); if (rc) { // Error during nvs_init return rc; @@ -86,6 +100,7 @@ int init_contact_storage(void) { rc = load_storage_information(); printk("Currently %d contacts stored!\n", contact_information.count); + printk("Space available: %d\n", FLASH_AREA_SIZE(storage)); return rc; } @@ -112,12 +127,12 @@ int add_contact(contact_t* src) { } // TODO handle start and end -// TODO lome: do we need this? int delete_contact(record_sequence_number_t sn) { storage_id_t id = convert_sn_to_storage_id(sn); int rc = nvs_delete(&fs, id); if (!rc) { // TODO lome: what happens, if contacts in the middle are deleted? + // Magic contact_information.count--; if (sn_equal(sn, get_oldest_sequence_number())) { contact_information.oldest_contact = sn_increment(contact_information.oldest_contact); diff --git a/zephyr/nrf52840_mdk.overlay b/zephyr/nrf52840_mdk.overlay index b9f3b97..fa80a1e 100644 --- a/zephyr/nrf52840_mdk.overlay +++ b/zephyr/nrf52840_mdk.overlay @@ -1,4 +1,6 @@ +/delete-node/ &storage_partition; + / { buttons { compatible = "gpio-keys"; @@ -12,3 +14,16 @@ }; }; + +&mx25r64 { + partitions { + compatible = "fixed-partitions"; + #address-cells = <1>; + #size-cells = <1>; + + partition@0 { + label = "storage"; + reg = <0x0000000 0x00300000>; + }; + }; +}; \ No newline at end of file diff --git a/zephyr/nrf52840dk_nrf52840.overlay b/zephyr/nrf52840dk_nrf52840.overlay new file mode 100644 index 0000000..b2acc58 --- /dev/null +++ b/zephyr/nrf52840dk_nrf52840.overlay @@ -0,0 +1,15 @@ + +/delete-node/ &storage_partition; + +&mx25r64 { + partitions { + compatible = "fixed-partitions"; + #address-cells = <1>; + #size-cells = <1>; + + partition@0 { + label = "storage"; + reg = <0x0000000 0x00300000>; + }; + }; +}; \ No newline at end of file diff --git a/zephyr/prj.conf b/zephyr/prj.conf index d2e571f..8be03c2 100644 --- a/zephyr/prj.conf +++ b/zephyr/prj.conf @@ -20,6 +20,21 @@ CONFIG_FLASH=y CONFIG_FLASH_PAGE_LAYOUT=y CONFIG_NVS=y -CONFIG_LOG=y CONFIG_NVS_LOG_LEVEL_DBG=y CONFIG_MPU_ALLOW_FLASH_WRITE=y + +# CONFIG_DISK_ACCESS=y +CONFIG_MAIN_STACK_SIZE=2048 + +# Let __ASSERT do its job +CONFIG_DEBUG=y + +CONFIG_LOG=y + +CONFIG_FLASH=y +CONFIG_FLASH_MAP=y +CONFIG_FLASH_PAGE_LAYOUT=y + +# Need this when storage is on MX25R64 +CONFIG_NORDIC_QSPI_NOR=y +CONFIG_NORDIC_QSPI_NOR_FLASH_LAYOUT_PAGE_SIZE=4096