** 1、FreeRTOS**
FreeRTOS是一個迷你的實時操作系統內核。作為一個輕量級的操作系統,功能包括:任務管理、時間管理、信號量、消息隊列、內存管理、記錄功能、軟件定時器、協程等,可基本滿足較小系統的需要。由于RTOS需占用一定的系統資源(尤其是RAM資源),只有μC/OS-II、embOS、salvo、FreeRTOS等少數實時操作系統能在小RAM單片機上運行。相對μC/OS-II、embOS等商業操作系統,FreeRTOS操作系統是完全免費的操作系統,具有源碼公開、可移植、可裁減、調度策略靈活的特點,可以方便地移植到各種單片機上運行。
2、移植
(1)第一步:創建工程
修改時鐘
(2)第二步:LED配置
(3)第三步:FreeRTOS配置
FreeRTOS公共部分配置
LED線程配置
(4)第四部:任務入口函數編寫
#include "led_task.h"
#include "led.h"
/* led entry function */
/* pvParameters contains TaskHandle_t */
void led_task_entry(void * pvParameters)
{
FSP_PARAMETER_NOT_USED(pvParameters);
/* TODO: add your own code here */
while(1)
{
led_on();
vTaskDelay(750);
led_off();
vTaskDelay(750);
}
}
(5)第五步:主函數處理
/* generated main source file - do not edit */
#include "bsp_api.h"
#include "FreeRTOS.h"
#include "task.h"
#include "semphr.h"
extern void prt_task_create(void);
extern TaskHandle_t prt_task;
extern void led_task_create(void);
extern TaskHandle_t led_task;
uint32_t g_fsp_common_thread_count;
bool g_fsp_common_initialized;
SemaphoreHandle_t g_fsp_common_initialized_semaphore;
#if configSUPPORT_STAT IC _ALLOCATION
StaticSemaphore_t g_fsp_common_initialized_semaphore_memory;
#endif
void g_hal_init(void);
/** Weak reference for tx_err_callback */
#if defined(__ICCARM__)
#define rtos_startup_err_callback_WEAK_ATTRIBUTE
#pragma weak rtos_startup_err_callback = rtos_startup_err_callback_internal
#elif defined(__GNUC__)
#define rtos_startup_err_callback_WEAK_ATTRIBUTE __attribute__ ((weak, alias("rtos_startup_err_callback_internal")))
#endif
void rtos_startup_err_callback_internal(void * p_instance, void * p_data);
void rtos_startup_err_callback(void * p_instance, void * p_data) rtos_startup_err_callback_WEAK_ATTRIBUTE;
/*********************************************************************************************************************
* @brief This is a weak example initialization error function. It should be overridden by defining a user function
* with the prototype below.
* - void rtos_startup_err_callback(void * p_instance, void * p_data)
*
* @param[in] p_instance arguments used to identify which instance caused the error and p_data Callback arguments used to identify what error caused the callback.
**********************************************************************************************************************/
void rtos_startup_err_callback_internal(void * p_instance, void * p_data);
void rtos_startup_err_callback_internal(void * p_instance, void * p_data)
{
/** Suppress compiler warning for not using parameters. */
FSP_PARAMETER_NOT_USED(p_instance);
FSP_PARAMETER_NOT_USED(p_data);
/** An error has occurred. Please check function arguments for more information. */
BSP_CFG_HANDLE_UNRECOVERABLE_ERROR(0);
}
void rtos_startup_common_init(void);
void rtos_startup_common_init(void)
{
/* First thread will take care of common initialization. */
BaseType_t err;
err = xSemaphoreTake(g_fsp_common_initialized_semaphore, portMAX_DELAY);
if (pdPASS != err)
{
/* Check err, problem occurred. */
rtos_startup_err_callback(g_fsp_common_initialized_semaphore, 0);
}
/* Only perform common initialization if this is the first thread to execute. */
if (false == g_fsp_common_initialized)
{
/* Later threads will not run this code. */
g_fsp_common_initialized = true;
/* Perform common module initialization. */
g_hal_init();
/* Now that common initialization is done, let other threads through. */
/* First decrement by 1 since 1 thread has already come through. */
g_fsp_common_thread_count--;
while (g_fsp_common_thread_count > 0)
{
err = xSemaphoreGive(g_fsp_common_initialized_semaphore);
if (pdPASS != err)
{
/* Check err, problem occurred. */
rtos_startup_err_callback(g_fsp_common_initialized_semaphore, 0);
}
g_fsp_common_thread_count--;
}
}
}
int main(void)
{
g_fsp_common_thread_count = 0;
g_fsp_common_initialized = false;
/* Create semaphore to make sure common init is done before threads start running. */
g_fsp_common_initialized_semaphore =
#