FreeRTOS 是一个迷你的实时操作系统内核。作为一个轻量级的操作系统,功能包括:任务管理、时间管理、信号量、消息队列、内存管理、记录功能、软件定时器、协程等,可基本满
足较小系统的需要。 由于 RTOS 需占用一定的系统资源(尤其是 RAM 资源),只有 μC/OS-II、
embOS、salvo、FreeRTOS 等少数实时操作系统能在小 RAM 单片机上运行。相对 μC/OS-II、embOS 等商业操作系统,FreeRTOS 操作系统是完全免费的操作系统,具有源码公开、可移植、可裁减、调度策略灵活的特点,可以方便地移植到各种单片机上运行,其最新版本为 10.4.4 版。
/* Task to be created. */
void vTaskCode( void * pvParameters )
{
/* The parameter value is expected to be 1 as 1 is passed in the
pvParameters value in the call to xTaskCreate() below.
configASSERT( ( ( uint32_t ) pvParameters ) == 1 );
for( ;; )
{
/* Task code goes here. */
}
}
/* Function that creates a task. */
void vOtherFunction( void )
{
BaseType_t xReturned;
TaskHandle_t xHandle = NULL;
/* Create the task, storing the handle. */
xReturned = xTaskCreate(
vTaskCode, /* Function that implements the task. */
"NAME", /* Text name for the task. */
STACK_SIZE, /* Stack size in words, not bytes. */
( void * ) 1, /* Parameter passed into the task. */
tskIDLE_PRIORITY,/* Priority at which the task is created. */
&xHandle ); /* Used to pass out the created task's handle. */
if( xReturned == pdPASS )
{
/* The task was created. Use the task's handle to delete the task. */
vTaskDelete( xHandle );
}
}
void vTaskDelete(TaskHandle_t xTaskToDelete);
- 只需将待删除的任务句柄传入该函数,即可将该任务删除。
- 当传入的参数为NULL,则代表删除任务自身(当前正在运行的任务)。
//freertos.c
/* USER CODE END Header_StartTaskLED1 */
void StartTaskLED1(void const * argument)
{
/* USER CODE BEGIN StartTaskLED1 */
/* Infinite loop */
for(;;)
{
HAL_GPIO_TogglePin(GPIOB,GPIO_PIN_8);
osDelay(500);
}
/* USER CODE END StartTaskLED1 */
}
/* USER CODE END Header_StartTaskLED2 */
void StartTaskLED2(void const * argument)
{
/* USER CODE BEGIN StartTaskLED2 */
/* Infinite loop */
for(;;)
{
HAL_GPIO_TogglePin(GPIOB,GPIO_PIN_9);
osDelay(1000);
}
/* USER CODE END StartTaskLED2 */
}
//freertos.c
void StartTaskKEY1(void const * argument)
{
/* USER CODE BEGIN StartTaskKEY1 */
/* Infinite loop */
for(;;)
{
if(HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_0) == GPIO_PIN_RESET)
{
osDelay(20);
if(HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_0) == GPIO_PIN_RESET)//按键消抖
{
printf("KEY1按下!\r\n");
if(taskLED1Handle == NULL)
{
printf("任务1不存在,准备创建任务1\r\n");
osThreadDef(taskLED1, StartTaskLED1, osPriorityNormal, 0, 128);
taskLED1Handle = osThreadCreate(osThread(taskLED1), NULL);
if(taskLED1Handle != NULL)
printf("任务1创建完成\r\n");
}
else
{
printf("删除任务1\r\n");
osThreadTerminate(taskLED1Handle);
taskLED1Handle = NULL;
}
}
while(HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_0) == GPIO_PIN_RESET);//防止按下时多次一直打印
}
osDelay(10);
}
/* USER CODE END StartTaskKEY1 */
}
/* USER CODE END Header_StartTaskKEY2 */
void StartTaskKEY2(void const * argument)
{
/* USER CODE BEGIN StartTaskKEY2 */
static int flag = 0;
/* Infinite loop */
for(;;)
{
if(HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_1) == GPIO_PIN_RESET)
{
osDelay(20);
if(HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_1) == GPIO_PIN_RESET)
{
printf("KEY2按下!\r\n");
if(flag == 0)
{
osThreadSuspend(taskLED2Handle);
printf("任务2已暂停\r\n");
flag = 1;
}
else
{
osThreadResume(taskLED2Handle);
printf("任务2已恢复\r\n");
flag = 0;
}
}
while(HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_1) == GPIO_PIN_RESET);//防止按下时多次一直打印
}
osDelay(10);
}
/* USER CODE END StartTaskKEY2 */
}
QueueHandle_t xQueueCreate( UBaseType_t uxQueueLength, UBaseType_t uxItemSize );
BaseType_t xQueueSend(
QueueHandle_t xQueue,
const void * pvItemToQueue,
TickType_t xTicksToWait
);
BaseType_t xQueueReceive(
QueueHandle_t xQueue,
void *pvBuffer,
TickType_t xTicksToWait
);
//freertos.c
void StartTaskSend(void const * argument)
{
/* USER CODE BEGIN StartTaskSend */
uint16_t buf = 99;
BaseType_t status;
/* Infinite loop */
for(;;)
{
if(HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_0) == GPIO_PIN_RESET)
{
osDelay(20);
if(HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_0) == GPIO_PIN_RESET)
{
status = xQueueSend(myQueueHandle, &buf, 0);
if(status == pdTRUE)
printf("写入队列成功,写入值为%d\r\n", buf);
else
printf("写入队列失败\r\n");
}
while(HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_0) == GPIO_PIN_RESET);//防止按下时多次一直打印
}
osDelay(10);
}
/* USER CODE END StartTaskSend */
}
void StartTaskReceive(void const * argument)
{
/* USER CODE BEGIN StartTaskReceive */
uint16_t buf;
BaseType_t status;
/* Infinite loop */
for(;;)
{
if(HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_1) == GPIO_PIN_RESET)
{
osDelay(20);
if(HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_1) == GPIO_PIN_RESET)
{
status = xQueueReceive(myQueueHandle, &buf, 0);
if(status == pdTRUE)
printf("读取队列成功,读出值为%d\r\n", buf);
else
printf("读取队列失败\r\n");
}
while(HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_1) == GPIO_PIN_RESET);//防止按下时多次一直打印
}
osDelay(10);
}
/* USER CODE END StartTaskReceive */
}