main.c 13 KB

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