Beispiel #1
0
    void NVMRead (BYTE *dest, WORD  src, BYTE count)
    {
        #if !defined(__C30__)
        BYTE oldGIEH;
        #endif
        
        #ifdef ENABLE_DEBUG
            ConsolePut('r');
            PrintChar( (BYTE)(((WORD)src>>8)&0xFF) );
            PrintChar( (BYTE)((WORD)src&0xFF) );
            ConsolePut('-');
            PrintChar( count );
        #endif
        #if !defined(__C30__)
            oldGIEH = 0;
            if ( INTCONbits.GIEH )
            {
                oldGIEH = 1;
            }
            INTCONbits.GIEH = 0;
        #endif

        SPISelectEEPROM();
        SPIPut( SPIREAD );
        SPIPut( (BYTE)(((WORD)src>>8) & 0xFF) );
        SPIPut( (BYTE)((WORD)src & 0xFF) );
        while( count )
        {
            *dest = SPIGet();
            #ifdef ENABLE_DEBUG
            #endif
            dest++;
            count--;
        }
        SPIUnselectEEPROM();

        #if !defined(__C30__)
            if (oldGIEH)
            {
                INTCONbits.GIEH = 1;
            }
        #endif

        #ifdef ENABLE_DEBUG
            ConsolePutROMString((ROM char * const)"\r\n");
        #endif
    }
Beispiel #2
0
    void NVMRead (BYTE *dest, WORD  src, BYTE count)
    {
        #if !defined(__C30__) && !defined (__C32__)
        BYTE oldGIEH;
        #endif

        
        #if !defined(__C30__) && !defined(__C32__)
            oldGIEH = 0;
            if ( INTCONbits.GIEH )
            {
                oldGIEH = 1;
            }
            INTCONbits.GIEH = 0;
        #endif

        SPISelectEEPROM();
        SPIPut( SPIREAD );
        SPIPut( (BYTE)(((WORD)src>>8) & 0xFF) );
        SPIPut( (BYTE)((WORD)src & 0xFF) );
        while( count )
        {
            *dest = SPIGet();
            #ifdef ENABLE_DEBUG
            #endif
            dest++;
            count--;
        }
        SPIUnselectEEPROM();

        #if !defined(__C30__) && !defined(__C32__)
            if (oldGIEH)
            {
                INTCONbits.GIEH = 1;
            }
        #endif
    }
Beispiel #3
0
    void NVMWrite( WORD dest, BYTE *src, BYTE count )
    {
        BYTE    bytesOnPage;
        BYTE    bytesOnPageCounter;
    	#if !defined(__C30__)
        	BYTE    oldGIEH;
    	#endif
        BYTE    *srcCounter;
        BYTE    status;
        WORD    writeStart;

        if (!count)
        {
            return;
        }

        #if !defined(__C30__)
            oldGIEH = 0;
            if ( INTCONbits.GIEH )
            {
                oldGIEH = 1;
            }
            INTCONbits.GIEH = 0;
        #endif


        // Make sure the chip is unlocked.
        SPISelectEEPROM();                  // Enable chip select
        SPIPut( SPIWRSR );                  // Send WRSR - Write Status Register opcode
        SPIPut( 0x00 );                     // Write the status register
        SPIUnselectEEPROM();                // Disable Chip Select

        #ifdef ENABLE_DEBUG
            ConsolePut('w');
            PrintChar( (BYTE)(((WORD)dest>>8)&0xFF) );
            PrintChar( (BYTE)((WORD)dest&0xFF) );
            ConsolePut('-');
            PrintChar( count );
        #endif

        writeStart = dest;
        while (count)
        {
            bytesOnPage = EEPROM_PAGE_SIZE - (writeStart & (EEPROM_PAGE_SIZE-1));
            if (bytesOnPage > count)
            {
                bytesOnPage = count;
            }

            #ifdef PRINT_MULTIPLE_WRITE_ATTEMPTS
                flag = 0;
            #endif
            CLRWDT();

            #if defined(VERIFY_WRITE)
                while (ComparePage( writeStart, src, bytesOnPage ))
            #elif defined(CHECK_BEFORE_WRITE)
                if (ComparePage( writeStart, src, bytesOnPage ))
            #endif
            {
                #ifdef PRINT_MULTIPLE_WRITE_ATTEMPTS
                    flag = 1;
                #endif
                SPISelectEEPROM();                          // Enable chip select
                SPIPut( SPIWREN );                          // Transmit the write enable instruction
                SPIUnselectEEPROM();                        // Disable Chip Select to enable Write Enable Latch

                SPISelectEEPROM();                          // Enable chip select
                SPIPut( SPIWRITE );                         // Transmit write instruction
                SPIPut( (BYTE)((writeStart>>8) & 0xFF) );   // Transmit address high byte
                SPIPut( (BYTE)((writeStart) & 0xFF) );      // Trabsmit address low byte

                // Loop until the required number of bytes have been written or
                // until the maximum number of bytes per write cycle have been written
                bytesOnPageCounter = bytesOnPage;
                srcCounter = src;
                do
                {
                    SPIPut (*srcCounter++);                    // Write the source byte
                    bytesOnPageCounter--;
                }
                while (bytesOnPageCounter);

                // Disable chip select to start write cycle.  We'll let it finish in the background if we can.
                SPIUnselectEEPROM();

                // Wait for the write to complete.  We have to wait here, because we can't
                // do a read of the memory while the write is in progress.
                do
                {
                    SPISelectEEPROM();                  // Enable chip select
                    SPIPut( SPIRDSR );                  // Send RDSR - Read Status Register opcode
                    status = SPIGet();;                 // Read the status register
                    SPIUnselectEEPROM();                // Disable Chip Select
                }
                while (status & WIP_MASK);
            }

            count -= bytesOnPage;
            writeStart += bytesOnPage;
            src = &src[bytesOnPage];
        }

        #if !defined(__C30__)
            if (oldGIEH)        // If interrupts were enabled before this function
            {
                INTCONbits.GIEH = 1;       // re-enable them
            }
        #endif

        #ifdef ENABLE_DEBUG
            ConsolePut('.');
            ConsolePutROMString((ROM char * const)"\r\n");
        #endif
    }
Beispiel #4
0
    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;
    }
Beispiel #5
0
    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(&current_SAS,default_SAS);
		       if( MSDCL_Commission.ValidCleanStartUp == MSDCL_COMMISSION_DATA_VALID)
		       {
					memcpy(&current_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;
    }