ARMEBS4  revision-26.06.2015
bsp_armebs4_eth.c
1 #ifdef BOARD_ARMEBS4
2 
4 #include "heivs/stm32_gpio.h"
6 #include "stm32/stm32f4xx_eth.h"
7 #include "stm32/stm32f4xx_misc.h"
8 #include "stm32/stm32f4xx_rcc.h"
10 #include "heivs/stm32_gpio.h"
11 #include "heivs/delay.h"
12 
13 static uint32_t EthStatus = 0;
14 
15 static void eth_config(void)
16 {
17  ETH_InitTypeDef ETH_InitStructure;
18 
19  /* Enable ETHERNET clock */
20  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_ETH_MAC | RCC_AHB1Periph_ETH_MAC_Tx |
21  RCC_AHB1Periph_ETH_MAC_Rx, ENABLE);
22 
23  /* Reset ETHERNET on AHB Bus */
24  ETH_DeInit();
25 
26  /* Software reset */
27  ETH_SoftwareReset();
28 
29  /* Wait for software reset */
30  while (ETH_GetSoftwareResetStatus() == SET);
31 
32  /* ETHERNET Configuration --------------------------------------------------*/
33  /* Call ETH_StructInit if you don't like to configure all ETH_InitStructure parameter */
34  ETH_StructInit(&ETH_InitStructure);
35 
36  /* Fill ETH_InitStructure parametrs */
37  /*------------------------ MAC -----------------------------------*/
38  ETH_InitStructure.ETH_AutoNegotiation = ETH_AutoNegotiation_Enable;
39  //ETH_InitStructure.ETH_AutoNegotiation = ETH_AutoNegotiation_Disable;
40  // ETH_InitStructure.ETH_Speed = ETH_Speed_10M;
41  // ETH_InitStructure.ETH_Mode = ETH_Mode_FullDuplex;
42 
43  ETH_InitStructure.ETH_LoopbackMode = ETH_LoopbackMode_Disable;
44  ETH_InitStructure.ETH_RetryTransmission = ETH_RetryTransmission_Disable;
45  ETH_InitStructure.ETH_AutomaticPadCRCStrip = ETH_AutomaticPadCRCStrip_Disable;
46  ETH_InitStructure.ETH_ReceiveAll = ETH_ReceiveAll_Disable;
47  ETH_InitStructure.ETH_BroadcastFramesReception =
48  ETH_BroadcastFramesReception_Enable;
49  ETH_InitStructure.ETH_PromiscuousMode = ETH_PromiscuousMode_Disable;
50  ETH_InitStructure.ETH_MulticastFramesFilter =
51  ETH_MulticastFramesFilter_Perfect;
52  ETH_InitStructure.ETH_UnicastFramesFilter = ETH_UnicastFramesFilter_Perfect;
53 #ifdef CHECKSUM_BY_HARDWARE
54  ETH_InitStructure.ETH_ChecksumOffload = ETH_ChecksumOffload_Enable;
55 #endif
56 
57  /*------------------------ DMA -----------------------------------*/
58 
59  /* When we use the Checksum offload feature, we need to enable the Store and Forward mode:
60  the store and forward guarantee that a whole frame is stored in the FIFO, so the MAC can insert/verify the checksum,
61  if the checksum is OK the DMA can handle the frame otherwise the frame is dropped */
62  ETH_InitStructure.ETH_DropTCPIPChecksumErrorFrame =
63  ETH_DropTCPIPChecksumErrorFrame_Enable;
64  ETH_InitStructure.ETH_ReceiveStoreForward = ETH_ReceiveStoreForward_Enable;
65  ETH_InitStructure.ETH_TransmitStoreForward =
66  ETH_TransmitStoreForward_Enable;
67 
68  ETH_InitStructure.ETH_ForwardErrorFrames = ETH_ForwardErrorFrames_Disable;
69  ETH_InitStructure.ETH_ForwardUndersizedGoodFrames =
70  ETH_ForwardUndersizedGoodFrames_Disable;
71  ETH_InitStructure.ETH_SecondFrameOperate = ETH_SecondFrameOperate_Enable;
72  ETH_InitStructure.ETH_AddressAlignedBeats = ETH_AddressAlignedBeats_Enable;
73  ETH_InitStructure.ETH_FixedBurst = ETH_FixedBurst_Enable;
76  ETH_InitStructure.ETH_DMAArbitration =
77  ETH_DMAArbitration_RoundRobin_RxTx_2_1;
78 
79  //ETH_InitStructure.ETH_PromiscuousMode = ETH_PromiscuousMode_Enable;
80  //ETH_InitStructure.ETH_LoopbackMode = ETH_LoopbackMode_Enable;
81 
82  /* Configure Ethernet */
83  if (ETH_Init(&ETH_InitStructure, LAN8720_PHY_ADDRESS) == ETH_SUCCESS)
84  {
85  EthStatus = ETH_INIT_FLAG | ETH_LINK_FLAG;
86  }
87 
88  //ETH_PHYLoopBackCmd(LAN8720_PHY_ADDRESS, ENABLE);
89 
90  /* Enable the Ethernet Rx Interrupt */
91  ETH_DMAITConfig(ETH_DMA_IT_NIS | ETH_DMA_IT_R, ENABLE);
92 }
93 
94 /**
95  * \brief Setup the gpios and reset the Ethernet chip
96  */
97 static void gpio_config(void)
98 {
99  /* Enable SYSCFG clock */
100  RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);
101 
102  /* MII/RMII Media interface selection --------------------------------------*/
103  SYSCFG_ETH_MediaInterfaceConfig(SYSCFG_ETH_MediaInterface_RMII);
104 
105  const struct gpio_t gpios[] =
106  {
107  PIN_RESET_NOT_ETHERNET,
108  GPIO_ETH_MDIO_PA2,
109  GPIO_ETH_MDC_PC1,
110 
111  GPIO_ETH_MII_RX_CLK_PA1,
112 
113  GPIO_ETH_RMII_CRS_DV_PA7,
114  GPIO_ETH_RMII_RXD0_PC4,
115  GPIO_ETH_RMII_RXD1_PC5,
116  GPIO_ETH_RMII_TXD0_PG13,
117  GPIO_ETH_RMII_TXD1_PG14,
118  GPIO_ETH_RMII_TX_EN_PG11,
119  };
120 
121  gpio_setup_list(gpios, ARRAY_SIZE(gpios));
122 
123  gpio_set(&gpios[0], 0);
124  delay_wait_us(100);
125  gpio_set(&gpios[0], 1);
126  delay_wait_us(1);
127 }
128 
129 static void interrupt_config(void)
130 {
131  extern void ETH_IRQ_Handler(void);
132  NVIC_InitTypeDef NVIC_InitStructure;
133 
134  /* Enable the Ethernet global Interrupt */
135  NVIC_InitStructure.NVIC_IRQChannel = ETH_IRQn;
136  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 5;
137  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
138  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
139 
140  NVIC_InitStructure.NVIC_Handler = ETH_IRQ_Handler;
141  NVIC_Init(&NVIC_InitStructure);
142 }
143 
144 void bsp_eth_config(void)
145 {
146  // Configure the GPIO ports for ethernet pins
147  gpio_config();
148 
149  // Config NVIC for Ethernet
150  interrupt_config();
151 
152  /// Configure the Ethernet MAC/DMA
153  eth_config();
154 }
155 
156 uint32_t bsp_eth_status(void)
157 {
158  return EthStatus;
159 }
160 
162 {
163  gpio_config();
164  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_ETH_MAC | RCC_AHB1Periph_ETH_MAC_Tx | RCC_AHB1Periph_ETH_MAC_Rx, ENABLE);
165  ETH_DeInit();
166  ETH_SoftwareReset();
167  while (ETH_GetSoftwareResetStatus() == SET);
168  ETH_Init_MII_Only();
169 
170  ETH_WritePHYRegister(LAN8720_PHY_ADDRESS, 0, 1 << 11);
171  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_ETH_MAC | RCC_AHB1Periph_ETH_MAC_Tx | RCC_AHB1Periph_ETH_MAC_Rx, DISABLE);
172 }
173 
174 
175 #endif
This file contains all the functions prototypes for the RCC firmware library.
uint32_t ETH_RxDMABurstLength
uint32_t ETH_AddressAlignedBeats
uint32_t ETH_MulticastFramesFilter
uint8_t NVIC_IRQChannel
uint32_t ETH_ForwardErrorFrames
uint32_t ETH_ChecksumOffload
Definition: stm32f4xx_eth.h:95
uint32_t ETH_ReceiveAll
Header for stm32f4x7_eth_bsp.c file.
uint32_t ETH_TransmitStoreForward
uint32_t ETH_DMAArbitration
uint8_t NVIC_IRQChannelSubPriority
uint32_t ETH_ReceiveStoreForward
NVIC Init Structure definition.
static void gpio_set(const struct gpio_t *gpio, uint32_t value)
Set a gpio.
Definition: stm32_gpio.h:260
This file contains all the functions prototypes for the SYSCFG firmware library.
uint32_t ETH_TxDMABurstLength
uint32_t ETH_UnicastFramesFilter
uint32_t ETH_SecondFrameOperate
#define ETH_TxDMABurstLength_32Beat
uint32_t bsp_eth_status(void)
Get the status (link, ...) of the Ethernet.
board specific defines
ETH MAC Init structure definition.
Definition: stm32f4xx_eth.h:56
#define ETH_DMA_IT_NIS
BSP for ARMEBS4 Ethernet.
GPIO control structure.
Definition: stm32_gpio.h:96
void bsp_eth_config(void)
configure the Ethernet interface
uint32_t ETH_BroadcastFramesReception
uint32_t ETH_AutoNegotiation
/ * MAC
Definition: stm32f4xx_eth.h:60
FunctionalState NVIC_IRQChannelCmd
simple delays
uint32_t ETH_FixedBurst
uint8_t NVIC_IRQChannelPreemptionPriority
uint32_t ETH_RetryTransmission
Definition: stm32f4xx_eth.h:98
uint32_t ETH_ForwardUndersizedGoodFrames
#define ETH_DMA_IT_R
uint32_t ETH_LoopbackMode
Definition: stm32f4xx_eth.h:89
uint32_t ETH_PromiscuousMode
void delay_wait_us(uint32_t us)
Wait for at least that time.
Definition: delay.c:41
void bsp_eth_powerdown(void)
Shutdown the Ethernet interface.
status_e gpio_setup_list(const struct gpio_t gpio[], size_t len)
Setup an array of gpio.
Definition: stm32_gpio.c:47
uint32_t ETH_AutomaticPadCRCStrip
#define ARRAY_SIZE(x)
Number of elements in the array.
Definition: utils.h:19
#define ETH_RxDMABurstLength_32Beat
uint32_t ETH_DropTCPIPChecksumErrorFrame
/ * DMA