【GD32开发学习】四、FreeRTOS移植

文章目录


前言

准备

1、GD32F系列开发版,我使用的事F427R-START开发版

2、安装有Keil的电脑,当前keil版本为v5.31.0.0

3、FreeRTOS版本 FreeRTOS Kernel V10.1.1

一、保留RTOS目录下这些文件

 

 

 

 

二、添加源文件到工程

 其中 heap_4.c 文件在FreeRTOS\Source\portable\MemMang文件夹下,

port.c在\FreeRTOS\Source\portable\RVDS\ARM_CM4F文件夹下

/*
 * FreeRTOS Kernel V10.0.1
 * Copyright (C) 2017 Amazon.com, Inc. or its affiliates.  All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of
 * this software and associated documentation files (the "Software"), to deal in
 * the Software without restriction, including without limitation the rights to
 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
 * the Software, and to permit persons to whom the Software is furnished to do so,
 * subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 * http://www.FreeRTOS.org
 * http://aws.amazon.com/freertos
 *
 * 1 tab == 4 spaces!
 */

#ifndef FREERTOS_CONFIG_H
#define FREERTOS_CONFIG_H

#if defined(__CC_ARM) || defined(__ICCARM__)
#include <stdint.h>
extern uint32_t SystemCoreClock;
#endif

#define configUSE_PREEMPTION                          1
#define configCPU_CLOCK_HZ                            ( SystemCoreClock )
#define configTICK_RATE_HZ                            ( ( TickType_t ) 1000 )
#define configMAX_PRIORITIES                          ( 8 )
#define configMINIMAL_STACK_SIZE                      ( ( unsigned short ) 130 )
#define configTOTAL_HEAP_SIZE                         ( ( size_t ) ( 16 * 1024 ) )
#define configMAX_TASK_NAME_LEN                       ( 16 )
#define configUSE_16_BIT_TICKS                        0
#define configIDLE_SHOULD_YIELD                       1
#define configUSE_MUTEXES                             1
#define configUSE_RECURSIVE_MUTEXES                   1
#define configUSE_COUNTING_SEMAPHORES                 1
#define configQUEUE_REGISTRY_SIZE                     8
#define configUSE_QUEUE_SETS                          0
#define configUSE_APPLICATION_TASK_TAG                0


/* hook function related definitions */
#define configUSE_IDLE_HOOK                           0
#define configUSE_TICK_HOOK                           0
#define configCHECK_FOR_STACK_OVERFLOW                0
#define configUSE_MALLOC_FAILED_HOOK                  0


#define configSUPPORT_STATIC_ALLOCATION               0
#define configUSE_TICKLESS_IDLE                       0
#define configNUM_THREAD_LOCAL_STORAGE_POINTERS       0


#define configUSE_TRACE_FACILITY                      1
/* This demo makes use of one or more example stats formatting functions.  These
format the raw data provided by the uxTaskGetSystemState() function in to human
readable ASCII form.  See the notes in the implementation of vTaskList() within
FreeRTOS/Source/tasks.c for limitations. */
#define configUSE_STATS_FORMATTING_FUNCTIONS          0


/* Run time stats gathering definitions. */
#define configGENERATE_RUN_TIME_STATS                 0
#define portCONFIGURE_TIMER_FOR_RUN_TIME_STATS()
#define portGET_RUN_TIME_COUNTER_VALUE()              0

/* co-routine definitions */
#define configUSE_CO_ROUTINES                         0
#define configMAX_CO_ROUTINE_PRIORITIES               ( 2 )

/* software timer definitions */
#define configUSE_TIMERS                              0
#define configTIMER_TASK_PRIORITY                     ( 2 )
#define configTIMER_QUEUE_LENGTH                      10
#define configTIMER_TASK_STACK_DEPTH                  ( configMINIMAL_STACK_SIZE * 2 )

/* set to 1 to include the API function, or 0 to exclude the API function */
#define INCLUDE_vTaskPrioritySet                      1
#define INCLUDE_uxTaskPriorityGet                     1
#define INCLUDE_vTaskDelete                           1
#define INCLUDE_vTaskCleanUpResources                 0
#define INCLUDE_vTaskSuspend                          1
#define INCLUDE_vTaskDelayUntil                       1
#define INCLUDE_vTaskDelay                            1
#define INCLUDE_eTaskGetState                         1
#define INCLUDE_xTimerPendFunctionCall                0
#define INCLUDE_xSemaphoreGetMutexHolder              0
#define INCLUDE_xTaskGetHandle                        1
#define INCLUDE_xTaskGetCurrentTaskHandle             1
#define INCLUDE_xTaskGetIdleTaskHandle                0
#define INCLUDE_xTaskAbortDelay                       0
#define INCLUDE_xTaskGetSchedulerState                1
#define INCLUDE_uxTaskGetStackHighWaterMark           1



