main.c 13 KB

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