掌握FreeRTOS™实时内核 -- 第一章

在这里插入图片描述

Chapter 1 -The FreeRTOS Distribution(第一章 -FreeRTOS发行版)

1.1 Chapter Introduction and Scope(章节的简介与范围)

FreeRTOS is distributed as a single zip file archive that contains all the official FreeRTOS ports, and a large number of pre-configured demo applications.
FreeRTOS 作为单个 zip 文件存档进行分发,其中包含所有官方 FreeRTOS 端口以及大量预配置的演示应用程序。

Scope(范围)

This chapter aims to help users orientate themselves with the FreeRTOS files and directories by:
本章旨在通过以下方式帮助用户熟悉 FreeRTOS 文件和目录:
作者这里认为熟悉FreeRTOS目录是非常有必要的,为日后学习其他的操作系统提供便捷

  • Providing a top level view of the FreeRTOS directory structure.
  • 提供 FreeRTOS 目录结构的顶层视图。
  • Describing which files are actually required by any particular FreeRTOS project.
  • 描述任何特定 FreeRTOS 项目实际需要哪些文件。
  • Introducing the demo applications.
  • 介绍演示应用程序。
  • Providing information on how a new project can be created.
  • 提供有关如何创建新项目的信息。
    The description here relates only to the official FreeRTOS distribution. The examples that come with this book use a slightly different organization.
    这里的描述仅涉及官方 FreeRTOS 发行版。本书附带的示例使用了略有不同的组织方式。
    获取本书可以在freertos官网进行获取

1.2 Understanding the FreeRTOS Distribution(了解 FreeRTOS 发行版)

Definition: FreeRTOS Port(FreeRTOS端口的定义描述)

FreeRTOS can be built with approximately twenty different compilers, and can run on more than thirty different processor architectures. Each supported combination of compiler and processor is considered to be a separate FreeRTOS port.
FreeRTOS 可以使用大约二十种不同的编译器构建,并且可以在三十多种不同的处理器架构上运行。每个受支持的编译器和处理器组合都被视为一个单独的 FreeRTOS 端口。

Building FreeRTOS(构建FreeRTOS)

FreeRTOS can be thought of as a library that provides multi-tasking capabilities to what would otherwise be a bare metal application.
FreeRTOS 可以被认为是一个为裸机应用程序提供多任务处理功能的库。

FreeRTOS is supplied as a set of C source files. Some of the source files are common to all ports, while others are specific to a port. Build the source files as part of your project to make the FreeRTOS API available to your application. To make this easy for you, each official FreeRTOS port is provided with a demo application. The demo application is pre-configured to build the correct source files, and include the correct header files.
FreeRTOS 作为一组 C 源文件提供。一些源文件对于所有端口都是通用的,而其他源文件则特定于某个端口。将源文件构建为项目的一部分,以使 FreeRTOS API 可用于您的应用程序。为了让您轻松完成此操作,每个官方 FreeRTOS 端口都提供了一个演示应用程序。演示应用程序已预先配置为构建正确的源文件,并包含正确的头文件。

Demo applications should build ‘out of the box’, although some demos are older than others, and sometimes a change in the build tools made since the demo was released can cause an issue. Section 1.3 describes the demo applications.
演示应用程序应该“开箱即用”构建,尽管有些演示比其他演示更旧,有时自演示发布以来构建工具的更改可能会导致问题。 1.3 节描述了演示应用程序。

FreeRTOSConfig.h(这里是介绍这个C文件)

FreeRTOS is configured by a header file called FreeRTOSConfig.h.
FreeRTOS 由名为 FreeRTOSConfig.h 的头文件配置。

FreeRTOSConfig.h is used to tailor FreeRTOS for use in a specific application. For example, FreeRTOSConfig.h contains constants such as configUSE_PREEMPTION, the setting of which defines whether the co-operative or pre-emptive scheduling algorithm will be used(Please refer to Chapter 3.12). As FreeRTOSConfig.h contains application specific definitions, it should be located in a directory that is part of the application being built, not in a directory that contains the FreeRTOS source code.
FreeRTOSConfig.h 用于定制 FreeRTOS 以在特定应用程序中使用。例如,FreeRTOSConfig.h 包含 configUSE_PREEMPTION 等常量,其设置定义是使用协作调度算法还是抢占式调度算法(请参考3.12章)。由于 FreeRTOSConfig.h 包含应用程序特定的定义,因此它应该位于正在构建的应用程序的目录中,而不是包含 FreeRTOS 源代码的目录中。

