Hello,
I am attempting to set up NVS to capture GPS data received from the modem for transmission to NB-IoT after collection has ended.
I have successfully implimented the NVS example into my code and have been able to store and read back data from flash (although I keep reading back random characters at the end of the strings for some reason that I would like to understand also). I have an issue however when I try to increase the sector count of the flash storage. I can add 5 sectors without any issue but more than 5 (according to the OPS pg21, there are 255 pages of flash), the operations nvs_clear/write/read all return -22 as an error.
The code below is a trimmed down version, using simulated GPS values.
/* * Copyright (c) 2018 Laczen * * SPDX-License-Identifier: Apache-2.0 */ #include <zephyr.h> #include <misc/reboot.h> #include <device.h> #include <string.h> #include "Header/UDP.h" #include "Header/Types_Defines.h" #define value1 "53.760241,-5.147095,1.023,11:20:22" #define value2 "53.760241,-5.147095,1.023,11:20:23" #define value3 "53.760241,-5.147095,1.023,11:20:24" #define value4 "53.760241,-5.147095,1.023,11:20:25" #define value5 "53.760241,-5.147095,1.023,11:20:26" #define value6 "53.760241,-5.147095,1.023,11:20:27" #define value7 "53.760241,-5.147095,1.023,11:20:28" #define value8 "53.760241,-5.147095,1.023,11:20:29" #define value9 "53.760241,-5.147095,1.023,11:20:30" #define value10 "53.760241,-5.147095,1.023,11:20:31" #define value11 "53.760241,-5.147095,1.023,11:20:32" #define value12 "53.760241,-5.147095,1.023,11:20:33" static const char results[][60] = { value1,value2,value3,value4,value5,value6,value7,value8,value9,value10,value11,value12}; static struct nvs_fs fs; struct flash_pages_info info; int nvs_setup(void) { int err; fs.offset = DT_FLASH_AREA_STORAGE_OFFSET; err = flash_get_page_info_by_offs(device_get_binding(DT_FLASH_DEV_NAME), fs.offset, &info); if (err) { printk("Unable to get page info"); } fs.sector_size = info.size; fs.sector_count = 250U; err = nvs_init(&fs, DT_FLASH_DEV_NAME); if (err) { printk("Flash Init failed\n"); } err=nvs_clear(&fs); if(err){ printk("nvs_clear failed\n"); } return err; } void main(void) { int err; err = enable_modem(); if(err){ printk("Modem init failed\n"); } err = nvs_setup(); if(err){ printk("nvs_setup failed\n"); } for(int i=0; i<ARRAY_SIZE(results); i++) { printk("Writing %s to NVS\n", results[i]); ssize_t bytes_written = nvs_write(&fs, i, results[i], strlen(results[i])); printk("Bytes written to nvs: %d at ID %d\n", bytes_written, i); } ssize_t freespace = nvs_calc_free_space(&fs); printk("Remaining free space in nvs sector is %d Bytes\n", freespace); k_sleep(K_MSEC(5000)); char nvs_rx_buff[39]; // char nvs_UDP_buff[2145] = "\0"; for(int i=0; i<ARRAY_SIZE(results); i++) { ssize_t bytes_read = nvs_read(&fs, i, nvs_rx_buff, sizeof(nvs_rx_buff)); printk("Bytes read from nvs: %d at ID %d\n", bytes_read, i); printk("Data read from nvs: %s at ID %d\n", nvs_rx_buff, i); // strcat(nvs_UDP_buff,nvs_rx_buff); // strcat(nvs_UDP_buff,"\n"); } // int count=0; // do // { // if(count==5) // { // break; // } // printk("Waiting 5 more seconds!\n"); // k_sleep(K_MSEC(5000)); // count++; // } // while((err=check_network())<0); // // err = UDP_Send(nvs_UDP_buff); // if(err) // { // printk("UDP_send failed\n"); // } }
Output at 5U Sectors:
09 NRF_EGU1 Non-Secure OK 10 NRF_EGU2 Non-Secure OK 11 NRF_TWIM2 Non-Secure OK 12 NRF_SPIM3 Non-Secure OK 13 NRF_TIMER0 Non-Secure OK 14 NRF_TIMER1 Non-Secure OK 15 NRF_TIMER2 Non-Secure OK 16 NRF_SAADC Non-Secure OK 17 NRF_GPIOTE1 Non-Secure OK SPM: NS image at 0x8000 SPM: NS MSP at 0x200232e0 SPM: NS reset vector at 0xe329 SPM: prepare to jump to Non-Secure image. ***** Booting Zephyr OS v1.14.99-ncs1 ***** Socket ID: 2 Sending AT command: AT+CFUN=4 Modem response: OK Sending AT command: AT+CFUN=1 Modem response: OK Modem enabled Writing 53.760241,-5.147095,1.023,11:20:22 to NVS Bytes written to nvs: 34 at ID 0 Writing 53.760241,-5.147095,1.023,11:20:23 to NVS Bytes written to nvs: 34 at ID 1 Writing 53.760241,-5.147095,1.023,11:20:24 to NVS Bytes written to nvs: 34 at ID 2 Writing 53.760241,-5.147095,1.023,11:20:25 to NVS Bytes written to nvs: 34 at ID 3 Writing 53.760241,-5.147095,1.023,11:20:26 to NVS Bytes written to nvs: 34 at ID 4 Writing 53.760241,-5.147095,1.023,11:20:27 to NVS Bytes written to nvs: 34 at ID 5 Writing 53.760241,-5.147095,1.023,11:20:28 to NVS Bytes written to nvs: 34 at ID 6 Writing 53.760241,-5.147095,1.023,11:20:29 to NVS Bytes written to nvs: 34 at ID 7 Writing 53.760241,-5.147095,1.023,11:20:30 to NVS Bytes written to nvs: 34 at ID 8 Writing 53.760241,-5.147095,1.023,11:20:31 to NVS Bytes written to nvs: 34 at ID 9 Writing 53.760241,-5.147095,1.023,11:20:32 to NVS Bytes written to nvs: 34 at ID 10 Writing 53.760241,-5.147095,1.023,11:20:33 to NVS Bytes written to nvs: 34 at ID 11 Remaining free space in nvs sector is 15848 Bytes [00:00:01.271,820] [0m<inf> fs_nvs: 5 Sectors of 4096 bytes[0m [00:00:01.271,820] [0m<inf> fs_nvs: alloc wra: 0, f90[0m [00:00:01.271,820] [0m<inf> fs_nvs: data wra: 0, 198[0m [00:00:01.271,850] [0m<dbg> fs_nvs.nvs_flash_erase_sector: Erasing flash at fa000, len 4096[0m Bytes read from nvs: 34 at ID 0 Data read from nvs: 53.760241,-5.147095,1.023,11:20:22ßËùò at ID 0 Bytes read from nvs: 34 at ID 1 Data read from nvs: 53.760241,-5.147095,1.023,11:20:23ßËùò at ID 1 Bytes read from nvs: 34 at ID 2 Data read from nvs: 53.760241,-5.147095,1.023,11:20:24ßËùò at ID 2 Bytes read from nvs: 34 at ID 3 Data read from nvs: 53.760241,-5.147095,1.023,11:20:25ßËùò at ID 3 Bytes read from nvs: 34 at ID 4 Data read from nvs: 53.760241,-5.147095,1.023,11:20:26ßËùò at ID 4 Bytes read from nvs: 34 at ID 5 Data read from nvs: 53.760241,-5.147095,1.023,11:20:27ßËùò at ID 5 Bytes read from nvs: 34 at ID 6 Data read from nvs: 53.760241,-5.147095,1.023,11:20:28ßËùò at ID 6 Bytes read from nvs: 34 at ID 7 Data read from nvs: 53.760241,-5.147095,1.023,11:20:29ßËùò at ID 7 Bytes read from nvs: 34 at ID 8 Data read from nvs: 53.760241,-5.147095,1.023,11:20:30ßËùò at ID 8 Bytes read from nvs: 34 at ID 9 Data read from nvs: 53.760241,-5.147095,1.023,11:20:31ßËùò at ID 9 Bytes read from nvs: 34 at ID 10 Data read from nvs: 53.760241,-5.147095,1.023,11:20:32ßËùò at ID 10 Bytes read from nvs: 34 at ID 11 Data read from nvs: 53.760241,-5.147095,1.023,11:20:33ßËùò at ID 11
Output at 10U Sectors:
10 NRF_EGU2 Non-Secure OK 11 NRF_TWIM2 Non-Secure OK 12 NRF_SPIM3 Non-Secure OK 13 NRF_TIMER0 Non-Secure OK 14 NRF_TIMER1 Non-Secure OK 15 NRF_TIMER2 Non-Secure OK 16 NRF_SAADC Non-Secure OK 17 NRF_GPIOTE1 Non-Secure OK SPM: NS image at 0x8000 SPM: NS MSP at 0x200232e0 SPM: NS reset vector at 0xe329 SPM: prepare to jump to Non-Secure image. ***** Booting Zephyr OS v1.14.99-ncs1 ***** Socket ID: 2 Sending AT command: AT+CFUN=4 Modem response: OK Sending AT command: AT+CFUN=1 Modem response: OK Modem enabled nvs_clear failed nvs_setup failed Writing 53.760241,-5.147095,1.023,11:20:22 to NVS Bytes written to nvs: -22 at ID 0 Writing 53.760241,-5.147095,1.023,11:20:23 to NVS Bytes written to nvs: -22 at ID 1 Writing 53.760241,-5.147095,1.023,11:20:24 to NVS Bytes written to nvs: -22 at ID 2 Writing 53.760241,-5.147095,1.023,11:20:25 to NVS Bytes written to nvs: -22 at ID 3 Writing 53.760241,-5.147095,1.023,11:20:26 to NVS Bytes written to nvs: -22 at ID 4 Writing 53.760241,-5.147095,1.023,11:20:27 to NVS Bytes written to nvs: -22 at ID 5 Writing 53.760241,-5.147095,1.023,11:20:28 to NVS Bytes written to nvs: -22 at ID 6 Writing 53.760241,-5.147095,1.023,11:20:29 to NVS Bytes written to nvs: -22 at ID 7 Writing 53.760241,-5.147095,1.023,11:20:30 to NVS Bytes written to nvs: -22 at ID 8 Writing 53.760241,-5.147095,1.023,11:20:31 to NVS Bytes written to nvs: -22 at ID 9 Writing 53.760241,-5.147095,1.023,11:20:32 to NVS Bytes written to nvs: -22 at ID 10 Writing 53.760241,-5.147095,1.023,11:20:33 to NVS Bytes written to nvs: -22 at ID 11 Remaining free space in nvs sector is -22 Bytes [00:00:02.058,654] [0m<inf> fs_nvs: 10 Sectors of 4096 bytes[0m [00:00:02.058,654] [0m<inf> fs_nvs: alloc wra: 0, ff0[0m [00:00:02.058,685] [0m<inf> fs_nvs: data wra: 0, f98[0m [00:00:02.058,746] [0m<dbg> fs_nvs.nvs_flash_erase_sector: Erasing flash at fa000, len 4096[0m Bytes read from nvs: -22 at ID 0 Data read from nvs: UUUUUUUUUUUU at ID 0 Bytes read from nvs: -22 at ID 1 Data read from nvs: UUUUUUUUUUUU at ID 1 Bytes read from nvs: -22 at ID 2 Data read from nvs: UUUUUUUUUUUU at ID 2 Bytes read from nvs: -22 at ID 3 Data read from nvs: UUUUUUUUUUUU at ID 3 Bytes read from nvs: -22 at ID 4 Data read from nvs: UUUUUUUUUUUU at ID 4 Bytes read from nvs: -22 at ID 5 Data read from nvs: UUUUUUUUUUUU at ID 5 Bytes read from nvs: -22 at ID 6 Data read from nvs: UUUUUUUUUUUU at ID 6 Bytes read from nvs: -22 at ID 7 Data read from nvs: UUUUUUUUUUUU at ID 7 Bytes read from nvs: -22 at ID 8 Data read from nvs: UUUUUUUUUUUU at ID 8 Bytes read from nvs: -22 at ID 9 Data read from nvs: UUUUUUUUUUUU at ID 9 Bytes read from nvs: -22 at ID 10 Data read from nvs: UUUUUUUUUUUU at ID 10 Bytes read from nvs: -22 at ID 11 Data read from nvs: UUUUUUUUUUUU at ID 11
Output at 250U Sectors:
09 NRF_EGU1 Non-Secure OK 10 NRF_EGU2 Non-Secure OK 11 NRF_TWIM2 Non-Secure OK 12 NRF_SPIM3 Non-Secure OK 13 NRF_TIMER0 Non-Secure OK 14 NRF_TIMER1 Non-Secure OK 15 NRF_TIMER2 Non-Secure OK 16 NRF_SAADC Non-Secure OK 17 NRF_GPIOTE1 Non-Secure OK SPM: NS image at 0x8000 SPM: NS MSP at 0x200232e0 SPM: NS reset vector at 0xe329 SPM: prepare to jump to Non-Secure image. ***** Booting Zephyr OS v1.14.99-ncs1 ***** Socket ID: 2 Sending AT command: AT+CFUN=4 Modem response: OK Sending AT command: AT+CFUN=1 Modem response: OK Modem enabled nvs_clear failed nvs_setup failed Writing 53.760241,-5.147095,1.023,11:20:22 to NVS Bytes written to nvs: -22 at ID 0 Writing 53.760241,-5.147095,1.023,11:20:23 to NVS Bytes written to nvs: -22 at ID 1 Writing 53.760241,-5.147095,1.023,11:20:24 to NVS Bytes written to nvs: -22 at ID 2 Writing 53.760241,-5.147095,1.023,11:20:25 to NVS Bytes written to nvs: -22 at ID 3 Writing 53.760241,-5.147095,1.023,11:20:26 to NVS Bytes written to nvs: -22 at ID 4 Writing 53.760241,-5.147095,1.023,11:20:27 to NVS Bytes written to nvs: -22 at ID 5 Writing 53.760241,-5.147095,1.023,11:20:28 to NVS Bytes written to nvs: -22 at ID 6 Writing 53.760241,-5.147095,1.023,11:20:29 to NVS Bytes written to nvs: -22 at ID 7 Writing 53.760241,-5.147095,1.023,11:20:30 to NVS Bytes written to nvs: -22 at ID 8 Writing 53.760241,-5.147095,1.023,11:20:31 to NVS Bytes written to nvs: -22 at ID 9 Writing 53.760241,-5.147095,1.023,11:20:32 to NVS Bytes written to nvs: -22 at ID 10 Writing 53.760241,-5.147095,1.023,11:20:33 to NVS Bytes written to nvs: -22 at ID 11 Remaining free space in nvs sector is -22 Bytes [00:00:01.221,343] [0m<inf> fs_nvs: 250 Sectors of 4096 bytes[0m [00:00:01.221,343] [0m<inf> fs_nvs: alloc wra: 0, ff0[0m [00:00:01.221,343] [0m<inf> fs_nvs: data wra: 0, 0[0m Bytes read from nvs: -22 at ID 0 Data read from nvs: UUUUUUUUUUUU at ID 0 Bytes read from nvs: -22 at ID 1 Data read from nvs: UUUUUUUUUUUU at ID 1 Bytes read from nvs: -22 at ID 2 Data read from nvs: UUUUUUUUUUUU at ID 2 Bytes read from nvs: -22 at ID 3 Data read from nvs: UUUUUUUUUUUU at ID 3 Bytes read from nvs: -22 at ID 4 Data read from nvs: UUUUUUUUUUUU at ID 4 Bytes read from nvs: -22 at ID 5 Data read from nvs: UUUUUUUUUUUU at ID 5 Bytes read from nvs: -22 at ID 6 Data read from nvs: UUUUUUUUUUUU at ID 6 Bytes read from nvs: -22 at ID 7 Data read from nvs: UUUUUUUUUUUU at ID 7 Bytes read from nvs: -22 at ID 8 Data read from nvs: UUUUUUUUUUUU at ID 8 Bytes read from nvs: -22 at ID 9 Data read from nvs: UUUUUUUUUUUU at ID 9 Bytes read from nvs: -22 at ID 10 Data read from nvs: UUUUUUUUUUUU at ID 10 Bytes read from nvs: -22 at ID 11 Data read from nvs: UUUUUUUUUUUU at ID 11
Is there something missing to enable the use of more than a few pages of flash?
Many thanks,
MJD093