Featured image of post HAL_Freertos的使用记录1

HAL_Freertos的使用记录1

HAL_Freertos的使用记录1


主要利用的是vscode里面stm32forvscode的插件

第一步就是创建cubmax的工程

  • RCC

  • 调试方式 和 时基 这里Debug 不整好 这个板子就会变砖 要用线刷了

  • 选择FREERTOS

  • 选择最大频率
  • .c 和 .h 文件分开生成 生成!!(原神启动)

关于这个框架的建立

我选择如下这样 应用和驱动:

你新建了两个文件夹所以需要再makefile文件添加进来

这个插件可以帮你添加但是需要你设置一下

Drivers 层面

.c 文件

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include "led_driver.h"

/// @brief 全彩灯初始化
/// @param  
void led_init(void)
{
    HAL_GPIO_WritePin(RGB_PORT_R, RGB_PIN_R, GPIO_PIN_SET);
    HAL_GPIO_WritePin(RGB_PORT_G, RGB_PIN_G, GPIO_PIN_SET);
    HAL_GPIO_WritePin(RGB_PORT_B, RGB_PIN_B, GPIO_PIN_SET);
}
/// @brief 设置灯的颜色
/// @param red 
/// @param green 
/// @param blue 
void color_led_set(uint8_t red, uint8_t green, uint8_t blue)
{
    //红灯:
    HAL_GPIO_WritePin(RGB_PORT_R, RGB_PIN_R, (GPIO_PinState)!red);
    //绿灯:
    HAL_GPIO_WritePin(RGB_PORT_G, RGB_PIN_G, (GPIO_PinState)!green);
    //蓝灯:
    HAL_GPIO_WritePin(RGB_PORT_B, RGB_PIN_B, (GPIO_PinState)!blue);
}
  

.h文件

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#ifndef __LED_DRIVER_H__
#define __LED_DRIVER_H__
#include "main.h"
#ifdef __cplusplus
 extern "C" {
#endif

#define RGB_PORT_R   GPIOB
#define RGB_PORT_G   GPIOB
#define RGB_PORT_B   GPIOB

#define RGB_PIN_R   GPIO_PIN_5
#define RGB_PIN_G   GPIO_PIN_0
#define RGB_PIN_B   GPIO_PIN_1

void color_led_set(uint8_t red, uint8_t green, uint8_t blue);

void led_init(void);

#ifdef __cplusplus
}
#endif

#endif /* __LED_DRIVER_H__  */

app应用层面

.h 文件

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#ifndef MAIN_INCLUDE_APPLICATION_H_
#define MAIN_INCLUDE_APPLICATION_H_

#ifdef __cplusplus
extern "C" {
#endif

/*Driver Include*/
#include "led_driver.h"
#include "beep_driver.h"
#include "my_printf.h"

/*User lib Include*/


/*pnblic Include*/
#include "FreeRTOS.h"
#include "task.h"
#include "main.h"
#include "cmsis_os.h"
#include <stdio.h>  // sprintf
#include <stdbool.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>



/********************************************************API********************************************************/


void AppTaskCreate(void);
/*LED app*/
void color_led_task_fun(void *argument);
/* 测试任务 */
void test_task_fun(void *arg);

#ifdef __cplusplus
}
#endif

#endif /* MAIN_INCLUDE_APPLICATION_H_ */

.c 创建任务函数文件

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87

#include "Application.h"
/**************************** 任务句柄 ********************************/
/*
 * 任务句柄是一个指针,用于指向一个任务,当任务创建好之后,它就具有了一个任务句柄
 * 以后我们要想操作这个任务都需要通过这个任务句柄,如果是自身的任务操作自己,那么
 * 这个句柄可以为NULL。
 */

