About SPI.transferBytes

arespno
Posts: 9
Joined: Sat Feb 20, 2016 12:19 pm

About SPI.transferBytes

Postby arespno » Thu Dec 15, 2016 1:59 am

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();
}

DonaldTheDinosaur
Posts: 1
Joined: Thu Oct 24, 2019 5:56 pm

Re: About SPI.transferBytes

Postby DonaldTheDinosaur » Thu Oct 24, 2019 6:10 pm

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.

Who is online

Users browsing this forum: No registered users and 54 guests