main.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /*!
  2. \file main.c
  3. \brief led spark with systick, USART print and key example
  4. \version 2014-12-26, V1.0.0, firmware for GD32F10x
  5. \version 2017-06-20, V2.0.0, firmware for GD32F10x
  6. \version 2018-07-31, V2.1.0, firmware for GD32F10x
  7. \version 2020-09-30, V2.2.0, firmware for GD32F10x
  8. */
  9. /*
  10. Copyright (c) 2020, GigaDevice Semiconductor Inc.
  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 "gd32f10x.h"
  33. #include "systick.h"
  34. #include <stdio.h>
  35. #include "main.h"
  36. #include "usart.h"
  37. #include "led.h"
  38. #include "config.h"
  39. #include "log.h"
  40. #include "ec800m.h"
  41. #include "parseDeviceMessage.h"
  42. #include "otaEvent.h"
  43. #include "systick.h"
  44. #include "w25q32.h"
  45. #include "key.h"
  46. #define UNIQUE_ID 0x1FFFF7E8
  47. #define VERSION "V1.0.2" // 版本号写死、通过人工手动控制
  48. #define GATEWAYNAME "DTU"
  49. #define GATEWAYMODE "4G" // 后续此mode可以切换4g
  50. void MQTT_Alarm_clock(uint32_t time, GATEWAY_PARAMS *gateway);
  51. void readID(char *strId);
  52. void http_load_config(uint8_t *httpURL);
  53. void fwdgt_init();
  54. void task_fwdgt_reload();
  55. int main(void)
  56. {
  57. /* configure systick */
  58. rcu_osci_on(RCU_HXTAL);
  59. systick_config();
  60. nvic_config();
  61. /* initilize the LEDs, USART */
  62. // W25Q32_Init(); //弃用w25q32,改用片内flash存储
  63. gd_eval_led_init(LED1);
  64. gd_com_init(COM_232);
  65. gd_com_init(COM_EC800);
  66. dma_config();
  67. gd_EC800M_pin_init();
  68. gd_eval_key_init(KEY_SYS_RECOVERY, KEY_MODE_EXTI);
  69. EC800MPwoerOn();
  70. EC800MWaitReady();
  71. EC800MSetPDP();
  72. char *strID = malloc(24);
  73. readID(strID);
  74. //加载网关信息
  75. GATEWAY_PARAMS *gateway;
  76. gateway = get_gateway_config_params();
  77. strcpy((char *)&gateway->gatewayId, strID);
  78. strcpy((char *)&gateway->gatewayMode, GATEWAYMODE);
  79. strcpy((char *)&gateway->gateName,GATEWAYNAME);
  80. char *httpURL = malloc(100);
  81. memset(httpURL, 0, 100);
  82. sprintf(httpURL, "http://gpu.ringzle.com:8082/iot/transmit/getTransmitConfig/%s",strID);
  83. http_load_config(httpURL);
  84. free(httpURL);
  85. httpURL=NULL;
  86. gateway = get_gateway_config_params();
  87. strcpy((char *)&gateway->gatewayId, strID);
  88. strcpy((char *)&gateway->gatewayMode, GATEWAYMODE);
  89. strcpy((char *)&gateway->gateName,GATEWAYNAME);
  90. free(strID);
  91. config_485_port(COM_485, gateway->baudrate, gateway->dataBits, gateway->stopBit, gateway->checkBit);
  92. gd_485_DE_pin_init();
  93. gd_485_DE_tx();
  94. ConnectMQTTSever(gateway->host, gateway->port, 60, gateway->deviceId);
  95. MQTTSubTopic(gateway->commandTopic);
  96. ring_buffer_init(&mqttRecv);
  97. mmodbus_init(1);
  98. dlt645_init(1);
  99. //开启看门狗
  100. // fwdgt_init();
  101. while (1)
  102. {
  103. check_ota_event(); //检测是否有ota事件产生
  104. MQTT_Alarm_clock(gateway->inboundTime, gateway);
  105. }
  106. }
  107. /*
  108. * 函数名:void MQTT_Alarm_clock(uint32_t time)
  109. * 输入参数:time设定MQTT多久进行一次上传
  110. * 输出参数:无
  111. * 返回值:无
  112. * 函数作用:按照设定好时间进行执行,在未到达指定时间之前重复做led反转、MQTT订阅的主题是否有下发主题信息判断
  113. * 当time为0时进行数据采集和数据上传
  114. */
  115. void MQTT_Alarm_clock(uint32_t time, GATEWAY_PARAMS *gateway)
  116. {
  117. while (1)
  118. {
  119. if (time <= 0)
  120. {
  121. Read_Data();
  122. MQTTPublish();
  123. break; // 跳出while循环
  124. }
  125. else
  126. {
  127. gd_eval_led_toggle(LED1);
  128. parseMQTTData(gateway);
  129. check_ota_event();
  130. }
  131. delay_1ms(1000);
  132. time-=1000;
  133. task_fwdgt_reload();
  134. }
  135. }
  136. /*
  137. * 函数名:void readID(char *strId)
  138. * 输入参数:无
  139. * 输出参数:strId
  140. * 返回值:无
  141. * 函数作用:读取长度12的芯片Id并组合从中取出对应字符防止其重复
  142. */
  143. void readID(char *strId)
  144. {
  145. static const char *hex_table = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMN";
  146. uint32_t CpuID1, CpuID2, CpuID3;
  147. CpuID1 = *(volatile uint32_t *)(0x1FFFF7E8);
  148. CpuID2 = *(volatile uint32_t *)(0x1FFFF7EC);
  149. CpuID3 = *(volatile uint32_t *)(0x1FFFF7F0);
  150. strId[0] = 'D';
  151. strId[1] = 'T';
  152. for (uint8_t i = 0, j = 0; i < 8; i += 2)
  153. {
  154. uint32_t sum1 = (CpuID1 >> ((j % 4) * 8)) & 0xFF; // 按字节取出
  155. uint8_t index1 = sum1 / 16;
  156. uint8_t remainder1 = sum1 % 16;
  157. uint32_t sum2 = (CpuID2 >> ((j % 4) * 8)) & 0xFF; // 按字节取出
  158. uint8_t index2 = sum2 / 16;
  159. uint8_t remainder2 = sum2 % 16;
  160. uint32_t sum3 = (CpuID3 >> ((j % 4) * 8)) & 0xFF; // 按字节取出
  161. uint8_t index3 = sum3 / 16;
  162. uint8_t remainder3 = sum3 % 16;
  163. strId[i + 2] = hex_table[index1 + index2 + index3];
  164. strId[i + 1 + 2] = hex_table[remainder1 + remainder2 + remainder3];
  165. j++;
  166. }
  167. strId[10] = '\0';
  168. }
  169. /*
  170. * 函数名:void http_load_config(uint8_t *httpURL)
  171. * 输入参数:httpURL 获取参数配置的网址
  172. * 输出参数:strId
  173. * 返回值:无
  174. * 函数作用:拉取http配置
  175. */
  176. void http_load_config(uint8_t *httpURL)
  177. {
  178. uint32_t BufferSize=46*1024;//最大获取的数据空间
  179. char *dmabuffer=malloc(BufferSize);
  180. __LOAD_CONFIG:
  181. if (EC800MGetUrl(httpURL,dmabuffer,BufferSize) == true) // 从http获取信息,获取成功则保存到本地
  182. {
  183. if(WaitForUpData(dmabuffer)==false) goto __LOAD_CONFIG;
  184. }
  185. else//如果没有从http拉取到配置则直接读取文件系统内的json数据
  186. {
  187. dma_config_change(dmabuffer,BufferSize);
  188. delay_1ms(1000);
  189. EC800MSendCmd(CMD_READ_FILE, strlen(CMD_READ_FILE)); //"AT+QFDWL=http.txt\r\n"
  190. delay_1ms(3000);
  191. WaitForUpData(dmabuffer);
  192. }
  193. dma_config();
  194. free(dmabuffer);
  195. }
  196. /*
  197. * 函数名:void fwdgt_init(void)
  198. * 输入参数:无
  199. * 输出参数:无
  200. * 返回值:无
  201. * 函数作用:初始化硬件看门狗
  202. */
  203. void fwdgt_init(void)
  204. {
  205. rcu_osci_on(RCU_IRC40K); // IRC40K时钟打开
  206. /* confiure FWDGT counter clock: 40KHz(IRC40K) / 256= 0.015625 KHz */
  207. fwdgt_config(0x0FFF, FWDGT_PSC_DIV256); // 看门狗时间设置
  208. fwdgt_enable(); // 看门狗使能
  209. }
  210. void task_fwdgt_reload(void)
  211. {
  212. /* uncock fwdgt write protect*/
  213. fwdgt_write_enable();
  214. /* feed fwdgt */
  215. fwdgt_counter_reload();
  216. }
  217. #if 0 // 本地信息调试使用
  218. gateway->dataSource=MODBUS;
  219. gateway->baudrate=9600;
  220. gateway->dataBits=8;
  221. gateway->stopBit=1;
  222. gateway->checkBit=0;
  223. gateway->flowControl=0;
  224. gateway->device_read_data_num=1;
  225. gateway->device_read_data[0].deviceID645[0]=0x20;
  226. gateway->device_read_data[0].deviceID645[1]=0x92;
  227. gateway->device_read_data[0].deviceID645[2]=0x20;
  228. gateway->device_read_data[0].deviceID645[3]=0x00;
  229. gateway->device_read_data[0].deviceID645[4]=0x73;
  230. gateway->device_read_data[0].deviceID645[5]=0x81;
  231. gateway->device_read_data[0].dataType645=0x01010000;
  232. gateway->device_read_data[0].mdbSlave=0x17;
  233. gateway->device_read_data[0].mdbFunctionCode=0x03;
  234. gateway->device_read_data[0].mdbRegister=0x0000;
  235. gateway->device_read_data[0].registerLength=2;
  236. #endif