@@ -109,8 +109,10 @@ MicroBitRadio::MicroBitRadio(uint16_t id) : datagram(*this), event (*this)
109
109
{
110
110
this ->id = id;
111
111
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 ;
114
116
this ->rssi = 0 ;
115
117
this ->rxQueue = NULL ;
116
118
this ->rxBuf = NULL ;
@@ -130,6 +132,9 @@ int MicroBitRadio::setTransmitPower(int power)
130
132
if (power < 0 || power >= MICROBIT_RADIO_POWER_LEVELS)
131
133
return DEVICE_INVALID_PARAMETER;
132
134
135
+ // Record our power locally
136
+ this ->power = power;
137
+
133
138
NRF_RADIO->TXPOWER = (uint32_t )MICROBIT_RADIO_POWER_LEVEL[power];
134
139
135
140
return DEVICE_OK;
@@ -151,7 +156,30 @@ int MicroBitRadio::setFrequencyBand(int band)
151
156
if (band < 0 || band > 100 )
152
157
return DEVICE_INVALID_PARAMETER;
153
158
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
+ }
155
183
156
184
return DEVICE_OK;
157
185
}
@@ -277,9 +305,9 @@ int MicroBitRadio::enable()
277
305
NRF_CLOCK->TASKS_HFCLKSTART = 1 ;
278
306
while (NRF_CLOCK->EVENTS_HFCLKSTARTED == 0 );
279
307
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 ;
283
311
284
312
// Configure for 1Mbps throughput.
285
313
// This may sound excessive, but running a high data rates reduces the chances of collisions...
0 commit comments