A demo application is provided for every FreeRTOS port, and every demo application contains a FreeRTOSConfig.h file. It is therefore never necessary to create a FreeRTOSConfig.h file from scratch. Instead, it is recommended to start with, then adapt, the FreeRTOSConfig.h used by the demo application provided for the FreeRTOS port in use.
每个FreeRTOS端口都提供了一个演示应用程序,每个演示应用程序都包含一个FreeRTOSConfig.h文件。因此,无需从头开始创建FreeRTOSConfig.h文件。相反,建议从使用中的FreeRTOS端口提供的演示应用程序所使用的FreeRTOSConfig.h开始,然后对其进行修改

The Official FreeRTOS Distribution( FreeRTOS 官方发行版)

FreeRTOS is distributed in a single zip file. The zip file contains source code for all the
FreeRTOS ports, and project files for all the FreeRTOS demo applications. It also contains a selection of FreeRTOS+ ecosystem components, and a selection of FreeRTOS+ ecosystem demo applications.
FreeRTOS 以单个 zip 文件形式分发。该 zip 文件包含所有 FreeRTOS 端口的源代码以及所有 FreeRTOS 演示应用程序的项目文件。它还包含精选的 FreeRTOS+ 生态系统组件以及精选的 FreeRTOS+ 生态系统演示应用程序。

Do not be put off by the number of files in the FreeRTOS distribution! Only a very small number of files are required in any one application.
不要因 FreeRTOS 发行版中的文件数量而吓到!在任何一个应用程序中,只需要极少量的文件。

The Top Directories in the FreeRTOS Distribution(FreeRTOS发行版中的主要目录结构)

The first and second level directories of the FreeRTOS distribution are shown and described in Figure 1.
FreeRTOS 发行版的第一级和第二级目录如图 1 所示和描述。
 Figure 1.  Top level directories within the FreeRTOS distribution
Figure 1. Top level directories within the FreeRTOS distribution (图 1. FreeRTOS 发行版的主要目录结构)


  • FreeRTOS
    • Source Directory containing the FreeRTOS source files
    • Demo Directory containing pre-configured and port specific FreeRTOS demo projects
  • FreeRTOS-Plus
    • Source Directory containing source code for some FreeRTOS+ ecosystem components
    • Demo Directory containing demo projects for FreeRTOS+ ecosystem components


  • FreeRTOS
    • Source 包含 FreeRTOS 源文件的目录
    • Demo 包含预配置和特定于端口的 FreeRTOS 演示项目的目录
  • FreeRTOS-Plus
    • Source 包含一些 FreeRTOS+ 生态系统组件的源代码的目录
    • Demo 包含 FreeRTOS+ 生态系统组件演示项目的目录

注:这里作者不太会使用表格和图,如果有能够流畅的使用markdown,能使用markdown创建figure1,欢迎交流交流。
The zip file only contains one copy of the FreeRTOS source files; all the FreeRTOS demo projects, and all the FreeRTOS+ demo projects, expect to find the FreeRTOS source files in the FreeRTOS/Source directory, and may not build if the directory structure is changed.
zip 文件仅包含 FreeRTOS 源文件的一份副本;所有 FreeRTOS 演示项目和所有 FreeRTOS+ 演示项目都希望在 FreeRTOS/Source 目录中找到 FreeRTOS 源文件,如果目录结构发生更改,则可能无法构建。

FreeRTOS Source Files Common to All Ports(特定于某个端口的FreeRTOS源文件)

