RTOS Basics with TWAI and UART

espriot
Posts: 2
Joined: Sat Jul 10, 2021 10:46 am

RTOS Basics with TWAI and UART

Postby espriot » Fri Jan 05, 2024 8:34 pm

I am working on a project which involves receiving TWAI messages which are updated every 1ms and send them through UART.
As I do not have access to TWAI Transmitter at the moment, I am simulating incoming messages with a GPTimer running at 1ms and updating a global shared twai_message_t object.
Somehow it is working, but I am not getting my data in the correct order and maybe even missing some data. I wanted some help in understanding what is going on with my RTOS Tasks.

Here is the code:

Code: Select all

#include <stdio.h>
#include <stdlib.h>
#include <inttypes.h>
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
#include <freertos/semphr.h>
#include <freertos/queue.h>
#include "freertos/event_groups.h"
#include <driver/uart.h>
#include "driver/gptimer.h"
#include "driver/twai.h"
#include "esp_log.h"

/*Configuration Settings*/
enum { BUF_LEN = 20 };

/*Pins*/
#define TXD_PIN (GPIO_NUM_17)
#define RXD_PIN (GPIO_NUM_16)

/*Globals*/

#define UART_NUM UART_NUM_1
#define BUF_SIZE (1024)
#define TWAI_RX_TASK_PRIORITY (configMAX_PRIORITIES - 1)
#define UART_TX_TASK_PRIORITY (configMAX_PRIORITIES - 2)

static const char *Timer_TAG = "Timer";
static const char *TX_TASK_TAG = "TX_TASK";

static TaskHandle_t processing_task = NULL;
static SemaphoreHandle_t sem_done_reading = NULL;
static portMUX_TYPE spinlock = portMUX_INITIALIZER_UNLOCKED;
static portMUX_TYPE uartlock = portMUX_INITIALIZER_UNLOCKED;

gptimer_handle_t gptimer = NULL;

QueueHandle_t uart_tx_queue[2];		      // Two buffers for UART transmit
volatile int active_buffer = 0;
static volatile uint8_t buf_overrun = 0;      // Double buffer overrun flag

TaskHandle_t uart_tx_task_handle;

twai_message_t rx_message = { .identifier = 1,
                                .data_length_code = 3,
                                .data = {0, 0, 0, 0, 0, 0, 0, 0}
                              };

// Message struct to wrap strings for queue
typedef struct CAN_Serial_Frame {
  uint8_t sof;
  uint32_t timestamp;
  uint8_t dlc;
  uint32_t msg_id;
  uint8_t payload[8];
  uint8_t eof;
}CAN_Serial_Frame; 

CAN_Serial_Frame CSF = {0xAA,100,8,1,{0},0xBB};

/*Timer ISR*/
uint8_t update_flag = 1;
void IRAM_ATTR onTimer() {
  xQueueSend(uart_tx_queue[active_buffer], &rx_message, 0.001/portTICK_PERIOD_MS);
  static uint16_t idx = 0;
  
  BaseType_t task_woken = pdFALSE;
  portENTER_CRITICAL(&spinlock);
  if(update_flag){
    rx_message.data[0]++;
  }
  
  if(rx_message.data[0]>84){
    rx_message.data[0] = 0;
    // update_flag = 0;
  }
  // active_buffer = 1 - active_buffer;
  portEXIT_CRITICAL(&spinlock);
  xTaskNotifyFromISR(uart_tx_task_handle,0,eNoAction,&task_woken);

  if (task_woken == pdTRUE) {
        portYIELD_FROM_ISR(task_woken);
  }
}

void Data_Update_Task(void *parameters){

  // Start a timer to run ISR every 1 ms
  // %%% We move this here so it runs in core 0
  ESP_LOGI(Timer_TAG, "Create timer handle");
  
  gptimer_config_t timer_config = {
      .clk_src = GPTIMER_CLK_SRC_DEFAULT,
      .direction = GPTIMER_COUNT_UP,
      .resolution_hz = 1000000, // 1MHz, 1 tick=1us
  };
  ESP_ERROR_CHECK(gptimer_new_timer(&timer_config, &gptimer));

  gptimer_event_callbacks_t cbs = {
      .on_alarm = onTimer,
  };
  ESP_ERROR_CHECK(gptimer_register_event_callbacks(gptimer, &cbs, NULL));

  ESP_LOGI(Timer_TAG, "Enable timer");
  ESP_ERROR_CHECK(gptimer_enable(gptimer));

  ESP_LOGI(Timer_TAG, "Start timer, auto-reload at alarm event");
  gptimer_alarm_config_t alarm_config = {
      .reload_count = 0,
      .alarm_count = 1000, // period = 1ms
      .flags.auto_reload_on_alarm = true,
  };

  ESP_ERROR_CHECK(gptimer_set_alarm_action(gptimer, &alarm_config));
  ESP_ERROR_CHECK(gptimer_start(gptimer));

  while(1){

  }
}

/*UART*/
void uart_init() {
    uart_config_t uart_config = {
        .baud_rate = 921600,
        .data_bits = UART_DATA_8_BITS,
        .parity    = UART_PARITY_DISABLE,
        .stop_bits = UART_STOP_BITS_1,
        .flow_ctrl = UART_HW_FLOWCTRL_DISABLE
    };

    uart_param_config(UART_NUM, &uart_config);
    ESP_ERROR_CHECK( uart_set_pin(UART_NUM, TXD_PIN, RXD_PIN, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE) );
    
    // Install the UART driver with a buffer size of 1024 bytes
    ESP_ERROR_CHECK( uart_driver_install(UART_NUM, 1024, 1024*4, 0, NULL, 0) );
    ESP_LOGI(TX_TASK_TAG, "UART Driver Installed");
}

