stm32f4x7_eth_bsp.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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. static void ETH_NVIC_Config(void);
  55. /* Private functions ---------------------------------------------------------*/
  56. /**
  57. * @brief ETH_BSP_Config
  58. * @param None
  59. * @retval None
  60. */
  61. void ETH_BSP_Config(void)
  62. {
  63. /* Configure the GPIO ports for ethernet pins */
  64. ETH_GPIO_Config();
  65. /* Config NVIC for Ethernet */
  66. /* TODO remove if tested: ETH_NVIC_Config moved after ETH_MACDMA_Config *
  67. * to avoid ETH IRQ to stuck the system */
  68. /* Configure the Ethernet MAC/DMA */
  69. ETH_MACDMA_Config();
  70. ETH_NVIC_Config();
  71. if (EthInitStatus == 0) {
  72. // LCD_SetTextColor(LCD_COLOR_RED);
  73. // LCD_DisplayStringLine(Line5, (uint8_t*)" Ethernet Init ");
  74. // LCD_DisplayStringLine(Line6, (uint8_t*)" failed ");
  75. // STM_EVAL_LEDOn(LED5);
  76. while(1);
  77. }
  78. }
  79. /**
  80. * @brief Configures the Ethernet Interface
  81. * @param None
  82. * @retval None
  83. */
  84. static void ETH_MACDMA_Config(void)
  85. {
  86. ETH_InitTypeDef ETH_InitStructure;
  87. /* Enable ETHERNET clock */
  88. RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_ETH_MAC | RCC_AHB1Periph_ETH_MAC_Tx |
  89. RCC_AHB1Periph_ETH_MAC_Rx, ENABLE);
  90. /* Reset ETHERNET on AHB Bus */
  91. ETH_DeInit();
  92. /* Software reset */
  93. ETH_SoftwareReset();
  94. /* Wait for software reset */
  95. while (ETH_GetSoftwareResetStatus() == SET);
  96. /* ETHERNET Configuration --------------------------------------------------*/
  97. /* Call ETH_StructInit if you don't like to configure all ETH_InitStructure parameter */
  98. ETH_StructInit(&ETH_InitStructure);
  99. /* Fill ETH_InitStructure parametrs */
  100. /*------------------------ MAC -----------------------------------*/
  101. ETH_InitStructure.ETH_AutoNegotiation = ETH_AutoNegotiation_Enable;
  102. //ETH_InitStructure.ETH_AutoNegotiation = ETH_AutoNegotiation_Disable;
  103. // ETH_InitStructure.ETH_Speed = ETH_Speed_10M;
  104. // ETH_InitStructure.ETH_Mode = ETH_Mode_FullDuplex;
  105. ETH_InitStructure.ETH_LoopbackMode = ETH_LoopbackMode_Disable;
  106. ETH_InitStructure.ETH_RetryTransmission = ETH_RetryTransmission_Disable;
  107. ETH_InitStructure.ETH_AutomaticPadCRCStrip = ETH_AutomaticPadCRCStrip_Disable;
  108. ETH_InitStructure.ETH_ReceiveAll = ETH_ReceiveAll_Disable;
  109. ETH_InitStructure.ETH_BroadcastFramesReception = ETH_BroadcastFramesReception_Enable;
  110. ETH_InitStructure.ETH_PromiscuousMode = ETH_PromiscuousMode_Disable;
  111. ETH_InitStructure.ETH_MulticastFramesFilter = ETH_MulticastFramesFilter_Perfect;
  112. ETH_InitStructure.ETH_UnicastFramesFilter = ETH_UnicastFramesFilter_Perfect;
  113. #ifdef CHECKSUM_BY_HARDWARE
  114. ETH_InitStructure.ETH_ChecksumOffload = ETH_ChecksumOffload_Enable;
  115. #endif
  116. /*------------------------ DMA -----------------------------------*/
  117. /* When we use the Checksum offload feature, we need to enable the Store and Forward mode:
  118. the store and forward guarantee that a whole frame is stored in the FIFO, so the MAC can insert/verify the checksum,
  119. if the checksum is OK the DMA can handle the frame otherwise the frame is dropped */
  120. ETH_InitStructure.ETH_DropTCPIPChecksumErrorFrame = ETH_DropTCPIPChecksumErrorFrame_Enable;
  121. ETH_InitStructure.ETH_ReceiveStoreForward = ETH_ReceiveStoreForward_Enable;
  122. ETH_InitStructure.ETH_TransmitStoreForward = ETH_TransmitStoreForward_Enable;
  123. ETH_InitStructure.ETH_ForwardErrorFrames = ETH_ForwardErrorFrames_Disable;
  124. ETH_InitStructure.ETH_ForwardUndersizedGoodFrames = ETH_ForwardUndersizedGoodFrames_Disable;
  125. ETH_InitStructure.ETH_SecondFrameOperate = ETH_SecondFrameOperate_Enable;
  126. ETH_InitStructure.ETH_AddressAlignedBeats = ETH_AddressAlignedBeats_Enable;
  127. ETH_InitStructure.ETH_FixedBurst = ETH_FixedBurst_Enable;
  128. ETH_InitStructure.ETH_RxDMABurstLength = ETH_RxDMABurstLength_32Beat;
  129. ETH_InitStructure.ETH_TxDMABurstLength = ETH_TxDMABurstLength_32Beat;
  130. ETH_InitStructure.ETH_DMAArbitration = ETH_DMAArbitration_RoundRobin_RxTx_2_1;
  131. /* Configure Ethernet */
  132. EthInitStatus = ETH_Init(&ETH_InitStructure, LAN8720_PHY_ADDRESS);
  133. /* Enable the Ethernet Rx Interrupt */
  134. ETH_DMAITConfig(ETH_DMA_IT_NIS | ETH_DMA_IT_R, ENABLE);
  135. }
  136. /**
  137. * @brief Configures the different GPIO ports.
  138. * @param None
  139. * @retval None
  140. */
  141. void ETH_GPIO_Config(void)
  142. {
  143. volatile uint32_t i;
  144. GPIO_InitTypeDef GPIO_InitStructure;
  145. /* Enable GPIOs clocks */
  146. RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA | RCC_AHB1Periph_GPIOB
  147. | RCC_AHB1Periph_GPIOC, ENABLE);
  148. /* Enable SYSCFG clock */
  149. RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);
  150. /* MII/RMII Media interface selection --------------------------------------*/
  151. SYSCFG_ETH_MediaInterfaceConfig(SYSCFG_ETH_MediaInterface_RMII);
  152. /* Ethernet pins configuration ************************************************/
  153. /*
  154. ETH_MDIO --------------> PA2 +
  155. ETH_MDC ---------------> PC1 +
  156. ETH_RMII_REF_CLK-------> PA1 +
  157. ETH_RMII_CRS_DV -------> PA7 +
  158. ETH_MII_RX_ER -------> PB10 -
  159. ETH_RMII_RXD0 -------> PC4 +
  160. ETH_RMII_RXD1 -------> PC5 +
  161. ETH_RMII_TX_EN -------> PB11 +
  162. ETH_RMII_TXD0 -------> PB12 +
  163. ETH_RMII_TXD1 -------> PB13 +
  164. ETH_RST_PIN -------> PE2 - замена на PE13
  165. */
  166. #ifdef HARDWARE_BT6711
  167. #define ETH_RST_PIN GPIO_Pin_7
  168. #else
  169. #define ETH_RST_PIN GPIO_Pin_13
  170. #endif
  171. /* Configure PA1,PA2 and PA7 */
  172. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_7;
  173. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
  174. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  175. GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  176. GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
  177. GPIO_Init(GPIOA, &GPIO_InitStructure);
  178. GPIO_PinAFConfig(GPIOA, GPIO_PinSource1, GPIO_AF_ETH);
  179. GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_ETH);
  180. GPIO_PinAFConfig(GPIOA, GPIO_PinSource7, GPIO_AF_ETH);
  181. /* Configure PB10,PB11,PB12 and PB13 */
  182. GPIO_InitStructure.GPIO_Pin = /* GPIO_Pin_10 | */ GPIO_Pin_11 | GPIO_Pin_12 | GPIO_Pin_13;
  183. GPIO_Init(GPIOB, &GPIO_InitStructure);
  184. //GPIO_PinAFConfig(GPIOB, GPIO_PinSource10, GPIO_AF_ETH);
  185. GPIO_PinAFConfig(GPIOB, GPIO_PinSource11, GPIO_AF_ETH);
  186. GPIO_PinAFConfig(GPIOB, GPIO_PinSource12, GPIO_AF_ETH);
  187. GPIO_PinAFConfig(GPIOB, GPIO_PinSource13, GPIO_AF_ETH);
  188. /* Configure PC1, PC4 and PC5 */
  189. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1 | GPIO_Pin_4 | GPIO_Pin_5;
  190. GPIO_Init(GPIOC, &GPIO_InitStructure);
  191. GPIO_PinAFConfig(GPIOC, GPIO_PinSource1, GPIO_AF_ETH);
  192. GPIO_PinAFConfig(GPIOC, GPIO_PinSource4, GPIO_AF_ETH);
  193. GPIO_PinAFConfig(GPIOC, GPIO_PinSource5, GPIO_AF_ETH);
  194. /* Configure the PHY RST pin */
  195. GPIO_InitStructure.GPIO_Pin = ETH_RST_PIN;
  196. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
  197. GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  198. GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
  199. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  200. GPIO_Init(GPIOE, &GPIO_InitStructure);
  201. GPIO_ResetBits(GPIOE, ETH_RST_PIN);
  202. for (i = 0; i < 20000; i++);
  203. GPIO_SetBits(GPIOE, ETH_RST_PIN);
  204. for (i = 0; i < 20000; i++);
  205. }
  206. /**
  207. * @brief Configures and enable the Ethernet global interrupt.
  208. * @param None
  209. * @retval None
  210. */
  211. void ETH_NVIC_Config(void)
  212. {
  213. NVIC_InitTypeDef NVIC_InitStructure;
  214. /* 2 bit for pre-emption priority, 2 bits for subpriority */
  215. NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4);
  216. /* Enable the Ethernet global Interrupt */
  217. NVIC_InitStructure.NVIC_IRQChannel = ETH_IRQn;
  218. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 5;
  219. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  220. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  221. NVIC_Init(&NVIC_InitStructure);
  222. }
  223. /*********** Portions COPYRIGHT 2012 Embest Tech. Co., Ltd.*****END OF FILE****/