static TaskHandle_t COLOR_LED_Handle = NULL; /* LED任务句柄 */
static TaskHandle_t TEST_TASK_Handle = NULL; /* 任务句柄 */
/********************************** 内核对象句柄 *********************************/
/*
 * 信号量,消息队列,事件标志组,软件定时器这些都属于内核的对象,要想使用这些内核
 * 对象,必须先创建,创建成功之后会返回一个相应的句柄。实际上就是一个指针,后续我
 * 们就可以通过这个句柄操作这些内核对象。
 *
 * 内核对象说白了就是一种全局的数据结构,通过这些数据结构我们可以实现任务间的通信,
 * 任务间的事件同步等各种功能。至于这些功能的实现我们是通过调用这些内核对象的函数
 * 来完成的
 *
 */

/******************************* 全局变量声明 ************************************/
/*
 * 当我们在写应用程序的时候,可能需要用到一些全局变量。
 */

/*
*************************************************************************
*                             函数声明
*************************************************************************
*/
void app_main_delay_ms(uint16_t ms);
/*
*************************************************************************
*************************************************************************
*/

/// @brief 创建任务
/// @param
void AppTaskCreate(void)
{
    taskENTER_CRITICAL();        // 进入临界区
    BaseType_t xReturn = pdPASS; /* 定义一个创建信息返回值,默认为pdPASS */
    uint32_t count = 0;
    /* 创建Test_Task任务 */
    xReturn = xTaskCreate((TaskFunction_t)color_led_task_fun, /* 任务入口函数 */
                          (const char *)"COLOR_LED_TASK",     /* 任务名字 */
                          (uint16_t)256,                      /* 任务栈大小 */
                          (void *)NULL,                       /* 任务入口函数参数 */
                          (UBaseType_t)osPriorityNormal,      /* 任务的优先级 */
                          (TaskHandle_t *)&COLOR_LED_Handle); /* 任务控制块指针 */
    if (pdPASS != xReturn)
    {
        goto error;
    }
    UART1_Printf("COLOR_LED_TASK[info]:Runing, Priority:%d\n", osPriorityNormal);

    xReturn = xTaskCreate((TaskFunction_t)test_task_fun, /* 任务入口函数 */
                          (const char *)"TEST_TASK",     /* 任务名字 */
                          (uint16_t)256,                      /* 任务栈大小 */
                          (void *)NULL,                       /* 任务入口函数参数 */
                          (UBaseType_t)osPriorityNormal,      /* 任务的优先级 */
                          (TaskHandle_t *)&TEST_TASK_Handle); /* 任务控制块指针 */
    if (pdPASS != xReturn)
    {
        goto error;
    }
    UART1_Printf("TEST_TASK[info]:Runing, Priority:%d\n", osPriorityNormal);


    return;
error:
    UART1_Printf("App_Task_Create[error]:Task Create fail,system exception\n");
    while (1)
    {
        UART1_Printf("error count:%d\n", (int)count++);
        app_main_delay_ms(1000);
    }
    taskEXIT_CRITICAL(); // 退出临界区
}

void app_main_delay_ms(uint16_t ms)
{
    vTaskDelay(pdMS_TO_TICKS(ms));
}

.c 任务分离文件

关于如何应用

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include "Application.h"
/// @brief color_led_task_fun   RGB 灯的应用
/// @param arg 
void color_led_task_fun(void *arg)
{
 
  while (true)
  {
    /* color_led的应用 */
    //红灯亮:
    color_led_set(1, 0, 0);
    os_Delay(1000);
    //绿灯亮:
    color_led_set(0, 1, 0);
    os_Delay(1000);
    //蓝灯亮:
    color_led_set(0, 0, 1);
    os_Delay(1000);
    //黄灯亮:
    color_led_set(1, 1, 0);
    os_Delay(1000);
    //紫灯亮:
    color_led_set(1, 0, 1);
    os_Delay(1000);
    //粉灯亮
    color_led_set(0, 1, 1);
    os_Delay(1000);
  }
}

关于如何嵌入框架

  • 你只需要以下两步就行
  • 第二步
最后更新于 Mar 20, 2025 15:26 +0800
世界是你们
使用 Hugo 构建
主题 StackJimmy 设计