[牛人杂谈] M480可以直接使用LWIP了

[复制链接]
1869|5
 楼主| 幸福小强 发表于 2018-9-19 21:54 | 显示全部楼层 |阅读模式
 楼主| 幸福小强 发表于 2018-9-19 21:55 | 显示全部楼层
  1. /*
  2.     FreeRTOS V7.4.0 - Copyright (C) 2013 Real Time Engineers Ltd.

  3.     FEATURES AND PORTS ARE ADDED TO FREERTOS ALL THE TIME.  PLEASE VISIT
  4.     http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.

  5.     ***************************************************************************
  6.      *                                                                       *
  7.      *    FreeRTOS tutorial books are available in pdf and paperback.        *
  8.      *    Complete, revised, and edited pdf reference manuals are also       *
  9.      *    available.                                                         *
  10.      *                                                                       *
  11.      *    Purchasing FreeRTOS documentation will not only help you, by       *
  12.      *    ensuring you get running as quickly as possible and with an        *
  13.      *    in-depth knowledge of how to use FreeRTOS, it will also help       *
  14.      *    the FreeRTOS project to continue with its mission of providing     *
  15.      *    professional grade, cross platform, de facto standard solutions    *
  16.      *    for microcontrollers - completely free of charge!                  *
  17.      *                                                                       *
  18.      *    >>> See http://www.FreeRTOS.org/Documentation for details. <<<     *
  19.      *                                                                       *
  20.      *    Thank you for using FreeRTOS, and thank you for your support!      *
  21.      *                                                                       *
  22.     ***************************************************************************


  23.     This file is part of the FreeRTOS distribution.

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

  27.     >>>>>>NOTE<<<<<< The modification to the GPL is included to allow you to
  28.     distribute a combined work that includes FreeRTOS without being obliged to
  29.     provide the source code for proprietary components outside of the FreeRTOS
  30.     kernel.

  31.     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
  32.     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  33.     FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
  34.     details. You should have received a copy of the GNU General Public License
  35.     and the FreeRTOS license exception along with FreeRTOS; if not itcan be
  36.     viewed here: http://www.freertos.org/a00114.html and also obtained by
  37.     writing to Real Time Engineers Ltd., contact details for whom are available
  38.     on the FreeRTOS WEB site.

  39.     1 tab == 4 spaces!

  40.     ***************************************************************************
  41.      *                                                                       *
  42.      *    Having a problem?  Start by reading the FAQ "My application does   *
  43.      *    not run, what could be wrong?"                                     *
  44.      *                                                                       *
  45.      *    http://www.FreeRTOS.org/FAQHelp.html                               *
  46.      *                                                                       *
  47.     ***************************************************************************


  48.     http://www.FreeRTOS.org - Documentation, books, training, latest versions,
  49.     license and Real Time Engineers Ltd. contact details.

  50.     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
  51.     including FreeRTOS+Trace - an indispensable productivity tool, and our new
  52.     fully thread aware and reentrant UDP/IP stack.

  53.     http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
  54.     Integrity Systems, who sell the code with commercial support,
  55.     indemnification and middleware, under the OpenRTOS brand.

  56.     http://www.SafeRTOS.com - High Integrity Systems also provide a safety
  57.     engineered and independently SIL3 certified version for use in safety and
  58.     mission critical applications that require provable dependability.
  59. */

  60. /******************************************************************************
  61. * >>>>>> NOTE 1: <<<<<<
  62. *
  63. * main() can be configured to create either a very simple LED flasher demo, or
  64. * a more comprehensive test/demo application.
  65. *
  66. * To create a very simple LED flasher example, set the
  67. * mainCREATE_SIMPLE_LED_FLASHER_DEMO_ONLY constant (defined below) to 1.  When
  68. * this is done, only the standard demo flash tasks are created.  The standard
  69. * demo flash example creates three tasks, each of which toggle an LED at a
  70. * fixed but different frequency.
  71. *
  72. * To create a more comprehensive test and demo application, set
  73. * mainCREATE_SIMPLE_LED_FLASHER_DEMO_ONLY to 0.
  74. *
  75. * >>>>>> NOTE 2: <<<<<<
  76. *
  77. * In addition to the normal set of standard demo tasks, the comprehensive test
  78. * makes heavy use of the floating point unit, and forces floating point
  79. * instructions to be used from interrupts that nest three deep.  The nesting
  80. * starts from the tick hook function, resulting is an abnormally long context
  81. * switch time.  This is done purely to stress test the FPU context switching
  82. * implementation, and that part of the test can be removed by setting
  83. * configUSE_TICK_HOOK to 0 in FreeRTOSConfig.h.
  84. ******************************************************************************
  85. *
  86. * main() creates all the demo application tasks and software timers, then starts
  87. * the scheduler.  The web documentation provides more details of the standard
  88. * demo application tasks, which provide no particular functionality, but do
  89. * provide a good example of how to use the FreeRTOS API.
  90. *
  91. */

  92. /* A simple HTTP server demonstrates LwIP under FreeRTOS.
  93.    The serverˇs IP address could configure statically to
  94.    192.168.0.2, or assign by DHCP server. */

  95. #include <stdio.h>

  96. /* Kernel includes. */
  97. #include "FreeRTOS.h"
  98. #include "task.h"
  99. #include "timers.h"
  100. #include "semphr.h"

  101. /* Demo application includes. */
  102. #include "partest.h"
  103. #include "flash.h"
  104. #include "flop.h"
  105. #include "integer.h"
  106. #include "PollQ.h"
  107. #include "semtest.h"
  108. #include "dynamic.h"
  109. #include "BlockQ.h"
  110. #include "blocktim.h"
  111. #include "countsem.h"
  112. #include "GenQTest.h"
  113. #include "QueueSet.h"
  114. #include "recmutex.h"
  115. #include "death.h"

  116. /* Hardware and starter kit includes. */
  117. #include "NuMicro.h"


  118. #include "lwip/netifapi.h"
  119. #include "lwip/tcpip.h"
  120. #include "netif/ethernetif.h"
  121. #include "httpserver-netconn.h"

  122. /* Priorities for the demo application tasks. */
  123. #if 0
  124. #define mainFLASH_TASK_PRIORITY            ( tskIDLE_PRIORITY + 1UL )
  125. #define mainQUEUE_POLL_PRIORITY            ( tskIDLE_PRIORITY + 2UL )
  126. #define mainSEM_TEST_PRIORITY              ( tskIDLE_PRIORITY + 1UL )
  127. #define mainBLOCK_Q_PRIORITY               ( tskIDLE_PRIORITY + 2UL )
  128. #define mainCHECK_TASK_PRIORITY            ( tskIDLE_PRIORITY + 3UL )
  129. #else
  130. #define mainFLASH_TASK_PRIORITY            ( tskIDLE_PRIORITY + 1UL )
  131. #define mainQUEUE_POLL_PRIORITY            ( tskIDLE_PRIORITY + 1UL )
  132. #define mainSEM_TEST_PRIORITY              ( tskIDLE_PRIORITY + 1UL )
  133. #define mainCHECK_TASK_PRIORITY            ( tskIDLE_PRIORITY + 3UL )
  134. #endif

  135. #define mainCHECK_TASK_STACK_SIZE            ( configMINIMAL_STACK_SIZE )

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

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

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

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

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

  150. /* Set mainCREATE_SIMPLE_LED_FLASHER_DEMO_ONLY to 1 to create a simple demo.
  151. Set mainCREATE_SIMPLE_LED_FLASHER_DEMO_ONLY to 0 to create a much more
  152. comprehensive test application.  See the comments at the top of this file, and
  153. the documentation page on the http://www.FreeRTOS.org web site for more
  154. information. */
  155. #define mainCREATE_SIMPLE_LED_FLASHER_DEMO_ONLY        0

  156. //#define USE_DHCP

  157. #ifdef USE_DHCP
  158. #include "lwip/dhcp.h"
  159. #endif
  160. /*-----------------------------------------------------------*/

  161. /*
  162. * Set up the hardware ready to run this demo.
  163. */
  164. static void prvSetupHardware( void );
  165. /*-----------------------------------------------------------*/


  166. unsigned char my_mac_addr[6] = {0x00, 0x00, 0x00, 0x55, 0x66, 0x77};
  167. struct netif netif;
  168. static void vWebTask( void *pvParameters );

  169. int main(void)
  170. {
  171.     /* Configure the hardware ready to run the test. */
  172.     prvSetupHardware();

  173.     xTaskCreate( vWebTask, "Web", TCPIP_THREAD_STACKSIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );

  174.     vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );
  175.     vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );
  176.     vStartGenericQueueTasks( tskIDLE_PRIORITY );
  177.     vStartQueueSetTasks();


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

  179.     /* Start the scheduler. */
  180.     vTaskStartScheduler();

  181.     /* If all is well, the scheduler will now be running, and the following line
  182.     will never be reached.  If the following line does execute, then there was
  183.     insufficient FreeRTOS heap memory available for the idle and/or timer tasks
  184.     to be created.  See the memory management section on the FreeRTOS web site
  185.     for more details. */
  186.     for( ;; );
  187. }
  188. /*-----------------------------------------------------------*/

  189. static void prvSetupHardware( void )
  190. {
  191.     /* Unlock protected registers */
  192.     SYS_UnlockReg();

  193.     /* Set XT1_OUT(PF.2) and XT1_IN(PF.3) to input mode */
  194.     PF->MODE &= ~(GPIO_MODE_MODE2_Msk | GPIO_MODE_MODE3_Msk);

  195.     /* Enable External XTAL (4~24 MHz) */
  196.     CLK_EnableXtalRC(CLK_PWRCTL_HXTEN_Msk);

  197.     /* Waiting for 12MHz clock ready */
  198.     CLK_WaitClockReady( CLK_STATUS_HXTSTB_Msk);

  199.     /* Set core clock as PLL_CLOCK from PLL */
  200.     CLK_SetCoreClock(FREQ_192MHZ);

  201.     /* Set both PCLK0 and PCLK1 as HCLK/2 */
  202.     CLK->PCLKDIV = CLK_PCLKDIV_PCLK0DIV2 | CLK_PCLKDIV_PCLK1DIV2;

  203.     /* Enable IP clock */
  204.     CLK_EnableModuleClock(UART0_MODULE);
  205.     CLK_EnableModuleClock(EMAC_MODULE);

  206.     /* Select IP clock source */
  207.     CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_UART0SEL_HXT, CLK_CLKDIV0_UART0(1));

  208.     // Configure MDC clock rate to HCLK / (127 + 1) = 1.5 MHz if system is running at 192 MHz
  209.     CLK_SetModuleClock(EMAC_MODULE, 0, CLK_CLKDIV3_EMAC(127));

  210.     /* Update System Core Clock */
  211.     /* User can use SystemCoreClockUpdate() to calculate SystemCoreClock. */
  212.     SystemCoreClockUpdate();


  213.     /* Set GPB multi-function pins for UART0 RXD and TXD */
  214.     SYS->GPB_MFPH &= ~(SYS_GPB_MFPH_PB12MFP_Msk | SYS_GPB_MFPH_PB13MFP_Msk);
  215.     SYS->GPB_MFPH |= (SYS_GPB_MFPH_PB12MFP_UART0_RXD | SYS_GPB_MFPH_PB13MFP_UART0_TXD);
  216.     // Configure RMII pins
  217.     SYS->GPA_MFPL |= SYS_GPA_MFPL_PA6MFP_EMAC_RMII_RXERR | SYS_GPA_MFPL_PA7MFP_EMAC_RMII_CRSDV;
  218.     SYS->GPC_MFPL |= SYS_GPC_MFPL_PC6MFP_EMAC_RMII_RXD1 | SYS_GPC_MFPL_PC7MFP_EMAC_RMII_RXD0;
  219.     SYS->GPC_MFPH |= SYS_GPC_MFPH_PC8MFP_EMAC_RMII_REFCLK;
  220.     SYS->GPE_MFPH |= SYS_GPE_MFPH_PE8MFP_EMAC_RMII_MDC |
  221.                      SYS_GPE_MFPH_PE9MFP_EMAC_RMII_MDIO |
  222.                      SYS_GPE_MFPH_PE10MFP_EMAC_RMII_TXD0 |
  223.                      SYS_GPE_MFPH_PE11MFP_EMAC_RMII_TXD1 |
  224.                      SYS_GPE_MFPH_PE12MFP_EMAC_RMII_TXEN;

  225.     // Enable high slew rate on all RMII TX output pins
  226.     PE->SLEWCTL = (GPIO_SLEWCTL_HIGH << GPIO_SLEWCTL_HSREN10_Pos) |
  227.                   (GPIO_SLEWCTL_HIGH << GPIO_SLEWCTL_HSREN11_Pos) |
  228.                   (GPIO_SLEWCTL_HIGH << GPIO_SLEWCTL_HSREN12_Pos);

  229.     /* Lock protected registers */
  230.     SYS_LockReg();

  231.     /* Init UART to 115200-8n1 for print message */
  232.     UART_Open(UART0, 115200);
  233. }
  234. /*-----------------------------------------------------------*/

  235. void vApplicationMallocFailedHook( void )
  236. {
  237.     /* vApplicationMallocFailedHook() will only be called if
  238.     configUSE_MALLOC_FAILED_HOOK is set to 1 in FreeRTOSConfig.h.  It is a hook
  239.     function that will get called if a call to pvPortMalloc() fails.
  240.     pvPortMalloc() is called internally by the kernel whenever a task, queue,
  241.     timer or semaphore is created.  It is also called by various parts of the
  242.     demo application.  If heap_1.c or heap_2.c are used, then the size of the
  243.     heap available to pvPortMalloc() is defined by configTOTAL_HEAP_SIZE in
  244.     FreeRTOSConfig.h, and the xPortGetFreeHeapSize() API function can be used
  245.     to query the size of free heap space that remains (although it does not
  246.     provide information on how the remaining heap might be fragmented). */
  247.     taskDISABLE_INTERRUPTS();
  248.     for( ;; );
  249. }
  250. /*-----------------------------------------------------------*/

  251. void vApplicationIdleHook( void )
  252. {
  253.     /* vApplicationIdleHook() will only be called if configUSE_IDLE_HOOK is set
  254.     to 1 in FreeRTOSConfig.h.  It will be called on each iteration of the idle
  255.     task.  It is essential that code added to this hook function never attempts
  256.     to block in any way (for example, call xQueueReceive() with a block time
  257.     specified, or call vTaskDelay()).  If the application makes use of the
  258.     vTaskDelete() API function (as this demo application does) then it is also
  259.     important that vApplicationIdleHook() is permitted to return to its calling
  260.     function, because it is the responsibility of the idle task to clean up
  261.     memory allocated by the kernel to any task that has since been deleted. */
  262. }
  263. /*-----------------------------------------------------------*/

  264. void vApplicationStackOverflowHook( xTaskHandle pxTask, signed char *pcTaskName )
  265. {
  266.     ( void ) pcTaskName;
  267.     ( void ) pxTask;

  268.     /* Run time stack overflow checking is performed if
  269.     configCHECK_FOR_STACK_OVERFLOW is defined to 1 or 2.  This hook
  270.     function is called if a stack overflow is detected. */
  271.     taskDISABLE_INTERRUPTS();
  272.     for( ;; );
  273. }
  274. /*-----------------------------------------------------------*/

  275. void vApplicationTickHook( void )
  276. {
  277.     /* This function will be called by each tick interrupt if
  278.     configUSE_TICK_HOOK is set to 1 in FreeRTOSConfig.h.  User code can be
  279.     added here, but the tick hook is called from an interrupt context, so
  280.     code must not attempt to block, and only the interrupt safe FreeRTOS API
  281.     functions can be used (those that end in FromISR()).  */

  282. #if ( mainCREATE_SIMPLE_BLINKY_DEMO_ONLY == 0 )
  283.     {
  284.         /* In this case the tick hook is used as part of the queue set test. */
  285.         vQueueSetAccessQueueSetFromISR();
  286.     }
  287. #endif /* mainCREATE_SIMPLE_BLINKY_DEMO_ONLY */
  288. }

  289. static void vWebTask( void *pvParameters )
  290. {
  291.     ip_addr_t ipaddr;
  292.     ip_addr_t netmask;
  293.     ip_addr_t gw;

  294.     IP4_ADDR(&gw, 192,168,0,1);
  295.     IP4_ADDR(&ipaddr, 192,168,0,2);
  296.     IP4_ADDR(&netmask, 255,255,255,0);

  297.     tcpip_init(NULL, NULL);

  298.     netif_add(&netif, &ipaddr, &netmask, &gw, NULL, ethernetif_init, tcpip_input);

  299.     netif_set_default(&netif);
  300.     netif_set_up(&netif);

  301. #ifdef USE_DHCP
  302.     dhcp_start(&netif);
  303. #endif

  304.     NVIC_SetPriority(EMAC_TX_IRQn, configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY + 1);
  305.     NVIC_EnableIRQ(EMAC_TX_IRQn);
  306.     NVIC_SetPriority(EMAC_RX_IRQn, configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY + 1);
  307.     NVIC_EnableIRQ(EMAC_RX_IRQn);

  308.     http_server_netconn_init();

  309.     vTaskSuspend( NULL );

  310. }

 楼主| 幸福小强 发表于 2018-9-19 21:55 | 显示全部楼层
这个是httpd的例子,大家可以下载学习。
643757107 发表于 2018-9-19 23:06 | 显示全部楼层
不用用户再移植担忧了。
zhuomuniao110 发表于 2018-9-20 11:17 | 显示全部楼层
学会FreeRTOS就好学这个了。
捉虫天师 发表于 2018-9-20 20:39 | 显示全部楼层
确实方便了很多。未来是有线的天下还是无线的天下,就这个网络?
我认为5G的兴起,将会导致流量费超低,甚至无线可以用带宽来购买,比如我买2M码流。4M,10M,这种。
10年后,有线网络将会逐渐消失,至少终端到网络这一层是要消失的。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

143

主题

1727

帖子

3

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