/** * This function must be called before any of the * stack or its component routines be used. * * Side Affect: Stack and its componentns are initialized */ void StackInit(void) { smStack = SM_STACK_IDLE; #if defined(STACK_USE_IP_GLEANING) || defined(STACK_USE_DHCP) /* * If DHCP or IP Gleaning is enabled, * startup in Config Mode. */ stackFlags.bits.bInConfigMode = TRUE; #endif MACInit(); ARPInit(); #if defined(STACK_USE_UDP) UDPInit(); #endif #if defined(STACK_USE_TCP) TCPInit(); #endif }
/** * This function must be called before any of the * stack or its component routines be used. * * Side Affect: Stack and its componentns are initialized */ void StackInit(void) { smStack = SM_STACK_IDLE; #if defined(STACK_USE_IP_GLEANING) || defined(STACK_USE_DHCP) /* * If DHCP or IP Gleaning is enabled, * startup in Config Mode. */ stackFlags.bits.bInConfigMode = TRUE; #endif MACInit(); ARPInit(); #if defined(STACK_USE_UDP) UDPInit(); #endif #if defined(STACK_USE_TCP) TCPInit(); #endif //MODIFIED DHCP BEGIN #if defined(STACK_USE_DHCP) if(!STACK_IS_DHCP_ENABLED) { DHCPDisable(); } #endif //MODIFIED DHCP END }
void EthernetInit(void) { // Initialize hardware InitBoard(); // Initialize stack-related hardware components that may be // required by the UART configuration routines TickInit(); // Initialize Stack and application related NV variables into AppConfig. InitAppConfig(); // Seed the LFSRRand() function. LFSRSeedRand(GenerateRandomDWORD()); // Initialize the MAC library. MACInit(); // Initialize the ARP library. ARPInit(); // Initialize UDP. UDPInit(); // Open up a socket for our UDP server. localServerSocket = UDPOpenEx(NULL, UDP_OPEN_SERVER, UDP_SERVER_PORT, UDP_SERVER_PORT); if (localServerSocket == INVALID_UDP_SOCKET) { FATAL_ERROR(); } // Open up a socket for our UDP client. remoteServerSocket = UDPOpenEx(NULL, UDP_OPEN_IP_ADDRESS, UDP_CLIENT_PORT, UDP_CLIENT_PORT); if (remoteServerSocket == INVALID_UDP_SOCKET) { FATAL_ERROR(); } }
/********************************************************************* * Function: void StackInit(void) * * PreCondition: None * * Input: None * * Output: Stack and its componets are initialized * * Side Effects: None * * Note: This function must be called before any of the * stack or its component routines are used. * ********************************************************************/ void StackInit(void) { smStack = SM_STACK_IDLE; #if defined(STACK_USE_IP_GLEANING) || defined(STACK_USE_DHCP_CLIENT) /* * If DHCP or IP Gleaning is enabled, * startup in Config Mode. */ AppConfig.Flags.bInConfigMode = TRUE; #endif // Seed the LFSRRand() function LFSRSeedRand(GenerateRandomDWORD()); MACInit(); #if defined(WF_CS_TRIS) && defined(STACK_USE_EZ_CONFIG) && !defined(__18CXX) WFEasyConfigInit(); #endif ARPInit(); #if defined(STACK_USE_UDP) UDPInit(); #endif #if defined(STACK_USE_TCP) TCPInit(); #endif #if defined(STACK_USE_BERKELEY_API) BerkeleySocketInit(); #endif #if defined(STACK_USE_HTTP2_SERVER) HTTPInit(); #endif #if defined(STACK_USE_RSA) RSAInit(); #endif #if defined(STACK_USE_SSL) SSLInit(); #endif #if defined(STACK_USE_FTP_SERVER) && defined(STACK_USE_MPFS2) FTPInit(); #endif #if defined(STACK_USE_SNMP_SERVER) SNMPInit(); #endif #if defined(STACK_USE_DHCP_CLIENT) DHCPInit(0); if(!AppConfig.Flags.bIsDHCPEnabled) { DHCPDisable(0); } #endif #if defined(STACK_USE_AUTO_IP) AutoIPInit(0); #endif #if defined(STACK_USE_DYNAMICDNS_CLIENT) DDNSInit(); #endif #if defined(STACK_USE_RANDOM) RandomInit(); #endif }
int1 MACGetHeader(MAC_ADDR *remote, int8* type) { NE_PREAMBLE header; int8 NICWritePtr; WORD_VAL temp; *type = MAC_UNKNOWN; // Reset NIC if overrun has occured. if ( NICGet(ISR) & 0x10 ) { #if 1 NICPut(CMDR, 0x21); Delay(0xff); NICPut(RBCR0, 0); NICPut(RBCR1, 0); NICPut(TCR, 0x02); NICPut(CMDR, 0x20); MACDiscardRx(); NICPut(ISR, 0xff); NICPut(TCR, 0x00); return FALSE; #else MACInit(); return FALSE; #endif } NICPut(CMDR, 0x60); NICWritePtr = NICGet(CURRP); NICPut(CMDR, 0x20); if ( NICWritePtr != NICReadPtr ) { temp.v[1] = NICReadPtr; temp.v[0] = 0; NICSetAddr(temp.Val); //MACGetArray((int8*)&header, sizeof(header)); debug("\r\n***************************************\r\n"); MACGetArray(&header, sizeof(NE_PREAMBLE)); debug(debug_putc,"\r\n\r\nGOT HDR = ST:%X NPP:%X LEN:%LU", (int8)header.Status,header.NextPacketPointer,header.ReceivedBytes); debug(debug_putc,"\r\n DEST: %X.%X.%X.%X.%X.%X SRC: %X.%X.%X.%X.%X.%X TYPE:%LX", header.DestMACAddr.v[0],header.DestMACAddr.v[1],header.DestMACAddr.v[2], header.DestMACAddr.v[3],header.DestMACAddr.v[4],header.DestMACAddr.v[5], header.SourceMACAddr.v[0],header.SourceMACAddr.v[1],header.SourceMACAddr.v[2], header.SourceMACAddr.v[3],header.SourceMACAddr.v[4],header.SourceMACAddr.v[5], header.Type.Val); // Validate packet length and status. if ( header.Status.PRX && (header.ReceivedBytes >= MINFRAMEC) && (header.ReceivedBytes <= MAXFRAMEC) ) { debug(debug_putc," VALID"); header.Type.Val = swaps(header.Type.Val); //memcpy((char*)remote->v, (char*)header.SourceMACAddr.v, sizeof(*remote)); memcpy(&remote->v[0], &header.SourceMACAddr.v[0], sizeof(MAC_ADDR)); if ( (header.Type.v[1] == 0x08) && ((header.Type.v[0] == ETHER_IP) || (header.Type.v[0] == ETHER_ARP)) ) *type = header.Type.v[0]; } NICCurrentRdPtr = NICReadPtr; NICReadPtr = header.NextPacketPointer; return TRUE; } return FALSE; }
/********************************************************************* * Function: void StackInit(void) * * PreCondition: None * * Input: None * * Output: Stack and its componets are initialized * * Side Effects: None * * Note: This function must be called before any of the * stack or its component routines are used. * ********************************************************************/ void StackInit(void) { smStack = SM_STACK_IDLE; #if defined(STACK_USE_IP_GLEANING) || defined(STACK_USE_DHCP_CLIENT) /* * If DHCP or IP Gleaning is enabled, * startup in Config Mode. */ AppConfig.Flags.bInConfigMode = TRUE; #endif // Seed the rand() function srand(GenerateRandomDWORD()); MACInit(); #if defined( ZG_CS_TRIS ) #if defined(ZG_CONFIG_LINKMGRII) ZGLibInitialize(); ZGLinkMgrInit(); #endif #endif ARPInit(); #if defined(STACK_USE_UDP) UDPInit(); #endif #if defined(STACK_USE_TCP) TCPInit(); #endif #if defined(STACK_USE_BERKELEY_API) BerkeleySocketInit(); #endif #if defined(STACK_USE_HTTP_SERVER) || defined(STACK_USE_HTTP2_SERVER) HTTPInit(); #endif #if defined(STACK_USE_RSA) RSAInit(); #endif #if defined(STACK_USE_SSL) SSLInit(); #endif #if defined(STACK_USE_FTP_SERVER) && defined(STACK_USE_MPFS) FTPInit(); #endif #if defined(STACK_USE_SNMP_SERVER) SNMPInit(); #endif #if defined(STACK_USE_DHCP_CLIENT) DHCPInit(0); if(!AppConfig.Flags.bIsDHCPEnabled) { DHCPDisable(0); } #endif #if defined(STACK_USE_DYNAMICDNS_CLIENT) DDNSInit(); #endif }
/********************************************************************* * Function: void StackInit(void) * * PreCondition: None * * Input: None * * Output: Stack and its componets are initialized * * Side Effects: None * * Note: This function must be called before any of the * stack or its component routines are used. * ********************************************************************/ void StackInit(void) { static BOOL once = FALSE; smStack = SM_STACK_IDLE; #if defined(STACK_USE_IP_GLEANING) || defined(STACK_USE_DHCP_CLIENT) /* * If DHCP or IP Gleaning is enabled, * startup in Config Mode. */ AppConfig.Flags.bInConfigMode = TRUE; #endif #if defined (WF_CS_TRIS) && defined (STACK_USE_DHCP_CLIENT) g_DhcpRenew = FALSE; g_DhcpRetryTimer = 0; #endif if (!once) { // Seed the LFSRRand() function LFSRSeedRand(GenerateRandomDWORD()); once = TRUE; } MACInit(); #if defined (WF_AGGRESSIVE_PS) && defined (WF_CS_TRIS) WFEnableAggressivePowerSave(); #endif #if defined(WF_CS_TRIS) && defined(STACK_USE_EZ_CONFIG) && !defined(__18CXX) WFEasyConfigInit(); #endif ARPInit(); #if defined(STACK_USE_UDP) UDPInit(); #endif #if defined(STACK_USE_TCP) TCPInit(); #endif #if defined(STACK_USE_BERKELEY_API) BerkeleySocketInit(); #endif #if defined(STACK_USE_HTTP2_SERVER) HTTPInit(); #endif #if defined(STACK_USE_RSA) RSAInit(); #endif #if defined(STACK_USE_SSL) SSLInit(); #endif #if defined(STACK_USE_FTP_SERVER) && defined(STACK_USE_MPFS2) FTPInit(); #endif #if defined(STACK_USE_SNMP_SERVER) SNMPInit(); #endif #if defined(STACK_USE_DHCP_CLIENT) DHCPInit(0); if(!AppConfig.Flags.bIsDHCPEnabled) { DHCPDisable(0); } #endif #if defined(STACK_USE_AUTO_IP) AutoIPInit(0); #endif #if defined(STACK_USE_DYNAMICDNS_CLIENT) DDNSInit(); #endif #if defined(STACK_USE_RANDOM) RandomInit(); #endif #if defined(STACK_USE_CCS_SMTP) SMTPInit(); #endif #if defined(STACK_USE_CCS_SNTP_CLIENT) NTPInit(); #endif #if defined(STACK_USE_CCS_GRATUITOUS_ARP) GratArpInit(); #endif #if defined(STACK_USE_CCS_HTTP1_SERVER) || defined(STACK_USE_CCS_HTTP2_SERVER) HTTPInit(); #endif #if defined(STACK_USE_CCS_TFTP_SERVER) TFTPSInit(); #endif }
bool AutoUpdate_UartXMODEM_24G(void) { enum SM_FIRMWARE_UPDATE { SM_FIRMWARE_UPDATE_SOH, SM_FIRMWARE_UPDATE_BLOCK, SM_FIRMWARE_UPDATE_BLOCK_CMP, SM_FIRMWARE_UPDATE_DATA, SM_FIRMWARE_UPDATE_CHECKSUM, SM_FIRMWARE_UPDATE_FINISH, } state; uint8_t c; // MPFS_HANDLE handle; bool lbDone; uint8_t blockLen=0; bool lResult = false; uint8_t BlockNumber=0, preBlockNum=0; uint8_t checksum=0; uint32_t lastTick; uint32_t currentTick; state = SM_FIRMWARE_UPDATE_SOH; lbDone = false; #if 0 //use button to begin update if( BUTTON3_IO == 1u) return false; putsUART("\n\rPress S2 (on Explorer16) to start the update.\n\r"); while(BUTTON2_IO == 1u); #else //use console command to begin update if(false == wf_update_begin_uart) return false; wf_update_begin_uart = false; #endif MACInit(); DelayMs(100); AutoUpdate_Initialize(); putsUART("Please initiate file transfer of firmware patch by XMODEM.\r\n"); putsUART("If S3 pressed (on Explorer16), update will stop and previous image will be restored.\r\n"); lastTick = TickGet(); do { currentTick = TickGet(); if ( currentTick - lastTick >= (TICK_SECOND*2) ) { lastTick = TickGet(); while(BusyUART()); WriteUART(XMODEM_NAK); } } while(!DataRdyUART()); while(!lbDone) { if (BUTTON3_IO == 0u) // If you want to cancel AutoUpdate, please press S3 { putsUART("You press S3 button, revert begin...\r\n"); AutoUpdate_Restore(); putsUART("revert done\r\n"); return false; } if(DataRdyUART()) { c = ReadUART(); lastTick = TickGet(); } else { // put some timeout to make sure that we do not wait forever. currentTick = TickGet(); if ( currentTick - lastTick >= (TICK_SECOND*10) ) { //if time out, copy old patch image from bank2 to bank1 putsUART("timeout, revert begin...\r\n"); AutoUpdate_Restore(); putsUART("revert done\r\n"); return false; } continue; } //dbgPrintf("(%02x) ",c); switch(state) { case SM_FIRMWARE_UPDATE_SOH: if(c == XMODEM_SOH) { state = SM_FIRMWARE_UPDATE_BLOCK; dbgPrintf("\r\n! "); checksum = c; lResult = true; } else if ( c == XMODEM_EOT ) { state = SM_FIRMWARE_UPDATE_FINISH; while(BusyUART()); WriteUART(XMODEM_ACK); lbDone = true; } else { dbgPrintf("\n!error\n"); while(1); } break; case SM_FIRMWARE_UPDATE_BLOCK: BlockNumber = c; dbgPrintf("BLK=%d ",BlockNumber); checksum += c; state = SM_FIRMWARE_UPDATE_BLOCK_CMP; break; case SM_FIRMWARE_UPDATE_BLOCK_CMP: dbgPrintf("%d ",c); dbgPrintf("@:"); //Judge: Is it correct ? if(c != (BlockNumber ^ 0xFF)) { lResult = false; dbgPrintf("\nBLOCK_CMP err: %x,%x\n", c, BlockNumber ^ 0xFF ); } else { if((uint8_t)(preBlockNum+1) != BlockNumber) { lResult = false; dbgPrintf("\nBLOCK err %x %x\n",preBlockNum+1,BlockNumber); } } checksum += c; blockLen = 0; state = SM_FIRMWARE_UPDATE_DATA; break; case SM_FIRMWARE_UPDATE_DATA: // Buffer block data until it is over. tempData[blockLen++] = c; if ( blockLen == XMODEM_BLOCK_LEN ) { state = SM_FIRMWARE_UPDATE_CHECKSUM; } checksum += c; break; case SM_FIRMWARE_UPDATE_CHECKSUM: dbgPrintf("Checksum=%x=%x ",checksum,c); if(checksum != c) { lResult = false; dbgPrintf("\nchecksum err\n"); } XMODEM_SendToModule(tempData); while(BusyUART()); if(lResult == true) { WriteUART(XMODEM_ACK); preBlockNum++; } else { WriteUART(XMODEM_NAK); } state = SM_FIRMWARE_UPDATE_SOH; break; default: dbgPrintf("\n!error\n"); while(1); break; } } AutoUpdate_Completed(); return true; }
/********************************************************************* * Function: void StackInit(void) * * PreCondition: None * * Input: None * * Output: Stack and its componets are initialized * * Side Effects: None * * Note: This function must be called before any of the * stack or its component routines are used. * ********************************************************************/ void StackInit(void) { static bool once = false; smStack = SM_STACK_IDLE; #if defined(STACK_USE_IP_GLEANING) || defined(STACK_USE_DHCP_CLIENT) /* * If DHCP or IP Gleaning is enabled, * startup in Config Mode. */ AppConfig.Flags.bInConfigMode = true; #endif #if defined (WF_CS_TRIS) && defined (STACK_USE_DHCP_CLIENT) g_DhcpRenew = false; g_DhcpRetryTimer = 0; #endif if (!once) { // Seed the LFSRRand() function LFSRSeedRand(GenerateRandomDWORD()); once = true; } MACInit(); #if defined(WF_CS_TRIS) && defined(STACK_USE_EZ_CONFIG) && !defined(__XC8) WFEasyConfigInit(); #endif ARPInit(); #if defined(STACK_USE_ANNOUNCE) AnnounceInit(); #endif #if defined(STACK_USE_UDP) UDPInit(); #endif #if defined(STACK_USE_TCP) TCPInit(); #endif #if defined(STACK_USE_BERKELEY_API) BerkeleySocketInit(); #endif #if defined(STACK_USE_HTTP2_SERVER) HTTPInit(); #endif #if defined(STACK_USE_RSA) RSAInit(); #endif #if defined(STACK_USE_SSL) SSLInit(); #endif #if defined(STACK_USE_FTP_SERVER) && defined(STACK_USE_MPFS2) FTPInit(); #endif #if defined(STACK_USE_DHCP_CLIENT) DHCPInit(0); if(!AppConfig.Flags.bIsDHCPEnabled) { DHCPDisable(0); } #endif #if defined(STACK_USE_AUTO_IP) AutoIPInit(0); #endif #if defined(STACK_USE_DYNAMICDNS_CLIENT) DDNSInit(); #endif #if defined(STACK_USE_RANDOM) RandomInit(); #endif #if defined(STACK_USE_NBNS) NBNSInit(); #endif }
/* * Main entry point. */ void main(void) { static TICK tickHeartBeat = 0xffffffff; static BYTE testLED; testLED = 1; // Destination address - Always MAC broadcast address broadcastTargetMACAddr.v[ 0 ] = 0xff; broadcastTargetMACAddr.v[ 1 ] = 0xff; broadcastTargetMACAddr.v[ 2 ] = 0xff; broadcastTargetMACAddr.v[ 3 ] = 0xff; broadcastTargetMACAddr.v[ 4 ] = 0xff; broadcastTargetMACAddr.v[ 5 ] = 0xff; //Set SWDTEN bit, this will enable the watch dog timer WDTCON_SWDTEN = 1; aliveCntrMain = 0xff; //Disable alive counter during initialization. Setting to 0xff disables it. //Initialize any application specific hardware. InitializeBoard(); //Initialize all stack related components. Following steps must //be performed for all applications using PICmicro TCP/IP Stack. TickInit(); //Initialize serial ports early, because they could be required for debugging if (appcfgGetc(APPCFG_USART1_CFG & APPCFG_USART_ENABLE)) { appcfgUSART(); //Configure the USART1 } #if defined(BRD_SBC65EC) if (appcfgGetc(APPCFG_USART2_CFG & APPCFG_USART_ENABLE)) { appcfgUSART2(); //Configure the USART2 } #endif //After initializing all modules that use interrupts, enable global interrupts INTCON_GIEH = 1; INTCON_GIEL = 1; //Initialize Stack and application related NV variables. appcfgInit(); //First call appcfgCpuIOValues() and then only appcfgCpuIO()!!! This ensures the value are set, before enabling ports. appcfgCpuIOValues(); //Configure the CPU's I/O port pin default values appcfgCpuIO(); //Configure the CPU's I/O port pin directions - input or output appcfgADC(); //Configure ADC unit appcfgPWM(); //Configure PWM Channels MACInit(); #if (DEBUG_MAIN >= LOG_DEBUG) debugPutMsg(1); //@mxd:1:Starting main loop #endif /* * Once all items are initialized, go into infinite loop and let * stack items execute their tasks. * If application needs to perform its own task, it should be * done at the end of while 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 broken * down into smaller pieces so that other tasks can have CPU time. */ while ( 1 ) { //aliveCntrMain = 38; //Reset if not services in 52.42ms x 38 = 2 seconds aliveCntrMain = 0xff; CLRWDT(); // Check for event if ( vscp_getRawPacket() ) { feedVSCP(); } // Send heartbeat every 30 seconds if ( TickGetDiff( tickHeartBeat ) >= ( TICKS_PER_SECOND * 30 ) ) { //vscpevent.head = VSCP_PRIORITY_NORMAL; //vscpevent.vscp_class = VSCP_CLASS2_LEVEL1_INFORMATION; //vscpevent.vscp_type = VSCP_TYPE_INFORMATION_NODE_HEARTBEAT; //vscpevent.sizeData = 3; //vscpevent.data[ 0 ] = 0; //vscpevent.data[ 1 ] = 0; // Zone //vscpevent.data[ 2 ] = 0; // subzone //vscp_sendRawPacket( &vscpevent ); //SendTestVSCPPacket(); tickHeartBeat = TickGet(); /* //If B6 is configured as input, change to output if (appcfgGetc(APPCFG_TRISB) & 0x40) { appcfgPutc(APPCFG_TRISB, appcfgGetc(APPCFG_TRISB) & 0b10111111); } TRISB_RB6 = 0; LATB6 ^= 1; //Toggle //Toggle IOR5E LED, if IOR5E is present if (appcfgGetc(APPCFG_XBRD_TYPE) == XBRD_TYPE_IOR5E) { ior5eLatchData.bits.ledPWR ^= 1; // Toggle } */ } // Do MAC work StackTask(); //MACTask();; //I2C Task i2cTask(); //Add your application specific tasks here. ProcessIO(); // Do VSCP periodic tasks periodicVSCPWork(); } }