main.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. #include "main.h"
  2. #include "ethernetif.h"
  3. #include "lwip/netif.h"
  4. #include "lwip/tcpip.h"
  5. #include "app_ethernet.h"
  6. #include "log.h"
  7. #include "bsp_fsmc_sram.h"
  8. #include "bsp_fsmc_nandflash.h"
  9. #include "nandflash.h"
  10. #include "myFile.h"
  11. #include "string.h"
  12. /* Private typedef -----------------------------------------------------------*/
  13. /* Private define ------------------------------------------------------------*/
  14. /* Private macro -------------------------------------------------------------*/
  15. /* Private variables ---------------------------------------------------------*/
  16. struct netif gnetif; /* network interface structure */
  17. /* Semaphore to signal Ethernet Link state update */
  18. osSemaphoreId Netif_LinkSemaphore = NULL;
  19. /* Ethernet link thread Argument */
  20. struct link_str link_arg;
  21. /* Private function prototypes -----------------------------------------------*/
  22. static void SystemClock_Config(void);
  23. static void StartThread(void const * argument);
  24. static void ToggleLed4(void const * argument);
  25. static void BSP_Config(void);
  26. static void Netif_Config(void);
  27. /* Private functions ---------------------------------------------------------*/
  28. /**
  29. * @brief Main program
  30. * @param None
  31. * @retval None
  32. */
  33. int main(void)
  34. {
  35. /* STM32F2xx HAL library initialization:
  36. - Configure the Flash prefetch, instruction and Data caches
  37. - Configure the Systick to generate an interrupt each 1 msec
  38. - Set NVIC Group Priority to 4
  39. - Global MSP (MCU Support Package) initialization
  40. */
  41. HAL_Init();
  42. /* Configure the system clock to 120 MHz */
  43. SystemClock_Config();
  44. MX_FSMC_SRAM_Init();
  45. NAND_Init();
  46. /* Init task */
  47. osThreadDef(Start, StartThread, osPriorityNormal, 0, configMINIMAL_STACK_SIZE * 4);
  48. osThreadCreate (osThread(Start), NULL);
  49. /* Start scheduler */
  50. osKernelStart();
  51. /* We should never get here as control is now taken by the scheduler */
  52. for( ;; );
  53. }
  54. /**
  55. * @brief Start Thread
  56. * @param argument not used
  57. * @retval None
  58. */
  59. static void StartThread(void const * argument)
  60. {
  61. /* Initialize LCD and LEDs */
  62. BSP_Config();
  63. log_init();
  64. /* Create tcp_ip stack thread */
  65. tcpip_init(NULL, NULL);
  66. /* Initialize the LwIP stack */
  67. Netif_Config();
  68. /* Notify user about the network interface config */
  69. User_notification(&gnetif);
  70. #ifdef USE_DHCP
  71. /* Start DHCPClient */
  72. osThreadDef(DHCP, DHCP_thread, osPriorityBelowNormal, 0, configMINIMAL_STACK_SIZE * 2);
  73. osThreadCreate (osThread(DHCP), &gnetif);
  74. #endif
  75. /* Start toogleLed4 task : Toggle LED4 every 250ms */
  76. // osThreadDef(LED4, ToggleLed4, osPriorityNormal, 0, configMINIMAL_STACK_SIZE*8);
  77. // osThreadCreate(osThread(LED4), NULL);
  78. for( ;; )
  79. {
  80. /* Delete the Init Thread */
  81. osThreadTerminate(NULL);
  82. }
  83. }
  84. /**
  85. * @brief Initializes the lwIP stack
  86. * @param None
  87. * @retval None
  88. */
  89. static void Netif_Config(void)
  90. {
  91. ip_addr_t ipaddr;
  92. ip_addr_t netmask;
  93. ip_addr_t gw;
  94. #ifdef USE_DHCP
  95. ip_addr_set_zero_ip4(&ipaddr);
  96. ip_addr_set_zero_ip4(&netmask);
  97. ip_addr_set_zero_ip4(&gw);
  98. #else
  99. IP_ADDR4(&ipaddr,IP_ADDR0,IP_ADDR1,IP_ADDR2,IP_ADDR3);
  100. IP_ADDR4(&netmask,NETMASK_ADDR0,NETMASK_ADDR1,NETMASK_ADDR2,NETMASK_ADDR3);
  101. IP_ADDR4(&gw,GW_ADDR0,GW_ADDR1,GW_ADDR2,GW_ADDR3);
  102. #endif /* USE_DHCP */
  103. netif_add(&gnetif, &ipaddr, &netmask, &gw, NULL, &ethernetif_init, &tcpip_input);
  104. /* Registers the default network interface. */
  105. netif_set_default(&gnetif);
  106. if (netif_is_link_up(&gnetif))
  107. {
  108. /* When the netif is fully configured this function must be called.*/
  109. netif_set_up(&gnetif);
  110. }
  111. else
  112. {
  113. /* When the netif link is down this function must be called */
  114. netif_set_down(&gnetif);
  115. }
  116. netif_set_link_callback(&gnetif,ethernetif_update_config);
  117. /* create a binary semaphore used for informing ethernetif of frame reception */
  118. osSemaphoreDef(Netif_SEM);
  119. Netif_LinkSemaphore = osSemaphoreCreate(osSemaphore(Netif_SEM) , 1 );
  120. link_arg.netif = &gnetif;
  121. link_arg.semaphore = Netif_LinkSemaphore;
  122. osThreadDef(LinkThr, ethernetif_set_link, osPriorityBelowNormal, 0, configMINIMAL_STACK_SIZE * 2);
  123. osThreadCreate (osThread(LinkThr), &link_arg);
  124. }
  125. /**
  126. * @brief Initializes the LCD and LEDs resources.
  127. * @param None
  128. * @retval None
  129. */
  130. static void BSP_Config(void)
  131. {
  132. }
  133. ///**
  134. // * @brief Toggle LED4 thread
  135. // * @param pvParameters not used
  136. // * @retval None
  137. // */
  138. //static void ToggleLed4(void const * argument)
  139. //{
  140. // for( ;; )
  141. // {
  142. // /* Toggle LED4 each 250ms */
  143. // LOG_PRINT(LOG_INFO,"udp");
  144. // osDelay(100);
  145. // }
  146. //}
  147. ///**
  148. // * @brief EXTI line detection callbacks
  149. // * @param GPIO_Pin: Specifies the pins connected EXTI line
  150. // * @retval None
  151. // */
  152. //void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
  153. //{
  154. // if (GPIO_Pin == GPIO_PIN_14)
  155. // {
  156. // osSemaphoreRelease(Netif_LinkSemaphore);
  157. // }
  158. //}
  159. /**
  160. * @brief System Clock Configuration
  161. * The system Clock is configured as follow :
  162. * System Clock source = PLL (HSE)
  163. * SYSCLK(Hz) = 120000000
  164. * HCLK(Hz) = 120000000
  165. * AHB Prescaler = 1
  166. * APB1 Prescaler = 4
  167. * APB2 Prescaler = 2
  168. * HSE Frequency(Hz) = 25000000
  169. * PLL_M = 25
  170. * PLL_N = 240
  171. * PLL_P = 2
  172. * PLL_Q = 5
  173. * VDD(V) = 3.3
  174. * Flash Latency(WS) = 3
  175. * @param None
  176. * @retval None
  177. */
  178. static void SystemClock_Config(void)
  179. {
  180. RCC_ClkInitTypeDef RCC_ClkInitStruct;
  181. RCC_OscInitTypeDef RCC_OscInitStruct;
  182. /* Enable HSE Oscillator and activate PLL with HSE as source */
  183. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  184. RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  185. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  186. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  187. RCC_OscInitStruct.PLL.PLLM = 25;
  188. RCC_OscInitStruct.PLL.PLLN = 240;
  189. RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  190. RCC_OscInitStruct.PLL.PLLQ = 4;
  191. if(HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  192. {
  193. Error_Handler();
  194. }
  195. /* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2
  196. clocks dividers */
  197. RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);
  198. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  199. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  200. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
  201. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
  202. if(HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_3) != HAL_OK)
  203. {
  204. Error_Handler();
  205. }
  206. }
  207. /**
  208. * @brief This function is executed in case of error occurrence.
  209. * @param None
  210. * @retval None
  211. */
  212. static void Error_Handler(void)
  213. {
  214. /* User may add here some code to deal with this error */
  215. while(1)
  216. {
  217. }
  218. }
  219. void vApplicationMallocFailedHook( void )
  220. {
  221. LOG_PRINT(LOG_ERROR,"malloc error");
  222. }
  223. void vApplicationStackOverflowHook( TaskHandle_t xTask, char *pcTaskName )
  224. {
  225. LogPrint(LOG_ERROR,__FILE__,__FUNCTION__,__LINE__,"task :%s Õ»Òç³ö\r\n",pcTaskName);
  226. }
  227. int fputc(int ch, FILE *stream)
  228. {
  229. return ch;
  230. }
  231. #ifdef USE_FULL_ASSERT
  232. /**
  233. * @brief Reports the name of the source file and the source line number
  234. * where the assert_param error has occurred.
  235. * @param file: pointer to the source file name
  236. * @param line: assert_param error line source number
  237. * @retval None
  238. */
  239. void assert_failed(uint8_t* file, uint32_t line)
  240. {
  241. /* User can add his own implementation to report the file name and line number,
  242. ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  243. /* Infinite loop */
  244. while (1)
  245. {
  246. }
  247. }
  248. #endif
  249. void ethernetif_notify_conn_changed(struct netif *netif)
  250. {
  251. if (netif_is_link_up(netif))
  252. {
  253. LOG_PRINT(LOG_INFO,"link up");
  254. }
  255. else
  256. {
  257. LOG_PRINT(LOG_INFO,"link down");
  258. }
  259. }
  260. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/