[DemoCode下载] M451在FreeRTOS下驱动 DM9051

[复制链接]
1996|3
 楼主| 玛尼玛尼哄 发表于 2024-11-22 19:11 | 显示全部楼层 |阅读模式
EC_M451_SPI_DM9051_uIP_FreeRTOS_V1.00.zip (2.68 MB, 下载次数: 6)

本范例代码使用M451系列通过SPI接口驱动DM9051,在FreeRTOS下实现uIP/httpd功能,以及PC9闪灯。

DM9051是一个高集成和低成本的少管脚数的单芯片快速以太网控制器,具有串行外围接口(SPI), 10/100M PHY和MAC,以及16k字节的SRAM。低功耗、高性能的工艺接口,支持3.3V、5V IO融差。 DM9051的物理层可以通过HP Auto-MDIX与10Base-T中的UTP3、4、5和100Base-TX中的UTP5连接。它完全符合IEEE 802.3u规范,其自动协商功能将自动配置DM9051,最大限度地利用其10M或100M的能力。 DM9051在PHY和MAC中支持IEEE 802.3az,以节省以太网空闲时的功耗。支持ieee802.3X全双工流量控制和半双工背压功能,避免与链路伙伴丢失以太网数据包。
作为SPI从机接口支持与CPU的SPI主机接口兼容的SPI时钟模式0和3。时钟速度可以高达50Mhz,以配合高速的主SPI。SPI突发命令格式是有效的代码,可以最小化访问DM9051内部寄存器和内存中的包数据时的命令开销。


45212674066df91171.png
 楼主| 玛尼玛尼哄 发表于 2024-11-22 19:11 | 显示全部楼层

  1. /*************************************************************************//**
  2. * [url=home.php?mod=space&uid=288409]@file[/url]     main.c
  3. * [url=home.php?mod=space&uid=895143]@version[/url]  V1.00
  4. * [url=home.php?mod=space&uid=247401]@brief[/url]    A uIP httpd sample for M451 MCU and DM9051 with FreeRTOS.
  5. *
  6. *
  7. * [url=home.php?mod=space&uid=17282]@CopyRight[/url] (C) 2019 Nuvoton Technology Corp. All rights reserved.
  8. *****************************************************************************/
  9. /* Standard includes. */
  10. #include <stdio.h>

  11. /* Scheduler includes. */
  12. #include "FreeRTOS.h"
  13. #include "task.h"
  14. #include "queue.h"
  15. #include "M451Series.h"
  16. #include "uIP_Task.h"

  17. /* Task function*/
  18. static void vLEDTask(void *pvParameters);
  19. /* Sys init function*/
  20. static void prvSetupHardware(void);
  21. void SYS_Init(void);
  22. void UART0_Init(void);
  23. int Web_LED_FLASH = 1; // Default set 1 use freertos task control led, if set 0 web control

  24. int main(void)
  25. {
  26.     /* Init System */
  27.     prvSetupHardware();
  28.     /* create task */
  29.     xTaskCreate(vuIP_Task,  "uIP", configMINIMAL_STACK_SIZE * 3, NULL, tskIDLE_PRIORITY + 4, NULL);
  30.     xTaskCreate(vLEDTask,  "LED", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY + 1, NULL);
  31.     printf("FreeRTOS is starting ...\n");
  32.     /* start OS */
  33.     vTaskStartScheduler();

  34.     for (;;);
  35. }

  36. /*-----------------------------------------------------------*/
  37. void vLEDTask(void *pvParameters)
  38. {
  39.     /* Set LED */
  40.     GPIO_EnableInt(PC, 9, GPIO_INT_FALLING);
  41.     GPIO_SetMode(PC, BIT9, GPIO_MODE_OUTPUT);

  42.     for (;;)
  43.     {
  44.         if (Web_LED_FLASH == 1)
  45.         {
  46.             PC9 = 1;
  47.             vTaskDelay(500 / portTICK_RATE_MS);
  48.             PC9 = 0;
  49.             vTaskDelay(500 / portTICK_RATE_MS);
  50.         }
  51.     }
  52. }

  53. /*-----------------------------------------------------------*/
  54. static void prvSetupHardware(void)
  55. {
  56.     SYS_Init();
  57.     UART0_Init();
  58. }

  59. /*---------------------------------------------------------------------------------------------------------*/
  60. /* Init NUC_M451 System Clock                                                                                       */
  61. /*---------------------------------------------------------------------------------------------------------*/
  62. void SYS_Init(void)
  63. {
  64.     /* Unlock protected registers */
  65.     SYS_UnlockReg();
  66.     /*---------------------------------------------------------------------------------------------------------*/
  67.     /* Init System Clock                                                                                       */
  68.     /*---------------------------------------------------------------------------------------------------------*/
  69.     /* Enable HIRC clock (Internal RC 22.1184MHz) */
  70.     CLK_EnableXtalRC(CLK_PWRCTL_HIRCEN_Msk);

  71.     /* Wait for HIRC clock ready */
  72.     CLK_WaitClockReady(CLK_STATUS_HIRCSTB_Msk);

  73.     /* Select HCLK clock source as HIRC and and HCLK source divider as 1 */
  74.     CLK_SetHCLK(CLK_CLKSEL0_HCLKSEL_HIRC, CLK_CLKDIV0_HCLK(1));

  75.     /* Enable HXT clock (external XTAL 12MHz) */
  76.     CLK_EnableXtalRC(CLK_PWRCTL_HXTEN_Msk);

  77.     /* Wait for HXT clock ready */
  78.     CLK_WaitClockReady(CLK_STATUS_HXTSTB_Msk);

  79.     /* Set core clock as PLL_CLOCK from PLL */
  80.     CLK_SetCoreClock(FREQ_72MHZ);

  81.     CLK_EnableModuleClock(TMR0_MODULE);

  82.     /* Update System Core Clock */
  83.     /* User can use SystemCoreClockUpdate() to calculate SystemCoreClock. */
  84.     SystemCoreClockUpdate();

  85.     /* Lock protected registers */
  86.     SYS_LockReg();
  87. }

  88. /*-----------------------------------------------------------*/
  89. void UART0_Init(void)
  90. {
  91.     /* Enable UART0 Module clock */
  92.     CLK_EnableModuleClock(UART0_MODULE);
  93.     /* UART0 module clock from EXT */
  94.     CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_UARTSEL_HXT, CLK_CLKDIV0_UART(1));
  95.     /*---------------------------------------------------------------------------------------------------------*/
  96.     /* Init I/O Multi-function                                                                                 */
  97.     /*---------------------------------------------------------------------------------------------------------*/
  98.     /* Configure multi-function pins for UART0 RXD and TXD */
  99.     SYS->GPD_MFPL &= ~(SYS_GPD_MFPL_PD0MFP_Msk | SYS_GPD_MFPL_PD1MFP_Msk);
  100.     SYS->GPD_MFPL |= (SYS_GPD_MFPL_PD0MFP_UART0_RXD | SYS_GPD_MFPL_PD1MFP_UART0_TXD);
  101.     /* Configure UART0 and set UART0 Baud-rate */
  102.     UART_Open(UART0, 115200);
  103. }

  104. void vApplicationMallocFailedHook(void)
  105. {
  106.     /* vApplicationMallocFailedHook() will only be called if
  107.     configUSE_MALLOC_FAILED_HOOK is set to 1 in FreeRTOSConfig.h.  It is a hook
  108.     function that will get called if a call to pvPortMalloc() fails.
  109.     pvPortMalloc() is called internally by the kernel whenever a task, queue,
  110.     timer or semaphore is created.  It is also called by various parts of the
  111.     demo application.  If heap_1.c or heap_2.c are used, then the size of the
  112.     heap available to pvPortMalloc() is defined by configTOTAL_HEAP_SIZE in
  113.     FreeRTOSConfig.h, and the xPortGetFreeHeapSize() API function can be used
  114.     to query the size of free heap space that remains (although it does not
  115.     provide information on how the remaining heap might be fragmented). */

  116.     //taskDISABLE_INTERRUPTS();
  117.     //for(;;);
  118. }

  119. void vApplicationTickHook(void)
  120. {
  121.     /* This function will be called by each tick interrupt if
  122.     configUSE_TICK_HOOK is set to 1 in FreeRTOSConfig.h.  User code can be
  123.     added here, but the tick hook is called from an interrupt context, so
  124.     code must not attempt to block, and only the interrupt safe FreeRTOS API
  125.     functions can be used (those that end in FromISR()).  */

  126. #if ( mainCREATE_SIMPLE_BLINKY_DEMO_ONLY == 0 )
  127.     {
  128.         /* In this case the tick hook is used as part of the queue set test. */
  129.         //vQueueSetAccessQueueSetFromISR();
  130.     }
  131. #endif /* mainCREATE_SIMPLE_BLINKY_DEMO_ONLY */
  132. }

  133. void vApplicationIdleHook(void)
  134. {
  135.     /* vApplicationIdleHook() will only be called if configUSE_IDLE_HOOK is set
  136.     to 1 in FreeRTOSConfig.h.  It will be called on each iteration of the idle
  137.     task.  It is essential that code added to this hook function never attempts
  138.     to block in any way (for example, call xQueueReceive() with a block time
  139.     specified, or call vTaskDelay()).  If the application makes use of the
  140.     vTaskDelete() API function (as this demo application does) then it is also
  141.     important that vApplicationIdleHook() is permitted to return to its calling
  142.     function, because it is the responsibility of the idle task to clean up
  143.     memory allocated by the kernel to any task that has since been deleted. */
  144. }
  145. /*** (C) COPYRIGHT 2019 Nuvoton Technology Corp. ***/
 楼主| 玛尼玛尼哄 发表于 2024-11-22 19:12 | 显示全部楼层
RTOS 让多任务开发变的更容易。
caoqing 发表于 2024-11-22 20:56 | 显示全部楼层
https://bbs.21ic.com/icview-1348414-1-1.html,不是有吗
您需要登录后才可以回帖 登录 | 注册

本版积分规则

196

主题

3261

帖子

2

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