You are on page 1of 6

///////////////////////////////////////////////////////////////////////////

// Product/Project : Embedded Connectivity


//
///////////////////////////////////////////////////////////////////////////
//
// File Name
: webserver.c
// Version
: 1.0
// Description : This is the boiler-plate code for your project
//
// Author
: Jim Clermonts
// Target
: Cerebot 32MX4 (PIC32MX460L512F)
// Compiler
: MPLAB C Compiler for PIC32
// IDE
: MPLAB X IDE Beta5.0
// Programmer : SKDE PIC32
// Last Updated : 14-04-2012
//
// Copyright (c) Fontys 2012.
// All rights are reserved. Reproduction in whole or in part is
// prohibited without the written consent of the copyright owner.
//
///////////////////////////////////////////////////////////////////////////
#define SET_FUSES_CEREBOT32MX4
#include
#include
#include
#include
#include
#include
#include

"TCPIP Stack/TCPIP.h"
"webserver.h"
"servo.h"
"srf08.h"
<p32xxxx.h>
<peripheral/timer.h> //neccesary for interrupt timers
"timer.h"

#define COREMS 32000


// Declare AppConfig structure and some other supporting stack variables
APP_CONFIG AppConfig;
static unsigned short wOriginalAppConfigChecksum; // Checksum of the ROM default
s for AppConfig
static DWORD t = 0;
int main(void)
{
servo_init(8e6, 1, -30);
srf08_init(8e6);
//interrupt stuff
// set up the core timer interrupt with a prioirty of 2 and zero sub-priorit
y
mConfigIntCoreTimer((CT_INT_ON | CT_INT_PRIOR_2 | CT_INT_SUB_PRIOR_0)); //de
precated warnings? meh, supressed. (start interrupt with priority 2.0)
INTEnableSystemMultiVectoredInt();
OpenCoreTimer(COREMS); //start timer with 1ms interval.
SYSTEMConfig(GetSystemClock(), SYS_CFG_WAIT_STATES | SYS_CFG_PCACHE);
INTEnableInterrupts();
InitializeBoard();
// Initialize stack-related hardware components that may be

// required by the UART configuration routines


TickInit();
#if defined(STACK_USE_MPFS) || defined(STACK_USE_MPFS2)
MPFSInit();
#endif
// Initialize Stack and application related NV variables into AppConfig.
InitAppConfig();
// Initialize core stack layers (MAC, ARP, TCP, UDP) and
// application modules (HTTP, SNMP, etc.)
StackInit();
// Now that all items are initialized, begin the co-operative
// multitasking loop. This infinite loop will continuously
// execute all stack-related tasks, as well as your own
// application's functions.
// Note that this is a "co-operative mult-tasking" mechanism
// where every task performs its tasks (whether all in one shot
// or part of it) and returns so that other tasks can do their
// job.
// If a task needs very long time to do its job, it must be broken
// down into smaller pieces so that other tasks can have CPU time.
while (1)
{
BlinkLed1();
// This task performs normal stack task including checking
// for incoming packet, type of packet and calling
// appropriate stack entity to process it.
StackTask();
// This tasks invokes each of the core stack application tasks
StackApplications();
}
}
/****************************************************************************
Function:
static void InitializeBoard(void)
Description:
This routine initializes the hardware. It is a generic initialization
routine for many of the Microchip development boards, using definitions
in HardwareProfile.h to determine specific initialization.
Precondition:
None
Parameters:
None - None
Returns:
None
Remarks:
None
***************************************************************************/
void InitializeBoard(void)

{
// LEDs
LED0_TRIS = 0;
LED1_TRIS = 0;
LED2_TRIS = 0;
LED3_TRIS = 0;
LED4_TRIS = 0;
LED5_TRIS = 0;
LED6_TRIS = 0;
LED7_TRIS = 0;
LED_PUT(0x00);
#if defined(__PIC32MX__)
{
// Enable multi-vectored interrupts
INTEnableSystemMultiVectoredInt();
// Enable optimal performance
SYSTEMConfigPerformance(GetSystemClock());
mOSCSetPBDIV(OSC_PB_DIV_1); // Use 1:1 CPU Core:Peripheral clocks
// Disable JTAG port so we get our I/O pins back, but first
// wait 50ms so if you want to reprogram the part with
// JTAG, you'll still have a tiny window before JTAG goes away.
// The PIC32 Starter Kit debuggers use JTAG and therefore must not
// disable JTAG.
DelayMs(50);
#if !defined(__MPLAB_DEBUGGER_PIC32MXSK) && !defined(__MPLAB_DEBUGGER_FS2)
DDPCONbits.JTAGEN = 0;
#endif
LED_PUT(0x00); // Turn the LEDs off
CNPUESET = 0x00098000; // Turn on weak pull ups on CN15, CN16, CN19 (RD5
, RD7, RD13), which is connected to buttons on PIC32 Starter Kit boards
}
#endif
AD1CHS = 0; // Input to AN0 (potentiometer)
AD1PCFGbits.PCFG4 = 0; // Disable digital input on AN4 (TC1047A temp sensor)
AD1PCFGbits.PCFG2 = 0; // Disable digital input on AN2 (potentiometer)
// ADC
AD1CON1 =
on parts with
AD1CON2 =
AD1CON3 =
AD1CSSL =

0x84E4;
a 12bit
0x0404;
0x1003;
1 << 2;

// Turn on, auto sample start, auto-convert, 12 bit mode (


A/D)
// AVdd, AVss, int every 2 conversions, MUXA only, scan
// 16 Tad auto-sample, Tad = 3*Tcy
// Scan pot

// Deassert all chip select lines so there isn't any problem with
// initialization order. Ex: When ENC28J60 is on SPI2 with Explorer 16,
// MAX3232 ROUT2 pin will drive RF12/U2CTS ENC28J60 CS line asserted,
// preventing proper 25LC256 EEPROM operation.
#if defined(ENC_CS_TRIS)
ENC_CS_IO = 1;
ENC_CS_TRIS = 0;
#endif
}
/*********************************************************************
* Function:
void InitAppConfig(void)

*
* PreCondition:
MPFSInit() is already called.
*
* Input:
None
*
* Output:
Write/Read non-volatile config variables.
*
* Side Effects:
None
*
* Overview:
None
*
* Note:
None
********************************************************************/
// MAC Address Serialization using a MPLAB PM3 Programmer and
// Serialized Quick Turn Programming (SQTP).
// The advantage of using SQTP for programming the MAC Address is it
// allows you to auto-increment the MAC address without recompiling
// the code for each unit. To use SQTP, the MAC address must be fixed
// at a specific location in program memory. Uncomment these two pragmas
// that locate the MAC address at 0x1FFF0. Syntax below is for MPLAB C
// Compiler for PIC18 MCUs. Syntax will vary for other compilers.
//#pragma romdata MACROM=0x1FFF0
static ROM BYTE SerializedMACAddress[6] = {MY_DEFAULT_MAC_BYTE1, MY_DEFAULT_MAC_
BYTE2, MY_DEFAULT_MAC_BYTE3, MY_DEFAULT_MAC_BYTE4, MY_DEFAULT_MAC_BYTE5, MY_DEFA
ULT_MAC_BYTE6};
//#pragma romdata
void InitAppConfig(void)
{
#if defined(EEPROM_CS_TRIS) || defined(SPIFLASH_CS_TRIS)
unsigned char vNeedToSaveDefaults = 0;
#endif
while (1)
{
// Start out zeroing all AppConfig bytes to ensure all fields are
// deterministic for checksum generation
memset((void*) &AppConfig, 0x00, sizeof (AppConfig));
AppConfig.Flags.bIsDHCPEnabled = TRUE;
AppConfig.Flags.bInConfigMode = TRUE;
memcpypgm2ram((void*) &AppConfig.MyMACAddr, (ROM void*) SerializedMACAdd
ress, sizeof (AppConfig.MyMACAddr));
//
{
//
_prog_addressT MACAddressAddress;
//
MACAddressAddress.next = 0x157F8;
//
_memcpy_p2d24((char*)&AppConfig.MyMACAddr, MACAd
dressAddress, sizeof(AppConfig.MyMACAddr));
//
}
AppConfig.MyIPAddr.Val = MY_DEFAULT_IP_ADDR_BYTE1 | MY_DEFAULT_IP_ADDR_B
YTE2 << 8ul | MY_DEFAULT_IP_ADDR_BYTE3 << 16ul | MY_DEFAULT_IP_ADDR_BYTE4 << 24u
l;
AppConfig.DefaultIPAddr.Val = AppConfig.MyIPAddr.Val;
AppConfig.MyMask.Val = MY_DEFAULT_MASK_BYTE1 | MY_DEFAULT_MASK_BYTE2 <<
8ul | MY_DEFAULT_MASK_BYTE3 << 16ul | MY_DEFAULT_MASK_BYTE4 << 24ul;
AppConfig.DefaultMask.Val = AppConfig.MyMask.Val;
AppConfig.MyGateway.Val = MY_DEFAULT_GATE_BYTE1 | MY_DEFAULT_GATE_BYTE2
<< 8ul | MY_DEFAULT_GATE_BYTE3 << 16ul | MY_DEFAULT_GATE_BYTE4 << 24ul;
AppConfig.PrimaryDNSServer.Val = MY_DEFAULT_PRIMARY_DNS_BYTE1 | MY_DEFAU
LT_PRIMARY_DNS_BYTE2 << 8ul | MY_DEFAULT_PRIMARY_DNS_BYTE3 << 16ul | MY_DEFAULT_

PRIMARY_DNS_BYTE4 << 24ul;


AppConfig.SecondaryDNSServer.Val = MY_DEFAULT_SECONDARY_DNS_BYTE1 | MY_D
EFAULT_SECONDARY_DNS_BYTE2 << 8ul | MY_DEFAULT_SECONDARY_DNS_BYTE3 << 16ul | MY_
DEFAULT_SECONDARY_DNS_BYTE4 << 24ul;
// Load the default NetBIOS Host Name
memcpypgm2ram(AppConfig.NetBIOSName, (ROM void*) MY_DEFAULT_HOST_NAME, 1
6);
FormatNetBIOSName(AppConfig.NetBIOSName);
// Compute the checksum of the AppConfig defaults as loaded from ROM
wOriginalAppConfigChecksum = CalcIPChecksum((BYTE*) & AppConfig, sizeof
(AppConfig));
#if defined(EEPROM_CS_TRIS) || defined(SPIFLASH_CS_TRIS)
{
NVM_VALIDATION_STRUCT NVMValidationStruct;
// Check to see if we have a flag set indicating that we need to
// save the ROM default AppConfig values.
if (vNeedToSaveDefaults)
SaveAppConfig(&AppConfig);
// Read the NVMValidation record and AppConfig struct out of EEPROM/
Flash
#if defined(EEPROM_CS_TRIS)
{
XEEReadArray(0x0000, (BYTE*) & NVMValidationStruct, sizeof (NVMV
alidationStruct));
XEEReadArray(sizeof (NVMValidationStruct), (BYTE*) & AppConfig,
sizeof (AppConfig));
}
#elif defined(SPIFLASH_CS_TRIS)
{
SPIFlashReadArray(0x0000, (BYTE*) & NVMValidationStruct, sizeof
(NVMValidationStruct));
SPIFlashReadArray(sizeof (NVMValidationStruct), (BYTE*) & AppCon
fig, sizeof (AppConfig));
}
#endif
//
//
//
if

Check EEPROM/Flash validitity. If it isn't valid, set a flag so


that we will save the ROM default values on the next loop
iteration.
((NVMValidationStruct.wConfigurationLength != sizeof (AppConfig))

||
(NVMValidationStruct.wOriginalChecksum != wOriginalAppConfig
Checksum) ||
(NVMValidationStruct.wCurrentChecksum != CalcIPChecksum((BYT
E*) & AppConfig, sizeof (AppConfig))))
{
// Check to ensure that the vNeedToSaveDefaults flag is zero,
// indicating that this is the first iteration through the do
// loop. If we have already saved the defaults once and the
// EEPROM/Flash still doesn't pass the validity check, then it
// means we aren't successfully reading or writing to the
// EEPROM/Flash. This means you have a hardware error and/or
// SPI configuration error.
if (vNeedToSaveDefaults)

{
while (1);
}
// Set flag and restart loop to load ROM defaults and save them
vNeedToSaveDefaults = 1;
continue;
}
// If we get down here, it means the EEPROM/Flash has valid contents
// and either matches the ROM defaults or previously matched and
// was run-time reconfigured by the user. In this case, we shall
// use the contents loaded from EEPROM/Flash.
break;
}
#endif
break;
}
}
void BlinkLed1(void)
{
// Blink LD1 every second.
if (TickGet() - t >= TICK_SECOND / 2ul)
{
t = TickGet();
LED0_IO ^= 1;
}
}

You might also like