#include #include #include #include "main.h" #include "usart-board.h" #include #include #include #include "netdrv.h" #include "data.h" #if !defined(LOG_TAG) #define LOG_TAG "CAT1" #endif #include ALIGN (RT_ALIGN_SIZE) static char thread_stack[4096]; static struct rt_thread netapp_thread; static char tcprxbbuff[1200]; static char tcptxbbuff[1200]; netDrv_t g_cat1Drv = { 0, // 信号值 "", // iccid Init, // 状态 tcprxbbuff, // 接受缓存 0, // 接受数据长度 tcptxbbuff, // 发送数据缓存 0, // 发送数据长度 }; static void ec200_resp_csq_callback (char * data, uint16_t length) { char * p; p = strstr (data, "+CSQ:"); p = p + 6; g_cat1Drv.Csq = atoi (p); rt_kprintf ("CSQ: <<< <%d>\r\n", g_cat1Drv.Csq); } // 校准时间程序 static void ec200_resp_qlts_callback (char * data, uint16_t length) { uint8_t k[6] = {NULL}; char *p = NULL; p = strstr (data, "+QLTS:") ; p = p + 10; k[0] = atoi (p); // 年 k[0] = (k[0] + (k[0] / 10) * 6) ; // 十进制转bcd码 p = p + 3; k[1] = atoi (p); // 月 k[1] = (k[1] + (k[1] / 10) * 6) ; p = p + 3; k[2] = atoi (p); // 日 k[2] = (k[2] + (k[2] / 10) * 6) ; p = p + 3; // p k[3] = atoi (p); // 时 k[3] = (k[3] + (k[3] / 10) * 6) ; p = p + 3; // p k[4] = atoi (p); // 分 k[4] = (k[4] + (k[4] / 10) * 6) ; p = p + 3; // p k[5] = atoi (p); // 秒 k[5] = (k[5] + (k[5] / 10) * 6) ; rt_kprintf ("20%02x-%02x-%02x %02x:%02x:%02x\r\n", k[0], k[1], k[2], k[3], k[4], k[5]); } static void ec200_resp_iccid_callback (char * data, uint16_t length) { char *p; p = strstr (data, "+QCCID:"); p = p + 8; rt_memset (SysData.iccid, 0, 32); rt_memcpy (SysData.iccid, p, 20); rt_kprintf ("ICCID: <<< <%d> [%s]\r\n", length, SysData.iccid); } static void ec200_resp_qird_callback (char * data, uint16_t length) { char * p; p = strstr (data, "+QIRD: "); p = p + 7; g_cat1Drv.rx_size = atoi (p); if (g_cat1Drv.rx_size > 0) // 读取到数据 { p = strstr (p, "\r\n"); p = p + 2; for (int i = 0; i < g_cat1Drv.rx_size; i++) { g_cat1Drv.rxbuff[i] = *p; p++; } rt_event_send (gTrigEvents, EVENT_TRIG_NETMODE_RX1) ; // rt_kprintf("EVENT_TRIG_NETMODE_RX1\r\n"); // for(int i =0 ;i < g_cat1Drv.rx_size;i++) // { // rt_kprintf("%02X ",g_cat1Drv.rxbuff[i]); // } } } // type 0 无需打包 1 需要重新打包 const struct { char * cmd; char * ack; uint32_t delayms; uint32_t waitms; uint32_t retry; void (*call) (char *, uint16_t); } cat1list[] = { [ATE] = {"ATE0\r\n", "OK", 10, 2000, 10, NULL}, [ATI] = {"ATI\r\n", "OK", 10, 2000, 3, NULL}, [CPIN] = {"AT+CPIN?\r\n", "+CPIN: READY", 10, 2000, 10, NULL}, // [CREG] = {"AT+CREG?\r\n", "+CREG: 0,1", 10, 2000, 30, NULL}, // [CGREG] = {"AT+CGREG?\r\n", "+CGREG: 0,1", 10, 2000, 30, NULL}, [CGATT] = {"AT+CGATT?\r\n", "+CGATT: 1", 10, 2000, 30, NULL}, [CSQ] = {"AT+CSQ\r\n", "+CSQ:", 10, 2000, 1, ec200_resp_csq_callback}, [QLTS] = {"AT+QLTS=2\r\n", "+QLTS:", 10, 2000, 1, ec200_resp_qlts_callback}, [QCCID] = {"AT+QCCID\r\n", "+QCCID:", 10, 2000, 1, ec200_resp_iccid_callback}, }; static void cat1_rx_done (void) { rt_event_send (gUartEvents, EVENT_UART_GSM_RX); // for (int i = 0 ; i < g_GSMDrv.rx_size; i++) // { // rt_kprintf ("%c", g_GSMDrv.rx_buffer[i]); // } // MX_UART_Clear (&g_GSMDrv); } // cat1 发送数据 //0 : 结束,下一个 //1 : 运行中,下一个 static CommRet_T cat1_sendcmd_and_responce (int time, char * tx, int size, char * ack, void (*call) (char *, uint16_t)) { MX_UART_Clear (&g_GSMDrv); MX_UART_PutBuffer (&g_GSMDrv, tx, size); //rt_kprintf ("%.*s", size, tx) ; //timeout = cat1list[i].waitms; int timeout = time; while (timeout--) { if (RT_EOK == rt_event_recv (gUartEvents, EVENT_UART_GSM_RX, RT_EVENT_FLAG_AND | RT_EVENT_FLAG_CLEAR, 0, RT_NULL)) { //rt_kprintf ("[%.*s]", g_GSMDrv.rx_size, g_GSMDrv.rx_buffer) ; if (NULL != rt_strstr ( (char *) g_GSMDrv.rx_buffer, ack)) { if (call != NULL) { call ( (char *) g_GSMDrv.rx_buffer, g_GSMDrv.rx_size) ; } return RetSucc; // 成功 } MX_UART_Clear (&g_GSMDrv); } rt_thread_mdelay (1); } return RetErr; // 超时 } static void thread_entry (void * argument) { char at_cmd[128] = {NULL}; char ipaddr[32] = {NULL}; int ipport; char *p; int retry = 0; p = ef_get_env ("mqtt_addr"); rt_strncpy (ipaddr, p, rt_strlen (p)); p = ef_get_env ("mqtt_port"); ipport = atoi (p); rt_sprintf (at_cmd, "AT+QIOPEN=1,0,\"TCP\",\"%s\",%d,0,0\r", ipaddr, ipport); g_GSMDrv.rxdone = cat1_rx_done; g_GSMDrv.BaudRate = 115200; // 启动4G 模块电源 // 拉高4G 模块复位 while (1) { CAT1RST: // 初始化串口,打开模块电源 g_cat1Drv.State = Init; MX_UART_Init (&g_GSMDrv); HAL_GPIO_WritePin (GSM_PW_GPIO_Port, GSM_PW_Pin, GPIO_PIN_RESET); // 关闭4G 模块电源 rt_thread_mdelay (3000); HAL_GPIO_WritePin (GSM_PW_GPIO_Port, GSM_PW_Pin, GPIO_PIN_SET); // 开启4G rt_thread_mdelay (100); // 拉高模组复位 HAL_GPIO_WritePin (GSM_RST_GPIO_Port, GSM_RST_Pin, GPIO_PIN_RESET); rt_thread_mdelay (100); HAL_GPIO_WritePin (GSM_RST_GPIO_Port, GSM_RST_Pin, GPIO_PIN_SET); rt_thread_mdelay (8000); for (int i = 0; i < MAXSETP; i++) { retry = cat1list[i].retry; while (1) { if (cat1_sendcmd_and_responce (cat1list[i].waitms, cat1list[i].cmd, strlen (cat1list[i].cmd), cat1list[i].ack, cat1list[i].call) == RetSucc) { break; } else { retry--; if (retry <= 0) { rt_kprintf ("Join Faild%d \r\n", i); goto CAT1RST; } } } } rt_kprintf ("Join Success \r\n"); rt_sprintf (at_cmd, "AT+QIOPEN=1,0,\"TCP\",\"%s\",%d,0,0\r", ipaddr, ipport); if (cat1_sendcmd_and_responce (15000, at_cmd, strlen (at_cmd), "+QIOPEN: 0,0", NULL) == RetSucc) { g_cat1Drv.State = Conn; rt_kprintf ("Net is ready \r\n"); } else { rt_kprintf ("Net is Error \r\n"); goto CAT1RST; } while (1) { // 发送数据 if (RT_EOK == rt_event_recv (gTrigEvents, EVENT_TRIG_NETMODE_TX1, RT_EVENT_FLAG_AND | RT_EVENT_FLAG_CLEAR, RT_WAITING_NO, RT_NULL)) // 需要发送数据 { if (g_cat1Drv.txbuff != NULL) { rt_sprintf (at_cmd, "AT+QISEND=0,%d\r\n", g_cat1Drv.tx_size); rt_thread_mdelay (10); if (cat1_sendcmd_and_responce (1000, at_cmd, strlen (at_cmd), ">", NULL) != RetSucc) { rt_kprintf ("NET:+CME:ERROR QISEND\r\n"); goto CAT1RST; } if (cat1_sendcmd_and_responce (3000, g_cat1Drv.txbuff, g_cat1Drv.tx_size, "SEND OK", NULL) != RetSucc) { rt_kprintf ("NET:+CME:ERROR QPUSH\r\n"); goto CAT1RST; } } } // 读取数据 rt_thread_mdelay (300); if (cat1_sendcmd_and_responce (1000, "AT+QIRD=0,1500\r\n", strlen ("AT+QIRD=0,1500\r\n"), "+QIRD:", ec200_resp_qird_callback) != RetSucc) { rt_kprintf ("NET:+CME:ERROR QIRD\r\n"); goto CAT1RST; } // // 通过通道2上报数据 // if (RT_EOK == rt_event_recv (gTrigEvents, EVENT_TRIG_NETMODE_TX2, RT_EVENT_FLAG_AND | RT_EVENT_FLAG_CLEAR, RT_WAITING_NO, RT_NULL)) // 需要发送数据 // { // if (g_cat1Drv.txbuff != NULL) // { // rt_sprintf (at_cmd, "AT+QISEND=0,%d\r\n", g_cat1Drv.tx_size); // rt_thread_mdelay (10); // if (cat1_sendcmd_and_responce (1000, at_cmd, strlen (at_cmd), ">", NULL) != RetSucc) // { // rt_kprintf ("NET:+CME:ERROR QISEND\r\n"); // goto CAT1RST; // } // if (cat1_sendcmd_and_responce (3000, g_cat1Drv.txbuff, g_cat1Drv.tx_size, "SEND OK", NULL) != RetSucc) // { // rt_kprintf ("NET:+CME:ERROR QPUSH\r\n"); // goto CAT1RST; // } // } // } } rt_thread_mdelay (3000); } } void cat1_service_tx (char * buff, int size) { rt_memcpy (g_cat1Drv.txbuff, buff, size); g_cat1Drv.tx_size = size; rt_event_send (gTrigEvents, EVENT_TRIG_NETMODE_TX1) ; } // 通过网络通道2发送数据 void cat1_service2_tx (char * buff, int size) { rt_memcpy (g_cat1Drv.txbuff, buff, size); g_cat1Drv.tx_size = size; rt_event_send (gTrigEvents, EVENT_TRIG_NETMODE_TX2) ; } void CAT1ThreadStart (void) { /* 创建线程1,名称是thread1,入口是thread1_entry */ rt_thread_init (&netapp_thread, "netapp_thread", thread_entry, RT_NULL, &thread_stack[0], sizeof (thread_stack), CAT1_THREAD_PRIORITY, CAT1_THREAD_TIMESLICE); rt_thread_startup (&netapp_thread); return ; }