コード例 #1
0
void RnCheckSelfCh(BYTE channel)
{
    BYTE j =0;
    uint temp[10]={0};
    uint tempp=0;

    //printf("\r\n channel_%d ",channel);
    //APOSA
    for(j=0;j<10;j++)
    {
        while(1)
        {
            OSTimeDlyHMSM(0, 0, 0, 100);
            spiRead(channel, RIF, &tempp, WIDTH_ONE_B);
            if(tempp&0x01)//更新中断
            {
                break;
            }
        }
        spiRead(channel, POWERPA, &tempp, WIDTH_FOUR_B);
        temp[j] = tempp;    
    }
      
    tempp =MeanValue(temp,10);
    tempp =(tempp^0xffffffff)+1;
    tempp =tempp&0xffff;
    spiWrite(channel, APOSA, tempp, WIDTH_TWO_B);
    *(WORD *)&SysParam[SP_RNAPOSA+channel*2]=tempp;
    printf("#"); 
#if(EleMeterCnt==6)
    //APOSB
    for(j=0;j<10;j++)
    {
        while(1)
        {
            OSTimeDlyHMSM(0, 0, 0, 100);
            spiRead(channel, RIF, &tempp, WIDTH_ONE_B);
            if(tempp&0x01)
            {
                break;
            }
        }
        spiRead(channel, POWERPB, &tempp, WIDTH_FOUR_B);
        temp[j]=tempp;    
    }
        
    tempp =MeanValue(temp,10);
    tempp =(tempp^0xffffffff)+1;
    tempp =tempp&0xffff;
    spiWrite(channel, APOSB, tempp, WIDTH_TWO_B);
    *(WORD *)&SysParam[SP_RNAPOSB+channel*2]=tempp;
    printf("#"); 
#endif
   //rposa/rposb
    for(j=0;j<10;j++)
    {
        while(1)
        {
            OSTimeDlyHMSM(0, 0, 0, 100);
            spiRead(channel, RIF, &tempp, WIDTH_ONE_B);
            if(tempp&0x01)
            {
                break;
            }
        }
        spiRead(channel, POWERQ, &tempp, WIDTH_FOUR_B);
        temp[j]=tempp;    
    }
       
    tempp =MeanValue(temp,10);
    tempp =(tempp^0xffffffff)+1;
    tempp =tempp&0xffff;
    spiRead(channel, EMUSTATUS, &temp[0], WIDTH_THREE_B);
    if(temp[0]&0x200000)
    {
        spiWrite(channel, RPOSB, tempp, WIDTH_TWO_B);
        *(WORD *)&SysParam[SP_RNRPOSB+channel*2]=tempp;
    }
    else
    {
        spiWrite(channel, RPOSA, tempp, WIDTH_TWO_B);
        *(WORD *)&SysParam[SP_RNRPOSA+channel*2]=tempp;
    }
    printf("#"); 
    
    //iarmsos
    for(j=0;j<10;j++)
    {
        while(1)
        {
            OSTimeDlyHMSM(0, 0, 0, 100);
            spiRead(channel, RIF, &tempp, WIDTH_ONE_B);
            if(tempp&0x01)
            {
                break;
            }
        }
        spiRead(channel, IARMS, &tempp, WIDTH_THREE_B);
        temp[j] = tempp;    
    }
        
    tempp =MeanValue(temp,10);
    tempp =tempp*tempp;
    tempp =(tempp^0xffffffff)+1;
    tempp =tempp>>8;
    //tempp =tempp&0xffff;
    tempp =(tempp&0xffff)|((tempp>>8)&0x8000);
    spiWrite(channel, IARMSOS, tempp, WIDTH_TWO_B);
    *(WORD *)&SysParam[SP_RNIARMSOS+channel*2]=tempp;
    printf("#"); 
#if(EleMeterCnt==6)    
    //ibrmsos
    for(j=0;j<10;j++)
    {
        while(1)
        {
            OSTimeDlyHMSM(0, 0, 0, 100);
            spiRead(channel, RIF, &tempp, WIDTH_ONE_B);
            if(tempp&0x01)
            {
                break;
            }
        }
        spiRead(channel, IBRMS, &tempp, WIDTH_THREE_B);
        temp[j] =tempp;    
    }
        
    tempp =MeanValue(temp,10);
    tempp =tempp*tempp;
    tempp =(tempp^0xffffffff)+1;
    tempp =tempp>>8;
    //tempp =tempp&0xffff;
    tempp =(tempp&0xffff)|((tempp>>8)&0x8000);
    spiWrite(channel, IBRMSOS, tempp, WIDTH_TWO_B);
    *(WORD *)&SysParam[SP_RNIBRMSOS+channel*2]=tempp;
#endif
}
コード例 #2
0
void DeviceMemCard::safeSelectOn(void)
  {
    implSelectEnable();
    spiRead();
  }
