diff --git a/apps/08_thread_control_apis/src/main.c b/apps/08_thread_control_apis/src/main.c index ad588d8..a997424 100644 --- a/apps/08_thread_control_apis/src/main.c +++ b/apps/08_thread_control_apis/src/main.c @@ -96,7 +96,7 @@ static void controller_entry(void *p1, void *p2, void *p3) { printk("[CTL] controller started (prio=%d)\r\n", k_thread_priority_get(k_current_get())); - // Initial yield so A & B get a turn immediately. + // Initial yield so A & B get a turn immediately printk("[CTL] yield once to let A & B run\r\n"); k_yield(); // voluntary yield diff --git a/apps/multithreaded_01/CMakeLists.txt b/apps/multithreaded_01/CMakeLists.txt new file mode 100644 index 0000000..82cead7 --- /dev/null +++ b/apps/multithreaded_01/CMakeLists.txt @@ -0,0 +1,7 @@ +cmake_minimum_required(VERSION 3.20.0) + +find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) + +project(multithreaded_01) + +target_sources(app PRIVATE src/main.c) diff --git a/apps/multithreaded_01/prj.conf b/apps/multithreaded_01/prj.conf new file mode 100644 index 0000000..814117e --- /dev/null +++ b/apps/multithreaded_01/prj.conf @@ -0,0 +1,16 @@ +# Set to -Og +CONFIG_DEBUG_OPTIMIZATIONS=y + +# Thread awareness support +CONFIG_DEBUG_THREAD_INFO=y + +# Generate stack usage per-function +CONFIG_STACK_USAGE=y + +# Other options in case not set by default +CONFIG_BUILD_OUTPUT_HEX=y +CONFIG_BUILD_OUTPUT_META=y +CONFIG_OUTPUT_SYMBOLS=y +CONFIG_OUTPUT_STAT=y +CONFIG_OUTPUT_DISASSEMBLY=y +CONFIG_OUTPUT_PRINT_MEMORY_USAGE=y diff --git a/apps/multithreaded_01/src/main.c b/apps/multithreaded_01/src/main.c new file mode 100644 index 0000000..b0f15b3 --- /dev/null +++ b/apps/multithreaded_01/src/main.c @@ -0,0 +1,60 @@ +#include + +/* Stack sizes are in bytes. Lower priority numbers mean higher priority. */ +#define THREAD_A_STACK_SIZE 512 +#define THREAD_A_PRIORITY 2 + +#define THREAD_B_STACK_SIZE 512 +#define THREAD_B_PRIORITY 10 + +/* Reserve Thread A's stack with the alignment required by Zephyr. */ +K_THREAD_STACK_DEFINE(thread_a_stack, THREAD_A_STACK_SIZE); + +/* Thread A's control block must remain valid after main() returns. */ +struct k_thread thread_a_data; + +/* Zephyr thread entry functions receive three optional arguments. + * This example passes NULL for all three and does not use them. + */ +void thread_a_entry(void *p1, void *p2, void *p3) { + /* Get the priority of the currently running thread (Thread A). */ + printk("Thread_A: I am at high priority, value: %d \n", + k_thread_priority_get(k_current_get())); +} + +void thread_b_entry(void *p1, void *p2, void *p3) { + /* Thread B has lower priority than Thread A: 10 versus 2. */ + printk("Thread_B: I am at low priority, value: %d \n", + k_thread_priority_get(k_current_get())); +} + +int main(void) { + /* Create Thread A at runtime using its reserved control block and stack. + * K_THREAD_STACK_SIZEOF supplies the usable stack size to Zephyr. + * NULL arguments are passed to thread_a_entry; options 0 means defaults. + * K_NO_WAIT makes the thread ready immediately, but the scheduler decides + * when it runs. The returned ID can be used in later thread API calls. + */ + k_tid_t thread_a_id = + k_thread_create(&thread_a_data, + thread_a_stack, + K_THREAD_STACK_SIZEOF(thread_a_stack), + thread_a_entry, NULL, NULL, NULL, + THREAD_A_PRIORITY, 0, K_NO_WAIT); + + ARG_UNUSED(thread_a_id); + + return 0; +} + +/* Define Thread B at compile time, including its stack and control block. + * Zephyr uses this metadata during startup to make the thread ready. + * This is not a function call executed after main: its location here does + * not determine execution order. The scheduler invokes thread_b_entry. + * The NULLs are entry arguments; the last two zeros mean default options + * and zero startup delay (in milliseconds), respectively. + */ +K_THREAD_DEFINE(thread_b_id, + THREAD_B_STACK_SIZE, + thread_b_entry, NULL, NULL, NULL, + THREAD_B_PRIORITY, 0, 0); \ No newline at end of file diff --git a/apps/multithreaded_02/CMakeLists.txt b/apps/multithreaded_02/CMakeLists.txt new file mode 100644 index 0000000..5f97adc --- /dev/null +++ b/apps/multithreaded_02/CMakeLists.txt @@ -0,0 +1,7 @@ +cmake_minimum_required(VERSION 3.20.0) + +find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) + +project(multithreaded_02) + +target_sources(app PRIVATE src/main.c) diff --git a/apps/multithreaded_02/prj.conf b/apps/multithreaded_02/prj.conf new file mode 100644 index 0000000..814117e --- /dev/null +++ b/apps/multithreaded_02/prj.conf @@ -0,0 +1,16 @@ +# Set to -Og +CONFIG_DEBUG_OPTIMIZATIONS=y + +# Thread awareness support +CONFIG_DEBUG_THREAD_INFO=y + +# Generate stack usage per-function +CONFIG_STACK_USAGE=y + +# Other options in case not set by default +CONFIG_BUILD_OUTPUT_HEX=y +CONFIG_BUILD_OUTPUT_META=y +CONFIG_OUTPUT_SYMBOLS=y +CONFIG_OUTPUT_STAT=y +CONFIG_OUTPUT_DISASSEMBLY=y +CONFIG_OUTPUT_PRINT_MEMORY_USAGE=y diff --git a/apps/multithreaded_02/src/main.c b/apps/multithreaded_02/src/main.c new file mode 100644 index 0000000..fe30772 --- /dev/null +++ b/apps/multithreaded_02/src/main.c @@ -0,0 +1,42 @@ +#include + +#define THREAD_A_STACK_SIZE 512 +#define THREAD_A_PRIORITY 2 + +#define THREAD_B_STACK_SIZE 512 +#define THREAD_B_PRIORITY 10 + +K_THREAD_STACK_DEFINE(thread_a_stack, THREAD_A_STACK_SIZE); + +struct k_thread thread_a_data; +k_tid_t thread_a_id; + +void thread_a_entry(void *p1, void *p2, void *p3) { + printk("Thread_A: running, about to suspend\n"); + k_thread_suspend(k_current_get()); + printk("Thread_A: resumed, exiting\n"); +} + +void thread_b_entry(void *p1, void *p2, void *p3) { + printk("Thread_B: running, sleeping for 500ms\n"); + k_sleep(K_MSEC(500)); + printk("Thread_B: woke up after 500ms, resuming Thread_A\n"); + k_thread_resume(thread_a_id); +} + +int main(void) { + thread_a_id = + k_thread_create(&thread_a_data, + thread_a_stack, + K_THREAD_STACK_SIZEOF(thread_a_stack), + thread_a_entry, NULL, NULL, NULL, + THREAD_A_PRIORITY, 0, K_NO_WAIT); + + ARG_UNUSED(thread_a_id); + + return 0; +} + +K_THREAD_DEFINE(thread_b_id, THREAD_B_STACK_SIZE, + thread_b_entry, NULL, NULL, NULL, + THREAD_B_PRIORITY, 0, 0); \ No newline at end of file diff --git a/apps/my_fifo/CMakeLists.txt b/apps/my_fifo/CMakeLists.txt new file mode 100644 index 0000000..0d19f13 --- /dev/null +++ b/apps/my_fifo/CMakeLists.txt @@ -0,0 +1,7 @@ +cmake_minimum_required(VERSION 3.20.0) + +find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) + +project(my_fifo) + +target_sources(app PRIVATE src/main.c) diff --git a/apps/my_fifo/prj.conf b/apps/my_fifo/prj.conf new file mode 100644 index 0000000..d98b2b8 --- /dev/null +++ b/apps/my_fifo/prj.conf @@ -0,0 +1,19 @@ +# Set to -Og +CONFIG_DEBUG_OPTIMIZATIONS=y + +# Thread awareness support +CONFIG_DEBUG_THREAD_INFO=y + +# Generate stack usage per-function +CONFIG_STACK_USAGE=y + +# Enable system heap +CONFIG_HEAP_MEM_POOL_SIZE=1024 + +# Other options in case not set by default +CONFIG_BUILD_OUTPUT_HEX=y +CONFIG_BUILD_OUTPUT_META=y +CONFIG_OUTPUT_SYMBOLS=y +CONFIG_OUTPUT_STAT=y +CONFIG_OUTPUT_DISASSEMBLY=y +CONFIG_OUTPUT_PRINT_MEMORY_USAGE=y diff --git a/apps/my_fifo/src/main.c b/apps/my_fifo/src/main.c new file mode 100644 index 0000000..35a5a4c --- /dev/null +++ b/apps/my_fifo/src/main.c @@ -0,0 +1,74 @@ +#include +#include + +// Thread A: higher priority +#define THREAD_A_STACK_SIZE 512 +#define THREAD_A_PRIORITY 2 + +// Thread B: lower priority +#define THREAD_B_STACK_SIZE 512 +#define THREAD_B_PRIORITY 10 + +// Allocate stack memory for Thread A +K_THREAD_STACK_DEFINE(thread_a_stack, THREAD_A_STACK_SIZE); + +// Thread A control structure and thread ID +struct k_thread thread_a_data; +k_tid_t thread_a_id; + +// Define the FIFO message structure +struct my_msg { + void *reserved; // Required by Zephyr FIFO for internal linkage + int tata; + int titi; +}; + +// Define and initialize a FIFO queue +K_FIFO_DEFINE(my_fifo); + +// Thread A: allocate and send a message through FIFO +void thread_a_entry(void *p1, void *p2, void *p3) { + // Dynamically allocate memory for the message + struct my_msg *msg = k_malloc(sizeof(struct my_msg)); + // Check for allocation failure + if (msg == NULL) { + printk("Message allocation failed\n"); + return; + } + // Populate the message fields + msg->tata = 42; + msg->titi = 100; + printk("Thread A: Sending tata=%d titi=%d\n", msg->tata, msg->titi); + // Enqueue the message pointer and transfer ownership to FIFO + k_fifo_put(&my_fifo, msg); +} + +// Thread B: receive and process a message from FIFO +void thread_b_entry(void *p1, void *p2, void *p3) { + // Block until a message becomes available + struct my_msg *msg = k_fifo_get(&my_fifo, K_FOREVER); + // Print the received message + printk("Thread B: Received tata=%d titi=%d\n", msg->tata, msg->titi); + // Release dynamically allocated memory after processing + k_free(msg); +} + +int main(void) { + // Create and start Thread A dynamically at runtime + thread_a_id = + k_thread_create(&thread_a_data, + thread_a_stack, + K_THREAD_STACK_SIZEOF(thread_a_stack), + thread_a_entry, NULL, NULL, NULL, + THREAD_A_PRIORITY, 0, K_NO_WAIT); + + // Suppress unused variable warnings + ARG_UNUSED(thread_a_id); + return 0; +} + +// Define Thread B statically and start it during system initialization +K_THREAD_DEFINE(thread_b_id, + THREAD_B_STACK_SIZE, + thread_b_entry, NULL, NULL, NULL, + THREAD_B_PRIORITY, 0, 0); \ No newline at end of file diff --git a/apps/my_msgq/CMakeLists.txt b/apps/my_msgq/CMakeLists.txt new file mode 100644 index 0000000..90c562b --- /dev/null +++ b/apps/my_msgq/CMakeLists.txt @@ -0,0 +1,7 @@ +cmake_minimum_required(VERSION 3.20.0) + +find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) + +project(my_msgq) + +target_sources(app PRIVATE src/main.c) diff --git a/apps/my_msgq/prj.conf b/apps/my_msgq/prj.conf new file mode 100644 index 0000000..d98b2b8 --- /dev/null +++ b/apps/my_msgq/prj.conf @@ -0,0 +1,19 @@ +# Set to -Og +CONFIG_DEBUG_OPTIMIZATIONS=y + +# Thread awareness support +CONFIG_DEBUG_THREAD_INFO=y + +# Generate stack usage per-function +CONFIG_STACK_USAGE=y + +# Enable system heap +CONFIG_HEAP_MEM_POOL_SIZE=1024 + +# Other options in case not set by default +CONFIG_BUILD_OUTPUT_HEX=y +CONFIG_BUILD_OUTPUT_META=y +CONFIG_OUTPUT_SYMBOLS=y +CONFIG_OUTPUT_STAT=y +CONFIG_OUTPUT_DISASSEMBLY=y +CONFIG_OUTPUT_PRINT_MEMORY_USAGE=y diff --git a/apps/my_msgq/src/main.c b/apps/my_msgq/src/main.c new file mode 100644 index 0000000..8342d7e --- /dev/null +++ b/apps/my_msgq/src/main.c @@ -0,0 +1,64 @@ +#include + +// Thread A: higher priority (lower number = higher priority) +#define THREAD_A_STACK_SIZE 512 +#define THREAD_A_PRIORITY 2 + +// Thread B: lower priority +#define THREAD_B_STACK_SIZE 512 +#define THREAD_B_PRIORITY 10 + +// Allocate stack memory for Thread A +K_THREAD_STACK_DEFINE(thread_a_stack, THREAD_A_STACK_SIZE); + +// Thread A control structure and thread ID +struct k_thread thread_a_data; +k_tid_t thread_a_id; + +// Define the message structure +struct my_msg { + int tata; + int titi; +}; + +// Define a message queue holding up to 3 messages, aligned to 4 bytes +K_MSGQ_DEFINE(my_msgq, sizeof(struct my_msg), 3, 4); + +// Thread A: send a message to the queue +void thread_a_entry(void *p1, void *p2, void *p3) { + struct my_msg msg = { + .tata = 42, + .titi = 100, + }; + // Send a copy of the message without waiting if the queue is full + k_msgq_put(&my_msgq, &msg, K_NO_WAIT); + printk("Thread_A: sent tata=%d titi=%d\n", msg.tata, msg.titi); +} + +// Thread B: receive a message from the queue +void thread_b_entry(void *p1, void *p2, void *p3) { + struct my_msg msg; + // Block until a message becomes available + k_msgq_get(&my_msgq, &msg, K_FOREVER); + printk("Thread_B: received tata=%d titi=%d\n", msg.tata, msg.titi); +} + +int main(void) { + // Create and start Thread A dynamically at runtime + thread_a_id = + k_thread_create(&thread_a_data, + thread_a_stack, + K_THREAD_STACK_SIZEOF(thread_a_stack), + thread_a_entry, NULL, NULL, NULL, + THREAD_A_PRIORITY, 0, K_NO_WAIT); + + // Suppress unused variable warnings + ARG_UNUSED(thread_a_id); + return 0; +} + +// Define Thread B statically and start it during system initialization +K_THREAD_DEFINE(thread_b_id, + THREAD_B_STACK_SIZE, + thread_b_entry, NULL, NULL, NULL, + THREAD_B_PRIORITY, 0, 0); \ No newline at end of file diff --git a/apps/my_semaphore/CMakeLists.txt b/apps/my_semaphore/CMakeLists.txt new file mode 100644 index 0000000..f5ca98f --- /dev/null +++ b/apps/my_semaphore/CMakeLists.txt @@ -0,0 +1,7 @@ +cmake_minimum_required(VERSION 3.20.0) + +find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) + +project(my_semaphore) + +target_sources(app PRIVATE src/main.c) diff --git a/apps/my_semaphore/prj.conf b/apps/my_semaphore/prj.conf new file mode 100644 index 0000000..814117e --- /dev/null +++ b/apps/my_semaphore/prj.conf @@ -0,0 +1,16 @@ +# Set to -Og +CONFIG_DEBUG_OPTIMIZATIONS=y + +# Thread awareness support +CONFIG_DEBUG_THREAD_INFO=y + +# Generate stack usage per-function +CONFIG_STACK_USAGE=y + +# Other options in case not set by default +CONFIG_BUILD_OUTPUT_HEX=y +CONFIG_BUILD_OUTPUT_META=y +CONFIG_OUTPUT_SYMBOLS=y +CONFIG_OUTPUT_STAT=y +CONFIG_OUTPUT_DISASSEMBLY=y +CONFIG_OUTPUT_PRINT_MEMORY_USAGE=y diff --git a/apps/my_semaphore/src/main.c b/apps/my_semaphore/src/main.c new file mode 100644 index 0000000..175a932 --- /dev/null +++ b/apps/my_semaphore/src/main.c @@ -0,0 +1,57 @@ +#include + +// Thread A: higher priority (lower number = higher priority) +#define THREAD_A_STACK_SIZE 512 +#define THREAD_A_PRIORITY 2 + +// Thread B: lower priority +#define THREAD_B_STACK_SIZE 512 +#define THREAD_B_PRIORITY 10 + +// Allocate stack memory for Thread A +K_THREAD_STACK_DEFINE(thread_a_stack, THREAD_A_STACK_SIZE); + +// Thread A control structure and thread ID +struct k_thread thread_a_data; +k_tid_t thread_a_id; + +// Define a semaphore with initial count 0 and maximum count 1 +K_SEM_DEFINE(sync_sem, 0, 1); + +// Thread A: wait for a semaphore signal from Thread B +void thread_a_entry(void *p1, void *p2, void *p3) { + printk("Thread_A: waiting for semaphore\n"); + // Block until the semaphore becomes available + k_sem_take(&sync_sem, K_FOREVER); + printk("Thread_A: semaphore received, continuing\n"); +} + +// Thread B: perform work and signal Thread A +void thread_b_entry(void *p1, void *p2, void *p3) { + printk("Thread_B: doing work\n"); + // Simulate work by sleeping for 1 second + k_sleep(K_SECONDS(1)); + printk("Thread_B: signaling semaphore\n"); + // Release the semaphore to unblock Thread A + k_sem_give(&sync_sem); +} + +int main(void) { + + // Create and start Thread A dynamically at runtime + thread_a_id = + k_thread_create(&thread_a_data, + thread_a_stack, K_THREAD_STACK_SIZEOF(thread_a_stack), + thread_a_entry, NULL, NULL, NULL, + THREAD_A_PRIORITY, 0, K_NO_WAIT); + + // Suppress unused variable warnings + ARG_UNUSED(thread_a_id); + + return 0; +} + +// Define Thread B statically and start it during system initialization +K_THREAD_DEFINE(thread_b_id, THREAD_B_STACK_SIZE, + thread_b_entry, NULL, NULL, NULL, + THREAD_B_PRIORITY, 0, 0); \ No newline at end of file diff --git a/apps/my_workqueue/CMakeLists.txt b/apps/my_workqueue/CMakeLists.txt new file mode 100644 index 0000000..4cccdf0 --- /dev/null +++ b/apps/my_workqueue/CMakeLists.txt @@ -0,0 +1,7 @@ +cmake_minimum_required(VERSION 3.20.0) + +find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) + +project(my_workqueue) + +target_sources(app PRIVATE src/main.c) diff --git a/apps/my_workqueue/prj.conf b/apps/my_workqueue/prj.conf new file mode 100644 index 0000000..814117e --- /dev/null +++ b/apps/my_workqueue/prj.conf @@ -0,0 +1,16 @@ +# Set to -Og +CONFIG_DEBUG_OPTIMIZATIONS=y + +# Thread awareness support +CONFIG_DEBUG_THREAD_INFO=y + +# Generate stack usage per-function +CONFIG_STACK_USAGE=y + +# Other options in case not set by default +CONFIG_BUILD_OUTPUT_HEX=y +CONFIG_BUILD_OUTPUT_META=y +CONFIG_OUTPUT_SYMBOLS=y +CONFIG_OUTPUT_STAT=y +CONFIG_OUTPUT_DISASSEMBLY=y +CONFIG_OUTPUT_PRINT_MEMORY_USAGE=y diff --git a/apps/my_workqueue/src/main.c b/apps/my_workqueue/src/main.c new file mode 100644 index 0000000..484fc9b --- /dev/null +++ b/apps/my_workqueue/src/main.c @@ -0,0 +1,49 @@ +#include + +// Define a work item with event data +struct my_operation { + int event_id; + int value; + struct k_work work; // Zephyr work item +}; + +// Work handler executed by the system workqueue thread +void my_work_handler(struct k_work *work) { + // Retrieve the parent structure from the work item pointer + struct my_operation *op = + CONTAINER_OF(work, struct my_operation, work); + // Process the event + printk("Workqueue: processing event_id=%d value=%d\n", + op->event_id, op->value); +} + +// Define three work items with different event data +struct my_operation op1 = {.event_id = 1, .value = 10}; +struct my_operation op2 = {.event_id = 2, .value = 20}; +struct my_operation op3 = {.event_id = 3, .value = 30}; + +int main(void) { + + // Initialize work items with the same work handler + printk("Main: initializing work items\n"); + k_work_init(&op1.work, my_work_handler); + k_work_init(&op2.work, my_work_handler); + k_work_init(&op3.work, my_work_handler); + + // Submit op1 to the system workqueue + printk("Main: submitting op1\n"); + k_work_submit(&op1.work); + + // Submit op2 to the system workqueue + printk("Main: submitting op2\n"); + k_work_submit(&op2.work); + + // Sleep for 500 ms, allowing other threads to execute + k_sleep(K_MSEC(500)); + + // Submit op3 after the delay + printk("Main: submitting op3\n"); + k_work_submit(&op3.work); + + return 0; +} \ No newline at end of file