diff --git "a/2025/\347\254\2543\347\273\204(PSoC62 evaluation Kit)/fsp/\344\275\234\344\270\232/\347\254\2542\345\244\251\344\275\234\344\270\232/main.c" "b/2025/\347\254\2543\347\273\204(PSoC62 evaluation Kit)/fsp/\344\275\234\344\270\232/\347\254\2542\345\244\251\344\275\234\344\270\232/main.c" new file mode 100644 index 0000000000000000000000000000000000000000..91fbb4112b2b2151038f76bafad65dfdfa020c68 --- /dev/null +++ "b/2025/\347\254\2543\347\273\204(PSoC62 evaluation Kit)/fsp/\344\275\234\344\270\232/\347\254\2542\345\244\251\344\275\234\344\270\232/main.c" @@ -0,0 +1,113 @@ +/* + * Copyright (c) 2006-2018, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2018-11-06 SummerGift first version + * 2018-11-19 flybreak add stm32f407-atk-explorer bsp + */ + +#include +#include +#include + +/* defined the LED0 pin: PF9 */ +#define LED0_PIN GET_PIN(F, 9) + +#define THREAD_PRIORITY 15 +#define THREAD_STACK_SIZE 1024 +#define THREAD_TIMESLICE 5 +int thread1_entry(void) +{ + rt_uint32_t count = 0; + + /* thread #2 occupies higher priority than that of thread #1 */ + for (count = 0; count < 10 ; count++) + { + /* thread #2 prints counting numbers */ + rt_kprintf("thread1 count: %d\n", count); + } + + rt_thread_mdelay(500); + rt_kprintf("thread2 exit\n"); + +} + +int thread2_entry(void) +{ + rt_uint32_t count = 0; + + /* thread #2 occupies higher priority than that of thread #1 */ + for (count = 0; count < 10 ; count++) + { + /* thread #2 prints counting numbers */ + rt_kprintf("thread2 count: %d\n", count); + } + rt_kprintf("thread2 exit\n"); + +} + +int thread3_entry(void) +{ + rt_uint32_t count = 0; + + /* thread #2 occupies higher priority than that of thread #1 */ + for (count = 0; count < 10 ; count++) + { + /* thread #2 prints counting numbers */ + rt_kprintf("thread3 count: %d\n", count); + } + rt_kprintf("thread2 exit\n"); + +} + +int thread_create(void) +{ + rt_thread_t tid1,tid2,tid3; + /* create thread #1 dynamically */ + tid1 = rt_thread_create("thread1", + thread1_entry, RT_NULL, + THREAD_STACK_SIZE, + THREAD_PRIORITY, THREAD_TIMESLICE); + + /* start thread #1 */ + if (tid1 != RT_NULL) + rt_thread_startup(tid1); + + /* create thread #2 statically */ + tid2 = rt_thread_create("thread2", + thread2_entry, RT_NULL, + THREAD_STACK_SIZE, + THREAD_PRIORITY + 1, THREAD_TIMESLICE); + + rt_thread_startup(tid2); /* start thread #2 */ + + tid3 = rt_thread_create("thread3", + thread3_entry, RT_NULL, + THREAD_STACK_SIZE, + THREAD_PRIORITY + 1, THREAD_TIMESLICE); + + rt_thread_startup(&tid3); /* start thread #3 */ + return 0; +} + +int main(void) +{ + int count = 1; + /* set LED0 pin mode to output */ +// rt_pin_mode(LED0_PIN, PIN_MODE_OUTPUT); + + thread_create(); + while (count++) + { +// rt_pin_write(LED0_PIN, PIN_HIGH); +// rt_thread_mdelay(500); +// rt_pin_write(LED0_PIN, PIN_LOW); +// rt_thread_mdelay(500); + rt_thread_mdelay(500); + } + + return RT_EOK; +} diff --git "a/2025/\347\254\2543\347\273\204(PSoC62 evaluation Kit)/fsp/\344\275\234\344\270\232/\347\254\2543\345\244\251\344\275\234\344\270\232/event.c" "b/2025/\347\254\2543\347\273\204(PSoC62 evaluation Kit)/fsp/\344\275\234\344\270\232/\347\254\2543\345\244\251\344\275\234\344\270\232/event.c" new file mode 100644 index 0000000000000000000000000000000000000000..26c9c7c8d334540bf6588fbf7b9815d47c5fb3cc --- /dev/null +++ "b/2025/\347\254\2543\347\273\204(PSoC62 evaluation Kit)/fsp/\344\275\234\344\270\232/\347\254\2543\345\244\251\344\275\234\344\270\232/event.c" @@ -0,0 +1,139 @@ +/* + * Copyright (c) 2006-2018, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2018-11-06 SummerGift first version + * 2018-11-19 flybreak add stm32f407-atk-explorer bsp + */ + +#include +#include +#include + +/* defined the LED0 pin: PF9 */ +#define LED0_PIN GET_PIN(F, 9) + + +#define THREAD_PRIORITY 9 +#define THREAD_TIMESLICE 5 + +#define EVENT_FLAG3 (1 << 3) +#define EVENT_FLAG5 (1 << 5) + +/* 事件控制块 */ +static struct rt_event event; + +ALIGN(RT_ALIGN_SIZE) +static char thread1_stack[1024]; +static struct rt_thread thread1; + +/* 线程 1 入口函数 */ +static void thread1_recv_event(void *param) +{ + rt_uint32_t e; + + /* 第一次接收事件,事件 3 或事件 5 任意一个可以触发线程 1,接收完后清除事件标志 */ + if (rt_event_recv(&event, (EVENT_FLAG3 | EVENT_FLAG5), + RT_EVENT_FLAG_OR | RT_EVENT_FLAG_CLEAR, + RT_WAITING_FOREVER, &e) == RT_EOK) + { + rt_kprintf("thread1: OR recv event 0x%x\n", e); + } + + rt_kprintf("thread1: delay 1s to prepare the second event\n"); + rt_thread_mdelay(1000); + + /* 第二次接收事件,事件 3 和事件 5 均发生时才可以触发线程 1,接收完后清除事件标志 */ + if (rt_event_recv(&event, (EVENT_FLAG3 | EVENT_FLAG5), + RT_EVENT_FLAG_AND | RT_EVENT_FLAG_CLEAR, + RT_WAITING_FOREVER, &e) == RT_EOK) + { + rt_kprintf("thread1: AND recv event 0x%x\n", e); + } + /* 执行完该事件集后进行事件集的脱离,事件集重复初始化会导致再次运行时,出现重复初始化的问题 */ + rt_event_detach(&event); + rt_kprintf("thread1 leave.\n"); +} + + +ALIGN(RT_ALIGN_SIZE) +static char thread2_stack[1024]; +static struct rt_thread thread2; + +/* 线程 2 入口 */ +static void thread2_send_event(void *param) +{ + rt_kprintf("thread2: send event3\n"); + rt_event_send(&event, EVENT_FLAG3); + rt_thread_mdelay(200); + + rt_kprintf("thread2: send event5\n"); + rt_event_send(&event, EVENT_FLAG5); + rt_thread_mdelay(200); + + rt_kprintf("thread2: send event3\n"); + rt_event_send(&event, EVENT_FLAG3); + rt_kprintf("thread2 leave.\n"); +} + +int event_sample(void) +{ + rt_err_t result; + + /* 初始化事件对象 */ + result = rt_event_init(&event, "event", RT_IPC_FLAG_PRIO); + if (result != RT_EOK) + { + rt_kprintf("init event failed.\n"); + return -1; + } + + rt_thread_init(&thread1, + "thread1", + thread1_recv_event, + RT_NULL, + &thread1_stack[0], + sizeof(thread1_stack), + THREAD_PRIORITY - 1, THREAD_TIMESLICE); + rt_thread_startup(&thread1); + + rt_thread_init(&thread2, + "thread2", + thread2_send_event, + RT_NULL, + &thread2_stack[0], + sizeof(thread2_stack), + THREAD_PRIORITY, THREAD_TIMESLICE); + rt_thread_startup(&thread2); + + return 0; +} + + + + + + + + +int main(void) +{ + int count = 1; + /* set LED0 pin mode to output */ +// rt_pin_mode(LED0_PIN, PIN_MODE_OUTPUT); + + event_sample(); + while (count++) + { +// rt_pin_write(LED0_PIN, PIN_HIGH); +// rt_thread_mdelay(500); +// rt_pin_write(LED0_PIN, PIN_LOW); +// rt_thread_mdelay(500); + rt_thread_mdelay(500); + } + + return RT_EOK; +} diff --git "a/2025/\347\254\2543\347\273\204(PSoC62 evaluation Kit)/fsp/\344\275\234\344\270\232/\347\254\2543\345\244\251\344\275\234\344\270\232/mailbox.c" "b/2025/\347\254\2543\347\273\204(PSoC62 evaluation Kit)/fsp/\344\275\234\344\270\232/\347\254\2543\345\244\251\344\275\234\344\270\232/mailbox.c" new file mode 100644 index 0000000000000000000000000000000000000000..407214b8e8f9c6a2f304c564affacc207ec1b450 --- /dev/null +++ "b/2025/\347\254\2543\347\273\204(PSoC62 evaluation Kit)/fsp/\344\275\234\344\270\232/\347\254\2543\345\244\251\344\275\234\344\270\232/mailbox.c" @@ -0,0 +1,148 @@ +/* + * Copyright (c) 2006-2018, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2018-11-06 SummerGift first version + * 2018-11-19 flybreak add stm32f407-atk-explorer bsp + */ + +#include +#include +#include + +/* defined the LED0 pin: PF9 */ +#define LED0_PIN GET_PIN(F, 9) + + +#include + +#define THREAD_PRIORITY 10 +#define THREAD_TIMESLICE 5 + +/* 邮箱控制块 */ +static struct rt_mailbox mb; +/* 用于放邮件的内存池 */ +static char mb_pool[128]; + +static char mb_str1[] = "I'm a mail!"; +static char mb_str2[] = "this is another mail!"; +static char mb_str3[] = "over"; + +ALIGN(RT_ALIGN_SIZE) +static char thread1_stack[1024]; +static struct rt_thread thread1; + +/* 线程 1 入口 */ +static void thread1_entry(void *parameter) +{ + char *str; + + while (1) + { + rt_kprintf("thread1: try to recv a mail\n"); + + /* 从邮箱中收取邮件 */ + if (rt_mb_recv(&mb, (rt_uint32_t *)&str, RT_WAITING_FOREVER) == RT_EOK) + { + rt_kprintf("thread1: get a mail from mailbox, the content:%s\n", str); + if (str == mb_str3) + break; + + /* 延时 100ms */ + rt_thread_mdelay(100); + } + } + /* 执行邮箱对象脱离 */ + rt_mb_detach(&mb); +} + +ALIGN(RT_ALIGN_SIZE) +static char thread2_stack[1024]; +static struct rt_thread thread2; + +/* 线程 2 入口 */ +static void thread2_entry(void *parameter) +{ + rt_uint8_t count; + + count = 0; + while (count < 10) + { + count ++; + if (count & 0x1) + { + /* 发送 mb_str1 地址到邮箱中 */ + rt_mb_send(&mb, (rt_uint32_t)&mb_str1); + } + else + { + /* 发送 mb_str2 地址到邮箱中 */ + rt_mb_send(&mb, (rt_uint32_t)&mb_str2); + } + + /* 延时 200ms */ + rt_thread_mdelay(200); + } + + /* 发送邮件告诉线程 1,线程 2 已经运行结束 */ + rt_mb_send(&mb, (rt_uint32_t)&mb_str3); +} + +int mailbox_sample(void) +{ + rt_err_t result; + + /* 初始化一个 mailbox */ + result = rt_mb_init(&mb, + "mbt", /* 名称是 mbt */ + &mb_pool[0], /* 邮箱用到的内存池是 mb_pool */ + sizeof(mb_pool) / 4, /* 邮箱中的邮件数目,因为一封邮件占 4 字节 */ + RT_IPC_FLAG_FIFO); /* 采用 FIFO 方式进行线程等待 */ + if (result != RT_EOK) + { + rt_kprintf("init mailbox failed.\n"); + return -1; + } + + rt_thread_init(&thread1, + "thread1", + thread1_entry, + RT_NULL, + &thread1_stack[0], + sizeof(thread1_stack), + THREAD_PRIORITY, THREAD_TIMESLICE); + rt_thread_startup(&thread1); + + rt_thread_init(&thread2, + "thread2", + thread2_entry, + RT_NULL, + &thread2_stack[0], + sizeof(thread2_stack), + THREAD_PRIORITY, THREAD_TIMESLICE); + rt_thread_startup(&thread2); + return 0; +} + + +int main(void) +{ + int count = 1; + /* set LED0 pin mode to output */ +// rt_pin_mode(LED0_PIN, PIN_MODE_OUTPUT); + + mailbox_sample(); + while (count++) + { +// rt_pin_write(LED0_PIN, PIN_HIGH); +// rt_thread_mdelay(500); +// rt_pin_write(LED0_PIN, PIN_LOW); +// rt_thread_mdelay(500); + rt_thread_mdelay(500); + } + + return RT_EOK; +} diff --git "a/2025/\347\254\2543\347\273\204(PSoC62 evaluation Kit)/fsp/\344\275\234\344\270\232/\347\254\2543\345\244\251\344\275\234\344\270\232/msg.c" "b/2025/\347\254\2543\347\273\204(PSoC62 evaluation Kit)/fsp/\344\275\234\344\270\232/\347\254\2543\345\244\251\344\275\234\344\270\232/msg.c" new file mode 100644 index 0000000000000000000000000000000000000000..b4c2e154454f485a23a424f5b9700b16609efa36 --- /dev/null +++ "b/2025/\347\254\2543\347\273\204(PSoC62 evaluation Kit)/fsp/\344\275\234\344\270\232/\347\254\2543\345\244\251\344\275\234\344\270\232/msg.c" @@ -0,0 +1,171 @@ +/* + * Copyright (c) 2006-2018, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2018-11-06 SummerGift first version + * 2018-11-19 flybreak add stm32f407-atk-explorer bsp + */ + +#include +#include +#include + +/* defined the LED0 pin: PF9 */ +#define LED0_PIN GET_PIN(F, 9) + + + +#define THREAD1_PRIORITY 25 +#define THREAD1_STACK_SIZE 512 +#define THREAD1_TIMESLICE 5 + +#define THREAD2_PRIORITY 25 +#define THREAD2_STACK_SIZE 512 +#define THREAD2_TIMESLICE 5 + +/* 消息队列控制块 */ +static struct rt_messagequeue mq; +/* 消息队列中用到的放置消息的内存池 */ +static rt_uint8_t msg_pool[2048]; + +static rt_thread_t tid1 = RT_NULL; +/* 线程 1 入口函数 */ +static void thread1_entry(void *parameter) +{ + char buf = 0; + rt_uint8_t cnt = 0; + + while (1) + { + /* 从消息队列中接收消息 */ +//#if defined(RT_VERSION_CHECK) && (RTTHREAD_VERSION >= RT_VERSION_CHECK(5, 0, 1)) +// if (rt_mq_recv(&mq, &buf, sizeof(buf), RT_WAITING_FOREVER) > 0) +//#else + if (rt_mq_recv(&mq, &buf, sizeof(buf), RT_WAITING_FOREVER) == RT_EOK) +//#endif + { + rt_kprintf("thread1: recv msg from msg queue, the content:%c\n", buf); + if (cnt == 19) + { + break; + } + } + /* 延时 50ms */ + cnt++; + rt_thread_mdelay(50); + } + rt_kprintf("thread1: detach mq \n"); + rt_mq_detach(&mq); +} + + +static rt_thread_t tid2 = RT_NULL; +/* 线程 2 入口 */ +static void thread2_entry(void *parameter) +{ + int result; + char buf = 'A'; + rt_uint8_t cnt = 0; + + while (1) + { + if (cnt == 8) + { + /* 发送紧急消息到消息队列中 */ + result = rt_mq_urgent(&mq, &buf, 1); + if (result != RT_EOK) + { + rt_kprintf("rt_mq_urgent ERR\n"); + } + else + { + rt_kprintf("thread2: send urgent message - %c\n", buf); + } + } + else if (cnt>= 20)/* 发送 20 次消息之后退出 */ + { + rt_kprintf("message queue stop send, thread2 quit\n"); + break; + } + else + { + /* 发送消息到消息队列中 */ + result = rt_mq_send(&mq, &buf, 1); + if (result != RT_EOK) + { + rt_kprintf("rt_mq_send ERR\n"); + } + + rt_kprintf("thread2: send message - %c\n", buf); + } + buf++; + cnt++; + /* 延时 5ms */ + rt_thread_mdelay(5); + } +} + +/* 消息队列示例的初始化 */ +int msgq_sample(void) +{ + rt_err_t result; + + /* 初始化消息队列 */ + result = rt_mq_init(&mq, + "mqt", + &msg_pool[0], /* 内存池指向 msg_pool */ + 1, /* 每个消息的大小是 1 字节 */ + sizeof(msg_pool), /* 内存池的大小是 msg_pool 的大小 */ + RT_IPC_FLAG_PRIO); /* 如果有多个线程等待,优先级大小的方法分配消息 */ + + if (result != RT_EOK) + { + rt_kprintf("init message queue failed.\n"); + return -1; + } + + /* 动态创建线程1 */ + tid1 = rt_thread_create("thread1", + thread1_entry, RT_NULL, + THREAD1_STACK_SIZE, + THREAD1_PRIORITY, THREAD1_TIMESLICE); + + if (tid1 != RT_NULL) + rt_thread_startup(tid1); + + /* 动态创建线程2 */ + tid2 = rt_thread_create("thread2", + thread2_entry, RT_NULL, + THREAD2_STACK_SIZE, + THREAD2_PRIORITY, THREAD2_TIMESLICE); + + if (tid2 != RT_NULL) + rt_thread_startup(tid2); + + return 0; +} + + + + +int main(void) +{ + int count = 1; + /* set LED0 pin mode to output */ +// rt_pin_mode(LED0_PIN, PIN_MODE_OUTPUT); + + msgq_sample(); + while (count++) + { +// rt_pin_write(LED0_PIN, PIN_HIGH); +// rt_thread_mdelay(500); +// rt_pin_write(LED0_PIN, PIN_LOW); +// rt_thread_mdelay(500); + rt_thread_mdelay(500); + } + + return RT_EOK; +} diff --git "a/2025/\347\254\2543\347\273\204(PSoC62 evaluation Kit)/fsp/\344\275\234\344\270\232/\347\254\2543\345\244\251\344\275\234\344\270\232/mutex.c" "b/2025/\347\254\2543\347\273\204(PSoC62 evaluation Kit)/fsp/\344\275\234\344\270\232/\347\254\2543\345\244\251\344\275\234\344\270\232/mutex.c" new file mode 100644 index 0000000000000000000000000000000000000000..7e6df9941325bbcc94bb63f963d359961921167f --- /dev/null +++ "b/2025/\347\254\2543\347\273\204(PSoC62 evaluation Kit)/fsp/\344\275\234\344\270\232/\347\254\2543\345\244\251\344\275\234\344\270\232/mutex.c" @@ -0,0 +1,152 @@ +/* + * Copyright (c) 2006-2018, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2018-11-06 SummerGift first version + * 2018-11-19 flybreak add stm32f407-atk-explorer bsp + */ + +#include +#include +#include + +/* defined the LED0 pin: PF9 */ +#define LED0_PIN GET_PIN(F, 9) + + +#define THREAD_PRIORITY 8 +#define THREAD_TIMESLICE 5 + +/* 指向互斥量的指针 */ +static rt_mutex_t dynamic_mutex = RT_NULL; +static rt_uint8_t number1, number2 = 0; +/* 线程退出标志 */ +static rt_bool_t thread_exit_flag = RT_FALSE; + +ALIGN(RT_ALIGN_SIZE) +static char thread1_stack[1024]; +static struct rt_thread thread1; + +static void rt_thread_entry1(void *parameter) +{ + while (1) + { + /* 线程 1 在获取互斥量前检查它是否存在 */ + if (dynamic_mutex == RT_NULL || thread_exit_flag) + { + number1 = 0; + number2 = 0; + + /* 重置退出标志 */ + thread_exit_flag = RT_FALSE; + break; /* 退出线程 */ + } + + /* 获取互斥量并进行操作 */ + if (rt_mutex_take(dynamic_mutex, RT_WAITING_FOREVER) == RT_EOK) + { + number1++; + number2++; + rt_kprintf("thread1 mutex protect, number1 = number2 is %d\n", number1); + rt_mutex_release(dynamic_mutex); + rt_thread_mdelay(10); + } + } +} + +ALIGN(RT_ALIGN_SIZE) +static char thread2_stack[1024]; +static struct rt_thread thread2; +static void rt_thread_entry2(void *parameter) +{ + while (1) + { + /* 获取互斥量 */ + if (rt_mutex_take(dynamic_mutex, RT_WAITING_FOREVER) == RT_EOK) + { + if (number1 != number2) + { + rt_kprintf("not protect. number1 = %d, number2 = %d\n", number1, number2); + } + else + { + rt_kprintf("mutex protect, number1 = number2 is %d\n", number1); + } + + number1++; + number2++; + rt_mutex_release(dynamic_mutex); + + /* 判断是否达到退出条件 */ + if (number1 >= 50) + { + thread_exit_flag = RT_TRUE; + + /* 删除互斥量 */ + rt_mutex_delete(dynamic_mutex); + dynamic_mutex = RT_NULL; + + break; /* 退出线程 */ + } + } + } +} + +/* 互斥量示例的初始化 */ +int mutex_sample(void) +{ + /* 创建一个动态互斥量 */ + dynamic_mutex = rt_mutex_create("dmutex", RT_IPC_FLAG_PRIO); + if (dynamic_mutex == RT_NULL) + { + rt_kprintf("create dynamic mutex failed.\n"); + return -1; + } + + rt_thread_init(&thread1, + "thread1", + rt_thread_entry1, + RT_NULL, + &thread1_stack[0], + sizeof(thread1_stack), + THREAD_PRIORITY, THREAD_TIMESLICE); + rt_thread_startup(&thread1); + + rt_thread_init(&thread2, + "thread2", + rt_thread_entry2, + RT_NULL, + &thread2_stack[0], + sizeof(thread2_stack), + THREAD_PRIORITY - 1, THREAD_TIMESLICE); + rt_thread_startup(&thread2); + + return 0; +} + + + + + + +int main(void) +{ + int count = 1; + /* set LED0 pin mode to output */ +// rt_pin_mode(LED0_PIN, PIN_MODE_OUTPUT); + + mutex_sample(); + while (count++) + { +// rt_pin_write(LED0_PIN, PIN_HIGH); +// rt_thread_mdelay(500); +// rt_pin_write(LED0_PIN, PIN_LOW); +// rt_thread_mdelay(500); + rt_thread_mdelay(500); + } + + return RT_EOK; +} diff --git "a/2025/\347\254\2543\347\273\204(PSoC62 evaluation Kit)/fsp/\344\275\234\344\270\232/\347\254\2543\345\244\251\344\275\234\344\270\232/semaphore.c" "b/2025/\347\254\2543\347\273\204(PSoC62 evaluation Kit)/fsp/\344\275\234\344\270\232/\347\254\2543\345\244\251\344\275\234\344\270\232/semaphore.c" new file mode 100644 index 0000000000000000000000000000000000000000..4b993d3126b8a26df58427582808609faa683f81 --- /dev/null +++ "b/2025/\347\254\2543\347\273\204(PSoC62 evaluation Kit)/fsp/\344\275\234\344\270\232/\347\254\2543\345\244\251\344\275\234\344\270\232/semaphore.c" @@ -0,0 +1,142 @@ +/* + * Copyright (c) 2006-2018, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2018-11-06 SummerGift first version + * 2018-11-19 flybreak add stm32f407-atk-explorer bsp + */ + +#include +#include +#include + +/* defined the LED0 pin: PF9 */ +#define LED0_PIN GET_PIN(F, 9) + +#include + +#define THREAD_PRIORITY 15 +#define THREAD_TIMESLICE 5 + +/* 信号量退出标志 */ +static rt_bool_t sem_flag = 0; +/* 指向信号量的指针 */ +static rt_sem_t dynamic_sem = RT_NULL; + +ALIGN(RT_ALIGN_SIZE) +static char thread1_stack[1024]; +static struct rt_thread thread1; +static void rt_thread1_entry(void *parameter) +{ + static rt_uint8_t count = 0; + + while (1) + { + if (count <= 100) + { + count++; + } + else + { + rt_kprintf("thread1 exiting...\n"); + sem_flag = 1; + rt_sem_release(dynamic_sem); + count = 0; + return; + } + + /* count 每计数 10 次,就释放一次信号量 */ + if (0 == (count % 10)) + { + rt_kprintf("t1 release a dynamic semaphore.\n"); + rt_sem_release(dynamic_sem); + } + } +} + +ALIGN(RT_ALIGN_SIZE) +static char thread2_stack[1024]; +static struct rt_thread thread2; +static void rt_thread2_entry(void *parameter) +{ + static rt_err_t result; + static rt_uint8_t number = 0; + while (1) + { + /* 永久方式等待信号量,获取到信号量,则执行 number 自加的操作 */ + result = rt_sem_take(dynamic_sem, RT_WAITING_FOREVER); + if (sem_flag && result == RT_EOK) + { + rt_kprintf("thread2 exiting...\n"); + rt_sem_delete(dynamic_sem); + sem_flag = 0; + number = 0; + return; + } + else + { + number++; + rt_kprintf("t2 take a dynamic semaphore. number = %d\n", number); + } + } +} + +/* 信号量示例的初始化 */ +int semaphore_sample(void) +{ + /* 创建一个动态信号量,初始值是 0 */ + dynamic_sem = rt_sem_create("dsem", 0, RT_IPC_FLAG_PRIO); + if (dynamic_sem == RT_NULL) + { + rt_kprintf("create dynamic semaphore failed.\n"); + return -1; + } + else + { + rt_kprintf("create done. dynamic semaphore value = 0.\n"); + } + + rt_thread_init(&thread1, + "thread1", + rt_thread1_entry, + RT_NULL, + &thread1_stack[0], + sizeof(thread1_stack), + THREAD_PRIORITY, THREAD_TIMESLICE); + rt_thread_startup(&thread1); + + rt_thread_init(&thread2, + "thread2", + rt_thread2_entry, + RT_NULL, + &thread2_stack[0], + sizeof(thread2_stack), + THREAD_PRIORITY - 1, THREAD_TIMESLICE); + rt_thread_startup(&thread2); + + return 0; +} + + + +int main(void) +{ + int count = 1; + /* set LED0 pin mode to output */ +// rt_pin_mode(LED0_PIN, PIN_MODE_OUTPUT); + + semaphore_sample(); + while (count++) + { +// rt_pin_write(LED0_PIN, PIN_HIGH); +// rt_thread_mdelay(500); +// rt_pin_write(LED0_PIN, PIN_LOW); +// rt_thread_mdelay(500); + rt_thread_mdelay(500); + } + + return RT_EOK; +} diff --git "a/2025/\347\254\2543\347\273\204(PSoC62 evaluation Kit)/fsp/\344\275\234\344\270\232/\347\254\2543\345\244\251\344\275\234\344\270\232/signal.c" "b/2025/\347\254\2543\347\273\204(PSoC62 evaluation Kit)/fsp/\344\275\234\344\270\232/\347\254\2543\345\244\251\344\275\234\344\270\232/signal.c" new file mode 100644 index 0000000000000000000000000000000000000000..dfbf3e9fed96461b05ad767c240935471951b844 --- /dev/null +++ "b/2025/\347\254\2543\347\273\204(PSoC62 evaluation Kit)/fsp/\344\275\234\344\270\232/\347\254\2543\345\244\251\344\275\234\344\270\232/signal.c" @@ -0,0 +1,96 @@ +/* + * Copyright (c) 2006-2018, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2018-11-06 SummerGift first version + * 2018-11-19 flybreak add stm32f407-atk-explorer bsp + */ + +#include +#include +#include + +/* defined the LED0 pin: PF9 */ +#define LED0_PIN GET_PIN(F, 9) + +#include + + +#define THREAD_PRIORITY 25 +#define THREAD_STACK_SIZE 512 +#define THREAD_TIMESLICE 5 + +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_sample(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; +} + + + + + + +int main(void) +{ + int count = 1; + /* set LED0 pin mode to output */ +// rt_pin_mode(LED0_PIN, PIN_MODE_OUTPUT); + + signal_sample(); + while (count++) + { +// rt_pin_write(LED0_PIN, PIN_HIGH); +// rt_thread_mdelay(500); +// rt_pin_write(LED0_PIN, PIN_LOW); +// rt_thread_mdelay(500); + rt_thread_mdelay(500); + } + + return RT_EOK; +} diff --git "a/2025/\347\254\2543\347\273\204(PSoC62 evaluation Kit)/fsp/\344\275\234\344\270\232/\347\254\2544\345\244\251\344\275\234\344\270\232/main.c" "b/2025/\347\254\2543\347\273\204(PSoC62 evaluation Kit)/fsp/\344\275\234\344\270\232/\347\254\2544\345\244\251\344\275\234\344\270\232/main.c" new file mode 100644 index 0000000000000000000000000000000000000000..eed69a3156c4af2e94b277884060ad374623cab5 --- /dev/null +++ "b/2025/\347\254\2543\347\273\204(PSoC62 evaluation Kit)/fsp/\344\275\234\344\270\232/\347\254\2544\345\244\251\344\275\234\344\270\232/main.c" @@ -0,0 +1,110 @@ +/* + * Copyright (c) 2006-2018, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2018-11-06 SummerGift first version + * 2018-11-19 flybreak add stm32f407-atk-explorer bsp + */ + +#include +#include +#include + +/* defined the LED0 pin: PF9 */ +#define LED0_PIN GET_PIN(F, 9) + +const static struct rt_device_ops vir_ops = +{ + rt_vir_init, + rt_vir_open, + rt_vir_close, + RT_NULL, + RT_NULL, + rt_vir_control, +}; + +rt_err_t rt_hw_virdevice_register(struct rt_watchdog_device *vir, + const char *name, + rt_uint32_t flag, + void *data) +{ + struct rt_device *device; + RT_ASSERT(wtd != RT_NULL); + + device = &(vir->parent); + + device->type = RT_Device_Class_Miscellaneous; + device->rx_indicate = RT_NULL; + device->tx_complete = RT_NULL; + + device->ops = &vir_ops; + device->user_data = data; + + /* register a character device */ + return rt_device_register(device, name, flag); +} + +#include +#include + +#define IVIR_DEVICE_NAME "vir" + +static rt_device_t vir_dev; + + +int main(void) +{ + rt_err_t res = RT_EOK; + rt_uint32_t timeout = 10; /* 溢出时间 */ + + /* 根据设备名称查找看门狗设备,获取设备句柄 */ + vir_dev = rt_device_find(IVIR_DEVICE_NAME); + if (!vir_dev) + { + rt_kprintf("find %s failed!\n", IVIR_DEVICE_NAME); + return RT_ERROR; + } + /* 初始化设备 */ + res = rt_device_init(vir_dev); + if (res != RT_EOK) + { + rt_kprintf("initialize %s failed!\n", IVIR_DEVICE_NAME); + return res; + } + /* 设置看门狗溢出时间 */ + res = rt_device_control(vir_dev, RT_DEVICE_CTRL_WDT_SET_TIMEOUT, &timeout); + if (res != RT_EOK) + { + rt_kprintf("set %s timeout failed!\n", Ivir_DEVICE_NAME); + return res; + } + + return res; +} + + + + + + +//int main(void) +//{ +// int count = 1; +// /* set LED0 pin mode to output */ +//// rt_pin_mode(LED0_PIN, PIN_MODE_OUTPUT); +// +// signal_sample(); +// while (count++) +// { +//// rt_pin_write(LED0_PIN, PIN_HIGH); +//// rt_thread_mdelay(500); +//// rt_pin_write(LED0_PIN, PIN_LOW); +//// rt_thread_mdelay(500); +// rt_thread_mdelay(500); +// } +// +// return RT_EOK; +//} diff --git "a/2025/\347\254\2543\347\273\204(PSoC62 evaluation Kit)/fsp/\344\275\234\344\270\232/\347\254\2545\345\244\251\344\275\234\344\270\232/test.c" "b/2025/\347\254\2543\347\273\204(PSoC62 evaluation Kit)/fsp/\344\275\234\344\270\232/\347\254\2545\345\244\251\344\275\234\344\270\232/test.c" new file mode 100644 index 0000000000000000000000000000000000000000..c600971284295d4ab0239e52e099f7e37f75666c --- /dev/null +++ "b/2025/\347\254\2543\347\273\204(PSoC62 evaluation Kit)/fsp/\344\275\234\344\270\232/\347\254\2545\345\244\251\344\275\234\344\270\232/test.c" @@ -0,0 +1,99 @@ + +/* + * @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 "broker.emqx.io" +#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 "rt-thread" +#endif +#ifndef KAWAII_MQTT_PASSWORD +#define KAWAII_MQTT_PASSWORD "rt-thread" +#endif +#ifndef KAWAII_MQTT_SUBTOPIC +#define KAWAII_MQTT_SUBTOPIC "rtt-sub" +#endif +#ifndef KAWAII_MQTT_PUBTOPIC +#define KAWAII_MQTT_PUBTOPIC "rtt-pub" +#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 *)"{\"name\":\"fsp\",\"study\":\"Keep up the good work!\"}"; + + 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);