The core FreeRTOS source code is contained in just two C files that are common to all the FreeRTOS ports. These are called tasks.c, and list.c, and they are located directly in the FreeRTOS/Source directory, as shown in Figure 2. In addition to these two files, the following source files are located in the same directory:
核心 FreeRTOS 源代码仅包含在所有 FreeRTOS 端口通用的两个 C 文件中。它们称为tasks.c和list.c,它们直接位于FreeRTOS/Source目录中,如图2所示。除了这两个文件之外,以下源文件也位于同一目录中:

  • queue.c
    queue.c provides both queue and semaphore services, as described later in this book. queue.c is nearly always required.
    queue.c 提供队列和信号量服务,如本书后面所述。几乎总是需要queue.c。
  • timers.c
    timers.c provides software timer functionality, as described later in this book. It need only be included in the build if software timers are actually going to be used.
    timers.c 提供软件定时器功能,如本书后面所述。仅当实际要使用软件计时器时才需要将其包含在构建中。
  • event_groups.c
    event_groups.c provides event group functionality, as described later in this book. It need only be included in the build if event groups are actually going to be used.
    event_groups.c 提供事件组功能,如本书后面所述。仅当实际要使用事件组时才需要将其包含在构建中。
  • croutine.c
    croutine.c implements the FreeRTOS co-routine functionality. It need only be included in the build if co-routines are actually going to be used. Co-routines were intended for use on very small microcontrollers, are rarely used now, and are therefore not maintained to the same level as other FreeRTOS features. Co-routines are not described in this book.
    croutine.c 实现 FreeRTOS 协同例程功能。仅当实际要使用协同例程时才需要将其包含在构建中。协同例程旨在用于非常小的微控制器,现在很少使用,因此无法保持与其他 FreeRTOS 功能相同的水平。本书中没有描述协同例程。
     Figure 2.  Core FreeRTOS source files within the FreeRTOS directory tree
    Figure 2. Core FreeRTOS source files within the FreeRTOS directory tree(图 2. FreeRTOS 目录分支中的核心 FreeRTOS 源文件)

  • FreeRTOS
    • Source
      • task.c FreeRTOS source file - always required
      • list.c FreeRTOS source file - always required
      • queue.c FreeRTOS source file - nearly always required
      • timers.c FreeRTOS source file - optional
      • event_groups.c FreeRTOS source file - optional
      • croutine.c FreeRTOS source file - optional


  • FreeRTOS
    • Source
      • task.c FreeRTOS 源文件 - 始终必需
      • list.c FreeRTOS 源文件 - 始终必需
      • queue.c FreeRTOS 源文件 - 几乎始终必需
      • timers.c FreeRTOS 源文件 - 可选
      • event_groups.c FreeRTOS 源文件 - 可选
      • croutine.c FreeRTOS 源文件 - 可选

It is recognized that the file names may result in name space clashes, as many projects will already include files that have the same names. It is however considered that changing the names of the files now would be problematic, as to do so would break compatibility with the many thousands of projects that use FreeRTOS, as well as automation tools, and IDE plug-ins.
众所周知,文件名可能会导致名称空间冲突,因为许多项目已经包含具有相同名称的文件。然而,现在更改文件名可能会带来问题,因为这样做会破坏与使用FreeRTOS的数千个项目以及自动化工具和IDE插件的兼容性。

FreeRTOS Source Files Specific to a Port(特定于某个端口的FreeRTOS源文件)

Source files specific to a FreeRTOS port are contained within the FreeRTOS/Source/portable directory. The portable directory is arranged as a hierarchy, first by compiler, then by processor architecture. The hierarchy is shown in Figure3.
特定于 FreeRTOS 端口的源文件包含在 FreeRTOS/Source/portable 目录中。可移植目录按层次结构排列,首先由编译器排列,然后由处理器体系结构排列。层次结构如图 3 所示。

If you are running FreeRTOS on a processor with architecture ‘architecture’ using compiler ‘compiler’ then, in addition to the core FreeRTOS source files, you must also build the files located in FreeRTOS/Source/portable/[compiler]/[architecture] directory.
如果您使用编译器“compiler”在具有“architecture”架构的处理器上运行 FreeRTOS,那么除了核心 FreeRTOS 源文件之外,您还必须构建位于 FreeRTOS/Source/portable/[compiler]/[architecture] 中的文件目录。

As will be described in Chapter 2, Heap Memory Management, FreeRTOS also considers heap memory allocation to be part of the portable layer. Projects that use a FreeRTOS version older than V9.0.0 must include a heap memory manager. From FreeRTOS V9.0.0 a heap memory manager is only required if configSUPPORT_DYNAMIC_ALLOCATION is set to 1 in FreeRTOSConfig.h, or if configSUPPORT_DYNAMIC_ALLOCATION is left undefined.
正如第 2 章“堆内存管理”中所述,FreeRTOS 还将堆内存分配视为可移植层的一部分。使用 V9.0.0 之前的 FreeRTOS 版本的项目必须包含堆内存管理器。从 FreeRTOS V9.0.0 开始,仅当 FreeRTOSConfig.h 中的 configSUPPORT_DYNAMIC_ALLOCATION 设置为 1 或 configSUPPORT_DYNAMIC_ALLOCATION 未定义时才需要堆内存管理器。

