Possible error in SPI code Teensy 4.1

hoek67

Active member
Hi, had a function that draws a filled rectangle on the screen using SPI. I use to be able to send a 16 bit value "n" times but had to change it ATM to send a 256 byte buffer multiple times.

Even though I used SPI.transfer(buffer, count) the contents of the buffer were getting 0'ed after the first iteration. After looking at the code I can see why.


Unless I can be corrected, because the buffer is passed to both RX and TX the buffer will be overwritten by RX which is always 0's in my case.

err.jpg

Code with DMA workaround.
Code:
void cDMA_spi_send_value(uint8_t channel, uint16_t value, uint32_t n) // send a uint16_t multiple times
{
	uint8_t buff[256 + 2];

	buff[0] = value & 255;
	buff[1] = value >> 8;

	n *= 2;

	int i = 0;

	while (i <= 254)
	{
		buff[i + 2] = buff[0];
		buff[i + 3] = buff[1];

		i += 2;
	}

	cDMA_spi_send_do_wait_buffer(channel);

	while (n >= 256)
	{
		dma_waiting = 1;
		SPI.transfer(buff, NULL, 256, resp1);
		n -= 256;
		cDMA_spi_send_do_wait_buffer(channel);
	}

	if (n)
	{
		dma_waiting = 1;
		SPI.transfer(buff, NULL, n, resp1);
		cDMA_spi_send_do_wait_buffer(channel);
	}

}

PS - Would be nice to have SPI.transfer16(val16, count) and SPI.transfer16(val16, count, resp1)
 
As Frank mentioned, that is how the Arduino API was speced...

however we do have: SPI.transfer(buffer, nullptr, count);

which will only do a write and not change the contents of your buffer. Note the nullptr is pointer to rxBuffer, which allows you to optionally retrieve the results of the transfer...
 
Back
Top