Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Includes ------------------------------------------------------------------*/
- #include "stm32f4x7_eth.h"
- #include "stm32f4x7_eth_bsp.h"
- #include "stm32f4xx_rcc.h"
- #include "stm32f4xx_syscfg.h"
- #include "../drivers.h"
- /* Private typedef -----------------------------------------------------------*/
- typedef enum
- {
- NotLinked,
- Autonegotiation,
- Linked
- } TLinkState;
- /* Private define ------------------------------------------------------------*/
- static const int GPIO_AF_ETH = 11;
- /* Private macro -------------------------------------------------------------*/
- /* Private variables ---------------------------------------------------------*/
- __IO uint32_t EthInitStatus = 0;
- __IO uint8_t EthLinkStatus = 0;
- TLinkState LinkedStatus = NotLinked;
- static const TPin ETH_RST_PIN = { PD, 2 };
- static const TPin ETH_MDIO = { PA, 2 };
- static const TPin ETH_MDC = { PC, 1 };
- static const TPin ETH_RMII_REF_CLK = { PA, 1 };
- static const TPin ETH_RMII_CRS_DV = { PA, 7 };
- static const TPin ETH_RMII_RXD0 = { PC, 4 };
- static const TPin ETH_RMII_RXD1 = { PC, 5 };
- static const TPin ETH_RMII_TX_EN = { PG, 11 };
- static const TPin ETH_RMII_TXD0 = { PG, 13 };
- static const TPin ETH_RMII_TXD1 = { PB, 13 };
- /* Private function prototypes -----------------------------------------------*/
- static void ETH_GPIO_Config(void);
- static void ETH_MACDMA_Config(void);
- /* Private functions ---------------------------------------------------------*/
- ETH_InitTypeDef ETH_InitStructure;
- int NegotiationDelay = 0;
- /**
- * @brief ETH_BSP_Config
- * @param None
- * @retval None
- */
- void ETH_BSP_Config(void)
- {
- /* Configure the GPIO ports for ethernet pins */
- ETH_GPIO_Config();
- /* Configure the Ethernet MAC/DMA */
- ETH_MACDMA_Config();
- }
- void ETH_BSP_Monitor(void)
- {
- switch(LinkedStatus)
- {
- case NotLinked:
- if(ETH_HasLink(LAN8720_PHY_ADDRESS))
- {
- ETH_RestartAutoneg(LAN8720_PHY_ADDRESS);
- LinkedStatus = Autonegotiation;
- NegotiationDelay = 40;
- }
- break;
- case Autonegotiation:
- if(ETH_AutoNegCompleted(LAN8720_PHY_ADDRESS))
- {
- ETH_AutonegotiationCompleted(Ð_InitStructure, LAN8720_PHY_ADDRESS);
- LinkedStatus = Linked;
- }
- else if(!ETH_HasLink(LAN8720_PHY_ADDRESS))
- {
- if(NegotiationDelay)
- NegotiationDelay--;
- else
- LinkedStatus = NotLinked;
- }
- break;
- case Linked:
- if(!ETH_HasLink(LAN8720_PHY_ADDRESS))
- LinkedStatus = NotLinked;
- break;
- }
- }
- bool ETH_BSP_HasLink(void)
- {
- return LinkedStatus == Linked;
- }
- bool ETH_BSP_IsAutoNeg(void)
- {
- return LinkedStatus == Autonegotiation;
- }
- /**
- * @brief Configures the Ethernet Interface
- * @param None
- * @retval None
- */
- static void ETH_MACDMA_Config(void)
- {
- /* Enable ETHERNET clock */
- RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_ETH_MAC | RCC_AHB1Periph_ETH_MAC_Tx |
- RCC_AHB1Periph_ETH_MAC_Rx, ENABLE);
- /* Reset ETHERNET on AHB Bus */
- ETH_DeInit();
- /* Software reset */
- ETH_SoftwareReset();
- /* Wait for software reset */
- while (ETH_GetSoftwareResetStatus() == SET);
- /* ETHERNET Configuration --------------------------------------------------*/
- /* Call ETH_StructInit if you don't like to configure all ETH_InitStructure parameter */
- ETH_StructInit(Ð_InitStructure);
- /* Fill ETH_InitStructure parametrs */
- /*------------------------ MAC -----------------------------------*/
- ETH_InitStructure.ETH_AutoNegotiation = ETH_AutoNegotiation_Enable;
- //ETH_InitStructure.ETH_AutoNegotiation = ETH_AutoNegotiation_Disable;
- // ETH_InitStructure.ETH_Speed = ETH_Speed_10M;
- // ETH_InitStructure.ETH_Mode = ETH_Mode_HalfDuplex;
- 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_None;
- ETH_InitStructure.ETH_UnicastFramesFilter = ETH_UnicastFramesFilter_Perfect;
- #ifdef CHECKSUM_BY_HARDWARE
- ETH_InitStructure.ETH_ChecksumOffload = ETH_ChecksumOffload_Enable;
- #endif
- /*------------------------ DMA -----------------------------------*/
- /* When we use the Checksum offload feature, we need to enable the Store and Forward mode:
- the store and forward guarantee that a whole frame is stored in the FIFO, so the MAC can insert/verify the checksum,
- if the checksum is OK the DMA can handle the frame otherwise the frame is dropped */
- 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;
- /* Configure Ethernet */
- if((EthInitStatus = ETH_Init(Ð_InitStructure, LAN8720_PHY_ADDRESS)) == ETH_ERROR)
- LinkedStatus = Linked;
- else
- LinkedStatus = NotLinked;
- // Слежение за сетью
- timer_AddFunction(10, Ð_BSP_Monitor);
- }
- /**
- * @brief Configures the different GPIO ports.
- * @param None
- * @retval None
- */
- void ETH_GPIO_Config(void)
- {
- volatile uint32_t i;
- /* Enable SYSCFG clock */
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);
- /* MII/RMII Media interface selection --------------------------------------*/
- SYSCFG_ETH_MediaInterfaceConfig(SYSCFG_ETH_MediaInterface_RMII);
- while(GPIOA->IDR & (1 << 1)) {}
- while(!(GPIOA->IDR & (1 << 1))) {}
- gp_FastSpeed(Ð_RMII_REF_CLK);
- gp_FastSpeed(Ð_MDIO);
- gp_FastSpeed(Ð_RMII_CRS_DV);
- // gp_FastSpeed(Ð_MII_RX_ER);
- gp_FastSpeed(Ð_RMII_TX_EN);
- gp_FastSpeed(Ð_RMII_TXD0);
- gp_FastSpeed(Ð_RMII_TXD1);
- gp_FastSpeed(Ð_MDC);
- gp_FastSpeed(Ð_RMII_RXD0);
- gp_FastSpeed(Ð_RMII_RXD1);
- gp_AlternateFunction(Ð_RMII_REF_CLK, GPIO_AF_ETH);
- gp_AlternateFunction(Ð_MDIO, GPIO_AF_ETH);
- gp_AlternateFunction(Ð_RMII_CRS_DV, GPIO_AF_ETH);
- // gpio_SetAlternateFunction(Ð_MII_RX_ER, GPIO_AF_ETH);
- gp_AlternateFunction(Ð_RMII_TX_EN, GPIO_AF_ETH);
- gp_AlternateFunction(Ð_RMII_TXD0, GPIO_AF_ETH);
- gp_AlternateFunction(Ð_RMII_TXD1, GPIO_AF_ETH);
- gp_AlternateFunction(Ð_MDC, GPIO_AF_ETH);
- gp_AlternateFunction(Ð_RMII_RXD0, GPIO_AF_ETH);
- gp_AlternateFunction(Ð_RMII_RXD1, GPIO_AF_ETH);
- gp_Output(Ð_RST_PIN);
- gp_FastSpeed(Ð_RST_PIN);
- gp_High(Ð_RST_PIN);
- for (i = 0; i < 20000; i++);
- gp_Low(Ð_RST_PIN);
- for (i = 0; i < 20000; i++);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement