[DemoCode下载] M451跑FreeRTOS

[复制链接]
910|2
 楼主| yiyigirl2014 发表于 2019-4-30 21:33 | 显示全部楼层 |阅读模式
  1. #include <stdio.h>

  2. /* Kernel includes. */
  3. #include "FreeRTOS.h"
  4. #include "task.h"
  5. #include "timers.h"
  6. #include "semphr.h"

  7. /* Demo application includes. */
  8. #include "partest.h"
  9. #include "flash.h"
  10. #include "flop.h"
  11. #include "integer.h"
  12. #include "PollQ.h"
  13. #include "semtest.h"
  14. #include "dynamic.h"
  15. #include "BlockQ.h"
  16. #include "blocktim.h"
  17. #include "countsem.h"
  18. #include "GenQTest.h"
  19. #include "QueueSet.h"
  20. #include "recmutex.h"
  21. #include "death.h"

  22. /* Hardware and starter kit includes. */
  23. #include "M451Series.h"

  24. #define PLL_CLOCK           72000000

  25. /* Priorities for the demo application tasks. */
  26. #define mainFLASH_TASK_PRIORITY             ( tskIDLE_PRIORITY + 1UL )
  27. #define mainQUEUE_POLL_PRIORITY             ( tskIDLE_PRIORITY + 2UL )
  28. #define mainSEM_TEST_PRIORITY               ( tskIDLE_PRIORITY + 1UL )
  29. #define mainBLOCK_Q_PRIORITY                ( tskIDLE_PRIORITY + 2UL )
  30. #define mainCREATOR_TASK_PRIORITY           ( tskIDLE_PRIORITY + 3UL )
  31. #define mainFLOP_TASK_PRIORITY              ( tskIDLE_PRIORITY )
  32. #define mainCHECK_TASK_PRIORITY             ( tskIDLE_PRIORITY + 3UL )

  33. #define mainCHECK_TASK_STACK_SIZE           ( configMINIMAL_STACK_SIZE )

  34. /* The time between cycles of the 'check' task. */
  35. #define mainCHECK_DELAY                     ( ( portTickType ) 5000 / portTICK_RATE_MS )

  36. /* The LED used by the check timer. */
  37. #define mainCHECK_LED                       ( 3UL )

  38. /* A block time of zero simply means "don't block". */
  39. #define mainDONT_BLOCK                      ( 0UL )

  40. /* The period after which the check timer will expire, in ms, provided no errors
  41. have been reported by any of the standard demo tasks.  ms are converted to the
  42. equivalent in ticks using the portTICK_RATE_MS constant. */
  43. #define mainCHECK_TIMER_PERIOD_MS           ( 3000UL / portTICK_RATE_MS )

  44. /* The period at which the check timer will expire, in ms, if an error has been
  45. reported in one of the standard demo tasks.  ms are converted to the equivalent
  46. in ticks using the portTICK_RATE_MS constant. */
  47. #define mainERROR_CHECK_TIMER_PERIOD_MS     ( 200UL / portTICK_RATE_MS )

  48. /* Set mainCREATE_SIMPLE_LED_FLASHER_DEMO_ONLY to 1 to create a simple demo.
  49. Set mainCREATE_SIMPLE_LED_FLASHER_DEMO_ONLY to 0 to create a much more
  50. comprehensive test application.  See the comments at the top of this file, and
  51. the documentation page on the http://www.FreeRTOS.org web site for more
  52. information. */
  53. #define mainCREATE_SIMPLE_LED_FLASHER_DEMO_ONLY     0

  54. #define CHECK_TEST

  55. /*-----------------------------------------------------------*/

  56. /*
  57. * Set up the hardware ready to run this demo.
  58. */
  59. static void prvSetupHardware(void);
  60. /*-----------------------------------------------------------*/

  61. #ifdef CHECK_TEST
  62. static void vCheckTask(void *pvParameters);
  63. #endif

  64. int main(void)
  65. {
  66.     /* Configure the hardware ready to run the test. */
  67.     prvSetupHardware();


  68. #ifdef CHECK_TEST
  69.     xTaskCreate(vCheckTask, (signed portCHAR *) "Check", mainCHECK_TASK_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL);
  70. #endif


  71.     /* Start standard demo/test application flash tasks.  See the comments at
  72.     the top of this file.  The LED flash tasks are always created.  The other
  73.     tasks are only created if mainCREATE_SIMPLE_LED_FLASHER_DEMO_ONLY is set to
  74.     0 (at the top of this file).  See the comments at the top of this file for
  75.     more information. */
  76.     vStartLEDFlashTasks(mainFLASH_TASK_PRIORITY);

  77.     vStartPolledQueueTasks(mainQUEUE_POLL_PRIORITY);

  78.     /* The following function will only create more tasks and timers if
  79.     mainCREATE_SIMPLE_LED_FLASHER_DEMO_ONLY is set to 0 (at the top of this
  80.     file).  See the comments at the top of this file for more information. */
  81.     //prvOptionallyCreateComprehensveTestApplication();

  82.     printf("FreeRTOS is starting ...\n");

  83.     /* Start the scheduler. */
  84.     vTaskStartScheduler();

  85.     /* If all is well, the scheduler will now be running, and the following line
  86.     will never be reached.  If the following line does execute, then there was
  87.     insufficient FreeRTOS heap memory available for the idle and/or timer tasks
  88.     to be created.  See the memory management section on the FreeRTOS web site
  89.     for more details. */
  90.     for(;;);
  91. }
  92. /*-----------------------------------------------------------*/

  93. static void prvSetupHardware(void)
  94. {
  95.     /* Unlock protected registers */
  96.     SYS_UnlockReg();

  97.     /* Enable HIRC clock */
  98.     CLK_EnableXtalRC(CLK_PWRCTL_HIRCEN_Msk);

  99.     /* Waiting for HIRC clock ready */
  100.     CLK_WaitClockReady(CLK_STATUS_HIRCSTB_Msk);

  101.     /* Switch HCLK clock source to HIRC */
  102.     CLK_SetHCLK(CLK_CLKSEL0_HCLKSEL_HIRC, CLK_CLKDIV0_HCLK(1));

  103.     /* Enable HXT clock (external XTAL 12MHz) */
  104.     CLK_EnableXtalRC(CLK_PWRCTL_HXTEN_Msk);

  105.     /* Wait for HXT clock ready */
  106.     CLK_WaitClockReady(CLK_STATUS_HXTSTB_Msk);

  107.     /* Configure PLL */
  108.     CLK_EnablePLL(CLK_PLLCTL_PLLSRC_HXT, PLL_CLOCK);

  109.     /* Switch HCLK clock source to PLL */
  110.     CLK_SetHCLK(CLK_CLKSEL0_HCLKSEL_PLL, CLK_CLKDIV0_HCLK(1));

  111.     /* Select UART module clock source as HXT and UART module clock divider as 1 */
  112.     CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_UARTSEL_HXT, CLK_CLKDIV0_UART(1));

  113.     /* Enable peripheral clock */
  114.     CLK_EnableModuleClock(UART0_MODULE);
  115.     CLK_EnableModuleClock(TMR0_MODULE);

  116.     /* Update System Core Clock */
  117.     /* User can use SystemCoreClockUpdate() to calculate SystemCoreClock. */
  118.     SystemCoreClockUpdate();


  119.     /*---------------------------------------------------------------------------------------------------------*/
  120.     /* Init I/O Multi-function                                                                                 */
  121.     /*---------------------------------------------------------------------------------------------------------*/
  122.     /* Set PD multi-function pins for UART0 RXD and TXD */
  123.     SYS->GPD_MFPL &= ~(SYS_GPD_MFPL_PD0MFP_Msk | SYS_GPD_MFPL_PD1MFP_Msk);
  124.     SYS->GPD_MFPL |= (SYS_GPD_MFPL_PD0MFP_UART0_RXD | SYS_GPD_MFPL_PD1MFP_UART0_TXD);

  125.     /* Lock protected registers */
  126.     SYS_LockReg();

  127.     /* Init UART to 115200-8n1 for print message */
  128.     UART_Open(UART0, 115200);
  129. }
  130. /*-----------------------------------------------------------*/

  131. void vApplicationMallocFailedHook(void)
  132. {
  133.     /* vApplicationMallocFailedHook() will only be called if
  134.     configUSE_MALLOC_FAILED_HOOK is set to 1 in FreeRTOSConfig.h.  It is a hook
  135.     function that will get called if a call to pvPortMalloc() fails.
  136.     pvPortMalloc() is called internally by the kernel whenever a task, queue,
  137.     timer or semaphore is created.  It is also called by various parts of the
  138.     demo application.  If heap_1.c or heap_2.c are used, then the size of the
  139.     heap available to pvPortMalloc() is defined by configTOTAL_HEAP_SIZE in
  140.     FreeRTOSConfig.h, and the xPortGetFreeHeapSize() API function can be used
  141.     to query the size of free heap space that remains (although it does not
  142.     provide information on how the remaining heap might be fragmented). */
  143.     taskDISABLE_INTERRUPTS();
  144.     for(;;);
  145. }
  146. /*-----------------------------------------------------------*/

  147. void vApplicationIdleHook(void)
  148. {
  149.     /* vApplicationIdleHook() will only be called if configUSE_IDLE_HOOK is set
  150.     to 1 in FreeRTOSConfig.h.  It will be called on each iteration of the idle
  151.     task.  It is essential that code added to this hook function never attempts
  152.     to block in any way (for example, call xQueueReceive() with a block time
  153.     specified, or call vTaskDelay()).  If the application makes use of the
  154.     vTaskDelete() API function (as this demo application does) then it is also
  155.     important that vApplicationIdleHook() is permitted to return to its calling
  156.     function, because it is the responsibility of the idle task to clean up
  157.     memory allocated by the kernel to any task that has since been deleted. */
  158. }
  159. /*-----------------------------------------------------------*/

  160. void vApplicationStackOverflowHook(xTaskHandle pxTask, signed char *pcTaskName)
  161. {
  162.     (void) pcTaskName;
  163.     (void) pxTask;

  164.     /* Run time stack overflow checking is performed if
  165.     configCHECK_FOR_STACK_OVERFLOW is defined to 1 or 2.  This hook
  166.     function is called if a stack overflow is detected. */
  167.     taskDISABLE_INTERRUPTS();
  168.     for(;;);
  169. }
  170. /*-----------------------------------------------------------*/

  171. void vApplicationTickHook(void)
  172. {
  173.     /* This function will be called by each tick interrupt if
  174.     configUSE_TICK_HOOK is set to 1 in FreeRTOSConfig.h.  User code can be
  175.     added here, but the tick hook is called from an interrupt context, so
  176.     code must not attempt to block, and only the interrupt safe FreeRTOS API
  177.     functions can be used (those that end in FromISR()).  */

  178. #if ( mainCREATE_SIMPLE_BLINKY_DEMO_ONLY == 0 )
  179.     {
  180.         /* In this case the tick hook is used as part of the queue set test. */
  181.         vQueueSetAccessQueueSetFromISR();
  182.     }
  183. #endif /* mainCREATE_SIMPLE_BLINKY_DEMO_ONLY */
  184. }
  185. /*-----------------------------------------------------------*/
  186. #ifdef CHECK_TEST
  187. static void vCheckTask(void *pvParameters)
  188. {
  189.     portTickType xLastExecutionTime;

  190.     xLastExecutionTime = xTaskGetTickCount();

  191.     printf("M451 Check Task is running ...\n");
  192.     printf("LED toggle at PA.13\n\n");

  193.     for(;;)
  194.     {
  195.         /* Perform this check every mainCHECK_DELAY milliseconds. */
  196.         vTaskDelayUntil(&xLastExecutionTime, mainCHECK_DELAY);
  197.         if(xArePollingQueuesStillRunning() != pdTRUE)
  198.         {
  199.             printf("ERROR IN POLL Q\n");
  200.         }
  201.     }
  202. }
  203. #endif


 楼主| yiyigirl2014 发表于 2019-4-30 21:34 | 显示全部楼层
FreeRTOS.rar (20.68 KB, 下载次数: 11)
zhuomuniao110 发表于 2019-4-30 21:37 | 显示全部楼层
可以,这个是不是可以很容易移植到其他的新唐系列?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

230

主题

3676

帖子

10

粉丝
快速回复 在线客服 返回列表 返回顶部