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

flashwrite operation does not work under specific circumstances

Hi,

I've been trying to read/write data from/to the internal flash storage of my nRF52 development board.

I am using SDK11 and softdevice s132

I have modified the flashwrite main function as follows-

int main(void)
{
    uint32_t * addr;
    uint8_t    patwr;
    uint8_t    patrd;
    uint8_t    patold;
    uint32_t   i;
    uint32_t   pg_size;
    uint32_t   pg_num;

    LEDS_CONFIGURE(BSP_LED_0_MASK);
	LEDS_CONFIGURE(BSP_LED_1_MASK);
	LEDS_OFF(BSP_LED_0_MASK);
	LEDS_OFF(BSP_LED_1_MASK);
	
    pg_size = NRF_FICR->CODEPAGESIZE;
    pg_num  = NRF_FICR->CODESIZE - 1;  // Use last page in flash

        // Start address:
        addr = (uint32_t *)(pg_size * pg_num);
        // Erase page:
        flash_page_erase(addr);
		
		patwr=0x45;
		flash_word_write(addr, (uint32_t)patwr);
		//patrd = (uint8_t)*(addr);     //gave same result as memcpy
		memcpy(&patrd,addr,1);
		uint8_t flag=4;
		if(patrd==0x45){
			flag=1;
			LEDS_ON(BSP_LED_0_MASK);
		}
		else{
			flag=0;
			LEDS_ON(BSP_LED_1_MASK); 
		}
}

It initially showed the LED1 (else case) to evaluated.

When i followed execution using debug, it resulted in the correct LED (LED0) being turned on but when it reaches the end of the main loop it goes into "Systeminit()" and returns to main. On this 2nd execution, write action did not occur and hence LED1 got triggered. I don't know the reason for this or if I should be bothered about it.

I actually intend for this operation to be performed on a central+peripheral BLE device but when i tried using the page erase+write parts of the above snippet, it caused a runtime error which i could not identify with keil. 

For clarification, I have this flash write operation set to occur when the "Disconnect event" occurs and after advertising and scanning have been stopped.It occurs before scan/advertising are restarted. I observed with debug that when flash code is included after sd_ble_gap_adv_stop(); it undergoes the runtime error in this function and when the flash code is absent there is no error thrown at this point.

Any solution for this would be appreciated.

Thank you

Parents
  • Is flash_word_write() a function that you have written? In that case, it would be useful to see what it looks like,

     

    Usually, issues regarding flash operation errors that a flash operation is not finished before starting the next one. For all I know, you do this in flash_word_write(), but I don't know. If it is a function from the SDK, could you please specify what SDK you are using?

     

    If would recommend you to check out the flash_fds example (at least what it is called in SDK15).

    There you will see that there is an event handler in main.c called fds_evt_handler(). This is used to set flags telling the application that the previous operation is done, so that it can start the next operation. In the main() function you can see that it calls:

    rc = fds_init();
    APP_ERORR_CHECK(rc);

    wait_for_fds_ready();

    ...

     

    Best regards,

    Edvin

  • I'm using functions flash_word_write() and flash_page_erase() from the flashwrite example of SDK11 under peripheral examples.

    I am currently testing using nRF52 and keil uvision5, however the actual implementation will be performed on a custom nRF51 board. Thus I haven't used the higher SDKs since i beileve they don't support nRF51 anymore.

    I did find an FDS example online for SDK11 but when i try to use snippets from that in my code, i'm not able to add the dependencies for the dependencies properly (I'm inexperienced with keil). For eg-in the example i found online a dependency is like this -

    but when i try adding fds.c using <add existing files to group> i get something like this-

    This results in compile time errors for fds_config.h and fds_internal_defs.h because i'm not able to make them come directly under fds.c but they are defined within fds.c

    If you could help me figure out either FDS or flashwrite issue, my problem would be solved.

    Thanks

  • Hello,

    You are right. The later SDKs does not support nRF51. However, the SDKs up to SDK12.3.0 does support nRF51, so if you are in the beginning of your development, I would recommend that you use this version. One of the reasons is the logging module that is included in this SDK, which makes development a bit easier.

     

    If you want to use the FDS library, I suggest that you check out this site (there is an equivalent for SDK11 here).

     

    In addition, you should wait for a process to finish before moving on, when you are doing flash operations. E.g. a flash write takes some time, so if you call flash read right after, it will fail.

    If I remember correctly, you must also add a function to your event dispatcher to get the events. Please see this thread.

     

    Best regards,

    Edvin

  • Yes but I am unable to add the necessary dependencies in my uvision project as described in the previous comment.That's the reason why i resorted to flashwrite. I am unable to add dependencies under fds.c which gives errors during compilation saying certain header files are missing (even though i have them as dependencies, just not under fds.c)

    Since development is almost completed, I doubt it will be easy for me change SDKs at this point.

    I am actually trying to use the flash r/w code in the following way-

    case BLE_GAP_EVT_DISCONNECTED:
                err_code = bsp_indication_set(BSP_INDICATE_IDLE);
                APP_ERROR_CHECK(err_code);
                m_conn_handle = BLE_CONN_HANDLE_INVALID;
    			sd_ble_gap_adv_stop();
    			sd_ble_gap_scan_stop();
    			NRF_UART0->TASKS_STOPTX = 1;
    			NRF_UART0->TASKS_STOPRX = 1;
    			NRF_UART0->ENABLE = 0;
    					
    			uint32_t * addr;
                uint8_t    patwr;
                uint8_t    patrd;
    
          
                uint32_t   pg_size;
                uint32_t   pg_num;
    
                pg_size = NRF_FICR->CODEPAGESIZE;
                pg_num  = NRF_FICR->CODESIZE - 1;  // Use last page in flash
    
        
                // Start address:
                addr = (uint32_t *)(pg_size * pg_num);
                // Erase page:
                flash_page_erase(addr);
    		    patwr=0x45;
    		    flash_word_write(addr, (uint32_t)patwr);
    		    memcpy(&patrd,addr,1);
    	        //patrd = (uint8_t)*(addr);
    	    	uint8_t flag=4;
    		    if(patrd==0x45){
    		    	flag=1;
    		    	LEDS_ON(BSP_LED_0_MASK);
    	        }
    		    else{
    			    flag=0;
    			    LEDS_ON(BSP_LED_1_MASK); 
    			}
    	    	
    	    	err_code = ble_advertising_start(BLE_ADV_MODE_FAST);
    			APP_ERROR_CHECK(err_code);
    		
    			//Start scanning for peripherals which advertise. 
    			scan_start();
                break;

    This results in a kind of runtime error causing all LEDs to light up and freezing the device. I observed using debug mode that this occurs at sd_ble_gap_adv_stop(); when the flash code is present but does not occur when the flash code is absent. This is confusing because the flash part of the code appears after the sd_ble_gap_adv_stop(); and I'm assuming if the code is executed sequentially there shouldn't be any difference at that step.

    So i need help in either getting flashwrite or the dependencies for FDS to work

    Thank you

