True is false.

Mcu32

Well-known member
Code:
#include <Arduino.h>
#include <Audio.h>
#include <EventResponder.h>
#include "imxrt.h"
DMAMEM audio_block_t audioBlock;
DMAMEM volatile bool audioBlockReady;
EventResponder reportEvent;
void report(EventResponderRef event) {
    if (audioBlockReady == true)
        Serial.println("True is true !!");
    else
        Serial.println("True is false !!");
}
void setup() {
    Serial.begin(115200);
    while (!Serial && millis() < 6000) {}


    audioBlockReady = false;


    arm_dcache_flush((void *)&audioBlockReady, sizeof(audioBlockReady));
    reportEvent.attach(report);
    reportEvent.triggerEvent();

    //Code.... code....code
    //Code.... code....code
    //Code.... code....code
    //Code.... code....code
    //Code.... code....code

    audioBlockReady = true;

    //Some heavy audio computations....
    //Code.... code....code
    //Code.... code....code
    //nope.. lets delete them....
    //Code.... code....code
    //Code.... code....code

    arm_dcache_delete(&audioBlock, sizeof(audioBlock));

    //Code.... code....code
    //Code.... code....code

}
void loop() {
    //Code.... code....code
    //Code.... code....code
    //Code.... code....code
}
A quick request for the veterans: Please don't spoil what's causing it right away...
 
Last edited:
From imxrt.h line 10336:

Code:
// Delete data from the cache, without touching memory
//
// WARNING: This function is DANGEROUS!!  The address must be
// 32 byte aligned and the size must be a multiple of 32 bytes.
//
// DO NOT USE this function with arbitrarily aligned data,
// especially pointers from malloc() or C++ new.  The ARM cache
// can only delete with granularity of 32 byte cache rows.  If
// you attempt to delete improperly aligned data, any other
// cached variables shared within the same 32 byte cache row(s)
// will become collateral damage!
//
// If you wish to assure some variable or array or other data
// is not cached, use arm_dcache_flush_delete().  This
// arm_dcache_delete() should only be used for very special
// cases like DMA buffers or hardware testing & benchmarks.
//
// See this forum thread for more detail:
// https://forum.pjrc.com/threads/68100-BUG-in-arm_dcache_delete
//
// Normally arm_dcache_delete() is used before receiving data via
// DMA or from bus-master peripherals which write to memory.  You
// want to delete anything the cache may have stored, so your next
// read is certain to access the physical memory.
 
Dear Paul, if you don't know this and aren't aware of the existence of this well-hidden comment, it will take you an hour or more to figure out why. You're accessing a completely different variable. The value of a variable that has nothing to do with this is being changed—perhaps hundreds of lines later.. in a different .cpp… and… especially since you're assuming that DMAMEM already handles this correctly.
That's why it's called “DMAMEM,” after all. So the error isn't even this function; it's somewhere else entirely.
Whether this is a user error at all is certainly a topic that could be debated at length.
 
Last edited:
Whether this is a user error at all is certainly a topic that could be debated at length.
Nah. This is user error, full stop. If you're going to dirty your hands with the cache functions you should know how they work (and at the very least, read the documentation that directly precedes the function declaration).
 
Search the core .. there are many calls of this function. You don't have to call it yourself. Then, add 3rd party libs.
The function ist not the problem. It's DMAMEM. It's enough to declare a DMAMEM var without align(32). If the ggc linker places it in an awkward spot because no one told it not to... ..then, a random call somewhere...
But you can argue about everything for hours on end, and nothing comes of it... when it could be solved so easily.

U could say "This is user error, full stop." and remove all nullptr checks everywhere, because.. why check it? It's user code.

Oops. I found some unaligned DMAMEM vars in the core...
 
Last edited:
Oh... now that I've looked into it... that could actually be the reason behind some “unexplained” phenomena that have never been resolved. Without any user error at all.
If you fix that, an entire class of such phenomena is solved in one fell swoop.
 
You gave example code that directly used arm_dcache_delete() in an incorrect way. That is user error.

What is the point of coming back to the forum with a new username and being just as obnoxious as you were years ago? Get a new hobby.
 
