123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276 |
- #include "gd32f10x.h"
- #include "systick.h"
- #include <stdio.h>
- #include "main.h"
- #include "usart.h"
- #include "led.h"
- #include "config.h"
- #include "log.h"
- #include "ec800m.h"
- #include "parseDeviceMessage.h"
- #include "otaEvent.h"
- #include "systick.h"
- #include "w25q32.h"
- #include "key.h"
- #define UNIQUE_ID 0x1FFFF7E8
- #define VERSION "V1.0.2"
- #define GATEWAYNAME "DTU"
- #define GATEWAYMODE "4G"
- void MQTT_Alarm_clock(uint32_t time, GATEWAY_PARAMS *gateway);
- void readID(char *strId);
- void http_load_config(uint8_t *httpURL);
- void fwdgt_init();
- void task_fwdgt_reload();
- int main(void)
- {
-
- rcu_osci_on(RCU_HXTAL);
- systick_config();
- nvic_config();
-
- gd_eval_led_init(LED1);
- gd_com_init(COM_232);
- gd_com_init(COM_EC800);
- dma_config();
- gd_EC800M_pin_init();
- gd_eval_key_init(KEY_SYS_RECOVERY, KEY_MODE_EXTI);
- EC800MPwoerOn();
- EC800MWaitReady();
- EC800MSetPDP();
-
- char *strID = malloc(24);
-
- readID(strID);
-
- GATEWAY_PARAMS *gateway;
- gateway = get_gateway_config_params();
-
- strcpy((char *)&gateway->gatewayId, strID);
- strcpy((char *)&gateway->gatewayMode, GATEWAYMODE);
- strcpy((char *)&gateway->gateName,GATEWAYNAME);
-
-
- char *httpURL = malloc(100);
- memset(httpURL, 0, 100);
- sprintf(httpURL, "http://gpu.ringzle.com:8082/iot/transmit/getTransmitConfig/%s",strID);
- http_load_config(httpURL);
- free(httpURL);
- httpURL=NULL;
-
- gateway = get_gateway_config_params();
- strcpy((char *)&gateway->gatewayId, strID);
- strcpy((char *)&gateway->gatewayMode, GATEWAYMODE);
- strcpy((char *)&gateway->gateName,GATEWAYNAME);
-
- free(strID);
-
- config_485_port(COM_485, gateway->baudrate, gateway->dataBits, gateway->stopBit, gateway->checkBit);
- gd_485_DE_pin_init();
- gd_485_DE_tx();
- ConnectMQTTSever(gateway->host, gateway->port, 60, gateway->deviceId);
- MQTTSubTopic(gateway->commandTopic);
- ring_buffer_init(&mqttRecv);
- mmodbus_init(1);
- dlt645_init(1);
-
- while (1)
- {
- check_ota_event();
- MQTT_Alarm_clock(gateway->inboundTime, gateway);
- }
- }
- void MQTT_Alarm_clock(uint32_t time, GATEWAY_PARAMS *gateway)
- {
- while (1)
- {
- if (time <= 0)
- {
- Read_Data();
- MQTTPublish();
- break;
- }
- else
- {
- gd_eval_led_toggle(LED1);
- parseMQTTData(gateway);
- check_ota_event();
- }
- delay_1ms(1000);
- time-=1000;
- task_fwdgt_reload();
- }
- }
- void readID(char *strId)
- {
- static const char *hex_table = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMN";
- uint32_t CpuID1, CpuID2, CpuID3;
- CpuID1 = *(volatile uint32_t *)(0x1FFFF7E8);
- CpuID2 = *(volatile uint32_t *)(0x1FFFF7EC);
- CpuID3 = *(volatile uint32_t *)(0x1FFFF7F0);
- strId[0] = 'D';
- strId[1] = 'T';
- for (uint8_t i = 0, j = 0; i < 8; i += 2)
- {
- uint32_t sum1 = (CpuID1 >> ((j % 4) * 8)) & 0xFF;
- uint8_t index1 = sum1 / 16;
- uint8_t remainder1 = sum1 % 16;
- uint32_t sum2 = (CpuID2 >> ((j % 4) * 8)) & 0xFF;
- uint8_t index2 = sum2 / 16;
- uint8_t remainder2 = sum2 % 16;
- uint32_t sum3 = (CpuID3 >> ((j % 4) * 8)) & 0xFF;
- uint8_t index3 = sum3 / 16;
- uint8_t remainder3 = sum3 % 16;
- strId[i + 2] = hex_table[index1 + index2 + index3];
- strId[i + 1 + 2] = hex_table[remainder1 + remainder2 + remainder3];
- j++;
- }
- strId[10] = '\0';
- }
- void http_load_config(uint8_t *httpURL)
- {
- uint32_t BufferSize=46*1024;
- char *dmabuffer=malloc(BufferSize);
- __LOAD_CONFIG:
- if (EC800MGetUrl(httpURL,dmabuffer,BufferSize) == true)
- {
- if(WaitForUpData(dmabuffer)==false) goto __LOAD_CONFIG;
- }
- else
- {
- dma_config_change(dmabuffer,BufferSize);
- delay_1ms(1000);
- EC800MSendCmd(CMD_READ_FILE, strlen(CMD_READ_FILE));
- delay_1ms(3000);
- WaitForUpData(dmabuffer);
- }
- dma_config();
- free(dmabuffer);
- }
- void fwdgt_init(void)
- {
- rcu_osci_on(RCU_IRC40K);
-
- fwdgt_config(0x0FFF, FWDGT_PSC_DIV256);
- fwdgt_enable();
- }
- void task_fwdgt_reload(void)
- {
-
- fwdgt_write_enable();
-
- fwdgt_counter_reload();
- }
- #if 0
- gateway->dataSource=MODBUS;
- gateway->baudrate=9600;
- gateway->dataBits=8;
- gateway->stopBit=1;
- gateway->checkBit=0;
- gateway->flowControl=0;
- gateway->device_read_data_num=1;
-
- gateway->device_read_data[0].deviceID645[0]=0x20;
- gateway->device_read_data[0].deviceID645[1]=0x92;
- gateway->device_read_data[0].deviceID645[2]=0x20;
- gateway->device_read_data[0].deviceID645[3]=0x00;
- gateway->device_read_data[0].deviceID645[4]=0x73;
- gateway->device_read_data[0].deviceID645[5]=0x81;
-
- gateway->device_read_data[0].dataType645=0x01010000;
- gateway->device_read_data[0].mdbSlave=0x17;
- gateway->device_read_data[0].mdbFunctionCode=0x03;
- gateway->device_read_data[0].mdbRegister=0x0000;
- gateway->device_read_data[0].registerLength=2;
- #endif
|