main.c 8.3 KB

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