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; }
void NVMWrite( NVM_ADDR *dest, BYTE *src, BYTE count ) { NVM_ADDR *pEraseBlock; BYTE *memBlock; BYTE *pMemBlock; BYTE writeIndex; BYTE writeStart; BYTE writeCount; BYTE oldGIEH; DWORD oldTBLPTR; #if defined(VERIFY_WRITE) while (memcmppgm2ram( src, (ROM void *)dest, count )) #elif defined(CHECK_BEFORE_WRITE) if (memcmppgm2ram( src, (ROM void *)dest, count )) #endif { if ((memBlock = SRAMalloc( ERASE_BLOCK_SIZE )) == NULL) return; #if 0 ConsolePutROMString( (ROM char * const)"NVMWrite at " ); PrintChar( (BYTE)(((WORD)dest>>8)&0xFF) ); PrintChar( (BYTE)((WORD)dest&0xFF) ); ConsolePutROMString( (ROM char * const)" count " ); PrintChar( count ); ConsolePutROMString( (ROM char * const)"\r\n" ); #endif // First, get the nearest "left" erase block boundary pEraseBlock = (NVM_ADDR*)((long)dest & (long)(~(ERASE_BLOCK_SIZE-1))); writeStart = (BYTE)((BYTE)dest & (BYTE)(ERASE_BLOCK_SIZE-1)); while( count ) { // Now read the entire erase block size into RAM. NVMRead(memBlock, (ROM void*)pEraseBlock, ERASE_BLOCK_SIZE); // Erase the block. // Erase flash memory, enable write control. EECON1 = 0x94; oldGIEH = 0; if ( INTCONbits.GIEH ) oldGIEH = 1; INTCONbits.GIEH = 0; #if defined(MCHP_C18) TBLPTR = (unsigned short long)pEraseBlock; #elif defined(HI_TECH_C) TBLPTR = (void*)pEraseBlock; #endif CLRWDT(); EECON2 = 0x55; EECON2 = 0xaa; EECON1bits.WR = 1; NOP(); EECON1bits.WREN = 0; oldTBLPTR = TBLPTR; if ( oldGIEH ) INTCONbits.GIEH = 1; // Modify 64-byte block of RAM buffer as per what is required. pMemBlock = &memBlock[writeStart]; while( writeStart < ERASE_BLOCK_SIZE && count ) { *pMemBlock++ = *src++; count--; writeStart++; } // After first block write, next start would start from 0. writeStart = 0; // Now write entire 64 byte block in one write block at a time. writeIndex = ERASE_BLOCK_SIZE / WRITE_BLOCK_SIZE; pMemBlock = memBlock; while( writeIndex ) { oldGIEH = 0; if ( INTCONbits.GIEH ) oldGIEH = 1; INTCONbits.GIEH = 0; TBLPTR = oldTBLPTR; // Load individual block writeCount = WRITE_BLOCK_SIZE; while( writeCount-- ) { TABLAT = *pMemBlock++; TBLWTPOSTINC(); } // Start the write process: reposition tblptr back into memory block that we want to write to. #if defined(MCHP_C18) _asm tblrdpostdec _endasm #elif defined(HITECH_C18) asm(" tblrd*-"); #endif // Write flash memory, enable write control. EECON1 = 0x84; CLRWDT(); EECON2 = 0x55; EECON2 = 0xaa; EECON1bits.WR = 1; NOP(); EECON1bits.WREN = 0; // One less block to write writeIndex--; TBLPTR++; oldTBLPTR = TBLPTR; if ( oldGIEH ) INTCONbits.GIEH = 1; } // Go back and do it all over again until we write all // data bytes - this time the next block. #if !defined(WIN32) pEraseBlock += ERASE_BLOCK_SIZE; #endif } SRAMfree( memBlock ); } }
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; }