/* Cortex-M specific definitions */
#ifdef __NVIC_PRIO_BITS
    /* __NVIC_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   2

/* interrupt priorities used by the kernel port layer itself */
#define configKERNEL_INTERRUPT_PRIORITY                ( configLIBRARY_LOWEST_INTERRUPT_PRIORITY << (8 - configPRIO_BITS) )
/* configMAX_SYSCALL_INTERRUPT_PRIORITY must not be set to zero */
#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( ;; ); } 

/* map the FreeRTOS port interrupt handlers to CMSIS standard names */
#define vPortSVCHandler SVC_Handler
#define xPortPendSVHandler PendSV_Handler
#define xPortSysTickHandler SysTick_Handler



#endif /* FREERTOS_CONFIG_H */

其余\FreeRTOS\Source下

三、头文件包含

 编译报错,找不到 FreeRTOSConfig.h文件

可以从官方移植的例程中找到这个文件,放到\FreeRTOS\Source\include下即可

/*
 * FreeRTOS Kernel V10.0.1
 * Copyright (C) 2017 Amazon.com, Inc. or its affiliates.  All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of
 * this software and associated documentation files (the "Software"), to deal in
 * the Software without restriction, including without limitation the rights to
 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
 * the Software, and to permit persons to whom the Software is furnished to do so,
 * subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 * http://www.FreeRTOS.org
 * http://aws.amazon.com/freertos
 *
 * 1 tab == 4 spaces!
 */

#ifndef FREERTOS_CONFIG_H
#define FREERTOS_CONFIG_H

#if defined(__CC_ARM) || defined(__ICCARM__)
#include <stdint.h>
extern uint32_t SystemCoreClock;
#endif

#define configUSE_PREEMPTION                          1
#define configCPU_CLOCK_HZ                            ( SystemCoreClock )
#define configTICK_RATE_HZ                            ( ( TickType_t ) 1000 )
#define configMAX_PRIORITIES                          ( 8 )
#define configMINIMAL_STACK_SIZE                      ( ( unsigned short ) 130 )
#define configTOTAL_HEAP_SIZE                         ( ( size_t ) ( 16 * 1024 ) )
#define configMAX_TASK_NAME_LEN                       ( 16 )
#define configUSE_16_BIT_TICKS                        0
#define configIDLE_SHOULD_YIELD                       1
#define configUSE_MUTEXES                             1
#define configUSE_RECURSIVE_MUTEXES                   1
#define configUSE_COUNTING_SEMAPHORES                 1
#define configQUEUE_REGISTRY_SIZE                     8
#define configUSE_QUEUE_SETS                          0
#define configUSE_APPLICATION_TASK_TAG                0


/* hook function related definitions */
#define configUSE_IDLE_HOOK                           0
#define configUSE_TICK_HOOK                           0
#define configCHECK_FOR_STACK_OVERFLOW                0
#define configUSE_MALLOC_FAILED_HOOK                  0


#define configSUPPORT_STATIC_ALLOCATION               0
#define configUSE_TICKLESS_IDLE                       0
#define configNUM_THREAD_LOCAL_STORAGE_POINTERS       0


#define configUSE_TRACE_FACILITY                      1
/* This demo makes use of one or more example stats formatting functions.  These
format the raw data provided by the uxTaskGetSystemState() function in to human
readable ASCII form.  See the notes in the implementation of vTaskList() within
FreeRTOS/Source/tasks.c for limitations. */
#define configUSE_STATS_FORMATTING_FUNCTIONS          0


/* Run time stats gathering definitions. */
#define configGENERATE_RUN_TIME_STATS                 0
#define portCONFIGURE_TIMER_FOR_RUN_TIME_STATS()
#define portGET_RUN_TIME_COUNTER_VALUE()              0

/* co-routine definitions */
#define configUSE_CO_ROUTINES                         0
#define configMAX_CO_ROUTINE_PRIORITIES               ( 2 )

/* software timer definitions */
#define configUSE_TIMERS                              0
#define configTIMER_TASK_PRIORITY                     ( 2 )
#define configTIMER_QUEUE_LENGTH                      10
#define configTIMER_TASK_STACK_DEPTH                  ( configMINIMAL_STACK_SIZE * 2 )

/* set to 1 to include the API function, or 0 to exclude the API function */
#define INCLUDE_vTaskPrioritySet                      1
#define INCLUDE_uxTaskPriorityGet                     1
#define INCLUDE_vTaskDelete                           1
#define INCLUDE_vTaskCleanUpResources                 0
#define INCLUDE_vTaskSuspend                          1
#define INCLUDE_vTaskDelayUntil                       1
#define INCLUDE_vTaskDelay                            1
#define INCLUDE_eTaskGetState                         1
#define INCLUDE_xTimerPendFunctionCall                0
#define INCLUDE_xSemaphoreGetMutexHolder              0
#define INCLUDE_xTaskGetHandle                        1
#define INCLUDE_xTaskGetCurrentTaskHandle             1
#define INCLUDE_xTaskGetIdleTaskHandle                0
#define INCLUDE_xTaskAbortDelay                       0
#define INCLUDE_xTaskGetSchedulerState                1
#define INCLUDE_uxTaskGetStackHighWaterMark           1



