Rev Author Line No. Line
3328 povik 1 /**
2 ******************************************************************************
3 * @file main.c
4 * @author MCD Application Team
5 * @version V1.0.0
6 * @date 06/19/2009
7 * @brief Main program body
8 ******************************************************************************
9 * @copy
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; COPYRIGHT 2009 STMicroelectronics</center></h2>
19 */
20  
21 /* Includes ------------------------------------------------------------------*/
22 #include "stm32f10x.h"
23 #include "stm32_eval.h"
24 #include "stm32_eth.h"
25 #include <stdio.h>
26 #include <errno.h>
27 #include "uip.h"
28  
29 /** @addtogroup Webserver_Demo_uIP
30 * @{
31 */
32  
33 /* Private typedef -----------------------------------------------------------*/
34 /* Private define ------------------------------------------------------------*/
35 /*---------------LCD Messages ------------------------------------------------*/
36  
37 #define DP83848_PHY /* Ethernet pins mapped on STM3210C-EVAL Board */
38 #define PHY_ADDRESS 0x01 /* Relative to STM3210C-EVAL Board */
39  
40 #define MII_MODE /* MII mode for STM3210C-EVAL Board (MB784) (check jumpers setting) */
41 //#define RMII_MODE /* RMII mode for STM3210C-EVAL Board (MB784) (check jumpers setting) */
42  
43 #define ETH_RXBUFNB 8
44 #define ETH_TXBUFNB 2
45  
46 /* Private macro -------------------------------------------------------------*/
47 /* Private variables ---------------------------------------------------------*/
48 ETH_InitTypeDef ETH_InitStructure;
49 /* Ethernet Rx & Tx DMA Descriptors */
50 ETH_DMADESCTypeDef DMARxDscrTab[ETH_RXBUFNB], DMATxDscrTab[ETH_TXBUFNB];
51 /* Ethernet buffers */
52 u8 Rx_Buff[ETH_RXBUFNB][ETH_MAX_PACKET_SIZE], Tx_Buff[ETH_TXBUFNB][ETH_MAX_PACKET_SIZE];
53 ErrorStatus HSEStartUpStatus;
54 vu32 Value = 0;
55  
56 /* Private functions ---------------------------------------------------------*/
57 void RCC_Configuration(void);
58 void GPIO_Configuration(void);
59 void NVIC_Configuration(void);
60 void ADC_Configuration(void);
61 void uIPMain(void);
62  
63 int _write(int file, char *ptr, int len)
64 {
65 int i;
66  
67 if (file == 1) {
68 for (i = 0; i < len; i++) {
69 USART_SendData(USART1, *ptr++);
70 while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
71 }
72  
73 return i;
74 }
75  
76 errno = EIO;
77 return -1;
78 }
79  
80 /**
81 * @brief Main program.
82 * @param None
83 * @retval None
84 */
85 int main(void)
86 {
87 /* Setup STM32 system (clock, PLL and Flash configuration) */
88 SystemInit();
89  
90 /* Initialize LEDs **********************************************************/
91 STM_EVAL_LEDInit(LED1);
92 STM_EVAL_LEDInit(LED2);
93 STM_EVAL_LEDInit(LED3);
94 STM_EVAL_LEDInit(LED4);
95  
96 /* Turn on leds available on STM3210X-EVAL **********************************/
97 STM_EVAL_LEDOff(LED1);
98 STM_EVAL_LEDOff(LED2);
99 STM_EVAL_LEDOff(LED3);
100 STM_EVAL_LEDOff(LED4);
101  
102 /* System Clocks Configuration */
103 RCC_Configuration();
104  
105 /* NVIC configuration */
106 NVIC_Configuration();
107  
108 /* ADC configuration */
109 ADC_Configuration();
110  
111 /* ETHERNET pins remapp in STM3210C-EVAL board: RX_DV and RxD[3:0] */
112 //GPIO_PinRemapConfig(GPIO_Remap_ETH, ENABLE);
113  
114 /* MII/RMII Media interface selection */
115 #ifdef MII_MODE /* Mode MII with STM3210C-EVAL */
116 GPIO_ETH_MediaInterfaceConfig(GPIO_ETH_MediaInterface_MII);
117  
118 /* Get HSE clock = 25MHz on PA8 pin(MCO) */
119 //RCC_MCOConfig(RCC_MCO_HSE);
120  
121 #elif defined RMII_MODE /* Mode RMII with STM3210C-EVAL */
122 GPIO_ETH_MediaInterfaceConfig(GPIO_ETH_MediaInterface_RMII);
123  
124 /* Get HSE clock = 25MHz on PA8 pin(MCO) */
125 /* set PLL3 clock output to 50MHz (25MHz /5 *10 =50MHz) */
126 RCC_PLL3Config(RCC_PLL3Mul_10);
127 /* Enable PLL3 */
128 RCC_PLL3Cmd(ENABLE);
129 /* Wait till PLL3 is ready */
130 while (RCC_GetFlagStatus(RCC_FLAG_PLL3RDY) == RESET)
131 {}
132  
133 /* Get clock PLL3 clock on PA8 pin */
134 RCC_MCOConfig(RCC_MCO_PLL3CLK);
135 #endif
136  
137 /* Configure the GPIO ports */
138 GPIO_Configuration();
139  
140 USART_Cmd(USART1, ENABLE);
141 USART_InitTypeDef USART_InitStructure;
142 USART_InitStructure.USART_BaudRate = 9600;
143 USART_InitStructure.USART_WordLength = USART_WordLength_8b;
144 USART_InitStructure.USART_StopBits = USART_StopBits_1;
145 USART_InitStructure.USART_Parity = USART_Parity_No;
146 USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
147 USART_InitStructure.USART_Mode = USART_Mode_Tx;
148 USART_Init(USART1, &USART_InitStructure);
149  
150 /* Enable ETHERNET clock */
151 RCC_AHBPeriphClockCmd(RCC_AHBPeriph_ETH_MAC | RCC_AHBPeriph_ETH_MAC_Tx |
152 RCC_AHBPeriph_ETH_MAC_Rx, ENABLE);
153  
154 /* Reset ETHERNET on AHB Bus */
155 ETH_DeInit();
156  
157 /* Software reset */
158 ETH_SoftwareReset();
159  
160 /* Wait for software reset */
161 while(ETH_GetSoftwareResetStatus()==SET);
162  
163 /* ETHERNET Configuration ------------------------------------------------------*/
164 /* Call ETH_StructInit if you don't like to configure all ETH_InitStructure parameter */
165 ETH_StructInit(&ETH_InitStructure);
166  
167 /* Fill ETH_InitStructure parametrs */
168 /*------------------------ MAC -----------------------------------*/
169 ETH_InitStructure.ETH_AutoNegotiation = ETH_AutoNegotiation_Disable;
170 //ETH_InitStructure.ETH_Speed = ETH_Speed_100M;
171 ETH_InitStructure.ETH_LoopbackMode = ETH_LoopbackMode_Disable;
172 //ETH_InitStructure.ETH_Mode = ETH_Mode_FullDuplex;
173 ETH_InitStructure.ETH_RetryTransmission = ETH_RetryTransmission_Disable;
174 ETH_InitStructure.ETH_AutomaticPadCRCStrip = ETH_AutomaticPadCRCStrip_Disable;
175 ETH_InitStructure.ETH_ReceiveAll = ETH_ReceiveAll_Enable;
176 ETH_InitStructure.ETH_BroadcastFramesReception = ETH_BroadcastFramesReception_Disable;
177 ETH_InitStructure.ETH_PromiscuousMode = ETH_PromiscuousMode_Disable;
178 ETH_InitStructure.ETH_MulticastFramesFilter = ETH_MulticastFramesFilter_Perfect;
179 ETH_InitStructure.ETH_UnicastFramesFilter = ETH_UnicastFramesFilter_Perfect;
180  
181 /* Configure ETHERNET */
182  
183 printf("Before init...\r\n");
184  
185 Value = ETH_Init(&ETH_InitStructure, PHY_ADDRESS);
186  
187 printf("After init... Value: %x\r\n", Value);
188  
189 /* Initialize Tx Descriptors list: Chain Mode */
190 ETH_DMATxDescChainInit(DMATxDscrTab, &Tx_Buff[0][0], ETH_TXBUFNB);
191 /* Initialize Rx Descriptors list: Chain Mode */
192 ETH_DMARxDescChainInit(DMARxDscrTab, &Rx_Buff[0][0], ETH_RXBUFNB);
193  
194 /* Enable MAC and DMA transmission and reception */
195 ETH_Start();
196  
197 /* uIP stack main loop */
198 uIPMain();
199  
200 /* Infinite loop */
201 while (1)
202 {
203 }
204 }
205 /**
206 * @brief Configures the clock
207 * @param None
208 * @retval None
209 */
210 void RCC_Configuration(void)
211 {
212 RCC_ClocksTypeDef RCC_ClockFreq;
213  
214 /* RCC system reset(for debug purpose) */
215 RCC_DeInit();
216  
217 /* Enable HSE */
218 RCC_HSEConfig(RCC_HSE_ON);
219  
220 /* Wait till HSE is ready */
221 HSEStartUpStatus = RCC_WaitForHSEStartUp();
222  
223 if(HSEStartUpStatus != ERROR)
224 {
225 /* Enable Prefetch Buffer */
226 FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
227  
228 /****************************************************************/
229 /* HSE=25MHz, HCLK=72MHz, PCLK2=72MHz, PCLK1=36MHz */
230 /****************************************************************/
231 /* Flash 2 wait state */
232 FLASH_SetLatency(FLASH_Latency_2);
233 /* HCLK = SYSCLK */
234 RCC_HCLKConfig(RCC_SYSCLK_Div1);
235 /* PCLK2 = HCLK */
236 RCC_PCLK2Config(RCC_HCLK_Div1);
237 /* PCLK1 = HCLK/2 */
238 RCC_PCLK1Config(RCC_HCLK_Div2);
239 /* ADCCLK = PCLK2/4 */
240 RCC_ADCCLKConfig(RCC_PCLK2_Div6);
241  
242 /* Configure PLLs *********************************************************/
243 /* PPL2 configuration: PLL2CLK = (HSE / 5) * 8 = 40 MHz */
244 RCC_PREDIV2Config(RCC_PREDIV2_Div5);
245 RCC_PLL2Config(RCC_PLL2Mul_8);
246  
247 /* Enable PLL2 */
248 RCC_PLL2Cmd(ENABLE);
249  
250 /* Wait till PLL2 is ready */
251 while (RCC_GetFlagStatus(RCC_FLAG_PLL2RDY) == RESET)
252 {}
253  
254 /* PPL1 configuration: PLLCLK = (PLL2 / 5) * 9 = 72 MHz */
255 RCC_PREDIV1Config(RCC_PREDIV1_Source_PLL2, RCC_PREDIV1_Div5);
256 RCC_PLLConfig(RCC_PLLSource_PREDIV1, RCC_PLLMul_9);
257  
258 /* Enable PLL */
259 RCC_PLLCmd(ENABLE);
260  
261 /* Wait till PLL is ready */
262 while (RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
263 {}
264  
265 /* Select PLL as system clock source */
266 RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
267  
268 /* Wait till PLL is used as system clock source */
269 while (RCC_GetSYSCLKSource() != 0x08)
270 {}
271 }
272  
273 RCC_GetClocksFreq(&RCC_ClockFreq);
274  
275 /* Enable USART1 clock */
276 RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
277  
278 /* Enable GPIOs clocks */
279 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOC |
280 RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOE| RCC_APB2Periph_AFIO, ENABLE);
281  
282 /* Enable ADC1 clock */
283 RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
284 }
285  
286 /**
287 * @brief Configures the different GPIO ports.
288 * @param None
289 * @retval None
290 */
291 void GPIO_Configuration(void)
292 {
293 GPIO_InitTypeDef GPIO_InitStructure;
294  
295 /* ETHERNET pins configuration */
296 /* AF Output Push Pull */
297  
298 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_9;
299 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
300 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
301 GPIO_Init(GPIOA, &GPIO_InitStructure);
302  
303 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1 | GPIO_Pin_2;
304 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
305 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
306 GPIO_Init(GPIOC, &GPIO_InitStructure);
307  
308 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_8 | GPIO_Pin_11 |
309 GPIO_Pin_12 | GPIO_Pin_13;
310 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
311 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
312 GPIO_Init(GPIOB, &GPIO_InitStructure);
313  
314 /* Input (Reset Value) */
315  
316 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_3;
317 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
318 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
319 GPIO_Init(GPIOA, &GPIO_InitStructure);
320  
321 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_10;
322 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
323 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
324 GPIO_Init(GPIOB, &GPIO_InitStructure);
325  
326 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_5;
327 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
328 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
329 GPIO_Init(GPIOC, &GPIO_InitStructure);
330  
331 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
332 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
333 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
334 GPIO_Init(GPIOA, &GPIO_InitStructure);
335 }
336  
337 /**
338 * @brief Configures the ADC.
339 * @param None
340 * @retval None
341 */
342 void ADC_Configuration(void)
343 {
344 ADC_InitTypeDef ADC_InitStructure;
345  
346 /* ADC1 Configuration ------------------------------------------------------*/
347 ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
348 ADC_InitStructure.ADC_ScanConvMode = DISABLE;
349 ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
350 ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
351 ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
352 ADC_InitStructure.ADC_NbrOfChannel = 1;
353 ADC_Init(ADC1, &ADC_InitStructure);
354  
355 /* ADC1 regular channel4 configuration */
356 ADC_RegularChannelConfig(ADC1, ADC_Channel_4, 1, ADC_SampleTime_13Cycles5);
357  
358 /* Enable ADC1 */
359 ADC_Cmd(ADC1, ENABLE);
360  
361 /* Start ADC1 Software Conversion */
362 ADC_SoftwareStartConvCmd(ADC1, ENABLE);
363 }
364  
365 /**
366 * @brief Configures the nested vectored interrupt controller.
367 * @param None
368 * @retval None
369 */
370 void NVIC_Configuration(void)
371 {
372 /* Set the Vector Table base location at 0x08000000 */
373 NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);
374 }
375  
376 #ifdef USE_FULL_ASSERT
377  
378 /**
379 * @brief Reports the name of the source file and the source line number
380 * where the assert_param error has occurred.
381 * @param file: pointer to the source file name
382 * @param line: assert_param error line source number
383 * @retval None
384 */
385 void assert_failed(uint8_t* file, uint32_t line)
386 {
387 /* User can add his own implementation to report the file name and line number,
388 ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
389  
390 /* Infinite loop */
391 while (1)
392 {
393 }
394 }
395 #endif
396  
397 /**
398 * @}
399 */
400  
401  
402 /******************* (C) COPYRIGHT 2009 STMicroelectronics *****END OF FILE****/