123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244 |
- #include "stm32f4x7_phy.h"
- #include "dwt.h"
- #include "lwip/api.h"
- #include "lwip/sockets.h"
- #include "stm32f4x7_eth.h"
- ETH_InitTypeDef ETH_InitStructure;
- __IO uint32_t EthStatus = 0;
- extern struct netif gnetif;
- static void phy_gpio_config(void);
- static void phy_mac_dma_config(void);
- static void nvic_configuration(void);
- void ETH_Delay10ms(uint32_t nCount)
- {
-
- us_delay(10 * nCount);
- }
- void eth_init(void)
- {
- nvic_configuration();
-
- phy_gpio_config();
-
- phy_mac_dma_config();
-
- if (ETH_ReadPHYRegister(ETHERNET_PHY_ADDRESS, PHY_SR) & 1)
- {
- EthStatus |= ETH_LINK_FLAG;
- }
-
-
- ETH_DMAITConfig(ETH_DMA_IT_R | ETH_DMA_IT_NIS, ENABLE);
- }
- static void phy_mac_dma_config(void)
- {
-
- ETH_DeInit();
-
- ETH_SoftwareReset();
-
- while (ETH_GetSoftwareResetStatus() == SET)
- ;
-
-
- ETH_StructInit(Ð_InitStructure);
-
-
-
- ETH_InitStructure.ETH_AutoNegotiation = ETH_AutoNegotiation_Enable;
-
-
-
-
- ETH_InitStructure.ETH_LoopbackMode = ETH_LoopbackMode_Disable;
-
- ETH_InitStructure.ETH_RetryTransmission = ETH_RetryTransmission_Disable;
-
- ETH_InitStructure.ETH_AutomaticPadCRCStrip =
- ETH_AutomaticPadCRCStrip_Disable;
-
- ETH_InitStructure.ETH_ReceiveAll = ETH_ReceiveAll_Disable;
-
- ETH_InitStructure.ETH_BroadcastFramesReception =
- ETH_BroadcastFramesReception_Enable;
-
- ETH_InitStructure.ETH_PromiscuousMode = ETH_PromiscuousMode_Disable;
-
- ETH_InitStructure.ETH_MulticastFramesFilter =
- ETH_MulticastFramesFilter_Perfect;
-
- ETH_InitStructure.ETH_UnicastFramesFilter =
- ETH_UnicastFramesFilter_Perfect;
- #ifdef CHECKSUM_BY_HARDWARE
-
- ETH_InitStructure.ETH_ChecksumOffload = ETH_ChecksumOffload_Enable;
- #endif
-
-
-
- ETH_InitStructure.ETH_DropTCPIPChecksumErrorFrame =
- ETH_DropTCPIPChecksumErrorFrame_Enable;
-
- ETH_InitStructure.ETH_ReceiveStoreForward =
- ETH_ReceiveStoreForward_Enable;
-
- ETH_InitStructure.ETH_TransmitStoreForward =
- ETH_TransmitStoreForward_Enable;
-
- ETH_InitStructure.ETH_ForwardErrorFrames =
- ETH_ForwardErrorFrames_Disable;
-
- ETH_InitStructure.ETH_ForwardUndersizedGoodFrames =
- ETH_ForwardUndersizedGoodFrames_Disable;
-
- ETH_InitStructure.ETH_SecondFrameOperate =
- ETH_SecondFrameOperate_Enable;
-
- ETH_InitStructure.ETH_AddressAlignedBeats =
- ETH_AddressAlignedBeats_Enable;
-
- ETH_InitStructure.ETH_FixedBurst = ETH_FixedBurst_Enable;
-
- ETH_InitStructure.ETH_RxDMABurstLength = ETH_RxDMABurstLength_32Beat;
-
- ETH_InitStructure.ETH_TxDMABurstLength = ETH_TxDMABurstLength_32Beat;
- ETH_InitStructure.ETH_DMAArbitration =
- ETH_DMAArbitration_RoundRobin_RxTx_2_1;
-
- EthStatus = ETH_Init(Ð_InitStructure, ETHERNET_PHY_ADDRESS);
- }
- static void nvic_configuration(void)
- {
- NVIC_InitTypeDef NVIC_InitStructure;
- NVIC_InitStructure.NVIC_IRQChannel = ETH_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&NVIC_InitStructure);
- }
- static void phy_gpio_config(void)
- {
-
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);
- #ifdef MII_MODE
- #ifdef PHY_CLOCK_MCO
-
- RCC_MCO1Config(RCC_MCO1Source_HSE, RCC_MCO1Div_1);
- #endif
- SYSCFG_ETH_MediaInterfaceConfig(SYSCFG_ETH_MediaInterface_MII);
- #elif defined RMII_MODE
- SYSCFG_ETH_MediaInterfaceConfig(SYSCFG_ETH_MediaInterface_RMII);
- #endif
- }
- void ETH_CheckLinkStatus(uint16_t PHYAddress)
- {
- static uint8_t status = 0;
- uint32_t t = ETH_ReadPHYRegister(ETHERNET_PHY_ADDRESS, PHY_SR) & 1;
-
- if (t && !status)
- {
-
- netif_set_link_up(&gnetif);
- status = 1;
- }
-
- if (!t && status)
- {
-
-
- netif_set_link_down(&gnetif);
- status = 0;
- }
- }
- uint32_t Eth_Link_PHYITConfig(uint16_t PHYAddress)
- {
- uint16_t tmpreg = 0;
-
- tmpreg = ETH_ReadPHYRegister(PHYAddress, PHY_MICR);
-
- tmpreg |= (uint16_t)(PHY_MICR_INT_EN | PHY_MICR_INT_OE);
- if (!(ETH_WritePHYRegister(PHYAddress, PHY_MICR, tmpreg)))
- {
-
- return ETH_ERROR;
- }
-
- tmpreg = ETH_ReadPHYRegister(PHYAddress, PHY_MISR);
-
- tmpreg |= (uint16_t)PHY_MISR_LINK_INT_EN;
- if (!(ETH_WritePHYRegister(PHYAddress, PHY_MISR, tmpreg)))
- {
-
- return ETH_ERROR;
- }
-
- return ETH_SUCCESS;
- }
|