Official Sheffield ARMBand micro:bit program

Committer:
MrBedfordVan
Date:
Mon Oct 17 12:41:20 2016 +0000
Revision:
0:b9164b348919
Official Sheffield ARMBand Micro:bit program

Who changed what in which revision?

UserRevisionLine numberNew contents of line
MrBedfordVan 0:b9164b348919 1 /*
MrBedfordVan 0:b9164b348919 2 The MIT License (MIT)
MrBedfordVan 0:b9164b348919 3
MrBedfordVan 0:b9164b348919 4 Copyright (c) 2016 British Broadcasting Corporation.
MrBedfordVan 0:b9164b348919 5 This software is provided by Lancaster University by arrangement with the BBC.
MrBedfordVan 0:b9164b348919 6
MrBedfordVan 0:b9164b348919 7 Permission is hereby granted, free of charge, to any person obtaining a
MrBedfordVan 0:b9164b348919 8 copy of this software and associated documentation files (the "Software"),
MrBedfordVan 0:b9164b348919 9 to deal in the Software without restriction, including without limitation
MrBedfordVan 0:b9164b348919 10 the rights to use, copy, modify, merge, publish, distribute, sublicense,
MrBedfordVan 0:b9164b348919 11 and/or sell copies of the Software, and to permit persons to whom the
MrBedfordVan 0:b9164b348919 12 Software is furnished to do so, subject to the following conditions:
MrBedfordVan 0:b9164b348919 13
MrBedfordVan 0:b9164b348919 14 The above copyright notice and this permission notice shall be included in
MrBedfordVan 0:b9164b348919 15 all copies or substantial portions of the Software.
MrBedfordVan 0:b9164b348919 16
MrBedfordVan 0:b9164b348919 17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
MrBedfordVan 0:b9164b348919 18 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
MrBedfordVan 0:b9164b348919 19 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
MrBedfordVan 0:b9164b348919 20 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
MrBedfordVan 0:b9164b348919 21 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
MrBedfordVan 0:b9164b348919 22 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
MrBedfordVan 0:b9164b348919 23 DEALINGS IN THE SOFTWARE.
MrBedfordVan 0:b9164b348919 24 */
MrBedfordVan 0:b9164b348919 25
MrBedfordVan 0:b9164b348919 26 #include "MicroBitConfig.h"
MrBedfordVan 0:b9164b348919 27 #include "MicroBitRadio.h"
MrBedfordVan 0:b9164b348919 28 #include "MicroBitComponent.h"
MrBedfordVan 0:b9164b348919 29 #include "EventModel.h"
MrBedfordVan 0:b9164b348919 30 #include "MicroBitDevice.h"
MrBedfordVan 0:b9164b348919 31 #include "ErrorNo.h"
MrBedfordVan 0:b9164b348919 32 #include "MicroBitFiber.h"
MrBedfordVan 0:b9164b348919 33 #include "MicroBitBLEManager.h"
MrBedfordVan 0:b9164b348919 34
MrBedfordVan 0:b9164b348919 35 /**
MrBedfordVan 0:b9164b348919 36 * Provides a simple broadcast radio abstraction, built upon the raw nrf51822 RADIO module.
MrBedfordVan 0:b9164b348919 37 *
MrBedfordVan 0:b9164b348919 38 * The nrf51822 RADIO module supports a number of proprietary modes of operation oher than the typical BLE usage.
MrBedfordVan 0:b9164b348919 39 * This class uses one of these modes to enable simple, point to multipoint communication directly between micro:bits.
MrBedfordVan 0:b9164b348919 40 *
MrBedfordVan 0:b9164b348919 41 * TODO: The protocols implemented here do not currently perform any significant form of energy management,
MrBedfordVan 0:b9164b348919 42 * which means that they will consume far more energy than their BLE equivalent. Later versions of the protocol
MrBedfordVan 0:b9164b348919 43 * should look to address this through energy efficient broadcast techbiques / sleep scheduling. In particular, the GLOSSY
MrBedfordVan 0:b9164b348919 44 * approach to efficient rebroadcast and network synchronisation would likely provide an effective future step.
MrBedfordVan 0:b9164b348919 45 *
MrBedfordVan 0:b9164b348919 46 * TODO: Meshing should also be considered - again a GLOSSY approach may be effective here, and highly complementary to
MrBedfordVan 0:b9164b348919 47 * the master/slave arachitecture of BLE.
MrBedfordVan 0:b9164b348919 48 *
MrBedfordVan 0:b9164b348919 49 * TODO: This implementation may only operated whilst the BLE stack is disabled. The nrf51822 provides a timeslot API to allow
MrBedfordVan 0:b9164b348919 50 * BLE to cohabit with other protocols. Future work to allow this colocation would be benefical, and would also allow for the
MrBedfordVan 0:b9164b348919 51 * creation of wireless BLE bridges.
MrBedfordVan 0:b9164b348919 52 *
MrBedfordVan 0:b9164b348919 53 * NOTE: This API does not contain any form of encryption, authentication or authorisation. Its purpose is solely for use as a
MrBedfordVan 0:b9164b348919 54 * teaching aid to demonstrate how simple communications operates, and to provide a sandpit through which learning can take place.
MrBedfordVan 0:b9164b348919 55 * For serious applications, BLE should be considered a substantially more secure alternative.
MrBedfordVan 0:b9164b348919 56 */
MrBedfordVan 0:b9164b348919 57
MrBedfordVan 0:b9164b348919 58 MicroBitRadio* MicroBitRadio::instance = NULL;
MrBedfordVan 0:b9164b348919 59
MrBedfordVan 0:b9164b348919 60 extern "C" void RADIO_IRQHandler(void)
MrBedfordVan 0:b9164b348919 61 {
MrBedfordVan 0:b9164b348919 62 if(NRF_RADIO->EVENTS_READY)
MrBedfordVan 0:b9164b348919 63 {
MrBedfordVan 0:b9164b348919 64 NRF_RADIO->EVENTS_READY = 0;
MrBedfordVan 0:b9164b348919 65
MrBedfordVan 0:b9164b348919 66 // Start listening and wait for the END event
MrBedfordVan 0:b9164b348919 67 NRF_RADIO->TASKS_START = 1;
MrBedfordVan 0:b9164b348919 68 }
MrBedfordVan 0:b9164b348919 69
MrBedfordVan 0:b9164b348919 70 if(NRF_RADIO->EVENTS_END)
MrBedfordVan 0:b9164b348919 71 {
MrBedfordVan 0:b9164b348919 72 NRF_RADIO->EVENTS_END = 0;
MrBedfordVan 0:b9164b348919 73 if(NRF_RADIO->CRCSTATUS == 1)
MrBedfordVan 0:b9164b348919 74 {
MrBedfordVan 0:b9164b348919 75 uint8_t sample = NRF_RADIO->RSSISAMPLE;
MrBedfordVan 0:b9164b348919 76
MrBedfordVan 0:b9164b348919 77 // Associate this packet's rssi value with the data just
MrBedfordVan 0:b9164b348919 78 // transferred by DMA receive
MrBedfordVan 0:b9164b348919 79 MicroBitRadio::instance->setRSSI(sample);
MrBedfordVan 0:b9164b348919 80
MrBedfordVan 0:b9164b348919 81 // Now move on to the next buffer, if possible.
MrBedfordVan 0:b9164b348919 82 // The queued packet will get the rssi value set above.
MrBedfordVan 0:b9164b348919 83 MicroBitRadio::instance->queueRxBuf();
MrBedfordVan 0:b9164b348919 84
MrBedfordVan 0:b9164b348919 85 // Set the new buffer for DMA
MrBedfordVan 0:b9164b348919 86 NRF_RADIO->PACKETPTR = (uint32_t) MicroBitRadio::instance->getRxBuf();
MrBedfordVan 0:b9164b348919 87 }
MrBedfordVan 0:b9164b348919 88 else
MrBedfordVan 0:b9164b348919 89 {
MrBedfordVan 0:b9164b348919 90 MicroBitRadio::instance->setRSSI(0);
MrBedfordVan 0:b9164b348919 91 }
MrBedfordVan 0:b9164b348919 92
MrBedfordVan 0:b9164b348919 93 // Start listening and wait for the END event
MrBedfordVan 0:b9164b348919 94 NRF_RADIO->TASKS_START = 1;
MrBedfordVan 0:b9164b348919 95 }
MrBedfordVan 0:b9164b348919 96 }
MrBedfordVan 0:b9164b348919 97
MrBedfordVan 0:b9164b348919 98 /**
MrBedfordVan 0:b9164b348919 99 * Constructor.
MrBedfordVan 0:b9164b348919 100 *
MrBedfordVan 0:b9164b348919 101 * Initialise the MicroBitRadio.
MrBedfordVan 0:b9164b348919 102 *
MrBedfordVan 0:b9164b348919 103 * @note This class is demand activated, as a result most resources are only
MrBedfordVan 0:b9164b348919 104 * committed if send/recv or event registrations calls are made.
MrBedfordVan 0:b9164b348919 105 */
MrBedfordVan 0:b9164b348919 106 MicroBitRadio::MicroBitRadio(uint16_t id) : datagram(*this), event (*this)
MrBedfordVan 0:b9164b348919 107 {
MrBedfordVan 0:b9164b348919 108 this->id = id;
MrBedfordVan 0:b9164b348919 109 this->status = 0;
MrBedfordVan 0:b9164b348919 110 this->group = MICROBIT_RADIO_DEFAULT_GROUP;
MrBedfordVan 0:b9164b348919 111 this->queueDepth = 0;
MrBedfordVan 0:b9164b348919 112 this->rssi = 0;
MrBedfordVan 0:b9164b348919 113 this->rxQueue = NULL;
MrBedfordVan 0:b9164b348919 114 this->rxBuf = NULL;
MrBedfordVan 0:b9164b348919 115
MrBedfordVan 0:b9164b348919 116 instance = this;
MrBedfordVan 0:b9164b348919 117 }
MrBedfordVan 0:b9164b348919 118
MrBedfordVan 0:b9164b348919 119 /**
MrBedfordVan 0:b9164b348919 120 * Change the output power level of the transmitter to the given value.
MrBedfordVan 0:b9164b348919 121 *
MrBedfordVan 0:b9164b348919 122 * @param power a value in the range 0..7, where 0 is the lowest power and 7 is the highest.
MrBedfordVan 0:b9164b348919 123 *
MrBedfordVan 0:b9164b348919 124 * @return MICROBIT_OK on success, or MICROBIT_INVALID_PARAMETER if the value is out of range.
MrBedfordVan 0:b9164b348919 125 */
MrBedfordVan 0:b9164b348919 126 int MicroBitRadio::setTransmitPower(int power)
MrBedfordVan 0:b9164b348919 127 {
MrBedfordVan 0:b9164b348919 128 if (power < 0 || power >= MICROBIT_BLE_POWER_LEVELS)
MrBedfordVan 0:b9164b348919 129 return MICROBIT_INVALID_PARAMETER;
MrBedfordVan 0:b9164b348919 130
MrBedfordVan 0:b9164b348919 131 NRF_RADIO->TXPOWER = (uint32_t)MICROBIT_BLE_POWER_LEVEL[power];
MrBedfordVan 0:b9164b348919 132
MrBedfordVan 0:b9164b348919 133 return MICROBIT_OK;
MrBedfordVan 0:b9164b348919 134 }
MrBedfordVan 0:b9164b348919 135
MrBedfordVan 0:b9164b348919 136 /**
MrBedfordVan 0:b9164b348919 137 * Change the transmission and reception band of the radio to the given channel
MrBedfordVan 0:b9164b348919 138 *
MrBedfordVan 0:b9164b348919 139 * @param band a frequency band in the range 0 - 100. Each step is 1MHz wide, based at 2400MHz.
MrBedfordVan 0:b9164b348919 140 *
MrBedfordVan 0:b9164b348919 141 * @return MICROBIT_OK on success, or MICROBIT_INVALID_PARAMETER if the value is out of range,
MrBedfordVan 0:b9164b348919 142 * or MICROBIT_NOT_SUPPORTED if the BLE stack is running.
MrBedfordVan 0:b9164b348919 143 */
MrBedfordVan 0:b9164b348919 144 int MicroBitRadio::setFrequencyBand(int band)
MrBedfordVan 0:b9164b348919 145 {
MrBedfordVan 0:b9164b348919 146 if (ble_running())
MrBedfordVan 0:b9164b348919 147 return MICROBIT_NOT_SUPPORTED;
MrBedfordVan 0:b9164b348919 148
MrBedfordVan 0:b9164b348919 149 if (band < 0 || band > 100)
MrBedfordVan 0:b9164b348919 150 return MICROBIT_INVALID_PARAMETER;
MrBedfordVan 0:b9164b348919 151
MrBedfordVan 0:b9164b348919 152 NRF_RADIO->FREQUENCY = (uint32_t)band;
MrBedfordVan 0:b9164b348919 153
MrBedfordVan 0:b9164b348919 154 return MICROBIT_OK;
MrBedfordVan 0:b9164b348919 155 }
MrBedfordVan 0:b9164b348919 156
MrBedfordVan 0:b9164b348919 157 /**
MrBedfordVan 0:b9164b348919 158 * Retrieve a pointer to the currently allocated receive buffer. This is the area of memory
MrBedfordVan 0:b9164b348919 159 * actively being used by the radio hardware to store incoming data.
MrBedfordVan 0:b9164b348919 160 *
MrBedfordVan 0:b9164b348919 161 * @return a pointer to the current receive buffer.
MrBedfordVan 0:b9164b348919 162 */
MrBedfordVan 0:b9164b348919 163 FrameBuffer* MicroBitRadio::getRxBuf()
MrBedfordVan 0:b9164b348919 164 {
MrBedfordVan 0:b9164b348919 165 return rxBuf;
MrBedfordVan 0:b9164b348919 166 }
MrBedfordVan 0:b9164b348919 167
MrBedfordVan 0:b9164b348919 168 /**
MrBedfordVan 0:b9164b348919 169 * Attempt to queue a buffer received by the radio hardware, if sufficient space is available.
MrBedfordVan 0:b9164b348919 170 *
MrBedfordVan 0:b9164b348919 171 * @return MICROBIT_OK on success, or MICROBIT_NO_RESOURCES if a replacement receiver buffer
MrBedfordVan 0:b9164b348919 172 * could not be allocated (either by policy or memory exhaustion).
MrBedfordVan 0:b9164b348919 173 */
MrBedfordVan 0:b9164b348919 174 int MicroBitRadio::queueRxBuf()
MrBedfordVan 0:b9164b348919 175 {
MrBedfordVan 0:b9164b348919 176 if (rxBuf == NULL)
MrBedfordVan 0:b9164b348919 177 return MICROBIT_INVALID_PARAMETER;
MrBedfordVan 0:b9164b348919 178
MrBedfordVan 0:b9164b348919 179 if (queueDepth >= MICROBIT_RADIO_MAXIMUM_RX_BUFFERS)
MrBedfordVan 0:b9164b348919 180 return MICROBIT_NO_RESOURCES;
MrBedfordVan 0:b9164b348919 181
MrBedfordVan 0:b9164b348919 182 // Store the received RSSI value in the frame
MrBedfordVan 0:b9164b348919 183 rxBuf->rssi = getRSSI();
MrBedfordVan 0:b9164b348919 184
MrBedfordVan 0:b9164b348919 185 // Ensure that a replacement buffer is available before queuing.
MrBedfordVan 0:b9164b348919 186 FrameBuffer *newRxBuf = new FrameBuffer();
MrBedfordVan 0:b9164b348919 187
MrBedfordVan 0:b9164b348919 188 if (newRxBuf == NULL)
MrBedfordVan 0:b9164b348919 189 return MICROBIT_NO_RESOURCES;
MrBedfordVan 0:b9164b348919 190
MrBedfordVan 0:b9164b348919 191 // We add to the tail of the queue to preserve causal ordering.
MrBedfordVan 0:b9164b348919 192 rxBuf->next = NULL;
MrBedfordVan 0:b9164b348919 193
MrBedfordVan 0:b9164b348919 194 if (rxQueue == NULL)
MrBedfordVan 0:b9164b348919 195 {
MrBedfordVan 0:b9164b348919 196 rxQueue = rxBuf;
MrBedfordVan 0:b9164b348919 197 }
MrBedfordVan 0:b9164b348919 198 else
MrBedfordVan 0:b9164b348919 199 {
MrBedfordVan 0:b9164b348919 200 FrameBuffer *p = rxQueue;
MrBedfordVan 0:b9164b348919 201 while (p->next != NULL)
MrBedfordVan 0:b9164b348919 202 p = p->next;
MrBedfordVan 0:b9164b348919 203
MrBedfordVan 0:b9164b348919 204 p->next = rxBuf;
MrBedfordVan 0:b9164b348919 205 }
MrBedfordVan 0:b9164b348919 206
MrBedfordVan 0:b9164b348919 207 // Increase our received packet count
MrBedfordVan 0:b9164b348919 208 queueDepth++;
MrBedfordVan 0:b9164b348919 209
MrBedfordVan 0:b9164b348919 210 // Allocate a new buffer for the receiver hardware to use. the old on will be passed on to higher layer protocols/apps.
MrBedfordVan 0:b9164b348919 211 rxBuf = newRxBuf;
MrBedfordVan 0:b9164b348919 212
MrBedfordVan 0:b9164b348919 213 return MICROBIT_OK;
MrBedfordVan 0:b9164b348919 214 }
MrBedfordVan 0:b9164b348919 215
MrBedfordVan 0:b9164b348919 216 /**
MrBedfordVan 0:b9164b348919 217 * Sets the RSSI for the most recent packet.
MrBedfordVan 0:b9164b348919 218 *
MrBedfordVan 0:b9164b348919 219 * @param rssi the new rssi value.
MrBedfordVan 0:b9164b348919 220 *
MrBedfordVan 0:b9164b348919 221 * @note should only be called from RADIO_IRQHandler...
MrBedfordVan 0:b9164b348919 222 */
MrBedfordVan 0:b9164b348919 223 int MicroBitRadio::setRSSI(uint8_t rssi)
MrBedfordVan 0:b9164b348919 224 {
MrBedfordVan 0:b9164b348919 225 if (!(status & MICROBIT_RADIO_STATUS_INITIALISED))
MrBedfordVan 0:b9164b348919 226 return MICROBIT_NOT_SUPPORTED;
MrBedfordVan 0:b9164b348919 227
MrBedfordVan 0:b9164b348919 228 this->rssi = rssi;
MrBedfordVan 0:b9164b348919 229
MrBedfordVan 0:b9164b348919 230 return MICROBIT_OK;
MrBedfordVan 0:b9164b348919 231 }
MrBedfordVan 0:b9164b348919 232
MrBedfordVan 0:b9164b348919 233 /**
MrBedfordVan 0:b9164b348919 234 * Retrieves the current RSSI for the most recent packet.
MrBedfordVan 0:b9164b348919 235 *
MrBedfordVan 0:b9164b348919 236 * @return the most recent RSSI value or MICROBIT_NOT_SUPPORTED if the BLE stack is running.
MrBedfordVan 0:b9164b348919 237 */
MrBedfordVan 0:b9164b348919 238 int MicroBitRadio::getRSSI()
MrBedfordVan 0:b9164b348919 239 {
MrBedfordVan 0:b9164b348919 240 if (!(status & MICROBIT_RADIO_STATUS_INITIALISED))
MrBedfordVan 0:b9164b348919 241 return MICROBIT_NOT_SUPPORTED;
MrBedfordVan 0:b9164b348919 242
MrBedfordVan 0:b9164b348919 243 return this->rssi;
MrBedfordVan 0:b9164b348919 244 }
MrBedfordVan 0:b9164b348919 245
MrBedfordVan 0:b9164b348919 246 /**
MrBedfordVan 0:b9164b348919 247 * Initialises the radio for use as a multipoint sender/receiver
MrBedfordVan 0:b9164b348919 248 *
MrBedfordVan 0:b9164b348919 249 * @return MICROBIT_OK on success, MICROBIT_NOT_SUPPORTED if the BLE stack is running.
MrBedfordVan 0:b9164b348919 250 */
MrBedfordVan 0:b9164b348919 251 int MicroBitRadio::enable()
MrBedfordVan 0:b9164b348919 252 {
MrBedfordVan 0:b9164b348919 253 // If the device is already initialised, then there's nothing to do.
MrBedfordVan 0:b9164b348919 254 if (status & MICROBIT_RADIO_STATUS_INITIALISED)
MrBedfordVan 0:b9164b348919 255 return MICROBIT_OK;
MrBedfordVan 0:b9164b348919 256
MrBedfordVan 0:b9164b348919 257 // Only attempt to enable this radio mode if BLE is disabled.
MrBedfordVan 0:b9164b348919 258 if (ble_running())
MrBedfordVan 0:b9164b348919 259 return MICROBIT_NOT_SUPPORTED;
MrBedfordVan 0:b9164b348919 260
MrBedfordVan 0:b9164b348919 261 // If this is the first time we've been enable, allocate out receive buffers.
MrBedfordVan 0:b9164b348919 262 if (rxBuf == NULL)
MrBedfordVan 0:b9164b348919 263 rxBuf = new FrameBuffer();
MrBedfordVan 0:b9164b348919 264
MrBedfordVan 0:b9164b348919 265 if (rxBuf == NULL)
MrBedfordVan 0:b9164b348919 266 return MICROBIT_NO_RESOURCES;
MrBedfordVan 0:b9164b348919 267
MrBedfordVan 0:b9164b348919 268 // Enable the High Frequency clock on the processor. This is a pre-requisite for
MrBedfordVan 0:b9164b348919 269 // the RADIO module. Without this clock, no communication is possible.
MrBedfordVan 0:b9164b348919 270 NRF_CLOCK->EVENTS_HFCLKSTARTED = 0;
MrBedfordVan 0:b9164b348919 271 NRF_CLOCK->TASKS_HFCLKSTART = 1;
MrBedfordVan 0:b9164b348919 272 while (NRF_CLOCK->EVENTS_HFCLKSTARTED == 0);
MrBedfordVan 0:b9164b348919 273
MrBedfordVan 0:b9164b348919 274 // Bring up the nrf51822 RADIO module in Nordic's proprietary 1MBps packet radio mode.
MrBedfordVan 0:b9164b348919 275 setTransmitPower(MICROBIT_RADIO_DEFAULT_TX_POWER);
MrBedfordVan 0:b9164b348919 276 setFrequencyBand(MICROBIT_RADIO_DEFAULT_FREQUENCY);
MrBedfordVan 0:b9164b348919 277
MrBedfordVan 0:b9164b348919 278 // Configure for 1Mbps throughput.
MrBedfordVan 0:b9164b348919 279 // This may sound excessive, but running a high data rates reduces the chances of collisions...
MrBedfordVan 0:b9164b348919 280 NRF_RADIO->MODE = RADIO_MODE_MODE_Nrf_1Mbit;
MrBedfordVan 0:b9164b348919 281
MrBedfordVan 0:b9164b348919 282 // Configure the addresses we use for this protocol. We run ANONYMOUSLY at the core.
MrBedfordVan 0:b9164b348919 283 // A 40 bit addresses is used. The first 32 bits match the ASCII character code for "uBit".
MrBedfordVan 0:b9164b348919 284 // Statistically, this provides assurance to avoid other similar 2.4GHz protocols that may be in the vicinity.
MrBedfordVan 0:b9164b348919 285 // We also map the assigned 8-bit GROUP id into the PREFIX field. This allows the RADIO hardware to perform
MrBedfordVan 0:b9164b348919 286 // address matching for us, and only generate an interrupt when a packet matching our group is received.
MrBedfordVan 0:b9164b348919 287 NRF_RADIO->BASE0 = MICROBIT_RADIO_BASE_ADDRESS;
MrBedfordVan 0:b9164b348919 288
MrBedfordVan 0:b9164b348919 289 // Join the default group. This will configure the remaining byte in the RADIO hardware module.
MrBedfordVan 0:b9164b348919 290 setGroup(this->group);
MrBedfordVan 0:b9164b348919 291
MrBedfordVan 0:b9164b348919 292 // The RADIO hardware module supports the use of multiple addresses, but as we're running anonymously, we only need one.
MrBedfordVan 0:b9164b348919 293 // Configure the RADIO module to use the default address (address 0) for both send and receive operations.
MrBedfordVan 0:b9164b348919 294 NRF_RADIO->TXADDRESS = 0;
MrBedfordVan 0:b9164b348919 295 NRF_RADIO->RXADDRESSES = 1;
MrBedfordVan 0:b9164b348919 296
MrBedfordVan 0:b9164b348919 297 // Packet layout configuration. The nrf51822 has a highly capable and flexible RADIO module that, in addition to transmission
MrBedfordVan 0:b9164b348919 298 // and reception of data, also contains a LENGTH field, two optional additional 1 byte fields (S0 and S1) and a CRC calculation.
MrBedfordVan 0:b9164b348919 299 // Configure the packet format for a simple 8 bit length field and no additional fields.
MrBedfordVan 0:b9164b348919 300 NRF_RADIO->PCNF0 = 0x00000008;
MrBedfordVan 0:b9164b348919 301 NRF_RADIO->PCNF1 = 0x02040000 | MICROBIT_RADIO_MAX_PACKET_SIZE;
MrBedfordVan 0:b9164b348919 302
MrBedfordVan 0:b9164b348919 303 // Most communication channels contain some form of checksum - a mathematical calculation taken based on all the data
MrBedfordVan 0:b9164b348919 304 // in a packet, that is also sent as part of the packet. When received, this calculation can be repeated, and the results
MrBedfordVan 0:b9164b348919 305 // from the sender and receiver compared. If they are different, then some corruption of the data ahas happened in transit,
MrBedfordVan 0:b9164b348919 306 // and we know we can't trust it. The nrf51822 RADIO uses a CRC for this - a very effective checksum calculation.
MrBedfordVan 0:b9164b348919 307 //
MrBedfordVan 0:b9164b348919 308 // Enable automatic 16bit CRC generation and checking, and configure how the CRC is calculated.
MrBedfordVan 0:b9164b348919 309 NRF_RADIO->CRCCNF = RADIO_CRCCNF_LEN_Two;
MrBedfordVan 0:b9164b348919 310 NRF_RADIO->CRCINIT = 0xFFFF;
MrBedfordVan 0:b9164b348919 311 NRF_RADIO->CRCPOLY = 0x11021;
MrBedfordVan 0:b9164b348919 312
MrBedfordVan 0:b9164b348919 313 // Set the start random value of the data whitening algorithm. This can be any non zero number.
MrBedfordVan 0:b9164b348919 314 NRF_RADIO->DATAWHITEIV = 0x18;
MrBedfordVan 0:b9164b348919 315
MrBedfordVan 0:b9164b348919 316 // Set up the RADIO module to read and write from our internal buffer.
MrBedfordVan 0:b9164b348919 317 NRF_RADIO->PACKETPTR = (uint32_t)rxBuf;
MrBedfordVan 0:b9164b348919 318
MrBedfordVan 0:b9164b348919 319 // Configure the hardware to issue an interrupt whenever a task is complete (e.g. send/receive).
MrBedfordVan 0:b9164b348919 320 NRF_RADIO->INTENSET = 0x00000008;
MrBedfordVan 0:b9164b348919 321 NVIC_ClearPendingIRQ(RADIO_IRQn);
MrBedfordVan 0:b9164b348919 322 NVIC_EnableIRQ(RADIO_IRQn);
MrBedfordVan 0:b9164b348919 323
MrBedfordVan 0:b9164b348919 324 NRF_RADIO->SHORTS |= RADIO_SHORTS_ADDRESS_RSSISTART_Msk;
MrBedfordVan 0:b9164b348919 325
MrBedfordVan 0:b9164b348919 326 // Start listening for the next packet
MrBedfordVan 0:b9164b348919 327 NRF_RADIO->EVENTS_READY = 0;
MrBedfordVan 0:b9164b348919 328 NRF_RADIO->TASKS_RXEN = 1;
MrBedfordVan 0:b9164b348919 329 while(NRF_RADIO->EVENTS_READY == 0);
MrBedfordVan 0:b9164b348919 330
MrBedfordVan 0:b9164b348919 331 NRF_RADIO->EVENTS_END = 0;
MrBedfordVan 0:b9164b348919 332 NRF_RADIO->TASKS_START = 1;
MrBedfordVan 0:b9164b348919 333
MrBedfordVan 0:b9164b348919 334 // register ourselves for a callback event, in order to empty the receive queue.
MrBedfordVan 0:b9164b348919 335 fiber_add_idle_component(this);
MrBedfordVan 0:b9164b348919 336
MrBedfordVan 0:b9164b348919 337 // Done. Record that our RADIO is configured.
MrBedfordVan 0:b9164b348919 338 status |= MICROBIT_RADIO_STATUS_INITIALISED;
MrBedfordVan 0:b9164b348919 339
MrBedfordVan 0:b9164b348919 340 return MICROBIT_OK;
MrBedfordVan 0:b9164b348919 341 }
MrBedfordVan 0:b9164b348919 342
MrBedfordVan 0:b9164b348919 343 /**
MrBedfordVan 0:b9164b348919 344 * Disables the radio for use as a multipoint sender/receiver.
MrBedfordVan 0:b9164b348919 345 *
MrBedfordVan 0:b9164b348919 346 * @return MICROBIT_OK on success, MICROBIT_NOT_SUPPORTED if the BLE stack is running.
MrBedfordVan 0:b9164b348919 347 */
MrBedfordVan 0:b9164b348919 348 int MicroBitRadio::disable()
MrBedfordVan 0:b9164b348919 349 {
MrBedfordVan 0:b9164b348919 350 // Only attempt to enable.disable the radio if the protocol is alreayd running.
MrBedfordVan 0:b9164b348919 351 if (ble_running())
MrBedfordVan 0:b9164b348919 352 return MICROBIT_NOT_SUPPORTED;
MrBedfordVan 0:b9164b348919 353
MrBedfordVan 0:b9164b348919 354 if (!(status & MICROBIT_RADIO_STATUS_INITIALISED))
MrBedfordVan 0:b9164b348919 355 return MICROBIT_OK;
MrBedfordVan 0:b9164b348919 356
MrBedfordVan 0:b9164b348919 357 // Disable interrupts and STOP any ongoing packet reception.
MrBedfordVan 0:b9164b348919 358 NVIC_DisableIRQ(RADIO_IRQn);
MrBedfordVan 0:b9164b348919 359
MrBedfordVan 0:b9164b348919 360 NRF_RADIO->EVENTS_DISABLED = 0;
MrBedfordVan 0:b9164b348919 361 NRF_RADIO->TASKS_DISABLE = 1;
MrBedfordVan 0:b9164b348919 362 while(NRF_RADIO->EVENTS_DISABLED == 0);
MrBedfordVan 0:b9164b348919 363
MrBedfordVan 0:b9164b348919 364 // deregister ourselves from the callback event used to empty the receive queue.
MrBedfordVan 0:b9164b348919 365 fiber_remove_idle_component(this);
MrBedfordVan 0:b9164b348919 366
MrBedfordVan 0:b9164b348919 367 // record that the radio is now disabled
MrBedfordVan 0:b9164b348919 368 status &= ~MICROBIT_RADIO_STATUS_INITIALISED;
MrBedfordVan 0:b9164b348919 369
MrBedfordVan 0:b9164b348919 370 return MICROBIT_OK;
MrBedfordVan 0:b9164b348919 371 }
MrBedfordVan 0:b9164b348919 372
MrBedfordVan 0:b9164b348919 373 /**
MrBedfordVan 0:b9164b348919 374 * Sets the radio to listen to packets sent with the given group id.
MrBedfordVan 0:b9164b348919 375 *
MrBedfordVan 0:b9164b348919 376 * @param group The group to join. A micro:bit can only listen to one group ID at any time.
MrBedfordVan 0:b9164b348919 377 *
MrBedfordVan 0:b9164b348919 378 * @return MICROBIT_OK on success, or MICROBIT_NOT_SUPPORTED if the BLE stack is running.
MrBedfordVan 0:b9164b348919 379 */
MrBedfordVan 0:b9164b348919 380 int MicroBitRadio::setGroup(uint8_t group)
MrBedfordVan 0:b9164b348919 381 {
MrBedfordVan 0:b9164b348919 382 if (ble_running())
MrBedfordVan 0:b9164b348919 383 return MICROBIT_NOT_SUPPORTED;
MrBedfordVan 0:b9164b348919 384
MrBedfordVan 0:b9164b348919 385 // Record our group id locally
MrBedfordVan 0:b9164b348919 386 this->group = group;
MrBedfordVan 0:b9164b348919 387
MrBedfordVan 0:b9164b348919 388 // Also append it to the address of this device, to allow the RADIO module to filter for us.
MrBedfordVan 0:b9164b348919 389 NRF_RADIO->PREFIX0 = (uint32_t)group;
MrBedfordVan 0:b9164b348919 390
MrBedfordVan 0:b9164b348919 391 return MICROBIT_OK;
MrBedfordVan 0:b9164b348919 392 }
MrBedfordVan 0:b9164b348919 393
MrBedfordVan 0:b9164b348919 394 /**
MrBedfordVan 0:b9164b348919 395 * A background, low priority callback that is triggered whenever the processor is idle.
MrBedfordVan 0:b9164b348919 396 * Here, we empty our queue of received packets, and pass them onto higher level protocol handlers.
MrBedfordVan 0:b9164b348919 397 */
MrBedfordVan 0:b9164b348919 398 void MicroBitRadio::idleTick()
MrBedfordVan 0:b9164b348919 399 {
MrBedfordVan 0:b9164b348919 400 // Walk the list of packets and process each one.
MrBedfordVan 0:b9164b348919 401 while(rxQueue)
MrBedfordVan 0:b9164b348919 402 {
MrBedfordVan 0:b9164b348919 403 FrameBuffer *p = rxQueue;
MrBedfordVan 0:b9164b348919 404
MrBedfordVan 0:b9164b348919 405 switch (p->protocol)
MrBedfordVan 0:b9164b348919 406 {
MrBedfordVan 0:b9164b348919 407 case MICROBIT_RADIO_PROTOCOL_DATAGRAM:
MrBedfordVan 0:b9164b348919 408 datagram.packetReceived();
MrBedfordVan 0:b9164b348919 409 break;
MrBedfordVan 0:b9164b348919 410
MrBedfordVan 0:b9164b348919 411 case MICROBIT_RADIO_PROTOCOL_EVENTBUS:
MrBedfordVan 0:b9164b348919 412 event.packetReceived();
MrBedfordVan 0:b9164b348919 413 break;
MrBedfordVan 0:b9164b348919 414
MrBedfordVan 0:b9164b348919 415 default:
MrBedfordVan 0:b9164b348919 416 MicroBitEvent(MICROBIT_ID_RADIO_DATA_READY, p->protocol);
MrBedfordVan 0:b9164b348919 417 }
MrBedfordVan 0:b9164b348919 418
MrBedfordVan 0:b9164b348919 419 // If the packet was processed, it will have been recv'd, and taken from the queue.
MrBedfordVan 0:b9164b348919 420 // If this was a packet for an unknown protocol, it will still be there, so simply free it.
MrBedfordVan 0:b9164b348919 421 if (p == rxQueue)
MrBedfordVan 0:b9164b348919 422 {
MrBedfordVan 0:b9164b348919 423 recv();
MrBedfordVan 0:b9164b348919 424 delete p;
MrBedfordVan 0:b9164b348919 425 }
MrBedfordVan 0:b9164b348919 426 }
MrBedfordVan 0:b9164b348919 427 }
MrBedfordVan 0:b9164b348919 428
MrBedfordVan 0:b9164b348919 429 /**
MrBedfordVan 0:b9164b348919 430 * Determines the number of packets ready to be processed.
MrBedfordVan 0:b9164b348919 431 *
MrBedfordVan 0:b9164b348919 432 * @return The number of packets in the receive buffer.
MrBedfordVan 0:b9164b348919 433 */
MrBedfordVan 0:b9164b348919 434 int MicroBitRadio::dataReady()
MrBedfordVan 0:b9164b348919 435 {
MrBedfordVan 0:b9164b348919 436 return queueDepth;
MrBedfordVan 0:b9164b348919 437 }
MrBedfordVan 0:b9164b348919 438
MrBedfordVan 0:b9164b348919 439 /**
MrBedfordVan 0:b9164b348919 440 * Retrieves the next packet from the receive buffer.
MrBedfordVan 0:b9164b348919 441 * If a data packet is available, then it will be returned immediately to
MrBedfordVan 0:b9164b348919 442 * the caller. This call will also dequeue the buffer.
MrBedfordVan 0:b9164b348919 443 *
MrBedfordVan 0:b9164b348919 444 * @return The buffer containing the the packet. If no data is available, NULL is returned.
MrBedfordVan 0:b9164b348919 445 *
MrBedfordVan 0:b9164b348919 446 * @note Once recv() has been called, it is the callers responsibility to
MrBedfordVan 0:b9164b348919 447 * delete the buffer when appropriate.
MrBedfordVan 0:b9164b348919 448 */
MrBedfordVan 0:b9164b348919 449 FrameBuffer* MicroBitRadio::recv()
MrBedfordVan 0:b9164b348919 450 {
MrBedfordVan 0:b9164b348919 451 FrameBuffer *p = rxQueue;
MrBedfordVan 0:b9164b348919 452
MrBedfordVan 0:b9164b348919 453 if (p)
MrBedfordVan 0:b9164b348919 454 {
MrBedfordVan 0:b9164b348919 455 // Protect shared resource from ISR activity
MrBedfordVan 0:b9164b348919 456 NVIC_DisableIRQ(RADIO_IRQn);
MrBedfordVan 0:b9164b348919 457
MrBedfordVan 0:b9164b348919 458 rxQueue = rxQueue->next;
MrBedfordVan 0:b9164b348919 459 queueDepth--;
MrBedfordVan 0:b9164b348919 460
MrBedfordVan 0:b9164b348919 461 // Allow ISR access to shared resource
MrBedfordVan 0:b9164b348919 462 NVIC_EnableIRQ(RADIO_IRQn);
MrBedfordVan 0:b9164b348919 463 }
MrBedfordVan 0:b9164b348919 464
MrBedfordVan 0:b9164b348919 465 return p;
MrBedfordVan 0:b9164b348919 466 }
MrBedfordVan 0:b9164b348919 467
MrBedfordVan 0:b9164b348919 468 /**
MrBedfordVan 0:b9164b348919 469 * Transmits the given buffer onto the broadcast radio.
MrBedfordVan 0:b9164b348919 470 * The call will wait until the transmission of the packet has completed before returning.
MrBedfordVan 0:b9164b348919 471 *
MrBedfordVan 0:b9164b348919 472 * @param data The packet contents to transmit.
MrBedfordVan 0:b9164b348919 473 *
MrBedfordVan 0:b9164b348919 474 * @return MICROBIT_OK on success, or MICROBIT_NOT_SUPPORTED if the BLE stack is running.
MrBedfordVan 0:b9164b348919 475 */
MrBedfordVan 0:b9164b348919 476 int MicroBitRadio::send(FrameBuffer *buffer)
MrBedfordVan 0:b9164b348919 477 {
MrBedfordVan 0:b9164b348919 478 if (ble_running())
MrBedfordVan 0:b9164b348919 479 return MICROBIT_NOT_SUPPORTED;
MrBedfordVan 0:b9164b348919 480
MrBedfordVan 0:b9164b348919 481 if (buffer == NULL)
MrBedfordVan 0:b9164b348919 482 return MICROBIT_INVALID_PARAMETER;
MrBedfordVan 0:b9164b348919 483
MrBedfordVan 0:b9164b348919 484 if (buffer->length > MICROBIT_RADIO_MAX_PACKET_SIZE + MICROBIT_RADIO_HEADER_SIZE - 1)
MrBedfordVan 0:b9164b348919 485 return MICROBIT_INVALID_PARAMETER;
MrBedfordVan 0:b9164b348919 486
MrBedfordVan 0:b9164b348919 487 // Firstly, disable the Radio interrupt. We want to wait until the trasmission completes.
MrBedfordVan 0:b9164b348919 488 NVIC_DisableIRQ(RADIO_IRQn);
MrBedfordVan 0:b9164b348919 489
MrBedfordVan 0:b9164b348919 490 // Turn off the transceiver.
MrBedfordVan 0:b9164b348919 491 NRF_RADIO->EVENTS_DISABLED = 0;
MrBedfordVan 0:b9164b348919 492 NRF_RADIO->TASKS_DISABLE = 1;
MrBedfordVan 0:b9164b348919 493 while(NRF_RADIO->EVENTS_DISABLED == 0);
MrBedfordVan 0:b9164b348919 494
MrBedfordVan 0:b9164b348919 495 // Configure the radio to send the buffer provided.
MrBedfordVan 0:b9164b348919 496 NRF_RADIO->PACKETPTR = (uint32_t) buffer;
MrBedfordVan 0:b9164b348919 497
MrBedfordVan 0:b9164b348919 498 // Turn on the transmitter, and wait for it to signal that it's ready to use.
MrBedfordVan 0:b9164b348919 499 NRF_RADIO->EVENTS_READY = 0;
MrBedfordVan 0:b9164b348919 500 NRF_RADIO->TASKS_TXEN = 1;
MrBedfordVan 0:b9164b348919 501 while (NRF_RADIO->EVENTS_READY == 0);
MrBedfordVan 0:b9164b348919 502
MrBedfordVan 0:b9164b348919 503 // Start transmission and wait for end of packet.
MrBedfordVan 0:b9164b348919 504 NRF_RADIO->TASKS_START = 1;
MrBedfordVan 0:b9164b348919 505 NRF_RADIO->EVENTS_END = 0;
MrBedfordVan 0:b9164b348919 506 while(NRF_RADIO->EVENTS_END == 0);
MrBedfordVan 0:b9164b348919 507
MrBedfordVan 0:b9164b348919 508 // Return the radio to using the default receive buffer
MrBedfordVan 0:b9164b348919 509 NRF_RADIO->PACKETPTR = (uint32_t) rxBuf;
MrBedfordVan 0:b9164b348919 510
MrBedfordVan 0:b9164b348919 511 // Turn off the transmitter.
MrBedfordVan 0:b9164b348919 512 NRF_RADIO->EVENTS_DISABLED = 0;
MrBedfordVan 0:b9164b348919 513 NRF_RADIO->TASKS_DISABLE = 1;
MrBedfordVan 0:b9164b348919 514 while(NRF_RADIO->EVENTS_DISABLED == 0);
MrBedfordVan 0:b9164b348919 515
MrBedfordVan 0:b9164b348919 516 // Start listening for the next packet
MrBedfordVan 0:b9164b348919 517 NRF_RADIO->EVENTS_READY = 0;
MrBedfordVan 0:b9164b348919 518 NRF_RADIO->TASKS_RXEN = 1;
MrBedfordVan 0:b9164b348919 519 while(NRF_RADIO->EVENTS_READY == 0);
MrBedfordVan 0:b9164b348919 520
MrBedfordVan 0:b9164b348919 521 NRF_RADIO->EVENTS_END = 0;
MrBedfordVan 0:b9164b348919 522 NRF_RADIO->TASKS_START = 1;
MrBedfordVan 0:b9164b348919 523
MrBedfordVan 0:b9164b348919 524 // Re-enable the Radio interrupt.
MrBedfordVan 0:b9164b348919 525 NVIC_ClearPendingIRQ(RADIO_IRQn);
MrBedfordVan 0:b9164b348919 526 NVIC_EnableIRQ(RADIO_IRQn);
MrBedfordVan 0:b9164b348919 527
MrBedfordVan 0:b9164b348919 528 return MICROBIT_OK;
MrBedfordVan 0:b9164b348919 529 }