7.11.2. 参数配置

7.11.2.1. 以太网配置

内核配置主要是通过 scons --menuconfig 命令进行 以太网的功能配置, 配置完成后的项目存储在 “target/configs/xxx_defconfig” 文件中

7.11.2.1.1. MAC参数配置

内核中要使用以太网功能,首先需要配置 MAC 的属性参数。MAC 的参数随着板卡的不同而不同

Board options --->
    [*] Using Gmac0
        gmac0 parameter --->
            (PE.6) gmac0 phy reset gpio
               Select gmac0 bus (RMII) --->
               Select phy clk (external clk) --->
            (12) gmac0 rx delay
            (12) gmac0 tx delay
            (192.168.1.2) gmac0 IPv4 addr
            (192.168.1.1) gmac0 Gateway
            (255.255.255.0) gmac0 Subnet Mask
            (002244887766) gmac0 MAC addr
            (1) gmac0 PHY MDIO addr
  • gmac0 phy reset gpio

    外部 PHY 芯片的复位脚相连接的IO引脚,根据板卡设计决定

  • Select gmac0 bus

    选择 MAC 与 PHY 相连接的接口类型,根据板卡决定是 RGMII 还是 RMII

  • Select phy clk

    选择MAC使用的总线参考时钟类型是 external clk (外部时钟)还是 internal clk (内部时钟)。如果选用的是 RGMII 接口,且硬件没有连接 GMAC_CLKIN 引脚的前提下,只能使用 internal clk ,除此之外,建议全部选用 external clk

  • gmac0 rx delay

    gmac 的接收时延,适用于 RGMII 接口模式下, MAC 匹配 PHY 芯片的数据建立时间和保留时 间(单位:0.17ns), 需要根据外部使用的PHY芯片调整。一般情况下,设置为 12 ,即 2.04ns

  • gmac0 tx delay

    gmac0的发送时延,适用于RGMII接口模式下,MAC 匹配 PHY 芯片的数据建立时间和保留时间 (单位:0.17ns),需要根据外部使用的PHY芯片调整。一般情况下,可设置为12,即2.04ns

  • gmac0 IPv4 addr

    当前网卡的初始静态IP地址,如打开DHCP功能,此处设置无效

  • gmac0 Gateway

    当前网卡的初始静态网关地址,如打开DHCP功能,此处设置无效

  • gmac0 Subnet Mask

    当前网卡初始静态子网掩码,如打开DHCP功能,此处设置无效

  • gmac0 MAC addr

    gmac0默认MAC地址

  • gmac0 PHY MDIO addr

    PHY 在 总线上的挂载地址,PHY芯片地址一般取决于PHY芯片外部电路设计,请根据板卡设计决定

7.11.2.1.2. LwIP协议配置

zx-rtt内置LwIP2.1.3协议栈,用户可以根据自身需求通过 scons --menuconfig 进行裁剪。 zx-rtt提供的可裁剪属性如下,如有其他特别需求,可自行配置 “packages/third-party/lwip/lwipopts.h” 配置文件

Local packages options --->
    Third-party packages options --->
        [*] LwIP: light weight TCP/IP stack --->
            [ ] IPV6 protocol
            [*] LwIP Example --->
                [*] example test
            [ ] IGMP protocol
            [*] ICMP protocol
            [ ] SNMP protocol
            [ ] Enable DNS for name resolution
            [ ] Enable alloc ip address through DHCP
            [*] UDP protocol
            [*] TCP protocol
            [*] RAW protocol
            [ ] PPP protocol
            (8) the number of struct netconns
            (16) the number of PBUF
            (15360) the size of LwIP's dynamic memory
            (4) the number of raw connection
            (4) the number of UDP socket
            (4) the number of TCP socket
            (40) the number of TCP segment
            (8196) the size of send buffer
            (8196) the size of TCP send window
            (10) the priority level value of lwIP thread
            (8) the number of mail in the lwIP thread mailbox
            (3072) the stack size of lwIP thread
            [ ] Enable IP reassembly and frag
            [ ] Enable lwIP statistics
            [ ] Enable tx hardware generate checksum
            [ ] Enable rx hardware check checksum
            [ ] Enable ping features  ----
                [ ] Using socket interface during ping requests
                    ( ) the priority level value of ping thread
            [ ] Using MQTT protocol
            [ ] Enable lwIP Debugging Options  ----
  • IPV6 protocol

    LwIP支持IPV6协议

  • LwIP Example

    SDK 提供的初始化以太网示例,示例程序源码在“packages/third-party/lwip/contrib/examples/example_app/” 目录下的 test.c netif_start.c netif_start.h。打开此选项后,SDK会在启动过程中自动 初始化以太网。用户可根据自身需求定义以太网初始化流程

  • IGMP protocol

    以太网支持IGMP协议,使用组播功能时打开

  • ICMP protocol

    以太网支持ICMP协议,使用ping命令时打开

  • SNMP protocol

    以太网支持SNMP协议,需要时打开

  • Enable DNS for name resolution

    支持DNS客户端功能,需要时打开

  • Enable alloc ip address through DHCP

    支持DHCP客户端功能,需要时打开。如果打开DHCP选项,默认IP地址会失效,如果IP申请失败,则此时IP地址为0.0.0.0

  • UDP protocol

    支持UDP协议,默认打开

  • TCP protocol

    支持TCP协议,默认打开

  • RAW protocol

    支持原始套接字接口,如需要编写基于RAW套接字的程序时,需要打开

  • PPP protocol

    支持PPP协议,需要时打开

  • the number of struct netconns

    struct netconns接口的数量,默认为8个,可根据需求自行修改

  • the number of PBUF

    pbuf 结构的数量,如果需要发送较多的ROM类型消息,可自行修改

  • the size of LwIP’s dynamic memory

    LwIP堆空间大小,与数据发送有关,不建议修改

  • the number of raw connection

    RAW 控制块数量,根据需求修改

  • the number of UDP socket

    UDP 控制块数量,根据需求修改

  • the number of TCP socket

    TCP 控制块数量,根据需求修改

  • the number of TCP segment

    TCP等待队列的个数,这个值应该不小于TCP的发送队列的个数

  • the size of send buffer

    TCP发送缓存大小,如无特别需求,请保持默认值

  • the size of TCP send window

    TCP滑动窗口大小,如无特别需求,请保持默认值

  • the priority level value of lwIP thread

    LwIP协议栈任务优先级,为防止网络丢包,建议保持较高优先级

  • the number of mail in the lwIP thread mailbox

    消息邮箱数量,如无特别需求,不建议修改此数量

  • the stack size of lwIP thread

    LwIP协议栈任务的栈空间大小,如没有添加基于RAW接口的应用程序,不建议修改任务栈。 否则,应该加大协议栈任务的栈空间

  • Enable IP reassembly and frag

    使能IP包的分片重装功能

  • Enable lwIP statistics

    使能LwIP统计信息

  • Enable tx hardware generate checksum

    使能发送时自动生成硬件校验和

  • Enable rx hardware check checksum

    使能接收时硬件检查校验和

  • Enable ping features —-

    ping命令支持,使能此选项后,可在终端中使用ping命令

  • Using MQTT protocol

    支持MQTT协议

  • Enable lwIP Debugging Options —-

    LwIP调试信息选项,可根据自身需求进行打开

7.11.2.1.3. pinmux配置

在 target/soc name/board name/pinmux.c 中设置 MAC 端口的 pinmux, 需要根据开发板板的不同进行不同的设置

#ifdef AIC_USING_GMAC0
    /* gmac0 */
    {6, PIN_PULL_DIS, 3, "PE.0"},
    {6, PIN_PULL_DIS, 3, "PE.1"},
    {6, PIN_PULL_DIS, 3, "PE.2"},
    {6, PIN_PULL_DIS, 3, "PE.3"},
    {6, PIN_PULL_DIS, 3, "PE.4"},
    {6, PIN_PULL_DIS, 3, "PE.5"},
    {6, PIN_PULL_DIS, 3, "PE.7"},
    {6, PIN_PULL_DIS, 3, "PE.8"},
    {6, PIN_PULL_DIS, 3, "PE.9"},
    /* phy0 reset gpio */
    {1, PIN_PULL_DIS, 3, "PE.6"},
    /* clk_out2 */
    {6, PIN_PULL_DIS, 3, "PE.10"},
#endif

7.11.2.2. 时钟

MAC 和 PHY 的协同工作共牵涉到四组时钟, zx-rtt已将时钟部分处理完整,不需要用户除 scons --menuconfig 配置MAC使用 external clkinternal clk 之外的其 他任何修改

7.11.2.2.1. MAC 工作时钟

MAC 的内部工作时钟通过 PLL 分频获得,为 50M, 工作时钟不会因为方案的不同而不同,为一固定值 配置代码在 “packages/third-party/lwip/contrib/ports/drv/aic/aic_mac_ll.c” 中。

void aicmac_low_level_init(uint32_t port, bool en)
{
    uint32_t id = CLK_GMAC0 + port;

    /* pin-mux */

    /* mac clock */
    if (en) {
        /* Set clock frequence = 50M */
        hal_clk_set_freq(id, 50000000);
        /* clock enable & reset deassert */
        hal_clk_enable_deassertrst_iter(id);
    } else {
        /* clock enable & reset deassert */
        hal_clk_disable_assertrst(id);
    }
}

7.11.2.2.2. MDC 时钟

MDC 时钟是 mdio 的工作时钟,是 MAC 和 PHY 进行配置的工作时钟,双方在使用初期会有一个协 商,一般是 MAC 通知 PHY,该时钟为 MAC 通过模块的内部工作时钟处理生成,也不会因为方案的 不同而不同,为一固定值。MDIO时钟初始化在 “packages/third-party/lwip/contrib/ports/drv/aic/aicmac.c” 文件的aicmac_init函数中

int aicmac_init(uint32_t port)
{
    ...

    /* MDCIO Internal Clock Select */
    tmpreg = readl(MAC(port, mdioctl));
    tmpreg &= ~(ETH_MDIOCTL_CR_MSK);
    ahbclk = hal_clk_get_freq(CLK_AHB0);
    if ((ahbclk >= 20000000) && (ahbclk < 35000000)) {
        tmpreg |= ETH_MDIOCTL_CR_Div16;
    } else if ((ahbclk >= 35000000) && (ahbclk < 60000000)) {
        tmpreg |= ETH_MDIOCTL_CR_Div26;
    } else if ((ahbclk >= 60000000) && (ahbclk < 100000000)) {
        tmpreg |= ETH_MDIOCTL_CR_Div42;
    } else if ((ahbclk >= 100000000) && (ahbclk < 150000000)) {
        tmpreg |= ETH_MDIOCTL_CR_Div62;
    } else if ((ahbclk >= 150000000) && (ahbclk < 250000000)) {
        tmpreg |= ETH_MDIOCTL_CR_Div102;
    } else /* ((ahbclk >= 250000000)&&(ahbclk <= 300000000)) */
    {
        tmpreg |= ETH_MDIOCTL_CR_Div124;
    }
    writel(tmpreg, MAC(port, mdioctl));
    ...
    ...
}

MDC 时钟配置错误,则 MAC 和 PHY 的通信不通,呈现的现象是 MAC 无法发现 PHY 设备

7.11.2.2.3. PHY 工作时钟

一般 PHY 的工作时钟是 25M, 一般的设计是外挂一个晶振提供时钟给 PHY 模组, AIC不同型号芯片都对外提供几组时钟,也可以使用该时钟供给 PHY 模块工作。

该时钟 (clk-out2) 使能在 “target/soc name/board name/sys_clk.c” 中配置。如果需要可以打开

struct aic_sysclk aic_sysclk_config[] = {
    ...
    {25000000, CLK_OUT2},
    ...
};

void aic_board_sysclk_init(void)
{
    uint32_t i = 0;

    for (i=0; i<sizeof(aic_sysclk_config)/sizeof(struct aic_sysclk); i++) {
        hal_clk_set_freq(aic_sysclk_config[i].clk_id, aic_sysclk_config[i].freq);
    }

    /* Enable sys clk */
    hal_clk_enable_deassertrst_iter(CLK_GPIO);
    hal_clk_enable_deassertrst_iter(CLK_GTC);
#ifdef AIC_USING_GMAC0
    hal_clk_enable_deassertrst_iter(CLK_OUT2);
#endif
}

7.11.2.2.4. MDATA 时钟

MDATA 时钟 为 MAC 和 PHY 进行数据传输的时钟,对于百兆和千兆有不同的使用方式

  • 百兆网络:可以是 MAC 供给 PHY, 也可以是 PHY 供给 MAC

  • 千兆网络:只能是 MAC 供给 PHY,RGMII0-TXCK 端口

代码中会根据 scons --menuconfig 中的配置来决定MAC使用的时钟来源是内部时钟还是外部时钟

static s32 syscfg_gmac_init(u32 ch)
{
    #ifdef AIC_SYSCFG_DRV_V10
    u32 cfg_reg = ch ? SYSCFG_GMAC1_CFG : SYSCFG_GMAC0_CFG;
    #else
    u32 cfg_reg =  SYSCFG_GMAC0_CFG;
    #endif
    s32 cfg;

    cfg = syscfg_readl(cfg_reg);

    if (ch == 0) {
        #ifdef AIC_SYSCFG_DRV_V10
        #ifdef AIC_DEV_GMAC0_RGMII
        cfg |= SYSCFG_GMAC_PHY_RGMII_1000M;
        #else
        cfg &= ~SYSCFG_GMAC_PHY_RGMII_1000M;
        #endif
        #endif
        #ifdef AIC_DEV_GMAC0_PHY_EXTCLK
        cfg |= SYSCFG_GMAC_RMII_EXTCLK_SEL;
        #endif
        #if AIC_DEV_GMAC0_TXDELAY
        cfg |= (AIC_DEV_GMAC0_TXDELAY << SYSCFG_GMAC_TXDLY_SEL_SHIFT);
        #endif
        #if AIC_DEV_GMAC0_RXDELAY
        cfg |= (AIC_DEV_GMAC0_RXDELAY << SYSCFG_GMAC_RXDLY_SEL_SHIFT);
        #endif
    } else if (ch == 1) {
        #ifdef AIC_SYSCFG_DRV_V10
        #ifdef AIC_DEV_GMAC1_RGMII
        cfg |= SYSCFG_GMAC_PHY_RGMII_1000M;
        #else
        cfg &= ~SYSCFG_GMAC_PHY_RGMII_1000M;
        #endif
        #endif
        #ifdef AIC_DEV_GMAC1_PHY_EXTCLK
        cfg |= SYSCFG_GMAC_RMII_EXTCLK_SEL;
        #endif
        #if AIC_DEV_GMAC1_TXDELAY
        cfg |= (AIC_DEV_GMAC1_TXDELAY << SYSCFG_GMAC_TXDLY_SEL_SHIFT);
        #endif
        #if AIC_DEV_GMAC1_RXDELAY
        cfg |= (AIC_DEV_GMAC1_RXDELAY << SYSCFG_GMAC_RXDLY_SEL_SHIFT);
        #endif
    }

    syscfg_writel(cfg, cfg_reg);

    return 0;
}