FreeRTOS provides five example heap allocation schemes. The five schemes are named heap_1 to heap_5, and are implemented by the source files heap_1.c to heap_5.c respectively. The example heap allocation schemes are contained in the FreeRTOS/Source/portable/MemMang directory. If you have configured FreeRTOS to use dynamic memory allocation then it is necessary to build one of these five source files in your project, unless your application provides an alternative implementation.
FreeRTOS 提供了五种示例堆分配方案。这五个方案分别命名为heap_1到heap_5,分别由源文件heap_1.c到heap_5.c实现。示例堆分配方案包含在 FreeRTOS/Source/portable/MemMang 目录中。如果您已将 FreeRTOS 配置为使用动态内存分配,则有必要在项目中构建这五个源文件之一,除非您的应用程序提供替代实现。
在使用FreeRTOS堆管理,使用heap4即可

 Figure 3.  Port specific source files within the FreeRTOS directory tree
Figure 3. Port specific source files within the FreeRTOS directory tree(图 3. FreeRTOS 目录树中的端口特定源文件)


FreeRTOS 
|	Source
|   |   portable Directory containing all port specific source files 
|	|	|   MemMang Directory containing the 5 alternative heap allocation source files  
|	|	|   [compiler 1] Directory containing port files specific to compiler 1 
|	|	|	|   [architecture 1] Contains files for the compiler 1 architecture 1 port 
|	|	|	|   [architecture 2] Contains files for the compiler 1 architecture 2 port
|	|	|	|   [architecture 3] Contains files for the compiler 1 architecture 3 port
		└───[compiler 2] Directory containing port files specific to compiler 2 
	    |	|   [architecture 1] Contains files for the compiler 2 architecture 1 port
	    |	|   [architecture 2] Contains files for the compiler 2 architecture 2 port  
	    	└───[etc.]
FreeRTOS 
|	Source
|   |   portable(可移植的) 包含所有端口特定源文件的目录 
|	|	|   MemMang(内存管理)包含 5 个替代堆分配源文件的目录 
|	|	|   [compiler 1]  包含特定于编译器 1 的端口文件的目录    
|	|	|	|   [architecture 1] 包含编译器 1 架构 1 端口的文件 
|	|	|	|   [architecture 2] 包含编译器 1 架构 2 端口的文件
|	|	|	|   [architecture 3] 包含编译器 1 架构 3 端口的文件
		└───[compiler 2] 包含特定于编译器 2 的端口文件的目录 
	    |	|   [architecture 1] 包含编译器 2 架构 1 端口的文件
	    |	|   [architecture 2] 包含编译器 2 架构 2 端口的文件
	    	└───[etc.]

Include Paths(包含路径)

FreeRTOS requires three directories to be included in the compiler’s include path. These are:
  1. The path to the core FreeRTOS header files, which is always FreeRTOS/Source/include.
  2. The path to the source files that are specific to the FreeRTOS port in use. As described above, this is FreeRTOS/Source/portable/[compiler]/[architecture].
  3. A path to the FreeRTOSConfig.h header file
FreeRTOS 要求编译器的包含路径中包含三个目录。这些都是:
  1. FreeRTOS 核心头文件的路径,始终为 FreeRTOS/Source/include。

  2. 特定于所使用的 FreeRTOS 端口的源文件的路径。如上所述,这是FreeRTOS/Source/portable/[compiler]/[architecture]。

  3. FreeRTOSConfig.h 头文件的路径。

Header Files(头文件)

A source file that uses the FreeRTOS API must include ‘FreeRTOS.h’, followed by the header file that contains the prototype for the API function being used—either ‘task.h’, ‘queue.h’, ‘semphr.h’, ‘timers.h’ or ‘event_groups.h’.
使用 FreeRTOS API 的源文件必须包含“FreeRTOS.h”,后跟包含所使用的 API 函数原型的头文件 -“task.h”、“queue.h”、“semphr.h” 、“timers.h”或“event_groups.h”等。

1.3 Demo Applications(应用演示程序)

Each FreeRTOS port comes with at least one demo application that should build with no errors or warnings being generated, although some demos are older than others, and sometimes a change in the build tools made since the demo was released can cause an issue.
每个 FreeRTOS 端口都至少附带一个演示应用程序,该应用程序在构建时应该不会产生任何错误或警告,尽管某些演示比其他演示更旧,有时自演示发布以来对构建工具所做的更改可能会导致问题。