void uart_tx_task(void *parameters){
  uart_init();
  twai_message_t tx_message;
  while (1) {
    // Process all available TWAI messages in the active buffer
    ulTaskNotifyTake(pdTRUE, portMAX_DELAY);
    ESP_ERROR_CHECK (xQueueReceive(uart_tx_queue[active_buffer], &tx_message, portMAX_DELAY) );

    // Process the TWAI message and send it over UART
    CSF.dlc = tx_message.data_length_code;
    CSF.msg_id = tx_message.identifier;
    for (int i = 0; i < tx_message.data_length_code && i < sizeof(CSF.payload); i++) {
        CSF.payload[i] = tx_message.data[i];
    }
    // ESP_ERROR_CHECK( uart_write_bytes(UART_NUM, (const char *)&CSF, sizeof(CSF)) );
    ESP_LOGI(TX_TASK_TAG, "Data Sent: %d",CSF.payload[0]);
  }
}

void app_main(void){

  /*Create two queues for double buffering of UART transmit*/
  uart_tx_queue[0] = xQueueCreate(100, sizeof(twai_message_t));
  uart_tx_queue[1] = xQueueCreate(100, sizeof(twai_message_t));

  /*Initialize TWAI*/
  // twai_general_config_t g_config = TWAI_GENERAL_CONFIG_DEFAULT(TWAI_RX_PIN_CONFIG);
  // twai_init(&g_config);

  /*Create TWAI Receive Task pinned to core 0*/
  xTaskCreate(Data_Update_Task, "twai_receive_task", 1024*5, NULL, TWAI_RX_TASK_PRIORITY, &processing_task);
  /*Create UART Transmit Task pinned to core 1*/
  xTaskCreate(uart_tx_task, "uart_tx_task", 1024*5, NULL, UART_TX_TASK_PRIORITY, &uart_tx_task_handle);

}


Here is the output:

Code: Select all

