nrf24l01: added new feature BLE broadcasting with example and new methods to nrf24l01 (setChannel, setPower, etc... )

Signed-off-by: Kiveisha Yevgeniy <yevgeniy.kiveisha@intel.com>
This commit is contained in:
Kiveisha Yevgeniy 2014-09-10 18:55:16 +00:00
parent 4aa51c0edb
commit 81e1fd281c
6 changed files with 656 additions and 306 deletions

View File

@ -38,6 +38,7 @@ add_executable (th02-example th02.cxx)
add_executable (lsm303-example lsm303.cxx) add_executable (lsm303-example lsm303.cxx)
add_executable (joystick12-example joystick12-example.cxx) add_executable (joystick12-example joystick12-example.cxx)
add_executable (lol-example lol-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/hmc5883l)
include_directories (${PROJECT_SOURCE_DIR}/src/grove) 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 (lsm303-example lsm303 ${CMAKE_THREAD_LIBS_INIT})
target_link_libraries (joystick12-example joystick12 ${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 (lol-example lol ${CMAKE_THREAD_LIBS_INIT})
target_link_libraries (nrf_ble_broadcast-example nrf24l01 ${CMAKE_THREAD_LIBS_INIT})

View File

@ -0,0 +1,81 @@
/*
* Author: Yevgeniy Kiveisha <yevgeniy.kiveisha@intel.com>
* 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 <string.h>
#include <unistd.h>
#include <iostream>
#include "nrf24l01.h"
#include <signal.h>
#include <stdio.h>
#include <vector>
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<std::string> 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<std::string>::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;
}

View File

@ -29,7 +29,10 @@
#include <signal.h> #include <signal.h>
int running = 0; 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 void
sig_handler(int signo) sig_handler(int signo)
@ -43,7 +46,7 @@ sig_handler(int signo)
//! [Interesting] //! [Interesting]
void nrf_handler () { 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] //! [Interesting]
@ -51,17 +54,19 @@ int
main(int argc, char **argv) main(int argc, char **argv)
{ {
//! [Interesting] //! [Interesting]
comm = new upm::NRF24l01(7); comm = new upm::NRF24L01(7, 8);
comm->nrfSetRXaddr ((uint8_t *) "devi1"); comm->setSourceAddress ((uint8_t *) local_address);
comm->nrfSetTXaddr ((uint8_t *) "devi2"); comm->setDestinationAddress ((uint8_t *) broadcast_address);
comm->nrfSetPayload (MAX_BUFFER); comm->setPayload (MAX_BUFFER);
comm->nrfConfigModule (); comm->configure ();
comm->setSpeedRate (upm::NRF_250KBPS);
comm->setChannel (99);
comm->dataRecievedHandler = nrf_handler; comm->dataRecievedHandler = nrf_handler;
signal(SIGINT, sig_handler); signal(SIGINT, sig_handler);
while (!running) { while (!running) {
comm->nrfListenForChannel (); comm->pollListener ();
} }
std::cout << "exiting application" << std::endl; std::cout << "exiting application" << std::endl;

View File

@ -29,7 +29,10 @@
#include <signal.h> #include <signal.h>
int running = 0; 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 void
sig_handler(int signo) sig_handler(int signo)
@ -49,18 +52,19 @@ main(int argc, char **argv)
{ {
//! [Interesting] //! [Interesting]
uint32_t dummyData = 0; uint32_t dummyData = 0;
comm = new upm::NRF24l01(7); comm = new upm::NRF24L01(7, 8);
comm->nrfSetRXaddr ((uint8_t *) "devi2"); comm->setSourceAddress ((uint8_t *) srcAddress);
comm->nrfSetTXaddr ((uint8_t *) "devi1"); comm->setDestinationAddress ((uint8_t *) destAddress);
comm->nrfSetPayload (MAX_BUFFER); comm->setPayload (MAX_BUFFER);
comm->nrfConfigModule (); // comm->setChannel (99);
comm->configure ();
comm->dataRecievedHandler = nrf_handler; comm->dataRecievedHandler = nrf_handler;
signal(SIGINT, sig_handler); signal(SIGINT, sig_handler);
while (!running) { while (!running) {
memcpy (comm->m_txBuffer, &dummyData, sizeof (dummyData)); memcpy (comm->m_txBuffer, &dummyData, sizeof (dummyData));
comm->nrfSend (); comm->send ();
std::cout << "devi2 :: sending data ...." << dummyData << std::endl; std::cout << "devi2 :: sending data ...." << dummyData << std::endl;
usleep (3000000); usleep (3000000);
dummyData += 3000; dummyData += 3000;

View File

@ -1,6 +1,7 @@
/* /*
* Author: Yevgeniy Kiveisha <yevgeniy.kiveisha@intel.com> * Author: Yevgeniy Kiveisha <yevgeniy.kiveisha@intel.com>
* Copyright (c) 2014 Intel Corporation. * 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 * Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the * a copy of this software and associated documentation files (the
@ -30,12 +31,12 @@
using namespace upm; using namespace upm;
NRF24l01::NRF24l01 (uint8_t cs) { NRF24L01::NRF24L01 (uint8_t cs, uint8_t ce) {
mraa_init(); mraa_init();
nrfInitModule (cs, 8); init (cs, ce);
} }
NRF24l01::~NRF24l01 () { NRF24L01::~NRF24L01 () {
mraa_result_t error = MRAA_SUCCESS; mraa_result_t error = MRAA_SUCCESS;
error = mraa_spi_stop(m_spi); error = mraa_spi_stop(m_spi);
if (error != MRAA_SUCCESS) { if (error != MRAA_SUCCESS) {
@ -52,12 +53,12 @@ NRF24l01::~NRF24l01 () {
} }
void 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; mraa_result_t error = MRAA_SUCCESS;
m_csn = chip_select; m_csn = chip_select;
m_ce = chip_enable; m_ce = chip_enable;
m_channel = 1; m_channel = 99;
m_csnPinCtx = mraa_gpio_init (m_csn); m_csnPinCtx = mraa_gpio_init (m_csn);
if (m_csnPinCtx == NULL) { if (m_csnPinCtx == NULL) {
@ -81,163 +82,35 @@ NRF24l01::nrfInitModule (uint8_t chip_select, uint8_t chip_enable) {
mraa_result_print (error); mraa_result_print (error);
} }
nrfCELow (); ceLow();
csOff ();
m_spi = mraa_spi_init (0); m_spi = mraa_spi_init (0);
} }
void void
NRF24l01::nrfConfigModule() { NRF24L01::configure () {
/* Set RF channel */ /* Set RF channel */
nrfConfigRegister (RF_CH, m_channel); setRegister (RF_CH, m_channel);
/* Set length of incoming payload */ /* Set length of incoming payload */
nrfConfigRegister (RX_PW_P0, m_payload); setRegister (RX_PW_P0, m_payload);
nrfConfigRegister (RX_PW_P1, m_payload);
/* Set length of incoming payload for broadcast */ /* Set length of incoming payload for broadcast */
nrfConfigRegister (RX_PW_P2, m_payload); setRegister (RX_PW_P1, m_payload);
/* Start receiver */ /* Start receiver */
nrfPowerUpRX (); rxPowerUp ();
nrfFlushRX (); rxFlushBuffer ();
}
/* 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 ();
} }
void void
NRF24l01::nrfPowerUpRX() { NRF24L01::send (uint8_t * value) {
m_ptx = 0;
nrfCELow();
nrfConfigRegister(CONFIG, NRF_CONFIG | ( (1<<PWR_UP) | (1<<PRIM_RX) ) );
nrfCEHigh();
nrfConfigRegister(STATUS,(1 << TX_DS) | (1 << MAX_RT));
}
void
NRF24l01::nrfFlushRX() {
nrfCSOn ();
mraa_spi_write (m_spi, FLUSH_RX);
nrfCSOff ();
}
/* Sets the receiving address */
void
NRF24l01::nrfSetRXaddr(uint8_t * addr) {
nrfCELow();
nrfWriteRegister(RX_ADDR_P1, addr, ADDR_LEN);
nrfCEHigh();
}
/* Sets the transmitting address */
void
NRF24l01::nrfSetTXaddr(uint8_t * addr)
{
/* RX_ADDR_P0 must be set to the sending addr for auto ack to work. */
nrfWriteRegister (RX_ADDR_P0, addr, ADDR_LEN);
nrfWriteRegister (TX_ADDR, addr, ADDR_LEN);
}
/* The broadcast address should be 0xFFFFF */
void
NRF24l01::nrfSetBroadcastAddr (uint8_t * addr) {
nrfCELow ();
nrfWriteRegister (RX_ADDR_P2, addr, ADDR_LEN);
nrfCEHigh ();
}
void
NRF24l01::nrfSetPayload (uint8_t load) {
m_payload = load;
}
void
NRF24l01::nrfWriteRegister(uint8_t reg, uint8_t * value, uint8_t len)
{
nrfCSOn ();
mraa_spi_write (m_spi, W_REGISTER | (REGISTER_MASK & reg));
nrfTransmitSync(value, len);
nrfCSOff ();
}
void
NRF24l01::nrfTransmitSync(uint8_t *dataout, uint8_t len){
uint8_t i;
for(i = 0; i < len; i++) {
mraa_spi_write (m_spi, dataout[i]);
}
}
/* Checks if data is available for reading */
bool
NRF24l01::nrfDataReady() {
uint8_t status = nrfGetStatus();
if ( status & (1 << RX_DR) ) {
return 1;
}
return !nrfRXFifoEmpty();
}
uint8_t
NRF24l01::nrfGetStatus () {
uint8_t rv;
nrfReadRegister (STATUS, &rv, 1);
return rv;
}
/* Reads an array of bytes from the given start position in the MiRF registers. */
void
NRF24l01::nrfReadRegister (uint8_t reg, uint8_t * value, uint8_t len)
{
nrfCSOn ();
mraa_spi_write (m_spi, R_REGISTER | (REGISTER_MASK & reg));
nrfTransferSync (value, value, len);
nrfCSOff ();
}
void
NRF24l01::nrfTransferSync (uint8_t *dataout,uint8_t *datain,uint8_t len) {
uint8_t i;
for(i = 0;i < len;i++) {
datain[i] = mraa_spi_write (m_spi, dataout[i]);
}
}
bool
NRF24l01::nrfRXFifoEmpty () {
uint8_t fifo_status;
nrfReadRegister (FIFO_STATUS, &fifo_status, sizeof(fifo_status));
return (fifo_status & (1 << RX_EMPTY));
}
/* Reads payload bytes into data array */
void
NRF24l01::nrfGetData (uint8_t * data)
{
nrfCSOn ();
/* Send cmd to read rx payload */
mraa_spi_write (m_spi, R_RX_PAYLOAD);
/* Read payload */
nrfTransferSync(data, data, m_payload);
nrfCSOff ();
nrfConfigRegister(STATUS, (1<<RX_DR));
}
/* Sends a data package to the default address. Be sure to send the correct
* amount of bytes as configured as payload on the receiver. */
void
NRF24l01::nrfSend(uint8_t * value) {
uint8_t status; uint8_t status;
status = nrfGetStatus(); status = getStatus();
while (m_ptx) { while (m_ptx) {
status = nrfGetStatus(); status = getStatus();
if((status & ((1 << TX_DS) | (1 << MAX_RT)))){ if((status & ((1 << TX_DS) | (1 << MAX_RT)))){
m_ptx = 0; m_ptx = 0;
@ -245,32 +118,69 @@ NRF24l01::nrfSend(uint8_t * value) {
} }
} // Wait until last paket is send } // Wait until last paket is send
nrfCELow(); ceLow ();
nrfPowerUpTX(); // Set to transmitter mode , Power up txPowerUp (); // Set to transmitter mode , Power up
nrfCSOn (); txFlushBuffer ();
mraa_spi_write (m_spi, FLUSH_TX); // Write cmd to flush tx fifo
nrfCSOff ();
nrfCSOn (); csOn ();
mraa_spi_write (m_spi, W_TX_PAYLOAD); // Write cmd to write payload mraa_spi_write (m_spi, W_TX_PAYLOAD); // Write cmd to write payload
nrfTransmitSync(value, m_payload); // Write payload writeBytes (value, NULL, m_payload); // Write payload
nrfCSOff (); csOff ();
nrfCEHigh(); // Start transmission ceHigh(); // Start transmission
while (dataSending ()) { }
usleep (10000);
} }
void void
NRF24l01::nrfSend () { NRF24L01::send () {
nrfSend (m_txBuffer); send (m_txBuffer);
}
void
NRF24L01::setSourceAddress (uint8_t * addr) {
ceLow ();
writeRegister (RX_ADDR_P0, addr, ADDR_LEN);
ceHigh ();
}
void
NRF24L01::setDestinationAddress (uint8_t * addr) {
writeRegister (TX_ADDR, addr, ADDR_LEN);
}
void
NRF24L01::setBroadcastAddress (uint8_t * addr) {
writeRegister (RX_ADDR_P1, addr, ADDR_LEN);
}
void
NRF24L01::setPayload (uint8_t payload) {
m_payload = payload;
} }
bool bool
NRF24l01::nrfIsSending () { NRF24L01::dataReady () {
/* See note in getData() function - just checking RX_DR isn't good enough */
uint8_t status = getStatus();
/* We can short circuit on RX_DR, but if it's not set, we still need
* to check the FIFO for any pending packets */
if ( status & (1 << RX_DR) ) {
return 1;
}
return !rxFifoEmpty();
}
bool
NRF24L01::dataSending () {
uint8_t status; uint8_t status;
if (m_ptx) { // Sending mode. if(m_ptx) { // Sending mode.
status = nrfGetStatus(); status = getStatus();
/* if sending successful (TX_DS) or max retries exceded (MAX_RT). */ /* if sending successful (TX_DS) or max retries exceded (MAX_RT). */
if((status & ((1 << TX_DS) | (1 << MAX_RT)))){ if((status & ((1 << TX_DS) | (1 << MAX_RT)))){
nrfPowerUpRX(); rxPowerUp ();
return false; return false;
} }
return true; return true;
@ -279,41 +189,347 @@ NRF24l01::nrfIsSending () {
} }
void void
NRF24l01::nrfPowerUpTX () { NRF24L01::getData (uint8_t * data) {
m_ptx = 1; csOn ();
nrfConfigRegister (CONFIG, NRF_CONFIG | ( (1<<PWR_UP) | (0<<PRIM_RX) ) ); /* Send cmd to read rx payload */
mraa_spi_write (m_spi, R_RX_PAYLOAD);
/* Read payload */
writeBytes (data, data, m_payload);
csOff ();
/* NVI: per product spec, p 67, note c:
* "The RX_DR IRQ is asserted by a new packet arrival event. The procedure
* for handling this interrupt should be: 1) read payload through SPI,
* 2) clear RX_DR IRQ, 3) read FIFO_STATUS to check if there are more
* payloads available in RX FIFO, 4) if there are more data in RX FIFO,
* repeat from step 1)."
* So if we're going to clear RX_DR here, we need to check the RX FIFO
* in the dataReady() function */
/* Reset status register */
setRegister (STATUS, (1<<RX_DR));
}
uint8_t
NRF24L01::getStatus() {
return getRegister (STATUS);
}
bool
NRF24L01::rxFifoEmpty () {
uint8_t fifoStatus = getRegister (FIFO_STATUS);
return (fifoStatus & (1 << RX_EMPTY));
} }
void void
NRF24l01::nrfPowerDown () { NRF24L01::rxPowerUp () {
nrfCELow (); m_ptx = 0;
nrfConfigRegister (CONFIG, NRF_CONFIG); ceLow ();
setRegister (CONFIG, _CONFIG | ( (1 << PWR_UP) | (1 << PRIM_RX) ));
ceHigh ();
setRegister (STATUS, (1 << TX_DS) | (1 << MAX_RT));
}
void
NRF24L01::rxFlushBuffer () {
sendCommand (FLUSH_RX);
}
void
NRF24L01::txPowerUp () {
m_ptx = 1;
setRegister (CONFIG, _CONFIG | ( (1 << PWR_UP) | (0 << PRIM_RX) ));
}
void
NRF24L01::powerDown(){
ceLow ();
setRegister (CONFIG, _CONFIG);
}
void
NRF24L01::setChannel (uint8_t channel) {
m_channel = channel;
setRegister (RF_CH, channel);
}
void
NRF24L01::setPower (power_t power) {
uint8_t setupRegisterData = 0;
switch (power) {
case NRF_0DBM:
m_power = 3;
break;
case NRF_6DBM:
m_power = 2;
break;
case NRF_12DBM:
m_power = 1;
break;
case NRF_18DBM:
m_power = 0;
break;
}
setupRegisterData = getRegister (RF_SETUP); // Read current value.
setupRegisterData &= 0xFC; // Erase the old value;
setupRegisterData |= (m_power & 0x3);
setRegister (RF_SETUP, setupRegisterData); // Write the new value.
}
uint8_t
NRF24L01::setSpeedRate (speed_rate_t rate) {
uint8_t setupRegisterData = 0;
setupRegisterData = getRegister (RF_SETUP); // Read current value.
setupRegisterData &= ~((1 << RF_DR_LOW) | (1 << RF_DR_HIGH));
switch (rate) {
case NRF_250KBPS:
setupRegisterData |= (1 << RF_DR_LOW) ;
break;
case NRF_1MBPS:
break;
case NRF_2MBPS:
setupRegisterData |= (1 << RF_DR_HIGH);
break;
}
setRegister (RF_SETUP, setupRegisterData); // Write the new value.
if (setupRegisterData == getRegister (RF_SETUP)) {
return 0x0;
}
return 0x1;
} }
mraa_result_t mraa_result_t
NRF24l01::nrfCEHigh () { NRF24L01::ceHigh () {
return mraa_gpio_write (m_cePinCtx, HIGH); return mraa_gpio_write (m_cePinCtx, HIGH);
} }
mraa_result_t mraa_result_t
NRF24l01::nrfCELow () { NRF24L01::ceLow () {
return mraa_gpio_write (m_cePinCtx, LOW); return mraa_gpio_write (m_cePinCtx, LOW);
} }
mraa_result_t mraa_result_t
NRF24l01::nrfCSOn () { NRF24L01::csOn () {
return mraa_gpio_write (m_csnPinCtx, LOW); return mraa_gpio_write (m_csnPinCtx, LOW);
} }
mraa_result_t mraa_result_t
NRF24l01::nrfCSOff () { NRF24L01::csOff () {
return mraa_gpio_write (m_csnPinCtx, HIGH); return mraa_gpio_write (m_csnPinCtx, HIGH);
} }
void void
NRF24l01::nrfListenForChannel() { NRF24L01::pollListener() {
if(!nrfIsSending() && nrfDataReady()) { if (dataReady()) {
nrfGetData(m_rxBuffer); getData (m_rxBuffer);
dataRecievedHandler(); /* let know that data arrived */ dataRecievedHandler (); /* let know that data arrived */
} }
} }
void
NRF24L01::txFlushBuffer () {
sendCommand (FLUSH_TX);
}
void
NRF24L01::setBeaconingMode () {
setRegister (CONFIG, 0x12); // on, no crc, int on RX/TX done
setRegister (EN_AA, 0x00); // no auto-acknowledge
setRegister (EN_RXADDR, 0x00); // no RX
setRegister (SETUP_AW, 0x02); // 5-byte address
setRegister (SETUP_RETR, 0x00); // no auto-retransmit
setRegister (RF_SETUP, 0x06); // 1MBps at 0dBm
setRegister (STATUS, 0x3E); // clear various flags
setRegister (DYNPD, 0x00); // no dynamic payloads
setRegister (FEATURE, 0x00); // no features
setRegister (RX_PW_P0, 32); // always RX 32 bytes
setRegister (EN_RXADDR, 0x01); // RX on pipe 0
uint8_t addr[4] = { swapbits(0x8E), swapbits(0x89), swapbits(0xBE), swapbits(0xD6)};
writeRegister (TX_ADDR, addr, 4);
writeRegister (RX_ADDR_P0, addr, 4);
uint8_t index = 0;
m_bleBuffer[index++] = 0x42; // PDU type, given address is random
m_bleBuffer[index++] = 0x1B; // 6+3+2+16 = 27 bytes of payload
m_bleBuffer[index++] = BLE_MAC_0;
m_bleBuffer[index++] = BLE_MAC_1;
m_bleBuffer[index++] = BLE_MAC_2;
m_bleBuffer[index++] = BLE_MAC_3;
m_bleBuffer[index++] = BLE_MAC_4;
m_bleBuffer[index++] = BLE_MAC_5;
m_bleBuffer[index++] = 2; // flags (LE-only, limited discovery mode)
m_bleBuffer[index++] = 0x01;
m_bleBuffer[index++] = 0x05;
m_bleBuffer[index++] = 17;
m_bleBuffer[index++] = 0x08;
}
void
NRF24L01::sendBeaconingMsg (uint8_t * msg) {
const uint8_t chRf[] = {2, 26,80};
const uint8_t chLe[] = {37,38,39};
uint8_t index = BLE_PAYLOAD_OFFSET + 16;
memcpy (&m_bleBuffer[BLE_PAYLOAD_OFFSET], msg, 16);
m_bleBuffer[index++] = 0x55;
m_bleBuffer[index++] = 0x55;
m_bleBuffer[index++] = 0x55;
uint8_t channel = 0;
while (++channel != sizeof(chRf)) {
setRegister (RF_CH, chRf[channel]);
setRegister (STATUS, 0x6E); //clear flags
blePacketEncode (m_bleBuffer, index, chLe[channel]);
sendCommand (FLUSH_TX); // Clear RX Fifo
sendCommand (FLUSH_RX); // Clear TX Fifo
csOn ();
mraa_spi_write (m_spi, W_TX_PAYLOAD); // Write cmd to write payload
writeBytes (m_bleBuffer, NULL, 32); // Write payload
csOff ();
setRegister (CONFIG, 0x12); // tx on
ceHigh (); // Start transmission
usleep (10000);
ceLow ();
}
}
/*
* ---------------
* PRIVATE SECTION
* ---------------
*/
void
NRF24L01::writeBytes (uint8_t * dataout, uint8_t * datain, uint8_t len) {
for (uint8_t i = 0; i < len; i++) {
if (datain != NULL) {
datain[i] = mraa_spi_write (m_spi, dataout[i]);
} else {
mraa_spi_write (m_spi, dataout[i]);
}
}
}
void
NRF24L01::setRegister (uint8_t reg, uint8_t value) {
csOn ();
mraa_spi_write (m_spi, W_REGISTER | (REGISTER_MASK & reg));
mraa_spi_write (m_spi, value);
csOff ();
}
uint8_t
NRF24L01::getRegister (uint8_t reg) {
uint8_t data = 0;
csOn ();
mraa_spi_write (m_spi, R_REGISTER | (REGISTER_MASK & reg));
data = mraa_spi_write (m_spi, data);
csOff ();
return data;
}
void
NRF24L01::readRegister (uint8_t reg, uint8_t * value, uint8_t len) {
csOn ();
mraa_spi_write (m_spi, R_REGISTER | (REGISTER_MASK & reg));
writeBytes (value, value, len);
csOff ();
}
void
NRF24L01::writeRegister (uint8_t reg, uint8_t * value, uint8_t len) {
csOn ();
mraa_spi_write (m_spi, W_REGISTER | (REGISTER_MASK & reg));
writeBytes (value, NULL, len);
csOff ();
}
void
NRF24L01::sendCommand (uint8_t cmd) {
csOn ();
mraa_spi_write (m_spi, cmd);
csOff ();
}
void
NRF24L01::bleCrc (const uint8_t* data, uint8_t len, uint8_t* dst) {
uint8_t v, t, d;
while(len--) {
d = *data++;
for(v = 0; v < 8; v++, d >>= 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;
}

View File

@ -1,6 +1,7 @@
/* /*
* Author: Yevgeniy Kiveisha <yevgeniy.kiveisha@intel.com> * Author: Yevgeniy Kiveisha <yevgeniy.kiveisha@intel.com>
* Copyright (c) 2014 Intel Corporation. * 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 * Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the * a copy of this software and associated documentation files (the
@ -27,6 +28,7 @@
#include <mraa/aio.h> #include <mraa/aio.h>
#include <mraa/gpio.h> #include <mraa/gpio.h>
#include <mraa/spi.h> #include <mraa/spi.h>
#include <cstring>
/* Memory Map */ /* Memory Map */
#define CONFIG 0x00 #define CONFIG 0x00
@ -53,6 +55,8 @@
#define RX_PW_P4 0x15 #define RX_PW_P4 0x15
#define RX_PW_P5 0x16 #define RX_PW_P5 0x16
#define FIFO_STATUS 0x17 #define FIFO_STATUS 0x17
#define DYNPD 0x1C
#define FEATURE 0x1D
/* Bit Mnemonics */ /* Bit Mnemonics */
#define MASK_RX_DR 6 #define MASK_RX_DR 6
@ -105,19 +109,47 @@
#define REUSE_TX_PL 0xE3 #define REUSE_TX_PL 0xE3
#define NOP 0xFF #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 */ /* Nrf24l settings */
#define ADDR_LEN 5 #define ADDR_LEN 5
#define NRF_CONFIG ((1<<EN_CRC) | (0<<CRCO) ) #define _CONFIG ((1<<EN_CRC) | (0<<CRCO) )
#define MAX_BUFFER 32 #define MAX_BUFFER 32
#define HIGH 1 #define HIGH 1
#define LOW 0 #define LOW 0
/* BLE beaconing */
#define BLE_MAC_0 0xEF
#define BLE_MAC_1 0xFF
#define BLE_MAC_2 0xC0
#define BLE_MAC_3 0xAA
#define BLE_MAC_4 0x18
#define BLE_MAC_5 0x00
#define BLE_PAYLOAD_OFFSET 13
namespace upm { namespace upm {
typedef void (* funcPtrVoidVoid) (); typedef void (* funcPtrVoidVoid) ();
typedef enum {
NRF_250KBPS = 0,
NRF_1MBPS = 1,
NRF_2MBPS = 2,
} speed_rate_t;
typedef enum {
NRF_0DBM = 0,
NRF_6DBM = 1,
NRF_12DBM = 2,
NRF_18DBM = 3,
} power_t;
/** /**
* @brief C++ API for NRF24l01 transceiver module * @brief C++ API for NRF24l01 transceiver module
* *
@ -126,19 +158,19 @@ typedef void (* funcPtrVoidVoid) ();
* @snippet nrf_receiver.cxx Interesting * @snippet nrf_receiver.cxx Interesting
* @snippet nrf_transmitter.cxx Interesting * @snippet nrf_transmitter.cxx Interesting
*/ */
class NRF24l01 { class NRF24L01 {
public: public:
/** /**
* Instanciates a NRF24l01 object * Instanciates a NRF24l01 object
* *
* @param cs chip select pin * @param cs chip select pin
*/ */
NRF24l01 (uint8_t cs); NRF24L01 (uint8_t cs, uint8_t ce);
/** /**
* NRF24l01 object destructor * NRF24l01 object destructor
*/ */
~NRF24l01 (); ~NRF24L01 ();
/** /**
* Return name of the component * Return name of the component
@ -154,32 +186,32 @@ class NRF24l01 {
* @param chipSelect setting up the chip select pin * @param chipSelect setting up the chip select pin
* @param chipEnable setting up the chip enable pin * @param chipEnable setting up the chip enable pin
*/ */
void nrfInitModule (uint8_t chipSelect, uint8_t chipEnable); void init (uint8_t chipSelect, uint8_t chipEnable);
/** /**
* Configure NRF24l01 chip * Configure NRF24l01 chip
*/ */
void nrfConfigModule (); void configure ();
/** /**
* Send the buffer data * Send the buffer data
* *
* @param value pointer to the buffer * @param *value pointer to the buffer
*/ */
void nrfSend (uint8_t *value); void send (uint8_t * value);
/** /**
* Send the data located in inner bufer, user must fill the * Send the data located in inner bufer, user must fill the
* m_txBuffer buffer * m_txBuffer buffer
*/ */
void nrfSend (); void send ();
/** /**
* Set recieving address of the device * Set recieving address of the device
* *
* @param addr 5 bytes addres * @param addr 5 bytes addres
*/ */
void nrfSetRXaddr (uint8_t * addr); void setSourceAddress (uint8_t * addr);
/** /**
* Set recipient address. nrfSend method will send the data buffer * Set recipient address. nrfSend method will send the data buffer
@ -187,161 +219,171 @@ class NRF24l01 {
* *
* @param addr 5 bytes addres * @param addr 5 bytes addres
*/ */
void nrfSetTXaddr (uint8_t * addr); void setDestinationAddress (uint8_t * addr);
/** /**
* Set broadcasting address. * Set broadcasting address.
* *
* @param addr 5 bytes addres * @param addr 5 bytes addres
*/ */
void nrfSetBroadcastAddr (uint8_t * addr); void setBroadcastAddress (uint8_t * addr);
/** /**
* Set payload size. * Set payload size.
* *
* @param load size of the payload (MAX 32) * @param load size of the payload (MAX 32)
*/ */
void nrfSetPayload (uint8_t load); void setPayload (uint8_t load);
/** /**
* Check if data arrived * Check if data arrived
*/ */
bool nrfDataReady (); bool dataReady ();
/** /**
* Check if chip in sending mode * Check if chip in sending mode
*/ */
bool nrfIsSending (); bool dataSending ();
/**
* Check if recieving stack is empty
*/
bool nrfRXFifoEmpty ();
/**
* Check if transmitting stack is empty
*/
bool nrfTXFifoEmpty ();
/** /**
* Sink all arrived data into the provided buffer * Sink all arrived data into the provided buffer
* *
* @param data pointer to buffer of data * @param load size of the payload (MAX 32)
*/ */
void nrfGetData (uint8_t * data); void getData (uint8_t * data);
/** /**
* Check the chip state * Check the chip state
*/ */
uint8_t nrfGetStatus (); uint8_t getStatus ();
/** /**
* Transmit provided data to the chip * Check if recieving stack is empty
*
* @param dataout pointer to the buffer with data
* @param len length of the buffer
*/ */
void nrfTransmitSync (uint8_t *dataout, uint8_t len); bool rxFifoEmpty ();
/**
* Recieve data from the chip
*
* @param dataout pointer to the buffer with data
* @param datain pointer to the buffer where the arrived data
* will be sinked
* @param len length of the buffer
*/
void nrfTransferSync (uint8_t *dataout ,uint8_t *datain, uint8_t len);
/**
* Write byte value into a register
*
* @param reg register address
* @param value the value to write
*/
void nrfConfigRegister (uint8_t reg, uint8_t value);
/**
* Read continues data from register
*
* @param reg register address
* @param value pointer to the buffer
* @param len length of the buffer
*/
void nrfReadRegister (uint8_t reg, uint8_t * value, uint8_t len);
/**
* Write continues data to register
*
* @param reg register address
* @param value pointer to the buffer
* @param len length of the buffer
*/
void nrfWriteRegister (uint8_t reg, uint8_t * value, uint8_t len);
/** /**
* Power up reciever * Power up reciever
*/ */
void nrfPowerUpRX (); void rxPowerUp ();
/**
* Power up transmitter
*/
void nrfPowerUpTX ();
/**
* Power down all
*/
void nrfPowerDown ();
/**
* Set chip enable pin HIGH
*/
mraa_result_t nrfCEHigh ();
/**
* Set chip enable LOW
*/
mraa_result_t nrfCELow ();
/**
* Set chip select pin LOW
*/
mraa_result_t nrfCSOn ();
/**
* Set chip select pin HIGH
*/
mraa_result_t nrfCSOff ();
/** /**
* Flush reciver stack * Flush reciver stack
*/ */
void nrfFlushRX (); void rxFlushBuffer ();
/**
* Power up transmitter
*/
void txPowerUp ();
/**
* Power down all
*/
void powerDown ();
void setChannel (uint8_t channel);
void setPower (power_t power);
uint8_t setSpeedRate (speed_rate_t rate);
/**
* Flush transmit stack
*/
void txFlushBuffer ();
/** /**
* Pulling method which listenning for arrived data, if data * Pulling method which listenning for arrived data, if data
* arrived dataRecievedHandler will be triggered * arrived dataRecievedHandler will be triggered
*/ */
void nrfListenForChannel(); void pollListener ();
uint8_t m_rxBuffer[MAX_BUFFER]; /**< Reciver buffer */ /**
uint8_t m_txBuffer[MAX_BUFFER]; /**< Transmit buffer */ * Set chip enable pin HIGH
*/
mraa_result_t ceHigh ();
/**
* Set chip enable LOW
*/
mraa_result_t ceLow ();
/**
* Set chip select pin LOW
*/
mraa_result_t csOn ();
/**
* Set chip select pin HIGH
*/
mraa_result_t csOff ();
/**
* Configure nRF24l01 module to behave as BLE
* (Bluetooth Low Energy) beaconing devcie.
*/
void setBeaconingMode ();
/**
* Beaconing the provided message to BLE scanners.
*
* @param msg beacon the provated message (max length is 16 bytes)
*/
void sendBeaconingMsg (uint8_t * msg);
uint8_t m_rxBuffer[MAX_BUFFER]; /**< Reciver buffer */
uint8_t m_txBuffer[MAX_BUFFER]; /**< Transmit buffer */
uint8_t m_bleBuffer [32]; /**< BLE buffer */
funcPtrVoidVoid dataRecievedHandler; /**< Data arrived handler */ funcPtrVoidVoid dataRecievedHandler; /**< Data arrived handler */
private: private:
/**
* Write bytes to the SPI device.
*/
void writeBytes (uint8_t * dataout, uint8_t * datain, uint8_t len);
/**
* Set register value on SPI device. [one byte]
*/
void setRegister (uint8_t reg, uint8_t value);
/**
* Get register value from SPI device. [one byte]
*/
uint8_t getRegister (uint8_t reg);
/**
* Reads an array of bytes from the given start position in the nrf24l01 registers.
*/
void readRegister (uint8_t reg, uint8_t * value, uint8_t len);
/**
* Writes an array of bytes into inte the nrf24l01 registers.
*/
void writeRegister (uint8_t reg, uint8_t * value, uint8_t len);
/**
* Send command to the nrf24l01.
*/
void sendCommand (uint8_t cmd);
void bleCrc (const uint8_t* data, uint8_t len, uint8_t* dst);
void bleWhiten (uint8_t* data, uint8_t len, uint8_t whitenCoeff);
void blePacketEncode(uint8_t* packet, uint8_t len, uint8_t chan);
uint8_t swapbits (uint8_t a);
mraa_spi_context m_spi; mraa_spi_context m_spi;
uint8_t m_ce; uint8_t m_ce;
uint8_t m_csn; uint8_t m_csn;
uint8_t m_channel; uint8_t m_channel;
uint8_t m_ptx; uint8_t m_power;
uint8_t m_payload; uint8_t m_ptx;
uint8_t m_localAddress[5]; uint8_t m_payload;
uint8_t m_localAddress[5];
mraa_gpio_context m_csnPinCtx; mraa_gpio_context m_csnPinCtx;
mraa_gpio_context m_cePinCtx; mraa_gpio_context m_cePinCtx;
std::string m_name; std::string m_name;
}; };
} }