找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 3332|回復(fù): 2
打印 上一主題 下一主題
收起左側(cè)

STM32 CubeIDE 使用RT-Thread Nano

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
#
ID:606388 發(fā)表于 2020-9-24 21:44 | 只看該作者 回帖獎勵 |正序瀏覽 |閱讀模式
本帖最后由 qingyemurong 于 2020-9-24 22:13 編輯

STM32 CubeIDE 使用RT-Thread Nano

使用的 STM32 CubeIDE 版本 1.4
  在STM32 CubeIDE中已經(jīng)集成了RT-Thread Nano,可以直接在 IDE 中進(jìn)行下載添加。工程文件附件自己下載。

  • # 1、RT-Thread Nano pack 安裝
打開 STM32 CubeIDE   --------->**Software Packs**     ------------>**Manager Software Packs**界面(

  獲取 RT-Thread Nano 軟件包,需要在 STM32CubeIDE 中添加 [https://www.rt-thread.org/download/cube/RealThread.RT-Thread.pdsc](https://www.rt-thread.org/download/cube/RealThread.RT-Thread.pdsc)


回到 Manage software packages 界面,就會發(fā)現(xiàn) RT-Thread Nano 3.1.3 軟件包,選擇該軟件包,點擊 Install Now,如下圖所示(顏色填充表示已安裝)(


(
  • # 2、創(chuàng)建工程添加 RT-Thread Nano
## 2.1 、創(chuàng)建一個基本工程
創(chuàng)建一個基本的工程文件,包含2個LED燈和USART1。





(

#
  • # 2.2、配置 Nano


勾選 RT-Thread
(?x-oss
  • **適配 RT-Thread Nano**
中斷與異常處理
RT-Thread 操作系統(tǒng)重定義 **HardFault_Handler**、**PendSV_Handler**、**SysTick_Handler** 中斷函數(shù),為了避免重復(fù)定義的問題,在生成工程之前,需要在中斷配置中,代碼生成的選項中,取消選擇三個中斷函數(shù)(對應(yīng)注釋選項是 Hard fault interrupt, Pendable request, Time base :System tick timer),最后點擊生成代碼,具體操作如下圖中

(

# 3、工程代碼修改
  • ## 3.1 需要修改的部分
1 、修改啟動文件  startup_stm32f103rctx.s
**bl main**  修改為  **bl  entry**     
(

  • ## 3.2 、配置rt_kprintf端口輸出
端口映射,函數(shù)可以放在main.c文件里面。
(


(

  1. /* USER CODE BEGIN 4 */
  2. char rt_hw_console_getchar(void)
  3. {
  4.         int ch = -1;
  5.         if (__HAL_UART_GET_FLAG(&huart1, UART_FLAG_RXNE) != RESET)
  6.         {
  7.                 ch = huart1.Instance->DR & 0xff;
  8.         }
  9.         else
  10.         {
  11.                 if (__HAL_UART_GET_FLAG(&huart1, UART_FLAG_ORE) != RESET)
  12.                 {
  13.                         __HAL_UART_CLEAR_OREFLAG(&huart1);
  14.                 }
  15.                 rt_thread_mdelay(10);
  16.         }
  17.         return ch;
  18. }
  19. void rt_hw_console_output(const char *str)
  20. {
  21.         rt_size_t i = 0, size = 0;
  22.         char a = '\r';
  23.         __HAL_UNLOCK(&huart1);
  24.         size = rt_strlen(str);
  25.         for (i = 0; i < size; i++)
  26.         {
  27.                 if (*(str + i) == '\n')
  28.                 {
  29.                         ITM_SendChar(a);
  30.                         HAL_UART_Transmit(&huart1, (uint8_t*) &a, 1, 1);
  31.                 }
  32.                 HAL_UART_Transmit(&huart1, (uint8_t*) (str + i), 1, 1);
  33.         }
  34. }

  35. /* USER CODE END 4 */
復(fù)制代碼


## 3.3 、編寫線程文件
創(chuàng)建一個**app_rt_thread.c**文件用于保存線程代碼
(


app_rt_thread.c文件內(nèi)容:

  1. #include "rtthread.h"
  2. #include "main.h"
  3. #include "stdio.h"
  4. #include <finsh.h>        


  5. /* 定義線程控制塊 */
  6. //添加LED閃爍線程
  7. static struct rt_thread led_thread;
  8. static char led_thread_stack[256];
  9. static void led_thread_entry(void *parameter);
  10. int MX_RT_Thread_Init(void);

  11. int MX_RT_Thread_Init(void)
  12. {
  13.         //初始化線程
  14.         rt_err_t rst;
  15.         rst = rt_thread_init(&led_thread,
  16.                                                 (const char *)"ledshine",  /* 線程名字 */
  17.                                                 led_thread_entry,  /* 線程入口函數(shù) */
  18.                                                 RT_NULL,           /* 線程入口函數(shù)參數(shù) */
  19.                                                 &led_thread_stack[0],
  20.                                                 sizeof(led_thread_stack),   /* 線程棧大小 */
  21.                                                 RT_THREAD_PRIORITY_MAX-2,  /* 線程的優(yōu)先級 */
  22.                                                 20); /* 線程時間片 */
  23.         if(rst == RT_EOK)
  24.         {///* 啟動線程,開啟調(diào)度 */
  25.                 rt_thread_startup(&led_thread);
  26.         }

  27. }


  28. /*
  29. *************************************************************************
  30. * 線程定義
  31. *************************************************************************
  32. */
  33. static void led_thread_entry(void *parameter)
  34. {
  35.         while(1)
  36.         {
  37.                 rt_kprintf("led1_thread running,LED1_ON\r\n");
  38.                 HAL_GPIO_WritePin(GPIOA, GPIO_PIN_8, GPIO_PIN_RESET);
  39.                 rt_thread_mdelay(500);
  40.                 HAL_GPIO_WritePin(GPIOA, GPIO_PIN_8, GPIO_PIN_SET);
  41.                 rt_thread_mdelay(500);
  42.         }
  43. }

  44. MSH_CMD_EXPORT(led_thread_entry,thread running);
復(fù)制代碼


  • ## 3.4 、main.c 修改
  • (

  1. /* USER CODE BEGIN Includes */
  2. #include "rtthread.h"

  3. extern int MX_RT_Thread_Init(void);
復(fù)制代碼





(

  1. int main(void)
  2. {
  3.   /* USER CODE BEGIN 1 */

  4.   /* USER CODE END 1 */

  5.   /* MCU Configuration--------------------------------------------------------*/

  6.   /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  7.   HAL_Init();

  8.   /* USER CODE BEGIN Init */

  9.   /* USER CODE END Init */

  10.   /* Configure the system clock */
  11.   SystemClock_Config();

  12.   /* USER CODE BEGIN SysInit */

  13.   /* USER CODE END SysInit */

  14.   /* Initialize all configured peripherals */
  15.   MX_GPIO_Init();
  16.   MX_USART1_UART_Init();
  17.   /* USER CODE BEGIN 2 */
  18.   MX_RT_Thread_Init();
  19.   /* USER CODE END 2 */

  20.   /* Infinite loop */
  21.   /* USER CODE BEGIN WHILE */
  22.   while (1)
  23.   {
  24.           HAL_GPIO_TogglePin(GPIOD, GPIO_PIN_2);
  25.           rt_kprintf("led1_thread TEST\r\n");
  26.           rt_thread_mdelay(100);
  27.     /* USER CODE END WHILE */

  28.     /* USER CODE BEGIN 3 */
  29.   }
  30.   /* USER CODE END 3 */
  31. }
復(fù)制代碼


串口輸出:
(






RT_Thread.7z

1.61 MB, 下載次數(shù): 15, 下載積分: 黑幣 -5

工程文件

分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩
回復(fù)

使用道具 舉報

沙發(fā)
ID:688008 發(fā)表于 2021-8-2 08:29 | 只看該作者
學(xué)習(xí)一下,后續(xù)開始搞rt
回復(fù)

使用道具 舉報

樓主
ID:538806 發(fā)表于 2021-6-21 16:56 | 只看該作者
好用嗎?有沒有bug啊
回復(fù)

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規(guī)則

手機(jī)版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表