Flash Memory in MSP430

I want to read value stored on memory location 0x1000. how can i store that value in variable.

  • You can define a variable at this address. How to do it is different for each compiler. It is the same process that is used for generating all these registers, so you can check the msp430xxx.h files how it is done.

    The other, plain C way is to define a pointer variable.

    unsigned int * const myVarPtr = (unsigned int *)(0x1000);

    [...]
    x = *myVarPtr;
    [...]

    if the value in 0x1000 can suddenly change (either by hardware or through an interrupt function), then add a 'volatile' before the definition.

  • you can initialise a pointer at adress 0x0, and then increment it

      char *p; // On déclare un pointeur vers un char
      p = 0;                

      p += 0x1000;

     

    OR

    use the DEFC fonction

     

    DEFC(name,adress)

     

    DEFC(PTR,0x1000)

     

  • In reply to Guillaume FINISTERE:

    Guillaume FINISTERE
    you can initialise a pointer at adress 0x0, and then increment it


    Or use an index p[1000]. Both only work with char, since the index/advance stepping depends on the size of the pointer type. Which is 2 for short int *.

    Also, the newer MSPs don't like if you access an address where nothing is. It gives a vacant memory exception.

    Even more, if you use a constant pointer, the compiler can optimize it access to a direct address. If you use a pointer variable, indirect addressing is necessary, slowing things down and increasing code size. Not to mention the risk of accidentally overwriting/changing the pointer value which makes debugging more difficult.

    Guillaume FINISTERE
    char *p; // On déclare un pointeur vers un char
      p = 0;   


    This should give an 'assigning int to pinter' warning. Proper typecasting is required.

    Guillaume FINISTERE
    DEFC(name,adress)

    This is a compiler specific macro. Using it would break the code if you switch to a different compiler. On mspgcc, the corresponding macro is
    sfrb(name, address)
    for 'special function register byte'. There's an sfrw and sfra (430X) too.

    Those things should only be used in compiler specific header files, unless absolutely necessary.

  • In reply to Jens-Michael Gross:

    I am using

    flash_ptr= (unsigned int *) 0x1000;

    and incrementing this pointer each time when i write to location.

    can i use  value = &flash_ptr; to read value stored at flash

  • In reply to abhishek Sabnis:

    abhishek Sabnis
    can i use  value = &flash_ptr; to read value stored at flash

    No.

    & references the variable behind it. So &flash_ptr will give the address at which flash_ptr is stored. Its type would be unsigned int **, a pointer to a pointer to an unsigned int.

    Whyt you need it to dereference the pointer to get the value to which the pointer is pointing to. It is done with the * operator.

    value=*flash_ptr;
    or
    value= flash_ptr[0];
    which is equivalent and should (but not necessarily must) generate the same code (depending on compiler smartness) but will in any case have the same effect.

  • In reply to Jens-Michael Gross:

    thanks jens.

    I tried using value= *flash_ptr;

    but as i am incrementing flash_ptr after every write i am not able to retrive the value from flash as it reads to the very next location of flash.

  • In reply to abhishek Sabnis:

    Indeed, if flash_ptr doesn't point to the correct location, ou cannot read the correct values. That's obvious.

    Either you decrement it again (or set it back to start) before you read, or you use a second, independent pointer for reading. A pointer, unless declared static, can be changed everytime. So you can just assign it its start value again before you start reading.

    Alternatively, you keep flash_ptr at its start value and write and read by indexing to it:
    value = flash_ptr[x];
    where x is the index, starting with 0. Keep in mind that since flash_ptr is an int*, each index or each increment adds 2 to the address, since 2 is the size of the type it points to.

    However, are you sure you properly wrote the values at all? While you can enable writing to flash easily, you can only write '0' bits to flash. If there's already a '0'-Bit in this location, you cannot write it to '1' again.
    It's like the punched tape in a flexowriter. If you just overwrite it, you'll add more holes, but to close the ones which are already there, you'll have to apply a patch. For Flash, this means that you'll have to 'erase' the whole segment in which the data resides, setting a whole segment of 64 or 128 bytes (for info mem area) or even 512 bytes (program flash) back to 0xff.

  • In reply to Jens-Michael Gross:

    thanks jens.

    I am using  value = *(flash_ptr - 1); and it works for me. as every time i write to flash it gets incremented to write to next location if i decrement pointer by one i can read the recent value at that location