1.FreeRTOS的简介&移植FreeRTOS到STM32平台
I.说明
作者:WXP(翱翔云端的鸟)
联系方式:aaa@qq.com || 13100610853(联系请注明CSDN)
申明:个人原创,转载请先经过本人同意!
要说的话:个人水平有限,不足之处,还请指正!有疑问欢迎大家联系我交流探讨!
=============================================================================================================================
=============================================================================================================================
II.环境
软件环境:KEIL-MDK v-5.24a
硬件:STM32F103ZET6最小系统板
=============================================================================================================================
=============================================================================================================================
III.FreeRTOS简介
FreeRTOS作为一个RTOS(Real Time Operating System),Free,即免费。公司或者个人在自己的产品中使用了FreeRTOS而不必为各种授权问题而苦恼! 这也是相对其它嵌入式RTOS的优势。其次它十分精简短小,占用很小的空间(4-9K字节大小),同时它支持很多平台,官方的文档也相对比较齐全,上手比较容易,所以比较适合初次接触RTOS的工程师。
FreeRTOS的具体特点如下
1.免商业许可,可以在具体产品中免费试用FreeRTOS
2.支持众多半导体厂家的平台,应用范围广,资料齐全
3.相比较于其它OS,代码更精简,移植更容易
4.提供了一个用于低功耗的Tickless模式
5.任务数量不受限制
6.内部有软件定时器和堆栈检测功能
7.内核对象齐全,在任务同步通信中使用起来很方便
=============================================================================================================================
=============================================================================================================================
IV.移植FreeRTOS到STM32F103平台
1.准备FreeRTOS源码和一个裸机STM32-Demo程序(STM32F103ZET6),FreeRTOS源码可以在官网www.freertos.org下载,同时上面也可以下载到很多文档。
这里我给出我的百度云下载地址(FreeRTOS源码和STM32裸机Demo):http://pan.baidu.com/s/1nvuTOPN
2.查看FreeRTOS源码目录,分为Demo、License、Source 3个目录
Demo目录下存放的是FreeRTOS的相关历程 如下图:
LICENSE目录是FreeRTOS的许可,可以不看
Source就是真正的FreeRTOS的源码目录
目录视图如下:
可以看到Source目录文件也很少,两个目录和一些.c文件 。
include:FreeRTOS的头文件
protable:平台及编译环境所需要的移植文件
xxx.c FreeRTOS源文件
这里我们只需注意 protable目录下的部分文件,这部分文件是等下移植所需要用到的
Keil:MDK编译环境所需要的文件
MemMang:内存管理相关文件,移植必须
RVDS:MDK编译环境所需要的文件
3.在裸机Demo工程文件中新建一个FreeRTOS的文件夹,并将FreeRTOS的源码全部添加到这个文件夹当中
如图所示:
4.删除protable目录下除Keil MemMang和RVDS外的所有文件
5.在MDK中新添加两个分组 FreeRTOS_Source和FreeRTOS_Ports,并添加对应的文件
{ croutine.c
FreeRTOS_Source { event_groups.c
{ list.c
{ queue.c
{ task.c
{ timers.c
FreeRTOS_Ports { port.c RVDS/ARM_CM3目录下
{heap_4.c MemMang目录下
{portmacro.h RVDS/ARM_CM3目录下 这里添加.h是为了比较容易查看
添加完成后如下图:
5.添加对应的文件路径 ..\FreeRTOS\include..\FreeRTOS\portable\RVDS\ARM_CM3
如图所示
6.编译,提示缺少FreeRTOSConfig.h文件
7.这里我们有两种方法添加这个文件,一种是自己创建,这个会很麻烦对于不懂FreeRTOS的人来说很难。第二种就是直接去官方移植好的例程中找到这个文件
这里推荐第二种 FreeRTOSv9.0.0->FreeRTOS->Demo->CORTEX_STM32F103_Keil 中找到FreeRTOSConfig.h
然后复制到裸机工程的USER目录下
如图
8.然后将FreeRTOSConfig.h文件添加到MDK USER组中(这里是为了容易查看和修改)
9.修改stm32f10x_it.c
修改bsp_systick.c
修改bsp.c如下
.
10.添加一个includes.h 用来管理所有的头文件
#ifndef __INCLUDES_H__
#define __INCLUDES_H__
/***********************************************************************************
* STD
************************************************************************************/
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "stm32f10x.h"
/************************************************************************************
* OS
*************************************************************************************/
#include "FreeRTOS.h"
#include "task.h"
#include "queue.h"
#include "croutine.h"
/************************************************************************************
* MACRO DEFINE
*************************************************************************************/
/************************************************************************************
* APP/BSP
*************************************************************************************/
#include "bsp.h"
#endif
11.修改FreeRTOSConfig.h和main.c
FreeRTOSConfig.h
/*
FreeRTOS V9.0.0 - Copyright (C) 2016 Real Time Engineers Ltd.
All rights reserved
VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
This file is part of the FreeRTOS distribution.
FreeRTOS is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License (version 2) as published by the
Free Software Foundation >>>> AND MODIFIED BY <<<< the FreeRTOS exception.
***************************************************************************
>>! NOTE: The modification to the GPL is included to allow you to !<<
>>! distribute a combined work that includes FreeRTOS without being !<<
>>! obliged to provide the source code for proprietary components !<<
>>! outside of the FreeRTOS kernel. !<<
***************************************************************************
FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. Full license text is available on the following
link: http://www.freertos.org/a00114.html
***************************************************************************
* *
* FreeRTOS provides completely free yet professionally developed, *
* robust, strictly quality controlled, supported, and cross *
* platform software that is more than just the market leader, it *
* is the industry's de facto standard. *
* *
* Help yourself get started quickly while simultaneously helping *
* to support the FreeRTOS project by purchasing a FreeRTOS *
* tutorial book, reference manual, or both: *
* http://www.FreeRTOS.org/Documentation *
* *
***************************************************************************
http://www.FreeRTOS.org/FAQHelp.html - Having a problem? Start by reading
the FAQ page "My application does not run, what could be wrong?". Have you
defined configASSERT()?
http://www.FreeRTOS.org/support - In return for receiving this top quality
embedded software for free we request you assist our global community by
participating in the support forum.
http://www.FreeRTOS.org/training - Investing in training allows your team to
be as productive as possible as early as possible. Now you can receive
FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers
Ltd, and the world's leading authority on the world's leading RTOS.
http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
including FreeRTOS+Trace - an indispensable productivity tool, a DOS
compatible FAT file system, and our tiny thread aware UDP/IP stack.
http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.
Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.
http://www.OpenRTOS.com - Real Time Engineers ltd. license FreeRTOS to High
Integrity Systems ltd. to sell under the OpenRTOS brand. Low cost OpenRTOS
licenses offer ticketed support, indemnification and commercial middleware.
http://www.SafeRTOS.com - High Integrity Systems also provide a safety
engineered and independently SIL3 certified version for use in safety and
mission critical applications that require provable dependability.
1 tab == 4 spaces!
*/
#ifndef FREERTOS_CONFIG_H
#define FREERTOS_CONFIG_H
/*-----------------------------------------------------------
* Application specific definitions.
*
* These definitions should be adjusted for your particular hardware and
* application requirements.
*
* THESE PARAMETERS ARE DESCRIBED WITHIN THE 'CONFIGURATION' SECTION OF THE
* FreeRTOS API DOCUMENTATION AVAILABLE ON THE FreeRTOS.org WEB SITE.
*
* See http://www.freertos.org/a00110.html.
*----------------------------------------------------------*/
//抢占式调度使能 1--ENABLE 0--DISABLE
#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
/* Co-routine definitions. */
#define configUSE_CO_ROUTINES 0
#define configMAX_CO_ROUTINE_PRIORITIES ( 2 )
/* Set the following definitions to 1 to include the API function, or zero
to exclude the API function. */
#define INCLUDE_vTaskPrioritySet 1
#define INCLUDE_uxTaskPriorityGet 1
#define INCLUDE_vTaskDelete 1
#define INCLUDE_vTaskCleanUpResources 0
#define INCLUDE_vTaskSuspend 1
#define INCLUDE_vTaskDelayUntil 1
#define INCLUDE_vTaskDelay 1
#ifdef __NVIC_PRIO_BITS
#define configPRIO_BITS __NVIC_PRIO_BITS
#else
#define configPRIO_BITS 4
#endif
/* This is the value being used as per the ST library which permits 16
priority values, 0 to 15. This must correspond to the
configKERNEL_INTERRUPT_PRIORITY setting. Here 15 corresponds to the lowest
NVIC value of 255. */
#define configLIBRARY_KERNEL_INTERRUPT_PRIORITY 0x0F
#define configLIBRARY_LOWEST_INTERRUPT_PRIORITY 0x0F
#define configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY 0x01
/* Interrupt priorities used by the kernel port layer itself. These are generic
to all Cortex-M ports, and do not rely on any particular library functions. */
#define configKERNEL_INTERRUPT_PRIORITY ( configLIBRARY_LOWEST_INTERRUPT_PRIORITY << (8 -configPRIO_BITS) )
/* !!!! configMAX_SYSCALL_INTERRUPT_PRIORITY must not be set to zero !!!!
See http://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html. */
#define configMAX_SYSCALL_INTERRUPT_PRIORITY ( configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY << (8 -configPRIO_BITS) )
/* Normal assert() semantics without relying on the provision of an assert.h
header file. */
#define configASSERT( x ) if( ( x ) == 0 ) { taskDISABLE_INTERRUPTS(); for( ;; ); }
/* Definitions that map the FreeRTOS port interrupt handlers to their CMSIS
standard names. */
#define vPortSVCHandler SVC_Handler
#define xPortPendSVHandler PendSV_Handler
#define xPortSysTickHandler SysTick_Handler
#endif /* FREERTOS_CONFIG_H */
main.c
/******************************************************************************
* File : main.c
* Function : 主文件
* Description: None
* Version : V1.00
* Author : WXP
* Date :
* History :
******************************************************************************/
#include "includes.h"
/*****************************************************************************
* 函数声明
******************************************************************************/
static void vTask_LED2(void *pvParameters);
static void vTask_LED3(void *pvParameters);
static TaskHandle_t xHandleTask_LED2 = NULL;
static TaskHandle_t xHandleTask_LED3 = NULL;
static void AppTaskCreate(void)
{
xTaskCreate( vTask_LED2,
"LED2",
512,
NULL,
1,
&xHandleTask_LED2);
xTaskCreate( vTask_LED3,
"LED3",
512,
NULL,
2,
&xHandleTask_LED3);
}
int main(void)
{
Bsp_Init();
AppTaskCreate();
vTaskStartScheduler();
while(1);
}
static void vTask_LED2(void *pvParameters)
{
while(1)
{
LED2(ON);
vTaskDelay(100);
LED2(OFF);
vTaskDelay(100);
}
}
static void vTask_LED3(void *pvParameters)
{
while(1)
{
LED3(ON);
vTaskDelay(300);
LED3(OFF);
vTaskDelay(300);
}
}
12.FreeRTOSConfig.h中的内容将在以后讲解,main.c中就是添加了两个不同延时闪LED的任务,编译下载就可以看到两个灯以不同频率闪烁,由此FreeRTOS移植成功!
最好附上完整工程:
百度云盘:链接:http://pan.baidu.com/s/1qXZiWdY 密码:5s6z
上一篇: S3C2440之提高系统时钟