From af13c940c159b36f113d5d31ebaa0c5009db7383 Mon Sep 17 00:00:00 2001 From: James Devine Date: Thu, 4 Feb 2016 03:45:57 +0000 Subject: [PATCH] microbit: add RSSI to MicroBitRadio Added the ability to capture the RSSI value, and an accessor and mutator for the last received RSSI value. --- inc/MicroBitRadio.h | 14 ++++++- source/ble-services/MicroBitRadio.cpp | 54 +++++++++++++++++++++++++-- 2 files changed, 64 insertions(+), 4 deletions(-) diff --git a/inc/MicroBitRadio.h b/inc/MicroBitRadio.h index 5c3f16a..8e1c548 100644 --- a/inc/MicroBitRadio.h +++ b/inc/MicroBitRadio.h @@ -116,7 +116,19 @@ class MicroBitRadio : MicroBitComponent int queueRxBuf(); /** - * Initialises the radio for use as a multipoint sender/receiver + * Sets the RSSI for the most recent packet. + * + * @param rssi the new rssi value + */ + int setRSSI(uint8_t rssi); + + /** + * Retrieves the current RSSI for the most recent packet. + */ + int getRSSI(); + + /** + * Initialises the radio for use as a multipoint sender/receiver * @return MICROBIT_OK on success, MICROBIT_NOT_SUPPORTED if SoftDevice is enabled. */ int enable(); diff --git a/source/ble-services/MicroBitRadio.cpp b/source/ble-services/MicroBitRadio.cpp index ef7fbbc..111e9c5 100644 --- a/source/ble-services/MicroBitRadio.cpp +++ b/source/ble-services/MicroBitRadio.cpp @@ -31,9 +31,28 @@ extern "C" void RADIO_IRQHandler(void) MicroBitRadio::instance->queueRxBuf(); NRF_RADIO->PACKETPTR = (uint32_t) MicroBitRadio::instance->getRxBuf(); - // Start listening for the next packet. - NRF_RADIO->EVENTS_END = 0; - NRF_RADIO->TASKS_START = 1; + if(NRF_RADIO->EVENTS_READY) + { + NRF_RADIO->EVENTS_READY = 0; + + // Start listening and wait for the END event + NRF_RADIO->TASKS_START = 1; + } + + if(NRF_RADIO->EVENTS_END) + { + NRF_RADIO->EVENTS_END = 0; + + if(NRF_RADIO->CRCSTATUS == 1) + { + uint8_t sample = NRF_RADIO->RSSISAMPLE; + + uBit.radio.setRSSI(sample); + } + + // Start listening and wait for the END event + NRF_RADIO->TASKS_START = 1; + } } /** @@ -48,6 +67,7 @@ MicroBitRadio::MicroBitRadio(uint16_t id) : datagram() this->status = 0; this->group = 0; this->queueDepth = 0; + this->rssi = 0; this->rxQueue = NULL; this->rxBuf = NULL; @@ -148,6 +168,34 @@ int MicroBitRadio::queueRxBuf() return MICROBIT_OK; } +/** + * Sets the RSSI for the most recent packet. + * + * @param rssi the new rssi value + * + * @note should only be called from RADIO_IRQHandler... + */ +int MicroBitRadio::setRSSI(uint8_t rssi) +{ + if (!(status & MICROBIT_RADIO_STATUS_INITIALISED)) + return MICROBIT_NOT_SUPPORTED; + + this->rssi = rssi; + + return MICROBIT_OK; +} + +/** + * Retrieves the current RSSI for the most recent packet. + */ +int MicroBitRadio::getRSSI() +{ + if (!(status & MICROBIT_RADIO_STATUS_INITIALISED)) + return MICROBIT_NOT_SUPPORTED; + + return this->rssi; +} + /** * Initialises the radio for use as a multipoint sender/receiver. * This is currently only possible if the BLE stack (Soft Device) is disabled.