This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Flash storage, unable to write and read 1024 and 2048 bytes

Hi, I am testing the flash storage of nrf52840 with NVS example from NCS v1.4.2

i have run the example code successfully. from the example code i have changed the

uint8_t longarray[128]; to uint8_t longarray[1024];

memset(&longarray , 'A',sizeof(longarray));

output log is

*** Booting Zephyr OS build v2.4.0-ncs2  ***

 rd - ID : 1 ; D :  ; L:16
No address found, adding 192.168.1.1 at id 1
 
wd - ID : 1 ; D : 192.168.1.1 ; L:12

write : O:f8000, D:192.168.1.1, L:12
write : O:f8ff0, D:, L:8
 rd - ID : 2 ; D : è ; L:8
No key found, adding it at id 2
 
wd - ID : 2 ; D : ÿþýüûúùø ; L:8

write : O:f800c, D:ÿþýüûúùø, L:8
write : O:f8fe8, D:, L:8
 rd - ID : 3 ; D :  ; L:4
No Reboot counter found, adding it at id 3
 
wd - ID : 3 ; D :  ; L:4

write : O:f8014, D:, L:4
write : O:f8fe0, D:, L:8
 rd - ID : 4 ; D : 192.168.1.1 ; L:16
Id: 4 not found, adding it
 
wd - ID : 4 ; D : DATA ; L:5

write : O:f8018, D:DATA, L:4
write : O:f8fd8, D:, L:8
 rd - ID : 5 ; D : Pa ; L:1024
Longarray not found, adding it as id 5

Query is

1. How to properly write and read the 1024 bytes,2048 bytes

2. I need to work in ble_his_keyboard example. where flash storage is initialized for pairing data storage. I need to store the sensor data . how to interface flash write into the main application of ble_hid_keyboard

