• AVR Freaks

Hot!How to create one or more task

Author
abhi143
Junior Member
  • Total Posts : 115
  • Reward points : 0
  • Joined: 2018/05/20 09:06:52
  • Location: 0
  • Status: offline
2020/04/05 00:30:29 (permalink)
0

How to create one or more task

 
I am reading free rtos documents I have question for information given in page https://www.freertos.org/a00125.html
 
consider I want to create three task 
 
First task print string "Task A"
Second task print string "Task B "
Third task print string "Task C"
 
How to create three task that print the string ?
 
 
 
#1

4 Replies Related Threads

    mlp
    boots too small
    • Total Posts : 912
    • Reward points : 0
    • Joined: 2012/09/10 15:12:07
    • Location: previously Microchip XC8 team
    • Status: offline
    Re: How to create one or more task 2020/04/06 13:05:11 (permalink)
    0
    abhi143
    How to create three task that print the string ?

    What code have you tried so far?
    What error messages do you get?
    What behaviour do you see?

    Mark (this opinion available for hire)
    #2
    ric
    Super Member
    • Total Posts : 26942
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: How to create one or more task 2020/04/06 13:23:54 (permalink)
    0
    Three tasks all trying to output to the same physical device.
    Nahh, I can't see any problems with that... ;)
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #3
    NKurzman
    A Guy on the Net
    • Total Posts : 18655
    • Reward points : 0
    • Joined: 2008/01/16 19:33:48
    • Location: 0
    • Status: online
    Re: How to create one or more task 2020/04/06 14:24:58 (permalink)
    +1 (1)
    1.  You are doing this on an 8bit PIC?
    2. You would have some options:
        a.  use a semiphore  / mutex to insure only one task at a time has the port.
        b. use a mail box.
        c. use  buffered output to the UART.
    #4
    LdB_ECM
    Super Member
    • Total Posts : 355
    • Reward points : 0
    • Joined: 2019/04/16 22:01:25
    • Location: 0
    • Status: offline
    Re: How to create one or more task 2020/04/06 21:12:31 (permalink)
    +1 (1)
    The formal way on FreeRTOS is to use a binary semaphore
     
    So in the code below printSem is a signalling semaphore. When you ask to "take" the semaphore if it is already in use the task is added to the semaphore signal queue and then the task is set to blocked state and a context switch to next waiting task done. So while waiting the task takes up no CPU time. If the semaphore was available it is marked as now in use and the code simply moves straight thru. If the task was blocked it is woken when the task that currently has the semaphore gives it back. The give semaphore call looks at the queue and if there are tasks waiting for the semaphore it removes the first task from the waiting queue and wakes it.
     
    So what it is setting up is that any task that is "waiting" takes 0% CPU time. This is an important concept on FreeRTOS you don't want things doing polled loops wasting CPU time you use signalling. If you are polling something it's a clear case you need to look and see if you can replace it with signalling.
     
    Any this is a minimal implementation of what you asked
    #include "FreeRTOS\include\FreeRTOS.h"
    #include "FreeRTOS\include\task.h"
    #include "FreeRTOS\include\semphr.h"

    static SemaphoreHandle_t printSem;

    void task1 (void *pParam)
    {
        while (1)
        {
        xSemaphoreTake(printSem, portMAX_DELAY);
        printf("Task 1\n");
        xSemaphoreGive(printSem);
        }
    }

    void task2 (void *pParam)
    {
        while (1)
        {
        xSemaphoreTake(printSem, portMAX_DELAY);
        printf("Task 2\n");
        xSemaphoreGive(printSem);
        }
    }

    void task3 (void *pParam)
    {
        while (1)
        {
        xSemaphoreTake(printSem, portMAX_DELAY);
        printf("Task 3\n");
        xSemaphoreGive(printSem);
        }
    }

    int main(void)
    {
        printSem = xSemaphoreCreateBinary();
        xTaskCreate(task1, "Task1", 200, NULL, 3, NULL);
        xTaskCreate(task2, "Task2", 200, NULL, 3, NULL);
         xTaskCreate(task3, "Task3", 200, NULL, 3, NULL);
         vTaskStartScheduler();
        
        /* should never reach here*/
        while (1){};    
        return 0;
    }

    #5
    Jump to:
    © 2020 APG vNext Commercial Version 4.5