main.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  1. #include "main.h"
  2. #include "ethernetif.h"
  3. #include "lwip/netif.h"
  4. #include "lwip/tcpip.h"
  5. #include "lwip/dns.h"
  6. #include "app_ethernet.h"
  7. #include "log.h"
  8. #include "bsp_fsmc_sram.h"
  9. #include "bsp_fsmc_nandflash.h"
  10. #include "nandflash.h"
  11. #include "myFile.h"
  12. #include "string.h"
  13. #include "data_task.h"
  14. #include "tcp_server.h"
  15. #include "usart_server.h"
  16. #include "timer.h"
  17. #include "usart.h"
  18. #include "sys_http.h"
  19. #include "sys_mqtt.h"
  20. #include "data_task.h"
  21. #include "mmodbus.h"
  22. #include "led.h"
  23. #include "lte.h"
  24. #include "iwdg.h"
  25. #include "CJT188.h"
  26. #include "sys_ping.h"
  27. #include "reset.h"
  28. #include "lwip/dhcp.h"
  29. #include "master_example.h"
  30. #include "slave_example.h"
  31. #include "simple_server.h"
  32. /* Private typedef -----------------------------------------------------------*/
  33. /* Private define ------------------------------------------------------------*/
  34. /* Private macro -------------------------------------------------------------*/
  35. /* Private variables ---------------------------------------------------------*/
  36. struct netif gnetif; /* network interface structure */
  37. /* Semaphore to signal Ethernet Link state update */
  38. osSemaphoreId Netif_LinkSemaphore = NULL;
  39. /* Ethernet link thread Argument */
  40. struct link_str link_arg;
  41. /* Private function prototypes -----------------------------------------------*/
  42. static void SystemClock_Config(void);
  43. static void StartThread(void const * argument);
  44. static void ToggleLed4(void const * argument);
  45. static void BSP_Config(void);
  46. static void Netif_Config(void);
  47. void EC800M_taskFuntcion(void const*arg);
  48. void write_config(void const * argument);
  49. void load_unique(void);
  50. /* Private functions ---------------------------------------------------------*/
  51. char gatewayId[11];
  52. char ETH_flag = 1; // 1,启用网线,0 启用LTE
  53. uint16_t data[2];
  54. /**
  55. * @brief Main program
  56. * @param None
  57. * @retval None
  58. */
  59. int main(void)
  60. {
  61. int status;
  62. /* STM32F2xx HAL library initialization:
  63. - Configure the Flash prefetch, instruction and Data caches
  64. - Configure the Systick to generate an interrupt each 1 msec
  65. - Set NVIC Group Priority to 4
  66. - Global MSP (MCU Support Package) initialization
  67. */
  68. HAL_Init();
  69. load_unique();
  70. sprintf(gatewayId,"DTtest0001");//DT8pd3ac6h DTbma5ac6h DTtest0001 DTrzts0001
  71. /* Configure the system clock to 120 MHz */
  72. SystemClock_Config();
  73. MX_FSMC_SRAM_Init();
  74. status = NAND_Init();
  75. while(status){
  76. NAND_Format();
  77. status = NAND_Init();
  78. }
  79. TIMER_config();
  80. my_mem_init(SRAMEX);
  81. my_mem_init(SRAMIN);
  82. USART_232_config();
  83. USART_485_config();
  84. USART_485_DE_TX();
  85. printf("hello\n");
  86. // IWDG_Configuration();
  87. LED_GPIO_Config(); // 339 loraRX
  88. // NET_STATUS_LED_Config();// 339 设备指示灯
  89. resetConfig();// 339 loraTX
  90. // uint8_t args;
  91. // void const* arg = &args;
  92. // iec_master_test(arg);
  93. // iec_client_test();
  94. // osThreadDef(master, iec_master_test, osPriorityNormal, 0, 1024);
  95. // osThreadCreate (osThread(master), NULL);
  96. osThreadDef(client, iec_client_test, osPriorityNormal, 0, 1024);
  97. osThreadCreate (osThread(client), NULL);
  98. read_file("ETH.txt", &ETH_flag);
  99. DeleteDirFile("ETH.txt");
  100. // if(ETH_flag == 1)// 网线启动
  101. // {
  102. // /* Init task */
  103. // osThreadDef(Start, StartThread, osPriorityNormal, 0, configMINIMAL_STACK_SIZE * 4);
  104. // osThreadCreate (osThread(Start), NULL);
  105. // }
  106. // else // LTE启动
  107. // {
  108. // DEBUG_USART_Config();
  109. // USART_DMA_Config();
  110. // /* LTE task */
  111. // osThreadDef(LTE_task, EC800M_taskFuntcion, osPriorityNormal, 0, configMINIMAL_STACK_SIZE * 4);
  112. // osThreadCreate (osThread(LTE_task), NULL);
  113. // }
  114. /* Start scheduler */
  115. osKernelStart();
  116. /* We should never get here as control is now taken by the scheduler */
  117. for( ;; );
  118. }
  119. /**
  120. * @brief Start Thread
  121. * @param argument not used
  122. * @retval None
  123. */
  124. static void StartThread(void const * argument)
  125. {
  126. /* Initialize LCD and LEDs */
  127. BSP_Config();
  128. log_init();
  129. /* Create tcp_ip stack thread */
  130. tcpip_init(NULL, NULL);
  131. /* Initialize the LwIP stack */
  132. Netif_Config();
  133. /* Notify user about the network interface config */
  134. User_notification(&gnetif);
  135. reset_task_creat();
  136. tcp_server_init();
  137. //#ifdef USE_DHCP
  138. /* Start DHCPClient */
  139. osThreadDef(DHCP, DHCP_thread, osPriorityNormal, 0, configMINIMAL_STACK_SIZE * 2);
  140. osThreadCreate (osThread(DHCP), &gnetif);
  141. //#endif
  142. /* Start toogleLed4 task : Toggle LED4 every 250ms */
  143. // osThreadDef(LED4, ToggleLed4, osPriorityNormal, 0, configMINIMAL_STACK_SIZE*8);
  144. // osThreadCreate(osThread(LED4), NULL);
  145. #ifdef USE_DHCP
  146. // 确保dhcp配置完成
  147. while(dhcp_done != 1)
  148. {
  149. feedDog();
  150. vTaskDelay(100);
  151. }
  152. #endif
  153. // http_getDemo();
  154. // mqtt_task_creat();
  155. //
  156. // osThreadDef(DataTask, data_task, osPriorityNormal, 0, configMINIMAL_STACK_SIZE * 8);
  157. // osThreadCreate (osThread(DataTask), NULL);
  158. //
  159. // ETH_PingWork("192.168.0.2");
  160. // osThreadDef(client, cs104_server, osPriorityNormal, 0, 1024);
  161. // osThreadCreate (osThread(client), NULL);
  162. for( ;; )
  163. {
  164. /* Delete the Init Thread */
  165. osThreadTerminate(NULL);
  166. }
  167. }
  168. /**
  169. * @brief Initializes the lwIP stack
  170. * @param None
  171. * @retval None
  172. */
  173. static void Netif_Config(void)
  174. {
  175. ip_addr_t ipaddr;
  176. ip_addr_t netmask;
  177. ip_addr_t gw;
  178. ip_addr_t dns_server;
  179. #ifdef USE_DHCP
  180. ip_addr_set_zero_ip4(&ipaddr);
  181. ip_addr_set_zero_ip4(&netmask);
  182. ip_addr_set_zero_ip4(&gw);
  183. #else
  184. IP_ADDR4(&ipaddr,IP_ADDR0,IP_ADDR1,IP_ADDR2,IP_ADDR3);
  185. IP_ADDR4(&netmask,NETMASK_ADDR0,NETMASK_ADDR1,NETMASK_ADDR2,NETMASK_ADDR3);
  186. IP_ADDR4(&gw,GW_ADDR0,GW_ADDR1,GW_ADDR2,GW_ADDR3);
  187. // 设置DNS服务器地址
  188. IP4_ADDR(&dns_server, DNS_SERVER_ADDR0, DNS_SERVER_ADDR1, DNS_SERVER_ADDR2, DNS_SERVER_ADDR3);
  189. dns_setserver(0, &dns_server); // 设置第一个DNS服务器地址
  190. #endif /* USE_DHCP */
  191. netif_add(&gnetif, &ipaddr, &netmask, &gw, NULL, &ethernetif_init, &tcpip_input);
  192. /* Registers the default network interface. */
  193. netif_set_default(&gnetif);
  194. if (netif_is_link_up(&gnetif))
  195. {
  196. /* When the netif is fully configured this function must be called.*/
  197. netif_set_up(&gnetif);
  198. }
  199. else
  200. {
  201. /* When the netif link is down this function must be called */
  202. netif_set_down(&gnetif);
  203. }
  204. dhcp_stop(&gnetif);
  205. netif_set_link_callback(&gnetif,ethernetif_update_config);
  206. /* create a binary semaphore used for informing ethernetif of frame reception */
  207. osSemaphoreDef(Netif_SEM);
  208. Netif_LinkSemaphore = osSemaphoreCreate(osSemaphore(Netif_SEM) , 1 );
  209. link_arg.netif = &gnetif;
  210. link_arg.semaphore = Netif_LinkSemaphore;
  211. osThreadDef(LinkThr, ethernetif_set_link, osPriorityBelowNormal, 0, configMINIMAL_STACK_SIZE * 2);
  212. osThreadCreate (osThread(LinkThr), &link_arg);
  213. }
  214. void set_ipaddr(char* buf)
  215. {
  216. ip_addr_t ipaddr;
  217. ip_addr_t netmask;
  218. ip_addr_t gw;
  219. int ipaddr0,ipaddr1,ipaddr2,ipaddr3;
  220. int maskaddr0,maskaddr1,maskaddr2,maskaddr3;
  221. int gwaddr0,gwaddr1,gwaddr2,gwaddr3;
  222. ipaddr0 = parseIntField(buf,"\"ipv4\":\"" );
  223. ipaddr1 = parseIntField(strstr(buf,"\"ipv4\":\"") + 1,"." );
  224. ipaddr2 = parseIntField(strstr(strstr(buf,"\"ipv4\":\"") + 1,"." ) + 1,".");
  225. ipaddr3 = parseIntField(strstr(strstr(strstr(buf,"\"ipv4\":\"") + 1,"." ) + 1,".") + 1 ,".");
  226. maskaddr0 = parseIntField(buf,"\"subnetMask\":\"" );
  227. maskaddr1 = parseIntField(strstr(buf,"\"subnetMask\":\"") + 1,"." );
  228. maskaddr2 = parseIntField(strstr(strstr(buf,"\"subnetMask\":\"") + 1,"." ) + 1,".");
  229. maskaddr3 = parseIntField(strstr(strstr(strstr(buf,"\"subnetMask\":\"") + 1,"." ) + 1,".") + 1 ,".");
  230. gwaddr0 = parseIntField(buf,"\"defaultGateway\":\"" );
  231. gwaddr1 = parseIntField(strstr(buf,"\"defaultGateway\":\"") + 1,"." );
  232. gwaddr2 = parseIntField(strstr(strstr(buf,"\"defaultGateway\":\"") + 1,"." ) + 1,".");
  233. gwaddr3 = parseIntField(strstr(strstr(strstr(buf,"\"defaultGateway\":\"") + 1,"." ) + 1,".") + 1 ,".");
  234. IP4_ADDR(&ipaddr, ipaddr0 ,ipaddr1 , ipaddr2 , ipaddr3 );
  235. IP4_ADDR(&netmask, maskaddr0, maskaddr1, maskaddr2, maskaddr3);
  236. IP4_ADDR(&gw, gwaddr0, gwaddr1, gwaddr2,gwaddr3);
  237. //#ifdef USE_DHCP
  238. // 停止DHCP客户端
  239. DHCP_close();
  240. //#endif
  241. // 禁用网口
  242. netif_set_down(&gnetif);
  243. // 设置网络接口的IP地址
  244. // netif_set_gw(&gnetif, &gw);
  245. // netif_set_netmask(&gnetif, &netmask);
  246. // netif_set_ipaddr(&gnetif, &ipaddr);
  247. netif_set_addr(&gnetif, &ipaddr , &netmask, &gw);
  248. // 重启网络接口以使更改生效
  249. netif_set_up(&gnetif);
  250. }
  251. /**
  252. * @brief Initializes the LCD and LEDs resources.
  253. * @param None
  254. * @retval None
  255. */
  256. static void BSP_Config(void)
  257. {
  258. }
  259. ///**
  260. // * @brief Toggle LED4 thread
  261. // * @param pvParameters not used
  262. // * @retval None
  263. // */
  264. //static void ToggleLed4(void const * argument)
  265. //{
  266. // for( ;; )
  267. // {
  268. // /* Toggle LED4 each 250ms */
  269. // LOG_PRINT(LOG_INFO,"udp");
  270. // osDelay(100);
  271. // }
  272. //}
  273. ///**
  274. // * @brief EXTI line detection callbacks
  275. // * @param GPIO_Pin: Specifies the pins connected EXTI line
  276. // * @retval None
  277. // */
  278. //void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
  279. //{
  280. // if (GPIO_Pin == GPIO_PIN_14)
  281. // {
  282. // osSemaphoreRelease(Netif_LinkSemaphore);
  283. // }
  284. //}
  285. /**
  286. * @brief System Clock Configuration
  287. * The system Clock is configured as follow :
  288. * System Clock source = PLL (HSE)
  289. * SYSCLK(Hz) = 120000000
  290. * HCLK(Hz) = 120000000
  291. * AHB Prescaler = 1
  292. * APB1 Prescaler = 4
  293. * APB2 Prescaler = 2
  294. * HSE Frequency(Hz) = 25000000
  295. * PLL_M = 25
  296. * PLL_N = 240
  297. * PLL_P = 2
  298. * PLL_Q = 5
  299. * VDD(V) = 3.3
  300. * Flash Latency(WS) = 3
  301. * @param None
  302. * @retval None
  303. */
  304. static void SystemClock_Config(void)
  305. {
  306. RCC_ClkInitTypeDef RCC_ClkInitStruct;
  307. RCC_OscInitTypeDef RCC_OscInitStruct;
  308. /* Enable HSE Oscillator and activate PLL with HSE as source */
  309. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  310. RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  311. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  312. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  313. RCC_OscInitStruct.PLL.PLLM = 25;
  314. RCC_OscInitStruct.PLL.PLLN = 240;
  315. RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  316. RCC_OscInitStruct.PLL.PLLQ = 4;
  317. if(HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  318. {
  319. Error_Handler();
  320. }
  321. /* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2
  322. clocks dividers */
  323. RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);
  324. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  325. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  326. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
  327. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
  328. if(HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_3) != HAL_OK)
  329. {
  330. Error_Handler();
  331. }
  332. }
  333. /**
  334. * @brief This function is executed in case of error occurrence.
  335. * @param None
  336. * @retval None
  337. */
  338. static void Error_Handler(void)
  339. {
  340. /* User may add here some code to deal with this error */
  341. while(1)
  342. {
  343. }
  344. }
  345. void vApplicationMallocFailedHook( void )
  346. {
  347. LOG_PRINT(LOG_ERROR,"malloc error");
  348. }
  349. void vApplicationStackOverflowHook( TaskHandle_t xTask, char *pcTaskName )
  350. {
  351. LogPrint(LOG_ERROR,__FILE__,__FUNCTION__,__LINE__,"task :%s 栈溢出\r\n",pcTaskName);
  352. }
  353. //int fputc(int ch, FILE *stream)
  354. //{
  355. // return ch;
  356. //}
  357. #ifdef USE_FULL_ASSERT
  358. /**
  359. * @brief Reports the name of the source file and the source line number
  360. * where the assert_param error has occurred.
  361. * @param file: pointer to the source file name
  362. * @param line: assert_param error line source number
  363. * @retval None
  364. */
  365. void assert_failed(uint8_t* file, uint32_t line)
  366. {
  367. /* User can add his own implementation to report the file name and line number,
  368. ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  369. /* Infinite loop */
  370. while (1)
  371. {
  372. }
  373. }
  374. #endif
  375. void ethernetif_notify_conn_changed(struct netif *netif)
  376. {
  377. if (netif_is_link_up(netif))
  378. {
  379. LOG_PRINT(LOG_INFO,"link up");
  380. }
  381. else
  382. {
  383. LOG_PRINT(LOG_INFO,"link down");
  384. }
  385. }
  386. void load_unique(void)
  387. {
  388. char *hex_table = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMN";
  389. uint32_t CpuID1, CpuID2, CpuID3;
  390. HAL_GetUIDw0();
  391. CpuID1 = *(volatile uint32_t *)(UID_BASE);
  392. CpuID2 = *(volatile uint32_t *)(UID_BASE+8);
  393. CpuID3 = *(volatile uint32_t *)(UID_BASE+16);
  394. char strId[11];
  395. for (uint8_t i = 0, j = 0; i < 8; i += 2)
  396. {
  397. uint32_t sum1 = (CpuID1 >> ((j % 4) * 8)) & 0xFF; // 按字节取出
  398. uint8_t index1 = sum1 / 16;
  399. uint8_t remainder1 = sum1 % 16;
  400. uint32_t sum2 = (CpuID2 >> ((j % 4) * 8)) & 0xFF; // 按字节取出
  401. uint8_t index2 = sum2 / 16;
  402. uint8_t remainder2 = sum2 % 16;
  403. uint32_t sum3 = (CpuID3 >> ((j % 4) * 8)) & 0xFF; // 按字节取出
  404. uint8_t index3 = sum3 / 16;
  405. uint8_t remainder3 = sum3 % 16;
  406. strId[i + 2] = hex_table[index1 + index2 + index3];
  407. strId[i + 1 + 2] = hex_table[remainder1 + remainder2 + remainder3];
  408. j++;
  409. }
  410. strId[10] = '\0';
  411. strId[0]='L';
  412. strId[1]='R';
  413. strcpy(gatewayId,strId);
  414. }
  415. /*
  416. * 函数名:void EC800M_taskFuntcion(void *arg)
  417. * 输入参数:无
  418. * 输出参数:无
  419. * 返回值:无
  420. * 函数作用:开启EC800M模块任务
  421. */
  422. void EC800M_taskFuntcion(void const*arg)
  423. {
  424. // /* Create tcp_ip stack thread */
  425. // tcpip_init(NULL, NULL);
  426. //
  427. // /* Initialize the LwIP stack */
  428. // Netif_Config();
  429. EC800M_open();
  430. ec800_init();
  431. ec800_TCP();
  432. char *http=mymalloc(SRAMEX,80); // 根据要访问的 URL修改分配的大小
  433. sprintf(http,"http://gpu.ringzle.com:8082/iot/transmit/getTransmitConfig/%s",gatewayId);
  434. // sprintf(http,"http://gpu.ringzle.com:8082/witcarbon-admin/profile/2024/03/25/7935dfd8-b398-41ba-85a3-8f04a6f1625e.bin");
  435. http_set_url(http);
  436. myfree(SRAMEX,http);
  437. LTE_HTTP_get();
  438. MQ_threadCreate();
  439. osThreadDef(DataTask, data_task, osPriorityNormal, 0, configMINIMAL_STACK_SIZE * 8);
  440. osThreadCreate (osThread(DataTask), NULL);
  441. for( ;; )
  442. {
  443. /* Delete the LTE Thread */
  444. osThreadTerminate(NULL);
  445. }
  446. }
  447. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/