Compiling the previous example, a warning has appeared about the variables that catches my attention.
../Core/Src/filter.c:20:20: warning: initialization discards 'volatile' qualifier from pointer target type [-Wdiscarded-qualifiers]
In the following line of code:
uint16_t *p1 = &adc1_buff[init];
adc1_buff is volatile. Perhaps the compiler is asking for the pointer to a volatile to be volatile as well?
Yes, if adc1_buff[] is volatile, then it expects a pointer to a volatile destination, otherwise the volatile qualifier is lost and and the accesses of the function to adc1_buff[] are not volatile. Therefore the warning.
Unfortunately, gcc emits more instructions if the pointer destination is volatile
But I do not see a stringent need for declaring adc1_buff[] volatile. I can hardly imagine a situation where the generated code would not reload the buffer contents from memory each time the function is executed. To be on the safe side when the function happens to be inlined in another function multiple times in a row or in a loop, you can add a compiler barrier to the very beginning of the function.
On other occasions compiler optimizations have led to not updating the buffer by not declaring it volatile.
I think when you encountered this problem, you had declared the buffer as a local variable inside the function.