ssize_t mdm_getline(char *buff, size_t size, int timeout_ms) { int cin = -1; int cin_last; if (NULL == buff || size == 0) { return -1; } size_t len = 0; Timer timer; timer.start(); while ((len < (size-1)) && (timer.read_ms() < timeout_ms)) { if (mdm.readable()) { cin_last = cin; cin = mdm.getc(); if (isprint(cin)) { buff[len++] = (char)cin; continue; } else if (('\r' == cin_last) && ('\n' == cin)) { break; } } wait_ms(1); } buff[len] = (char)NULL; return len; }
int mdm_sendAtCmdRsp(const char *cmd, const char **rsp_list, int timeout_ms, string * rsp, int * len) { static char cmd_buf[3200]; // Need enough room for the WNC sockreads (over 3000 chars) size_t n = strlen(cmd); if (cmd && n > 0) { if (mdm_dbgmask & MDM_DBG_AT_CMDS) { printf(MAG "ATCMD: " DEF "--> " GRN "%s" DEF "\n", cmd); } while (n--) { mdm.putc(*cmd++); wait_ms(1); }; mdm.putc('\r'); wait_ms(1); mdm.putc('\n'); wait_ms(1); } if (rsp_list) { rsp->erase(); // Clean up from prior cmd response *len = 0; Timer timer; timer.start(); while (timer.read_ms() < timeout_ms) { int lenCmd = mdm_getline(cmd_buf, sizeof(cmd_buf), timeout_ms - timer.read_ms()); if (lenCmd == 0) continue; if (lenCmd < 0) return MDM_ERR_TIMEOUT; else { *len += lenCmd; *rsp += cmd_buf; } if (mdm_dbgmask & MDM_DBG_AT_CMDS) { printf(MAG "ATRSP: " DEF "<-- " CYN "%s" DEF "\n", cmd_buf); } int rsp_idx = 0; while (rsp_list[rsp_idx]) { if (strcasecmp(cmd_buf, rsp_list[rsp_idx]) == 0) { return rsp_idx; } rsp_idx++; } } return MDM_ERR_TIMEOUT; } pc.printf("D %s",rsp); return MDM_OK; }
int main() { mbed_interface_disconnect(); //disable debug interface. and acces to MAC and local FS. ADC will work better! timer.start(); serial_buffered.baud(BAUDRATE); setup(); while (1) { loop(); } }
int mdm_init(void) { // Hard reset the modem (doesn't go through // the signal level translator) mdm_reset = 0; // disable signal level translator (necessary // for the modem to boot properly). All signals // except mdm_reset go through the level translator // and have internal pull-up/down in the module. While // the level translator is disabled, these pins will // be in the correct state. shield_3v3_1v8_sig_trans_ena = 0; // While the level translator is disabled and ouptut pins // are tristated, make sure the inputs are in the same state // as the WNC Module pins so that when the level translator is // enabled, there are no differences. mdm_uart2_rx_boot_mode_sel = 1; // UART2_RX should be high mdm_power_on = 0; // powr_on should be low mdm_wakeup_in = 1; // wake-up should be high mdm_uart1_cts = 0; // indicate that it is ok to send // Now, wait for the WNC Module to perform its initial boot correctly wait(1.0); // The WNC module initializes comms at 115200 8N1 so set it up mdm.baud(115200); //Now, enable the level translator, the input pins should now be the //same as how the M14A module is driving them with internal pull ups/downs. //When enabled, there will be no changes in these 4 pins... shield_3v3_1v8_sig_trans_ena = 1; // Now, give the modem 60 secons to start responding by // sending simple 'AT' commands to modem once per second. Timer timer; timer.start(); while (timer.read() < 60) { const char * rsp_lst[] = { ok_str, error_str, NULL }; int rc = mdm_sendAtCmd("AT", rsp_lst, 500); if (rc == 0) return true; //timer.read(); wait_ms(1000 - (timer.read_ms() % 1000)); pc.printf("\r%d",timer.read_ms()/1000); } return false; }
int mdm_sendAtCmd(const char *cmd, const char **rsp_list, int timeout_ms) { if (cmd && strlen(cmd) > 0) { if (mdm_dbgmask & MDM_DBG_AT_CMDS) { printf(MAG "ATCMD: " DEF "--> " GRN "%s" DEF "\n", cmd); } mdm.printf("%s\r\n", cmd); } if (rsp_list) { Timer timer; char rsp[MAX_AT_RSP_LEN+1]; int len; timer.start(); while (timer.read_ms() < timeout_ms) { len = mdm_getline(rsp, sizeof(rsp), timeout_ms - timer.read_ms()); if (len < 0) return MDM_ERR_TIMEOUT; if (len == 0) continue; if (mdm_dbgmask & MDM_DBG_AT_CMDS) { printf(MAG "ATRSP: " DEF "<-- " CYN "%s" DEF "\n", rsp); } if (rsp_list) { int rsp_idx = 0; while (rsp_list[rsp_idx]) { if (strcasecmp(rsp, rsp_list[rsp_idx]) == 0) { return rsp_idx; } rsp_idx++; } } } return MDM_ERR_TIMEOUT; } return MDM_OK; }