main.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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. volatile uint32_t nandid=NAND_ReadID();
  47. // NAND_Format();
  48. char *p=malloc(10);
  49. sprintf(p,"hello");
  50. write_file("device.txt",p,strlen(p));
  51. SRAM_Test();
  52. /* Init task */
  53. osThreadDef(Start, StartThread, osPriorityNormal, 0, configMINIMAL_STACK_SIZE * 4);
  54. osThreadCreate (osThread(Start), NULL);
  55. /* Start scheduler */
  56. osKernelStart();
  57. /* We should never get here as control is now taken by the scheduler */
  58. for( ;; );
  59. }
  60. /**
  61. * @brief Start Thread
  62. * @param argument not used
  63. * @retval None
  64. */
  65. static void StartThread(void const * argument)
  66. {
  67. /* Initialize LCD and LEDs */
  68. BSP_Config();
  69. log_init();
  70. /* Create tcp_ip stack thread */
  71. tcpip_init(NULL, NULL);
  72. /* Initialize the LwIP stack */
  73. Netif_Config();
  74. /* Notify user about the network interface config */
  75. User_notification(&gnetif);
  76. #ifdef USE_DHCP
  77. /* Start DHCPClient */
  78. osThreadDef(DHCP, DHCP_thread, osPriorityBelowNormal, 0, configMINIMAL_STACK_SIZE * 2);
  79. osThreadCreate (osThread(DHCP), &gnetif);
  80. #endif
  81. /* Start toogleLed4 task : Toggle LED4 every 250ms */
  82. osThreadDef(LED4, ToggleLed4, osPriorityNormal, 0, configMINIMAL_STACK_SIZE*8);
  83. osThreadCreate(osThread(LED4), NULL);
  84. for( ;; )
  85. {
  86. /* Delete the Init Thread */
  87. osThreadTerminate(NULL);
  88. }
  89. }
  90. /**
  91. * @brief Initializes the lwIP stack
  92. * @param None
  93. * @retval None
  94. */
  95. static void Netif_Config(void)
  96. {
  97. ip_addr_t ipaddr;
  98. ip_addr_t netmask;
  99. ip_addr_t gw;
  100. #ifdef USE_DHCP
  101. ip_addr_set_zero_ip4(&ipaddr);
  102. ip_addr_set_zero_ip4(&netmask);
  103. ip_addr_set_zero_ip4(&gw);
  104. #else
  105. IP_ADDR4(&ipaddr,IP_ADDR0,IP_ADDR1,IP_ADDR2,IP_ADDR3);
  106. IP_ADDR4(&netmask,NETMASK_ADDR0,NETMASK_ADDR1,NETMASK_ADDR2,NETMASK_ADDR3);
  107. IP_ADDR4(&gw,GW_ADDR0,GW_ADDR1,GW_ADDR2,GW_ADDR3);
  108. #endif /* USE_DHCP */
  109. netif_add(&gnetif, &ipaddr, &netmask, &gw, NULL, &ethernetif_init, &tcpip_input);
  110. /* Registers the default network interface. */
  111. netif_set_default(&gnetif);
  112. if (netif_is_link_up(&gnetif))
  113. {
  114. /* When the netif is fully configured this function must be called.*/
  115. netif_set_up(&gnetif);
  116. }
  117. else
  118. {
  119. /* When the netif link is down this function must be called */
  120. netif_set_down(&gnetif);
  121. }
  122. }
  123. /**
  124. * @brief Initializes the LCD and LEDs resources.
  125. * @param None
  126. * @retval None
  127. */
  128. static void BSP_Config(void)
  129. {
  130. }
  131. /**
  132. * @brief Toggle LED4 thread
  133. * @param pvParameters not used
  134. * @retval None
  135. */
  136. static void ToggleLed4(void const * argument)
  137. {
  138. for( ;; )
  139. {
  140. /* Toggle LED4 each 250ms */
  141. LOG_PRINT(LOG_INFO,"udp");
  142. osDelay(100);
  143. }
  144. }
  145. ///**
  146. // * @brief EXTI line detection callbacks
  147. // * @param GPIO_Pin: Specifies the pins connected EXTI line
  148. // * @retval None
  149. // */
  150. //void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
  151. //{
  152. // if (GPIO_Pin == GPIO_PIN_14)
  153. // {
  154. // osSemaphoreRelease(Netif_LinkSemaphore);
  155. // }
  156. //}
  157. /**
  158. * @brief System Clock Configuration
  159. * The system Clock is configured as follow :
  160. * System Clock source = PLL (HSE)
  161. * SYSCLK(Hz) = 120000000
  162. * HCLK(Hz) = 120000000
  163. * AHB Prescaler = 1
  164. * APB1 Prescaler = 4
  165. * APB2 Prescaler = 2
  166. * HSE Frequency(Hz) = 25000000
  167. * PLL_M = 25
  168. * PLL_N = 240
  169. * PLL_P = 2
  170. * PLL_Q = 5
  171. * VDD(V) = 3.3
  172. * Flash Latency(WS) = 3
  173. * @param None
  174. * @retval None
  175. */
  176. static void SystemClock_Config(void)
  177. {
  178. RCC_ClkInitTypeDef RCC_ClkInitStruct;
  179. RCC_OscInitTypeDef RCC_OscInitStruct;
  180. /* Enable HSE Oscillator and activate PLL with HSE as source */
  181. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  182. RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  183. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  184. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  185. RCC_OscInitStruct.PLL.PLLM = 25;
  186. RCC_OscInitStruct.PLL.PLLN = 240;
  187. RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  188. RCC_OscInitStruct.PLL.PLLQ = 4;
  189. if(HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  190. {
  191. Error_Handler();
  192. }
  193. /* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2
  194. clocks dividers */
  195. RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);
  196. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  197. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  198. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
  199. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
  200. if(HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_3) != HAL_OK)
  201. {
  202. Error_Handler();
  203. }
  204. }
  205. /**
  206. * @brief This function is executed in case of error occurrence.
  207. * @param None
  208. * @retval None
  209. */
  210. static void Error_Handler(void)
  211. {
  212. /* User may add here some code to deal with this error */
  213. while(1)
  214. {
  215. }
  216. }
  217. void vApplicationMallocFailedHook( void )
  218. {
  219. LOG_PRINT(LOG_ERROR,"malloc error");
  220. }
  221. void vApplicationStackOverflowHook( TaskHandle_t xTask, char *pcTaskName )
  222. {
  223. LogPrint(LOG_ERROR,__FILE__,__FUNCTION__,__LINE__,"task :%s Õ»Òç³ö\r\n",pcTaskName);
  224. }
  225. int fputc(int ch, FILE *stream)
  226. {
  227. return ch;
  228. }
  229. #ifdef USE_FULL_ASSERT
  230. /**
  231. * @brief Reports the name of the source file and the source line number
  232. * where the assert_param error has occurred.
  233. * @param file: pointer to the source file name
  234. * @param line: assert_param error line source number
  235. * @retval None
  236. */
  237. void assert_failed(uint8_t* file, uint32_t line)
  238. {
  239. /* User can add his own implementation to report the file name and line number,
  240. ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  241. /* Infinite loop */
  242. while (1)
  243. {
  244. }
  245. }
  246. #endif
  247. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/