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_msgq)
target_sources(app PRIVATE src/main.c)
+19
View File
@@ -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
+64
View File
@@ -0,0 +1,64 @@
#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 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);