Author Topic: Directly accessing FLASH memory in the simulator  (Read 3073 times)

Offline FAQ

  • Newbie
  • *
  • Posts: 26
    • View Profile
Directly accessing FLASH memory in the simulator
« on: November 08, 2009, 11:11:09 PM »
Although I can open and access files via the uFileSystem in the simulator (eg. filePointer = uOpenFile("test_file.txt"); followed by fnGetParsFile()) any attempt to directly access the FLASH in the simulator causes an exception (eg. uMemcpy(buffer, filePointer, 10); or ucByte = *filePointer;).

On the target it works correctly...

Why is this and how can one work directly with data in FLASH when simulating?

Offline mark

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3158
    • View Profile
    • uTasker
Re: Directly accessing FLASH memory in the simulator
« Reply #1 on: November 08, 2009, 11:19:10 PM »

Assuming that the pointer to FLASH memory has a value of 0x10000 there is no problem with directly accessing this on teh target since it is normal memory-mapped memory. However when simulating an attempt to access such an address will usually result in an exception since this memory range either doesn't exist or is protected. The simulator uses a simulated FLASH memory area which will be anywhere in PC memory so to test code that access it directly an intermediate mapping call is required as shown below:

The mapping functions are called fnGetFlashAdd() and fnPutFlashAdd()

These functions are zero defines for the target but convert between real FLASH addresses and simulated FLASH addresses when simulating.

Therefore the command
ucByte = *filePointer; // crash on simulator

needs to be written as
ucByte = *fnGetFlashAdd(filePointer);  // works on simulator AND target

Sometimes you need an extra cast since it passes *unsigned char and returns *unsigned char but that is a detail.

You usually only need fnPutFlashAdd() when you actually 'carry' the converted value Eg.
unsigned char *ptrFlash = fnGetFlashAdd(BasicFilePtr); // the pointer is valid for simulation but is NOT a real FLASH address...

This is OK for reads in the simulator but not of any use when doing something like.
if (ptrFlash == (unsigned char *)0x10000) // will work on target but NOT simulator..

To put back to real address
if (fnPutFlashAdd(ptrFlash) == (unsigned char *)0x10000)

Therefore it is best to try to not 'carry' a converted value but always use the fnGetFlasgAdd() conversion only where used.