This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

SK-AM62B-P1: Alternate ways to this code?

Part Number: SK-AM62B-P1

Tool/software:

#include <stdlib.h>
#include <kernel/dpl/DebugP.h>
#include "ti_drivers_config.h"
#include "ti_board_config.h"
#include "ti_drivers_open_close.h"
#include "ti_board_open_close.h"
#include "FreeRTOS.h"
#include "task.h"

#define MAIN_TASK_PRI  (configMAX_PRIORITIES-1)
#define MAIN_TASK_SIZE (16384U/sizeof(configSTACK_DEPTH_TYPE))
#define I2C_TASK_PRI (configMAX_PRIORITIES-2)
#define I2C_TASK_SIZE (4096U/sizeof(configSTACK_DEPTH_TYPE))
#define GPIO_TASK_PRI (configMAX_PRIORITIES-3)
#define GPIO_TASK_SIZE (1024U/sizeof(configSTACK_DEPTH_TYPE))

StackType_t gMainTaskStack[MAIN_TASK_SIZE] __attribute__((aligned(32)));
StaticTask_t gMainTaskObj;
TaskHandle_t gMainTask;

StackType_t gI2CTaskStack[I2C_TASK_SIZE] __attribute__((aligned(32)));
StaticTask_t gI2CTaskObj;
TaskHandle_t gI2CTask;

StackType_t gGPIOTaskStack[GPIO_TASK_SIZE] __attribute__((aligned(32)));
StaticTask_t gGPIOTaskObj;
TaskHandle_t gGPIOTask;

void empty_main_i2c(void *arg0);
void gpio_led_blink_main(void *args);
void *empty_main_spi(void *arg0);

void freertos_main(void *args)
{
    int32_t status = SystemP_SUCCESS;

    /* Open drivers */
    Drivers_open();
    /* Open flash and board drivers */
    status = Board_driversOpen();
    DebugP_assert(status == SystemP_SUCCESS);

    /* Create the hello world task */
    gI2CTask = xTaskCreateStatic(empty_main_i2c,    /* Pointer to the task function */
                                   "empty_main_i2c",  /* Task name for debugging */
                                   I2C_TASK_SIZE,     /* Stack size */
                                   NULL,                /* Task parameter */
                                   I2C_TASK_PRI,      /* Task priority */
                                   gI2CTaskStack,     /* Stack buffer */
                                   &gI2CTaskObj);     /* Task control block */
    configASSERT(gI2CTask != NULL);

    /* Create the beautiful task */
    gGPIOTask = xTaskCreateStatic(gpio_led_blink_main,    /* Pointer to the task function */
                                       "gpio_led_blink_main",  /* Task name for debugging */
                                       GPIO_TASK_SIZE,/* Stack size */
                                       NULL,              /* Task parameter */
                                       GPIO_TASK_PRI, /* Task priority */
                                       gGPIOTaskStack,/* Stack buffer */
                                       &gGPIOTaskObj);/* Task control block */
    configASSERT(gGPIOTask != NULL);

    empty_main_spi(NULL);

    /* Close board and flash drivers */
    Board_driversClose();
    /* Close drivers */
    Drivers_close();

    vTaskDelete(NULL);
}

int main()
{
    /* init SOC specific modules */
    System_init();
    Board_init();

    /* This task is created at highest priority, it should create more tasks and then delete itself */
    gMainTask = xTaskCreateStatic(freertos_main,   /* Pointer to the function that implements the task. */
                                  "freertos_main", /* Task name for debugging */
                                  MAIN_TASK_SIZE,  /* Stack size */
                                  NULL,            /* Task parameter */
                                  MAIN_TASK_PRI,   /* Task priority */
                                  gMainTaskStack,  /* Stack buffer */
                                  &gMainTaskObj);  /* Task control block */
    configASSERT(gMainTask != NULL);

    /* Start the scheduler to start the tasks executing. */
    vTaskStartScheduler();

    /* This line should never be reached */
    DebugP_assertNoLog(0);

    return 0;
}


