Difference between revisions of "NonVolatileMemmory4Apss"

From Nutwiki
Jump to: navigation, search
(Types of memory available)
 
m (1 revision imported)
 
(No difference)

Latest revision as of 18:02, 27 October 2016

NutOS provides NutNvMemLoad and NutNvMemSave functions to load/store data in a non volatile memory. Nut/OS uses these functions to store its own configuration parameters, but they might also be used by other applications to store their own data.

Types of memory available

The NutNvMem* functions will use the appropriate memory device depending on the platform used. In case of Ethernut 2 the EEPROM provided by the ATmega128 is the memory used to store the data. In case of Ethernut 3 we have 2 options, the EEPROM of the x12 and the AT49Bv flash. Which one is used can be selected through the configurator, in "Device Drivers->X12XX Driver->System Configuration" and "Device Drivers->AT49BV Flash Memory->System Configuration" respectively (Note that even if both can be selected simultaneously only one will be used, the X12). Selecting one or the other will define the macros NUT_CONFIG_X12RTC and NUT_CONFIG_AT49BV respectively that will be available from (defined in) in include/cfg/eeprom.h

By default Ethernut 3 uses the X12 (check the ethernut*.conf file used for your board!)

What addresses does Nut/OS use to stores its configuration data?

Whatever memory you decide to use, the Nut/OS and Nut/Net configuration parameters (CONFOS and CONFNET structures) will be stored in addresses with offsets defined by the CONFOS_EE_OFFSET and CONFNET_EE_OFFSET macros, both configurable through the configurator ("RTOS Kernel->Configuration->Location" and "Network(general)->Network Configuration->Location") and available from (defined in) include/cfg/eeprom.h. If EEPROM is used (either from the ATmega128 or the X12), the base address to where the offsets are added is 0, so the starting address used to store the Nut/OS and Nut/Net config data will be CONFOS_EE_OFFSET and CONFNET_EE_OFFSET respectively.

If the AT49BV in Ethernut 3 is used then there is yet another issue to take into account, being that the same memory (most likely) is used to store the program. If you are sure that you will not use all the 4Mb for it, you can use for example one sector (or more) for storing application data. In this sense the configurator lets you choose which sector to start writing the data to, in "Device Drivers->AT49BV Flash Memory->Configuration Section Address", and defines the macro FLASH_CONF_SECTOR in include/cfg/memory.h. So the real starting address used to store the Nut/OS and Nut/Net config data in the AT49BV would be FLASH_CONF_SECTOR+CONFOS_EE_OFFSET and FLASH_CONF_SECTOR+CONFNET_EE_OFFSET respectively. However, the NutNvMem* functions will use the sector defined by FLASH_CONF_SECTOR as the origin of addresses, so FLASH_CONF_SECTOR should not be used to specify the addresses when NutNvMem* functions are used. For example, the NutLoadConfig() function used to load the NutOS config data calls NutNvMemLoad as follows <source lang="c"> ... NutNvMemLoad(CONFOS_EE_OFFSET, &confos, sizeof(CONFOS)) ... </source>

What addresses can I use to store my application data?

The simple answer is: put your data after the Nut/OS and Nut/Net config data. The size of the CONFOS and CONFNET structures that are used to store the Nut/OS and Nut/Net configuration can be calculated with

<source lang="c">

  1. include <sys/confos.h>
  2. include <sys/confnet.h>

... sizeof(CONFOS); sizeof(CONFNET); ... </source>

and the addresses that NutOS uses to store its own data are the ones explained before. So you just want to not to write on top of them (neither on top of your program if you use the AT49BV in Ethernut3). The offset addresses for NutOS and NutNet parameters don't change often, neither the size of the CONFOS and CONFNET parameters, so using a fixed offset address for the application data start address (of about 512 bytes) is usually done and considered more or less safe. However you might also want to be sure 100% and check at compile time. For example, you could use the following code that leaves a margin of 100 bytes between them and your application data.

<source lang="c">

  1. include <sys/confos.h>
  2. include <sys/confnet.h>

...

  1. define SAVE_MARGIN 100
  2. if (CONFOS_EE_OFFSET < CONFNET_EE_OFFSET)

const unsigned int APP_DATA_START_ADDRESS=CONFNET_EE_OFFSET+sizeof(CONFNET)+SAVE_MARGIN;

  1. elif

const unsigned int APP_DATA_START_ADDRESS=CONFOS_EE_OFFSET+sizeof(CONFOS)+SAVE_MARGIN;

  1. endif

</source>

Then use APP_DATA_START_ADDRESS as the start address to store data application.