/**************************************************************************** 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; LED_PUT(0x00); // 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 DelayMs(50); DDPCONbits.JTAGEN = 0; LED_PUT(0x00); // Turn the LEDs off AD1PCFGbits.PCFG2 = 0; // Disable digital input on AN2 (potentiometer) // 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 // PIC32MX460F512L PIM has different pinout to accomodate USB module AD1CSSL = 1<<2; // Scan pot }
/**************************************************************************** Function: void BSP_Initialize(void) Description: This routine initializes the hardware. Precondition: None Parameters: None - None Returns: None Remarks: None ***************************************************************************/ void BSP_Initialize(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); LED_PUT(0x00); // Turn the LEDs off CNPUESET = 0x00098000; // Turn on weak pull ups on CN15, CN16, CN19 (RD5, RD7, RD13), which is connected to buttons on PIC32 Starter Kit boards // Initialize MEB CPLD // Graphics 16-bit LATBCLR = 0x100; // WiFi CE signal TRISBCLR = 0x100; LATGSET = 0x4000; // G14: 0=8-bit, 1=16-bit TRISGCLR = 0x4000; // SPI2 input LATGCLR = 0x1000; // G12: 0=SPI3A, 1=SPI2 TRISGCLR = 0x1000; LATACLR = 0xc0; // Clear SPI MUX // 00 - 16MB SPI Flash on board // 01 - 802.11 module // 10 - Expansion Connector // 11 - Reserved LATASET = 0x40; // 01 - 802.11 TRISACLR = 0xc0; // 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 AD1CSSL = 1 << 2; // Scan pot }
/**************************************************************************** Function: void BSP_Initialize(void) Description: This routine initializes the hardware. Precondition: None Parameters: None - None Returns: None Remarks: None ***************************************************************************/ void BSP_Initialize(void) { // LEDs LED0_TRIS = 0; LED1_TRIS = 0; LED2_TRIS = 0; LED_PUT(0x00); CNPUESET = 0x00098000; // Turn on weak pull ups on CN15, CN16, CN19 (RD5, RD7, RD13), which is connected to buttons on PIC32 Starter Kit boards // 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 AD1CSSL = 1 << 2; // Scan pot }
void RestoreWifiConfig(void) { putrsUART((ROM char*) "\r\nButton push, restore wifi configuration!!!\r\n"); #if defined(EEPROM_CS_TRIS) XEEBeginWrite(0x0000); XEEWrite(0xFF); XEEWrite(0xFF); XEEEndWrite(); #elif defined(SPIFLASH_CS_TRIS) SPIFlashBeginWrite(0x0000); SPIFlashWrite(0xFF); SPIFlashWrite(0xFF); #endif // reboot here... LED_PUT(0x00); while (BUTTON3_IO == 0u); Reset(); }
/**************************************************************************** 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; //LED0 LED1_TRIS = 0; //LED1 LED2_TRIS = 0; //LED2 LED3_TRIS = 0; //LED_LCD1 LED4_TRIS = 0; //LED_LCD2 LED5_TRIS = 0; //LED5=RELAY1 LED6_TRIS = 0; //LED7=RELAY2 #if (!defined(EXPLORER_16) &&!defined(OLIMEX_MAXI)) // Pin multiplexed with // a button on EXPLORER_16 and not used on OLIMEX_MAXI LED7_TRIS = 0; #endif LED_PUT(0x00); //turn off LED0 - LED2 RELAY_PUT(0x00); //turn relays off to save power // Enable backlight LED3_IO = 1; //set clock to 25 MHz // Enable PLL but disable pre and postscalers: the primary oscillator // runs at the speed of the 25MHz external quartz OSCTUNE = 0x40; // Switch to primary oscillator mode, // regardless of if the config fuses tell us to start operating using // the the internal RC // The external clock must be running and must be 25MHz for the // Ethernet module and thus this Ethernet bootloader to operate. if(OSCCONbits.IDLEN) //IDLEN = 0x80; 0x02 selects the primary clock OSCCON = 0x82; else OSCCON = 0x02; // Enable Interrupts RCONbits.IPEN = 1; // Enable interrupt priorities INTCONbits.GIEH = 1; INTCONbits.GIEL = 1; }
/**************************************************************************** Function: void BSP_Initialize(void) Description: This routine initializes the hardware. Precondition: None Parameters: None - None Returns: None Remarks: None ***************************************************************************/ void BSP_Initialize(void) { // Make LED and BUTTON pins digital ANSELHCLR = 0x0007; ANSELBCLR = 0x7000; CNPUBbits.CNPUB12 = 1; CNPUBbits.CNPUB13 = 1; CNPUBbits.CNPUB14 = 1; // 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); }
// // Main application entry point. // int main(void) { #if defined(APP_USE_IPERF) static uint8_t iperfOk = 0; #endif static SYS_TICK startTick = 0; static IP_ADDR dwLastIP[sizeof (TCPIP_HOSTS_CONFIGURATION) / sizeof (*TCPIP_HOSTS_CONFIGURATION)]; uint8_t i; // perform system initialization if(!SYS_Initialize()) { return 0; } SYS_CONSOLE_MESSAGE("\r\n\n\n --- Unified TCPIP Demo Starts! --- \r\n"); SYS_OUT_MESSAGE("TCPStack " TCPIP_STACK_VERSION " "" "); #if defined(TCPIP_STACK_USE_MPFS) || defined(TCPIP_STACK_USE_MPFS2) MPFSInit(); #endif // Initiates board setup process if button is depressed // on startup if(BUTTON0_IO == 0u) { #if defined(TCPIP_STACK_USE_STORAGE) && (defined(SPIFLASH_CS_TRIS) || defined(EEPROM_CS_TRIS)) // Invalidate the EEPROM contents if BUTTON0 is held down for more than 4 seconds SYS_TICK StartTime = SYS_TICK_Get(); LED_PUT(0x00); while(BUTTON0_IO == 0u) { if(SYS_TICK_Get() - StartTime > 4*SYS_TICK_TicksPerSecondGet()) { TCPIP_STORAGE_HANDLE hStorage; // just in case we execute this before the stack is initialized TCPIP_STORAGE_Init(0); hStorage = TCPIP_STORAGE_Open(0, false); // no refresh actually needed if(hStorage) { TCPIP_STORAGE_Erase(hStorage); SYS_CONSOLE_MESSAGE("\r\n\r\nBUTTON0 held for more than 4 seconds. Default settings restored.\r\n\r\n"); TCPIP_STORAGE_Close(hStorage); } else { SYS_ERROR(SYS_ERROR_WARN, "\r\n\r\nCould not restore the default settings!!!.\r\n\r\n"); } TCPIP_STORAGE_DeInit(0); LED_PUT(0x0F); // wait 4.5 seconds here then reset while((SYS_TICK_Get() - StartTime) <= (9*SYS_TICK_TicksPerSecondGet()/2)); LED_PUT(0x00); while(BUTTON0_IO == 0u); SYS_Reboot(); break; } } #endif // defined(TCPIP_STACK_USE_STORAGE) && (defined(SPIFLASH_CS_TRIS) || defined(EEPROM_CS_TRIS)) } // Initialize the TCPIP stack if(!TCPIP_STACK_Init(TCPIP_HOSTS_CONFIGURATION, sizeof(TCPIP_HOSTS_CONFIGURATION)/sizeof(*TCPIP_HOSTS_CONFIGURATION), TCPIP_STACK_MODULE_CONFIG_TBL, sizeof(TCPIP_STACK_MODULE_CONFIG_TBL)/sizeof(*TCPIP_STACK_MODULE_CONFIG_TBL) )) { return 0; } #if defined(TCPIP_STACK_USE_TELNET_SERVER) TelnetRegisterCallback(ProcessIO); #endif // defined(TCPIP_STACK_USE_TELNET_SERVER) #if defined (TCPIP_STACK_USE_IPV6) TCPIP_ICMPV6_RegisterCallback (ICMPv6Callback); #endif #if defined(TCPIP_STACK_USE_ICMP_CLIENT) || defined(TCPIP_STACK_USE_ICMP_SERVER) ICMPRegisterCallback (PingProcessIPv4); #endif #if defined(TCPIP_STACK_USE_EVENT_NOTIFICATION) TCPIP_NET_HANDLE hWiFi = TCPIP_STACK_NetHandle("MRF24W"); if(hWiFi) { TCPIP_STACK_SetNotifyEvents(hWiFi, TCPIP_EV_RX_ALL|TCPIP_EV_TX_ALL|TCPIP_EV_RXTX_ERRORS); TCPIP_STACK_SetNotifyHandler(hWiFi, StackNotification, 0); } #endif // defined(TCPIP_STACK_USE_EVENT_NOTIFICATION) #if defined(APP_USE_IPERF) IperfConsoleInit(); iperfOk = IperfAppInit(TCPIP_HOSTS_CONFIGURATION[0].interface); #endif // 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) { // Blink LED0 (right most one) every second. if(SYS_TICK_Get() - startTick >= SYS_TICK_TicksPerSecondGet()/2ul) { startTick = SYS_TICK_Get(); LED0_IO ^= 1; } // This task performs normal stack task including checking // for incoming packet, type of packet and calling // appropriate stack entity to process it. TCPIP_STACK_Task(); // 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(TCPIP_STACK_USE_GENERIC_TCP_CLIENT_EXAMPLE) GenericTCPClient(); #endif #if defined(TCPIP_STACK_USE_GENERIC_TCP_SERVER_EXAMPLE) GenericTCPServer(); #endif #if defined(TCPIP_STACK_USE_SMTP_CLIENT) SMTPDemo(); #endif #if defined(TCPIP_STACK_USE_ICMP_CLIENT) || defined (TCPIP_STACK_USE_ICMP_SERVER) || defined (TCPIP_STACK_USE_IPV6) // use ping on the default interface PingDemoTask(); #endif #if defined(TCPIP_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(TCPIP_STACK_USE_BERKELEY_API) BerkeleyTCPClientDemo(); BerkeleyTCPServerDemo(); BerkeleyUDPClientDemo(0); #endif #if defined(APP_USE_IPERF) IperfConsoleProcess(); if (iperfOk) IperfAppCall(); // Only running in case of init succeed IperfConsoleProcessEpilogue(); #endif // If the local IP address has changed (ex: due to DHCP lease change) // write the new IP address to the console display, UART, and Announce // service // We use the default interface for (i = 0; i < sizeof(TCPIP_HOSTS_CONFIGURATION)/sizeof(*TCPIP_HOSTS_CONFIGURATION); i++) { TCPIP_NET_HANDLE netH = TCPIP_STACK_NetHandle(TCPIP_HOSTS_CONFIGURATION[i].interface); if((uint32_t)dwLastIP[i].Val != TCPIP_STACK_NetAddress(netH)) { dwLastIP[i].Val = TCPIP_STACK_NetAddress(netH); SYS_CONSOLE_MESSAGE(TCPIP_HOSTS_CONFIGURATION[i].interface); SYS_CONSOLE_MESSAGE(" new IP Address: "); DisplayIPValue(dwLastIP[i]); SYS_CONSOLE_MESSAGE("\r\n"); } } #if defined(TCPIP_STACK_USE_EVENT_NOTIFICATION) if(stackNotifyCnt) { stackNotifyCnt = 0; ProcessNotification(stackNotifyHandle); } #endif // defined(TCPIP_STACK_USE_EVENT_NOTIFICATION) } }
/**************************************************************************** Function: void BSP_Initialize(void) Description: This routine initializes the hardware. Precondition: None Parameters: None - None Returns: None Remarks: None ***************************************************************************/ void BSP_Initialize(void) { // LEDs LED0_TRIS = 0; LED1_TRIS = 0; LED2_TRIS = 0; LED3_TRIS = 0; LED4_TRIS = 0; LED5_TRIS = 0; LED6_TRIS = 0; LED_PUT(0x00); DDPCONbits.JTAGEN = 0; CNPUESET = 0x00098000; // Turn on weak pull ups on CN15, CN16, CN19 (RD5, RD7, RD13), which is connected to buttons on PIC32 Starter Kit boards 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 // 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 #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(SPIRAM_CS_TRIS) SPIRAMInit(); #endif #if defined(EEPROM_CS_TRIS) XEEInit(); #endif #if defined(SPIFLASH_CS_TRIS) SPIFlashInit(); #endif }
/**************************************************************************** Function: void BSP_Initialize(void) Description: This routine initializes the hardware. Precondition: None Parameters: None - None Returns: None Remarks: None ***************************************************************************/ void BSP_Initialize(void) { // LEDs LED0_TRIS = 0; LED1_TRIS = 0; LED2_TRIS = 0; LED3_TRIS = 0; LED4_TRIS = 0; LED5_TRIS = 0; LED6_TRIS = 0; LED_PUT(0x00); #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 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 AD1PCFGHbits.PCFG23 = 1; // Make RA7 (BUTTON1) a digital input AD1PCFGHbits.PCFG20 = 1; // Make RA12 (INT1) a digital input for MRF24W 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) TRISFbits.TRISF6 = 0; TRISFbits.TRISF7 = 0; TRISFbits.TRISF8 = 0; #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__) #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) AD1PCFGbits.PCFG5 = 0; // Disable digital input on AN5 (potentiometer) #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 AD1CSSL = 1 << 5; // Scan pot // Deassert all chip select lines so there isn't any problem with // initialization order. Ex: When ENC28J60 is on SPI2 with Explorer 16, // MAX3232 ROUT2 pin will drive RF12/U2CTS ENC28J60 CS line asserted, // preventing proper 25LC256 EEPROM operation. #if defined(ENC_CS_TRIS) ENC_CS_IO = 1; ENC_CS_TRIS = 0; #endif #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(TCPIP_IF_MRF24W) // Removed CS operation here for better code organization // Tested fine with removing these two lines #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__) __builtin_write_OSCCONL(OSCCON & 0xBF); // Unlock PPS // Inputs RPINR19bits.U2RXR = 11; // U2RX = RP11 RPINR20bits.SDI1R = 0; // SDI1 = RP0 RPINR0bits.INT1R = 34; // Assign RE9/RPI34 to INT1 (input) for MRF24W Wi-Fi PICtail Plus interrupt // Outputs RPOR8bits.RP16R = 5; // RP16 = U2TX RPOR1bits.RP2R = 8; // RP2 = SCK1 RPOR0bits.RP1R = 7; // RP1 = SDO1 __builtin_write_OSCCONL(OSCCON | 0x40); // Lock PPS #endif #if defined(__PIC24FJ256GB110__) || defined(__PIC24FJ256GB210__) __builtin_write_OSCCONL(OSCCON & 0xBF); // Unlock PPS // Configure SPI1 PPS pins (ENC28J60/ENCX24J600/MRF24W or other PICtail Plus cards) RPOR0bits.RP0R = 8; // Assign RP0 to SCK1 (output) RPOR7bits.RP15R = 7; // Assign RP15 to SDO1 (output) RPINR20bits.SDI1R = 23; // Assign RP23 to SDI1 (input) // Configure SPI2 PPS pins (25LC256 EEPROM on Explorer 16) RPOR10bits.RP21R = 11; // Assign RG6/RP21 to SCK2 (output) RPOR9bits.RP19R = 10; // Assign RG8/RP19 to SDO2 (output) RPINR22bits.SDI2R = 26; // Assign RG7/RP26 to SDI2 (input) // Configure UART2 PPS pins (MAX3232 on Explorer 16) #if !defined(ENC100_INTERFACE_MODE) || (ENC100_INTERFACE_MODE == 0) || defined(ENC100_PSP_USE_INDIRECT_RAM_ADDRESSING) RPINR19bits.U2RXR = 10; // Assign RF4/RP10 to U2RX (input) RPOR8bits.RP17R = 5; // Assign RF5/RP17 to U2TX (output) #endif // Configure INT1 PPS pin (MRF24W Wi-Fi PICtail Plus interrupt signal when in SPI slot 1) RPINR0bits.INT1R = 33; // Assign RE8/RPI33 to INT1 (input) // Configure INT3 PPS pin (MRF24W Wi-Fi PICtail Plus interrupt signal when in SPI slot 2) RPINR1bits.INT3R = 40; // Assign RC3/RPI40 to INT3 (input) __builtin_write_OSCCONL(OSCCON | 0x40); // Lock PPS #endif #if defined(__PIC24FJ256GA110__) __builtin_write_OSCCONL(OSCCON & 0xBF); // Unlock PPS // Configure SPI2 PPS pins (25LC256 EEPROM on Explorer 16 and ENC28J60/ENCX24J600/MRF24W or other PICtail Plus cards) // Note that the ENC28J60/ENCX24J600/MRF24W PICtails SPI PICtails must be inserted into the middle SPI2 socket, not the topmost SPI1 slot as normal. This is because PIC24FJ256GA110 A3 silicon has an input-only RPI PPS pin in the ordinary SCK1 location. Silicon rev A5 has this fixed, but for simplicity all demos will assume we are using SPI2. RPOR10bits.RP21R = 11; // Assign RG6/RP21 to SCK2 (output) RPOR9bits.RP19R = 10; // Assign RG8/RP19 to SDO2 (output) RPINR22bits.SDI2R = 26; // Assign RG7/RP26 to SDI2 (input) // Configure UART2 PPS pins (MAX3232 on Explorer 16) RPINR19bits.U2RXR = 10; // Assign RF4/RP10 to U2RX (input) RPOR8bits.RP17R = 5; // Assign RF5/RP17 to U2TX (output) // Configure INT3 PPS pin (MRF24W PICtail Plus interrupt signal) RPINR1bits.INT3R = 36; // Assign RA14/RPI36 to INT3 (input) __builtin_write_OSCCONL(OSCCON | 0x40); // Lock PPS #endif #if defined(SPIRAM_CS_TRIS) SPIRAMInit(); #endif #if defined(EEPROM_CS_TRIS) XEEInit(); #endif #if defined(SPIFLASH_CS_TRIS) SPIFlashInit(); #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 = 0x00098000; // Turn on weak pull ups on CN15, CN16, CN19 (RD5, RD7, RD13), which 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 #if defined (WIFI_BOARD_FOC_HUB) AD1PCFGL = 0xFFFF; // All pins digital #else 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) #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 #if defined (WIFI_BOARD_FOC_HUB) // Don't need to do the stuff below #else 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 #endif // UART #if defined(STACK_USE_UART) 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 (WIFI_BOARD_FOC_HUB) // Inputs (WIFI/EE) on SPI1 _SDI1R = 8; // SDI1 = RP8 _INT1R = 7; // Assign RB7/RP7 to INT1 (input) for MRF24WB0M Wi-Fi PICtail Plus interrupt // Inputs (MOTHER) on SPI2 _SDI2R = 12; // SDI2 = RP12 _SCK2R = 11; // SCK2 = RP11 - clock is input because this is the slave _SS2R = 13; // SS2 = RP13 - set slave select pin. // Outputs (WIFI/EEP) on SPI1 _RP6R = 8; // RP6 = SCK1 _RP5R = 7; // RP5 = SDO1 // Outputs (MOTHER) on SPI2 _RP10R = 10; // RP10 = SDO2 #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__) __builtin_write_OSCCONL(OSCCON & 0xBF); // Unlock PPS // Inputs RPINR19bits.U2RXR = 11; // U2RX = RP11 RPINR20bits.SDI1R = 0; // SDI1 = RP0 RPINR0bits.INT1R = 34; // Assign RE9/RPI34 to INT1 (input) for MRF24WB0M Wi-Fi PICtail Plus interrupt // Outputs RPOR8bits.RP16R = 5; // RP16 = U2TX RPOR1bits.RP2R = 8; // RP2 = SCK1 RPOR0bits.RP1R = 7; // RP1 = SDO1 __builtin_write_OSCCONL(OSCCON | 0x40); // Lock PPS #endif #if defined(__PIC24FJ256GB110__) || defined(__PIC24FJ256GB210__) __builtin_write_OSCCONL(OSCCON & 0xBF); // Unlock PPS // Configure SPI1 PPS pins (ENC28J60/ENCX24J600/MRF24WB0M or other PICtail Plus cards) RPOR0bits.RP0R = 8; // Assign RP0 to SCK1 (output) RPOR7bits.RP15R = 7; // Assign RP15 to SDO1 (output) RPINR20bits.SDI1R = 23; // Assign RP23 to SDI1 (input) // Configure SPI2 PPS pins (25LC256 EEPROM on Explorer 16) RPOR10bits.RP21R = 11; // Assign RG6/RP21 to SCK2 (output) RPOR9bits.RP19R = 10; // Assign RG8/RP19 to SDO2 (output) RPINR22bits.SDI2R = 26; // Assign RG7/RP26 to SDI2 (input) // Configure UART2 PPS pins (MAX3232 on Explorer 16) #if !defined(ENC100_INTERFACE_MODE) || (ENC100_INTERFACE_MODE == 0) || defined(ENC100_PSP_USE_INDIRECT_RAM_ADDRESSING) RPINR19bits.U2RXR = 10; // Assign RF4/RP10 to U2RX (input) RPOR8bits.RP17R = 5; // Assign RF5/RP17 to U2TX (output) #endif // Configure INT1 PPS pin (MRF24WB0M Wi-Fi PICtail Plus interrupt signal when in SPI slot 1) RPINR0bits.INT1R = 33; // Assign RE8/RPI33 to INT1 (input) // Configure INT3 PPS pin (MRF24WB0M Wi-Fi PICtail Plus interrupt signal when in SPI slot 2) RPINR1bits.INT3R = 40; // Assign RC3/RPI40 to INT3 (input) __builtin_write_OSCCONL(OSCCON | 0x40); // Lock PPS #endif #if defined(__PIC24FJ256GA110__) __builtin_write_OSCCONL(OSCCON & 0xBF); // Unlock PPS // Configure SPI2 PPS pins (25LC256 EEPROM on Explorer 16 and ENC28J60/ENCX24J600/MRF24WB0M or other PICtail Plus cards) // Note that the ENC28J60/ENCX24J600/MRF24WB0M PICtails SPI PICtails must be inserted into the middle SPI2 socket, not the topmost SPI1 slot as normal. This is because PIC24FJ256GA110 A3 silicon has an input-only RPI PPS pin in the ordinary SCK1 location. Silicon rev A5 has this fixed, but for simplicity all demos will assume we are using SPI2. RPOR10bits.RP21R = 11; // Assign RG6/RP21 to SCK2 (output) RPOR9bits.RP19R = 10; // Assign RG8/RP19 to SDO2 (output) RPINR22bits.SDI2R = 26; // Assign RG7/RP26 to SDI2 (input) // Configure UART2 PPS pins (MAX3232 on Explorer 16) RPINR19bits.U2RXR = 10; // Assign RF4/RP10 to U2RX (input) RPOR8bits.RP17R = 5; // Assign RF5/RP17 to U2TX (output) // Configure INT3 PPS pin (MRF24WB0M PICtail Plus interrupt signal) RPINR1bits.INT3R = 36; // Assign RA14/RPI36 to INT3 (input) __builtin_write_OSCCONL(OSCCON | 0x40); // Lock PPS #endif #if defined(DSPICDEM11) // Deselect the LCD controller (PIC18F252 onboard) to ensure there is no SPI2 contention LCDCTRL_CS_TRIS = 0; LCDCTRL_CS_IO = 1; // Hold the codec in reset to ensure there is no SPI2 contention CODEC_RST_TRIS = 0; CODEC_RST_IO = 0; #endif #if defined(SPIRAM_CS_TRIS) SPIRAMInit(); #endif #if defined(EEPROM_CS_TRIS) XEEInit(); #endif #if defined(SPIFLASH_CS_TRIS) SPIFlashInit(); #endif }
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, "WebVend Demo App" " "); 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 } // Initialize core stack layers (MAC, ARP, TCP, UDP) and // application modules (HTTP, SNMP, etc.) StackInit(); #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 // 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) { // Blink LED0 (right most one) every second. if(TickGet() - t >= TICK_SECOND/2ul) { t = TickGet(); LED0_IO ^= 1; } // 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(); #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. ProcessIO(); // 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 // If not vending, show the new IP if(smVend == SM_IDLE || smVend == SM_DISPLAY_WAIT) { memcpypgm2ram(LCDText, "WebVend Demo App", 16); DisplayIPValue(AppConfig.MyIPAddr); // Print to UART #if defined(STACK_USE_UART) putrsUART((ROM char*)"\r\n"); #endif displayTimeout = TickGet() + 2*TICK_SECOND; smVend = SM_DISPLAY_WAIT; } #if defined(STACK_USE_ANNOUNCE) AnnounceIP(); #endif #if defined(STACK_USE_ZEROCONF_MDNS_SD) mDNSFillHostRecord(); #endif } } }
int main(void) #endif { static DWORD t = 0; static DWORD dwLastIP = 0; #if defined (EZ_CONFIG_STORE) static DWORD ButtonPushStart = 0; #endif #if (MY_DEFAULT_NETWORK_TYPE == WF_SOFT_AP) UINT8 channelList[] = MY_DEFAULT_CHANNEL_LIST_PRESCAN; // WF_PRESCAN tWFScanResult bssDesc; #endif // 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(); #if defined ( EZ_CONFIG_SCAN ) WFInitScan(); #endif #if (MY_DEFAULT_NETWORK_TYPE == WF_SOFT_AP) // WF_PRESCAN: Pre-scan before starting up as SoftAP mode WF_CASetScanType(MY_DEFAULT_SCAN_TYPE); WF_CASetChannelList(channelList, sizeof(channelList)); if (WFStartScan() == WF_SUCCESS) { SCAN_SET_DISPLAY(SCANCXT.scanState); SCANCXT.displayIdx = 0; //putsUART("main: Prescan WFStartScan() success ................. \r\n"); } // Needed to trigger g_scan_done WFRetrieveScanResult(0, &bssDesc); #else #if defined(WF_CS_TRIS) WF_Connect(); #endif // defined(WF_CS_TRIS) #endif // (MY_DEFAULT_NETWORK_TYPE == WF_SOFT_AP) // 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); #if defined(STACK_USE_TCP_MOBILE_APP_SERVER) mDNSServiceRegister( (const char *) "HomeControlServer", // base name of the service "_home-control._tcp.local", // type of the service 27561, // TCP or UDP port, at which this service is available ((const BYTE *)"control home devices"), // TXT info 1, // auto rename the service when if needed NULL, // no callback function NULL // no application context ); #else /* !defined(STACK_USE_TCP_MOBILE_APP_SERVER) */ 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 ); #endif /* defined(STACK_USE_TCP_MOBILE_APP_SERVER) */ mDNSMulticastFilterRegister(); #endif #if defined(WF_CONSOLE) WFConsoleInit(); #endif // 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) { #if (MY_DEFAULT_NETWORK_TYPE == WF_SOFT_AP) if (g_scan_done) { if (g_prescan_waiting) { putrsUART((ROM char*)"\n SoftAP prescan results ........ \r\n\n"); SCANCXT.displayIdx = 0; while (IS_SCAN_STATE_DISPLAY(SCANCXT.scanState)) { WFDisplayScanMgr(); } putrsUART((ROM char*)"\r\n "); #if defined(WF_CS_TRIS) WF_Connect(); #endif g_scan_done = 0; g_prescan_waiting = 0; } } #endif // (MY_DEFAULT_NETWORK_TYPE == WF_SOFT_AP) #if defined(WF_PRE_SCAN_IN_ADHOC) if(g_prescan_adhoc_done) { WFGetScanResults(); g_prescan_adhoc_done = 0; } #endif #if defined (EZ_CONFIG_STORE) // Hold button3 for 4 seconds to reset to defaults. if (BUTTON3_IO == 0u) { // Button is pressed if (ButtonPushStart == 0) //Just pressed ButtonPushStart = TickGet(); else if(TickGet() - ButtonPushStart > 4*TICK_SECOND) RestoreWifiConfig(); } else { ButtonPushStart = 0; //Button release reset the clock } if (AppConfig.saveSecurityInfo) { // set true by WF_ProcessEvent after connecting to a new network // get the security info, and if required, push the PSK to EEPROM if ((AppConfig.SecurityMode == WF_SECURITY_WPA_WITH_PASS_PHRASE) || (AppConfig.SecurityMode == WF_SECURITY_WPA2_WITH_PASS_PHRASE) || (AppConfig.SecurityMode == WF_SECURITY_WPA_AUTO_WITH_PASS_PHRASE)) { // only need to save when doing passphrase tWFCPElements profile; UINT8 connState; UINT8 connID; WF_CMGetConnectionState(&connState, &connID); WF_CPGetElements(connID, &profile); memcpy((char*)AppConfig.SecurityKey, (char*)profile.securityKey, 32); AppConfig.SecurityMode--; // the calc psk is exactly one below for each passphrase option AppConfig.SecurityKeyLength = 32; SaveAppConfig(&AppConfig); } AppConfig.saveSecurityInfo = FALSE; } #endif // EZ_CONFIG_STORE #if defined (STACK_USE_EZ_CONFIG) // Blink LED0 twice per sec when unconfigured, once per sec after config if((TickGet() - t >= TICK_SECOND/(4ul - (CFGCXT.isWifiDoneConfigure*2ul)))) #else // Blink LED0 (right most one) every second. if(TickGet() - t >= TICK_SECOND/2ul) #endif // STACK_USE_EZ_CONFIG { t = TickGet(); LED0_IO ^= 1; } // 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(); #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(WF_CONSOLE) WFConsoleProcess(); WFConsoleProcessEpilogue(); #endif #if defined(STACK_USE_GENERIC_TCP_CLIENT_EXAMPLE) GenericTCPClient(); #endif #if defined(STACK_USE_GENERIC_TCP_SERVER_EXAMPLE) GenericTCPServer(); #endif #if defined(STACK_USE_TCP_MOBILE_APP_SERVER) MobileTCPServer(); #endif #if defined(STACK_USE_SMTP_CLIENT) SMTPDemo(); #endif #if defined(STACK_USE_ICMP_CLIENT) PingDemo(); PingConsole(); #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 // 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 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 } } }
/**************************************************************************** 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(__PIC32MX__) { // Enable multi-vectored interrupts INTEnableSystemMultiVectoredInt(); // Enable optimal performance SYSTEMConfigPerformance(GetSystemClock()); mOSCSetPBDIV(OSC_PB_DIV_1); // Use 1:1 CPU Core:Peripheral clocks // Disable JTAG port so we get our I/O pins back, but first // wait 50ms so if you want to reprogram the part with // JTAG, you'll still have a tiny window before JTAG goes away. // The PIC32 Starter Kit debuggers use JTAG and therefore must not // disable JTAG. DelayMs(50); #if !defined(__MPLAB_DEBUGGER_PIC32MXSK) && !defined(__MPLAB_DEBUGGER_FS2) DDPCONbits.JTAGEN = 0; #endif LED_PUT(0x00); // Turn the LEDs off CNPUESET = 0x00098000; // Turn on weak pull ups on CN15, CN16, CN19 (RD5, RD7, RD13), which is connected to buttons on PIC32 Starter Kit boards } #endif AD1CHS = 0; // Input to AN0 (potentiometer) AD1PCFGbits.PCFG4 = 0; // Disable digital input on AN4 (TC1047A temp sensor) #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 // 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 // Deassert all chip select lines so there isn't any problem with // initialization order. #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(SPIRAM_CS_TRIS) SPIRAMInit(); #endif #if defined(EEPROM_CS_TRIS) XEEInit(); #endif #if defined(SPIFLASH_CS_TRIS) SPIFlashInit(); #endif }
int main(void) #endif { static DWORD t = 0; static DWORD dwLastIP = 0; #if defined(WF_USE_POWER_SAVE_FUNCTIONS) BOOL PsPollEnabled; BOOL psConfDone = FALSE; #endif // 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(); #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 // 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) { //while (1) /*{ if(BUTTON0_IO == 0u && LED0_IO == 0) { LED0_IO =1; } if(BUTTON0_IO == 0u && LED0_IO ==1) { LED0_IO =0; } }*/ #if defined(WF_USE_POWER_SAVE_FUNCTIONS) if (!psConfDone && WFisConnected()) { PsPollEnabled = (MY_DEFAULT_PS_POLL == WF_ENABLED); if (!PsPollEnabled) { /* disable low power (PS-Poll) mode */ #if defined(STACK_USE_UART) putrsUART("Disable PS-Poll\r\n"); #endif WF_PsPollDisable(); } else { /* Enable low power (PS-Poll) mode */ #if defined(STACK_USE_UART) putrsUART("Enable PS-Poll\r\n"); #endif WF_PsPollEnable(TRUE); } psConfDone = TRUE; } #endif // Blink LED0 (right most one) every second. // if(TickGet() - t >= TICK_SECOND/2ul) // { // t = TickGet(); // LED0_IO ^= 1; // } // 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(); #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 ProcessIO(); // 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 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 } } }
/**************************************************************************** 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) { // Board Type. TRISEbits.TRISE0 = TRISEbits.TRISE1 = 1; // LEDs LED0_TRIS = 0; LED1_TRIS = 0; LED2_TRIS = 0; LED3_TRIS = 0; LED4_TRIS = 0; LED5_TRIS = 0; LED6_TRIS = 0; #if !defined(EXPLORER_16) // Pin multiplexed with a button on EXPLORER_16 LED7_TRIS = 0; #endif 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(__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; #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(__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 #endif #if defined(SPIRAM_CS_TRIS) SPIRAMInit(); #endif #if defined(EEPROM_CS_TRIS) XEEInit(); #endif #if defined(SPIFLASH_CS_TRIS) SPIFlashInit(); #endif }
int main(void) #endif { unsigned char counter = 0; static DWORD Ping_Start_Time = 0; static unsigned char Ping_Counter = 0; static DWORD t = 0; static DWORD dwLastIP = 0; LED0_TRIS = 0; LED0_IO = 1; Delay10KTCYx(0); // Initialize application specific hardware InitializeBoard(); #ifdef APP_USE_USB InitializeUSB(); #if defined(USB_INTERRUPT) USBDeviceAttach(); #endif #endif #if defined(USE_LCD) // Initialize and display the stack version on the LCD LCDInit(); DelayMs(100); strcpypgm2ram((char*)LCDText, "TCPStack " VERSION " " " "); LCDUpdate(); #endif // Initialize stack-related hardware components that may be // required by the UART configuration routines TickInit(); #if defined(STACK_USE_MPFS) || defined(STACK_USE_MPFS2) MPFSInit(); #endif // Initialize Stack and application related NV variables into AppConfig. InitAppConfig(); // 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); #ifdef TRANSCEIVER_BOARD #elif defined( SINGLEPHASEMETER_MCU1 ) while(BUTTON0_IO == 0u) { if(TickGet() - StartTime > 4*TICK_SECOND) { #if defined(EEPROM_CS_TRIS) XEEBeginWrite(0x0000); XEEWrite(0xFF); XEEEndWrite(); #elif defined(SPIFLASH_CS_TRIS) SPIFlashBeginWrite(0x0000); 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; } } #else #error "No board defined." #endif #endif #if defined(STACK_USE_UART) DoUARTConfig(); #endif } // Initialize core stack layers (MAC, ARP, TCP, UDP) and // application modules (HTTP, SNMP, etc.) StackInit(); // 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 #ifdef SINGLEPHASEMETER_MCU1 MCUOpen(); #endif #ifdef APP_USE_ZIGBEE ZigbeeOpen(); #else //#error no zigbee. #endif #ifdef APP_USE_RGB OpenRGB(); #endif // ROUTER CODES #ifdef APP_USE_ROUTER_CODES { } #endif // END // 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) { #ifdef SINGLEPHASEMETER_MCU1 MCUTasks(); #endif #ifdef APP_USE_RGB RGBTasks(); #endif /**********************************************/ /**** Handle USB ******************************/ /**********************************************/ #if defined(USB_POLLING) // Check bus status and service USB interrupts. USBDeviceTasks(); // Interrupt or polling method. If using polling, must call // this function periodically. This function will take care // of processing and responding to SETUP transactions // (such as during the enumeration process when you first // plug in). USB hosts require that USB devices should accept // and process SETUP packets in a timely fashion. Therefore, // when using polling, this function should be called // frequently (such as once about every 100 microseconds) at any // time that a SETUP packet might reasonably be expected to // be sent by the host to your device. In most cases, the // USBDeviceTasks() function does not take very long to // execute (~50 instruction cycles) before it returns. #endif // Application-specific tasks. // Application related code may be added here, or in the ProcessIO() function. ProcessUSBIO(); /**********************************************/ /**** Handle Zigbee ******************************/ /**********************************************/ #ifdef APP_USE_ZIGBEE ZigbeeTasks(); { if( counter++ > 200 ) { char s[16] = {0x10, 0x01, 0, 0, 0, 0, 0, 0, 0xff, 0xfe, 0xff, 0xfe, 0, 0, 'A', '4'}; // , 0x64}; ZigbeeAPISendString(16, s); counter = 0; } } #endif // Main program loop. // Set up ping and node statuses. A ping is sent every 4 mins and a check is done every minute. // Nodes that have not pinged within 5 min frame will be delisted as in the network. if( Ping_Start_Time != 0 && (TickGet() - Ping_Start_Time) > (TICK_MINUTE) ) { // Check nodes that have not sent their ping within the past 5 minutes. {} // Send out a ping if 4 minutes have lapsed. if( Ping_Counter++ >= 4 ) {} } Ping_Start_Time = TickGet(); // Blink LED0 (right most one) every second. if(TickGet() - t >= TICK_SECOND/2ul) { t = TickGet(); LED0_IO ^= 1; } // 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(); // 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) SNMPTrapDemo(); if(gSendTrapFlag) SNMPSendTrap(); #endif #if defined(STACK_USE_BERKELEY_API) BerkeleyTCPClientDemo(); BerkeleyTCPServerDemo(); BerkeleyUDPClientDemo(); #endif #ifdef APP_USE_RGB RGBTasks(); #endif //ProcessIO(); // 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 DisplayIPValue(AppConfig.MyIPAddr); #if defined(STACK_USE_UART) putrsUART((ROM char*)"\r\n"); #endif #if defined(STACK_USE_ANNOUNCE) AnnounceIP(); #endif } } }