Reply
  • Yes but I am unable to add the necessary dependencies in my uvision project as described in the previous comment.That's the reason why i resorted to flashwrite. I am unable to add dependencies under fds.c which gives errors during compilation saying certain header files are missing (even though i have them as dependencies, just not under fds.c)

    Since development is almost completed, I doubt it will be easy for me change SDKs at this point.

    I am actually trying to use the flash r/w code in the following way-

    case BLE_GAP_EVT_DISCONNECTED:
                err_code = bsp_indication_set(BSP_INDICATE_IDLE);
                APP_ERROR_CHECK(err_code);
                m_conn_handle = BLE_CONN_HANDLE_INVALID;
    			sd_ble_gap_adv_stop();
    			sd_ble_gap_scan_stop();
    			NRF_UART0->TASKS_STOPTX = 1;
    			NRF_UART0->TASKS_STOPRX = 1;
    			NRF_UART0->ENABLE = 0;
    					
    			uint32_t * addr;
                uint8_t    patwr;
                uint8_t    patrd;
    
          
                uint32_t   pg_size;
                uint32_t   pg_num;
    
                pg_size = NRF_FICR->CODEPAGESIZE;
                pg_num  = NRF_FICR->CODESIZE - 1;  // Use last page in flash
    
        
                // Start address:
                addr = (uint32_t *)(pg_size * pg_num);
                // Erase page:
                flash_page_erase(addr);
    		    patwr=0x45;
    		    flash_word_write(addr, (uint32_t)patwr);
    		    memcpy(&patrd,addr,1);
    	        //patrd = (uint8_t)*(addr);
    	    	uint8_t flag=4;
    		    if(patrd==0x45){
    		    	flag=1;
    		    	LEDS_ON(BSP_LED_0_MASK);
    	        }
    		    else{
    			    flag=0;
    			    LEDS_ON(BSP_LED_1_MASK); 
    			}
    	    	
    	    	err_code = ble_advertising_start(BLE_ADV_MODE_FAST);
    			APP_ERROR_CHECK(err_code);
    		
    			//Start scanning for peripherals which advertise. 
    			scan_start();
                break;

    This results in a kind of runtime error causing all LEDs to light up and freezing the device. I observed using debug mode that this occurs at sd_ble_gap_adv_stop(); when the flash code is present but does not occur when the flash code is absent. This is confusing because the flash part of the code appears after the sd_ble_gap_adv_stop(); and I'm assuming if the code is executed sequentially there shouldn't be any difference at that step.

    So i need help in either getting flashwrite or the dependencies for FDS to work

    Thank you

Children
  • Hello,

    Can you try to put err_code =  in front of your sd_ calls?

     

    err_code = sd_ble_gap_adv_stop();
    if (err_code != NRF_ERROR_INVALID_STATE)
    {
        APP_ERROR_CHECK(err_code);
    }
    err_code = sd_ble_gap_scan_stop();
    if (err_code != NRF_ERROR_INVALID_STATE)
    {
        APP_ERROR_CHECK(err_code);
    }

     

    See if any of those APP_ERROR_CHECKS() are hit if you debug. Note that if you set a breakpoint in the code while in a connection or advertising/scanning, it will hardfault after that, because the softdevice will miss some time critical events.

     

    I also saw that the flash_page_erase() and flash_word_write() functions from SDK11 does the waiting for you. If these functions are executed, but does not finish, can you please try to set some breakpoints inside them to see if you can figure out what event that it is waiting for which does not occur?

     

    Regarding including fds.c:

    I believe that the problem just is that it is missing some header files or .c files. Can you please try to add fds.c and fstorage.c, and make sure that the following paths are included in your projects "include paths" settings:

    ..\..\..\..\..\components\libraries\fds
    ..\..\..\..\..\components\libraries\fds\config
    ..\..\..\..\..\components\libraries\fstorage
    ..\..\..\..\..\components\libraries\fstorage\config
    ..\..\..\..\..\components\libraries\experimental_section_vars

     

    If that doesn't work, what sort of compiler errors do you get?

     

    Best regards,

    Edvin

Related