Esempio n. 1
0
/***
*
*	void tick() 
*	checks if there is a complete transmission and calls the users callback function
*
***/
void megaRF::tick()
{
	rfm12_tick();
	uint8_t i=0;
	if(rfm12_rx_status()==STATUS_COMPLETE){
		extbuffer = rfm12_rx_buffer();		
		for(i=0;i<rfm12_rx_len();i++)
		{
			intbuffer[i] = *extbuffer;
			extbuffer++;
		}
		intbuffer[i]='\0';
		packetLength=i;
		
		rfm12_rx_clear();
		  
		 
		// don't bother if user hasn't registered a callback
 		if(!user_onReceive){
    		
  		}
  		else{
			user_onReceive();
		}
	}
	
	
	rfm12_tick();
	return;
}
Esempio n. 2
0
// behind the scenes function that is called when data is received
void TwoWire::onReceiveService(uint8_t* inBytes, int numBytes)
{
  // don't bother if user hasn't registered a callback
  if(!user_onReceive){
    return;
  }
  // don't bother if rx buffer is in use by a master requestFrom() op
  // i know this drops data, but it allows for slight stupidity
  // meaning, they may not have read all the master requestFrom() data yet
    user_onReceive(numBytes);
}
Esempio n. 3
0
/**
 * Internal process handling the rx buffers, and calling the user's interrupt handles
 */
void DWire::_handleReceive( uint8_t * rxBuffer ) 
{
    // No need to do anything if there is no handler registered
    if (!user_onReceive)
        return;

    // reset the RX buffer index to prepare the readout
    *pRxBufferIndex = 0;

	// call the user-defined receive handler
    user_onReceive( *pRxBufferSize );
}
Esempio n. 4
0
/**
 *	void tick()
 *	checks if there is a complete transmission and calls the users callback function
 **/
void RFM70::tick() {
    byte len = receivePayload(rcvBuffer);
    if (len < 32) {
        rcvBuffer[len] = '\0';
    }
    //new packet if len > 0
    if (len != 0) {
        packetLength = (uint8_t) len;

        // don't bother if user hasn't registered a callback
        if (user_onReceive) {
            user_onReceive();
        }
    }
    return;
}
Esempio n. 5
0
void TwoWire::sda_rising_isr(void)
{
	//digitalWrite(3, HIGH);
	if (!(port().S & I2C_S_BUSY)) {
		detachInterrupt(hardware.sda_pin[sda_pin_index]);
		if (user_onReceive != NULL) {
			rxBufferIndex = 0;
			user_onReceive(rxBufferLength);
		}
		//delayMicroseconds(100);
	} else {
		if (++irqcount >= 2 || !slave_mode) {
			detachInterrupt(hardware.sda_pin[sda_pin_index]);
		}
	}
	//digitalWrite(3, LOW);
}
Esempio n. 6
0
/**
 * Internal process handling the rx buffers, and calling the user's interrupt handles
 */
void DWire::_handleReceive(uint8_t * rxBuffer) {
	// No need to do anything if there is no handler registered
	if (!user_onReceive)
		return;

	// Check whether the user application is still reading out the local buffer.
	// This needs to be tested to make sure it doesn't give any problems.
	if (rxReadIndex != 0 && rxReadLength != 0)
		return;

	// Copy the main buffer into a local buffer
	rxReadLength = *pRxBufferIndex;
	rxReadIndex = 0;

	for (int i = 0; i < rxReadLength; i++)
		this->rxLocalBuffer[i] = rxBuffer[i];

	// Reset the main buffer
	(*pRxBufferIndex) = 0;

	user_onReceive(rxReadLength);
}
Esempio n. 7
0
// behind the scenes function that is called when data is received
void TwoWire::onReceiveService(uint8_t* inBytes, int numBytes)
{
  // don't bother if user hasn't registered a callback
  if(!user_onReceive){
    return;
  }
  // don't bother if rx buffer is in use by a master requestFrom() op
  // i know this drops data, but it allows for slight stupidity
  // meaning, they may not have read all the master requestFrom() data yet
  if(rxBufferIndex < rxBufferLength){
    return;
  }
  // copy twi rx buffer into local read buffer
  // this enables new reads to happen in parallel
  for(uint8_t i = 0; i < numBytes; ++i){
    rxBuffer[i] = inBytes[i];    
  }
  // set rx iterator vars
  rxBufferIndex = 0;
  rxBufferLength = numBytes;
  // alert user program
  user_onReceive(numBytes);
}
Esempio n. 8
0
/**
 *	void tick()
 *	checks if there is a complete transmission and calls the users callback function
 **/
