Skip to content

Cheatsheet — FreeRTOS Patterns (M04)

This content is not available in your language yet.

Course 1 · Module 4

ใช้ควบคู่บทเรียนและแล็บ — snippet จาก TESA Firmware SDK + FreeRTOS API

Lesson · Lab · ← Table of Contents


(void)cm55_initialize(system_ready_callback, NULL);
(void)cm55_start_scheduler(); /* does not return */

อย่าเรียก vTaskStartScheduler() ซ้ำถ้าใช้ cm55_start_scheduler() แล้ว


xTaskCreate(fn, "NAME", stack_words, NULL, tskIDLE_PRIORITY + 1U, &handle);
vTaskDelay(pdMS_TO_TICKS(500));
Do Don’t
ลูป for (;;) ใน task คาบ busy-wait นานโดยไม่ delay
ตรวจ pdPASS ตอน create ละเลย heap หมด

Docs: xTaskCreate · vTaskDelay


q = xQueueCreate(8, sizeof(item_t));
xQueueSend(q, &item, 0);
xQueueReceive(q, &item, portMAX_DELAY);
/* ISR: */ xQueueSendFromISR(q, &item, &woken); portYIELD_FROM_ISR(woken);

RAM ≈ depth × sizeof(item) (+ overhead)

Docs: xQueueCreate


mtx = xSemaphoreCreateMutex();
xSemaphoreTake(mtx, portMAX_DELAY);
xSemaphoreGive(mtx);
sem = xSemaphoreCreateBinary();
xSemaphoreTake(sem, pdMS_TO_TICKS(1000));
xSemaphoreGive(sem); /* or GiveFromISR */

I²C ในหลักสูตร:

cm55_i2c_manager_i2c_lock();
/* ... */
cm55_i2c_manager_i2c_unlock();

Docs: Mutexes


eg = xEventGroupCreate();
xEventGroupSetBits(eg, BIT0);
xEventGroupWaitBits(eg, BIT0, pdFALSE, pdFALSE, ticks);

Docs: Event Groups


Allowed (สั้น ๆ) Forbidden
xQueueSendFromISR printf / I²C / vTaskDelay
xSemaphoreGiveFromISR xSemaphoreTake แบบ block
portYIELD_FROM_ISR งานยาวใน ISR

งาน API ที่ใช้จริง
Create blink tasks
Delay
Queue send/receive
Mutex / I²C lock
Event group / binary sem

แหล่ง ลิงก์
Examples TESAIoT Developer Hub
Host Bitstream Studio
Lab pack TESAIoT_Hackathon

Lesson · Lab · ← Table of Contents

TESA Open Knowledge · © 2026 สมาคมสมองกลฝังตัวไทย (TESA) · CC BY-NC 4.0

Content is licensed CC BY-NC 4.0. Reuse it non-commercially and credit the Thai Embedded Systems Association (TESA) every time. · How to cite TESA