1.新建項目
2.main.c
將以下代碼直接拷貝至main.c
/*
* 2019-09-09 RT-Thread
*/
#include <rtthread.h>
#include <board.h>
#include <rtdevice.h>
#define DBG_TAG "main"
#define DBG_LVL DBG_LOG
#include <rtdbg.h>
/* PLEASE DEFINE the LED0 pin for your board, such as: PA5 */
#define LED0_PIN GET_PIN(C, 13)
#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);
}
}
ALIGN(RT_ALIGN_SIZE)
static char thread2_stack[1024];
static struct rt_thread thread2;
/* 線程2入口 */
static void thread2_entry(void *param)
{
int count = 1;
/* set LED0 pin mode to output */
rt_pin_mode(LED0_PIN, PIN_MODE_OUTPUT);
while (count++)
{
/* set LED0 pin level to high or low */
rt_pin_write(LED0_PIN, count % 2);
LOG_D("Hello RT-Thread!");
rt_thread_mdelay(100);
}
// rt_uint32_t count = 0;
//
// /* 線程2擁有較高的優先級,以搶佔線程1而獲得執行 */
// for (count = 0; count < 10 ; count++)
// {
// /* 線程2打印計數值 */
// rt_kprintf("thread2 count: %d\n", count);
// rt_thread_mdelay(500);
// }
// rt_kprintf("thread2 exit\n");
// /* 線程2運行結束後也將自動被系統刪除
// (線程控制塊和線程棧依然在idle線程中釋放) */
}
/* 刪除線程示例的初始化 */
int thread_sample(void)
{
/* 創建線程1,名稱是thread1,入口是thread1_entry*/
tid1 = rt_thread_create("thread1",
thread1_entry, RT_NULL,
THREAD_STACK_SIZE,
THREAD_PRIORITY-1, 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);
//啓動線程
rt_thread_startup(&thread2);
return 0;
}
int main(void)
{
thread_sample();
return RT_EOK;
}