SPI Callback Functions

mustafa_aus
Posts: 3
Joined: Thu Sep 19, 2019 12:41 am

SPI Callback Functions

Postby mustafa_aus » Wed Feb 19, 2020 2:12 pm

Hello everyone,

I have a question regarding SPI callback functions as I need to use handshaking to synchronise SPI transactions between a master and two slaves. As demonstrated in the spi_slave example on github, I am using the functions post_setup_cb() to drive a handshaking GPIO high and post_trans_cb() to subsequently drive it low. This action is performed both SPI slaves as an indication to the master to initiate a transaction. The master receives the handshake and initiates a transaction with the corresponding slave. For the sake of brevity, I can say that the transactions are simply used to implement some voting logic and upon inconsistencies, the three devices will change their functionality (slave/master) in a "round-robin" fashion.

This works as intended for a few cycles (i.e. functionality changes) before SPI transactions "freeze" and voting stops working. After many days of debugging, I have identified the problem as being the handshake GPIO(s) as it is constantly high and not producing a rising edge signal for the SPI master. I suspect that post_trans_cb() is not being called therefore not clearing the handshake. I am fairly confident that this is the case as I have used a push button to simulate this signal and it is working correctly. Also, referring to the lines of code below, I am not receiving the binary semaphore testSem2 in the slave's app_main().

void IRAM_ATTR post_trans_cb(spi_slave_transaction_t *trans)
{
WRITE_PERI_REG(GPIO_OUT_W1TC_REG, (1<<GPIO_HANDSHAKE));
xSemaphoreGiveFromISR(testSem2, NULL);
portYIELD_FROM_ISR();
}

Sorry for the long-winded spiel, essentially I want to know when these functions (post_setup_cb and post_trans_cb) are called and if clearing SPI data registers can be used to call (or force) post_trans_cb? I've tried WRITE_PERI_REG(SPI_W0_REG(HSPI_HOST), 0) to "clear" SPI registers, but does not seem to do much.

This is a very big bottleneck to my project and I can't progress without this resolved. I would appreciate any help or advice anybody on this forum can offer.

Thanks in advanced,

mikemoy
Posts: 605
Joined: Fri Jan 12, 2018 9:10 pm

Re: SPI Callback Functions

Postby mikemoy » Fri Feb 21, 2020 11:11 am

What IDF version are you using?

mustafa_aus
Posts: 3
Joined: Thu Sep 19, 2019 12:41 am

Re: SPI Callback Functions

Postby mustafa_aus » Fri Feb 21, 2020 11:42 am

I am using V3.3.

Thanks,

mikemoy
Posts: 605
Joined: Fri Jan 12, 2018 9:10 pm

Re: SPI Callback Functions

Postby mikemoy » Fri Feb 21, 2020 6:53 pm

When you run those 2 (master & slave ) examples as is without any changes does it work fine ?

lucascamaradb
Posts: 1
Joined: Wed Sep 12, 2018 6:29 pm

Re: SPI Callback Functions

Postby lucascamaradb » Sun Feb 23, 2020 5:18 pm

I have the same issue. I had a project that was fully working in an older version of the IDF. Now that I tried running it on a different PC with the latest version (v4.0) (same ESP-DevKit-C as before), the SPI communication stopped working. I even tried using IDF v3.2.2 instead, but the issue persisted.

After investigating a lot I tracked the issue down to the Handshake pin (GPIO 2, in my case). Using a Logic Analyzer I could see that it wasn't changing its state as expected. I then tried moving "WRITE_PERI_REG(GPIO_OUT_W1TC_REG, (1<<GPIO_HANDSHAKE));" outside the post transaction callback, placing it right after the "spi_slave_transmit" call; the equivalent was done with the SET instruction: I placed it before "spi_slave_transmit". When I did this, the Handshake pin worked properly once more.

I believe the post setup and post transaction callbacks are not being called together with the "spi_slave_transmit" command.

Who is online

Users browsing this forum: Baoshi, Google [Bot], sangk82 and 223 guests