基于freertos用C语言写出电脑串口发送指令”1111”, 单片机串口发送当前所有任务状态; 电脑串口发送指令”2222”,单片机串口发送当前CPU的使用率;
时间: 2023-12-31 17:06:32 浏览: 125
以下是基于FreeRTOS使用C语言编写的单片机代码:
```c
#include "FreeRTOS.h"
#include "task.h"
#include "queue.h"
#include "timers.h"
#include "stdio.h"
#define UART_TX_PIN 1 //假设串口TX引脚为1
#define UART_RX_PIN 2 //假设串口RX引脚为2
#define BAUD_RATE 9600 //波特率为9600
//任务状态枚举类型
typedef enum
{
RUNNING,
BLOCKED,
SUSPENDED,
READY,
DELETED
} task_status_t;
//任务信息结构体
typedef struct
{
char task_name[20];
task_status_t task_status;
uint32_t stack_size;
uint32_t stack_used;
} task_info_t;
//串口初始化函数
void uart_init(void)
{
//在这里进行串口初始化的操作,包括引脚设置、波特率设置等
}
//发送任务状态到串口
void send_task_status(void)
{
const char *status_str[] = {"RUNNING", "BLOCKED", "SUSPENDED", "READY", "DELETED"};
task_info_t task_info;
task_status_t task_status;
TaskHandle_t task_handle = NULL;
UBaseType_t task_num = uxTaskGetNumberOfTasks();
uint32_t i;
printf("Current Task Status:\r\n");
for (i = 0; i < task_num; i++)
{
task_handle = (TaskHandle_t)uxTaskGetTaskHandle(i);
if (task_handle != NULL)
{
task_info.stack_size = uxTaskGetStackHighWaterMark(task_handle);
task_info.stack_used = uxTaskGetStackHighWaterMark(task_handle);
task_info.task_status = eTaskGetState(task_handle);
vTaskGetInfo(task_handle, &task_info, pdTRUE, eInvalid);
printf("Task Name: %s, Status: %s, Stack Size: %u bytes, Stack Used: %u bytes\r\n",
task_info.task_name, status_str[task_info.task_status], task_info.stack_size, task_info.stack_used);
}
}
}
//计算CPU使用率
void calculate_cpu_usage(TimerHandle_t xTimer)
{
static uint32_t prev_tick_count = 0;
uint32_t current_tick_count = xTaskGetTickCount();
uint32_t tick_diff = current_tick_count - prev_tick_count;
uint32_t cpu_usage = 0;
prev_tick_count = current_tick_count;
//计算CPU使用率
cpu_usage = (tick_diff * 100) / ((uint32_t)configTICK_RATE_HZ);
printf("Current CPU Usage: %u%%\r\n", cpu_usage);
}
//串口接收中断处理函数
void uart_rx_isr(void)
{
char recv_char;
static char recv_buf[5] = {0};
static uint8_t recv_index = 0;
//从串口接收字符
recv_char = uart_receive_char();
//判断是否接收到了"1111"指令
if (recv_char == '1')
{
recv_buf[recv_index++] = recv_char;
if (recv_index == 4 && strcmp(recv_buf, "1111") == 0)
{
//发送当前所有任务状态
send_task_status();
}
}
else
{
recv_index = 0;
}
//判断是否接收到了"2222"指令
if (recv_char == '2')
{
recv_buf[recv_index++] = recv_char;
if (recv_index == 4 && strcmp(recv_buf, "2222") == 0)
{
//启动定时器计算CPU使用率
xTimerStartFromISR(xTimer, NULL);
}
}
else
{
recv_index = 0;
}
}
int main(void)
{
//初始化串口
uart_init();
//创建定时器
TimerHandle_t cpu_usage_timer = xTimerCreate("CPU Usage Timer", pdMS_TO_TICKS(1000), pdTRUE, NULL, calculate_cpu_usage);
//启动调度器
vTaskStartScheduler();
//定时器销毁
xTimerDelete(cpu_usage_timer, 0);
return 0;
}
```
在上面的代码中,我们首先定义了一个枚举类型`task_status_t`,用于表示任务的状态。然后定义了一个`task_info_t`结构体,用于记录任务的一些信息,包括任务名、任务状态、堆栈大小和已用堆栈大小。在`send_task_status()`函数中,我们通过调用FreeRTOS提供的API,获取当前所有任务的信息,并发送到串口。在`calculate_cpu_usage()`函数中,我们使用定时器来计算CPU使用率,具体实现是记录当前和上一次调用该函数时的时间戳,然后计算两个时间戳之间的差值,再根据差值和FreeRTOS提供的`configTICK_RATE_HZ`宏定义来计算CPU使用率。在`uart_rx_isr()`函数中,我们检测串口接收到的数据是否为字符串"1111"或"2222",如果是,则执行相应的操作,即发送当前所有任务状态或启动计算CPU使用率的定时器。
阅读全文