查看: 2995|回复: 1

[原创] FreeRTOS教程一FreeRTOS之KL27移植

[复制链接]
  • TA的每日心情

    2017-1-4 08:05
  • 签到天数: 11 天

    连续签到: 1 天

    [LV.3]偶尔看看II

    85

    主题

    1629

    帖子

    1

    版主

    Rank: 7Rank: 7Rank: 7

    积分
    2569

    优秀版主

    最后登录
    2019-3-28
    发表于 2016-7-14 19:25:23 | 显示全部楼层 |阅读模式
    本帖最后由 技术范儿 于 2016-7-14 19:30 编辑

    好几天没有放大招了,由于人长得比较帅最近比较烦,收到多个人的骚扰。哈哈,开玩笑的由于是年中了公司项目验收比较忙,日日夜夜的加班撸代码,抽空写了这一KL27移植的教程,不足之处还请各位大神批评订正!下面开始:第一步:准备一个KL27基本的工程(采用SDK2.0,不会用SDK组建工程的下面跟帖,如果人多的话我连SDK2.0额用法一并讲一下)

    基础工程

    基础工程
    6.png

    第二步:准备FreeRTOS源码(我们这次移植的是8.3.2)

    第三步:讲源码放到我们第一步准备的工程目录下面(这一句貌似是废话,哈哈)
    第四步:在Keil中建立相应的工程分组,并添加相应文件,FreeRTOS目录添加内核的源码以及内存管理代码文件,FreeRTOP_Porttable添加与内核相关的文件具体请看图。
    第五步:添加头文件的路径(如图)
    第六步:完善main函数,及配置FreeRTOS第七步:编译运行
    1. /*
    2.     FreeRTOS V8.2.3 - Copyright (C) 2015 Real Time Engineers Ltd.
    3.     All rights reserved

    4.     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.

    5.     This file is part of the FreeRTOS distribution.

    6.     FreeRTOS is free software; you can redistribute it and/or modify it under
    7.     the terms of the GNU General Public License (version 2) as published by the
    8.     Free Software Foundation >>>> AND MODIFIED BY <<<< the FreeRTOS exception.

    9.     ***************************************************************************
    10.     >>!   NOTE: The modification to the GPL is included to allow you to     !<<
    11.     >>!   distribute a combined work that includes FreeRTOS without being   !<<
    12.     >>!   obliged to provide the source code for proprietary components     !<<
    13.     >>!   outside of the FreeRTOS kernel.                                   !<<
    14.     ***************************************************************************

    15.     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
    16.     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    17.     FOR A PARTICULAR PURPOSE.  Full license text is available on the following
    18.     link: http://www.freertos.org/a00114.html

    19.     ***************************************************************************
    20.      *                                                                       *
    21.      *    FreeRTOS provides completely free yet professionally developed,    *
    22.      *    robust, strictly quality controlled, supported, and cross          *
    23.      *    platform software that is more than just the market leader, it     *
    24.      *    is the industry's de facto standard.                               *
    25.      *                                                                       *
    26.      *    Help yourself get started quickly while simultaneously helping     *
    27.      *    to support the FreeRTOS project by purchasing a FreeRTOS           *
    28.      *    tutorial book, reference manual, or both:                          *
    29.      *    http://www.FreeRTOS.org/Documentation                              *
    30.      *                                                                       *
    31.     ***************************************************************************

    32.     http://www.FreeRTOS.org/FAQHelp.html - Having a problem?  Start by reading
    33.     the FAQ page "My application does not run, what could be wrong?".  Have you
    34.     defined configASSERT()?

    35.     http://www.FreeRTOS.org/support - In return for receiving this top quality
    36.     embedded software for free we request you assist our global community by
    37.     participating in the support forum.

    38.     http://www.FreeRTOS.org/training - Investing in training allows your team to
    39.     be as productive as possible as early as possible.  Now you can receive
    40.     FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers
    41.     Ltd, and the world's leading authority on the world's leading RTOS.

    42.     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
    43.     including FreeRTOS+Trace - an indispensable productivity tool, a DOS
    44.     compatible FAT file system, and our tiny thread aware UDP/IP stack.

    45.     http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.
    46.     Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.

    47.     http://www.OpenRTOS.com - Real Time Engineers ltd. license FreeRTOS to High
    48.     Integrity Systems ltd. to sell under the OpenRTOS brand.  Low cost OpenRTOS
    49.     licenses offer ticketed support, indemnification and commercial middleware.

    50.     http://www.SafeRTOS.com - High Integrity Systems also provide a safety
    51.     engineered and independently SIL3 certified version for use in safety and
    52.     mission critical applications that require provable dependability.

    53.     1 tab == 4 spaces!
    54. */

    55. #ifndef FREERTOS_CONFIG_H
    56. #define FREERTOS_CONFIG_H

    57. /*-----------------------------------------------------------
    58. * Application specific definitions.
    59. *
    60. * These definitions should be adjusted for your particular hardware and
    61. * application requirements.
    62. *
    63. * THESE PARAMETERS ARE DESCRIBED WITHIN THE 'CONFIGURATION' SECTION OF THE
    64. * FreeRTOS API DOCUMENTATION AVAILABLE ON THE FreeRTOS.org WEB SITE.
    65. *
    66. * See http://www.freertos.org/a00110.html.
    67. *----------------------------------------------------------*/

    68. #define configUSE_PREEMPTION 1
    69. #define configUSE_IDLE_HOOK 0
    70. #define configUSE_TICK_HOOK 0
    71. #define configCPU_CLOCK_HZ (SystemCoreClock)
    72. #define configTICK_RATE_HZ ((TickType_t)1000)
    73. #define configMAX_PRIORITIES (5)
    74. #define configMINIMAL_STACK_SIZE ((unsigned short)90)
    75. #define configTOTAL_HEAP_SIZE ((size_t)(10 * 1024))
    76. #define configMAX_TASK_NAME_LEN (10)
    77. #define configUSE_TRACE_FACILITY 1
    78. #define configUSE_16_BIT_TICKS 0
    79. #define configIDLE_SHOULD_YIELD 1
    80. #define configUSE_MUTEXES 1
    81. #define configQUEUE_REGISTRY_SIZE 8
    82. #define configCHECK_FOR_STACK_OVERFLOW 0
    83. #define configUSE_RECURSIVE_MUTEXES 1
    84. #define configUSE_MALLOC_FAILED_HOOK 0
    85. #define configUSE_APPLICATION_TASK_TAG 0
    86. #define configUSE_COUNTING_SEMAPHORES 1
    87. #define configGENERATE_RUN_TIME_STATS 0
    88. #define configUSE_TIME_SLICING 0

    89. /* Co-routine definitions. */
    90. #define configUSE_CO_ROUTINES 0
    91. #define configMAX_CO_ROUTINE_PRIORITIES (2)

    92. /* Software timer definitions. */
    93. #define configUSE_TIMERS 1
    94. #define configTIMER_TASK_PRIORITY (2)
    95. #define configTIMER_QUEUE_LENGTH 10
    96. #define configTIMER_TASK_STACK_DEPTH (configMINIMAL_STACK_SIZE * 2)

    97. /* Set the following definitions to 1 to include the API function, or zero
    98. to exclude the API function. */
    99. #define INCLUDE_vTaskPrioritySet 1
    100. #define INCLUDE_uxTaskPriorityGet 1
    101. #define INCLUDE_vTaskDelete 1
    102. #define INCLUDE_vTaskCleanUpResources 1
    103. #define INCLUDE_vTaskSuspend 1
    104. #define INCLUDE_vTaskDelayUntil 1
    105. #define INCLUDE_vTaskDelay 1

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

    113. /* The lowest interrupt priority that can be used in a call to a "set priority"
    114. function. */
    115. #define configLIBRARY_LOWEST_INTERRUPT_PRIORITY 0xf

    116. /* The highest interrupt priority that can be used by any interrupt service
    117. routine that makes calls to interrupt safe FreeRTOS API functions.  DO NOT CALL
    118. INTERRUPT SAFE FREERTOS API FUNCTIONS FROM ANY INTERRUPT THAT HAS A HIGHER
    119. PRIORITY THAN THIS! (higher priorities are lower numeric values. */
    120. #define configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY 5

    121. /* Interrupt priorities used by the kernel port layer itself.  These are generic
    122. to all Cortex-M ports, and do not rely on any particular library functions. */
    123. #define configKERNEL_INTERRUPT_PRIORITY (configLIBRARY_LOWEST_INTERRUPT_PRIORITY << (8 - configPRIO_BITS))
    124. /* !!!! configMAX_SYSCALL_INTERRUPT_PRIORITY must not be set to zero !!!!
    125. See http://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html. */
    126. #define configMAX_SYSCALL_INTERRUPT_PRIORITY (configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY << (8 - configPRIO_BITS))

    127. /* Normal assert() semantics without relying on the provision of an assert.h
    128. header file. */
    129. #define configASSERT(x)           \
    130.     if ((x) == 0)                 \
    131.     {                             \
    132.         taskDISABLE_INTERRUPTS(); \
    133.         for (;;)                  \
    134.             ;                     \
    135.     }

    136. /* Definitions that map the FreeRTOS port interrupt handlers to their CMSIS
    137. standard names. */
    138. #define vPortSVCHandler SVC_Handler
    139. #define xPortPendSVHandler PendSV_Handler
    140. #define xPortSysTickHandler SysTick_Handler

    141. #endif /* FREERTOS_CONFIG_H */
    复制代码

    1. #include "FreeRTOS.h"
    2. #include "task.h"
    3. #include "queue.h"
    4. #include "timers.h"

    5. #include "hardware.h"

    6. #define hello_task_PRIORITY (configMAX_PRIORITIES - 1)

    7. static void hello_task(void *pvParameters);


    8. /*!
    9. * @brief Application entry point.
    10. */
    11. int main(void)
    12. {
    13.     /* Init board hardware. */
    14.    
    15.     HARDWARE_Init();
    16.     BOARD_InitDebugConsole();
    17.     xTaskCreate(hello_task, "Hello_task", configMINIMAL_STACK_SIZE, NULL, hello_task_PRIORITY, NULL);
    18.     vTaskStartScheduler();
    19.     for (;;)
    20.         ;
    21. }

    22. /*!
    23. * @brief Task responsible for printing of "Hello world." message.
    24. */
    25. static void hello_task(void *pvParameters)
    26. {
    27.     for (;;)
    28.     {
    29.         PRINTF("Hello world.\r\n");
    30.         vTaskSuspend(NULL);
    31.     }
    32. }
    复制代码



    2.png
    4.png
    5.png
    7.png
    8.png
    7.png
    回复

    使用道具 举报

    该用户从未签到

    712

    主题

    6371

    帖子

    0

    超级版主

    Rank: 8Rank: 8

    积分
    24861
    最后登录
    2025-7-18
    发表于 2016-7-19 11:18:12 | 显示全部楼层
    感谢楼主的经验分享,楼主可以把移植好的工程也放进来给大家参考。
    回复 支持 反对

    使用道具 举报

    您需要登录后才可以回帖 注册/登录

    本版积分规则

    关闭

    站长推荐上一条 /3 下一条

    Archiver|手机版|小黑屋|恩智浦技术社区

    GMT+8, 2025-7-18 19:32 , Processed in 0.081407 second(s), 20 queries , MemCache On.

    Powered by Discuz! X3.4

    Copyright © 2001-2024, Tencent Cloud.

    快速回复 返回顶部 返回列表