stm32f4x7_eth_bsp.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /**
  2. ******************************************************************************
  3. * @file stm32f4x7_eth_bsp.c
  4. * @author MCD Application Team
  5. * @version V1.0.0
  6. * @date 31-October-2011
  7. * @brief STM32F4x7 Ethernet hardware configuration.
  8. ******************************************************************************
  9. * @attention
  10. *
  11. * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  12. * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
  13. * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
  14. * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
  15. * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
  16. * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  17. *
  18. * <h2><center>&copy; Portions COPYRIGHT 2011 STMicroelectronics</center></h2>
  19. ******************************************************************************
  20. */
  21. /**
  22. ******************************************************************************
  23. * <h2><center>&copy; Portions COPYRIGHT 2012 Embest Tech. Co., Ltd.</center></h2>
  24. * @file stm32f4x7_eth_bsp.c
  25. * @author CMP Team
  26. * @version V1.0.0
  27. * @date 28-December-2012
  28. * @brief STM32F4x7 Ethernet hardware configuration.
  29. * Modified to support the STM32F4DISCOVERY, STM32F4DIS-BB and
  30. * STM32F4DIS-LCD modules.
  31. ******************************************************************************
  32. * @attention
  33. *
  34. * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  35. * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
  36. * TIME. AS A RESULT, Embest SHALL NOT BE HELD LIABLE FOR ANY DIRECT, INDIRECT
  37. * OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE CONTENT
  38. * OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING INFORMATION
  39. * CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  40. ******************************************************************************
  41. */
  42. /* Includes ------------------------------------------------------------------*/
  43. #include "stm32f4x7_eth.h"
  44. #include "stm32f4x7_eth_bsp.h"
  45. #include "main.h"
  46. /* Private typedef -----------------------------------------------------------*/
  47. /* Private define ------------------------------------------------------------*/
  48. /* Private macro -------------------------------------------------------------*/
  49. /* Private variables ---------------------------------------------------------*/
  50. __IO uint32_t EthInitStatus = 0;
  51. /* Private function prototypes -----------------------------------------------*/
  52. static void ETH_GPIO_Config(void);
  53. static void ETH_MACDMA_Config(void);
  54. /* Private functions ---------------------------------------------------------*/
  55. /**
  56. * @brief ETH_BSP_Config
  57. * @param None
  58. * @retval None
  59. */
  60. void ETH_BSP_Config(void)
  61. {
  62. /* Configure the GPIO ports for ethernet pins */
  63. ETH_GPIO_Config();
  64. /* Config NVIC for Ethernet */
  65. //ETH_NVIC_Config();
  66. /* Configure the Ethernet MAC/DMA */
  67. ETH_MACDMA_Config();
  68. if (EthInitStatus == 0) {
  69. // LCD_SetTextColor(LCD_COLOR_RED);
  70. // LCD_DisplayStringLine(Line5, (uint8_t*)" Ethernet Init ");
  71. // LCD_DisplayStringLine(Line6, (uint8_t*)" failed ");
  72. // STM_EVAL_LEDOn(LED5);
  73. while(1);
  74. }
  75. }
  76. /**
  77. * @brief Configures the Ethernet Interface
  78. * @param None
  79. * @retval None
  80. */
  81. static void ETH_MACDMA_Config(void)
  82. {
  83. ETH_InitTypeDef ETH_InitStructure;
  84. /* Enable ETHERNET clock */
  85. RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_ETH_MAC | RCC_AHB1Periph_ETH_MAC_Tx |
  86. RCC_AHB1Periph_ETH_MAC_Rx, ENABLE);
  87. /* Reset ETHERNET on AHB Bus */
  88. ETH_DeInit();
  89. /* Software reset */
  90. ETH_SoftwareReset();
  91. /* Wait for software reset */
  92. while (ETH_GetSoftwareResetStatus() == SET);
  93. /* ETHERNET Configuration --------------------------------------------------*/
  94. /* Call ETH_StructInit if you don't like to configure all ETH_InitStructure parameter */
  95. ETH_StructInit(&ETH_InitStructure);
  96. /* Fill ETH_InitStructure parametrs */
  97. /*------------------------ MAC -----------------------------------*/
  98. ETH_InitStructure.ETH_AutoNegotiation = ETH_AutoNegotiation_Enable;
  99. //ETH_InitStructure.ETH_AutoNegotiation = ETH_AutoNegotiation_Disable;
  100. // ETH_InitStructure.ETH_Speed = ETH_Speed_10M;
  101. // ETH_InitStructure.ETH_Mode = ETH_Mode_FullDuplex;
  102. ETH_InitStructure.ETH_LoopbackMode = ETH_LoopbackMode_Disable;
  103. ETH_InitStructure.ETH_RetryTransmission = ETH_RetryTransmission_Disable;
  104. ETH_InitStructure.ETH_AutomaticPadCRCStrip = ETH_AutomaticPadCRCStrip_Disable;
  105. ETH_InitStructure.ETH_ReceiveAll = ETH_ReceiveAll_Disable;
  106. ETH_InitStructure.ETH_BroadcastFramesReception = ETH_BroadcastFramesReception_Enable;
  107. ETH_InitStructure.ETH_PromiscuousMode = ETH_PromiscuousMode_Disable;
  108. ETH_InitStructure.ETH_MulticastFramesFilter = ETH_MulticastFramesFilter_Perfect;
  109. ETH_InitStructure.ETH_UnicastFramesFilter = ETH_UnicastFramesFilter_Perfect;
  110. #ifdef CHECKSUM_BY_HARDWARE
  111. ETH_InitStructure.ETH_ChecksumOffload = ETH_ChecksumOffload_Enable;
  112. #endif
  113. /*------------------------ DMA -----------------------------------*/
  114. /* When we use the Checksum offload feature, we need to enable the Store and Forward mode:
  115. the store and forward guarantee that a whole frame is stored in the FIFO, so the MAC can insert/verify the checksum,
  116. if the checksum is OK the DMA can handle the frame otherwise the frame is dropped */
  117. ETH_InitStructure.ETH_DropTCPIPChecksumErrorFrame = ETH_DropTCPIPChecksumErrorFrame_Enable;
  118. ETH_InitStructure.ETH_ReceiveStoreForward = ETH_ReceiveStoreForward_Enable;
  119. ETH_InitStructure.ETH_TransmitStoreForward = ETH_TransmitStoreForward_Enable;
  120. ETH_InitStructure.ETH_ForwardErrorFrames = ETH_ForwardErrorFrames_Disable;
  121. ETH_InitStructure.ETH_ForwardUndersizedGoodFrames = ETH_ForwardUndersizedGoodFrames_Disable;
  122. ETH_InitStructure.ETH_SecondFrameOperate = ETH_SecondFrameOperate_Enable;
  123. ETH_InitStructure.ETH_AddressAlignedBeats = ETH_AddressAlignedBeats_Enable;
  124. ETH_InitStructure.ETH_FixedBurst = ETH_FixedBurst_Enable;
  125. ETH_InitStructure.ETH_RxDMABurstLength = ETH_RxDMABurstLength_32Beat;
  126. ETH_InitStructure.ETH_TxDMABurstLength = ETH_TxDMABurstLength_32Beat;
  127. ETH_InitStructure.ETH_DMAArbitration = ETH_DMAArbitration_RoundRobin_RxTx_2_1;
  128. /* Configure Ethernet */
  129. EthInitStatus = ETH_Init(&ETH_InitStructure, LAN8720_PHY_ADDRESS);
  130. /* Enable the Ethernet Rx Interrupt */
  131. ETH_DMAITConfig(ETH_DMA_IT_NIS | ETH_DMA_IT_R, ENABLE);
  132. }
  133. /**
  134. * @brief Configures the different GPIO ports.
  135. * @param None
  136. * @retval None
  137. */
  138. void ETH_GPIO_Config(void)
  139. {
  140. volatile uint32_t i;
  141. GPIO_InitTypeDef GPIO_InitStructure;
  142. /* Enable GPIOs clocks */
  143. RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA | RCC_AHB1Periph_GPIOB
  144. | RCC_AHB1Periph_GPIOC, ENABLE);
  145. /* Enable SYSCFG clock */
  146. RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);
  147. /* MII/RMII Media interface selection --------------------------------------*/
  148. SYSCFG_ETH_MediaInterfaceConfig(SYSCFG_ETH_MediaInterface_RMII);
  149. /* Ethernet pins configuration ************************************************/
  150. /*
  151. ETH_MDIO --------------> PA2 +
  152. ETH_MDC ---------------> PC1 +
  153. ETH_RMII_REF_CLK-------> PA1 +
  154. ETH_RMII_CRS_DV -------> PA7 +
  155. ETH_MII_RX_ER -------> PB10 -
  156. ETH_RMII_RXD0 -------> PC4 +
  157. ETH_RMII_RXD1 -------> PC5 +
  158. ETH_RMII_TX_EN -------> PB11 +
  159. ETH_RMII_TXD0 -------> PB12 +
  160. ETH_RMII_TXD1 -------> PB13 +
  161. ETH_RST_PIN -------> PE2 - замена на PE13
  162. */
  163. #ifdef HARDWARE_BT6711
  164. #define ETH_RST_PIN GPIO_Pin_7
  165. #else
  166. #define ETH_RST_PIN GPIO_Pin_13
  167. #endif
  168. /* Configure PA1,PA2 and PA7 */
  169. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_7;
  170. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
  171. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  172. GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  173. GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
  174. GPIO_Init(GPIOA, &GPIO_InitStructure);
  175. GPIO_PinAFConfig(GPIOA, GPIO_PinSource1, GPIO_AF_ETH);
  176. GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_ETH);
  177. GPIO_PinAFConfig(GPIOA, GPIO_PinSource7, GPIO_AF_ETH);
  178. /* Configure PB10,PB11,PB12 and PB13 */
  179. GPIO_InitStructure.GPIO_Pin = /* GPIO_Pin_10 | */ GPIO_Pin_11 | GPIO_Pin_12 | GPIO_Pin_13;
  180. GPIO_Init(GPIOB, &GPIO_InitStructure);
  181. //GPIO_PinAFConfig(GPIOB, GPIO_PinSource10, GPIO_AF_ETH);
  182. GPIO_PinAFConfig(GPIOB, GPIO_PinSource11, GPIO_AF_ETH);
  183. GPIO_PinAFConfig(GPIOB, GPIO_PinSource12, GPIO_AF_ETH);
  184. GPIO_PinAFConfig(GPIOB, GPIO_PinSource13, GPIO_AF_ETH);
  185. /* Configure PC1, PC4 and PC5 */
  186. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1 | GPIO_Pin_4 | GPIO_Pin_5;
  187. GPIO_Init(GPIOC, &GPIO_InitStructure);
  188. GPIO_PinAFConfig(GPIOC, GPIO_PinSource1, GPIO_AF_ETH);
  189. GPIO_PinAFConfig(GPIOC, GPIO_PinSource4, GPIO_AF_ETH);
  190. GPIO_PinAFConfig(GPIOC, GPIO_PinSource5, GPIO_AF_ETH);
  191. /* Configure the PHY RST pin */
  192. GPIO_InitStructure.GPIO_Pin = ETH_RST_PIN;
  193. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
  194. GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  195. GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
  196. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  197. GPIO_Init(GPIOE, &GPIO_InitStructure);
  198. GPIO_ResetBits(GPIOE, ETH_RST_PIN);
  199. for (i = 0; i < 20000; i++);
  200. GPIO_SetBits(GPIOE, ETH_RST_PIN);
  201. for (i = 0; i < 20000; i++);
  202. }
  203. /*********** Portions COPYRIGHT 2012 Embest Tech. Co., Ltd.*****END OF FILE****/