A note to Linux users: FreeRTOS is developed and tested on a Windows host. Occasionally this results in build errors when demo projects are built on a Linux host. Build errors are almost always related to the case of letters used when referencing file names, or the direction of slash characters used in file paths. Please use the FreeRTOS contact form (http://www.FreeRTOS.org/contact) to alert us to any such errors.
Linux 用户注意:当在Linux环境下构建演示项目时,FreeRTOS由于Windows和Linux的文件系统命名规则和路径分隔符的不同,偶尔会导致构建错误。。构建错误几乎总是与引用文件名时使用的字母大小写或文件路径中使用的斜杠字符的方向有关。如果有任何这样的错误,请使用FreeRTOS的联系方式(http://www.FreeRTOS.org/contact)来告知我们。


The demo application has several purposes:

  • To provide an example of a working and pre-configured project, with the correct files included, and the correct compiler options set.
  • To allow ‘out of the box’ experimentation with minimal setup or prior knowledge.
  • As a demonstration of how the FreeRTOS API can be used.
  • As a base from which real applications can be created.

该演示应用程序有几个目的:

  • 提供工作和预配置项目的示例,其中包含正确的文件和正确的编译器选项集。
  • 以最少的设置或先验知识进行“开箱即用”的实验。
  • 作为如何使用 FreeRTOS API 的演示。
  • 作为创建实际应用程序的基础。

Each demo project is located in a unique sub-directory under the FreeRTOS/Demo directory. The name of the sub-directory indicates the port to which the demo project relates.
每个演示项目都位于 FreeRTOS/Demo 目录下的唯一子目录中。子目录的名称表示演示项目所涉及的端口。

Every demo application is also described by a web page on the FreeRTOS.org web site. The web page includes information on
1、How to locate the project file for the demo within the FreeRTOS directory structure.
2、Which hardware the project is configured to use.
3、 How to set up the hardware for running the demo.
4、How to build the demo.
5、 How the demo is expected to behave.
每个演示应用程序也由 FreeRTOS.org 网站上的网页进行描述。该网页包含以下信息:
1、如何在 FreeRTOS 目录结构中找到演示的项目文件。
2、项目配置为使用哪些硬件。
3、 如何设置运行演示的硬件。
4、如何构建演示。
5、演示的预期表现如何。

All the demo projects create a subset of the common demo tasks, the implementations of which are contained in the FreeRTOS/Demo/Common/Minimal directory. The common demo tasks exist purely to demonstrate how the FreeRTOS API can be used—they do not implement any particular useful functionality.
所有演示项目都会创建常见演示任务的子集,其实现包含在 FreeRTOS/Demo/Common/Minimal 目录中。常见演示任务的存在纯粹是为了演示如何使用 FreeRTOS API,它们不实现任何特定有用的功能。

More recent demo projects can also build a beginners ‘blinky’ project. Blinky projects are very basic. Typically they will create just two tasks and one queue.
最近的演示项目还可以构建初学者“blinky”项目。 Blinky 项目非常基础。通常他们只会创建两个任务和一个队列。

Every demo project includes a file called main.c. This contains the main() function, from where all the demo application tasks are created. See the comments within the individual main.c files for information specific to that demo.
每个演示项目都包含一个名为 main.c 的文件。它包含 main() 函数,所有演示应用程序任务都是从该函数创建的。有关该演示的特定信息,请参阅各个 main.c 文件中的注释。

The FreeRTOS/Demo directory hierarchy is shown in Figure 4.
FreeRTOS/Demo 目录层次结构如图 4 所示。
fig4
Figure 4. The demo directory hierarchy(演示程序的层次结构)


  • FreeRTOS
    • Demo Directory containing all the demo projects
      • [Demo x] Contains the project file that builds demo ‘x’
      • [Demo y] Contains the project file that builds demo ‘y’
      • [Demo z] Contains the project file that builds demo ‘z’
      • Common Contains files that are built by all the demo applications


  • FreeRTOS
    • Demo 包含所有演示项目的目录
      • [Demo x] 包含构建演示(demo)‘x’ 的项目文件
      • [Demo y] 包含构建演示(demo)‘y’ 的项目文件
      • [Demo z] 包含构建演示(demo)‘z’ 的项目文件
      • Common 包含由所有演示应用程序构建的文件

1.4 Creating a FreeRTOS Project(创建FreeRTOS项目)

Adapting One of the Supplied Demo Projects(更改所提供的演示项目之一)

Every FreeRTOS port comes with at least one pre-configured demo application that should build with no errors or warnings. It is recommended that new projects are created by adapting one of these existing projects; this will allow the project to have the correct files included, the correct interrupt handlers installed, and the correct compiler options set.
每个 FreeRTOS 端口都附带至少一个预配置的演示应用程序,该应用程序构建时应该不会出现错误或警告。建议通过改编这些现有项目之一来创建新项目;这将允许项目包含正确的文件、安装正确的中断处理程序以及设置正确的编译器选项。

To start a new application from an existing demo project:

  1. Open the supplied demo project and ensure that it builds and executes as expected.
  2. Remove the source files that define the demo tasks. Any file that is located within the Demo/Common directory can be removed from the project.
  3. Delete all the function calls within main(), except prvSetupHardware() and vTaskStartScheduler(), as shown in Listing 1.
  4. Check the project still builds.

要从现有演示项目启动新应用程序:

  1. 打开提供的演示项目并确保它按预期构建和执行。
  2. 删除定义演示任务的源文件。位于 Demo/Common 目录中的任何文件都可以从项目中删除。
  3. 删除 main() 中除 prvSetupHardware() 和 vTaskStartScheduler() 之外的所有函数调用,如清单 1 所示。
  4. 检查项目是否仍在构建。

Following these steps will create a project that includes the correct FreeRTOS source files, but does not define any functionality.
按照这些步骤将创建一个包含正确的 FreeRTOS 源文件的项目,但不定义任何功能。

int main( void ) 
{ 
    /* Perform any hardware setup necessary. */ 
    prvSetupHardware(); 
 
    /* --- APPLICATION TASKS CAN BE CREATED HERE --- */ 
     
    /* Start the created tasks running. */ 
    vTaskStartScheduler(); 
     
    /* Execution will only reach here if there was insufficient heap to 
    start the scheduler. */ 
    for( ;; ); 
    return 0; 
} 

Listing 1. The template for a new main() function (清单 1. 新 main() 函数的模板)
这里为什么使用for(;;)而不使用while(1),在使用while时候需要进行一次判断,而for不需要,因此for的执行效率比while要快,因此这里使用while循环不使用for循环

Creating a New Project from Scratch(从头开始创建一个新项目)


As already mentioned, it is recommended that new projects are created from an existing demo project. If this is not desirable, then a new project can be created using the following procedure:

  1. Using your chosen tool chain, create a new project that does not yet include any FreeRTOS source files.
  2. Ensure the new project can be built, downloaded to your target hardware, and
    executed.
  3. Only when you are sure you already have a working project, add the FreeRTOS source files detailed in Table 1 to the project.
  4. Copy the FreeRTOSConfig.h header file used by the demo project provided for the port in use into the project directory.
  5. Add the following directories to the path the project will search to locate header files:
    · FreeRTOS/Source/include
    · FreeRTOS/Source/portable/[compiler]/[architecture] (where [compiler] and [architecture] are correct for your chosen port)
    · The directory containing the FreeRTOSConfig.h header file
  6. Copy the compiler settings from the relevant demo project.
  7. Install any FreeRTOS interrupt handlers that might be necessary. Use the web page that describes the port in use, and the demo project provided for the port in use, as a reference.

如前所述,建议从现有演示项目创建新项目。如果不希望这样做,则可以使用以下过程创建一个新项目:

  1. 使用您选择的工具链,创建一个尚不包含任何 FreeRTOS 源文件的新项目。
  2. 确保新项目可以构建、下载到目标硬件并执行。
  3. 仅当您确定已经有一个工作项目时,才将表(Table1) 1 中详细列出的 FreeRTOS 源文件添加到该项目中。
  4. 将所用端口提供的demo工程使用的FreeRTOSConfig.h头文件复制到工程目录下。
  5. 将以下目录添加到项目将搜索以查找头文件的路径中:
    · FreeRTOS/Source/include
    · FreeRTOS/Source/portable/[compiler]/[architecture](其中 [compiler] 和 [architecture] 是正确的)您选择的端口)
    · 包含 FreeRTOSConfig.h 头文件的目录
  6. 从相关演示项目复制编译器设置。
  7. 安装可能需要的任何 FreeRTOS 中断处理程序。请参考所使用端口的网页描述以及所使用端口提供的demo工程作为参考。

Table 1. FreeRTOS source files to include in the project(表 1. 要包含在项目中的 FreeRTOS 源文件)

FileLocation
tasks.cFreeRTOS/Source
queue.cFreeRTOS/Source
list.cFreeRTOS/Source
timers.cFreeRTOS/Source
evernt_groups.cFreeRTOS/Source
AII C and assembler filesFreeRTOS/Source/portable/[compiler]/[architecture]
heap_n.cFreeRTOS/Source/portable/MemMang, where n is either 1, 2, 3, 4 or 5. This file became optional from FreeRTOS V9.0.0.

Projects that use a FreeRTOS version older than V9.0.0 must build one of the heap_n.c files. From FreeRTOS V9.0.0 a heap_n.c file is only required if configSUPPORT_DYNAMIC_ALLOCATION is set to 1 in FreeRTOSConfig.h or if configSUPPORT_DYNAMIC_ALLOCATION is left undefined. Refer to Chapter 2, Heap Memory Management, for more information.
使用 V9.0.0 之前的 FreeRTOS 版本的项目必须构建 heap_n.c 文件之一。从 FreeRTOS V9.0.0 开始,仅当 FreeRTOSConfig.h 中的 configSUPPORT_DYNAMIC_ALLOCATION 设置为 1 或 configSUPPORT_DYNAMIC_ALLOCATION 未定义时才需要 heap_n.c 文件。有关详细信息,请参阅第 2 章“堆内存管理”。

1.5 Data Types and Coding Style Guide(数据类型和编码风格指南)

Data Types(数据类型)

Each port of FreeRTOS has a unique portmacro.h header file that contains (amongst other things) definitions for two port specific data types: TickType_t and BaseType_t. These data types are described in Table 2.
FreeRTOS 的每个端口都有一个唯一的 portmacro.h 头文件,其中包含(除其他外)两个端口特定数据类型的定义:TickType_t 和 BaseType_t。表 2 中描述了这些数据类型。
Table 2. Port specific data types used by FreeRTOS (表 2. FreeRTOS 使用的端口特定数据类型)

Macro or typedef usedActual type
TickType_tFreeRTOS configures a periodic interrupt called the tick interrupt.
The number of tick interrupts that have occurred since the FreeRTOS application started is called the tick count. The tick count is used as a measure of time.
The time between two tick interrupts is called the tick period. Times are specified as multiples of tick periods.
TickType_t is the data type used to hold the tick count value, and to specify times.
TickType_t can be either an unsigned 16-bit type, or an unsigned 32-bit type, depending on the setting of configUSE_16_BIT_TICKS within FreeRTOSConfig.h. If configUSE_16_BIT_TICKS is set to 1, then TickType_t is defined as uint16_t. If configUSE_16_BIT_TICKS is set to 0 then TickType_t is defined as uint32_t.
Using a 16-bit type can greatly improve efficiency on 8-bit and 16-bit architectures, but severely limits the maximum block period that can be specified. There is no reason to use a 16-bit type on a 32-bit architecture.
BaseType_tThis is always defined as the most efficient data type for the architecture. Typically, this is a 32-bit type on a 32-bit architecture, a 16-bit type on a 16-bit architecture, and an 8-bit type on an 8-bit architecture.
BaseType_t is generally used for return types that can take only a very limited range of values, and for pdTRUE/pdFALSE type Booleans.
使用宏或typedef实际类型
TickType_tFreeRTOS 配置一个称为节拍中断的周期性中断。
自 FreeRTOS 应用程序启动以来发生的滴答中断数称为滴答计数。滴答计数用作时间的度量。
两次滴答中断之间的时间称为滴答周期。时间被指定为刻度周期的倍数。
TickType_t 是用于保存刻度计数值并指定时间的数据类型。
TickType_t 可以是无符号 16 位类型,也可以是无符号 32 位类型,具体取决于 FreeRTOSConfig.h 中 configUSE_16_BIT_TICKS 的设置。如果 configUSE_16_BIT_TICKS 设置为 1,则 TickType_t 定义为 uint16_t。如果 configUSE_16_BIT_TICKS 设置为 0,则 TickType_t 定义为 uint32_t。
使用 16 位类型可以极大地提高 8 位和 16 位架构上的效率,但严重限制了可以指定的最大块周期。没有理由在 32 位体系结构上使用 16 位类型
BaseType_t这始终被定义为架构中(单片机)最有效的数据类型。通常,这是 32 位架构上的 32 位类型、16 位架构上的 16 位类型以及 8 位架构上的 8 位类型。
BaseType_t 通常用于只能采用非常有限的值范围的返回类型,以及 pdTRUE/pdFALSE 类型布尔值。

Some compilers make all unqualified char variables unsigned, while others make them signed. For this reason, the FreeRTOS source code explicitly qualifies every use of char with either ‘signed’ or ‘unsigned’, unless the char is used to hold an ASCII character, or a pointer to char is used to point to a string.
一些编译器使所有不合格的 char 变量无符号,而另一些编译器则使它们有符号。因此,FreeRTOS 源代码显式地使用“有符号”或“无符号”来限定 char 的每次使用,除非该 char 用于保存 ASCII 字符,或者使用指向 char 的指针来指向字符串。

Plain int types are never used.
从不使用普通 int 类型。

Variable Names(变量名称)

Variables are prefixed with their type: ‘c’ for char, ‘s’ for int16_t (short), ‘l’ int32_t (long), and ‘x’ for BaseType_t and any other non-standard types (structures, task handles, queue handles, etc.).
变量以其类型为前缀:“c”表示 char、“s”表示 int16_t(短)、“l”int32_t(长)、“x”表示 BaseType_t 和任何其他非标准类型(结构、任务句柄、队列)手柄等)。
If a variable is unsigned, it is also prefixed with a ‘u’. If a variable is a pointer, it is also prefixed with a ‘p’. For example, a variable of type uint8_t will be prefixed with ‘uc’, and a variable of type pointer to char will be prefixed with ‘pc’.
如果变量是无符号的,它也会以“u”为前缀。如果变量是指针,它也会以“p”为前缀。例如,uint8_t 类型的变量将带有“uc”前缀,指向 char 的指针类型变量将带有“pc”前缀。