I (4004) TX_TASK: Data Sent: 12
I (4004) TX_TASK: Data Sent: 16
I (4014) TX_TASK: Data Sent: 38
I (4014) TX_TASK: Data Sent: 42
I (4014) TX_TASK: Data Sent: 70
I (4024) TX_TASK: Data Sent: 74
I (4024) TX_TASK: Data Sent: 78
I (4034) TX_TASK: Data Sent: 82
I (4034) TX_TASK: Data Sent: 19
I (4034) TX_TASK: Data Sent: 23
I (4044) TX_TASK: Data Sent: 46
I (4044) TX_TASK: Data Sent: 50
I (4044) TX_TASK: Data Sent: 54
I (4054) TX_TASK: Data Sent: 80
I (4054) TX_TASK: Data Sent: 84
I (4064) TX_TASK: Data Sent: 27
I (4064) TX_TASK: Data Sent: 31
I (4064) TX_TASK: Data Sent: 35
I (4074) TX_TASK: Data Sent: 57
I (4074) TX_TASK: Data Sent: 61
I (4084) TX_TASK: Data Sent: 65
I (4084) TX_TASK: Data Sent: 3
I (4084) TX_TASK: Data Sent: 7
I (4094) TX_TASK: Data Sent: 11
I (4094) TX_TASK: Data Sent: 33
I (4094) TX_TASK: Data Sent: 69
I (4104) TX_TASK: Data Sent: 73
I (4104) TX_TASK: Data Sent: 77
I (4114) TX_TASK: Data Sent: 14
I (4114) TX_TASK: Data Sent: 18
I (4114) TX_TASK: Data Sent: 22
I (4124) TX_TASK: Data Sent: 37
I (4124) TX_TASK: Data Sent: 41
I (4124) TX_TASK: Data Sent: 45
I (4134) TX_TASK: Data Sent: 49
I (4134) TX_TASK: Data Sent: 53
I (4144) TX_TASK: Data Sent: 26
I (4144) TX_TASK: Data Sent: 30
I (4144) TX_TASK: Data Sent: 56
I (4154) TX_TASK: Data Sent: 60
I (4154) TX_TASK: Data Sent: 64
I (4164) TX_TASK: Data Sent: 68
I (4164) TX_TASK: Data Sent: 75
I (4164) TX_TASK: Data Sent: 79
I (4174) TX_TASK: Data Sent: 83
I (4174) TX_TASK: Data Sent: 2
I (4174) TX_TASK: Data Sent: 6
I (4184) TX_TASK: Data Sent: 72
I (4184) TX_TASK: Data Sent: 9
I (4194) TX_TASK: Data Sent: 13
I (4194) TX_TASK: Data Sent: 17
I (4194) TX_TASK: Data Sent: 21
I (4204) TX_TASK: Data Sent: 28
I (4204) TX_TASK: Data Sent: 32
I (4204) TX_TASK: Data Sent: 36
I (4214) TX_TASK: Data Sent: 40
I (4214) TX_TASK: Data Sent: 44
I (4224) TX_TASK: Data Sent: 25
I (4224) TX_TASK: Data Sent: 47
I (4224) TX_TASK: Data Sent: 51
I (4234) TX_TASK: Data Sent: 55
I (4234) TX_TASK: Data Sent: 59
I (4244) TX_TASK: Data Sent: 63
I (4244) TX_TASK: Data Sent: 70
I (4244) TX_TASK: Data Sent: 74
I (4254) TX_TASK: Data Sent: 78
I (4254) TX_TASK: Data Sent: 82
I (4254) TX_TASK: Data Sent: 1
I (4264) TX_TASK: Data Sent: 67
I (4264) TX_TASK: Data Sent: 4
I (4274) TX_TASK: Data Sent: 8
I (4274) TX_TASK: Data Sent: 12
I (4274) TX_TASK: Data Sent: 19
I (4284) TX_TASK: Data Sent: 23
I (4284) TX_TASK: Data Sent: 27
I (4284) TX_TASK: Data Sent: 31
I (4294) TX_TASK: Data Sent: 35
I (4294) TX_TASK: Data Sent: 39
I (4304) TX_TASK: Data Sent: 16
I (4304) TX_TASK: Data Sent: 42
I (4304) TX_TASK: Data Sent: 46
I (4314) TX_TASK: Data Sent: 50
I (4314) TX_TASK: Data Sent: 54
I (4314) TX_TASK: Data Sent: 65
I (4324) TX_TASK: Data Sent: 69
I (4324) TX_TASK: Data Sent: 73
I (4334) TX_TASK: Data Sent: 77
I (4334) TX_TASK: Data Sent: 81
I (4334) TX_TASK: Data Sent: 18
I (4344) TX_TASK: Data Sent: 58
I (4344) TX_TASK: Data Sent: 62
I (4354) TX_TASK: Data Sent: 84
I (4354) TX_TASK: Data Sent: 3
I (4354) TX_TASK: Data Sent: 7
I (4364) TX_TASK: Data Sent: 22
I (4364) TX_TASK: Data Sent: 26
I (4364) TX_TASK: Data Sent: 30
I (4374) TX_TASK: Data Sent: 34
I (4374) TX_TASK: Data Sent: 60
I (4384) TX_TASK: Data Sent: 11
I (4384) TX_TASK: Data Sent: 15
I (4384) TX_TASK: Data Sent: 37
I (4394) TX_TASK: Data Sent: 41
I (4394) TX_TASK: Data Sent: 45
I (4394) TX_TASK: Data Sent: 49
I (4404) TX_TASK: Data Sent: 64
I (4404) TX_TASK: Data Sent: 68
I (4414) TX_TASK: Data Sent: 72
I (4414) TX_TASK: Data Sent: 76
I (4414) TX_TASK: Data Sent: 13
I (4424) TX_TASK: Data Sent: 53
I (4424) TX_TASK: Data Sent: 57
I (4434) TX_TASK: Data Sent: 79
I (4434) TX_TASK: Data Sent: 83
I (4434) TX_TASK: Data Sent: 2
I (4444) TX_TASK: Data Sent: 17
I (4444) TX_TASK: Data Sent: 21
I (4444) TX_TASK: Data Sent: 25
I (4454) TX_TASK: Data Sent: 29
I (4454) TX_TASK: Data Sent: 51
I (4464) TX_TASK: Data Sent: 55
I (4464) TX_TASK: Data Sent: 6
I (4464) TX_TASK: Data Sent: 10
I (4474) TX_TASK: Data Sent: 32
I (4474) TX_TASK: Data Sent: 36
I (4484) TX_TASK: Data Sent: 40
I (4484) TX_TASK: Data Sent: 59
I (4484) TX_TASK: Data Sent: 63
I (4494) TX_TASK: Data Sent: 67
I (4494) TX_TASK: Data Sent: 71
I (4494) TX_TASK: Data Sent: 8
I (4504) TX_TASK: Data Sent: 44
I (4504) TX_TASK: Data Sent: 48
I (4514) TX_TASK: Data Sent: 74
I (4514) TX_TASK: Data Sent: 78
I (4514) TX_TASK: Data Sent: 82
I (4524) TX_TASK: Data Sent: 12
I (4524) TX_TASK: Data Sent: 16
I (4524) TX_TASK: Data Sent: 20
I (4534) TX_TASK: Data Sent: 24
I (4534) TX_TASK: Data Sent: 28
I (4544) TX_TASK: Data Sent: 46
I (4544) TX_TASK: Data Sent: 1
I (4544) TX_TASK: Data Sent: 5
I (4554) TX_TASK: Data Sent: 31
I (4554) TX_TASK: Data Sent: 35
I (4554) TX_TASK: Data Sent: 39
I (4564) TX_TASK: Data Sent: 50
I (4564) TX_TASK: Data Sent: 54
I (4574) TX_TASK: Data Sent: 58
I (4574) TX_TASK: Data Sent: 62
I (4574) TX_TASK: Data Sent: 66
I (4584) TX_TASK: Data Sent: 43
I (4584) TX_TASK: Data Sent: 69
I (4594) TX_TASK: Data Sent: 73
I (4594) TX_TASK: Data Sent: 77
I (4594) TX_TASK: Data Sent: 81
I (4604) TX_TASK: Data Sent: 0
I (4604) TX_TASK: Data Sent: 3
I (4604) TX_TASK: Data Sent: 7
I (4614) TX_TASK: Data Sent: 11
I (4614) TX_TASK: Data Sent: 15
I (4624) TX_TASK: Data Sent: 26
I (4624) TX_TASK: Data Sent: 30
I (4624) TX_TASK: Data Sent: 34
I (4634) TX_TASK: Data Sent: 38
I (4634) TX_TASK: Data Sent: 60
I (4634) TX_TASK: Data Sent: 19
I (4644) TX_TASK: Data Sent: 23
I (4644) TX_TASK: Data Sent: 41
I (4654) TX_TASK: Data Sent: 45
I (4654) TX_TASK: Data Sent: 49
I (4654) TX_TASK: Data Sent: 53
I (4664) TX_TASK: Data Sent: 64
I (4664) TX_TASK: Data Sent: 68
I (4674) TX_TASK: Data Sent: 72
I (4674) TX_TASK: Data Sent: 76
I (4674) TX_TASK: Data Sent: 17
I (4684) TX_TASK: Data Sent: 57
I (4684) TX_TASK: Data Sent: 79
I (4684) TX_TASK: Data Sent: 83
I (4694) TX_TASK: Data Sent: 2
I (4694) TX_TASK: Data Sent: 6
I (4704) TX_TASK: Data Sent: 21
I (4704) TX_TASK: Data Sent: 25
I (4704) TX_TASK: Data Sent: 29
I (4714) TX_TASK: Data Sent: 33
I (4714) TX_TASK: Data Sent: 55
I (4714) TX_TASK: Data Sent: 10
I (4724) TX_TASK: Data Sent: 14
I (4724) TX_TASK: Data Sent: 36
I (4734) TX_TASK: Data Sent: 40
I (4734) TX_TASK: Data Sent: 44
I (4734) TX_TASK: Data Sent: 48
I (4744) TX_TASK: Data Sent: 59
I (4744) TX_TASK: Data Sent: 63
I (4754) TX_TASK: Data Sent: 67
I (4754) TX_TASK: Data Sent: 71
I (4754) TX_TASK: Data Sent: 8
I (4764) TX_TASK: Data Sent: 52
I (4764) TX_TASK: Data Sent: 74
I (4764) TX_TASK: Data Sent: 78
I (4774) TX_TASK: Data Sent: 82
I (4774) TX_TASK: Data Sent: 1
I (4784) TX_TASK: Data Sent: 12
I (4784) TX_TASK: Data Sent: 16
I (4784) TX_TASK: Data Sent: 20
I (4794) TX_TASK: Data Sent: 24
I (4794) TX_TASK: Data Sent: 28
I (4794) TX_TASK: Data Sent: 50
I (4804) TX_TASK: Data Sent: 5
I (4804) TX_TASK: Data Sent: 31
I (4814) TX_TASK: Data Sent: 35
I (4814) TX_TASK: Data Sent: 39
I (4814) TX_TASK: Data Sent: 43
I (4824) TX_TASK: Data Sent: 54
I (4824) TX_TASK: Data Sent: 58
I (4834) TX_TASK: Data Sent: 62
I (4834) TX_TASK: Data Sent: 66
I (4834) TX_TASK: Data Sent: 70
I (4844) TX_TASK: Data Sent: 47
I (4844) TX_TASK: Data Sent: 73
I (4844) TX_TASK: Data Sent: 77
I (4854) TX_TASK: Data Sent: 81
I (4854) TX_TASK: Data Sent: 0
I (4864) TX_TASK: Data Sent: 3
I (4864) TX_TASK: Data Sent: 7
I (4864) TX_TASK: Data Sent: 11
I (4874) TX_TASK: Data Sent: 15
I (4874) TX_TASK: Data Sent: 19
I (4874) TX_TASK: Data Sent: 26
I (4884) TX_TASK: Data Sent: 30
I (4884) TX_TASK: Data Sent: 34
I (4894) TX_TASK: Data Sent: 38
I (4894) TX_TASK: Data Sent: 42
I (4894) TX_TASK: Data Sent: 23
I (4904) TX_TASK: Data Sent: 45
I (4904) TX_TASK: Data Sent: 49
I (4914) TX_TASK: Data Sent: 53
I (4914) TX_TASK: Data Sent: 57
I (4914) TX_TASK: Data Sent: 61
I (4924) TX_TASK: Data Sent: 68
I (4924) TX_TASK: Data Sent: 72
I (4924) TX_TASK: Data Sent: 76
I (4934) TX_TASK: Data Sent: 80
I (4934) TX_TASK: Data Sent: 84
I (4944) TX_TASK: Data Sent: 65
I (4944) TX_TASK: Data Sent: 2
I (4944) TX_TASK: Data Sent: 6
I (4954) TX_TASK: Data Sent: 10
I (4954) TX_TASK: Data Sent: 14
I (4954) TX_TASK: Data Sent: 21
I (4964) TX_TASK: Data Sent: 25
I (4964) TX_TASK: Data Sent: 29
I (4974) TX_TASK: Data Sent: 33
I (4974) TX_TASK: Data Sent: 37
I (4974) TX_TASK: Data Sent: 63
I (4984) TX_TASK: Data Sent: 18
I (4984) TX_TASK: Data Sent: 40
I (4994) TX_TASK: Data Sent: 44
I (4994) TX_TASK: Data Sent: 48
I (4994) TX_TASK: Data Sent: 52
I (5004) TX_TASK: Data Sent: 67
I (5004) TX_TASK: Data Sent: 71
I (5004) TX_TASK: Data Sent: 75
I (5014) TX_TASK: Data Sent: 79
I (5014) TX_TASK: Data Sent: 12
I (5024) TX_TASK: Data Sent: 16
I (5024) TX_TASK: Data Sent: 56
I (5024) TX_TASK: Data Sent: 60
I (5034) TX_TASK: Data Sent: 82
I (5034) TX_TASK: Data Sent: 1
I (5034) TX_TASK: Data Sent: 5
I (5044) TX_TASK: Data Sent: 20
I (5044) TX_TASK: Data Sent: 24
I (5054) TX_TASK: Data Sent: 28
I (5054) TX_TASK: Data Sent: 32
I (5054) TX_TASK: Data Sent: 54
I (5064) TX_TASK: Data Sent: 9
I (5064) TX_TASK: Data Sent: 35
I (5074) TX_TASK: Data Sent: 39
I (5074) TX_TASK: Data Sent: 43
I (5074) TX_TASK: Data Sent: 47
I (5084) TX_TASK: Data Sent: 58
I (5084) TX_TASK: Data Sent: 62
I (5084) TX_TASK: Data Sent: 66
I (5094) TX_TASK: Data Sent: 70
I (5094) TX_TASK: Data Sent: 74
I (5104) TX_TASK: Data Sent: 51
I (5104) TX_TASK: Data Sent: 77
I (5104) TX_TASK: Data Sent: 81
I (5114) TX_TASK: Data Sent: 0
I (5114) TX_TASK: Data Sent: 4
I (5114) TX_TASK: Data Sent: 11
I (5124) TX_TASK: Data Sent: 15
I (5124) TX_TASK: Data Sent: 19
I (5134) TX_TASK: Data Sent: 23
I (5134) TX_TASK: Data Sent: 27
I (5134) TX_TASK: Data Sent: 8
I (5144) TX_TASK: Data Sent: 30
I (5144) TX_TASK: Data Sent: 34
I (5154) TX_TASK: Data Sent: 38
I (5154) TX_TASK: Data Sent: 42
I (5154) TX_TASK: Data Sent: 53
I (5164) TX_TASK: Data Sent: 57
I (5164) TX_TASK: Data Sent: 61
I (5164) TX_TASK: Data Sent: 65
I (5174) TX_TASK: Data Sent: 69
I (5174) TX_TASK: Data Sent: 6
I (5184) TX_TASK: Data Sent: 46
I (5184) TX_TASK: Data Sent: 50
I (5184) TX_TASK: Data Sent: 72
I (5194) TX_TASK: Data Sent: 76
I (5194) TX_TASK: Data Sent: 80
I (5194) TX_TASK: Data Sent: 10
I (5204) TX_TASK: Data Sent: 14
I (5204) TX_TASK: Data Sent: 18
I (5214) TX_TASK: Data Sent: 22
I (5214) TX_TASK: Data Sent: 26
I (5214) TX_TASK: Data Sent: 48
I (5224) TX_TASK: Data Sent: 84
I (5224) TX_TASK: Data Sent: 3
I (5234) TX_TASK: Data Sent: 29
I (5234) TX_TASK: Data Sent: 33
I (5234) TX_TASK: Data Sent: 37
I (5244) TX_TASK: Data Sent: 52
I (5244) TX_TASK: Data Sent: 56
I (5244) TX_TASK: Data Sent: 60
I (5254) TX_TASK: Data Sent: 64
I (5254) TX_TASK: Data Sent: 1
I (5264) TX_TASK: Data Sent: 41
I (5264) TX_TASK: Data Sent: 45
I (5264) TX_TASK: Data Sent: 67
I (5274) TX_TASK: Data Sent: 71
I (5274) TX_TASK: Data Sent: 75
I (5274) TX_TASK: Data Sent: 5
I (5284) TX_TASK: Data Sent: 9
I (5284) TX_TASK: Data Sent: 13
I (5294) TX_TASK: Data Sent: 17
I (5294) TX_TASK: Data Sent: 21
I (5294) TX_TASK: Data Sent: 79
I (5304) TX_TASK: Data Sent: 83
I (5304) TX_TASK: Data Sent: 24
I (5314) TX_TASK: Data Sent: 28
I (5314) TX_TASK: Data Sent: 32
I (5314) TX_TASK: Data Sent: 43
I (5324) TX_TASK: Data Sent: 47
I (5324) TX_TASK: Data Sent: 51
I (5324) TX_TASK: Data Sent: 55
I (5334) TX_TASK: Data Sent: 59
I (5334) TX_TASK: Data Sent: 0
I (5344) TX_TASK: Data Sent: 36
I (5344) TX_TASK: Data Sent: 40
I (5344) TX_TASK: Data Sent: 62
I (5354) TX_TASK: Data Sent: 66
I (5354) TX_TASK: Data Sent: 70
I (5354) TX_E (5364) task_wdt: Task watchdog got triggered. The following tasks/users did not reset the watchdog in time:
E (5364) task_wdt:  - IDLE (CPU 1)
E (5364) task_wdt: Tasks currently running:
E (5364) task_wdt: CPU 0: IDLE
E (5364) task_wdt: CPU 1: uart_tx_task
E (5364) task_wdt: Print CPU 1 backtrace


