⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 common.c

📁 ppp拨号程序源代码采用atmel芯片c语言编
💻 C
📖 第 1 页 / 共 3 页
字号:

/*
 * Copyright (C) 2003-2004 by Clive Moss. Email c.a.m@blueyonder.co.uk All rights reserved.
 *
 * Help & Contributions from D.J.Armstrong Email heli.pad@ntlworld.com
 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of the copyright holders nor the names of
 *    contributors may be used to endorse or promote products derived
 *    from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY CLIVE MOSS 'AS IS' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.
 * IN NO EVENT SHALL CLIVE MOSS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 * INDIRECT, INCIDENTAL,SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
 * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

//#ifdef CPU_eZ8
//	#pragma stkck									// enable stack checking
//#endif

#ifdef CPU_eZ8
	#include <eZ8.h>
#endif	

#ifdef CPU_ATmega128
	#include <iom128v.h>
	#include <macros.h>
#endif

#include <string.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>

#include "common.h"
#include "fcs.h"
#include "at.h"
#include "ppp.h"
#include "ip.h"

#ifdef IncludeTCP
#include "tcp.h"
#endif

#ifdef IncludeICMP
#include "icmp.h"
#endif

#ifdef IncludeUDP
#include "udp.h"
#endif

#ifdef IncludeNTP
#include "ntp.h"
#endif

// *********************************************************************************
// Flash constants

flash char	Str3[]					=	"\n#####\nRebooted .....\n\n";

flash u16	Version					=	0x0028;				// version (BCD)
flash char	VersionStr[]			=	"Ver: v";

flash char	Title[]					=	"AT/PPP/ICMP/UDP/TCP by C.Moss\n";
flash char	Date[]					=	"5th Feb 2004\n";

flash char	HelpStr[]				=	"\n"
										"  help or ? ........ guess\n"
										"  ipconfig ......... display ip addresses\n"
										"  dial ............. connect\n"
										"  disc ............. disconnect\n"
										#ifdef Debug
										"  debug off ........ debug messages off\n"
										"  debug on ......... debug messages on\n"
										#endif
										#ifdef IncludeICMP
										"  ping <ip> ........ ping\n"
										#endif
										#ifdef IncludeTCP
										"  tcp <ip>[:port] .. tcp into a server\n"
										"  tcp down ......... tcp disconnect\n"
										#endif
										#ifdef IncludeNTP
										"  ntp <ip> ......... sync clock to ntp server\n"
										#endif
										#ifndef WindowsPPP
										"  at<modem cmd> .... gets sent to modem\n"
										#endif
										"  reboot ........... reboot!\n";

#ifdef Debug
flash char	DebugMessagesStr1[]		=	"\n*** Debug Msgs: ";
flash char	DebugMessagesStr2[]		=	"off\n";
flash char	DebugMessagesStr3[]		=	"on\n";
#endif

flash char	RebootingStr[]			=	"\nRebooting ..\n";

#ifdef Debug
flash char	Str4[]					=	"\nButton\n";
#endif

// *********************************************************************************
// EEPROM locations - 4096 bytes
//
// every entry must have it's own crc.
// This means we don't loose the whole lot due to a single byte corruption - which would be
// the case if we just crc error checked the entire eeprom in one go.

#ifdef CPU_ATmega128

#define Unit_ID_Len			32	
#define MaxPhoneNumLen		32

#pragma data:eeprom

char			Unit_ID[Unit_ID_Len]			= {""};												// Unit ID
u16				Unit_ID_crc						= 0xffff;

T_IP_Addr		ServerIP1						= {0, 0, 0, 0};										// primary server ip
u16				ServerIP1_crc					= 0xffff;

T_IP_Addr		ServerIP2						= {0, 0, 0, 0};										// seconday server ip
u16				ServerIP2_crc					= 0xffff;

char			ServerPhoneNum1[MaxPhoneNumLen]	= {""};												// primary server phone number
u16				ServerPhoneNum1_crc				= 0xffff;

char			ServerPhoneNum2[MaxPhoneNumLen]	= {""};												// secondary server phone number
u16				ServerPhoneNum2_crc				= 0xffff;

#pragma data:data

#endif

// *********************************************************************************

#ifdef CPU_eZ8

near u8				LastResetReason = 0;

volatile near u8	WatchdogCounter = 0;

volatile near u8	Flags1 = 0;
volatile near u8	Flags2 = 0;

volatile near u8	TimerIntCounter = 0;

volatile near u8	button_push = 255;

volatile near u32	Random32;

near u16			MainBufferRd_Rx = 0;
near s16			MainBufferWr_Rx = -1;
near u16			MainBufferWr_Tx = 0;

volatile near u8	UART0_RxBuffer[32];			// UART-0 ring buffer
volatile near u8	UART0_RxBufferWr = 0;
volatile near u8	UART0_RxBufferRd = 0;

volatile near u8	UART1_RxBuffer[96];			// UART-1 ring buffer
volatile near u8	UART1_RxBufferWr = 0;
volatile near u8	UART1_RxBufferRd = 0;

#endif

#ifdef CPU_ATmega128

u8				LastResetReason = 0;

volatile u8		WatchdogCounter = 0;

volatile u8		Flags1 = 0;
volatile u8		Flags2 = 0;

volatile u8		TimerIntCounter = 0;

volatile u8		button_push = 255;

volatile u32	Random32;

u16				MainBufferRd_Rx = 0;
s16				MainBufferWr_Rx = -1;
u16				MainBufferWr_Tx = 0;

volatile u8		UART0_RxBuffer[32];			// UART-0 ring buffer
volatile u8		UART0_RxBufferWr = 0;
volatile u8		UART0_RxBufferRd = 0;

volatile u8		UART1_RxBuffer[96];			// UART-1 ring buffer
volatile u8		UART1_RxBufferWr = 0;
volatile u8		UART1_RxBufferRd = 0;

#endif

volatile u16	ADC_Input[8];				// this is where the ADC input values are stored

u8				CommandBuffer[32];			// console command buffer
u8				ScratchPad[256];			// general usage buffer for strings etc

//u8				MainBuffer[1550];			// must be big enough for the 1500 ppp/udp/tcp packets
u8				MainBuffer[MainBufferSize];	// - sod that, we just aint got the ram

// *********************************************************************************
// macros

#define BRG(freq, baud) ((unsigned long)(freq) / ((unsigned long)(baud) * 16))

// *********************************************************************************

#ifdef CPU_ATmega128

void _StackOverflowed(char c)
{	// we end up here if a stack overflow occured - this replaces iccavr's routine

	Disable_Ints();    
	Reset_WD();

// c = 0 ... software stack overflow
// c = 1 ... hardware stack overflow

	#ifdef Debug
   	SendDebugStr("\n\n*** Stack Overflow\n");	// doubt if this will work - the stack is messed up :(
	#endif
   
	for (;;);						// infinate loop - let the watch dog reset us
}

#endif

// **************************************************************************
// saftly read multi-byte values from the executive that are modified by an interrupt.
// at the moment, I'm just disabling global ints while I read/set the value - maybe not be the best way to go about this

void u16_Put(volatile u16 *pnter, u16 w)
{
	#ifdef CPU_eZ8
	*pnter = w;									// read the value    
	#endif

	#ifdef CPU_ATmega128
	u8 savedSREG = SREG;						// keep interrupt setting
	Disable_Ints();								//
	*pnter = w;									// set the value    
	SREG = savedSREG;							// restore interrupt setting
	#endif
}
  
u16 u16_Get(volatile u16 *pnter)
{
	#ifdef CPU_eZ8
	return *pnter;								// read the value    
	#endif

	#ifdef CPU_ATmega128
	u16	w;
	u8 savedSREG = SREG;						// keep interrupt setting
	Disable_Ints();								//
	w = *pnter;									// read the value    
	SREG = savedSREG;							// restore interrupt setting
	return w;									//
	#endif
}
  
s16 s16_Get(volatile s16 *pnter)
{
	#ifdef CPU_eZ8
	return *pnter;								// read the value    
	#endif

	#ifdef CPU_ATmega128
	s16	i;
	u8 savedSREG = SREG;						// keep interrupt setting
	Disable_Ints();								//
	i = *pnter;									// read the value    
	SREG = savedSREG;							// restore interrupt setting
	return i;									//
	#endif
}
  
void u32_Put(volatile u32 *pnter, u32 dw)
{
	#ifdef CPU_eZ8
	*pnter = dw;								// set the value    
	#endif

	#ifdef CPU_ATmega128
	u8 savedSREG = SREG;						// keep interrupt setting
	Disable_Ints();								//
	*pnter = dw;								// set the value    
	SREG = savedSREG;							// restore interrupt setting
	#endif
}
  
u32 u32_Get(volatile u32 *pnter)
{
	#ifdef CPU_eZ8
	return *pnter;								// read the value    
	#endif

	#ifdef CPU_ATmega128
	u32	dw;
	u8 savedSREG = SREG;						// keep interrupt setting
	Disable_Ints();								//
	dw = *pnter;								// read the value    
	SREG = savedSREG;							// restore interrupt setting
	return dw;									//
	#endif
}
  
// **************************************************************************

#ifdef CPU_ATmega128

void EPROMWrite(u16 Addr, u8 Data)
{
	u8 savedSREG;

	savedSREG = SREG;							// keep setting so the interrupt setting can be restored
	Disable_Ints();								//    
												//
	for (; bit_is_set(EECR, EEWE); );			// wait for previous write to finish
	EEAR = Addr;								// set address
	EEDR = Data;								// set data
	sbi(EECR, EEMWE);							// set "write enable" bit
	sbi(EECR, EEWE);							// set "write" bit
												//  
	SREG = savedSREG;							// restore SREG - ie, the CLI/SEI state
	EEAR = 0;									//
}

u8 EPROMRead(u16 Addr)
{
	for (; bit_is_set(EECR, EEWE); );			// wait for previous write to finish
	EEAR = Addr;								// set address
	sbi(EECR, EERE);							// set "read enable" bit
	EEAR = 0;									//
	return (u8)EEDR;							// return the EEPROM byte
}

void EPROMWrite_Data(u16 Addr, u8 *src, u16 len)
{
	u8	c;
	u16	crc;

	if (!src)											//
	{													// fill with '0'
		for (; len; len--) EPROMWrite(Addr++, 0);		// data
		EPROMWrite(Addr++, 0xff);						// crc LS-Byte
		EPROMWrite(Addr, 0xff);							// crc MS-Byte
		return;											//
	}

	crc = 0xffff;										// init crc
	crc = UpdateFCS_16(crc, 10);						// update crc
	crc = UpdateFCS_16(crc, (u8)(len & 0xff));			// update crc
	crc = UpdateFCS_16(crc, (u8)(len >> 8));			// update crc
	for (; len; len--)									//
	{													//
		c = *src++;										// get byte
		crc = UpdateFCS_16(crc, c);						// update crc
		EPROMWrite(Addr++, c);							// save it into the eeprom
	}													//
	crc = ~crc;											// finalize the crc    
	EPROMWrite(Addr++, (u8)(crc & 0xff));				// save LS-Byte of crc
	EPROMWrite(Addr++, (u8)(crc >> 8));					// save MS-Byte of crc
}

bool EPROMRead_Data(u16 Addr, u8 *dest, u16 len)
{
	u8	c;
	u16	i, j;
	u16	crc1, crc2;

	if (!dest) return false;

	// first check the data is valid
	crc1 = 0xffff;										// init crc
	crc1 = UpdateFCS_16(crc1, 10);						// update crc
	crc1 = UpdateFCS_16(crc1, (u8)(len & 0xff));		// update crc
	crc1 = UpdateFCS_16(crc1, (u8)(len >> 8));			// update crc
	i = Addr;											// eeprom address to read from
	for (j = len; j; j--)								//
	{													//
		c = EPROMRead(i++);								// read byte from eeprom
		crc1 = UpdateFCS_16(crc1, c);					// update crc
	}													//
	crc1 = ~crc1;										// finalize the crc    
	crc2 = (u16)EPROMRead(i++);							// read LS-Byte of crc
	crc2 |= ((u16)EPROMRead(i)) << 8;					// read MS-Byte of crc
	if (crc1 != crc2)									//
	{													//
		memset(dest, 0, len);							//	
		return false;									// data is invalid
	}													//

	// go get the data from the eeprom
	for (; len; len--) *dest++ = EPROMRead(Addr++);		// read byte from eeprom
														//
	return true;										// data is valid
}

#endif

// *********************************************************************************
// set/get the unit id

#ifdef CPU_ATmega128

void Set_UnitID(char *s)
{
	int i;

	char	UnitID[Unit_ID_Len];

	if (!s)															//
	{																//
		EPROMWrite_Data((u16)&Unit_ID, 0, Unit_ID_Len);				//
		return;														//
	}																//

	memset(UnitID, 0, Unit_ID_Len);									//
	i = strlen(s);													//
	if (i >= Unit_ID_Len) i = Unit_ID_Len - 1;						// 
	strncpy(UnitID, s, i);											//
	EPROMWrite_Data((u16)&Unit_ID, (u8*)UnitID, Unit_ID_Len);		// write it to eeprom	
}

bool Get_UnitID(char *buf)
{
	return EPROMRead_Data((u16)&Unit_ID, (u8*)buf, Unit_ID_Len);	//
}
      
#endif
   
// *********************************************************************************
// test the 32K SRAM

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -