1.线程
rt_thread_t tid1 = rt_thread_create("thread1",
thread1_entry, RT_NULL,
THREAD_STACK_SIZE,
THREAD_PRIORITY, THREAD_TIMESLICE);
/* 如果获得线程控制块,启动这个线程 */
if (tid1 != RT_NULL)
rt_thread_startup(tid1);
static void thread1_entry(void *parameter)
{
}
rt_thread_suspend(rt_thread_self());//当前线程,继续线程调度
current_tick = rt_tick_get(); //获取当前时间点
1.消息队列
/*
1.队列
RT_WAITING_NO 无等待
RT_WAITING_FOREVER 永久等待
RT_EOK 成功
-RT_EFULL 消息队列已满
-RT_ERROR 失败,表示发送的消息长度大于消息队列中消息的最大长度
*/
static rt_mq_t mq;
if (rt_mq_recv(mq, &buf, sizeof(buf), RT_WAITING_FOREVER) == RT_EOK)
{}
result = rt_mq_urgent(mq, &buf, 1);
result = rt_mq_send(mq, &buf, 1);
result = rt_mq_init (mq,
"mqt",
&msg_pool[0], /* 内存池指向 msg_pool */
1, /* 每个消息的大小是 1 字节 */
sizeof(msg_pool), /* 内存池的大小是 msg_pool 的大小 */
RT_IPC_FLAG_FIFO); /* 如果有多个线程等待,按照先来先得到的方法分配消息 */
/*动态创建
rt_mq_t rt_mq_create(const char* name, rt_size_t msg_size,
rt_size_t max_msgs, rt_uint8_t flag);
*/
mq = rt_mq_create("sys_control_msg_queue", sizeof(SYS_MSG), SYS_CONTROL_MSG_QUEUE_LENGTH, RT_IPC_FLAG_FIFO );
//2.信号量
rt_sem_release(dynamic_sem);
result = rt_sem_take(dynamic_sem, RT_WAITING_FOREVER);
dynamic_sem = rt_sem_create("dsem", 0, RT_IPC_FLAG_FIFO);
2.定时器
//RT_TIMER_FLAG_PERIODIC
//RT_TIMER_FLAG_ONE_SHOT
//RT_TIMER_FLAG_SOFT_TIMER
static rt_timer_t timer1;
/* 创建定时器 1 周期定时器 */
timer1 = rt_timer_create("timer1", timeout1,
RT_NULL, 10,
RT_TIMER_FLAG_PERIODIC);
/* 启动定时器 1 */
if (timer1 != RT_NULL)
rt_timer_start(timer1);
static void timeout1(void *parameter)
{
rt_kprintf("periodic timer is timeout %d\n", cnt);
/* 运行第 10 次,停止周期定时器 */
if (cnt++>= 9)
{
rt_timer_stop(timer1);
rt_kprintf("periodic timer was stopped! \n");
}
}
if(timer->parent.flag & RT_TIMER_FLAG_ACTIVATED) ;//判断定时器是否激活
/*
rt_err_t rt_timer_control(rt_timer_t timer, rt_uint8_t cmd, void* arg);
#define RT_TIMER_CTRL_SET_TIME 0x0 设置定时器超时时间
#define RT_TIMER_CTRL_GET_TIME 0x1 获得定时器超时时间
#define RT_TIMER_CTRL_SET_ONESHOT 0x2 设置定时器为单次定时器
#define RT_TIMER_CTRL_SET_PERIODIC 0x3 设置定时器为周期型定时器
RT_TIMER_FLAG_SOFT_TIMER
定时器使用不要加引起阻塞的函数,硬件定时器在中断上下文时不允许的,软件定时器加阻塞会导致其它定时器可能不能正常使用
*/
错误类型:
#define RT_EOK 0 无错误
#define RT_ERROR 1 一般错误
#define RT_ETIMEOUT 2 超时错误
#define RT_EFULL 3 资源已满
#define RT_EEMPTY 4 资源已空
#define RT_ENOMEM 5 内存不足
#define RT_ENOSYS 6 无系统
#define RT_EBUSY 7 忙线中
#define RT_EIO 8 IO错误
#define RT_EINTR 9 中断系统调用
#define RT_EINVAL 10 无效参数
互质信号量
static rt_mutex_t dynamic_mutex = RT_NULL;
rt_mutex_take(dynamic_mutex, RT_WAITING_FOREVER);
rt_mutex_release(dynamic_mutex);
dynamic_mutex = rt_mutex_create("dmutex", RT_IPC_FLAG_FIFO);