打印
[DemoCode下载]

M451在FreeRTOS下驱动 DM9051

[复制链接]
61|3
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
EC_M451_SPI_DM9051_uIP_FreeRTOS_V1.00.zip (2.68 MB)

本范例代码使用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内部寄存器和内存中的包数据时的命令开销。



使用特权

评论回复
沙发
玛尼玛尼哄|  楼主 | 2024-11-22 19:11 | 只看该作者

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

/* Scheduler includes. */
#include "FreeRTOS.h"
#include "task.h"
#include "queue.h"
#include "M451Series.h"
#include "uIP_Task.h"

/* Task function*/
static void vLEDTask(void *pvParameters);
/* Sys init function*/
static void prvSetupHardware(void);
void SYS_Init(void);
void UART0_Init(void);
int Web_LED_FLASH = 1; // Default set 1 use freertos task control led, if set 0 web control

int main(void)
{
    /* Init System */
    prvSetupHardware();
    /* create task */
    xTaskCreate(vuIP_Task,  "uIP", configMINIMAL_STACK_SIZE * 3, NULL, tskIDLE_PRIORITY + 4, NULL);
    xTaskCreate(vLEDTask,  "LED", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY + 1, NULL);
    printf("FreeRTOS is starting ...\n");
    /* start OS */
    vTaskStartScheduler();

    for (;;);
}

/*-----------------------------------------------------------*/
void vLEDTask(void *pvParameters)
{
    /* Set LED */
    GPIO_EnableInt(PC, 9, GPIO_INT_FALLING);
    GPIO_SetMode(PC, BIT9, GPIO_MODE_OUTPUT);

    for (;;)
    {
        if (Web_LED_FLASH == 1)
        {
            PC9 = 1;
            vTaskDelay(500 / portTICK_RATE_MS);
            PC9 = 0;
            vTaskDelay(500 / portTICK_RATE_MS);
        }
    }
}

/*-----------------------------------------------------------*/
static void prvSetupHardware(void)
{
    SYS_Init();
    UART0_Init();
}

/*---------------------------------------------------------------------------------------------------------*/
/* Init NUC_M451 System Clock                                                                                       */
/*---------------------------------------------------------------------------------------------------------*/
void SYS_Init(void)
{
    /* Unlock protected registers */
    SYS_UnlockReg();
    /*---------------------------------------------------------------------------------------------------------*/
    /* Init System Clock                                                                                       */
    /*---------------------------------------------------------------------------------------------------------*/
    /* Enable HIRC clock (Internal RC 22.1184MHz) */
    CLK_EnableXtalRC(CLK_PWRCTL_HIRCEN_Msk);

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

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

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

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

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

    CLK_EnableModuleClock(TMR0_MODULE);

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

    /* Lock protected registers */
    SYS_LockReg();
}

/*-----------------------------------------------------------*/
void UART0_Init(void)
{
    /* Enable UART0 Module clock */
    CLK_EnableModuleClock(UART0_MODULE);
    /* UART0 module clock from EXT */
    CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_UARTSEL_HXT, CLK_CLKDIV0_UART(1));
    /*---------------------------------------------------------------------------------------------------------*/
    /* Init I/O Multi-function                                                                                 */
    /*---------------------------------------------------------------------------------------------------------*/
    /* Configure multi-function pins for UART0 RXD and TXD */
    SYS->GPD_MFPL &= ~(SYS_GPD_MFPL_PD0MFP_Msk | SYS_GPD_MFPL_PD1MFP_Msk);
    SYS->GPD_MFPL |= (SYS_GPD_MFPL_PD0MFP_UART0_RXD | SYS_GPD_MFPL_PD1MFP_UART0_TXD);
    /* Configure UART0 and set UART0 Baud-rate */
    UART_Open(UART0, 115200);
}

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

    //taskDISABLE_INTERRUPTS();
    //for(;;);
}

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

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

void vApplicationIdleHook(void)
{
    /* vApplicationIdleHook() will only be called if configUSE_IDLE_HOOK is set
    to 1 in FreeRTOSConfig.h.  It will be called on each iteration of the idle
    task.  It is essential that code added to this hook function never attempts
    to block in any way (for example, call xQueueReceive() with a block time
    specified, or call vTaskDelay()).  If the application makes use of the
    vTaskDelete() API function (as this demo application does) then it is also
    important that vApplicationIdleHook() is permitted to return to its calling
    function, because it is the responsibility of the idle task to clean up
    memory allocated by the kernel to any task that has since been deleted. */
}
/*** (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,不是有吗

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

179

主题

3074

帖子

2

粉丝