Advertisement
teplofizik

stm32f4x7_eth_bsp_v2.c

Jun 8th, 2016
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.84 KB | None | 0 0
  1. /* Includes ------------------------------------------------------------------*/
  2. #include "stm32f4x7_eth.h"
  3. #include "stm32f4x7_eth_bsp.h"
  4. #include "stm32f4xx_rcc.h"
  5. #include "stm32f4xx_syscfg.h"
  6.  
  7. #include "../drivers.h"
  8.  
  9. /* Private typedef -----------------------------------------------------------*/
  10. typedef enum
  11. {
  12.     NotLinked,
  13.     Autonegotiation,
  14.     Linked
  15. } TLinkState;
  16.  
  17. /* Private define ------------------------------------------------------------*/
  18. static const int GPIO_AF_ETH = 11;
  19.  
  20. /* Private macro -------------------------------------------------------------*/
  21. /* Private variables ---------------------------------------------------------*/
  22. __IO uint32_t  EthInitStatus = 0;
  23. __IO uint8_t EthLinkStatus = 0;
  24. TLinkState LinkedStatus = NotLinked;
  25.  
  26.  
  27. static const TPin ETH_RST_PIN      = { PD, 2 };
  28.    
  29. static const TPin ETH_MDIO         = { PA, 2 };
  30. static const TPin ETH_MDC          = { PC, 1 };
  31.    
  32. static const TPin ETH_RMII_REF_CLK = { PA, 1 };
  33.  
  34. static const TPin ETH_RMII_CRS_DV  = { PA, 7 };
  35. static const TPin ETH_RMII_RXD0    = { PC, 4 };
  36. static const TPin ETH_RMII_RXD1    = { PC, 5 };
  37. static const TPin ETH_RMII_TX_EN   = { PG, 11 };
  38. static const TPin ETH_RMII_TXD0    = { PG, 13 };
  39. static const TPin ETH_RMII_TXD1    = { PB, 13 };
  40.  
  41. /* Private function prototypes -----------------------------------------------*/
  42. static void ETH_GPIO_Config(void);
  43. static void ETH_MACDMA_Config(void);
  44.  
  45. /* Private functions ---------------------------------------------------------*/
  46.  
  47. ETH_InitTypeDef ETH_InitStructure;
  48. int NegotiationDelay = 0;
  49.  
  50. /**
  51.   * @brief  ETH_BSP_Config
  52.   * @param  None
  53.   * @retval None
  54.   */
  55. void ETH_BSP_Config(void)
  56. {
  57.     /* Configure the GPIO ports for ethernet pins */
  58.     ETH_GPIO_Config();
  59.  
  60.     /* Configure the Ethernet MAC/DMA */
  61.     ETH_MACDMA_Config();
  62. }
  63. void ETH_BSP_Monitor(void)
  64. {
  65.     switch(LinkedStatus)
  66.     {
  67.     case NotLinked:
  68.         if(ETH_HasLink(LAN8720_PHY_ADDRESS))
  69.         {
  70.             ETH_RestartAutoneg(LAN8720_PHY_ADDRESS);
  71.             LinkedStatus = Autonegotiation;
  72.             NegotiationDelay = 40;
  73.         }
  74.         break;
  75.     case Autonegotiation:
  76.         if(ETH_AutoNegCompleted(LAN8720_PHY_ADDRESS))
  77.         {
  78.             ETH_AutonegotiationCompleted(&ETH_InitStructure, LAN8720_PHY_ADDRESS);
  79.             LinkedStatus = Linked;
  80.         }
  81.         else if(!ETH_HasLink(LAN8720_PHY_ADDRESS))
  82.         {
  83.             if(NegotiationDelay)
  84.                 NegotiationDelay--;
  85.             else
  86.                 LinkedStatus = NotLinked;
  87.         }
  88.         break;
  89.     case Linked:
  90.         if(!ETH_HasLink(LAN8720_PHY_ADDRESS))
  91.             LinkedStatus = NotLinked;
  92.         break;
  93.     }
  94. }
  95.  
  96. bool ETH_BSP_HasLink(void)
  97. {
  98.     return LinkedStatus == Linked;
  99. }
  100.  
  101. bool ETH_BSP_IsAutoNeg(void)
  102. {
  103.     return LinkedStatus == Autonegotiation;
  104. }
  105.  
  106. /**
  107.   * @brief  Configures the Ethernet Interface
  108.   * @param  None
  109.   * @retval None
  110.   */
  111. static void ETH_MACDMA_Config(void)
  112. {
  113.     /* Enable ETHERNET clock  */
  114.     RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_ETH_MAC | RCC_AHB1Periph_ETH_MAC_Tx |
  115.                            RCC_AHB1Periph_ETH_MAC_Rx, ENABLE);
  116.  
  117.     /* Reset ETHERNET on AHB Bus */
  118.     ETH_DeInit();
  119.  
  120.     /* Software reset */
  121.     ETH_SoftwareReset();
  122.  
  123.     /* Wait for software reset */
  124.     while (ETH_GetSoftwareResetStatus() == SET);
  125.  
  126.     /* ETHERNET Configuration --------------------------------------------------*/
  127.     /* Call ETH_StructInit if you don't like to configure all ETH_InitStructure parameter */
  128.     ETH_StructInit(&ETH_InitStructure);
  129.  
  130.     /* Fill ETH_InitStructure parametrs */
  131.     /*------------------------   MAC   -----------------------------------*/
  132.     ETH_InitStructure.ETH_AutoNegotiation = ETH_AutoNegotiation_Enable;
  133.     //ETH_InitStructure.ETH_AutoNegotiation = ETH_AutoNegotiation_Disable;
  134.     //  ETH_InitStructure.ETH_Speed = ETH_Speed_10M;
  135.     //  ETH_InitStructure.ETH_Mode = ETH_Mode_HalfDuplex;  
  136.  
  137.     ETH_InitStructure.ETH_LoopbackMode = ETH_LoopbackMode_Disable;
  138.     ETH_InitStructure.ETH_RetryTransmission = ETH_RetryTransmission_Disable;
  139.     ETH_InitStructure.ETH_AutomaticPadCRCStrip = ETH_AutomaticPadCRCStrip_Disable;
  140.     ETH_InitStructure.ETH_ReceiveAll = ETH_ReceiveAll_Disable;
  141.     ETH_InitStructure.ETH_BroadcastFramesReception = ETH_BroadcastFramesReception_Enable;
  142.     ETH_InitStructure.ETH_PromiscuousMode = ETH_PromiscuousMode_Disable;
  143.     ETH_InitStructure.ETH_MulticastFramesFilter = ETH_MulticastFramesFilter_None;
  144.     ETH_InitStructure.ETH_UnicastFramesFilter = ETH_UnicastFramesFilter_Perfect;
  145. #ifdef CHECKSUM_BY_HARDWARE
  146.     ETH_InitStructure.ETH_ChecksumOffload = ETH_ChecksumOffload_Enable;
  147. #endif
  148.  
  149.     /*------------------------   DMA   -----------------------------------*/  
  150.  
  151.     /* When we use the Checksum offload feature, we need to enable the Store and Forward mode:
  152.     the store and forward guarantee that a whole frame is stored in the FIFO, so the MAC can insert/verify the checksum,
  153.     if the checksum is OK the DMA can handle the frame otherwise the frame is dropped */
  154.     ETH_InitStructure.ETH_DropTCPIPChecksumErrorFrame = ETH_DropTCPIPChecksumErrorFrame_Enable;
  155.     ETH_InitStructure.ETH_ReceiveStoreForward = ETH_ReceiveStoreForward_Enable;        
  156.     ETH_InitStructure.ETH_TransmitStoreForward = ETH_TransmitStoreForward_Enable;    
  157.  
  158.     ETH_InitStructure.ETH_ForwardErrorFrames = ETH_ForwardErrorFrames_Disable;      
  159.     ETH_InitStructure.ETH_ForwardUndersizedGoodFrames = ETH_ForwardUndersizedGoodFrames_Disable;  
  160.     ETH_InitStructure.ETH_SecondFrameOperate = ETH_SecondFrameOperate_Enable;
  161.     ETH_InitStructure.ETH_AddressAlignedBeats = ETH_AddressAlignedBeats_Enable;      
  162.     ETH_InitStructure.ETH_FixedBurst = ETH_FixedBurst_Enable;                
  163.     ETH_InitStructure.ETH_RxDMABurstLength = ETH_RxDMABurstLength_32Beat;          
  164.     ETH_InitStructure.ETH_TxDMABurstLength = ETH_TxDMABurstLength_32Beat;
  165.     ETH_InitStructure.ETH_DMAArbitration = ETH_DMAArbitration_RoundRobin_RxTx_2_1;
  166.  
  167.     /* Configure Ethernet */
  168.     if((EthInitStatus = ETH_Init(&ETH_InitStructure, LAN8720_PHY_ADDRESS)) == ETH_ERROR)
  169.         LinkedStatus = Linked;
  170.     else
  171.         LinkedStatus = NotLinked;
  172.    
  173.     // Слежение за сетью
  174.     timer_AddFunction(10, &ETH_BSP_Monitor);
  175. }
  176.  
  177. /**
  178.   * @brief  Configures the different GPIO ports.
  179.   * @param  None
  180.   * @retval None
  181.   */
  182. void ETH_GPIO_Config(void)
  183. {
  184.     volatile uint32_t i;
  185.    
  186.     /* Enable SYSCFG clock */
  187.     RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);  
  188.  
  189.     /* MII/RMII Media interface selection --------------------------------------*/
  190.     SYSCFG_ETH_MediaInterfaceConfig(SYSCFG_ETH_MediaInterface_RMII);
  191.  
  192.     while(GPIOA->IDR & (1 << 1)) {}
  193.     while(!(GPIOA->IDR & (1 << 1))) {}
  194.    
  195.     gp_FastSpeed(&ETH_RMII_REF_CLK);
  196.     gp_FastSpeed(&ETH_MDIO);
  197.     gp_FastSpeed(&ETH_RMII_CRS_DV);
  198.  
  199.   //  gp_FastSpeed(&ETH_MII_RX_ER);
  200.     gp_FastSpeed(&ETH_RMII_TX_EN);
  201.     gp_FastSpeed(&ETH_RMII_TXD0);
  202.     gp_FastSpeed(&ETH_RMII_TXD1);
  203.  
  204.     gp_FastSpeed(&ETH_MDC);
  205.     gp_FastSpeed(&ETH_RMII_RXD0);
  206.     gp_FastSpeed(&ETH_RMII_RXD1);
  207.  
  208.     gp_AlternateFunction(&ETH_RMII_REF_CLK, GPIO_AF_ETH);
  209.     gp_AlternateFunction(&ETH_MDIO, GPIO_AF_ETH);
  210.     gp_AlternateFunction(&ETH_RMII_CRS_DV, GPIO_AF_ETH);
  211.  
  212.  //   gpio_SetAlternateFunction(&ETH_MII_RX_ER, GPIO_AF_ETH);
  213.     gp_AlternateFunction(&ETH_RMII_TX_EN, GPIO_AF_ETH);
  214.     gp_AlternateFunction(&ETH_RMII_TXD0, GPIO_AF_ETH);
  215.     gp_AlternateFunction(&ETH_RMII_TXD1, GPIO_AF_ETH);
  216.  
  217.     gp_AlternateFunction(&ETH_MDC, GPIO_AF_ETH);
  218.     gp_AlternateFunction(&ETH_RMII_RXD0, GPIO_AF_ETH);
  219.     gp_AlternateFunction(&ETH_RMII_RXD1, GPIO_AF_ETH);
  220.    
  221.     gp_Output(&ETH_RST_PIN);
  222.     gp_FastSpeed(&ETH_RST_PIN);
  223.    
  224.     gp_High(&ETH_RST_PIN);
  225.     for (i = 0; i < 20000; i++);
  226.     gp_Low(&ETH_RST_PIN);
  227.     for (i = 0; i < 20000; i++);
  228. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement