windows clion MingW cmake 编译运行 FreeRTOS

说明

在 windows 平台上,使用 clion IDE,cmake 方式编译 FreeRTOS,编译工具链使用 MingW gcc

下载 FreeRTOS

  • 可以只下载 FreeRTOS-Kernel,也可以下载整个 FreeRTOS(包括 git 子模块,体积较大)

  • https://github.com/FreeRTOS/FreeRTOS-Kernel.git

clion 工程目录

  • 目录如下,FreeRTOS-Kernel 为 FreeRTOS 内核代码, port 为 FreeRTOS 适配, sim 为 main 函数入口,用户应用。
c 复制代码
CMakeLists.txt
FreeRTOS-Kernel
port
sim

编写 CMakeLists.txt

  • 当前不直接使用 FreeRTOS-Kernel 自带的 CMakeLists.txt,内容如下
bash 复制代码
cmake_minimum_required(VERSION 3.15)

project(rtos-sim)

add_definitions(-DprojCOVERAGE_TEST=0)

include_directories(port)
include_directories(
    FreeRTOS-Kernel/include
    FreeRTOS-Kernel/portable/MSVC-MingW
    port
)

set(FREERTOS_SOURCES
    FreeRTOS-Kernel/croutine.c
    FreeRTOS-Kernel/event_groups.c
    FreeRTOS-Kernel/list.c
    FreeRTOS-Kernel/queue.c
    FreeRTOS-Kernel/stream_buffer.c
    FreeRTOS-Kernel/tasks.c
    FreeRTOS-Kernel/timers.c
)

set(FREERTOS_PORT_SOURCES
    FreeRTOS-Kernel/portable/MSVC-MingW/port.c
    FreeRTOS-Kernel/portable/MemMang/heap_4.c
    port/rtos_port.c
)

add_executable(rtos-sim
    sim/main.c
    ${FREERTOS_SOURCES}
    ${FREERTOS_PORT_SOURCES}
)

target_link_libraries(rtos-sim
    winmm
)
  • 【注意】PC端使用 FreeRTOS-Kernel/portable/MSVC-MingW

  • port/rtos_port.c 内容如下,主要为 FreeRTOS 需要适配的几个函数,其中 HOOK 函数可以为空函数

c 复制代码
#include <stdio.h>
#include "FreeRTOS.h"

void vAssertCalled( unsigned long ulLine, const char * const pcFileName )
{

}

void vApplicationGetIdleTaskMemory( StaticTask_t **ppxIdleTaskTCBBuffer,
                                    StackType_t **ppxIdleTaskStackBuffer,
                                    uint32_t *pulIdleTaskStackSize )
{
    static StaticTask_t xIdleTaskTCB;
    static StackType_t uxIdleTaskStack[ configMINIMAL_STACK_SIZE ];

    *ppxIdleTaskTCBBuffer = &xIdleTaskTCB;
    *ppxIdleTaskStackBuffer = uxIdleTaskStack;
    *pulIdleTaskStackSize = configMINIMAL_STACK_SIZE;
}

void vApplicationGetTimerTaskMemory( StaticTask_t **ppxTimerTaskTCBBuffer,
                                     StackType_t **ppxTimerTaskStackBuffer,
                                     uint32_t *pulTimerTaskStackSize )
{
    static StaticTask_t xTimerTaskTCB;
    static StackType_t uxTimerTaskStack[ configTIMER_TASK_STACK_DEPTH ];

    *ppxTimerTaskTCBBuffer = &xTimerTaskTCB;

    *ppxTimerTaskStackBuffer = uxTimerTaskStack;

    *pulTimerTaskStackSize = configTIMER_TASK_STACK_DEPTH;
}

unsigned long ulGetRunTimeCounterValue( void )
{
#if 0
    LARGE_INTEGER liCurrentCount;
    unsigned long ulReturn;

    /* What is the performance counter value now? */
    QueryPerformanceCounter( &liCurrentCount );

    /* Subtract the performance counter value reading taken when the
    application started to get a count from that reference point, then
    scale to (simulated) 1/100ths of a millisecond. */
    if( llTicksPerHundedthMillisecond == 0 )
    {
        /* The trace macros are probably calling this function before the
        scheduler has been started. */
        ulReturn = 0;
    }
    else
    {
        ulReturn = ( unsigned long ) ( ( liCurrentCount.QuadPart - llInitialRunTimeCounterValue ) / llTicksPerHundedthMillisecond );
    }

    return ulReturn;
#endif
    return 0;
}

void vApplicationIdleHook( void )
{

}

void vApplicationDaemonTaskStartupHook(void)
{

}

void vConfigureTimerForRunTimeStats( void )
{
#if 0
    LARGE_INTEGER liPerformanceCounterFrequency, liInitialRunTimeValue;

    /* Initialise the variables used to create the run time stats time base.
    Run time stats record how much time each task spends in the Running
    state. */

    if( QueryPerformanceFrequency( &liPerformanceCounterFrequency ) == 0 )
    {
        llTicksPerHundedthMillisecond = 1;
    }
    else
    {
        /* How many times does the performance counter increment in 1/100th
        millisecond. */
        llTicksPerHundedthMillisecond = liPerformanceCounterFrequency.QuadPart / 100000LL;

        /* What is the performance counter value now, this will be subtracted
        from readings taken at run time. */
        QueryPerformanceCounter( &liInitialRunTimeValue );
        llInitialRunTimeCounterValue = liInitialRunTimeValue.QuadPart;
    }
#endif
}

void vApplicationTickHook(void)
{

}

void vApplicationMallocFailedHook(void)
{
    printf("%s : memory alloc failed\n", __func__);
}
  • port/FreeRTOSConfig.h,这个来自 FreeRTOS 的 demo,FreeRTOS\FreeRTOS\Demo\WIN32-MingW,基本可以不改动,如下
c 复制代码
/*
 * FreeRTOS V202212.00
 * 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 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
 * https://www.FreeRTOS.org/a00110.html
 *----------------------------------------------------------*/

#define configUSE_PREEMPTION					1
#define configUSE_PORT_OPTIMISED_TASK_SELECTION	1
#define configUSE_IDLE_HOOK						1
#define configUSE_TICK_HOOK						1
#define configUSE_DAEMON_TASK_STARTUP_HOOK		1
#define configTICK_RATE_HZ						( 1000 ) /* In this non-real time simulated environment the tick frequency has to be at least a multiple of the Win32 tick frequency, and therefore very slow. */
#define configMINIMAL_STACK_SIZE				( ( unsigned short ) 70 ) /* In this simulated case, the stack only has to hold one small structure as the real stack is part of the win32 thread. */
#define configTOTAL_HEAP_SIZE					( ( size_t ) ( 100 * 1024 ) )
#define configMAX_TASK_NAME_LEN					( 12 )
#define configUSE_TRACE_FACILITY				1
#define configIDLE_SHOULD_YIELD					1
#define configUSE_MUTEXES						1
#define configCHECK_FOR_STACK_OVERFLOW			0
#define configUSE_RECURSIVE_MUTEXES				1
#define configQUEUE_REGISTRY_SIZE				20
#define configUSE_APPLICATION_TASK_TAG			1
#define configUSE_COUNTING_SEMAPHORES			1
#define configUSE_ALTERNATIVE_API				0
#define configUSE_QUEUE_SETS					1
#define configUSE_TASK_NOTIFICATIONS			1
#define configSUPPORT_STATIC_ALLOCATION			1

/* Tick type width is defined based on the compiler type (32bit or 64bit). */
#ifdef __x86_64__
	#define configTICK_TYPE_WIDTH_IN_BITS		TICK_TYPE_WIDTH_64_BITS
#else
	#define configTICK_TYPE_WIDTH_IN_BITS		TICK_TYPE_WIDTH_32_BITS
#endif


/* Software timer related configuration options.  The maximum possible task
priority is configMAX_PRIORITIES - 1.  The priority of the timer task is
deliberately set higher to ensure it is correctly capped back to
configMAX_PRIORITIES - 1. */
#define configUSE_TIMERS						1
#define configTIMER_TASK_PRIORITY				( configMAX_PRIORITIES - 1 )
#define configTIMER_QUEUE_LENGTH				20
#define configTIMER_TASK_STACK_DEPTH			( configMINIMAL_STACK_SIZE * 2 )

#define configMAX_PRIORITIES					( 7 )

/* Run time stats gathering configuration options. */
unsigned long ulGetRunTimeCounterValue( void ); /* Prototype of function that returns run time counter. */
void vConfigureTimerForRunTimeStats( void );	/* Prototype of function that initialises the run time counter. */
#define configGENERATE_RUN_TIME_STATS			1
#define portCONFIGURE_TIMER_FOR_RUN_TIME_STATS() vConfigureTimerForRunTimeStats()
#define portGET_RUN_TIME_COUNTER_VALUE() ulGetRunTimeCounterValue()

/* Co-routine related configuration options. */
#define configUSE_CO_ROUTINES 					0
#define configMAX_CO_ROUTINE_PRIORITIES			( 2 )

/* This demo can use of one or more example stats formatting functions.  These
format the raw data provided by the uxTaskGetSystemState() function in to human
readable ASCII form.  See the notes in the implementation of vTaskList() within
FreeRTOS/Source/tasks.c for limitations. */
#define configUSE_STATS_FORMATTING_FUNCTIONS	0

/* Enables the test whereby a stack larger than the total heap size is
requested. */
#define configSTACK_DEPTH_TYPE uint32_t

/* Set the following definitions to 1 to include the API function, or zero
to exclude the API function.  In most cases the linker will remove unused
functions anyway. */
#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
#define INCLUDE_uxTaskGetStackHighWaterMark		1
#define INCLUDE_uxTaskGetStackHighWaterMark2	1
#define INCLUDE_xTaskGetSchedulerState			1
#define INCLUDE_xTimerGetTimerDaemonTaskHandle	1
#define INCLUDE_xTaskGetIdleTaskHandle			1
#define INCLUDE_xTaskGetHandle					1
#define INCLUDE_eTaskGetState					1
#define INCLUDE_xSemaphoreGetMutexHolder		1
#define INCLUDE_xTimerPendFunctionCall			1
#define INCLUDE_xTaskAbortDelay					1

