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

📄 main.c

📁 ppp拨号程序源代码采用atmel芯片c语言编
💻 C
📖 第 1 页 / 共 2 页
字号:
	// Define external memory mapping
  
	#ifdef CPU_ATmega128
	asm(	".area memory(abs)\n"
			".org 0x1100\n"
			" _SRAM:: .blkb 1\n"
			".text\n");
	#endif

	// ****************************
	// set the watchdog timeout ASAP - you'll find the timeout value in common.h
	// see page 158 of the Z8F6423 product spec's pdf
	// the file config\Z8F6423.ini for the option bit settings - changing these makes no difference :(

	#ifdef CPU_eZ8
	WDTCTL = 0x55;									// unlock the watchdog - so we can set it's time out
	WDTCTL = 0xAA;									//   "    "    "
	WDTU = (u8)((WatchdogTimeout >> 16) & 0xff);	// Set the timeout
	WDTH = (u8)((WatchdogTimeout >> 8) & 0xff);		//  "   "     "
	WDTL = (u8)(WatchdogTimeout & 0xff);			//  "   "     " 
    #endif

	Reset_WD();

	#ifdef CPU_ATmega128
	WDTCR |= (1 << WDCE) | (1 << WDE);										// Enable watchdog config change
	WDTCR = (1 << WDE) | (1 << WDP2) | (1 << WDP1) | (1 << WDP0);		// Enable watchdog to 2 second time out
	#endif
    
	// ****************************
	// disable all interrupts as default - or should i trust that the reset condition is reliable ?

	#ifdef CPU_eZ8
	IRQ0ENH = 0;							// disable each individual interrupt
	IRQ0ENL = 0;							//
											//
	IRQ1ENH = 0;							//
	IRQ1ENL = 0;							//
											//
	IRQ2ENH = 0;							//
	IRQ2ENL = 0;							//
	#endif
    
	// ****************************
	// set all ports to input state as default

	#ifdef CPU_eZ8
	PADD = 0xff;	// as inputs
	PAADDR = 0;		// clear to protect sub registers
					//
	PBDD = 0xff;	// as inputs
	PBADDR = 0;		// clear to protect sub registers
					//
	PCDD = 0xff;	// as inputs
	PCADDR = 0;		// clear to protect sub registers
					//
	PDDD = 0xff;	// as inputs
	PDADDR = 0;		// clear to protect sub registers
					//
	PEDD = 0xff;	// as inputs
	PEADDR = 0;		// clear to protect sub registers
					//
	PFDD = 0xff;	// as inputs
	PFADDR = 0;		// clear to protect sub registers
					//
	PGDD = 0xff;	// as inputs
	PGADDR = 0;		// clear to protect sub registers
					//
	PHDD = 0xff;	// as inputs
	PHADDR = 0;		// clear to protect sub registers
	#endif
    
	#ifdef CPU_ATmega128
    PORTA = 0xff;	// enable pull-ups
    DDRA = 0x00;	// as inputs

    PORTB = 0xff;	// enable pull-ups
    DDRB = 0x00;	// as inputs

    PORTC = 0xff;	// enable pull-ups
    DDRC = 0x00;	// as inputs

    PORTD = 0xff;	// enable pull-ups
    DDRD = 0x00;	// as inputs

    PORTE = 0xff;	// enable pull-ups
    DDRE = 0x00;	// as inputs

    PORTF = 0xfc;	// enable pull-ups
    DDRF = 0x00;	// as inputs

    PORTG = 0xff;	// enable pull-ups
    DDRG = 0x00;	// as inputs
	#endif
        
	// ****************************
	// delay a bit - stops irratic boot-ups when programming etc
   
	for (i = 0; i < 25; i++)
	{
		for (j = 0; j < 32767; j++)
		{
			Reset_WD();
		}           
	}

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

	#ifdef CPU_eZ8
	RLed_ddPort &= ~(1 << RLed_Pin);		// set as output
	RLed_hdePort &= ~(1 << RLed_Pin);		// disable high drive
	RLed_addrPort = 0;						// clear to protect sub registers
	RLed_Off;								//

	YLed_ddPort &= ~(1 << YLed_Pin);		// set as output
	YLed_hdePort &= ~(1 << YLed_Pin);		// disable high drive
	YLed_addrPort = 0;						// clear to protect sub registers
	YLed_Off;								//

	GLed_ddPort &= ~(1 << GLed_Pin);		// set as output
	GLed_hdePort &= ~(1 << GLed_Pin);		// disable high drive
	GLed_addrPort = 0;						// clear to protect sub registers
	GLed_Off;								//

	RTS0_ddPort &= ~(1 << RTS0_Pin);		// set as output
	RTS0_hdePort &= ~(1 << RTS0_Pin);		// disable high drive
	RTS0_addrPort = 0;						// clear to protect sub registers
	RTS0_OK;								//

	RTS1_ddPort &= ~(1 << RTS1_Pin);		// set as output
	RTS1_hdePort &= ~(1 << RTS1_Pin);		// disable high drive
	RTS1_addrPort = 0;						// clear to protect sub registers
	RTS1_OK;								//
    #endif

	#ifdef CPU_ATmega128
	sbi(RLed_ddPort,  RLed_Pin);			// set as output
	RLed_Off;								//

	sbi(YLed_ddPort, YLed_Pin);				// set as output
	YLed_Off;								//

	sbi(GLed_ddPort, GLed_Pin);				// set as output
	GLed_Off;								//

	sbi(SS_ddPort, SS_Pin);					// set as output
	sbi(SS_oPort, SS_Pin);					// set high
    
	sbi(SCLK_ddPort, SCLK_Pin);				// set as output
	sbi(SCLK_oPort, SCLK_Pin);				// set high
    
	sbi(MOSI_ddPort, MOSI_Pin);				// set as output
	sbi(MOSI_oPort, MOSI_Pin);				// set high
    
	sbi(TXD0_ddPort, TXD0_Pin);				// set as output
	sbi(TXD0_oPort, TXD0_Pin);				// set high
    
	sbi(RTS0_ddPort, RTS0_Pin);				// set as output
	RTS0_OK;								//

	sbi(TXD1_ddPort, TXD1_Pin);				// set as output
	sbi(TXD1_oPort, TXD1_Pin);				// set high
    
	sbi(RTS1_ddPort, RTS1_Pin);				// set as output
	RTS1_OK;								//

	sbi(SRAM_CS_ddPort, SRAM_CS_Pin);		// set as output
	SRAM_Disable;							//					
	#endif
    
	// ****************************
    
	#ifdef CPU_ATmega128
	XDIV = 0;											// xtal divider - run at xtal clock freq
	   													//
	EICRA = 0;											// no extended ext ints
	EICRB = 0;											// no extended ext ints
	EIMSK = 0;											// no external ints (INT0-INT7)
	EIFR = 0;											//
   														//
	MCUCR = (1<<SRE);									// enable external memory
	XMCRA = (1<<SRL2)|(1<<SRL0);						// lower sector = 0x1100 - 0x9fff .. no wait states
