1
\$\begingroup\$

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?

\$\endgroup\$
  • 1
    \$\begingroup\$ Since you're not using GCC, have you tried declaring the storage const? \$\endgroup\$ – Ignacio Vazquez-Abrams Dec 19 '14 at 20:25
  • \$\begingroup\$ Isn't there some built in EEPROM you could use, or is there not enough? \$\endgroup\$ – Scott Seidman Dec 19 '14 at 20:39
  • \$\begingroup\$ @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. \$\endgroup\$ – brhans Dec 19 '14 at 20:57
  • \$\begingroup\$ @brhans You are correct, there are no PIC32 chips with EEPROM. When you have so much flash, why would you need EEPROM as well? \$\endgroup\$ – Majenko Dec 20 '14 at 0:06
  • \$\begingroup\$ @IgnacioVazquez-Abrams Point of interest: XC32 is actually GCC with some customizations. (gcc version 4.5.2 MPLAB XC32 Compiler v1.33 (Microchip Technology)) \$\endgroup\$ – Majenko Dec 20 '14 at 0:13
4
\$\begingroup\$

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'.

\$\endgroup\$
  • \$\begingroup\$ 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. \$\endgroup\$ – Matt Young Dec 19 '14 at 21:41
  • \$\begingroup\$ 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. \$\endgroup\$ – brhans Dec 19 '14 at 21:54
  • \$\begingroup\$ Sometimes you need to update config info. The "const" approach doesn't readily allow that. \$\endgroup\$ – Scott Seidman Dec 19 '14 at 22:49
  • \$\begingroup\$ 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. \$\endgroup\$ – brhans May 2 '17 at 15:31
3
\$\begingroup\$

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).

\$\endgroup\$
1
\$\begingroup\$

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)

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.