<div dir="ltr"><div dir="ltr">I think I understand what SHOULD work, but it doesn't!!<div><br></div><div>JPEG encode occurs in 3 (4) stages</div><div><ol><li style="margin-left:15px">prepare first block bitmap data by converting to YCbCr. Do a SCB_CleanDCache_by_Addr() cache flush to make sure it's all in RAM and not cache and accessible to DMA</li><li style="margin-left:15px">after processing, flush the cache with SCB_InvalidateDCache_by_Addr() to ensure the data read not stale before writing to filesystem <br></li><li style="margin-left:15px">prepare the next YCbCrblock and like (1) flush the cache to make sure its all accessible to DMA</li><li style="margin-left:15px">repeat 2-3 until all bitmap data done</li></ol>Now I'm really stuck (other than random rearrangements of the code)</div><div class="gmail-yj6qo gmail-ajU" style="outline:none;padding:10px 0px;width:22px;margin:2px 0px 0px"><br class="gmail-Apple-interchange-newline"></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Tue, Feb 8, 2022 at 1:38 PM Robin Gilks <<a href="mailto:gb7ipd@gmail.com">gb7ipd@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr">I think I understand what SHOULD work, but it doesn't!!<div><br></div><div>JPEG encode occurs in 3 (4) stages</div><div><ol><li>prepare first block bitmap data by converting to YCbCr. Do a SCB_CleanDCache_by_Addr() cache flush to make sure it's all in RAM and not cache and accessible to DMA</li><li>after processing, flush the cache with SCB_InvalidateDCache_by_Addr() to ensure the data read not stale before writing to filesystem <br></li><li>prepare the next YCbCrblock and like (1) flush the cache to make sure its all accessible to DMA</li><li>repeat 2-3 until all bitmap data done</li></ol>Now I'm really stuck (other than random rearrangements of the code)</div><div><br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Mon, Feb 7, 2022 at 3:01 PM Charles Manning <<a href="mailto:cdhmanning@gmail.com" target="_blank">cdhmanning@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div>Hi Robin</div><div><br></div><div>The packing of the structure should not be a factor unless you are fiddling with the packed attribute.</div><div><br></div><div>When we're talking about caching then there are many things that enter the picture.</div><div><br></div><div>You say that the buffer itself (which I expect is the target of the DMA rather than anything else) has the correct caching attributes. What are those? From a brief glimpse at the Cortex M7 manuals, you should be ensuring this has the shared  attribute. If this is set correctly then the caching should not matter. What have you done to check the caching is correct?</div><div>Do not assume that the caching is correct from compiler attributes. Those might not match the settings in the MMU.<br></div><div><br></div><div>What you are observing is that in one case the cache appears to be fetched correctly and in the other not.</div><div>This can be caused by execution of code far away from this point due to how the cache works.</div><div><br></div><div>A compiler change can reorder instructions and data accesses(especially at -O3). THis can completely change the CPU's interaction with the cache. Throw in an out of order CPU like the M7 and a lot can change.<br></div><div><br></div><div>A cache has multiple cache lines and the address being accessed can only map to a few of these cache lines (termed a set). If other code elsewhere needs something in the cache that maps to the same set, then this could be forcing a new cache read - causing the data to be healthy. If, however, the cache is not being refreshed, then the old cached value might be used forever.</div><div><br></div><div>Assuming the DMA controller is only modifying the buffer, I would try adding the following just before accessing the buffer:</div><div><br></div><div>/* Force fresh data into the cache */</div><div>uint32_t jpeg_base = ((uint32_t) JPEG_Data_Buffer) & (~0x1f); /* Calc base 32-byte boundary */</div><div>uint32_t n_bytes =  ((uint32_t) JPEG_Data_Buffer) - jpeg_base + sizeof(JPEG_Data_Buffer);</div><div>SCB_InvalidateDCache_by_Addr(jpeg_base, n_bytes);</div><div>... now access stuff in the cache.</div><div><br></div><div><div><br></div><div><br></div><div><br></div><div><br></div></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr"><br></div></div></blockquote></div>
</blockquote></div></div>