//   XMCRA = (1<<SRL2)|(1<<SRL0)|(1<<SW00);				// lower sector = 0x1100 - 0x9fff .. 1 wait state
//   XMCRA = (1<<SRL2)|(1<<SRL0)|(1<<SW01);				// lower sector = 0x1100 - 0x9fff .. 2 wait states
//   XMCRA = (1<<SRL2)|(1<<SRL0)|(1<<SW01)|(1<<SW00);	// lower sector = 0x1100 - 0x9fff .. 2 wait states during read/write, wait one cycle before driving out new address
	XMCRB = (1<<XMM0);									// leave pin PC7 free for user use
														//
	ASSR = 0;											//
														//
	TIMSK = 0;											// timer interrupt sources
	ETIMSK = 0;											// extended timer interrupt sources
	#endif
    
	// ****************************
	// setup a 10ms timer interrupt

	#ifdef CPU_eZ8
	// Initialize Timer-0
	SET_VECTOR(TIMER0, isr_timer0); 		// Set timer interrupt vector
 	T0H		= (u8)(TimerIntSpeed >> 8);		// Timer High	
	T0L 	= (u8)(TimerIntSpeed & 0xff);	// Timer Low
	T0RH 	= (u8)(TimerIntSpeed >> 8);		// Reload Compare High
	T0RL 	= (u8)(TimerIntSpeed & 0xff);	// Reload Compare Low
	T0CTL 	= (1<<TEN)|(1<<TPOL)|(1<<PRES2)|(1<<PRES1)|(1<<PRES0)|(1<<TMODE0);			// Enabled, clk/128, Continous mode
	IRQ0ENH |= (1<<T0EN);					// Set Interrupt Priority Nominal 
	IRQ0ENL &= ~(1<<T0EN);					//  "
    #endif

	#ifdef CPU_ATmega128
	// Initialize Timer-1
	TCCR1B = 0;								// stop
   											//
	TCNT1 = TimerIntSpeed;					// reload counter high value
//	TCNT1H = (u8)(TimerIntSpeed >> 8);		// reload counter high value
//	TCNT1L = (u8)(TimerIntSpeed & 0xff);	//   "
   											//
	OCR1A = 9216;							//
	OCR1B = 9216;							//
	OCR1C = 9216;							//
	ICR1 = 9216;							//
   											//
	TCCR1A = 0;								//
	TCCR1B = (1<<CS11);						// start Timer, MainClk/8
											//
	sbi(TIMSK, TOIE1);						// Enable timer overflow interupt
	#endif
    
	// ****************************
	// Initialize Uart-0 (Console/Debug terminal)

	InitUart(UART0, UART0_BaudRate);		// Setup uart 

	#ifdef CPU_eZ8
	SET_VECTOR(UART0_RX, isr_uart0_rx);		// setup an RX interrupt
											//
	IRQ0ENH |= (1<<U0REN);					// Set Interrupt Priority normal
	IRQ0ENL &= ~(1<<U0REN);					//  "
    #endif

	// ****************************
	// Initialize Uart-1 (PPP)

	InitUart(UART1, UART1_BaudRate);		// Setup uart

	#ifdef CPU_eZ8
	SET_VECTOR(UART1_RX, isr_uart1_rx);		// setup an RX interrupt
											//
	IRQ2ENH |= (1<<U1REN);					// Set Interrupt Priority high
	IRQ2ENL |= (1<<U1REN);					//  "
    #endif

	// ****************************
	// Initialize the SPI - though we don't use it yet

	SPI_Init();

	// ****************************
	// setup the ADC

	#ifdef CPU_eZ8
	#endif

	#ifdef CPU_ATmega128
	SFIOR = 0;												//
															//
	ADCSRA = 0;												// disable adc
															//
	cbi(PORTF, 0);											// disable pull-up on ADC-0 input
	cbi(PORTF, 1);											// disable pull-up on ADC-1 input
//	cbi(PORTF, 2);											// disable pull-up on ADC-2 input
//	cbi(PORTF, 3);											// disable pull-up on ADC-3 input
//	cbi(PORTF, 4);											// disable pull-up on ADC-4 input
//	cbi(PORTF, 5);											// disable pull-up on ADC-5 input
//	cbi(PORTF, 6);											// disable pull-up on ADC-6 input
//	cbi(PORTF, 7);											// disable pull-up on ADC-7 input
															//
	ADMUX = 0;												// reference = AREF pin, Right adjusted, select adc input 0,
	ACSR = (1<<ACD);										// disable the Analogue comparator
	ADCSRA = (1<<ADEN)|(1<<ADPS2)|(1<<ADPS1)|(1<<ADPS0);	// enable the ADC, Prescaler = 128 (Conversion time = 224uS)
	sbi(ADCSRA, ADSC);										// start a conversion
															//																
	#endif

	memset((void*)ADC_Input, 0, sizeof(ADC_Input));			// clear our ADC values - the timer interrupt saves the ADC values into this buffer
 
	// ****************************
	// Initialize Test Button interrupt on port-C - we don't use it though as we just poll the pin in the timer interrupt

	#ifdef CPU_eZ8
//	SET_VECTOR(C0, isr_C0);							// set interrupt vector
//	IRQ2ENH |= (1<<C0EN);							// Set Interrupt Priority High
//	IRQ2ENL |= (1<<C0EN);							//  "
    #endif

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

	WatchdogCounter = 0;							//

	CommandBuffer[0] = 0;							//

	MainBufferWr_Rx = -1;							//
	MainBufferWr_Tx = 0;							//

	AT_Stage(AT_Idle);								//
													//
	PPP.Stage = PPPS_None;							//
													//
	#ifdef IncludeTCP
		#ifdef StaticTCPSocket
			TCP_Socket = (T_TCP_Socket*)&TCP_Static_Socket;	//
			TCP_SocketStage(TCP_Socket, TCP_CLOSED);		//
		#else
			TCP_Socket = NULL;								//
		#endif
	#endif

	// ****************************
	// test to see if the button is pressed when booting up

	if (!TestButton)
	{	// the button is pressed
		RLed_On;										//
		Flags1 |= (1<<Flags1_Button);					// set flag
														//
		// wait for the button to be released - short debounce detector
		for (i = 32767; i; i--)							//
		{												//
			Reset_WD();									//
			if (!TestButton) i = 32767;					// button is still pressed
		}												//
														//       
		RLed_Off;										//

		#ifdef CPU_ATmega128
		Set_UnitID("test 123456");						// reset our ID
		#endif
	}

	// ****************************
	// main loop

	#ifdef Debug
	Flags1 |= (1<<Flags1_Debug);					// enable debug as default - for now
	#endif

	Enable_Ints();									// enable global interrupts
    
	#ifdef Debug
		SendDetails();								//
		SendHelp();									//
	#endif	

	PPP_Reset(0, 0);								//

	#ifdef CPU_ATmega128
	_StackCheck();									// check for stack overflow
	#endif
     
//	_StackOverflowed(0);							// test our stack overflow routine
//	StackOverflowTest(0);							// ditto

	// **************
	// main executive loop

	for (;;)
	{
		// *************
		// tell timer interrupt to keep the watchdog at bay

		if (!WatchdogCounter) WatchdogCounter++;								//

		// *************
		// do stuff when the push button is pressed

		if (Flags1 & (1<<Flags1_ButtonPress))									//
		{																		// Button has been pressed
			Flags1 &= ~(1<<Flags1_ButtonPress);									// clear the flag
																				//
			#ifdef Debug
				SendDebugRStr(Str4);											//
			#endif
																				//
			if (AT.Stage == AT_Idle)											//
				AT_Start();														// start a ppp link
			else																//
				AT_End();														// terminate the ppp link
		}																		//

		// *************
		// do stuff to keep things rolling - AT Modem, PPP, TCP etc

		ProcessMainLoop();														//

		// *************
		// deal with rx'ing and tx'ing data from/to the tcp socket

		#ifdef IncludeTCP
		if (TCP_Socket == NULL) continue;										// no tcp socket to process
		if (TCP_Socket->Stage != TCP_ESTABLISHED) continue;						// the tcp socket is not connected
																				//
		i = TCP_RxBytes(TCP_Socket);											// number of bytes the tcp socket has for us
		j = TCP_TxFree(TCP_Socket);												// number of bytes free in the tcp socket tx buffer
		if ((i <= 0) || (j <= 0)) continue;										// no data in rx buffer or no space left in tx buffer
																				//
		if (i > sizeof(ScratchPad)) i = sizeof(ScratchPad);						// limit to the temp buffer size
																				//
		if (!TCP_Socket->OutGoing)												//
		{																		// echo back - as we are in server mode
			if (i > j) i = j;													// limit the amount of data we move from tcp rx buffer back into the tcp tx buffer
			i = TCP_RxData(TCP_Socket, ScratchPad, i);							// move data from tcp rx buffer into temp buffer
			TCP_TxData(TCP_Socket, ScratchPad, i);								// move data from temp buffer into the tcp tx buffer
		}																		//
		else																	//
		{																		// don't echo back - as we are in client mode
			i = TCP_RxData(TCP_Socket, ScratchPad, i);							// move data from tcp rx buffer into temp buffer
																				//
		}																		//
		#ifdef Debug
		SendDebugAsciiDump(ScratchPad, i);										// display the buffer contents on the console
		#endif
		#endif

		// *************
	}

	// **************
}

⌨️ 快捷键说明

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