STM32基于HAL库RT-Thread Demo测试

STM32基于HAL库RT-Thread Demo测试


  • ✨在STM32上使用推荐使用RT-Thread Nano版本。
  • 🌿如果仅仅是想使用此系统框架,那么就不需要按照官方介绍的复杂配置,只需拷贝一个工程模板来使用即可。

  • 📍ARM.Keil官网提供的rt-thread开发框架例程资源:https://www.keil.arm.com/boards/?q=&vendor=stmicroelectronics&core=Cortex-M3&sort_by=

  • 或者是Keil中直接下载:

  • 🌿按照官方的学习路线:先利用现成的工程案例模拟运行。可参考官方的教程《Keil 模拟器 STM32F103 上手指南

  • 🔖当然你如果手上有对应的STM32开发板,也是可以直接烧录和运行来的。测试方法和仿真模拟是一样的。

🛠Env配置

  • 🌿按照官方提供的ENv安装教程安装完成并设置好后,在项目文件夹内右键通过ConEmu Here打开,输入menuconfig
  • 🌿bsp例程配置CPU核心数
  • 🌿其他外设功能开启设置,按个人需求配置,不同型号的项目例程外设有差异,目前官方所提供的芯片例程,外设配置内容并不是很完善。当然可以按照官方给出的教程创建《STM32 系列 BSP 制作教程


  • 🌿配置好待测试的外设功能后,记得保存配置选项。

  • 🌿已完成项目的配置工作,可以通过Keil直接打开工程进行编译,也可以使用ENv自带的编译工具链,运行 scons 命令即可使用默认的 ARM_GCC 工具链编译 bsp例程。


  • 🔖Keil中编译和下载代码就不介绍了。
  • 📝测试例程:(rt-thread\bsp\stm32\stm32f103-blue-pill
c 复制代码
/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2019-03-08     obito0       first version
 */

#include <rtthread.h>
#include <rtdevice.h>
#include <board.h>

/* defined the LED0 pin: PC13 */
#define LED0_PIN    GET_PIN(E, 5)
#define LED1_PIN    GET_PIN(B, 5)
#define PWM_DEV_NAME        "pwm2"  /* PWM设备名称 */
#define PWM_DEV_CHANNEL1     1       /* PWM通道 */
#define PWM_DEV_CHANNEL2     1       /* PWM通道 */
struct rt_device_pwm* pwm_dev;      /* PWM设备句柄 */
rt_uint32_t period, pulse;

//TIM_HandleTypeDef htim2;
int main(void)
{
    period = 5000;    /* 周期为5ms,单位为纳秒ns */
    pulse = 2000;          /* PWM脉冲宽度值,单位为纳秒ns */
    /* 查找设备 */
    pwm_dev = (struct rt_device_pwm*)rt_device_find(PWM_DEV_NAME);
    /* 设置PWM周期和脉冲宽度 */
	//如果想通过串口命令来开启,就不需要启用下面的注释代码
//    rt_pwm_set(pwm_dev, PWM_DEV_CHANNEL1, period, pulse);
//    rt_pwm_set(pwm_dev, PWM_DEV_CHANNEL2, period, pulse);
    /* 使能设备 */
//    rt_pwm_enable(pwm_dev, 1); //开启PWM输出通道:PA15
//    rt_pwm_enable(pwm_dev, 2);//开启PWM输出通道:PB3
    /* 关闭设备通道 */
//    rt_pwm_disable(pwm_dev, PWM_DEV_CHANNEL);
    /* set LED0 pin mode to output */
    rt_pin_mode(LED0_PIN, PIN_MODE_OUTPUT);
    rt_pin_mode(LED1_PIN, PIN_MODE_OUTPUT);
//    HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_1); //开启PWM输出通道:PA15
//    HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_2); //开启PWM输出通道:PB3
    while(1)
    {
        rt_pin_write(LED0_PIN, PIN_LOW);
        rt_pin_write(LED1_PIN, PIN_HIGH);
        rt_thread_mdelay(500);
        rt_pin_write(LED0_PIN, PIN_HIGH);
        rt_pin_write(LED1_PIN, PIN_LOW);
        rt_thread_mdelay(500);
    }
}
  • 🌿通过串口命令配置对应PWM通道输出:
c 复制代码
[21:36:06.342] O-9[32m[I/I2C] I2C bus [i2c1] regi
[21:36:06.400] stered[0m
[21:36:06.400] 
[21:36:06.400]  \ | /
[21:36:06.400] - RT -     Thread Operating System
[21:36:06.400]  / | \     5.1.0 build Oct 20 2023 20:37:06
[21:36:06.400]  2006 - 2022 Copyright by RT-Thread team
[21:36:06.400] msh >
[21:36:08.932] pwm probe pwm2

[21:36:08.943] pwm probe pwm2
[21:36:08.943] probe pwm2 success
[21:36:08.943] msh >
[21:36:08.943] msh >
[21:36:31.093] pwm enable 2

[21:36:31.104] pwm enable 2
[21:36:31.104] pwm2 channel 2 is enabled success 
[21:36:31.104] msh >
[21:36:31.104] msh >
[21:36:38.840] pwm enable 1

[21:36:38.851] pwm enable 1
[21:36:38.851] pwm2 channel 1 is enabled success 
[21:36:38.851] msh >
[21:36:38.851] msh >
[21:37:26.475] pwm set 1 5000 2000

[21:37:26.487] pwm set 1 5000 2000
[21:37:26.487] pwm info set on pwm2 at channel 1
[21:37:26.487] msh >
[21:37:26.487] msh >
[21:37:32.232] pwm set 2 5000 2000

[21:37:32.243] pwm set 2 5000 2000
[21:37:32.243] pwm info set on pwm2 at channel 2
[21:37:32.243] msh >
[21:37:32.243] msh >
[21:37:52.630] pwm get 1

[21:37:52.640] pwm get 1
[21:37:52.640] Info of device [pwm2] channel [1]:
[21:37:52.640] period      : 5000
[21:37:52.647] pulse       : 2000
[21:37:52.647] Duty cycle  : 40%
[21:37:52.647] msh >
[21:37:52.647] msh >
[21:37:58.043] pwm get 2

[21:37:58.053] pwm get 2
[21:37:58.053] Info of device [pwm2] channel [2]:
[21:37:58.053] period      : 5000
[21:37:58.060] pulse       : 2000
[21:37:58.060] Duty cycle  : 40%
[21:37:58.060] msh >
[21:37:58.060] msh >
  • 📚测试工程
c 复制代码
链接:https://pan.baidu.com/s/1ujR9pf4UA7lqIidIk_BV9w 
提取码:x014
相关推荐
充哥单片机设计1 小时前
【STM32项目开源】基于STM32的人体健康监测系统
stm32·单片机·嵌入式硬件
hazy1k2 小时前
51单片机基础-独立按键
stm32·单片机·嵌入式硬件·51单片机
沐欣工作室_lvyiyi4 小时前
基于单片机的 220v车载逆变电源的设计与制作(论文+图纸)
stm32·单片机·车载逆变器·12v到220v
兆龙电子单片机设计6 小时前
【STM32项目开源】STM32单片机智能农业大棚控制系统
stm32·单片机·物联网·开源·自动化
listhi5209 小时前
基于STM32F407与FT245R芯片实现USB转并口通信时序控制
stm32·单片机·嵌入式硬件
Hello_Embed21 小时前
STM32 环境监测项目笔记(一):DHT11 温湿度传感器原理与驱动实现
c语言·笔记·stm32·单片机·嵌入式软件
炸膛坦客1 天前
Cortex-M3 内核 MCU-STM32F1 开发之路:(一)单片机 MCU 的构成,包括 FLASH 和 SRAM 的区别,以及引脚类型
stm32·单片机·嵌入式硬件
A9better1 天前
嵌入式开发学习日志39——stm32之I2C总线物理层与常用术语
stm32·单片机·嵌入式硬件·学习
充哥单片机设计1 天前
【STM32项目开源】基于STM32的智能衣柜系统
stm32·单片机·嵌入式硬件
刻BITTER1 天前
用CMake 实现U8g2 的 SDL2 模拟环境
c++·stm32·单片机·嵌入式硬件·arduino