回答

收藏

【Avnet | NXP FRDM-MCXN947试用活动】移植FreeRTOS并驱动LED计数

NXP NXP 1596 人阅读 | 0 人回复 | 2024-12-04

这篇移植FreeRTOS并驱动LED计数。

一、下载FreeRTOS源码

下载地址:https://www.freertos.org/
我下载的是以下版本。


二、添加文件到工程

添加下载的FreeRTOS文件到项目中


三、程序代码

3.1、FreeRTOSConfig.h
  1. /*
  2. * FreeRTOS V202212.01
  3. * Copyright (C) 2020 Amazon.com, Inc. or its affiliates.  All Rights Reserved.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a copy of
  6. * this software and associated documentation files (the "Software"), to deal in
  7. * the Software without restriction, including without limitation the rights to
  8. * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  9. * the Software, and to permit persons to whom the Software is furnished to do so,
  10. * subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in all
  13. * copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  17. * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  18. * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  19. * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  20. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  21. *
  22. * https://www.FreeRTOS.org
  23. * https://github.com/FreeRTOS
  24. *
  25. */

  26. #ifndef FREERTOS_CONFIG_H
  27. #define FREERTOS_CONFIG_H

  28. /*-----------------------------------------------------------
  29. * Application specific definitions.
  30. *
  31. * These definitions should be adjusted for your particular hardware and
  32. * application requirements.
  33. *
  34. * THESE PARAMETERS ARE DESCRIBED WITHIN THE 'CONFIGURATION' SECTION OF THE
  35. * FreeRTOS API DOCUMENTATION AVAILABLE ON THE FreeRTOS.org WEB SITE.
  36. *
  37. * See http://www.freertos.org/a00110.html.
  38. *----------------------------------------------------------*/

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

  44. #ifndef configENABLE_FPU
  45.   #define configENABLE_FPU                        1
  46. #endif
  47. #ifndef configENABLE_MPU
  48.   #define configENABLE_MPU                        0
  49. #endif
  50. #ifndef configENABLE_TRUSTZONE
  51.   #define configENABLE_TRUSTZONE                  0
  52. #endif
  53. #ifndef configRUN_FREERTOS_SECURE_ONLY
  54.   #define configRUN_FREERTOS_SECURE_ONLY          1
  55. #endif

  56. #define configUSE_PREEMPTION                                        1
  57. #define configSUPPORT_STATIC_ALLOCATION                        0//1
  58. #define configSUPPORT_DYNAMIC_ALLOCATION                1
  59. #define configUSE_IDLE_HOOK                                                0
  60. #define configUSE_TICK_HOOK                                                0
  61. #define configCPU_CLOCK_HZ                                                ( SystemCoreClock )
  62. #define configTICK_RATE_HZ                                                ( ( TickType_t ) 1000 )
  63. #define configMAX_PRIORITIES                                        ( 56 )
  64. #define configMINIMAL_STACK_SIZE                                ( ( uint16_t ) 512 )
  65. #define configTOTAL_HEAP_SIZE                                        ( ( size_t ) 15 * 1024 )
  66. #define configMAX_TASK_NAME_LEN                                        ( 16 )
  67. #define configUSE_TRACE_FACILITY                                1
  68. #define configUSE_16_BIT_TICKS                                        0
  69. #define configUSE_MUTEXES                                                1
  70. #define configQUEUE_REGISTRY_SIZE                                8
  71. #define configUSE_RECURSIVE_MUTEXES                                1
  72. #define configUSE_COUNTING_SEMAPHORES                        1
  73. #define configUSE_PORT_OPTIMISED_TASK_SELECTION        0
  74. #define configUSE_MALLOC_FAILED_HOOK                        0//1
  75. #define configCHECK_FOR_STACK_OVERFLOW                        0//2

  76. /* Defaults to size_t for backward compatibility, but can be changed
  77. * if lengths will always be less than the number of bytes in a size_t. */
  78. #define configMESSAGE_BUFFER_LENGTH_TYPE                size_t
  79. /* USER CODE END MESSAGE_BUFFER_LENGTH_TYPE */

  80. /* Software timer definitions. */
  81. #define configUSE_TIMERS                                                1
  82. #define configTIMER_TASK_PRIORITY                                ( 2 )
  83. #define configTIMER_QUEUE_LENGTH                                10
  84. #define configTIMER_TASK_STACK_DEPTH                        256

  85. /* Set the following definitions to 1 to include the API function, or zero
  86. * to exclude the API function. */
  87. #define INCLUDE_vTaskPrioritySet                                1
  88. #define INCLUDE_uxTaskPriorityGet                                1
  89. #define INCLUDE_vTaskDelete                                                1
  90. #define INCLUDE_vTaskCleanUpResources                        0
  91. #define INCLUDE_vTaskSuspend                                        1
  92. #define INCLUDE_vTaskDelayUntil                                        1
  93. #define INCLUDE_vTaskDelay                                                1
  94. #define INCLUDE_xTaskGetSchedulerState                        1
  95. #define INCLUDE_xTimerPendFunctionCall                        1
  96. #define INCLUDE_xQueueGetMutexHolder                        1
  97. #define INCLUDE_uxTaskGetStackHighWaterMark                1
  98. #define INCLUDE_eTaskGetState                                        1

  99. /* Cortex-M specific definitions. */
  100. #ifdef __NVIC_PRIO_BITS
  101.         /* __BVIC_PRIO_BITS will be specified when CMSIS is being used. */
  102.         #define configPRIO_BITS                                                __NVIC_PRIO_BITS
  103. #else
  104.         #define configPRIO_BITS                                                4
  105. #endif

  106. /* The lowest interrupt priority that can be used in a call to a "set priority"
  107. * function. */
  108. #define configLIBRARY_LOWEST_INTERRUPT_PRIORITY        15

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

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

  120. /* Normal assert() semantics without relying on the provision of an assert.h
  121. * header file. */
  122. #define configASSERT( x )                                                        if ( ( x ) == 0 ) { taskDISABLE_INTERRUPTS(); for( ;; ); }

  123. /* Definitions that map the FreeRTOS port interrupt handlers to their CMSIS
  124. * standard names. */
  125. #define vPortSVCHandler                                                                SVC_Handler
  126. #define xPortPendSVHandler                                                        PendSV_Handler
  127. #define xPortSysTickHandler                                                        SysTick_Handler

  128. /* Allow system call from within FreeRTOS kernel only. */
  129. #define configENFORCE_SYSTEM_CALLS_FROM_KERNEL_ONLY        1

  130. /* STM32H743 has 16 MPU regions and therefore it is necessary to configure
  131. * configTOTAL_MPU_REGIONS correctly. */
  132. #define configTOTAL_MPU_REGIONS                                                16

  133. /* The default TEX,S,C,B setting marks the SRAM as shareable and as a result,
  134. * disables cache. Do not mark the SRAM as shareable because caching is being
  135. * used. TEX=0, S=0, C=1, B=1. */
  136. #define configTEX_S_C_B_SRAM                                                ( 0x03UL )

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