void RFM70::tick() {
	uint8_t i=0;
	uint8_t len = receivePayload(rcvBuffer);
	if (len < 32) {
		rcvBuffer[len] = '\0';
	}
	printf("rec:%d:%d->%d:%d:%s\n", rcvBuffer[0],
					rcvBuffer[1],
					rcvBuffer[2],
					rcvBuffer[3],
					&rcvBuffer[4]
					);
	//new packet if len > 0
	if (len != 0) {
		packetLength = len;
		// don't bother if user hasn't registered a callback
		if (user_onReceive) {
			user_onReceive(rcvBuffer, len);

		}
	}
	return;
}
Esempio n. 9
0
void TwoWire::I2CIntHandler(void) {
    //clear data interrupt
    HWREG(SLAVE_BASE + I2C_O_SICR) = I2C_SICR_DATAIC;
    uint8_t startDetected = 0;
    uint8_t stopDetected = 0;

    if(HWREG(SLAVE_BASE + I2C_O_SRIS) & I2C_SLAVE_INT_START) {
        startDetected = 1;
        //clear raw start interrupt
        HWREG(SLAVE_BASE + I2C_O_SICR) = I2C_SICR_STARTIC;
    }
    else if(HWREG(SLAVE_BASE + I2C_O_SRIS) & I2C_SLAVE_INT_STOP) {
        stopDetected = 1;
        HWREG(SLAVE_BASE + I2C_O_SICR) = I2C_SICR_STOPIC;
    }

    switch(I2CSlaveStatus(SLAVE_BASE) & (I2C_SCSR_TREQ | I2C_SCSR_RREQ)) {

    case(I2C_SLAVE_ACT_RREQ)://data received
        if(I2CSlaveStatus(SLAVE_BASE) & I2C_SCSR_FBR)
            currentState = SLAVE_RX;
        if(!RX_BUFFER_FULL) {
            rxBuffer[rxWriteIndex] = I2CSlaveDataGet(SLAVE_BASE);
            rxWriteIndex = (rxWriteIndex + 1) % BUFFER_LENGTH;
        }

        break;

    case(I2C_SLAVE_ACT_TREQ)://data requested

        if(startDetected) {
            uint8_t oldWriteIndex = txWriteIndex;
            user_onRequest();

            //
            // send data if onRequest() wrote data that has
            // yet to be sent
            //
            if(oldWriteIndex != txWriteIndex) {
                I2CSlaveDataPut(SLAVE_BASE, txBuffer[txReadIndex]);
                txReadIndex = (txReadIndex + 1) % BUFFER_LENGTH;
            }

        }

        else if(!TX_BUFFER_EMPTY) {
            I2CSlaveDataPut(SLAVE_BASE, txBuffer[txReadIndex]);
            txReadIndex = (txReadIndex + 1) % BUFFER_LENGTH;
        }

        else
            I2CSlaveDataPut(SLAVE_BASE, 0);

        break;

    default:
        break;
    }

    if(stopDetected && currentState == SLAVE_RX) {
        int avail = available();
        user_onReceive(avail);
        currentState = IDLE;
    }

}
Esempio n. 10
0
void TwoWire::isr(void)
{
	uint8_t status, c1, data;
	static uint8_t receiving=0;

	status = port().S;
	//serial_print(".");
	if (status & I2C_S_ARBL) {
		// Arbitration Lost
		port().S = I2C_S_ARBL;
		//serial_print("a");
		if (receiving && rxBufferLength > 0) {
			// TODO: does this detect the STOP condition in slave receive mode?


		}
		if (!(status & I2C_S_IAAS)) return;
	}
	if (status & I2C_S_IAAS) {
		//serial_print("\n");
		// Addressed As A Slave
		if (status & I2C_S_SRW) {
			//serial_print("T");
			// Begin Slave Transmit
			receiving = 0;
			txBufferLength = 0;
			if (user_onRequest != NULL) {
				user_onRequest();
			}
			if (txBufferLength == 0) {
				// is this correct, transmitting a single zero
				// when we should send nothing?  Arduino's AVR
				// implementation does this, but is it ok?
				txBufferLength = 1;
				txBuffer[0] = 0;
			}
			port().C1 = I2C_C1_IICEN | I2C_C1_IICIE | I2C_C1_TX;
			port().D = txBuffer[0];
			txBufferIndex = 1;
		} else {
			// Begin Slave Receive
			//serial_print("R");
			receiving = 1;
			rxBufferLength = 0;
			port().C1 = I2C_C1_IICEN | I2C_C1_IICIE;
			data = port().D;
		}
		port().S = I2C_S_IICIF;
		return;
	}
	#if defined(WIRE_HAS_STOP_INTERRUPT)
	c1 = port().FLT;
	if ((c1 & I2C_FLT_STOPF) && (c1 & I2C_FLT_STOPIE)) {
		port().FLT = c1 & ~I2C_FLT_STOPIE;
		if (user_onReceive != NULL) {
			rxBufferIndex = 0;
			user_onReceive(rxBufferLength);
		}
	}
	#endif
	c1 = port().C1;
	if (c1 & I2C_C1_TX) {
		// Continue Slave Transmit
		//serial_print("t");
		if ((status & I2C_S_RXAK) == 0) {
			//serial_print(".");
			// Master ACK'd previous byte
			if (txBufferIndex < txBufferLength) {
				port().D = txBuffer[txBufferIndex++];
			} else {
				port().D = 0;
			}
			port().C1 = I2C_C1_IICEN | I2C_C1_IICIE | I2C_C1_TX;
		} else {
			//serial_print("*");
			// Master did not ACK previous byte
			port().C1 = I2C_C1_IICEN | I2C_C1_IICIE;
			data = port().D;
		}
	} else {
		// Continue Slave Receive
		irqcount = 0;
		#ifdef WIRE_HAS_STOP_INTERRUPT
		port().FLT |= I2C_FLT_STOPIE;
		#else
		#if defined(WIRE_IMPLEMENT_WIRE) && !defined(WIRE_IMPLEMENT_WIRE1)
		attachInterrupt(hardware.sda_pin[sda_pin_index], sda_rising_isr0, RISING);
		#elif !defined(WIRE_IMPLEMENT_WIRE) && defined(WIRE_IMPLEMENT_WIRE1)
		attachInterrupt(hardware.sda_pin[sda_pin_index], sda_rising_isr1, RISING);
		#elif defined(WIRE_IMPLEMENT_WIRE) && defined(WIRE_IMPLEMENT_WIRE1)
		if (this == &Wire) {
			attachInterrupt(hardware.sda_pin[sda_pin_index], sda_rising_isr0, RISING);
		} else if (this == &Wire1) {
			attachInterrupt(hardware.sda_pin[sda_pin_index], sda_rising_isr1, RISING);
		}
		#endif
		#endif // WIRE_HAS_STOP_INTERRUPT
		//digitalWriteFast(4, HIGH);
		data = port().D;
		//serial_phex(data);
		if (rxBufferLength < BUFFER_LENGTH && receiving) {
			rxBuffer[rxBufferLength++] = data;
		}
		//digitalWriteFast(4, LOW);
	}
	port().S = I2C_S_IICIF;
}