Showing results for 
Show  only  | Search instead for 
Did you mean: 
Registered: ‎04-29-2010

volatile vs disabling Cache


in my PL. I have developed an AXI Master memory-mapped interface, and the code works fine.

ARM is not involved in initiating the DMA, and it only receives an interrupt once the DMA is done.


I found some issue when both ARM and my DMA trying to access the same memory location. Let's say if I first write some data with FW to some memory location:



  // preload memory with some known data (just a test)
  for (k = 0; k < DATA_LENGTH; k++) {
    baseAddr[k] = k;

    printf( "memory addr: %p, value: %8lx\n\r", &baseAddr[k], baseAddr[k]);


  Then when I do a DMA to the same memory location, And when I try to read back the data, with a simple read operation:



  for (k = 0; k < 100; k++) {
	printf( "memory addr: %p, value: %8lx\n\r", &baseAddr[k], baseAddr[k]);


It doesn't work, and it will still report the old data that I had initially written and not the new data that I DMA into memory!!! 


And to some extent that made sense, because when I first wrote the data it cached the data, instead of writing it directly into memory, and when I read it back, it read back the Cached data, so that is why it didn't read back the new data.



What I thought would solve the issue was actually to use the keyword "volatile".... So, I decided to allocate part of the memory as volatile and then this should've taken care of the problem. So I did this:


   volatile uint32_t *baseAddr = malloc(DATA_LENGTH * sizeof(uint32_t));

    if(baseAddr == NULL)
    	printf("error: malloc failed \r\n");
    	return 1;


But this didn't work either !!!


And the only way that I was able to get it to work was to make sure that I am disabling the Cache memory, by doing this:


	 * Flush the Caches

	 * Disable Data Cache



So, how come using "Volatile" didn't work, and I had to explicitly disable the Cache memory !!!

If that is the case, then what is "Volatile" used for in embedded system design, and what is it good for?




0 Kudos
1 Reply
Registered: ‎08-14-2007

The "volatile" keyword just lets the compiler know that the value can change outside of your program.  Typically it's used for data that gets updated by an interrupt routine, but still by the processor.  It prevents compile-time optimization from optimizing away reads to locations that have been recently written and whose value (at write time) is still in a register.  Data that gets dumped directly into memory by external hardware still requires a cache flush.  Even using a volatile pointer, you would "re-read" the data from cache unless the cache has been flushed or overwritten.

-- Gabor
0 Kudos