Parents
  • As continuation of 2.

    I have added NVS example to ble_hid_keyboard

    only added address_id

    void main(void)
    {
    	int err;
    	int blink_status = 0;
    
    	printk("Starting Bluetooth Peripheral HIDS keyboard example\n");
    
    	configure_gpio();
    
    	bt_conn_cb_register(&conn_callbacks);
    	bt_conn_auth_cb_register(&conn_auth_callbacks);
    
    	err = bt_enable(NULL);
    	if (err) {
    		printk("Bluetooth init failed (err %d)\n", err);
    		return;
    	}
    
    	printk("Bluetooth initialized\n");
    
    	hid_init();
    
    	if (IS_ENABLED(CONFIG_SETTINGS)) {
    		settings_load();
    	}
    
    #if CONFIG_NFC_OOB_PAIRING
    	k_work_init(&adv_work, delayed_advertising_start);
    	app_nfc_init();
    #else
    	advertising_start();
    #endif
    
    	k_work_init(&pairing_work, pairing_process);
    
            const struct flash_area *fa;
    	struct flash_sector hw_flash_sector;
            uint16_t cnt = 0;
    	size_t nvs_sector_size, nvs_size = 0;
    	
    	uint32_t sector_cnt = 1;
    
    	/* define the nvs file system by settings with:
    	 *	sector_size equal to the pagesize,
    	 *	3 sectors
    	 *	starting at FLASH_AREA_OFFSET(storage)
    	 */
             err = flash_area_open(FLASH_AREA_ID(sensor), &fa);
    	if (err) {
    		//return err;
    	}
             printk(" \nflash area : %s,%x,%x,%x,%d",fa->fa_dev_name,fa->fa_device_id,fa->fa_id,fa->fa_off,fa->fa_size);
    	err = flash_area_get_sectors(FLASH_AREA_ID(storage), &sector_cnt,
    				    &hw_flash_sector);
             printk(" \nflash area sector : %d,%d",hw_flash_sector.fs_off,hw_flash_sector.fs_size);
    	if (err == -ENODEV) {
    		//return err;
    	} else if (err != 0 && err != -ENOMEM) {
    		k_panic();
    	}
    
    	nvs_sector_size = CONFIG_SETTINGS_NVS_SECTOR_SIZE_MULT *
    			  hw_flash_sector.fs_size;
    
    	if (nvs_sector_size > UINT16_MAX) {
    		//return -EDOM;
    	}
    
    	while (cnt < CONFIG_SETTINGS_NVS_SECTOR_COUNT) {
    		nvs_size += nvs_sector_size;
    		if (nvs_size > fa->fa_size) {
    			break;
    		}
    		cnt++;
    	}
    
          printk(" \nflash area sector : %d,%d,%x",nvs_sector_size,cnt,FLASH_AREA_OFFSET(storage));
          
        
             struct flash_pages_info info;
    	//fs.offset = FLASH_AREA_OFFSET(storage);
            fs.offset = fa->fa_off;
            printk("\n f_sec : %x",fs.offset);
    	int rc = flash_get_page_info_by_offs(
    		device_get_binding(DT_CHOSEN_ZEPHYR_FLASH_CONTROLLER_LABEL),
    		fs.offset, &info);
    	if (rc) {
    		printk("Unable to get page info");
    	}
            
    	fs.sector_size = nvs_sector_size;
    	fs.sector_count = cnt;
    
             rc = nvs_init(&fs, fa->fa_dev_name);
    	if (rc) {
    		printk("Flash Init failed\n");
    	}
            char buf[20];
            //uint32_t longarray[100];
            rc = nvs_read(&fs, ADDRESS_ID, &buf, sizeof(buf));
    	if (rc > 0) { /* item was found, show it */
    		printk("Id: %d, Address: %s\n", ADDRESS_ID, buf);
    	} else   {/* item was not found, add it */
    		strcpy(buf, "192.168.1.1");
    		printk("No address found, adding %s at id %d\n", buf,
    		       ADDRESS_ID);
    		(void)nvs_write(&fs, ADDRESS_ID, &buf, strlen(buf)+1);
    	}
     //       rc = nvs_read(&fs, LONG_ID, &longarray, sizeof(longarray));
    	//if (rc > 0) {
    	//	/* item was found, show it */
    	//	printk("Id: %d, Longarray: ", LONG_ID);
    	//	for (int n = 0; n < sizeof(longarray); n++) {
    	//		printk("%x ", longarray[n]);
    	//	}
    	//	printk("\n");
    	//} else   {
    		
    	//		printk("Longarray not found, adding it as id %d\n",
    	//		       LONG_ID);
    	//		//for (int n = 0; n < sizeof(longarray); n++) {
    	//		//	longarray[n] = n;
    	//		//}
     //                       memset(&longarray,1,sizeof(longarray));
    	//		(void)nvs_write(
    	//			&fs, LONG_ID, &longarray, sizeof(longarray));
    		
    	//}
    
    
    	for (;;) {
    		if (is_adv) {
    			dk_set_led(ADV_STATUS_LED, (++blink_status) % 2);
    		} else {
    			dk_set_led_off(ADV_STATUS_LED);
    		}
    		k_sleep(K_MSEC(ADV_LED_BLINK_INTERVAL));
    		/* Battery level simulation */
    		bas_notify();
    	}
    }
    

    *** Booting Zephyr OS build v2.4.0-ncs2  ***
    Starting Bluetooth Peripheral HIDS keyboard example
     
    flash area : NRF_FLASH_DRV_NAME,0,5,f8000,12288 
    flash area sector : 0,4096 
    flash area sector : 4096,3,f8000I: 3 Sectors of 4096 bytes
    I: alloc wra: 0, ff0
    I: data wra: 0, 0
    
     rd - ID : 32768 ; D :  ; L:2
    I: SoftDevice Controller build revision: 
    I: cf 5c 0f 11 88 9c d7 02 |.\......
    I: 15 27 c7 c3 ca 60 19 85 |.'...`..
    I: b7 c4 50 e3             |..P.    
    I: HW Platform: Nordic Semiconductor (0x0002)
    I: HW Variant: nRF52x (0x0002)
    I: Firmware: Standard Bluetooth controller (0x00) Version 207.3932 Build 3617359889
    I: No ID address. App must call settings_load()
    Bluetooth initialized
    I: Identity: d2:a8:f9:27:ba:13 (random)
    I: HCI: version 5.2 (0x0b) revision 0x1123, manufacturer 0x0059
    I: LMP: version 5.2 (0x0b) subver 0x1123
    E: ***** MPU FAULT *****
    E:   Stacking error (context area might be not valid)
    E:   Data Access Violation
    E:   MMFAR Address: 0x20006728
    E: r0/a1:  0xf825df3a  r1/a2:  0x1acf0468  r2/a3:  0x00000000
    E: r3/a4:  0x0006d435 r12/ip:  0xffffffff r14/lr:  0x00003948
    E:  xpsr:  0x0000c600
    E: Faulting instruction address (r15/pc): 0x00003950
    E: >>> ZEPHYR FATAL ERROR 2: Stack overflow on CPU 0
    E: Current thread: 0x20002ed8 (unknown)
    E: Resetting system

