Manage FreeRTOS tasks - Suspend, Delay, Resume, Delete (ESP32 + Arduino series)

00:05:47
https://www.youtube.com/watch?v=jJaGRCgDo9s

概要

TLDRO vídeo apresenta um tutorial sobre como gerenciar tarefas no FreeRTOS, focando em suspender, interromper e verificar o status das tarefas. O apresentador utiliza um código simples com duas tarefas que incrementam um contador e demonstra como usar funções como vTaskSuspend, vTaskResume e vTaskDelete. Ele explica a importância de manipuladores de tarefas para controlar a execução de tarefas de forma mais eficiente. Além disso, aborda a falha crítica de esquecer de retomar tarefas após suspensões e discute a diferença entre suspender e deletar tarefas, proporcionando uma compreensão mais profunda das funcionalidades do FreeRTOS.

収穫

  • 🛠️ FreeRTOS permite gerenciamento eficiente de tarefas.
  • 🔁 Use vTaskSuspend para pausar tarefas específicas.
  • 🗑️ vTaskDelete remove tarefas que não são mais necessárias.
  • 📏 Manipuladores de tarefas permitem controle global.
  • ⚠️ Cuidado ao suspender tarefas para não afetar o loop principal.
  • ⏸️ Suspender todas as tarefas com vTaskSuspendAll é possível.
  • ⏩ Retomar tarefas suspensas com vTaskResume.
  • 📊 Verifique sempre o status da tarefa antes de suspendê-la.
  • 🚀 A execução crítica deve ter tarefas suspensas temporariamente.

タイムライン

  • 00:00:00 - 00:05:47

    Neste vídeo, abordamos o gerenciamento de tarefas no FreeRTOS, incluindo a suspensão, parada e verificação do status das tarefas. Começamos com um código que possui duas tarefas incrementando um contador. Mostramos como parar a tarefa 1 após atingir um contador de 3, utilizando a função 'vTaskDelete'. Para controlar tarefas externamente, definimos manipuladores de tarefa para que possamos suspender ou retomar tarefas a partir de outras partes do código. Explicamos o uso de 'vTaskSuspend' e 'vTaskResume', enfatizando a importância de verificar se o manipulador não é nulo antes de chamar a suspensão para evitar a suspensão da tarefa principal. Também introduzimos a suspensão de todas as tarefas para executar um código crítico. Por fim, abordamos a exclusão completa de uma tarefa usando 'vTaskDelete', garantindo que, ao atingir um contador específico, a tarefa possa ser excluída quando não for mais necessária. Para mais informações sobre o ESP32 e o FreeRTOS, sugerimos assistir aos outros vídeos da série.

マインドマップ

ビデオQ&A

  • O que é FreeRTOS?

    FreeRTOS é um sistema operacional de tempo real para microcontroladores.

  • Como suspender uma tarefa no FreeRTOS?

    Use a função vTaskSuspend e passe o manipulador da tarefa como argumento.

  • Como deletar uma tarefa?

    Utilize a função vTaskDelete e passe o manipulador da tarefa a ser deletada.

  • Posso controlar tarefas de fora do seu código?

    Sim, usando manipuladores de tarefas que permitem acesso global.

  • Qual é a diferença entre suspender e deletar uma tarefa?

    Suspender mantém a tarefa na memória para ser retomada, enquanto deletar remove a tarefa completamente.

  • O que acontece se não chamar xTaskResumeAll?

    Se não chamar xTaskResumeAll, outras tarefas não serão executadas, incluindo o loop principal do Arduino.

  • O que é um manipulador de tarefa?

    Um manipulador de tarefa é uma referência que permite controlar uma tarefa específica.

  • É possível suspender todas as tarefas?

    Sim, usando vTaskSuspendAll e depois xTaskResumeAll.

  • Como posso reiniciar uma tarefa suspensa?

    Utilize a função vTaskResume passando o manipulador da tarefa.

ビデオをもっと見る

