前言
- 最近STM32的芯片价格高的离谱,当然开发板也不例外,最近翻出来一块【火牛STM32F103VCT6】的开发板,吃灰很多年了,那就擦擦,跑跑RT-Thread,主要是我看上面有个【LCD】,想搞下【LVGL】显示
- 经过擦拭一番,看上去像新的一样,买来没怎么研究,记得就被工作与其他的开发板【淹没了】
上电
- 开发板的资料找不到了,还好在网上找到了【原理图】,有了原理图,就好办多了
- 插入电源,发现电源【指示LED】不亮,用万用表测量,发现没有供电过去,看了一下原理图,发现需要一个跳线,跳线后,发现【开发板】电源LED红灯亮起,上电成功了
- 使用的CN10【牛角座】5V 直流电源供电
- 排针:J5 需要跳线,才能正常的供电
开发环境
- Win10 64位
- Keil MDK5
- 【火牛开发板 STM32F103VCT6】
- USB 转串口线(CH340),这里使用RS232的串口,注意区分 TTL电平的
- 5V 直流电源,用于给开发板【牛角座】供电,当然可以使用USB线供电
- J-Link V9,用于调试与下载程序,新的Keil MDK5,对J-Link V8 支持的不够友好
RT-Thread 移植
- RT-Thread 对 STM32支持的很好,STM32系列的开发板,基本上很容易适配,也就是简单的配置,就可以让开发板运行RT-Thread 操作系统
- 首先更新 RT-Thread 到最新版本,这里使用gitee的仓库,更新起来很快,整个仓库很大,主要是BSP目录下支持众多的开发板
git clone https://gitee.com/rtthread/rt-thread.git
- RT-Thread 的官方网站:https://www.rt-thread.org/
- RT-Thread 的文档中心:https://www.rt-thread.org/document/site/#/
- RT-Thread 的社区:https://club.rt-thread.org/index.html
- RT-Thread 的ENV工具、RT-Thread Studio等的下载地址:https://www.rt-thread.org/page/download.html
- 我这里使用Keil MDK5.30,当然可以使用:RT-Thread Studio开发,类似于eclipse gcc 开发环境,对各类开发板支持还不错
工程搭建
-
直接使用 git clone 下来的RT-Thread 源码包,发现体积太大,所以这里重新搭建一个最小环境,BSP只需要 STM32F103系列的,其他的不需要
-
最小系统的搭建流程,可以参考我之前的【文章】,简单操作如下
-
创建三个目录即可:
- rt-thread:不包括 BSP、git相关的目录
- libraries:STM32F103系列的HAL库与Drivers
- led:这里从BSP目录中复制一个F103系列的,改下MCU型号,连接脚本改下Flash与SRAM的大小
- 工程目录如下,其他的外设功能,都重新建一个,公用相同的libraries与rt-thread操作系统代码
-
链接脚本的修改,我这里从STM32F103ZE 改为:STM32F103VC,需要调整Flash与SRAM的大小,RT-Thread 中,使用链接脚本,Keil MDK如:
board\linker_scripts\link.sct
-
修改MUC的型号,这里修改的是:
template.uvprojx
这个功能模板,因为使用RT-Thread ENV工具,scons 构建工程后,是从这个工程模板生成的,所以改这个工程模板
- 调试改为:J-Link
- 调试改为:J-Link SW模式
- 保存模板工程即可
修复构建路径
-
因为工程的目录调整了,所以需要根据目录调整一下,否则RT-Thread ENV 工具 menuconfig 或 scons 构建会报错
-
修改工程的BSP目录下的
Kconfig
-
修改工程的BSP目录下的
SConstruct
构建与编译
- RT-Thread ENV 工具:
menuconfig
,保存 - 然后:scons --target=mdk5构建 Keil MDK5工程
- 编译下载,发现LED默认配置的跟开发板不一致,串口 shell可以工作
LED 驱动
- 查看原理图,配置正确的LED引脚
- led.c
#include <led.h>
#include <board.h>
int led_gpio_init(void)
{
/* set LED0 pin mode to output */
rt_pin_mode(LED1_PIN, PIN_MODE_OUTPUT);
rt_pin_mode(LED2_PIN, PIN_MODE_OUTPUT);
rt_pin_mode(LED3_PIN, PIN_MODE_OUTPUT);
rt_pin_mode(LED4_PIN, PIN_MODE_OUTPUT);
return 0;
}
void led1_power_on(rt_uint8_t bon)
{
if (bon == 0x00)
{
rt_pin_write(LED1_PIN, PIN_LOW);
}
else
{
rt_pin_write(LED1_PIN, PIN_HIGH);
}
}
void led2_power_on(rt_uint8_t bon)
{
if (bon == 0x00)
{
rt_pin_write(LED2_PIN, PIN_LOW);
}
else
{
rt_pin_write(LED2_PIN, PIN_HIGH);
}
}
void led3_power_on(rt_uint8_t bon)
{
if (bon == 0x00)
{
rt_pin_write(LED3_PIN, PIN_LOW);
}
else
{
rt_pin_write(LED3_PIN, PIN_HIGH);
}
}
void led4_power_on(rt_uint8_t bon)
{
if (bon == 0x00)
{
rt_pin_write(LED4_PIN, PIN_LOW);
}
else
{
rt_pin_write(LED4_PIN, PIN_HIGH);
}
}
- led.h
#ifndef __LED_H__
#define __LED_H__
#include <rtthread.h>
/* defined the LED1 pin: PD8 */
#define LED1_PIN GET_PIN(D, 8)
/* defined the LED2 pin: PD9 */
#define LED2_PIN GET_PIN(D, 9)
/* defined the LED3 pin: PD10 */
#define LED3_PIN GET_PIN(D, 10)
/* defined the LED4 pin: PD11 */
#define LED4_PIN GET_PIN(D, 11)
int led_gpio_init(void);
void led1_power_on(rt_uint8_t bon);
void led2_power_on(rt_uint8_t bon);
void led3_power_on(rt_uint8_t bon);
void led4_power_on(rt_uint8_t bon);
#endif
LED点灯
- 配置好LED引脚后,在main函数里执行点灯即可(备注:这个main函数,其实是个RT-Thread的线程,main 线程)
#include <rtthread.h>
#include <rtdevice.h>
#include <board.h>
#include "key.h"
#include "led.h"
#include "joystick.h"
int main(void)
{
led_gpio_init();
key_gpio_init();
joystick_gpio_init();
while (1)
{
led1_power_on(1);
rt_thread_mdelay(1000);
led1_power_on(0);
rt_thread_mdelay(1000);
}
}
- 测试发现LED 正常的闪烁起来,最小工程搭建完成
串口 shell
- 连接好串口1,我使用的USB转RS232串口线
\ | /
- RT - Thread Operating System
/ | \ 4.1.1 build May 8 2022 16:12:24
2006 - 2022 Copyright by RT-Thread team
[D/key] key_gpio_init.
[D/key] PIN_KEY0=45,PIN_KEY1=0,PIN_KEY2=8
[D/joystick] joystick_gpio_init.
msh >
msh >help
RT-Thread shell commands:
reboot - Reboot System
help - RT-Thread shell help.
ps - List threads in the system.
free - Show the memory usage in the system.
clear - clear the terminal screen
version - show RT-Thread version information
list_thread - list thread
list_sem - list semaphore in system
list_event - list event in system
list_mutex - list mutex in system
list_mailbox - list mail box in system
list_msgqueue - list message queue in system
list_memheap - list memory heap in system
list_mempool - list memory pool in system
list_timer - list timer in system
list_device - list device in system
- 串口正常跑起来了
小结
- 搭建【火牛开发板】STM32F103VCT6 的RT-Thread 开发调试环境
- 后面研究一下其他的外设,如点亮LCD等