Skip to content

Commit bd5e46e

Browse files
authored
MicroBitRadio: fix setFrequencyBand() and do not reset tx power and frequency band in enable() (#252)
* MicroBitRadio::enable - Do not reset tx power or frequency band * MicroBitRadio::setFrequencyBand - restart radio
1 parent a61573a commit bd5e46e

File tree

2 files changed

+36
-6
lines changed

2 files changed

+36
-6
lines changed

inc/MicroBitRadio.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,8 @@ namespace codal
100100

101101
class MicroBitRadio : CodalComponent
102102
{
103+
uint8_t band; // The radio transmission and reception frequency band.
104+
uint8_t power; // The radio output power level of the transmitter.
103105
uint8_t group; // The radio group to which this micro:bit belongs.
104106
uint8_t queueDepth; // The number of packets in the receiver queue.
105107
int rssi;

source/MicroBitRadio.cpp

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,10 @@ MicroBitRadio::MicroBitRadio(uint16_t id) : datagram(*this), event (*this)
109109
{
110110
this->id = id;
111111
this->status = 0;
112-
this->group = MICROBIT_RADIO_DEFAULT_GROUP;
113-
this->queueDepth = 0;
112+
this->band = MICROBIT_RADIO_DEFAULT_FREQUENCY;
113+
this->power = MICROBIT_RADIO_DEFAULT_TX_POWER;
114+
this->group = MICROBIT_RADIO_DEFAULT_GROUP;
115+
this->queueDepth = 0;
114116
this->rssi = 0;
115117
this->rxQueue = NULL;
116118
this->rxBuf = NULL;
@@ -130,6 +132,9 @@ int MicroBitRadio::setTransmitPower(int power)
130132
if (power < 0 || power >= MICROBIT_RADIO_POWER_LEVELS)
131133
return DEVICE_INVALID_PARAMETER;
132134

135+
// Record our power locally
136+
this->power = power;
137+
133138
NRF_RADIO->TXPOWER = (uint32_t)MICROBIT_RADIO_POWER_LEVEL[power];
134139

135140
return DEVICE_OK;
@@ -151,7 +156,30 @@ int MicroBitRadio::setFrequencyBand(int band)
151156
if (band < 0 || band > 100)
152157
return DEVICE_INVALID_PARAMETER;
153158

154-
NRF_RADIO->FREQUENCY = (uint32_t)band;
159+
// Record our frequency band locally
160+
this->band = band;
161+
162+
if ( NRF_RADIO->FREQUENCY != (uint32_t) band && (status & MICROBIT_RADIO_STATUS_INITIALISED))
163+
{
164+
// We need to restart the radio for the frequency change to take effect
165+
NVIC_DisableIRQ(RADIO_IRQn);
166+
NRF_RADIO->EVENTS_DISABLED = 0;
167+
NRF_RADIO->TASKS_DISABLE = 1;
168+
while (NRF_RADIO->EVENTS_DISABLED == 0);
169+
170+
NRF_RADIO->FREQUENCY = (uint32_t) band;
171+
172+
// Reenable the radio to wait for the next packet
173+
NRF_RADIO->EVENTS_READY = 0;
174+
NRF_RADIO->TASKS_RXEN = 1;
175+
while (NRF_RADIO->EVENTS_READY == 0);
176+
177+
NRF_RADIO->EVENTS_END = 0;
178+
NRF_RADIO->TASKS_START = 1;
179+
180+
NVIC_ClearPendingIRQ(RADIO_IRQn);
181+
NVIC_EnableIRQ(RADIO_IRQn);
182+
}
155183

156184
return DEVICE_OK;
157185
}
@@ -277,9 +305,9 @@ int MicroBitRadio::enable()
277305
NRF_CLOCK->TASKS_HFCLKSTART = 1;
278306
while (NRF_CLOCK->EVENTS_HFCLKSTARTED == 0);
279307

280-
// Bring up the nrf51822 RADIO module in Nordic's proprietary 1MBps packet radio mode.
281-
setTransmitPower(MICROBIT_RADIO_DEFAULT_TX_POWER);
282-
setFrequencyBand(MICROBIT_RADIO_DEFAULT_FREQUENCY);
308+
// Bring up the nrf RADIO module in Nordic's proprietary 1MBps packet radio mode.
309+
NRF_RADIO->TXPOWER = (uint32_t)MICROBIT_RADIO_POWER_LEVEL[this->power];
310+
NRF_RADIO->FREQUENCY = (uint32_t)this->band;
283311

284312
// Configure for 1Mbps throughput.
285313
// This may sound excessive, but running a high data rates reduces the chances of collisions...

0 commit comments

Comments
 (0)