打印
[牛人杂谈]

RT-Thread在NUC029上运行线程

[复制链接]
919|16
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主

之前发了一个移植的帖,这次修改一下跑两个线程,来证明RTOS用于多线程的妙处

这个是main.c内容

#include "stdio.h"
#include "NUC029xGE.h"
#include <rtthread.h>
//配置优先级,栈大小,时间片,设置不对没法运行的。
#define THREAD_PRIORITY         5
#define THREAD_STACK_SIZE       256
#define THREAD_TIMESLICE        10


void led(void *parameter)
{
    printf("\n\nCPU [url=home.php?mod=space&uid=72445]@[/url] %d Hz\n", SystemCoreClock);
    printf("+-------------------------------------------------+\n");
    printf("|    PC.5(Output)  Sample Code     |\n");
    printf("+-------------------------------------------------+\n\n");

    /*-----------------------------------------------------------------------------------------------------*/
    /* GPIO Basic Mode Test --- Use Pin Data Input/Output to control GPIO pin                              */
    /*-----------------------------------------------------------------------------------------------------*/

    /* Configure PC.5 as Output mode*/
//    GPIO_SetMode(PC, BIT5, GPIO_MODE_OUTPUT);

    /* Use Pin Data Input/Output Control to pull specified I/O or get I/O pin status */
        rt_kprintf("Hello RTT_NANO\n");
        while(1)
                {
                        PC5=0;
                        rt_thread_mdelay(3333);
                        printf("\nLED is ON\n");
                        PC5=1;
                        rt_thread_mdelay(3333);
                        printf("\nLED is OFF\n");       
                }
//return 0;
}
/* 导出到 msh 命令列表中 */
MSH_CMD_EXPORT(led, RT-Thread first led sample);

void led2(void *parameter)
{


    /* Configure PC.5 as Output mode*/
//    GPIO_SetMode(PC, BIT5, GPIO_MODE_OUTPUT);

    /* Use Pin Data Input/Output Control to pull specified I/O or get I/O pin status */
        rt_kprintf("Hello RTT_NANO\n");
        while(1)
                {
                        PC5=0;
                        rt_thread_mdelay(2000);
                        printf("\nLED2 is ON\n");
                        PC5=1;
                        rt_thread_mdelay(2000);
                        printf("\nLED2 is OFF\n");       
                }
//return 0;
}

MSH_CMD_EXPORT(led2, RT-Thread second led sample);

int led_sample(void)
{
static rt_thread_t tid = RT_NULL;
static rt_thread_t tid2 = RT_NULL;
        /* 创建线程1 */
    tid = rt_thread_create("thread1",
                            led, RT_NULL,
                            THREAD_STACK_SIZE,
                            THREAD_PRIORITY, THREAD_TIMESLICE);
   
    if (tid != RT_NULL)      
        rt_thread_startup(tid);

        /* 创建线程2 */
    tid2 = rt_thread_create("thread2",
                            led2, RT_NULL,
                            THREAD_STACK_SIZE,
                            THREAD_PRIORITY, THREAD_TIMESLICE);
   
    if (tid2 != RT_NULL)      
        rt_thread_startup(tid2);               
                //该例子共用PC5端口,所以一并在创建线程时候初始化为输出模式
                GPIO_SetMode(PC, BIT5, GPIO_MODE_OUTPUT);               
    return 0;
}
MSH_CMD_EXPORT(led_sample, RT-Thread sample);
/*---------------------------------------------------------------------------------------------------------*/
/*  Main Function                                                                                          */
/*---------------------------------------------------------------------------------------------------------*/
int32_t main(void)
{
        led_sample();
        return 0;
}
这个是rtconfig.h内容
/* RT-Thread config file */

#ifndef __RTTHREAD_CFG_H__
#define __RTTHREAD_CFG_H__

#if defined(__CC_ARM) || defined(__CLANG_ARM)
//#include "RTE_Components.h"

//#if defined(RTE_USING_FINSH)
#define RT_USING_FINSH
//#endif //RTE_USING_FINSH

#endif //(__CC_ARM) || (__CLANG_ARM)

