bool RFM69::receiveDone() { //ATOMIC_BLOCK(ATOMIC_FORCEON) //{ uint8_t rd_SREG = SREG; noInterrupts(); // Note: New SPI library prefers to use EIMSK (external interrupt mask) if available // to mask (only) interrupts registered via SPI::usingInterrupt(). It only // falls back to disabling ALL interrupts SREG if EIMSK cannot be used. // Hence We cannot assume that methods calls below that call select() unselect() // will result in a call to reenable interrupts(). // Thus the code below needs to explicitly do this to be safe. if (_mode == RF69_MODE_RX && PAYLOADLEN > 0) { setMode(RF69_MODE_STANDBY); SREG = rd_SREG; // restore interrupts return true; } else if (_mode == RF69_MODE_RX) // already in RX no payload yet { SREG = rd_SREG; // restore interrupts return false; } receiveBegin(); SREG = rd_SREG; // restore interrupts return false; //} }
bool RFM69::receiveDone() { // ATOMIC_BLOCK(ATOMIC_FORCEON) // { noInterrupts(); //re-enabled in unselect() via setMode() or via receiveBegin() if (_mode == RF69_MODE_RX && PAYLOADLEN>0) { setMode(RF69_MODE_STANDBY); //enables interrupts return true; } else if (_mode == RF69_MODE_RX) //already in RX no payload yet { interrupts(); //explicitly re-enable interrupts return false; } receiveBegin(); return false; //} }
void RFM69::interruptHandler() { //pinMode(4, OUTPUT); //digitalWrite(4, 1); if (_mode == RF69_MODE_RX && (readReg(REG_IRQFLAGS2) & RF_IRQFLAGS2_PAYLOADREADY)) { //RSSI = readRSSI(); setMode(RF69_MODE_STANDBY); select(); SPI.transfer(REG_FIFO & 0x7F); PAYLOADLEN = SPI.transfer(0); PAYLOADLEN = PAYLOADLEN > 66 ? 66 : PAYLOADLEN; // precaution TARGETID = SPI.transfer(0); if(!(_promiscuousMode || TARGETID == _address || TARGETID == RF69_BROADCAST_ADDR) // match this node's address, or broadcast address or anything in promiscuous mode || PAYLOADLEN < 3) // address situation could receive packets that are malformed and don't fit this libraries extra fields { PAYLOADLEN = 0; unselect(); receiveBegin(); //digitalWrite(4, 0); return; } DATALEN = PAYLOADLEN - 3; SENDERID = SPI.transfer(0); uint8_t CTLbyte = SPI.transfer(0); ACK_RECEIVED = CTLbyte & 0x80; // extract ACK-received flag ACK_REQUESTED = CTLbyte & 0x40; // extract ACK-requested flag for (uint8_t i = 0; i < DATALEN; i++) { DATA[i] = SPI.transfer(0); } if (DATALEN < RF69_MAX_DATA_LEN) DATA[DATALEN] = 0; // add null at end of string unselect(); setMode(RF69_MODE_RX); } RSSI = readRSSI(); //digitalWrite(4, 0); }
// internal function - interrupt gets called when a packet is received void RFM69::interruptHandler() { #ifdef RASPBERRY unsigned char thedata[67]; char i; for(i = 0; i < 67; i++) thedata[i] = 0; // printf("interruptHandler %d\n", intCount); #endif //pinMode(4, OUTPUT); //digitalWrite(4, 1); if (_mode == RF69_MODE_RX && (readReg(REG_IRQFLAGS2) & RF_IRQFLAGS2_PAYLOADREADY)) { //RSSI = readRSSI(); setMode(RF69_MODE_STANDBY); #ifdef RASPBERRY thedata[0] = REG_FIFO & 0x7F; thedata[1] = 0; // PAYLOADLEN thedata[2] = 0; // TargetID wiringPiSPIDataRW(SPI_DEVICE, thedata, 3); delayMicroseconds(MICROSLEEP_LENGTH); PAYLOADLEN = thedata[1]; PAYLOADLEN = PAYLOADLEN > 66 ? 66 : PAYLOADLEN; // precaution TARGETID = thedata[2]; #else select(); SPI.transfer(REG_FIFO & 0x7F); PAYLOADLEN = SPI.transfer(0); PAYLOADLEN = PAYLOADLEN > 66 ? 66 : PAYLOADLEN; // precaution TARGETID = SPI.transfer(0); #endif if(!(_promiscuousMode || TARGETID == _address || TARGETID == RF69_BROADCAST_ADDR) // match this node's address, or broadcast address or anything in promiscuous mode || PAYLOADLEN < 3) // address situation could receive packets that are malformed and don't fit this libraries extra fields { PAYLOADLEN = 0; unselect(); receiveBegin(); //digitalWrite(4, 0); return; } #ifdef RASPBERRY DATALEN = PAYLOADLEN - 3; thedata[0] = REG_FIFO & 0x77; thedata[1] = 0; //SENDERID thedata[2] = 0; //CTLbyte; for(i = 0; i< DATALEN; i++) { thedata[i+3] = 0; } wiringPiSPIDataRW(SPI_DEVICE, thedata, DATALEN + 3); SENDERID = thedata[1]; uint8_t CTLbyte = thedata[2]; ACK_RECEIVED = CTLbyte & 0x80; //extract ACK-requested flag ACK_REQUESTED = CTLbyte & 0x40; //extract ACK-received flag for (i= 0; i < DATALEN; i++) { DATA[i] = thedata[i+3]; } #else DATALEN = PAYLOADLEN - 3; SENDERID = SPI.transfer(0); uint8_t CTLbyte = SPI.transfer(0); ACK_RECEIVED = CTLbyte & RFM69_CTL_SENDACK; // extract ACK-received flag ACK_REQUESTED = CTLbyte & RFM69_CTL_REQACK; // extract ACK-requested flag interruptHook(CTLbyte); // TWS: hook to derived class interrupt function for (uint8_t i = 0; i < DATALEN; i++) { DATA[i] = SPI.transfer(0); } #endif if (DATALEN < RF69_MAX_DATA_LEN) DATA[DATALEN] = 0; // add null at end of string unselect(); setMode(RF69_MODE_RX); } RSSI = readRSSI(); //digitalWrite(4, 0); }