基于IAR-stm32裸板工程,完美移植RT-Thread Nano系统(附源码)

  • 开发环境:Window 10 64bit
  • 开发工具:IAR Embedded Workbench
  • 硬件:stm32f103c8t6

准备工作:

     下载一份IAR的stm32裸机工程(包含标准库),参考博客,下载链接。

     下载RT-Thread Nano源码,下载链接。

添加RT-Thread Nano源码到IAR的工程:

在STM32-IAR-Demo目录下创建文件夹RT-Thread,并将以下文件添加到该文件夹里:

  • Nano 源码中的 includelibcpusrc 文件夹。

  • 配置文件:源码代码 rtthread/bsp 文件夹中的两个文件:board.crtconfig.c

基于IAR-stm32裸板工程,完美移植RT-Thread Nano系统(附源码)_第1张图片

打开STM32-IAR-Demo/Project下的IAR工程STM32-IAR-Demo.eww,添加Rtthread分组,并将以下文件添加到该分组:

  • 添加工程下RT-Thread/src/文件夹中所有的文件;
  • 添加工程下RT-Thread//libcpu/ 文件夹中相应内核的 CPU 移植文件及上下文切换文件:cpuport.c以及context_iar.S
  • 添加工程下RT-Thread/文件夹下的board.c

基于IAR-stm32裸板工程,完美移植RT-Thread Nano系统(附源码)_第2张图片

添加头文件路径,点击Project->Options->C/C++Compiler->Preprocessor。

(提供复制)
$PROJ_DIR$\..\RT-Thread
$PROJ_DIR$\..\RT-Thread\include

基于IAR-stm32裸板工程,完美移植RT-Thread Nano系统(附源码)_第3张图片

适配 RT-Thread Nano

修改User组下的stm32f10x_it.c中断函数文件:

  • 去掉void HardFault_Handler(void)函数
  • 去掉void PendSV_Handler(void)函数
  • 去掉void SysTick_Handler(void)函数

以上三个函数由RT-Thread系统实现,避免重复定义。

实现rt_kprintf串口打印功能,rt_kprintf函数系统已经实现,我们只要实现它里面的rt_hw_console_output函数和串口的初始化。

  • rtconfig.h文件里打开宏#define RT_USING_CONSOLE
  • 在board.c文件末尾加入一下代码:
// USART1  PA9-TX  PA10-RX
static int uart_init(void)
{
    GPIO_InitTypeDef GPIO_InitStructure;
    USART_InitTypeDef USART_InitStructure;
    NVIC_InitTypeDef NVIC_InitStructure;
    
    //配置串口1 (USART1) 时钟
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA, ENABLE);
    
    //配置串口1接收终端的优先级
    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0); 
    NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; 
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; 
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; 
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure);

    //配置串口1 发送引脚(PA.09)
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOA, &GPIO_InitStructure);    
    //配置串口1 接收引脚 (PA.10)
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
    GPIO_Init(GPIOA, &GPIO_InitStructure);

   //串口1工作模式(USART1 mode)配置 
    USART_InitStructure.USART_BaudRate = 115200;//一般设置为9600;
    USART_InitStructure.USART_WordLength = USART_WordLength_8b; //数据位为8个字节
    USART_InitStructure.USART_StopBits = USART_StopBits_1; //一位停止位
    USART_InitStructure.USART_Parity = USART_Parity_No ; //无校验位
    USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; //不需要流控制
    USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //接收发送模式
    USART_Init(USART1, &USART_InitStructure); 
		
    //USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);//开启中断
    USART_Cmd(USART1, ENABLE);//使能串口
    return 0;
}
INIT_BOARD_EXPORT(uart_init);

void rt_hw_console_output(const char *str)
{
    rt_size_t i = 0, size = 0;
    char a = '\r';

    size = rt_strlen(str);
    for (i = 0; i < size; i++)
    {
        if (*(str + i) == '\n')
        {
            USART_SendData(USART1, a);
            while( USART_GetFlagStatus(USART1,USART_FLAG_TC)!= SET);
        }
        USART_SendData(USART1, *(str + i));
        while( USART_GetFlagStatus(USART1,USART_FLAG_TC)!= SET);
    }
}

int rt_hw_console_getchar(void)
{
    int ch = -1;

    if (USART_GetFlagStatus(USART1, USART_FLAG_RXNE) != RESET)
    {
        ch = USART_ReceiveData(USART1);
        USART_ClearFlag(USART1, USART_FLAG_RXNE);
    }
    else
    {
        //rt_thread_mdelay(10);
    }
    return ch;
}

uart_init函数不需要添加到初始化函数调用,使用INIT_BOARD_EXPORT宏之后再系统初始化时自动调用。 

编写第一个应用:

  • 在main文件首部增加RT-Thread的相关头文件
  • 在main()函数中实现PC13引脚的LED指示等闪烁,并使用串口打印信息
  • 调用rt_thread_mdelay(500)延时函数时,切换到其他线程运行,体现了线程实时性的特点。
#include "stm32f10x.h"
#include 

#define LD2_GPIO_PORT  GPIOC
#define LD2_PIN        GPIO_Pin_13

static void MX_GPIO_Init(void);

int main(void)
{
    MX_GPIO_Init();

    while (1)
    {
        GPIO_SetBits(LD2_GPIO_PORT, LD2_PIN);
        rt_thread_mdelay(500);
        GPIO_ResetBits(LD2_GPIO_PORT, LD2_PIN);
        rt_thread_mdelay(500);
        rt_kprintf("main runing.\r\n");
    }
}

static void MX_GPIO_Init(void)
{
    GPIO_InitTypeDef GPIO_InitStruct = {0};

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE);
    GPIO_ResetBits(LD2_GPIO_PORT, LD2_PIN);

    GPIO_InitStruct.GPIO_Pin  = LD2_PIN;
    GPIO_InitStruct.GPIO_Mode  = GPIO_Mode_Out_PP;
    GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(LD2_GPIO_PORT, &GPIO_InitStruct);
}


编译下载到开发板,程序运行,led正常闪烁,接上串口助手,可看到打印信息。

基于IAR-stm32裸板工程,完美移植RT-Thread Nano系统(附源码)_第4张图片

已移植的完整工程代码,下载链接。

 

如若有误,还望指出,谢谢。

你可能感兴趣的:(RT-Thread)