Kuba0040
Well-known member
Hello,
I’ve encountered some weird behavior when using pointers. My goal is to write the address in memory of an array to the DMA Source Adress register (SADDR). However, some weird things happen.
Here is my code:
Ok, first we create the array in memory and setup a pointer pointing to it.
Next, we have a function called “write_adress_to_DMA” which when called like so: write_adress_to_DMA(TestChunk_pointer);
will take the address the pointer is pointing to, (so the array address) and write it into the SADDR register. And it does all that correctly.
However, when I setup the rest of the DMA and start it, it doesn’t work. Also when I read the SADDR and DADDR registers I see there’s a lot of garbage in front of the value I’ve written to these registers in the write_adress_to_DMA function!
Example:
When we read SADDR after the write_adress_to_DMA function -> 0x20001888
When we read SADDR after setting up and starting the DMA -> 0x400E900020001888
However, if I write the array address like this, where I first store it in a uint32_t variable and then write it to SADDR like so:
and run the function like so: write_adress_to_DMA(TestChunk_adress);
Then even though the same things get written to SADDR, the DMA magically works now, and we don’t see any garbage when we read the SADDR register (value after starting the DMA with the same configuration as before): 0x20000A90
What is going on? How can writing the same thing yield two different results? And lastly, I know it’s better to use DMAChannel.h for any DMA related stuff, but I really want to play with it in this bare metal way. Thank you for understanding.
I'm using a Teensy 4.0.
Thank you for the help.
I’ve encountered some weird behavior when using pointers. My goal is to write the address in memory of an array to the DMA Source Adress register (SADDR). However, some weird things happen.
Here is my code:
Code:
int32_t TestChunkMemory[64]; //Array in memory
int32_t *TestChunk_pointer=TestChunkMemory; //Pointer to this array
void write_adress_to_DMA(int32_t *LocalAdress)
{
Serial.print("Local adress: ");
print64ln(LocalAdress);
DMA_TCD0_SADDR=LocalAdress;
Serial.print("What got written: ");
print64ln(DMA_TCD0_SADDR);
}
Ok, first we create the array in memory and setup a pointer pointing to it.
Next, we have a function called “write_adress_to_DMA” which when called like so: write_adress_to_DMA(TestChunk_pointer);
will take the address the pointer is pointing to, (so the array address) and write it into the SADDR register. And it does all that correctly.
However, when I setup the rest of the DMA and start it, it doesn’t work. Also when I read the SADDR and DADDR registers I see there’s a lot of garbage in front of the value I’ve written to these registers in the write_adress_to_DMA function!
Example:
When we read SADDR after the write_adress_to_DMA function -> 0x20001888
When we read SADDR after setting up and starting the DMA -> 0x400E900020001888
However, if I write the array address like this, where I first store it in a uint32_t variable and then write it to SADDR like so:
Code:
int32_t TestChunkMemory[64]; //Array in memory
int32_t *TestChunk_pointer=TestChunkMemory; //Pointer to this array
uint32_t TestChunk_adress=&TestChunk_pointer; //Memory address
void write_adress_to_DMA(uint32_t LocalAdress)
{
Serial.print("Local adress: ");
print64ln(LocalAdress);
DMA_TCD0_SADDR=LocalAdress;
Serial.print("What got written: ");
print64ln(DMA_TCD0_SADDR);
}
and run the function like so: write_adress_to_DMA(TestChunk_adress);
Then even though the same things get written to SADDR, the DMA magically works now, and we don’t see any garbage when we read the SADDR register (value after starting the DMA with the same configuration as before): 0x20000A90
What is going on? How can writing the same thing yield two different results? And lastly, I know it’s better to use DMAChannel.h for any DMA related stuff, but I really want to play with it in this bare metal way. Thank you for understanding.
I'm using a Teensy 4.0.
Thank you for the help.