1 | #include "stm32f2xx_conf.h"
|
2 |
|
3 | #include "FSMC.h"
|
4 | #include "stm32f2x7_eth.h"
|
5 | #include "netconf.h"
|
6 | //#include "stm32f2x7_eth_bsp.h"
|
7 |
|
8 | #define SYSTEMTICK_PERIOD_MS 10
|
9 | #define DP83848_PHY_ADDRESS 0x01
|
10 | /* Specific defines for EXTI line, used to manage Ethernet link status */
|
11 | #define ETH_LINK_EXTI_LINE EXTI_Line14
|
12 | #define ETH_LINK_EXTI_PORT_SOURCE EXTI_PortSourceGPIOB
|
13 | #define ETH_LINK_EXTI_PIN_SOURCE EXTI_PinSource14
|
14 | #define ETH_LINK_EXTI_IRQn EXTI15_10_IRQn
|
15 | /* PB14 */
|
16 | #define ETH_LINK_PIN GPIO_Pin_14
|
17 | #define ETH_LINK_GPIO_PORT GPIOB
|
18 | #define ETH_LINK_GPIO_CLK RCC_AHB1Periph_GPIOB
|
19 | /* PHY registers */
|
20 | #define PHY_MICR 0x11 /* MII Interrupt Control Register */
|
21 | #define PHY_MICR_INT_EN ((uint16_t)0x0002) /* PHY Enable interrupts */
|
22 | #define PHY_MICR_INT_OE ((uint16_t)0x0001) /* PHY Enable output interrupt events */
|
23 | #define PHY_MISR 0x12 /* MII Interrupt Status and Misc. Control Register */
|
24 | #define PHY_MISR_LINK_INT_EN ((uint16_t)0x0020) /* Enable Interrupt on change of link status */
|
25 | #define PHY_LINK_STATUS ((uint16_t)0x2000) /* PHY link status interrupt mask */
|
26 |
|
27 | __IO uint32_t LocalTime = 0;
|
28 | __IO uint8_t EthLinkStatus = 0;
|
29 |
|
30 | void SendString(USART_TypeDef * usart, char * text)
|
31 | {
|
32 | while(*text != 0)
|
33 | {
|
34 | while (USART_GetFlagStatus (usart, USART_FLAG_TXE)== RESET);
|
35 | USART_SendData(usart, *text);
|
36 | text++;
|
37 | }
|
38 | }
|
39 |
|
40 | void Delay2(unsigned long a)
|
41 | {
|
42 | while(a > 0) a--;
|
43 | }
|
44 |
|
45 | void InitUart()
|
46 | {
|
47 | GPIO_InitTypeDef GPIO_InitStructure;
|
48 | RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
|
49 | RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
|
50 |
|
51 | GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_USART1);
|
52 | GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
|
53 | GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
|
54 | GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
|
55 | GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
|
56 | GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
|
57 | GPIO_Init(GPIOA, &GPIO_InitStructure);
|
58 |
|
59 | /* RX Pin */
|
60 | GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_USART1);
|
61 | GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
|
62 | GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
|
63 | GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
|
64 | GPIO_Init(GPIOA, &GPIO_InitStructure);
|
65 |
|
66 | USART_InitTypeDef USART_InitStructure;
|
67 |
|
68 | USART_InitStructure.USART_BaudRate = 57600;
|
69 | USART_InitStructure.USART_WordLength = USART_WordLength_8b;
|
70 | USART_InitStructure.USART_StopBits = USART_StopBits_1;
|
71 | USART_InitStructure.USART_Parity = USART_Parity_No;
|
72 | USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
|
73 | USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
|
74 |
|
75 | USART_Init(USART1, &USART_InitStructure);
|
76 |
|
77 | USART_Cmd(USART1, ENABLE);
|
78 | }
|
79 |
|
80 | uint8_t InitMAC()
|
81 | {
|
82 | GPIO_InitTypeDef GPIO_InitStructure;
|
83 | ETH_InitTypeDef ETH_InitStructure;
|
84 | RCC_ClocksTypeDef RCC_Clocks;
|
85 |
|
86 | RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
|
87 | RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
|
88 | RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
|
89 |
|
90 | GPIO_PinAFConfig(GPIOA, GPIO_PinSource1, GPIO_AF_ETH);
|
91 | GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_ETH);
|
92 | GPIO_PinAFConfig(GPIOA, GPIO_PinSource7, GPIO_AF_ETH);
|
93 | GPIO_PinAFConfig(GPIOA, GPIO_PinSource8, GPIO_AF_MCO);
|
94 | GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
|
95 | GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
|
96 | GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_7 | GPIO_Pin_8;
|
97 | GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
|
98 | GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
|
99 | GPIO_Init(GPIOA, &GPIO_InitStructure);
|
100 |
|
101 | GPIO_PinAFConfig(GPIOB, GPIO_PinSource11, GPIO_AF_ETH);
|
102 | GPIO_PinAFConfig(GPIOB, GPIO_PinSource12, GPIO_AF_ETH);
|
103 | GPIO_PinAFConfig(GPIOB, GPIO_PinSource13, GPIO_AF_ETH);
|
104 | GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11 | GPIO_Pin_12 | GPIO_Pin_13;
|
105 | GPIO_Init(GPIOB, &GPIO_InitStructure);
|
106 |
|
107 | GPIO_PinAFConfig(GPIOC, GPIO_PinSource1, GPIO_AF_ETH);
|
108 | GPIO_PinAFConfig(GPIOC, GPIO_PinSource4, GPIO_AF_ETH);
|
109 | GPIO_PinAFConfig(GPIOC, GPIO_PinSource5, GPIO_AF_ETH);
|
110 | GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1 | GPIO_Pin_4 | GPIO_Pin_5;
|
111 | GPIO_Init(GPIOC, &GPIO_InitStructure);
|
112 |
|
113 |
|
114 | RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_ETH_MAC, ENABLE);
|
115 | RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_ETH_MAC_Rx, ENABLE);
|
116 | RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_ETH_MAC_Tx, ENABLE);
|
117 | RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);
|
118 | RCC_AHB1PeriphClockCmd(ETH_LINK_GPIO_CLK, ENABLE);
|
119 | SYSCFG_ETH_MediaInterfaceConfig(SYSCFG_ETH_MediaInterface_RMII);
|
120 |
|
121 |
|
122 | // Set PLL3 clock output to 50MHz
|
123 | RCC_MCO1Config(RCC_MCO1Source_PLLCLK, RCC_MCO1Div_2);
|
124 |
|
125 | /* Reset ETHERNET on AHB Bus */
|
126 | ETH_DeInit();
|
127 |
|
128 | // Software reset
|
129 | ETH_SoftwareReset();
|
130 | GPIO_SetBits(GPIOA, GPIO_Pin_6);
|
131 | // Wait for software reset
|
132 | while (ETH_GetSoftwareResetStatus() == SET);
|
133 | GPIO_ResetBits(GPIOA, GPIO_Pin_6);
|
134 |
|
135 | // ETHERNET Configuration ------------------------------------------------------
|
136 | // Call ETH_StructInit if you don't like to configure all ETH_InitStructure parameter
|
137 | ETH_StructInit(Ð_InitStructure);
|
138 |
|
139 | /* Fill ETH_InitStructure parametrs */
|
140 | /*------------------------ MAC -----------------------------------*/
|
141 | ETH_InitStructure.ETH_AutoNegotiation = ETH_AutoNegotiation_Enable ;
|
142 | ETH_InitStructure.ETH_LoopbackMode = ETH_LoopbackMode_Disable;
|
143 | ETH_InitStructure.ETH_RetryTransmission = ETH_RetryTransmission_Disable;
|
144 | ETH_InitStructure.ETH_AutomaticPadCRCStrip = ETH_AutomaticPadCRCStrip_Disable;
|
145 | ETH_InitStructure.ETH_ReceiveAll = ETH_ReceiveAll_Disable;
|
146 | ETH_InitStructure.ETH_BroadcastFramesReception = ETH_BroadcastFramesReception_Enable;
|
147 | ETH_InitStructure.ETH_PromiscuousMode = ETH_PromiscuousMode_Disable;
|
148 | ETH_InitStructure.ETH_MulticastFramesFilter = ETH_MulticastFramesFilter_Perfect;
|
149 | ETH_InitStructure.ETH_UnicastFramesFilter = ETH_UnicastFramesFilter_Perfect;
|
150 | #ifdef CHECKSUM_BY_HARDWARE
|
151 | ETH_InitStructure.ETH_ChecksumOffload = ETH_ChecksumOffload_Enable;
|
152 | #endif
|
153 |
|
154 | /*------------------------ DMA -----------------------------------*/
|
155 |
|
156 | /* When we use the Checksum offload feature, we need to enable the Store and Forward mode:
|
157 | the store and forward guarantee that a whole frame is stored in the FIFO, so the MAC can insert/verify the checksum,
|
158 | if the checksum is OK the DMA can handle the frame otherwise the frame is dropped */
|
159 | ETH_InitStructure.ETH_DropTCPIPChecksumErrorFrame = ETH_DropTCPIPChecksumErrorFrame_Enable;
|
160 | ETH_InitStructure.ETH_ReceiveStoreForward = ETH_ReceiveStoreForward_Enable;
|
161 | ETH_InitStructure.ETH_TransmitStoreForward = ETH_TransmitStoreForward_Enable;
|
162 |
|
163 | ETH_InitStructure.ETH_ForwardErrorFrames = ETH_ForwardErrorFrames_Disable;
|
164 | ETH_InitStructure.ETH_ForwardUndersizedGoodFrames = ETH_ForwardUndersizedGoodFrames_Disable;
|
165 | ETH_InitStructure.ETH_SecondFrameOperate = ETH_SecondFrameOperate_Enable;
|
166 | ETH_InitStructure.ETH_AddressAlignedBeats = ETH_AddressAlignedBeats_Enable;
|
167 | ETH_InitStructure.ETH_FixedBurst = ETH_FixedBurst_Enable;
|
168 | ETH_InitStructure.ETH_RxDMABurstLength = ETH_RxDMABurstLength_32Beat;
|
169 | ETH_InitStructure.ETH_TxDMABurstLength = ETH_TxDMABurstLength_32Beat;
|
170 | ETH_InitStructure.ETH_DMAArbitration = ETH_DMAArbitration_RoundRobin_RxTx_2_1;
|
171 |
|
172 | /* Configure Ethernet */
|
173 | if(ETH_Init(Ð_InitStructure, DP83848_PHY_ADDRESS) == 0)
|
174 | {
|
175 | GPIO_SetBits(GPIOA, GPIO_Pin_6);
|
176 | }
|
177 |
|
178 | /* Enable the Ethernet Rx Interrupt */
|
179 | ETH_DMAITConfig(ETH_DMA_IT_NIS | ETH_DMA_IT_R, ENABLE);
|
180 | SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK);
|
181 | RCC_GetClocksFreq(&RCC_Clocks);
|
182 | SysTick_Config(RCC_Clocks.HCLK_Frequency / 100);
|
183 | /* Configure the PHY to generate an interrupt on change of link status */
|
184 | //Eth_Link_PHYITConfig(DP83848_PHY_ADDRESS);
|
185 |
|
186 | return SUCCESS;
|
187 |
|
188 | }
|
189 |
|
190 | void Time_Update(void)
|
191 | {
|
192 | LocalTime += SYSTEMTICK_PERIOD_MS;
|
193 | }
|
194 |
|
195 | /**
|
196 | * @brief Configures and enable the Ethernet global interrupt.
|
197 | * @param None
|
198 | * @retval None
|
199 | */
|
200 | void ETH_NVIC_Config(void)
|
201 | {
|
202 | NVIC_InitTypeDef NVIC_InitStructure;
|
203 |
|
204 | /* 2 bit for pre-emption priority, 2 bits for subpriority */
|
205 | NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
|
206 |
|
207 | /* Enable the Ethernet global Interrupt */
|
208 | NVIC_InitStructure.NVIC_IRQChannel = ETH_IRQn;
|
209 | NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
|
210 | NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
|
211 | NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
|
212 | NVIC_Init(&NVIC_InitStructure);
|
213 | }
|
214 |
|
215 | int main(void)
|
216 | {
|
217 | GPIO_InitTypeDef GPIO_InitStructure;
|
218 |
|
219 | SystemInit();
|
220 |
|
221 | RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
|
222 | RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
|
223 |
|
224 | GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
|
225 | GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
|
226 | GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
|
227 | GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
|
228 | GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
|
229 | GPIO_Init(GPIOB, &GPIO_InitStructure);
|
230 | GPIO_Init(GPIOA, &GPIO_InitStructure);
|
231 |
|
232 | GPIO_ToggleBits(GPIOA, GPIO_Pin_6);
|
233 | Delay2(10000000L);
|
234 |
|
235 | RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA1, ENABLE);
|
236 | RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2, ENABLE);
|
237 |
|
238 | GPIO_SetBits(GPIOA, GPIO_Pin_6);
|
239 | InitUart();
|
240 | InitFSMC();
|
241 | ETH_NVIC_Config();
|
242 | InitMAC();
|
243 | GPIO_SetBits(GPIOB, GPIO_Pin_6);
|
244 | LwIP_Init();
|
245 | GPIO_ResetBits(GPIOA, GPIO_Pin_6);
|
246 | char text[] = "Hallo Welt!\n\0";
|
247 | uint16_t buffer[] = {1, 2, 3, 4, 5, 6, 7, 8, 10};
|
248 | uint16_t rBuffer[10];
|
249 | int i;
|
250 |
|
251 | GPIO_WriteBit(GPIOB, GPIO_Pin_6, Bit_SET);
|
252 | EthLinkStatus = 1;
|
253 | while(1)
|
254 | {
|
255 | SendString(USART1, "1");
|
256 | if (ETH_CheckFrameReceived())
|
257 | {
|
258 | GPIO_ToggleBits(GPIOA, GPIO_Pin_6);
|
259 | SendString(USART1, "Frame Recieved\n\0");
|
260 | /* process received ethernet packet */
|
261 | LwIP_Pkt_Handle();
|
262 | }
|
263 | /* handle periodic timers for LwIP */
|
264 | LwIP_Periodic_Handle(LocalTime);
|
265 | GPIO_ToggleBits(GPIOB, GPIO_Pin_6);
|
266 | }
|
267 | }
|