jac_goudsmit
New member
available() has a bug?
Hey @tonton81,
I'm working on adding slave mode to all the Teensy's, using your libraries (TSPISlave and SPISlave_T4) as inspiration. Thank you very much for writing them!
I noticed that your "available()" function in the SPISlave_T4 library uses the following code:
So basically the function returns true if bit 8 in the SR register is set. From what I can see in the reference manual, that bit is WCF (Word Complete Flag). However I think what you really want to test here is the RDF flag (Read Data Flag) in bit 1. That bit indicates whether the number of words in the FIFO buffer is greater than the RXWATER setting in FCR (which your code sets to 0).
I must say I just got started on the Teensy 4, and I haven't tested your code yet. So I apologize if I have it wrong. But reading through the documentation I figured that RDF would be the obvious flag to test and apparently WCF simply tests if the currently arriving data frame (byte/word/longword) has been received completely. So I wonder if this could be why others reported above that it was behaving strangely.
By the way, your code might be easier to read if you would use the symbolic names from imxrt.h instead of values. With my proposed change, the code would then change into the following (I think):
Thanks!
===Jac
Hey @tonton81,
I'm working on adding slave mode to all the Teensy's, using your libraries (TSPISlave and SPISlave_T4) as inspiration. Thank you very much for writing them!
I noticed that your "available()" function in the SPISlave_T4 library uses the following code:
Code:
SPISlave_T4_FUNC bool SPISlave_T4_OPT::available() {
SLAVE_PORT_ADDR;
return ( (SLAVE_SR & (1UL << 8)) ) ? 1 : 0;
}
So basically the function returns true if bit 8 in the SR register is set. From what I can see in the reference manual, that bit is WCF (Word Complete Flag). However I think what you really want to test here is the RDF flag (Read Data Flag) in bit 1. That bit indicates whether the number of words in the FIFO buffer is greater than the RXWATER setting in FCR (which your code sets to 0).
I must say I just got started on the Teensy 4, and I haven't tested your code yet. So I apologize if I have it wrong. But reading through the documentation I figured that RDF would be the obvious flag to test and apparently WCF simply tests if the currently arriving data frame (byte/word/longword) has been received completely. So I wonder if this could be why others reported above that it was behaving strangely.
By the way, your code might be easier to read if you would use the symbolic names from imxrt.h instead of values. With my proposed change, the code would then change into the following (I think):
Code:
SPISlave_T4_FUNC bool SPISlave_T4_OPT::available() {
// Change the following into a macro. Or better: calculate the address at construction time and keep the pointer as member variable
volatile IMXRT_LPSPI_t *spiAddr = (volatile IMXRT_LPSPI_t*)(0x40394000 + (0x4000 * _portnum));
return (spiAddr.SR & LPSPI_SR_RDF) != 0; // My change
// Your code would be equivalent to: return (spiAddr.SR & LPSPI_SR_WCF) != 0;
}
Thanks!
===Jac
Last edited: