void APP_Run(void) { static const uint8_t chRf[] = {2, 26,80}; static const uint8_t chLe[] = {37,38,39}; uint8_t i, L, ch = 0; uint8_t buf[32]; nrf_cmd(0x20, 0x12); //on, no crc, int on RX/TX done nrf_cmd(0x21, 0x00); //no auto-acknowledge nrf_cmd(0x22, 0x00); //no RX nrf_cmd(0x23, 0x02); //5-byte address nrf_cmd(0x24, 0x00); //no auto-retransmit nrf_cmd(0x26, 0x06); //1MBps at 0dBm nrf_cmd(0x27, 0x3E); //clear various flags nrf_cmd(0x3C, 0x00); //no dynamic payloads nrf_cmd(0x3D, 0x00); //no features nrf_cmd(0x31, 32); //always RX 32 bytes nrf_cmd(0x22, 0x01); //RX on pipe 0 buf[0] = 0x30; //set addresses buf[1] = swapbits(0x8E); buf[2] = swapbits(0x89); buf[3] = swapbits(0xBE); buf[4] = swapbits(0xD6); #if 0 nrf_manybytes(buf, 5); #else RF1_WriteRegisterData(RF1_TX_ADDR, &buf[1], 4); #endif buf[0] = 0x2A; #if 0 nrf_manybytes(buf, 5); #else RF1_WriteRegisterData(RF1_RX_ADDR_P0, &buf[1], 4); #endif while(1) { L = 0; buf[L++] = 0x40; //PDU type, given address is random buf[L++] = 11; //17 bytes of payload buf[L++] = MY_MAC_0; buf[L++] = MY_MAC_1; buf[L++] = MY_MAC_2; buf[L++] = MY_MAC_3; buf[L++] = MY_MAC_4; buf[L++] = MY_MAC_5; buf[L++] = 2; //flags (LE-only, limited discovery mode) buf[L++] = 0x01; buf[L++] = 0x05; buf[L++] = 7; buf[L++] = 0x08; buf[L++] = 'n'; buf[L++] = 'R'; buf[L++] = 'F'; buf[L++] = ' '; buf[L++] = 'L'; buf[L++] = 'E'; buf[L++] = 0x55; //CRC start value: 0x555555 buf[L++] = 0x55; buf[L++] = 0x55; if(++ch == sizeof(chRf)) { /* channel hopping */ ch = 0; } //nrf_cmd(0x25, chRf[ch]); (void)RF1_SetChannel(chRf[ch]); //nrf_cmd(0x27, 0x6E); //clear flags RF1_WriteRegister(RF1_STATUS, RF1_STATUS_RX_DR|RF1_STATUS_TX_DS|RF1_STATUS_RX_P_NO_RX_FIFO_EMPTY); /* clear flags */ btLePacketEncode(buf, L, chLe[ch]); //nrf_simplebyte(0xE2); //Clear RX Fifo RF1_Write(RF1_FLUSH_RX); /* flush old data */ //nrf_simplebyte(0xE1); //Clear TX Fifo //RF1_Write(RF1_FLUSH_TX); /* flush old data */ /* done in RF1_TxPayload() */ RF1_TxPayload(buf, L); #if 0 cbi(PORTB, PIN_nCS); spi_byte(0xA0); for(i = 0 ; i < L ; i++) { spi_byte(buf[i]); } sbi(PORTB, PIN_nCS); nrf_cmd(0x20, 0x12); //tx on sbi(PORTB, PIN_CE); //do tx delay_ms(10); cbi(PORTB, PIN_CE); (in preparation of switching to RX quickly); #endif LED1_Neg(); WAIT1_Waitms(10); } /* for */ }
void send_beacon() { // Channel hopping ch++; if (ch>2 ) { ch = 0; } // sending on channel 37 only to use whitening array if (ONE_CHANNEL) ch = 0; xn_writereg(RF_CH, chRf[ch]); uint8_t L=0; #ifdef USE_IBEACON L=0; // ibeacon packet structure buf[L++] = B00100010; //PDU type, given address is random; 0x42 for Android and 0x40 for iPhone buf[L++] = 36; // length of payload buf[L++] = MY_MAC_0; buf[L++] = MY_MAC_1; buf[L++] = MY_MAC_2; buf[L++] = MY_MAC_3; buf[L++] = MY_MAC_4; buf[L++] = MY_MAC_5; // packet data unit buf[L++] = 2; //flags (LE-only, limited discovery mode) buf[L++] = 0x01; buf[L++] = 0x06; buf[L++] = 0x1A; // length of the name, including type byte buf[L++] = 0xff; buf[L++] = 0x4c; buf[L++] = 0x00; buf[L++] = 0x02; buf[L++] = 0x15; buf[L++] = 0x58; buf[L++] = 0x5c; buf[L++] = 0xde; buf[L++] = 0x93; buf[L++] = 0x1b; buf[L++] = 0x01; buf[L++] = 0x42; buf[L++] = 0xcc; buf[L++] = 0x9a; buf[L++] = 0x13; buf[L++] = 0x25; buf[L++] = 0x00; buf[L++] = 0x9b; buf[L++] = 0xed; buf[L++] = 0xc6; buf[L++] = 0xe5; buf[L++] = 0x00; buf[L++] = 0x00; buf[L++] = 0x00; buf[L++] = 0x00; buf[L++] = 0xCA; // tx power #else extern float vbattfilt; int vbatt = vbattfilt *1000.0f; unsigned int time = gettime(); time = time>>20; // divide by 1024*1024, no time for accuracy here time = time * 10; L=0; buf[L++] = B00100010; //PDU type, given address is random; 0x42 for Android and 0x40 for iPhone //buf[L++] = 0x42; //PDU type, given address is random; 0x42 for Android and 0x40 for iPhone // max len 27 with 5 byte address = 37 total payload bytes buf[L++] = 10+ 21; // length of payload buf[L++] = MY_MAC_0; buf[L++] = MY_MAC_1; buf[L++] = MY_MAC_2; buf[L++] = MY_MAC_3; buf[L++] = MY_MAC_4; buf[L++] = MY_MAC_5; // packet data unit buf[L++] = 2; //flags lenght(LE-only, limited discovery mode) buf[L++] = 0x01; // compulsory flags buf[L++] = 0x06; // flag value buf[L++] = 0x03; // Length of next block buf[L++] = 0x03; // Param: Service List buf[L++] = 0xAA; // Eddystone ID - 16 bit 0xFEAA buf[L++] = 0xFE; // Eddystone ID buf[L++] = 0x11; // Length of next block buf[L++] = 0x16; // Service Data buf[L++] = 0xAA; // Eddystone ID buf[L++] = 0xFE; // Eddystone ID buf[L++] = 0x20; // TLM flag buf[L++] = 0x00; // TLM version buf[L++] = vbatt>>8; // Battery voltage buf[L++] = vbatt; // Battery voltage buf[L++] = 0x80; // temperature 8.8 fixed point buf[L++] = 0x00; // temperature 8.8 fixed point buf[L++] = 0x00; // advertisment count 0 buf[L++] = 0x00; // advertisment count 1 buf[L++] = packetpersecond>>8&0xff; // advertisment count 2 buf[L++] = packetpersecond&0xff; // advertisment count 3 buf[L++] = time>>24; // powerup time 0 buf[L++] = time>>16; // powerup time 1 buf[L++] = time>>8; // powerup time 2 buf[L++] = time; // powerup time 3 in seconds times 10. #endif L=L+3; //crc btLePacketEncode(buf, L, ch ); // undo xn297 data whitening for (uint8_t i = 0; i < L; ++i) { buf[i] = buf[i] ^ xn297_scramble_rev[i+ XN297_ADDRESS_SIZE_BLE] ; } for( int i = 0 ; i < L ; i++) buffint[i] = buf[i]; xn_command( FLUSH_TX); xn_writereg( 0 , XN_TO_TX ); payloadsize = L; xn_writepayload( buffint , L ); ble_txtime = gettime(); return; }
void main (void) { static const uint8_t chRf[] = {2, 26,80}; static const uint8_t chLe[] = {37,38,39}; uint8_t i, L, ch = 0; // initialize buffer with set tx address command // address 8E 89 BE D6 bit-reversed // static rather than local saves 18 bytes static uint8_t buf[32]; //= {0x30, 0x6B, 0x7D, 0x91, 0x71}; SPI_init(); nrf_cmd(0x20, 0x12); //on, no crc, int on RX/TX done _delay_ms(2); // Tpd2stby //nrf_cmd(0x22, 0x00); //no RX nrf_cmd(0x21, 0x00); //no auto-acknowledge //nrf_cmd(0x22, 0x01); //RX on pipe 0 nrf_cmd(0x23, 0x02); //4-byte address nrf_cmd(0x24, 0x00); //no auto-retransmit //nrf_cmd(0x26, 0x06); //1MBps at 0dBm nrf_cmd(0x26, 0x00); //1MBps at -18dBm //nrf_cmd(0x27, 0x3E); //clear various flags //nrf_cmd(0x31, 32); //always RX 32 bytes buf[0] = 0x30; //set tx address buf[1] = swapbits(0x8E); buf[2] = swapbits(0x89); buf[3] = swapbits(0xBE); buf[4] = swapbits(0xD6); nrf_manybytes(buf, 5); //buf[0] = 0x2A; // rx address P0 //nrf_manybytes(buf, 5); while(1){ if(++ch == sizeof(chRf)){ ch = 0; sbi(NRF_PORT, DEBUG_PIN); _delay_ms(109); // wait between advertisements cbi(NRF_PORT, DEBUG_PIN); } L = 0; buf[L++] = 0x02; //PDU type, given address is random buf[L++] = 11; //11 + 6 = 17 bytes of payload buf[L++] = MY_MAC_0; buf[L++] = MY_MAC_1; buf[L++] = MY_MAC_2; buf[L++] = MY_MAC_3; buf[L++] = MY_MAC_4; buf[L++] = MY_MAC_5; buf[L++] = 2; //flags (LE-only, limited discovery mode) buf[L++] = 0x01; buf[L++] = 0x05; buf[L++] = 7; buf[L++] = 0x08; buf[L++] = 'n'; buf[L++] = 'R'; buf[L++] = 'F'; buf[L++] = ' '; buf[L++] = 'L'; buf[L++] = 'E'; buf[L++] = 0x55; //CRC start value: 0x555555 buf[L++] = 0x55; buf[L++] = 0x55; btLePacketEncode(buf, L, chLe[ch]); nrf_cmd(0x25, chRf[ch]); nrf_cmd(0x27, 0x6E); //clear flags //nrf_simplebyte(0xE2); //Clear RX Fifo //nrf_simplebyte(0xE1); //Clear TX Fifo cbi(NRF_PORT, PIN_nCS); spi_byte(0xA0); for(i = 0 ; i < L ; i++) spi_byte(buf[i]); sbi(NRF_PORT, PIN_nCS); //nrf_cmd(0x20, 0x12); //tx on // no CE pulse required when CE tied high sbi(NRF_PORT, PIN_CE); //do tx _delay_us(15); cbi(NRF_PORT, PIN_CE); // only delay for debugging - i.e. check IRQ trigger //_delay_ms(1); // let tx finish } }