Skip to content

The SDK is based on FreeRTOS (components/os/freertos); tasks, queues, semaphores, event groups and software timers are standard interfaces. See FreeRTOS Scheduling Fundamentals for the theory and FreeRTOS OS for the example.

Common FreeRTOS APIs (Standard Interfaces) ​

xTaskCreate(task, name, stack, arg, prio, handle)

Create a task (a task is an independent piece of functionality).

Parameters:

  • task: task function
  • name: task name (for debugging)
  • stack: stack size in words
  • arg: argument passed to the task
  • prio: priority — larger number means higher priority
  • handle: task handle output

Return value: pdPASS on success

vTaskDelete(task)

Delete a task (pass NULL to delete the current task).

Parameters:

  • task: task handle

Return value: none

vTaskDelay(ticks)

Delay the task: move it to Blocked and yield the CPU.

Parameters:

  • ticks: delay in ticks

Return value: none

vTaskDelayUntil(&prev, ticks)

Precise periodic delay: wakes at a fixed period regardless of task run time.

Parameters:

  • prev: previous wake-time record
  • ticks: period in ticks

Return value: none

vTaskStartScheduler()

Start the scheduler; tasks begin running. It normally never returns.

Parameters: none

Return value: none (does not return)

xTaskGetTickCount()

Get the current system tick (often used for timeout checks).

Parameters: none

Return value: current tick

xQueueCreate(len, size)

Create a queue (a data pipe between tasks).

Parameters:

  • len: queue length
  • size: bytes per item

Return value: queue handle; NULL on failure

xQueueSend(queue, item, timeout)

Send a message to the queue.

Parameters:

  • queue: queue handle
  • item: data to send
  • timeout: max wait when the queue is full

Return value: pdPASS on success

xQueueReceive(queue, buf, timeout)

Receive a message from the queue (blocks when empty).

Parameters:

  • queue: queue handle
  • buf: receive buffer
  • timeout: max wait

Return value: pdPASS on success

xSemaphoreCreateBinary()

Create a binary semaphore (for event notification).

Parameters: none

Return value: semaphore handle

xSemaphoreCreateMutex()

Create a mutex (ownership + priority inheritance; protects shared resources).

Parameters: none

Return value: mutex handle

xSemaphoreTake / xSemaphoreGive(sem, timeout) / (sem)

Take (wait for) a semaphore / give a semaphore.

Parameters:

  • sem: semaphore handle
  • timeout: wait timeout (Take only)

Return value: pdPASS on success

xEventGroupCreate / xEventGroupSetBits / xEventGroupWaitBits()

Event groups: bit flags for "multi-condition triggers".

Parameters: see the FreeRTOS documentation (create has none; set/wait take the group handle and bit mask)

Return value: event group handle / current event bits

xTimerCreate / xTimerStart / xTimerStop(...)

Software timers: run a callback when the period expires, without occupying a hardware timer.

Parameters:

  • xTimerCreate: name, period, auto-reload, callback

Return value: timer handle / pdPASS

xStreamBufferCreate / xStreamBufferSend / xStreamBufferReceive(...)

Stream buffers: byte-stream send/receive (e.g., UART data → processing task).

Parameters: see the FreeRTOS documentation

Return value: handle / number of bytes sent or received

Note: the SDK uses the standard FreeRTOS API; full declarations are in components/os/freertos/include/ (task.h, queue.h, semphr.h, timers.h, event_groups.h, stream_buffer.h).

Common Exam & Interview Questions ​

Difference between task delay and busy-wait delay?

vTaskDelay moves the task to Blocked and yields the CPU; a busy while-loop keeps spinning, wasting CPU and preventing sleep.

Semaphore or mutex?

Semaphores are for event notification/counting (no ownership); mutexes protect shared resources (ownership + priority inheritance against priority inversion).

Have questions?

For any other questions, visit the unified Q&A and discussion board: Ai-Thinker Discussions

Released under the MIT License. Build Time 2026-09-30 17:31:25