/** \brief Low level init of the MAC and PHY. * * \param[in] netif Pointer to LWIP netif structure */ static err_t low_level_init(struct netif *netif) { enet_dev_if_t * enetIfPtr; uint32_t device = BOARD_DEBUG_ENET_INSTANCE_ADDR; enet_rxbd_config_t rxbdCfg; enet_txbd_config_t txbdCfg; enet_phy_speed_t phy_speed; enet_phy_duplex_t phy_duplex; k64f_init_eth_hardware(); /* Initialize device*/ enetIfPtr = (enet_dev_if_t *)&enetDevIf[BOARD_DEBUG_ENET_INSTANCE]; enetIfPtr->deviceNumber = device; enetIfPtr->macCfgPtr = &g_enetMacCfg[BOARD_DEBUG_ENET_INSTANCE]; enetIfPtr->phyCfgPtr = &g_enetPhyCfg[BOARD_DEBUG_ENET_INSTANCE]; enetIfPtr->macApiPtr = &g_enetMacApi; enetIfPtr->phyApiPtr = (void *)&g_enetPhyApi; memcpy(enetIfPtr->macCfgPtr->macAddr, (char*)netif->hwaddr, kEnetMacAddrLen); /* Allocate buffer for ENET mac context*/ enetIfPtr->macContextPtr = (enet_mac_context_t *)calloc(1, sizeof(enet_mac_context_t)); if (!enetIfPtr->macContextPtr) { return ERR_BUF; } /* Initialize enet buffers*/ if(k64f_rx_setup(netif, &rxbdCfg) != ERR_OK) { return ERR_BUF; } /* Initialize enet buffers*/ if(k64f_tx_setup(netif, &txbdCfg) != ERR_OK) { return ERR_BUF; } /* Initialize enet module*/ if (enet_mac_init(enetIfPtr, &rxbdCfg, &txbdCfg) == kStatus_ENET_Success) { /* Initialize PHY*/ if (enetIfPtr->macCfgPtr->isPhyAutoDiscover) { if (((enet_phy_api_t *)(enetIfPtr->phyApiPtr))->phy_auto_discover(enetIfPtr) != kStatus_PHY_Success) return ERR_IF; } if (((enet_phy_api_t *)(enetIfPtr->phyApiPtr))->phy_init(enetIfPtr) != kStatus_PHY_Success) return ERR_IF; enetIfPtr->isInitialized = true; } else { // TODOETH: cleanup memory return ERR_IF; } /* Get link information from PHY */ phy_get_link_speed(enetIfPtr, &phy_speed); phy_get_link_duplex(enetIfPtr, &phy_duplex); BW_ENET_RCR_RMII_10T(enetIfPtr->deviceNumber, phy_speed == kEnetSpeed10M ? kEnetCfgSpeed10M : kEnetCfgSpeed100M); BW_ENET_TCR_FDEN(enetIfPtr->deviceNumber, phy_duplex == kEnetFullDuplex ? kEnetCfgFullDuplex : kEnetCfgHalfDuplex); /* Enable Ethernet module*/ enet_hal_config_ethernet(BOARD_DEBUG_ENET_INSTANCE_ADDR, true, true); /* Active Receive buffer descriptor must be done after module enable*/ enet_hal_active_rxbd(enetIfPtr->deviceNumber); return ERR_OK; }
/** \brief Low level init of the MAC and PHY. * * \param[in] netif Pointer to LWIP netif structure */ NyLPC_TBool low_level_init(const unsigned char* i_ethaddr,int i_addr_len) { enet_dev_if_t * enetIfPtr; uint32_t device = BOARD_DEBUG_ENET_INSTANCE; enet_rxbd_config_t rxbdCfg; enet_txbd_config_t txbdCfg; enet_phy_speed_t phy_speed; enet_phy_duplex_t phy_duplex; //RX/TXメモリはデバイス選択時に確保 k64f_init_eth_hardware(); /* Initialize device*/ enetIfPtr = (enet_dev_if_t *)&enetDevIf[device]; enetIfPtr->deviceNumber = device; enetIfPtr->macCfgPtr = &g_enetMacCfg[device]; enetIfPtr->phyCfgPtr = &g_enetPhyCfg[device]; enetIfPtr->macApiPtr = &g_enetMacApi; enetIfPtr->phyApiPtr = (void *)&g_enetPhyApi; //macアドレスのコピー memcpy(enetIfPtr->macCfgPtr->macAddr,(char*)i_ethaddr,i_addr_len); //enetIfPtr->macContextPtrはgetInterface if(ENET_MAC_CONTEXT_BUF!=NULL){ free(ENET_MAC_CONTEXT_BUF); ENET_MAC_CONTEXT_BUF=NULL; } ENET_MAC_CONTEXT_BUF=calloc(1, sizeof(enet_mac_context_t)); if(ENET_MAC_CONTEXT_BUF==NULL){ return NyLPC_TBool_FALSE;//ERR_BUF; } enetIfPtr->macContextPtr = (enet_mac_context_t *)ENET_MAC_CONTEXT_BUF; /* Initialize enet buffers*/ if(!k64f_rx_setup(&rxbdCfg)) { return NyLPC_TBool_FALSE;//ERR_BUF; } /* Initialize enet buffers*/ if(!k64f_tx_setup(&txbdCfg)) { return NyLPC_TBool_FALSE;//ERR_BUF; } /* Initialize enet module*/ if (enet_mac_init(enetIfPtr, &rxbdCfg, &txbdCfg) == kStatus_ENET_Success) { /* Initialize PHY*/ if (enetIfPtr->macCfgPtr->isPhyAutoDiscover) { if (((enet_phy_api_t *)(enetIfPtr->phyApiPtr))->phy_auto_discover(enetIfPtr) != kStatus_PHY_Success) return NyLPC_TBool_FALSE;//ERR_IF; } if (((enet_phy_api_t *)(enetIfPtr->phyApiPtr))->phy_init(enetIfPtr) != kStatus_PHY_Success) return NyLPC_TBool_FALSE;//ERR_IF; enetIfPtr->isInitialized = true; }else{ // TODOETH: cleanup memory return NyLPC_TBool_FALSE;//ERR_IF; } /* Get link information from PHY */ phy_get_link_speed(enetIfPtr, &phy_speed); phy_get_link_duplex(enetIfPtr, &phy_duplex); BW_ENET_RCR_RMII_10T(enetIfPtr->deviceNumber, phy_speed == kEnetSpeed10M ? kEnetCfgSpeed10M : kEnetCfgSpeed100M); BW_ENET_TCR_FDEN(enetIfPtr->deviceNumber, phy_duplex == kEnetFullDuplex ? kEnetCfgFullDuplex : kEnetCfgHalfDuplex); /* Enable Ethernet module*/ enet_hal_config_ethernet(device, true, true); /* Active Receive buffer descriptor must be done after module enable*/ enet_hal_active_rxbd(enetIfPtr->deviceNumber); enet_hal_active_txbd(enetIfPtr->deviceNumber); return NyLPC_TBool_TRUE;//ERR_OK; }
/** \brief Low level init of the MAC and PHY. * * \param[in] netif Pointer to LWIP netif structure */ static err_t low_level_init(struct netif *netif) { struct k64f_enetdata *k64f_enet = netif->state; uint8_t i; uint32_t sysClock; phy_speed_t phy_speed; phy_duplex_t phy_duplex; uint32_t phyAddr = 0; bool link = false; enet_config_t config; // Allocate RX descriptors rx_desc_start_addr = (uint8_t *)calloc(1, sizeof(enet_rx_bd_struct_t) * ENET_RX_RING_LEN + ENET_BUFF_ALIGNMENT); if(!rx_desc_start_addr) return ERR_MEM; // Allocate TX descriptors tx_desc_start_addr = (uint8_t *)calloc(1, sizeof(enet_tx_bd_struct_t) * ENET_TX_RING_LEN + ENET_BUFF_ALIGNMENT); if(!tx_desc_start_addr) return ERR_MEM; rx_desc_start_addr = (uint8_t *)ENET_ALIGN(rx_desc_start_addr, ENET_BUFF_ALIGNMENT); tx_desc_start_addr = (uint8_t *)ENET_ALIGN(tx_desc_start_addr, ENET_BUFF_ALIGNMENT); /* Create buffers for each receive BD */ for (i = 0; i < ENET_RX_RING_LEN; i++) { rx_buff[i] = pbuf_alloc(PBUF_RAW, ENET_ETH_MAX_FLEN + ENET_BUFF_ALIGNMENT, PBUF_RAM); if (NULL == rx_buff[i]) return ERR_MEM; /* K64F note: the next line ensures that the RX buffer is properly aligned for the K64F RX descriptors (16 bytes alignment). However, by doing so, we're effectively changing a data structure which is internal to lwIP. This might not prove to be a good idea in the long run, but a better fix would probably involve modifying lwIP itself */ rx_buff[i]->payload = (void*)ENET_ALIGN((uint32_t)rx_buff[i]->payload, ENET_BUFF_ALIGNMENT); rx_ptr[i] = rx_buff[i]->payload; } k64f_enet->tx_consume_index = k64f_enet->tx_produce_index = 0; /* prepare the buffer configuration. */ enet_buffer_config_t buffCfg = { ENET_RX_RING_LEN, ENET_TX_RING_LEN, ENET_ALIGN(ENET_ETH_MAX_FLEN, ENET_BUFF_ALIGNMENT), 0, (volatile enet_rx_bd_struct_t *)rx_desc_start_addr, (volatile enet_tx_bd_struct_t *)tx_desc_start_addr, (uint8_t *)&rx_ptr, NULL, }; #if (defined(TARGET_K64F) && (defined(TARGET_FRDM))) k64f_init_eth_hardware(); #endif #if (defined(TARGET_K66F) && (defined(TARGET_FRDM))) k66f_init_eth_hardware(); #endif sysClock = CLOCK_GetFreq(kCLOCK_CoreSysClk); ENET_GetDefaultConfig(&config); PHY_Init(ENET, 0, sysClock); PHY_GetLinkStatus(ENET, phyAddr, &link); if (link) { /* Get link information from PHY */ PHY_GetLinkSpeedDuplex(ENET, phyAddr, &phy_speed, &phy_duplex); /* Change the MII speed and duplex for actual link status. */ config.miiSpeed = (enet_mii_speed_t)phy_speed; config.miiDuplex = (enet_mii_duplex_t)phy_duplex; config.interrupt = kENET_RxFrameInterrupt | kENET_TxFrameInterrupt; } config.rxMaxFrameLen = ENET_ETH_MAX_FLEN; config.macSpecialConfig = kENET_ControlFlowControlEnable; config.txAccelerConfig = kENET_TxAccelIsShift16Enabled; config.rxAccelerConfig = kENET_RxAccelisShift16Enabled | kENET_RxAccelMacCheckEnabled; ENET_Init(ENET, &g_handle, &config, &buffCfg, netif->hwaddr, sysClock); ENET_SetCallback(&g_handle, ethernet_callback, netif); ENET_ActiveRead(ENET); return ERR_OK; }