1、完成实验例程中综合实验; 2、在综合实验基础上对ucos系统进行裁剪,创建三个任务: 任务一:LED常亮; 任务二:LED交替闪烁,上按键触发; 任务三:LED灭,下按键触发。
时间: 2024-03-11 09:46:45 浏览: 118
1. 综合实验代码:
```c
#include "main.h"
#include "bsp_led.h"
#include "bsp_key.h"
#include "bsp_uart.h"
#include "bsp_delay.h"
#include "os.h"
#define TASK1_STACK_SIZE 256u
#define TASK2_STACK_SIZE 256u
#define TASK3_STACK_SIZE 256u
static OS_TCB Task1TCB;
static CPU_STK Task1Stack[TASK1_STACK_SIZE];
static void Task1(void* p_arg);
static OS_TCB Task2TCB;
static CPU_STK Task2Stack[TASK2_STACK_SIZE];
static void Task2(void* p_arg);
static OS_TCB Task3TCB;
static CPU_STK Task3Stack[TASK3_STACK_SIZE];
static void Task3(void* p_arg);
int main(void)
{
HAL_Init();
SystemClock_Config();
LED_GPIO_Init();
KEY_GPIO_Init();
UART_GPIO_Init();
Delay_Init();
OS_ERR err;
OSInit(&err);
if (err != OS_ERR_NONE) {
printf("OS init failed: %d\n", err);
while (1);
}
OSTaskCreate(&Task1TCB, "Task1", Task1, NULL, 2u, &Task1Stack[0], TASK1_STACK_SIZE / 10u, TASK1_STACK_SIZE, 0u, 0u, NULL, (OS_OPT_TASK_STK_CHK | OS_OPT_TASK_STK_CLR), &err);
if (err != OS_ERR_NONE) {
printf("Task1 create failed: %d\n", err);
while (1);
}
OSTaskCreate(&Task2TCB, "Task2", Task2, NULL, 3u, &Task2Stack[0], TASK2_STACK_SIZE / 10u, TASK2_STACK_SIZE, 0u, 0u, NULL, (OS_OPT_TASK_STK_CHK | OS_OPT_TASK_STK_CLR), &err);
if (err != OS_ERR_NONE) {
printf("Task2 create failed: %d\n", err);
while (1);
}
OSTaskCreate(&Task3TCB, "Task3", Task3, NULL, 4u, &Task3Stack[0], TASK3_STACK_SIZE / 10u, TASK3_STACK_SIZE, 0u, 0u, NULL, (OS_OPT_TASK_STK_CHK | OS_OPT_TASK_STK_CLR), &err);
if (err != OS_ERR_NONE) {
printf("Task3 create failed: %d\n", err);
while (1);
}
OSStart(&err);
if (err != OS_ERR_NONE) {
printf("OS start failed: %d\n", err);
while (1);
}
while (1);
}
void Task1(void* p_arg)
{
(void)p_arg;
while (1) {
LED_ON();
OSTimeDlyHMSM(0, 0, 1, 0, OS_OPT_TIME_HMSM_STRICT, NULL);
LED_OFF();
OSTimeDlyHMSM(0, 0, 1, 0, OS_OPT_TIME_HMSM_STRICT, NULL);
}
}
void Task2(void* p_arg)
{
(void)p_arg;
while (1) {
if (KEY1_PRESSED()) {
LED_TOGGLE();
OSTimeDlyHMSM(0, 0, 0, 500, OS_OPT_TIME_HMSM_STRICT, NULL);
} else {
OSTimeDlyHMSM(0, 0, 0, 10, OS_OPT_TIME_HMSM_STRICT, NULL);
}
}
}
void Task3(void* p_arg)
{
(void)p_arg;
while (1) {
if (KEY2_PRESSED()) {
LED_OFF();
OSTimeDlyHMSM(0, 0, 0, 500, OS_OPT_TIME_HMSM_STRICT, NULL);
} else {
OSTimeDlyHMSM(0, 0, 0, 10, OS_OPT_TIME_HMSM_STRICT, NULL);
}
}
}
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
{
Error_Handler();
}
}
```
2. 裁剪ucos系统后的代码:
```c
#include "main.h"
#include "bsp_led.h"
#include "bsp_key.h"
#include "bsp_uart.h"
#include "bsp_delay.h"
#include "os.h"
#define TASK1_STACK_SIZE 256u
#define TASK2_STACK_SIZE 256u
#define TASK3_STACK_SIZE 256u
static OS_TCB Task1TCB;
static CPU_STK Task1Stack[TASK1_STACK_SIZE];
static void Task1(void* p_arg);
static OS_TCB Task2TCB;
static CPU_STK Task2Stack[TASK2_STACK_SIZE];
static void Task2(void* p_arg);
static OS_TCB Task3TCB;
static CPU_STK Task3Stack[TASK3_STACK_SIZE];
static void Task3(void* p_arg);
int main(void)
{
HAL_Init();
SystemClock_Config();
LED_GPIO_Init();
KEY_GPIO_Init();
UART_GPIO_Init();
Delay_Init();
OS_ERR err;
OSInit(&err);
if (err != OS_ERR_NONE) {
printf("OS init failed: %d\n", err);
while (1);
}
OSTaskCreate(&Task1TCB, "Task1", Task1, NULL, 2u, &Task1Stack[0], TASK1_STACK_SIZE / 10u, TASK1_STACK_SIZE, 0u, 0u, NULL, (OS_OPT_TASK_STK_CHK | OS_OPT_TASK_STK_CLR), &err);
if (err != OS_ERR_NONE) {
printf("Task1 create failed: %d\n", err);
while (1);
}
OSTaskCreate(&Task2TCB, "Task2", Task2, NULL, 3u, &Task2Stack[0], TASK2_STACK_SIZE / 10u, TASK2_STACK_SIZE, 0u, 0u, NULL, (OS_OPT_TASK_STK_CHK | OS_OPT_TASK_STK_CLR), &err);
if (err != OS_ERR_NONE) {
printf("Task2 create failed: %d\n", err);
while (1);
}
OSTaskCreate(&Task3TCB, "Task3", Task3, NULL, 4u, &Task3Stack[0], TASK3_STACK_SIZE / 10u, TASK3_STACK_SIZE, 0u, 0u, NULL, (OS_OPT_TASK_STK_CHK | OS_OPT_TASK_STK_CLR), &err);
if (err != OS_ERR_NONE) {
printf("Task3 create failed: %d\n", err);
while (1);
}
OSStart(&err);
if (err != OS_ERR_NONE) {
printf("OS start failed: %d\n", err);
while (1);
}
while (1);
}
void Task1(void* p_arg)
{
(void)p_arg;
while (1) {
LED_ON();
OSTimeDlyHMSM(0, 0, 1, 0, OS_OPT_TIME_HMSM_STRICT, NULL);
LED_OFF();
OSTimeDlyHMSM(0, 0, 1, 0, OS_OPT_TIME_HMSM_STRICT, NULL);
}
}
void Task2(void* p_arg)
{
(void)p_arg;
while (1) {
if (KEY1_PRESSED()) {
LED_TOGGLE();
OSTimeDlyHMSM(0, 0, 0, 500, OS_OPT_TIME_HMSM_STRICT, NULL);
} else {
OSTimeDlyHMSM(0, 0, 0, 10, OS_OPT_TIME_HMSM_STRICT, NULL);
}
}
}
void Task3(void* p_arg)
{
(void)p_arg;
while (1) {
if (KEY2_PRESSED()) {
LED_OFF();
OSTimeDlyHMSM(0, 0, 0, 500, OS_OPT_TIME_HMSM_STRICT, NULL);
} else {
OSTimeDlyHMSM(0, 0, 0, 10, OS_OPT_TIME_HMSM_STRICT, NULL);
}
}
}
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
{
Error_Handler();
}
}
/* 裁剪ucos系统:去掉不需要的功能模块 */
#define OS_CFG_APP_HOOKS_EN DEF_DISABLED
#define OS_CFG_DBG_EN DEF_DISABLED
#define OS_CFG_SCHED_LOCK_TIME_MEAS_EN DEF_DISABLED
#define OS_CFG_TICK_RATE_HZ 1000u
#define OS_CFG_PRIO_MAX 10u
#include "os_cfg.h"
```
阅读全文