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

How to handle flashwrit in an safe way.

Hi,

i need to store data in my Flash, for that i use the flashwrite example. Am i right taht i can use the remain of my application flash? So all flash = 256 kB - App = 20 kB = Flash i can use to write my Data to ?

I read that if i use flashwrite and the Softdevie, that i have to halt the Softdevie becauose of the latncy of flahswriting is that right? How can i handle this in a safe way?

Best reagrds Nils

  • There are two banks for Flash. One bank (80KB) is set aside for the S110 Softdevice and the other is available for your consumption (app and as data storage).

    Yes, you have to the Flash in a Softdevice 'friendly' manner. There're some vague clues in the HRM app, if I recall correctly.

    -m

  • All of the flash can be used both for code and for data, but you do of course have to make sure that you don't try to write data to the areas occupied by the softdevice or the application. Depending on application size, the upper pages should be free, except for the ones used by the bond manager.

    Flash writing and erasing will halt the CPU, and for that reason, you must make sure to not do it when the softdevice needs the CPU. This means that you must do it between connection events (or advertising events). The softdevice provides a radio notification feature to allow you to know when a radio event is finished. At that time, you can do flash operations for a period equal to the connection interval*(slave latency-1) - some ms. Note that erasing a flash page takes typical 21 ms, so if you have a short connection interval, that might not be possible without slave latency. Also, if you have queued data to be sent, for example by sending a notification, the softdevice might need to wake up even if you do have slave latency.

    You can take a look at the ble_radio_notification module in the SDK for more information on how the radio notification feature can be used. Also, there are some pointers given in different questions here on the developer zone, so I'd recommend you to do a few searches. Let me know if you have specific problems.

  • Hi Ole,

    ok i understand, but in my case i don't need the softdevice so i write to the flash when the softdevie is not needed, so how can i stop the softdevice?

    In the exampkle of the sdk there is used this: 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); So is this safe for me, am i writing then in the flash area over my application and the softdevie?

    And in the Code the use "page_size" what is ment by this, i dont't realy understand what they mean with pages in the flash, is there any Information about that?

    Thanks for your help :)

  • I Think my main problem is that i don#t understand what in the sdk example happens. I only need to safe some data in free flash and i need to knwo wher it begins and where it and. Maybe you can explain me how the address in the example is calculated. And is this static, so could i define this addres as beginning address?

  • Flash is normally divided into parts, called pages. On the nRF51822, a page is a 1024 byte area of the flash, so a 256 kB version of the chip will have NRF_FICR->CODESIZE = 0x100 = 256.

    The example snippet you show takes this number, subtracts 1, and multiplies it with the page size in bytes. This will give you the start address of the last page, which is then written to.

    There isn't any magic way to know if a page is used by the application, you simply have to use your application size to calculate which pages are used. In addition, the bond manager and possibly the error handler also writes to flash, so you should avoid those pages as well. If a flash address contains only 0xFFs, this normally indicates that it is not written. You can inspect any address using either J-Link Commander, nrfjprog or Keil's debugger.

Related