Quality RTOS & Embedded Software

 Real time embedded FreeRTOS RSS feed 
Quick Start Supported MCUs PDF Books Trace Tools Ecosystem


Socket Recv Interrupt on Atmel SAMW25

Posted by mutty00 on August 21, 2016


I'm expecting a little issue using FreeRTOS on my project that uses TCP socket on a SAMW25 system.

Here follows a simplified stub of code I'm using. The first part is the callback function that handle received message interrupt, the second part is the task that manage the received message.

~~~ static void socketcb(SOCKET sock, uint8t u8Msg, void pvMsg) { switch (u8Msg) { / Message receive */ case SOCKETMSGRECV: { tstrSocketRecvMsg *pstrRecv = (tstrSocketRecvMsg *)pvMsg; if (sock==tcpclientsocket) { if (pstrRecv && pstrRecv->s16BufferSize > 0) { BaseTypet xHigherPriorityTaskWoken=pdFALSE; portpinsetoutputlevel(LEDREDKEY,false); xTaskNotifyFromISR(xSerialTaskH,pstrRecv>s16BufferSize,eSetValueWithoutOverwrite,&xHigherPriorityTaskWoken ); } else { close(tcpserversocket); tcpserver_socket = -1; } }


default: break;

} ~~~

~~~ static void Task(void *params) {

for (;;) {

//Do Something

portDISABLEINTERRUPTS(); /// I added this to fix the issue..... recv(tcpclientsocket, TCPBuffer, sizeof(TCPBuffer), 0); xTaskNotifyWait( 0x00, ULONGMAX, &ulSize, portMAX_DELAY );

//endless repeat for incoming all messages

} } ~~~

The problem I have is that if I've many pending messages on Wifi, as soon as recv function is called, interrupt is generated. If interrupt if executed BEFORE xTaskNotifyWait is completed, the xTaskNotifyFromISR called form ISR is not effective and task is never waked up. The only solution I've fond at the moment is to DISABLE INTERRUPT before calling recv and xTaskNotifyWait ( that atomatically re-enable them.

I'd like to know if there are other solution for this.

Thank you.

Socket Recv Interrupt on Atmel SAMW25

Posted by rtel on August 21, 2016

Its is very hard to say as I don't know which TCP/IP stack you are using, where the code comes from, how the driver is implemented, etc....but if I were to guess that socket_cb() is called from an interrupt then I would also guess that functions such as close() probably cannot be called from interrupts.

Socket Recv Interrupt on Atmel SAMW25

Posted by mutty00 on August 21, 2016


probably I've not explained very well my problem. The question is not related to the specific TCP stack, is a more generic problem that I expected also in the past with FreeRTOS.

I try to provide you another example of more or less the same problem that I had on another project on AVR32 + FreeRTOS. In this case I have to read a 512 bytes sector from an SD card via SPI and DMA mode.Very simple operation, ho drivers of additional firmware library involved in this case.

Here follows the other example:

~~~ void vReadBlock(void *RamPointer) { xMMCPDCAHandle = xTaskGetCurrentTaskHandle();

AVR32PDCA.channel[8].mar = (unsigned int)RamPointer; AVR32PDCA.channel[8].TCR.tcv = 512; AVR32_PDCA.channel[8].CR.ten = 1;

AVR32PDCA.channel[9].mar = (unsigned int)ucMMCBufferTX; AVR32PDCA.channel[9].TCR.tcv = 512; _disableinterrupt(); ///<- !!!!!!!!! HERE I DISABLE INTERRUPT AVR32PDCA.channel[8].IER.trc = 1; AVR32PDCA.channel[9].CR.ten = 1;




pragma optimize = no_inline

static portBASETYPE vSPI1RxCompletedISR( void ) { AVR32PDCA.channel[8].IDR.trc = 1; AVR32SPI1.CR.lastxfer = 1; portENTERCRITICAL(); xTaskResumeFromISR(xMMCPDCAHandle); portEXITCRITICAL(); AVR32PDCA.channel[8].SR; //Dummy Read return pdFALSE; }//////////////////////////////////////////////////////////////////////////////// ~~~

The first function vReadBlock set and trigger the SPI DMA for SD Card reading ( pay attention only at the last 4 lines ): once DMA is triggered task suspend ifself waiting for operation completed.

The second function is the DMA interrupt that wakes vReadBlock when reading is completed. ( focus only in the xTaskResume function, other code is for DMA handling of AVR32)

Let's look at vReadBlock: suppose that data block to read is only few bytes and SPI data rate very high, and suppose that system have to process many other interrupt request from other prefipherals. Suppose also that I skip _disableinterrupt()

~~~ //_disableinterrupt();
AVR32PDCA.channel[8].IER.trc = 1; AVR32PDCA.channel[9].CR.ten = 1;

vTaskSuspend(NULL); ~~~

As soon as AVR32_PDCA.channel[9].CR.ten is executed DMA start tranfer. Suppose that now you have to serve some other interrupts that takes some times to be executed. ( suppose up to 10 ms ) Meantime DMA transfer is for sure completed and vSPI1RxCompletedISR is called and executed. xTaskResumeFromISR(xMMCPDCAHandle) inside ISR is also executed. Afted this execution of vReadBlock restarts and vTaskSuspend is executed, locking the task forever. ( because xTaskResume was already hit from DMA ISR )

This is avoided if i temporarly disable any int ( just for the time to execute 3rd and part of 4th code row :

~~~ _disableinterrupt();
AVR32PDCA.channel[8].IER.trc = 1; AVR32PDCA.channel[9].CR.ten = 1;

vTaskSuspend(NULL); ~~~

I hope now is more clear.

There is some other way to do that ??

Socket Recv Interrupt on Atmel SAMW25

Posted by davedoors on August 21, 2016

Dont use task suspends to trigger a task, see http://www.freertos.org/taskresumefromisr.html

Socket Recv Interrupt on Atmel SAMW25

Posted by mutty00 on August 22, 2016

Ok Dave. Seems to be fine.


[ Back to the top ]    [ About FreeRTOS ]    [ Sitemap ]    [ ]

Copyright (C) Amazon Web Services, Inc. or its affiliates. All rights reserved.

Latest News

Meet us at Embedded World. Hall 3A-525.

Hear from Richard Barry at Embedded World. Feb 28, 16:00, Hall 4-428.

Video: Watch James Gosling & Richard Barry at re:Invent, Las Vegas 2017.

FreeRTOS kernel V10.0.1 is available for immediate download. Now MIT licensed.

FreeRTOS Partners

ARM Connected RTOS partner for all ARM microcontroller cores

IAR Partner

Microchip Premier RTOS Partner

RTOS partner of NXP for all NXP ARM microcontrollers

STMicro RTOS partner supporting ARM7, ARM Cortex-M3, ARM Cortex-M4 and ARM Cortex-M0

Texas Instruments MCU Developer Network RTOS partner for ARM and MSP430 microcontrollers

OpenRTOS and SafeRTOS