day3按键灭灯
This commit is contained in:
parent
5f50c06c8f
commit
da6779f796
34
.config
34
.config
|
@ -262,12 +262,6 @@ CONFIG_RT_LIBC_TZ_DEFAULT_SEC=0
|
|||
# CONFIG_RT_USING_FDT is not set
|
||||
# end of RT-Thread Components
|
||||
|
||||
#
|
||||
# RT-Thread Utestcases
|
||||
#
|
||||
# CONFIG_RT_USING_UTESTCASES is not set
|
||||
# end of RT-Thread Utestcases
|
||||
|
||||
#
|
||||
# RT-Thread online packages
|
||||
#
|
||||
|
@ -420,6 +414,7 @@ CONFIG_RT_LIBC_TZ_DEFAULT_SEC=0
|
|||
# CONFIG_PKG_USING_JSMN is not set
|
||||
# CONFIG_PKG_USING_AGILE_JSMN is not set
|
||||
# CONFIG_PKG_USING_PARSON is not set
|
||||
# CONFIG_PKG_USING_RYAN_JSON is not set
|
||||
# end of JSON: JavaScript Object Notation, a lightweight data-interchange format
|
||||
|
||||
#
|
||||
|
@ -827,6 +822,7 @@ CONFIG_RT_LIBC_TZ_DEFAULT_SEC=0
|
|||
# CONFIG_PKG_USING_SYSTEM_RUN_LED is not set
|
||||
# CONFIG_PKG_USING_BT_MX01 is not set
|
||||
# CONFIG_PKG_USING_RGPOWER is not set
|
||||
# CONFIG_PKG_USING_BT_MX02 is not set
|
||||
# CONFIG_PKG_USING_SPI_TOOLS is not set
|
||||
# end of peripheral libraries and drivers
|
||||
|
||||
|
@ -868,7 +864,31 @@ CONFIG_RT_LIBC_TZ_DEFAULT_SEC=0
|
|||
#
|
||||
# samples: kernel and components samples
|
||||
#
|
||||
# CONFIG_PKG_USING_KERNEL_SAMPLES is not set
|
||||
CONFIG_PKG_USING_KERNEL_SAMPLES=y
|
||||
CONFIG_PKG_KERNEL_SAMPLES_PATH="/packages/misc/samples/kernel_samples"
|
||||
# CONFIG_PKG_USING_KERNEL_SAMPLES_V030 is not set
|
||||
# CONFIG_PKG_USING_KERNEL_SAMPLES_V040 is not set
|
||||
CONFIG_PKG_USING_KERNEL_SAMPLES_LATEST_VERSION=y
|
||||
CONFIG_PKG_KERNEL_SAMPLES_VER="latest"
|
||||
CONFIG_PKG_USING_KERNEL_SAMPLES_EN=y
|
||||
# CONFIG_PKG_USING_KERNEL_SAMPLES_ZH is not set
|
||||
CONFIG_KERNEL_SAMPLES_USING_THREAD=y
|
||||
CONFIG_KERNEL_SAMPLES_USING_SEMAPHORE=y
|
||||
CONFIG_KERNEL_SAMPLES_USING_MUTEX=y
|
||||
CONFIG_KERNEL_SAMPLES_USING_MAILBOX=y
|
||||
CONFIG_KERNEL_SAMPLES_USING_EVENT=y
|
||||
CONFIG_KERNEL_SAMPLES_USING_MESSAGEQUEUE=y
|
||||
# CONFIG_KERNEL_SAMPLES_USING_TIMER is not set
|
||||
# CONFIG_KERNEL_SAMPLES_USING_HEAP is not set
|
||||
# CONFIG_KERNEL_SAMPLES_USING_MEMHEAP is not set
|
||||
# CONFIG_KERNEL_SAMPLES_USING_MEMPOOL is not set
|
||||
# CONFIG_KERNEL_SAMPLES_USING_IDLEHOOK is not set
|
||||
# CONFIG_KERNEL_SAMPLES_USING_SIGNAL is not set
|
||||
# CONFIG_KERNEL_SAMPLES_USING_INTERRUPT is not set
|
||||
# CONFIG_KERNEL_SAMPLES_USING_PRI_INVERSION is not set
|
||||
# CONFIG_KERNEL_SAMPLES_USING_TIME_SLICE is not set
|
||||
# CONFIG_KERNEL_SAMPLES_USING_SCHEDULER_HOOK is not set
|
||||
# CONFIG_KERNEL_SAMPLES_USING_PRODUCER_CONSUMER is not set
|
||||
# CONFIG_PKG_USING_FILESYSTEM_SAMPLES is not set
|
||||
# CONFIG_PKG_USING_NETWORK_SAMPLES is not set
|
||||
# CONFIG_PKG_USING_PERIPHERAL_SAMPLES is not set
|
||||
|
|
|
@ -1,117 +0,0 @@
|
|||
/*
|
||||
* 程序清单:邮箱例程
|
||||
*
|
||||
* 这个程序会创建2个动态线程,一个静态的邮箱对象,其中一个线程往邮箱中发送邮件,
|
||||
* 一个线程往邮箱中收取邮件。
|
||||
*/
|
||||
#include <rtthread.h>
|
||||
|
||||
#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";
|
||||
|
||||
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_ubase_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);
|
||||
}
|
||||
|
||||
static char thread2_stack[1024];
|
||||
static struct rt_thread thread2;
|
||||
|
||||
/* 线程2入口 */
|
||||
static void thread2_entry(void *parameter)
|
||||
{
|
||||
rt_uint8_t count;
|
||||
|
||||
count = 0; // 初始化计数器为0
|
||||
while (count < 10) // 循环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) / sizeof(rt_ubase_t), /* 邮箱中的邮件数目,sizeof(rt_ubase_t)表示指针大小 */
|
||||
RT_IPC_FLAG_PRIO); /* 采用PRIO方式进行线程等待 */
|
||||
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;
|
||||
}
|
||||
|
||||
/* 导出到 msh 命令列表中 */
|
||||
MSH_CMD_EXPORT(mailbox_sample, mailbox sample);
|
|
@ -1,140 +0,0 @@
|
|||
/*
|
||||
* 程序清单:消息队列例程
|
||||
*
|
||||
* 这个程序会创建2个动态线程,一个线程会从消息队列中收取消息;一个线程会定时给消
|
||||
* 息队列发送 普通消息和紧急消息。
|
||||
*/
|
||||
#include <rtthread.h>
|
||||
|
||||
#define THREAD_PRIORITY 25
|
||||
#define THREAD_TIMESLICE 5
|
||||
|
||||
/* 消息队列控制块 */
|
||||
static struct rt_messagequeue mq;
|
||||
/* 消息队列中用到的放置消息的内存池 */
|
||||
static rt_uint8_t msg_pool[2048];
|
||||
|
||||
static char thread1_stack[1024];
|
||||
static struct rt_thread thread1;
|
||||
|
||||
/* 线程1入口函数 */
|
||||
static void thread1_entry(void *parameter)
|
||||
{
|
||||
char buf = 0; // 用于接收消息的缓冲区
|
||||
rt_uint8_t cnt = 0; // 消息计数器
|
||||
|
||||
while (1)
|
||||
{
|
||||
/* 从消息队列中接收消息 */
|
||||
if (rt_mq_recv(&mq, &buf, sizeof(buf), RT_WAITING_FOREVER) > 0)
|
||||
{
|
||||
rt_kprintf("thread1: recv msg from msg queue, the content: %c\n", buf);
|
||||
if (cnt == 19)
|
||||
{
|
||||
break; // 接收20次消息后退出
|
||||
}
|
||||
}
|
||||
/* 延时50ms */
|
||||
cnt++;
|
||||
rt_thread_mdelay(50);
|
||||
}
|
||||
rt_kprintf("thread1: detach mq \n");
|
||||
rt_mq_detach(&mq); // 脱离消息队列
|
||||
}
|
||||
|
||||
static char thread2_stack[1024];
|
||||
static struct rt_thread thread2;
|
||||
|
||||
/* 线程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", /* 名称是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 */
|
||||
rt_thread_init(&thread1,
|
||||
"thread1", /* 线程名称 */
|
||||
thread1_entry, /* 线程入口函数 */
|
||||
RT_NULL, /* 入口函数参数 */
|
||||
&thread1_stack[0], /* 线程栈起始地址 */
|
||||
sizeof(thread1_stack), /* 线程栈大小 */
|
||||
THREAD_PRIORITY, THREAD_TIMESLICE); /* 线程优先级和时间片大小 */
|
||||
|
||||
/* 启动线程1 */
|
||||
rt_thread_startup(&thread1);
|
||||
|
||||
/* 初始化线程2 */
|
||||
rt_thread_init(&thread2,
|
||||
"thread2", /* 线程名称 */
|
||||
thread2_entry, /* 线程入口函数 */
|
||||
RT_NULL, /* 入口函数参数 */
|
||||
&thread2_stack[0], /* 线程栈起始地址 */
|
||||
sizeof(thread2_stack), /* 线程栈大小 */
|
||||
THREAD_PRIORITY-1, THREAD_TIMESLICE); /* 线程优先级和时间片大小 */
|
||||
|
||||
/* 启动线程2 */
|
||||
rt_thread_startup(&thread2);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* 导出到 msh 命令列表中 */
|
||||
MSH_CMD_EXPORT(msgq_sample, msgq sample);
|
|
@ -1,94 +0,0 @@
|
|||
/*
|
||||
* 程序清单:互斥锁例程
|
||||
*
|
||||
* 互斥锁是一种保护共享资源的方法。当一个线程拥有互斥锁的时候,
|
||||
* 可以保护共享资源不被其他线程破坏。线程1对2个number分别进行加1操作
|
||||
* 线程2也会对2个number分别进行加1操作。使用互斥量保证2个number值保持一致
|
||||
*/
|
||||
#include <rtthread.h>
|
||||
|
||||
#define THREAD_PRIORITY 8
|
||||
#define THREAD_TIMESLICE 5
|
||||
|
||||
/* 指向互斥量的指针 */
|
||||
static rt_mutex_t dynamic_mutex = RT_NULL;
|
||||
static rt_uint8_t number1, number2 = 0;
|
||||
|
||||
|
||||
static char thread1_stack[1024];
|
||||
static struct rt_thread thread1;
|
||||
static void rt_thread_entry1(void *parameter)
|
||||
{
|
||||
while (1)
|
||||
{
|
||||
/* 线程1获取到互斥量后,先后对number1、number2进行加1操作,然后释放互斥量 */
|
||||
rt_mutex_take(dynamic_mutex, RT_WAITING_FOREVER);
|
||||
number1++;
|
||||
rt_thread_mdelay(10);
|
||||
number2++;
|
||||
rt_mutex_release(dynamic_mutex);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static char thread2_stack[1024];
|
||||
static struct rt_thread thread2;
|
||||
static void rt_thread_entry2(void *parameter)
|
||||
{
|
||||
while (1)
|
||||
{
|
||||
/* 线程2获取到互斥量后,检查number1、number2的值是否相同,相同则表示mutex起到了锁的作用 */
|
||||
rt_mutex_take(dynamic_mutex, RT_WAITING_FOREVER);
|
||||
if (number1 != number2)
|
||||
{
|
||||
rt_kprintf("not protect.number1 = %d, mumber2 = %d \n", number1, number2);
|
||||
}
|
||||
else
|
||||
{
|
||||
rt_kprintf("mutex protect ,number1 = mumber2 is %d\n", number1);
|
||||
}
|
||||
|
||||
number1++;
|
||||
number2++;
|
||||
rt_mutex_release(dynamic_mutex);
|
||||
|
||||
if (number1 >= 50)
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
/* 互斥量示例的初始化 */
|
||||
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;
|
||||
}
|
||||
|
||||
/* 导出到 msh 命令列表中 */
|
||||
MSH_CMD_EXPORT(mutex_sample, mutex sample);
|
|
@ -1,102 +0,0 @@
|
|||
#include <rtthread.h>
|
||||
|
||||
#define THREAD_PRIORITY 25
|
||||
#define THREAD_TIMESLICE 5
|
||||
|
||||
/* 指向信号量的指针 */
|
||||
static rt_sem_t dynamic_sem = RT_NULL;
|
||||
|
||||
static char thread1_stack[1024];
|
||||
static struct rt_thread thread1;
|
||||
|
||||
/* 线程1入口函数 */
|
||||
static void rt_thread1_entry(void *parameter)
|
||||
{
|
||||
static rt_uint8_t count = 0;
|
||||
|
||||
while(1)
|
||||
{
|
||||
if(count <= 100)
|
||||
{
|
||||
count++; // 计数器递增
|
||||
}
|
||||
else
|
||||
{
|
||||
return; // 计数器大于100时退出线程
|
||||
}
|
||||
|
||||
/* count 每计数 10 次,就释放一次信号量 */
|
||||
if(0 == (count % 10))
|
||||
{
|
||||
rt_kprintf("t1 release a dynamic semaphore.\n");
|
||||
rt_sem_release(dynamic_sem); // 释放信号量
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static char thread2_stack[1024];
|
||||
static struct rt_thread thread2;
|
||||
|
||||
/* 线程2入口函数 */
|
||||
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 (result != RT_EOK)
|
||||
{
|
||||
rt_kprintf("t2 take a dynamic semaphore, failed.\n");
|
||||
rt_sem_delete(dynamic_sem); // 删除信号量
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
number++; // 信号量获取成功,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");
|
||||
}
|
||||
|
||||
/* 初始化并启动线程1 */
|
||||
rt_thread_init(&thread1,
|
||||
"thread1", /* 线程名称 */
|
||||
rt_thread1_entry, /* 线程入口函数 */
|
||||
RT_NULL, /* 入口函数参数 */
|
||||
&thread1_stack[0], /* 线程栈起始地址 */
|
||||
sizeof(thread1_stack), /* 线程栈大小 */
|
||||
THREAD_PRIORITY, THREAD_TIMESLICE); /* 线程优先级和时间片大小 */
|
||||
rt_thread_startup(&thread1);
|
||||
|
||||
/* 初始化并启动线程2 */
|
||||
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;
|
||||
}
|
||||
/* 导出到 msh 命令列表中 */
|
||||
MSH_CMD_EXPORT(semaphore_sample, semaphore sample);
|
|
@ -1,75 +0,0 @@
|
|||
/*
|
||||
* 程序清单:创建、初始化/脱离线程
|
||||
*
|
||||
* 这个例子会创建两个线程,一个动态线程,一个静态线程。
|
||||
* 静态线程在运行完毕后自动被系统脱离,动态线程一直打印计数。
|
||||
*/
|
||||
#include <rtthread.h>
|
||||
|
||||
#define THREAD_PRIORITY 25
|
||||
#define THREAD_STACK_SIZE 512
|
||||
#define THREAD_TIMESLICE 5
|
||||
|
||||
static rt_thread_t tid1 = RT_NULL;
|
||||
|
||||
/* 线程1的入口函数 */
|
||||
static void thread1_entry(void *parameter)
|
||||
{
|
||||
rt_uint32_t count = 0;
|
||||
|
||||
while (1)
|
||||
{
|
||||
/* 线程1采用低优先级运行,一直打印计数值 */
|
||||
rt_kprintf("thread1 count: %d\n", count++);
|
||||
rt_thread_mdelay(500); // 延时500毫秒
|
||||
}
|
||||
}
|
||||
|
||||
static char thread2_stack[1024];
|
||||
static struct rt_thread thread2;
|
||||
|
||||
/* 线程2入口 */
|
||||
static void thread2_entry(void *param)
|
||||
{
|
||||
rt_uint32_t count = 0;
|
||||
|
||||
/* 线程2拥有较高的优先级,以抢占线程1而获得执行 */
|
||||
for (count = 0; count < 10 ; count++)
|
||||
{
|
||||
/* 线程2打印计数值 */
|
||||
rt_kprintf("thread2 count: %d\n", count);
|
||||
}
|
||||
rt_kprintf("thread2 exit\n");
|
||||
|
||||
/* 线程2运行结束后也将自动被系统脱离 */
|
||||
}
|
||||
|
||||
/* 线程示例 */
|
||||
int thread_sample(void)
|
||||
{
|
||||
/* 创建线程1,名称是thread1,入口是thread1_entry */
|
||||
tid1 = rt_thread_create("thread1",
|
||||
thread1_entry, RT_NULL,
|
||||
THREAD_STACK_SIZE,
|
||||
THREAD_PRIORITY, THREAD_TIMESLICE);
|
||||
/* 如果获得线程控制块,启动这个线程 */
|
||||
if (tid1 != RT_NULL)
|
||||
rt_thread_startup(tid1);
|
||||
|
||||
/* 初始化线程2,名称是thread2,入口是thread2_entry */
|
||||
rt_thread_init(&thread2,
|
||||
"thread2",
|
||||
thread2_entry,
|
||||
RT_NULL,
|
||||
&thread2_stack[0],
|
||||
sizeof(thread2_stack),
|
||||
THREAD_PRIORITY - 1, THREAD_TIMESLICE);
|
||||
|
||||
/* 启动线程2 */
|
||||
rt_thread_startup(&thread2);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* 导出到 msh 命令列表中 */
|
||||
MSH_CMD_EXPORT(thread_sample, thread sample);
|
|
@ -33,7 +33,7 @@ static void thread2_entry(void *parameter)
|
|||
rt_kprintf("thread2 is exiting\n");
|
||||
}
|
||||
|
||||
int thread(void)
|
||||
int tgame(void)
|
||||
{
|
||||
tid1=rt_thread_create("thread1",
|
||||
thread1_entry,
|
||||
|
|
|
@ -169,5 +169,19 @@ rt_err_t rt_mb_init(rt_mailbox_t mb, const char* name, rt_uint32_t size, rt_uint
|
|||
![alt text](image-12.png)
|
||||
1消息 多线程?
|
||||
FIFO 对于谁
|
||||
launch
|
||||
停止?
|
||||
。。。
|
||||
[一个好用的串口工具类似mobaxterm](https://wterm.wkjay.com/)
|
||||
[一个好用的串口工具类似mobaxterm](https://wterm.wkjay.com/)
|
||||
|
||||
dist 的好处
|
||||
加入sample
|
||||
配置完任何软件包都要在env中
|
||||
``` c
|
||||
pkgs --update
|
||||
```
|
||||
|
||||
## 示例点灯
|
||||
### 灭了怎么点都不亮
|
||||
在key线程循环中每次点亮?
|
||||
因为按键按灭就没再点亮
|
|
@ -18,19 +18,111 @@
|
|||
|
||||
#define GPIO_LED_B GET_PIN(F, 11)
|
||||
#define GPIO_LED_R GET_PIN(F, 12)
|
||||
|
||||
#define THREAD_PRIORITY 25
|
||||
#define THREAD_STACK_SIZE 1024
|
||||
#define THREAD_TIMESLICE 5
|
||||
|
||||
#define PIN_KEY0 GET_PIN(C,0)
|
||||
|
||||
static rt_thread_t tid1 = RT_NULL;
|
||||
static rt_thread_t tid2 = RT_NULL;
|
||||
|
||||
static void key_name_entry(void *parameter);
|
||||
static void led_name_entry(void *parameter);
|
||||
static rt_sem_t dynamic_sem = RT_NULL;
|
||||
|
||||
int main(void)
|
||||
{
|
||||
rt_pin_mode(GPIO_LED_R, PIN_MODE_OUTPUT);
|
||||
rt_pin_mode(PIN_KEY0, PIN_MODE_INPUT_PULLUP);
|
||||
|
||||
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");
|
||||
}
|
||||
|
||||
tid1 = rt_thread_create("key_thread",
|
||||
key_name_entry, RT_NULL,
|
||||
THREAD_STACK_SIZE,
|
||||
THREAD_PRIORITY, THREAD_TIMESLICE);
|
||||
if (tid1 != RT_NULL)
|
||||
{
|
||||
rt_thread_startup(tid1);
|
||||
}
|
||||
|
||||
tid2 = rt_thread_create("led_thread",
|
||||
led_name_entry, RT_NULL,
|
||||
THREAD_STACK_SIZE,
|
||||
THREAD_PRIORITY, THREAD_TIMESLICE);
|
||||
if (tid2 != RT_NULL)
|
||||
{
|
||||
rt_thread_startup(tid2);
|
||||
}
|
||||
|
||||
// while (1)
|
||||
// {
|
||||
// rt_pin_write(GPIO_LED_R, PIN_HIGH);
|
||||
// rt_thread_mdelay(500);
|
||||
// rt_pin_write(GPIO_LED_R, PIN_LOW);
|
||||
// rt_thread_mdelay(500);
|
||||
// }
|
||||
}
|
||||
static void key_name_entry(void *parameter)
|
||||
{
|
||||
rt_uint32_t count=0;
|
||||
while (1)
|
||||
{
|
||||
rt_pin_write(GPIO_LED_R, PIN_HIGH);
|
||||
rt_thread_mdelay(500);
|
||||
rt_pin_write(GPIO_LED_R, PIN_LOW);
|
||||
rt_thread_mdelay(500);
|
||||
|
||||
if(rt_pin_read(PIN_KEY0) == PIN_LOW)
|
||||
{
|
||||
rt_thread_mdelay(100);
|
||||
if(rt_pin_read(PIN_KEY0) == PIN_LOW)
|
||||
{
|
||||
rt_kprintf("key0 is pressed (%d)\r", count++);
|
||||
rt_sem_release(dynamic_sem);
|
||||
}
|
||||
else
|
||||
{
|
||||
rt_pin_write(GPIO_LED_R, PIN_LOW);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
rt_pin_write(GPIO_LED_R, PIN_LOW);
|
||||
}
|
||||
rt_thread_mdelay(10);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
static void led_name_entry(void *parameter)
|
||||
{
|
||||
rt_uint32_t count=0;
|
||||
rt_uint32_t result=0;
|
||||
while (1)
|
||||
{
|
||||
result = rt_sem_take(dynamic_sem, RT_WAITING_FOREVER);
|
||||
if (result == RT_EOK)
|
||||
{
|
||||
rt_kprintf("LED HIGH\n");
|
||||
rt_pin_write(GPIO_LED_R, PIN_HIGH);
|
||||
}
|
||||
else
|
||||
{
|
||||
rt_kprintf("LED LOW");
|
||||
rt_pin_write(GPIO_LED_R, PIN_LOW);
|
||||
}
|
||||
|
||||
rt_thread_mdelay(10);
|
||||
}
|
||||
}
|
||||
// #include <rtthread.h>
|
||||
// #include "hello.h"
|
||||
|
||||
|
@ -45,78 +137,3 @@ int main(void)
|
|||
// return 0;
|
||||
// }
|
||||
|
||||
/*
|
||||
* 程序清单:创建、初始化/脱离线程
|
||||
*
|
||||
* 这个例子会创建两个线程,一个动态线程,一个静态线程。
|
||||
* 静态线程在运行完毕后自动被系统脱离,动态线程一直打印计数。
|
||||
*/
|
||||
// #include <rtthread.h>
|
||||
|
||||
// #define THREAD_PRIORITY 25
|
||||
// #define THREAD_STACK_SIZE 512
|
||||
// #define THREAD_TIMESLICE 5
|
||||
|
||||
// static rt_thread_t tid1 = RT_NULL;
|
||||
|
||||
// /* 线程1的入口函数 */
|
||||
// static void thread1_entry(void *parameter)
|
||||
// {
|
||||
// rt_uint32_t count = 0;
|
||||
|
||||
// while (1)
|
||||
// {
|
||||
// /* 线程1采用低优先级运行,一直打印计数值 */
|
||||
// rt_kprintf("thread1 count: %d\n", count++);
|
||||
// rt_thread_mdelay(500); // 延时500毫秒
|
||||
// }
|
||||
// }
|
||||
|
||||
// static char thread2_stack[1024];
|
||||
// static struct rt_thread thread2;
|
||||
|
||||
// /* 线程2入口 */
|
||||
// static void thread2_entry(void *param)
|
||||
// {
|
||||
// rt_uint32_t count = 0;
|
||||
|
||||
// /* 线程2拥有较高的优先级,以抢占线程1而获得执行 */
|
||||
// for (count = 0; count < 10 ; count++)
|
||||
// {
|
||||
// /* 线程2打印计数值 */
|
||||
// rt_kprintf("thread2 count: %d\n", count);
|
||||
// }
|
||||
// rt_kprintf("thread2 exit\n");
|
||||
|
||||
// /* 线程2运行结束后也将自动被系统脱离 */
|
||||
// }
|
||||
|
||||
// /* 线程示例 */
|
||||
// int thread_sample(void)
|
||||
// {
|
||||
// /* 创建线程1,名称是thread1,入口是thread1_entry */
|
||||
// tid1 = rt_thread_create("thread1",
|
||||
// thread1_entry, RT_NULL,
|
||||
// THREAD_STACK_SIZE,
|
||||
// THREAD_PRIORITY, THREAD_TIMESLICE);
|
||||
// /* 如果获得线程控制块,启动这个线程 */
|
||||
// if (tid1 != RT_NULL)
|
||||
// rt_thread_startup(tid1);
|
||||
|
||||
// /* 初始化线程2,名称是thread2,入口是thread2_entry */
|
||||
// rt_thread_init(&thread2,
|
||||
// "thread2",
|
||||
// thread2_entry,
|
||||
// RT_NULL,
|
||||
// &thread2_stack[0],
|
||||
// sizeof(thread2_stack),
|
||||
// THREAD_PRIORITY - 1, THREAD_TIMESLICE);
|
||||
|
||||
// /* 启动线程2 */
|
||||
// rt_thread_startup(&thread2);
|
||||
|
||||
// return 0;
|
||||
// }
|
||||
|
||||
// /* 导出到 msh 命令列表中 */
|
||||
// MSH_CMD_EXPORT(thread_sample, thread sample);
|
||||
|
|
13
rtconfig.h
13
rtconfig.h
|
@ -141,10 +141,6 @@
|
|||
/* end of Using USB legacy version */
|
||||
/* end of RT-Thread Components */
|
||||
|
||||
/* RT-Thread Utestcases */
|
||||
|
||||
/* end of RT-Thread Utestcases */
|
||||
|
||||
/* RT-Thread online packages */
|
||||
|
||||
/* IoT - internet of things */
|
||||
|
@ -269,6 +265,15 @@
|
|||
|
||||
/* samples: kernel and components samples */
|
||||
|
||||
#define PKG_USING_KERNEL_SAMPLES
|
||||
#define PKG_USING_KERNEL_SAMPLES_LATEST_VERSION
|
||||
#define PKG_USING_KERNEL_SAMPLES_EN
|
||||
#define KERNEL_SAMPLES_USING_THREAD
|
||||
#define KERNEL_SAMPLES_USING_SEMAPHORE
|
||||
#define KERNEL_SAMPLES_USING_MUTEX
|
||||
#define KERNEL_SAMPLES_USING_MAILBOX
|
||||
#define KERNEL_SAMPLES_USING_EVENT
|
||||
#define KERNEL_SAMPLES_USING_MESSAGEQUEUE
|
||||
/* end of samples: kernel and components samples */
|
||||
|
||||
/* entertainment: terminal games and other interesting software packages */
|
||||
|
|
|
@ -2,122 +2,6 @@
|
|||
"folders": [
|
||||
{
|
||||
"path": "."
|
||||
},
|
||||
{
|
||||
"path": "..\\..\\..\\components\\drivers\\core",
|
||||
"name": "rtthread/components/drivers/core"
|
||||
},
|
||||
{
|
||||
"path": "..\\..\\..\\components\\drivers\\i2c",
|
||||
"name": "rtthread/components/drivers/i2c"
|
||||
},
|
||||
{
|
||||
"path": "..\\..\\..\\components\\drivers\\include",
|
||||
"name": "rtthread/components/drivers/include"
|
||||
},
|
||||
{
|
||||
"path": "..\\..\\..\\components\\drivers\\ipc",
|
||||
"name": "rtthread/components/drivers/ipc"
|
||||
},
|
||||
{
|
||||
"path": "..\\..\\..\\components\\drivers\\misc",
|
||||
"name": "rtthread/components/drivers/misc"
|
||||
},
|
||||
{
|
||||
"path": "..\\..\\..\\components\\drivers\\pin",
|
||||
"name": "rtthread/components/drivers/pin"
|
||||
},
|
||||
{
|
||||
"path": "..\\..\\..\\components\\drivers\\serial",
|
||||
"name": "rtthread/components/drivers/serial"
|
||||
},
|
||||
{
|
||||
"path": "..\\..\\..\\components\\finsh",
|
||||
"name": "rtthread/components/finsh"
|
||||
},
|
||||
{
|
||||
"path": "..\\..\\..\\components\\libc\\compilers\\common",
|
||||
"name": "rtthread/components/libc/compilers/common"
|
||||
},
|
||||
{
|
||||
"path": "..\\..\\..\\components\\libc\\compilers\\common\\include",
|
||||
"name": "rtthread/components/libc/compilers/common/include"
|
||||
},
|
||||
{
|
||||
"path": "..\\..\\..\\components\\libc\\compilers\\newlib",
|
||||
"name": "rtthread/components/libc/compilers/newlib"
|
||||
},
|
||||
{
|
||||
"path": "..\\..\\..\\components\\libc\\posix\\io\\epoll",
|
||||
"name": "rtthread/components/libc/posix/io/epoll"
|
||||
},
|
||||
{
|
||||
"path": "..\\..\\..\\components\\libc\\posix\\io\\eventfd",
|
||||
"name": "rtthread/components/libc/posix/io/eventfd"
|
||||
},
|
||||
{
|
||||
"path": "..\\..\\..\\components\\libc\\posix\\io\\poll",
|
||||
"name": "rtthread/components/libc/posix/io/poll"
|
||||
},
|
||||
{
|
||||
"path": "..\\..\\..\\components\\libc\\posix\\ipc",
|
||||
"name": "rtthread/components/libc/posix/ipc"
|
||||
},
|
||||
{
|
||||
"path": "..\\..\\..\\include",
|
||||
"name": "rtthread/include"
|
||||
},
|
||||
{
|
||||
"path": "..\\..\\..\\libcpu\\arm\\common",
|
||||
"name": "rtthread/libcpu/arm/common"
|
||||
},
|
||||
{
|
||||
"path": "..\\..\\..\\libcpu\\arm\\cortex-m4",
|
||||
"name": "rtthread/libcpu/arm/cortex-m4"
|
||||
},
|
||||
{
|
||||
"path": "..\\..\\..\\src",
|
||||
"name": "rtthread/src"
|
||||
},
|
||||
{
|
||||
"path": "..\\..\\..\\src\\klibc",
|
||||
"name": "rtthread/src/klibc"
|
||||
},
|
||||
{
|
||||
"path": "..\\libraries\\HAL_Drivers",
|
||||
"name": "rtthread/libraries/HAL_Drivers"
|
||||
},
|
||||
{
|
||||
"path": "..\\libraries\\HAL_Drivers\\CMSIS\\Include",
|
||||
"name": "rtthread/libraries/HAL_Drivers/CMSIS/Include"
|
||||
},
|
||||
{
|
||||
"path": "..\\libraries\\HAL_Drivers\\drivers",
|
||||
"name": "rtthread/libraries/HAL_Drivers/drivers"
|
||||
},
|
||||
{
|
||||
"path": "..\\libraries\\HAL_Drivers\\drivers\\config",
|
||||
"name": "rtthread/libraries/HAL_Drivers/drivers/config"
|
||||
},
|
||||
{
|
||||
"path": "..\\libraries\\STM32F4xx_HAL\\CMSIS\\Device\\ST\\STM32F4xx\\Include",
|
||||
"name": "rtthread/libraries/STM32F4xx_HAL/CMSIS/Device/ST/STM32F4xx/Include"
|
||||
},
|
||||
{
|
||||
"path": "..\\libraries\\STM32F4xx_HAL\\CMSIS\\Device\\ST\\STM32F4xx\\Source\\Templates",
|
||||
"name": "rtthread/libraries/STM32F4xx_HAL/CMSIS/Device/ST/STM32F4xx/Source/Templates"
|
||||
},
|
||||
{
|
||||
"path": "..\\libraries\\STM32F4xx_HAL\\CMSIS\\Device\\ST\\STM32F4xx\\Source\\Templates\\gcc",
|
||||
"name": "rtthread/libraries/STM32F4xx_HAL/CMSIS/Device/ST/STM32F4xx/Source/Templates/gcc"
|
||||
},
|
||||
{
|
||||
"path": "..\\libraries\\STM32F4xx_HAL\\STM32F4xx_HAL_Driver\\Inc",
|
||||
"name": "rtthread/libraries/STM32F4xx_HAL/STM32F4xx_HAL_Driver/Inc"
|
||||
},
|
||||
{
|
||||
"path": "..\\libraries\\STM32F4xx_HAL\\STM32F4xx_HAL_Driver\\Src",
|
||||
"name": "rtthread/libraries/STM32F4xx_HAL/STM32F4xx_HAL_Driver/Src"
|
||||
}
|
||||
]
|
||||
}
|
Loading…
Reference in New Issue