コード例 #3
0
// C++ level interrupt handler for this instance
void RH_RF22::handleInterrupt()
{
    digitalWrite(2, HIGH);
    uint8_t _lastInterruptFlags[2];
    // Read the interrupt flags which clears the interrupt
    spiBurstRead(RH_RF22_REG_03_INTERRUPT_STATUS1, _lastInterruptFlags, 2);

#if 0
    // Caution: Serial printing in this interrupt routine can cause mysterious crashes
    Serial.print("interrupt ");
    Serial.print(_lastInterruptFlags[0], HEX);
    Serial.print(" ");
    Serial.println(_lastInterruptFlags[1], HEX);
    if (_lastInterruptFlags[0] == 0 && _lastInterruptFlags[1] == 0)
	Serial.println("FUNNY: no interrupt!");
#endif

#if 0
    // TESTING: fake an RH_RF22_IFFERROR
    static int counter = 0;
    if (_lastInterruptFlags[0] & RH_RF22_IPKSENT && counter++ == 10)
    {
	_lastInterruptFlags[0] = RH_RF22_IFFERROR;
	counter = 0;
    }
#endif

    if (_lastInterruptFlags[0] & RH_RF22_IFFERROR)
    {
	resetFifos(); // Clears the interrupt
	if (_mode == RHModeTx)
	    restartTransmit();
	else if (_mode == RHModeRx)
	    clearRxBuf();
//	Serial.println("IFFERROR");  
    }
    // Caution, any delay here may cause a FF underflow or overflow
    if (_lastInterruptFlags[0] & RH_RF22_ITXFFAEM)
    {
	// See if more data has to be loaded into the Tx FIFO 
	sendNextFragment();
//	Serial.println("ITXFFAEM");  
    }
    if (_lastInterruptFlags[0] & RH_RF22_IRXFFAFULL)
    {
	// Caution, any delay here may cause a FF overflow
	// Read some data from the Rx FIFO
	readNextFragment();
//	Serial.println("IRXFFAFULL"); 
    }
    if (_lastInterruptFlags[0] & RH_RF22_IEXT)
    {
	// This is not enabled by the base code, but users may want to enable it
	handleExternalInterrupt();
//	Serial.println("IEXT"); 
    }
    if (_lastInterruptFlags[1] & RH_RF22_IWUT)
    {
	// This is not enabled by the base code, but users may want to enable it
	handleWakeupTimerInterrupt();
//	Serial.println("IWUT"); 
    }
    if (_lastInterruptFlags[0] & RH_RF22_IPKSENT)
    {
//	Serial.println("IPKSENT");   
	_txGood++; 
	// Transmission does not automatically clear the tx buffer.
	// Could retransmit if we wanted
	// RH_RF22 transitions automatically to Idle
	_mode = RHModeIdle;
    }
    if (_lastInterruptFlags[0] & RH_RF22_IPKVALID)
    {
	uint8_t len = spiRead(RH_RF22_REG_4B_RECEIVED_PACKET_LENGTH);
//	Serial.println("IPKVALID");   

	// May have already read one or more fragments
	// Get any remaining unread octets, based on the expected length
	// First make sure we dont overflow the buffer in the case of a stupid length
	// or partial bad receives
	if (   len >  RH_RF22_MAX_MESSAGE_LEN
	    || len < _bufLen)
	{
	    _rxBad++;
	    _mode = RHModeIdle;
	    clearRxBuf();
	    return; // Hmmm receiver buffer overflow. 
	}

	spiBurstRead(RH_RF22_REG_7F_FIFO_ACCESS, _buf + _bufLen, len - _bufLen);
	_rxHeaderTo = spiRead(RH_RF22_REG_47_RECEIVED_HEADER3);
	_rxHeaderFrom = spiRead(RH_RF22_REG_48_RECEIVED_HEADER2);
	_rxHeaderId = spiRead(RH_RF22_REG_49_RECEIVED_HEADER1);
	_rxHeaderFlags = spiRead(RH_RF22_REG_4A_RECEIVED_HEADER0);
	_rxGood++;
	_bufLen = len;
	_mode = RHModeIdle;
	_rxBufValid = true;
    }
    if (_lastInterruptFlags[0] & RH_RF22_ICRCERROR)
    {
//	Serial.println("ICRCERR");  
	_rxBad++;
	clearRxBuf();
	resetRxFifo();
	_mode = RHModeIdle;
	setModeRx(); // Keep trying
    }
    if (_lastInterruptFlags[1] & RH_RF22_IPREAVAL)
    {
//	Serial.println("IPREAVAL");  
	_lastRssi = (int8_t)(-(spiRead(RH_RF22_REG_26_RSSI) / 2));
	_lastPreambleTime = millis();
	resetRxFifo();
	clearRxBuf();
    }
}
コード例 #4
0
uint8_t RH_RF22::rssiRead()
{
    return spiRead(RH_RF22_REG_26_RSSI);
}
bool RH_RF22::init()
{
    if (!RHSPIDriver::init())
	return false;
    // Determine the interrupt number that corresponds to the interruptPin
    int interruptNumber = digitalPinToInterrupt(_interruptPin);
    if (interruptNumber == NOT_AN_INTERRUPT)
	return false;

    // Software reset the device
    reset();

    // Get the device type and check it
    // This also tests whether we are really connected to a device
    _deviceType = spiRead(RH_RF22_REG_00_DEVICE_TYPE);
    if (   _deviceType != RH_RF22_DEVICE_TYPE_RX_TRX
        && _deviceType != RH_RF22_DEVICE_TYPE_TX)
    {
	return false;
    }

    // Add by Adrien van den Bossche <*****@*****.**> for Teensy
    // ARM M4 requires the below. else pin interrupt doesn't work properly.
    // On all other platforms, its innocuous, belt and braces
    pinMode(_interruptPin, INPUT); 

    // Enable interrupt output on the radio. Interrupt line will now go high until
    // an interrupt occurs
    spiWrite(RH_RF22_REG_05_INTERRUPT_ENABLE1, RH_RF22_ENTXFFAEM | RH_RF22_ENRXFFAFULL | RH_RF22_ENPKSENT | RH_RF22_ENPKVALID | RH_RF22_ENCRCERROR | RH_RF22_ENFFERR);
    spiWrite(RH_RF22_REG_06_INTERRUPT_ENABLE2, RH_RF22_ENPREAVAL);

    // Set up interrupt handler
    // Since there are a limited number of interrupt glue functions isr*() available,
    // we can only support a limited number of devices simultaneously
    // On some devices, notably most Arduinos, the interrupt pin passed in is actually the 
    // interrupt number. You have to figure out the interruptnumber-to-interruptpin mapping
    // yourself based on knowledge of what Arduino board you are running on.
    _deviceForInterrupt[_interruptCount] = this;
    if (_interruptCount == 0)
	attachInterrupt(interruptNumber, isr0, FALLING);
    else if (_interruptCount == 1)
	attachInterrupt(interruptNumber, isr1, FALLING);
    else if (_interruptCount == 2)
	attachInterrupt(interruptNumber, isr2, FALLING);
    else
	return false; // Too many devices, not enough interrupt vectors
    _interruptCount++;

    setModeIdle();

    clearTxBuf();
    clearRxBuf();
  
    // Most of these are the POR default
    spiWrite(RH_RF22_REG_7D_TX_FIFO_CONTROL2, RH_RF22_TXFFAEM_THRESHOLD);
    spiWrite(RH_RF22_REG_7E_RX_FIFO_CONTROL,  RH_RF22_RXFFAFULL_THRESHOLD);
    spiWrite(RH_RF22_REG_30_DATA_ACCESS_CONTROL, RH_RF22_ENPACRX | RH_RF22_ENPACTX | RH_RF22_ENCRC | (_polynomial & RH_RF22_CRC));

    // Configure the message headers
    // Here we set up the standard packet format for use by the RH_RF22 library
    // 8 nibbles preamble
    // 2 SYNC words 2d, d4
    // Header length 4 (to, from, id, flags)
    // 1 octet of data length (0 to 255)
    // 0 to 255 octets data
    // 2 CRC octets as CRC16(IBM), computed on the header, length and data
    // On reception the to address is check for validity against RH_RF22_REG_3F_CHECK_HEADER3
    // or the broadcast address of 0xff
    // If no changes are made after this, the transmitted
    // to address will be 0xff, the from address will be 0xff
    // and all such messages will be accepted. This permits the out-of the box
    // RH_RF22 config to act as an unaddresed, unreliable datagram service
    spiWrite(RH_RF22_REG_32_HEADER_CONTROL1, RH_RF22_BCEN_HEADER3 | RH_RF22_HDCH_HEADER3);
    spiWrite(RH_RF22_REG_33_HEADER_CONTROL2, RH_RF22_HDLEN_4 | RH_RF22_SYNCLEN_2);

    setPreambleLength(8);
    uint8_t syncwords[] = { 0x2d, 0xd4 };
    setSyncWords(syncwords, sizeof(syncwords));
    setPromiscuous(false); 

    // Set some defaults. An innocuous ISM frequency, and reasonable pull-in
    setFrequency(434.0, 0.05);
//    setFrequency(900.0);
    // Some slow, reliable default speed and modulation
    setModemConfig(FSK_Rb2_4Fd36);
//    setModemConfig(FSK_Rb125Fd125);
    setGpioReversed(false);
    // Lowish power
    setTxPower(RH_RF22_TXPOW_8DBM);

    return true;
}
コード例 #6
0
ファイル: RH_RF95.cpp プロジェクト: assafsinger/arduino
bool RH_RF95::init()
{
    if (!RHSPIDriver::init())
	return false;

    // Determine the interrupt number that corresponds to the interruptPin
    int interruptNumber = digitalPinToInterrupt(_interruptPin);
    if (interruptNumber == NOT_AN_INTERRUPT)
	return false;
#ifdef RH_ATTACHINTERRUPT_TAKES_PIN_NUMBER
    interruptNumber = _interruptPin;
#endif

    // Tell the low level SPI interface we will use SPI within this interrupt
    spiUsingInterrupt(interruptNumber);

    // No way to check the device type :-(
    
    // Set sleep mode, so we can also set LORA mode:
    spiWrite(RH_RF95_REG_01_OP_MODE, RH_RF95_MODE_SLEEP | RH_RF95_LONG_RANGE_MODE);
    delay(10); // Wait for sleep mode to take over from say, CAD
    // Check we are in sleep mode, with LORA set
    if (spiRead(RH_RF95_REG_01_OP_MODE) != (RH_RF95_MODE_SLEEP | RH_RF95_LONG_RANGE_MODE))
    {
//	Serial.println(spiRead(RH_RF95_REG_01_OP_MODE), HEX);
	return false; // No device present?
    }

    // Add by Adrien van den Bossche <*****@*****.**> for Teensy
    // ARM M4 requires the below. else pin interrupt doesn't work properly.
    // On all other platforms, its innocuous, belt and braces
    pinMode(_interruptPin, INPUT); 

    // Set up interrupt handler
    // Since there are a limited number of interrupt glue functions isr*() available,
    // we can only support a limited number of devices simultaneously
    // ON some devices, notably most Arduinos, the interrupt pin passed in is actuallt the 
    // interrupt number. You have to figure out the interruptnumber-to-interruptpin mapping
    // yourself based on knwledge of what Arduino board you are running on.
    if (_myInterruptIndex == 0xff)
    {
	// First run, no interrupt allocated yet
	if (_interruptCount <= RH_RF95_NUM_INTERRUPTS)
	    _myInterruptIndex = _interruptCount++;
	else
	    return false; // Too many devices, not enough interrupt vectors
    }
    _deviceForInterrupt[_myInterruptIndex] = this;
    if (_myInterruptIndex == 0)
	attachInterrupt(interruptNumber, isr0, RISING);
    else if (_myInterruptIndex == 1)
	attachInterrupt(interruptNumber, isr1, RISING);
    else if (_myInterruptIndex == 2)
	attachInterrupt(interruptNumber, isr2, RISING);
    else
	return false; // Too many devices, not enough interrupt vectors

    // Set up FIFO
    // We configure so that we can use the entire 256 byte FIFO for either receive
    // or transmit, but not both at the same time
    spiWrite(RH_RF95_REG_0E_FIFO_TX_BASE_ADDR, 0);
    spiWrite(RH_RF95_REG_0F_FIFO_RX_BASE_ADDR, 0);

    // Packet format is preamble + explicit-header + payload + crc
    // Explicit Header Mode
    // payload is TO + FROM + ID + FLAGS + message data
    // RX mode is implmented with RXCONTINUOUS
    // max message data length is 255 - 4 = 251 octets

    setModeIdle();

    // Set up default configuration
    // No Sync Words in LORA mode.
    setModemConfig(Bw125Cr45Sf128); // Radio default
//    setModemConfig(Bw125Cr48Sf4096); // slow and reliable?
    setPreambleLength(8); // Default is 8
    // An innocuous ISM frequency, same as RF22's
    setFrequency(434.0);
    // Lowish power
    setTxPower(13);

    return true;
}
コード例 #7
0
ファイル: RF22.cpp プロジェクト: ykiveish/rfm22b-galileo
// C++ level interrupt handler for this instance
void RF22::handleInterrupt()
{
    uint8_t _lastInterruptFlags[2];
    // Read the interrupt flags which clears the interrupt
    spiBurstRead(RF22_REG_03_INTERRUPT_STATUS1, _lastInterruptFlags, 2);

    if (_lastInterruptFlags[0] & RF22_IFFERROR)
    { 
		resetFifos(); // Clears the interrupt
		if (_mode == RF22_MODE_TX)
			restartTransmit();
		else if (_mode == RF22_MODE_RX)
			clearRxBuf();
    }
    // Caution, any delay here may cause a FF underflow or overflow
    if (_lastInterruptFlags[0] & RF22_ITXFFAEM)
    {
		// See if more data has to be loaded into the Tx FIFO 
		sendNextFragment();
    }
    if (_lastInterruptFlags[0] & RF22_IRXFFAFULL)
    {
		// Caution, any delay here may cause a FF overflow
		// Read some data from the Rx FIFO
		readNextFragment(); 
    }
    if (_lastInterruptFlags[0] & RF22_IEXT)
    {
		// This is not enabled by the base code, but users may want to enable it
		handleExternalInterrupt();
    }
    if (_lastInterruptFlags[1] & RF22_IWUT)
    {
		// This is not enabled by the base code, but users may want to enable it
		handleWakeupTimerInterrupt();
    }
    if (_lastInterruptFlags[0] & RF22_IPKSENT)
    {
		_txGood++; 
		// Transmission does not automatically clear the tx buffer.
		// Could retransmit if we wanted
		// RF22 transitions automatically to Idle
		_mode = RF22_MODE_IDLE;
    }
    if (_lastInterruptFlags[0] & RF22_IPKVALID)
    {
		uint8_t len = spiRead(RF22_REG_4B_RECEIVED_PACKET_LENGTH);

		// May have already read one or more fragments
		// Get any remaining unread octets, based on the expected length
		// First make sure we dont overflow the buffer in the case of a stupid length
		// or partial bad receives
		if (   len >  RF22_MAX_MESSAGE_LEN
			|| len < _bufLen)
		{
			_rxBad++;
			_mode = RF22_MODE_IDLE;
			clearRxBuf();
			return; // Hmmm receiver buffer overflow. 
		}

		spiBurstRead(RF22_REG_7F_FIFO_ACCESS, _buf + _bufLen, len - _bufLen);
		_rxGood++;
		_bufLen = len;
		_mode = RF22_MODE_IDLE;
		_rxBufValid = true;
    }
    if (_lastInterruptFlags[0] & RF22_ICRCERROR)
    {
		_rxBad++;
		clearRxBuf();
		resetRxFifo();
		_mode = RF22_MODE_IDLE;
		setModeRx(); // Keep trying
    }
    if (_lastInterruptFlags[1] & RF22_IPREAVAL)
    {
		_lastRssi = spiRead(RF22_REG_26_RSSI);
		clearRxBuf();
    }
}
コード例 #8
0
ファイル: MPU6000.cpp プロジェクト: LeonSKennedy/CODE
void MPU6000::read() {
    sensors.accXByteH = spiRead(ACCEL_XOUT_H);
    sensors.accXByteL = spiRead(ACCEL_XOUT_L);
    sensors.accYByteH = spiRead(ACCEL_YOUT_H);
    sensors.accYByteL = spiRead(ACCEL_YOUT_L);
    sensors.accZByteH = spiRead(ACCEL_ZOUT_H);
    sensors.accZByteL = spiRead(ACCEL_ZOUT_L);
    sensors.gyrXByteH = spiRead(GYRO_XOUT_H);
    sensors.gyrXByteL = spiRead(GYRO_XOUT_L);
    sensors.gyrYByteH = spiRead(GYRO_YOUT_H);
    sensors.gyrYByteL = spiRead(GYRO_YOUT_L);
    sensors.gyrZByteH = spiRead(GYRO_ZOUT_H);
    sensors.gyrZByteL = spiRead(GYRO_ZOUT_L);
}
コード例 #9
0
ファイル: dvkreg.c プロジェクト: makr0900/sentio-framework
/**************************************************************************//**
 * @brief  Perform read from DVK board control register
 * @param  addr Address of register to read from
 *****************************************************************************/
uint16_t DVK_readRegister(uint16_t *addr)
{
  uint16_t data;

  if (addr != lastAddr)
  {
    spiWrite(0x00, 0xFFFF & ((uint32_t) addr));             /*LSBs of address*/
    spiWrite(0x01, 0xFF & ((uint32_t) addr >> 16));         /*MSBs of address*/
    spiWrite(0x02, (0x0C000000 & (uint32_t) addr) >> 26);   /*Chip select*/
  }
  /* Read twice */
  data     = spiRead(0x03, 0);
  data     = spiRead(0x03, 0);
  lastAddr = addr;
  return data;
}

/**************************************************************************//**
 * @brief  Perform write to DVK board control register
 * @param addr Address of register to write to
 * @param data 16-bit to  write into register
 *****************************************************************************/
void DVK_writeRegister(uint16_t *addr, uint16_t data)
{
  if (addr != lastAddr)
  {
    spiWrite(0x00, 0xFFFF & ((uint32_t) addr));             /*LSBs of address*/
コード例 #10
0
ファイル: RF22.cpp プロジェクト: blackbluegl/ambientlight
bool RF22::init() {
	//TODO check.
	wiringPiSetup();

//	// Wait for RF22 POR (up to 16msec)
//	delay(16);

	if (wiringPiSPISetup(_slaveSelectPin, 1000000) == -1) {
		printf("Could not initialize SPI\n");
		return false;
	}

	delay(20);

	// Software reset the device
	reset();
	delay(1);

	// Get the device type and check it
	// This also tests whether we are really connected to a device
	_deviceType = spiRead(RF22_REG_00_DEVICE_TYPE);
	if (_deviceType != RF22_DEVICE_TYPE_RX_TRX && _deviceType != RF22_DEVICE_TYPE_TX)
		return false;

	// Set up interrupt handler
	if (_interrupt == 0) {
		_RF22ForInterrupt[0] = this;
		//attaching interrupt to gpio pins. see wiringPi.com -> gpio chart
		wiringPiISR(6, INT_EDGE_FALLING, RF22::isr0);
	} else if (_interrupt == 1) {
		_RF22ForInterrupt[1] = this;
		wiringPiISR(4, INT_EDGE_FALLING, RF22::isr1);
	} else
		return false;

	clearTxBuf();
	clearRxBuf();

	// Most of these are the POR default
	spiWrite(RF22_REG_7D_TX_FIFO_CONTROL2, RF22_TXFFAEM_THRESHOLD);
	spiWrite(RF22_REG_7E_RX_FIFO_CONTROL, RF22_RXFFAFULL_THRESHOLD);
	spiWrite(RF22_REG_30_DATA_ACCESS_CONTROL, RF22_ENPACRX | RF22_ENPACTX | RF22_ENCRC | RF22_CRC_CRC_16_IBM);
	// Configure the message headers
	// Here we set up the standard packet format for use by the RF22 library
	// 8 nibbles preamble
	// 2 SYNC words 2d, d4
	// Header length 4 (to, from, id, flags)
	// 1 octet of data length (0 to 255)
	// 0 to 255 octets data
	// 2 CRC octets as CRC16(IBM), computed on the header, length and data
	// On reception the to address is check for validity against RF22_REG_3F_CHECK_HEADER3
	// or the broadcast address of 0xff
	// If no changes are made after this, the transmitted
	// to address will be 0xff, the from address will be 0xff
	// and all such messages will be accepted. This permits the out-of the box
	// RF22 config to act as an unaddresed, unreliable datagram service
	spiWrite(RF22_REG_32_HEADER_CONTROL1, RF22_BCEN_HEADER3 | RF22_HDCH_HEADER3);
	spiWrite(RF22_REG_33_HEADER_CONTROL2, RF22_HDLEN_4 | RF22_SYNCLEN_2 | 0x1);
	setPreambleLength(8);
	uint8_t syncwords[] = { 0x2d, 0xd4 };
	setSyncWords(syncwords, sizeof(syncwords));
	setPromiscuous(false);
	// Check the TO header against RF22_DEFAULT_NODE_ADDRESS
	spiWrite(RF22_REG_3F_CHECK_HEADER3, RF22_DEFAULT_NODE_ADDRESS);
	// Set the default transmit header values
	setHeaderTo(RF22_DEFAULT_NODE_ADDRESS);
	setHeaderFrom(RF22_DEFAULT_NODE_ADDRESS);
	setHeaderId(0);
	setHeaderFlags(0);

	// Ensure the antenna can be switched automatically according to transmit and receive
	// This assumes GPIO0(out) is connected to TX_ANT(in) to enable tx antenna during transmit
	// This assumes GPIO1(out) is connected to RX_ANT(in) to enable rx antenna during receive
	spiWrite(RF22_REG_0B_GPIO_CONFIGURATION0, 0x12); // TX state
	spiWrite(RF22_REG_0C_GPIO_CONFIGURATION1, 0x15); // RX state
	//spiWrite(RF22_REG_0D_GPIO_CONFIGURATION2, 0x14); //raw data to port 4 have to activate it later?
	//put into a mode where the interrupt frequency is not as high as the clock of the rfm22.
	//the raspberry is connected to that pin and would freeze if an isr is registered.
	spiWrite(RF22_REG_0D_GPIO_CONFIGURATION2, 0x13);
	// Enable interrupts
	spiWrite(RF22_REG_05_INTERRUPT_ENABLE1,
			RF22_ENTXFFAEM | RF22_ENRXFFAFULL | RF22_ENPKSENT | RF22_ENPKVALID | RF22_ENCRCERROR | RF22_ENFFERR);
	spiWrite(RF22_REG_06_INTERRUPT_ENABLE2, RF22_ENPREAVAL);

	// Set some defaults. An innocuous ISM frequency, and reasonable pull-in
	setFrequency(434.0, 0.05);
//    setFrequency(900.0);
	// Some slow, reliable default speed and modulation
	setModemConfig(FSK_Rb2_4Fd36);
//    setModemConfig(FSK_Rb125Fd125);
	// Minimum power
	setTxPower(RF22_TXPOW_8DBM);
//    setTxPower(RF22_TXPOW_17DBM);

	return true;
}
コード例 #11
0
void Check_KPA_KPB(BYTE channel,uint datp)
{
    BYTE j =0;
    BYTE i =0;

    uint temp =0;
    uint temp1[10]={0};    
    uint len=10;
	unsigned long long dbuf=0;
	
    for(j=0;j<len;j++)
    {
        while(1)
        {
            OSTimeDlyHMSM(0, 0, 0, 150);
            spiRead(channel, RIF, &temp, WIDTH_ONE_B);
            if(temp&0x01)
            {
                break;
            }
        }
        spiRead(channel, POWERPA, &temp, WIDTH_FOUR_B);
        if(temp < 0x80000000)//大于 0x80000000 时为负
        {
            temp1[i] = temp;
            i++;
        }
        temp = 0;
    }
    
    temp = MeanValue(temp1,i);
    dbuf = datp;//避免数据溢出
    
    *(WORD *)&SysParam[SP_RNKAP+2*channel] = dbuf*1000000/temp;//最小单位为0.1W 来计算的K
    i =0;
    printf("#");

    /*
    for(j=0;j<len;j++)
    {
        while(1)
        {
            OSTimeDlyHMSM(0, 0, 0, 100);
            spiRead(channel, RIF, &temp, WIDTH_ONE_B);
            if(temp&0x01)
            {
                break;
            }
        }
        spiRead(channel, POWERPB, &temp, WIDTH_FOUR_B);
        if(tempp < 0x80000000)//大于 0x80000000 时为负
        {
            temp1[i] = temp;
            i++;
        }
        temp = 0;
    }
    
    tempp = MeanValue(temp1,i);
    *(WORD *)&SysParam[SP_RNKBP+2*channel] = datp*1000000/tempp; //最小单位0.1W 来计算的K
    printf("*");*/
}
コード例 #12
0
void Check_KIA_KIB(BYTE channel,uint dati)
{
    BYTE j =0;
    BYTE i =0;
    uint temp=0;
    uint temp1[10]={0};
    uint len=10;
    unsigned long long dbuf = 0;
    
    for(j=0;j<len;j++)
    {
        while(1)
        {
            OSTimeDlyHMSM(0, 0, 0, 150);
            spiRead(channel, RIF, &temp, WIDTH_ONE_B);
            if(temp&0x01)
            {
                break;
            }
        }
        spiRead(channel, IARMS, &temp, WIDTH_THREE_B);
        if(temp < 0x800000)//大于 0x800000 时做零处理
        {
            temp1[i] = temp;
            i++;
        }
        temp = 0;
    }
    
    temp = MeanValue(temp1,i);
    dbuf = dati;
    
    *(WORD *)&SysParam[SP_RNKIA+2*channel] = dbuf*10000/temp; // 最小单位为1mA来计算的K
    i =0;
    printf("#");   
#if(EleMeterCnt==6) 
    for(j=0;j<len;j++)
    {
        while(1)
        {
            OSTimeDlyHMSM(0, 0, 0, 150);
            spiRead(channel, RIF, &temp, WIDTH_ONE_B);
            if(temp&0x01)
            {
                break;
            }
        }
        spiRead(channel, IBRMS, &temp, WIDTH_THREE_B);
        if(temp < 0x800000)//大于 0x800000 时做零处理
        {
            temp1[i] = temp;
            i++;
        }
        temp = 0;
    }
    
    tempp = MeanValue(temp1,i);
    *(WORD *)&SysParam[SP_RNKIB+2*channel] = dati*10000/tempp;
    printf("#");
#endif
 
}
コード例 #13
0
void taskPriod ( void *pParam )
{
 
   static unsigned long long data = 0;
   
   static uint Flag = 0;    
   static uint tempAi[9] ={0};
//   static uint tempBi[9] ={0};
   static uint tempU[9]  ={0};
   static uint tempEE[9] ={0};
   static uint tempPA[9] ={0};
//   static uint tempPB[9] ={0};

//   static ulong tempQE[9] ={0};
   static BYTE channel =0;
   static BYTE i=0;
   static BYTE count=0;
   printf ( "\r\nPriod task begin..." );

    while ( 1 )
    {
        
        OSTimeDly ( OS_TICKS_PER_SEC*2); 
        updatePeriodChk();
        ReadRealtime();
        count++;
        
        for(channel=0;channel<9;channel++)
        {
            spiRead(channel, IARMS, (uint *)&data, WIDTH_THREE_B);
            //printf("IARMS=%d\r\n",*(uint *)&data);
            if(data >= 0x800000)
            {
                data = 0;
                tempAi[channel]= 0;
            }
            else
            {
                tempAi[channel]= (uint)(((*(WORD *)&SysParam[SP_RNKIA+2*channel])*data)/10000+1);// 0.1mA   补偿0.1mA
            }
            #if(EleMeterCnt==6) 
            spiRead(channel, IBRMS, &data, WIDTH_THREE_B);
            if(data >= 0x800000)
            {
                data = 0;
                tempBi[channel] =0;
            }
            else
            {
                tempBi[channel] = ((*(WORD *)&SysParam[SP_RNKIB+2*channel]) * data)/10000+1;// 0.1mA 补偿0.1mA
            }
            #endif
            spiRead(channel, URMS, (uint *)&data, WIDTH_THREE_B);
            //printf("URMS=%d\r\n",*(uint *)&data);
            if((data >= 0x800000)||(data < 0x24B))
            {    
                data = 0;
            }
        
            tempU[channel] = (uint)(((*(WORD *)&SysParam[SP_RNKU+2*channel])* data)/1000000);// 0.1V
            
            //A通道功率
            spiRead(channel, POWERPA, (uint *)&data, WIDTH_FOUR_B);
            //printf("POWERPA=%d\r\n",*(uint *)&data);
            if(data>0x80000000)//负数
            {   
                spiRead(channel, EMUSTATUS, &Flag, WIDTH_THREE_B);
                if(Flag&0x20000)//REVP=1
                {
                    data = (data-1)^0x8fffffff;
                    data = data&0x7fffffff;
                }
                else
                {
                    data =0;    
                }
        
             }
            
            tempPA[channel] = (uint)(((*(WORD *)&SysParam[SP_RNKAP+2*channel])*data)/1000000);// 0.1W
        
            
            #if(EleMeterCnt==6)            
            spiRead(channel, POWERPB, (uint *)&data, WIDTH_FOUR_B);
            
            if(data>0x80000000)//负数
            {
                spiRead(channel, EMUSTATUS, &Flag, WIDTH_THREE_B);
                if(Flag&0x20000)//REVP=1
                {
                    data = (data-1)^0x8fffffff;
                }
                else
                {
                    data =0;    
                }
            }
        
            tempPB[channel] = ((*(WORD*)&SysParam[SP_RNKBP+2*channel])*data)/1000000;
            #endif
            #if 0
            //无功功率
            spiRead(channel, POWERQ, &data, WIDTH_FOUR_B);
            if(data>0x80000000)//负数
            {
                spiRead(channel, EMUSTATUS, &Flag, WIDTH_THREE_B);
                if(Flag&0x40000)//REVQ=1
                {
                    data = (data-1)^0x8fffffff;
                }
                else
                {
                    data =0;    
                }
            }
            #endif
            //读中断标志位
            spiRead(channel, RIF, (uint *)&data, WIDTH_ONE_B);
            if(data & RN_RPEOIF)//溢出中断
            {
                printf("overflow=%d\n",channel);
                
                *(DWORD *)&EEPROMPARAM[EEPROM_ENERGY+4*channel]= 0;
                EEPROMPARAM[EEPROM_OVERFLOW+channel] +=1;
                Write_FM24C(FMSLAVE_ADDR, EEPROM_OVERFLOWADDR+channel,&EEPROMPARAM[EEPROM_OVERFLOW+channel], 1);
            }
            
            spiRead(channel, ENERGYP, (uint *)&data, WIDTH_THREE_B);//
            
            *(DWORD *)&EEPROMPARAM[EEPROM_ENERGYLAST+4*channel] = data;
            
            data += *(DWORD *)&EEPROMPARAM[EEPROM_ENERGY+4*channel] ;
            
            if(count == 5)//10s
            {
                if(channel == 8)
                {
                    count = 0;
                } 
                Write_FM24C(FMSLAVE_ADDR, EEPROM_ENERGY+4*channel,(BYTE *)&data, 4);
                Write_FM24C(FMSLAVE_ADDR, EEPROM_ENERGYLAST+4*channel,(BYTE *)&EEPROMPARAM[EEPROM_ENERGYLAST+4*channel], 4);
            }
            tempEE[channel]=(data+EEPROMPARAM[EEPROM_OVERFLOW + channel]*0xffffff)/(EC/100);    
          
        }
        #if(EleMeterCnt==3)

        for(i=0;i<3;i++)
        {
            EMeterDat[i].Ai = tempAi[i]/10;//mA
            EMeterDat[i].Bi = tempAi[i+3]/10;
            EMeterDat[i].Ci = tempAi[i+6]/10;

            EMeterDat[i].Au = (tempU[0]+tempU[1]+tempU[2])/3;//100mV  0.1V
            EMeterDat[i].Bu = (tempU[3]+tempU[4]+tempU[5])/3;
            EMeterDat[i].Cu = (tempU[6]+tempU[7]+tempU[8])/3;
                                                                                            
            EMeterDat[i].AE = tempEE[i];// 0.01kWh,电表常数3200
            EMeterDat[i].BE = tempEE[i+3];
            EMeterDat[i].CE = tempEE[i+6];
            
            EMeterDat[i].PWRA = tempPA[i];//功率
            EMeterDat[i].PWRB = tempPA[i+3];
            EMeterDat[i].PWRC = tempPA[i+6];
            
            EMeterDat[i].PWR = tempPA[0+i]+tempPA[i+3]+tempPA[i+6];
            
            EMeterDat[i].EE = EMeterDat[i].AE+EMeterDat[i].BE+EMeterDat[i].CE;// 0.01kwh
                
        }
        #else          
        for(i=0;i<6;i++)
        {
            if((i%2)==0)
            {
                EMeterDat[i].Ai=tempAi[i/2]/10;//mA
                EMeterDat[i].Bi=tempAi[i/2+3]/10;
                EMeterDat[i].Ci=tempAi[i/2+6]/10;
            }
            else
            {
                EMeterDat[i].Ai=tempBi[i/2]/10;//mA
                EMeterDat[i].Bi=tempBi[i/2+3]/10;
                EMeterDat[i].Ci=tempBi[i/2+6]/10;
            }

            EMeterDat[i].Au=(tempU[0]+tempU[1]+tempU[2])/3;//100mV  0.1V
            EMeterDat[i].Bu=(tempU[3]+tempU[4]+tempU[5])/3;
            EMeterDat[i].Cu=(tempU[6]+tempU[7]+tempU[8])/3;

            if((i%2)==0)
            {
                EMeterDat[i].AE=tempEE[i/2];//0.01kwh 
                EMeterDat[i].BE=tempEE[i/2+3];
                EMeterDat[i].CE=tempEE[i/2+6];
            }
            else
            {
                EMeterDat[i].AE=EEB[i/2];//0.01kwh 
                EMeterDat[i].BE=EEB[i/2+3];
                EMeterDat[i].CE=EEB[i/2+6];
            }

            EMeterDat[i].EE=EMeterDat[i].AE+EMeterDat[i].BE+EMeterDat[i].CE;// 0.01kwh   
                
        }  
        #endif
    }
}
コード例 #14
0
void DeviceMemCard::safeSelectOff(void)
  {
    spiRead();
    implSelectDisable();
    spiRead();
  }
コード例 #15
0
ファイル: RF22.cpp プロジェクト: ykiveish/rfm22b-galileo
uint8_t RF22::headerTo()
{
    return spiRead(RF22_REG_47_RECEIVED_HEADER3);
}
コード例 #16
0
ファイル: RH_RF69.cpp プロジェクト: Phyma/RadioHead
bool RH_RF69::init()
{
    if (!RHSPIDriver::init())
	return false;

    // Determine the interrupt number that corresponds to the interruptPin
    int interruptNumber = digitalPinToInterrupt(_interruptPin);
    if (interruptNumber == NOT_AN_INTERRUPT)
	return false;
#ifdef RH_ATTACHINTERRUPT_TAKES_PIN_NUMBER
    interruptNumber = _interruptPin;
#endif

    // Get the device type and check it
    // This also tests whether we are really connected to a device
    // My test devices return 0x24
    _deviceType = spiRead(RH_RF69_REG_10_VERSION);
    if (_deviceType == 00 ||
	_deviceType == 0xff)
	return false;

    // Add by Adrien van den Bossche <*****@*****.**> for Teensy
    // ARM M4 requires the below. else pin interrupt doesn't work properly.
    // On all other platforms, its innocuous, belt and braces
    pinMode(_interruptPin, INPUT); 

    // Set up interrupt handler
    // Since there are a limited number of interrupt glue functions isr*() available,
    // we can only support a limited number of devices simultaneously
    // ON some devices, notably most Arduinos, the interrupt pin passed in is actuallt the 
    // interrupt number. You have to figure out the interruptnumber-to-interruptpin mapping
    // yourself based on knwledge of what Arduino board you are running on.
    if (_myInterruptIndex == 0xff)
    {
	// First run, no interrupt allocated yet
	if (_interruptCount <= RH_RF69_NUM_INTERRUPTS)
	    _myInterruptIndex = _interruptCount++;
	else
	    return false; // Too many devices, not enough interrupt vectors
    }
    _deviceForInterrupt[_myInterruptIndex] = this;
    if (_myInterruptIndex == 0)
	attachInterrupt(interruptNumber, isr0, RISING);
    else if (_myInterruptIndex == 1)
	attachInterrupt(interruptNumber, isr1, RISING);
    else if (_myInterruptIndex == 2)
	attachInterrupt(interruptNumber, isr2, RISING);
    else
	return false; // Too many devices, not enough interrupt vectors

    setModeIdle();

    // Configure important RH_RF69 registers
    // Here we set up the standard packet format for use by the RH_RF69 library:
    // 4 bytes preamble
    // 2 SYNC words 2d, d4
    // 2 CRC CCITT octets computed on the header, length and data (this in the modem config data)
    // 0 to 60 bytes data
    // RSSI Threshold -114dBm
    // We dont use the RH_RF69s address filtering: instead we prepend our own headers to the beginning
    // of the RH_RF69 payload
    spiWrite(RH_RF69_REG_3C_FIFOTHRESH, RH_RF69_FIFOTHRESH_TXSTARTCONDITION_NOTEMPTY | 0x0f); // thresh 15 is default
    // RSSITHRESH is default
//    spiWrite(RH_RF69_REG_29_RSSITHRESH, 220); // -110 dbM
    // SYNCCONFIG is default. SyncSize is set later by setSyncWords()
//    spiWrite(RH_RF69_REG_2E_SYNCCONFIG, RH_RF69_SYNCCONFIG_SYNCON); // auto, tolerance 0
    // PAYLOADLENGTH is default
//    spiWrite(RH_RF69_REG_38_PAYLOADLENGTH, RH_RF69_FIFO_SIZE); // max size only for RX
    // PACKETCONFIG 2 is default 
    spiWrite(RH_RF69_REG_6F_TESTDAGC, RH_RF69_TESTDAGC_CONTINUOUSDAGC_IMPROVED_LOWBETAOFF);
    // If high power boost set previously, disable it
    spiWrite(RH_RF69_REG_5A_TESTPA1, RH_RF69_TESTPA1_NORMAL);
    spiWrite(RH_RF69_REG_5C_TESTPA2, RH_RF69_TESTPA2_NORMAL);

    // The following can be changed later by the user if necessary.
    // Set up default configuration
    uint8_t syncwords[] = { 0x2d, 0xd4 };
    setSyncWords(syncwords, sizeof(syncwords)); // Same as RF22's
    // Reasonably fast and reliable default speed and modulation
    setModemConfig(GFSK_Rb250Fd250);

    // 3 would be sufficient, but this is the same as RF22's
    setPreambleLength(4);
    // An innocuous ISM frequency, same as RF22's
    setFrequency(434.0);
    // No encryption
    setEncryptionKey(NULL);
    // +13dBm, same as power-on default
    setTxPower(13); 

    return true;
}
コード例 #17
0
ファイル: RF22.cpp プロジェクト: ykiveish/rfm22b-galileo
uint8_t RF22::headerFrom()
{
    return spiRead(RF22_REG_48_RECEIVED_HEADER2);
}
コード例 #18
0
ファイル: RH_RF95.cpp プロジェクト: x893/LoRa
bool RH_RF95::init()
{
	bool result = false;

	if (_resetPin != PIN_UNUSED)
		pinMode(_resetPin, INPUT);

	if (_powerPin != PIN_UNUSED)
	{
		pinMode(_powerPin, OUTPUT);
		digitalWrite(_powerPin, HIGH);

		if (_resetPin != PIN_UNUSED)
			while (digitalRead(_resetPin) != HIGH)
				;
		delay(20);
	}

	if (!RHSPIDriver::init())
		return false;

	// Determine the interrupt number that corresponds to the interruptPin
	int interruptNumber = digitalPinToInterrupt(_interruptPin);
	if (interruptNumber == NOT_AN_INTERRUPT)
		return false;

	// Set sleep mode, so we can also set LORA mode:
	spiWrite(RH_RF95_REG_01_OP_MODE, RH_RF95_MODE_SLEEP | RH_RF95_LONG_RANGE_MODE);
	delay(10); // Wait for sleep mode to take over from say, CAD
	_revision = spiRead(RH_RF95_REG_42_VERSION);

	// Check we are in sleep mode, with LORA set
	if (spiRead(RH_RF95_REG_01_OP_MODE) == (RH_RF95_MODE_SLEEP | RH_RF95_LONG_RANGE_MODE))
	{
		// Add by Adrien van den Bossche <*****@*****.**> for Teensy
		// ARM M4 requires the below. else pin interrupt doesn't work properly.
		// On all other platforms, its innocuous, belt and braces
		pinMode(_interruptPin, INPUT_PULLDOWN);

		// Set up interrupt handler
		// Since there are a limited number of interrupt glue functions isr*() available,
		// we can only support a limited number of devices simultaneously
		// ON some devices, notably most Arduinos, the interrupt pin passed in is actuallt the 
		// interrupt number. You have to figure out the interruptnumber-to-interruptpin mapping
		// yourself based on knwledge of what Arduino board you are running on.
		if (_myInterruptIndex == 0xFF)
		{
			// First run, no interrupt allocated yet
			if (_interruptCount < RH_RF95_NUM_INTERRUPTS)
				_myInterruptIndex = _interruptCount++;
			// else Too many devices, not enough interrupt vectors
		}
		if (_myInterruptIndex < RH_RF95_NUM_INTERRUPTS)
		{
			_deviceForInterrupt[_myInterruptIndex] = this;

			if (_myInterruptIndex == 0)
				attachInterrupt(interruptNumber, isr0, RISING);
			else if (_myInterruptIndex == 1)
				attachInterrupt(interruptNumber, isr1, RISING);
			else if (_myInterruptIndex == 2)
				attachInterrupt(interruptNumber, isr2, RISING);

			// Set up FIFO
			// We configure so that we can use the entire 256 byte FIFO for either receive
			// or transmit, but not both at the same time
			spiWrite(RH_RF95_REG_0E_FIFO_TX_BASE_ADDR, 0);
			spiWrite(RH_RF95_REG_0F_FIFO_RX_BASE_ADDR, 0);

			result = true;
		}
	}

	if (result)
	{
		// Packet format is preamble + explicit-header + payload + crc
		// Explicit Header Mode
		// payload is TO + FROM + ID + FLAGS + message data
		// RX mode is implmented with RXCONTINUOUS
		// max message data length is 255 - 4 = 251 octets

		setModeIdle();

		// Set up default configuration
		// No Sync Words in LORA mode.

		//	setModemConfig(Bw125Cr45Sf128);		// Radio default medium
		//	setModemConfig(Bw125Cr48Sf4096);	// slow and reliable?
		setModemConfig(Bw31_25Cr48Sf512);

		setPreambleLength(8);	// Default is 8
		setFrequency(434.0);	// An innocuous ISM frequency, same as RF22's
		setTxPower(23);			// Highest power
	}

	return result;
}
コード例 #19
0
ファイル: RF22.cpp プロジェクト: ykiveish/rfm22b-galileo
uint8_t RF22::headerId()
{
    return spiRead(RF22_REG_49_RECEIVED_HEADER1);
}
コード例 #20
0
ファイル: rfm69.c プロジェクト: UKHASnet/LPC810
void RFM69_setMode(uint8_t newMode)
{
    spiWrite(RFM69_REG_01_OPMODE, (spiRead(RFM69_REG_01_OPMODE) & 0xE3) | newMode);
    _mode = newMode;
}
コード例 #21
0
ファイル: RF22.cpp プロジェクト: ykiveish/rfm22b-galileo
uint8_t RF22::headerFlags()
{
    return spiRead(RF22_REG_4A_RECEIVED_HEADER0);
}
コード例 #22
0
ファイル: 复件 mcp3301.c プロジェクト: wangdongqqqq/123
uint8_t mcp3301ReadBuf(uint8_t * d,uint16_t len){
 spiRead(MCP3301_SPI_PORT,1,d,len); 
}
コード例 #23
0
ファイル: RF22.cpp プロジェクト: ykiveish/rfm22b-galileo
uint8_t RF22::init()
{
    // Wait for RF22 POR (up to 16msec)
    usleep (16);

    // Initialise the slave select pin    
    _cs = mraa_gpio_init(_slaveSelectPin);
	mraa_gpio_dir(_cs, MRAA_GPIO_OUT);
	mraa_gpio_write(_cs, 0x1);

    // start the SPI library:
    // Note the RF22 wants mode 0, MSB first and default to 1 Mbps
    _spi = mraa_spi_init(0);
    mraa_spi_mode (_spi, MRAA_SPI_MODE0);
    mraa_spi_lsbmode(_spi, 0);
    mraa_spi_frequency(_spi, 1000000); // 1Mhz
    usleep (100);

    // Software reset the device
    reset();

    // Get the device type and check it
    // This also tests whether we are really connected to a device
    _deviceType = spiRead(RF22_REG_00_DEVICE_TYPE);
    if (   _deviceType != RF22_DEVICE_TYPE_RX_TRX
        && _deviceType != RF22_DEVICE_TYPE_TX)
	return 0;
 
    _irq = mraa_gpio_init(_interrupt + 2);
    mraa_gpio_dir(_irq, MRAA_GPIO_IN);
    gpio_edge_t edge = MRAA_GPIO_EDGE_FALLING;
	// Set up interrupt handler
    if (_interrupt == 0)
    {
		_RF22ForInterrupt[0] = this;
    	mraa_gpio_isr(_irq, edge, &RF22::isr0, NULL);
    }
    else if (_interrupt == 1)
    {
		_RF22ForInterrupt[1] = this;
    	mraa_gpio_isr(_irq, edge, &RF22::isr1, NULL);
    }
    else
	return 0;

    clearTxBuf();
    clearRxBuf();
  
    // Most of these are the POR default
    spiWrite(RF22_REG_7D_TX_FIFO_CONTROL2, RF22_TXFFAEM_THRESHOLD);
    spiWrite(RF22_REG_7E_RX_FIFO_CONTROL,  RF22_RXFFAFULL_THRESHOLD);
    spiWrite(RF22_REG_30_DATA_ACCESS_CONTROL, RF22_ENPACRX | RF22_ENPACTX | RF22_ENCRC | RF22_CRC_CRC_16_IBM);
    // Configure the message headers
    // Here we set up the standard packet format for use by the RF22 library
    // 8 nibbles preamble
    // 2 SYNC words 2d, d4
    // Header length 4 (to, from, id, flags)
    // 1 octet of data length (0 to 255)
    // 0 to 255 octets data
    // 2 CRC octets as CRC16(IBM), computed on the header, length and data
    // On reception the to address is check for validity against RF22_REG_3F_CHECK_HEADER3
    // or the broadcast address of 0xff
    // If no changes are made after this, the transmitted
    // to address will be 0xff, the from address will be 0xff
    // and all such messages will be accepted. This permits the out-of the box
    // RF22 config to act as an unaddresed, unreliable datagram service
    spiWrite(RF22_REG_32_HEADER_CONTROL1, RF22_BCEN_HEADER3 | RF22_HDCH_HEADER3);
    spiWrite(RF22_REG_33_HEADER_CONTROL2, RF22_HDLEN_4 | RF22_SYNCLEN_2);
    setPreambleLength(8);
    uint8_t syncwords[] = { 0x2d, 0xd4 };
    setSyncWords(syncwords, sizeof(syncwords));
    setPromiscuous(0); 
    // Check the TO header against RF22_DEFAULT_NODE_ADDRESS
    spiWrite(RF22_REG_3F_CHECK_HEADER3, RF22_DEFAULT_NODE_ADDRESS);
    // Set the default transmit header values
    setHeaderTo(RF22_DEFAULT_NODE_ADDRESS);
    setHeaderFrom(RF22_DEFAULT_NODE_ADDRESS);
    setHeaderId(0);
    setHeaderFlags(0);

    // Ensure the antenna can be switched automatically according to transmit and receive
    // This assumes GPIO0(out) is connected to TX_ANT(in) to enable tx antenna during transmit
    // This assumes GPIO1(out) is connected to RX_ANT(in) to enable rx antenna during receive
    spiWrite (RF22_REG_0B_GPIO_CONFIGURATION0, 0x12) ; // TX state
    spiWrite (RF22_REG_0C_GPIO_CONFIGURATION1, 0x15) ; // RX state

    // Enable interrupts
    spiWrite(RF22_REG_05_INTERRUPT_ENABLE1, RF22_ENTXFFAEM | RF22_ENRXFFAFULL | RF22_ENPKSENT | RF22_ENPKVALID | RF22_ENCRCERROR | RF22_ENFFERR);
    spiWrite(RF22_REG_06_INTERRUPT_ENABLE2, RF22_ENPREAVAL);

    // Set some defaults. An innocuous ISM frequency, and reasonable pull-in
    setFrequency(434.0, 0.05);
//    setFrequency(900.0);
    // Some slow, reliable default speed and modulation
    setModemConfig(FSK_Rb2_4Fd36);
//    setModemConfig(FSK_Rb125Fd125);
    // Minimum power
    setTxPower(RF22_TXPOW_8DBM);
//    setTxPower(RF22_TXPOW_17DBM);

    return 1;
}
コード例 #24
0
uint8_t RH_RF22::statusRead()
{
    return spiRead(RH_RF22_REG_02_DEVICE_STATUS);
}
コード例 #25
0
/*
 * initialise card.
 * logic from SD Simplified, page 95, with extentions
 * for SanDisk MMCs 1.3 that accept CMD55
 */
int DeviceMemCard::open(unsigned short vhs)
  {
    int i;
    unsigned char r1;
    int res;

    if (m_isOpened)
    return MEMCARD_ERROR_OPENED;

    m_bufLen = 0;
    m_capabilities = 0;

    implSelectInit();

    spiInitLowSpeed();

    // send more than 80 clocks prior to starting bus communication
    // before slecting the card
    for (i = 10; i != 0; --i)
    spiRead();

    // 1 mS for just in case, not from the specs
    OS::busyWaitMillis(1);
    res = 0;

    safeSelectOn();

    res = enterIdle();
    if (res != 0)
      {
        OSDeviceDebug::putString("idle=");
        OSDeviceDebug::putHex((unsigned short)res);
        OSDeviceDebug::putNewLine();

        goto out;
      }

    // now in idle state

    unsigned long l;
    unsigned char icmd;
    int ires;

    icmd = 0;
    ires = 0;

    sendCommand(SDC_SEND_IF_COND, (unsigned long) vhs);
    r1 = receiveR1();
    l = receiveLong();

    if ((r1 & ~MMC_R1_IN_IDLE_STATE) == 0)
      {
        // no error, v2.0 SD Card or later
#if defined(OS_INCLUDE_MEMCARD_MEMBER_VHS)
        m_VHS = l; // store to member
#endif

        OSDeviceDebug::putString("SD Card v2.0 or later");
        OSDeviceDebug::putNewLine();

#if defined(OS_INCLUDE_MEMCARD_VALIDATEVOLTAGE)

        if (!validateVoltage(l))
          {
            res = MEMCARD_ERROR_VOLTAGE;
            goto out;
          }
#endif

        // card with compatible voltage range

        sendCommand(SDC_READ_OCR, 0L);
        r1 = receiveR1();
        l = receiveLong();

        if ((r1 & ~MMC_R1_IN_IDLE_STATE) == 0)
          {
            // no error, v2.0 SD

            OSDeviceDebug::putString("OCR=");
            OSDeviceDebug::putHex((unsigned short) (l >> 16));
            OSDeviceDebug::putHex((unsigned short) l);
            OSDeviceDebug::putNewLine();

#if defined(OS_INCLUDE_MEMCARD_OCR)

            m_OCR = l;
#endif

            m_capabilities |= MEMCARD_CAPABILITIES_SDC;
            m_capabilities |= MEMCARD_CAPABILITIES_SDC2;

            if ((l & SDC_OCR_CAPACITY) != 0)
              {
                OSDeviceDebug::putString("SD Card v2.0 HC");
                OSDeviceDebug::putNewLine();

                m_capabilities |= MEMCARD_CAPABILITIES_SDC2_HC;
                res = MEMCARD_OPEN_SDC2HC;
              }
            else
              {
                OSDeviceDebug::putString("SD Card v2.0 SS");
                OSDeviceDebug::putNewLine();

                res = MEMCARD_OPEN_SDC2SS;
              }
            icmd = SDC_A_SEND_OP_COND;
            ires = MEMCARD_ERROR_SD_SEND_OP_COND;
          }
コード例 #26
0
uint8_t RH_RF22::ezmacStatusRead()
{
    return spiRead(RH_RF22_REG_31_EZMAC_STATUS);
}
コード例 #27
0
void getRawMeasure(int channel)
{
    unsigned long long  data=0;
    //ulong data=0;
    uint  Flag;
    //float temp = 0.0;
    uint temp =0;
    BYTE NflagPA =0;//有功通道A
//    BYTE NflagPB =0;//有功通道B
//    BYTE NflagPQ =0;//无功通道


    if(channel > 8 || channel < 0)
    {
        printf("\nSPI channel over range.\n");
        return ;
    }
    spiRead(channel, SYSCON, (uint *)&data, WIDTH_TWO_B);
    printf("\n SYSCON = 0x%x", (uint)data);
    
    spiRead(channel, EMUCON, (uint *)&data, WIDTH_TWO_B);
    printf("\n EMUCON = 0x%x", (uint)data);

    spiRead(channel, HFCONST, (uint *)&data, WIDTH_TWO_B);
    printf("\n HFCONST = 0x%x", (uint)data);
    
    spiRead(channel, PSTART, (uint *)&data, WIDTH_TWO_B);
    printf("\n PSTART = 0x%x", (uint)data);
    
    spiRead(channel, PFCNT, (uint *)&data, WIDTH_TWO_B);
    printf("\n PFCNT = 0x%x", (uint)data);
    
    spiRead(channel, QFCNT, (uint *)&data, WIDTH_TWO_B);
    printf("\n QFCNT = 0x%x\n", (uint)data);
 
    spiRead(channel, IARMSOS, (uint *)&data, WIDTH_TWO_B);
    printf("\n IARMSOS = 0x%x", (uint)data);   
    spiRead(channel, IBRMSOS, (uint *)&data, WIDTH_TWO_B);
    printf("\n IBRMSOS = 0x%x", (uint)data);
    
    spiRead(channel, GPQA, (uint *)&data, WIDTH_TWO_B);
    printf("\n GPQA  = 0x%x", (uint)data);   
    spiRead(channel, GPQB, (uint *)&data, WIDTH_TWO_B);
    printf("\n GPQB  = 0x%x", (uint)data);

    spiRead(channel, APOSA, (uint *)&data, WIDTH_TWO_B);
    printf("\n APOSA = 0x%x", (uint)data); 
    spiRead(channel, APOSB, (uint *)&data, WIDTH_TWO_B);
    printf("\n APOSB = 0x%x", (uint)data);
    
    spiRead(channel, RPOSA, (uint *)&data, WIDTH_TWO_B);
    printf("\n RPOSA = 0x%x", (uint)data);
    spiRead(channel, RPOSB, (uint *)&data, WIDTH_TWO_B);
    printf("\n RPOSB = 0x%x", (uint)data);

    spiRead(channel, PHSA, (uint *)&data, WIDTH_ONE_B);
    printf("\n PHSA = 0x%x", (uint)data);
    spiRead(channel, PHSB, (uint *)&data, WIDTH_ONE_B);
    printf("\n PHSB = 0x%x", (uint)data);

    spiRead(channel, QPHSCAL, (uint *)&data, WIDTH_TWO_B);
    printf("\n QPHSCAL = 0x%x\n", (uint)data);
      
    spiRead(channel, IARMS, (uint *)&data, WIDTH_THREE_B);
   
    if((data >= 0x800000)||(data == 0))
    {
        data = 0;
        temp = 0;
    }
    else
    {
        temp = (uint)(((*(WORD *)&SysParam[SP_RNKIA+2*channel])*data)/10000+1);//补偿0.1mA
    }
    printf("\n IARMS = 0x%x current:%d.%d mA  KiA=%d", (uint)data, temp/10,temp%10,*(WORD *)&SysParam[SP_RNKIA+2*channel]);
    printf("\n IARMSV =%dmV ",(uint)(data*2500/(0x7FFFFF+1)));
/*    
    spiRead(channel, IBRMS, &data, WIDTH_THREE_B);
    if((data >= 0x800000)||(data == 0))
    {
        data = 0;
        temp = 0;
    }
    else
    {
        temp = ((*(WORD *)&SysParam[SP_RNKIB+2*channel]) * data)/10000+1;// 补偿0.1mA
    }
    printf("\n IBRMS = 0x%x current:%d.%d mA  KiB=%d", data, temp/10,temp%10,*(WORD *)&SysParam[SP_RNKIB+2*channel]);
*/    
    spiRead(channel, URMS, (uint *)&data, WIDTH_THREE_B);
    printf("\n URMS = 0x%x ", (uint)data);
    
    if((data >= 0x800000)||(data < 0x24B))
    {    
        data = 0;
    }
 
    temp = (uint)(((*(WORD *)&SysParam[SP_RNKU+2*channel])* data)/1000000);
    printf("\n URMS  = 0x%x voltage:%d.%d V Ku=%d", (uint)data, temp/10,temp%10,(*(WORD *)&SysParam[SP_RNKU+2*channel]));
    printf("\n URMSV =%dmV ",(uint)(data*2500/(0x7FFFFF+1)));
    spiRead(channel, UFREQ, (uint *)&data, WIDTH_TWO_B);
    printf("\n UFREQ = 0x%x\n", (uint)data);
    
    spiRead(channel, POWERPA, (uint *)&data, WIDTH_FOUR_B);
    printf("\n POWERPA = 0x%x", (uint)data);
    if(data>0x80000000)//负数
    {   
        spiRead(channel, EMUSTATUS, &Flag, WIDTH_THREE_B);
        if((Flag&0x20000)&&((Flag&0x200000)==0))//REVP=1,chnsel=0
        {
            data = (data-1)^0x7fffffff;
            data =data&0x7fffffff;
            //data = ~data+1;
            NflagPA=1;
        }
        else
        {
            data =0;    
        }
        
    }
    //数据有溢出可能
   
   temp = (uint )(((*(WORD *)&SysParam[SP_RNKAP+2*channel])*data)/1000000);//扩大10
   
    if(NflagPA == 1)
    {
        NflagPA=0;
        printf("\n POWERPA = 0x%x PA=-%d.%d W  KpA=%d", (uint)data,temp/10,temp%10,*(WORD *)&SysParam[SP_RNKAP+2*channel]);   
    }
    else
    {
        printf("\n POWERPA = 0x%x PA= %d.%d W  KpA=%d", (uint)data,temp/10,temp%10,*(WORD *)&SysParam[SP_RNKAP+2*channel]);
    }
    /*
    spiRead(channel, POWERPB, &data, WIDTH_FOUR_B);
    printf("\n POWERPB = 0x%x", data );
    if(data>0x80000000)//负数
    {
        spiRead(channel, EMUSTATUS, &Flag, WIDTH_THREE_B);
        if((Flag&0x20000)&&((Flag&0x200000)==1))//REVP=1,chnsel=1
        {
            data = (data-1)^0x7fffffff;
            data = data&0x7fffffff;
            NflagPB =1;
        }
        else
        {
            data =0;    
        }
    }
    
    temp = ((*(WORD*)&SysParam[SP_RNKBP+2*channel])*data)/1000000;
    if(NflagPB == 1)
    {
        NflagPB =0;
        printf("\n POWERPB = 0x%x PB=-%d.%d W  KpB=%d", data,temp/10,temp%10,*(WORD*)&SysParam[SP_RNKBP+2*channel]);
    }
    else
    {
        printf("\n POWERPB = 0x%x PB= %d.%d W  KpB=%d", data,temp/10,temp%10,*(WORD*)&SysParam[SP_RNKBP+2*channel]);
    }    
    spiRead(channel, POWERQ, &data, WIDTH_FOUR_B);
    printf("\n POWERQ  = 0x%x", data);
    if(data>0x80000000)//负数
    {
        spiRead(channel, EMUSTATUS, &Flag, WIDTH_THREE_B);
        if(Flag&0x30000)//REVQ=1
        {
            data = ((data-1)^0x7fffffff);
            data = data&0x7fffffff;
            NflagPQ =1;
        }
        else
        {
            data =0;    
        }
    }
    if(NflagPQ ==1)
    {
        NflagPQ = 0; 
    }
    else
    {
    
    }
    printf("\n POWERQ  = 0x%x\n", data);
    */
    spiRead(channel, ENERGYP, (uint *)&data, WIDTH_THREE_B);
    temp = (data+ *(DWORD *)&EEPROMPARAM[EEPROM_ENERGY + 4*channel])*100/EC;
    printf("\n ENERGYP = 0x%x , EE= %d.%d%d kWh", (uint)data,temp/100,temp%100/10,temp%10);

    spiRead(channel, ENERGYP2, (uint *)&data, WIDTH_THREE_B);
    temp = data*100/EC;
    printf("\n ENERGYP2 = 0x%x ,EE2= %d.%d%d kWh", (uint)data,temp/100,temp%100/10,temp%10);
        
    spiRead(channel, ENERGYQ, (uint *)&data, WIDTH_THREE_B);
    printf("\n ENERGYQ = 0x%x", (uint)data);
    temp = data*100/EC;
    printf("\n ENERGYPQ = %d kVARh\n", (uint)data);

    
    spiRead(channel, IF, (uint *)&data, WIDTH_ONE_B);
    printf("\n interrupt = 0x%x", (uint)data);

    spiRead(channel, IE, (uint *)&data, WIDTH_ONE_B);
    printf("\n IE = 0x%x", (uint)data);
    
    spiRead(channel, EMUSTATUS, (uint *)&data, WIDTH_THREE_B);
    printf("\n EMUSTATUS = 0x%x", (uint)data);

    spiRead(channel, HFCONST, (uint *)&data, WIDTH_TWO_B);
    printf("\n HFCONST = 0x%x  buf=0x%x", (uint)data,*(WORD *)&SysParam[SP_RNHFCONST+2*channel]);

    QF_PF_IRQn_Output(channel);

}