// <<< Use Configuration Wizard in Context Menu >>>
// <h>Basic Configuration
// <o>Maximal level of thread priority <8-256>
//  <i>Default: 32
#define RT_THREAD_PRIORITY_MAX  8
// <o>OS tick per second
//  <i>Default: 1000   (1ms)
#define RT_TICK_PER_SECOND  1000
// <o>Alignment size for CPU architecture data access
//  <i>Default: 4
#define RT_ALIGN_SIZE   4
// <o>the max length of object name<2-16>
//  <i>Default: 8
#define RT_NAME_MAX    8
// <c1>Using RT-Thread components initialization
//  <i>Using RT-Thread components initialization
#define RT_USING_COMPONENTS_INIT
// </c>

#define RT_USING_USER_MAIN

// <o>the stack size of main thread<1-4086>
//  <i>Default: 512
#define RT_MAIN_THREAD_STACK_SIZE     256

// </h>

// <h>Debug Configuration
// <c1>enable kernel debug configuration
//  <i>Default: enable kernel debug configuration
#define RT_DEBUG
// </c>
// <o>enable components initialization debug configuration<0-1>
//  <i>Default: 0
#define RT_DEBUG_INIT 0
// <c1>thread stack over flow detect
//  <i> Diable Thread stack over flow detect
//#define RT_USING_OVERFLOW_CHECK
// </c>
// </h>

// <h>Hook Configuration
// <c1>using hook
//  <i>using hook
#define RT_USING_HOOK
// </c>
// <c1>using idle hook
//  <i>using idle hook
//#define RT_USING_IDLE_HOOK
// </c>
// </h>

// <e>Software timers Configuration
// <i> Enables user timers
#define RT_USING_TIMER_SOFT         0
#if RT_USING_TIMER_SOFT == 0
    #undef RT_USING_TIMER_SOFT
#endif
// <o>The priority level of timer thread <0-31>
//  <i>Default: 4
#define RT_TIMER_THREAD_PRIO        4
// <o>The stack size of timer thread <0-8192>
//  <i>Default: 512
#define RT_TIMER_THREAD_STACK_SIZE  512
// </e>

// <h>IPC(Inter-process communication) Configuration
// <c1>Using Semaphore
//  <i>Using Semaphore
#define RT_USING_SEMAPHORE
// </c>
// <c1>Using Mutex
//  <i>Using Mutex
//#define RT_USING_MUTEX
// </c>
// <c1>Using Event
//  <i>Using Event
//#define RT_USING_EVENT
// </c>
// <c1>Using MailBox
//  <i>Using MailBox
#define RT_USING_MAILBOX
// </c>
// <c1>Using Message Queue
//  <i>Using Message Queue
//#define RT_USING_MESSAGEQUEUE
// </c>
// </h>

// <h>Memory Management Configuration
// <c1>Dynamic Heap Management
//  <i>Dynamic Heap Management
#define RT_USING_HEAP
// </c>
// <c1>using small memory
//  <i>using small memory
#define RT_USING_SMALL_MEM
// </c>
// <c1>using tiny size of memory
//  <i>using tiny size of memory
//#define RT_USING_TINY_SIZE
// </c>
// </h>

// <h>Console Configuration
// <c1>Using console
//  <i>Using console
#define RT_USING_CONSOLE
// </c>
// <o>the buffer size of console <1-1024>
//  <i>the buffer size of console
//  <i>Default: 128  (128Byte)
#define RT_CONSOLEBUF_SIZE          128
// </h>

#if defined(RT_USING_FINSH)
    #define FINSH_USING_MSH
    #define FINSH_USING_MSH_ONLY
    // <h>Finsh Configuration
    // <o>the priority of finsh thread <1-7>
    //  <i>the priority of finsh thread
    //  <i>Default: 6
    #define __FINSH_THREAD_PRIORITY     5
    #define FINSH_THREAD_PRIORITY       (RT_THREAD_PRIORITY_MAX / 8 * __FINSH_THREAD_PRIORITY + 1)
    // <o>the stack of finsh thread <1-4096>
    //  <i>the stack of finsh thread
    //  <i>Default: 4096  (4096Byte)
    #define FINSH_THREAD_STACK_SIZE     512
    // <o>the history lines of finsh thread <1-32>
    //  <i>the history lines of finsh thread
    //  <i>Default: 5
    #define FINSH_HISTORY_LINES         1

    #define FINSH_USING_SYMTAB
    // </h>
#endif

// <<< end of configuration section >>>

#endif
这个是board.c内容
/*
* Copyright (c) 2006-2019, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date           Author       Notes
* 2017-07-24     Tanek        the first version
* 2018-11-12     Ernest Chen  modify copyright
*/
#include <stdio.h>
#include <stdint.h>
#include <rthw.h>
#include <rtthread.h>
//#include "uart.h"
#include "NUC029xGE.h"
#define PLL_CLOCK       72000000

#define _SCB_BASE       (0xE000E010UL)
#define _SYSTICK_CTRL   (*(rt_uint32_t *)(_SCB_BASE + 0x0))
#define _SYSTICK_LOAD   (*(rt_uint32_t *)(_SCB_BASE + 0x4))
#define _SYSTICK_VAL    (*(rt_uint32_t *)(_SCB_BASE + 0x8))
#define _SYSTICK_CALIB  (*(rt_uint32_t *)(_SCB_BASE + 0xC))
#define _SYSTICK_PRI    (*(rt_uint8_t  *)(0xE000ED23UL))

// Updates the variable SystemCoreClock and must be called
// whenever the core clock is changed during program execution.
extern void SystemCoreClockUpdate(void);

// Holds the system core clock, which is the system clock
// frequency supplied to the SysTick timer and the processor
// core clock.
extern uint32_t SystemCoreClock;

static uint32_t _SysTick_Config(rt_uint32_t ticks)
{
    if ((ticks - 1) > 0xFFFFFF)
    {
        return 1;
    }
   
    _SYSTICK_LOAD = ticks - 1;
    _SYSTICK_PRI = 0xFF;
    _SYSTICK_VAL  = 0;
    _SYSTICK_CTRL = 0x07;  
   
    return 0;
}

#if defined(RT_USING_USER_MAIN) && defined(RT_USING_HEAP)
#define RT_HEAP_SIZE 1024
static uint32_t rt_heap[RT_HEAP_SIZE];     // heap default size: 4K(1024 * 4)
RT_WEAK void *rt_heap_begin_get(void)
{
    return rt_heap;
}

RT_WEAK void *rt_heap_end_get(void)
{
    return rt_heap + RT_HEAP_SIZE;
}
#endif
/*自己添加跟硬件有关的时钟初始化*/
static void rt_hw_system_init(void)
{
    /*---------------------------------------------------------------------------------------------------------*/
    /* Init System Clock                                                                                       */
    /*---------------------------------------------------------------------------------------------------------*/
    SYS_UnlockReg();

  /* Enable HIRC clock (Internal RC 22.1184MHz) */
    CLK_EnableXtalRC(CLK_PWRCTL_HIRCEN_Msk);

    /* Wait for HIRC clock ready */
    CLK_WaitClockReady(CLK_STATUS_HIRCSTB_Msk);

    /* Select HCLK clock source as HIRC and HCLK clock divider as 1 */
    CLK_SetHCLK(CLK_CLKSEL0_HCLKSEL_HIRC, CLK_CLKDIV0_HCLK(1));

    /* Set core clock as PLL_CLOCK from PLL */
    CLK_SetCoreClock(PLL_CLOCK);
    /* Set SysTick clock source to HCLK source divide 2 */
    CLK_SetSysTickClockSrc(CLK_CLKSEL0_STCLKSEL_HCLK_DIV2);

    SYS_LockReg();
}
/*自己添加跟串口有关的时钟初始化*/
static void rt_hw_usart_init()
{
    /* Unlock protected registers */
    SYS_UnlockReg();

    /* Enable HXT clock (external XTAL 12MHz) */
    CLK_EnableXtalRC(CLK_PWRCTL_HXTEN_Msk);

    /* Wait for HXT clock ready */
    CLK_WaitClockReady(CLK_STATUS_HXTSTB_Msk);
  
    /* Enable UART module clock */
    CLK_EnableModuleClock(UART0_MODULE);

    /* Select UART module clock source as HXT and UART module clock divider as 1 */
    CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_UARTSEL_HXT, CLK_CLKDIV0_UART(1));

    /*---------------------------------------------------------------------------------------------------------*/
    /* Init I/O Multi-function                                                                                 */
    /*---------------------------------------------------------------------------------------------------------*/

    /* Set multi-function pins for UART0 RXD and TXD */
    SYS->GPA_MFPL &= ~(SYS_GPA_MFPL_PA2MFP_Msk | SYS_GPA_MFPL_PA3MFP_Msk);
    SYS->GPA_MFPL |= (SYS_GPA_MFPL_PA3MFP_UART0_RXD | SYS_GPA_MFPL_PA2MFP_UART0_TXD);

    /* Lock protected registers */
    SYS_LockReg();
               
                    /* Reset UART0 */
    SYS_ResetModule(UART0_RST);

    /* Configure UART0 and set UART0 baud rate */
    UART_Open(UART0, 115200);
}

/**
* This function will initial your board.
*/
void rt_hw_board_init()
{
    /* Configure the system clock */
    rt_hw_system_init();

                /* System Clock Update */
    SystemCoreClockUpdate();
   
    /* System Tick Configuration */
    _SysTick_Config(SystemCoreClock / RT_TICK_PER_SECOND);

        /* Initial usart deriver, and set console device */
                rt_hw_usart_init();       
       
    /* Call components board initial (use INIT_BOARD_EXPORT()) */
#ifdef RT_USING_COMPONENTS_INIT
    rt_components_board_init();
#endif

#if defined(RT_USING_USER_MAIN) && defined(RT_USING_HEAP)
    rt_system_heap_init(rt_heap_begin_get(), rt_heap_end_get());
#endif
}

void SysTick_Handler(void)
{
    /* enter interrupt */
    rt_interrupt_enter();

    rt_tick_increase();

    /* leave interrupt */
    rt_interrupt_leave();
}
/*自己添加对接串口控制台*/
void rt_hw_console_output(const char *str)
{
        printf("%s",str);
}

char rt_hw_console_getchar(void)
{
return getchar();
}
其实只要这三个文件这么弄,其他的是不用修改的,就可以运行了,当然了,你要会自己新建一个RTT的项目工程。而且在包括NUC029的BSP的,另外就是替换上面三个文件内容了。

使用特权

评论回复
沙发
gaoyang9992006|  楼主 | 2020-8-1 15:33 | 只看该作者

使用特权

评论回复
板凳
gaoyang9992006|  楼主 | 2020-8-1 15:33 | 只看该作者
我这是直接将创建线程函数放main里面上电就执行了,你也可以不放进去,然后通过msh指令执行。

使用特权

评论回复
地板
gaoyang9992006|  楼主 | 2020-8-1 15:34 | 只看该作者

使用特权

评论回复
5
gaoyang9992006|  楼主 | 2020-8-1 15:35 | 只看该作者

执行的时候也可以发送msh命令

使用特权

评论回复
6
gaoyang9992006|  楼主 | 2020-8-1 15:36 | 只看该作者

发送help命令,可以看到MSH列表什么可以使用的指令。

使用特权

评论回复
7
huangcunxiake| | 2020-8-2 12:22 | 只看该作者
给楼主点赞,多谢分享。

使用特权

评论回复
8
小灵通2018| | 2020-8-2 19:58 | 只看该作者
真不错,学习学习

使用特权

评论回复
9
幸福小强| | 2020-8-3 19:38 | 只看该作者
跑的很6,学了一招高级的。

使用特权

评论回复
10
wanduzi| | 2020-8-3 19:45 | 只看该作者
连续两个高水平贴,给力。

使用特权

评论回复
11
21mengnan| | 2020-8-3 21:29 | 只看该作者
跟楼主学技术,楼主继续更。

使用特权

评论回复
12
21mengnan| | 2020-8-3 21:34 | 只看该作者
动态线程,应该一个静态的更换。

使用特权

评论回复
13
xinxianshi| | 2020-8-3 23:35 | 只看该作者
给力,简单好学。

使用特权

评论回复
14
wahahaheihei| | 2020-8-3 23:35 | 只看该作者
能弄到51上用吗

使用特权

评论回复
15
xixi2017| | 2020-8-4 20:05 | 只看该作者
直接使用。多谢。

使用特权

评论回复
16
yiyigirl2014| | 2020-8-4 22:02 | 只看该作者
前面三个宏大小如何设置。没太懂,该设置多少合适。

使用特权

评论回复
17
jiekou001| | 2020-8-6 11:22 | 只看该作者
前来学习。

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

认证:西安公路研究院南京院
简介:主要工作从事监控网络与通信网络设计,以及从事基于嵌入式的通信与控制设备研发。擅长单片机嵌入式系统物联网设备开发,音频功放电路开发。

1896

主题

15631

帖子

198

粉丝