Calling or initilising I2C from within a task

greg-dickson
Posts: 24
Joined: Sun Nov 01, 2020 1:51 am

Calling or initilising I2C from within a task

Postby greg-dickson » Wed Dec 07, 2022 4:44 am

Is there some trick I missed about calling I2C from within a task?

if I create a task and try and do a periodic call to an I2C slave it blocks and nothing continues.
This is on an S3 using esp-idf V5.0
SDA 38
SCL 39

I2C is started outside the task.

However if I try to start it from within the task it blocks at the init
At which point I haven't yet determined.

....

Actually just calling the init function does not even run the function.
the first line is

Code: Select all

printf("Init in %s\n",__FUNCTION__);
which prints nothing.
This function is the init function with a separate component that functions fine when called from app main just not from within the task,

What have I missed here.



Any insight would be gratefully appreciated.

Thanks,
Gregory

greg-dickson
Posts: 24
Joined: Sun Nov 01, 2020 1:51 am

Re: Calling or initilising I2C from within a task

Postby greg-dickson » Wed Dec 07, 2022 7:16 am

After simplfying the problem and just try to toggle a GPIO
even that crashes if called from a task.

siimple to duplicate

idf.py create-project task_test
cd task_test/
mkdir components
cd components/
idf.py create-component my_task
cd my_task


edit my_task

add
REQUIRES driver
to CMakeLists.txt in components/my_task

my_task.c
  1. #include <stdio.h>
  2.  
  3. #include "my_task.h"
  4.  
  5. void func(void)
  6. {
  7.   printf("Init in %s\n",__FUNCTION__);
  8.   printf("reset GPIO %d\n",40);
  9.   gpio_reset_pin(40);
  10.   printf("Setup GPIO done\n");
  11. }
add

REQUIRES my_task

to CMakeLists.txt in main/

task_test.c
  1. #include <stdio.h>
  2. #include "freertos/FreeRTOS.h"
  3. #include "freertos/task.h"
  4. #include "my_task.h"
  5.  
  6.  
  7. static TaskHandle_t task_handle;
  8.  
  9.  
  10. void task( void * pvParameters )
  11. {
  12.      
  13.    printf("Runing main task\n");
  14.    func();
  15.    while(1)
  16.    {
  17.       vTaskDelay(1000 / portTICK_PERIOD_MS);
  18.       printf(".\n");
  19.    }
  20.  
  21. }
  22.  
  23.  
  24. void run_task( uint8_t priority)
  25. {
  26.    printf("Starting main task\n");
  27.    xTaskCreate(
  28.       task,      // Function to implement the task */
  29.       "main fork",  // Name of the task
  30.       1024,              // Stack size in bytes ( vanilla FreeRTOS uses words! https://www.freertos.org/a00125.html )
  31.       NULL,               // Task input parameter
  32.       priority,                  // Priority of the task higher = higher priority  
  33.       &task_handle   // Task handle to keep track of created task.
  34.    );                
  35.    vTaskDelay(500 / portTICK_PERIOD_MS);  // retained old code needed to start-up task
  36.    printf("Started main task\n");
  37. }
  38.  
  39. void app_main(void)
  40. {
  41.   vTaskDelay(1000 / portTICK_PERIOD_MS);
  42.   run_task(3);  
  43.   while(1)
  44.    {
  45.       printf(">\n");
  46.       vTaskDelay(1000 / portTICK_PERIOD_MS);
  47.    }
  48.  
  49. }
  50.  
  51.  

compile and upload

runs to the reset and fails at gpio_reset

this code produces

I (278) cpu_start: Starting scheduler on PRO CPU.
I (0) cpu_start: Starting scheduler on APP CPU.
Starting main task
Runing main task
Init in func
reset GPIO 40
E (2318) task_wdt: esp_task_wdt_reset(712): task not found
E (2328) task_wdt: esp_task_wdt_reset(712): task not found

greg-dickson
Posts: 24
Joined: Sun Nov 01, 2020 1:51 am

Re: Calling or initilising I2C from within a task

Postby greg-dickson » Wed Dec 07, 2022 7:51 am

Ok so even this code fails.
What am I doing wrong here?
Please could someone point me in the right direction?
  1. #include <stdio.h>
  2. #include "freertos/FreeRTOS.h"
  3. #include "freertos/task.h"
  4. #include "driver/gpio.h"
  5.  
  6.  
  7. static TaskHandle_t task_handle;
  8.  
  9. void func(void)
  10. {
  11.   printf("Init in %s\n",__FUNCTION__);
  12.   printf("reset GPIO %d\n",GPIO_NUM_40);
  13.   gpio_reset_pin( GPIO_NUM_40);
  14.   printf("Setup GPIO done\n");
  15.  
  16. }
  17.  
  18.  
  19. void task( void * pvParameters )
  20. {
  21.      
  22.    printf("Runing main task\n");
  23.    func();
  24.    while(1)
  25.    {
  26.       vTaskDelay(1000 / portTICK_PERIOD_MS);
  27.       printf(".\n");
  28.    }
  29.  
  30. }
  31.  
  32.  
  33. void run_task( uint8_t priority)
  34. {
  35.    printf("Starting main task\n");
  36.    xTaskCreate(
  37.       task,      // Function to implement the task */
  38.       "main fork",  // Name of the task
  39.       1024,              // Stack size in bytes ( vanilla FreeRTOS uses words! https://www.freertos.org/a00125.html )
  40.       NULL,               // Task input parameter
  41.       priority,                  // Priority of the task higher = higher priority  
  42.       &task_handle   // Task handle to keep track of created task.
  43.    );                
  44.    vTaskDelay(500 / portTICK_PERIOD_MS);  // retained old code needed to start-up task
  45.    printf("Started main task\n");
  46. }
  47.  
  48. void app_main(void)
  49. {
  50.   printf("from app main reset GPIO %d\n",GPIO_NUM_40);
  51.   gpio_reset_pin( GPIO_NUM_40);
  52.   vTaskDelay(1000 / portTICK_PERIOD_MS);
  53.   run_task(3);  
  54.   while(1)
  55.    {
  56.       printf(">\n");
  57.       vTaskDelay(1000 / portTICK_PERIOD_MS);
  58.    }
  59.  
  60. }

boarchuz
Posts: 606
Joined: Tue Aug 21, 2018 5:28 am

Re: Calling or initilising I2C from within a task

Postby boarchuz » Wed Dec 07, 2022 11:00 am

Try a larger stack size (eg. 4096).

Is anything connected to GPIO 40? Are you using JTAG? Does a different pin number change the behaviour?

chegewara
Posts: 2371
Joined: Wed Jun 14, 2017 9:00 pm

Re: Calling or initilising I2C from within a task

Postby chegewara » Wed Dec 07, 2022 11:54 am

Task stack size is too low. When you are using printf like functions or ESP_LOGx the stack should be at least 3kB.
Many topics/questions/issues related on this forum.
Even somewhere in esp-idf minimum stack size is around 1300-something bytes.

greg-dickson
Posts: 24
Joined: Sun Nov 01, 2020 1:51 am

Re: Calling or initilising I2C from within a task

Postby greg-dickson » Wed Dec 07, 2022 12:42 pm

Thanks heaps chegewara
It was really doing my head in.
I didn't think of that.
Changing the stack size sorted it.
I did search a bit but as I didn't think of that as being the problem the searches weren't much help.

Who is online

Users browsing this forum: stvlaa and 123 guests