This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Variable storage in memory of MSP430

Other Parts Discussed in Thread: MSP430F5338, ASH

Hi all,

I am using MSP430f5338. I have a basic questions as related to the variables and its storage memory in MSP430.

As we have have the two types of memory in MSP430 as Flash and RAM
a) Is CODE and FLASH memory is different types of memory? -> My understanding is both the memory are same.
b) Is RAM and STACK memory is different types of memory? -> My understanding is both the memory are same.
c) In where global variables are stored -> My understanding is the global variables are stored in Flash(CODE) memory. If a variable is stored flash in memory,
it can't be modified wright? But most of the times the global variables values are varied as per the program, how? Is my understanding is correct?
d) CONST variable are stored in CODE memory, wright?
e) Is volatile type variables are stored in memory? -> My understanding is volatile variable is not stored in any of the memory and during run this variable will be stored is STACK memory for processing, wright?
f) Is there any document available with the explanation of variables storage memory?

g) How he status registers values are updated, is it CONST VOLATILE type, since these status registers values are not modified by the programmed but it will be updated by uC, how?

  • Code segments are stored in flash memory.
    The stack is located in RAM.
    Global variables are stored in flash or RAM, depending on whether they are writable.
    "const" objects are stored in flash.
    The "volatile" modifier does not affect where objects are stored.
    Most registers can be modfied by software. Some of the other registers indeed are declared as "const volatile". That "const" affects only what software can do; the MCU itself just can return another value when software reads it again.
  • Thanks for the clarification. But my main objective is to understand where the variables and code are stored?
    1) Are ROM memory and FLASH memory and CODE memory same?
    2) Are RAM memory and DATA memory and STACK memory same?
    3) There are two types of memory in msp430 such as 256KB of Flash and 18KB of RAM. During the compiling in the ELF file defines the sections as .data,.bss, .stack,.const/.rodata,.text, my question is where these section are resides in msp430 memory?
    4) I understand the code will be stored in flash and during the run it will be copied to RAM and executed and stores in flash, is it correct?

    If any documents available to answers my questions, please share the that too.

    Thanks in advance
  • Most of the terms you asked came from the very old days. RAM stands for Random Access Memory as oppose to Sequential Access Memory (such as a Tape or Drum). There were computers with Drum to store everything. But RAM is much preferred as the Primary memory with Tape or Drum as Secondary memory. Compiler/Linker will try to put code, stack, data, and other things in RAM as much as possible. Flash memory is newer. It is also Random Access for read operation. Thus one can use it for code and constants directly. It is also used to store the initial values of RAM.

    1) Are ROM memory and FLASH memory and CODE memory same?

    Yes, more or less.

    2) Are RAM memory and DATA memory and STACK memory same?

    Yes, more or less.

    3) There are two types of memory in msp430 such as 256KB of Flash and 18KB of RAM. During the compiling in the ELF file defines the sections as .data,.bss, .stack,.const/.rodata,.text, my question is where these section are resides in msp430 memory?

    The linker has to be told where within the address range Flash and RAM are. It will then put the sections in the Flash or RAM appropriately.

    4) I understand the code will be stored in flash and during the run it will be copied to RAM and executed and stores in flash, is it correct?

    No. Code (.text) can be executed from Flash directly. So is .rodata. There is no need to copy it to RAM.
  • Hi all,

    As per the user guide fl,ash section "The flash memory is partitioned into main and information memory sections. There is no difference in the
    operation of the main and information memory sections. Code and data can be located in either section."
    so data memory will, reside in flash means, can assume data memory is is flash and the same can be used to store the global, and static variable also it can be modified during run time? Since the flash memory variables can't changed at run time and how it will be handled?
  • >Since the flash memory variables can't changed at run time and how it will be handled?
    As read-only variables which cannot be changed.
  • Prakash Balagangatharan said:

    As per the user guide fl,ash section "The flash memory is partitioned into main and information memory sections. There is no difference in the
    operation of the main and information memory sections. Code and data can be located in either section."
    so data memory will, reside in flash means, can assume data memory is is flash and the same can be used to store the global, and static variable also it can be modified during run time? Since the flash memory variables can't changed at run time and how it will be handled?

    Your are correct in that flash can't be modified during run-time. The sentence in the user guide only makes sense if it would read "Code and constant data", where constant data contains things like variables declared "const", string literals, etc.

        -- Anders Lindgren, IAR Systems

  • Hi,

    Could anyone explain the storage area of the below type of variables in MSP430.

    int a; - local variable declared inside a function

    int  b; -  Global variable 

    extern int b; - extern global variable

    static int c; - static variable declared inside a function

    static int d; - Global static variable

    const int e; -  constant variable declared inside a function 

    const int f; - Global constant variable 

    Thanks in advance

  • a: stack (SRAM)
    b: SRAM
    b: no storage (extern just references a variable defined elsewhere)
    c: SRAM
    d: SRAM
    e: stack
    f: flash

    Variables on the stack might be optimized away. In C++, f might be optimized away if its address is never taken.

  • Learning C language using forum which is not designated for C (learning) is not the best idea. There are plenty of C language learning resources on the internet and paper. C data types are explained even in wikipedia. There are many books either. For example this one :)

  • I guess you plan to use C and not Asm?
    In C you don't have to worry about this, Put const if you want to put it in Flash as read-only.
    Registers is part of ALU, C uses it to pass along parameters to function calls.
  • Prakash Balagangatharan said:

    Could anyone explain the storage area of the below type of variables in MSP430.

    int a; - local variable declared inside a function

    int  b; -  Global variable 

    extern int b; - extern global variable

    static int c; - static variable declared inside a function

    static int d; - Global static variable

    const int e; -  constant variable declared inside a function 

    const int f; - Global constant variable 

    There are a number of different kind of variables in a language like C.

    - "auto" variables only live for a short while. This include function parameters and (non-static) variables defined inside functions. They are temporarily stored on the processor stack, or in processor register, and only live from when a function starts until it returns.

    - A variable with "static storage duration" (this includes global, file-static, and function-static variables) have are assigned a location in memory where they live. You linker can generate a map file with the actual location they are assigned to.

    - A constant variable can be placed in some kind of read-only memory, or simply be optimized away. (In fact, in C++, it is recommended to use constant variables over preprocessor constants.)

    - Global variables should only be defined (i.e. without using "extern") in one source file (well, in one "compilation unit", if we want to be correct). All other source files that also should refer to the variables should declare it using "extern". Typically, the declaration is placed in a header file which is included by all relevant source files. If you don't define a variable in any source files, the linker will complain about a missing definition. If you define it in more than one, the linker will issue a "multiple definition" error.

    If you have the IAR tools, there is more information in the compiler manual about this.

        -- Anders Lindgren, IAR Systems

**Attention** This is a public forum