上回书说到我们向工程中添加了main.c和FreeRTOSConfig.h文件,但是内容是什么不知道,这篇博客就来说说这两个文件的内容。
作者:wangyijieonline
链接:https://blog.csdn.net/wangyijieonline/article/details/109715678
来源:CSDN
著作权归作者所有。商业转载请联系作者获得授权,非商业转载必须注明出处。
FreeRTOSConfig.h其实是一个backup或者把它当作一个提出不同意见的人,这里面的的选项在 ./FreeRTOS/Source/include/FreeRTOS.h
文件里面都有默认的配置,而在这里面做的大部分是一个和下面类似的操作:
#ifndef config...
#define config...
#endif
所以看到这里就明白了,这个文件如果没有的话也是可以的,那么就会调用FreeRTOS.h
里的默认操作。
那么重点来了,具体有哪些需要关注的点呢?我们需要一个模板,这里拿之前删掉的./FreeRTOS/Demo/RISC-V_RV32_SiFive_HiFive1-RevB_IAR/FreeRTOSConfig.h
为例,这里很多人又要问了,不是之前整个Demo文件夹删了,现在怎么又拿出来了?没办法啊,用的别人的东西,要拿别人的做参考啊。还有一个小Tip,这里的声明中提到这个文件的版本是比较新的
不像./FreeRTOS/RISC-V_RV32M1_Vega_GCC_Eclipse/projects/RTOSDemo_ri5cy/FreeRTOSConfig.h
,这个估计都好几年没人维护了
有人可能觉得这个可能是忘了改了,所以说这也反映出一些细节问题。
已经删掉了也不用再去翻回收站了,我直接拿过来粘在下面:
/*
* FreeRTOS V202011.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.
*
* http://www.FreeRTOS.org
* http://aws.amazon.com/freertos
*
* 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.
*----------------------------------------------------------*/
#define configISR_STACK_SIZE_WORDS ( 200 ) //(1)
#define CLINT_CTRL_ADDR ( 0x02000000UL ) //(1)
#define configMTIME_BASE_ADDRESS ( CLINT_CTRL_ADDR + 0xBFF8UL ) //(1)
#define configMTIMECMP_BASE_ADDRESS ( CLINT_CTRL_ADDR + 0x4000UL ) //(1)
#define configUSE_PREEMPTION 1 //(2)
#define configUSE_IDLE_HOOK 0 //(3)
#define configUSE_TICK_HOOK 1 //(3)
#define configCPU_CLOCK_HZ ( ( uint32_t ) ( 32768 ) ) //(1)
#define configTICK_RATE_HZ ( ( TickType_t ) 1000 )
#define configMAX_PRIORITIES ( 7 ) //(4)
#define configMINIMAL_STACK_SIZE ( ( uint32_t ) 100 ) /* Can be as low as 60 but some of the demo tasks that use this constant require it to be higher. */
#define configTOTAL_HEAP_SIZE ( ( size_t ) ( 12 * 1024 ) )
#define configMAX_TASK_NAME_LEN ( 16 )
#define configUSE_TRACE_FACILITY 0
#define configUSE_16_BIT_TICKS 0
#define configIDLE_SHOULD_YIELD 0
#define configUSE_MUTEXES 1
#define configQUEUE_REGISTRY_SIZE 8
#define configCHECK_FOR_STACK_OVERFLOW 2 //(3)
#define configUSE_RECURSIVE_MUTEXES 1
#define configUSE_MALLOC_FAILED_HOOK 1 //(3)
#define configUSE_APPLICATION_TASK_TAG 0
#define configUSE_COUNTING_SEMAPHORES 1
//ADD START//
#define configUSE_TASK_NOTIFICATIONS 1
#define configUSE_QUEUE_SETS 0
//ADD END///
#define configGENERATE_RUN_TIME_STATS 0
/* Co-routine definitions. */
#define configUSE_CO_ROUTINES 0
#define configMAX_CO_ROUTINE_PRIORITIES ( 2 )
/* Software timer definitions. */
#define configUSE_TIMERS 1
#define configTIMER_TASK_PRIORITY ( configMAX_PRIORITIES - 1 )
#define configTIMER_QUEUE_LENGTH 4
#define configTIMER_TASK_STACK_DEPTH ( configMINIMAL_STACK_SIZE )
/* Task priorities. Allow these to be overridden. */
#ifndef uartPRIMARY_PRIORITY
#define uartPRIMARY_PRIORITY ( configMAX_PRIORITIES - 3 )
#endif
/* 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 1
#define INCLUDE_vTaskSuspend 1
#define INCLUDE_vTaskDelayUntil 1
#define INCLUDE_vTaskDelay 1
#define INCLUDE_eTaskGetState 1
#define INCLUDE_xTimerPendFunctionCall 1
#define INCLUDE_xTaskAbortDelay 1
#define INCLUDE_xTaskGetHandle 1
#define INCLUDE_xSemaphoreGetMutexHolder 1
/* Normal assert() semantics without relying on the provision of an assert.h
header file. */
#define configASSERT( x ) if( ( x ) == 0 ) { taskDISABLE_INTERRUPTS(); __asm volatile( "ebreak" ); for( ;; ); }
ADD START
/* The size of the global output buffer that is available for use when there
are multiple command interpreters running at once (for example, one on a UART
and one on TCP/IP). This is done to prevent an output buffer being defined by
each implementation - which would waste RAM. In this case, there is only one
command interpreter running. */
/* The buffer into which output generated by FreeRTOS+CLI is placed. This must
be at least big enough to contain the output of the task-stats command, as the
example implementation does not include buffer overlow checking. */
#define configCOMMAND_INT_MAX_OUTPUT_SIZE 2096
#define configINCLUDE_QUERY_HEAP_COMMAND 1
/* This file is included from assembler files - make sure C code is not included
in assembler files. */
#ifndef __ASSEMBLER__
void vAssertCalled( const char * pcFile, unsigned long ulLine );
void vConfigureTickInterrupt( void );
void vClearTickInterrupt( void );
void vPreSleepProcessing( unsigned long uxExpectedIdleTime );
void vPostSleepProcessing( unsigned long uxExpectedIdleTime );
#endif /* __ASSEMBLER__ */
/****** Hardware/compiler specific settings. *******************************************/
#if 1
/*
* The application must provide a function that configures a peripheral to
* create the FreeRTOS tick interrupt, then define configSETUP_TICK_INTERRUPT()
* in FreeRTOSConfig.h to call the function.
*/
#define configSETUP_TICK_INTERRUPT() vConfigureTickInterrupt()
#define configCLEAR_TICK_INTERRUPT() vClearTickInterrupt()
#endif
/* The configPRE_SLEEP_PROCESSING() and configPOST_SLEEP_PROCESSING() macros
allow the application writer to add additional code before and after the MCU is
placed into the low power state respectively. The empty implementations
provided in this demo can be extended to save even more power. */
#define configPRE_SLEEP_PROCESSING( uxExpectedIdleTime ) vPreSleepProcessing( uxExpectedIdleTime );
#define configPOST_SLEEP_PROCESSING( uxExpectedIdleTime ) vPostSleepProcessing( uxExpectedIdleTime );
/* Compiler specifics. */
#define fabs( x ) __builtin_fabs( x )
/* Enable Hardware Stack Protection and Recording mechanism. */
#define configHSP_ENABLE 0
/* Record the highest address of stack. */
#if(configHSP_ENABLE == 1 && configRECORD_STACK_HIGH_ADDRESS != 1)
#define configRECORD_STACK_HIGH_ADDRESS 1
#endif
/ADD END
#endif /* FREERTOS_CONFIG_H */
仔细看过以后,发现这个模板还是有点乱,一些HOOK和Function的defination都挤在一起了。
我重新整理了一个放在gitee上了,有兴趣可以看下。
强调一下,这里只讨论方法,不针对任何一款MCU,如果有人想在这几篇blog或者git里复制粘贴几下就把freertos porting好了,那我写这几篇blog也是极其失败的!
其中标注//(1)
的是需要重点根据自己系统调整的,
好了,这样就可以了,是不是很简单。
说到int main( void ),我们都很熟悉,是约定俗成的主程序的入口,当然不例外, 也是我们的入口 ,但是main.c也是一个上层函数的概念,这也就是说如果main函数之前的工作都做的完善了,那么应该进入main()以后,执行应该很顺畅,具体的code可以参考我的gitee仓库。
这个没什么好说的,其中包含了一些HOOK(钩子),出错或者idle之类的时候就调进去了(别小看idle进程,用来统计操作系统哦的loading很好使),此外,创建了两个最简单的任务,现象就是任务1和任务2交替打印一些字符出来,表明我们的多任务已经跑起来了,简单到都不需要操作系统,直接while就可以完成。
直接贴在下面:
/*
* FreeRTOS Kernel V10.1.1
* Copyright (C) 2018 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.
*
* http://www.FreeRTOS.org
* http://aws.amazon.com/freertos
*
* 1 tab == 4 spaces!
*/
/* Standard includes. */
#include
/* Kernel includes. */
#include "FreeRTOS.h"
#include "task.h"
/* Platfrom includes. */
#include "platform.h"
/*
* Configure the hardware as necessary to run this demo.
*/
static void prvSetupHardware( void );
static void AppTaskCreate (void);
/* Prototypes for the standard FreeRTOS callback/hook functions implemented
within this file. */
void vApplicationMallocFailedHook( void );
void vApplicationIdleHook( void );
void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName );
void vApplicationTickHook( void );
/*-----------------------------------------------------------*/
#define CUSTOMER_TEST
int main( void )
{
/* Configure the hardware ready to run the demo. */
prvSetupHardware();
#ifdef CUSTOMER_TEST
AppTaskCreate();
// start the task
vTaskStartScheduler();
#endif
for( ;; );
/* Don't expect to reach here. */
return 0;
}
#ifdef CUSTOMER_TEST
static TaskHandle_t xHandleTaskB = NULL;
static TaskHandle_t xHandleTaskC = NULL;
static void vTaskB(void *pvParameters)
{
static uint16_t TaskBnum=0;
while(1)
{
TaskBnum++;
printf("Task B : %d \r\n",TaskBnum);
vTaskDelay(500);
}
}
static void vTaskC(void *pvParameters)
{
static uint16_t TaskCnum=0;
while(1)
{
TaskCnum++;
printf("Task C : %d \r\n",TaskCnum);
vTaskDelay(500);//退出调度500个ticks
}
}
static void AppTaskCreate (void)
{
xTaskCreate(
vTaskB, /* 任务函数 */
"vTaskB", /* 任务名 */
128, /* 任务栈大小,单位 word,也就是 4 字节 */
NULL, /* 任务参数 */
4, /* 任务优先级*/
&xHandleTaskB /* 任务句柄 */
);
xTaskCreate(
vTaskC,
"vTaskC",
128,
NULL,
3,
&xHandleTaskC
);
}
#endif
/*-----------------------------------------------------------*/
static void prvSetupHardware( void )
{
/* Ensure no interrupts execute while the scheduler is in an inconsistent
state. Interrupts are automatically enabled when the scheduler is
started. */
portDISABLE_INTERRUPTS();
/* Enable UART port to output messages. */
uart_print_config();
}
/*-----------------------------------------------------------*/
void vAssertCalled( const char * pcFile, unsigned long ulLine )
{
volatile unsigned long ul = 0;
( void ) pcFile;
( void ) ulLine;
printf( "ASSERT! Line %d, file %s\r\n", ( int )ulLine, pcFile );
taskENTER_CRITICAL();
{
/* Set ul to a non-zero value using the debugger to step out of this
function. */
while( ul == 0 )
{
portNOP();
}
}
taskEXIT_CRITICAL();
}
/*-----------------------------------------------------------*/
void vApplicationMallocFailedHook( void )
{
/* vApplicationMallocFailedHook() will only be called if
configUSE_MALLOC_FAILED_HOOK is set to 1 in FreeRTOSConfig.h. It is a hook
function that will get called if a call to pvPortMalloc() fails.
pvPortMalloc() is called internally by the kernel whenever a task, queue,
timer or semaphore is created. It is also called by various parts of the
demo application. If heap_1.c, heap_2.c or heap_4.c is being used, then the
size of the heap available to pvPortMalloc() is defined by
configTOTAL_HEAP_SIZE in FreeRTOSConfig.h, and the xPortGetFreeHeapSize()
API function can be used to query the size of free heap space that remains
(although it does not provide information on how the remaining heap might be
fragmented). See http://www.freertos.org/a00111.html for more
information. */
vAssertCalled( __FILE__, __LINE__ );
}
/*-----------------------------------------------------------*/
void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName )
{
( void ) pcTaskName;
( void ) pxTask;
/* Run time stack overflow checking is performed if
configCHECK_FOR_STACK_OVERFLOW is defined to 1 or 2. This hook
function is called if a stack overflow is detected. */
vAssertCalled( __FILE__, __LINE__ );
}
/*-----------------------------------------------------------*/
void vApplicationIdleHook( void )
{
volatile size_t xFreeHeapSpace;
/* This is just a trivial example of an idle hook. It is called on each
cycle of the idle task. It must *NOT* attempt to block. In this case the
idle task just queries the amount of FreeRTOS heap that remains. See the
memory management section on the http://www.FreeRTOS.org web site for memory
management options. If there is a lot of heap memory free then the
configTOTAL_HEAP_SIZE value in FreeRTOSConfig.h can be reduced to free up
RAM. */
xFreeHeapSpace = xPortGetFreeHeapSize();
/* Remove compiler warning about xFreeHeapSpace being set but never used. */
( void ) xFreeHeapSpace;
}
/*-----------------------------------------------------------*/
void vApplicationTickHook( void )
{
#if( mainSELECTED_APPLICATION == 1 )
{
/* Only the comprehensive demo actually uses the tick hook. */
extern void vFullDemoTickHook( void );
vFullDemoTickHook();
}
#endif
}
/*-----------------------------------------------------------*/
/* configUSE_STATIC_ALLOCATION is set to 1, so the application must provide an
implementation of vApplicationGetIdleTaskMemory() to provide the memory that is
used by the Idle task. */
void vApplicationGetIdleTaskMemory( StaticTask_t **ppxIdleTaskTCBBuffer, StackType_t **ppxIdleTaskStackBuffer, uint32_t *pulIdleTaskStackSize )
{
/* If the buffers to be provided to the Idle task are declared inside this
function then they must be declared static - otherwise they will be allocated on
the stack and so not exists after this function exits. */
static StaticTask_t xIdleTaskTCB;
static StackType_t uxIdleTaskStack[ configMINIMAL_STACK_SIZE ];
/* Pass out a pointer to the StaticTask_t structure in which the Idle task's
state will be stored. */
*ppxIdleTaskTCBBuffer = &xIdleTaskTCB;
/* Pass out the array that will be used as the Idle task's stack. */
*ppxIdleTaskStackBuffer = uxIdleTaskStack;
/* Pass out the size of the array pointed to by *ppxIdleTaskStackBuffer.
Note that, as the array is necessarily of type StackType_t,
configMINIMAL_STACK_SIZE is specified in words, not bytes. */
*pulIdleTaskStackSize = configMINIMAL_STACK_SIZE;
}
/*-----------------------------------------------------------*/
/* configUSE_STATIC_ALLOCATION and configUSE_TIMERS are both set to 1, so the
application must provide an implementation of vApplicationGetTimerTaskMemory()
to provide the memory that is used by the Timer service task. */
void vApplicationGetTimerTaskMemory( StaticTask_t **ppxTimerTaskTCBBuffer, StackType_t **ppxTimerTaskStackBuffer, uint32_t *pulTimerTaskStackSize )
{
/* If the buffers to be provided to the Timer task are declared inside this
function then they must be declared static - otherwise they will be allocated on
the stack and so not exists after this function exits. */
static StaticTask_t xTimerTaskTCB;
static StackType_t uxTimerTaskStack[ configTIMER_TASK_STACK_DEPTH ];
/* Pass out a pointer to the StaticTask_t structure in which the Timer
task's state will be stored. */
*ppxTimerTaskTCBBuffer = &xTimerTaskTCB;
/* Pass out the array that will be used as the Timer task's stack. */
*ppxTimerTaskStackBuffer = uxTimerTaskStack;
/* Pass out the size of the array pointed to by *ppxTimerTaskStackBuffer.
Note that, as the array is necessarily of type StackType_t,
configMINIMAL_STACK_SIZE is specified in words, not bytes. */
*pulTimerTaskStackSize = configTIMER_TASK_STACK_DEPTH;
}
/*-----------------------------------------------------------*/
void vPreSleepProcessing( unsigned long uxModifiableIdleTime )
{
/* Called by the kernel before it places the MCU into a sleep mode because
configPRE_SLEEP_PROCESSING() is #defined to vPreSleepProcessing().
NOTE: Additional actions can be taken here to get the power consumption
even lower. For example, peripherals can be turned off here, and then back
on again in the post sleep processing function. For maximum power saving
ensure all unused pins are in their lowest power state. */
/* Avoid compiler warnings about the unused parameter. */
( void ) uxModifiableIdleTime;
}
/*-----------------------------------------------------------*/
void vPostSleepProcessing( unsigned long uxModifiableIdleTime )
{
/* Called by the kernel when the MCU exits a sleep mode because
configPOST_SLEEP_PROCESSING is #defined to vPostSleepProcessing(). */
/* Avoid compiler warnings about the unused parameter. */
( void ) uxModifiableIdleTime;
}
这篇文章主要是完成了上回遗留的一点问题,随着我们冒险的深入,越来越大的谜题即将解开,敬请关注下面的文章。