Reply
  • As continuation of 2.

    I have added NVS example to ble_hid_keyboard

    only added address_id

    void main(void)
    {
    	int err;
    	int blink_status = 0;
    
    	printk("Starting Bluetooth Peripheral HIDS keyboard example\n");
    
    	configure_gpio();
    
    	bt_conn_cb_register(&conn_callbacks);
    	bt_conn_auth_cb_register(&conn_auth_callbacks);
    
    	err = bt_enable(NULL);
    	if (err) {
    		printk("Bluetooth init failed (err %d)\n", err);
    		return;
    	}
    
    	printk("Bluetooth initialized\n");
    
    	hid_init();
    
    	if (IS_ENABLED(CONFIG_SETTINGS)) {
    		settings_load();
    	}
    
    #if CONFIG_NFC_OOB_PAIRING
    	k_work_init(&adv_work, delayed_advertising_start);
    	app_nfc_init();
    #else
    	advertising_start();
    #endif
    
    	k_work_init(&pairing_work, pairing_process);
    
            const struct flash_area *fa;
    	struct flash_sector hw_flash_sector;
            uint16_t cnt = 0;
    	size_t nvs_sector_size, nvs_size = 0;
    	
    	uint32_t sector_cnt = 1;
    
    	/* define the nvs file system by settings with:
    	 *	sector_size equal to the pagesize,
    	 *	3 sectors
    	 *	starting at FLASH_AREA_OFFSET(storage)
    	 */
             err = flash_area_open(FLASH_AREA_ID(sensor), &fa);
    	if (err) {
    		//return err;
    	}
             printk(" \nflash area : %s,%x,%x,%x,%d",fa->fa_dev_name,fa->fa_device_id,fa->fa_id,fa->fa_off,fa->fa_size);
    	err = flash_area_get_sectors(FLASH_AREA_ID(storage), &sector_cnt,
    				    &hw_flash_sector);
             printk(" \nflash area sector : %d,%d",hw_flash_sector.fs_off,hw_flash_sector.fs_size);
    	if (err == -ENODEV) {
    		//return err;
    	} else if (err != 0 && err != -ENOMEM) {
    		k_panic();
    	}
    
    	nvs_sector_size = CONFIG_SETTINGS_NVS_SECTOR_SIZE_MULT *
    			  hw_flash_sector.fs_size;
    
    	if (nvs_sector_size > UINT16_MAX) {
    		//return -EDOM;
    	}
    
    	while (cnt < CONFIG_SETTINGS_NVS_SECTOR_COUNT) {
    		nvs_size += nvs_sector_size;
    		if (nvs_size > fa->fa_size) {
    			break;
    		}
    		cnt++;
    	}
    
          printk(" \nflash area sector : %d,%d,%x",nvs_sector_size,cnt,FLASH_AREA_OFFSET(storage));
          
        
             struct flash_pages_info info;
    	//fs.offset = FLASH_AREA_OFFSET(storage);
            fs.offset = fa->fa_off;
            printk("\n f_sec : %x",fs.offset);
    	int rc = flash_get_page_info_by_offs(
    		device_get_binding(DT_CHOSEN_ZEPHYR_FLASH_CONTROLLER_LABEL),
    		fs.offset, &info);
    	if (rc) {
    		printk("Unable to get page info");
    	}
            
    	fs.sector_size = nvs_sector_size;
    	fs.sector_count = cnt;
    
             rc = nvs_init(&fs, fa->fa_dev_name);
    	if (rc) {
    		printk("Flash Init failed\n");
    	}
            char buf[20];
            //uint32_t longarray[100];
            rc = nvs_read(&fs, ADDRESS_ID, &buf, sizeof(buf));
    	if (rc > 0) { /* item was found, show it */
    		printk("Id: %d, Address: %s\n", ADDRESS_ID, buf);
    	} else   {/* item was not found, add it */
    		strcpy(buf, "192.168.1.1");
    		printk("No address found, adding %s at id %d\n", buf,
    		       ADDRESS_ID);
    		(void)nvs_write(&fs, ADDRESS_ID, &buf, strlen(buf)+1);
    	}
     //       rc = nvs_read(&fs, LONG_ID, &longarray, sizeof(longarray));
    	//if (rc > 0) {
    	//	/* item was found, show it */
    	//	printk("Id: %d, Longarray: ", LONG_ID);
    	//	for (int n = 0; n < sizeof(longarray); n++) {
    	//		printk("%x ", longarray[n]);
    	//	}
    	//	printk("\n");
    	//} else   {
    		
    	//		printk("Longarray not found, adding it as id %d\n",
    	//		       LONG_ID);
    	//		//for (int n = 0; n < sizeof(longarray); n++) {
    	//		//	longarray[n] = n;
    	//		//}
     //                       memset(&longarray,1,sizeof(longarray));
    	//		(void)nvs_write(
    	//			&fs, LONG_ID, &longarray, sizeof(longarray));
    		
    	//}
    
    
    	for (;;) {
    		if (is_adv) {
    			dk_set_led(ADV_STATUS_LED, (++blink_status) % 2);
    		} else {
    			dk_set_led_off(ADV_STATUS_LED);
    		}
    		k_sleep(K_MSEC(ADV_LED_BLINK_INTERVAL));
    		/* Battery level simulation */
    		bas_notify();
    	}
    }
    

    *** Booting Zephyr OS build v2.4.0-ncs2  ***
    Starting Bluetooth Peripheral HIDS keyboard example
     
    flash area : NRF_FLASH_DRV_NAME,0,5,f8000,12288 
    flash area sector : 0,4096 
    flash area sector : 4096,3,f8000I: 3 Sectors of 4096 bytes
    I: alloc wra: 0, ff0
    I: data wra: 0, 0
    
     rd - ID : 32768 ; D :  ; L:2
    I: SoftDevice Controller build revision: 
    I: cf 5c 0f 11 88 9c d7 02 |.\......
    I: 15 27 c7 c3 ca 60 19 85 |.'...`..
    I: b7 c4 50 e3             |..P.    
    I: HW Platform: Nordic Semiconductor (0x0002)
    I: HW Variant: nRF52x (0x0002)
    I: Firmware: Standard Bluetooth controller (0x00) Version 207.3932 Build 3617359889
    I: No ID address. App must call settings_load()
    Bluetooth initialized
    I: Identity: d2:a8:f9:27:ba:13 (random)
    I: HCI: version 5.2 (0x0b) revision 0x1123, manufacturer 0x0059
    I: LMP: version 5.2 (0x0b) subver 0x1123
    E: ***** MPU FAULT *****
    E:   Stacking error (context area might be not valid)
    E:   Data Access Violation
    E:   MMFAR Address: 0x20006728
    E: r0/a1:  0xf825df3a  r1/a2:  0x1acf0468  r2/a3:  0x00000000
    E: r3/a4:  0x0006d435 r12/ip:  0xffffffff r14/lr:  0x00003948
    E:  xpsr:  0x0000c600
    E: Faulting instruction address (r15/pc): 0x00003950
    E: >>> ZEPHYR FATAL ERROR 2: Stack overflow on CPU 0
    E: Current thread: 0x20002ed8 (unknown)
    E: Resetting system

Children
No Data
Related