#define configINCLUDE_MESSAGE_BUFFER_AMP_DEMO	0
#if ( configINCLUDE_MESSAGE_BUFFER_AMP_DEMO == 1 )
	extern void vGenerateCoreBInterrupt( void * xUpdatedMessageBuffer );
	#define sbSEND_COMPLETED( pxStreamBuffer ) vGenerateCoreBInterrupt( pxStreamBuffer )
#endif /* configINCLUDE_MESSAGE_BUFFER_AMP_DEMO */

extern void vAssertCalled( unsigned long ulLine, const char * const pcFileName );

/* projCOVERAGE_TEST should be defined on the command line so this file can be
used with multiple project configurations.  If it is
 */
#ifndef projCOVERAGE_TEST
	#error projCOVERAGE_TEST should be defined to 1 or 0 on the command line.
#endif

#if( projCOVERAGE_TEST == 1 )
	/* Insert NOPs in empty decision paths to ensure both true and false paths
	are being tested. */
	#define mtCOVERAGE_TEST_MARKER() __asm volatile( "NOP" )

	/* Ensure the tick count overflows during the coverage test. */
	#if( configTICK_TYPE_WIDTH_IN_BITS == TICK_TYPE_WIDTH_64_BITS )
		#define configINITIAL_TICK_COUNT 0xffffffffffffd800ULL
	#else
		#define configINITIAL_TICK_COUNT 0xffffd800UL
	#endif

	/* Allows tests of trying to allocate more than the heap has free. */
	#define configUSE_MALLOC_FAILED_HOOK			0

	/* To test builds that remove the static qualifier for debug builds. */
	#define portREMOVE_STATIC_QUALIFIER
#else
	/* It is a good idea to define configASSERT() while developing.  configASSERT()
	uses the same semantics as the standard C assert() macro.  Don't define
	configASSERT() when performing code coverage tests though, as it is not
	intended to asserts() to fail, some some code is intended not to run if no
	errors are present. */
	#define configASSERT( x ) if( ( x ) == 0 ) vAssertCalled( __LINE__, __FILE__ )

	#define configUSE_MALLOC_FAILED_HOOK			1

	/* Include the FreeRTOS+Trace FreeRTOS trace macro definitions. */
	// #include "trcRecorder.h"
#endif


#endif /* FREERTOS_CONFIG_H */
  • 接下来就是 sim/main.c,也就是主程序 main 函数,当前创建一个 FreeRTOS 的 task,如果 task 正常运行,就说明 FreeRTOS 在 PC 端适配初步成功
c 复制代码
#include <stdio.h>
#include "FreeRTOS.h"
#include "task.h"

#define TEST_01_TASK_PRIORITY       2
#define TEST_01_STACK_SIZE          1024

static void test_01( void * pvParameters )
{
    uint32_t cnt = 0;

    while (1)
    {
        vTaskDelay( 2000 * portTICK_PERIOD_MS );
        printf("%s : cnt = %d\n", __func__, cnt++);
    }
}

int main(int argc, char *argv[])
{
    xTaskCreate( test_01, "test_01", TEST_01_STACK_SIZE,
        NULL, TEST_01_TASK_PRIORITY, NULL );
    /* Start the scheduler itself. */
    vTaskStartScheduler();
    return 0;
}

编译与运行

  • clion 中 【同步】Cmake,然后进行编译,如果链接失败,注意确认
c 复制代码
target_link_libraries(rtos-sim
    winmm
)
  • windows 上链接 winmm lib

  • 正常编译链接后,运行效果如下

  • 说明 FreeRTOS clion cmake 编译,然后成功运行在 windows 上了。后续一些 FreeRTOS 的开发与调试,比如开发一些组件,可以基于 windows 进行【模拟】调试验证,非常的方便,不需要实际的硬件开发板与调试器。
相关推荐
AI 智能服务10 小时前
第6课__本地工具调用(文件操作)
服务器·人工智能·windows·php
IDC02_FEIYA11 小时前
SQL Server 2025数据库安装图文教程(附SQL Server2025数据库下载安装包)
数据库·windows
Kevin Wang72712 小时前
欧拉系统服务部署注意事项
网络·windows
取个名字太难了a14 小时前
初始化APC
windows
C++ 老炮儿的技术栈15 小时前
什么是通信规约
开发语言·数据结构·c++·windows·算法·安全·链表
Ankie Wan16 小时前
windows技巧:要将 Windows 资源管理器的默认查看方式设置为详细信息
windows·windows11·效率提升·文件夹·windows技巧·详细信息
ayaya_mana18 小时前
VS Code 远程开发:SSH连接与远程资源管理器的配置
linux·ide·windows·vscode·远程资源管理
龙潜月七18 小时前
做一个背单词的脚本
数据库·windows·c#·aigc·程序那些事
ohoy18 小时前
RedisTemplate 使用之List
数据结构·windows·list