<div dir="ltr"><div>Paul:</div><div>Sorry your comment is too cryptic for this self taught machine code programmer.</div><div>It could well be that I should add a few memory barrier instructions but none of the ST Micro example code uses them and the only helpful? comment in any of the example code is as follows:</div><div>@Note If the user code size exceeds the DTCM-RAM size or starts from internal cacheable memories (SRAM1 and SRAM2),that is shared between several processors,<br> then it is highly recommended to enable the CPU cache and maintain its coherence at application level.<br> The address and the size of cacheable buffers (shared between CPU and other masters) must be properly updated to be aligned to cache line size (32 bytes).<br><br>@Note It is recommended to enable the cache and maintain its coherence, but depending on the use case<br> It is also possible to configure the MPU as "Write through", to guarantee the write access coherence.<br> In that case, the MPU must be configured as Cacheable/Bufferable/Not Shareable.<br> Even though the user must manage the cache coherence for read accesses.<br> Please refer to the AN4838 “Managing memory protection unit (MPU) in STM32 MCUs”<br> Please refer to the AN4839 “Level 1 cache on STM32F7 Series”<br></div><div><br></div><div>As already noted, I've assigned the memory explicitly in the DTCM area and made sure it fits!</div><div><br></div>Charles:<div>1) Processor is stm32f779ii so it has all the fandangles<div>2) I'm using -O3 currently as although I'm topping out the bus bandwidth with DMA, the CPU has a lot to do at the same time</div><div>3) I'm aware that volatile and caching are not related as such but since the buffer point and state variable in the stuct are the only things changed between compiler versions, and the map file(s) indicates that EVERYTHING is in the same place (in the DTCM sections anyway - minor changes elsewhere in the code as you would expect with over 1.2m of code) I can only assume it is the way the compiler treats a struct element between static and run time setting. All bets are off by the way if the struct is NOT volatile, as expected as it is referenced by the end-of-DMA callback as well as the code that keeps the buffers filled as the JPEG engine works its way through a bitmap image.</div><div><br></div><div><br></div></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Sun, Feb 6, 2022 at 9:45 AM Charles Manning <<a href="mailto:cdhmanning@gmail.com">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>There are a few important points missing here:</div><div>1) Which STM32 processor? They are not all just "STM32" processors. They range from the very simple Cortex M0 and M0+, through the M3, M4 and M33 up to the M7.</div><div>Different cores have different caching behaviour. The M0 and M0+ have zero caching and the M7 has extensive caching. If you're having a caching issue then I suppose it is more likely to be an M7. Can you confirm that?<br></div><div>2) What compilation options are you using?</div><div>3) Volatile has pretty much nothing to do with caching. All volatile does is ensuring that a registered value is not used and a memory access is forced. It also ensures that the order of volatile accesses is maintained (from a CPU perspective, not a cache perspective). It does nothing to ensure cache coherency. For that you need to do what Paul says: uses memory barriers.</div><div><br></div><div>Assuming you're using an M7, this is not your granny's microcontroller. It does not perform memory accesses in order. It is superscaler (ie. it can execute multiple instructions in parallel and does not execute them in order), has a sophisticated branch predictor and, most importantly here - it has a large cache. Thus you need to be careful to get your various accesses in order.<br></div><div><br></div><div><a href="https://www.sciencedirect.com/topics/engineering/m7-processor" target="_blank">https://www.sciencedirect.com/topics/engineering/m7-processor</a></div><div><a href="http://ww1.microchip.com/downloads/en/DeviceDoc/Managing-Cache-Coherency-on-Cortex-M7-Based-MCUs-DS90003195A.pdf" target="_blank">http://ww1.microchip.com/downloads/en/DeviceDoc/Managing-Cache-Coherency-on-Cortex-M7-Based-MCUs-DS90003195A.pdf</a></div><div><a href="https://www.sciencedirect.com/topics/engineering/memory-barrier-instruction" target="_blank">https://www.sciencedirect.com/topics/engineering/memory-barrier-instruction</a></div><div><br></div><div>The Microchip doc gives some good explanations. It doesn't really matter if it is an ST - what matters is that the core is the same.</div><div><br></div><div>You might find the cache controll functions and data barrier instructions are helpful.<br></div><div><span style="font-size:16.6667px;font-family:sans-serif" role="presentation" dir="ltr">SCB_CleanDCache_by_Addr (uint32_t *addr,</span><span style="font-size:16.6667px;font-family:sans-serif" role="presentation" dir="ltr"> int32_t dsize)</span></div><div><span style="font-size:16.6667px;font-family:sans-serif" role="presentation" dir="ltr">__DSB()</span></div><div><span style="font-size:16.6667px;font-family:sans-serif" role="presentation" dir="ltr">etc</span></div><div><span style="font-size:16.6667px;font-family:sans-serif" role="presentation" dir="ltr"><br></span></div><div><span style="font-size:16.6667px;font-family:sans-serif" role="presentation" dir="ltr">Regards</span></div><div><span style="font-size:16.6667px;font-family:sans-serif" role="presentation" dir="ltr"><br></span></div><div><span style="font-size:16.6667px;font-family:sans-serif" role="presentation" dir="ltr">Charles<br></span></div><div><br></div><div><br></div><div><br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Sun, Feb 6, 2022 at 1:12 AM Paul Davey <<a href="mailto:plmdvy@gmail.com" target="_blank">plmdvy@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">Have you tried sprinkling memory barriers between the CPU access to<br>
the DMA memory and the DMA operation trigger?<br>
<br>
On Sat, Feb 5, 2022 at 8:23 PM Andrew Dachs <<a href="mailto:dachsa492@gmail.com" target="_blank">dachsa492@gmail.com</a>> wrote:<br>
><br>
> I see. Could it be that the initialisation code changes are just moving things about in memory rather than root cause? Why is it important that the control struct is volatile or is it really just the contents of DataBuffer that’s being changed?<br>
><br>
><br>
><br>
> Sent from my iPhone<br>
><br>
> On 5/02/2022, at 7:17 PM, Robin Gilks <<a href="mailto:gb7ipd@gmail.com" target="_blank">gb7ipd@gmail.com</a>> wrote:<br>
><br>
> <br>
> It is outside .data and explicitly NOT initialised.<br>
> Excerpts from the linker file...<br>
><br>
> /* Specify the memory areas */<br>
> MEMORY<br>
> {<br>
> DTCM (xrw) : ORIGIN = 0x20000000, LENGTH = 128k<br>
> RAM (xrw) : ORIGIN = 0x20020000, LENGTH = 368K<br>
> BOOTRAM (xrw) : ORIGIN = 0x2007C000, LENGTH = 16K<br>
> FLASH (rx) : ORIGIN = 0x8000000, LENGTH = 2048K<br>
> }<br>
><br>
> /* used by audio buffers and JPEG h/w */<br>
> .MCU_Data_section 0x20000000 (NOLOAD) : { *(.MCU_Data_section) } >DTCM<br>
><br>
> The DTCM memory is by default available to all internal DMA buses (so saves playing with the cache setup using 'HAL_MPU_ConfigRegion()'.<br>
><br>
><br>
> On Sat, Feb 5, 2022 at 6:47 PM Andrew Dachs <<a href="mailto:dachsa492@gmail.com" target="_blank">dachsa492@gmail.com</a>> wrote:<br>
>><br>
>> Hi Robin,<br>
>> Is your special area of memory inside .data and part of the initialisation on startup?<br>
>> Andy<br>
>><br>
>><br>
>><br>
>> Sent from my iPhone<br>
>><br>
>> On 5/02/2022, at 5:42 PM, Robin Gilks <<a href="mailto:gb7ipd@gmail.com" target="_blank">gb7ipd@gmail.com</a>> wrote:<br>
>><br>
>> <br>
>> Hmmm - thanks Mark, I hadn't thought of packing issues.<br>
>> It's not like I'm trying a union to another struct so positioning would be critical. That being the case I think I'll keep that thought on the back burner for now..<br>
>><br>
>> Cheers<br>
>><br>
>><br>
>> On Sat, Feb 5, 2022 at 4:10 PM Mark Atherton <<a href="mailto:markaren1@xtra.co.nz" target="_blank">markaren1@xtra.co.nz</a>> wrote:<br>
>>><br>
>>> I have seen issues with data segments, where large chunks (u32, u8*)<br>
>>> follow small chunks (u8).<br>
>>><br>
>>> Try changing<br>
>>><br>
>>> > uint8_t State;<br>
>>> > uint8_t *DataBuffer;<br>
>>> > uint32_t DataBufferSize;<br>
>>><br>
>>> to<br>
>>><br>
>>> > uint32_t DataBufferSize;<br>
>>> > uint8_t *DataBuffer;<br>
>>> > uint8_t State;<br>
>>><br>
>>> -Mark<br>
>>><br>
>>><br>
>>><br>
>>><br>
>>> On 5/02/2022 1:50 PM, Robin Gilks wrote:<br>
>>> > Some may recall that a year or 2 ago I had great problems getting the<br>
>>> > hardware JPEG encode to work on an STM32 processor. It turned out that<br>
>>> > the memory cache attributes manifested as a race condition between DMA<br>
>>> > and CPU access.<br>
>>> > The problem has just reappeared as a result of updating<br>
>>> > from arm-none-eabi-gcc-9.2.1-1.1 to arm-none-eabi-gcc-10.3.1-2.3.<br>
>>> ><br>
>>> > After a few pokes at the code I've narrowed the issue down to whether a<br>
>>> > volatile struct element is initialized statically or at run time.<br>
>>> > Interestingly the issue is inverted between compiler versions<br>
>>> > The buffer referenced by the structure element 'DataBuffer' is defined<br>
>>> > and instantiated in a reserved section of memory that has the correct<br>
>>> > cache attributes<br>
>>> ><br>
>>> > typedef struct<br>
>>> > {<br>
>>> > uint8_t State;<br>
>>> > uint8_t *DataBuffer;<br>
>>> > uint32_t DataBufferSize;<br>
>>> > }JPEG_Data_BufferTypeDef;<br>
>>> > uint8_t<br>
>>> > JPEG_Data_InBuffer[CHUNK_SIZE_IN]__attribute__((section(".MCU_Data_section")));<br>
>>> > original code - works with gcc-9; fails with gcc-10:<br>
>>> > volatile JPEG_Data_BufferTypeDef Jpeg_IN_BufferTab = {JPEG_BUFFER_EMPTY,<br>
>>> > JPEG_Data_InBuffer, 0};<br>
>>> > current code fails with gcc-9 works with gcc-10:<br>
>>> > volatileJPEG_Data_BufferTypeDefJpeg_IN_BufferTab= {0, 0, 0};<br>
>>> > ...<br>
>>> > Jpeg_IN_BufferTab.DataBufferSize = 0;<br>
>>> > Jpeg_IN_BufferTab.State = JPEG_BUFFER_EMPTY;<br>
>>> > Jpeg_IN_BufferTab.DataBuffer = JPEG_Data_InBuffer;<br>
>>> ><br>
>>> > Note that NO other changes (apart from the compiler version) were made!!<br>
>>> > I'm at a total loss as to what is going on, hopefully someone can shine<br>
>>> > a light ;)<br>
>>> ><br>
>>> > --<br>
>>> > Robin Gilks<br>
>>> ><br>
>>> ><br>
>>> > _______________________________________________<br>
>>> > Chchrobotics mailing list <a href="mailto:Chchrobotics@lists.ourshack.com" target="_blank">Chchrobotics@lists.ourshack.com</a><br>
>>> > <a href="https://lists.ourshack.com/mailman/listinfo/chchrobotics" rel="noreferrer" target="_blank">https://lists.ourshack.com/mailman/listinfo/chchrobotics</a><br>
>>> > Mail Archives: <a href="http://lists.ourshack.com/pipermail/chchrobotics/" rel="noreferrer" target="_blank">http://lists.ourshack.com/pipermail/chchrobotics/</a><br>
>>> > Meetings usually 3rd Monday each month. See <a href="http://kiwibots.org" rel="noreferrer" target="_blank">http://kiwibots.org</a> for venue, directions and dates.<br>
>>> > When replying, please edit your Subject line to reflect new subjects.<br>
>>> ><br>
>>><br>
>>><br>
>>> _______________________________________________<br>
>>> Chchrobotics mailing list <a href="mailto:Chchrobotics@lists.ourshack.com" target="_blank">Chchrobotics@lists.ourshack.com</a><br>
>>> <a href="https://lists.ourshack.com/mailman/listinfo/chchrobotics" rel="noreferrer" target="_blank">https://lists.ourshack.com/mailman/listinfo/chchrobotics</a><br>
>>> Mail Archives: <a href="http://lists.ourshack.com/pipermail/chchrobotics/" rel="noreferrer" target="_blank">http://lists.ourshack.com/pipermail/chchrobotics/</a><br>
>>> Meetings usually 3rd Monday each month. See <a href="http://kiwibots.org" rel="noreferrer" target="_blank">http://kiwibots.org</a> for venue, directions and dates.<br>
>>> When replying, please edit your Subject line to reflect new subjects.<br>
>><br>
>> _______________________________________________<br>
>> Chchrobotics mailing list <a href="mailto:Chchrobotics@lists.ourshack.com" target="_blank">Chchrobotics@lists.ourshack.com</a><br>
>> <a href="https://lists.ourshack.com/mailman/listinfo/chchrobotics" rel="noreferrer" target="_blank">https://lists.ourshack.com/mailman/listinfo/chchrobotics</a><br>
>> Mail Archives: <a href="http://lists.ourshack.com/pipermail/chchrobotics/" rel="noreferrer" target="_blank">http://lists.ourshack.com/pipermail/chchrobotics/</a><br>
>> Meetings usually 3rd Monday each month. See <a href="http://kiwibots.org" rel="noreferrer" target="_blank">http://kiwibots.org</a> for venue, directions and dates.<br>
>> When replying, please edit your Subject line to reflect new subjects.<br>
>><br>
>> _______________________________________________<br>
>> Chchrobotics mailing list <a href="mailto:Chchrobotics@lists.ourshack.com" target="_blank">Chchrobotics@lists.ourshack.com</a><br>
>> <a href="https://lists.ourshack.com/mailman/listinfo/chchrobotics" rel="noreferrer" target="_blank">https://lists.ourshack.com/mailman/listinfo/chchrobotics</a><br>
>> Mail Archives: <a href="http://lists.ourshack.com/pipermail/chchrobotics/" rel="noreferrer" target="_blank">http://lists.ourshack.com/pipermail/chchrobotics/</a><br>
>> Meetings usually 3rd Monday each month. See <a href="http://kiwibots.org" rel="noreferrer" target="_blank">http://kiwibots.org</a> for venue, directions and dates.<br>
>> When replying, please edit your Subject line to reflect new subjects.<br>
><br>
> _______________________________________________<br>
> Chchrobotics mailing list <a href="mailto:Chchrobotics@lists.ourshack.com" target="_blank">Chchrobotics@lists.ourshack.com</a><br>
> <a href="https://lists.ourshack.com/mailman/listinfo/chchrobotics" rel="noreferrer" target="_blank">https://lists.ourshack.com/mailman/listinfo/chchrobotics</a><br>
> Mail Archives: <a href="http://lists.ourshack.com/pipermail/chchrobotics/" rel="noreferrer" target="_blank">http://lists.ourshack.com/pipermail/chchrobotics/</a><br>
> Meetings usually 3rd Monday each month. See <a href="http://kiwibots.org" rel="noreferrer" target="_blank">http://kiwibots.org</a> for venue, directions and dates.<br>
> When replying, please edit your Subject line to reflect new subjects.<br>
><br>
> _______________________________________________<br>
> Chchrobotics mailing list <a href="mailto:Chchrobotics@lists.ourshack.com" target="_blank">Chchrobotics@lists.ourshack.com</a><br>
> <a href="https://lists.ourshack.com/mailman/listinfo/chchrobotics" rel="noreferrer" target="_blank">https://lists.ourshack.com/mailman/listinfo/chchrobotics</a><br>
> Mail Archives: <a href="http://lists.ourshack.com/pipermail/chchrobotics/" rel="noreferrer" target="_blank">http://lists.ourshack.com/pipermail/chchrobotics/</a><br>
> Meetings usually 3rd Monday each month. See <a href="http://kiwibots.org" rel="noreferrer" target="_blank">http://kiwibots.org</a> for venue, directions and dates.<br>
> When replying, please edit your Subject line to reflect new subjects.<br>
<br>
_______________________________________________<br>
Chchrobotics mailing list <a href="mailto:Chchrobotics@lists.ourshack.com" target="_blank">Chchrobotics@lists.ourshack.com</a><br>
<a href="https://lists.ourshack.com/mailman/listinfo/chchrobotics" rel="noreferrer" target="_blank">https://lists.ourshack.com/mailman/listinfo/chchrobotics</a><br>
Mail Archives: <a href="http://lists.ourshack.com/pipermail/chchrobotics/" rel="noreferrer" target="_blank">http://lists.ourshack.com/pipermail/chchrobotics/</a><br>
Meetings usually 3rd Monday each month. See <a href="http://kiwibots.org" rel="noreferrer" target="_blank">http://kiwibots.org</a> for venue, directions and dates.<br>
When replying, please edit your Subject line to reflect new subjects.</blockquote></div>
_______________________________________________<br>
Chchrobotics mailing list <a href="mailto:Chchrobotics@lists.ourshack.com" target="_blank">Chchrobotics@lists.ourshack.com</a><br>
<a href="https://lists.ourshack.com/mailman/listinfo/chchrobotics" rel="noreferrer" target="_blank">https://lists.ourshack.com/mailman/listinfo/chchrobotics</a><br>
Mail Archives: <a href="http://lists.ourshack.com/pipermail/chchrobotics/" rel="noreferrer" target="_blank">http://lists.ourshack.com/pipermail/chchrobotics/</a><br>
Meetings usually 3rd Monday each month. See <a href="http://kiwibots.org" rel="noreferrer" target="_blank">http://kiwibots.org</a> for venue, directions and dates.<br>
When replying, please edit your Subject line to reflect new subjects.</blockquote></div>