main.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /*!
  2. \file main.c
  3. \brief enet demo
  4. \version 2017-02-10, V1.0.0, firmware for GD32F30x
  5. \version 2018-10-10, V1.1.0, firmware for GD32F30x
  6. \version 2018-12-25, V2.0.0, firmware for GD32F30x
  7. */
  8. /*
  9. Copyright (c) 2018, GigaDevice Semiconductor Inc.
  10. All rights reserved.
  11. Redistribution and use in source and binary forms, with or without modification,
  12. are permitted provided that the following conditions are met:
  13. 1. Redistributions of source code must retain the above copyright notice, this
  14. list of conditions and the following disclaimer.
  15. 2. Redistributions in binary form must reproduce the above copyright notice,
  16. this list of conditions and the following disclaimer in the documentation
  17. and/or other materials provided with the distribution.
  18. 3. Neither the name of the copyright holder nor the names of its contributors
  19. may be used to endorse or promote products derived from this software without
  20. specific prior written permission.
  21. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  22. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  23. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  24. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  25. INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  26. NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  27. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  28. WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  29. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  30. OF SUCH DAMAGE.
  31. */
  32. #include "gd32f30x.h"
  33. #include "hd_eth.h"
  34. #include "main.h"
  35. #include "tcpip.h"
  36. #include "hello_gigadevice.h"
  37. #include "tcp_client.h"
  38. #include "led.h"
  39. #include "usart.h"
  40. #include "string.h"
  41. #include <stdio.h>
  42. #include <stdlib.h>
  43. #include "netconf.h"
  44. #include "sys_mqtt.h"
  45. #include "tcp_server.h"
  46. #include "log.h"
  47. #include "gd32_flash.h"
  48. #include "timer.h"
  49. #include "wwdgt.h"
  50. #include "sys_http.h"
  51. #include "data_task.h"
  52. #include "mmodbus.h"
  53. /*
  54. *freertos优先级管理
  55. *管理启动流程任务 init_task 优先级 1
  56. *freertos时间任务 prvTimerTask 优先级 2
  57. *上位机交互任务 tcp_server 优先级 1
  58. *可能会开启的任务 dhcp_task 优先级 4
  59. *mqtt通信任务 mqtt_publish_task 优先级 4
  60. *以太网输入任务 ethernetif_input 优先级 5
  61. *数据处理任务 data_task 优先级 3
  62. *OTA升级任务 updata_task 优先级 5
  63. */
  64. #define INIT_TASK_PRIO ( tskIDLE_PRIORITY + 1 )
  65. #define DHCP_TASK_PRIO ( tskIDLE_PRIORITY + 4 )
  66. #define LED_TASK_PRIO ( tskIDLE_PRIORITY + 2 )
  67. void led_task(void * pvParameters);
  68. void init_task(void * pvParameters);
  69. void readID(char *strId);
  70. char gatewayId[11];
  71. /*!
  72. \brief main function
  73. \param[in] none
  74. \param[out] none
  75. \retval none
  76. */
  77. int main(void)
  78. {
  79. readID(gatewayId);
  80. /* remap PB3 PB4 */
  81. rcu_periph_clock_enable(RCU_AF);// 使能映射功能的时钟
  82. gpio_pin_remap_config(GPIO_SWJ_SWDPENABLE_REMAP, ENABLE);// 重映射PB3、PB4
  83. /* LED init */
  84. gd_eval_led_init(LED_DEV); // 状态灯
  85. gd_eval_led_init(LED_485RX); // 485 RX 通信灯
  86. gd_eval_led_init(LED_485TX); // 485 TX 通信灯
  87. /* configure 4 bits pre-emption priority */
  88. nvic_priority_group_set(NVIC_PRIGROUP_PRE4_SUB0);
  89. /* 485 init */
  90. gd_485_DE_pin_init();
  91. config_485_port(COM_485,9600,8,1,0);
  92. printf("start\n");
  93. /* timer init */
  94. timer5_init();
  95. // 02设备直连电源时需要延迟
  96. //delay_ms(50);
  97. /* FWDGT init */
  98. watchdog_init();
  99. /* LED_DEV open */
  100. gd_eval_led_on(LED_DEV);
  101. /* init task */
  102. xTaskCreate(init_task, "INIT",256, NULL, INIT_TASK_PRIO, NULL);
  103. /* start scheduler */
  104. vTaskStartScheduler();
  105. while(1)
  106. {
  107. }
  108. }
  109. /*!
  110. \brief init task
  111. \param[in] pvParameters not used
  112. \param[out] none
  113. \retval none
  114. */
  115. void init_task(void * pvParameters)
  116. {
  117. /* configure ethernet (GPIOs, clocks, MAC, DMA) */
  118. enet_system_setup();
  119. /* initilaize the LwIP stack */
  120. lwip_stack_init();
  121. log_init();
  122. /* initilaize the tcp server: telnet 8000 */
  123. // hello_gigadevice_init();
  124. // /* initilaize the tcp client: echo 1026 */
  125. // tcp_client_init();
  126. /* initilaize the udp: echo 1025 */
  127. tcp_server_init();
  128. #ifdef USE_DHCP
  129. /* start DHCP client */
  130. xTaskCreate(dhcp_task, "DHCP", 512, NULL, DHCP_TASK_PRIO, NULL);
  131. #endif /* USE_DHCP */
  132. /* start toogle LED task every 250ms */
  133. // xTaskCreate(led_task, "LED", 512, NULL, LED_TASK_PRIO, NULL);
  134. char* buf = pvPortMalloc(10);
  135. while(read_data_from_flash(buf)){};
  136. vPortFree(buf);
  137. http_getDemo();
  138. xTaskCreate(data_task, "DATA",512, NULL, DATA_TASK_PRIO, NULL);
  139. mqtt_task_creat();
  140. for( ;; ){
  141. vTaskDelete(NULL);
  142. }
  143. }
  144. /*!
  145. \brief led task
  146. \param[in] pvParameters not used
  147. \param[out] none
  148. \retval none
  149. */
  150. // 为了看到闪烁的效果,把485的一些数据丢掉
  151. void led_task(void * pvParameters)
  152. {
  153. for( ;; ){
  154. // /* toggle LED3 each 250ms */
  155. // gd_eval_led_toggle(LED_DEV);
  156. vTaskDelay(100);
  157. }
  158. }
  159. void vApplicationStackOverflowHook(TaskHandle_t xTask, char *pcTaskName )
  160. {
  161. LogPrint(LOG_ERROR,__FILE__,__FUNCTION__,__LINE__,"task :%s 栈溢出\r\n",pcTaskName);
  162. }
  163. void vApplicationHeapOverflowHook(TaskHandle_t xTask, char *pcTaskName )
  164. {
  165. LogPrint(LOG_ERROR,__FILE__,__FUNCTION__,__LINE__,"task :%s 堆溢出\r\n",pcTaskName);
  166. }
  167. void vApplicationMallocFailedHook()
  168. {
  169. LogPrint(LOG_ERROR,__FILE__,__FUNCTION__,__LINE__,"malloc error");
  170. }
  171. /*
  172. * 函数名:void readID(char *strId)
  173. * 输入参数:无
  174. * 输出参数:strId
  175. * 返回值:无
  176. * 函数作用:读取长度12的芯片Id并组合从中取出对应字符防止其重复
  177. */
  178. void readID(char *strId)
  179. {
  180. static const char *hex_table = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMN";
  181. uint32_t CpuID1, CpuID2, CpuID3;
  182. CpuID1 = *(volatile uint32_t *)(0x1FFFF7E8);
  183. CpuID2 = *(volatile uint32_t *)(0x1FFFF7EC);
  184. CpuID3 = *(volatile uint32_t *)(0x1FFFF7F0);
  185. strId[0] = 'D';
  186. strId[1] = 'T';
  187. for (uint8_t i = 0, j = 0; i < 8; i += 2)
  188. {
  189. uint32_t sum1 = (CpuID1 >> ((j % 4) * 8)) & 0xFF; // 按字节取出
  190. uint8_t index1 = sum1 / 16;
  191. uint8_t remainder1 = sum1 % 16;
  192. uint32_t sum2 = (CpuID2 >> ((j % 4) * 8)) & 0xFF; // 按字节取出
  193. uint8_t index2 = sum2 / 16;
  194. uint8_t remainder2 = sum2 % 16;
  195. uint32_t sum3 = (CpuID3 >> ((j % 4) * 8)) & 0xFF; // 按字节取出
  196. uint8_t index3 = sum3 / 16;
  197. uint8_t remainder3 = sum3 % 16;
  198. strId[i + 2] = hex_table[index1 + index2 + index3];
  199. strId[i + 1 + 2] = hex_table[remainder1 + remainder2 + remainder3];
  200. j++;
  201. }
  202. strId[10] = '\0';
  203. }