0

I have declared a tydef struct as shown below.

#define START_ADDR 0xXXXXX
typedef struct{
     uint32_t checksum;
     uint16_t index[len];
} block;

I changed the memory allocation of block using the below statement:

block *value = (block*) START__ADDR;

I verified the change in memory allocation as well and no issue with it. Now i am trying to update the value of checksum using

value->checksum=0xa5a5a5a5;

But the value of checksum is 0x00000000 and not getting updated to 0xa5a5a5a5. Can anyone please tell me how can i change the value of checksum.

Thanks in advance.

Regards Vybhav

Lundin
  • 195,001
  • 40
  • 254
  • 396
  • Please post a [mcve] so we can reproduce the problem and help you debug it – user3629249 Mar 20 '19 at 04:23
  • 2
    If you are running above code on X86 processor then it's not possible to write to arbitrary memory address like `0xXXXXX`. Your code causes **UB** – Achal Mar 20 '19 at 04:43
  • @Achal I am running my code on ARM cortex M4 processor – Vybhav Jayaraman Mar 20 '19 at 04:45
  • 2
    As here [How to assign pointer address manually in C programming language?](https://stackoverflow.com/questions/4532000/how-to-assign-pointer-address-manually-in-c-programming-language) @crowder quoted "Arbitrary address must be some kind of "**well-known address**" and those are typically (though by no means always) **read-only** . – Achal Mar 20 '19 at 04:48
  • Where is this address, in RAM or flash or some other kind of memory? If in flash memory, then it's probable to that you need to use special methods to write into it, you cannot use simple assignment. – user694733 Mar 20 '19 at 08:25
  • @user694733 its in flash memory – Vybhav Jayaraman Mar 20 '19 at 08:30
  • In that case you should check the user manual of your processor to see what kind of flash memory programming interfaces it offers. – user694733 Mar 20 '19 at 08:45
  • @user694733 ,it provides 1MB of flash memory . I am using only 8KB of Flash memory dedicated for Application usage. What is the special method to write into flash ? – Vybhav Jayaraman Mar 20 '19 at 09:49
  • I don't know. It depends on your microcontroller model. Like I said, you need to check your user manual to see if it's possible, and how it's possible. – user694733 Mar 20 '19 at 10:09
  • You might browse the web pages of the flash vendor or your board vendor to find application notes regarding flash programming. Maybe the vendor provides some library as well. It depends on vendor, model and your usage of the chips. – Gerhardh Mar 20 '19 at 12:59

1 Answers1

2

You can't write to flash memory as if it was RAM and that's it. Flash being ROM. This would be why all your variables allocated in flash is/must be const qualified, meaning read-only.

It is possible to change flash and your part could have dedicated data flash/eeprom for this purpose. But writing to such parts of the memory isn't something a compiler will do for you. You need to write a flash programming driver yourself. As in, you have to study the flash programming part in the manual + app notes.

Lundin
  • 195,001
  • 40
  • 254
  • 396