1

I am trying to get int variables to be stored into RAM of my STM32. I have whats below and I believe that is a start but I'm not entirely sure. I'm trying to basically do what a .word array such as array: .word 1,2,3,4,5,6,7,8,9,10,-1 is in assembly but in C and have it stored into the RAM. Any advice? As an example of what i'm looking for, I'm trying to do like 0x20000000 = min 0x20000004 = max 0x20000008 = avg etc... We also don't have access to stdlib

const int32_t final[9] = {min, max, avg, median, a, b, c, d, f};
Topus
  • 11
  • 2
  • 1
    I'd suggest reading up on C memory model + linker scripts and may answer your own question here. – Brian McFarland Apr 07 '22 at 02:55
  • What are you trying to achieve though? Is this data than needs to go in a specific place in order for hardware to access it? Something you have other C code operating on? Homework problem? – Brian McFarland Apr 07 '22 at 02:56
  • Is your compiler + toolchain (or the way you're using it) broken and not producing programs that correctly set up statically-initialized arrays? Or do you mean copied to *RAM* as opposed to having constants in flash, but still be able to use `const` in C? For that, maybe use `__attribute__((section(".data")))` or something? – Peter Cordes Apr 07 '22 at 02:58
  • It is a homework issue where im having to create a program to sort an array of grades, find certain values, then store them into the RAM I dont think it matters exactly where as long as the values are all one byte away from each other so showing like in 0x2000000 max min etc... Im just having trouble figuring out how to store it like it would a .word array in Assembly. We are using an eclipse IDE connected to STM32 hardware. – Topus Apr 07 '22 at 03:04
  • 2
    non-`const` C global arrays will be in RAM, and C arrays are always contiguous. Note that an `int32_t` is 4 bytes, not 1, so you'll need to use `int8_t` if you actually want then one byte away like you commented, unlike what you wrote in your question. – Peter Cordes Apr 07 '22 at 04:28
  • 2
    Do you also need the C array to be located at a specific absolute address? If so, maybe put it in a custom section with `__attribute__((section(".data.foo")))` and use a linker script to set the address for that section? Otherwise seems a simple duplicate of a Q&A like [Does C99 guarantee that arrays are contiguous?](https://stackoverflow.com/q/2832970) (although that asks about 2D arrays, the answer is still yes; it's even simpler for 1D arrays.) – Peter Cordes Apr 07 '22 at 04:33
  • If you look at compiler output for a C array, it will often compile to `.word 1,2,3` or whatever in `.data` or `.rodata`. [How to remove "noise" from GCC/clang assembly output?](https://stackoverflow.com/q/38552116) – Peter Cordes Apr 07 '22 at 04:33
  • This is pretty universal: [What resides in the different memory types of a microcontroller?](https://electronics.stackexchange.com/a/237759/6102) – Lundin Apr 07 '22 at 13:57

0 Answers0