1 | #include "init.h"
|
2 | #include "gpio_conf.h"
|
3 | #include "stm32f10x.h"
|
4 | #include "STM32_Init.h"
|
5 | #include "stm32_eth.h"
|
6 |
|
7 |
|
8 |
|
9 | USART_InitTypeDef USART_InitStructure;
|
10 | ErrorStatus HSEStartUpStatus;
|
11 |
|
12 |
|
13 | void init(void)
|
14 | {
|
15 | //SysClk and Flash Configuration is Set from startup_stm32f37x.s
|
16 |
|
17 |
|
18 | /*GPIO_Configuration ******************************************************/
|
19 | SystemInit();
|
20 | GPIO_Configuration();
|
21 | RCC_Configuration();
|
22 | USART2_Configuration();
|
23 | ETH_Configuration();
|
24 |
|
25 |
|
26 | }
|
27 |
|
28 |
|
29 | void GPIO_Configuration(void) {
|
30 |
|
31 |
|
32 | /* Private typedef ---------------------------------------------------------*/
|
33 | GPIO_InitTypeDef GPIO_InitStruct;
|
34 |
|
35 |
|
36 | /** USART2 GPIO Configuration
|
37 | PD5 ------> USART2_TX
|
38 | PD6 ------> USART2_RX
|
39 | */
|
40 |
|
41 |
|
42 | /*Enable or disable APB2 peripheral clock */
|
43 | RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD|RCC_APB2Periph_AFIO, ENABLE);
|
44 |
|
45 |
|
46 | /*Configure GPIO pin */
|
47 | GPIO_InitStruct.GPIO_Pin = USART2_TX_PIN;
|
48 | GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP;
|
49 | GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
|
50 | GPIO_Init(USART2_PORT, &GPIO_InitStruct);
|
51 |
|
52 | /*Configure GPIO pin */
|
53 | GPIO_InitStruct.GPIO_Pin = USART2_RX_PIN;
|
54 | GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN_FLOATING;
|
55 | GPIO_Init(USART2_PORT, &GPIO_InitStruct);
|
56 |
|
57 | /*Configure peripheral I/O remapping */
|
58 | GPIO_PinRemapConfig(GPIO_Remap_USART2, ENABLE);
|
59 |
|
60 |
|
61 | /** Configure pins as GPIO
|
62 | PD13 ------> GPIO_Output
|
63 | PD3 ------> GPIO_Output
|
64 | PD4 ------> GPIO_Output
|
65 | PD7 ------> GPIO_Output
|
66 | PB9 ------> GPIO_Input
|
67 | */
|
68 |
|
69 |
|
70 | /*Enable or disable APB2 peripheral clock */
|
71 | RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD|RCC_APB2Periph_GPIOB, ENABLE);
|
72 |
|
73 | /*Configure GPIO pin */
|
74 | GPIO_InitStruct.GPIO_Pin = GPIO_Pin_13|GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_7;
|
75 | GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP;
|
76 | GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
|
77 | GPIO_Init(GPIOD, &GPIO_InitStruct);
|
78 |
|
79 | /*Configure GPIO pin */
|
80 | GPIO_InitStruct.GPIO_Pin = GPIO_Pin_9;
|
81 | GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN_FLOATING;
|
82 | GPIO_Init(GPIOB, &GPIO_InitStruct);
|
83 |
|
84 | //All below for Ethernet
|
85 |
|
86 | /** ETH_MII GPIO Configuration
|
87 | PC1 ------> ETH_MII_MDC
|
88 | PC2 ------> ETH_MII_TXD2
|
89 | PC3 ------> ETH_MII_TX_CLK
|
90 | PA0-WKUP ------> ETH_MII_CRS
|
91 | PA1 ------> ETH_MII_RX_CLK
|
92 | PA2 ------> ETH_MII_MDIO
|
93 | PA3 ------> ETH_MII_COL
|
94 | PB10 ------> ETH_MII_RX_ER
|
95 | PB11 ------> ETH_MII_TX_EN
|
96 | PB12 ------> ETH_MII_TXD0
|
97 | PB13 ------> ETH_MII_TXD1
|
98 | PD8 ------> ETH_MII_RX_DV
|
99 | PD9 ------> ETH_MII_RXD0
|
100 | PD10 ------> ETH_MII_RXD1
|
101 | PD11 ------> ETH_MII_RXD2
|
102 | PD12 ------> ETH_MII_RXD3
|
103 | PB8 ------> ETH_MII_TXD3
|
104 | */
|
105 |
|
106 |
|
107 | /*Enable or disable APB2 peripheral clock */
|
108 | RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC|RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOD, ENABLE);
|
109 |
|
110 | RCC_AHBPeriphClockCmd(RCC_AHBPeriph_ETH_MAC | RCC_AHBPeriph_ETH_MAC_Tx |RCC_AHBPeriph_ETH_MAC_Rx, ENABLE);
|
111 | RCC_PLL2Cmd(RCC_PLL2Mul_8);
|
112 | RCC_PLL3Cmd(RCC_PLL3Mul_10);
|
113 | RCC_AHBPeriphClockCmd(RCC_AHBENR_ETHMACEN | RCC_AHBENR_ETHMACRXEN | RCC_AHBENR_ETHMACTXEN, ENABLE);
|
114 |
|
115 |
|
116 | /*Configure GPIO pin */
|
117 | GPIO_InitStruct.GPIO_Pin = GPIO_Pin_1|GPIO_Pin_2;
|
118 | GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP;
|
119 | GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
|
120 | GPIO_Init(GPIOC, &GPIO_InitStruct);
|
121 |
|
122 | /*Configure GPIO pin */
|
123 | GPIO_InitStruct.GPIO_Pin = GPIO_Pin_3;
|
124 | GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN_FLOATING;
|
125 | GPIO_Init(GPIOC, &GPIO_InitStruct);
|
126 |
|
127 | /*Configure GPIO pin */
|
128 | GPIO_InitStruct.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3;
|
129 | GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN_FLOATING;
|
130 | GPIO_Init(GPIOA, &GPIO_InitStruct);
|
131 |
|
132 | /*Configure GPIO pin */
|
133 | GPIO_InitStruct.GPIO_Pin = GPIO_Pin_10;
|
134 | GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN_FLOATING;
|
135 | GPIO_Init(GPIOB, &GPIO_InitStruct);
|
136 |
|
137 | /*Configure GPIO pin */
|
138 | GPIO_InitStruct.GPIO_Pin = GPIO_Pin_11|GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_8;
|
139 | GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP;
|
140 | GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
|
141 | GPIO_Init(GPIOB, &GPIO_InitStruct);
|
142 |
|
143 | /*Configure GPIO pin */
|
144 | GPIO_InitStruct.GPIO_Pin = GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_10|GPIO_Pin_11|GPIO_Pin_12;
|
145 | GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN_FLOATING;
|
146 | GPIO_Init(GPIOD, &GPIO_InitStruct);
|
147 |
|
148 |
|
149 | /** SYS GPIO Configuration
|
150 | PA8 ------> SYS_MCO
|
151 | */
|
152 |
|
153 |
|
154 | /*Enable or disable APB2 peripheral clock */
|
155 | RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
|
156 |
|
157 | /*Configure GPIO pin */
|
158 | GPIO_InitStruct.GPIO_Pin = GPIO_Pin_8;
|
159 | GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP;
|
160 | GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
|
161 | GPIO_Init(GPIOA, &GPIO_InitStruct);
|
162 |
|
163 |
|
164 | }
|
165 |
|
166 | void ETH_Configuration(void){
|
167 | unsigned int PhyAddr;
|
168 |
|
169 |
|
170 | ETH_InitTypeDef ETH_InitStructure;
|
171 |
|
172 | ETH_DeInit();
|
173 | ETH_SoftwareReset();
|
174 | ETH_Start();
|
175 | // while(ETH_GetSoftwareResetStatus()==SET);
|
176 | ETH_Start();
|
177 | ETH_StructInit(Ð_InitStructure);
|
178 |
|
179 | ETH_InitStructure.ETH_AutoNegotiation = ETH_AutoNegotiation_Disable;
|
180 | ETH_InitStructure.ETH_LoopbackMode = ETH_LoopbackMode_Disable;
|
181 | ETH_InitStructure.ETH_RetryTransmission = ETH_RetryTransmission_Disable;
|
182 | ETH_InitStructure.ETH_AutomaticPadCRCStrip = ETH_AutomaticPadCRCStrip_Disable;
|
183 | ETH_InitStructure.ETH_ReceiveAll = ETH_ReceiveAll_Enable;
|
184 | ETH_InitStructure.ETH_BroadcastFramesReception = ETH_BroadcastFramesReception_Disable;
|
185 | ETH_InitStructure.ETH_PromiscuousMode = ETH_PromiscuousMode_Disable;
|
186 | ETH_InitStructure.ETH_MulticastFramesFilter = ETH_MulticastFramesFilter_Perfect;
|
187 | ETH_InitStructure.ETH_UnicastFramesFilter = ETH_UnicastFramesFilter_Perfect;
|
188 | ETH_InitStructure.ETH_Mode = ETH_Mode_FullDuplex;
|
189 | ETH_InitStructure.ETH_Speed = ETH_Speed_100M;
|
190 |
|
191 |
|
192 |
|
193 | for(PhyAddr = 1; 32 >= PhyAddr; PhyAddr++)
|
194 | {
|
195 | if((0x0006 == ETH_ReadPHYRegister(PhyAddr,2))
|
196 | && (0x1c50 == (ETH_ReadPHYRegister(PhyAddr,3)&0xFFF0))) break;
|
197 | }
|
198 |
|
199 | if(0 == ETH_Init(Ð_InitStructure, PhyAddr))
|
200 | {
|
201 | LEDTest();
|
202 |
|
203 | }
|
204 |
|
205 |
|
206 | }
|
207 |
|
208 |
|
209 |
|
210 |
|
211 | void RCC_Configuration(void)
|
212 | {
|
213 | /* RCC system reset(for debug purpose) */
|
214 | RCC_DeInit();
|
215 |
|
216 | /* Enable HSE */
|
217 | RCC_HSEConfig(RCC_HSE_ON);
|
218 |
|
219 | /* Wait till HSE is ready */
|
220 | HSEStartUpStatus = RCC_WaitForHSEStartUp();
|
221 |
|
222 | if(HSEStartUpStatus == SUCCESS)
|
223 | {
|
224 | /* HCLK = SYSCLK */
|
225 | RCC_HCLKConfig(RCC_SYSCLK_Div1);
|
226 |
|
227 | /* PCLK2 = HCLK */
|
228 | RCC_PCLK2Config(RCC_HCLK_Div1);
|
229 |
|
230 | /* PCLK1 = HCLK/2 */
|
231 | RCC_PCLK1Config(RCC_HCLK_Div2);
|
232 |
|
233 | /* Enable PLL */
|
234 | RCC_PLLCmd(ENABLE);
|
235 |
|
236 | /* Wait till PLL is ready */
|
237 | while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
|
238 | {
|
239 | }
|
240 |
|
241 | /* Select PLL as system clock source */
|
242 | RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
|
243 |
|
244 | /* Wait till PLL is used as system clock source */
|
245 | while(RCC_GetSYSCLKSource() != 0x08)
|
246 | {
|
247 | }
|
248 | }
|
249 |
|
250 | /* Enable USART1, GPIOA, GPIOD and AFIO clocks */
|
251 | RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA |RCC_APB2Periph_GPIOD
|
252 | | RCC_APB2Periph_AFIO, ENABLE);
|
253 | /* Enable USART2 clock */
|
254 | RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
|
255 |
|
256 | /*Enable or disable APB2 peripheral clock */
|
257 | RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC|RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOD, ENABLE);
|
258 |
|
259 | RCC_AHBPeriphClockCmd(RCC_AHBPeriph_ETH_MAC | RCC_AHBPeriph_ETH_MAC_Tx |RCC_AHBPeriph_ETH_MAC_Rx, ENABLE);
|
260 | }
|
261 |
|
262 |
|
263 |
|
264 | void USART2_Configuration(void)
|
265 | {
|
266 | USART_InitTypeDef USART2_TypeDef;
|
267 |
|
268 | USART_DeInit(USART2);
|
269 |
|
270 |
|
271 | RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD|RCC_APB2Periph_AFIO, ENABLE);
|
272 |
|
273 | USART_InitStructure.USART_BaudRate = 115200;
|
274 | USART_InitStructure.USART_WordLength = USART_WordLength_8b;
|
275 | USART_InitStructure.USART_StopBits = USART_StopBits_1;
|
276 | USART_InitStructure.USART_Parity = USART_Parity_No;
|
277 | USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
|
278 | USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
|
279 |
|
280 |
|
281 |
|
282 | USART_Init(USART2, &USART_InitStructure);
|
283 |
|
284 |
|
285 | USART_Cmd(USART2, ENABLE);
|
286 |
|
287 | }
|
288 |
|
289 |
|
290 |
|
291 | #ifdef USE_FULL_ASSERT
|
292 |
|
293 | /**
|
294 | * @brief Reports the name of the source file and the source line number
|
295 | * where the assert_param error has occurred.
|
296 | * @param file: pointer to the source file name
|
297 | * @param line: assert_param error line source number
|
298 | * @retval None
|
299 | */
|
300 | void assert_failed(uint8_t* file, uint32_t line)
|
301 | {
|
302 | /* User can add his own implementation to report the file name and line number,
|
303 | ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
|
304 |
|
305 | /* Infinite loop */
|
306 | while (1)
|
307 | {
|
308 | }
|
309 | }
|
310 | #endif
|