[牛人杂谈] RT-Thread在NUC029上运行线程

[复制链接]
1794|16
 楼主| gaoyang9992006 发表于 2020-8-1 15:32 | 显示全部楼层 |阅读模式

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

这个是main.c内容

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


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

  14.     /*-----------------------------------------------------------------------------------------------------*/
  15.     /* GPIO Basic Mode Test --- Use Pin Data Input/Output to control GPIO pin                              */
  16.     /*-----------------------------------------------------------------------------------------------------*/

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

  19.     /* Use Pin Data Input/Output Control to pull specified I/O or get I/O pin status */
  20.         rt_kprintf("Hello RTT_NANO\n");
  21.         while(1)
  22.                 {
  23.                         PC5=0;
  24.                         rt_thread_mdelay(3333);
  25.                         printf("\nLED is ON\n");
  26.                         PC5=1;
  27.                         rt_thread_mdelay(3333);
  28.                         printf("\nLED is OFF\n");       
  29.                 }
  30. //return 0;
  31. }
  32. /* 导出到 msh 命令列表中 */
  33. MSH_CMD_EXPORT(led, RT-Thread first led sample);

  34. void led2(void *parameter)
  35. {


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

  38.     /* Use Pin Data Input/Output Control to pull specified I/O or get I/O pin status */
  39.         rt_kprintf("Hello RTT_NANO\n");
  40.         while(1)
  41.                 {
  42.                         PC5=0;
  43.                         rt_thread_mdelay(2000);
  44.                         printf("\nLED2 is ON\n");
  45.                         PC5=1;
  46.                         rt_thread_mdelay(2000);
  47.                         printf("\nLED2 is OFF\n");       
  48.                 }
  49. //return 0;
  50. }

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

  52. int led_sample(void)
  53. {
  54. static rt_thread_t tid = RT_NULL;
  55. static rt_thread_t tid2 = RT_NULL;
  56.         /* 创建线程1 */
  57.     tid = rt_thread_create("thread1",
  58.                             led, RT_NULL,
  59.                             THREAD_STACK_SIZE,
  60.                             THREAD_PRIORITY, THREAD_TIMESLICE);
  61.    
  62.     if (tid != RT_NULL)      
  63.         rt_thread_startup(tid);

  64.         /* 创建线程2 */
  65.     tid2 = rt_thread_create("thread2",
  66.                             led2, RT_NULL,
  67.                             THREAD_STACK_SIZE,
  68.                             THREAD_PRIORITY, THREAD_TIMESLICE);
  69.    
  70.     if (tid2 != RT_NULL)      
  71.         rt_thread_startup(tid2);               
  72.                 //该例子共用PC5端口,所以一并在创建线程时候初始化为输出模式
  73.                 GPIO_SetMode(PC, BIT5, GPIO_MODE_OUTPUT);               
  74.     return 0;
  75. }
  76. MSH_CMD_EXPORT(led_sample, RT-Thread sample);
  77. /*---------------------------------------------------------------------------------------------------------*/
  78. /*  Main Function                                                                                          */
  79. /*---------------------------------------------------------------------------------------------------------*/
  80. int32_t main(void)
  81. {
  82.         led_sample();
  83.         return 0;
  84. }
这个是rtconfig.h内容
  1. /* RT-Thread config file */

  2. #ifndef __RTTHREAD_CFG_H__
  3. #define __RTTHREAD_CFG_H__

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

  6. //#if defined(RTE_USING_FINSH)
  7. #define RT_USING_FINSH
  8. //#endif //RTE_USING_FINSH

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

  10. // <<< Use Configuration Wizard in Context Menu >>>
  11. // <h>Basic Configuration
  12. // <o>Maximal level of thread priority <8-256>
  13. //  <i>Default: 32
  14. #define RT_THREAD_PRIORITY_MAX  8
  15. // <o>OS tick per second
  16. //  <i>Default: 1000   (1ms)
  17. #define RT_TICK_PER_SECOND  1000
  18. // <o>Alignment size for CPU architecture data access
  19. //  <i>Default: 4
  20. #define RT_ALIGN_SIZE   4
  21. // <o>the max length of object name<2-16>
  22. //  <i>Default: 8
  23. #define RT_NAME_MAX    8
  24. // <c1>Using RT-Thread components initialization
  25. //  <i>Using RT-Thread components initialization
  26. #define RT_USING_COMPONENTS_INIT
  27. // </c>

  28. #define RT_USING_USER_MAIN

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

  32. // </h>

  33. // <h>Debug Configuration
  34. // <c1>enable kernel debug configuration
  35. //  <i>Default: enable kernel debug configuration
  36. #define RT_DEBUG
  37. // </c>
  38. // <o>enable components initialization debug configuration<0-1>
  39. //  <i>Default: 0
  40. #define RT_DEBUG_INIT 0
  41. // <c1>thread stack over flow detect
  42. //  <i> Diable Thread stack over flow detect
  43. //#define RT_USING_OVERFLOW_CHECK
  44. // </c>
  45. // </h>

  46. // <h>Hook Configuration
  47. // <c1>using hook
  48. //  <i>using hook
  49. #define RT_USING_HOOK
  50. // </c>
  51. // <c1>using idle hook
  52. //  <i>using idle hook
  53. //#define RT_USING_IDLE_HOOK
  54. // </c>
  55. // </h>

  56. // <e>Software timers Configuration
  57. // <i> Enables user timers
  58. #define RT_USING_TIMER_SOFT         0
  59. #if RT_USING_TIMER_SOFT == 0
  60.     #undef RT_USING_TIMER_SOFT
  61. #endif
  62. // <o>The priority level of timer thread <0-31>
  63. //  <i>Default: 4
  64. #define RT_TIMER_THREAD_PRIO        4
  65. // <o>The stack size of timer thread <0-8192>
  66. //  <i>Default: 512
  67. #define RT_TIMER_THREAD_STACK_SIZE  512
  68. // </e>

  69. // <h>IPC(Inter-process communication) Configuration
  70. // <c1>Using Semaphore
  71. //  <i>Using Semaphore
  72. #define RT_USING_SEMAPHORE
  73. // </c>
  74. // <c1>Using Mutex
  75. //  <i>Using Mutex
  76. //#define RT_USING_MUTEX
  77. // </c>
  78. // <c1>Using Event
  79. //  <i>Using Event
  80. //#define RT_USING_EVENT
  81. // </c>
  82. // <c1>Using MailBox
  83. //  <i>Using MailBox
  84. #define RT_USING_MAILBOX
  85. // </c>
  86. // <c1>Using Message Queue
  87. //  <i>Using Message Queue
  88. //#define RT_USING_MESSAGEQUEUE
  89. // </c>
  90. // </h>

  91. // <h>Memory Management Configuration
  92. // <c1>Dynamic Heap Management
  93. //  <i>Dynamic Heap Management
  94. #define RT_USING_HEAP
  95. // </c>
  96. // <c1>using small memory
  97. //  <i>using small memory
  98. #define RT_USING_SMALL_MEM
  99. // </c>
  100. // <c1>using tiny size of memory
  101. //  <i>using tiny size of memory
  102. //#define RT_USING_TINY_SIZE
  103. // </c>
  104. // </h>

  105. // <h>Console Configuration
  106. // <c1>Using console
  107. //  <i>Using console
  108. #define RT_USING_CONSOLE
  109. // </c>
  110. // <o>the buffer size of console <1-1024>
  111. //  <i>the buffer size of console
  112. //  <i>Default: 128  (128Byte)
  113. #define RT_CONSOLEBUF_SIZE          128
  114. // </h>

  115. #if defined(RT_USING_FINSH)
  116.     #define FINSH_USING_MSH
  117.     #define FINSH_USING_MSH_ONLY
  118.     // <h>Finsh Configuration
  119.     // <o>the priority of finsh thread <1-7>
  120.     //  <i>the priority of finsh thread
  121.     //  <i>Default: 6
  122.     #define __FINSH_THREAD_PRIORITY     5
  123.     #define FINSH_THREAD_PRIORITY       (RT_THREAD_PRIORITY_MAX / 8 * __FINSH_THREAD_PRIORITY + 1)
  124.     // <o>the stack of finsh thread <1-4096>
  125.     //  <i>the stack of finsh thread
  126.     //  <i>Default: 4096  (4096Byte)
  127.     #define FINSH_THREAD_STACK_SIZE     512
  128.     // <o>the history lines of finsh thread <1-32>
  129.     //  <i>the history lines of finsh thread
  130.     //  <i>Default: 5
  131.     #define FINSH_HISTORY_LINES         1

  132.     #define FINSH_USING_SYMTAB
  133.     // </h>
  134. #endif

  135. // <<< end of configuration section >>>

  136. #endif
这个是board.c内容
  1. /*
  2. * Copyright (c) 2006-2019, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date           Author       Notes
  8. * 2017-07-24     Tanek        the first version
  9. * 2018-11-12     Ernest Chen  modify copyright
  10. */
  11. #include <stdio.h>
  12. #include <stdint.h>
  13. #include <rthw.h>
  14. #include <rtthread.h>
  15. //#include "uart.h"
  16. #include "NUC029xGE.h"
  17. #define PLL_CLOCK       72000000

  18. #define _SCB_BASE       (0xE000E010UL)
  19. #define _SYSTICK_CTRL   (*(rt_uint32_t *)(_SCB_BASE + 0x0))
  20. #define _SYSTICK_LOAD   (*(rt_uint32_t *)(_SCB_BASE + 0x4))
  21. #define _SYSTICK_VAL    (*(rt_uint32_t *)(_SCB_BASE + 0x8))
  22. #define _SYSTICK_CALIB  (*(rt_uint32_t *)(_SCB_BASE + 0xC))
  23. #define _SYSTICK_PRI    (*(rt_uint8_t  *)(0xE000ED23UL))

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

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

  31. static uint32_t _SysTick_Config(rt_uint32_t ticks)
  32. {
  33.     if ((ticks - 1) > 0xFFFFFF)
  34.     {
  35.         return 1;
  36.     }
  37.    
  38.     _SYSTICK_LOAD = ticks - 1;
  39.     _SYSTICK_PRI = 0xFF;
  40.     _SYSTICK_VAL  = 0;
  41.     _SYSTICK_CTRL = 0x07;  
  42.    
  43.     return 0;
  44. }

  45. #if defined(RT_USING_USER_MAIN) && defined(RT_USING_HEAP)
  46. #define RT_HEAP_SIZE 1024
  47. static uint32_t rt_heap[RT_HEAP_SIZE];     // heap default size: 4K(1024 * 4)
  48. RT_WEAK void *rt_heap_begin_get(void)
  49. {
  50.     return rt_heap;
  51. }

  52. RT_WEAK void *rt_heap_end_get(void)
  53. {
  54.     return rt_heap + RT_HEAP_SIZE;
  55. }
  56. #endif
  57. /*自己添加跟硬件有关的时钟初始化*/
  58. static void rt_hw_system_init(void)
  59. {
  60.     /*---------------------------------------------------------------------------------------------------------*/
  61.     /* Init System Clock                                                                                       */
  62.     /*---------------------------------------------------------------------------------------------------------*/
  63.     SYS_UnlockReg();

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

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

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

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

  74.     SYS_LockReg();
  75. }
  76. /*自己添加跟串口有关的时钟初始化*/
  77. static void rt_hw_usart_init()
  78. {
  79.     /* Unlock protected registers */
  80.     SYS_UnlockReg();

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

  83.     /* Wait for HXT clock ready */
  84.     CLK_WaitClockReady(CLK_STATUS_HXTSTB_Msk);
  85.   
  86.     /* Enable UART module clock */
  87.     CLK_EnableModuleClock(UART0_MODULE);

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

  90.     /*---------------------------------------------------------------------------------------------------------*/
  91.     /* Init I/O Multi-function                                                                                 */
  92.     /*---------------------------------------------------------------------------------------------------------*/

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

  96.     /* Lock protected registers */
  97.     SYS_LockReg();
  98.                
  99.                     /* Reset UART0 */
  100.     SYS_ResetModule(UART0_RST);

  101.     /* Configure UART0 and set UART0 baud rate */
  102.     UART_Open(UART0, 115200);
  103. }

  104. /**
  105. * This function will initial your board.
  106. */
  107. void rt_hw_board_init()
  108. {
  109.     /* Configure the system clock */
  110.     rt_hw_system_init();

  111.                 /* System Clock Update */
  112.     SystemCoreClockUpdate();
  113.    
  114.     /* System Tick Configuration */
  115.     _SysTick_Config(SystemCoreClock / RT_TICK_PER_SECOND);

  116.         /* Initial usart deriver, and set console device */
  117.                 rt_hw_usart_init();       
  118.        
  119.     /* Call components board initial (use INIT_BOARD_EXPORT()) */
  120. #ifdef RT_USING_COMPONENTS_INIT
  121.     rt_components_board_init();
  122. #endif

  123. #if defined(RT_USING_USER_MAIN) && defined(RT_USING_HEAP)
  124.     rt_system_heap_init(rt_heap_begin_get(), rt_heap_end_get());
  125. #endif
  126. }

  127. void SysTick_Handler(void)
  128. {
  129.     /* enter interrupt */
  130.     rt_interrupt_enter();

  131.     rt_tick_increase();

  132.     /* leave interrupt */
  133.     rt_interrupt_leave();
  134. }
  135. /*自己添加对接串口控制台*/
  136. void rt_hw_console_output(const char *str)
  137. {
  138.         printf("%s",str);
  139. }

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

 楼主| gaoyang9992006 发表于 2020-8-1 15:33 | 显示全部楼层
 楼主| gaoyang9992006 发表于 2020-8-1 15:33 | 显示全部楼层
我这是直接将创建线程函数放main里面上电就执行了,你也可以不放进去,然后通过msh指令执行。
 楼主| gaoyang9992006 发表于 2020-8-1 15:34 | 显示全部楼层
 楼主| gaoyang9992006 发表于 2020-8-1 15:35 | 显示全部楼层
87005f251b224f44a.png
执行的时候也可以发送msh命令
 楼主| gaoyang9992006 发表于 2020-8-1 15:36 | 显示全部楼层
119705f251b470867a.png
发送help命令,可以看到MSH列表什么可以使用的指令。
huangcunxiake 发表于 2020-8-2 12:22 | 显示全部楼层
给楼主点赞,多谢分享。
小灵通2018 发表于 2020-8-2 19:58 | 显示全部楼层
真不错,学习学习
幸福小强 发表于 2020-8-3 19:38 | 显示全部楼层
跑的很6,学了一招高级的。
wanduzi 发表于 2020-8-3 19:45 | 显示全部楼层
连续两个高水平贴,给力。
21mengnan 发表于 2020-8-3 21:29 | 显示全部楼层
跟楼主学技术,楼主继续更。
21mengnan 发表于 2020-8-3 21:34 | 显示全部楼层
动态线程,应该一个静态的更换。
xinxianshi 发表于 2020-8-3 23:35 | 显示全部楼层
给力,简单好学。
wahahaheihei 发表于 2020-8-3 23:35 | 显示全部楼层
能弄到51上用吗
xixi2017 发表于 2020-8-4 20:05 | 显示全部楼层
直接使用。多谢。
yiyigirl2014 发表于 2020-8-4 22:02 | 显示全部楼层
前面三个宏大小如何设置。没太懂,该设置多少合适。
jiekou001 发表于 2020-8-6 11:22 | 显示全部楼层
前来学习。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

个人签名:如果你觉得我的分享或者答复还可以,请给我点赞,谢谢。

2053

主题

16417

帖子

222

粉丝
快速回复 在线客服 返回列表 返回顶部