Subversion Repositories HomeAutomation

Rev

Rev 363 | Blame | Compare with Previous | Last modification | View Log | SVN | RSS feed

  1. /*********************************************************************
  2.  *
  3.  *       Example Web Server Application using Microchip TCP/IP Stack
  4.  *
  5.  *********************************************************************
  6.  * FileName:        MainDemo.c
  7.  * Dependencies:    string.H
  8.  *                  StackTsk.h
  9.  *                  Tick.h
  10.  *                  http.h
  11.  *                  MPFS.h
  12.  *                  mac.h
  13.  * Processor:       PIC18, PIC24F, PIC24H, dsPIC30F, dsPIC33F
  14.  * Complier:        Microchip C18 v3.02 or higher
  15.  *                  Microchip C30 v2.01 or higher
  16.  * Company:         Microchip Technology, Inc.
  17.  *
  18.  * Software License Agreement
  19.  *
  20.  * This software is owned by Microchip Technology Inc. ("Microchip")
  21.  * and is supplied to you for use exclusively as described in the
  22.  * associated software agreement.  This software is protected by
  23.  * software and other intellectual property laws.  Any use in
  24.  * violation of the software license may subject the user to criminal
  25.  * sanctions as well as civil liability.  Copyright 2006 Microchip
  26.  * Technology Inc.  All rights reserved.
  27.  *
  28.  * This software is provided "AS IS."  MICROCHIP DISCLAIMS ALL
  29.  * WARRANTIES, EXPRESS, IMPLIED, STATUTORY OR OTHERWISE, NOT LIMITED
  30.  * TO MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND
  31.  * INFRINGEMENT.  Microchip shall in no event be liable for special,
  32.  * incidental, or consequential damages.
  33.  *
  34.  *
  35.  * Author              Date         Comment
  36.  *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  37.  * Nilesh Rajbharti     4/19/01     Original (Rev. 1.0)
  38.  * Nilesh Rajbharti     2/09/02     Cleanup
  39.  * Nilesh Rajbharti     5/22/02     Rev 2.0 (See version.log for detail)
  40.  * Nilesh Rajbharti     7/9/02      Rev 2.1 (See version.log for detail)
  41.  * Nilesh Rajbharti     4/7/03      Rev 2.11.01 (See version log for detail)
  42.  * Howard Schlunder     10/1/04     Beta Rev 0.9 (See version log for detail)
  43.  * Howard Schlunder     10/8/04     Beta Rev 0.9.1 Announce support added
  44.  * Howard Schlunder     11/29/04    Beta Rev 0.9.2 (See version log for detail)
  45.  * Howard Schlunder     2/10/05     Rev 2.5.0
  46.  * Howard Schlunder     1/5/06      Rev 3.00
  47.  * Howard Schlunder     1/18/06     Rev 3.01 ENC28J60 fixes to TCP,
  48.  *                                  UDP and ENC28J60 files
  49.  * Howard Schlunder     3/01/06     Rev. 3.16 including 16-bit micro support
  50.  * Howard Schlunder     4/12/06     Rev. 3.50 added LCD for Explorer 16
  51.  * Howard Schlunder     6/19/06     Rev. 3.60 finished dsPIC30F support, added PICDEM.net 2 support
  52.  * Howard Schlunder     8/02/06     Rev. 3.75 added beta DNS, NBNS, and HTTP client (GenericTCPClient.c) services
  53.  ********************************************************************/
  54.  
  55. /*
  56.  * Following define uniquely deines this file as main
  57.  * entry/application In whole project, there should only be one such
  58.  * definition and application file must define AppConfig variable as
  59.  * described below.
  60.  */
  61. #define THIS_IS_STACK_APPLICATION
  62.  
  63. #define VERSION         "v3.75"     // TCP/IP stack version
  64.  
  65. // These headers must be included for required defs.
  66. #include <string.h>
  67. #include "..\Include\Compiler.h"
  68. #include "..\Include\StackTsk.h"
  69. #include "..\Include\Tick.h"
  70. #include "..\Include\MAC.h"
  71. #include "..\Include\Helpers.h"
  72. #include "..\Include\Delay.h"
  73.  
  74. #include "..\canBoot\Include\boot.h"
  75. #include <stackTasks.h>
  76. #include <funcdefs.h>
  77. #include <CAN.h>
  78.  
  79. #if defined(STACK_USE_HTTP_SERVER)
  80. #include "..\Include\HTTP.h"
  81. #endif
  82.  
  83. #if defined(STACK_USE_SGP_SERVER)
  84. #include "..\Include\SGP.h"
  85. #endif
  86.  
  87. #if defined(STACK_USE_DNS)
  88. #include "..\Include\DNS.h"
  89. #endif
  90.  
  91. #if defined(STACK_USE_TIMESYNC)
  92. #include "..\Include\TimeSync.h"
  93. #endif
  94.  
  95. #if defined(MPFS_USE_EEPROM)
  96. #include "..\Include\XEEPROM.h"
  97. #endif
  98.  
  99.  
  100. // This is used by other stack elements.
  101. // Main application must define this and initialize it with proper values.
  102. APP_CONFIG AppConfig;
  103.  
  104. // Private helper functions.
  105. // These may or may not be present in all applications.
  106. static void InitAppConfig(void);
  107. static void InitializeBoard(void);
  108. static void ProcessIO(void);
  109.  
  110. #define SaveAppConfig()
  111.  
  112. #pragma interrupt high_isr
  113. void high_isr(void)
  114. {
  115.     canISR();
  116. }
  117.  
  118.  
  119. #pragma interrupt low_isr
  120. void low_isr(void)
  121. {
  122.  
  123. }
  124.  
  125. void main(void)
  126. {
  127.     static TICK t = 0;
  128.    
  129.     // Initialize any application specific hardware.
  130.     InitializeBoard();
  131.  
  132.     canInit();
  133.  
  134.     // Initialize Stack and application related NV variables.
  135.     InitAppConfig();
  136.  
  137.     StackInit();
  138.  
  139. #if defined(STACK_USE_HTTP_SERVER)
  140.     HTTPInit();
  141. #endif
  142.  
  143. #if defined(STACK_USE_SGP_SERVER)
  144.     SGPInit();
  145. #endif
  146.  
  147.  
  148.     // Once all items are initialized, go into infinite loop and let
  149.     // stack items execute their tasks.
  150.     // If application needs to perform its own task, it should be
  151.     // done at the end of while loop.
  152.     // Note that this is a "co-operative mult-tasking" mechanism
  153.     // where every task performs its tasks (whether all in one shot
  154.     // or part of it) and returns so that other tasks can do their
  155.     // job.
  156.     // If a task needs very long time to do its job, it must broken
  157.     // down into smaller pieces so that other tasks can have CPU time.
  158.     while(1)
  159.     {
  160.         // Blink SYSTEM LED every second.
  161.         if ( tickGet()-t >= TICK_1S/2 )
  162.         {
  163.             t = tickGet();
  164.             LED0_IO ^= 1;
  165.         }
  166.  
  167.         // This task performs normal stack task including checking
  168.         // for incoming packet, type of packet and calling
  169.         // appropriate stack entity to process it.
  170.         StackTask();
  171.  
  172. #if defined(STACK_USE_HTTP_SERVER)
  173.         // This is a TCP application.  It listens to TCP port 80
  174.         // with one or more sockets and responds to remote requests.
  175.         HTTPServer();
  176. #endif
  177.  
  178. #if defined(STACK_USE_SGP_SERVER)
  179.         // This is a TCP application.  It listens to TCP port 6666
  180.         // with one or more sockets and responds to remote requests.
  181.         SGPServer();
  182. #endif
  183.  
  184. #if defined(STACK_USE_TIMESYNC)
  185.     timeSync();
  186. #endif
  187.  
  188.         // In future, as new TCP/IP applications are written, it
  189.         // will be added here as new tasks.
  190.  
  191.         // Add your application speicifc tasks here.
  192.         ProcessIO();
  193.  
  194.     }
  195. }
  196.  
  197. static void ProcessIO(void)
  198. {
  199. }
  200.  
  201.  
  202. /*********************************************************************
  203.  * Function:        void HTTPExecCmd(BYTE** argv, BYTE argc)
  204.  *
  205.  * PreCondition:    None
  206.  *
  207.  * Input:           argv        - List of arguments
  208.  *                  argc        - Argument count.
  209.  *
  210.  * Output:          None
  211.  *
  212.  * Side Effects:    None
  213.  *
  214.  * Overview:        This function is a "callback" from HTTPServer
  215.  *                  task.  Whenever a remote node performs
  216.  *                  interactive task on page that was served,
  217.  *                  HTTPServer calls this functions with action
  218.  *                  arguments info.
  219.  *                  Main application should interpret this argument
  220.  *                  and act accordingly.
  221.  *
  222.  *                  Following is the format of argv:
  223.  *                  If HTTP action was : thank.htm?name=Joe&age=25
  224.  *                      argv[0] => thank.htm
  225.  *                      argv[1] => name
  226.  *                      argv[2] => Joe
  227.  *                      argv[3] => age
  228.  *                      argv[4] => 25
  229.  *
  230.  *                  Use argv[0] as a command identifier and rests
  231.  *                  of the items as command arguments.
  232.  *
  233.  * Note:            THIS IS AN EXAMPLE CALLBACK.
  234.  ********************************************************************/
  235. #if defined(STACK_USE_HTTP_SERVER)
  236.  
  237. void HTTPExecCmd(BYTE** argv, BYTE argc)
  238. {
  239.     setVariable(argv[1][0],(argv[2][0]=='1'?1:0));
  240. }
  241.  
  242. void setVariable(BYTE var, DWORD val)
  243. {
  244.     if (var==VAR_LED1) LED1_IO=val;
  245. }
  246.  
  247. DWORD getVariable(BYTE var)
  248. {
  249.     if (var==VAR_LED1) return LED1_IO;
  250. }
  251.  
  252. void getVariableName(BYTE var,char *str)
  253. {
  254.     BYTE i;
  255.     if (var==VAR_LED1)
  256.     {
  257.         for(i=0;i<sizeof(VAR_LED1_HUMAN);i++)
  258.             str[i]=VAR_LED1_HUMAN[i];
  259.     }
  260. }
  261.  
  262. #endif
  263.  
  264.  
  265.  
  266. /*
  267. *   Function: canParse
  268. *
  269. *   Input:  Received can message
  270. *   Output: none
  271. *   Pre-conditions: canInit and received packet.
  272. *   Affects: Sensors/actuators/etc. See code.
  273. *   Depends: none.
  274. */
  275. void canParse(CAN_PACKET cp)
  276. {
  277.  
  278. }
  279.  
  280.  
  281.  
  282.  
  283.  
  284. /*********************************************************************
  285.  * Function:        void InitializeBoard(void)
  286.  *
  287.  * PreCondition:    None
  288.  *
  289.  * Input:           None
  290.  *
  291.  * Output:          None
  292.  *
  293.  * Side Effects:    None
  294.  *
  295.  * Overview:        Initialize board specific hardware.
  296.  *
  297.  * Note:            None
  298.  ********************************************************************/
  299. static void InitializeBoard(void)
  300. {
  301.     // LEDs
  302.     LED0_TRIS = 0;
  303.     LED1_TRIS =0;
  304.     LED0_IO = 0;
  305.     LED1_IO = 0;
  306.  
  307.  
  308.     // Enable internal PORTB pull-ups
  309.     INTCON2bits.RBPU = 0;
  310.  
  311.     // Enable Interrupts
  312.     T0CON = 0;
  313.     RCONbits.IPEN=1;
  314.     INTCONbits.GIEH = 1;
  315.     INTCONbits.GIEL = 1;
  316.    
  317.  
  318. }
  319.  
  320. /*********************************************************************
  321.  * Function:        void InitAppConfig(void)
  322.  *
  323.  * PreCondition:    MPFSInit() is already called.
  324.  *
  325.  * Input:           None
  326.  *
  327.  * Output:          Write/Read non-volatile config variables.
  328.  *
  329.  * Side Effects:    None
  330.  *
  331.  * Overview:        None
  332.  *
  333.  * Note:            None
  334.  ********************************************************************/
  335. static void InitAppConfig(void)
  336. {
  337.  
  338.  AppConfig.Flags.bIsDHCPEnabled = FALSE;
  339.  
  340.     // IP
  341. //  AppConfig.MyIPAddr.v[0]=193;
  342. //  AppConfig.MyIPAddr.v[1]=11;
  343. //  AppConfig.MyIPAddr.v[2]=249;
  344. //  AppConfig.MyIPAddr.v[3]=228;
  345.  
  346.     AppConfig.MyIPAddr.v[0]=192;
  347.     AppConfig.MyIPAddr.v[1]=168;
  348.     AppConfig.MyIPAddr.v[2]=0;
  349.     AppConfig.MyIPAddr.v[3]=228;
  350.    
  351.     //MAC
  352.     AppConfig.MyMACAddr.v[0]=0x00;
  353.     AppConfig.MyMACAddr.v[1]=0x10;
  354.     AppConfig.MyMACAddr.v[2]=0xA7;
  355.     AppConfig.MyMACAddr.v[3]=0x02;
  356.     AppConfig.MyMACAddr.v[4]=0xF2;
  357.     AppConfig.MyMACAddr.v[5]=0xBF;
  358.  
  359.     // Mask
  360.     AppConfig.MyMask.v[0]=255;
  361.     AppConfig.MyMask.v[1]=255;
  362.     AppConfig.MyMask.v[2]=255;
  363.     AppConfig.MyMask.v[3]=0;
  364.  
  365.     // Gateway
  366. //  AppConfig.MyGateway.v[0]=193;
  367. //  AppConfig.MyGateway.v[1]=11;
  368. //  AppConfig.MyGateway.v[2]=249;
  369. //  AppConfig.MyGateway.v[3]=1;
  370.  
  371.     // Gateway
  372.     AppConfig.MyGateway.v[0]=192;
  373.     AppConfig.MyGateway.v[1]=168;
  374.     AppConfig.MyGateway.v[2]=0;
  375.     AppConfig.MyGateway.v[3]=1;
  376.  
  377.     // Dns
  378.     AppConfig.PrimaryDNSServer.v[0]=193;
  379.     AppConfig.PrimaryDNSServer.v[1]=11;
  380.     AppConfig.PrimaryDNSServer.v[2]=255;
  381.     AppConfig.PrimaryDNSServer.v[3]=230;
  382. }
  383.  
  384.  
  385.  
  386.  
  387. // Below are mandantory when using bootloader
  388. // define DEBUG_MODE to use without bootloader.
  389.  
  390. #ifndef DEBUG_MODE
  391. extern void _startup (void);
  392. #pragma code _RESET_INTERRUPT_VECTOR = RM_RESET_VECTOR
  393. void _reset (void)
  394. {
  395.     _asm goto _startup _endasm
  396. }
  397. #pragma code
  398. #endif
  399.  
  400. #pragma code _HIGH_INTERRUPT_VECTOR = RM_HIGH_INTERRUPT_VECTOR
  401. void _high_ISR (void)
  402. {
  403.     _asm GOTO high_isr _endasm
  404. }
  405. #pragma code
  406.  
  407. #pragma code _LOW_INTERRUPT_VECTOR = RM_LOW_INTERRUPT_VECTOR
  408. void _low_ISR (void)
  409. {
  410.     _asm GOTO low_isr _endasm
  411. }
  412. #pragma code
  413.  
  414.