www.pudn.com > pppcode.rar > modemdrv.c


/*///////////////////////////////////////////////////////////////////////////// 
File Name : ModemDrv.C 
Author : Rene Trenado 
Location : Motorola Applications Lab, Baja California 
Date Created : December 2000 
Current Revision : 0.0 
Notes : This file contains the functions required to handle an external modem 
/////////////////////////////////////////////////////////////////////////////*/ 
 
#include "commdrv.h" 
#include "modemdrv.h" 
#define MODEM_BUFFER_SIZE 32 // Size of Modem Buffer 
#define DTR_ON P1_0=1; // DTR Pin is PORTD0, Macro to set it ON 
#define DTR_OFF P1_0=0; // Macro to set DTR OFF 
 
// Byte pointers of the ring buffer (FIFO) 
volatile BYTE mDataSlot = 0; // Points to the next available character 
volatile BYTE mEmptySlot = 0; // Points to next available slot of the FIFO 
static BYTE *ModemBuffer; // Pointer to Modem buffer 
/*********************************************************************** 
Function : ModemInit 
Parameters : None 
Date : December 2000 
Desc : Initializes the ring buffer & clears the DTR pin 
***********************************************************************/ 
void ModemInit (void) { 
	mDataSlot = 0; // Initialize FIFO Modem pointers 
	mEmptySlot = 0; 
//	DTR_OFF; // DTR Off 
} 
/*********************************************************************** 
Function : ModemBuffFlush 
Parameters : None 
Date : January 2001 
Desc : Flushes the receiving FIFO (ring buffer) 
***********************************************************************/ 
void ModemBuffFlush (void) { 
	mDataSlot = mEmptySlot; 
} 
/*********************************************************************** 
Function : ModemDial 
Parameters : A string containing the phone number to dial 
Date : December 2000 
Desc : It sets the modem response mode to numeric (instead of verbose), 
then it dials a phone number & sets the DTR pin. This function 
returns a numeric code describing a response from the modem or 
a timeout. Applications should handle this reaponse code. 
***********************************************************************/ 
BYTE ModemDial (char * Number) { 
	signed char delayCount = 80; 
	transmit ("ATV0\r"); // Force a numeric response from modem 
	if (!Waitfor ("0", 30)) { // Wait for an OK response 
		return -1; 
	} 
	DTR_ON; // Set DTR to ON 
	transmit ("ATDT"); // Dial the ISP number 
	transmit (Number); 
	transmit ("\r"); 
	ModemBuffFlush (); // Flush contents of buffer 
	// Wait for a reply 
	while ((!ModemBuffNotEmpty()) && (--delayCount > 0)) { 
		Delay (250); 
	} 
	if (delayCount) { 
		return ModemGetch (); // Return the numeric response to caller 
	} 
	return -1; // No response received from modem 
} 
/*********************************************************************** 
Function : ModemHangUp 
Parameters : None 
Date : December 2000 
Desc : This function clears DTR to force the modem to hang up if 
it was on line and/or make the modem to go to command mode. 
***********************************************************************/ 
void ModemHangUp (void) { 
	DTR_ON; // Make a DTR transition to hang-up 
	Delay (40); // Wait a couple of miliSeconds 
	DTR_OFF; // Finish the DTR transition 
} 
/*********************************************************************** 
Function : ModemOnLine 
Parameters : None 
Date : January 2001 
Desc : Returns the status of the CD (carrier detect) signal. 
***********************************************************************/ 
BYTE ModemOnLine (void) { 
	return P1^1 ;// Return the status of the CD line 
} 
/*********************************************************************** 
Function : ModemBindBuff 
Parameters : A pointer to a buffer in RAM 
Date : January 2001 
Desc : Binds the FIFO capabilities of this module to a buffer 
in RAM. 
***********************************************************************/ 
void ModemBindBuff (BYTE *lpInBuffer) { 
	ModemBuffer = lpInBuffer; 
	ModemBuffer [0] = 0;//?? 
} 
/*********************************************************************** 
Function : ModemReset 
Parameters : None 
Date : January 2001 
Desc : Resets the Modem 
***********************************************************************/ 
void ModemReset (void) { 
	ModemInit (); 
} 
/*********************************************************************** 
Function : ModemBuffNotEmpty 
Parameters : None 
Date : January 2001 
Desc : Returns True if modem buffer NOT empty, false otherwise. 
***********************************************************************/ 
BYTE ModemBuffNotEmpty (void) { 
	return !(mDataSlot == mEmptySlot); 
} 
/*********************************************************************** 
Function : ModemInBufferCount 
Parameters : None 
Date : January 2001 
Desc : Returns the number of characters available in the Modem 
Queue. 
***********************************************************************/ 
BYTE ModemInBufferCount (void) { 
	if ((mEmptySlot - mDataSlot) >= 0) 
		return (BYTE)(mEmptySlot - mDataSlot); 
	else { 
		return (BYTE)((mEmptySlot + MODEM_BUFFER_SIZE) - mDataSlot); 
	} 
} 
/*********************************************************************** 
Function : Waitfor 
Parameters : A string to wait for 
A Time out value 
Date : January 2001 
Desc : Returns True if Modem response matches the String argument, 
False otherwise. Time is the number of times the Delay funtion 
will be called from within the waiting loop. 
***********************************************************************/ 
BYTE Waitfor (BYTE* String, BYTE Time) { 
	BYTE c = 0; 
	BYTE Offset = 0; 
	while (Time-- > 0) { 
		Delay (100); // Wait =~ 150 mSec 
		while (ModemBuffNotEmpty()) { // Wait for characters 
			c = ModemGetch (); // Extract a character from FIFO 
			if (c==*(String[Offset])) { // Is C a part of the string? 
				Offset++; // Compare with next character 
				if (*(String[Offset])==0) { // is this the end of string? 
					return True; // match = True 
				} 
			} 
			else // c does not belong to String 
				Offset = 0; // Reset String pointer 
		} 
	} 
	return False; 
} 
/*********************************************************************** 
Function : ProcModemReceive 
Parameters : A character received from the SCI 
Date : November 2000 
Desc : Stores incoming characters in the Modem Queue 
***********************************************************************/ 
void ProcModemReceive (BYTE c) { 
	ModemBuffer [mEmptySlot++] = c; 
	if (mEmptySlot > MODEM_BUFFER_SIZE) { 
		mEmptySlot = 0; 
	} 
} 
/*********************************************************************** 
Function : ModemGetch 
Parameters : None 
Date : November 2000 
Desc : Dequeue a previously stored character in the Modem Queue. 
Returns a null character if the Queue is empty 
***********************************************************************/ 
BYTE ModemGetch (void) { 
	BYTE c = 0; 
	if (mDataSlot != mEmptySlot) { 
		c = ModemBuffer [mDataSlot]; 
		mDataSlot++; 
		if (mDataSlot > MODEM_BUFFER_SIZE) mDataSlot = 0; 
		return(c); 
	} 
	else { 
		return (BYTE)0x00; 
	} 
} 
/*********************************************************************** 
Function : transmit 
Parameters : A string to transmit to the Modem 
Date : November 2000 
Desc : Any data passed to this function will be sended to the Modem. 
Applications can build complex scripts by calling transmit and 
Waifor functions however, its up to the application to control 
the appropriate flow of data when the Modem is on command mode 
and on-line mode. 
***********************************************************************/ 
void transmit (char *dt) { 
	Delay (250); 
	while (*dt) { 
		WriteComm (*dt++); 
	} 
}