1.准备工作2.新建文件夹及复制必要文件3.添加c文件4.配置运行环境5.代码修改
*//void SVC_Handler(void)//{//}*//void PendSV_Handler(void)//{//}*/*在c文件中添加如下代码*//*freeRTOS中的systick中断函数*/extern void xPortSysTickHandler(void);void SysTick_Handler(void){#if(INCLUDE_xTaskGetSchedulerState==1)if(xTaskGetSchedulerState()!=taskSCHEDULER_NOT_STARTED){#endifxPortSysTickHandler();#if (INCLUDE_xTaskGetSchedulerState==1) }#endif}
/*FreeRTOS与中断服务函数的配置选项*/#define xPortPendSVHandler PendSV_Handler#define vPortSVCHandler SVC_Handler/*启用软件定时器相关宏定义*/#define configUSE_TIMERS 1 #define configTIMER_TASK_PRIORITY (configMAX_PRIORITIES-1) #define configTIMER_QUEUE_LENGTH 10 #define configTIMER_TASK_STACK_DEPTH (configMINIMAL_STACK_SIZE*2)
#include "FreeRTOS.h"#include "task.h"#include "bsp_led.h"#include "bsp_usart.h"#include "bsp_key.h"#include "queue.h"#include "semphr.h"#include "event_groups.h"#include "timers.h"/*Function definition*/static void BSP_Init(void);static void Send_Task(void* pvParameters);static void Callback_LEDTOGGLE(void* pvParameters);static void AppTaskCreate(void);/*Task Handle*/static TaskHandle_t AppTaskCreate_Handle=NULL;static TaskHandle_t Send_Task_Handle=NULL; //PRO:3/*Timer handle*/static TimerHandle_t Timer1=NULL;static void Callback_LEDTOGGLE(void* pvParameters){LED1_TOGGLE;printf("TickValue=%d\n",(TickType_t)xTaskGetTickCount());}int main(void){BaseType_t xReturn =pdPASS;BSP_Init();printf("FreeRTOS creates tasks dynamically!\r\n");xReturn=xTaskCreate((TaskFunction_t)AppTaskCreate,\(const char *)"APPTASKCREATE",\(uint16_t)512,(void*)NULL,(UBaseType_t)1,\(TaskHandle_t*)&AppTaskCreate_Handle);if(pdPASS==xReturn)vTaskStartScheduler();elsereturn -1;while(1);}/*Hardware initialization*/static void BSP_Init(void){/*Use this priority grouping uniformly, never group again * 4 bits for pre-emption priority *//*led initialization*/LED_GPIO_Config();/*Serial port initialization*/USART_Config();/*Key initialization*/Key_GPIO_Config();}/*********** *@Function name:SEND_Task *@description:SEND_Task body *@Parameter:None *@return:None*************/static void Send_Task(void* pvParameters){while(1){vTaskDelay(100); //只是为了演示任务切换}}static void AppTaskCreate(void){BaseType_t xReturn=pdPASS;taskENTER_CRITICAL();/* Create Timer1 Handle*/Timer1=xTimerCreate((const char*)"1sec-toggle",(TickType_t)pdMS_TO_TICKS(1000),\(UBaseType_t)pdTRUE,(void*)1,(TimerCallbackFunction_t)Callback_LEDTOGGLE);if(NULL!=Timer1)printf("Timer1 created successfully.\r\n");xTimerStart(Timer1,0);/*SEND task*/xReturn=xTaskCreate((TaskFunction_t)Send_Task,\(const char*)"SEND_TASK",\(uint16_t)512,(void*)NULL,(UBaseType_t)3,\(TaskHandle_t)&Send_Task_Handle);if(pdPASS==xReturn)printf("\r\nCreate Send_Task success!\r\n");vTaskDelete(AppTaskCreate_Handle);taskEXIT_CRITICAL();}/*********************************************END OF FILE**********************/
原帖地址:https://blog.csdn.net/zrb2753/article/details/109560305?spm=1001.2014.3001.5501