⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cc2420receivep.nc

📁 tinyos-2.x.rar
💻 NC
📖 第 1 页 / 共 2 页
字号:
								case SHORT_ADDRESS:
												//read the short address + destination PAN identifier
												////printfUART("s ad",""); 
												call RXFIFO.continueRead((uint8_t*)rxmpdu_ptr + 4, 6); 
													break;
								
								case LONG_ADDRESS:
												//read the long address + destination PAN identifier
												call RXFIFO.continueRead((uint8_t*)rxmpdu_ptr + 4, 12);
												break;
							}
						}
						else
						{
							//destination address fields not present
							m_state = S_RX_PAYLOAD;
							//it is not possible to do the address decoding, there is no destination address fields
							//send the full packet up
							call RXFIFO.continueRead((uint8_t*)rxmpdu_ptr + 4, rxmpdu_ptr->length- 3);
							
						}
					}
					else
					{
						//address decode is not activated
						m_state = S_RX_PAYLOAD;
						call RXFIFO.continueRead((uint8_t*)rxmpdu_ptr + 4, rxmpdu_ptr->length - 3);
					}
	
	
					break;			
	case S_RX_ADDR:
					m_state = S_RX_PAYLOAD;
					
					
					switch ((rxmpdu_ptr->frame_control1 & 0x7))
					{
						case TYPE_BEACON:
									////printfUART("RB \n","");
									
									beacon_addr_short_ptr = (beacon_addr_short *) &rxmpdu_ptr->data[0];
									
									////printfUART("pb %x %x %x %x\n",rxmpdu_ptr->seq_num, beacon_addr_short_ptr->destination_PAN_identifier,beacon_addr_short_ptr->destination_address, beacon_addr_short_ptr->source_address);
																		
									/*
									for (i=0;i<6;i++)
									{
										//printfUART("r %i %x %x %x\n",i,rxmpdu_ptr->data[i],rx_buf[i],rx_buf[i+4]);
									}
									*/
									
									//printfUART("RB %x %x \n",ver_macCoordShortAddress,ver_macShortAddress);

									
									//avoid VERIFY static assignment of coordinator parent
									if (beacon_addr_short_ptr->source_address != ver_macCoordShortAddress)
									{
											//printfUART("bad bec %x %x\n", beacon_addr_short_ptr->source_address,ver_macCoordShortAddress);
											
											m_state = S_RX_DISCARD;
											call RXFIFO.continueRead((uint8_t*)rxmpdu_ptr + 10, rxmpdu_ptr->length - 9);
											return;
									}
									/*
									if (ver_macShortAddress != 0xffff)
									{
										if ( beacon_addr_short_ptr->source_address != ver_macShortAddress)
										{
											//printfUART("pb %x %x\n", beacon_addr_short_ptr->source_address,ver_macShortAddress);
											
											m_state = S_RX_DISCARD;
											call RXFIFO.continueRead((uint8_t*)rxmpdu_ptr + 10, rxmpdu_ptr->length - 9);
											return;
										}
									}
									*/
									break;
							case TYPE_DATA:
							case TYPE_CMD:
							
									//VALIDATION OF DESTINATION ADDRESSES - NOT TO OVERLOAD THE PROCESSOR
									if (destination_address > 1)
									{
										switch(destination_address)
										{
											case SHORT_ADDRESS: 
													dest_short_ptr = (dest_short *) &rxmpdu_ptr->data[0];
																
													if ( dest_short_ptr->destination_address != 0xffff && dest_short_ptr->destination_address != ver_macShortAddress)
													{
														//printfUART("nsm %x %x\n", dest_short_ptr->destination_address,ver_macShortAddress); 
														
														m_state = S_RX_DISCARD;
														call RXFIFO.continueRead((uint8_t*)rxmpdu_ptr + 10, rxmpdu_ptr->length - 9);
														return;
													}
													
													//If a destination PAN identifier is included in the frame, it shall match macPANId or shall be the
													//broadcast PAN identifier (0 x ffff).
													if(dest_short_ptr->destination_PAN_identifier != 0xffff && dest_short_ptr->destination_PAN_identifier != ver_macPANId )
													{
														//printfUART("wsP %x %x \n", dest_short_ptr->destination_PAN_identifier,ver_macPANId); 
														
														m_state = S_RX_DISCARD;
														call RXFIFO.continueRead((uint8_t*)rxmpdu_ptr + 10, rxmpdu_ptr->length - 9);
														return;
													}
													
													break;
											
											case LONG_ADDRESS: 
													
													dest_long_ptr = (dest_long *) &rxmpdu_ptr->data[0];
													/*
													if ( dest_long_ptr->destination_address0 !=ver_aExtendedAddress0 && dest_long_ptr->destination_address1 !=ver_aExtendedAddress1 )
													{
														//printfUART("nlm %x %x \n",dest_long_ptr->destination_address0,dest_long_ptr->destination_address1); 
														
														m_state = S_RX_DISCARD;
														call RXFIFO.continueRead((uint8_t*)rxmpdu_ptr + 16, rxmpdu_ptr->length - 15);
														return;
													}
													
													
													//If a destination PAN identifier is included in the frame, it shall match macPANId or shall be the
													//broadcast PAN identifier (0 x ffff).
													if(dest_long_ptr->destination_PAN_identifier != 0xffff && dest_long_ptr->destination_PAN_identifier != ver_macPANId )
													{
														//printfUART("wLP %x %x\n", dest_long_ptr->destination_PAN_identifier,ver_macPANId); 
																	
														m_state = S_RX_DISCARD;
														call RXFIFO.continueRead((uint8_t*)rxmpdu_ptr + 16, rxmpdu_ptr->length - 15);
														return;
													}
													*/
													break;
										}
									}
									
									break;
									
							case TYPE_ACK:
							
										//printfUART("error ack \n",""); 
										//call RXFIFO.continueRead((uint8_t*)rxmpdu_ptr);
										
										return;
										
									break;
									

					}
					
					//read the remaining packet
					switch(destination_address)
					{
						case SHORT_ADDRESS:
							////printfUART("as %i\n", (rxmpdu_ptr->length - 9));
								
							call RXFIFO.continueRead((uint8_t*)rxmpdu_ptr + 10, rxmpdu_ptr->length - 9); //7
							break;
								
						case LONG_ADDRESS:
							////printfUART("al %i\n", (rxmpdu_ptr->length - 15));
							call RXFIFO.continueRead((uint8_t*)rxmpdu_ptr + 16, rxmpdu_ptr->length - 15);
							break;
					}
			
			break;
					
					
	case S_RX_PAYLOAD:
				call CSN.set();
				//signal Receiveframe.receive((uint8_t*)rxmpdu_ptr, rssi);
				
				
				//rssi= 255 - rx_buf[len-1];
				
				/*
				for (i=6;i<12;i++)
				{
					//printfUART("p %i %x %x\n",i,rxmpdu_ptr->data[i],rx_buf[i-6]);
				}
				*/
				
				if(!m_missed_packets) {
					// Release the SPI only if there are no more frames to download
					call SpiResource.release();
				}
				
				rssi = 255 - rxmpdu_ptr->data[rxmpdu_ptr->length-4];
				
				//printfUART("pay %i %x %i\n",rxmpdu_ptr->seq_num, rssi,m_missed_packets);
				
				signal Receiveframe.receive((uint8_t*)rxmpdu_ptr, rssi);
				
				
				if (m_missed_packets == 0)
				{
					flush();
				}
				else
				{
					waitForNextPacket();
				}
				
			break;
	
	
	case S_RX_DISCARD:
			atomic receivingPacket = FALSE;
			call CSN.set();
			call SpiResource.release();	
				if (m_missed_packets == 0)
				{
					flush();
				}
				else
				{
					waitForNextPacket();
				}
				
			break;
				
	default:
			  atomic receivingPacket = FALSE;
			  call CSN.set();
			  call SpiResource.release();
			  break;
	
	
	}
  
  }
  
  
  /*
    cc2420_header_t* header = call CC2420PacketBody.getHeader( m_p_rx_buf );
    cc2420__t* metadata = call CC2420PacketBody.getMetadata( m_p_rx_buf );
    uint8_t* buf = (uint8_t*) header;
    rxFrameLength = buf[ 0 ];

    switch( m_state ) {

    case S_RX_LENGTH:
      m_state = S_RX_FCF;
      if ( rxFrameLength + 1 > m_bytes_left ) {
        // Length of this packet is bigger than the RXFIFO, flush it out.
        flush();
        
      } else {
        if ( !call FIFO.get() && !call FIFOP.get() ) {
          m_bytes_left -= rxFrameLength + 1;
        }
        
        if(rxFrameLength <= MAC_PACKET_SIZE) {
          if(rxFrameLength > 0) {
            if(rxFrameLength > SACK_HEADER_LENGTH) {
              // This packet has an FCF byte plus at least one more byte to read
              call RXFIFO.continueRead(buf + 1, SACK_HEADER_LENGTH);
              
            } else {
              // This is really a bad packet, skip FCF and get it out of here.
              m_state = S_RX_PAYLOAD;
              call RXFIFO.continueRead(buf + 1, rxFrameLength);
            }
                            
          } else {
            // Length == 0; start reading the next packet
            atomic receivingPacket = FALSE;
            call CSN.set();
            call SpiResource.release();
            waitForNextPacket();
          }
          
        } else {
          // Length is too large; we have to flush the entire Rx FIFO
          flush();
        }
      }
      break;
      
    case S_RX_FCF:
      m_state = S_RX_PAYLOAD;
      
      
       * The destination address check here is not completely optimized. If you 
       * are seeing issues with dropped acknowledgements, try removing
       * the address check and decreasing SACK_HEADER_LENGTH to 2.
       * The length byte and the FCF byte are the only two bytes required
       * to know that the packet is valid and requested an ack.  The destination
       * address is useful when we want to sniff packets from other transmitters
       * while acknowledging packets that were destined for our local address.
       *//*
      if(call CC2420Config.isAutoAckEnabled() && !call CC2420Config.isHwAutoAckDefault()) {
        if (((( header->fcf >> IEEE154_FCF_ACK_REQ ) & 0x01) == 1)
            && (header->dest == call CC2420Config.getShortAddr())
            && ((( header->fcf >> IEEE154_FCF_FRAME_TYPE ) & 7) == IEEE154_TYPE_DATA)) {
          // CSn flippage cuts off our FIFO; SACK and begin reading again
          call CSN.set();
          call CSN.clr();
          call SACK.strobe();
          call CSN.set();
          call CSN.clr();
          call RXFIFO.beginRead(buf + 1 + SACK_HEADER_LENGTH, 
              rxFrameLength - SACK_HEADER_LENGTH);
          return;
        }
		
		
      }
      
      // Didn't flip CSn, we're ok to continue reading.
      call RXFIFO.continueRead(buf + 1 + SACK_HEADER_LENGTH, 
          rxFrameLength - SACK_HEADER_LENGTH);
      break;
    
    case S_RX_PAYLOAD:
      call CSN.set();
      
      if(!m_missed_packets) {
        // Release the SPI only if there are no more frames to download
        call SpiResource.release();
      }
      
      if ( m_timestamp_size ) {
        if ( rxFrameLength > 10 ) {
          metadata->time = m_timestamp_queue[ m_timestamp_head ];
          m_timestamp_head = ( m_timestamp_head + 1 ) % TIMESTAMP_QUEUE_SIZE;
          m_timestamp_size--;
        }
      } else {
        metadata->time = 0xffff;
      }
      
      // We may have received an ack that should be processed by Transmit
      // buf[rxFrameLength] >> 7 checks the CRC
      if ( ( buf[ rxFrameLength ] >> 7 ) && rx_buf ) {
        uint8_t type = ( header->fcf >> IEEE154_FCF_FRAME_TYPE ) & 7;
       // signal CC2420Receive.receive( type, m_p_rx_buf );
        if ( type == IEEE154_TYPE_DATA ) {
          post receiveDone_task();
          return;
        }
      }
      
      waitForNextPacket();
      break;

    default:
      atomic receivingPacket = FALSE;
      call CSN.set();
      call SpiResource.release();
      break;
      
    }
    */
  }

  async event void RXFIFO.writeDone( uint8_t* tx_buf, uint8_t tx_len, error_t error ) {
  }  
  
  /***************** Tasks *****************/
  /**
   * Fill in metadata details, pass the packet up the stack, and
   * get the next packet.
   */
   /*
  task void receiveDone_task() {
    cc2420_metadata_t* metadata = call CC2420PacketBody.getMetadata( m_p_rx_buf );
    uint8_t* buf = (uint8_t*) call CC2420PacketBody.getHeader( m_p_rx_buf );;
    
    metadata->crc = buf[ rxFrameLength ] >> 7;
    metadata->rssi = buf[ rxFrameLength - 1 ];
    metadata->lqi = buf[ rxFrameLength ] & 0x7f;
    m_p_rx_buf = signal Receive.receive( m_p_rx_buf, m_p_rx_buf->data, 
                                         rxFrameLength );

    atomic receivingPacket = FALSE;
    waitForNextPacket();
  }
  */
  /****************** CC2420Config Events ****************/
  event void CC2420Config.syncDone( error_t error ) {
  }
  
  /****************** Functions ****************/
  /**
   * Attempt to acquire the SPI bus to receive a packet.
   */
   
  void beginReceive() { 
  
    m_state = S_RX_LENGTH;
    
    atomic receivingPacket = TRUE;
  
  ////printfUART("br %i\n",m_state);

	if(call SpiResource.isOwner()) {
      receive();
      
    } else if (call SpiResource.immediateRequest() == SUCCESS) {
      receive();
      
    } else {
      call SpiResource.request();
    }
  }
  
  /**
   * Flush out the Rx FIFO
   */
  void flush() {
	//printfUART("f %i\n",m_state);
    reset_state();
    call CSN.set();
    call CSN.clr();
    call SFLUSHRX.strobe();
    call SFLUSHRX.strobe();
    call CSN.set();
    call SpiResource.release();
    waitForNextPacket();
  }
  
  /**
   * The first byte of each packet is the length byte.  Read in that single
   * byte, and then read in the rest of the packet.  The CC2420 could contain
   * multiple packets that have been buffered up, so if something goes wrong, 
   * we necessarily want to flush out the FIFO unless we have to.
   */
  void receive() {
	//call Leds.led2Toggle();
  
    call CSN.clr();
    
	//call RXFIFO.beginRead( (uint8_t*)(call CC2420PacketBody.getHeader( m_p_rx_buf )), 1 );
  
	//call RXFIFO.beginRead ((uint8_t*)rxmpdu_ptr,100);
  
	//my old
	call RXFIFO.beginRead((uint8_t*)rxmpdu_ptr,1);
	
	

  
  }


  /**
   * Determine if there's a packet ready to go, or if we should do nothing
   * until the next packet arrives
   */
   
  void waitForNextPacket() {
    atomic {
      if ( m_state == S_STOPPED ) {
        call SpiResource.release();
        return;
      }
      
	   //printfUART("wn %i %i\n",m_state,m_missed_packets );
	  
	  
      atomic receivingPacket = FALSE;
	  
      if ( ( m_missed_packets && call FIFO.get() ) || !call FIFOP.get() ) {
        // A new packet is buffered up and ready to go
        if ( m_missed_packets ) {
          m_missed_packets--;
        }
        
        beginReceive();
        
      } else {
        // Wait for the next packet to arrive
		
		
		
        m_state = S_STARTED;
		
		//printfUART("wnP %i\n",m_state);
		
        m_missed_packets = 0;
        call SpiResource.release();
      }
    }
  }
 
  /**
   * Reset this component
   */
   
  void reset_state() {
    m_bytes_left = RXFIFO_SIZE;
    atomic receivingPacket = FALSE;
    //m_timestamp_head = 0;
    //m_timestamp_size = 0;
    m_missed_packets = 0;
  }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -