RT-Thread-v2.0.0移植到STM32及驱动LCD和测温

先简单记录下安装:

1. 安装 sourcery工具链或 keil都行
安装python 和 scons:
配置这些工具路径以及RT-Thread源码路径RTT_ROOT,添加到环境变量

2. 开始编译 bspstm32f10x工程,在编译以前需要稍微修改如下几个文件
rtconfig.py 此文件必须修改
rtconfig.h 此文件用于裁剪rt-thread(根据需要修改)
比如使用 keil工具来编译:
修改rtconfig.py,
CROSS_TOOL='keil'
EXEC_PATH='D:Keil_v5'
cmd命令行 cd到bspstm32f10x:
D: t-threadspstm32f10x>scons
即可在 bspstm32f10x路径下生成 axf文件和bin文件
scons -c 清除编译出来的文件,如.o
scons -j4 多线程编译,2个线程/每个CPU核

主要在 rt-thread-v2.0.0spstm32f10x 下的文件进行修改和添加:

driversSConscript 添加驱动文件,

# add the general drivers.
src = Split("""
board.c
stm32f10x_it.c
led.c
LCD5110.c
sys.c
ds18b20.c
usart.c
""")

这些基本的驱动文件里的延时需要调试下。

其他文件不用怎么改,接下来在 applicationsapplication.c 中创建自己的线程函数了,实现相关功能。

/*
application.c
2015.12.4 by Huangtao
 
 */

#include <board.h>
#include <rtthread.h>

#ifdef  RT_USING_COMPONENTS_INIT
#include <components.h>
#endif  /* RT_USING_COMPONENTS_INIT */

#ifdef RT_USING_DFS
/* dfs filesystem:ELM filesystem init */
#include <dfs_elm.h>
/* dfs Filesystem APIs */
#include <dfs_fs.h>
#endif

#ifdef RT_USING_RTGUI
#include <rtgui/rtgui.h>
#include <rtgui/rtgui_server.h>
#include <rtgui/rtgui_system.h>
#include <rtgui/driver.h>
#include <rtgui/calibration.h>
#endif

#include "led.h"
#include "LCD5110.h"
#include "ds18b20.h"

// Thread ID
static rt_thread_t led_id = RT_NULL;
static rt_thread_t lcd5110_id = RT_NULL;
static rt_thread_t ds18b20_id = RT_NULL;

#define CPU_USAGE_CALC_TICK    10
#define CPU_USAGE_LOOP        100

static rt_uint8_t  cpu_usage_major = 0, cpu_usage_minor= 0;
static rt_uint32_t total_count = 0;

static void cpu_usage_idle_hook()
{
    rt_tick_t tick;
    rt_uint32_t count;
    volatile rt_uint32_t loop;

    if (total_count == 0)
    {
        /* get total count */
        rt_enter_critical();
        tick = rt_tick_get();
        while(rt_tick_get() - tick < CPU_USAGE_CALC_TICK)
        {
            total_count ++;
            loop = 0;
            while (loop < CPU_USAGE_LOOP) loop ++;
        }
        rt_exit_critical();
    }

    count = 0;
    /* get CPU usage */
    tick = rt_tick_get();
    while (rt_tick_get() - tick < CPU_USAGE_CALC_TICK)
    {
        count ++;
        loop  = 0;
        while (loop < CPU_USAGE_LOOP) loop ++;
    }

    /* calculate major and minor */
    if (count < total_count)
    {
        count = total_count - count;
        cpu_usage_major = (count * 100) / total_count;
        cpu_usage_minor = ((count * 100) % total_count) * 100 / total_count;
    }
    else
    {
        total_count = count;

        /* no CPU usage */
        cpu_usage_major = 0;
        cpu_usage_minor = 0;
    }

}

/*void cpu_usage_get(rt_uint8_t *major, rt_uint8_t *minor)
{
    RT_ASSERT(major != RT_NULL);
    RT_ASSERT(minor != RT_NULL);

    *major = cpu_usage_major;
    *minor = cpu_usage_minor;
}*/

void cpu_usage_init()
{
    /* set idle thread hook */
    rt_thread_idle_sethook(cpu_usage_idle_hook);
}


/*
// led
ALIGN(RT_ALIGN_SIZE)
static rt_uint8_t led_stack[ 512 ];
static struct rt_thread led_thread;*/
static void led_thread_entry(void* parameter)
{
    rt_hw_led_init();

    while (1)
    {
        /* led1 on */
        rt_hw_led_on(0);

        // 顺便清屏
        ClearScreen();

        rt_thread_delay( 100 ); /* sleep 1 second and switch to other thread */

        /* led1 off */
        rt_hw_led_off(0);

        // 顺便清屏
        //ClearScreen();

        rt_thread_delay( 100 );

    }
}

// lcd5110
static void lcd5110_thread_entry(void* parameter)
{
    LcdInit();

    while(1)
    {
        DispString(15,0,"RT-Thread");
        DispString(0,1,"CPU:");
        DispNum(30,1,cpu_usage_major);
        DispChar(45,1,'%');

        rt_thread_delay( 5 );

    }

}

// ds18b20
static void ds18b20_thread_entry(void* parameter)
{
    short temperature = 0;
    while(DS18B20_Init());

    while(1)
    {
        DispString(0,3,"Temp: ");
        temperature = DS18B20_Get_Temp();
        if(temperature<0)
        {
            DispChar(40,3,'-');         
            temperature=-temperature;   
        }
        else 
            DispChar(40,3,' '); 

        DispNum(48,3,((u16)temperature)/10);    //显示正数部分    
        DispChar(60,3,'.');     
        DispNum(67,3,((u16)temperature)%10);    //显示小数部分 

        rt_thread_delay( 5 );
    }

}


void rt_init_thread_entry(void* parameter)
{
#ifdef RT_USING_COMPONENTS_INIT
    /* initialization RT-Thread Components */
    rt_components_init();
#endif

#ifdef  RT_USING_FINSH
    finsh_set_device(RT_CONSOLE_DEVICE_NAME);
#endif  /* RT_USING_FINSH */

    /* Filesystem Initialization */
#if defined(RT_USING_DFS) && defined(RT_USING_DFS_ELMFAT)
    /* mount sd card fat partition 1 as root directory */
    if (dfs_mount("sd0", "/", "elm", 0, 0) == 0)
    {
        rt_kprintf("File System initialized!
");
    }
    else
        rt_kprintf("File System initialzation failed!
");
#endif  /* RT_USING_DFS */

#ifdef RT_USING_RTGUI
    {
        extern void rt_hw_lcd_init();
        extern void rtgui_touch_hw_init(void);

        rt_device_t lcd;

        /* init lcd */
        rt_hw_lcd_init();

        /* init touch panel */
        rtgui_touch_hw_init();

        /* find lcd device */
        lcd = rt_device_find("lcd");

        /* set lcd device as rtgui graphic driver */
        rtgui_graphic_set_device(lcd);

#ifndef RT_USING_COMPONENTS_INIT
        /* init rtgui system server */
        rtgui_system_server_init();
#endif

        calibration_set_restore(cali_setup);
        calibration_set_after(cali_store);
        calibration_init();
    }
#endif /* #ifdef RT_USING_RTGUI */
}

int rt_application_init(void)
{
    rt_thread_t init_thread;


    /*
    rt_err_t result;
    // 静态创建 led 线程 
    result = rt_thread_init(&led_thread,                // 线程控制块内存地址
                            "led",                      // 线程名称
                            led_thread_entry,           // 线程入口入口函数
                            RT_NULL,                    // 线程入口入口函数参数
                            (rt_uint8_t*)&led_stack[0], // 线程栈起始地址
                            sizeof(led_stack),          // 线程栈大小
                            20,                         // 线程优先级
                            5);                         // 线程时间片大小
    if (result == RT_EOK)
    {
        rt_thread_startup(&led_thread);
    }
    */

    // 动态创建 led 线程
    led_id = rt_thread_create("led",            // 线程名称
                            led_thread_entry,   // 线程入口入口函数 
                            RT_NULL,            // 线程入口入口函数参数 
                            512,                // 线程栈大小
                            21,                 // 线程优先级
                            10);                // 线程时间片大小
    // 如果获得线程控制块,启动这个线程
    if (led_id != RT_NULL) 
        rt_thread_startup(led_id);
    
    // 动态创建 lcd5110 线程
    lcd5110_id = rt_thread_create("lcd5110",        // 线程名称
                            lcd5110_thread_entry,   // 线程入口入口函数 
                            RT_NULL,                // 线程入口入口函数参数 
                            2048,                   // 线程栈大小
                            20,                     // 线程优先级
                            20);                    // 线程时间片大小
    if (lcd5110_id != RT_NULL) 
        rt_thread_startup(lcd5110_id);

    // 动态创建 ds18b20 线程
    ds18b20_id = rt_thread_create("ds18b20",        // 线程名称
                            ds18b20_thread_entry,   // 线程入口入口函数 
                            RT_NULL,                // 线程入口入口函数参数 
                            2048,                   // 线程栈大小
                            19,                     // 线程优先级
                            20);                    // 线程时间片大小
    if (ds18b20_id != RT_NULL) 
        rt_thread_startup(ds18b20_id);

    // CPU %
    cpu_usage_init();

#if (RT_THREAD_PRIORITY_MAX == 32)
    init_thread = rt_thread_create("init",
                                   rt_init_thread_entry, RT_NULL,
                                   2048, 8, 20);
#else
    init_thread = rt_thread_create("init",
                                   rt_init_thread_entry, RT_NULL,
                                   2048, 80, 20);
#endif

    if (init_thread != RT_NULL)
        rt_thread_startup(init_thread);

    return 0;
}

/*@}*/

这些线程时间片和每个线程中的延时根据需要调整,不然出现屏幕显示异常。^_^

下面放张效果图:

 

原文地址:https://www.cnblogs.com/ht-beyond/p/5020148.html