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.

Flash Memory RAM or ROM

Other Parts Discussed in Thread: MSP430F2274
I'm new to embedded field currently working with MSP430f2274. I wish to know how the codes are stored in the memory. I been reading mazidi book about embedded . I have a few doubts . Stack is internal section of RAM . What exactly does stack do? Flash is RAM or ROM?

RAM is a volatile memory which loses its contents when power is switched off 
ROM is non volatile . Our codes are stored here and constants are stored here ..

Take a LED a led on off program?

What are the functions of RAM and Flash?
Is flash a RAM or ROM

Flash is program memory which executes our program right?
Where is the C code we have written stored RAM or Flash?
What exactly does stack do here.
Lets say We write a delay function? Is it stored in Flash or RAM.

Can someone explain me the flow . Been keen to know this . Searched around counldn't find ..
  • Hello Vinot,

    Vinoth Ramalingam said:
    I wish to know how the codes are stored in the memory.

    Code is stored in flash memory. Flash isn't neither RAM nor ROM.

    ROM is a "Read Only Memory". You can write flash memory so it's not ROM. There is no ROM in most MSP devices.

    RAM needs to be refreshed so if you turn off the power you will lost your data. RAM is faster than flash so you can store there variables, adresses etc.

    Flash doesn't need to be refreshed. There is a code and it isn't erased after shut down.

    Vinoth Ramalingam said:
    Where is the C code we have written stored RAM or Flash?

    Flash doesn't store C code. Compiler translates code into instructions MCU can understand. MSP doesn't understand what "delay" means but for example this "delay" can be executed by instructions*:

     1. "Copy number 120 to register R12."

     2. "Decrement value in R12 register."

     3. "If value in R12 equals 0, jump next instruction."

     4. "Jump to instruction number 2."

     5.  .... (next instruction). 

    *I don't say "delay" is executed exactly in this way. MCUs of other brands have other instruction sets, different compilers can translate functions in different way etc.

    Anyway, suppose that:

    - copying number to register requires 2 cycles,

    - decrementing value in register requires 1 cycle,

    - jumping next instruction if value in register is 0 requires 3 cycles if jumping or 1 cycle if not,

    - jumping from instruction number 4 to instruction number 2 requires 4 cycels,

    then you can count number of cycles you delay is equal: 2+1*120+1*(120-1)+4*(120-1)+3=720.

    -----------

    Ok, you want to use delay function mentioned above, but it is possible there is a variable in R12 register that will be lost if we write "120" there. It's no problem because we have got a RAM. We can write current R12 register value in RAM and load it back after "delay".  It is related to stack but I wouldn't describe it  here.

    Vinoth Ramalingam said:
    Flash is program memory which executes our program right?

    Flash doesn't execute anything. There is a much more components in MSP. Please check the User's Guide and other files.

    Best regards,

    Mikolaj

  • What exactly does stack do?

    Mostly, the stack provides a convenient place to store and recover a memory address (RAM or flash) using the MSP430 "Call " and "RET" instructions.  Understand those two instructions, and you will understand the stack.

    Flash is RAM or ROM?  Flash is program memory which executes our program right?

    Flash stores the instructions of your program, and your program instructions  don't change very often.  RAM stores the data that your program manipulates, which DOES change often. 

    After being changed a few thousand times, Flash memory wears out.  RAM never wears out.   Because Flash wears out eventually when you write to it, you should think of it as "Read mostly" memory..

    Where is the C code we have written stored RAM or Flash?

    The majority of your C code will be stored in flash.  Your variables and things that change a lot are stored in RAM. 

    I hope some of that helps!  Keep exploring and reading, and your understanding will grow!

  • Vinoth Ramalingam said:
    I been reading mazidi book about embedded

    That is a totally inadequate description!

    There are several books with authors named "mazidi" - which one are you talking about?!

    This one, perhaps: http://www.8052.com/forum/read/175610 ?

  • Curt Carpenter said:
    The majority of your C code will be stored in flash

    Wrong.

    As  has already said, none of your 'C' code is stored anywhere in the microcontroller!

  • Forgive me, but our friend here is wondering what a stack is for, and you're splitting hairs?  Certainly a derivative of his C code is in the microcontroller.   You know that.

    So while I wouldn't call your comment Wrong, I do think I'd call it "Not very helpful."  

    And the object is to try to be helpful, isn't it?

  • I do understand C code code will converted to assembly code then to binary code . I just wanted to know where those binary will be placed? I believe the answer is FLASH. 

    Re STACK . Does stack store address during execution or before? Stack itself is a section of RAM no? It stores address of RAM? 

  • 8051 and embedded Systems bu mohammed mazidi 

  • Vinoth Ramalingam said:

    I do understand C code code will converted to assembly code then to binary code .

    I wrote it to avoid misunderstanding in the future.

    Stack is a block of memory in RAM but it isn't a hardware implementation. It is creating by software and may be placed "anywhere" in RAM.

    There is a special register SP (stack pointer) in MCU. It contains the address of the top of the stack. MCU architectures makes pushing (saving) or poping (loading) data into or onto the stack faster than recalling to RAM of any adrress. After PUSH instruction the stack pointer is automatically increment (or decrement*) and after POP instruction stack pointer is automatically decrement (or increment*).

    *Depends from architecture. In MSP430 SP is incremented for PUSH and decrementated for POP instruction.

    You can download "MSP430 family instruction set summary"  and try to understand asm code provided for MSP you use.

    Best regards,

    Mikolaj

  • Mikolaj Filar said:

    I wrote it to avoid misunderstanding in the future.

    Much Appreciated . 

    I read this document about STACK . It was helpful . http://eleceng.dit.ie/frank/msp430/msp430.pdf . Incase anyone need it . 

    I have another doubt. 

    The function of Program counter is to point to the next instruction to be executed right?

    Main()

    {

    int a=1,ba;

    ba=ba+1;

    txuart();

    .......

    }

    void txuart()

    {

    ...

    }

    PC keeps point pointing to next instruction. After int ba addition is executed . It points to txuart() subroutine. When it occurs next instruction is stored on the stack and it goes to subroutine. I just want to know does PC know the address of subroutine?? This part i want to know . 

    I know after finishing subroutine when RET instruction occurs . Microcontroller pops the address from the STACK to PC and keeps executing . I just want to know the middlepart how PC knows subroutine address... 

    Thanks to all the guys who helped me here about STACK esp  and  

  • Vinoth Ramalingam said:
    The function of Program counter is to point to the next instruction to be executed right?

    Yes.

    Vinoth Ramalingam said:
    When it occurs next instruction is stored on the stack and it goes to subroutine.

    Adrdess of the next instruction, of course.

    Vinoth Ramalingam said:
    I just want to know does PC know the address of subroutine?? This part i want to know .

    PC doesn't know anything beside it contains :P Compiller knows this address and it's enough. I'm not brave enough to go lower than assemler, but I can take a guess.

    Maybe CALL instruction is executed by 2 commands: "PUSH PC onto @SP"  and "JMP to subroutine addresss". I don't know. You can debug these 2 ways to entering the routine and count number of cycles.

    Maybe there is any special component which load the adress to PC?

  • I wish to know where data segment is implemented? Is it in RAM or ROM/Flash? 

    Our local variables are stored in stack which is implemented in RAM . 

    static , global variables are stored in data segmented is it implemnted in RAM? Is it so ? If will it lose contents when power is turned off?

    Heap is implemented in RAM or Flash?

  • Vinoth Ramalingam said:
    I wish to know where data segment is implemented? Is it in RAM or ROM/Flash? 

    Some data is storing in flash. You haven't any variables in RAM after switching on, so it isn't possible to store there constants or variables you will use.

    Vinoth Ramalingam said:
    static , global variables are stored in data segmented is it implemnted in RAM? Is it so ? If will it lose contents when power is turned off?

    When you have declared variable the first time:

    char my_number=1245;

    there is no other option than storing it in flash, but if you change this value in program it won't be saved. When you starting to operate on the variable you are doing it in RAM memory or in registers.

    char my_number=1245; can be implemented as "Move 1245 value to R15 register" and maybe in other situation it can be impleented in other way (because compiler can simplify something).

    You can check this document (slau132c- MSP430 Optimizing C/C++ Compiler v 3.1 User's Guide):

    http://www.ti.com.cn/general/cn/docs/lit/getliterature.tsp?literatureNumber=slau132c&fileType=pdf

    (for example page 79 shows assembled code declaration with pragma CODE_SECTION ).

    I recommend you to debug some applications and check different codes. Practice is the best way to learn.

    Best regards,

    Mikolaj

    edit.

    I see you double your questions. My suggestion: close this thread.

  • Thanks again . How do you find these documents . I keep searching in TI forum . I dont find any relevant documents . Any searching tips?

    Regards

    Vinoth

  • Some documents you can find here: http://www.ti.com/mcu/docs/mcuprodtechdoc.tsp?sectionId=95&viewType=mostuseful&tabId=1201&rootFamilyId=4&familyId=342&docCategoryId=2&docCategoryId=1&docCategoryId=6&docCategoryId=10&docCategoryId=11&docCategoryId=9&docCategoryId=3&docCategoryId=5

    Other sheets can be located on pages with MSP devices.

    You can find something on this forum or on other pages.

    And probably there is a lot of interesting documents hidden in abyss of hell you will never find.

    Best regards,

    Mikolaj

**Attention** This is a public forum