Can this be done using a single main file?

  • Hello Shreyan,

    Thanks for reaching out to Texas Instruments E2E support forum.

    The above code can be written in a single main.c file.

    Can you please tell why you want to keep all the code in single file?

    Are you facing any issues while doing the same?

    Regards,

    Tushar

  • Dear Tushar,

    How do I implement multi-threading of  those 3 functions in a single main file ?

    Regards,

    Shreyan

  • Hello Shreyan,

    I can see in the above provided code you have created 3 tasks and it seems fine to me.

    Are you facing any build error with this or is the task not running?

    Regards,

    Tushar

  • Dear Tushar,
    Is it possible to put those 3 tasks in a single file and call the main function for that file?
    I want threading to be implemented but not in the main.c file,but in empty.c

    Regards,

    Shreyan

  • Hello Shreyan,

    The above implementation is not the correct way.

    First we need to create all the tasks, later we need to start the RTOS timer. In your implementation, first you are enabled the RTOS start timer and one Task later creating Tasks.

    If you want to create all the Tasks in empty.c file, then call one function from the main.c file.

    The call implementation must have the all Tasks initializations in the empty.c file.

    Please look at the code below for your reference .

    main.c file 
    
    int main()
    {
        /* init SOC specific modules */
        System_init();
        Board_init();
    
    
        Tasks_init();
    
        /* This line should never be reached */
        DebugP_assertNoLog(0);
    
        return 0;
    }
    
    empty.c file : 
    
    void Tasks_init(void *args)
    {
        int32_t status = SystemP_SUCCESS;
    
     
    
        /* Create the hello world task */
        gI2CTask = xTaskCreateStatic(empty_main_i2c,    /* Pointer to the task function */
                                       "empty_main_i2c",  /* Task name for debugging */
                                       I2C_TASK_SIZE,     /* Stack size */
                                       NULL,                /* Task parameter */
                                       I2C_TASK_PRI,      /* Task priority */
                                       gI2CTaskStack,     /* Stack buffer */
                                       &gI2CTaskObj);     /* Task control block */
        configASSERT(gI2CTask != NULL);
    
        /* Create the beautiful task */
        gGPIOTask = xTaskCreateStatic(gpio_led_blink_main,    /* Pointer to the task function */
                                           "gpio_led_blink_main",  /* Task name for debugging */
                                           GPIO_TASK_SIZE,/* Stack size */
                                           NULL,              /* Task parameter */
                                           GPIO_TASK_PRI, /* Task priority */
                                           gGPIOTaskStack,/* Stack buffer */
                                           &gGPIOTaskObj);/* Task control block */
        configASSERT(gGPIOTask != NULL);
    
    
            /* This task is created at highest priority, it should create more tasks and then delete itself */
        gMainTask = xTaskCreateStatic(freertos_main,   /* Pointer to the function that implements the task. */
                                      "freertos_main", /* Task name for debugging */
                                      MAIN_TASK_SIZE,  /* Stack size */
                                      NULL,            /* Task parameter */
                                      MAIN_TASK_PRI,   /* Task priority */
                                      gMainTaskStack,  /* Stack buffer */
                                      &gMainTaskObj);  /* Task control block */
        configASSERT(gMainTask != NULL);
    
    
        
        /* Start the scheduler to start the tasks executing. */
        vTaskStartScheduler
    
    
    }
    
    
    void empty_main_i2c(void)
    {
    
       while(1)
       {
       
       }
    }
    
    
    void gpio_led_blink_main(void)
    {
    
       while(1)
       {
       
       }
    }
    
    
    
    void freertos_main(void)
    {
    
       while(1)
       {
       
       }
    }
    
    

    Regards,


    Anil.

  • Dear Anil,

    In the main.c file I gave,the vTaskStartScheduler(); is after main,so doesn't it start RTOS timer after the tasks have been created?
    Regards,


    Shreyan

  • Dear Anil,

    Could you just take a look at this issue??

    Regards,

    Shreyan

  • Hello Shreyan,

    In the each Task for every 1 sec,  print some uart logs and see if the Task switching is happening or not.

    Regards,

    Anil.