Backtrace: 0x400844E2:0x3FFB1370 0x40082A91:0x3FFB1390 0x400D519B:0x3FFB8FA0 0x400D595F:0x3FFB8FD0 0x400D47DB:0x3FFB8FF0 0x400D43A2:0x3FFB9010 0x4000BD83:0x3FFB9030 0x4000117D:0x3FFB9050 0x400592FE:0x3FFB9070 0x4005937A:0x3FFB9090 0x400DB4A7:0x3FFB90B0 0x400E3243:0x3FFB90E0 0x400DEE3E:0x3FFB9100 0x400DEFA9:0x3FFB9420 0x400E7135:0x3FFB9450 0x4008BB41:0x3FFB9480 0x400D5B9F:0x3FFB94D0 0x40088D35:0x3FFB9510
0x400844e2: esp_crosscore_isr at C:/Users/shauryachandra/esp/esp-idf/components/esp_system/crosscore_int.c:96

0x40082a91: _xt_lowint1 at C:/Users/shauryachandra/esp/esp-idf/components/freertos/FreeRTOS-Kernel/portable/xtensa/xtensa_vectors.S:1240

0x400d519b: uart_ll_get_txfifo_len at C:/Users/shauryachandra/esp/esp-idf/components/hal/esp32/include/hal/uart_ll.h:313 (discriminator 1)
 (inlined by) uart_tx_char at C:/Users/shauryachandra/esp/esp-idf/components/vfs/vfs_uart.c:168 (discriminator 1)   

