atcmd.c
资源名称:de8681_w.zip [点击查看]
上传用户:sdttscl
上传日期:2010-01-04
资源大小:683k
文件大小:19k
源码类别:
Modem编程
开发平台:
C/C++
- //---------------------------------------------------------------------------------------------------
- // Project:- DE8681
- // Filename:- ATCMD.C
- // Description:- Routines for AT Command Functions and Interpretation.
- // Programmer:- D.T.F
- // Version:- 2.0
- // Created:- 28th February 2002
- // Last modified:-
- //---------------------------------------------------------------------------------------------------
- // (C) Consumer Microcircuits Ltd 2002
- //
- // This firmware was designed by:-
- // Consumer Microcircuits Ltd,
- // Langford, Maldon,
- // ESSEX
- // CM9 6WG.
- // in the UK for use with CML evaluation kits only and is based on UK originated technology.
- // Please contact
- // sales@cmlmicro.co.uk
- // +44 (0)1621 875500
- // for licensing details.
- //---------------------------------------------------------------------------------------------------
- #define ATCMD_C
- #include "ef8681.h"
- unsigned char atcmd_interpret(void)
- {
- unsigned char retval;
- ATBUFPTR = 0;
- while (ATBUF[ATBUFPTR] != NUL) // Check if NULL encountered to indicate end of AT command.
- {
- switch(ATBUF[ATBUFPTR])
- {
- case 'A': case 'a':
- retval = atacmd(); // Routine sets Answer flag in S register
- // AT Command Mode Flag cleared to indicate Handshaking
- break;
- case 'B': case 'b':
- retval = atbcmd(); // Routine to control communication standard
- break;
- case 'D': case 'd':
- retval = atdcmd(); // Routine to extract DTMF digits and set Call flag in S register
- // AT Command Mode Flag cleared to indicate Handshaking
- break;
- case 'E': case'e':
- retval = atecmd(); // Routine to control echo
- break;
- case 'H': case'h':
- retval = athcmd(); // Routine to control hook swicth
- break;
- case 'I': case'i':
- retval = aticmd(); // Routine to provide user product information
- break;
- case 'N': case'n':
- retval = atncmd(); // Routine to enable/disable modulation fallback
- break;
- case 'O': case'o':
- retval = atocmd(); // Routine to revert back to online mode
- break;
- case 'Q': case'q':
- retval = atqcmd(); // Routine to control Modem responses
- break;
- case 'S': case's':
- retval = atscmd(); // Routine to write or read S-Registers
- break;
- case 'V': case'v':
- retval = atvcmd(); // Routine to control result code format
- break;
- case 'X': case'x':
- retval = atxcmd(); // Routine to control calling characteristics
- break;
- case 'Z': case'z':
- retval = atzcmd(); // Routine to reset modem and restore factory profiles
- break;
- case '&':
- retval = xatcmd(); // Routine to excute extended AT Commands
- break;
- case '@':
- retval = cmlatcmd(); // Routine to excute CML specific AT Commands
- break;
- default:
- retval = ERROR;
- break;
- }
- // If an error occurs during interpretation
- // or Manual Answer or Dial has been requested
- // AT Command interpretation will be terminated
- if (retval != OK)
- {
- return (retval);
- }
- ATBUFPTR++; // Increment AT Command Buffer pointer.
- }
- return (OK); // Intepretation has completed successfully or no commands had been entered.
- }
- unsigned char atacmd(void)
- {
- ATCMDMODE = 0;
- ANSORIG = 0; // Set to indicate answering
- return (NORESULT);
- }
- unsigned char atbcmd(void) // Modify Call Characteristic bits located in S27 reg
- {
- unsigned char tempreg;
- ATBUFPTR++; // Increment AT Command Buffer pointer.
- switch(ATBUF[ATBUFPTR])
- {
- case '0':
- tempreg = PROTB0;
- break;
- case '1':
- tempreg = PROTB1;
- break;
- case '2':
- tempreg = PROTB2;
- break;
- case '3':
- tempreg = PROTB3;
- break;
- case '4':
- tempreg = PROTB4;
- break;
- case '5':
- tempreg = PROTB5;
- break;
- case '6':
- tempreg = PROTB6;
- break;
- case '7':
- tempreg = PROTB7;
- break;
- case '8':
- tempreg = PROTB8;
- break;
- case '9':
- tempreg = PROTB9;
- break;
- default:
- return (ERROR); // Return ERROR to indicate invalid character
- }
- S27 &= 0b00001111; // Clear existing Protocol settings
- S27 ^= tempreg; // Modify S27 to reflect new settings
- return (OK); // Return OK to indicate success
- }
- unsigned char atdcmd(void)
- {
- unsigned char i, tmp;
- ATBUFPTR++; // Increment AT Command Buffer pointer.
- tmp = ATBUF[ATBUFPTR]; // Load temporary character register
- if ((tmp == 'L') || (tmp == 'l'))
- {
- if (TELNUM[0] != NUL)
- {
- ATCMDMODE = 0; // Clear AT Command Mode Flag
- ANSORIG = 1; // Set to indicate originate
- return (NORESULT); // Return NORESULT to indicate success without result code.
- }
- else
- {
- return (ERROR); // Return ERROR to indicate no previous telephone number exists
- }
- }
- i = 0;
- while ((tmp <= '9' && tmp >= '0') || (tmp <= 'D' && tmp >= 'A')
- || (tmp <= 'd' && tmp >= 'a') || tmp == '#' || tmp == '*' || tmp == ',')
- {
- TELNUM[i++] = tmp; // Load character into telephone digit buffer
- if (i >= maxteldigits)
- {
- TELNUM[0] = NUL; // Destroy previous loaded digits due to error
- return(ERROR); // Return ERROR to indicate telephone digit buffer is full
- }
- ATBUFPTR++; // Increment AT Command Buffer pointer.
- tmp = ATBUF[ATBUFPTR]; // Load temporary character register
- }
- if (i > 0)
- {
- ATBUFPTR--; // Decrement AT Command Buffer pointer (not essential because interpreter will be terminated)
- TELNUM[i] = NUL; // Null terminate telephone digit string
- ATCMDMODE = 0; // Clear AT Command Mode Flag
- ANSORIG = 1; // Set to indicate originate
- return(NORESULT); // Return NORESULT to indicate success without result code.
- }
- else
- {
- return (ERROR); // Return ERROR to indicate no previous telephone number exists
- }
- }
- unsigned char atecmd(void) // Modify Echo Character bit located in S14 reg
- {
- ATBUFPTR++; // Increment AT Command Buffer pointer.
- switch(ATBUF[ATBUFPTR])
- {
- case '0':
- ECHO = OFF; // Disable Echoing
- break;
- case '1':
- ECHO = ON; // Enable Echoing
- break;
- default:
- return (ERROR); // Return ERROR to indicate invalid character
- }
- return (OK); // Return OK to indicate success
- }
- unsigned char athcmd(void) // Hook switch control
- {
- ATBUFPTR++; // Increment AT Command Buffer pointer.
- switch(ATBUF[ATBUFPTR])
- {
- case '0':
- hook(0); // On-Hook
- break;
- case '1':
- hook(1); // Off-Hook
- break;
- default:
- return (ERROR); // Return ERROR to indicate invalid character
- }
- return (OK); // Return OK to indicate success
- }
- unsigned char aticmd(void) // Send information to terminal
- {
- ATBUFPTR++; // Increment AT Command Buffer pointer.
- switch(ATBUF[ATBUFPTR])
- {
- case '0':
- genmsgtopc(idmsg); // Send id message to terminal
- break;
- case '1':
- genmsgtopc(prodmsg); // Send product message to terminal
- break;
- case '2':
- genmsgtopc(cmlukmsg); // Send CML UK message to terminal
- genmsgtopc(cmlukwwwmsg); // Send CML UK Web address message to terminal
- genmsgtopc(cmlukemmsg); // Send CML UK Email address message to terminal
- genmsgtopc(cmluktelmsg); // Send CML UK Telephone message to terminal
- break;
- case '3':
- genmsgtopc(cmlsgmsg); // Send CML Singapore message to terminal
- genmsgtopc(cmlsgemmsg); // Send CML Singapore Email address message to terminal
- break;
- case '4':
- genmsgtopc(mxcommsg); // Send MX-COM message to terminal
- genmsgtopc(mxcomemmsg); // Send MXCOM Email address message to terminal
- break;
- default:
- return (ERROR); // Return ERROR to indicate invalid character
- }
- return (OK); // Return OK to indicate success
- }
- unsigned char atncmd(void)
- {
- ATBUFPTR++; // Increment AT Command Buffer pointer.
- switch(ATBUF[ATBUFPTR])
- {
- case '0':
- FALLBACK = OFF; // Disable Modulation Fallback
- break;
- case '1':
- FALLBACK = ON; // Enable Modulation Fallback
- break;
- default:
- return (ERROR); // Return ERROR to indicate invalid character
- }
- return (OK); // Return OK to indicate success
- }
- unsigned char atocmd(void)
- {
- DATAXFER = 1; // Revert back to online data transfer mode
- ATCMDMODE = 0;
- return (NORESULT);
- }
- unsigned char atqcmd(void) // Modify Response bit located in S14 reg
- {
- ATBUFPTR++; // Increment AT Command Buffer pointer.
- switch(ATBUF[ATBUFPTR])
- {
- case '0':
- RESOFF = FALSE; // Enable result code responses
- break;
- case '1':
- RESOFF = TRUE; // Disable result code responses
- break;
- default:
- return (ERROR); // Return ERROR to indicate invalid character
- }
- return (OK); // Return OK to indicate success
- }
- unsigned char atscmd(void)
- {
- unsigned long i;
- unsigned char k=0;
- char tempbuf[3];
- volatile unsigned char bank2 * sregptr;
- unsigned char sreg_offset;
- ATBUFPTR++; // Increment AT Command Buffer pointer.
- if ((ATBUF[ATBUFPTR] > '9') || (ATBUF[ATBUFPTR] < '0'))
- {
- return(ERROR);
- }
- tempbuf[k++] = ATBUF[ATBUFPTR++];
- if ((ATBUF[ATBUFPTR] <= '9') && (ATBUF[ATBUFPTR] >= '0'))
- {
- tempbuf[k++] = ATBUF[ATBUFPTR++];
- }
- tempbuf[k] = NUL;
- sreg_offset = (atoi(tempbuf)) & 0xFF; // Convert ASCII decimal to integer
- if (sreg_offset >= numsregs) // Return ERROR if offset exceeds max value
- { // specified in header file
- return(ERROR);
- }
- sregptr = &S0;
- sregptr += sreg_offset;
- if (ATBUF[ATBUFPTR] == '=')
- {
- ATBUFPTR++; // Increment AT Command Buffer pointer.
- i = ascregtolong();
- if (i > 255) // If 8-bit register exceeds 255 return error.
- {
- return (ERROR);
- }
- *sregptr = i;
- }
- else if (ATBUF[ATBUFPTR] == '?')
- {
- if (HEXOP)
- {
- hexnum2scrn(*sregptr,2,WORDRES); // Send LF char if word results enabled
- }
- else
- {
- decnum2scrn(*sregptr,3,WORDRES); // Send LF char if word results enabled
- }
- }
- else
- {
- return (ERROR); // Return ERROR to indicate invalid character
- }
- return (OK); // Return OK to indicate success
- }
- unsigned char atvcmd(void) // Modify Word Response bit located in S14 reg
- {
- ATBUFPTR++; // Increment AT Command Buffer pointer.
- switch(ATBUF[ATBUFPTR])
- {
- case '0':
- WORDRES = OFF; // Enable number responses
- break;
- case '1':
- WORDRES = ON; // Enable Word responses
- break;
- default:
- return (ERROR); // Return ERROR to indicate invalid character
- }
- return (OK); // Return OK to indicate success
- }
- unsigned char atxcmd(void) // Modify Call Characteristic bits located in S22 reg
- {
- unsigned char tempreg;
- ATBUFPTR++; // Increment AT Command Buffer pointer.
- switch(ATBUF[ATBUFPTR])
- {
- case '0':
- tempreg = X0CALLING;
- break;
- case '1':
- tempreg = X1CALLING;
- break;
- case '2':
- tempreg = X2CALLING;
- break;
- case '3':
- tempreg = X3CALLING;
- break;
- case '4':
- tempreg = X4CALLING;
- break;
- default:
- return (ERROR); // Return ERROR to indicate invalid character
- }
- S22 &= 0b00011111; // Clear existing calling characteristics
- S22 ^= tempreg; // Modify S22 to reflect new settings
- return (OK); // Return OK to indicate success
- }
- unsigned char atzcmd(void) // Reset Modem and recall factory profile
- {
- ATBUFPTR++; // Increment AT Command Buffer pointer.
- switch(ATBUF[ATBUFPTR])
- {
- case '0':
- init_sregs(0); // Recall factory profile 0.
- break;
- case '1':
- init_sregs(1); // Recall factory profile 1.
- break;
- default:
- return (ERROR); // Return ERROR to indicate invalid character
- }
- reset_cbus(); // Perform CMX868 General reset and clear device write shadow registers
- return (OK); // Return OK to indicate success
- }
- unsigned char xatcmd(void)
- {
- ATBUFPTR++; // Increment AT Command Buffer pointer.
- switch(ATBUF[ATBUFPTR])
- {
- case 'F': case 'f':
- return (xatfcmd());
- case 'G': case 'g':
- return (xatgcmd());
- default:
- return (ERROR);
- }
- }
- unsigned char xatfcmd(void) // Recall factory profile
- {
- ATBUFPTR++; // Increment AT Command Buffer pointer.
- switch(ATBUF[ATBUFPTR])
- {
- case '0':
- init_sregs(0); // Recall factory profile 0.
- break;
- case '1':
- init_sregs(1); // Recall factory profile 1.
- break;
- default:
- return (ERROR); // Return ERROR to indicate invalid character
- }
- return (OK); // Return OK to indicate success
- }
- unsigned char xatgcmd(void) // Modify Guard Tone bits located in S23 reg
- {
- unsigned char tempreg;
- ATBUFPTR++; // Increment AT Command Buffer pointer.
- switch(ATBUF[ATBUFPTR])
- {
- case '0':
- tempreg = NOGUARD;
- break;
- case '1':
- tempreg = GUARD550;
- break;
- case '2':
- tempreg = GUARD1800;
- break;
- default:
- return (ERROR); // Return ERROR to indicate invalid character
- }
- S23 &= 0b00111111; // Clear existing Guard tone settings
- S23 ^= tempreg; // Modify S23 to reflect new settings
- return (OK); // Return OK to indicate success
- }
- unsigned char cmlatcmd(void)
- {
- ATBUFPTR++; // Increment AT Command Buffer pointer.
- switch(ATBUF[ATBUFPTR])
- {
- case 'F': case 'f': // CML Test Functions
- return (cmlatfcmd());
- case 'R': case 'r':
- return (cmlatrcmd());
- default:
- return (ERROR);
- }
- }
- unsigned char cmlatfcmd(void) // CML Test Functions
- {
- ATBUFPTR++; // Increment AT Command Buffer pointer.
- switch(ATBUF[ATBUFPTR])
- {
- case '0':
- PORTA = 0b00010000; // Turn off DCDIND LED
- PORTB |= 0b00000110; // Ensure RIN and DCDN o/p's are set
- hook(0); // On-Hook (Hook LED off)
- LED_OVERRIDE = 0;
- break;
- case '1':
- PORTA = 0b00001111; // Turn on DCDIND, RXDIND, TXDIND, DTRIND and HOOK LEDs
- PORTB &= 0b11111001; // Clear RIN and DCDN o/p's
- hook(1); // Off-Hook (Hook LED on)
- LED_OVERRIDE = 1;
- break;
- case '2':
- DelayMs(100); // Wait 100ms
- break;
- case '3':
- Delay1s(1); // Wait 1s
- break;
- case '4':
- T0IE = 0; // Initially disable Timer interrupt
- BERTEND = 0; // BERT Rx End
- BERTFLAG = 1; // Set BERT Flag
- break;
- case '5':
- T0IE = 0; // Initially disable Timer interrupt
- BERTEND = 1; // BERT Tx End
- BERTFLAG = 1; // Set BERT Flag
- break;
- case '6':
- TESTFUNC1 = 1; // Set Test Function One flag
- break;
- case '7':
- GPT6 = 30; // Load GPT6 for 30 second
- while (PICIRQN != 0)
- {
- if (GPT6 == 0)
- {
- return (ERROR); // Return ERROR to indicate invalid character
- }
- }
- CMXSTAT = rd16_cbus(CMXSTAT_ADDR); // Update CMX868 Status Shadow register
- if (HEXOP)
- {
- hexnum2scrn(CMXSTAT,4,WORDRES); // Send LF char if word results enabled
- }
- else
- {
- decnum2scrn(CMXSTAT,5,WORDRES); // Send LF char if word results enabled
- }
- break;
- case '8':
- return (NYI); // Return NYI to indicate function not implemented yet
- case '9':
- return (NYI); // Return NYI to indicate function not implemented yet
- default:
- return (ERROR); // Return ERROR to indicate invalid character
- }
- return (OK); // Return OK to indicate success
- }
- unsigned char cmlatrcmd(void)
- {
- unsigned int j;
- unsigned long i;
- volatile unsigned char bank3 * byteregptr;
- volatile unsigned int bank3 * wordregptr;
- unsigned char cbus_addr, reg16bit, writereg;
- ATBUFPTR++; // Increment AT Command Buffer pointer.
- switch(ATBUF[ATBUFPTR])
- {
- case 'E': case 'e':
- ATBUFPTR++; // Increment AT Command Buffer pointer.
- reg16bit = 1; // Assume 16 bit word unless modified
- writereg = 1; // Assume write register unless modified
- switch(ATBUF[ATBUFPTR])
- {
- case '0':
- cbus_addr = CMXGENCTRL_ADDR;
- wordregptr = &CMXGENCTRL;
- break;
- case '1':
- cbus_addr = CMXTXMODE_ADDR;
- wordregptr = &CMXTXMODE;
- break;
- case '2':
- cbus_addr = CMXRXMODE_ADDR;
- wordregptr = &CMXRXMODE;
- break;
- case '3':
- reg16bit = 0;
- cbus_addr = CMXTXDATA_ADDR;
- byteregptr = &CMXTXDATA;
- break;
- case '4':
- reg16bit = 0;
- cbus_addr = CMXTXDATAV14_ADDR;
- byteregptr = &CMXTXDATAV14;
- break;
- case '5':
- writereg = 0;
- reg16bit = 0;
- cbus_addr = CMXRXDATA_ADDR;
- byteregptr = &CMXRXDATA;
- break;
- case '6':
- writereg = 0;
- cbus_addr = CMXSTAT_ADDR;
- wordregptr = &CMXSTAT;
- break;
- case '8':
- cbus_addr = CMXPROG_ADDR;
- wordregptr = &CMXPROG;
- break;
- case '9':
- cbus_addr = CMXTESTADDR_ADDR;
- wordregptr = &CMXTESTADDR;
- break;
- case 'A': case 'a':
- cbus_addr = CMXTESTWR_ADDR;
- wordregptr = &CMXTESTWR;
- break;
- case 'B': case 'b':
- writereg = 0;
- cbus_addr = CMXTESTRD_ADDR;
- wordregptr = &CMXTESTRD;
- break;
- default:
- return (ERROR);
- }
- ATBUFPTR++; // Increment AT Command Buffer pointer.
- if ((ATBUF[ATBUFPTR] == '=') && writereg)
- {
- ATBUFPTR++; // Increment AT Command Buffer pointer.
- i = ascregtolong();
- if ((i > 65535) || (!reg16bit && (i > 255))) // If 8-bit register and value exceeds 255 return error.
- {
- return (ERROR);
- }
- if (reg16bit)
- {
- *wordregptr = i & 0xFFFF;
- wr16_cbus(cbus_addr,*wordregptr);
- }
- else
- {
- *byteregptr = i & 0xFF;
- wr_cbus(cbus_addr,*byteregptr);
- }
- break;
- }
- else if ((ATBUF[ATBUFPTR] == '?'))
- {
- if (reg16bit)
- {
- if (!writereg) // Read CBUS if 16 bit read register
- {
- *wordregptr = rd16_cbus(cbus_addr); // Place CBUS reply data into shadow register
- }
- j = *wordregptr & 0xFFFF; // Copy 16 bit shadow register
- }
- else
- {
- if (!writereg) // Read CBUS if 8 bit read register
- {
- *byteregptr = rd_cbus(cbus_addr); // Place CBUS reply data into shadow register
- }
- j = *byteregptr & 0xFF; // Copy 8 bit shadow register
- }
- if (HEXOP)
- {
- hexnum2scrn(j,4,WORDRES); // Send LF char if word results enabled
- }
- else
- {
- decnum2scrn(j,5,WORDRES); // Send LF char if word results enabled
- }
- break;
- }
- else
- {
- return (ERROR);
- }
- case 'R': case 'r':
- reset_cbus(); // Perform CMX868 General Reset and clear device write shadow registers
- break;
- default:
- return (ERROR);
- }
- return (OK); // Return OK to indicate success
- }
- unsigned long ascregtolong(void)
- {
- unsigned char tempbuf[6];
- unsigned long tempnum=0;
- unsigned long j=1;
- signed char i,k;
- i=0;
- // Loop while characters are decimal digits
- // and are no more than 5 digits long i.e. 65535
- while ((ATBUF[ATBUFPTR] <= '9') && (ATBUF[ATBUFPTR] >= '0') && (i < 6))
- {
- tempbuf[i++] = ATBUF[ATBUFPTR++];
- }
- ATBUFPTR--;
- i--;
- for (k=i;k>=0;k--)
- {
- tempnum += (tempbuf[k] - 0x30) * j;
- j *= 10;
- }
- return (tempnum);
- }