diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 8f6cb885..4ea12c3e 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -38,6 +38,7 @@ add_executable (th02-example th02.cxx) add_executable (lsm303-example lsm303.cxx) add_executable (joystick12-example joystick12-example.cxx) add_executable (lol-example lol-example.cxx) +add_executable (nrf_ble_broadcast-example ble_broadcast.cxx) include_directories (${PROJECT_SOURCE_DIR}/src/hmc5883l) include_directories (${PROJECT_SOURCE_DIR}/src/grove) @@ -111,3 +112,4 @@ target_link_libraries (th02-example th02 ${CMAKE_THREAD_LIBS_INIT}) target_link_libraries (lsm303-example lsm303 ${CMAKE_THREAD_LIBS_INIT}) target_link_libraries (joystick12-example joystick12 ${CMAKE_THREAD_LIBS_INIT}) target_link_libraries (lol-example lol ${CMAKE_THREAD_LIBS_INIT}) +target_link_libraries (nrf_ble_broadcast-example nrf24l01 ${CMAKE_THREAD_LIBS_INIT}) diff --git a/examples/ble_broadcast.cxx b/examples/ble_broadcast.cxx new file mode 100644 index 00000000..86af2457 --- /dev/null +++ b/examples/ble_broadcast.cxx @@ -0,0 +1,81 @@ +/* + * Author: Yevgeniy Kiveisha + * Copyright (c) 2014 Intel Corporation. + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +#include +#include +#include +#include "nrf24l01.h" +#include +#include +#include + +int running = 0; +upm::NRF24L01 *sensor = NULL; + +void +sig_handler(int signo) +{ + printf("got signal\n"); + if (signo == SIGINT) { + printf("exiting application\n"); + running = 1; + } +} + +void nrf_handler () { +} + +int +main(int argc, char **argv) +{ + sensor = new upm::NRF24L01(7, 8); + sensor->setBeaconingMode (); + + std::vector msgs; + + msgs.push_back ("Hello World 1!!!"); + msgs.push_back ("Hello World 2!!!"); + msgs.push_back ("Hello World 3!!!"); + msgs.push_back ("Hello World 4!!!"); + msgs.push_back ("Hello World 5!!!"); + + signal(SIGINT, sig_handler); + + while (!running) { + for (std::vector::iterator item = msgs.begin(); item != msgs.end(); ++item) { + std::cout << "BROADCASTING " << (*item).c_str() << std::endl; + + for (int i = 0; i < 3; i++) { + sensor->sendBeaconingMsg ((uint8_t*) (*item).c_str()); + usleep (1000000); + } + } + } + + std::cout << "exiting application" << std::endl; + + msgs.clear(); + delete sensor; + return 0; +} diff --git a/examples/nrf_receiver.cxx b/examples/nrf_receiver.cxx index 16faa83c..c0ea19d1 100644 --- a/examples/nrf_receiver.cxx +++ b/examples/nrf_receiver.cxx @@ -29,7 +29,10 @@ #include int running = 0; -upm::NRF24l01 *comm = NULL; +upm::NRF24L01 *comm = NULL; + +uint8_t local_address[5] = {0x01, 0x01, 0x01, 0x01, 0x01}; +uint8_t broadcast_address[5] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF}; void sig_handler(int signo) @@ -43,7 +46,7 @@ sig_handler(int signo) //! [Interesting] void nrf_handler () { - std::cout << "devi1 :: " << *((uint32_t *)&(comm->m_rxBuffer[0])) << std::endl; + std::cout << "Reciever :: " << *((uint32_t *)&(comm->m_rxBuffer[0])) << std::endl; } //! [Interesting] @@ -51,17 +54,19 @@ int main(int argc, char **argv) { //! [Interesting] - comm = new upm::NRF24l01(7); - comm->nrfSetRXaddr ((uint8_t *) "devi1"); - comm->nrfSetTXaddr ((uint8_t *) "devi2"); - comm->nrfSetPayload (MAX_BUFFER); - comm->nrfConfigModule (); + comm = new upm::NRF24L01(7, 8); + comm->setSourceAddress ((uint8_t *) local_address); + comm->setDestinationAddress ((uint8_t *) broadcast_address); + comm->setPayload (MAX_BUFFER); + comm->configure (); + comm->setSpeedRate (upm::NRF_250KBPS); + comm->setChannel (99); comm->dataRecievedHandler = nrf_handler; signal(SIGINT, sig_handler); while (!running) { - comm->nrfListenForChannel (); + comm->pollListener (); } std::cout << "exiting application" << std::endl; diff --git a/examples/nrf_transmitter.cxx b/examples/nrf_transmitter.cxx index 2761442a..3d571792 100644 --- a/examples/nrf_transmitter.cxx +++ b/examples/nrf_transmitter.cxx @@ -29,7 +29,10 @@ #include int running = 0; -upm::NRF24l01 *comm = NULL; +upm::NRF24L01 *comm = NULL; + +uint8_t destAddress[5] = {0x01, 0x01, 0x01, 0x01, 0x02}; +uint8_t srcAddress[5] = {0x01, 0x01, 0x01, 0x01, 0x01}; void sig_handler(int signo) @@ -49,18 +52,19 @@ main(int argc, char **argv) { //! [Interesting] uint32_t dummyData = 0; - comm = new upm::NRF24l01(7); - comm->nrfSetRXaddr ((uint8_t *) "devi2"); - comm->nrfSetTXaddr ((uint8_t *) "devi1"); - comm->nrfSetPayload (MAX_BUFFER); - comm->nrfConfigModule (); + comm = new upm::NRF24L01(7, 8); + comm->setSourceAddress ((uint8_t *) srcAddress); + comm->setDestinationAddress ((uint8_t *) destAddress); + comm->setPayload (MAX_BUFFER); + // comm->setChannel (99); + comm->configure (); comm->dataRecievedHandler = nrf_handler; signal(SIGINT, sig_handler); while (!running) { memcpy (comm->m_txBuffer, &dummyData, sizeof (dummyData)); - comm->nrfSend (); + comm->send (); std::cout << "devi2 :: sending data ...." << dummyData << std::endl; usleep (3000000); dummyData += 3000; diff --git a/src/nrf24l01/nrf24l01.cxx b/src/nrf24l01/nrf24l01.cxx index 631b0ef5..08f66c78 100644 --- a/src/nrf24l01/nrf24l01.cxx +++ b/src/nrf24l01/nrf24l01.cxx @@ -1,6 +1,7 @@ /* * Author: Yevgeniy Kiveisha * Copyright (c) 2014 Intel Corporation. + * BLE Beaconing based on http://dmitry.gr/index.php?r=05.Projects&proj=11.%20Bluetooth%20LE%20fakery * * Permission is hereby granted, free of charge, to any person obtaining * a copy of this software and associated documentation files (the @@ -30,12 +31,12 @@ using namespace upm; -NRF24l01::NRF24l01 (uint8_t cs) { +NRF24L01::NRF24L01 (uint8_t cs, uint8_t ce) { mraa_init(); - nrfInitModule (cs, 8); + init (cs, ce); } -NRF24l01::~NRF24l01 () { +NRF24L01::~NRF24L01 () { mraa_result_t error = MRAA_SUCCESS; error = mraa_spi_stop(m_spi); if (error != MRAA_SUCCESS) { @@ -52,12 +53,12 @@ NRF24l01::~NRF24l01 () { } void -NRF24l01::nrfInitModule (uint8_t chip_select, uint8_t chip_enable) { +NRF24L01::init (uint8_t chip_select, uint8_t chip_enable) { mraa_result_t error = MRAA_SUCCESS; - m_csn = chip_select; - m_ce = chip_enable; - m_channel = 1; + m_csn = chip_select; + m_ce = chip_enable; + m_channel = 99; m_csnPinCtx = mraa_gpio_init (m_csn); if (m_csnPinCtx == NULL) { @@ -81,163 +82,35 @@ NRF24l01::nrfInitModule (uint8_t chip_select, uint8_t chip_enable) { mraa_result_print (error); } - nrfCELow (); + ceLow(); + csOff (); + m_spi = mraa_spi_init (0); } void -NRF24l01::nrfConfigModule() { +NRF24L01::configure () { /* Set RF channel */ - nrfConfigRegister (RF_CH, m_channel); + setRegister (RF_CH, m_channel); /* Set length of incoming payload */ - nrfConfigRegister (RX_PW_P0, m_payload); - nrfConfigRegister (RX_PW_P1, m_payload); + setRegister (RX_PW_P0, m_payload); + /* Set length of incoming payload for broadcast */ - nrfConfigRegister (RX_PW_P2, m_payload); + setRegister (RX_PW_P1, m_payload); /* Start receiver */ - nrfPowerUpRX (); - nrfFlushRX (); -} - -/* Clocks only one byte into the given MiRF register */ -void -NRF24l01::nrfConfigRegister(uint8_t reg, uint8_t value) { - nrfCSOn (); - mraa_spi_write (m_spi, W_REGISTER | (REGISTER_MASK & reg)); - mraa_spi_write (m_spi, value); - nrfCSOff (); + rxPowerUp (); + rxFlushBuffer (); } void -NRF24l01::nrfPowerUpRX() { - m_ptx = 0; - nrfCELow(); - nrfConfigRegister(CONFIG, NRF_CONFIG | ( (1<>= 1){ + t = dst[0] >> 7; + dst[0] <<= 1; + if(dst[1] & 0x80) dst[0] |= 1; + dst[1] <<= 1; + if(dst[2] & 0x80) dst[1] |= 1; + dst[2] <<= 1; + + if(t != (d & 1)) { + dst[2] ^= 0x5B; + dst[1] ^= 0x06; + } + } + } +} + +void +NRF24L01::bleWhiten (uint8_t* data, uint8_t len, uint8_t whitenCoeff) { + uint8_t m; + while(len--) { + for(m = 1; m; m <<= 1) { + if(whitenCoeff & 0x80){ + whitenCoeff ^= 0x11; + (*data) ^= m; + } + whitenCoeff <<= 1; + } + data++; + } +} + +void +NRF24L01::blePacketEncode(uint8_t* packet, uint8_t len, uint8_t chan) { + //length is of packet, including crc. pre-populate crc in packet with initial crc value! + uint8_t i, dataLen = len - 3; + + bleCrc(packet, dataLen, packet + dataLen); + for(i = 0; i < 3; i++, dataLen++) { + packet[dataLen] = swapbits(packet[dataLen]); + } + + bleWhiten(packet, len, (swapbits(chan) | 2)); + for(i = 0; i < len; i++) { + packet[i] = swapbits(packet[i]); + } +} + +uint8_t +NRF24L01::swapbits(uint8_t a) { + uint8_t v = 0; + + if(a & 0x80) v |= 0x01; + if(a & 0x40) v |= 0x02; + if(a & 0x20) v |= 0x04; + if(a & 0x10) v |= 0x08; + if(a & 0x08) v |= 0x10; + if(a & 0x04) v |= 0x20; + if(a & 0x02) v |= 0x40; + if(a & 0x01) v |= 0x80; + + return v; +} diff --git a/src/nrf24l01/nrf24l01.h b/src/nrf24l01/nrf24l01.h index 76cea9f0..6e7258e8 100644 --- a/src/nrf24l01/nrf24l01.h +++ b/src/nrf24l01/nrf24l01.h @@ -1,6 +1,7 @@ /* * Author: Yevgeniy Kiveisha * Copyright (c) 2014 Intel Corporation. + * BLE Beaconing based on http://dmitry.gr/index.php?r=05.Projects&proj=11.%20Bluetooth%20LE%20fakery * * Permission is hereby granted, free of charge, to any person obtaining * a copy of this software and associated documentation files (the @@ -27,6 +28,7 @@ #include #include #include +#include /* Memory Map */ #define CONFIG 0x00 @@ -53,6 +55,8 @@ #define RX_PW_P4 0x15 #define RX_PW_P5 0x16 #define FIFO_STATUS 0x17 +#define DYNPD 0x1C +#define FEATURE 0x1D /* Bit Mnemonics */ #define MASK_RX_DR 6 @@ -105,19 +109,47 @@ #define REUSE_TX_PL 0xE3 #define NOP 0xFF +#define RF_DR_LOW 5 +#define RF_DR_HIGH 3 +#define RF_PWR_LOW 1 +#define RF_PWR_HIGH 2 + /* Nrf24l settings */ #define ADDR_LEN 5 -#define NRF_CONFIG ((1<