RT-Thread nano 3.1.3 添加控制台与 FinSH

一、使用STM32CubeMX 5.5.0创建rt-thread nano工程

1.选择芯片,潘多拉开发板芯片为STM32L475VET6

RT-Thread nano 3.1.3 添加控制台与 FinSH

2.设置PE7、PE8、PE9为推挽输出(用作验证)

RT-Thread nano 3.1.3 添加控制台与 FinSH

3.添加RT-Thread nano 3.1.3 kernel

RT-Thread nano 3.1.3 添加控制台与 FinSH

4.取消自动生成HardFault_Handler、PendSV_Handler、SysTick_Handler 代码

RT-Thread nano 3.1.3 添加控制台与 FinSH

5.生成MDK工程文件

RT-Thread nano 3.1.3 添加控制台与 FinSH

6.使用keil打开MDK工程,添加LED闪烁线程

RT-Thread nano 3.1.3 添加控制台与 FinSH
RT-Thread nano 3.1.3 添加控制台与 FinSH

//引入头文件
#include <rtthread.h>
//初始化静态线程结构体和线程栈
static struct rt_thread led_thread;
static char led_thread_stack[256];
//编写线程入口函数
static void led_thread_entry(void *parameter)
{
	while(1)
	{
		HAL_GPIO_WritePin(GPIOE, GPIO_PIN_7, GPIO_PIN_RESET);
		rt_thread_mdelay(500);
		HAL_GPIO_WritePin(GPIOE, GPIO_PIN_7, GPIO_PIN_SET);
		rt_thread_mdelay(500);
	}
}
//测试线程
int main(void)
{
  HAL_Init();
  MX_GPIO_Init();

  /* USER CODE BEGIN 2 */
	HAL_GPIO_WritePin(GPIOE, GPIO_PIN_8, GPIO_PIN_SET);
	HAL_GPIO_WritePin(GPIOE, GPIO_PIN_9, GPIO_PIN_SET);
	rt_err_t rst;
	rst = rt_thread_init(&led_thread,
						"ledshine",
						led_thread_entry,
						RT_NULL,
						&led_thread_stack[0],
						sizeof(led_thread_stack),
						RT_THREAD_PRIORITY_MAX-2,
						20);
	if(rst == RT_EOK)
	{
		rt_thread_startup(&led_thread);
	}
}
7.编译成功,下载程序,LED正常闪烁!RT-Thread nano 3.1.3内核移植成功。

RT-Thread nano 3.1.3 添加控制台与 FinSH

二、移植finsh组件

1.未移植finsh组件的工程目录结构如下

RT-Thread nano 3.1.3 添加控制台与 FinSH

2.在STM32CubeMX中添加rt-thread shell组件

RT-Thread nano 3.1.3 添加控制台与 FinSH

3.生成代码,工程结构如下:

RT-Thread nano 3.1.3 添加控制台与 FinSH

4.在board.c\color{#FF3030}{board.c}board.c中增加以下代码
4.1引入头文件
#include "stm32l4xx_hal.h"
4.2添加代码
static UART_HandleTypeDef huart1;

static void MX_USART1_UART_Init(void)
{
  huart1.Instance = USART1;
  huart1.Init.BaudRate = 115200;
  huart1.Init.WordLength = UART_WORDLENGTH_8B;
  huart1.Init.StopBits = UART_STOPBITS_1;
  huart1.Init.Parity = UART_PARITY_NONE;
  huart1.Init.Mode = UART_MODE_TX_RX;
  huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  huart1.Init.OverSampling = UART_OVERSAMPLING_16;
  huart1.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
  huart1.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
  if (HAL_UART_Init(&huart1) != HAL_OK)
  {
    ;
  }
}
//INIT_BOARD_EXPORT(MX_USART1_UART_Init);

void rt_hw_console_output(const char *str)
{
        rt_size_t i = 0, size = 0;
        char a = '\r';
        
        __HAL_UNLOCK(&huart1);
        
        size = rt_strlen(str);
        for (i = 0; i < size; i++)
        {
        if (*(str + i) == '\n')
        {
                HAL_UART_Transmit(&huart1, (uint8_t *)&a, 1, 1);
        }
                HAL_UART_Transmit(&huart1, (uint8_t *)(str + i), 1, 1);
        }
}

char rt_hw_console_getchar(void)
{
        int ch = -1;
        if (__HAL_UART_GET_FLAG(&huart1, UART_FLAG_RXNE) != RESET)
        {
                ch = huart1.Instance->RDR & 0xff;
        }
        else
        {
                if(__HAL_UART_GET_FLAG(&huart1, UART_FLAG_ORE) != RESET)
                {
                        __HAL_UART_CLEAR_OREFLAG(&huart1);
                }
                rt_thread_mdelay(10);
        }
        return ch;
}
4.3在rt_hw_board_init()中加入MX_USART1_UART_Init();
void rt_hw_board_init()
{
    /* System Clock Update */
    SystemCoreClockUpdate();
    /* 添加串口初始化代码 */
	MX_USART1_UART_Init();
    
    /* System Tick Configuration */
    _SysTick_Config(SystemCoreClock / RT_TICK_PER_SECOND);

    /* 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
}
5.成功

RT-Thread nano 3.1.3 添加控制台与 FinSH

RT-Thread nano 3.1.3 添加控制台与 FinSHRT-Thread nano 3.1.3 添加控制台与 FinSH 超级大洋葱806 发布了9 篇原创文章 · 获赞 2 · 访问量 1143 私信 关注
上一篇:STM32HAL库移植FreeModbus协议


下一篇:STM32学习笔记——USART