0x400d595f: uart_write at C:/Users/shauryachandra/esp/esp-idf/components/vfs/vfs_uart.c:221

0x400d47db: console_write at C:/Users/shauryachandra/esp/esp-idf/components/vfs/vfs_console.c:73

0x400d43a2: esp_vfs_write at C:/Users/shauryachandra/esp/esp-idf/components/vfs/vfs.c:445 (discriminator 4)

0x4000bd83: _write_r in ROM

0x4000117d: __swrite in ROM

0x400592fe: __sflush_r in ROM

0x4005937a: _fflush_r in ROM

0x400db4a7: __sfvwrite_r at /builds/idf/crosstool-NG/.build/HOST-x86_64-w64-mingw32/xtensa-esp32-elf/src/newlib/newlib/libc/stdio/fvwrite.c:251

0x400e3243: __sprint_r at /builds/idf/crosstool-NG/.build/HOST-x86_64-w64-mingw32/xtensa-esp32-elf/src/newlib/newlib/libc/stdio/vfprintf.c:429
 (inlined by) __sprint_r at /builds/idf/crosstool-NG/.build/HOST-x86_64-w64-mingw32/xtensa-esp32-elf/src/newlib/newlib/libc/stdio/vfprintf.c:399

0x400dee3e: _vfprintf_r at /builds/idf/crosstool-NG/.build/HOST-x86_64-w64-mingw32/xtensa-esp32-elf/src/newlib/newlib/libc/stdio/vfprintf.c:1777 (discriminator 1)