Function Names(函数名称)

Functions are prefixed with both the type they return, and the file they are defined within. For example:

  • vTaskPrioritySet() returns a void and is defined within task.c.
  • xQueueReceive() returns a variable of type BaseType_t and is defined within queue.c.
  • pvTimerGetTimerID() returns a pointer to void and is defined within timers.c.

函数的前缀是它们返回的类型以及它们定义的文件。例如:

  • vTaskPrioritySet() 返回一个 void 并在 task.c 中定义。
  • xQueueReceive() 返回 BaseType_t 类型的变量,并在queue.c 中定义。
  • pvTimerGetTimerID() 返回一个指向 void 的指针,并在timers.c 中定义。

File scope (private) functions are prefixed with ‘prv’.
文件作用域(私有)函数以“prv”为前缀。

Formatting(格式化)

One tab is always set to equal four spaces.
一个制表符(tab)始终设置为等于四个空格。

Macro Names(宏名称)

Most macros are written in upper case, and prefixed with lower case letters that indicate where the macro is defined. Table 3 provides a list of prefixes.
大多数宏都是用大写字母编写的,并以小写字母作为前缀,指示宏的定义位置。表 3 提供了前缀列表。
Table 3. Macro prefixes(表 3. 宏前缀)

PrefixLocation of macro definition(宏定义位置)
port (for example, portMAX_DELAY)portable.h or portmacro.h
task (for example, taskENTER_CRITICAL())task.h
pd (for example, pdTRUE)projdefs.h
config (for example, configUSE_PREEMPTION)FreeRTOSConfig.h
err (for example, errQUEUE_FULL)projdefs.h

Note that the semaphore API is written almost entirely as a set of macros, but follows the function naming convention, rather than the macro naming convention.
请注意,信号量 API 几乎完全作为一组宏编写,但遵循函数命名约定,而不是宏命名约定。

The macros defined in Table 4 are used throughout the FreeRTOS source code.
表 4 中定义的宏在整个 FreeRTOS 源代码中使用。
Table 4. Common macro definitions(表 4. 常用宏定义)

MacroValue
pdTRUE1
pdFALSE0
pdPASS1
pdFAIL0

Rationale for Excessive Type Casting(过度类型转换的理由)

The FreeRTOS source code can be compiled with many different compilers, all of which differ in how and when they generate warnings. In particular, different compilers want casting to be used in different ways. As a result, the FreeRTOS source code contains more type casting than would normally be warranted.
FreeRTOS 源代码可以使用许多不同的编译器进行编译,所有编译器在生成警告的方式和时间方面有所不同。特别是,不同的编译器希望以不同的方式使用强制转换。因此,FreeRTOS 源代码包含的类型转换比通常所保证的要多。

  • 17
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值