• RT-Thread操作系统学习笔记(STM32F103C8T6)标准库(参考野火和江科大例程)----逻辑代码加入操作系统以LED灯闪烁为例


    第一步打开任意能够正常编译下载实现功能的LED灯闪烁的工程模板。这里我用的是江科大的LED灯闪烁的工程模板。

    第二步根据这个链接下载RT-Thread压缩包,解压之后双击安装。

    https://www.rt-thread.org/download/mdk/RealThread.RT-Thread.3.1.5.pack

    第三步查看Keil软件中这个位置和我的是否一致,不一致就是未安装成功,一般不会安装失败。

    第四步将你Keil的安装路径下\ARM\PACK\RealThread处的文件夹整体复制并粘贴到你之前找好的工程中。

    第五步:将你的工程路径\Fire_RT-Thread\RealThread\3.1.5\bsp\_template下的rtconfig.h与

    board.c文件拷贝一份,粘贴在你的工程中的User文件夹下。
    第六步://参考野火的放置方式
    在开发环境里面新建 rtt/source rtt/ports 两个组文件夹,其中 rtt/source
    于存放RealThread\3.1.5\ src 文件夹的内容, rtt/ports 用于存放 RealThread\3.1.5\ libcpu/arm/cortex-m?文件夹的内容,“?”表 示 3 4 或者 7
    如图
    第七步:将上述RealThread\3.1.5\bsp\_template、RealThread\3.1.5\ src、RealThread\3.1.5\ libcpu/arm/cortex-m?、RealThread\3.1.5\ libcpu、RealThread\3.1.5\ inlcude、RealThread\3.1.5\components\finsh等在开发环境中指定一下路径
    第八步:创建board.h文件
    1. #ifndef __BOARD_H__
    2. #define __BOARD_H__
    3. /* STM32 固件库头文件 */
    4. #include "stm32f10x.h"
    5. /* 开发板硬件 bsp 头文件 */
    6. #include "bsp_led.h"
    7. void rt_hw_board_init(void);
    8. void SysTick_Handler(void);
    9. #endif /* __BOARD_H__ */

    第九步:完善工程中的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. * 2021-05-24 the first version
    9. */
    10. /* 开发板硬件相关头文件 */
    11. #include "board.h"
    12. #include <rthw.h>
    13. #include <rtthread.h>
    14. #if defined(RT_USING_USER_MAIN) && defined(RT_USING_HEAP)
    15. /*
    16. * Please modify RT_HEAP_SIZE if you enable RT_USING_HEAP
    17. * the RT_HEAP_SIZE max value = (sram size - ZI size), 1024 means 1024 bytes
    18. */
    19. #define RT_HEAP_SIZE (15*1024)
    20. static rt_uint8_t rt_heap[RT_HEAP_SIZE];
    21. RT_WEAK void *rt_heap_begin_get(void)
    22. {
    23. return rt_heap;
    24. }
    25. RT_WEAK void *rt_heap_end_get(void)
    26. {
    27. return rt_heap + RT_HEAP_SIZE;
    28. }
    29. #endif
    30. void rt_os_tick_callback(void)
    31. {
    32. rt_interrupt_enter();
    33. rt_tick_increase();
    34. rt_interrupt_leave();
    35. }
    36. /**
    37. * This function will initial your board.
    38. */
    39. void rt_hw_board_init(void)
    40. {
    41. //#error "TODO 1: OS Tick Configuration."
    42. // /*
    43. // * TODO 1: OS Tick Configuration
    44. // * Enable the hardware timer and call the rt_os_tick_callback function
    45. // * periodically with the frequency RT_TICK_PER_SECOND.
    46. // */
    47. // /* Call components board initial (use INIT_BOARD_EXPORT()) */
    48. /* 初始化 SysTick *///这一句是加入的
    49. SysTick_Config( SystemCoreClock / RT_TICK_PER_SECOND );
    50. //下面放置硬件初始化
    51. bsp_led_Init();
    52. #ifdef RT_USING_COMPONENTS_INIT
    53. rt_components_board_init();
    54. #endif
    55. #if defined(RT_USING_USER_MAIN) && defined(RT_USING_HEAP)
    56. rt_system_heap_init(rt_heap_begin_get(), rt_heap_end_get());
    57. #endif
    58. }
    59. #ifdef RT_USING_CONSOLE
    60. static int uart_init(void)
    61. {
    62. #error "TODO 2: Enable the hardware uart and config baudrate."
    63. return 0;
    64. }
    65. INIT_BOARD_EXPORT(uart_init);
    66. void rt_hw_console_output(const char *str)
    67. {
    68. #error "TODO 3: Output the string 'str' through the uart."
    69. }
    70. #endif
    71. //这是加入的
    72. /**
    73. * @brief SysTick中断服务函数
    74. * @param 无
    75. * @retval 无
    76. *
    77. * @attention
    78. * SysTick中断服务函数在固件库文件stm32f10x_it.c中也定义了,而现在
    79. * 在board.c中又定义一次,那么编译的时候会出现重复定义的错误,解决
    80. * 方法是可以把stm32f10x_it.c中的注释或者删除即可。
    81. */
    82. void SysTick_Handler(void)
    83. {
    84. /* 进入中断 */
    85. rt_interrupt_enter();
    86. /* 更新时基 */
    87. rt_tick_increase();
    88. /* 离开中断 */
    89. rt_interrupt_leave();
    90. }

    下面就是主函数的测试程序LED灯闪烁

    1. #include "board.h"
    2. #include "rtthread.h"
    3. /*定义线程控制块*/
    4. static struct rt_thread led1_thread;
    5. /* 定义线程控栈时要求 RT_ALIGN_SIZE 个字节对齐 */
    6. ALIGN(RT_ALIGN_SIZE)
    7. /* 定义线程栈 */
    8. static rt_uint8_t rt_led1_thread_stack[1024];
    9. //声明线程函数
    10. static void led1_thread_entry(void* parameter);
    11. int main()
    12. {
    13. //硬件初始化不需要在主函数中显式调用
    14. /*
    15. * 开发板硬件初始化,RTT 系统初始化已经在 main 函数之前完成,
    16. * 即在 component.c 文件中的 rtthread_startup()函数中完成了。
    17. * 所以在 main 函数中,只需要创建线程和启动线程即可。
    18. */
    19. //初始化线程
    20. rt_thread_init(&led1_thread, /*线程控制块*/
    21. "led1", /*线程名字*/
    22. led1_thread_entry, /*线程入口函数*/
    23. RT_NULL, /*线程入口函数参数*/
    24. &rt_led1_thread_stack[0],/*线程栈起始地址*/
    25. sizeof(rt_led1_thread_stack),/*线程栈大小*/
    26. 3, /*线程的优先级*/
    27. 20); /*线程时间片*/
    28. //启动线程
    29. rt_thread_startup(&led1_thread);
    30. }
    31. //定义线程函数
    32. static void led1_thread_entry(void* parameter)
    33. {
    34. while(1)
    35. {
    36. bsp_led_move(0);
    37. rt_thread_delay(500);/* 延时 500 个 tick 操作系统中的阻塞延时*/
    38. bsp_led_move(1);
    39. rt_thread_delay(500);/* 延时 500 个 tick */
    40. }
    41. }

  • 相关阅读:
    Zebec联合Visa推出实体借记卡持续利好生态,$ZBC表现强劲
    【毕业季·进击的技术er】大学生计算机毕业设计应该这样写
    实验室信息管理系统设计原理
    Linux:线程互斥与同步 | 生产者消费者模型 | 线程伪唤醒、唤醒丢失 | 死锁
    [附源码]java毕业设计基于的旅游信息管理系统
    (免费分享)基于springboot健康运动-带论文
    精准用户画像!商城用户分群2.0!
    G1D16-fraud-SVM
    云原生2.0网关API标准发展趋势
    11.11练习题
  • 原文地址:https://blog.csdn.net/Cola_psoda/article/details/133212844