0x400defa9: vprintf at /builds/idf/crosstool-NG/.build/HOST-x86_64-w64-mingw32/xtensa-esp32-elf/src/newlib/newlib/libc/stdio/vprintf.c:34 (discriminator 5)

0x400e7135: esp_log_writev at C:/Users/shauryachandra/esp/esp-idf/components/log/log.c:200

0x4008bb41: esp_log_write at C:/Users/shauryachandra/esp/esp-idf/components/log/log.c:210

0x400d5b9f: uart_tx_task at C:/Users/shauryachandra/Timer_UART_MultiCore/main/test.c:237 (discriminator 13)

0x40088d35: vPortTaskWrapper at C:/Users/shauryachandra/esp/esp-idf/components/freertos/FreeRTOS-Kernel/portable/xtensa/port.c:162


TASK: Data Sent: 4
I (5434) TX_TASK: Data Sent: 8
I (5434) TX_TASK: Data Sent: 74
I (5434) TX_TASK: Data Sent: 78
I (5444) TX_TASK: Data Sent: 82
I (5444) TX_TASK: Data Sent: 15
I (5454) TX_TASK: Data Sent: 19
I (5454) TX_TASK: Data Sent: 12
I (5454) TX_TASK: Data Sent: 34
I (5464) TX_TASK: Data Sent: 38
I (5464) TX_TASK: Data Sent: 42
I (5464) TX_TASK: Data Sent: 46
I (5474) TX_TASK: Data Sent: 50
I (5474) TX_TASK: Data Sent: 23
I (5484) TX_TASK: Data Sent: 27
I (5484) TX_TASK: Data Sent: 31
I (5484) TX_TASK: Data Sent: 57
I (5494) TX_TASK: Data Sent: 61
I (5494) TX_TASK: Data Sent: 54
I (5504) TX_TASK: Data Sent: 76
I (5504) TX_TASK: Data Sent: 80
I (5504) TX_TASK: Data Sent: 84
I (5514) TX_TASK: Data Sent: 3
I (5514) TX_TASK: Data Sent: 65
I (5514) TX_TASK: Data Sent: 69
I (5524) TX_TASK: Data Sent: 73
I (5524) TX_TASK: Data Sent: 10
I (5534) TX_TASK: Data Sent: 14
I (5534) TX_TASK: Data Sent: 18
I (5534) TX_TASK: Data Sent: 7
I (5544) TX_TASK: Data Sent: 29
I (5544) TX_TASK: Data Sent: 33
I (5544) TX_TASK: Data Sent: 37
I (5554) TX_TASK: Data Sent: 41
I (5554) TX_TASK: Data Sent: 22
I (5564) TX_TASK: Data Sent: 26
I (5564) TX_TASK: Data Sent: 52
I (5564) TX_TASK: Data Sent: 56
I (5574) TX_TASK: Data Sent: 60
I (5574) TX_TASK: Data Sent: 64
I (5584) TX_TASK: Data Sent: 45
I (5584) TX_TASK: Data Sent: 49
I (5584) TX_TASK: Data Sent: 71
I (5594) TX_TASK: Data Sent: 75
I (5594) TX_TASK: Data Sent: 79
I (5594) TX_TASK: Data Sent: 68
I (5604) TX_TASK: Data Sent: 5
I (5604) TX_TASK: Data Sent: 9
I (5614) TX_TASK: Data Sent: 13
I (5614) TX_TASK: Data Sent: 17
I (5614) TX_TASK: Data Sent: 83
I (5624) TX_TASK: Data Sent: 2
I (5624) TX_TASK: Data Sent: 28
I (5624) TX_TASK: Data Sent: 32
I (5634) TX_TASK: Data Sent: 36
I (5634) TX_TASK: Data Sent: 21
I (5644) TX_TASK: Data Sent: 25
I (5644) TX_TASK: Data Sent: 47
I (5644) TX_TASK: Data Sent: 51
I (5654) TX_TASK: Data Sent: 55
I (5654) TX_TASK: Data Sent: 40
I (5664) TX_TASK: Data Sent: 44
I (5664) TX_TASK: Data Sent: 66
I (5664) TX_TASK: Data Sent: 70
I (5674) TX_TASK: Data Sent: 74
I (5674) TX_TASK: Data Sent: 78
I (5674) TX_TASK: Data Sent: 59
I (5684) TX_TASK: Data Sent: 63
I (5684) TX_TASK: Data Sent: 0
I (5694) TX_TASK: Data Sent: 4
I (5694) TX_TASK: Data Sent: 82
I (5694) TX_TASK: Data Sent: 19
I (5704) TX_TASK: Data Sent: 23
I (5704) TX_TASK: Data Sent: 27
I (5704) TX_TASK: Data Sent: 31
I (5714) TX_TASK: Data Sent: 35
I (5714) TX_TASK: Data Sent: 8
I (5724) TX_TASK: Data Sent: 12
I (5724) TX_TASK: Data Sent: 16
I (5724) TX_TASK: Data Sent: 38
I (5734) TX_TASK: Data Sent: 42
I (5734) TX_TASK: Data Sent: 61
I (5744) TX_TASK: Data Sent: 65
I (5744) TX_TASK: Data Sent: 69
I (5744) TX_TASK: Data Sent: 73
I (5754) TX_TASK: Data Sent: 77
I (5754) TX_TASK: Data Sent: 46
I (5754) TX_TASK: Data Sent: 50
I (5764) TX_TASK: Data Sent: 54
I (5764) TX_TASK: Data Sent: 58
I (5774) TX_TASK: Data Sent: 80
I (5774) TX_TASK: Data Sent: 84
I (5774) TX_TASK: Data Sent: 14
I (5784) TX_TASK: Data Sent: 18
I (5784) TX_TASK: Data Sent: 22
I (5794) TX_TASK: Data Sent: 26
I (5794) TX_TASK: Data Sent: 30
I (5794) TX_TASK: Data Sent: 34
I (5804) TX_TASK: Data Sent: 3
I (5804) TX_TASK: Data Sent: 7
I (5804) TX_TASK: Data Sent: 11
I (5814) TX_TASK: Data Sent: 37
I (5814) TX_TASK: Data Sent: 56
I (5824) TX_TASK: Data Sent: 60
I (5824) TX_TASK: Data Sent: 64
I (5824) TX_TASK: Data Sent: 68
I (5834) TX_TASK: Data Sent: 72
I (5834) TX_TASK: Data Sent: 76
I (5834) TX_TASK: Data Sent: 41
I (5844) TX_TASK: Data Sent: 45
I (5844) TX_TASK: Data Sent: 49
I (5854) TX_TASK: Data Sent: 53
I (5854) TX_TASK: Data Sent: 79
I (5854) TX_TASK: Data Sent: 13
I (5864) TX_TASK: Data Sent: 17
I (5864) TX_TASK: Data Sent: 21
I (5874) TX_TASK: Data Sent: 25
I (5874) TX_TASK: Data Sent: 29
I (5874) TX_TASK: Data Sent: 51
I (5884) TX_TASK: Data Sent: 83
I (5884) TX_TASK: Data Sent: 2
I (5884) TX_TASK: Data Sent: 6
I (5894) TX_TASK: Data Sent: 10
I (5894) TX_TASK: Data Sent: 55
I (5904) TX_TASK: Data Sent: 59
I (5904) TX_TASK: Data Sent: 63
I (5904) TX_TASK: Data Sent: 67
I (5914) TX_TASK: Data Sent: 4
I (5914) TX_TASK: Data Sent: 32
I (5914) TX_TASK: Data Sent: 36
I (5924) TX_TASK: Data Sent: 40
I (5924) TX_TASK: Data Sent: 44
I (5934) TX_TASK: Data Sent: 48
I (5934) TX_TASK: Data Sent: 70
I (5934) TX_TASK: Data Sent: 8
I (5944) TX_TASK: Data Sent: 12
I (5944) TX_TASK: Data Sent: 16
I (5944) TX_TASK: Data Sent: 20
I (5954) TX_TASK: Data Sent: 42
I (5954) TX_TASK: Data Sent: 74
I (5964) TX_TASK: Data Sent: 78
I (5964) TX_TASK: Data Sent: 82
I (5964) TX_TASK: Data Sent: 1
I (5974) TX_TASK: Data Sent: 23
I (5974) TX_TASK: Data Sent: 46
I (5984) TX_TASK: Data Sent: 50
I (5984) TX_TASK: Data Sent: 54
I (5984) TX_TASK: Data Sent: 58
I (5994) TX_TASK: Data Sent: 62
I (5994) TX_TASK: Data Sent: 84
I (5994) TX_TASK: Data Sent: 27
I (6004) TX_TASK: Data Sent: 31
I (6004) TX_TASK: Data Sent: 35
I (6014) TX_TASK: Data Sent: 39
I (6014) TX_TASK: Data Sent: 65
I (6014) TX_TASK: Data Sent: 3
I (6024) TX_TASK: Data Sent: 7
I (6024) TX_TASK: Data Sent: 11
I (6024) TX_TASK: Data Sent: 15
I (6034) TX_TASK: Data Sent: 19
I (6034) TX_TASK: Data Sent: 69
I (6044) TX_TASK: Data Sent: 73
I (6044) TX_TASK: Data Sent: 77
I (6044) TX_TASK: Data Sent: 81
I (6054) TX_TASK: Data Sent: 22
I (6054) TX_TASK: Data Sent: 41
I (6064) TX_TASK: Data Sent: 45
I (6064) TX_TASK: Data Sent: 49
I (6064) TX_TASK: Data Sent: 53
I (6074) TX_TASK: Data Sent: 57
I (6074) TX_TASK: Data Sent: 79
I (6074) TX_TASK: Data Sent: 26
I (6084) TX_TASK: Data Sent: 30
I (6084) TX_TASK: Data Sent: 34
I (6094) TX_TASK: Data Sent: 38
I (6094) TX_TASK: Data Sent: 60
I (6094) TX_TASK: Data Sent: 83
I (6104) TX_TASK: Data Sent: 2
I (6104) TX_TASK: Data Sent: 6
I (6104) TX_TASK: Data Sent: 10
I (6114) TX_TASK: Data Sent: 32
I (6114) TX_TASK: Data Sent: 64
I (6124) TX_TASK: Data Sent: 68
I (6124) TX_TASK: Data Sent: 72
I (6124) TX_TASK: Data Sent: 76
I (6134) TX_TASK: Data Sent: 13
I (6134) TX_TASK: Data Sent: 36
I (6144) TX_TASK: Data Sent: 40
I (6144) TX_TASK: Data Sent: 44
I (6144) TX_TASK: Data Sent: 48
I (6154) TX_TASK: Data Sent: 52
I (6154) TX_TASK: Data Sent: 74
I (6154) TX_TASK: Data Sent: 17
I (6164) TX_TASK: Data Sent: 21
I (6164) TX_TASK: Data Sent: 25
I (6174) TX_TASK: Data Sent: 29
I (6174) TX_TASK: Data Sent: 55
I (6174) TX_TASK: Data Sent: 31
I (6184) TX_TASK: Data Sent: 53
I (6184) TX_TASK: Data Sent: 57
I (6194) TX_TASK: Data Sent: 61
I (6194) TX_TASK: Data Sent: 65
I (6194) TX_TASK: Data Sent: 69
I (6204) TX_TASK: Data Sent: 59
I (6204) TX_TASK: Data Sent: 63
I (6204) TX_TASK: Data Sent: 67
I (6214) TX_TASK: Data Sent: 71
I (6214) TX_TASK: Data Sent: 34
I (6224) TX_TASK: Data Sent: 38
I (6224) TX_TASK: Data Sent: 73
I (6224) TX_TASK: Data Sent: 10
I (6234) TX_TASK: Data Sent: 14
I (6234) TX_TASK: Data Sent: 18
I (6234) TX_TASK: Data Sent: 22
I (6244) TX_TASK: Data Sent: 42
I (6244) TX_TASK: Data Sent: 46
I (6254) TX_TASK: Data Sent: 50
I (6254) TX_TASK: Data Sent: 76
I (6254) TX_TASK: Data Sent: 80
I (6264) TX_TASK: Data Sent: 84
I (6264) TX_TASK: Data Sent: 26
I (6274) TX_TASK: Data Sent: 52
I (6274) TX_TASK: Data Sent: 56
I (6274) TX_TASK: Data Sent: 60
I (6284) TX_TASK: Data Sent: 64
I (6284) TX_TASK: Data Sent: 3
I (6284) TX_TASK: Data Sent: 7
I (6294) TX_TASK: Data Sent: 29
I (6294) TX_TASK: Data Sent: 33
I (6304) TX_TASK: Data Sent: 37
I (6304) TX_TASK: Data Sent: 68
I (6304) TX_TASK: Data Sent: 9
I (6314) TX_TASK: Data Sent: 13
I (6314) TX_TASK: Data Sent: 17
I (6314) TX_TASK: Data Sent: 21
I (6324) TX_TASK: Data Sent: 41
I (6324) TX_TASK: Data Sent: 45
I (6334) TX_TASK: Data Sent: 49
I (6334) TX_TASK: Data Sent: 71
I (6334) TX_TASK: Data Sent: 75
I (6344) TX_TASK: Data Sent: 25
I (6344) TX_TASK: Data Sent: 47
I (6354) TX_TASK: Data Sent: 51
I (6354) TX_TASK: Data Sent: 55
I (6354) TX_TASK: Data Sent: 59
I (6364) TX_TASK: Data Sent: 63
I (6364) TX_TASK: Data Sent: 79
I (6364) TX_TASK: Data Sent: 83
I (6374) TX_TASK: Data Sent: 2
I (6374) TX_TASK: Data Sent: 6
I (6384) TX_TASK: Data Sent: 0

