void ClearNVM( WORD dest, WORD count ) { BYTE dummy = 0; WORD i; BYTE oldByte; #ifdef ENABLE_DEBUG ConsolePut('c'); #endif for (i=0; i<count; i++) { #if defined(VERIFY_WRITE) NVMRead( &oldByte, dest, 1 ); while (oldByte) #elif defined(CHECK_BEFORE_WRITE) NVMRead( &oldByte, dest, 1 ); if (oldByte) #endif { NVMWrite( dest, &dummy, 1 ); #if defined(VERIFY_WRITE) NVMRead( &oldByte, dest, 1 ); #endif } dest++; CLRWDT(); } }
void ClearNVM( NVM_ADDR *dest, WORD count ) { BYTE dummy = 0; WORD i; for (i=0; i<count; i++) { #if defined(VERIFY_WRITE) while (memcmppgm2ram( &dummy, (rom void *)dest, 1 )) #elif defined(CHECK_BEFORE_WRITE) if (memcmppgm2ram( &dummy, (rom void *)dest, 1 )) #endif { NVMWrite( dest, &dummy, 1 ); } dest++; CLRWDT(); } }
void CheckDataAndSend(void) { LONG_ADDR IeeAddress; int Tp; WORD_VAL ZDODstAddr; BYTE addr1[2]; BYTE i = 0; int LogAddrPos; unsigned int SenLen; WORD_VAL MSDCL_ClusterID; unsigned char SenUart2Buffer[800]; //Check for start Network command from HHU if( memcmp( (void*)Uart2Buffer, (void*)NetworkStartCommand, sizeof(NetworkStartCommand) ) == 0 ) { StartNetworkFlag = TRUE; //This will trigger the start of network from the main function. Till this function is sent, it will wait SendModuleNetworkSetCommandResponse( (const unsigned char*) NetworkStartCommandResponse, sizeof(NetworkStartCommandResponse) ); } else if( memcmp( (void*)Uart2Buffer, (void*)NetworkStatusCommand, sizeof(NetworkStatusCommand) ) == 0 ) { NetworkStatusCommandResponse[sizeof(NetworkStatusCommandResponse) - 1] = StartNetworkFlag; //StartNetworkFlag; //This will trigger the start of network from the main function. Till this function is sent, it will wait SendModuleNetworkSetCommandResponse( (unsigned char*) NetworkStatusCommandResponse, sizeof(NetworkStatusCommandResponse) ); } else if( memcmp( (void*)Uart2Buffer, (void*)NetworkStopCommand, sizeof(NetworkStopCommand) ) == 0 ) { SendModuleNetworkSetCommandResponse( (const unsigned char*) NetworkStopCommandResponse, sizeof(NetworkStopCommandResponse) ); DelayMs(1000); Reset(); //Simple reset the device on reception of this command } else if( memcmp( (void*)Uart2Buffer, (void*)SetNetworkExtendedPANIdCommand, sizeof(SetNetworkExtendedPANIdCommand) ) == 0 ) { NVMRead ( (BYTE*)&MSDCL_Commission, MSDCL_Commission_Locations, sizeof(MSDCL_Commission)); //Set the Network Extended PAN Id sent from the HHU memcpy( (void*)MSDCL_Commission.ExtendedPANId, (void*)&Uart2Buffer[sizeof(SetNetworkExtendedPANIdCommand)], sizeof(MSDCL_Commission.ExtendedPANId) ); NVMWrite( MSDCL_Commission_Locations, (BYTE*)&MSDCL_Commission, sizeof(MSDCL_Commission) ); SendModuleNetworkSetCommandResponse( (const unsigned char*) SetNetworkExtendedPANIdCommandResponse, sizeof(SetNetworkExtendedPANIdCommandResponse) ); } else if( memcmp( (void*)Uart2Buffer, (void*)SetNetworkLinkKeyCommand, sizeof(SetNetworkLinkKeyCommand) ) == 0 ) { NVMRead ( (BYTE*)&MSDCL_Commission, MSDCL_Commission_Locations, sizeof(MSDCL_Commission)); //Set the Network Link Key sent from the HHU memcpy( (void*)MSDCL_Commission.LinkKey, (void*)&Uart2Buffer[sizeof(SetNetworkLinkKeyCommand)], sizeof(MSDCL_Commission.LinkKey) ); NVMWrite( MSDCL_Commission_Locations, (BYTE*)&MSDCL_Commission, sizeof(MSDCL_Commission) ); SendModuleNetworkSetCommandResponse( (const unsigned char*) SetNetworkLinkKeyCommandResponse, sizeof(SetNetworkLinkKeyCommandResponse) ); } else if( memcmp( (void*)Uart2Buffer, (void*)SetNetworkDeviceTypeCommand, sizeof(SetNetworkDeviceTypeCommand) ) == 0 ) { NVMRead ( (BYTE*)&MSDCL_Commission, MSDCL_Commission_Locations, sizeof(MSDCL_Commission)); //Set the Network StartUp Status sent from the HHU if( Uart2Buffer[sizeof(SetNetworkDeviceTypeCommand)] == SET_NETWORK_DEVICE_TYPE_ESP ) { MSDCL_Commission.StartupStatus = STARTUP_CONTROL_FORM_NEW_NETWORK; } else if( Uart2Buffer[sizeof(SetNetworkDeviceTypeCommand)] == SET_NETWORK_DEVICE_TYPE_MTR ) { MSDCL_Commission.StartupStatus = STARTUP_CONTROL_JOIN_NEW_NETWORK; } NVMWrite( MSDCL_Commission_Locations, (BYTE*)&MSDCL_Commission, sizeof(MSDCL_Commission) ); SendModuleNetworkSetCommandResponse( (const unsigned char*) SetNetworkDeviceTypeCommandResponse, sizeof(SetNetworkDeviceTypeCommandResponse) ); } else if( memcmp( (void*)Uart2Buffer, (void*)SetNetworkResetCommand, sizeof(SetNetworkResetCommand) ) == 0 ) { NVMRead ( (BYTE*)&MSDCL_Commission, MSDCL_Commission_Locations, sizeof(MSDCL_Commission)); //Set the Network Reset sent from the HHU MSDCL_Commission.ChannelMask.Val = ALLOWED_CHANNELS; //0b00000000000000001000000000000000; //Channel 15 as default MSDCL_Commission.ValidCleanStartUp = MSDCL_COMMISSION_DATA_VALID; NVMWrite( MSDCL_Commission_Locations, (BYTE*)&MSDCL_Commission, sizeof(MSDCL_Commission) ); SendModuleNetworkSetCommandResponse( (const unsigned char*) SetNetworkResetCommandResponse, sizeof(SetNetworkResetCommandResponse) ); DelayMs(1000); Reset(); } else if( memcmp( (void*)Uart2Buffer, (void*)SetNetworkChannelMaskCommand, sizeof(SetNetworkChannelMaskCommand) ) == 0 ) { SendModuleNetworkSetCommandResponse( (const unsigned char*) SetNetworkChannelMaskResponce, sizeof(SetNetworkChannelMaskResponce) ); } else if( memcmp( (void*)Uart2Buffer, (void*)SetNetworkDefaultESPCommand, sizeof(SetNetworkDefaultESPCommand) ) == 0 ) { NVMRead ( (BYTE*)&MSDCL_Commission, MSDCL_Commission_Locations, sizeof(MSDCL_Commission)); //Set the Network Extended Pan Id Status sent from the HHU MSDCL_Commission.ExtendedPANId[0] = NWK_EXTENDED_PAN_ID_BYTE0; MSDCL_Commission.ExtendedPANId[1] = NWK_EXTENDED_PAN_ID_BYTE1; MSDCL_Commission.ExtendedPANId[2] = NWK_EXTENDED_PAN_ID_BYTE2; MSDCL_Commission.ExtendedPANId[3] = NWK_EXTENDED_PAN_ID_BYTE3; MSDCL_Commission.ExtendedPANId[4] = NWK_EXTENDED_PAN_ID_BYTE4; MSDCL_Commission.ExtendedPANId[5] = NWK_EXTENDED_PAN_ID_BYTE5; MSDCL_Commission.ExtendedPANId[6] = NWK_EXTENDED_PAN_ID_BYTE6; MSDCL_Commission.ExtendedPANId[7] = NWK_EXTENDED_PAN_ID_BYTE7; //Set Channel Mask MSDCL_Commission.ChannelMask.Val = ALLOWED_CHANNELS_PRE_CONFIG;// ALLOWED_CHANNELS; //Set Link Key MSDCL_Commission.LinkKey[0] = PRECONFIGURED_LINK_KEY00; MSDCL_Commission.LinkKey[1] = PRECONFIGURED_LINK_KEY01; MSDCL_Commission.LinkKey[2] = PRECONFIGURED_LINK_KEY02; MSDCL_Commission.LinkKey[3] = PRECONFIGURED_LINK_KEY03; MSDCL_Commission.LinkKey[4] = PRECONFIGURED_LINK_KEY04; MSDCL_Commission.LinkKey[5] = PRECONFIGURED_LINK_KEY05; MSDCL_Commission.LinkKey[6] = PRECONFIGURED_LINK_KEY06; MSDCL_Commission.LinkKey[7] = PRECONFIGURED_LINK_KEY07; MSDCL_Commission.LinkKey[8] = PRECONFIGURED_LINK_KEY08; MSDCL_Commission.LinkKey[9] = PRECONFIGURED_LINK_KEY09; MSDCL_Commission.LinkKey[10] = PRECONFIGURED_LINK_KEY10; MSDCL_Commission.LinkKey[11] = PRECONFIGURED_LINK_KEY11; MSDCL_Commission.LinkKey[12] = PRECONFIGURED_LINK_KEY12; MSDCL_Commission.LinkKey[13] = PRECONFIGURED_LINK_KEY13; MSDCL_Commission.LinkKey[14] = PRECONFIGURED_LINK_KEY14; MSDCL_Commission.LinkKey[15] = PRECONFIGURED_LINK_KEY15; //Set Startup Status MSDCL_Commission.StartupStatus = STARTUP_CONTROL_FORM_NEW_NETWORK; MSDCL_Commission.ValidCleanStartUp = MSDCL_COMMISSION_DATA_VALID; NVMWrite( MSDCL_Commission_Locations, (BYTE*)&MSDCL_Commission, sizeof(MSDCL_Commission) ); SendModuleNetworkSetCommandResponse( (const unsigned char*) SetNetworkDefaultESPCommandResponse, sizeof(SetNetworkDefaultESPCommandResponse) ); } else if(memcmp( (void*)Uart2Buffer, (void*)NumberOfAddedMetersCommand, sizeof(NumberOfAddedMetersCommand) ) == 0 ) { FindNumberofAddedMetersResponce(); } else if(memcmp( (void*)Uart2Buffer, (void*)MyRouteRequest, sizeof(MyRouteRequest) ) == 0 ) { } else if(memcmp( (void*)Uart2Buffer, (void*)MyChannelNumberCommand, sizeof(MyChannelNumberCommand) ) == 0 ) { FindMyChannel(); } else if(memcmp( (void*)Uart2Buffer, (void*)AddMeterCommand, sizeof(AddMeterCommand) ) == 0 ) { unsigned char Tp2 = 0; char TpStr3[20]; unsigned char success =0; if(MACAddMeter.NumberMeter<MAX_NEIGHBORS) { success = 1; Tp2 = 10; for(Tp=0;Tp<=7;Tp++) { MACAddMeter.AddMeter[MACAddMeter.NumberMeter].v[Tp] = Uart2Buffer[Tp2]; Tp2++; } MACAddMeter.NumberMeter++; } SendAddMeterCommandResponce(success); } else if( (Uart2Buffer[2] == 0x02) && (Uart2Buffer[4] == 0x72) ) { SenLen = 0; SenUart2Buffer[SenLen++] = 0x2D; SenUart2Buffer[SenLen++] = 0x01; SenUart2Buffer[SenLen++] = 0x82; SenUart2Buffer[SenLen++] = 0x72; SenUart2Buffer[SenLen++] = 0x74; SenUart2Buffer[SenLen++] = 0x00; SenUart2Buffer[SenLen++] = EndDeviceAnounceTable.Counter; if(EndDeviceAnounceTable.Counter>0) { for(Tp=0;Tp<EndDeviceAnounceTable.Counter;Tp++) { int Tp2; for(Tp2=7;Tp2>=0;Tp2--) { SenUart2Buffer[SenLen++] = EndDeviceAnounceTable.EndDevAddr[Tp].longaddress.v[Tp2]; } SenUart2Buffer[SenLen++] = EndDeviceAnounceTable.EndDevAddr[Tp].shortaddress.v[1]; SenUart2Buffer[SenLen++] = EndDeviceAnounceTable.EndDevAddr[Tp].shortaddress.v[0]; } } SenUart2Buffer[1] = SenLen-1; //xprintf("\n\r I Send Data = "); for(Tp=0;Tp<SenLen;Tp++) { //PrintChar(SenUart2Buffer[Tp]); //ConsolePut(' '); x2putc(SenUart2Buffer[Tp]); //Delay10us(1); } } else if( (Uart2Buffer[2] == 0x02) && (Uart2Buffer[4] == 0x73) ) { //AckReceived = 1; SendExtPANIdResponse(); } else if((Uart2Buffer[2] == 0x04)) { i = 0; { unsigned char cntr; for( cntr = 15; cntr <=Uart2Datalen; cntr++ ) { asduData[i++] = Uart2Buffer[cntr]; PrintChar(Uart2Buffer[cntr]); ConsolePut(' '); } MSDCL_ClusterID.byte.HB = Uart2Buffer[13]; MSDCL_ClusterID.byte.LB = Uart2Buffer[14]; } ZDODstAddr.Val = 0xFFFD; //xprintf("\n\raddr1[0] = %02x \n\r",addr1[0]); //xprintf("\n\raddr1[1] = %02x \n\r",addr1[1]); //xprintf("\n\rZDODstAddr.Val = %04X \n\r",ZDODstAddr.Val); SendAPPLRequest( ZDODstAddr, MSDCL_ClusterID.Val , asduData, i); /* for(Tp=0;Tp<NumberOfDevicesConnected;Tp++) { i = 0; { unsigned char cntr; for( cntr = 15; cntr <=Uart2Datalen; cntr++ ) { asduData[i++] = Uart2Buffer[cntr]; PrintChar(Uart2Buffer[cntr]); ConsolePut(' '); } MSDCL_ClusterID.byte.HB = Uart2Buffer[13]; MSDCL_ClusterID.byte.LB = Uart2Buffer[14]; } addr1[0] = CurrentDeviceConnectedInfo[Tp].shortAddr.v[0]; addr1[1] = CurrentDeviceConnectedInfo[Tp].shortAddr.v[1]; ZDODstAddr.Val = ( (addr1[1] << 8) | addr1[0] ); //xprintf("\n\raddr1[0] = %02x \n\r",addr1[0]); //xprintf("\n\raddr1[1] = %02x \n\r",addr1[1]); //xprintf("\n\rZDODstAddr.Val = %04X \n\r",ZDODstAddr.Val); SendAPPLRequest( ZDODstAddr, MSDCL_ClusterID.Val , asduData, i); SendModuleNetworkSetCommandResponse( (const unsigned char*) SetNetworkResetCommandResponse, sizeof(SetNetworkResetCommandResponse) ); }*/ } else { IeeAddress.v[7] = Uart2Buffer[10]; IeeAddress.v[6] = Uart2Buffer[9]; IeeAddress.v[5] = Uart2Buffer[8]; IeeAddress.v[4] = Uart2Buffer[7]; IeeAddress.v[3] = Uart2Buffer[6]; IeeAddress.v[2] = Uart2Buffer[5]; IeeAddress.v[1] = Uart2Buffer[4]; IeeAddress.v[0] = Uart2Buffer[3]; //xprintf("\n\r IeeAddress Received ="); //PrintChar(IeeAddress.v[7]); //PrintChar(IeeAddress.v[6]); //PrintChar(IeeAddress.v[5]); //PrintChar(IeeAddress.v[4]); //PrintChar(IeeAddress.v[3]); //PrintChar(IeeAddress.v[2]); //PrintChar(IeeAddress.v[1]); //PrintChar(IeeAddress.v[0]); //xprintf("\n\rNow Going to send Data = "); { unsigned char cntr; for( cntr = 15; cntr <=Uart2Datalen; cntr++ ) { asduData[i++] = Uart2Buffer[cntr]; //PrintChar(Uart2Buffer[cntr]); //ConsolePut(' '); } MSDCL_ClusterID.byte.HB = Uart2Buffer[13]; MSDCL_ClusterID.byte.LB = Uart2Buffer[14]; } { LogAddrPos = -1; if(EndDeviceAnounceTable.Counter>0) { for(Tp=0;Tp<EndDeviceAnounceTable.Counter;Tp++) { //xprintf("\n\r IeeAddress Received ="); //PrintChar(IeeAddress.v[7]); //PrintChar(IeeAddress.v[6]); //PrintChar(IeeAddress.v[5]); //PrintChar(IeeAddress.v[4]); //PrintChar(IeeAddress.v[3]); //PrintChar(IeeAddress.v[2]); //PrintChar(IeeAddress.v[1]); //PrintChar(IeeAddress.v[0]); //xprintf("\n\r Cur Device Log Addr ="); //PrintChar(CurrentDeviceConnectedInfo[Tp].longAddr.v[7]); //PrintChar(CurrentDeviceConnectedInfo[Tp].longAddr.v[6]); //PrintChar(CurrentDeviceConnectedInfo[Tp].longAddr.v[5]); //PrintChar(CurrentDeviceConnectedInfo[Tp].longAddr.v[4]); //PrintChar(CurrentDeviceConnectedInfo[Tp].longAddr.v[3]); //PrintChar(CurrentDeviceConnectedInfo[Tp].longAddr.v[2]); //PrintChar(CurrentDeviceConnectedInfo[Tp].longAddr.v[1]); //PrintChar(CurrentDeviceConnectedInfo[Tp].longAddr.v[0]); if( IeeAddress.v[7] == EndDeviceAnounceTable.EndDevAddr[Tp].longaddress.v[7] && IeeAddress.v[6] == EndDeviceAnounceTable.EndDevAddr[Tp].longaddress.v[6] && IeeAddress.v[5] == EndDeviceAnounceTable.EndDevAddr[Tp].longaddress.v[5] && IeeAddress.v[4] == EndDeviceAnounceTable.EndDevAddr[Tp].longaddress.v[4] && IeeAddress.v[3] == EndDeviceAnounceTable.EndDevAddr[Tp].longaddress.v[3] && IeeAddress.v[2] == EndDeviceAnounceTable.EndDevAddr[Tp].longaddress.v[2] && IeeAddress.v[1] == EndDeviceAnounceTable.EndDevAddr[Tp].longaddress.v[1] && IeeAddress.v[0] == EndDeviceAnounceTable.EndDevAddr[Tp].longaddress.v[0]) { LogAddrPos = Tp; break; } } } //xprintf("\n\r LogAddrPos = %u\n\r",LogAddrPos); if(LogAddrPos != -1) { addr1[0] = EndDeviceAnounceTable.EndDevAddr[LogAddrPos].shortaddress.v[0]; addr1[1] = EndDeviceAnounceTable.EndDevAddr[LogAddrPos].shortaddress.v[1]; ZDODstAddr.Val = ( (addr1[1] << 8) | addr1[0] ); xprintf("\n\raddr1[0] = %02x \n\r",addr1[0]); xprintf("\n\raddr1[1] = %02x \n\r",addr1[1]); xprintf("\n\rZDODstAddr.Val = %04X \n\r",ZDODstAddr.Val); SendAPPLRequest( ZDODstAddr, MSDCL_ClusterID.Val , asduData, i); } else { MyAskForDeviceAddress(FALSE,IeeAddress); } } } }
BYTE NVMInit( void ) { BYTE *memBlock; BYTE result = 0; SPIUnselectEEPROM(); CLRWDT(); #ifdef ENABLE_DEBUG ConsolePutROMString((ROM char * const)"NVMInit started...\r\n"); #endif #if defined(USE_EXTERNAL_NVM) && defined(STORE_MAC_EXTERNAL) result |= NVMalloc(sizeof (LONG_ADDR), &macLongAddrEE ); #ifdef ZCP_DEBUG PutMACAddress(macLongAddrByte); #endif #endif #if defined(I_SUPPORT_BINDINGS) result |= NVMalloc( sizeof(WORD), &bindingValidityKey ); result |= NVMalloc( sizeof(BINDING_RECORD) * MAX_BINDINGS, &apsBindingTable ); result |= NVMalloc( BINDING_USAGE_MAP_SIZE, &bindingTableUsageMap ); result |= NVMalloc( BINDING_USAGE_MAP_SIZE, &bindingTableSourceNodeMap ); #endif #if defined(I_SUPPORT_GROUP_ADDRESSING) result |= NVMalloc( sizeof(APS_GROUP_RECORD) * MAX_GROUP, &apsGroupAddressTable); #endif result |= NVMalloc( sizeof(NEIGHBOR_TABLE_INFO), &neighborTableInfo ); result |= NVMalloc( sizeof(NEIGHBOR_RECORD) * MAX_NEIGHBORS, &neighborTable ); #if defined(I_SUPPORT_ROUTING) && !defined(USE_TREE_ROUTING_ONLY) result |= NVMalloc( sizeof(ROUTING_ENTRY) * ROUTING_TABLE_SIZE, &routingTable ); #endif result |= NVMalloc( sizeof(NODE_DESCRIPTOR), &configNodeDescriptor ); result |= NVMalloc( sizeof(NODE_POWER_DESCRIPTOR), &configPowerDescriptor ); // NOTE - the simple descriptor for the ZDO has been removed in later specs, so the "+1" will go away. result |= NVMalloc( sizeof(NODE_SIMPLE_DESCRIPTOR) * (NUM_USER_ENDPOINTS+1), &configSimpleDescriptors ); #if MAX_APS_ADDRESSES > 0 result |= NVMalloc( sizeof(WORD), &apsAddressMapValidityKey ); result |= NVMalloc( sizeof(APS_ADDRESS_MAP) * MAX_APS_ADDRESSES, &apsAddressMap ); #endif #if defined(I_SUPPORT_SECURITY) result |= NVMalloc( sizeof(BYTE), &nwkActiveKeyNumber ); result |= NVMalloc( sizeof(NETWORK_KEY_INFO) * NUM_NWK_KEYS, &networkKeyInfo ); #if !(defined(I_AM_COORDINATOR) || defined(I_AM_TRUST_CENTER)) result |= NVMalloc( sizeof(LONG_ADDR), &trustCenterLongAddr ); #endif #endif if (!result) { #ifdef ENABLE_DEBUG ConsolePutROMString((ROM char * const)"Initializing EE...\r\n"); #endif // If the MAC Address is stored externally, then the user is responsible // for programming it. They may choose to preprogram the EEPROM, or program // it based on other input. It should be programmed with the PutMACAddress() macro. // Initialize the trust center address #if defined(I_SUPPORT_SECURITY) && (defined(I_AM_COORDINATOR) || defined(I_AM_TRUST_CENTER)) if ((memBlock = SRAMalloc( 8 )) == NULL) { result = 1; } else { int i = 0; memBlock[i++] = TRUST_CENTER_LONG_ADDR_BYTE0; memBlock[i++] = TRUST_CENTER_LONG_ADDR_BYTE1; memBlock[i++] = TRUST_CENTER_LONG_ADDR_BYTE2; memBlock[i++] = TRUST_CENTER_LONG_ADDR_BYTE3; memBlock[i++] = TRUST_CENTER_LONG_ADDR_BYTE4; memBlock[i++] = TRUST_CENTER_LONG_ADDR_BYTE5; memBlock[i++] = TRUST_CENTER_LONG_ADDR_BYTE6; memBlock[i++] = TRUST_CENTER_LONG_ADDR_BYTE7; NVMWrite( trustCenterLongAddr, memBlock, 8 ); SRAMfree( memBlock ); } #endif // Initialize the descriptors using the ROM copy. if ((memBlock = SRAMalloc( sizeof(NODE_DESCRIPTOR) )) == NULL) { result = 1; } else { memcpypgm2ram( memBlock, (void *)&Config_Node_Descriptor, sizeof(NODE_DESCRIPTOR) ); NVMWrite( configNodeDescriptor, memBlock, sizeof(NODE_DESCRIPTOR) ); SRAMfree( memBlock ); } if ((memBlock = SRAMalloc( sizeof(NODE_POWER_DESCRIPTOR) )) == NULL) { result = 1; } else { memcpypgm2ram( memBlock, (void *)&Config_Power_Descriptor, sizeof(NODE_POWER_DESCRIPTOR) ); NVMWrite( configPowerDescriptor, memBlock, sizeof(NODE_POWER_DESCRIPTOR) ); SRAMfree( memBlock ); } if ((memBlock = SRAMalloc( sizeof(NODE_SIMPLE_DESCRIPTOR) )) == NULL) { result = 1; } else { // NOTE - Currently, a simple descriptor is needed for the ZDO endpoint. When this requirement // goes away, take off the "+1". int i; for (i=0; i<NUM_USER_ENDPOINTS+1; i++) { memcpypgm2ram( memBlock, (void *)Config_Simple_Descriptors + i * sizeof(NODE_SIMPLE_DESCRIPTOR), sizeof(NODE_SIMPLE_DESCRIPTOR) ); NVMWrite( configSimpleDescriptors + (WORD)i * (WORD)sizeof(NODE_SIMPLE_DESCRIPTOR), memBlock, sizeof(NODE_SIMPLE_DESCRIPTOR) ); } SRAMfree( memBlock ); } } #ifdef ENABLE_DEBUG ConsolePutROMString((ROM char * const)"NVMInit complete.\r\n"); #endif CLRWDT(); return result; }
BYTE NVMInit( void ) { BYTE *memBlock; BYTE result = 0; WORD NodeDescriptorValiditykey; WORD validitykey; SPIUnselectEEPROM(); CLRWDT(); #if defined(I_SUPPORT_BINDINGS) result |= NVMalloc( sizeof(WORD), &bindingValidityKey ); result |= NVMalloc( sizeof(BINDING_RECORD) * MAX_BINDINGS, &apsBindingTable ); result |= NVMalloc( BINDING_USAGE_MAP_SIZE, &bindingTableUsageMap ); result |= NVMalloc( BINDING_USAGE_MAP_SIZE, &bindingTableSourceNodeMap ); #endif #if defined(USE_EXTERNAL_NVM) && defined(STORE_MAC_EXTERNAL) result |= NVMalloc( sizeof(WORD), &macLongAddrValidityKey ); result |= NVMalloc( sizeof(LONG_ADDR), &macLongAddrEE ); GetMACAddressValidityKey(&validitykey); if (validitykey != MAC_ADDRESS_VALID) { PutMACAddress(macLongAddrByte); MACEnable(); } #endif #if defined(I_SUPPORT_GROUP_ADDRESSING) result |= NVMalloc( sizeof(APS_GROUP_RECORD) * MAX_GROUP, &apsGroupAddressTable); #endif result |= NVMalloc( sizeof(NEIGHBOR_TABLE_INFO), &neighborTableInfo ); result |= NVMalloc( sizeof(NEIGHBOR_RECORD) * MAX_NEIGHBORS, &neighborTable ); #if defined(I_SUPPORT_ROUTING) && !defined(USE_TREE_ROUTING_ONLY) result |= NVMalloc( sizeof(ROUTING_ENTRY) * ROUTING_TABLE_SIZE, &routingTable ); #endif result |= NVMalloc( sizeof(WORD), &nodeDescriptorValidityKey); result |= NVMalloc( sizeof(NODE_DESCRIPTOR), &configNodeDescriptor ); result |= NVMalloc( sizeof(NODE_POWER_DESCRIPTOR), &configPowerDescriptor ); // NOTE - the simple descriptor for the ZDO has been removed in later specs, so the "+1" will go away. result |= NVMalloc( sizeof(NODE_SIMPLE_DESCRIPTOR) * (NUM_USER_ENDPOINTS+1), &configSimpleDescriptors ); result |= NVMalloc(sizeof(PERSISTENCE_PIB), &persistencePIB); #if MAX_APS_ADDRESSES > 0 result |= NVMalloc( sizeof(WORD), &apsAddressMapValidityKey ); result |= NVMalloc( sizeof(APS_ADDRESS_MAP) * MAX_APS_ADDRESSES, &apsAddressMap ); #endif #if defined(I_SUPPORT_SECURITY) result |= NVMalloc( sizeof(BYTE), &nwkActiveKeyNumber ); result |= NVMalloc( sizeof(NETWORK_KEY_INFO) * NUM_NWK_KEYS, &networkKeyInfo ); /* location for outgoing nwk frame counters */ result |= NVMalloc(( sizeof(DWORD_VAL)*2), &outgoingFrameCounterIndex); #endif #if I_SUPPORT_LINK_KEY == 1 result |= NVMalloc( sizeof(APS_KEY_PAIR_DESCRIPTOR) * MAX_APPLICATION_LINK_KEY_SUPPORTED, &appLinkKeyTable ); #endif #if I_SUPPORT_LINK_KEY == 1 #if I_SUPPORT_MULTIPLE_TC_LINK_KEY == 1 result |= NVMalloc( sizeof(TC_LINK_KEY_TABLE) * MAX_TC_LINK_KEY_SUPPORTED, &TCLinkKeyTable ); #endif #endif #if defined(I_SUPPORT_COMMISSIONING) result |= NVMalloc( sizeof(BYTE), &activeSASIndex ); result |= NVMalloc( sizeof(STARTUP_ATTRIBUTE_SET), &default_SAS ); result |= NVMalloc( (sizeof(STARTUP_ATTRIBUTE_SET) * 2), &Commissioned_SAS ); #endif #if defined(I_SUPPORT_SECURITY) result |= NVMalloc(( sizeof(DWORD_VAL)*2), &outgoingFrameCounterIndex); #endif if (!result) { #ifdef ENABLE_DEBUG ConsolePutROMString((ROM char * const)"Initializing EE...\r\n"); #endif #ifdef I_SUPPORT_COMMISSIONING BYTE index; #endif // If the MAC Address is stored externally, then the user is responsible // for programming it. They may choose to preprogram the EEPROM, or program // it based on other input. It should be programmed with the PutMACAddress() macro. /* // Initialize the trust center address //below is code is comment #if defined(I_SUPPORT_SECURITY) && (defined(I_AM_COORDINATOR) || defined(I_AM_TRUST_CENTER)) if ((memBlock = SRAMalloc( 8 )) == NULL) { result = 1; } else { int i = 0; memBlock[i++] = current_SAS.spas.TrustCenterAddress.v[0]; memBlock[i++] = current_SAS.spas.TrustCenterAddress.v[1]; memBlock[i++] = current_SAS.spas.TrustCenterAddress.v[2]; memBlock[i++] = current_SAS.spas.TrustCenterAddress.v[3]; memBlock[i++] = current_SAS.spas.TrustCenterAddress.v[4]; memBlock[i++] = current_SAS.spas.TrustCenterAddress.v[5]; memBlock[i++] = current_SAS.spas.TrustCenterAddress.v[6]; memBlock[i++] = current_SAS.spas.TrustCenterAddress.v[7]; NVMWrite( trustCenterLongAddr, memBlock, 8 ); SRAMfree( memBlock ); } #endif*/ GetNodeDescriptorValidity(&NodeDescriptorValiditykey); if (NodeDescriptorValiditykey != NODE_DESCRIPTOR_VALID) { // Initialize the descriptors using the ROM copy. if ((memBlock = SRAMalloc( sizeof(NODE_DESCRIPTOR) )) == NULL) { result = 1; } else { memcpy( memBlock, (void *)&Config_Node_Descriptor, sizeof(NODE_DESCRIPTOR) ); NVMWrite( configNodeDescriptor, memBlock, sizeof(NODE_DESCRIPTOR) ); SRAMfree( memBlock ); NodeDescriptorValiditykey = NODE_DESCRIPTOR_VALID; PutNodeDescriptorValidity(&NodeDescriptorValiditykey); } } if ((memBlock = SRAMalloc( sizeof(NODE_POWER_DESCRIPTOR) )) == NULL) { result = 1; } else { memcpy( memBlock, (void *)&Config_Power_Descriptor, sizeof(NODE_POWER_DESCRIPTOR) ); NVMWrite( configPowerDescriptor, memBlock, sizeof(NODE_POWER_DESCRIPTOR) ); SRAMfree( memBlock ); } if ((memBlock = SRAMalloc( sizeof(NODE_SIMPLE_DESCRIPTOR) )) == NULL) { result = 1; } else { // NOTE - Currently, a simple descriptor is needed for the ZDO endpoint. When this requirement // goes away, take off the "+1". int i; for (i=0; i<NUM_USER_ENDPOINTS+1; i++) { if(NOW_I_AM_A_ROUTER()) memcpypgm2ram( memBlock, (void *)Config_Simple_Descriptors_MTR + i * sizeof(NODE_SIMPLE_DESCRIPTOR), sizeof(NODE_SIMPLE_DESCRIPTOR) ); else if (NOW_I_AM_A_CORDINATOR()) memcpypgm2ram( memBlock, (void *)Config_Simple_Descriptors_ESP + i * sizeof(NODE_SIMPLE_DESCRIPTOR), sizeof(NODE_SIMPLE_DESCRIPTOR) ); //memcpypgm2ram( memBlock, (void *)Config_Simple_Descriptors + i * sizeof(NODE_SIMPLE_DESCRIPTOR), sizeof(NODE_SIMPLE_DESCRIPTOR) ); NVMWrite( configSimpleDescriptors + (WORD)i * (WORD)sizeof(NODE_SIMPLE_DESCRIPTOR), memBlock, sizeof(NODE_SIMPLE_DESCRIPTOR) ); } SRAMfree( memBlock ); } #ifdef I_SUPPORT_COMMISSIONING GetSAS(¤t_SAS,default_SAS); if( MSDCL_Commission.ValidCleanStartUp == MSDCL_COMMISSION_DATA_VALID) { memcpy(¤t_SAS.spas.ExtendedPANId.v[0], &MSDCL_Commission.ExtendedPANId[0], sizeof(MSDCL_Commission.ExtendedPANId) ); current_SAS.spas.PreconfiguredLinkKey[15]= MSDCL_Commission.LinkKey[15]; current_SAS.spas.PreconfiguredLinkKey[14]= MSDCL_Commission.LinkKey[14]; current_SAS.spas.PreconfiguredLinkKey[13]= MSDCL_Commission.LinkKey[13]; current_SAS.spas.PreconfiguredLinkKey[12]= MSDCL_Commission.LinkKey[12]; current_SAS.spas.PreconfiguredLinkKey[11]= MSDCL_Commission.LinkKey[11]; current_SAS.spas.PreconfiguredLinkKey[10]= MSDCL_Commission.LinkKey[10]; current_SAS.spas.PreconfiguredLinkKey[9]= MSDCL_Commission.LinkKey[9]; current_SAS.spas.PreconfiguredLinkKey[8]= MSDCL_Commission.LinkKey[8]; current_SAS.spas.PreconfiguredLinkKey[7]= MSDCL_Commission.LinkKey[7]; current_SAS.spas.PreconfiguredLinkKey[6]= MSDCL_Commission.LinkKey[6]; current_SAS.spas.PreconfiguredLinkKey[5]= MSDCL_Commission.LinkKey[5]; current_SAS.spas.PreconfiguredLinkKey[4]= MSDCL_Commission.LinkKey[4]; current_SAS.spas.PreconfiguredLinkKey[3]= MSDCL_Commission.LinkKey[3]; current_SAS.spas.PreconfiguredLinkKey[2]= MSDCL_Commission.LinkKey[2]; current_SAS.spas.PreconfiguredLinkKey[1]= MSDCL_Commission.LinkKey[1]; current_SAS.spas.PreconfiguredLinkKey[0]= MSDCL_Commission.LinkKey[0]; current_SAS.spas.ChannelMask.Val = MSDCL_Commission.ChannelMask.Val & 0x07FFF800UL; current_SAS.spas.StartupControl = MSDCL_Commission.StartupStatus; //PutNeighborTableInfo(); //MSDCL_Commission.DoCleanStartUp = 0; //NVMWrite( MSDCL_Commission_Locations, (BYTE*)&MSDCL_Commission, sizeof(MSDCL_Commission) ); } if(current_SAS.validitykey != SAS_TABLE_VALID) { Initdefault_SAS(); index = 0xFF; PutActiveSASIndex(&index); } #endif } CLRWDT(); return result; }
int main () { unsigned char zigbee_mode = 0; HardwareInit(); ConsoleInit(); InitSymbolTimer(); uart_init(); initMSDCLTimers(); IFS1bits.U2RXIF = 0; ConsolePutROMString( (ROM char *)"\r\nW to exit"); SendModuleStartData(); while( StartNetworkFlag == FALSE ) { HanddleUART2(); if(IFS1bits.U2RXIF) { zigbee_mode = U2RXREG; if(zigbee_mode == 'W') break; } } StartNetworkFlag = FALSE; zigbee_mode = 0; ConsolePutROMString( (ROM char *)"\r\n*********************************" ); ConsolePutROMString( (ROM char *)"\r\nMicrochip SE Profile 1.0.version.0.5.3" ); ConsolePutROMString( (ROM char *)"\r\n*********************************" ); ConsolePutROMString( (ROM char *)"\r\nE:Comission device as ESP\r\n" ); ConsolePutROMString( (ROM char *)"\r\nM:Comission device as MTR\r\n" ); { TICK startTime = TickGet(); do { IFS1bits.U2RXIF = 0; do { if( TickGetDiff(TickGet(),startTime) > (2 * ONE_SECOND)) { break; } } while( !IFS1bits.U2RXIF ); if( TickGetDiff(TickGet(),startTime) > (2 * ONE_SECOND)) { break; } zigbee_mode = U2RXREG; ConsolePut(zigbee_mode); }while( (zigbee_mode != 'M') && (zigbee_mode != 'm') && (zigbee_mode != 'E') && (zigbee_mode != 'e') ); NVMRead ( (BYTE*)&MSDCL_Commission, MSDCL_Commission_Locations, sizeof(MSDCL_Commission)); if( ( MSDCL_Commission.ValidCleanStartUp != MSDCL_COMMISSION_DATA_VALID ) && (MSDCL_Commission.ValidCleanStartUp != MSDCL_DEFAULT_MTR) && (MSDCL_Commission.ValidCleanStartUp != MSDCL_DEFAULT_ESP) && (zigbee_mode != 'E') && (zigbee_mode != 'e') ) { zigbee_mode = 'M'; } if( ((zigbee_mode == 'M') || (zigbee_mode == 'm') || (MSDCL_Commission.ValidCleanStartUp == MSDCL_DEFAULT_MTR)) && (zigbee_mode != 'E') && (zigbee_mode != 'e') ) { NowIam = 0; NowIam = A_ROUTER | A_FFD; // These variables are exported in Zigbee.def I_AM_TRUST_CENTER = 0; //Trust center enabled Enabled USE_COMMON_TC_LINK_KEY = 1; MAX_ENERGY_THRESHOLD = 112; DEFAULT_STARTUP_CONTROL = DEFAULT_STARTUP_CONTROL_MTR; MSDCL_Commission.StartupStatus = STARTUP_CONTROL_JOIN_NEW_NETWORK; ALLOWED_CHANNELS = ALLOWED_CHANNELS_PRE_CONFIG; if(MSDCL_Commission.ValidCleanStartUp != MSDCL_DEFAULT_MTR) { MSDCL_Commission.ValidCleanStartUp = MSDCL_DEFAULT_MTR; NVMWrite( MSDCL_Commission_Locations, (BYTE*)&MSDCL_Commission, sizeof(MSDCL_Commission) ); } } else if( (zigbee_mode == 'E') || (zigbee_mode == 'e') || (MSDCL_Commission.ValidCleanStartUp == MSDCL_DEFAULT_ESP) ) { NowIam = 0; NowIam = A_CORDINATOR | A_FFD; // These variables are exported in Zigbee.def I_AM_TRUST_CENTER = 1; //Trust center enabled Enabled USE_COMMON_TC_LINK_KEY = 1; MAX_ENERGY_THRESHOLD = 241; DEFAULT_STARTUP_CONTROL = DEFAULT_STARTUP_CONTROL_ESP; MSDCL_Commission.StartupStatus = STARTUP_CONTROL_FORM_NEW_NETWORK; ALLOWED_CHANNELS = ALLOWED_CHANNELS_PRE_CONFIG; if(MSDCL_Commission.ValidCleanStartUp != MSDCL_DEFAULT_ESP) { MSDCL_Commission.ValidCleanStartUp = MSDCL_DEFAULT_ESP; NVMWrite( MSDCL_Commission_Locations, (BYTE*)&MSDCL_Commission, sizeof(MSDCL_Commission) ); } } if((MSDCL_Commission.ValidCleanStartUp == MSDCL_COMMISSION_DATA_VALID) ) { switch( MSDCL_Commission.StartupStatus ) { case STARTUP_CONTROL_FORM_NEW_NETWORK: ConsolePutROMString( (ROM char *)"\r\nStarting as ESP\r\n" ); NowIam = 0; NowIam = A_CORDINATOR | A_FFD; // These variables are exported in Zigbee.def I_AM_TRUST_CENTER = 1; //Trust center enabled Enabled USE_COMMON_TC_LINK_KEY = 1; MAX_ENERGY_THRESHOLD = 241; ALLOWED_CHANNELS = MSDCL_Commission.ChannelMask.Val ; DEFAULT_STARTUP_CONTROL = DEFAULT_STARTUP_CONTROL_ESP; break; case STARTUP_CONTROL_PART_OF_NETWORK_NO_EXPLICIT_ACTION: case STARTUP_CONTROL_JOIN_NEW_NETWORK: case STARTUP_CONTROL_START_FROM_SCRATCH_AS_ROUTER: default: ConsolePutROMString( (ROM char *)"\r\nStarting as MTR\r\n" ); NowIam = 0; NowIam = A_ROUTER | A_FFD; // These variables are exported in Zigbee.def I_AM_TRUST_CENTER = 1; //Trust center enabled Enabled USE_COMMON_TC_LINK_KEY = 0; MAX_ENERGY_THRESHOLD = 112; ALLOWED_CHANNELS = MSDCL_Commission.ChannelMask.Val ; DEFAULT_STARTUP_CONTROL = DEFAULT_STARTUP_CONTROL_MTR; break; } } } // if (NOW_I_AM_A_CORDINATOR()) // USE_COMMON_TC_LINK_KEY = 1; // else // USE_COMMON_TC_LINK_KEY = 0; if(NOW_I_AM_A_ROUTER()) I_AM_TRUST_CENTER = 0; if(NOW_I_AM_A_ROUTER()) appNextSeqNum_PTR = &appNextSeqNum_MTR; else if (NOW_I_AM_A_CORDINATOR()) appNextSeqNum_PTR = &appNextSeqNum_ESP; if(NOW_I_AM_A_ROUTER()) App_AttributeStorageTable = App_AttributeStorageTable_MTR; else if (NOW_I_AM_A_CORDINATOR()) App_AttributeStorageTable = App_AttributeStorageTable_ESP; if(NOW_I_AM_A_ROUTER()) Config_Node_Descriptor.NodeLogicalType = 0x01; else if (NOW_I_AM_A_CORDINATOR()) Config_Node_Descriptor.NodeLogicalType = 0x00; if( NOW_I_AM_A_ROUTER() ) pAppListOfDeviceServerInfo[0] = &Meter_DeviceServerinfo; else if( NOW_I_AM_A_CORDINATOR() ) pAppListOfDeviceServerInfo[0] = &ESP_DeviceServerinfo; if( NOW_I_AM_A_ROUTER() ) pAppListOfDeviceClientInfo[0] = &Meter_DeviceClientinfo; else if( NOW_I_AM_A_CORDINATOR() ) pAppListOfDeviceClientInfo[0] = &ESP_DeviceClientinfo; if( NOW_I_AM_A_ROUTER() ) Config_Simple_Descriptors = Config_Simple_Descriptors_MTR; else if( NOW_I_AM_A_CORDINATOR() ) Config_Simple_Descriptors = Config_Simple_Descriptors_ESP; if( MSDCL_Commission.ValidCleanStartUp == MSDCL_COMMISSION_DATA_VALID) { if( ChannelsToBeScanned.Val == 0 ) { ChannelsToBeScanned.Val = MSDCL_Commission.ChannelMask.Val & 0x03FFF800UL; } } else { if( ChannelsToBeScanned.Val == 0 ) { ChannelsToBeScanned.Val = ALLOWED_CHANNELS_PRE_CONFIG & 0x03FFF800UL; } } { unsigned long channelMaskToScan = 0x00000800UL; if( ( ChannelsToBeScanned.Val & 0x03FFF800UL ) == 0 ) { ChannelsToBeScanned.Val = ALLOWED_CHANNELS_PRE_CONFIG & 0x03FFF800UL; } ChannelsToBeScanned.Val &= 0x03FFF800UL; while( !(ChannelsToBeScanned.Val & channelMaskToScan) ) { channelMaskToScan <<= 1; } ALLOWED_CHANNELS = channelMaskToScan; ChannelsToBeScanned.Val &= channelMaskToScan ^ 0xFFFFFFFFUL; //ALLOWED_CHANNELS = 0x3FFFC00UL; } //ALLOWED_CHANNELS = 0b000000000111111111101111100000000000; ALLOWED_CHANNELS = 0b0000000000010000000000000000000000; while(1) { if (NOW_I_AM_A_CORDINATOR()) main_ESP(); else { main_MTR(); } } }