Communicating with spi with slave in interrupt, the system will be dumped

thanhphuct
Posts: 3
Joined: Fri Jan 29, 2021 4:40 am

Communicating with spi with slave in interrupt, the system will be dumped

Postby thanhphuct » Fri Jan 29, 2021 6:06 am

Hi everybody,
I have a problem and hope recive support from everyone.

I am trying to do spi communication with external flash memory (MT25QL128) in interrupt, this leads to the system dump. I want to ask if this is possible? If possible please give me an instruction

Below is my spi communication function:

void spiTranfer(uint8_t *writeData , uint32_t length, uint8_t *readData)
{
//Transaction descriptors. Declared static so they're not allocated on the stack; we need this memory even when this
//function is finished because the SPI driver needs access to it even while we're already calculating the next line.
static spi_transaction_t trans;

//In theory, it's better to initialize trans and data only once and hang on to the initialized
//variables. We allocate them on the stack, so we need to re-init them each call.

memset(&trans, 0, sizeof(spi_transaction_t));

//Odd transfers are data
trans.length = length*8;
trans.user = (void *)1;
trans.tx_buffer = writeData; //finally send the line data
trans.rx_buffer = readData;
trans.flags = 0; //undo SPI_TRANS_USE_TXDATA flag

//Queue all transactions.
spi_device_queue_trans(g_spi, &trans, portMAX_DELAY);
}


Thank you for your time.

ESP_Sprite
Posts: 9746
Joined: Thu Nov 26, 2015 4:08 am

Re: Communicating with spi with slave in interrupt, the system will be dumped

Postby ESP_Sprite » Fri Jan 29, 2021 6:21 am

Does your readData and writeData actually exist as long as the SPI transfer is in progress?

thanhphuct
Posts: 3
Joined: Fri Jan 29, 2021 4:40 am

Re: Communicating with spi with slave in interrupt, the system will be dumped

Postby thanhphuct » Fri Jan 29, 2021 6:32 am

ESP_Sprite wrote:
Fri Jan 29, 2021 6:21 am
Does your readData and writeData actually exist as long as the SPI transfer is in progress?
Yes yes, I have tried using spiTranfer () function with 1 external flash memory slave and it works normally, I get correct readData array. But if I call spiTranfer () in an interrupt my system gets dumped

ESP_Sprite
Posts: 9746
Joined: Thu Nov 26, 2015 4:08 am

Re: Communicating with spi with slave in interrupt, the system will be dumped

Postby ESP_Sprite » Fri Jan 29, 2021 7:10 am

thanhphuct wrote:
Fri Jan 29, 2021 6:32 am
But if I call spiTranfer () in an interrupt my system gets dumped
Easy solution: Don't do that. You can't call routines from ISRs unless they're specifically marked (either because they have _from_isr in the name or otherwise) to be OK to call from an interrupt.

thanhphuct
Posts: 3
Joined: Fri Jan 29, 2021 4:40 am

Re: Communicating with spi with slave in interrupt, the system will be dumped

Postby thanhphuct » Fri Jan 29, 2021 7:32 am

[quote=Easy solution: Don't do that. You can't call routines from ISRs unless they're specifically marked (either because they have _from_isr in the name or otherwise) to be OK to call from an interrupt.
[/quote]

So is there a way to distinguish which process can be called in the interrupt or not?

tam1111574
Posts: 3
Joined: Mon Jan 25, 2021 1:56 pm

Re: Communicating with spi with slave in interrupt, the system will be dumped

Postby tam1111574 » Fri Jan 29, 2021 9:53 am

ESP_Sprite wrote:
Fri Jan 29, 2021 7:10 am
thanhphuct wrote:
Fri Jan 29, 2021 6:32 am
But if I call spiTranfer () in an interrupt my system gets dumped
Easy solution: Don't do that. You can't call routines from ISRs unless they're specifically marked (either because they have _from_isr in the name or otherwise) to be OK to call from an interrupt.
Hi ESP_Sprite, I have same problem with him. I have a sensor with SPI interfaces. My sensor will trigger a signal (GPIO) when complete convert data.
I connect this signal of sensor into GPIO interrupt of ESP32, configure SPI with master mode and DMA function.
When I received a trigger signal, I will read convert data from sensor. To increase the performance of system, I put the code read data of sensor (spi_device_queue_trans) in ISRs function and system will be crashed. In spite of the ISRs function is mapped into IRAM_ATTR
So can you give me some advice about my case. ?.
Hope for your reply. Thank and best regards :)

ESP_Sprite
Posts: 9746
Joined: Thu Nov 26, 2015 4:08 am

Re: Communicating with spi with slave in interrupt, the system will be dumped

Postby ESP_Sprite » Mon Feb 01, 2021 3:30 am

thanhphuct: ...I answered that question in the text you quoted. Either the name contains something like _from_isr, or the ESP-IDF documentation indicate it's OK to call that function from an interrupt.

tam1111574: Exact same answer: do not do that in an interrupt. Instead, have a (high-priority) task waiting for e.g. a semaphore before queueing the transfer, then use the ISR to set the semaphore.

Who is online

Users browsing this forum: No registered users and 131 guests