From 4744d3e72038ca7f9f24cde4655b2827188a616b Mon Sep 17 00:00:00 2001 From: armink Date: Sun, 22 Jul 2018 16:34:30 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90=E5=A2=9E=E5=8A=A0=E3=80=91M26=20?= =?UTF-8?q?=E5=88=9D=E5=A7=8B=E5=8C=96=E9=80=BB=E8=BE=91=E5=8F=AF=E9=9D=A0?= =?UTF-8?q?=E6=80=A7=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: armink --- at_socket_m26.c | 100 ++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 92 insertions(+), 8 deletions(-) diff --git a/at_socket_m26.c b/at_socket_m26.c index 85b89b8..b344e3f 100644 --- a/at_socket_m26.c +++ b/at_socket_m26.c @@ -625,16 +625,21 @@ int at_client_port_init(void) #define AT_SEND_CMD(resp, resp_line, cmd) \ do \ { \ - if (at_exec_cmd(at_resp_set_info(resp, 64, resp_line, rt_tick_from_millisecond(5000)), cmd) < 0) \ + if (at_exec_cmd(at_resp_set_info(resp, 64, resp_line, rt_tick_from_millisecond(5000)), cmd) < 0) \ { \ - LOG_E("AT send commands(%s) error!", cmd); \ return -RT_ERROR; \ } \ } while(0); \ int m26_net_init(void) { +#define CSQ_RETRY 10 +#define CREG_RETRY 10 +#define CGREG_RETRY 10 + at_response_t resp = RT_NULL; + int i, qimux, qimode; + char parsed_data[10]; resp = at_create_resp(64, 0, rt_tick_from_millisecond(5000)); if (!resp) @@ -642,17 +647,96 @@ int m26_net_init(void) LOG_E("No memory for response structure!"); return -RT_ENOMEM; } - - if (at_exec_cmd(at_resp_set_info(resp, 64, 2, rt_tick_from_millisecond(5000)), "AT+QILOCIP") == RT_EOK) + LOG_D("Start initializing the M26 module"); + /* wait M26 startup finish */ + rt_thread_delay(rt_tick_from_millisecond(8000)); + /* disable echo */ + AT_SEND_CMD(resp, 0, "ATE0"); + /* get module version */ + AT_SEND_CMD(resp, 0, "ATI"); + /* show module version */ + for (i = 0; i < resp->line_counts - 1; i++) { - LOG_I("AT network is already initialized!"); - return RT_EOK; + LOG_D("%s", at_resp_get_line(resp, i + 1)) + } + /* check SIM card */ + AT_SEND_CMD(resp, 2, "AT+CPIN?"); + if (!at_resp_get_line_by_kw(resp, "READY")) + { + LOG_E("SIM card detection failed"); + return -RT_ERROR; + } + /* waiting for dirty data to be digested */ + rt_thread_delay(rt_tick_from_millisecond(10)); + /* check signal strength */ + for (i = 0; i < CSQ_RETRY; i++) + { + AT_SEND_CMD(resp, 0, "AT+CSQ"); + at_resp_parse_line_args(resp, 2, "+CSQ: %s", &parsed_data); + if (strncmp(parsed_data, "99,99", sizeof(parsed_data))) + { + LOG_D("Signal strength: %s", parsed_data); + break; + } + rt_thread_delay(rt_tick_from_millisecond(1000)); + } + if (i == CSQ_RETRY) + { + LOG_E("Signal strength check failed (%s)", parsed_data); + return -RT_ERROR; + } + /* check the GSM network is registered */ + for (i = 0; i < CREG_RETRY; i++) + { + AT_SEND_CMD(resp, 0, "AT+CREG?"); + at_resp_parse_line_args(resp, 2, "+CREG: %s", &parsed_data); + if (!strncmp(parsed_data, "0,1", sizeof(parsed_data)) || !strncmp(parsed_data, "0,5", sizeof(parsed_data))) + { + LOG_D("GSM network is registered (%s)", parsed_data); + break; + } + rt_thread_delay(rt_tick_from_millisecond(1000)); + } + if (i == CREG_RETRY) + { + LOG_E("The GSM network is register failed (%s)", parsed_data); + return -RT_ERROR; + } + /* check the GPRS network is registered */ + for (i = 0; i < CGREG_RETRY; i++) + { + AT_SEND_CMD(resp, 0, "AT+CGREG?"); + at_resp_parse_line_args(resp, 2, "+CGREG: %s", &parsed_data); + if (!strncmp(parsed_data, "0,1", sizeof(parsed_data)) || !strncmp(parsed_data, "0,5", sizeof(parsed_data))) + { + LOG_D("GPRS network is registered (%s)", parsed_data); + break; + } + rt_thread_delay(rt_tick_from_millisecond(1000)); + } + if (i == CGREG_RETRY) + { + LOG_E("The GPRS network is register failed (%s)", parsed_data); + return -RT_ERROR; } - AT_SEND_CMD(resp, 0, "ATE0"); AT_SEND_CMD(resp, 0, "AT+QIFGCNT=0"); AT_SEND_CMD(resp, 0, "AT+QICSGP=1, \"CMNET\""); - AT_SEND_CMD(resp, 0, "AT+QIMUX=1"); + AT_SEND_CMD(resp, 0, "AT+QIMODE?"); + + at_resp_parse_line_args(resp, 2, "+QIMODE: %d", &qimode); + if (qimode == 1) + { + AT_SEND_CMD(resp, 0, "AT+QIMODE=0"); + } + /* Set to multiple connections */ + AT_SEND_CMD(resp, 0, "AT+QIMUX?"); + at_resp_parse_line_args(resp, 2, "+QIMUX: %d", &qimux); + if (qimux == 0) + { + AT_SEND_CMD(resp, 0, "AT+QIMUX=1"); + } + AT_SEND_CMD(resp, 2, "AT+QIDEACT"); AT_SEND_CMD(resp, 0, "AT+QIREGAPP"); AT_SEND_CMD(resp, 0, "AT+QIACT");