Update repo

This commit is contained in:
hangpersonal committed 2026-10-19 23:09:38 -07:00
1 parent 42d54dc132
commit 6c33721f5e
19 files changed
+491 -1

No files matched your search

+7
View File
@@ -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)
+16
View File
@@ -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
+57
View File
@@ -0,0 +1,57 @@
#include <zephyr/kernel.h>
// 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);