void bindRX(bool timeout) { uint32_t start = millis(); printStrLn("waiting bind..."); init_rfm(1); RF_Mode = Receive; to_rx_mode(); while(!timeout || ((millis() - start) < 500)) { if (RF_Mode == Received) { printStrLn("Got pkt\n"); spiSendAddress(0x7f); // Send the package read command uint8_t rxb = spiReadData(); if (rxb == 'b') { for (uint8_t i = 0; i < sizeof(bind_data); i++) { *(((uint8_t*) &bind_data) + i) = spiReadData(); } if (bind_data.version == BINDING_VERSION) { printStrLn("data good\n"); rxb = 'B'; tx_packet(&rxb, 1); // ACK that we got bound bindWriteEeprom(); Red_LED_ON; //signal we got bound on LED:s Green_LED_ON; //signal we got bound on LED:s return; } } } } }
void slaveLoop() { watchdogReset(); uint32_t now = micros(); bool needHop=false; switch (state) { case 0: // waiting for packet if (RF_Mode == Received) { Green_LED_ON; Red_LED_OFF; // got packet lastReceived = now; lostpkts=0; linkQuality = (linkQuality << 1) | 1; RF_Mode = Receive; spiSendAddress(0x7f); // Send the package read command for (int16_t i = 0; i < bind_data.packetSize; i++) { rx_buf[i] = spiReadData(); } uint8_t payloadBytes = (rx_buf[0] & 0x3f); if (payloadBytes > (bind_data.packetSize - 1)) { // INVALID DATA payloadBytes = 0; } // Check if this is a new packet from master and not a resent one if ((rx_buf[0] ^ tx_buf[0]) & MASTER_SEQ) { tx_buf[0] ^= MASTER_SEQ; if (payloadBytes) { // DATA FRAME if (bind_data.flags & PACKET_MODE) { serialWrite(0xf0); serialWrite(payloadBytes); CRCtx = 0; } for (uint8_t i=0; i < payloadBytes; i++) { serialWrite(rx_buf[1 + i]); if ((bind_data.flags & PACKET_MODE) && (bind_data.flags & PACKETCRC_MODE)) { CRC16_add(&CRCtx, rx_buf[1 + i]); } } if ((bind_data.flags & PACKET_MODE) && (bind_data.flags & PACKETCRC_MODE)) { serialWrite(CRCtx >> 8); serialWrite(CRCtx & 0xff); } } }
/** * readBurstReg * * Read burst data from CC1101 via SPI * * 'buffer' Buffer where to copy the result to * 'regAddr' Register address * 'len' Data length */ void CC1101_readBurstReg(byte * buffer, byte regAddr, byte len) { byte addr, i; addr = regAddr | READ_BURST; cc1101_Select(); // Select CC1101 wait_Miso(); // Wait until MISO goes low //spi_send(addr); // Send register address spiWriteAddr(addr); //DSK6713_waitusec(5); for(i=0 ; i<len ; i++) { //buffer[i] = spi_send(0x00); // Read result byte by byte buffer[i] = spiReadData(); // Read result byte by byte //DSK6713_waitusec(15); } //DSK6713_waitusec(20); cc1101_Deselect(); // Deselect CC1101 }
void bindMode(void) { uint32_t prevsend = millis(); uint8_t tx_buf[sizeof(bind_data) + 1]; bool sendBinds = 1; init_rfm(1); while (serialAvailable()) { serialRead(); // flush serial } Red_LED_OFF; while (1) { if (sendBinds & (millis() - prevsend > 200)) { prevsend = millis(); Green_LED_ON; buzzerOn(BZ_FREQ); tx_buf[0] = 'b'; memcpy(tx_buf + 1, &bind_data, sizeof(bind_data)); tx_packet(tx_buf, sizeof(bind_data) + 1); Green_LED_OFF; buzzerOff(); RF_Mode = Receive; rx_reset(); delay(50); if (RF_Mode == Received) { RF_Mode = Receive; spiSendAddress(0x7f); // Send the package read command if ('B' == spiReadData()) { sendBinds = 0; } } } if (!digitalRead(BTN)) { sendBinds = 1; } while (serialAvailable()) { Red_LED_ON; Green_LED_ON; switch (serialRead()) { case '\n': case '\r': printStrLn("Enter menu..."); handleCLI(); init_rfm(1); break; case '#': scannerMode(); break; default: break; } Red_LED_OFF; Green_LED_OFF; } } }