找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 1714|回复: 0
打印 上一主题 下一主题
收起左侧

FreeRtos STM32工程 代码带部分中文注释

[复制链接]
跳转到指定楼层
楼主
ID:830386 发表于 2020-10-16 17:15 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
STM32F103C8T6的FreeRtos工程。
带部分中文注释。希望可以帮组初学者。

单片机源程序如下:
  1. /* Standard includes. */
  2. #include <stdio.h>

  3. /* Scheduler includes. */
  4. #include "FreeRTOS.h"
  5. #include "task.h"
  6. #include "queue.h"

  7. /* Library includes. */
  8. #include "stm32f10x_it.h"

  9. /* Demo app includes. */
  10. #include "lcd.h"
  11. #include "LCD_Message.h"
  12. #include "BlockQ.h"
  13. #include "death.h"
  14. #include "integer.h"
  15. #include "blocktim.h"
  16. #include "partest.h"
  17. #include "semtest.h"
  18. #include "PollQ.h"
  19. #include "flash.h"
  20. #include "comtest2.h"

  21. /* Task priorities. */
  22. #define mainQUEUE_POLL_PRIORITY                                ( tskIDLE_PRIORITY + 2 )
  23. #define mainCHECK_TASK_PRIORITY                                ( tskIDLE_PRIORITY + 3 )
  24. #define mainSEM_TEST_PRIORITY                                ( tskIDLE_PRIORITY + 1 )
  25. #define mainBLOCK_Q_PRIORITY                                ( tskIDLE_PRIORITY + 2 )
  26. #define mainCREATOR_TASK_PRIORITY           ( tskIDLE_PRIORITY + 3 )
  27. #define mainFLASH_TASK_PRIORITY                                ( tskIDLE_PRIORITY + 1 )
  28. #define mainCOM_TEST_PRIORITY                                ( tskIDLE_PRIORITY + 1 )
  29. #define mainINTEGER_TASK_PRIORITY           ( tskIDLE_PRIORITY )

  30. /* Constants related to the LCD. */
  31. #define mainMAX_LINE                                                ( 240 )
  32. #define mainROW_INCREMENT                                        ( 24 )
  33. #define mainMAX_COLUMN                                                ( 20 )
  34. #define mainCOLUMN_START                                        ( 319 )
  35. #define mainCOLUMN_INCREMENT                                 ( 16 )

  36. /* The maximum number of message that can be waiting for display at any one
  37. time. */
  38. #define mainLCD_QUEUE_SIZE                                        ( 3 )

  39. /* The check task uses the sprintf function so requires a little more stack. */
  40. #define mainCHECK_TASK_STACK_SIZE                        ( configMINIMAL_STACK_SIZE + 50 )

  41. /* Dimensions the buffer into which the jitter time is written. */
  42. #define mainMAX_MSG_LEN                                                25

  43. /* The time between cycles of the 'check' task. */
  44. #define mainCHECK_DELAY                                                ( ( TickType_t ) 5000 / portTICK_PERIOD_MS )

  45. /* The number of nano seconds between each processor clock. */
  46. #define mainNS_PER_CLOCK ( ( unsigned long ) ( ( 1.0 / ( double ) configCPU_CLOCK_HZ ) * 1000000000.0 ) )

  47. /* Baud rate used by the comtest tasks. */
  48. #define mainCOM_TEST_BAUD_RATE                ( 115200 )

  49. /* The LED used by the comtest tasks. See the comtest.c file for more
  50. information. */
  51. #define mainCOM_TEST_LED                        ( 3 )

  52. /*-----------------------------------------------------------*/

  53. /*
  54. * Configure the clocks, GPIO and other peripherals as required by the demo.
  55. */
  56. static void prvSetupHardware( void );

  57. /*
  58. * Configure the LCD as required by the demo.
  59. */
  60. static void prvConfigureLCD( void );

  61. /*
  62. * The LCD is written two by more than one task so is controlled by a
  63. * 'gatekeeper' task.  This is the only task that is actually permitted to
  64. * access the LCD directly.  Other tasks wanting to display a message send
  65. * the message to the gatekeeper.
  66. */
  67. static void vLCDTask( void *pvParameters );

  68. /*
  69. * Retargets the C library printf function to the USART.
  70. */
  71. int fputc( int ch, FILE *f );

  72. /*
  73. * Checks the status of all the demo tasks then prints a message to the
  74. * display.  The message will be either PASS - and include in brackets the
  75. * maximum measured jitter time (as described at the to of the file), or a
  76. * message that describes which of the standard demo tasks an error has been
  77. * discovered in.
  78. *
  79. * Messages are not written directly to the terminal, but passed to vLCDTask
  80. * via a queue.
  81. */
  82. static void vCheckTask( void *pvParameters );

  83. /*
  84. * Configures the timers and interrupts for the fast interrupt test as
  85. * described at the top of this file.
  86. */
  87. extern void vSetupTimerTest( void );

  88. /*-----------------------------------------------------------*/

  89. /* The queue used to send messages to the LCD task. */
  90. QueueHandle_t xLCDQueue;

  91. /*-----------------------------------------------------------*/

  92. int main( void )
  93. {
  94. #ifdef DEBUG
  95.   debug();
  96. #endif

  97.         prvSetupHardware();

  98.         /* Create the queue used by the LCD task.  Messages for display on the LCD
  99.         are received via this queue. */
  100.         xLCDQueue = xQueueCreate( mainLCD_QUEUE_SIZE, sizeof( xLCDMessage ) );   //创建LCD列队,并返回列队编号

  101.         /* Start the standard demo tasks. */
  102.         vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );                        //创建了6个任务,处理三组参数
  103.     vCreateBlockTimeTasks();                                                 //创建两个block任务  阻塞任务
  104.     vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );                           //
  105.     vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );                       //创建列队测试任务   待挖掘
  106.     vStartIntegerMathTasks( mainINTEGER_TASK_PRIORITY );                     //
  107.         vStartLEDFlashTasks( mainFLASH_TASK_PRIORITY );                          //创建LED翻转任务,创建了3个相同的任务,每个对应不同的灯
  108.         vAltStartComTestTasks( mainCOM_TEST_PRIORITY, mainCOM_TEST_BAUD_RATE, mainCOM_TEST_LED );                 //创建串口收发任务,收和发是两个任务

  109.         /* Start the tasks defined within this file/specific to this demo. */
  110.         xTaskCreate( vCheckTask, "Check", mainCHECK_TASK_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );       //创建LCD内容显示设置任务
  111.                 xTaskCreate( vLCDTask, "LCD", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );                   //创建LCD显示任务

  112.         /* The suicide tasks must be created last as they need to know how many
  113.         tasks were running prior to their creation in order to ascertain whether
  114.         or not the correct/expected number of tasks are running at any given time. */                             
  115.     vCreateSuicidalTasks( mainCREATOR_TASK_PRIORITY );                                                                                                                                                                                                                        //创建一批可以自动删除的任务

  116.         /* Configure the timers used by the fast interrupt timer test. */
  117.         vSetupTimerTest();                                                                                        //初始化定时器

  118.         /* Start the scheduler. */
  119.         vTaskStartScheduler();                                                                                    //启动调度器,开始操作系统

  120.         /* Will only get here if there was not enough heap space to create the
  121.         idle task. */
  122.         return 0;
  123. }
  124. /*-----------------------------------------------------------*/

  125. void vLCDTask( void *pvParameters )
  126. {
  127. xLCDMessage xMessage;

  128.         /* Initialise the LCD and display a startup message. */
  129.         prvConfigureLCD();
  130.         LCD_DrawMonoPict( ( unsigned long * ) pcBitmap );

  131.         for( ;; )
  132.         {
  133.                 /* Wait for a message to arrive that requires displaying. */
  134.                 while( xQueueReceive( xLCDQueue, &xMessage, portMAX_DELAY ) != pdPASS );  //从序列中读取数据,返回有没有

  135.                 /* Display the message.  Print each message to a different position. */
  136.                 printf( ( char const * ) xMessage.pcMessage );
  137.         }
  138. }
  139. /*-----------------------------------------------------------*/

  140. static void vCheckTask( void *pvParameters )
  141. {
  142. TickType_t xLastExecutionTime;
  143. xLCDMessage xMessage;
  144. static signed char cPassMessage[ mainMAX_MSG_LEN ];
  145. extern unsigned short usMaxJitter;

  146.         xLastExecutionTime = xTaskGetTickCount();
  147.         xMessage.pcMessage = cPassMessage;

  148.     for( ;; )
  149.         {
  150.                 /* Perform this check every mainCHECK_DELAY milliseconds. */
  151.                 vTaskDelayUntil( &xLastExecutionTime, mainCHECK_DELAY );    //绝对延时,如果某个延时被任务耽搁了,会在下一次调用时补偿回来

  152.                 /* Has an error been found in any task? */

  153.         if( xAreBlockingQueuesStillRunning() != pdTRUE )
  154.                 {
  155.                         xMessage.pcMessage = "ERROR IN BLOCK Q\n";
  156.                 }
  157.                 else if( xAreBlockTimeTestTasksStillRunning() != pdTRUE )
  158.                 {
  159.                         xMessage.pcMessage = "ERROR IN BLOCK TIME\n";
  160.                 }
  161.         else if( xAreSemaphoreTasksStillRunning() != pdTRUE )
  162.         {
  163.             xMessage.pcMessage = "ERROR IN SEMAPHORE\n";
  164.         }
  165.         else if( xArePollingQueuesStillRunning() != pdTRUE )
  166.         {
  167.             xMessage.pcMessage = "ERROR IN POLL Q\n";
  168.         }
  169.         else if( xIsCreateTaskStillRunning() != pdTRUE )
  170.         {
  171.             xMessage.pcMessage = "ERROR IN CREATE\n";
  172.         }
  173.         else if( xAreIntegerMathsTaskStillRunning() != pdTRUE )
  174.         {
  175.             xMessage.pcMessage = "ERROR IN MATH\n";
  176.         }
  177.                 else if( xAreComTestTasksStillRunning() != pdTRUE )
  178.                 {
  179.                         xMessage.pcMessage = "ERROR IN COM TEST\n";
  180.                 }
  181.                 else
  182.                 {
  183.                         sprintf( ( char * ) cPassMessage, "PASS [%uns]\n", ( ( unsigned long ) usMaxJitter ) * mainNS_PER_CLOCK );
  184.                 }

  185.                 /* Send the message to the LCD gatekeeper for display. */
  186.                 xQueueSend( xLCDQueue, &xMessage, portMAX_DELAY );
  187.         }
  188. }
  189. /*-----------------------------------------------------------*/

  190. static void prvSetupHardware( void )
  191. {
  192.         /* Start with the clocks in their expected state. */
  193.         RCC_DeInit();

  194. ……………………

  195. …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码

所有资料51hei提供下载:
FreeRTOS-project STM32.7z (3.09 MB, 下载次数: 54)


评分

参与人数 1黑币 +50 收起 理由
admin + 50 共享资料的黑币奖励!

查看全部评分

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

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