简体   繁体   中英

C: Read values of entire flash memory of MCU

I want to read the values of the memory locations of the entire program flash memory of an MCU, in particular, the CC2538 on the OpenMote-CC2538. The read values are then computed into, currently, a large sum of all the values.

At this moment, I have the following code working to traverse the memory and get the values

uint64_t readMemory() {

unsigned char * bytes = (char *) 0x200000;
size_t size = 0x0007FFD4;
size_t i;
uint64_t amount = 0;
for (i = 0; i < size; i++) {
    amount += bytes[i];
    }

return amount;
}

uint64_t readFlashMemory() {

unsigned int * bytes = (int *) 0x200000;
size_t size = 0x0007FFD4; 
size_t i;
uint64_t amount = 0;
for (i = 0; i < size; i+=4) {
    amount += FlashGet(bytes);
    bytes++;
    }

return amount;
}

address 0x200000 and its size is 0x0007FFD4. The first function works with a char and goes to each address one by one, while the second one uses an existing function FlashGet(uint32_t) from the flash.c file, which is a direct access to a register (HWREG).

FlashGet requires a uint32_t address and returns a uint32_t value, as such it has a length of 4 and the address should be moved with 4 in the loop .The first function uses char for the addressing, which is a length of 1 and so the address should also move by 1 in the loop. Am I correct in these statements? If so, am I executing them correctly? For the second function, incrementing the pointer with 1 should move it with 4 due to it being of type uint32_t (similar to int).

However, the functions return a different value. The first one returns: 674426297757 The second one returns: 8213668631160

As both functions should be doing the same, one or both must be incorrect and is not reading the entire program flash memory.

How can I fix both functions? Is there a better or easier way to read the entire memory when you have the starting address and size?

Consider you have a 4-byte flash memory with content

00 01 02 03

Adding by byte values will give you 0x000000000000006

Adding by 32-bit int values will give you 0x0000000003020100 assuming little-endian.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM