📄 main.c
字号:
// This program tests the ppp routine and shows how to use it
/*
* 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
#include "common.h"
#ifdef CPU_eZ8
#include <eZ8.h>
#endif
#ifdef CPU_ATmega128
#include <iom128v.h>
#include <macros.h>
#endif
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
//#include <spi.h>
#include "main.h"
#include "common.h"
#include "fcs.h"
#include "at.h"
#include "ppp.h"
#include "ip.h"
#ifdef IncludeTCP
#include "tcp.h"
#endif
// *********************************************************************************
void SendDetails(void)
{
u16 w;
if (!SendConsoleRStr(Str3)) return;
if (!SendConsoleRStr(VersionStr)) return;
sprintf((char*)ScratchPad, "%u.%02x\n", Version >> 8, Version & 0xff);
if (!SendConsoleStr((char*)ScratchPad)) return;
if (!SendConsoleRStr(Title)) return;
if (!SendConsoleRStr(Date)) return;
// ************
#ifdef CPU_ATmega128
if (!SendConsoleStr("\nUnit ID: ")) return;
if (Get_UnitID((char*)ScratchPad))
{
if (!SendConsoleStr(ScratchPad)) return;
if (!SendConsoleStr("\n")) return;
}
else
{
if (!SendConsoleStr("Invalid\n")) return;
}
#endif
// ************
sprintf((char*)ScratchPad, "\nLast reset reason: %02X", LastResetReason);
#ifdef CPU_eZ8
if (LastResetReason & 0x80) strcat((char*)ScratchPad, " POR");
if (LastResetReason & 0x40) strcat((char*)ScratchPad, " STOP");
if (LastResetReason & 0x20) strcat((char*)ScratchPad, " WD");
if (LastResetReason & 0x10) strcat((char*)ScratchPad, " EXT");
#endif
#ifdef CPU_ATmega128
if (LastResetReason & 0x10) strcat((char*)ScratchPad, " JTAG");
if (LastResetReason & 0x08) strcat((char*)ScratchPad, " WD");
if (LastResetReason & 0x04) strcat((char*)ScratchPad, " BRN-OUT");
if (LastResetReason & 0x02) strcat((char*)ScratchPad, " EXT");
if (LastResetReason & 0x01) strcat((char*)ScratchPad, " POR");
#endif
strcat((char*)ScratchPad, "\n");
if (!SendConsoleStr((char*)ScratchPad)) return;
// ************
// test the sram
#ifdef CPU_ATmega128
if (!SendConsoleStr("\nSRAM: ")) return;
w = SRAM_Test();
if (w == 0xffff)
{
if (!SendConsoleStr("OK\n")) return;
}
else
{
sprintf(ScratchPad, "%04X\n", w);
if (!SendConsoleStr(ScratchPad)) return;
}
#endif
// ************
#ifdef Debug
if (!SendConsoleRStr(PPP_ModeStr)) return;
#endif
}
// *********************************************************************************
// Test button Interrupt routine
#ifdef CPU_eZ8
#pragma interrupt
void isr_C0(void)
{
}
#endif
// *********************************************************************************
// 10ms timer interrupt routine
#ifdef CPU_ATmega128
#pragma interrupt_handler timer1_compa_isr:13
void timer1_compa_isr(void)
{ // alternative more accurate timer counter option
// compare occured TCNT1=OCR1A
}
#endif
#ifdef CPU_eZ8
#pragma interrupt
void isr_timer0(void)
#endif
#ifdef CPU_ATmega128
#pragma interrupt_handler isr_timer1_ovf:15
void isr_timer1_ovf(void)
#endif
{ // 10ms timer interrupt
register u16 i, w; //
// *************************
#ifdef CPU_ATmega128
// w = TCNT1L; // read the current counter low byte
// w |= ((u16)TCNT1H) << 8; // read the current counter low byte
w = TCNT1; // read the current count - depends how long it took to get here as to what we reload it with
w += TimerIntSpeed + 3; // update
TCNT1 = w; // reload counter
// TCNT1H = (u8)(w >> 8); // reload counter high byte
// TCNT1L = (u8)w; // reload counter low byte
#endif
// *************************
TimerIntCounter++; // update counter
Random32 = UpdateFCS_32(Random32, TimerIntCounter); // update the 32-bit random number
// *************************
// read the ADC if it's complete - then start the next conversion
// this way of doing it is not exactly the fastest way (1 conversion per 20ms), but it's easy
// and more than fast enough for monitoring battery voltage etc
#ifdef CPU_ATmega128
if (!(ADCSRA & (1<<ADSC))) // conversion complete ?
{ // yes
if (Flags1 & (1<<Flags1_ADC)) //
{ //
sbi(ADCSRA, ADSC); // Start the next conversion
Flags1 &= ~(1<<Flags1_ADC); // clear flag
} //
else //
{ //
w = (u16)ADCL; // read LS-Byte
w |= ((u16)ADCH) << 8; // read MS-Byte
//
i = (u16)(ADMUX & 0x07); // current ADC channel
//
w += ADC_Input[i]; //
w >>= 1; // average with previous value - helps with noise
ADC_Input[i] = w; // save the new value
//
i++; // next ADC channel
if (i > 1) i = 0; // only interested in channels 0 and 1
// if (i > 7) i = 0; // only interested in channels 0 to 7
ADMUX = (u8)i; // select it
//
Flags1 |= (1<<Flags1_ADC); // set flag - this tells the next timer int to start the new conversion.
// we leave starting the next conversion to the next timer int so as to
// allow the ADC input to settle to the newly selected input pin
}
}
#endif
// *************************
AT_10ms_Timer();
PPP_10ms_Timer();
// *************************
// Test button debounce
if (!TestButton) button_push = 0; // button is pressed - reset the debounce detector
else
{
if (button_push < 255) button_push++; // update the debounce counter
if (button_push == button_debounce) Flags1 |= (1<<Flags1_ButtonPress); // button has just been released
}
//**********************************
// do stuff with the led's
switch (AT.Stage)
{
case AT_Idle : if (TimerIntCounter & 0x20)
RLed_On;
else
RLed_Off;
break;
case AT_PPP : if (TimerIntCounter & 0x50)
RLed_On;
else
RLed_Off;
break;
default : if (TimerIntCounter & 0x10)
RLed_On;
else
RLed_Off;
break;
}
switch (PPP.Stage)
{
case PPPS_None : YLed_Off;
break;
case PPPS_IP : if (TimerIntCounter & 0x50)
YLed_On;
else
YLed_Off;
break;
default : if (TimerIntCounter & 0x04)
YLed_On;
else
YLed_Off;
break;
}
// CTS0 state
// if (CTS0) //
// GLed_Off; //
// else //
// GLed_On; //
// CTS1 state
// if (CTS1) //
// GLed_Off; //
// else //
// GLed_On; //
// TCP socket state
#ifdef IncludeTCP
if (TCP_Socket != NULL) //
{ //
switch (TCP_Socket->Stage) //
{ //
case TCP_CLOSED : GLed_Off; //
break; //
case TCP_LISTEN : if (TimerIntCounter & 0x20) //
GLed_On; //
else //
GLed_Off; //
break; //
case TCP_ESTABLISHED : if (TimerIntCounter & 0x50) //
GLed_On; //
else //
GLed_Off; //
break; //
default : if (TimerIntCounter & 0x10) //
GLed_On; //
else //
GLed_Off; //
break; //
} //
} //
else //
GLed_Off; //
#else
GLed_Off; //
#endif
// *************************
// keep the watchdog at bay.
// the WatchdogCounter variable is set in the executive - this makes resetting the WD a co-op affair between the exec and this interrupt.
// keep this code as the last thing to be done in the interrupt - incase a loop/dead-lock occurs before we get this far
if (WatchdogCounter == 1) //
{ //
Reset_WD(); // Keep the watchdog at bay
WatchdogCounter = 0; //
}
}
// *********************************************************************************
// Uart-0 Rx Interrupt routine
#ifdef CPU_eZ8
#pragma interrupt
#endif
#ifdef CPU_ATmega128
#pragma interrupt_handler isr_uart0_rx:19
#endif
void isr_uart0_rx(void)
{
#ifdef CPU_eZ8
UART0_RxBuffer[UART0_RxBufferWr++] = U0D; // save new byte into the buffer
#endif
#ifdef CPU_ATmega128
UART0_RxBuffer[UART0_RxBufferWr++] = UDR0; // save new byte into the buffer
#endif
if (UART0_RxBufferWr >= sizeof(UART0_RxBuffer)) UART0_RxBufferWr = 0; // wap awound
#ifdef ConsoleHandShaking
HardwareFlowControl(UART0); // tell em to hold it - if need be
#endif
}
// *********************************************************************************
// Uart-1 Rx Interrupt routine
#ifdef CPU_eZ8
#pragma interrupt
#endif
#ifdef CPU_ATmega128
#pragma interrupt_handler isr_uart1_rx:31
#endif
void isr_uart1_rx(void)
{
#ifdef CPU_eZ8
UART1_RxBuffer[UART1_RxBufferWr++] = U1D; // save new byte into the buffer
#endif
#ifdef CPU_ATmega128
UART1_RxBuffer[UART1_RxBufferWr++] = UDR1; // save new byte into the buffer
#endif
if (UART1_RxBufferWr >= sizeof(UART1_RxBuffer)) UART1_RxBufferWr = 0; // wap awound
#ifdef ModemHandShaking
HardwareFlowControl(UART1); // tell em to hold it - if need be
#endif
}
// *********************************************************************************
/*
#ifdef CPU_ATmega128
void StackOverflowTest(u32 dw)
{
USB_Get(); // get data from USB
USB_Send(); // send any data that needs sending via the USB
USB_ProcessGet(); // process the rx'ed packet from the USB
_StackCheck(); // check for stack overflow
if (!WatchdogCounter) WatchdogCounter++; //
dw++;
StackOverflowTest(dw);
}
#endif
*/
// *********************************************************************************
// Main program here
void main(void)
{
int i, j;
Disable_Ints();
// ****************************
// find out what caused the last reset
#ifdef CPU_eZ8
LastResetReason = WDTCTL;
#endif
#ifdef CPU_ATmega128
LastResetReason = MCUCSR;
MCUCSR &= 0xE0; // clear the bits
#endif
// **********************
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -