1.准备工作
(1) 准备好STM32f103标准库
(2) 安装RT-Thread pack链接:
2.复制文件
将安装好的RT-Thread文件复制到项目中。安装好的RT-Thread文件在
C:Keil_v5ARMPACKRealThreadRT-Thread 在项目中新建RTT文件夹,将上述路径中的文件复制过去。PS:复制过去的文件是只读的,需要取消只读属性。
3.修改项目文件
在项目中新建RTT组,并添加文件,基本文件如图:
4.包含头文件
如果不适用扩展包,则包含如下路径:
..RTTbsp
..RTTinclude
..RTTincludelibc
5.删除自带的中断函数
固件库中包含了如下三个函数需要删除。
void HardFault_Handler(void)
{
/* Go to infinite loop when Hard Fault exception occurs */
while (1)
{
}
}
void PendSV_Handler(void)
{
}
void SysTick_Handler(void)
{
}
6.修改库文件
rtconfig.h修改代码如下:
//#include "RTE_Components.h" board.c
修改文件如下:
/*
* Copyright (c) 2006-2018, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2017-07-24 Tanek the first version
* 2018-11-12 Ernest Chen modify copyright
*/
#include
#include
#include "stm32f10x.h"
#define _SCB_BASE (0xE000E010UL)
#define _SYSTICK_CTRL (*(rt_uint32_t *)(_SCB_BASE + 0x0))
#define _SYSTICK_LOAD (*(rt_uint32_t *)(_SCB_BASE + 0x4))
#define _SYSTICK_VAL (*(rt_uint32_t *)(_SCB_BASE + 0x8))
#define _SYSTICK_CALIB (*(rt_uint32_t *)(_SCB_BASE + 0xC))
#define _SYSTICK_PRI (*(rt_uint8_t *)(0xE000ED23UL))
// Updates the variable SystemCoreClock and must be called
// whenever the core clock is changed during program execution.
extern void SystemCoreClockUpdate(void);
// Holds the system core clock, which is the system clock
// frequency supplied to the SysTick timer and the processor
// core clock.
extern uint32_t SystemCoreClock;
static uint32_t _SysTick_Config(rt_uint32_t ticks)
{
if ((ticks - 1) > 0xFFFFFF)
{
return 1;
}
_SYSTICK_LOAD = ticks - 1;
_SYSTICK_PRI = 0xFF;
_SYSTICK_VAL = 0;
_SYSTICK_CTRL = 0x07;
return 0;
}
#if defined(RT_USING_USER_MAIN) && defined(RT_USING_HEAP)
#define RT_HEAP_SIZE 1536
static uint32_t rt_heap[RT_HEAP_SIZE]; // heap default size: 6K(1536 * 4)
RT_WEAK void *rt_heap_begin_get(void)
{
return rt_heap;
}
RT_WEAK void *rt_heap_end_get(void)
{
return rt_heap + RT_HEAP_SIZE;
}
#endif
/*******************************************************************************
* 函数名 : RCC_Configuration
* 描述 : 设置系统时钟为72MHZ(这个可以根据需要改)
* 输入 : 无
* 输出 : 无
* 返回值 : 无
* 说明 : STM32F107x和STM32F105x系列MCU与STM32F103x系列MCU时钟配置有所不同
*******************************************************************************/
void RCC_Configuration(void)
{
ErrorStatus HSEStartUpStatus; //外部高速时钟(HSE)的工作状态变量
RCC_DeInit(); //将所有与时钟相关的寄存器设置为默认值
RCC_HSEConfig(RCC_HSE_ON); //启动外部高速时钟HSE
HSEStartUpStatus = RCC_WaitForHSEStartUp(); //等待外部高速时钟(HSE)稳定
if(SUCCESS == HSEStartUpStatus) //如果外部高速时钟已经稳定
{
/* Enable Prefetch Buffer */
FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable); //Flash设置
/* Flash 2 wait state */
FLASH_SetLatency(FLASH_Latency_2);
RCC_HCLKConfig(RCC_SYSCLK_Div1); //设置AHB时钟等于系统时钟(1分频)/72MHZ
RCC_PCLK2Config(RCC_HCLK_Div1); //设置APB2时钟和HCLK时钟相等/72MHz(最大为72MHz)
RCC_PCLK1Config(RCC_HCLK_Div2); //设置APB1时钟是HCLK时钟的2分频/36MHz(最大为36MHz)
#ifndef STM32F10X_CL //如果使用的不是STM32F107x或STM32F105x系列MCU,PLL以下配置
RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9); //PLLCLK = 8MHz * 9 = 72 MHz
#else //如果使用的是STM32F107x或STM32F105x系列MCU,PLL以下配置
/***** 配置PLLx *****/
/* PLL2 configuration: PLL2CLK = (HSE / 5) * 8 = 40 MHz */
RCC_PREDIV2Config(RCC_PREDIV2_Div5);
RCC_PLL2Config(RCC_PLL2Mul_8);
RCC_PLL2Cmd(ENABLE); //使能PLL2
while (RCC_GetFlagStatus(RCC_FLAG_PLL2RDY) == RESET);//等待PLL2稳定
/* PLL configuration: PLLCLK = (PLL2 / 5) * 9 = 72 MHz */
RCC_PREDIV1Config(RCC_PREDIV1_Source_PLL2, RCC_PREDIV1_Div5);
RCC_PLLConfig(RCC_PLLSource_PREDIV1, RCC_PLLMul_9);
#endif
RCC_PLLCmd(ENABLE); //使能PLL
while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET); //等待PLL稳定
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK); //设置系统时钟的时钟源为PLL
while(RCC_GetSYSCLKSource() != 0x08); //检查系统的时钟源是否是PLL
RCC_ClockSecuritySystemCmd(ENABLE); //使能系统安全时钟
/* Enable peripheral clocks --------------------------------------------------*/
/* Enable I2C1 and I2C1 clock */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2|RCC_APB1Periph_SPI2, ENABLE);
/* Enable GPIOA GPIOB SPI1 and USART1 clocks */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB
| RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOD
| RCC_APB2Periph_GPIOE | RCC_APB2Periph_AFIO, ENABLE);
}
}
/**
* This function will initial your board.
*/
extern int Image$$RW_IRAM1$$ZI$$Limit;
#define HEAP_BEGIN ((void *)&Image$$RW_IRAM1$$ZI$$Limit)
#define STM32_SRAM_SIZE 64
#define STM32_SRAM_END (0x20000000 + STM32_SRAM_SIZE * 1024)
void rt_hw_board_init()
{
/* System Clock Update */
RCC_Configuration();
GPIO_InitTypeDef gpioInit;
//打开GPIOB的时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOE,ENABLE);
//LED上拉连接GPIOB 12引脚,所以设置如下,推挽输出,Pin12,2MHz输出速度
gpioInit.GPIO_Mode=GPIO_Mode_Out_PP;
gpioInit.GPIO_Pin=GPIO_Pin_5;
gpioInit.GPIO_Speed=GPIO_Speed_2MHz;
GPIO_Init(GPIOB,&gpioInit);
GPIO_Init(GPIOE,&gpioInit);
gpioInit.GPIO_Mode=GPIO_Mode_Out_PP;
gpioInit.GPIO_Pin=GPIO_Pin_12;
gpioInit.GPIO_Speed=GPIO_Speed_2MHz;
GPIO_Init(GPIOB,&gpioInit);
/* System Tick Configuration */
_SysTick_Config(SystemCoreClock / RT_TICK_PER_SECOND) ;
/* Call components board initial (use INIT_BOARD_EXPORT()) */
#ifdef RT_USING_COMPONENTS_INIT
rt_components_board_init();
#endif
#if defined(RT_USING_CONSOLE) && defined(RT_USING_DEVICE)
rt_console_set_device(RT_CONSOLE_DEVICE_NAME);
#endif
#if defined(RT_USING_USER_MAIN) && defined(RT_USING_HEAP)
//rt_system_heap_init(rt_heap_begin_get(), rt_heap_end_get());
rt_system_heap_init(HEAP_BEGIN, (void *)STM32_SRAM_END);
#endif
}
void SysTick_Handler(void)
{
/* enter interrupt */
rt_interrupt_enter();
rt_tick_increase();
/* leave interrupt */
rt_interrupt_leave();
}
void rt_hw_us_delay(rt_uint32_t us)
{
rt_uint32_t delta;
us = us * (SysTick->LOAD / (1000000 / RT_TICK_PER_SECOND));
delta = SysTick->VAL;
if (delta < us)
{
/* wait current OSTick left time gone */
while (SysTick->VAL < us);
us -= delta;
delta = SysTick->LOAD;
}
while (delta - SysTick->VAL < us);
}
至此,已经可以使用RTT系统了。
1.准备工作
(1) 准备好STM32f103标准库
(2) 安装RT-Thread pack链接:
2.复制文件
将安装好的RT-Thread文件复制到项目中。安装好的RT-Thread文件在
C:Keil_v5ARMPACKRealThreadRT-Thread 在项目中新建RTT文件夹,将上述路径中的文件复制过去。PS:复制过去的文件是只读的,需要取消只读属性。
3.修改项目文件
在项目中新建RTT组,并添加文件,基本文件如图:
4.包含头文件
如果不适用扩展包,则包含如下路径:
..RTTbsp
..RTTinclude
..RTTincludelibc
5.删除自带的中断函数
固件库中包含了如下三个函数需要删除。
void HardFault_Handler(void)
{
/* Go to infinite loop when Hard Fault exception occurs */
while (1)
{
}
}
void PendSV_Handler(void)
{
}
void SysTick_Handler(void)
{
}
6.修改库文件
rtconfig.h修改代码如下:
//#include "RTE_Components.h" board.c
修改文件如下:
/*
* Copyright (c) 2006-2018, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2017-07-24 Tanek the first version
* 2018-11-12 Ernest Chen modify copyright
*/
#include
#include
#include "stm32f10x.h"
#define _SCB_BASE (0xE000E010UL)
#define _SYSTICK_CTRL (*(rt_uint32_t *)(_SCB_BASE + 0x0))
#define _SYSTICK_LOAD (*(rt_uint32_t *)(_SCB_BASE + 0x4))
#define _SYSTICK_VAL (*(rt_uint32_t *)(_SCB_BASE + 0x8))
#define _SYSTICK_CALIB (*(rt_uint32_t *)(_SCB_BASE + 0xC))
#define _SYSTICK_PRI (*(rt_uint8_t *)(0xE000ED23UL))
// Updates the variable SystemCoreClock and must be called
// whenever the core clock is changed during program execution.
extern void SystemCoreClockUpdate(void);
// Holds the system core clock, which is the system clock
// frequency supplied to the SysTick timer and the processor
// core clock.
extern uint32_t SystemCoreClock;
static uint32_t _SysTick_Config(rt_uint32_t ticks)
{
if ((ticks - 1) > 0xFFFFFF)
{
return 1;
}
_SYSTICK_LOAD = ticks - 1;
_SYSTICK_PRI = 0xFF;
_SYSTICK_VAL = 0;
_SYSTICK_CTRL = 0x07;
return 0;
}
#if defined(RT_USING_USER_MAIN) && defined(RT_USING_HEAP)
#define RT_HEAP_SIZE 1536
static uint32_t rt_heap[RT_HEAP_SIZE]; // heap default size: 6K(1536 * 4)
RT_WEAK void *rt_heap_begin_get(void)
{
return rt_heap;
}
RT_WEAK void *rt_heap_end_get(void)
{
return rt_heap + RT_HEAP_SIZE;
}
#endif
/*******************************************************************************
* 函数名 : RCC_Configuration
* 描述 : 设置系统时钟为72MHZ(这个可以根据需要改)
* 输入 : 无
* 输出 : 无
* 返回值 : 无
* 说明 : STM32F107x和STM32F105x系列MCU与STM32F103x系列MCU时钟配置有所不同
*******************************************************************************/
void RCC_Configuration(void)
{
ErrorStatus HSEStartUpStatus; //外部高速时钟(HSE)的工作状态变量
RCC_DeInit(); //将所有与时钟相关的寄存器设置为默认值
RCC_HSEConfig(RCC_HSE_ON); //启动外部高速时钟HSE
HSEStartUpStatus = RCC_WaitForHSEStartUp(); //等待外部高速时钟(HSE)稳定
if(SUCCESS == HSEStartUpStatus) //如果外部高速时钟已经稳定
{
/* Enable Prefetch Buffer */
FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable); //Flash设置
/* Flash 2 wait state */
FLASH_SetLatency(FLASH_Latency_2);
RCC_HCLKConfig(RCC_SYSCLK_Div1); //设置AHB时钟等于系统时钟(1分频)/72MHZ
RCC_PCLK2Config(RCC_HCLK_Div1); //设置APB2时钟和HCLK时钟相等/72MHz(最大为72MHz)
RCC_PCLK1Config(RCC_HCLK_Div2); //设置APB1时钟是HCLK时钟的2分频/36MHz(最大为36MHz)
#ifndef STM32F10X_CL //如果使用的不是STM32F107x或STM32F105x系列MCU,PLL以下配置
RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9); //PLLCLK = 8MHz * 9 = 72 MHz
#else //如果使用的是STM32F107x或STM32F105x系列MCU,PLL以下配置
/***** 配置PLLx *****/
/* PLL2 configuration: PLL2CLK = (HSE / 5) * 8 = 40 MHz */
RCC_PREDIV2Config(RCC_PREDIV2_Div5);
RCC_PLL2Config(RCC_PLL2Mul_8);
RCC_PLL2Cmd(ENABLE); //使能PLL2
while (RCC_GetFlagStatus(RCC_FLAG_PLL2RDY) == RESET);//等待PLL2稳定
/* PLL configuration: PLLCLK = (PLL2 / 5) * 9 = 72 MHz */
RCC_PREDIV1Config(RCC_PREDIV1_Source_PLL2, RCC_PREDIV1_Div5);
RCC_PLLConfig(RCC_PLLSource_PREDIV1, RCC_PLLMul_9);
#endif
RCC_PLLCmd(ENABLE); //使能PLL
while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET); //等待PLL稳定
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK); //设置系统时钟的时钟源为PLL
while(RCC_GetSYSCLKSource() != 0x08); //检查系统的时钟源是否是PLL
RCC_ClockSecuritySystemCmd(ENABLE); //使能系统安全时钟
/* Enable peripheral clocks --------------------------------------------------*/
/* Enable I2C1 and I2C1 clock */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2|RCC_APB1Periph_SPI2, ENABLE);
/* Enable GPIOA GPIOB SPI1 and USART1 clocks */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB
| RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOD
| RCC_APB2Periph_GPIOE | RCC_APB2Periph_AFIO, ENABLE);
}
}
/**
* This function will initial your board.
*/
extern int Image$$RW_IRAM1$$ZI$$Limit;
#define HEAP_BEGIN ((void *)&Image$$RW_IRAM1$$ZI$$Limit)
#define STM32_SRAM_SIZE 64
#define STM32_SRAM_END (0x20000000 + STM32_SRAM_SIZE * 1024)
void rt_hw_board_init()
{
/* System Clock Update */
RCC_Configuration();
GPIO_InitTypeDef gpioInit;
//打开GPIOB的时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOE,ENABLE);
//LED上拉连接GPIOB 12引脚,所以设置如下,推挽输出,Pin12,2MHz输出速度
gpioInit.GPIO_Mode=GPIO_Mode_Out_PP;
gpioInit.GPIO_Pin=GPIO_Pin_5;
gpioInit.GPIO_Speed=GPIO_Speed_2MHz;
GPIO_Init(GPIOB,&gpioInit);
GPIO_Init(GPIOE,&gpioInit);
gpioInit.GPIO_Mode=GPIO_Mode_Out_PP;
gpioInit.GPIO_Pin=GPIO_Pin_12;
gpioInit.GPIO_Speed=GPIO_Speed_2MHz;
GPIO_Init(GPIOB,&gpioInit);
/* System Tick Configuration */
_SysTick_Config(SystemCoreClock / RT_TICK_PER_SECOND) ;
/* Call components board initial (use INIT_BOARD_EXPORT()) */
#ifdef RT_USING_COMPONENTS_INIT
rt_components_board_init();
#endif
#if defined(RT_USING_CONSOLE) && defined(RT_USING_DEVICE)
rt_console_set_device(RT_CONSOLE_DEVICE_NAME);
#endif
#if defined(RT_USING_USER_MAIN) && defined(RT_USING_HEAP)
//rt_system_heap_init(rt_heap_begin_get(), rt_heap_end_get());
rt_system_heap_init(HEAP_BEGIN, (void *)STM32_SRAM_END);
#endif
}
void SysTick_Handler(void)
{
/* enter interrupt */
rt_interrupt_enter();
rt_tick_increase();
/* leave interrupt */
rt_interrupt_leave();
}
void rt_hw_us_delay(rt_uint32_t us)
{
rt_uint32_t delta;
us = us * (SysTick->LOAD / (1000000 / RT_TICK_PER_SECOND));
delta = SysTick->VAL;
if (delta < us)
{
/* wait current OSTick left time gone */
while (SysTick->VAL < us);
us -= delta;
delta = SysTick->LOAD;
}
while (delta - SysTick->VAL < us);
}
至此,已经可以使用RTT系统了。
举报