[RV50] Write in non volatile memory

Hello,

I work on a RV50 and I’d like to use the flash memory to write data.

I try something which seems to work but datas aren’t saved if I run the program a second time.

In my main.lua :

print(mydata.file_exists())
	print("lancement fonction store")
	local writting_test = "Je suis un test d'écriture dans la mémoire flash RV50 \n"
	local writting_test2 = "Je suis un 2eme test d'écriture dans la mémoire flash RV50 \n"
	mydata.store(writting_test, string)
	mydata.store(writting_test2, string)
	mydata.read()
	print(mydata.file_exists())

I check if there is already the file in the memory, then I write 2 strings in the file and i check if the file is written.

In mydata.lua :

M.fichier_test = 'fichier_test_ecriture.txt'

-- Return true if the file "fichier_test" exists, false in the other case
function M.file_exists()
	local file,err = io.open(M.fichier_test, "rb")
	if file 	
		then file:close()
	end
	return file ~= nil
end

function M.store (buffer, string)
	
	print("Dans fonction store, nom du fichier :" .. M.fichier_test)
	local file, err = io.open(M.fichier_test,"a")
	if not file then -- Si le fichier_test n'existe pas ou n'a pas été ouvert
		print("Couldn't open " .. M.fichier_test .. ": err")
	else
		print("Ecriture dans le fichier :" .. M.fichier_test)
		file:write(buffer)
		--file:flush()		
		print("Fermeture du fichier après écriture")
		file:close()
		print("Fichier fermé")
	end
	print("End of function store")
end

function M.read()
	print("Dans fonction read, nom du fichier :" .. M.fichier_test)
	local file, err = io.open(M.fichier_test,"r")
	print("Lecture du fichier_test :")
	print(file:read("*a"))
end

Here what I have in the console :

So the .txt file is well written but if I run again the program the 1rst call “file_exists()” will return false.

I think the problem comes from the file location, when I launch the terminal files are in there :

Is there a way to write elsewhere in non volatile memory ? If yes, how ?
My file must be saved in case of power cut.

If you need other details, let me know.

Thanks,
Dylan

Hi Dylan,

When you run your App with the IDE (run as) it is run from /tmp, i.e. volatile memory, as you found out.
To run your app on a non volatile directory you have to ‘install’ it. The current working directory of an installed app is a non volatile memory.

However please note that non volatile memory is a flash device that wears. So it is important to not write too often in order to keep it in good shape. It is hard to give a strict rule as there are many parameters involved, but I would not recommend to write more often than once per minute. Also it is always better to write more data less often than less data more often :slight_smile:

If the next version of ALEOS (4.7) we actually provide a ‘cache’ table, that automatically write changes to a table after some delay occurred.
The API is:

utils.table.cache(store, timeout)
Cached table
Provide a RAM cache table.
Write changes are propagated to the store table after the specified timeouts.
Note: this require sched module to be enabled (i.e. require 'sched' before calling this function)
Note: tables values are written as is (i.e. not cloned) into the cache.
Note: only writing to the first level of the cached table will trigger a timed cache write. Writing to sub tables will not cause the cache timer write to be rearmed.
Parameters

store : table where the change are going to be written
timeout : is the maximum time the cache takes to write back (it can be less if there are several accesses)
Return value

the cached table

Oh I’m stupid I didn’t thought about that !
Thanks for those information :smiley:

By the way, I run a webpage on the RV50/LS300 and I wondered if I can use the memory to also store a picture (few Ko). I’d rather not to use url request to avoid data consumption.
(I suppose it’s possible but to be sure…)

Dylan

You can actually embedded any kind of file with your application package, but this is a bit tricky as the IDE only embeded Lua files.
The trick is to untar the tarball generated by the IDE, add the file to it and then retar and install the app.

Another workaround( but a bit ugly), is to rename your image file with the .lua suffix (picture.png.lua). This way the IDE will embedded it in your app. But the extension will not be correct, and do not try to open the file with the IDE, it will confuse it.

Hello,

I don’t know how to do that, do you have an example ?

Thanks,
Dylan

If you are under Windows you could use 7zip (http://www.7-zip.org/) tool to do that (a tar is similar to a zip).
Under Linux you could use the ‘tar’ command lien tool (tar -h to get the help)

An to generate the tarball with the IDE, when you ‘export’ the app, select a local directory rather than the device IP address.