<div dir="ltr"><div>Many thanks to the contributions - I'm making progress.</div><div><br></div><div>Turns out I have 2 issues with the jpeg encoder.</div><div>1. A library function (closed source, no idea what it does inside) gets clobbered when optimise is cranked up, looks like an undocumented 'feature' of just how large a working area is required by the encoder and what I had allocated was being overflowed. (I found a worked example that used much larger buffers than I had and that works fine, unfortunately the undocumented bit is how the buffer size is determined).</div><div><br></div><div>2. The module that works OK with optimisation off is full of DMA interrupt hazards! The stm32f7 jpeg hardware encoder/decoder (<a href="https://www.st.com/resource/en/application_note/dm00356635-hardware-jpeg-codec-peripheral-in-stm32f7677xxx-and-stm32h743534555475750a3b3b0xx-microcontrollers-stmicroelectronics.pdf">https://www.st.com/resource/en/application_note/dm00356635-hardware-jpeg-codec-peripheral-in-stm32f7677xxx-and-stm32h743534555475750a3b3b0xx-microcontrollers-stmicroelectronics.pdf</a>) is fast but it looks like the silicon has been borrowed from elsewhere and it has a lot of constraints - like aligned and uncached memory otherwise it just silently fails. I'm working my way through this module to try and understand where it errors. Basically an RGB buffer is converted to YCbCr (which is OK, despite my original thoughts) and the MCU (Minimum Coded Unit) so generated is DMAed into the JPEG engine and it coughs out JPEG data (again via DMA) into another buffer. It goes round and round processing the original bmp image until a new image 30th the size is created!<br></div><div> <br></div><div>I'm hoping that a bit of quiet time with the code in front of me will reveal where the problems lie.</div><div><br></div><div>Cheers<br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Fri, Jul 2, 2021 at 3:56 PM Helmut Walle <<a href="mailto:helmut.walle@gmail.com">helmut.walle@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">Regarding that question: if an element of a structure is qualified as <br>
volatile, only that element of any instances should become volatile, not <br>
the entire instance of the structure. Where you need the entire instance <br>
of the structure to be qualified as volatile, this can easily be <br>
achieved by qualifying the instantiation as volatile. Qualifying an <br>
entire instance of a structure makes all elements of that instance <br>
volatile. This is the respective example from the ISO C99 standard:<br>
<br>
"<br>
<br>
EXAMPLE 2 In:<br>
struct s { int i; const int ci; };<br>
struct s s;<br>
const struct s cs;<br>
volatile struct s vs;<br>
the various members have the types:<br>
s.i int<br>
<a href="http://s.ci" rel="noreferrer" target="_blank">s.ci</a> const int<br>
cs.i const int<br>
<a href="http://cs.ci" rel="noreferrer" target="_blank">cs.ci</a> const int<br>
vs.i volatile int<br>
<a href="http://vs.ci" rel="noreferrer" target="_blank">vs.ci</a> volatile const int<br>
<br>
"<br>
<br>
Optimisations tend to be highly implementation-dependent. However, the <br>
ISO C99 standard very clearly says the following about the volatile <br>
qualifier in subsection 6.7.3 (6), and in footnote 116, respectively:<br>
<br>
"6 An object that has volatile-qualified type may be modified in ways <br>
unknown to the<br>
implementation or have other unknown side effects. Therefore any <br>
expression referring<br>
to such an object shall be evaluated strictly according to the rules of <br>
the abstract machine,<br>
as described in 5.1.2.3. Furthermore, at every sequence point the value <br>
last stored in the<br>
object shall agree with that prescribed by the abstract machine, except <br>
as modified by the<br>
unknown factors mentioned previously.116) What constitutes an access to <br>
an object that<br>
has volatile-qualified type is implementation-defined."<br>
<br>
"116) A volatile declaration may be used to describe an object <br>
corresponding to a memory-mapped<br>
input/output port or an object accessed by an asynchronously <br>
interrupting function. Actions on<br>
objects so declared shall not be ‘‘optimized out’’ by an implementation <br>
or reordered except as<br>
permitted by the rules for evaluating expressions."<br>
<br>
So there is a little bit of wiggle room in that last sentence of 6.7.3 <br>
(6), "What constitutes an access to an object [...]"<br>
<br>
The remaining uncertainty then is is in the questions:<br>
<br>
- whether the compiler was developed aiming at ISO C99 conformity in the <br>
first place, and<br>
<br>
- even if it was intended to be ISO C99, whether it actually does meet <br>
all of the requirements of the standard.<br>
<br>
Is it possible in your case to compile twice with the same optimisation, <br>
while making the smallest possible change to the volatile qualifiers <br>
(e.g., add or remove just one of them), and then comapring the assembly <br>
results? That would be one way of checking potential compiler issues for <br>
the respective optimisation level, and if not too onerous, this might <br>
help more to understand what is going on than looking at overall system <br>
behaviour.<br>
<br>
The other general comment is that, while compiler bugs are not unheard <br>
of, they tend to be much rarer than errors in new code under <br>
development... So carefully reviewing your code for other common errors <br>
or risky constructs might be a good idea. I don't know whether this <br>
concern applies in your case, but if you get strange behaviour, how <br>
certain are you that the volatile questions are really causing it?<br>
<br>
Kind regards,<br>
<br>
Helmut.<br>
<br>
On 2/07/2021 12:37, Robin Gilks wrote:<br>
> I've managed to get the change in behaviour down to a single file now <br>
> and have started to sprinkle 'volatiles' about the place.<br>
><br>
> Question - if an element of a structure is defined as volatile, does <br>
> the instantiation of the structure need to be also or does the whole <br>
> struct effectively become volatile.<br>
><br>
> Cheers<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.</blockquote></div>