void InitializeComms() { msg_init(); InitializeUSB(&comms_sleep, &comms_wake); // Communications, only needs to be run when USB is connected comms_task = RegisterTask("Comms", ProcessComms); comms_task->state = tsRun; usb_connected = false; comms_status = cmDisconnected; }
void main (void) { int cnt = 0; InitDevice (); Delay10KTCYx (250); InitializeUSB (); InitLCD (); /* UCONbits.USBEN = 0; */ /* UCFGbits.UTRDIS = 0; */ /* UCFGbits.UPUEN = 1; */ /* UCFGbits.FSEN = 1; */ USBDeviceAttach (); while (1) { WriteLCDChar (UCONbits.USBEN ? '0' : '1'); WriteLCDChar (UCFGbits.FSEN ? '0' : '1'); WriteLCDChar (UCFGbits.UTRDIS ? '0' : '1'); WriteLCDChar (UCFGbits.UPUEN ? '0' : '1'); WriteLCDChar ('0' + UCFGbits.PPB); WriteLCDChar ('-'); WriteLCDChar ('0' + USTATbits.ENDP); WriteLCDChar ('0' + USTATbits.DIR); WriteLCDChar ('0' + USTATbits.PPBI); WriteLCDChar ('-'); putNumber (USBDeviceState); WriteLCDChar ('-'); putNumber (cnt++); // if (USBDeviceState == DETACHED_STATE) // USBDeviceAttach (); Delay10KTCYx (1000); LCDHome (); Delay10KTCYx (10); } // if (USBDeviceState < CONFIGURED_STATE) // continue; // bitset (PORTB, E_PORT); // Delay10TCYx (10); // ClearLCD (); // bitclr (PORTB, E_PORT); // Delay10TCYx (10); // } }
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 } } }