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;
use the DEFC fonction
In reply to Guillaume FINISTERE:
Guillaume FINISTEREyou can initialise a pointer at adress 0x0, and then increment it
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 FINISTEREchar *p; // On déclare un pointeur vers un char p = 0;
This is a compiler specific macro. Using it would break the code if you switch to a different compiler. On mspgcc, the corresponding macro issfrb(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 Sabniscan i use value = &flash_ptr; to read value stored at flash
& 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;orvalue= flash_ptr;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.
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.
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.
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
All content and materials on this site are provided "as is". TI and its respective suppliers and providers of content make no representations about the suitability of these materials for any purpose and disclaim all warranties and conditions with regard to these materials, including but not limited to all implied warranties and conditions of merchantability, fitness for a particular purpose, title and non-infringement of any third party intellectual property right. TI and its respective suppliers and providers of content make no representations about the suitability of these materials for any purpose and disclaim all warranties and conditions with respect to these materials. No license, either express or implied, by estoppel or otherwise, is granted by TI. Use of the information on this site may require a license from a third party, or a license from TI.
TI is a global semiconductor design and manufacturing company. Innovate with 100,000+ analog ICs andembedded processors, along with software, tools and the industry’s largest sales/support staff.