There have been some discussions before about how to store data on the cortex which remains intact when power is removed. Some such threads are
https://vexforum.com/t/non-volatile-storage/19788/1&highlight=flash
https://vexforum.com/t/writing-to-text-files/21067/1
It’s been on my to do list for some to address this for EasyC, not much I can do about it for ROBOTC unfortunately as it’s too much of a closed system.
I have created a small library that allows storing user parameters, currently limited at 32 bytes, in an area of flash memory beyond that normally used for the EasyC firmware. The idea is to be able to store the robot’s configuration, for example, red or blue alliance, autonomous strategy and perhaps runtime error codes for post analysis, and not lose the data when power is removed. The library only has three functions, this is the header file.
#ifndef VEXFLASHLIB_H_
#define VEXFLASHLIB_H_
#define ERROR_WRITE (-1)
#define ERROR_WRITE_LIMIT (-2)
#define ERROR_ERASE (-3)
#define ERROR_ERASE_LIMIT (-4)
// Number of user parameter words
// Do not change !!
#define USER_PARAM_WORDS 8
// Structure to hold user parameters
typedef struct _user_param {
// storage for the NV data
unsigned char data[USER_PARAM_WORDS * 4];
// useful debug data
unsigned int offset;
void *addr;
} user_param;
// public functions
user_param *UserParamRead( void );
int UserParamWrite( user_param *u );
int UserParamInit( void );
#endif // VEXFLASHLIB_H_
The library uses one complete page of the flash memory for parameter storage even though only 32 bytes are available to the user. A flash page is 2048 bytes and is the smallest unit of flash memory that can be erased. The library implements some simple wear leveling and protection mechanisms.
-
Each time the parameters are written, a new section of the flash page is used, 256 bytes are reserved at the beginning of the page as a type of index, the page only needs to be erased after 56 save operations.
-
There is a hard limit of 32 save operations each time the cortex is powered.
-
There is a hard limit of one erase operation each time the cortex is powered.
A typical use of these functions would be as follows.
The cortex is powered.
Parameters are read from flash and the firmware configuration set. This could be anything you want but would typically be perhaps the defaults for a LCD menu system.
The user interacts with the robot by using the LCD or other methods, the user initiates saving new default parameters to flash.
I have not yet decided if I’m going to release this as source code or a pre-compiled library, if I release the source then the built in safeguards could be bypassed, I’m just looking for some feedback at this point.
If a new EasyC program is downloaded to the cortex all the parameters are currently erased, there are ways around this but I’m not planning on implementing them. The best solution would be for the EasyC loader to only erase the necessary area of flash for the program rather than everything each time new code is downloaded.