samples: nvs: various cleanups

Replace device_get_binding with DEVICE_DT_GET(). Fix and clean up
error handling and debug printing during initialization.

Signed-off-by: Martí Bolívar <marti.bolivar@nordicsemi.no>
This commit is contained in:
Martí Bolívar 2021-04-26 09:18:26 -07:00 committed by Carles Cufí
commit bb46d8b0fa

View file

@ -68,25 +68,31 @@ void main(void)
uint8_t key[8], longarray[128];
uint32_t reboot_counter = 0U, reboot_counter_his;
struct flash_pages_info info;
const struct device *flash_dev;
/* define the nvs file system by settings with:
* sector_size equal to the pagesize,
* 3 sectors
* starting at FLASH_AREA_OFFSET(storage)
*/
flash_dev = DEVICE_DT_GET(DT_CHOSEN(zephyr_flash_controller));
if (!device_is_ready(flash_dev)) {
printk("Flash device %s is not ready\n", flash_dev->name);
return;
}
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(flash_dev, fs.offset, &info);
if (rc) {
printk("Unable to get page info");
printk("Unable to get page info\n");
return;
}
fs.sector_size = info.size;
fs.sector_count = 3U;
rc = nvs_init(&fs, DT_CHOSEN_ZEPHYR_FLASH_CONTROLLER_LABEL);
rc = nvs_init(&fs, flash_dev->name);
if (rc) {
printk("Flash Init failed\n");
return;
}
/* ADDRESS_ID is used to store an address, lets see if we can