標(biāo)題: FreeRtos STM32工程 代碼帶部分中文注釋 [打印本頁]

作者: 13523698526    時(shí)間: 2020-10-16 17:15
標(biāo)題: FreeRtos STM32工程 代碼帶部分中文注釋
STM32F103C8T6的FreeRtos工程。
帶部分中文注釋。希望可以幫組初學(xué)者。

單片機(jī)源程序如下:
  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 ) );   //創(chuàng)建LCD列隊(duì),并返回列隊(duì)編號

  101.         /* Start the standard demo tasks. */
  102.         vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );                        //創(chuàng)建了6個(gè)任務(wù),處理三組參數(shù)
  103.     vCreateBlockTimeTasks();                                                 //創(chuàng)建兩個(gè)block任務(wù)  阻塞任務(wù)
  104.     vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );                           //
  105.     vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );                       //創(chuàng)建列隊(duì)測試任務(wù)   待挖掘
  106.     vStartIntegerMathTasks( mainINTEGER_TASK_PRIORITY );                     //
  107.         vStartLEDFlashTasks( mainFLASH_TASK_PRIORITY );                          //創(chuàng)建LED翻轉(zhuǎn)任務(wù),創(chuàng)建了3個(gè)相同的任務(wù),每個(gè)對應(yīng)不同的燈
  108.         vAltStartComTestTasks( mainCOM_TEST_PRIORITY, mainCOM_TEST_BAUD_RATE, mainCOM_TEST_LED );                 //創(chuàng)建串口收發(fā)任務(wù),收和發(fā)是兩個(gè)任務(wù)

  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 );       //創(chuàng)建LCD內(nèi)容顯示設(shè)置任務(wù)
  111.                 xTaskCreate( vLCDTask, "LCD", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );                   //創(chuàng)建LCD顯示任務(wù)

  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 );                                                                                                                                                                                                                        //創(chuàng)建一批可以自動(dòng)刪除的任務(wù)

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

  118.         /* Start the scheduler. */
  119.         vTaskStartScheduler();                                                                                    //啟動(dòng)調(diào)度器,開始操作系統(tǒng)

  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 );  //從序列中讀取數(shù)據(jù),返回有沒有

  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 );    //絕對延時(shí),如果某個(gè)延時(shí)被任務(wù)耽擱了,會在下一次調(diào)用時(shí)補(bǔ)償回來

  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黑下載附件…………
復(fù)制代碼

所有資料51hei提供下載:
FreeRTOS-project STM32.7z (3.09 MB, 下載次數(shù): 57)







歡迎光臨 (http://www.torrancerestoration.com/bbs/) Powered by Discuz! X3.1