SHARE
TWEET

Untitled

a guest Oct 15th, 2019 70 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*********************************************************************
  2.  *
  3.  *  Main Application Entry Point and TCP/IP Stack Demo
  4.  *  Module for Microchip TCP/IP Stack
  5.  *   -Demonstrates how to call and use the Microchip TCP/IP stack
  6.  *   -Reference: AN833
  7.  *
  8.  *********************************************************************
  9.  * FileName:        MainDemo.c
  10.  * Dependencies:    TCPIP.h
  11.  * Processor:       PIC18, PIC24F, PIC24H, dsPIC30F, dsPIC33F, PIC32
  12.  * Compiler:        Microchip C32 v1.05 or higher
  13.  *                  Microchip C30 v3.12 or higher
  14.  *                  Microchip C18 v3.30 or higher
  15.  *                  HI-TECH PICC-18 PRO 9.63PL2 or higher
  16.  * Company:         Microchip Technology, Inc.
  17.  *
  18.  * Software License Agreement
  19.  *
  20.  * Copyright (C) 2002-2009 Microchip Technology Inc.  All rights
  21.  * reserved.
  22.  *
  23.  * Microchip licenses to you the right to use, modify, copy, and
  24.  * distribute:
  25.  * (i)  the Software when embedded on a Microchip microcontroller or
  26.  *      digital signal controller product ("Device") which is
  27.  *      integrated into Licensee's product; or
  28.  * (ii) ONLY the Software driver source files ENC28J60.c, ENC28J60.h,
  29.  *      ENCX24J600.c and ENCX24J600.h ported to a non-Microchip device
  30.  *      used in conjunction with a Microchip ethernet controller for
  31.  *      the sole purpose of interfacing with the ethernet controller.
  32.  *
  33.  * You should refer to the license agreement accompanying this
  34.  * Software for additional information regarding your rights and
  35.  * obligations.
  36.  *
  37.  * THE SOFTWARE AND DOCUMENTATION ARE PROVIDED "AS IS" WITHOUT
  38.  * WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT
  39.  * LIMITATION, ANY WARRANTY OF MERCHANTABILITY, FITNESS FOR A
  40.  * PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT SHALL
  41.  * MICROCHIP BE LIABLE FOR ANY INCIDENTAL, SPECIAL, INDIRECT OR
  42.  * CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF
  43.  * PROCUREMENT OF SUBSTITUTE GOODS, TECHNOLOGY OR SERVICES, ANY CLAIMS
  44.  * BY THIRD PARTIES (INCLUDING BUT NOT LIMITED TO ANY DEFENSE
  45.  * THEREOF), ANY CLAIMS FOR INDEMNITY OR CONTRIBUTION, OR OTHER
  46.  * SIMILAR COSTS, WHETHER ASSERTED ON THE BASIS OF CONTRACT, TORT
  47.  * (INCLUDING NEGLIGENCE), BREACH OF WARRANTY, OR OTHERWISE.
  48.  *
  49.  *
  50.  * Author              Date         Comment
  51.  *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  52.  * Nilesh Rajbharti     4/19/01     Original (Rev. 1.0)
  53.  * Nilesh Rajbharti     2/09/02     Cleanup
  54.  * Nilesh Rajbharti     5/22/02     Rev 2.0 (See version.log for detail)
  55.  * Nilesh Rajbharti     7/9/02      Rev 2.1 (See version.log for detail)
  56.  * Nilesh Rajbharti     4/7/03      Rev 2.11.01 (See version log for detail)
  57.  * Howard Schlunder     10/1/04     Beta Rev 0.9 (See version log for detail)
  58.  * Howard Schlunder     10/8/04     Beta Rev 0.9.1 Announce support added
  59.  * Howard Schlunder     11/29/04    Beta Rev 0.9.2 (See version log for detail)
  60.  * Howard Schlunder     2/10/05     Rev 2.5.0
  61.  * Howard Schlunder     1/5/06      Rev 3.00
  62.  * Howard Schlunder     1/18/06     Rev 3.01 ENC28J60 fixes to TCP,
  63.  *                                  UDP and ENC28J60 files
  64.  * Howard Schlunder     3/01/06     Rev. 3.16 including 16-bit micro support
  65.  * Howard Schlunder     4/12/06     Rev. 3.50 added LCD for Explorer 16
  66.  * Howard Schlunder     6/19/06     Rev. 3.60 finished dsPIC30F support, added PICDEM.net 2 support
  67.  * Howard Schlunder     8/02/06     Rev. 3.75 added beta DNS, NBNS, and HTTP client (GenericTCPClient.c) services
  68.  * Howard Schlunder     12/28/06    Rev. 4.00RC added SMTP, Telnet, substantially modified TCP layer
  69.  * Howard Schlunder     04/09/07    Rev. 4.02 added TCPPerformanceTest, UDPPerformanceTest, Reboot and fixed some bugs
  70.  * Howard Schlunder     xx/xx/07    Rev. 4.03
  71.  * HSchlunder & EWood   08/27/07    Rev. 4.11
  72.  * HSchlunder & EWood   10/08/07    Rev. 4.13
  73.  * HSchlunder & EWood   11/06/07    Rev. 4.16
  74.  * HSchlunder & EWood   11/08/07    Rev. 4.17
  75.  * HSchlunder & EWood   11/12/07    Rev. 4.18
  76.  * HSchlunder & EWood   02/11/08    Rev. 4.19
  77.  * HSchlunder & EWood   04/26/08    Rev. 4.50 Moved most code to other files for clarity
  78.  * KHesky               07/07/08    Added ZG2100-specific support
  79.  * HSchlunder & EWood   07/24/08    Rev. 4.51
  80.  * Howard Schlunder     11/10/08    Rev. 4.55
  81.  * Howard Schlunder     04/14/09    Rev. 5.00
  82.  ********************************************************************/
  83. /*
  84.  * This macro uniquely defines this file as the main entry point.
  85.  * There should only be one such definition in the entire project,
  86.  * and this file must define the AppConfig variable as described below.
  87.  */
  88. #define THIS_IS_STACK_APPLICATION
  89.  
  90. // Include all headers for any enabled TCPIP Stack functions
  91. #include "TCPIP Stack/TCPIP.h"
  92.  
  93. // Include functions specific to this stack application
  94. #include "MainDemo.h"
  95.  
  96. // Declare AppConfig structure and some other supporting stack variables
  97. APP_CONFIG AppConfig;
  98. BYTE AN0String[8];
  99. BYTE myDHCPBindCount = 0xFF;
  100. #if !defined(STACK_USE_DHCP_CLIENT)
  101.     #define DHCPBindCount   (1)
  102. #endif
  103.  
  104. // Use UART2 instead of UART1 for stdout (printf functions).  Explorer 16
  105. // serial port hardware is on PIC UART2 module.
  106. #if defined(EXPLORER_16)
  107.     int __C30_UART = 2;
  108. #endif
  109.  
  110.  
  111. // Private helper functions.
  112. // These may or may not be present in all applications.
  113. static void InitAppConfig(void);
  114. void InitializeBoard(void);
  115. static void ProcessIO(void);
  116. extern void InterruptHandlerHigh (void);
  117.  
  118. //
  119. // PIC18 Interrupt Service Routines
  120. //
  121. // NOTE: Several PICs, including the PIC18F4620 revision A3 have a RETFIE FAST/MOVFF bug
  122. // The interruptlow keyword is used to work around the bug when using C18
  123. #if defined(__18CXX)
  124.     #if defined(HI_TECH_C)
  125.     void interrupt low_priority LowISR(void)
  126.     #else
  127.     #pragma interruptlow LowISR
  128.     void LowISR(void)
  129.     #endif
  130.     {
  131.         TickUpdate();
  132.     }
  133.    
  134.     #if defined(HI_TECH_C)
  135.     void interrupt HighISR(void)
  136.     #else
  137.     #pragma interruptlow HighISR
  138.     void HighISR(void)
  139.     #endif
  140.     {
  141.         #if defined(STACK_USE_UART2TCP_BRIDGE)
  142.             UART2TCPBridgeISR();
  143.         #endif
  144.  
  145.         #if defined(ZG_CS_TRIS)
  146.             zgEintISR();
  147.         #endif // ZG_CS_TRIS
  148.         InterruptHandlerHigh();
  149.     }
  150.  
  151.     #if !defined(HI_TECH_C)
  152.     #pragma code lowVector=0x18
  153.     void LowVector(void){_asm goto LowISR _endasm}
  154.     #pragma code highVector=0x8
  155.     void HighVector(void){_asm goto HighISR _endasm}
  156.     #pragma code // Return to default code section
  157.     #endif
  158.  
  159. // C30 and C32 Exception Handlers
  160. // If your code gets here, you either tried to read or write
  161. // a NULL pointer, or your application overflowed the stack
  162. // by having too many local variables or parameters declared.
  163. #elif defined(__C30__)
  164.     void _ISR __attribute__((__no_auto_psv__)) _AddressError(void)
  165.     {
  166.         Nop();
  167.         Nop();
  168.     }
  169.     void _ISR __attribute__((__no_auto_psv__)) _StackError(void)
  170.     {
  171.         Nop();
  172.         Nop();
  173.     }
  174.    
  175. #elif defined(__C32__)
  176.     void _general_exception_handler(unsigned cause, unsigned status)
  177.     {
  178.         Nop();
  179.         Nop();
  180.     }
  181. #endif
  182.  
  183. //
  184. // Main application entry point.
  185. //
  186. #if defined(__18CXX)
  187. void main(void)
  188.  
  189. #else
  190. int main(void)
  191. #endif
  192. {
  193.  
  194.     static TICK t = 0;
  195.  
  196.     static TICK t_poll = 0;
  197.     // Initialize application specific hardware
  198.     InitializeBoard();
  199. TRISE = 0;
  200. PORTE = 0;
  201.     #if defined(USE_LCD)
  202.     // Initialize and display the stack version on the LCD
  203.     LCDInit();
  204.     DelayMs(100);
  205.     strcpypgm2ram((char*)LCDText, "TCPStack " VERSION "  "
  206.         "                ");
  207.     LCDUpdate();
  208.     #endif
  209.  
  210.     // Initialize stack-related hardware components that may be
  211.     // required by the UART configuration routines
  212.     TickInit();
  213.     #if defined(STACK_USE_MPFS) || defined(STACK_USE_MPFS2)
  214.     MPFSInit();
  215.     #endif
  216.  
  217.     // Initialize Stack and application related NV variables into AppConfig.
  218.     InitAppConfig();
  219.  
  220.     // Initiates board setup process if button is depressed
  221.     // on startup
  222.     if(BUTTON0_IO == 0u)
  223.     {
  224.         #if defined(EEPROM_CS_TRIS) || defined(SPIFLASH_CS_TRIS)
  225.         // Invalidate the EEPROM contents if BUTTON0 is held down for more than 4 seconds
  226.         TICK StartTime = TickGet();
  227.         LED_PUT(0x00);
  228.                
  229.         while(BUTTON0_IO == 0u)
  230.         {
  231.             if(TickGet() - StartTime > 4*TICK_SECOND)
  232.             {
  233.                 #if defined(EEPROM_CS_TRIS)
  234.                 XEEBeginWrite(0x0000);
  235.                 XEEWrite(0xFF);
  236.                 XEEEndWrite();
  237.                 #elif defined(SPIFLASH_CS_TRIS)
  238.                 SPIFlashBeginWrite(0x0000);
  239.                 SPIFlashWrite(0xFF);
  240.                 #endif
  241.                
  242.                 #if defined(STACK_USE_UART)
  243.                 //putrsUART("\r\n\r\nBUTTON0 held for more than 4 seconds.  Default settings restored.\r\n\r\n");
  244.                 #endif
  245.  
  246.                 LED_PUT(0x0F);
  247.                 while((LONG)(TickGet() - StartTime) <= (LONG)(9*TICK_SECOND/2));
  248.                 LED_PUT(0x00);
  249.                 while(BUTTON0_IO == 0u);
  250.                 Reset();
  251.                 break;
  252.             }
  253.         }
  254.         #endif
  255.  
  256.         #if defined(STACK_USE_UART)
  257.         DoUARTConfig();
  258.         #endif
  259.     }
  260.  
  261.     // Initialize core stack layers (MAC, ARP, TCP, UDP) and
  262.     // application modules (HTTP, SNMP, etc.)
  263.     StackInit();
  264.  
  265.     // Initialize any application-specific modules or functions/
  266.     // For this demo application, this only includes the
  267.     // UART 2 TCP Bridge
  268.     #if defined(STACK_USE_UART2TCP_BRIDGE)
  269.     UART2TCPBridgeInit();
  270.     #endif
  271.  
  272.     // Now that all items are initialized, begin the co-operative
  273.     // multitasking loop.  This infinite loop will continuously
  274.     // execute all stack-related tasks, as well as your own
  275.     // application's functions.  Custom functions should be added
  276.     // at the end of this loop.
  277.     // Note that this is a "co-operative mult-tasking" mechanism
  278.     // where every task performs its tasks (whether all in one shot
  279.     // or part of it) and returns so that other tasks can do their
  280.     // job.
  281.     // If a task needs very long time to do its job, it must be broken
  282.     // down into smaller pieces so that other tasks can have CPU time.
  283.     while(1)
  284.     {
  285.         // Blink LED0 (right most one) every second.
  286.         if(TickGet() - t >= TICK_SECOND/2ul)
  287.         {
  288.             t = TickGet();
  289.            // LED0_IO ^= 1;
  290.         }
  291.  
  292.  
  293. if(TickGet() - t_poll >= TICK_SECOND)
  294.     {
  295.         t_poll = TickGet();
  296.         //while(BusyUSART());
  297.         TXSTAbits.TX9D = 1;
  298.         while(BusyUSART());
  299.         putcUART(0x80);
  300.         //DelayMs(15);
  301.     //  while(BusyUSART());
  302.         //putcUART(0x81);
  303.         //DelayMs(10);
  304.         //TXSTAbits.TX9D = 0;
  305.         LED1_IO ^= 1;
  306.     }
  307.  
  308. if(BUTTON0_IO == 0u)
  309.     {
  310. //01 8A 00 10 00 00 00 D8 70
  311. // 01 8A 00 01 00 00 00 C2 AF
  312. TXSTAbits.TX9D = 1;
  313. while(BusyUSART());
  314. putcUART(0x80);
  315. while(BusyUSART());
  316. putcUART(0x01);
  317. TXSTAbits.TX9D = 0;
  318. while(BusyUSART());
  319. putcUART(0x8A);        
  320. while(BusyUSART());
  321. putcUART(0x00);
  322. while(BusyUSART());
  323. putcUART(0x10);
  324. while(BusyUSART());
  325. putcUART(0x00);
  326. while(BusyUSART());
  327. putcUART(0x00);
  328. while(BusyUSART());
  329. putcUART(0x00);
  330. while(BusyUSART());
  331. putcUART(0xD8);
  332. while(BusyUSART());
  333. putcUART(0x70);
  334. while (BUTTON0_IO == 0u);
  335.  
  336.     }
  337.  
  338.  
  339.  
  340.         // This task performs normal stack task including checking
  341.         // for incoming packet, type of packet and calling
  342.         // appropriate stack entity to process it.
  343.         StackTask();
  344.        
  345.         // This tasks invokes each of the core stack application tasks
  346.         StackApplications();
  347.  
  348.         // Process application specific tasks here.
  349.         // For this demo app, this will include the Generic TCP
  350.         // client and servers, and the SNMP, Ping, and SNMP Trap
  351.         // demos.  Following that, we will process any IO from
  352.         // the inputs on the board itself.
  353.         // Any custom modules or processing you need to do should
  354.         // go here.
  355.         #if defined(STACK_USE_GENERIC_TCP_CLIENT_EXAMPLE)
  356.         GenericTCPClient();
  357.         #endif
  358.        
  359.         #if defined(STACK_USE_GENERIC_TCP_SERVER_EXAMPLE)
  360.         GenericTCPServer();
  361.         #endif
  362.        
  363.         #if defined(STACK_USE_SMTP_CLIENT)
  364.         SMTPDemo();
  365.         #endif
  366.        
  367.         #if defined(STACK_USE_ICMP_CLIENT)
  368.         PingDemo();
  369.         #endif
  370.        
  371.         #if defined(STACK_USE_SNMP_SERVER) && !defined(SNMP_TRAP_DISABLED)
  372.         SNMPTrapDemo();
  373.         if(gSendTrapFlag)
  374.             SNMPSendTrap();
  375.         #endif
  376.        
  377.         #if defined(STACK_USE_BERKELEY_API)
  378.         BerkeleyTCPClientDemo();
  379.         BerkeleyTCPServerDemo();
  380.         BerkeleyUDPClientDemo();
  381.         #endif
  382.  
  383.         ProcessIO();
  384.  
  385.         // If the DHCP lease has changed recently, write the new
  386.         // IP address to the LCD display, UART, and ounce service
  387.         if(DHCPBindCount != myDHCPBindCount)
  388.         {
  389.             myDHCPBindCount = DHCPBindCount;
  390.  
  391.             //#if defined(STACK_USE_UART)
  392.             //  putrsUART((ROM char*)"\r\nNew IP Address: ");
  393.             //#endif
  394.  
  395.             //DisplayIPValue(AppConfig.MyIPAddr);
  396.  
  397.             //#if defined(STACK_USE_UART)
  398.             //  putrsUART((ROM char*)"\r\n");
  399.             //#endif
  400.  
  401.  
  402.             //#if defined(STACK_USE_ANNOUNCE)
  403.             //  AnnounceIP();
  404.             //#endif
  405.         }
  406.  
  407.     }
  408. }
  409.  
  410. // Writes an IP address to the LCD display and the UART as available
  411. void DisplayIPValue(IP_ADDR IPVal)
  412. {
  413. //  printf("%u.%u.%u.%u", IPVal.v[0], IPVal.v[1], IPVal.v[2], IPVal.v[3]);
  414.     BYTE IPDigit[4];
  415.     BYTE i;
  416. #ifdef USE_LCD
  417.     BYTE j;
  418.     BYTE LCDPos=16;
  419. #endif
  420.  
  421.     for(i = 0; i < sizeof(IP_ADDR); i++)
  422.     {
  423.         uitoa((WORD)IPVal.v[i], IPDigit);
  424.  
  425.         #if defined(STACK_USE_UART)
  426.             putsUART(IPDigit);
  427.         #endif
  428.  
  429.         #ifdef USE_LCD
  430.             for(j = 0; j < strlen((char*)IPDigit); j++)
  431.             {
  432.                 LCDText[LCDPos++] = IPDigit[j];
  433.             }
  434.             if(i == sizeof(IP_ADDR)-1)
  435.                 break;
  436.             LCDText[LCDPos++] = '.';
  437.         #else
  438.             if(i == sizeof(IP_ADDR)-1)
  439.                 break;
  440.         #endif
  441.  
  442.         #if defined(STACK_USE_UART)
  443.             while(BusyUART());
  444.             WriteUART('.');
  445.         #endif
  446.     }
  447.  
  448.     #ifdef USE_LCD
  449.         if(LCDPos < 32u)
  450.             LCDText[LCDPos] = 0;
  451.         LCDUpdate();
  452.     #endif
  453. }
  454.  
  455. // Processes A/D data from the potentiometer
  456. static void ProcessIO(void)
  457. {
  458. #if defined(__C30__) || defined(__C32__)
  459.     // Convert potentiometer result into ASCII string
  460.     uitoa((WORD)ADC1BUF0, AN0String);
  461. #else
  462.     // AN0 should already be set up as an analog input
  463.     ADCON0bits.GO = 1;
  464.  
  465.     // Wait until A/D conversion is done
  466.     while(ADCON0bits.GO);
  467.  
  468.     // AD converter errata work around (ex: PIC18F87J10 A2)
  469.     #if !defined(__18F87J50) && !defined(_18F87J50) && !defined(__18F87J11) && !defined(_18F87J11)
  470.         PRODL = ADCON2;
  471.         ADCON2 |= 0x7;  // Select Frc mode by setting ADCS0/ADCS1/ADCS2
  472.         ADCON2 = PRODL;
  473.     #endif
  474.  
  475.     // Convert 10-bit value into ASCII string
  476.     uitoa(*((WORD*)(&ADRESL)), AN0String);
  477. #endif
  478. }
  479.  
  480.  
  481. /****************************************************************************
  482.   Function:
  483.     void InitializeBoard(void)
  484.  
  485.   Description:
  486.     This routine initializes the hardware.  It is a generic initialization
  487.     routine for many of the Microchip development boards, using definitions
  488.     in HardwareProfile.h to determine specific initialization.
  489.  
  490.   Precondition:
  491.     None
  492.  
  493.   Parameters:
  494.     None - None
  495.  
  496.   Returns:
  497.     None
  498.  
  499.   Remarks:
  500.     None
  501.   ***************************************************************************/
  502. void InitializeBoard(void)
  503. {  
  504.     // LEDs
  505.     DIR_TRIS = 0;
  506.     DIR_IO = 0;
  507.     LED0_TRIS = 0;
  508.     LED1_TRIS = 0;
  509.     LED2_TRIS = 0;
  510.     LED3_TRIS = 0;
  511.     LED4_TRIS = 0;
  512.     LED5_TRIS = 0;
  513.     LED6_TRIS = 0;
  514. #if !defined(EXPLORER_16)   // Pin multiplexed with a button on EXPLORER_16
  515.     LED7_TRIS = 0;
  516. #endif
  517.     LED_PUT(0x00);
  518.  
  519. #if defined(__18CXX)
  520.     // Enable 4x/5x/96MHz PLL on PIC18F87J10, PIC18F97J60, PIC18F87J50, etc.
  521.     OSCTUNE = 0x40;
  522.  
  523.     // Set up analog features of PORTA
  524.  
  525.     // PICDEM.net 2 board has POT on AN2, Temp Sensor on AN3
  526.     #if defined(PICDEMNET2)
  527.         ADCON0 = 0x09;      // ADON, Channel 2
  528.         ADCON1 = 0x0B;      // Vdd/Vss is +/-REF, AN0, AN1, AN2, AN3 are analog
  529.     #elif defined(PICDEMZ)
  530.         ADCON0 = 0x81;      // ADON, Channel 0, Fosc/32
  531.         ADCON1 = 0x0F;      // Vdd/Vss is +/-REF, AN0, AN1, AN2, AN3 are all digital
  532.     #elif defined(__18F87J11) || defined(_18F87J11) || defined(__18F87J50) || defined(_18F87J50)
  533.         ADCON0 = 0x01;      // ADON, Channel 0, Vdd/Vss is +/-REF
  534.         WDTCONbits.ADSHR = 1;
  535.         ANCON0 = 0xFC;      // AN0 (POT) and AN1 (temp sensor) are anlog
  536.         ANCON1 = 0xFF;
  537.         WDTCONbits.ADSHR = 0;      
  538.     #else
  539.         ADCON0 = 0x01;      // ADON, Channel 0
  540.         ADCON1 = 0x0E;      // Vdd/Vss is +/-REF, AN0 is analog
  541.     #endif
  542.     ADCON2 = 0xBE;      // Right justify, 20TAD ACQ time, Fosc/64 (~21.0kHz)
  543.  
  544.  
  545.     // Enable internal PORTB pull-ups
  546.     INTCON2bits.RBPU = 0;
  547.  
  548.     // Configure USART
  549.     TXSTA = 0x60;
  550.     RCSTA = 0xD0;
  551.  
  552.     // See if we can use the high baud rate setting
  553.     #if ((GetPeripheralClock()+2*BAUD_RATE)/BAUD_RATE/4 - 1) <= 255
  554.         SPBRG = (GetPeripheralClock()+2*BAUD_RATE)/BAUD_RATE/4 - 1;
  555.         TXSTAbits.BRGH = 1;
  556.     #else   // Use the low baud rate setting
  557.         SPBRG = (GetPeripheralClock()+8*BAUD_RATE)/BAUD_RATE/16 - 1;
  558.     #endif
  559.  
  560.  
  561.     // Enable Interrupts
  562.     RCONbits.IPEN = 1;      // Enable interrupt priorities
  563.     INTCONbits.GIEH = 1;
  564.     INTCONbits.GIEL = 1;
  565.  
  566.     // Do a calibration A/D conversion
  567.     #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) || \
  568.          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)
  569.         ADCON0bits.ADCAL = 1;
  570.         ADCON0bits.GO = 1;
  571.         while(ADCON0bits.GO);
  572.         ADCON0bits.ADCAL = 0;
  573.     #elif defined(__18F87J11) || defined(__18F86J16) || defined(__18F86J11) || defined(__18F67J11) || defined(__18F66J16) || defined(__18F66J11) || \
  574.            defined(_18F87J11) ||  defined(_18F86J16) ||  defined(_18F86J11) ||  defined(_18F67J11) ||  defined(_18F66J16) ||  defined(_18F66J11) || \
  575.           defined(__18F87J50) || defined(__18F86J55) || defined(__18F86J50) || defined(__18F67J50) || defined(__18F66J55) || defined(__18F66J50) || \
  576.            defined(_18F87J50) ||  defined(_18F86J55) ||  defined(_18F86J50) ||  defined(_18F67J50) ||  defined(_18F66J55) ||  defined(_18F66J50)
  577.         ADCON1bits.ADCAL = 1;
  578.         ADCON0bits.GO = 1;
  579.         while(ADCON0bits.GO);
  580.         ADCON1bits.ADCAL = 0;
  581.     #endif
  582.  
  583. #else   // 16-bit C30 and and 32-bit C32
  584.     #if defined(__PIC32MX__)
  585.     {
  586.         // Enable multi-vectored interrupts
  587.         INTEnableSystemMultiVectoredInt();
  588.        
  589.         // Enable optimal performance
  590.         SYSTEMConfigPerformance(GetSystemClock());
  591.         mOSCSetPBDIV(OSC_PB_DIV_1);             // Use 1:1 CPU Core:Peripheral clocks
  592.        
  593.         // Disable JTAG port so we get our I/O pins back, but first
  594.         // wait 50ms so if you want to reprogram the part with
  595.         // JTAG, you'll still have a tiny window before JTAG goes away
  596.         DelayMs(50);
  597.         DDPCONbits.JTAGEN = 0;
  598.         LED_PUT(0x00);              // Turn the LEDs off
  599.     }
  600.     #endif
  601.  
  602.     #if defined(__dsPIC33F__) || defined(__PIC24H__)
  603.         // Crank up the core frequency
  604.         PLLFBD = 38;                // Multiply by 40 for 160MHz VCO output (8MHz XT oscillator)
  605.         CLKDIV = 0x0000;            // FRC: divide by 2, PLLPOST: divide by 2, PLLPRE: divide by 2
  606.    
  607.         // Port I/O
  608.         AD1PCFGHbits.PCFG23 = 1;    // Make RA7 (BUTTON1) a digital input
  609.  
  610.         // ADC
  611.         AD1CHS0 = 0;                // Input to AN0 (potentiometer)
  612.         AD1PCFGLbits.PCFG5 = 0;     // Disable digital input on AN5 (potentiometer)
  613.         AD1PCFGLbits.PCFG4 = 0;     // Disable digital input on AN4 (TC1047A temp sensor)
  614.     #else   //defined(__PIC24F__) || defined(__PIC32MX__)
  615.         #if defined(__PIC24F__)
  616.             CLKDIVbits.RCDIV = 0;       // Set 1:1 8MHz FRC postscalar
  617.         #endif
  618.        
  619.         // ADC
  620.         AD1CHS = 0;                 // Input to AN0 (potentiometer)
  621.         AD1PCFGbits.PCFG4 = 0;      // Disable digital input on AN4 (TC1047A temp sensor)
  622.         #if defined(__32MX460F512L__)   // PIC32MX460F512L PIM has different pinout to accomodate USB module
  623.             AD1PCFGbits.PCFG2 = 0;      // Disable digital input on AN2 (potentiometer)
  624.         #else
  625.             AD1PCFGbits.PCFG5 = 0;      // Disable digital input on AN5 (potentiometer)
  626.         #endif
  627.     #endif
  628.  
  629.     // ADC
  630.     AD1CON1 = 0x84E4;           // Turn on, auto sample start, auto-convert, 12 bit mode (on parts with a 12bit A/D)
  631.     AD1CON2 = 0x0404;           // AVdd, AVss, int every 2 conversions, MUXA only, scan
  632.     AD1CON3 = 0x1003;           // 16 Tad auto-sample, Tad = 3*Tcy
  633.     #if defined(__32MX460F512L__)   // PIC32MX460F512L PIM has different pinout to accomodate USB module
  634.         AD1CSSL = 1<<2;             // Scan pot
  635.     #else
  636.         AD1CSSL = 1<<5;             // Scan pot
  637.     #endif
  638.  
  639.     // UART
  640.     #if defined(STACK_USE_UART)
  641.         UARTTX_TRIS = 0;
  642.         UARTRX_TRIS = 1;
  643.         UMODE = 0x8000;         // Set UARTEN.  Note: this must be done before setting UTXEN
  644.  
  645.         #if defined(__C30__)
  646.             USTA = 0x0400;      // UTXEN set
  647.             #define CLOSEST_UBRG_VALUE ((GetPeripheralClock()+8ul*BAUD_RATE)/16/BAUD_RATE-1)
  648.             #define BAUD_ACTUAL (GetPeripheralClock()/16/(CLOSEST_UBRG_VALUE+1))
  649.         #else   //defined(__C32__)
  650.             USTA = 0x00001400;      // RXEN set, TXEN set
  651.             #define CLOSEST_UBRG_VALUE ((GetPeripheralClock()+8ul*BAUD_RATE)/16/BAUD_RATE-1)
  652.             #define BAUD_ACTUAL (GetPeripheralClock()/16/(CLOSEST_UBRG_VALUE+1))
  653.         #endif
  654.    
  655.         #define BAUD_ERROR ((BAUD_ACTUAL > BAUD_RATE) ? BAUD_ACTUAL-BAUD_RATE : BAUD_RATE-BAUD_ACTUAL)
  656.         #define BAUD_ERROR_PRECENT  ((BAUD_ERROR*100+BAUD_RATE/2)/BAUD_RATE)
  657.         #if (BAUD_ERROR_PRECENT > 3)
  658.             #warning UART frequency error is worse than 3%
  659.         #elif (BAUD_ERROR_PRECENT > 2)
  660.             #warning UART frequency error is worse than 2%
  661.         #endif
  662.    
  663.         UBRG = CLOSEST_UBRG_VALUE;
  664.     #endif
  665.  
  666. #endif
  667.  
  668. #if defined(PIC24FJ64GA004_PIM)
  669.     // Remove some LED outputs to regain other functions
  670.     LED1_TRIS = 1;      // Multiplexed with BUTTON0
  671.     LED5_TRIS = 1;      // Multiplexed with EEPROM CS
  672.     LED7_TRIS = 1;      // Multiplexed with BUTTON1
  673.    
  674.     // Inputs
  675.     RPINR19bits.U2RXR = 19;         //U2RX = RP19
  676.     RPINR22bits.SDI2R = 20;         //SDI2 = RP20
  677.     RPINR20bits.SDI1R = 17;         //SDI1 = RP17
  678.    
  679.     // Outputs
  680.     RPOR12bits.RP25R = U2TX_IO;     //RP25 = U2TX  
  681.     RPOR12bits.RP24R = SCK2OUT_IO;  //RP24 = SCK2
  682.     RPOR10bits.RP21R = SDO2_IO;     //RP21 = SDO2
  683.     RPOR7bits.RP15R = SCK1OUT_IO;   //RP15 = SCK1
  684.     RPOR8bits.RP16R = SDO1_IO;      //RP16 = SDO1
  685.    
  686.     AD1PCFG = 0xFFFF;               //All digital inputs - POT and Temp are on same pin as SDO1/SDI1, which is needed for ENC28J60 commnications
  687.  
  688.     // Lock the PPS
  689.     asm volatile (  "mov #OSCCON,w1 \n"
  690.                     "mov #0x46, w2 \n"
  691.                     "mov #0x57, w3 \n"
  692.                     "mov.b w2,[w1] \n"
  693.                     "mov.b w3,[w1] \n"
  694.                     "bset OSCCON, #6");
  695. #endif
  696.  
  697. #if defined(DSPICDEM11)
  698.     // Deselect the LCD controller (PIC18F252 onboard) to ensure there is no SPI2 contention
  699.     LCDCTRL_CS_TRIS = 0;
  700.     LCDCTRL_CS_IO = 1;
  701.  
  702.     // Hold the codec in reset to ensure there is no SPI2 contention
  703.     CODEC_RST_TRIS = 0;
  704.     CODEC_RST_IO = 0;
  705. #endif
  706.  
  707. #if defined(SPIRAM_CS_TRIS)
  708.     SPIRAMInit();
  709. #endif
  710. #if defined(EEPROM_CS_TRIS)
  711.     XEEInit();
  712. #endif
  713. #if defined(SPIFLASH_CS_TRIS)
  714.     SPIFlashInit();
  715. #endif
  716. }
  717.  
  718. /*********************************************************************
  719.  * Function:        void InitAppConfig(void)
  720.  *
  721.  * PreCondition:    MPFSInit() is already called.
  722.  *
  723.  * Input:           None
  724.  *
  725.  * Output:          Write/Read non-volatile config variables.
  726.  *
  727.  * Side Effects:    None
  728.  *
  729.  * Overview:        None
  730.  *
  731.  * Note:            None
  732.  ********************************************************************/
  733. // MAC Address Serialization using a MPLAB PM3 Programmer and
  734. // Serialized Quick Turn Programming (SQTP).
  735. // The advantage of using SQTP for programming the MAC Address is it
  736. // allows you to auto-increment the MAC address without recompiling
  737. // the code for each unit.  To use SQTP, the MAC address must be fixed
  738. // at a specific location in program memory.  Uncomment these two pragmas
  739. // that locate the MAC address at 0x1FFF0.  Syntax below is for MPLAB C
  740. // Compiler for PIC18 MCUs. Syntax will vary for other compilers.
  741. //#pragma romdata MACROM=0x1FFF0
  742. 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_DEFAULT_MAC_BYTE6};
  743. //#pragma romdata
  744.  
  745. static void InitAppConfig(void)
  746. {
  747.     AppConfig.Flags.bIsDHCPEnabled = TRUE;
  748.     AppConfig.Flags.bInConfigMode = TRUE;
  749.     memcpypgm2ram((void*)&AppConfig.MyMACAddr, (ROM void*)SerializedMACAddress, sizeof(AppConfig.MyMACAddr));
  750. //  {
  751. //      _prog_addressT MACAddressAddress;
  752. //      MACAddressAddress.next = 0x157F8;
  753. //      _memcpy_p2d24((char*)&AppConfig.MyMACAddr, MACAddressAddress, sizeof(AppConfig.MyMACAddr));
  754. //  }
  755.     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;
  756.     AppConfig.DefaultIPAddr.Val = AppConfig.MyIPAddr.Val;
  757.     AppConfig.MyMask.Val = MY_DEFAULT_MASK_BYTE1 | MY_DEFAULT_MASK_BYTE2<<8ul | MY_DEFAULT_MASK_BYTE3<<16ul | MY_DEFAULT_MASK_BYTE4<<24ul;
  758.     AppConfig.DefaultMask.Val = AppConfig.MyMask.Val;
  759.     AppConfig.MyGateway.Val = MY_DEFAULT_GATE_BYTE1 | MY_DEFAULT_GATE_BYTE2<<8ul | MY_DEFAULT_GATE_BYTE3<<16ul | MY_DEFAULT_GATE_BYTE4<<24ul;
  760.     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;
  761.     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;
  762.  
  763.  
  764.     // SNMP Community String configuration
  765.     #if defined(STACK_USE_SNMP_SERVER)
  766.     {
  767.         BYTE i;
  768.         static ROM char * ROM cReadCommunities[] = SNMP_READ_COMMUNITIES;
  769.         static ROM char * ROM cWriteCommunities[] = SNMP_WRITE_COMMUNITIES;
  770.         ROM char * strCommunity;
  771.        
  772.         for(i = 0; i < SNMP_MAX_COMMUNITY_SUPPORT; i++)
  773.         {
  774.             // Get a pointer to the next community string
  775.             strCommunity = cReadCommunities[i];
  776.             if(i >= sizeof(cReadCommunities)/sizeof(cReadCommunities[0]))
  777.                 strCommunity = "";
  778.  
  779.             // Ensure we don't buffer overflow.  If your code gets stuck here,
  780.             // it means your SNMP_COMMUNITY_MAX_LEN definition in TCPIPConfig.h
  781.             // is either too small or one of your community string lengths
  782.             // (SNMP_READ_COMMUNITIES) are too large.  Fix either.
  783.             if(strlenpgm(strCommunity) >= sizeof(AppConfig.readCommunity[0]))
  784.                 while(1);
  785.            
  786.             // Copy string into AppConfig
  787.             strcpypgm2ram((char*)AppConfig.readCommunity[i], strCommunity);
  788.  
  789.             // Get a pointer to the next community string
  790.             strCommunity = cWriteCommunities[i];
  791.             if(i >= sizeof(cWriteCommunities)/sizeof(cWriteCommunities[0]))
  792.                 strCommunity = "";
  793.  
  794.             // Ensure we don't buffer overflow.  If your code gets stuck here,
  795.             // it means your SNMP_COMMUNITY_MAX_LEN definition in TCPIPConfig.h
  796.             // is either too small or one of your community string lengths
  797.             // (SNMP_WRITE_COMMUNITIES) are too large.  Fix either.
  798.             if(strlenpgm(strCommunity) >= sizeof(AppConfig.writeCommunity[0]))
  799.                 while(1);
  800.  
  801.             // Copy string into AppConfig
  802.             strcpypgm2ram((char*)AppConfig.writeCommunity[i], strCommunity);
  803.         }
  804.     }
  805.     #endif
  806.  
  807.     // Load the default NetBIOS Host Name
  808.     memcpypgm2ram(AppConfig.NetBIOSName, (ROM void*)MY_DEFAULT_HOST_NAME, 16);
  809.     FormatNetBIOSName(AppConfig.NetBIOSName);
  810.  
  811.     #if defined(ZG_CS_TRIS)
  812.         // Load the default SSID Name
  813.         if (sizeof(MY_DEFAULT_SSID_NAME) > sizeof(AppConfig.MySSID))
  814.         {
  815.             ZGSYS_DRIVER_ASSERT(5, (ROM char *)"AppConfig.MySSID[] too small.\n");
  816.         }
  817.         memcpypgm2ram(AppConfig.MySSID, (ROM void*)MY_DEFAULT_SSID_NAME, sizeof(MY_DEFAULT_SSID_NAME));
  818.     #endif
  819.  
  820.     #if defined(EEPROM_CS_TRIS)
  821.     {
  822.         BYTE c;
  823.        
  824.         // When a record is saved, first byte is written as 0x60 to indicate
  825.         // that a valid record was saved.  Note that older stack versions
  826.         // used 0x57.  This change has been made to so old EEPROM contents
  827.         // will get overwritten.  The AppConfig() structure has been changed,
  828.         // resulting in parameter misalignment if still using old EEPROM
  829.         // contents.
  830.         XEEReadArray(0x0000, &c, 1);
  831.         if(c == 0x60u)
  832.             XEEReadArray(0x0001, (BYTE*)&AppConfig, sizeof(AppConfig));
  833.         else
  834.             SaveAppConfig();
  835.     }
  836.     #elif defined(SPIFLASH_CS_TRIS)
  837.     {
  838.         BYTE c;
  839.        
  840.         SPIFlashReadArray(0x0000, &c, 1);
  841.         if(c == 0x60u)
  842.             SPIFlashReadArray(0x0001, (BYTE*)&AppConfig, sizeof(AppConfig));
  843.         else
  844.             SaveAppConfig();
  845.     }
  846.     #endif
  847. }
  848.  
  849. #if defined(EEPROM_CS_TRIS) || defined(SPIFLASH_CS_TRIS)
  850. void SaveAppConfig(void)
  851. {
  852.     // Ensure adequate space has been reserved in non-volatile storage to
  853.     // store the entire AppConfig structure.  If you get stuck in this while(1)
  854.     // trap, it means you have a design time misconfiguration in TCPIPConfig.h.
  855.     // You must increase MPFS_RESERVE_BLOCK to allocate more space.
  856.     #if defined(STACK_USE_MPFS) || defined(STACK_USE_MPFS2)
  857.         if(sizeof(AppConfig) > MPFS_RESERVE_BLOCK)
  858.             while(1);
  859.     #endif
  860.  
  861.     #if defined(EEPROM_CS_TRIS)
  862.         XEEBeginWrite(0x0000);
  863.         XEEWrite(0x60);
  864.         XEEWriteArray((BYTE*)&AppConfig, sizeof(AppConfig));
  865.     #else
  866.         SPIFlashBeginWrite(0x0000);
  867.         SPIFlashWrite(0x60);
  868.         SPIFlashWriteArray((BYTE*)&AppConfig, sizeof(AppConfig));
  869.     #endif
  870. }
  871. #endif
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top