Timer interruption is unstable with wifi thread

janet chung
Posts: 3
Joined: Wed May 12, 2021 9:02 am

Timer interruption is unstable with wifi thread

Postby janet chung » Sun Jun 13, 2021 12:38 pm

Hello, i have some confusing problems with timer interrupt and freertos thread.
i ran wifi thread in core 0 and spi thread in core 1.
i used timer group 0 with every 200us interrupted as hardware timer source.
In timer callback , i just put AD7606 convert in order to get accuracy sampling rate and gave notify to SPI thread.

## There is timer code

Code: Select all

void Timer_Init(timer_group_t Timer_Group, timer_idx_t Timer_id, uint32_t Divider, uint64_t Timer_Ovfl, bool Timer_reload, TaskHandle_t *Taskptr)
{
	//define Struct for ISR Pointer
	Timer_Spec_t *Timer_Specs_p = calloc(sizeof(Timer_Spec_t), 1);
	Timer_Specs_p->Taskptr = *Taskptr;
	Timer_Specs_p->Timer_id = Timer_id;
	
	//Timer Init. Group0, Timer0
	timer_config_t timer_basis;
	timer_basis.divider = Divider;
	timer_basis.counter_dir = TIMER_COUNT_UP;
	timer_basis.counter_en = TIMER_PAUSE;
	timer_basis.alarm_en = TIMER_ALARM_EN;
	timer_basis.auto_reload = Timer_reload;
	timer_basis.intr_type = TIMER_INTR_LEVEL;
	
	//Init Timer
	timer_init(Timer_Group, Timer_id, &WPC_timer_basis);
	//Set timer to start value
	timer_set_counter_value(Timer_Group, Timer_id, 0x00000000ULL);
	//Set timer alarm
	timer_set_alarm_value(Timer_Group, Timer_id, Timer_Ovfl);
	//Interrupt enable
	timer_enable_intr(Timer_Group, Timer_id);	
	timer_isr_register(Timer_Group, Timer_id, timer_group0_isr, (void *) Timer_Specs_p, ESP_INTR_FLAG_NMI || ESP_INTR_FLAG_IRAM, NULL);	
	//Timer start
	timer_start(Timer_Group, Timer_id);		
	
}
 
void IRAM_ATTR timer_group0_isr(void *para)
{ 
	//save the data from the pointer
	Timer_Spec_t *Settings =  (Timer_Spec_t *) para;
	//Get interrupt status
	uint32_t intr_status = TIMERG0.int_st_timers.val;
	//Delete interrupt flags 
        TIMERG0.int_clr_timers.t0 = 1;
	//Reactivate alarm
	TIMERG0.hw_timer[Settings->Timer_id].config.alarm_en = TIMER_ALARM_EN;	
	
 	gpio_set_level(GPIO_CONVERT, 0);
        gpio_set_level(GPIO_CONVERT, 1);
        xTaskNotifyFromISR(Settings->Taskptr,0x00,eNoAction,NULL);
        portYIELD_FROM_ISR(); 
} 
Then, SPI receive notify from timer callback.

Code: Select all

void spi_thread(void *pvParameters){ 
        spi_bus_config_t buscfg={
        .miso_io_num=GPIO_MISO,
        .mosi_io_num=GPIO_MOSI,
        .sclk_io_num=GPIO_SCLK,
        .quadwp_io_num=-1,
        .quadhd_io_num=-1
        };

        spi_device_interface_config_t devcfg = {
        .clock_speed_hz = 10000000,
        .mode = 0,
        .spics_io_num = GPIO_CS,
        .queue_size = 8
        };

        //Initialize the SPI bus
        ret = spi_bus_initialize(VSPI_HOST, &buscfg, 2);
        assert(ret == ESP_OK);
        ret = spi_bus_add_device(VSPI_HOST, &devcfg, &dev);
        assert(ret == ESP_OK);

   	uint32_t remain ;
  	 spi_transaction_t trans = {
        .length = 8*8,
        .tx_buffer = tx_data,
        .rx_buffer = (void*)rx_data };
   while(1){
        xTaskNotifyWait(0x00, 0xffffffff, NULL, portMAX_DELAY);
        ret = spi_device_transmit(dev, &trans); 
        assert(ret == ESP_OK); 
        remain = xStreamBufferBytesAvailable( xStreamBuffer);
        if ( remain <= 110000) { 
                xStreamBufferSend(WPC_xStreamBuffer, (void*)(trans.rx_buffer), 8 ,1);
        }  
   }
## there is wifi thread.

Code: Select all

void  wifi_thread(void *pvParameters) {
    wifi_station_initialze();
    uint16_t ReturnBuf;
    while (1){
        ReturnBuf = xStreamBufferReceive(WPC_xStreamBuffer, (void*)ESP32_Tx_buffer, 800 ,portMAX_DELAY);
        send(sock, ESP32_Tx_buffer, ReturnBuf, 0);     
    }
}

Code: Select all

void app_main(void){
    WPC_xStreamBuffer = xStreamBufferCreate(110000,1);
    char ESP32_Tx_buffer[30000];
    TaskHandle_t tcp_TaskHandle;
    TaskHandle_t spi_TaskHandle;
    TaskHandle_t wifi_TaskHandle;
    xTaskCreatePinnedToCore( tcp_thread, "tcp_thread", 2048, (void*)AF_INET, 3, &tcp_TaskHandle,0);   
    xTaskCreatePinnedToCore( wifi_thread," wifi_thread", 2048, (void*)AF_INET, 2, &wifi_TaskHandle, 0);  
    xTaskCreatePinnedToCore( spi_thread, " spi_thread", 2048, (void*)AF_INET, 1, &spi_TaskHandle,1);  
    Timer_Init(TIMER_GROUP_0, TIMER_0, 80 ,200, TIMER_AUTORELOAD_EN, &spi_TaskHandle);  
}
In Oscilloscope , timer interrupt callback was strange because i found GPIO convert pin will happen in spi transaction.
Assumption 1 : wifi thread priority is higher than timer callback ?
Assumption 2 : timer callback with RTOS is unstable
If my assumption are correct, how could i resolve this problem??
Or, i missed some important factors ? Please give me some advice or solutions to this problem .
Thanks a lot ;) ;)
Attachments
oscilloscope.png
oscilloscope.png (362.63 KiB) Viewed 3356 times

darkhouse
Posts: 4
Joined: Sat May 29, 2021 9:17 am

Re: Timer interruption is unstable with wifi thread

Postby darkhouse » Tue Jun 15, 2021 6:35 am

similar problem. :(

WiFive
Posts: 3529
Joined: Tue Dec 01, 2015 7:35 am

Re: Timer interruption is unstable with wifi thread

Postby WiFive » Tue Jun 15, 2021 7:37 am

Spi task priority should be higher

janet chung
Posts: 3
Joined: Wed May 12, 2021 9:02 am

Re: Timer interruption is unstable with wifi thread

Postby janet chung » Thu Jun 17, 2021 8:04 am

Dear @WiFive
Thanks for your response in advance. spi thread run different core with wifi thread. i also set higher priority to spi thread, but it is no help. Additionally, In LabVIEW, the data i received would lose byte.
Result.PNG
Result.PNG (18.65 KiB) Viewed 3227 times

Who is online

Users browsing this forum: No registered users and 85 guests