AIを活用したYouTubeの無料動画要約に即アクセス!
字幕
en
オートスクロール:
  • 00:00:00
    once you start using free rtos tasks you
  • 00:00:03
    probably want to manage them
  • 00:00:05
    in this video i'll show you how to stop
  • 00:00:07
    and suspend tasks
  • 00:00:08
    as well as how to check on their status
  • 00:00:11
    in front of me i have the same code from
  • 00:00:13
    the previous video
  • 00:00:14
    and it consists out of two tasks that
  • 00:00:16
    simply increment
  • 00:00:17
    a counter every second now here we
  • 00:00:20
    already implemented the first level of
  • 00:00:22
    control and that is fee
  • 00:00:23
    task delay it suspends the current task
  • 00:00:26
    for a given number of ticks
  • 00:00:28
    now both these tasks will run forever
  • 00:00:30
    but how do we stop them
  • 00:00:32
    let's say i want to stop task 1 after
  • 00:00:34
    the counter has reached 3.
  • 00:00:37
    to do that i will create a check inside
  • 00:00:39
    this for loop
  • 00:00:40
    and i will say if count 1 is larger than
  • 00:00:44
    three
  • 00:00:44
    then i want to stop the task and we can
  • 00:00:47
    do that by calling
  • 00:00:48
    v task delete this one takes one
  • 00:00:51
    parameter it's the task to delete and if
  • 00:00:53
    we pass
  • 00:00:54
    null to it then it will delete the
  • 00:00:56
    current task
  • 00:00:58
    so this function basically tells free
  • 00:01:00
    rtos that the current task
  • 00:01:01
    should no longer be given any processing
  • 00:01:04
    time
  • 00:01:04
    but what if we want to stop task 1 from
  • 00:01:07
    the outside
  • 00:01:08
    say that we want to stop it from within
  • 00:01:10
    task number two
  • 00:01:13
    to do that we need to define task
  • 00:01:15
    handles
  • 00:01:16
    these allow us to control tasks from
  • 00:01:18
    anywhere in our code
  • 00:01:20
    let's create a task handle for task 1.
  • 00:01:23
    i'll do that at the top of the file
  • 00:01:25
    so i will say task handle underscore t
  • 00:01:28
    and i will give it a name say task 1
  • 00:01:30
    underscore handle
  • 00:01:32
    and we'll initialize it to null
  • 00:01:35
    now we have to give this task handle to
  • 00:01:37
    our xtask create function
  • 00:01:40
    so i'm going to scroll down to our setup
  • 00:01:42
    function where we create both of these
  • 00:01:44
    tasks
  • 00:01:45
    and i will say that for task 1 we do
  • 00:01:47
    want to keep track of a task handle
  • 00:01:49
    and in this case we're going to give it
  • 00:01:51
    a pointer to
  • 00:01:53
    task 1 underscore handle the one we've
  • 00:01:56
    just created
  • 00:01:57
    right here and that's it
  • 00:02:00
    now we can control task 1 from the
  • 00:02:03
    outside
  • 00:02:03
    with its handle now the first thing we
  • 00:02:06
    can do to task 1
  • 00:02:07
    is we can suspend it or resume it
  • 00:02:11
    let's say that we want to suspend task 1
  • 00:02:13
    when the counter reaches
  • 00:02:14
    a certain value say 3 while we can do v
  • 00:02:18
    task suspend and then we can pass along
  • 00:02:21
    null
  • 00:02:22
    if we want to suspend the current task
  • 00:02:24
    itself let's say v
  • 00:02:25
    task 1 but i can also suspend it from
  • 00:02:28
    the outside
  • 00:02:29
    so let's scroll down to the arduino loop
  • 00:02:32
    function
  • 00:02:32
    and here i can write something similar i
  • 00:02:34
    can say if count
  • 00:02:36
    one is larger than three then i want to
  • 00:02:38
    suspend
  • 00:02:39
    task one and to do that i'm just gonna
  • 00:02:41
    pass along
  • 00:02:42
    the task one handle to it now while this
  • 00:02:45
    code
  • 00:02:46
    will work we have to be careful with
  • 00:02:48
    task one underscore handle
  • 00:02:50
    it's initialized as null and if we call
  • 00:02:53
    v
  • 00:02:53
    task suspend with null it means that
  • 00:02:55
    freertos will suspend
  • 00:02:57
    the current task and if we run it here
  • 00:02:59
    that means that the arduino main loop
  • 00:03:02
    will be suspended that's not something
  • 00:03:04
    we want so i'll add a second check to
  • 00:03:06
    our if statement
  • 00:03:07
    and we're going to say that we're only
  • 00:03:09
    going to suspend task 1
  • 00:03:11
    if the count 1 is higher than 3 but also
  • 00:03:14
    if task 1 handle is not null
  • 00:03:18
    so these are two ways of suspending
  • 00:03:20
    tasks you can use a task handle to
  • 00:03:22
    suspend it from the outside
  • 00:03:24
    or you can just pass along null to v
  • 00:03:26
    tasks
  • 00:03:27
    spent to suspend the current task itself
  • 00:03:30
    suspending a task means that it won't
  • 00:03:32
    run but that it can be resumed at any
  • 00:03:34
    point
  • 00:03:35
    by using v task resume so i'm going to
  • 00:03:38
    scroll back down to the loop function
  • 00:03:40
    and let's say that i want to resume task
  • 00:03:43
    1
  • 00:03:43
    if count 2 is equal to five i'm going to
  • 00:03:46
    say
  • 00:03:46
    if if count two equals five
  • 00:03:50
    and our task handle one is not
  • 00:03:53
    null then i want to resume it by calling
  • 00:03:56
    v task resume task 1
  • 00:04:00
    underscore handle now obviously here
  • 00:04:02
    vtask resume will always need a task
  • 00:04:05
    handle
  • 00:04:05
    another level of control is to suspend
  • 00:04:08
    all the tasks and resume them later on
  • 00:04:10
    this might be useful when you want to
  • 00:04:12
    run some code that is time
  • 00:04:14
    sensitive and that should not be
  • 00:04:16
    interrupted
  • 00:04:17
    so let's say that we have a super
  • 00:04:19
    important
  • 00:04:20
    task what we can do is we can say
  • 00:04:24
    v task suspend all
  • 00:04:27
    then we can run our mission critical
  • 00:04:30
    code
  • 00:04:31
    here
  • 00:04:34
    and afterwards we can resume all of the
  • 00:04:37
    other tasks
  • 00:04:37
    by calling x task resume all
  • 00:04:41
    so what we're doing here is we're making
  • 00:04:43
    sure that no other tasks are running
  • 00:04:45
    when we're in between these two
  • 00:04:47
    statements just don't forget to run
  • 00:04:49
    x task resume all because without it no
  • 00:04:52
    other task will run
  • 00:04:53
    and that also includes the arduino main
  • 00:04:56
    loop
  • 00:04:57
    now the last thing you can do is you can
  • 00:04:58
    also delete a task
  • 00:05:00
    completely so let's say that when the
  • 00:05:02
    count reaches three
  • 00:05:04
    we no longer need task one then instead
  • 00:05:06
    of calling v
  • 00:05:07
    task suspend we can call v task
  • 00:05:11
    delete but we can also use v
  • 00:05:14
    task delete from within the task itself
  • 00:05:17
    if we scroll
  • 00:05:18
    up to task number one we can also say if
  • 00:05:21
    count
  • 00:05:22
    is higher than three we will we want to
  • 00:05:24
    call v task
  • 00:05:25
    delete and then we're going to pass
  • 00:05:28
    along null
  • 00:05:28
    which tells freeartos that we want to
  • 00:05:31
    delete the current
  • 00:05:32
    task itself which is in this case task
  • 00:05:35
    1.
  • 00:05:36
    so this was a quick overview of how you
  • 00:05:38
    can manage free rtos
  • 00:05:40
    tasks definitely check out the rest of
  • 00:05:42
    the series for more videos about the
  • 00:05:46
    esp32
タグ
  • FreeRTOS
  • Gerenciamento de Tarefas
  • Suspender Tarefas
  • Deletar Tarefas
  • Arduino
  • Manipuladores de Tarefas
  • vTaskSuspend
  • vTaskResume
  • vTaskDelete
  • Tarefas do ESP32