Repository navigation
Expand file tree
/
Copy pathMain.c
More file actions
334 lines (271 loc) · 11.8 KB
/
Copy pathMain.c
File metadata and controls
334 lines (271 loc) · 11.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
#pragma config WDT = OFF, STVR = ON, XINST = OFF, CP0 = OFF, FOSC = HSPLL, FOSC2 = ON, FCMEN = ON, IESO = ON, WDTPS = 512, ETHLED = ON
/*
* This macro uniquely defines this file as the main entry point.
* There should only be one such definition in the entire project,
* and this file must define the AppConfig variable as described below.
*/
#define THIS_IS_STACK_APPLICATION
// Include all headers for any enabled TCPIP Stack functions
#include "TCPIP Stack/TCPIP.h"
// Include functions specific to this stack application
#include "Main.h"
#include "eeprom.h"
// Declare AppConfig structure and some other supporting stack variables
APP_CONFIG AppConfig;
WEBSRV_CONFIG WebSrvConfig;
static unsigned short wOriginalAppConfigChecksum; // Checksum of the ROM defaults for AppConfig
static unsigned short wOriginalWebSrvConfigChecksum; // Checksum of the ROM defaults for WebSrvConfig
// Private helper functions.
// These may or may not be present in all applications.
static void InitAppConfig(void);
static void InitWebSrvConfig(void);
static void InitializeBoard(void);
//
// PIC18 Interrupt Service Routines
//
// NOTE: Several PICs, including the PIC18F4620 revision A3 have a RETFIE FAST/MOVFF bug
// The interruptlow keyword is used to work around the bug when using C18
#pragma interruptlow LowISR
void LowISR(void)
{
TickUpdate();
}
#pragma interruptlow HighISR
void HighISR(void)
{
if (PIR1bits.SSP1IF) {
BTCommRX();
PIR1bits.SSP1IF = 0; //Clear SSPIF Interrupt Flag
}
}
#pragma code lowVector=0x18
void LowVector(void){_asm goto LowISR _endasm}
#pragma code highVector=0x8
void HighVector(void){_asm goto HighISR _endasm}
#pragma code // Return to default code section
void main(void)
{
static DWORD t = 0;
// Initialize application specific hardware
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
if(BUTTON0_IO == 0u)
{
// Invalidate the EEPROM contents if BUTTON0 is held down for more than 4 seconds
DWORD StartTime = TickGet();
LED1_IO = LED2_IO = 1;
while(BUTTON0_IO == 0u)
{
if(TickGet() - StartTime > 4*TICK_SECOND)
{
//Clear EEPROM
eepromClear();
LED1_IO = LED2_IO = 0;
while(BUTTON0_IO == 0u);
break;
}
}
}
// Initialize Stack and application related NV variables into AppConfig.
InitAppConfig();
InitWebSrvConfig();
// Initialize core stack layers (MAC, ARP, TCP, UDP) and
// application modules (HTTP, SNMP, etc.)
StackInit();
BTCommInit();
WDTCONbits.SWDTEN = 1; //enable watchdog timer
while(1)
{
if(TickGet() - t >= TICK_SECOND/4ul)
{
t = TickGet();
LED1_IO ^= 1;
LED2_IO ^= 1;
}
if(BUTTON0_IO == 0u) {
Delay10KTCYx(0);
while (BUTTON0_IO == 0u);
RCONbits.POR = 0;
Reset();
}
// 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();
ClrWdt();
}
}
static 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);
CMCON = 0x00; //Turn off analogs inputs and make all digital
ADCON1 = 0x0F;
BUTTON0_TRIS = 1;
// Enable 4x/5x/96MHz PLL on PIC18F87J10, PIC18F97J60, PIC18F87J50, etc.
OSCTUNE = 0x40;
// Configure USART
TXSTA = 0x20;
RCSTA = 0x90;
// Enable Interrupts
RCONbits.IPEN = 1; // Enable interrupt priorities
INTCONbits.GIEH = 1;
INTCONbits.GIEL = 1;
// configure software SPI
TRIS_SW_CS_PIN = 0; // Make the EEPROM CS pin an output
TRIS_SW_CS2_PIN = 0; // Make the DATAFLASH CS pin an output
TRIS_SW_CS3_PIN = 0; // Make the SRAM CS pin an output
SW_CS_PIN = 1; // Pull EEPROM CS HIGH
SW_CS2_PIN = 1; // Pull DATAFLASH CS HIGH
SW_CS3_PIN = 1; // Pull SRAM CS HIGH
OpenSWSPI();
}
static void InitAppConfig(void)
{
unsigned char vNeedToSaveDefaults = 0;
while (1)
{
NVM_VALIDATION_STRUCT NVMValidationStruct;
memset((void*)&AppConfig, 0x00, sizeof(AppConfig));
AppConfig.Flags.bIsDHCPEnabled = TRUE;
AppConfig.Flags.bInConfigMode = TRUE;
eepromReadBytes((void*)&AppConfig.MyMACAddr, EEPROM_MAP_MACADDR, sizeof(AppConfig.MyMACAddr));
AppConfig.MyIPAddr.Val = MY_DEFAULT_IP_ADDR_BYTE1 | MY_DEFAULT_IP_ADDR_BYTE2<<8ul | MY_DEFAULT_IP_ADDR_BYTE3<<16ul | MY_DEFAULT_IP_ADDR_BYTE4<<24ul;
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_DEFAULT_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_DEFAULT_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, 16);
FormatNetBIOSName(AppConfig.NetBIOSName);
// Compute the checksum of the AppConfig defaults as loaded from ROM
wOriginalAppConfigChecksum = CalcIPChecksum((BYTE*)&AppConfig, sizeof(AppConfig));
// 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
eepromReadBytes((void*)&NVMValidationStruct, EEPROM_MAP_NVMVALID, sizeof(NVMValidationStruct));
eepromReadBytes((void*)&AppConfig, EEPROM_MAP_APPCONFIG, sizeof(AppConfig));
// 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.
if((NVMValidationStruct.wConfigurationLength != sizeof(AppConfig)) ||
(NVMValidationStruct.wOriginalChecksum != wOriginalAppConfigChecksum) ||
(NVMValidationStruct.wCurrentChecksum != CalcIPChecksum((BYTE*)&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;
}
}
static void InitWebSrvConfig(void)
{
unsigned char vNeedToSaveDefaults = 0;
while (1)
{
NVM_VALIDATION_STRUCT NVMValidationStruct;
memset((void*)&WebSrvConfig, 0x00, sizeof(WebSrvConfig));
WebSrvConfig.Flags.DataRequireHTTPS = TRUE;
WebSrvConfig.Flags.DataRequireAuth = TRUE;
memcpypgm2ram(WebSrvConfig.AuthUser, (ROM void*)WEBSRV_DEFAULTUSER, 16);
memcpypgm2ram(WebSrvConfig.AuthPwd, (ROM void*)WEBSRV_DEFAULTPWD, 16);
WebSrvConfig.HTTPPort = WEBSRV_DEFAULTHTTP;
WebSrvConfig.HTTPSPort = WEBSRV_DEFAULTHTTPS;
WebSrvConfig.StateTimeout[COMMSTATE_IDLE] = 0;
WebSrvConfig.StateTimeout[COMMSTATE_BUFFERING] = 500;
WebSrvConfig.StateTimeout[COMMSTATE_TXREADY] = 500;
WebSrvConfig.StateTimeout[COMMSTATE_TX] = 500;
WebSrvConfig.StateTimeout[COMMSTATE_WAIT] = 500;
WebSrvConfig.StateTimeout[COMMSTATE_RX] = 500;
WebSrvConfig.StateTimeout[COMMSTATE_MSG] = 500;
// Compute the checksum of the AppConfig defaults as loaded from ROM
wOriginalWebSrvConfigChecksum = CalcIPChecksum((BYTE*)&WebSrvConfig, sizeof(WebSrvConfig));
// Check to see if we have a flag set indicating that we need to
// save the ROM default AppConfig values.
if(vNeedToSaveDefaults) SaveWebSrvConfig(&WebSrvConfig);
// Read the NVMValidation record and AppConfig struct out of EEPROM/Flash
eepromReadBytes((void*)&NVMValidationStruct, EEPROM_MAP_WEBSRVVALID, sizeof(NVMValidationStruct));
eepromReadBytes((void*)&WebSrvConfig, EEPROM_MAP_WEBSRVCONFIG, sizeof(WebSrvConfig));
// 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.
if((NVMValidationStruct.wConfigurationLength != sizeof(WebSrvConfig)) ||
(NVMValidationStruct.wOriginalChecksum != wOriginalWebSrvConfigChecksum) ||
(NVMValidationStruct.wCurrentChecksum != CalcIPChecksum((BYTE*)&WebSrvConfig, sizeof(WebSrvConfig))))
{
// 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;
}
}
void SaveAppConfig(const APP_CONFIG *ptrAppConfig)
{
char count;
NVM_VALIDATION_STRUCT NVMValidationStruct;
// Get proper values for the validation structure indicating that we can use
// these EEPROM/Flash contents on future boot ups
NVMValidationStruct.wOriginalChecksum = wOriginalAppConfigChecksum;
NVMValidationStruct.wCurrentChecksum = CalcIPChecksum((BYTE*)ptrAppConfig, sizeof(APP_CONFIG));
NVMValidationStruct.wConfigurationLength = sizeof(APP_CONFIG);
// Write the validation struct and current AppConfig contents to EEPROM/Flash
eepromWriteBytes((void*)&NVMValidationStruct, EEPROM_MAP_NVMVALID, sizeof(NVMValidationStruct));
eepromWriteBytes((void*)ptrAppConfig, EEPROM_MAP_APPCONFIG, sizeof(APP_CONFIG));
}
void SaveWebSrvConfig(const WEBSRV_CONFIG *ptrWebSrvConfig)
{
char count;
NVM_VALIDATION_STRUCT NVMValidationStruct;
// Get proper values for the validation structure indicating that we can use
// these EEPROM/Flash contents on future boot ups
NVMValidationStruct.wOriginalChecksum = wOriginalWebSrvConfigChecksum;
NVMValidationStruct.wCurrentChecksum = CalcIPChecksum((BYTE*)ptrWebSrvConfig, sizeof(WEBSRV_CONFIG));
NVMValidationStruct.wConfigurationLength = sizeof(WEBSRV_CONFIG);
// Write the validation struct and current WebSrvConfig contents to EEPROM/Flash
eepromWriteBytes((void*)&NVMValidationStruct, EEPROM_MAP_WEBSRVVALID, sizeof(NVMValidationStruct));
eepromWriteBytes((void*)ptrWebSrvConfig, EEPROM_MAP_WEBSRVCONFIG, sizeof(WEBSRV_CONFIG));
}