/* Cortex-M specific definitions */
#ifdef __NVIC_PRIO_BITS
    /* __NVIC_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   2

/* interrupt priorities used by the kernel port layer itself */
#define configKERNEL_INTERRUPT_PRIORITY                ( configLIBRARY_LOWEST_INTERRUPT_PRIORITY << (8 - configPRIO_BITS) )
/* configMAX_SYSCALL_INTERRUPT_PRIORITY must not be set to zero */
#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( ;; ); } 

/* map the FreeRTOS port interrupt handlers to CMSIS standard names */
#define vPortSVCHandler SVC_Handler
#define xPortPendSVHandler PendSV_Handler
#define xPortSysTickHandler SysTick_Handler



#endif /* FREERTOS_CONFIG_H */

编译报错,port.o and gd32f4xx_it.o有几个函数重定义,屏蔽掉gd32f4xx_it中的这几个函数

 编译通过

 测试程序

/*!
    \file    main.c
    \brief   GPIO running led demo

    \version 2022-05-08, V2.0.0, demo for GD32F4xx
*/

/*
    Copyright (c) 2022, GigaDevice Semiconductor Inc.

    Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

    1. Redistributions of source code must retain the above copyright notice, this
       list of conditions and the following disclaimer.
    2. Redistributions in binary form must reproduce the above copyright notice,
       this list of conditions and the following disclaimer in the documentation
       and/or other materials provided with the distribution.
    3. Neither the name of the copyright holder nor the names of its contributors
       may be used to endorse or promote products derived from this software without
       specific prior written permission.

    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
OF SUCH DAMAGE.
*/

#include "FreeRTOS.h"
#include "task.h"
#include "semphr.h"
#include "systick.h"
#include <stdio.h>
#include "gd32f427r_start.h"

#define INIT_TASK_PRIO   ( tskIDLE_PRIORITY + 1 )
#define LED_TASK_PRIO    ( tskIDLE_PRIORITY + 2 )
 
void LED1_task(void * pvParameters); 
void LED2_task(void * pvParameters);
void init_task(void * pvParameters);

/*!
    \brief      main function
    \param[in]  none
    \param[out] none
    \retval     none
*/
int main(void)
{
    /* 系统时钟初始化为200M __SYSTEM_CLOCK_200M_PLL_25M_HXTAL */
    
    
    /* configure 4 bits pre-emption priority */
    nvic_priority_group_set(NVIC_PRIGROUP_PRE4_SUB0);    

    /* init task */
    xTaskCreate(init_task, "INIT", configMINIMAL_STACK_SIZE, NULL, INIT_TASK_PRIO, NULL);
    
    /* start scheduler */
    vTaskStartScheduler();
    while(1) 
    {

    }
}

/*!
    \brief      init task
    \param[in]  pvParameters not used
    \param[out] none
    \retval     none
*/
void init_task(void * pvParameters)
{
    /* 初始化按键 */
    gd_eval_key_init(KEY_USER, KEY_MODE_EXTI);
    /*LED 初始化*/
    gd_eval_led_init(LED1);
    gd_eval_led_on(LED1);
    /*LED 初始化*/    
    gd_eval_led_init(LED2);
    gd_eval_led_off(LED2);

    /* start toogle LED task every 250ms */
    xTaskCreate(LED1_task, "LED1", configMINIMAL_STACK_SIZE, NULL, LED_TASK_PRIO, NULL);
    xTaskCreate(LED2_task, "LED2", configMINIMAL_STACK_SIZE, NULL, LED_TASK_PRIO, NULL);
    for( ;; )
    {
        vTaskDelete(NULL);
    }
}

/*!
    \brief      LED1 task
    \param[in]  pvParameters not used
    \param[out] none
    \retval     none
*/
void LED1_task(void * pvParameters)
{  
    for( ;; )
    {
        /* toggle LED1 each 500ms */
        gd_eval_led_toggle(LED1);
        vTaskDelay(500 / portTICK_RATE_MS);
    }
}

/*!
    \brief      LED2 task
    \param[in]  pvParameters not used
    \param[out] none
    \retval     none
*/
void LED2_task(void * pvParameters)
{  
    BaseType_t err = pdFALSE;
    for( ;; )
    {
        /* toggle LED2 each 500ms */        
        gd_eval_led_toggle(LED2);
        vTaskDelay(500 / portTICK_RATE_MS);        
    }
}







创建两个任务 LED1_task()和LED2_task()、分别里面都对LED进行翻转,现象两个LED分别均匀闪烁


总结

  • 1
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值