@@ -33,7 +33,7 @@ extern "C" {
3333#include " py/runtime.h"
3434#include " microbitobj.h"
3535
36- static uint8_t *buf_start = NULL ; // XXX root pointer
36+ static uint8_t *buf_start = NULL ; // NULL when radio is disabled.
3737static uint8_t *buf_end = NULL ;
3838static uint8_t *rx_buf = NULL ;
3939
@@ -70,20 +70,26 @@ void RADIO_IRQHandler(void) {
7070 }
7171}
7272
73+ static void ensure_enabled (void ) {
74+ if (buf_start == NULL ) {
75+ nlr_raise (mp_obj_new_exception_msg (&mp_type_ValueError, " radio is not enabled" ));
76+ }
77+ }
78+
7379static void radio_disable (void ) {
7480 NVIC_DisableIRQ (RADIO_IRQn);
7581 NRF_RADIO->EVENTS_DISABLED = 0 ;
7682 NRF_RADIO->TASKS_DISABLE = 1 ;
7783 while (NRF_RADIO->EVENTS_DISABLED == 0 );
78- }
79-
80- static void radio_enable (size_t max_payload, size_t queue_len) {
81- radio_disable ();
82-
8384 // free any old buffers
8485 if (buf_start != NULL ) {
8586 m_del (uint8_t , buf_start, buf_end - buf_start);
87+ buf_start = NULL ;
8688 }
89+ }
90+
91+ static void radio_enable (size_t max_payload, size_t queue_len) {
92+ radio_disable ();
8793
8894 // allocate tx and rx buffers
8995 queue_len += 1 ; // one extra for tx buffer
@@ -154,6 +160,7 @@ static void radio_enable(size_t max_payload, size_t queue_len) {
154160}
155161
156162void radio_send (const uint8_t *buf, size_t len) {
163+ ensure_enabled ();
157164 // construct the packet
158165 // note: we must send from RAM
159166 size_t max_len = NRF_RADIO->PCNF1 & 0xff ;
@@ -230,6 +237,7 @@ STATIC mp_obj_t mod_radio_send(mp_obj_t buf_in) {
230237MP_DEFINE_CONST_FUN_OBJ_1 (mod_radio_send_obj, mod_radio_send);
231238
232239STATIC mp_obj_t mod_radio_recv (void ) {
240+ ensure_enabled ();
233241 NVIC_DisableIRQ (RADIO_IRQn);
234242 uint8_t *buf = buf_start + (NRF_RADIO->PCNF1 & 0xff ) + 1 ; // skip tx buf
235243 if (rx_buf == buf) {
0 commit comments