After around 5 seconds, the watchdog timer is also triggered.

If someone can please explain what is happening, it will be really helpful.

Thank you!

MicroController
Posts: 1705
Joined: Mon Oct 17, 2022 7:38 pm
Location: Europe, Germany

Re: RTOS Basics with TWAI and UART

Postby MicroController » Sat Jan 06, 2024 11:13 am

Data_Update_Task:

Code: Select all

  while(1){

  }
Will use all available CPU time, causing the watchdog to fire.
In fact, you don't need this task at all which only sets up the timer. Just set up and start the timer from app_main and be done.

onTimer:

Code: Select all

0.001/portTICK_PERIOD_MS
is not how to specify a timeout with FreeRTOS (will yield a 0-timeout in this case); and from an ISR context you shouldn't use blocking APIs at all (->xQueueSendFromISR(...)).

Code: Select all

QueueHandle_t uart_tx_queue[2];		      // Two buffers for UART transmit
...
/*Create two queues for double buffering of UART transmit*/
You should use a single queue; after all, a queue is specifically made to act as a FIFO buffer of up to N elements between tasks and/or ISRs.

Code: Select all

xTaskNotifyFromISR(uart_tx_task_handle,0,eNoAction,&task_woken);
Not needed because xQueueReceive(...) is blocking anyway.

Another thing to keep an eye on is the logging: Sending 1000x 30+ bytes of log output per second may be pushing or crossing the limit of what the logging sink (UART?) can sustainably process, eventually causing the log output to delay the logging task.

Who is online

Users browsing this forum: No registered users and 94 guests