1、官方源码下载
(1)进入FreeRTOS官网:FreeRTOS官网
(2)点击下载FreeRTOS。
(3)选择待示例的项目进行下载。
2、删减目录
(1)下载后解压的FreeRTOS文件如下图所示。
(2)删除下图中红框勾选的文件。
(3)删除"FreeRTOSv202212.01\FreeRTOS\Demo"目录下用不到的示例工程。
(4)删除“FreeRTOS-Plus”整个文件,FreeRTOS的生态文件,非比需的。
(5)删除“tools”整个文件,这里面是亚马逊相关的文件,我们也不需要。
(6)"FreeRTOSv202212.01\FreeRTOS\Source\portable"目录下只保留如下两个文件夹,其他全部删掉。
(7)"FreeRTOSv202212.01\FreeRTOS\Source\portable\RVDS"目录下只保留如下一个文件夹,其他全部删掉。
(8)删除后文件后,部分文件目录如下。(由目录树生成工具zDirTree生成)。、
E:\RTOS\官方源码\FreeRTOSv202212.01
├FreeRTOS
│ ├Demo // 预先制作好的示例工程
│ │ ├CORTEX_STM32F103_Keil // STM32F103在keil环境下的工程文件
│ │ │ ├FreeRTOSConfig.h
│ │ │ ├...
│ ├License
│ ├Source
│ │ ├croutine.c // 核心文件
│ │ ├event_groups.c // 核心文件
│ │ ├list.c // 核心文件
│ │ ├queue.c // 核心文件
│ │ ├stream_buffer.c // 核心文件
│ │ ├tasks.c // 核心文件
│ │ ├timers.c // 核心文件
│ │ ├include
│ │ ├portable // 移植时需要实现的文件
│ │ │ ├MemMang // 内存管理
│ │ │ │ ├heap_1.c
│ │ │ │ ├heap_2.c
│ │ │ │ ├heap_3.c
│ │ │ │ ├heap_4.c
│ │ │ │ ├heap_5.c
│ │ │ │ ├ReadMe.url
│ │ │ ├RVDS // IDE为RVD或keil
│ │ │ │ ├ARM_CM3 // CortexM3架构
│ │ │ │ │ ├port.c
│ │ │ │ │ ├portmacro.h
│ ├Test
3、编译
3.1、首次编译
(1)打开工程。
(2)弹出如下对话框,说明该工程是用KeilMDK4创建的。点击“Migrate to Device Pack”更新为KeilMDK5。
(3)弹出对话框,点击“确定”。
(4)更新后,关闭工程再重新打开。编译情况如下。
(5)编译后报错106,明显不正常。该程序原本是Keil4MDK中编写的,更新为KeilMDK5出现的问题。
3.2、IDE更新后程序报错
(1)KeilMDK4中使用的编译器是"Missing: Compiler Version 5",而KeilMDK5中该编译器默认是不安装的。解决思路就是安装该编译器在编译试试。(ARM Comliler是ARM编译器的意思)
(2)V5编译器的下载,参考链接。
- 我使用阿里云盘无法创建分享链接
- 可以从安装了KeilMDK4的电脑上找,查找路径后下文安装路径相同
- 网上搜索查找下载
(3)将下载“ARMCC文件”拷贝至Keil安装路径下的“ARM”文件夹中。
(4)打开一个Keil工程,点击如下图标。
(5)点击“Folders/Extensions”,在点击“...”,出现右侧弹窗。
(6)点击“Add another...”,找到我们Keil安装路径下的ARMCC文件,点击“确定”。
(7)出现如下图所示V5编译器,点击一下红框,在点“Close”,至此ARMV5编译器安装完成。
(8)选择V5编译器,再次编译项目,编译结果如下。
3.3、Common目录错误解决
(1)根据上图,说明Common目录不能删除。Common目录下是独立与demo的通用代码,大部分已经弃用。
(2)重新把它添加回去。
(3)添加后如下。
(4)重新编译。
4、添加串口打印功能
4.1、去掉无关代码
(1)去掉LCD代码。
(2)Demo Files文件下只保留“serial.c和main.c”文件,其他都删掉。
(3)编译
4.2、删除未定义报错内容
(1)在文件STM32F10x.s中,删除如下内容。
(2)删除其他未定义的相关内容,再次编译。
(3)报错的内容均删除或者注释,直到没错为止。
4.2、增加串口打印功能。
4.2.1、初始化串口。
(1)修改serial.c文件中的xSerialPortInitMinimal函数,删除其余用不到的内容。
(2)修改后serial.c文件。
/*
* FreeRTOS V202212.01
* Copyright (C) 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
* https://www.FreeRTOS.org
* https://github.com/FreeRTOS
*
*/
/*
BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER FOR UART0.
*/
/* Scheduler includes. */
#include "FreeRTOS.h"
#include "queue.h"
#include "semphr.h"
/* Library includes. */
#include "stm32f10x_lib.h"
/* Demo application includes. */
#include "serial.h"
/*-----------------------------------------------------------*/
/*
* See the serial2.h header file.
* 功能:串口初始化
* 注:
* 波特率:115200
*/
void SerialPortInit(void)
{
USART_InitTypeDef USART_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
/* Enable USART1 clock */
RCC_APB2PeriphClockCmd( RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA, ENABLE );
/* Configure USART1 Rx (PA10) as input floating */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init( GPIOA, &GPIO_InitStructure );
/* Configure USART1 Tx (PA9) as alternate function push-pull */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init( GPIOA, &GPIO_InitStructure );
USART_InitStructure.USART_BaudRate = 115200;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No ;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_InitStructure.USART_Clock = USART_Clock_Disable;
USART_InitStructure.USART_CPOL = USART_CPOL_Low;
USART_InitStructure.USART_CPHA = USART_CPHA_2Edge;
USART_InitStructure.USART_LastBit = USART_LastBit_Disable;
USART_Init( USART1, &USART_InitStructure );
USART_ITConfig( USART1, USART_IT_RXNE, ENABLE );
USART_Cmd( USART1, ENABLE );
}
/*-----------------------------------------------------------*/
(3)修改后serial.h文件。
/*
* FreeRTOS V202212.01
* Copyright (C) 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
* https://www.FreeRTOS.org
* https://github.com/FreeRTOS
*
*/
#ifndef SERIAL_COMMS_H
#define SERIAL_COMMS_H
typedef void * xComPortHandle;
typedef enum
{
serCOM1,
serCOM2,
serCOM3,
serCOM4,
serCOM5,
serCOM6,
serCOM7,
serCOM8
} eCOMPort;
typedef enum
{
serNO_PARITY,
serODD_PARITY,
serEVEN_PARITY,
serMARK_PARITY,
serSPACE_PARITY
} eParity;
typedef enum
{
serSTOP_1,
serSTOP_2
} eStopBits;
typedef enum
{
serBITS_5,
serBITS_6,
serBITS_7,
serBITS_8
} eDataBits;
typedef enum
{
ser50,
ser75,
ser110,
ser134,
ser150,
ser200,
ser300,
ser600,
ser1200,
ser1800,
ser2400,
ser4800,
ser9600,
ser19200,
ser38400,
ser57600,
ser115200
} eBaud;
void SerialPortInit(void);
#endif /* ifndef SERIAL_COMMS_H */
(4)修改后编译。报未定义vUARTInterruptHandler。
- 找到vUARTInterruptHandler,注释掉相关内容。
- 在STM32F10x.s文件中
- DCD 0;vUARTInterruptHandler ; USART1
- ;IMPORT vUARTInterruptHandler
(5)修改后编译。
4.2.2、实现fputc。
(1)fputc函数移动到serial.c文件中。
int fputc( int ch, FILE *f )
{
USART_TypeDef *USARTx = USART1;
while( (USARTx->SR & (1<<7)) == 0); // 等待上次的数据发送完成
USARTx->DR = ch;
return ch;
}
(2)串口初始化函数SerialPortInit在函数prvSetupHardware中调用;prvSetupHardware函数在main中调用。
(3)在main.c使用printf打印"Hello World.\r\n"
5、实验现象和程序下载
5.1、使用Keil模拟器查看实验现象
5.2、修改后完整工程下载地址
(1)完整工程存储在码云。
(2)FreeRTOS_CSDN: 用来保存FreeRTOS学习使用中用到的程序