10

kings669669 · 2022年04月01日

【GD32F310开发板试用】GD32F310移植FreeRTOS

【GD32F310开发板试用】GD32F310移植FreeRTOS

项目地址:https://github.com/kings669/G...

环境说明

首先我先说明我的环境:
我使用keil5的版本是V5.36,电脑系统是WIN10,使用的固件库版本是从官网下载的最新版2.2.0(官网下载地址:http://www.gd32mcu.com/cn/download/7?kw=GD32F3),我所移植的FreeRTOS版本为:v202112.00(官网下载地址:https://www.freertos.org/a00104.html
 title=
 title=

开发板信息

我们在来看看关于这款开发板:
GD32F310K-START 评估板使用 GD32F310K8T6 作为主控制器。LED的引脚是PA8,之后我们会用到。
image.png
我们在看看Flash和RAM大小分别是64KB和8KB。到时候我们会在FreeRTOSConfig.h里面设置大小。
image.png
板载的调试器是GDLink

移植过程

可以参考我项目里面的资料,需要的文件均放在了doc文件夹中。
建好文件夹,命好名字:
image.png
我们把FreeRTOS的文件放在中间件Middleware文件夹中。
下载好的FreeRTOS文件夹:
image.png
我们选用FreeRTOS就行,没必要使用Plus,我们需要的文件夹都在FreeRTOS中,将它复制到自己的项目文件中。
image.png
我们把下载好的固件库以及CMSIS文件放在我们建好的Drivers文件夹中。
image.png

接下来就keil5相关的设置:
1.预定义:记得勾选支持C99,以及添加头文件路径。
image.png

2.keil5中文件添加,按照文件夹的来做
image.png
这里要稍微注意的是:port.c是选择图中路径的文件(由于我们芯片是M4的内核),内存管理我们选择heap_4.c
image.png
image.png
到这里我们还缺少一个FreeRTOS的配置文件,FreeRTOSConfig.h;
内容是:

#ifndef FREERTOS_CONFIG_H
#define FREERTOS_CONFIG_H
#include "main.h"
/*-----------------------------------------------------------
 * Application specific definitions.
 *
 * These definitions should be adjusted for your particular hardware and
 * application requirements.
 *
 * THESE PARAMETERS ARE DESCRIBED WITHIN THE 'CONFIGURATION' SECTION OF THE
 * FreeRTOS API DOCUMENTATION AVAILABLE ON THE FreeRTOS.org WEB SITE.
 *
 * See http://www.freertos.org/a00110.html.
 *----------------------------------------------------------*/

/* Ensure stdint is only used by the compiler, and not the assembler. */
#if defined(__ICCARM__)   ||  defined(__CC_ARM) ||  defined(__GNUC__)
    #include <stdint.h>
    extern uint32_t SystemCoreClock;
#endif


#define configUSE_PREEMPTION            1   //1使用抢占式内核,0使用协程
#define configUSE_TIME_SLICING          1   //1使能时间片调度(默认式使能的)
  
#define configUSE_PORT_OPTIMISED_TASK_SELECTION    1    //1启用特殊方法来选择下一个要运行的任务
                                                     //一般是硬件计算前导零指令,如果所使用的
                                                     //MCU没有这些硬件指令的话此宏应该设置为0!
#define configUSE_TICKLESS_IDLE            0    //1启用低功耗tickless模式
#define configUSE_QUEUE_SETS            1    //为1时启用队列
#define configUSE_IDLE_HOOK                0    //1,使用空闲钩子;0,不使用
#define configUSE_TICK_HOOK                0    //1,使用时间片钩子;0,不使用
#define configCPU_CLOCK_HZ                ( SystemCoreClock ) //CPU频率
#define configTICK_RATE_HZ                ( ( TickType_t ) 1000 ) //时钟节拍频率,这里设置为1000,周期就是1ms
#define configMAX_PRIORITIES            ( 16 )  //可使用的最大优先级
#define configMINIMAL_STACK_SIZE        ( ( unsigned short ) 128 )  //空闲任务使用的堆栈大小
#define configTOTAL_HEAP_SIZE            ( ( size_t ) ( 6 * 1024 ) )   //系统所有总的堆大小
#define configMAX_TASK_NAME_LEN            ( 16 )  //任务名字字符串长度

#define configUSE_16_BIT_TICKS            0   //系统节拍计数器变量数据类型,
                                            //1表示为16位无符号整形,0表示为32位无符号整形

    #define INCLUDE_xTaskGetHandle 1

#define configIDLE_SHOULD_YIELD            1   //为1时空闲任务放弃CPU使用权给其他同优先级的用户任务
#define configUSE_TASK_NOTIFICATIONS    1   //为1时开启任务通知功能,默认开启
#define configUSE_MUTEXES                1   //为1时使用互斥信号量
#define configQUEUE_REGISTRY_SIZE        8   //不为0时表示启用队列记录,具体的值是可以
                                            //记录的队列和信号量最大数目。
#define configCHECK_FOR_STACK_OVERFLOW    0   //大于0时启用堆栈溢出检测功能,如果使用此功能
                                            //用户必须提供一个栈溢出钩子函数,如果使用的话
#define configUSE_RECURSIVE_MUTEXES        1   //为1时使用递归互斥信号量
#define configUSE_MALLOC_FAILED_HOOK    0   //1使用内存申请失败钩子函数
#define configUSE_APPLICATION_TASK_TAG    0
#define configUSE_COUNTING_SEMAPHORES    1   //为1时使用计数信号量
#define configSUPPORT_DYNAMIC_ALLOCATION 1  //支持动态内存申请
#define configUSE_TRACE_FACILITY        1   //为1启用可视化跟踪调试

#define configGENERATE_RUN_TIME_STATS    0   //为1时启用运行时间统计功能

#define configUSE_STATS_FORMATTING_FUNCTIONS    1       //与宏configUSE_TRACE_FACILITY同时为1时会编译下面3个函数
                                                        //prvWriteNameToBuffer(),vTaskList(),
                                                        //vTaskGetRunTimeStats()

#define INCLUDE_uxTaskGetStackHighWaterMark 1
/* Co-routine definitions. */
#define configUSE_CO_ROUTINES         0            //为1时启用协程,启用协程以后必须添加文件croutine.c
#define configMAX_CO_ROUTINE_PRIORITIES ( 2 )   //协程的有效优先级数目

/* Software timer definitions. */
#define configUSE_TIMERS                1        //为1时启用软件定时器
#define configTIMER_TASK_PRIORITY        ( 2 )   //软件定时器优先级
#define configTIMER_QUEUE_LENGTH        10      //软件定时器队列长度
#define configTIMER_TASK_STACK_DEPTH    ( configMINIMAL_STACK_SIZE * 2 )    //软件定时器任务堆栈大小

/* Set the following definitions to 1 to include the API function, or zero
to exclude the API function. */
#define INCLUDE_vTaskPrioritySet        1
#define INCLUDE_uxTaskPriorityGet        1
#define INCLUDE_vTaskDelete                1
#define INCLUDE_vTaskCleanUpResources    1
#define INCLUDE_vTaskSuspend            1
#define INCLUDE_vTaskDelayUntil            1
#define INCLUDE_vTaskDelay                1

/* Cortex-M specific definitions. */
#ifdef __NVIC_PRIO_BITS
    /* __BVIC_PRIO_BITS will be specified when CMSIS is being used. */
    #define configPRIO_BITS               __NVIC_PRIO_BITS
#else
    #define configPRIO_BITS               4        /* 15 priority levels */
#endif

/* The lowest interrupt priority that can be used in a call to a "set priority"
function. */
#define configLIBRARY_LOWEST_INTERRUPT_PRIORITY            0xf     //中断最低优先级

/* The highest interrupt priority that can be used by any interrupt service
routine that makes calls to interrupt safe FreeRTOS API functions.  DO NOT CALL
INTERRUPT SAFE FREERTOS API FUNCTIONS FROM ANY INTERRUPT THAT HAS A HIGHER
PRIORITY THAN THIS! (higher priorities are lower numeric values. */
#define configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY    5       //系统可管理的最高中断优先级

/* Interrupt priorities used by the kernel port layer itself.  These are generic
to all Cortex-M ports, and do not rely on any particular library functions. */
#define configKERNEL_INTERRUPT_PRIORITY         ( configLIBRARY_LOWEST_INTERRUPT_PRIORITY << (8 - configPRIO_BITS) )
/* !!!! configMAX_SYSCALL_INTERRUPT_PRIORITY must not be set to zero !!!!
See http://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html. */
#define configMAX_SYSCALL_INTERRUPT_PRIORITY     ( configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY << (8 - configPRIO_BITS) )
    
/* Normal assert() semantics without relying on the provision of an assert.h
header file. */
#define configASSERT( x ) if( ( x ) == 0 ) { taskDISABLE_INTERRUPTS(); for( ;; ); }    
    
/* Definitions that map the FreeRTOS port interrupt handlers to their CMSIS
standard names. */
#define vPortSVCHandler SVC_Handler
#define xPortPendSVHandler PendSV_Handler
//#define xPortSysTickHandler SysTick_Handler

#endif /* FREERTOS_CONFIG_H */

点灯测试

看了一下官方的例程,还是有点不太习惯,我习惯是结构体的写法。所以我把此处进行了改写,并且打算将后面的外设也一一写完,感兴趣的可以关注项目。
灯的引脚是PA8
image.png
main.c文件

#include "gd32f3x0.h"
#include "systick.h"

#include "FreeRTOS.h"
#include "task.h"

#include "gpio.h"

#define START_TASK_PRIO        1
#define START_STK_SIZE         128  
TaskHandle_t StartTask_Handler;
void start_task(void *pvParameters);

#define TASK1_TASK_PRIO        2
#define TASK1_STK_SIZE         128  
TaskHandle_t Task1Task_Handler;
void task1_task(void *pvParameters);

/*!
    \brief      main function
    \param[in]  none
    \param[out] none
    \retval     none
*/
int main(void)
{  
        nvic_priority_group_set(NVIC_PRIGROUP_PRE4_SUB0);
    systick_config();
    
        My_GPIO_Init();

    xTaskCreate((TaskFunction_t )start_task,           
                (const char*    )"start_task",        
                (uint16_t       )START_STK_SIZE,      
                (void*          )NULL,                  
                (UBaseType_t    )START_TASK_PRIO,      
                (TaskHandle_t*  )&StartTask_Handler);   
                
        vTaskStartScheduler(); 
    
    while(1){
    }
}

void start_task(void *pvParameters)
{
    taskENTER_CRITICAL();           
    
    xTaskCreate((TaskFunction_t )task1_task,             
                (const char*    )"task1_task",           
                (uint16_t       )TASK1_STK_SIZE,        
                (void*          )NULL,                  
                (UBaseType_t    )TASK1_TASK_PRIO,        
                (TaskHandle_t*  )&Task1Task_Handler);   
                
    vTaskDelete(StartTask_Handler); 
                
    taskEXIT_CRITICAL();            
}

void task1_task(void *pvParameters)
{
    while(1)
    {
        gpio_bit_write(GPIOA, GPIO_PIN_8, SET);
    vTaskDelay(1000);
        gpio_bit_write(GPIOA, GPIO_PIN_8, RESET);        
        vTaskDelay(1000);
    }
}


测试视频

https://www.bilibili.com/vide...

其他

感谢本次极术社区提供的开发板,这也是我第一次使用GD32,以前一直使用的都是STM32,其实国产的32板子非常不错。虽然STM32的生态确实是没话说,有CubeMX等工具,但也不是必需的。考虑到现在的STM32的价格,我觉得我们可以能够多多支持国产,MCU的生态也取决我们开发者,生态是靠我们一起建设。

推荐阅读
关注数
10693
内容数
187
中国高性能通用微控制器领域的领跑者兆易创新GD系列芯片技术专栏。
目录
极术微信服务号
关注极术微信号
实时接收点赞提醒和评论通知
安谋科技学堂公众号
关注安谋科技学堂
实时获取安谋科技及 Arm 教学资源
安谋科技招聘公众号
关注安谋科技招聘
实时获取安谋科技中国职位信息