main.c 13 KB

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