SPI Master Receive Problems
Posted: Tue Mar 28, 2017 4:29 am
Hi guys. First post. Great forum. Thanks a lot to everybody for their time, effort and expertise that you so kindly share.
Regarding the subject, I exhausted google and this forum for some answer I cannot find. Hope somebody can direct me to the proper place/forum/document.
SPI Master in simple transmit mode, speed up to 12MHZ (no more) I used Saleae to trace everything.
Write is Ok, not intuitive but Ok.
Reading is a rather difficult and strange. My case is I need to send a CMD+ ADDH+ADDL to get 1 or more bytes.
Single Byte request.
Need to send CMD+ADDH+ADDL+DUMMY to get 4 answers and return the last one. Not very intuitive. SPI Protocol I guess. Since its just 4 bytes of tx_buffer and rx_buffer not a real problem, just weird.
Now to receive 1000 bytes, the slave has the same procedure. CMD+ADDH+ADDL and keep reading until CS goes High to mark end of reception. To do this I need to SEND a tx_buffer of 1000 bytes!!!! because if I SEND just the 3 bytes of CMD_ADDR, I get no replies.
How can I make this happen WITHOUT making a local buffer (1000 bytes in this case but imagine about 10,000 ).
Code
int FramSPI::readMany (uint16_t framAddr, uint8_t *values,uint16_t howmany)
{
esp_err_t ret=0;
spi_transaction_t t;
uint8_t *tx=(uint8_t*)malloc(howmany+3);
memset(&t, 0, sizeof(t));
*tx=MBRSPI_READ;
*(tx+1)=framAddr>>8;
*(tx+2)=framAddr& 0xff;
t.length=(howmany+3)*8;
t.tx_buffer=tx;
t.rxlength=(howmany+3)*8;
t.rx_buffer=values;
ret=spi_device_transmit(spi, &t);
return ret;
}
BTW, I could write 10,000 bytes and confirmed them but the above code looks fine with the Saleae but the buffer "values" gets junk.
Attach some Salaea screens.
Screenshot 1, 1 byte read. Notice that a dummy had to be sent after CMD+ADDH+ADDL and return last reply (0x20) (Could not attach due to forum limitations rules I guess)
Screenshot 2. A 10,000 byte write to Fram. Perfect.!!!!
Screenshot 3. A 10,000 read request sending just the CMD+ADDH+ADDL. Stops after ADDL. How to send 10,000 dummies without a malloc?
Screenshot 4. A 10,000 read request sending the CMD+ADDH+ADDL+ 9997 dummies from a malloc buffer. Fram sends the 10,000 bytes but the "values" buffer gets only junk. Tried many varieties of buffers, in main app memory, malloc and others with no success.
Thanks again.
Regarding the subject, I exhausted google and this forum for some answer I cannot find. Hope somebody can direct me to the proper place/forum/document.
SPI Master in simple transmit mode, speed up to 12MHZ (no more) I used Saleae to trace everything.
Write is Ok, not intuitive but Ok.
Reading is a rather difficult and strange. My case is I need to send a CMD+ ADDH+ADDL to get 1 or more bytes.
Single Byte request.
Need to send CMD+ADDH+ADDL+DUMMY to get 4 answers and return the last one. Not very intuitive. SPI Protocol I guess. Since its just 4 bytes of tx_buffer and rx_buffer not a real problem, just weird.
Now to receive 1000 bytes, the slave has the same procedure. CMD+ADDH+ADDL and keep reading until CS goes High to mark end of reception. To do this I need to SEND a tx_buffer of 1000 bytes!!!! because if I SEND just the 3 bytes of CMD_ADDR, I get no replies.
How can I make this happen WITHOUT making a local buffer (1000 bytes in this case but imagine about 10,000 ).
Code
int FramSPI::readMany (uint16_t framAddr, uint8_t *values,uint16_t howmany)
{
esp_err_t ret=0;
spi_transaction_t t;
uint8_t *tx=(uint8_t*)malloc(howmany+3);
memset(&t, 0, sizeof(t));
*tx=MBRSPI_READ;
*(tx+1)=framAddr>>8;
*(tx+2)=framAddr& 0xff;
t.length=(howmany+3)*8;
t.tx_buffer=tx;
t.rxlength=(howmany+3)*8;
t.rx_buffer=values;
ret=spi_device_transmit(spi, &t);
return ret;
}
BTW, I could write 10,000 bytes and confirmed them but the above code looks fine with the Saleae but the buffer "values" gets junk.
Attach some Salaea screens.
Screenshot 1, 1 byte read. Notice that a dummy had to be sent after CMD+ADDH+ADDL and return last reply (0x20) (Could not attach due to forum limitations rules I guess)
Screenshot 2. A 10,000 byte write to Fram. Perfect.!!!!
Screenshot 3. A 10,000 read request sending just the CMD+ADDH+ADDL. Stops after ADDL. How to send 10,000 dummies without a malloc?
Screenshot 4. A 10,000 read request sending the CMD+ADDH+ADDL+ 9997 dummies from a malloc buffer. Fram sends the 10,000 bytes but the "values" buffer gets only junk. Tried many varieties of buffers, in main app memory, malloc and others with no success.
Thanks again.