Reputation: 1949
I am trying to print a piece of memory on a microcontroller.
It complaines about the line:
stringLength += sprintf(buffer + stringLength, "0x%X, ", *(startAddress + i));
due to the:
*(startAddress + i)
Why is this not working? Should it not just give me the value at the address?
Working on a ARM microcontroller and it jumps to HardFault_Handler() (which usually indicates illegal memory access).
How can I get the value at the memory location else wise?
Entire function:
void printMemory(uint32_t *startAddress, uint32_t lengthInBytes)
{
uint32_t stringLength = 0;
uint32_t i;
char buffer[1000];
uint32_t start = (uint32_t) startAddress;
for (i = start; i < (start + lengthInBytes); i++)
{
if ((i % 10) == 0)
{
stringLength += sprintf(buffer + stringLength, "\r\n");
}
stringLength += sprintf(buffer + stringLength, "0x%X, ", *(startAddress + i));
}
//Add line termination.
stringLength += sprintf(buffer + strlen(buffer), "%c", '\0');
//Printf buffer.
printf("%s", &buffer[0]);
}
EDIT:
Working example:
void printMemory(uint32_t startAddress, uint32_t lengthInBytes)
{
uint32_t i;
char buffer[1000];
uint32_t stringLength = 0;
uint8_t *start;
//Create a uint8_t pointer to address.
start = (uint8_t *) startAddress;
for (i = 0; i < lengthInBytes; i++)
{
if ((i % 10) == 0)
{
stringLength += sprintf(buffer + stringLength, "\r\n");
}
stringLength += sprintf(buffer + stringLength, "0x%X, ", *(start + i));
}
//Add line termination.
stringLength += sprintf(buffer + strlen(buffer), "%c", '\0');
//Printf buffer.
printf("%s", &buffer[0]);
}
Upvotes: 0
Views: 64
Reputation: 10251
You made a mistake:
i
starts at start
which is startAddress
, so at the first iteration you are fetching:
*(startAddress + startAddress)
Upvotes: 3