# PROGMEM - major feature requirement

**URL:** <https://community.particle.io/t/progmem-major-feature-requirement/3284>\
**Category:** uncategorized\
**Created:** [March 7, 2014, 2:20am UTC](https://community.particle.io/t/progmem-major-feature-requirement/3284 "2014-03-07T02:20:16Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![peekay123](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/peekay123/32/810_2.png) [@peekay123](https://community.particle.io/u/peekay123)\
**Post date:** [March 7, 2014, 2:20am UTC](https://community.particle.io/t/progmem-major-feature-requirement/3284/1 "2014-03-07T02:20:16Z")

</div>

Spark Team. At present, Spark does not have a PROGMEM variable modifier like arduino:

> The PROGMEM keyword is a variable modifier, it should be used only with the datatypes defined in pgmspace.h. It tells the compiler "put this information into flash memory", instead of into SRAM, where it would normally go.

This creates a situation where const and initialized variables data is stored in both flash _and_ RAM, using precious memory resources. It is crucial that this capability be added soon as important libraries and programs will be near impossible to port (eg u8glib). At present, I am unable to run my code on my Spark as I have hit the 16KB boundary on RAM usage, in large part due to this problem. Can you please let the community know when this will be "attacked" in one of your sprints?

---

<div class="post-metadata">

**Author:** ![BDub](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/bdub/32/5137_2.png) [@BDub](https://community.particle.io/u/BDub)\
**Post date:** [March 7, 2014, 2:35am UTC](https://community.particle.io/t/progmem-major-feature-requirement/3284/2 "2014-03-07T02:35:45Z")

</div>

> **[Fix for #include "avr/pgmspace.h"](https://community.particle.io/t/fix-for-include-avr-pgmspace-h/2269/7?redirected=true)**
>
> PROGMEM is only needed on AVR because the AVR has separate program memory and data memory spaces. On STM32, there is no need for PROGMEM because the memory space is unified. Declare the memory you wish to be placed only in flash as static const and...

I did some other testing before and found `const` to work great for ensuring the data structure you are defining is in FLASH and not RAM. I can’t find my post with test code at the moment. Not sure if `static` is needed, but might be a good idea to add it just to be explicit about things.

> <https://stackoverflow.com/questions/177437/what-does-const-static-mean-in-c-and-c>

A follow up question would be, can you think of a way to automatically redefine it so that it works for the STM32… I haven’t been able to. That is, create a macro called PROGMEM that does something to give us `static const` in the right place.

---

<div class="post-metadata">

**Author:** ![peekay123](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/peekay123/32/810_2.png) [@peekay123](https://community.particle.io/u/peekay123)\
**Post date:** [March 7, 2014, 3:31am UTC](https://community.particle.io/t/progmem-major-feature-requirement/3284/4 "2014-03-07T03:31:47Z")

</div>

Thanks BDub! I can’t definitively say that const works since my compile RAM sizes seem to say otherwise. I have tried static const and the compiler barks back at me. I will have to do more testing but something is not making sense. Part of the problem is the product of the compile is the core AND the user code and it does not disaggregate the portions of flash and RAM allocated to each. I has become clear, nonetheless that there is a 16KB RAM “wall” after which programs will no run.

If anything BDub, we need a clear understanding of how NOT to have stuff go to RAM and what resources are unequivocally left for the user. This is mega important for new users and advanced user planning a port or new code.

---

<div class="post-metadata">

**Author:** ![jjrosent](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/jjrosent/32/20816_2.png) [@jjrosent](https://community.particle.io/u/jjrosent)\
**Post date:** [April 30, 2014, 12:59pm UTC](https://community.particle.io/t/progmem-major-feature-requirement/3284/5 "2014-04-30T12:59:50Z")

</div>

I have a pull request in around this issue. Please take a look and if you think it best solves this problem comment! here (and there!) [https://github.com/spark/core-firmware/pull/187](https://github.com/spark/core-firmware/pull/187)

---

<div class="post-metadata">

**Author:** ![peekay123](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/peekay123/32/810_2.png) [@peekay123](https://community.particle.io/u/peekay123)\
**Post date:** [April 30, 2014, 6:37pm UTC](https://community.particle.io/t/progmem-major-feature-requirement/3284/6 "2014-04-30T18:37:26Z")

</div>

jjrosent, have you actually tried to compile your test code? In the web IDE it produces errors! You may want to reconsider your issue 😄

---

<div class="post-metadata">

**Author:** ![jjrosent](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/jjrosent/32/20816_2.png) [@jjrosent](https://community.particle.io/u/jjrosent)\
**Post date:** [May 1, 2014, 4:47am UTC](https://community.particle.io/t/progmem-major-feature-requirement/3284/7 "2014-05-01T04:47:55Z")

</div>

We got it all figured out! Remember it wont work in the web ide because my proposed changes arent in there yet 🙂

---

<div class="post-metadata">

**Author:** ![inof8or](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/inof8or/32/1040_2.png) [@inof8or](https://community.particle.io/u/inof8or)\
**Post date:** [May 29, 2014, 6:52pm UTC](https://community.particle.io/t/progmem-major-feature-requirement/3284/8 "2014-05-29T18:52:45Z")

</div>

Has anyone found a resolution to this issue yet?

---

<div class="post-metadata">

**Author:** ![bko](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/bko/32/20704_2.png) [@bko](https://community.particle.io/u/bko)\
**Post date:** [May 29, 2014, 7:06pm UTC](https://community.particle.io/t/progmem-major-feature-requirement/3284/9 "2014-05-29T19:06:15Z")

</div>

This pull request is in for both local builds and the web based IDE or CLI.

Are you having a specific problem?

These AVR specific macros do nothing on Spark since the right thing already happens generally speaking.

---

<div class="post-metadata">

**Author:** ![inof8or](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/inof8or/32/1040_2.png) [@inof8or](https://community.particle.io/u/inof8or)\
**Post date:** [May 29, 2014, 7:20pm UTC](https://community.particle.io/t/progmem-major-feature-requirement/3284/10 "2014-05-29T19:20:39Z")

</div>

Honestly I probably wont have a ram issue currently on the core with my [code](https://github.com/PaulBeaudet/ArduinoBraille/tree/sparkPort), but I get caried away pretty fast so i like to explictly put things I know to be constant in program flash. Bit confused as what is differant about the STM32. Still going to run out of ram if I throw enough at it right?

Basically what I’m trying to figure out is the semantics of variable assignment for the spark. The language in this thread kinda goes back and forth about it. Will defining all the progmem arrays as “static const” get things done?

---

<div class="post-metadata">

**Author:** ![bko](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/bko/32/20704_2.png) [@bko](https://community.particle.io/u/bko)\
**Post date:** [May 29, 2014, 8:31pm UTC](https://community.particle.io/t/progmem-major-feature-requirement/3284/11 "2014-05-29T20:31:26Z")

</div>

Yes, in my experience static const arrays are only in flash, not in RAM. You would think that the keyword static is not required since that is the default, but for some of my stuff it seemed to be required and it generally doesn’t hurt.

---

<div class="post-metadata">

**Author:** ![emr](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/emr/32/4680_2.png) [@emr](https://community.particle.io/u/emr)\
**Post date:** [February 26, 2015, 6:16pm UTC](https://community.particle.io/t/progmem-major-feature-requirement/3284/12 "2015-02-26T18:16:19Z")

</div>

Sorry to dig up this really old thread but I wasn’t able to find an answer to a related problem. @peekay123, I’m trying to use your u8glib library with the spark but as you noted in the first post, some of the arrays are being stored in RAM and that causes the core to run out of space. According to other threads, declaring the large arrays as static const should place them in Flash but that’s not what I’m seeing when compiling;

**Before incl. Large Array:**

```
text	data	bss	dec	hex
76096	1252	11912	89260	15

In a nutshell:
Flash used	77348 / 110592	69.9 %
RAM used	13164 / 20480	64.3 %

```

**After incl. Large Array**

```
text	data	bss	dec	hex
76144	1256	17832	95232	17400

In a nutshell:
Flash used	77400 / 110592	70.0 %
RAM used	19088 / 20480	93.2 

```

Are there any workarounds for this now? Searched through a few threads but wasn’t able to find anything.

---

<div class="post-metadata">

**Author:** ![ScruffR](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/scruffr/32/6952_2.png) [@ScruffR](https://community.particle.io/u/ScruffR)\
**Post date:** [February 26, 2015, 6:55pm UTC](https://community.particle.io/t/progmem-major-feature-requirement/3284/13 "2015-02-26T18:55:29Z")

</div>

I’m not sure why you see this behaviour, but this little test sketch does suggest it works as expected

```cpp
//uint8_t yy[2048] = "This is a rather large array";
//const uint8_t yy[2048] = "This is a rather large array";
static const uint8_t yy[2048] = "This is a rather large array";
uint8_t xx[2048];

void setup()
{
    xx[0] = 0;
}

void loop()
{
    for(int i = 0; i < 2048; i++)
      xx[i] = yy[i];
}

/*
uint8_t yy[2048] = "This is a rather large array";

Output of arm-none-eabi-size:

text	data	bss	dec	hex
75800	3272	13904	92976	16

In a nutshell:
Flash used	79072 / 110592	71.5 %
RAM used	17176 / 20480	83.9 %

-------------------------------------
const uint8_t yy[2048] = "This is a rather large array";

Output of arm-none-eabi-size:

text	data	bss	dec	hex
77848	1224	13904	92976	16

In a nutshell:
Flash used	79072 / 110592	71.5 %
RAM used	15128 / 20480	73.9 %

-------------------------------------

static const uint8_t yy[2048] = "This is a rather large array";

Output of arm-none-eabi-size:

text	data	bss	dec	hex
77848	1224	13904	92976	16

In a nutshell:
Flash used	79072 / 110592	71.5 %
RAM used	15128 / 20480	73.9 %
*/

```

What are the alterations between your Before and After results?

---

<div class="post-metadata">

**Author:** ![peekay123](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/peekay123/32/810_2.png) [@peekay123](https://community.particle.io/u/peekay123)\
**Post date:** [February 26, 2015, 10:51pm UTC](https://community.particle.io/t/progmem-major-feature-requirement/3284/14 "2015-02-26T22:51:02Z")

</div>

@ScruffR, @emr, you just need to declare “const” and “static” is not required except to limit the scope of the declaration 😄

---

<div class="post-metadata">

**Author:** ![bko](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/bko/32/20704_2.png) [@bko](https://community.particle.io/u/bko)\
**Post date:** [February 26, 2015, 11:09pm UTC](https://community.particle.io/t/progmem-major-feature-requirement/3284/15 "2015-02-26T23:09:07Z")

</div>

> [@peekay123](#):
>
> declare "const" and "static" is not required

I thought that too until I tried to have my included library have its constants in flash. I could not force them to be in flash without static in that case. As I said above, static is the default in C so it should not matter but it seemed to. Maybe the way you are compiling changes it somehow. I will try to test it again.

---

<div class="post-metadata">

**Author:** ![ScruffR](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/scruffr/32/6952_2.png) [@ScruffR](https://community.particle.io/u/ScruffR)\
**Post date:** [February 27, 2015, 8:17am UTC](https://community.particle.io/t/progmem-major-feature-requirement/3284/16 "2015-02-27T08:17:51Z")

</div>

@peekay123, as my attached results show in my test case it didn’t make any difference what so ever, but since @bko had mentioned his personal experiance in an earlier post, I just added it as a test case anyway.

I would actually love to see a code where it does make a difference to get to the bottom of how and why - just out of curiosity 😉

---

<div class="post-metadata">

**Author:** ![mdma](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/mdma/32/1924_2.png) [@mdma](https://community.particle.io/u/mdma)\
**Post date:** [February 27, 2015, 9:00am UTC](https://community.particle.io/t/progmem-major-feature-requirement/3284/17 "2015-02-27T09:00:14Z")

</div>

Pretty strange. `const` data are placed in `.rodata` (read only data) sections, and our linker script places these in sections of the flash memory. Only non-const data goes into the RAM data section.

If someone can send me some code to compile I’m happy to investigate.

---

<div class="post-metadata">

**Author:** ![emr](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/emr/32/4680_2.png) [@emr](https://community.particle.io/u/emr)\
**Post date:** [March 4, 2015, 4:26am UTC](https://community.particle.io/t/progmem-major-feature-requirement/3284/18 "2015-03-04T04:26:14Z")

</div>

Thanks everyone. Declaring const actually worked. There was more than one array that needed to be declared as const on my code…not sure how I missed that!

---

<div class="post-metadata">

**Author:** ![peekay123](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/peekay123/32/810_2.png) [@peekay123](https://community.particle.io/u/peekay123)\
**Post date:** [March 4, 2015, 1:20pm UTC](https://community.particle.io/t/progmem-major-feature-requirement/3284/19 "2015-03-04T13:20:30Z")

</div>

@emr, it would be great if you shared your u8glib code once its ported! 😉
