Accessing RAM as if it was a FileHandle Question

Hi all,

I’m stumped as to how I can use fopen fclose ftell fread fseek etc to access a file I have placed in RAM. My application will only make use of this single file during it’s runtime (but the file can be swapped out and another file loaded into RAM from the PC (using fwflash or SAM-BA).

Does anyone know how I can do this? I can’t restructure the application too much, but I can replace fopen etc calls with mem_open, mem_read, etc if such functions were to exist. I’m not sure how I’d go about implementing them.

Help :slight_smile:

PS. if anyone feels inclined to go into how to access memory in RAM from a program that’s in ROM feel free…I’m not entirely clear on this. My target is the ARM7(ATMEL S256)

Cheers and thanks!

spideyfly:
Hi all,

I’m stumped as to how I can use fopen fclose ftell fread fseek etc to access a file I have placed in RAM. My application will only make use of this single file during it’s runtime (but the file can be swapped out and another file loaded into RAM from the PC (using fwflash or SAM-BA).

Does anyone know how I can do this? I can’t restructure the application too much, but I can replace fopen etc calls with mem_open, mem_read, etc if such functions were to exist. I’m not sure how I’d go about implementing them.

Help :slight_smile:

PS. if anyone feels inclined to go into how to access memory in RAM from a program that’s in ROM feel free…I’m not entirely clear on this. My target is the ARM7(ATMEL S256)

Cheers and thanks!

I can’t tell you how to do it with those library calls, but I have very often had to do what I think you are trying to do. I can give you some general pointers on how I have done it in the past. My last application that had to access a file RAM consisted of an application that had to parse a script that began at a specific location. The script was variable length, but it was guaranteed to be smaller in size than the RAM space set aside for it. Since the script was ASCII text, I just declared a byte pointer to the first location of the RAM set aside for the script. My application then accessed the script using array notation and began parsing beginning at that first RAM location and continuing until it found the end of the script or reached the maximum RAM size defined in the compiled code. The application just parsed the script line by line, executing the script instructions at the end of each line. In my case the script was loaded by the debugger using the EmbeddedICE DCC (Debug Communications Channel), but you can certainly implement other ways of getting file into RAM. As long as your RAM space is always going to start at the same location and be of a defined size, than you can hardcode the parameters into you FLASH based code. You just need some kind of EOF indicator in the data that you load into RAM, and be certain that the data is smaller than the RAM that you allocate.

–Dave

Thanks Dave,

I’ll keep those points in mind…hmm…still thinking of how to return the equivalent of file pointers and do the normal file IO stuff (Guess I’m gonna haveta bite the bullet and write functions for this…

btw The arm-gcc toolchain has fopen, fclose etc…how come I can’t use these? Of course I don’t have a filesystem…but what if I had some initramfs disk or something like that…of course I’m not using a linux kernel or anything…but how could I access something like that?

Cheers,

The gcc toolchain has functions for those calls because they are part of the Standard C spec. It’s up to you to put some code behind those functions so that you can pretend to have a file system. That’s a lot of complications you don’t need to scan a string in memory.

Any book on the C programming language will explain how to examine each character in a string.