Page 1 of 1

About SPI.transferBytes

Posted: Thu Dec 15, 2016 1:59 am
by arespno
When you complete the transmission of data, immediately pullup CS Pin, then the data transmission is not complete.
For example:

Code: Select all

{
    ......
	
    const char *hello = "Hello, world!";
	
    chip_cs_low();  // pulldown cs pin
    SPI.transferBytes((uint8_t *) hello, 0, strlen(hello));  // write "hello world!"
    chip_cs_high();  // pullup cs pin
	
    ......
}

// "Hello, world!" may only be transmitted by "Hello".
Can be solved:

Code: Select all

On  arduino\hardware\espressif\esp32\cores\esp32\esp32-hal-spi.c

void spiTransferBytes(spi_t * spi, uint8_t * data, uint8_t * out, uint32_t size)
{
    if(!spi) {
        return;
    }
    SPI_MUTEX_LOCK();
    while(size) {
        if(size > 64) {
            __spiTransferBytes(spi, data, out, 64);
            size -= 64;
            if(data) {
                data += 64;
            }
            if(out) {
                out += 64;
            }
        } else {
            __spiTransferBytes(spi, data, out, size);
            size = 0;
+	        if (!out) {
+	            while(spi->dev->cmd.usr);
+	        }
        }
    }
    SPI_MUTEX_UNLOCK();
}

Re: About SPI.transferBytes

Posted: Thu Oct 24, 2019 6:10 pm
by DonaldTheDinosaur
Shouldn't

Code: Select all

while(spi->dev->cmd.usr);
be

Code: Select all

while(spi->dev->cmd.usr) yield();
to allow other threads on the same CPU to run while waiting for the SPI bus? This is needed to allow overlapping SPI transactions on different busses.