-
Notifications
You must be signed in to change notification settings - Fork 3
/
MainDemo.c
1319 lines (1125 loc) · 47.4 KB
/
MainDemo.c
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*********************************************************************
*
* Main Application Entry Point and TCP/IP Stack Demo
* Module for Microchip TCP/IP Stack
* -Demonstrates how to call and use the Microchip TCP/IP stack
* -Reference: Microchip TCP/IP Stack Help (TCPIP Stack Help.chm)
*
*********************************************************************
* FileName: MainDemo.c
* Dependencies: TCPIP.h
* Processor: PIC18, PIC24F, PIC24H, dsPIC30F, dsPIC33F, PIC32
* Compiler: Microchip C32 v1.11b or higher
* Microchip C30 v3.24 or higher
* Microchip C18 v3.36 or higher
* Company: Microchip Technology, Inc.
*
* Software License Agreement
*
* Copyright (C) 2002-2010 Microchip Technology Inc. All rights
* reserved.
*
* File Description:
* Change History:
* Rev Description
* ---- -----------------------------------------
* 1.0 Initial release
* V5.36 ---- STACK_USE_MPFS support has been removed
********************************************************************/
/*
* 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"
#if defined(STACK_USE_ZEROCONF_LINK_LOCAL)
#include "TCPIP Stack/ZeroconfLinkLocal.h"
#endif
#if defined(STACK_USE_ZEROCONF_MDNS_SD)
#include "TCPIP Stack/ZeroconfMulticastDNS.h"
#endif
#include "dp83640cfg1588.h"
#include "dp83640ptp1588.h" // IEEE 1588 PTPSend()
#include "ClientServer.h"
// Include functions specific to this stack application
#include "MainDemo.h"
// Used for Wi-Fi assertions
#define WF_MODULE_NUMBER WF_MODULE_MAIN_DEMO
// Declare AppConfig structure and some other supporting stack variables
APP_CONFIG AppConfig;
static unsigned short wOriginalAppConfigChecksum; // Checksum of the ROM defaults for AppConfig
BYTE AN0String[8];
// Use UART2 instead of UART1 for stdout (printf functions). Explorer 16
// serial port hardware is on PIC UART2 module.
#if defined(EXPLORER_16) || defined(PIC24FJ256DA210_DEV_BOARD)
int __C30_UART = 2;
#endif
// Private helper functions.
// These may or may not be present in all applications.
static void InitAppConfig(void);
static void InitializeBoard(void);
static void ProcessIO(void);
#if defined(WF_CS_TRIS)
static void WF_Connect(void);
extern BOOL gRFModuleVer1209orLater;
#endif
//
// 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
#if defined(__18CXX)
#if defined(HI_TECH_C)
void interrupt low_priority LowISR(void)
#else
#pragma interruptlow LowISR
void LowISR(void)
#endif
{
TickUpdate();
}
#if defined(HI_TECH_C)
void interrupt HighISR(void)
#else
#pragma interruptlow HighISR
void HighISR(void)
#endif
{
#if defined(STACK_USE_UART2TCP_BRIDGE)
UART2TCPBridgeISR();
#endif
#if defined(WF_CS_TRIS)
WFEintISR();
#endif // WF_CS_TRIS
}
#if !defined(HI_TECH_C)
#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
#endif
// C30 and C32 Exception Handlers
// If your code gets here, you either tried to read or write
// a NULL pointer, or your application overflowed the stack
// by having too many local variables or parameters declared.
#elif defined(__C30__)
void _ISR __attribute__((__no_auto_psv__)) _AddressError(void)
{
Nop();
Nop();
}
void _ISR __attribute__((__no_auto_psv__)) _StackError(void)
{
Nop();
Nop();
}
#elif defined(__C32__)
void _general_exception_handler(unsigned cause, unsigned status)
{
Nop();
Nop();
}
#endif
//
// Main application entry point.
//
#if defined(__18CXX)
void main(void)
#else
int main(void)
#endif
{
static DWORD t = 0;
static DWORD dwLastIP = 0;
// Initialize application specific hardware
InitializeBoard();
#if defined(USE_LCD)
// Initialize and display the stack version on the LCD
LCDInit();
DelayMs(100);
strcpypgm2ram((char*)LCDText, "TCPStack " TCPIP_STACK_VERSION " "
" ");
LCDUpdate();
#endif
// Initialize stack-related hardware components that may be
// required by the UART configuration routines
TickInit();
#if defined(STACK_USE_MPFS2)
MPFSInit();
#endif
// Initialize Stack and application related NV variables into AppConfig.
InitAppConfig();
// Initiates board setup process if button is depressed
// on startup
if(BUTTON0_IO == 0u)
{
#if defined(EEPROM_CS_TRIS) || defined(SPIFLASH_CS_TRIS)
// Invalidate the EEPROM contents if BUTTON0 is held down for more than 4 seconds
DWORD StartTime = TickGet();
LED_PUT(0x00);
while(BUTTON0_IO == 0u)
{
if(TickGet() - StartTime > 4*TICK_SECOND)
{
#if defined(EEPROM_CS_TRIS)
XEEBeginWrite(0x0000);
XEEWrite(0xFF);
XEEWrite(0xFF);
XEEEndWrite();
#elif defined(SPIFLASH_CS_TRIS)
SPIFlashBeginWrite(0x0000);
SPIFlashWrite(0xFF);
SPIFlashWrite(0xFF);
#endif
#if defined(STACK_USE_UART)
putrsUART("\r\n\r\nBUTTON0 held for more than 4 seconds. Default settings restored.\r\n\r\n");
#endif
LED_PUT(0x0F);
while((LONG)(TickGet() - StartTime) <= (LONG)(9*TICK_SECOND/2));
LED_PUT(0x00);
while(BUTTON0_IO == 0u);
Reset();
break;
}
}
#endif
#if defined(STACK_USE_UART)
DoUARTConfig();
#endif
}
// Initialize core stack layers (MAC, ARP, TCP, UDP) and
// application modules (HTTP, SNMP, etc.)
StackInit(); // calls TCPInit()
#if defined(WF_CS_TRIS)
WF_Connect();
#endif
// Initialize any application-specific modules or functions/
// For this demo application, this only includes the
// UART 2 TCP Bridge
#if defined(STACK_USE_UART2TCP_BRIDGE)
UART2TCPBridgeInit();
#endif
#if defined(STACK_USE_ZEROCONF_LINK_LOCAL)
ZeroconfLLInitialize();
#endif
#if defined(STACK_USE_ZEROCONF_MDNS_SD)
mDNSInitialize(MY_DEFAULT_HOST_NAME);
mDNSServiceRegister(
(const char *) "DemoWebServer", // base name of the service
"_http._tcp.local", // type of the service
80, // TCP or UDP port, at which this service is available
((const BYTE *)"path=/index.htm"), // TXT info
1, // auto rename the service when if needed
NULL, // no callback function
NULL // no application context
);
mDNSMulticastFilterRegister();
#endif
//AM Initialize IEEE 1588 related functionality
PTPEnable(TRUE);
PTPClockSet(0,0);
// NS_UINT clockConfigOptions, NS_UINT ptpClockDivideByValue, NS_UINT ptpClockSource, NS_UINT ptpClockSourcePeriod
// ptpClockSource: 0x00 - 125MHz from internal PGM (default), 0x01 - Divide-by-N from 125MHz internal PGM
// PTPSetClockConfig (CLKOPT_CLK_OUT_EN,0xFF,0x00,0);
PTPClockOutEnable(TRUE,250); //250
PTPPulseOutStart();
// void PTPSetEventConfig (IN NS_UINT event,IN NS_BOOL eventRiseFlag,IN NS_BOOL eventFallFlag,IN NS_BOOL eventSingle,IN NS_UINT gpioConnection);
// event - The event to configure, 0 – 7.
// eventRiseFlag - If set to TRUE, enables detection of rising edge on Event input.
// eventFallFlag - If set to TRUE, enables detection of falling edge on Event input.
// eventSingle - If set to TRUE, enables single event capture operation.
// gpioConnection - The GPIO pin the event should be connected to. A value of 0 – 12. If 0 is specified no GPIO pin connection is made.
const unsigned int GPIO_PIN = 10; // GPIO 1,2,3,4,8,9,12 have internal PullDOWN resisotors, others have pullUPs (see datasheet page 13)
PTPSetEventConfig(0, FALSE, TRUE, TRUE, GPIO_PIN);
BOOL master;
master = !(BOOL)(PORTDbits.RD4); // jumper setting
PTPConfigRxTimeInsert();
PTPEnableSyncTimestampInsertion(master); // insertion of timestamp in sync msgs
if(!master){
PTPEnableDelayReqTimestampInsertion(TRUE); // allow Delay_Req timestamp insertion
EnableSynchronousEthernet(TRUE); // recover clock on the slave side
}
PTPEthInit(); // configures default for PTPSendSync() and PTPProcess()
// DEBUGGING !!!
// unsigned int seconds = 0;
// unsigned int nanoSeconds = 0;
// PTPClockRead(&seconds, &nanoSeconds);
// PTPClockStepAdjustment(30,0);
// PTPClockRead(&seconds, &nanoSeconds);
// PTPClockStepAdjustment(-90,0);
// PTPClockRead(&seconds, &nanoSeconds);
// PTPClockStepAdjustment(0,-0x03FFFFFE);
// PTPClockRead(&seconds, &nanoSeconds);
// PTPClockRead(&seconds, &nanoSeconds); // break here
// 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. Custom functions should be added
// at the end of this loop.
// 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){
// This task performs normal stack task including checking
// for incoming packet, type of packet and calling
// appropriate stack entity to process it.
StackTask();
// Blink LED0 (right most one) every second.
if(TickGet() - t >= TICK_SECOND/2ul){
// re-enable GPIO inputs and send PTP Sync twice per second on master
PTPSetEventConfig(0, FALSE, TRUE, TRUE, GPIO_PIN);
if(master){
PTPSendSync();
}
t = TickGet();
LED0_IO ^= 1;
}
unsigned int sec, ns;
// clear timestamps queue in order for PHY to function correctly
PTPGetTxTimestamp(&sec, &ns); // clear & discard TX timestamps
PTPGetRxTimestamp(&sec, &ns); // clear & discard RX timestamps
if(PTPCheckForEvents()){
LED1_IO ^= 1;
unsigned int eventNum, raiseFlag, eventsMissed, seconds, nanoSec;
if( PTPGetEvent(&eventNum, &raiseFlag, &seconds, &nanoSec, &eventsMissed ) ){
char msg[128];
sprintf(msg," Timestamp from %s: %us %uns", master?"master":" slave", seconds, nanoSec);
Debug(msg);
sendTimeStampUdp(seconds, nanoSec);
sendTimeStampTcp(seconds, nanoSec);
}
}
#if defined(WF_CS_TRIS)
if (gRFModuleVer1209orLater)
WiFiTask();
#endif
// This tasks invokes each of the core stack application tasks
StackApplications();
#if defined(STACK_USE_ZEROCONF_LINK_LOCAL)
ZeroconfLLProcess();
#endif
#if defined(STACK_USE_ZEROCONF_MDNS_SD)
mDNSProcess();
// Use this function to exercise service update function
// HTTPUpdateRecord();
#endif
// Process application specific tasks here.
// For this demo app, this will include the Generic TCP
// client and servers, and the SNMP, Ping, and SNMP Trap
// demos. Following that, we will process any IO from
// the inputs on the board itself.
// Any custom modules or processing you need to do should
// go here.
#if defined(STACK_USE_GENERIC_TCP_CLIENT_EXAMPLE)
GenericTCPClient();
#endif
#if defined(STACK_USE_GENERIC_TCP_SERVER_EXAMPLE)
GenericTCPServer();
#endif
#if defined(STACK_USE_SMTP_CLIENT)
// SMTPDemo();
#endif
#if defined(STACK_USE_ICMP_CLIENT)
PingDemo();
#endif
#if defined(STACK_USE_SNMP_SERVER) && !defined(SNMP_TRAP_DISABLED)
//User should use one of the following SNMP demo
// This routine demonstrates V1 or V2 trap formats with one variable binding.
SNMPTrapDemo();
#if defined(SNMP_STACK_USE_V2_TRAP) || defined(SNMP_V1_V2_TRAP_WITH_SNMPV3)
//This routine provides V2 format notifications with multiple (3) variable bindings
//User should modify this routine to send v2 trap format notifications with the required varbinds.
//SNMPV2TrapDemo();
#endif
if(gSendTrapFlag)
SNMPSendTrap();
#endif
#if defined(STACK_USE_BERKELEY_API)
BerkeleyTCPClientDemo();
BerkeleyTCPServerDemo();
BerkeleyUDPClientDemo();
#endif
//A ProcessIO(); // ADC from a pot
// If the local IP address has changed (ex: due to DHCP lease change)
// write the new IP address to the LCD display, UART, and Announce
// service
if(dwLastIP != AppConfig.MyIPAddr.Val){
dwLastIP = AppConfig.MyIPAddr.Val;
#if defined(STACK_USE_UART)
putrsUART((ROM char*)"\r\nNew IP Address: ");
#endif
// display IP on LCD screen
//A DisplayIPValue(AppConfig.MyIPAddr);
#if defined(STACK_USE_UART)
putrsUART((ROM char*)"\r\n");
#endif
#if defined(STACK_USE_ANNOUNCE)
AnnounceIP();
#endif
#if defined(STACK_USE_ZEROCONF_MDNS_SD)
mDNSFillHostRecord();
#endif
}
} // while(1)
}
#if defined(WF_CS_TRIS)
/*****************************************************************************
* FUNCTION: WF_Connect
*
* RETURNS: None
*
* PARAMS: None
*
* NOTES: Connects to an 802.11 network. Customize this function as needed
* for your application.
*****************************************************************************/
static void WF_Connect(void)
{
UINT8 ConnectionProfileID;
UINT8 channelList[] = MY_DEFAULT_CHANNEL_LIST;
/* create a Connection Profile */
WF_CPCreate(&ConnectionProfileID);
#if defined(STACK_USE_UART)
putrsUART("Set SSID (");
putsUART(AppConfig.MySSID);
putrsUART(")\r\n");
#endif
WF_CPSetSsid(ConnectionProfileID,
AppConfig.MySSID,
AppConfig.SsidLength);
#if defined(STACK_USE_UART)
putrsUART("Set Network Type\r\n");
#endif
WF_CPSetNetworkType(ConnectionProfileID, MY_DEFAULT_NETWORK_TYPE);
#if defined(STACK_USE_UART)
putrsUART("Set Scan Type\r\n");
#endif
WF_CASetScanType(MY_DEFAULT_SCAN_TYPE);
#if defined(STACK_USE_UART)
putrsUART("Set Channel List\r\n");
#endif
WF_CASetChannelList(channelList, sizeof(channelList));
#if defined(STACK_USE_UART)
putrsUART("Set list retry count\r\n");
#endif
// The Retry Count parameter tells the WiFi Connection manager how many attempts to make when trying
// to connect to an existing network. In the Infrastructure case, the default is to retry forever so that
// if the AP is turned off or out of range, the radio will continue to attempt a connection until the
// AP is eventually back on or in range. In the Adhoc case, the default is to retry 3 times since the
// purpose of attempting to establish a network in the Adhoc case is only to verify that one does not
// initially exist. If the retry count was set to WF_RETRY_FOREVER in the AdHoc mode, an AdHoc network
// would never be established. The constants MY_DEFAULT_LIST_RETRY_COUNT_ADHOC and
// MY_DEFAULT_LIST_RETRY_COUNT_INFRASTRUCTURE have been created specifically for the June 2011 MAL release.
#if defined(EZ_CONFIG_STORE)
if (AppConfig.networkType == WF_ADHOC)
WF_CASetListRetryCount(MY_DEFAULT_LIST_RETRY_COUNT_ADHOC);
else
WF_CASetListRetryCount(MY_DEFAULT_LIST_RETRY_COUNT_INFRASTRUCTURE);
#else
#if (MY_DEFAULT_NETWORK_TYPE == WF_ADHOC)
WF_CASetListRetryCount(MY_DEFAULT_LIST_RETRY_COUNT_ADHOC);
#else
WF_CASetListRetryCount(MY_DEFAULT_LIST_RETRY_COUNT_INFRASTRUCTURE);
#endif
#endif
#if defined(STACK_USE_UART)
putrsUART("Set Event Notify\r\n");
#endif
WF_CASetEventNotificationAction(MY_DEFAULT_EVENT_NOTIFICATION_LIST);
#if defined(STACK_USE_UART)
putrsUART("Set Beacon Timeout\r\n");
#endif
WF_CASetBeaconTimeout(40);
if (gRFModuleVer1209orLater)
{
// If WEP security is used, set WEP Key Type. The default WEP Key Type is Shared Key.
if (AppConfig.SecurityMode == WF_SECURITY_WEP_40 || AppConfig.SecurityMode == WF_SECURITY_WEP_104)
{
WF_CPSetWepKeyType(ConnectionProfileID, MY_DEFAULT_WIFI_SECURITY_WEP_KEYTYPE);
}
}
/* Set Security */
#if (MY_DEFAULT_WIFI_SECURITY_MODE == WF_SECURITY_OPEN)
#if defined(STACK_USE_UART)
putrsUART("Set Security (Open)\r\n");
#endif
#elif (MY_DEFAULT_WIFI_SECURITY_MODE == WF_SECURITY_WEP_40)
#if defined(STACK_USE_UART)
putrsUART("Set Security (WEP40)\r\n");
#endif
#elif (MY_DEFAULT_WIFI_SECURITY_MODE == WF_SECURITY_WEP_104)
#if defined(STACK_USE_UART)
putrsUART("Set Security (WEP104)\r\n");
#endif
#elif MY_DEFAULT_WIFI_SECURITY_MODE == WF_SECURITY_WPA_WITH_KEY
#if defined(STACK_USE_UART)
putrsUART("Set Security (WPA with key)\r\n");
#endif
#elif MY_DEFAULT_WIFI_SECURITY_MODE == WF_SECURITY_WPA2_WITH_KEY
#if defined(STACK_USE_UART)
putrsUART("Set Security (WPA2 with key)\r\n");
#endif
#elif MY_DEFAULT_WIFI_SECURITY_MODE == WF_SECURITY_WPA_WITH_PASS_PHRASE
#if defined(STACK_USE_UART)
putrsUART("Set Security (WPA with pass phrase)\r\n");
#endif
#elif MY_DEFAULT_WIFI_SECURITY_MODE == WF_SECURITY_WPA2_WITH_PASS_PHRASE
#if defined(STACK_USE_UART)
putrsUART("Set Security (WPA2 with pass phrase)\r\n");
#endif
#elif MY_DEFAULT_WIFI_SECURITY_MODE == WF_SECURITY_WPA_AUTO_WITH_KEY
#if defined(STACK_USE_UART)
putrsUART("Set Security (WPA with key, auto-select)\r\n");
#endif
#elif MY_DEFAULT_WIFI_SECURITY_MODE == WF_SECURITY_WPA_AUTO_WITH_PASS_PHRASE
#if defined(STACK_USE_UART)
putrsUART("Set Security (WPA with pass phrase, auto-select)\r\n");
#endif
#endif /* MY_DEFAULT_WIFI_SECURITY_MODE */
WF_CPSetSecurity(ConnectionProfileID,
AppConfig.SecurityMode,
AppConfig.WepKeyIndex, /* only used if WEP enabled */
AppConfig.SecurityKey,
AppConfig.SecurityKeyLength);
#if MY_DEFAULT_PS_POLL == WF_ENABLED
WF_PsPollEnable(TRUE);
if (gRFModuleVer1209orLater)
WFEnableDeferredPowerSave();
#else
WF_PsPollDisable();
#endif
#ifdef WF_AGGRESSIVE_PS
if (gRFModuleVer1209orLater)
WFEnableAggressivePowerSave();
#endif
#if defined(STACK_USE_UART)
putrsUART("Start WiFi Connect\r\n");
#endif
WF_CMConnect(ConnectionProfileID);
}
#endif /* WF_CS_TRIS */
// Writes an IP address to the LCD display and the UART as available
void DisplayIPValue(IP_ADDR IPVal)
{
// printf("%u.%u.%u.%u", IPVal.v[0], IPVal.v[1], IPVal.v[2], IPVal.v[3]);
#if defined (__dsPIC33E__) || defined (__PIC24E__)
static BYTE IPDigit[4]; /* Needs to be declared as static to avoid the array getting optimized by C30 v3.30 compiler for dsPIC33E/PIC24E.
Otherwise the LCD displays corrupted IP address on Explorer 16. To be fixed in the future compiler release*/
#else
BYTE IPDigit[4];
#endif
BYTE i;
#ifdef USE_LCD
BYTE j;
BYTE LCDPos=16;
#endif
for(i = 0; i < sizeof(IP_ADDR); i++)
{
uitoa((WORD)IPVal.v[i], IPDigit);
#if defined(STACK_USE_UART)
putsUART((char *) IPDigit);
#endif
#ifdef USE_LCD
for(j = 0; j < strlen((char*)IPDigit); j++)
{
LCDText[LCDPos++] = IPDigit[j];
}
if(i == sizeof(IP_ADDR)-1)
break;
LCDText[LCDPos++] = '.';
#else
if(i == sizeof(IP_ADDR)-1)
break;
#endif
#if defined(STACK_USE_UART)
while(BusyUART());
WriteUART('.');
#endif
}
#ifdef USE_LCD
if(LCDPos < 32u)
LCDText[LCDPos] = 0;
LCDUpdate();
#endif
}
// Processes A/D data from the potentiometer
static void ProcessIO(void)
{
#if defined(__C30__) || defined(__C32__)
// Convert potentiometer result into ASCII string
uitoa((WORD)ADC1BUF0, AN0String);
#else
// AN0 should already be set up as an analog input
ADCON0bits.GO = 1;
// Wait until A/D conversion is done
while(ADCON0bits.GO);
// AD converter errata work around (ex: PIC18F87J10 A2)
#if !defined(__18F87J50) && !defined(_18F87J50) && !defined(__18F87J11) && !defined(_18F87J11)
{
BYTE temp = ADCON2;
ADCON2 |= 0x7; // Select Frc mode by setting ADCS0/ADCS1/ADCS2
ADCON2 = temp;
}
#endif
// Convert 10-bit value into ASCII string
uitoa(*((WORD*)(&ADRESL)), AN0String);
#endif
}
/****************************************************************************
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
***************************************************************************/
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);
#if defined(__18CXX)
// Enable 4x/5x/96MHz PLL on PIC18F87J10, PIC18F97J60, PIC18F87J50, etc.
OSCTUNE = 0x40;
// Set up analog features of PORTA
// PICDEM.net 2 board has POT on AN2, Temp Sensor on AN3
#if defined(PICDEMNET2)
ADCON0 = 0x09; // ADON, Channel 2
ADCON1 = 0x0B; // Vdd/Vss is +/-REF, AN0, AN1, AN2, AN3 are analog
#elif defined(PICDEMZ)
ADCON0 = 0x81; // ADON, Channel 0, Fosc/32
ADCON1 = 0x0F; // Vdd/Vss is +/-REF, AN0, AN1, AN2, AN3 are all digital
#elif defined(__18F87J11) || defined(_18F87J11) || defined(__18F87J50) || defined(_18F87J50)
ADCON0 = 0x01; // ADON, Channel 0, Vdd/Vss is +/-REF
WDTCONbits.ADSHR = 1;
ANCON0 = 0xFC; // AN0 (POT) and AN1 (temp sensor) are anlog
ANCON1 = 0xFF;
WDTCONbits.ADSHR = 0;
#else
ADCON0 = 0x01; // ADON, Channel 0
ADCON1 = 0x0E; // Vdd/Vss is +/-REF, AN0 is analog
#endif
ADCON2 = 0xBE; // Right justify, 20TAD ACQ time, Fosc/64 (~21.0kHz)
// Enable internal PORTB pull-ups
INTCON2bits.RBPU = 0;
// Configure USART
TXSTA = 0x20;
RCSTA = 0x90;
// See if we can use the high baud rate setting
#if ((GetPeripheralClock()+2*BAUD_RATE)/BAUD_RATE/4 - 1) <= 255
SPBRG = (GetPeripheralClock()+2*BAUD_RATE)/BAUD_RATE/4 - 1;
TXSTAbits.BRGH = 1;
#else // Use the low baud rate setting
SPBRG = (GetPeripheralClock()+8*BAUD_RATE)/BAUD_RATE/16 - 1;
#endif
// Enable Interrupts
RCONbits.IPEN = 1; // Enable interrupt priorities
INTCONbits.GIEH = 1;
INTCONbits.GIEL = 1;
// Do a calibration A/D conversion
#if defined(__18F87J10) || defined(__18F86J15) || defined(__18F86J10) || defined(__18F85J15) || defined(__18F85J10) || defined(__18F67J10) || defined(__18F66J15) || defined(__18F66J10) || defined(__18F65J15) || defined(__18F65J10) || defined(__18F97J60) || defined(__18F96J65) || defined(__18F96J60) || defined(__18F87J60) || defined(__18F86J65) || defined(__18F86J60) || defined(__18F67J60) || defined(__18F66J65) || defined(__18F66J60) || \
defined(_18F87J10) || defined(_18F86J15) || defined(_18F86J10) || defined(_18F85J15) || defined(_18F85J10) || defined(_18F67J10) || defined(_18F66J15) || defined(_18F66J10) || defined(_18F65J15) || defined(_18F65J10) || defined(_18F97J60) || defined(_18F96J65) || defined(_18F96J60) || defined(_18F87J60) || defined(_18F86J65) || defined(_18F86J60) || defined(_18F67J60) || defined(_18F66J65) || defined(_18F66J60)
ADCON0bits.ADCAL = 1;
ADCON0bits.GO = 1;
while(ADCON0bits.GO);
ADCON0bits.ADCAL = 0;
#elif defined(__18F87J11) || defined(__18F86J16) || defined(__18F86J11) || defined(__18F67J11) || defined(__18F66J16) || defined(__18F66J11) || \
defined(_18F87J11) || defined(_18F86J16) || defined(_18F86J11) || defined(_18F67J11) || defined(_18F66J16) || defined(_18F66J11) || \
defined(__18F87J50) || defined(__18F86J55) || defined(__18F86J50) || defined(__18F67J50) || defined(__18F66J55) || defined(__18F66J50) || \
defined(_18F87J50) || defined(_18F86J55) || defined(_18F86J50) || defined(_18F67J50) || defined(_18F66J55) || defined(_18F66J50)
ADCON1bits.ADCAL = 1;
ADCON0bits.GO = 1;
while(ADCON0bits.GO);
ADCON1bits.ADCAL = 0;
#endif
#else // 16-bit C30 and and 32-bit C32
#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 = 0x0009E000; // Turn on weak pull ups on CN13, CN14, CN15, CN16, CN19 (RD5, RD7, RD13) is connected to buttons on PIC32 Starter Kit boards
}
#endif
#if defined(__dsPIC33F__) || defined(__PIC24H__)
// Crank up the core frequency
PLLFBD = 38; // Multiply by 40 for 160MHz VCO output (8MHz XT oscillator)
CLKDIV = 0x0000; // FRC: divide by 2, PLLPOST: divide by 2, PLLPRE: divide by 2
// Port I/O
AD1PCFGHbits.PCFG23 = 1; // Make RA7 (BUTTON1) a digital input
AD1PCFGHbits.PCFG20 = 1; // Make RA12 (INT1) a digital input for MRF24WB0M PICtail Plus interrupt
// ADC
AD1CHS0 = 0; // Input to AN0 (potentiometer)
AD1PCFGLbits.PCFG5 = 0; // Disable digital input on AN5 (potentiometer)
AD1PCFGLbits.PCFG4 = 0; // Disable digital input on AN4 (TC1047A temp sensor)
#elif defined(__dsPIC33E__)||defined(__PIC24E__)
// Crank up the core frequency
PLLFBD = 38; /* M = 30 */
CLKDIVbits.PLLPOST = 0; /* N1 = 2 */
CLKDIVbits.PLLPRE = 0; /* N2 = 2 */
OSCTUN = 0;
/* Initiate Clock Switch to Primary
* Oscillator with PLL (NOSC= 0x3)*/
__builtin_write_OSCCONH(0x03);
__builtin_write_OSCCONL(0x01);
// Disable Watch Dog Timer
RCONbits.SWDTEN = 0;
while (OSCCONbits.COSC != 0x3);
while (_LOCK == 0); /* Wait for PLL lock at 60 MIPS */
// Port I/O
ANSELAbits.ANSA7 = 0 ; //Make RA7 (BUTTON1) a digital input
#if defined ENC100_INTERFACE_MODE > 0
ANSELEbits.ANSE0 = 0; // Make these PMP pins as digital output when the interface is parallel.
ANSELEbits.ANSE1 = 0;
ANSELEbits.ANSE2 = 0;
ANSELEbits.ANSE3 = 0;
ANSELEbits.ANSE4 = 0;
ANSELEbits.ANSE5 = 0;
ANSELEbits.ANSE6 = 0;
ANSELEbits.ANSE7 = 0;
ANSELBbits.ANSB10 = 0;
ANSELBbits.ANSB11 = 0;
ANSELBbits.ANSB12 = 0;
ANSELBbits.ANSB13 = 0;
ANSELBbits.ANSB15 = 0;
#endif
ANSELEbits.ANSE8= 0 ; // Make RE8(INT1) a digital input for ZeroG ZG2100M PICtail
AD1CHS0 = 0; // Input to AN0 (potentiometer)
ANSELBbits.ANSB0= 1; // Input to AN0 (potentiometer)
ANSELBbits.ANSB5= 1; // Disable digital input on AN5 (potentiometer)
ANSELBbits.ANSB4= 1; // Disable digital input on AN4 (TC1047A temp sensor)
ANSELDbits.ANSD7 =0; // Digital Pin Selection for S3(Pin 83) and S4(pin 84).
ANSELDbits.ANSD6 =0;
ANSELGbits.ANSG6 =0; // Enable Digital input for RG6 (SCK2)
ANSELGbits.ANSG7 =0; // Enable Digital input for RG7 (SDI2)
ANSELGbits.ANSG8 =0; // Enable Digital input for RG8 (SDO2)
ANSELGbits.ANSG9 =0; // Enable Digital input for RG9 (CS)
#if defined ENC100_INTERFACE_MODE == 0 // SPI Interface, UART can be used for debugging. Not allowed for other interfaces.
RPOR9 = 0x0300; //RP101= U2TX
RPINR19 = 0X0064; //RP100= U2RX
#endif
#if defined WF_CS_TRIS
RPINR1bits.INT3R = 30;
WF_CS_IO = 1;
WF_CS_TRIS = 0;
#endif
#else //defined(__PIC24F__) || defined(__PIC32MX__)
#if defined(__PIC24F__)
CLKDIVbits.RCDIV = 0; // Set 1:1 8MHz FRC postscalar
#endif
// ADC
#if defined(__PIC24FJ256DA210__) || defined(__PIC24FJ256GB210__)
// Disable analog on all pins
ANSA = 0x0000;
ANSB = 0x0000;
ANSC = 0x0000;
ANSD = 0x0000;
ANSE = 0x0000;
ANSF = 0x0000;
ANSG = 0x0000;
#else
AD1CHS = 0; // Input to AN0 (potentiometer)
AD1PCFGbits.PCFG4 = 0; // Disable digital input on AN4 (TC1047A temp sensor)
#if defined(__32MX460F512L__) || defined(__32MX795F512L__) // PIC32MX460F512L and PIC32MX795F512L PIMs has different pinout to accomodate USB module
AD1PCFGbits.PCFG2 = 0; // Disable digital input on AN2 (potentiometer)
#else
AD1PCFGbits.PCFG5 = 0; // Disable digital input on AN5 (potentiometer)
#endif
#endif
#endif
// ADC
AD1CON1 = 0x84E4; // Turn on, auto sample start, auto-convert, 12 bit mode (on parts with a 12bit A/D)
AD1CON2 = 0x0404; // AVdd, AVss, int every 2 conversions, MUXA only, scan
AD1CON3 = 0x1003; // 16 Tad auto-sample, Tad = 3*Tcy
#if defined(__32MX460F512L__) || defined(__32MX795F512L__) // PIC32MX460F512L and PIC32MX795F512L PIMs has different pinout to accomodate USB module
AD1CSSL = 1<<2; // Scan pot
#else
AD1CSSL = 1<<5; // Scan pot
#endif
// UART
#if defined(STACK_USE_UART)
#if defined(__PIC24E__) || defined(__dsPIC33E__)
#if defined (ENC_CS_IO) || defined (WF_CS_IO) // UART to be used in case of ENC28J60 or MRF24WB0M
__builtin_write_OSCCONL(OSCCON & 0xbf);
RPOR9bits.RP101R = 3; //Map U2TX to RF5
RPINR19bits.U2RXR = 0;
RPINR19bits.U2RXR = 0x64; //Map U2RX to RF4
__builtin_write_OSCCONL(OSCCON | 0x40);
#endif
#if(ENC100_INTERFACE_MODE == 0) // UART to be used only in case of SPI interface with ENC624Jxxx
__builtin_write_OSCCONL(OSCCON & 0xbf);
RPOR9bits.RP101R = 3; //Map U2TX to RF5
RPINR19bits.U2RXR = 0;
RPINR19bits.U2RXR = 0x64; //Map U2RX to RF4
__builtin_write_OSCCONL(OSCCON | 0x40);
#endif
#endif
UARTTX_TRIS = 0;
UARTRX_TRIS = 1;
UMODE = 0x8000; // Set UARTEN. Note: this must be done before setting UTXEN
#if defined(__C30__)
USTA = 0x0400; // UTXEN set
#define CLOSEST_UBRG_VALUE ((GetPeripheralClock()+8ul*BAUD_RATE)/16/BAUD_RATE-1)
#define BAUD_ACTUAL (GetPeripheralClock()/16/(CLOSEST_UBRG_VALUE+1))
#else //defined(__C32__)
USTA = 0x00001400; // RXEN set, TXEN set
#define CLOSEST_UBRG_VALUE ((GetPeripheralClock()+8ul*BAUD_RATE)/16/BAUD_RATE-1)
#define BAUD_ACTUAL (GetPeripheralClock()/16/(CLOSEST_UBRG_VALUE+1))
#endif
#define BAUD_ERROR ((BAUD_ACTUAL > BAUD_RATE) ? BAUD_ACTUAL-BAUD_RATE : BAUD_RATE-BAUD_ACTUAL)
#define BAUD_ERROR_PRECENT ((BAUD_ERROR*100+BAUD_RATE/2)/BAUD_RATE)
#if (BAUD_ERROR_PRECENT > 3)
#warning UART frequency error is worse than 3%
#elif (BAUD_ERROR_PRECENT > 2)
#warning UART frequency error is worse than 2%
#endif
UBRG = CLOSEST_UBRG_VALUE;
#endif
#endif
// 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
#if defined(ENC100_CS_TRIS)
ENC100_CS_IO = (ENC100_INTERFACE_MODE == 0);
ENC100_CS_TRIS = 0;
#endif
#if defined(EEPROM_CS_TRIS)
EEPROM_CS_IO = 1;
EEPROM_CS_TRIS = 0;
#endif
#if defined(SPIRAM_CS_TRIS)
SPIRAM_CS_IO = 1;
SPIRAM_CS_TRIS = 0;
#endif
#if defined(SPIFLASH_CS_TRIS)
SPIFLASH_CS_IO = 1;
SPIFLASH_CS_TRIS = 0;
#endif
#if defined(WF_CS_TRIS)
WF_CS_IO = 1;
WF_CS_TRIS = 0;
#endif
#if defined(PIC24FJ64GA004_PIM)
__builtin_write_OSCCONL(OSCCON & 0xBF); // Unlock PPS
// Remove some LED outputs to regain other functions
LED1_TRIS = 1; // Multiplexed with BUTTON0
LED5_TRIS = 1; // Multiplexed with EEPROM CS
LED7_TRIS = 1; // Multiplexed with BUTTON1
// Inputs
RPINR19bits.U2RXR = 19; //U2RX = RP19
RPINR22bits.SDI2R = 20; //SDI2 = RP20
RPINR20bits.SDI1R = 17; //SDI1 = RP17
// Outputs
RPOR12bits.RP25R = U2TX_IO; //RP25 = U2TX
RPOR12bits.RP24R = SCK2OUT_IO; //RP24 = SCK2
RPOR10bits.RP21R = SDO2_IO; //RP21 = SDO2
RPOR7bits.RP15R = SCK1OUT_IO; //RP15 = SCK1
RPOR8bits.RP16R = SDO1_IO; //RP16 = SDO1
AD1PCFG = 0xFFFF; //All digital inputs - POT and Temp are on same pin as SDO1/SDI1, which is needed for ENC28J60 commnications
__builtin_write_OSCCONL(OSCCON | 0x40); // Lock PPS
#endif
#if defined(__PIC24FJ256DA210__)