3.2、f_task.c
  1. <blockquote>#include "main.h"
复制代码


3.3、main.c

  1. #include "main.h"

  2. int main(void)
  3. {
  4.         uint8_t i;

  5.   CLOCK_EnableClock(kCLOCK_Gpio0);

  6.         BOARD_InitDEBUG_UARTPins();
  7.         BOARD_PowerMode_OD();
  8.         BOARD_InitBootClocks();
  9.         BOARD_InitDebugConsole();
  10.         init_led();
  11.         task_create();
  12.         while (1)
  13.         {
  14.         }
  15. }
复制代码

3.3、lm8168.c
  1. #include "main.h"

  2. const uint8_t led_num[10]={0xfc,0x60,0xda,0xf2,0x66,0xb6,0xbe,0xe0,0xfe,0xf6 };
  3. uint32_t dt=0;
  4. uint8_t cnt=0;
  5. uint8_t jsflag=0;
  6. uint8_t disp_temp[4]={0,0,0,0};
  7. uint8_t disp_js=0;

  8. void delay(void)
  9. {
  10.         volatile uint32_t i = 0;
  11.         for (i = 0; i < 100; ++i)
  12.         {
  13.                 __asm("NOP"); /* delay */
  14.         }
  15. }

  16. void init_lm8168(void)
  17. {
  18.         init_lm8168b_pins();
  19.         LM8168_CLK_L();
  20.         LM8168_DAT_L();
  21.         LM8168_EN_L();
  22. }

  23. void lm8168_writebyte(uint32_t dat)
  24. {
  25.         uint8_t i=0;
  26.         LM8168_CLK_L();
  27.         LM8168_DAT_L();
  28.         LM8168_EN_L();
  29.         
  30.         for(i=0;i<18;i++)
  31.         {
  32.                 if(dat&0x01)
  33.                 {
  34.                         LM8168_DAT_H();
  35.                 }
  36.                 else
  37.                 {
  38.                         LM8168_DAT_L();
  39.                 }
  40.                 dat=dat>>1;
  41.                 LM8168_CLK_L();
  42.                 delay();
  43.                 LM8168_CLK_H();
  44.                 delay();               
  45.         }
  46. }

  47. void lm8168_writedat(uint8_t dat, uint8_t seg)
  48. {
  49.         uint32_t dt=0;
  50.         
  51.         dt |= (seg&0x3f)<<1;
  52.         dt |= dat<<10;
  53.         lm8168_writebyte(dt);
  54.         LM8168_EN_H();
  55.         delay();
  56.         LM8168_EN_L();
  57. }
复制代码

四、程序运行

下载程序后,串口输出


LED显示



分享到:
回复

使用道具 举报

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

本版积分规则

关闭

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