The personal attacks are an ad hominem fallacy.
My username, alleged behavior years ago, and hobbies are irrelevant to the technical discussion.

That "user error" is in the core, too.

Edit:
It takes no more than about 5 seconds for anyone to implement the fix I mentioned (as well as many others that were mentioned).
That settles the matter once and for all. Why all the fuss?
 
Because that's not a "fix" for anything, let alone the correct one for user error.
All standard dynamic allocations come from DMAMEM, are you going to force alignment on those too? How, exactly? Rebuild newlib?
You know alignment only affects the starting address, right? So another variable can still exist after it in the same cacheline. To prevent that you would also have to round up the size of every allocation to a multiple of 32, wasting up to 31 bytes.
How about large arrays put in DMAMEM/EXTMEM that are never touched by anything outside of the CPU? Why is it necessary to align those and waste space if they are never touched by manual cache operations?

The only "5 second" fix for not understanding correctly how the cache works is to disable it, which would obviously kill performance.
 
  1. Newlib does not use the DMAMEM macro. DMAMEM is a Teensy core feature used by the core, Teensy libraries and user code. There is no evidence that newlib itself calls arm_dcache_delete() or arm_dcache_flush_delete(). Changing the DMAMEM macro does not require rebuilding newlib, since this change concerns statically declared variables, not dynamic memory allocation.
  2. If a cache-maintenance operation can modify memory outside the intended buffer, that needs to be addressed.
  3. Adding aligned(32) to DMAMEM ensures that every individually declared DMAMEM variable starts on a 32-byte boundary. This also prevents adjacent, individually declared DMAMEM variables from sharing a cache line, even if the preceding variable's size is not a multiple of 32 bytes. For example, a 33-byte array occupies bytes 0–32, and the next aligned variable can start at byte 64. This is precisely the kind of protection the proposed change provides.
  4. If additional cases need to be addressed, such as structure members or dynamically allocated buffers, those can be handled separately. They do not invalidate or diminish the benefit of this fix.
  5. If this fix is rejected, the relevant cache-maintenance call sites and all buffers must be audited individually to ensure that alignment and cache-line isolation are handled correctly wherever required.
To be clear, I am not claiming that this change automatically solves every possible cache-maintenance issue. It provides a useful and well-defined guarantee for individually declared DMAMEM variables. Other cases can be addressed separately without dismissing this improvement.
But at least errors of this kind are fixed. And the DMAMEM(unaligned) probs in the core, too, even without having to touch it.

Edit: And yes, that would resolve an entire class of phenomena.
 
Last edited:
That's a TX buffer - it only ever has arm_dcache_flush_delete() called on it which can't discard CPU-side data, i.e. it cannot possibly cause the same issue as the source code in the opening post. Try again, the only thing really wrong there is that arm_dcache_flush() would be sufficient.

Or don't. This really is a waste of time because all of the above points are based on bad assumptions. Again: if malloc allocates DMAMEM, how are you going to ensure it always returns aligned data and rounds every allocation up to a full cacheline? Why does this need to be done for every single allocation even when there will be no cache interaction?

If you really want these changes, fork the core and do it yourself. Then you can have your own happy little repo where none of these perceived problems exist, and this endless public stream of pointless nitpicks from someone who has confessed they don't even use Teensys any more can end.
 
Last edited:
...try it again.
Because the interaction may not be under your control.
It doesn’t matter whether the TX is cleared, whether it’s randomised, quartered, or filled with 1111. It isn’t 32-byte-aligned. It can reside after a completely different buffer with unfortunate length that is being cleared.
It's more or less a matter of chance where that is. Edit: okay, arm_dcache_flush_delete() gets called on this var? did not even look at this. That makes it worse as the delete can delete parts of a completely different variable that resides before it. (code: uint32_t location = (uint32_t)addr & 0xFFFFFFE0;)
Didn't you call this "User error"?

"every single allocation": No. Read my post? Malloc??? I'ts not about malloc.
Well, you decided to waste your time. It's waste of my time now, too. Have to locate the Ignore button...
 
Last edited:
Then the error would be that "other" buffer being invalidated when it's not aligned and a multiple of 32 bytes, contrary to the documentation of arm_dcache_delete() that Paul posted above - where is it?

