Date:2016.06.01
步骤
- 新建一个MDK5工程
- stm32f10x相关文件导入
- 工程设置
- 裸奔点灯
- freeRTOS系统文件导入
- 点个高级灯再试试
新建一个MDK5工程
-
新建工程目录和文件目录
stm32f10x相关文件导入
库文件导入
将..\STM32F10x_StdPeriph_Lib_V3.5.\Libraries\STM32F10x_StdPeriph_Driver
整个文件夹复制到\lib下内核和启动代码导入
2.0 将以下所有文件复制到\core
2.1 core_cm3.c和core_cm3.h位于:
..\STM32F10x_StdPeriph_Lib_V3.5.0\Libraries\CMSIS\CM3\CoreSupport
2.2 startup_stm32f10x_hd.s位于:
..\STM32F10x_StdPeriph_Lib_V3.5.0\Libraries\CMSIS\CM3\DeviceSupport\ST\STM32F10x\startup\arm
2.3 stm32f10x.h,system_stm32f10x.c,system_stm32f10x.h位于:
..\STM32F10x_StdPeriph_Lib_V3.5.0\Libraries\CMSIS\CM3 \DeviceSupport\ST\STM32F10x
2.4 stm32f10x_conf.h,stm32f10x_it.c,stm32f10x_it.h位于:
..\STM32F10x_StdPeriph_Lib_V3.5.0\Project\STM32F10x_StdPeriph_Template
工程设置
-
将文件导入工程
-
Obj路径设置并勾选生成hex文件
-
头文件路径设置
-
全局宏定义变量设置
-
JLink设置
裸奔点灯
- 在\init下添加一个main.c
#include "stm32f10x.h"
#include "stm32f10x_it.h"
void Delay(u32 count) {
u32 i=0;
for(;i<count;i++);
}
void LED_config() {
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA
| RCC_APB2Periph_GPIOD,
ENABLE
);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init( GPIOA, &GPIO_InitStructure );
GPIO_SetBits( GPIOA, GPIO_Pin_8 );
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_Init( GPIOD, &GPIO_InitStructure);
GPIO_SetBits( GPIOD, GPIO_Pin_2 );
}
int main(void) {
LED_config();
while(1) {
GPIO_ResetBits(GPIOA,GPIO_Pin_8);
GPIO_SetBits(GPIOD,GPIO_Pin_2);
Delay(3000000);
GPIO_SetBits(GPIOA,GPIO_Pin_8);
GPIO_ResetBits(GPIOD,GPIO_Pin_2);
Delay(3000000);
}
}
freeRTOS系统文件导入
与FreeRTOS内核有关的文件:list.c, queue.c, tasks.c
位于..\FreeRTOSv9.0.0\FreeRTOS\Source
复制到lib\freeRTOS\src与内存分配有关的文件: heap_1.c,heap_2.c,heap_3.c,heap_4.c,heap_5.c。五选一,STM32选择heap_2.c
位于..\FreeRTOSv9.0.0\FreeRTOS\Source\portable\MemMang
复制到lib\freeRTOS\src移植平台相关文件port.c
位于..\FreeRTOSv9.0.0\FreeRTOS\Source\portable\RVDS\ARM_CM3
复制到lib\freeRTOS\src配置文件portmacro.h,FreeRTOSConfig.h
分别位于
..\FreeRTOSv9.0.0\FreeRTOS\Source\portable\RVDS\ARM_CM3
..\FreeRTOSv9.0.0\FreeRTOS\Demo\CORTEX_STM32F103_Keil
复制到lib\freeRTOS\inc通用代码文件
将..\FreeRTOSv9.0.0\FreeRTOS\Source\include下的所有文件
复制到lib\freeRTOS\inc-
将freeRTOS系统文件导入工程结构
-
更改启动文件startup_stm32f10x_hd.s
. 更改1
具体代码:
__heap_limit
PRESERVE8
THUMB
IMPORT xPortPendSVHandler ; insert for freeRTOS
IMPORT xPortSysTickHandler ; insert for freeRTOS
IMPORT vPortSVCHandler ; insert for freeRTOS
. 更改2
具体代码:
__Vectors DCD __initial_sp ; Top of Stack
DCD Reset_Handler ; Reset Handler
DCD NMI_Handler ; NMI Handler
DCD HardFault_Handler ; Hard Fault Handler
DCD MemManage_Handler ; MPU Fault Handler
DCD BusFault_Handler ; Bus Fault Handler
DCD UsageFault_Handler ; Usage Fault Handler
DCD 0 ; Reserved
DCD 0 ; Reserved
DCD 0 ; Reserved
DCD 0 ; Reserved
; DCD SVC_Handler ; SVCall Handler
DCD vPortSVCHandler ; change for freeRTOS
DCD DebugMon_Handler ; Debug Monitor Handler
DCD 0 ; Reserved
; DCD PendSV_Handler ; PendSV Handler
; DCD SysTick_Handler ; SysTick Handler
DCD xPortPendSVHandler ; change for freeRTOS
DCD xPortSysTickHandler ; change for freeRTOS
点个高级灯再试试
. 更改main.c中的内容为:
#include "stm32f10x.h"
#include "stm32f10x_it.h"
#include "FreeRTOS.h"
#include "task.h"
#include "queue.h"
#include "list.h"
#include "portable.h"
#include "FreeRTOSConfig.h"
void LED_config() {
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA
| RCC_APB2Periph_GPIOD,
ENABLE
);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init( GPIOA, &GPIO_InitStructure );
GPIO_SetBits( GPIOA, GPIO_Pin_8 );
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_Init( GPIOD, &GPIO_InitStructure);
GPIO_SetBits( GPIOD, GPIO_Pin_2 );
}
void vLED_1_Task( void *pvParameters ) {
while( 1 ) {
GPIO_ResetBits( GPIOA, GPIO_Pin_8 );
vTaskDelay( 1000 / portTICK_RATE_MS );
GPIO_SetBits( GPIOA, GPIO_Pin_8);
vTaskDelay( 2000 / portTICK_RATE_MS );
}
}
void vLED_2_Task( void *pvParameters ) {
while( 1 ) {
GPIO_SetBits( GPIOD, GPIO_Pin_2);
vTaskDelay( 1000 / portTICK_RATE_MS );
GPIO_ResetBits( GPIOD, GPIO_Pin_2);
vTaskDelay( 1000 / portTICK_RATE_MS );
}
}
int main(void) {
LED_config();
xTaskCreate( vLED_1_Task, "LED1",configMINIMAL_STACK_SIZE,NULL,tskIDLE_PRIORITY+1 ,NULL);
xTaskCreate( vLED_2_Task, "LED2",configMINIMAL_STACK_SIZE,NULL,tskIDLE_PRIORITY+2 ,NULL);
vTaskStartScheduler();
return 0;
}
. 注意freeRTOSConfig.h中的配置
#define configUSE_PREEMPTION 1
#define configUSE_IDLE_HOOK 0
#define configUSE_TICK_HOOK 0
#define configCPU_CLOCK_HZ ( ( unsigned long ) 72000000 )
#define configTICK_RATE_HZ ( ( TickType_t ) 1000 )
#define configMAX_PRIORITIES ( 5 )
#define configMINIMAL_STACK_SIZE ( ( unsigned short ) 128 )
#define configTOTAL_HEAP_SIZE ( ( size_t ) ( 17 * 1024 ) )
#define configMAX_TASK_NAME_LEN ( 16 )
#define configUSE_TRACE_FACILITY 0
#define configUSE_16_BIT_TICKS 0
#define configIDLE_SHOULD_YIELD 1