diff --git "a/2025/\347\254\2542\347\273\204(RA8D1-Vision-Board)/\350\202\226\345\213\207/Day2/.keep" "b/2025/\347\254\2542\347\273\204(RA8D1-Vision-Board)/\350\202\226\345\213\207/Day2/.keep" new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git "a/2025/\347\254\2542\347\273\204(RA8D1-Vision-Board)/\350\202\226\345\213\207/Day2/main.c" "b/2025/\347\254\2542\347\273\204(RA8D1-Vision-Board)/\350\202\226\345\213\207/Day2/main.c" new file mode 100644 index 0000000000000000000000000000000000000000..661b73ee05260150c313744367c5902bade7d155 --- /dev/null +++ "b/2025/\347\254\2542\347\273\204(RA8D1-Vision-Board)/\350\202\226\345\213\207/Day2/main.c" @@ -0,0 +1,36 @@ +#include + +#define THREAD_PRIORITY_HIGH 6 // 高优先级 +#define THREAD_PRIORITY_MID 7 // 中优先级 +#define THREAD_PRIORITY_LOW 8 // 低优先级 +#define THREAD_TIMESLICE 5 // 时间片(单位:系统节拍数) + +static void thread_entry(void *param) { + const char *thread_name = (const char *)param; + rt_uint32_t count = 0; + + while (1) { + rt_kprintf("%s is running, count = %d\n", thread_name, ++count); + rt_thread_mdelay(100); // 模拟工作,延迟 100ms + } +} + + +static void demo_scheduler(void) { + rt_thread_t tid1, tid2, tid3; + + /* 创建高优先级线程(抢占其他线程) */ + tid1 = rt_thread_create("high_thread", thread_entry, "High", 512, + THREAD_PRIORITY_HIGH, THREAD_TIMESLICE); + if (tid1) rt_thread_startup(tid1); + + /* 创建中优先级线程(与低优先级线程时间片轮转) */ + tid2 = rt_thread_create("mid_thread", thread_entry, "Mid", 512, + THREAD_PRIORITY_MID, THREAD_TIMESLICE); + if (tid2) rt_thread_startup(tid2); + + /* 创建低优先级线程(与中优先级线程时间片轮转) */ + tid3 = rt_thread_create("low_thread", thread_entry, "Low", 512, + THREAD_PRIORITY_LOW, THREAD_TIMESLICE); + if (tid3) rt_thread_startup(tid3); +} diff --git "a/2025/\347\254\2542\347\273\204(RA8D1-Vision-Board)/\350\202\226\345\213\207/Day3/event_example.c" "b/2025/\347\254\2542\347\273\204(RA8D1-Vision-Board)/\350\202\226\345\213\207/Day3/event_example.c" new file mode 100644 index 0000000000000000000000000000000000000000..68282892f4d8d109f206e00fea21000018c02d4d --- /dev/null +++ "b/2025/\347\254\2542\347\273\204(RA8D1-Vision-Board)/\350\202\226\345\213\207/Day3/event_example.c" @@ -0,0 +1,134 @@ +/* + * Copyright (c) 2006-2021, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2025-07-23 xiaoyong the first version + */ + +#include "event_example.h" +/* 定义线程控制块 */ +static rt_thread_t sender_thread = RT_NULL; +static rt_thread_t receiver_thread = RT_NULL; + +/* 定义事件控制块 */ +static struct rt_event event; + +/* 事件接收线程入口函数 */ +static void receiver_thread_entry(void *parameter) +{ + rt_uint32_t recv_event = 0; + + while (1) + { + /* 等待事件发生,接收方式采用逻辑与,RT_WAITING_FOREVER表示永久等待 */ + if (rt_event_recv(&event, + (EVENT_FLAG1 | EVENT_FLAG2 | EVENT_FLAG3), + RT_EVENT_FLAG_AND | RT_EVENT_FLAG_CLEAR, + RT_WAITING_FOREVER, + &recv_event) == RT_EOK) + { + rt_kprintf("receiver thread received event: 0x%x\n", recv_event); + + if (recv_event & EVENT_FLAG1) + { + rt_kprintf("EVENT_FLAG1 received!\n"); + } + + if (recv_event & EVENT_FLAG2) + { + rt_kprintf("EVENT_FLAG2 received!\n"); + } + + if (recv_event & EVENT_FLAG3) + { + rt_kprintf("EVENT_FLAG3 received!\n"); + } + } + } +} + +/* 事件发送线程入口函数 */ +static void sender_thread_entry(void *parameter) +{ + rt_uint8_t count = 0; + + while (1) + { + count++; + + if (count % 3 == 0) + { + /* 发送事件1 */ + rt_event_send(&event, EVENT_FLAG1); + rt_kprintf("sender thread send EVENT_FLAG1\n"); + } + else if (count % 3 == 1) + { + /* 发送事件2 */ + rt_event_send(&event, EVENT_FLAG2); + rt_kprintf("sender thread send EVENT_FLAG2\n"); + } + else + { + /* 发送事件3 */ + rt_event_send(&event, EVENT_FLAG3); + rt_kprintf("sender thread send EVENT_FLAG3\n"); + } + + rt_thread_mdelay(2000); // 每2秒发送一次事件 + } +} + +/* 事件示例初始化函数 */ +int event_example_init(void) +{ + rt_err_t result; + + /* 初始化事件对象 */ + result = rt_event_init(&event, "event", RT_IPC_FLAG_FIFO); + if (result != RT_EOK) + { + rt_kprintf("init event failed.\n"); + return -1; + } + + /* 创建事件接收线程 */ + receiver_thread = rt_thread_create("receiver", + receiver_thread_entry, + RT_NULL, + 512, + 25, + 10); + if (receiver_thread != RT_NULL) + { + rt_thread_startup(receiver_thread); + } + else + { + rt_kprintf("create receiver thread failed.\n"); + return -1; + } + + /* 创建事件发送线程 */ + sender_thread = rt_thread_create("sender", + sender_thread_entry, + RT_NULL, + 512, + 24, + 10); + if (sender_thread != RT_NULL) + { + rt_thread_startup(sender_thread); + } + else + { + rt_kprintf("create sender thread failed.\n"); + return -1; + } + + return 0; +} + diff --git "a/2025/\347\254\2542\347\273\204(RA8D1-Vision-Board)/\350\202\226\345\213\207/Day3/event_example.h" "b/2025/\347\254\2542\347\273\204(RA8D1-Vision-Board)/\350\202\226\345\213\207/Day3/event_example.h" new file mode 100644 index 0000000000000000000000000000000000000000..53254eb651a1b0008323e8288ab0c3fd5da0d627 --- /dev/null +++ "b/2025/\347\254\2542\347\273\204(RA8D1-Vision-Board)/\350\202\226\345\213\207/Day3/event_example.h" @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2006-2021, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2025-07-23 xiaoyong the first version + */ +#ifndef RA_GEN_EVENT_EXAMPLE_H_ +#define RA_GEN_EVENT_EXAMPLE_H_ + +#include + +/* 定义事件标志 */ +#define EVENT_FLAG1 (1 << 0) // 事件1 +#define EVENT_FLAG2 (1 << 1) // 事件2 +#define EVENT_FLAG3 (1 << 2) // 事件3 +int event_example_init(void); + +#endif /* RA_GEN_EVENT_EXAMPLE_H_ */ diff --git "a/2025/\347\254\2542\347\273\204(RA8D1-Vision-Board)/\350\202\226\345\213\207/Day3/mailbox_example.c" "b/2025/\347\254\2542\347\273\204(RA8D1-Vision-Board)/\350\202\226\345\213\207/Day3/mailbox_example.c" new file mode 100644 index 0000000000000000000000000000000000000000..06b7ee93667d2d5bdb4220e48ea8aba7be602eea --- /dev/null +++ "b/2025/\347\254\2542\347\273\204(RA8D1-Vision-Board)/\350\202\226\345\213\207/Day3/mailbox_example.c" @@ -0,0 +1,84 @@ +/* + * Copyright (c) 2006-2021, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2025-07-23 xiaoyong the first version + */ +#include "mailbox_example.h" + +/* 邮箱控制块 */ +static struct rt_mailbox mb; +static char mb_pool[128]; + +static void thread1_entry(void *parameter) +{ + char buf[32]; + rt_uint32_t count = 0; + + while (count < 10) + { + rt_snprintf(buf, sizeof(buf), "mail%d", count); + /* 发送邮件 */ + rt_mb_send(&mb, (rt_uint32_t)buf); + rt_kprintf("thread1 send mail: %s\n", buf); + + count++; + rt_thread_mdelay(500); + } +} + +static void thread2_entry(void *parameter) +{ + char *str; + + while (1) + { + /* 接收邮件 */ + if (rt_mb_recv(&mb, (rt_uint32_t *)&str, RT_WAITING_FOREVER) == RT_EOK) + { + rt_kprintf("thread2 received mail: %s\n", str); + } + } +} + +int mailbox_example_init(void) +{ + rt_err_t result; + + /* 初始化邮箱 */ + result = rt_mb_init(&mb, + "mbt", /* 名称 */ + &mb_pool[0], /* 内存池 */ + sizeof(mb_pool) / 4, /* 大小 */ + RT_IPC_FLAG_FIFO); /* FIFO方式 */ + if (result != RT_EOK) + { + rt_kprintf("init mailbox failed.\n"); + return -1; + } + + /* 创建线程1 */ + rt_thread_t thread1 = rt_thread_create("thread1", + thread1_entry, + RT_NULL, + THREAD_STACK_SIZE, + THREAD_PRIORITY - 1, + THREAD_TIMESLICE); + if (thread1 != RT_NULL) + rt_thread_startup(thread1); + + /* 创建线程2 */ + rt_thread_t thread2 = rt_thread_create("thread2", + thread2_entry, + RT_NULL, + THREAD_STACK_SIZE, + THREAD_PRIORITY, + THREAD_TIMESLICE); + if (thread2 != RT_NULL) + rt_thread_startup(thread2); + + return 0; +} diff --git "a/2025/\347\254\2542\347\273\204(RA8D1-Vision-Board)/\350\202\226\345\213\207/Day3/mailbox_example.h" "b/2025/\347\254\2542\347\273\204(RA8D1-Vision-Board)/\350\202\226\345\213\207/Day3/mailbox_example.h" new file mode 100644 index 0000000000000000000000000000000000000000..6fcff803a50c54c84c64c14937ac2aa608418039 --- /dev/null +++ "b/2025/\347\254\2542\347\273\204(RA8D1-Vision-Board)/\350\202\226\345\213\207/Day3/mailbox_example.h" @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2006-2021, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2025-07-23 xiaoyong the first version + */ +#ifndef RA_GEN_MAILBOX_EXAMPLE_H_ +#define RA_GEN_MAILBOX_EXAMPLE_H_ + + +#include + +#define THREAD_PRIORITY 25 +#define THREAD_STACK_SIZE 512 +#define THREAD_TIMESLICE 5 +int mailbox_example_init(void); + +#endif /* RA_GEN_MAILBOX_EXAMPLE_H_ */ diff --git "a/2025/\347\254\2542\347\273\204(RA8D1-Vision-Board)/\350\202\226\345\213\207/Day3/message_queue_example.c" "b/2025/\347\254\2542\347\273\204(RA8D1-Vision-Board)/\350\202\226\345\213\207/Day3/message_queue_example.c" new file mode 100644 index 0000000000000000000000000000000000000000..3b1687faeca1798ff3dc4dd350437db10d7c25aa --- /dev/null +++ "b/2025/\347\254\2542\347\273\204(RA8D1-Vision-Board)/\350\202\226\345\213\207/Day3/message_queue_example.c" @@ -0,0 +1,96 @@ +/* + * Copyright (c) 2006-2021, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2025-07-23 xiaoyong the first version + */ + +#include "message_queue_example.h" +/* 消息队列控制块 */ +static struct rt_messagequeue mq; +static rt_uint8_t mq_pool[2048]; + +struct msg +{ + rt_uint8_t type; + char data[32]; +}; + +static void thread1_entry(void *parameter) +{ + struct msg msg; + rt_uint32_t count = 0; + + while (count < 10) + { + msg.type = count % 3; + rt_snprintf(msg.data, sizeof(msg.data), "message%d", count); + + /* 发送消息 */ + rt_mq_send(&mq, &msg, sizeof(struct msg)); + rt_kprintf("thread1 send message: %s\n", msg.data); + + count++; + rt_thread_mdelay(500); + } +} + +static void thread2_entry(void *parameter) +{ + struct msg msg; + rt_size_t recv_size; + + while (1) + { + /* 接收消息 */ + recv_size = rt_mq_recv(&mq, &msg, sizeof(struct msg), RT_WAITING_FOREVER); + if (recv_size > 0) + { + rt_kprintf("thread2 received message[%d]: %s\n", msg.type, msg.data); + } + } +} + +int message_queue_example_init(void) +{ + rt_err_t result; + + /* 初始化消息队列 */ + result = rt_mq_init(&mq, + "mqt", + &mq_pool[0], + 32, /* 消息大小 */ + sizeof(mq_pool), /* 内存池大小 */ + RT_IPC_FLAG_FIFO); /* FIFO方式 */ + if (result != RT_EOK) + { + rt_kprintf("init message queue failed.\n"); + return -1; + } + + /* 创建线程1 */ + rt_thread_t thread1 = rt_thread_create("thread1", + thread1_entry, + RT_NULL, + THREAD_STACK_SIZE, + THREAD_PRIORITY - 1, + THREAD_TIMESLICE); + if (thread1 != RT_NULL) + rt_thread_startup(thread1); + + /* 创建线程2 */ + rt_thread_t thread2 = rt_thread_create("thread2", + thread2_entry, + RT_NULL, + THREAD_STACK_SIZE, + THREAD_PRIORITY, + THREAD_TIMESLICE); + if (thread2 != RT_NULL) + rt_thread_startup(thread2); + + return 0; +} + diff --git "a/2025/\347\254\2542\347\273\204(RA8D1-Vision-Board)/\350\202\226\345\213\207/Day3/message_queue_example.h" "b/2025/\347\254\2542\347\273\204(RA8D1-Vision-Board)/\350\202\226\345\213\207/Day3/message_queue_example.h" new file mode 100644 index 0000000000000000000000000000000000000000..163b8753400d7eabc8a8ab244d1e5bbff121fa31 --- /dev/null +++ "b/2025/\347\254\2542\347\273\204(RA8D1-Vision-Board)/\350\202\226\345\213\207/Day3/message_queue_example.h" @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2006-2021, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2025-07-23 xiaoyong the first version + */ +#ifndef RA_GEN_MESSAGE_QUEUE_EXAMPLE_H_ +#define RA_GEN_MESSAGE_QUEUE_EXAMPLE_H_ + +#include + +#define THREAD_PRIORITY 25 +#define THREAD_STACK_SIZE 512 +#define THREAD_TIMESLICE 5 +int message_queue_example_init(void); + +#endif /* RA_GEN_MESSAGE_QUEUE_EXAMPLE_H_ */ diff --git "a/2025/\347\254\2542\347\273\204(RA8D1-Vision-Board)/\350\202\226\345\213\207/Day3/mutex_sample.c" "b/2025/\347\254\2542\347\273\204(RA8D1-Vision-Board)/\350\202\226\345\213\207/Day3/mutex_sample.c" new file mode 100644 index 0000000000000000000000000000000000000000..e2d97576ac559c420b7bc951966b8153733e5224 --- /dev/null +++ "b/2025/\347\254\2542\347\273\204(RA8D1-Vision-Board)/\350\202\226\345\213\207/Day3/mutex_sample.c" @@ -0,0 +1,87 @@ +/* + * Copyright (c) 2006-2021, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2025-07-23 xiaoyong the first version + */ +#include "mutex_sample.h" + +static rt_thread_t thread1 = RT_NULL; +static rt_thread_t thread2 = RT_NULL; +static rt_mutex_t mutex = RT_NULL; + +/* 共享资源 */ +static int shared_count = 0; + +static void thread1_entry(void *parameter) +{ + while (1) + { + if (rt_mutex_take(mutex, RT_WAITING_FOREVER) == RT_EOK) + { + /* 进入临界区 */ + rt_kprintf("Thread 1 entering critical section.\n"); + shared_count++; + rt_kprintf("Thread 1 updated count to %d\n", shared_count); + rt_thread_mdelay(500); // 模拟耗时操作 + rt_kprintf("Thread 1 leaving critical section.\n"); + rt_mutex_release(mutex); + } + rt_thread_mdelay(1000); + } +} + +static void thread2_entry(void *parameter) +{ + while (1) + { + if (rt_mutex_take(mutex, RT_WAITING_FOREVER) == RT_EOK) + { + /* 进入临界区 */ + rt_kprintf("Thread 2 entering critical section.\n"); + shared_count++; + rt_kprintf("Thread 2 updated count to %d\n", shared_count); + rt_thread_mdelay(500); // 模拟耗时操作 + rt_kprintf("Thread 2 leaving critical section.\n"); + rt_mutex_release(mutex); + } + rt_thread_mdelay(1000); + } +} + +int mutex_example_init(void) +{ + /* 创建互斥量 */ + mutex = rt_mutex_create("mutex_lock", RT_IPC_FLAG_FIFO); + if (mutex == RT_NULL) + { + rt_kprintf("Failed to create mutex.\n"); + return -1; + } + + /* 创建线程 1 */ + thread1 = rt_thread_create("t1", + thread1_entry, + RT_NULL, + THREAD_STACK_SIZE, + THREAD_PRIORITY, + THREAD_TIMESLICE); + if (thread1 != RT_NULL) + rt_thread_startup(thread1); + + /* 创建线程 2 */ + thread2 = rt_thread_create("t2", + thread2_entry, + RT_NULL, + THREAD_STACK_SIZE, + THREAD_PRIORITY, + THREAD_TIMESLICE); + if (thread2 != RT_NULL) + rt_thread_startup(thread2); + + return 0; +} + diff --git "a/2025/\347\254\2542\347\273\204(RA8D1-Vision-Board)/\350\202\226\345\213\207/Day3/mutex_sample.h" "b/2025/\347\254\2542\347\273\204(RA8D1-Vision-Board)/\350\202\226\345\213\207/Day3/mutex_sample.h" new file mode 100644 index 0000000000000000000000000000000000000000..d295ad5206cbb6dcca16a8420d6eb9fcb0d0c3fb --- /dev/null +++ "b/2025/\347\254\2542\347\273\204(RA8D1-Vision-Board)/\350\202\226\345\213\207/Day3/mutex_sample.h" @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2006-2021, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2025-07-23 xiaoyong the first version + */ +#ifndef RA_GEN_MUTEX_SAMPLE_H_ +#define RA_GEN_MUTEX_SAMPLE_H_ + +#include + +#define THREAD_STACK_SIZE 512 +#define THREAD_PRIORITY 20 +#define THREAD_TIMESLICE 10 +int mutex_example_init(void); + +#endif /* RA_GEN_MUTEX_SAMPLE_H_ */ diff --git "a/2025/\347\254\2542\347\273\204(RA8D1-Vision-Board)/\350\202\226\345\213\207/Day3/sem_sample.c" "b/2025/\347\254\2542\347\273\204(RA8D1-Vision-Board)/\350\202\226\345\213\207/Day3/sem_sample.c" new file mode 100644 index 0000000000000000000000000000000000000000..c3fcf3b6f1a023a5517c041507d0051c07f0113d --- /dev/null +++ "b/2025/\347\254\2542\347\273\204(RA8D1-Vision-Board)/\350\202\226\345\213\207/Day3/sem_sample.c" @@ -0,0 +1,116 @@ +/* + * Copyright (c) 2006-2021, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2025-07-23 xiaoyong the first version + */ +#include "sem_sample.h" +/* 缓冲区与指针 */ +static int buffer[BUFFER_SIZE]; +static int write_index = 0; +static int read_index = 0; + +/* 信号量和线程句柄 */ +static rt_sem_t sem_empty; // 表示空格数量 +static rt_sem_t sem_full; // 表示已有数据数量 +static rt_mutex_t buffer_mutex; // 保护共享资源 + +static rt_thread_t producer_tid = RT_NULL; +static rt_thread_t consumer_tid = RT_NULL; + +static void producer_entry(void *parameter) +{ + int item = 0; + + while (1) + { + item++; + + /* 等待空位 */ + rt_sem_take(sem_empty, RT_WAITING_FOREVER); + + /* 加锁 */ + rt_mutex_take(buffer_mutex, RT_WAITING_FOREVER); + + /* 放入数据 */ + buffer[write_index] = item; + rt_kprintf("Producer: produced item %d at index %d\n", item, write_index); + write_index = (write_index + 1) % BUFFER_SIZE; + + /* 解锁 */ + rt_mutex_release(buffer_mutex); + + /* 通知有新数据 */ + rt_sem_release(sem_full); + + rt_thread_mdelay(500); // 模拟生产时间 + } +} + +static void consumer_entry(void *parameter) +{ + int item; + + while (1) + { + /* 等待数据 */ + rt_sem_take(sem_full, RT_WAITING_FOREVER); + + /* 加锁 */ + rt_mutex_take(buffer_mutex, RT_WAITING_FOREVER); + + /* 取出数据 */ + item = buffer[read_index]; + rt_kprintf("Consumer: consumed item %d at index %d\n", item, read_index); + read_index = (read_index + 1) % BUFFER_SIZE; + + /* 解锁 */ + rt_mutex_release(buffer_mutex); + + /* 通知空位增加 */ + rt_sem_release(sem_empty); + + rt_thread_mdelay(1000); // 模拟消费时间 + } +} + +int sem_example_init(void) +{ + /* 创建信号量,初始空位 BUFFER_SIZE,已用位 0 */ + sem_empty = rt_sem_create("sem_e", BUFFER_SIZE, RT_IPC_FLAG_FIFO); + sem_full = rt_sem_create("sem_f", 0, RT_IPC_FLAG_FIFO); + buffer_mutex = rt_mutex_create("buf_mtx", RT_IPC_FLAG_FIFO); + + if (sem_empty == RT_NULL || sem_full == RT_NULL || buffer_mutex == RT_NULL) + { + rt_kprintf("Failed to create semaphores or mutex.\n"); + return -1; + } + + /* 创建生产者线程 */ + producer_tid = rt_thread_create("producer", + producer_entry, + RT_NULL, + THREAD_STACK_SIZE, + THREAD_PRIORITY, + THREAD_TIMESLICE); + if (producer_tid != RT_NULL) + rt_thread_startup(producer_tid); + + /* 创建消费者线程 */ + consumer_tid = rt_thread_create("consumer", + consumer_entry, + RT_NULL, + THREAD_STACK_SIZE, + THREAD_PRIORITY, + THREAD_TIMESLICE); + if (consumer_tid != RT_NULL) + rt_thread_startup(consumer_tid); + + return 0; +} + +//INIT_APP_EXPORT(sem_example_init); diff --git "a/2025/\347\254\2542\347\273\204(RA8D1-Vision-Board)/\350\202\226\345\213\207/Day3/sem_sample.h" "b/2025/\347\254\2542\347\273\204(RA8D1-Vision-Board)/\350\202\226\345\213\207/Day3/sem_sample.h" new file mode 100644 index 0000000000000000000000000000000000000000..d1714dedce22bc5aedc1f3231952534c7275eb2f --- /dev/null +++ "b/2025/\347\254\2542\347\273\204(RA8D1-Vision-Board)/\350\202\226\345\213\207/Day3/sem_sample.h" @@ -0,0 +1,23 @@ +/* + * Copyright (c) 2006-2021, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2025-07-23 xiaoyong the first version + */ +#ifndef RA_GEN_SEM_SAMPLE_H_ +#define RA_GEN_SEM_SAMPLE_H_ +#include + +#define THREAD_STACK_SIZE 512 +#define THREAD_PRIORITY 20 +#define THREAD_TIMESLICE 10 + +#define BUFFER_SIZE 5 + +int sem_example_init(void); + + +#endif /* RA_GEN_SEM_SAMPLE_H_ */ diff --git "a/2025/\347\254\2542\347\273\204(RA8D1-Vision-Board)/\350\202\226\345\213\207/Day3/signal_example.c" "b/2025/\347\254\2542\347\273\204(RA8D1-Vision-Board)/\350\202\226\345\213\207/Day3/signal_example.c" new file mode 100644 index 0000000000000000000000000000000000000000..39dbc2ce93daea7642cf35da5eb05b5750c606f6 --- /dev/null +++ "b/2025/\347\254\2542\347\273\204(RA8D1-Vision-Board)/\350\202\226\345\213\207/Day3/signal_example.c" @@ -0,0 +1,58 @@ +/* + * Copyright (c) 2006-2021, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2025-07-23 xiaoyong the first version + */ + +#include "signal_example.h" +static rt_thread_t tid1 = RT_NULL; + +/* 线程1的信号处理函数 */ +void thread1_signal_handler(int sig) +{ + rt_kprintf("thread1 received signal %d\n", sig); +} + +/* 线程1的入口函数 */ +static void thread1_entry(void *parameter) +{ + int cnt = 0; + + /* 安装信号 */ + rt_signal_install(SIGUSR1, thread1_signal_handler); + rt_signal_unmask(SIGUSR1); + + /* 运行10次 */ + while (cnt < 10) + { + /* 线程1采用低优先级运行,一直打印计数值 */ + rt_kprintf("thread1 count : %d\n", cnt); + + cnt++; + rt_thread_mdelay(100); + } +} + +/* 信号示例的初始化 */ +int signal_example_init(void) +{ + /* 创建线程1 */ + tid1 = rt_thread_create("thread1", + thread1_entry, RT_NULL, + THREAD_STACK_SIZE, + THREAD_PRIORITY, THREAD_TIMESLICE); + + if (tid1 != RT_NULL) + rt_thread_startup(tid1); + + rt_thread_mdelay(300); + + /* 发送信号 SIGUSR1 给线程1 */ + rt_thread_kill(tid1, SIGUSR1); + + return 0; +} diff --git "a/2025/\347\254\2542\347\273\204(RA8D1-Vision-Board)/\350\202\226\345\213\207/Day3/signal_example.h" "b/2025/\347\254\2542\347\273\204(RA8D1-Vision-Board)/\350\202\226\345\213\207/Day3/signal_example.h" new file mode 100644 index 0000000000000000000000000000000000000000..3d51a56c1984233656626ddc624f8417190bcdc8 --- /dev/null +++ "b/2025/\347\254\2542\347\273\204(RA8D1-Vision-Board)/\350\202\226\345\213\207/Day3/signal_example.h" @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2006-2021, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2025-07-23 xiaoyong the first version + */ +#ifndef RA_GEN_SIGNAL_EXAMPLE_H_ +#define RA_GEN_SIGNAL_EXAMPLE_H_ + +#include + +#define THREAD_PRIORITY 25 +#define THREAD_STACK_SIZE 512 +#define THREAD_TIMESLICE 5 +int signal_example_init(void); + +#endif /* RA_GEN_SIGNAL_EXAMPLE_H_ */ diff --git "a/2025/\347\254\2542\347\273\204(RA8D1-Vision-Board)/\350\202\226\345\213\207/Day4/drv_vir.c" "b/2025/\347\254\2542\347\273\204(RA8D1-Vision-Board)/\350\202\226\345\213\207/Day4/drv_vir.c" new file mode 100644 index 0000000000000000000000000000000000000000..f5fb04fafa524b0a5634040bd6b1fed72d3e88be --- /dev/null +++ "b/2025/\347\254\2542\347\273\204(RA8D1-Vision-Board)/\350\202\226\345\213\207/Day4/drv_vir.c" @@ -0,0 +1,43 @@ +#include "drv_vir.h" + +static rt_err_t add_one(struct rt_device *device) +{ + struct drv_vir *vir = rt_container_of(device, struct drv_vir, parent); + vir->counter++; + return RT_EOK; +} + +static rt_err_t subtract_one(struct rt_device *device) +{ + struct drv_vir *vir = rt_container_of(device, struct drv_vir, parent); + vir->counter--; + return RT_EOK; +} + +static rt_uint32_t get_val(struct rt_device *device) +{ + struct drv_vir *vir = rt_container_of(device, struct drv_vir, parent); + return vir->counter; +} + +static rt_err_t set_val(struct rt_device *device, rt_uint32_t val) +{ + struct drv_vir *vir = rt_container_of(device, struct drv_vir, parent); + vir->counter = val; + return RT_EOK; +} +static const struct rt_vir_ops ops = { + add_one, + subtract_one, + get_val, + set_val // 没有实现set_val +}; + +static struct drv_vir vir_dev; + +static int vir_init(void) +{ + vir_dev.counter = 0; + return rt_hw_vir_register(&vir_dev.parent, "vir", &ops, &vir_dev); +} +//INIT_APP_EXPORT(vir_init); diff --git "a/2025/\347\254\2542\347\273\204(RA8D1-Vision-Board)/\350\202\226\345\213\207/Day4/drv_vir.h" "b/2025/\347\254\2542\347\273\204(RA8D1-Vision-Board)/\350\202\226\345\213\207/Day4/drv_vir.h" new file mode 100644 index 0000000000000000000000000000000000000000..cb1628b8a1301da6c0fabed90e53c7000734f5ba --- /dev/null +++ "b/2025/\347\254\2542\347\273\204(RA8D1-Vision-Board)/\350\202\226\345\213\207/Day4/drv_vir.h" @@ -0,0 +1,13 @@ +#ifndef LIBRARIES_HAL_DRIVERS_DRV_VIR_H_ +#define LIBRARIES_HAL_DRIVERS_DRV_VIR_H_ + +#include +#include +#include "vir.h" + +struct drv_vir { + struct rt_vir_device parent; + rt_uint32_t counter; +}; + +#endif diff --git "a/2025/\347\254\2542\347\273\204(RA8D1-Vision-Board)/\350\202\226\345\213\207/Day4/vir.c" "b/2025/\347\254\2542\347\273\204(RA8D1-Vision-Board)/\350\202\226\345\213\207/Day4/vir.c" new file mode 100644 index 0000000000000000000000000000000000000000..152ce191aaa323d1bbefabdf9b3f90139048f45b --- /dev/null +++ "b/2025/\347\254\2542\347\273\204(RA8D1-Vision-Board)/\350\202\226\345\213\207/Day4/vir.c" @@ -0,0 +1,94 @@ +#include "vir.h" + +static rt_err_t _vir_init(struct rt_device *dev) +{ + rt_kprintf("vir init\n"); + return RT_EOK; +} + +static rt_err_t _vir_open(struct rt_device *dev, rt_uint16_t oflag) +{ + rt_kprintf("vir open\n"); + return RT_EOK; +} + +static rt_err_t _vir_close(struct rt_device *dev) +{ + rt_kprintf("vir close\n"); + return RT_EOK; +} + +static rt_ssize_t _vir_read(struct rt_device *dev, rt_off_t pos, + void *buffer, rt_size_t size) +{ + rt_vir_device_t vir_dev = (rt_vir_device_t)dev; + + if (vir_dev->ops && vir_dev->ops->get_val && size >= sizeof(rt_uint32_t)) { + *((rt_uint32_t *)buffer) = vir_dev->ops->get_val(dev); + return sizeof(rt_uint32_t); + } + return -RT_ERROR; +} + +static rt_ssize_t _vir_write(struct rt_device *dev, rt_off_t pos, + const void *buffer, rt_size_t size) +{ + rt_vir_device_t vir_dev = (rt_vir_device_t)dev; + + if (vir_dev->ops && vir_dev->ops->set_val && size >= sizeof(rt_uint32_t)) { + return vir_dev->ops->set_val(dev, *((const rt_uint32_t *)buffer)); + } + return -RT_ERROR; +} + +static rt_err_t _vir_control(struct rt_device *dev, int cmd, void *args) +{ + rt_kprintf("vir control %d\n", cmd); + return RT_EOK; +} + +rt_err_t rt_hw_vir_register(rt_vir_device_t device, const char *name, + const struct rt_vir_ops *ops, void *user_data) +{ + RT_ASSERT(device != RT_NULL); + RT_ASSERT(ops != RT_NULL); + + device->ops = ops; + device->user_data = user_data; + + device->parent.type = RT_Device_Class_Char; + device->parent.init = _vir_init; + device->parent.open = _vir_open; + device->parent.close = _vir_close; + device->parent.read = _vir_read; + device->parent.write = _vir_write; + device->parent.control = _vir_control; + + return rt_device_register(&device->parent, name, RT_DEVICE_FLAG_RDWR); +} + +rt_err_t rt_vir_read(rt_vir_device_t device, rt_uint32_t *value) +{ + RT_ASSERT(device != RT_NULL); + RT_ASSERT(value != RT_NULL); + + if (device->ops && device->ops->get_val) { + *value = device->ops->get_val(&device->parent); + return RT_EOK; + } + return -RT_ERROR; +} + +rt_err_t rt_vir_control(rt_vir_device_t device, rt_bool_t increase) +{ + RT_ASSERT(device != RT_NULL); + + if (device->ops) { + if (increase && device->ops->add_one) { + return device->ops->add_one(&device->parent); + } else if (!increase && device->ops->subtract_one) { + return device->ops->subtract_one(&device->parent); + } + } + return -RT_ERROR; +} diff --git "a/2025/\347\254\2542\347\273\204(RA8D1-Vision-Board)/\350\202\226\345\213\207/Day4/vir.h" "b/2025/\347\254\2542\347\273\204(RA8D1-Vision-Board)/\350\202\226\345\213\207/Day4/vir.h" new file mode 100644 index 0000000000000000000000000000000000000000..8087355bb214a31720f6a2902ef32251ec26e2f2 --- /dev/null +++ "b/2025/\347\254\2542\347\273\204(RA8D1-Vision-Board)/\350\202\226\345\213\207/Day4/vir.h" @@ -0,0 +1,30 @@ +#ifndef LIBRARIES_HAL_DRIVERS_VIR_H_ +#define LIBRARIES_HAL_DRIVERS_VIR_H_ + +#include +#include + +/* 虚拟设备操作结构体 */ +struct rt_vir_ops { + rt_err_t (*add_one)(struct rt_device *device); + rt_err_t (*subtract_one)(struct rt_device *device); + rt_uint32_t (*get_val)(struct rt_device *device); + rt_err_t (*set_val)(struct rt_device *device, rt_uint32_t value); +}; + +/* 虚拟设备结构体 */ +struct rt_vir_device { + struct rt_device parent; + const struct rt_vir_ops *ops; + void *user_data; +}; + +typedef struct rt_vir_device *rt_vir_device_t; + +/* 框架API */ +rt_err_t rt_hw_vir_register(rt_vir_device_t device, const char *name, + const struct rt_vir_ops *ops, void *user_data); +rt_err_t rt_vir_read(rt_vir_device_t device, rt_uint32_t *value); +rt_err_t rt_vir_control(rt_vir_device_t device, rt_bool_t increase); + +#endif diff --git "a/2025/\347\254\2542\347\273\204(RA8D1-Vision-Board)/\350\202\226\345\213\207/Day5/.keep" "b/2025/\347\254\2542\347\273\204(RA8D1-Vision-Board)/\350\202\226\345\213\207/Day5/.keep" new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git "a/2025/\347\254\2542\347\273\204(RA8D1-Vision-Board)/\350\202\226\345\213\207/Day5/PixPin_2025-07-26_00-46-51.png" "b/2025/\347\254\2542\347\273\204(RA8D1-Vision-Board)/\350\202\226\345\213\207/Day5/PixPin_2025-07-26_00-46-51.png" new file mode 100644 index 0000000000000000000000000000000000000000..99b73e7d94d26fbbe1860b6dcb0677abfb0751eb Binary files /dev/null and "b/2025/\347\254\2542\347\273\204(RA8D1-Vision-Board)/\350\202\226\345\213\207/Day5/PixPin_2025-07-26_00-46-51.png" differ diff --git "a/2025/\347\254\2542\347\273\204(RA8D1-Vision-Board)/\350\202\226\345\213\207/Day5/PixPin_2025-07-26_00-50-55.png" "b/2025/\347\254\2542\347\273\204(RA8D1-Vision-Board)/\350\202\226\345\213\207/Day5/PixPin_2025-07-26_00-50-55.png" new file mode 100644 index 0000000000000000000000000000000000000000..1da4886a9742a11684c9379f54d6388c712122a6 Binary files /dev/null and "b/2025/\347\254\2542\347\273\204(RA8D1-Vision-Board)/\350\202\226\345\213\207/Day5/PixPin_2025-07-26_00-50-55.png" differ diff --git "a/2025/\347\254\2542\347\273\204(RA8D1-Vision-Board)/\350\202\226\345\213\207/Day5/test.c" "b/2025/\347\254\2542\347\273\204(RA8D1-Vision-Board)/\350\202\226\345\213\207/Day5/test.c" new file mode 100644 index 0000000000000000000000000000000000000000..68f431cd1e6209df4e295ed7116f038a9931565a --- /dev/null +++ "b/2025/\347\254\2542\347\273\204(RA8D1-Vision-Board)/\350\202\226\345\213\207/Day5/test.c" @@ -0,0 +1,100 @@ + +/* + * @Author: jiejie + * @Github: https://github.com/jiejieTop + * @LastEditTime: 2020-06-17 14:35:29 + * @Description: the code belongs to jiejie, please keep the author information and source code according to the license. + */ +#include +#include +#include +#include +#include +#include "mqttclient.h" + +#ifndef KAWAII_MQTT_HOST +#define KAWAII_MQTT_HOST "jiejie01.top" +#endif +#ifndef KAWAII_MQTT_PORT +#define KAWAII_MQTT_PORT "1883" +#endif +#ifndef KAWAII_MQTT_CLIENTID +#define KAWAII_MQTT_CLIENTID "rtthread001" +#endif +#ifndef KAWAII_MQTT_USERNAME +#define KAWAII_MQTT_USERNAME "xiaoyong" +#endif +#ifndef KAWAII_MQTT_PASSWORD +#define KAWAII_MQTT_PASSWORD "x31415926y" +#endif +#ifndef KAWAII_MQTT_SUBTOPIC +#define KAWAII_MQTT_SUBTOPIC "rtt-sub" +#endif +#ifndef KAWAII_MQTT_PUBTOPIC +#define KAWAII_MQTT_PUBTOPIC "rtt-pub-314" +#endif + +static void sub_topic_handle1(void* client, message_data_t* msg) +{ + (void) client; + KAWAII_MQTT_LOG_I("-----------------------------------------------------------------------------------"); + KAWAII_MQTT_LOG_I("%s:%d %s()...\ntopic: %s\nmessage:%s", __FILE__, __LINE__, __FUNCTION__, msg->topic_name, (char*)msg->message->payload); + KAWAII_MQTT_LOG_I("-----------------------------------------------------------------------------------"); +} + + +static int mqtt_publish_handle1(mqtt_client_t *client) +{ + mqtt_message_t msg; + memset(&msg, 0, sizeof(msg)); + + msg.qos = QOS0; + msg.payload = (void *)"this is a kawaii mqtt test ..."; + + return mqtt_publish(client, KAWAII_MQTT_PUBTOPIC, &msg); +} + +static void kawaii_mqtt_demo(void *parameter) +{ + mqtt_client_t *client = NULL; + + rt_thread_delay(6000); + + mqtt_log_init(); + + client = mqtt_lease(); + + mqtt_set_host(client, KAWAII_MQTT_HOST); + mqtt_set_port(client, KAWAII_MQTT_PORT); + mqtt_set_user_name(client, KAWAII_MQTT_USERNAME); + mqtt_set_password(client, KAWAII_MQTT_PASSWORD); + mqtt_set_client_id(client, KAWAII_MQTT_CLIENTID); + mqtt_set_clean_session(client, 1); + + KAWAII_MQTT_LOG_I("The ID of the Kawaii client is: %s ", KAWAII_MQTT_CLIENTID); + + mqtt_connect(client); + + mqtt_subscribe(client, KAWAII_MQTT_SUBTOPIC, QOS0, sub_topic_handle1); + + while (1) { + mqtt_publish_handle1(client); + + mqtt_sleep_ms(4 * 1000); + } +} + +int ka_mqtt(void) +{ + rt_thread_t tid_mqtt; + + tid_mqtt = rt_thread_create("kawaii_demo", kawaii_mqtt_demo, RT_NULL, 2048, 17, 10); + if (tid_mqtt == RT_NULL) { + return -RT_ERROR; + } + + rt_thread_startup(tid_mqtt); + + return RT_EOK; +} +MSH_CMD_EXPORT(ka_mqtt, Kawaii MQTT client test program); diff --git "a/2025/\347\254\2542\347\273\204(RA8D1-Vision-Board)/\350\202\226\345\213\207/\347\254\224\350\256\2601.md" "b/2025/\347\254\2542\347\273\204(RA8D1-Vision-Board)/\350\202\226\345\213\207/\347\254\224\350\256\2601.md" new file mode 100644 index 0000000000000000000000000000000000000000..d1d6d4ca013db790bc4ea0300b407ff8980dd3a2 --- /dev/null +++ "b/2025/\347\254\2542\347\273\204(RA8D1-Vision-Board)/\350\202\226\345\213\207/\347\254\224\350\256\2601.md" @@ -0,0 +1 @@ +这只是一个测试 \ No newline at end of file