For the record, I use the cache management functions extensively in my USB host library - it is an absolute necessity, due to the host controller operating on specific memory structures and nearly all of the memory being dynamically allocated. Plus any driver that takes memory buffers as arguments is designed to work with memory from any region, i.e. whether it's FLASH, RAM1, RAM2 or EXTMEM. I know exactly when / how they should be used and what restrictions apply.
 
Last edited:
Yes Paul. Why not make the DMAMEM macro align(32) these vars?
Apparently, even you find it difficult to keep all this documentation in your head at all times. Wouldn't it be nice if, at least for simple static variables like these, we could rely on DMAMEM to get it right? See #14.
 
Last edited:
I need to weigh the needs of the many with the needs of the few.

Many programs exist using DMAMEM to allocate variables and arrays in RAM2. Few programs (essentially none) exist which incorrectly call arm_dcache_delete() in violation of the documented requirements.

Aligning all DMAMEM declarations could have subtle and unintended consequences. It is not the sort of decision to make lightly, especially not with only 5 seconds. For programs which have many small variables, this could result in considerable unused or wasted memory. It could also alter performance of existing programs, for the common case where 2 or more variable are currently allocated within the same cache row. Cache performance depends on locality of reference.

My belief is this only "fixes" the circumstance where programs disregard arm_dcache_delete() requirements, like the code in msg #1. I see you're arguing otherwise in msg #16, but I'm not convinced. Please make your point with a complete program which does follow the 32 bit align and multiple of 32 byte size requirement. I'm pretty sure you'll find following those documented requirements prevent harm to unrelated variables.

But to come back again to "why not", without a test case that demonstrates the problem (and with logical intuition that the problem can't exist if the requirements are followed), to me this looks like a case of risking subtle and difficult to fully anticipate changes to almost all programs, to solve a non-issue. That's why not.

Again, I will change my tune if an example is shown where the problem happens when the arm_dcache_delete() requirements are correctly followed.
 
Okay, then you have to search through the core and find more like the line in #14 - if you're willing to fix bugs to make teensyduino more stable and respect your own documentation.

Unfortunately, it's not possible to prove the correctness of non-trivial code. That's why “if it ain't broke, don't fix it” doesn't apply.
 
Mcu32, you really seem to bitch a lot! I use teensy 4.1 and it does what i need very well. I have no complaints. With all of your complaints, can't you just forget the teensy and go somewhere where you would be more happy? Or better yet, develop your own product? Jeez...
 
Just because you aren't running into a bug yourself right now doesn't mean others aren't affected by it.
I'd encourage you to take a look at the forum over a longer period of time.
For example, the "happy little" posts describe real bugs, all pretty easy to fix (yes, most of them "in seconds")
I've also seen plenty of user code using DMAMEM wrong. Many users simply aren't aware of this requirement. You can also read posts about "clicking usb audio".

If the position is that bugs shouldn't be reported or that known issues shouldn't be fixed, then please say so clearly. In that case, I don't see much point in continuing to contribute anything.
 
Ok, I guess they are not bugs i see. Having worked in development for most of my life (50 years of it), your bug reporting leaves something to be desired. But I am used to a professional hardware and software environment utilizing a more professional approach to reporting like:

  • Title (Summary): A short, clear sentence answering What, Where, and When (e.g., "Cart total fails to update on Chrome 120"). Avoid vague titles like "It doesn't work".
  • Description: A brief summary explaining the core defect.
  • Environment: Technical specs including OS, browser, device model, app version, or exact source URL.
  • Steps to Reproduce: A numbered, exact sequence of actions so anyone can trigger the bug from scratch.
  • Expected Result: What is supposed to happen based on requirements or user stories.
  • Actual Result: What actually happened, including exact error messages or console logs.
  • Severity & Priority:
    • Severity: How badly the bug breaks the system (Critical, Major, Minor, Trivial).
    • Priority: How fast it needs to be fixed based on business urgency (High, Medium, Low).
  • Attachments: Screenshots, screen recordings, or console/backend logs.
complete reports void of emotion.


Your reports seem like complaints, just saying...
 
Back
Top