# Using PIC32 Flash for storage

Continuing along on my PIC32 adventure, and using my PIC32MX210F016B. I have some configuration data for another IC that I have to program over SPI. I want to store that confurgation data, about 2K in the PIC flash. I'm not to clear how to do that though. I was trying to find some documentation or example about it.

In TI land for an MSP430 I remember I had to specify the flash page I was going to use in the memory map and then CC studio kept erasing it every time I reloaded code.

I feel like since this is static maybe I can declare it in my code, I just don't want it to end up trying to take up 2k of ram.

Any advice or references on the best way to accomplish this?

• Since you're not using GCC, have you tried declaring the storage const? – Ignacio Vazquez-Abrams Dec 19 '14 at 20:25
• Isn't there some built in EEPROM you could use, or is there not enough? – Scott Seidman Dec 19 '14 at 20:39
• @ScottSeidman I don't believe there are any PIC32's with EEPROM. It even seems a bit scarce in the 16-bit PIC24's and dsPICs. – brhans Dec 19 '14 at 20:57
• @brhans You are correct, there are no PIC32 chips with EEPROM. When you have so much flash, why would you need EEPROM as well? – Majenko Dec 20 '14 at 0:06
• @IgnacioVazquez-Abrams Point of interest: XC32 is actually GCC with some customizations. (gcc version 4.5.2 MPLAB XC32 Compiler v1.33 (Microchip Technology)) – Majenko Dec 20 '14 at 0:13

You can declare your data in your code using the 'const' storage class to tell the compiler to place the variable in flash instead of RAM.

So something like:

const char array_of_stuff[2048] = {0, 1, 2, 3, 4, 5, ... , 2047};


or even

const struct {
int   thing0,
thing1;
char  smaller_thing,
lots_of_little_things[512];
etc ...
} interesting_stuff = {
123,
456,
'p',
{'h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd', .... },
etc ...
};


or, if your data is stored in something like an external .csv file

const char my_data[2048] = {
#include "my_data.csv"
};


Leaving out the 'const' keyword might at first seem to produce the same effect, but in fact what you'll have is a big area of initialised RAM which the compiler will arrange to have populated with all of your stuff at startup instead of locating your variable(s) in flash 'directly'.

• You can #include .csv files likes that? Does the data in the file have to be comma delimited? I've never seen that done before. – Matt Young Dec 19 '14 at 21:41
• Yes you can. The data has to be formatted in the same way as if you wrote it in the code as initialiser values - so .CSV is typically the most appropriate. – brhans Dec 19 '14 at 21:54
• Sometimes you need to update config info. The "const" approach doesn't readily allow that. – Scott Seidman Dec 19 '14 at 22:49
• It can if you know how to specify the memory section you store it in - and then also write functions to erase & rewrite that section. – brhans May 2 '17 at 15:31

If you want to manipulate the Flash in your program, then there are two things you can look at that might help you. Both are part of the chipKIT project: There is "EEPROM Emulation" (emulation of the Arduino EEPROM library using Flash).

https://github.com/chipKIT32/chipKIT32-MAX/tree/master/hardware/pic32/libraries/EEPROM

It relies on a section of Flash being set aside in the linker script (linker scripts are in https://github.com/chipKIT32/chipKIT32-MAX/tree/master/hardware/pic32/cores/pic32 ) and also uses a form of wear levelling to increase the life of your flash.

Secondly, for general Flash reading and writing, another library for the chipKIT project (written by me) is here: https://github.com/MajenkoLibraries/Flash

Feel free to take any code from that you fancy.

If you aren't going to be making any changes to the data, and it is fine to include it in the code, then declaring it const is the way to go. When const it will reside in Flash (more specifically the .rodata section) so will not take up any RAM space.

Unlike simpler MCUs the PIC32 has a single monolithic address space, and the Flash is directly mapped into that 4MB memory address space. Consequently the data in Flash can be directly accessed and doesn't require any special register manipulations or instructions. Simpler MCUs often copy the read-only Flash data into RAM to make accessing it easier (unless you specifically force it to do otherwise).

Microchip has an EEPROM emulation library for PIC32 that should do just what you need. http://www.microchip.com/stellent/idcplg?IdcService=SS_GET_PAGE&nodeId=2680&dDocName=en538000

"const" will address the problem, I think, so long as you don't ever need to change the data while running, but the EEPROM emulation should do all the tricks

(Haven't used the method myself, so I can't vouch for it)