xTaskCreate - the propper usage

marko.brelak
Posts: 23
Joined: Wed Apr 10, 2019 1:26 pm

xTaskCreate - the propper usage

Postby marko.brelak » Fri Jun 28, 2019 9:37 pm

Dear colleagues,

I am writing a code which has several sensors to read, a couple of devices on I2C interface, some MQTT messaging, a TFT display running and some other stuff. Basically, I am using the xTaskCreate function in order to organize the function calling like tasks. Let's say, so far so good, the code is running but I am not sure if the approach is right. At the moment I am not sure for how long can I go this way and how much the code can expand. The following is the pseudo-code for my approach which is very simple:
  1. void task_function(void)
  2. {
  3.     Sensor_Init();
  4.     while(1)
  5.     {
  6.         Sensor_Read();
  7.         vTaskDelay(1000/portTICK_PERIOD_MS);
  8.     }
  9. }
So, the long story short, is there a better way of calling some function periodically, where while waiting, the CPU core is not loaded, or loaded very lightly?

Thanks for you answers in advance
Marko

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

Re: xTaskCreate - the propper usage

Postby ESP_Sprite » Sat Jun 29, 2019 2:04 am

No, this is perfectly fine. As an alternative, you can also use esp_timer, given that you don't spend too much time doing what you need to do (here: read the sensor)

Ritesh
Posts: 1383
Joined: Tue Sep 06, 2016 9:37 am
Location: India
Contact:

Re: xTaskCreate - the propper usage

Postby Ritesh » Sat Jun 29, 2019 11:07 am

marko.brelak wrote:
Fri Jun 28, 2019 9:37 pm
Dear colleagues,

I am writing a code which has several sensors to read, a couple of devices on I2C interface, some MQTT messaging, a TFT display running and some other stuff. Basically, I am using the xTaskCreate function in order to organize the function calling like tasks. Let's say, so far so good, the code is running but I am not sure if the approach is right. At the moment I am not sure for how long can I go this way and how much the code can expand. The following is the pseudo-code for my approach which is very simple:
  1. void task_function(void)
  2. {
  3.     Sensor_Init();
  4.     while(1)
  5.     {
  6.         Sensor_Read();
  7.         vTaskDelay(1000/portTICK_PERIOD_MS);
  8.     }
  9. }
So, the long story short, is there a better way of calling some function periodically, where while waiting, the CPU core is not loaded, or loaded very lightly?

Thanks for you answers in advance
Marko
Yes. Task or Timer can be used to fulfill your requirement to send data from sensor.

If timing is fixed then timer will be better option for you.

Do you have any other task to be running to fulfill any other requirement?
Regards,
Ritesh Prajapati

marko.brelak
Posts: 23
Joined: Wed Apr 10, 2019 1:26 pm

Re: xTaskCreate - the propper usage

Postby marko.brelak » Sat Jun 29, 2019 12:37 pm

Hi Ritesh,

Thank you for your reply. My application is not so time-sensitive, I can tolerate a slight drift of some amount of mS. The thing I am concerned about the number of tasks :) so here is a list of my tasks:

1. DS3231 RTC on I2C
2. Two PCF8574 GPIO Port expanders on I2C
3. Motor control using MCPWM
4. ST7789V TFT Display
5. DHT22 sensor on one-wire
6. Two DPS310 Pressure sensors
7. CCS811 TVOC sensor on I2C
8. MQ7 sensor reading (MCPWM + analog)
9. PMS5003 particle sensor
10. MQTT task
11. OTA task

These are just major tasks, but there are a couple of tasks that should be working around the scheduler, main algorithm, etc. Most of these tasks are called once in a couple of seconds so I believe that there is a time frame where the CPU is offloaded, but still, I don't have metrics to determine how much did I loaded the CPU.

Regards
Marko

Ritesh
Posts: 1383
Joined: Tue Sep 06, 2016 9:37 am
Location: India
Contact:

Re: xTaskCreate - the propper usage

Postby Ritesh » Sat Jun 29, 2019 6:11 pm

marko.brelak wrote:
Sat Jun 29, 2019 12:37 pm
Hi Ritesh,

Thank you for your reply. My application is not so time-sensitive, I can tolerate a slight drift of some amount of mS. The thing I am concerned about the number of tasks :) so here is a list of my tasks:

1. DS3231 RTC on I2C
2. Two PCF8574 GPIO Port expanders on I2C
3. Motor control using MCPWM
4. ST7789V TFT Display
5. DHT22 sensor on one-wire
6. Two DPS310 Pressure sensors
7. CCS811 TVOC sensor on I2C
8. MQ7 sensor reading (MCPWM + analog)
9. PMS5003 particle sensor
10. MQTT task
11. OTA task

These are just major tasks, but there are a couple of tasks that should be working around the scheduler, main algorithm, etc. Most of these tasks are called once in a couple of seconds so I believe that there is a time frame where the CPU is offloaded, but still, I don't have metrics to determine how much did I loaded the CPU.

Regards
Marko
Hello,

Thanks for providing information for same.

It seems your application requirement is critical which means need to take care about scheduling of tasks as per mentioned lists.

As you have listed down tasks in which you want to do some operations baed on timing. So, I believe you need to decide priority as well based on your task alignment. So that CPU can schedule each and every task as per time slots and schedule.

Still, you can also use timer parallel for few operations wherever needed.

You can also run few tasks into another core as well.
Regards,
Ritesh Prajapati

Who is online

Users browsing this forum: Google [Bot] and 134 guests