<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://devzone.nordicsemi.com/cfs-file/__key/system/syndication/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Low level 802.15.4 transmit code</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/121878/low-level-802-15-4-transmit-code</link><description>I am trying to make a simple MAC-layer transceiver using Arduino environment. I have some code derived from this example: https://github.com/IoTS-P/nRF52-802154-Raw-Transmitter and have discovered that it hangs after one to a few hundred transmissions</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Fri, 20 Jun 2025 13:35:08 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/121878/low-level-802-15-4-transmit-code" /><item><title>RE: Low level 802.15.4 transmit code</title><link>https://devzone.nordicsemi.com/thread/540047?ContentTypeID=1</link><pubDate>Fri, 20 Jun 2025 13:35:08 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:40ca4bbe-ec8d-4443-bd53-d0c117cdd844</guid><dc:creator>DU_Radio</dc:creator><description>&lt;p&gt;I have found the problem, it is a combination of two issues.&amp;nbsp; 1. The code as written relies on an interrupt from the radio to clear a transmit flag.&amp;nbsp; There are some rare cases where this interrupt is lost and the flag is never cleared. 2. The radio does indeed need full re-initialization in some cases but this is even rarer than #1.&lt;/p&gt;
&lt;p&gt;Statistics: 700,000 messages, 1 reset needed, then a thunderstorm blew through and in 500,000 messages, 14 resets were needed.&amp;nbsp; With a code change that clears the interrupt flag, and only resets of that did not work:&amp;nbsp; 1,099,000 messages, flag cleared 6 times, reset once.&amp;nbsp; I believe this resolves the issue.&lt;/p&gt;
&lt;p&gt;Thank you for your help.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Low level 802.15.4 transmit code</title><link>https://devzone.nordicsemi.com/thread/539546?ContentTypeID=1</link><pubDate>Tue, 17 Jun 2025 12:34:40 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:f6f9e345-099b-4d2c-87de-f61c8cac0c73</guid><dc:creator>DU_Radio</dc:creator><description>&lt;p&gt;In the4 second test it ran for 102,643 messages, then failed the CCA_busy.&amp;nbsp; I had it do a radio_set_sniff() which initializes all the radio registers the same as powerup, but that did not recover.&amp;nbsp; It does not hang, just always fails the CCA_busy.&amp;nbsp; &amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;while (!NRF_RADIO-&amp;gt;EVENTS_CCAIDLE &amp;amp;&amp;amp; !NRF_RADIO-&amp;gt;EVENTS_CCABUSY &amp;amp;&amp;amp; !sig_ccabusy &amp;amp;&amp;amp; --timeout);&lt;/pre&gt;Clearly the channel is not 100% busy for hours so is there another reset sequence that I should use?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Low level 802.15.4 transmit code</title><link>https://devzone.nordicsemi.com/thread/539457?ContentTypeID=1</link><pubDate>Tue, 17 Jun 2025 00:15:57 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:066ae957-74a7-4459-81e7-f37ee2ca975a</guid><dc:creator>hmolesworth</dc:creator><description>&lt;p&gt;I suggest adding a &lt;em&gt;--timeout&lt;/em&gt; check to every &lt;em&gt;while(..);&lt;/em&gt; instead of just a few of those; on timeout reset RADIO and start again. For example all &lt;em&gt;while&lt;/em&gt;s of the form:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;    while (!NRF_RADIO-&amp;gt;EVENTS_READY);&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;Most of us learn to adopt such defensive programming practices when dealing with hardware registers and devices which misbehave when least expected ..&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Low level 802.15.4 transmit code</title><link>https://devzone.nordicsemi.com/thread/539456?ContentTypeID=1</link><pubDate>Mon, 16 Jun 2025 23:57:17 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:b5d193d3-f5f4-4575-af5e-4884c01cdd93</guid><dc:creator>DU_Radio</dc:creator><description>&lt;p&gt;Susheel,&lt;/p&gt;
&lt;p&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;Thanks for the changes, they made a huge difference.&amp;nbsp; It runs for over 100K message, but then stops and fails the CCA forever after.&amp;nbsp; Is this a case where a full register-reload is needed?&amp;nbsp; I am testing that now.&amp;nbsp; Is it possible that some external event getting into the receiver, e.g. lightning, might flip a bit and require a register reload?&lt;/p&gt;
&lt;p&gt;Darryl&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Low level 802.15.4 transmit code</title><link>https://devzone.nordicsemi.com/thread/538547?ContentTypeID=1</link><pubDate>Mon, 09 Jun 2025 07:38:53 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:bebe802e-e96f-4e48-a298-69b2552dd001</guid><dc:creator>Susheel Nuguru</dc:creator><description>&lt;p&gt;I am no expert in this and also do not think that there is any flowchart readily available so I will try to write a template code for the suggestions I have provided below&lt;/p&gt;
&lt;p&gt;Can you please modify your&amp;nbsp;radio_send_custom in your radio.cpp as below&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;void radio_send_custom(uint8_t *pBuffer, uint8_t channel) {

  /* No shortcuts on disable. */
  NRF_RADIO-&amp;gt;SHORTS = 0x0;

  /* Switch radio to TX. */
  radio_disable();

  for (size_t try_ = 0; try_ &amp;lt; MAX_RETRIES; try_++) 
  {
    /* Switch packet buffer to tx_buffer. */
    NRF_RADIO-&amp;gt;PACKETPTR = (uint32_t)pBuffer;
    NRF_RADIO-&amp;gt;INTENSET = (1 &amp;lt;&amp;lt; RADIO_INTENSET_END_Pos);
    NVIC_ClearPendingIRQ(RADIO_IRQn);
    NVIC_EnableIRQ(RADIO_IRQn);

    NRF_RADIO-&amp;gt;EVENTS_READY = 0;
    NRF_RADIO-&amp;gt;TASKS_RXEN = 1;
    Serial.write(&amp;quot;While 1 &amp;quot;);
    while (!NRF_RADIO-&amp;gt;EVENTS_READY);

    NRF_RADIO-&amp;gt;EVENTS_CCAIDLE = 0;
    NRF_RADIO-&amp;gt;EVENTS_CCABUSY = 0;
    sig_ccabusy = false;
    NRF_RADIO-&amp;gt;TASKS_CCASTART = 1;

    Serial.write(&amp;quot;While 2 &amp;quot;);
    int timeout = 10000;
    while (!NRF_RADIO-&amp;gt;EVENTS_CCAIDLE &amp;amp;&amp;amp; !NRF_RADIO-&amp;gt;EVENTS_CCABUSY &amp;amp;&amp;amp; !sig_ccabusy &amp;amp;&amp;amp; --timeout);
    Serial.write(&amp;quot;While 3 &amp;quot;);

    if (timeout == 0 || sig_ccabusy || NRF_RADIO-&amp;gt;EVENTS_CCABUSY) 
    {
      Serial.println(&amp;quot;WARNING: retry for CCABUSY&amp;quot;);
      sig_ccabusy = false;
      radio_disable();
      delay(100);
      continue;
    }

    NRF_RADIO-&amp;gt;TXPOWER =
        (RADIO_TXPOWER_TXPOWER_Pos4dBm &amp;lt;&amp;lt; RADIO_TXPOWER_TXPOWER_Pos);
    NRF_RADIO-&amp;gt;FREQUENCY = channel_to_freq(channel);

    NRF_RADIO-&amp;gt;EVENTS_DISABLED = 0;
    NRF_RADIO-&amp;gt;TASKS_DISABLE = 1;
    while (!NRF_RADIO-&amp;gt;EVENTS_DISABLED);

    NRF_RADIO-&amp;gt;EVENTS_READY = 0;
    NRF_RADIO-&amp;gt;TASKS_TXEN = 1;
    Serial.write(&amp;quot;While 4\r\n&amp;quot;);
    while (!NRF_RADIO-&amp;gt;EVENTS_READY);
    NRF_RADIO-&amp;gt;TASKS_START = 1;
    return;
  }

  Serial.println(&amp;quot;ERROR: retry too many times&amp;quot;);
}
&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;Also&amp;nbsp;ensure CCAIDLE and CCABUSY are cleared in the IRQ handler&amp;nbsp;RADIO_IRQHandler&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;if (NRF_RADIO-&amp;gt;EVENTS_CCABUSY) {
  NRF_RADIO-&amp;gt;EVENTS_CCABUSY = 0;
  NRF_RADIO-&amp;gt;TASKS_DISABLE = 1;
  while (!NRF_RADIO-&amp;gt;EVENTS_DISABLED);
  sig_ccabusy = true;
}

if (NRF_RADIO-&amp;gt;EVENTS_CCAIDLE) {
  NRF_RADIO-&amp;gt;EVENTS_CCAIDLE = 0;
  sig_ccabusy = false;
}
&lt;/pre&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Low level 802.15.4 transmit code</title><link>https://devzone.nordicsemi.com/thread/538143?ContentTypeID=1</link><pubDate>Thu, 05 Jun 2025 03:53:05 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:6b283fac-e66f-4268-a23a-7e6bccef487c</guid><dc:creator>DU_Radio</dc:creator><description>&lt;p&gt;Do you have perhaps, a detailed flowchart of how to configure and operate the radio? Or perhaps someone who could correct the rather simple low-level radio software that I attached?&amp;nbsp; Only radio.c access the hardware. The project can be build with Arduino 1.8 IDE, most college engineering students could handle that.&amp;nbsp; Or are you suggesting that Nordic has lost the tribal knowledge to run their own hardware?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Low level 802.15.4 transmit code</title><link>https://devzone.nordicsemi.com/thread/537799?ContentTypeID=1</link><pubDate>Tue, 03 Jun 2025 08:38:12 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:f7bd882c-c639-44f4-bcdb-7c78948ff253</guid><dc:creator>Susheel Nuguru</dc:creator><description>&lt;p&gt;So DU_Radio,&amp;nbsp;&lt;/p&gt;
&lt;p&gt;We unfortunately do not have that as we are working with nRF Connect SDK now.&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Low level 802.15.4 transmit code</title><link>https://devzone.nordicsemi.com/thread/537762?ContentTypeID=1</link><pubDate>Tue, 03 Jun 2025 03:12:10 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:82685f7e-3020-4213-bc40-32c0f805a3b5</guid><dc:creator>DU_Radio</dc:creator><description>&lt;p&gt;Susheel,&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; If you have any low level transmit example code in C, for any platform, that would likely provide the quickest solution.&amp;nbsp;&amp;nbsp; There was some on the Nordic site at one time but it seems to have been taken down some years ago.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Low level 802.15.4 transmit code</title><link>https://devzone.nordicsemi.com/thread/537755?ContentTypeID=1</link><pubDate>Tue, 03 Jun 2025 02:03:27 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:030c50dc-ac3a-4621-8964-ff76b8aff9ff</guid><dc:creator>DU_Radio</dc:creator><description>&lt;p&gt;Susheel,&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Thank you for your advice on CCA cleanup.&amp;nbsp; Does it need a full powerup init (and if so why?) or just the CCA threshold and clear events and re-enable the tasks? I ask because a full powerup init when CCA_IDLE was false did not help.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; The code has most of the constants from the original GIT repo (not my project), these are the register addresses in the nRFD52840 datasheet so I thought they would be more meaningfull to the Nordic hardware folks.&amp;nbsp; Also I was trying to avlod introducing more problems by incorrectly editing the register references.&amp;nbsp; Yes it is certainly ugly. Once it is working I will do a major code cleanup.&lt;/p&gt;
&lt;p&gt;Since this is a Nordic radio issue and not related to the platform it was built on, is there another forum that would be better suited to troubleshooting the radio?&lt;/p&gt;
&lt;p&gt;Darryl&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Low level 802.15.4 transmit code</title><link>https://devzone.nordicsemi.com/thread/537636?ContentTypeID=1</link><pubDate>Mon, 02 Jun 2025 11:08:47 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:513ff57d-0b7e-4750-be85-74123abba53a</guid><dc:creator>Susheel Nuguru</dc:creator><description>&lt;p&gt;Hi DU_radio,&lt;/p&gt;
&lt;p&gt;The repo looks lt uses PlatformIO and the API is a unknow to me. It is hard for us to review something that is using external SDKs, too many unknows to properly give you a proper debugging direction.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Regarding the radio handling, I can say one thing that you did not seem to have handled failed CCA scenario properly, it is wise to do a cleanup, deinit and reinit in that case. Also there are a lot of hardcoded values in your code, not very pleasant to read or review. I suggest you take this question to a proper forum&amp;nbsp;API you are using here to implement this.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Low level 802.15.4 transmit code</title><link>https://devzone.nordicsemi.com/thread/537560?ContentTypeID=1</link><pubDate>Sat, 31 May 2025 04:25:55 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:18b1fcf4-a074-4f12-8a2e-ee6a1ee0d7e0</guid><dc:creator>DU_Radio</dc:creator><description>&lt;p&gt;My uploaded files went to some bit bucket.&amp;nbsp; Here is the cleaned up code (3 files):&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;//Raw802.ino
#include &amp;lt;Arduino.h&amp;gt;
#include &amp;lt;stdint.h&amp;gt;
//#include &amp;lt;stdbool.h&amp;gt;
#include &amp;quot;radio.h&amp;quot;
#include &amp;lt;nrf52840.h&amp;gt;
#include &amp;lt;Adafruit_TinyUSB.h&amp;gt;// Seems to need this without Serial or USB. Maybe DFU boot loader needs it?

#define PIN_LEDX 22 // Red LED

// channel 
#define DEFAULT_INITIAL_CHANNEL 19

// read buffer  
uint8_t *rx_buffer;
uint8_t u8SeqNum = 0;
// the frame length in phy layer header 
uint8_t pkt_len = 0;
uint8_t u8PktBuf[100];

// has data transmitting
uint8_t bTransmitting = false;

// packets
#define MAX_PKTS_IN 12 // Maximum packets in the queue
struct __attribute__((packed)) packets_structure
{
  uint16_t size[MAX_PKTS_IN] = {0};
  uint8_t *pkt_buffer[MAX_PKTS_IN];
  uint8_t head = 0;
  uint8_t tail = 0;
} packets;

// channel
uint8_t radio_channel = DEFAULT_INITIAL_CHANNEL;


/**
 * nRF51822 RADIO handler.
 *
 * This handler is called whenever a RADIO event occurs (IRQ).
 **/
extern &amp;quot;C&amp;quot; void RADIO_IRQHandler(void)
{
  // if (NRF_RADIO-&amp;gt;EVENTS_FRAMESTART)
  if (*(uint32_t *)(0x40001138)) //Events-FrameStart
  {
    *(uint32_t *)(0x40001138) = 0;
  }

  if (NRF_RADIO-&amp;gt;EVENTS_READY)
  {
    NRF_RADIO-&amp;gt;EVENTS_READY = 0;
    NRF_RADIO-&amp;gt;TASKS_START = 1;
  }

  if (NRF_RADIO-&amp;gt;EVENTS_END)
  {
    NRF_RADIO-&amp;gt;EVENTS_END = 0;
    // NRF_RADIO-&amp;gt;TASKS_START = 1;
    // If transmitt end
    if (bTransmitting)
    {
      bTransmitting = false;
      //Serial.write(&amp;quot;SENDED &amp;quot;);
       //radio_tx_to_rx();
      //return;// maybe interrupt needs special return?
      goto exit;
    }
  }
  if (*(uint32_t *)(0x40001148))//CCA-Busy
  {
    Serial.println(&amp;quot;CCABUSY&amp;quot;);
    *(uint32_t *)(0x40001148) = 0;
    NRF_RADIO-&amp;gt;TASKS_DISABLE = 1;
    while (NRF_RADIO-&amp;gt;EVENTS_DISABLED == 0);
    //Serial.println(&amp;quot;CCABUSY: disable the radio&amp;quot;);
    sig_ccabusy = true;
  }
  if (*(uint32_t *)(0x4000114C))
  {
    Serial.println(&amp;quot;CCASTOPPED&amp;quot;);
  }
  if (*(uint32_t *)(0x40001144))//CCA-IDLE
  {
    Serial.println(&amp;quot;CCAIDLE&amp;quot;);
    sig_ccabusy = false;
    *(uint32_t *)(0x40001144) = 0;
    // NRF_RADIO-&amp;gt;TASKS_CCASTART = 0
    //*(uint32_t *)(0x4000102c) = 1;
  }
  exit:
  {
  
  }
}

void setup()
{
  pinMode(LED_BUILTIN, OUTPUT);
  pinMode(PIN_LEDX, OUTPUT); // Going for the RED Led now
  Serial.begin(115200);
  rx_buffer = (uint8_t *)malloc(IEEE802154_FRAME_LEN_MAX + 3);

  radio_set_sniff(radio_channel, 0); // Setup the radio
}

uint32_t ctr = 0;
void loop()
{
  if (NRF_RADIO-&amp;gt;EVENTS_READY)
  {
    NRF_RADIO-&amp;gt;EVENTS_READY = 0;
    // NRF_RADIO-&amp;gt;TASKS_START;
  }
  delay(1);
  ctr++;
  if(ctr &amp;gt; 900)// Transmit every 200mS
  {
    ctr = 0;
    pkt_len = 17; 
    memcpy(u8PktBuf, &amp;quot;\x11\x00\x00\x00\x11\x22\x33\x44\x55\x66\x77\x88\x99\xaa\xbb\xcc\xdd\xee\xff&amp;quot;,19);
    u8PktBuf[3] = u8SeqNum++;
  }


  // memcpy pkt_buffer to tx_buffer
  if (!bTransmitting &amp;amp;&amp;amp; pkt_len)
  {
    Serial.write(&amp;quot;.&amp;quot;);
    digitalWrite(PIN_LEDX, HIGH);
    bTransmitting = true;
    radio_send_custom(u8PktBuf, radio_channel);
    pkt_len = 0;
    digitalWrite(PIN_LEDX, LOW);
  }
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;//radio.cpp
#include &amp;quot;radio.h&amp;quot;
#include &amp;lt;Arduino.h&amp;gt;
#include &amp;lt;assert.h&amp;gt;

// signal for CCABUSY
volatile uint8_t sig_ccabusy = false;

/**
 * channel_to_freq(int channel)
 *
 * Convert a BLE channel number into the corresponding frequency offset
 * for the nRF51822.
 **/
uint8_t channel_to_freq(int channel) {
  assert(channel &amp;gt; 10);
  assert(channel &amp;lt; 27);
  return 5 * (channel - 10);
}

/**
 * radio_disable()
 *
 * Disable the radio.
 **/

void radio_disable(void) {
  if (NRF_RADIO-&amp;gt;STATE &amp;gt; 0) {
    NVIC_DisableIRQ(RADIO_IRQn);

    NRF_RADIO-&amp;gt;EVENTS_DISABLED = 0;
    NRF_RADIO-&amp;gt;TASKS_DISABLE = 1;
    while (NRF_RADIO-&amp;gt;EVENTS_DISABLED == 0);
  }
}

/**
 * @brief   Convert from dBm to the internal representation, when the
 *          radio operates as a IEEE802.15.4 transceiver.
 */
static inline uint8_t _dbm_to_ieee802154_hwval(int8_t dbm) {
  return ((dbm - ED_RSSIOFFS) / ED_RSSISCALE);
}

static int set_cca_threshold(int8_t threshold) {

  if (threshold &amp;lt; ED_RSSIOFFS) {
    return -1;
  }

  uint8_t hw_val = _dbm_to_ieee802154_hwval(threshold);

   NRF_RADIO-&amp;gt;CCACTRL &amp;amp;= ~RADIO_CCACTRL_CCAEDTHRES_Msk;
  //*(uint32_t *)(0x4000166c) &amp;amp;= ~RADIO_CCACTRL_CCAEDTHRES_Msk;

   NRF_RADIO-&amp;gt;CCACTRL |= hw_val &amp;lt;&amp;lt; RADIO_CCACTRL_CCAEDTHRES_Pos;
  //*(uint32_t *)(0x4000166c) |= hw_val &amp;lt;&amp;lt; RADIO_CCACTRL_CCAEDTHRES_Pos;
  return 0;
}

/**
 * radio_set_sniff(int channel)
 *
 * Configure the nRF51822 to sniff on a specific channel.
 **/

void radio_set_sniff(int channel, uint32_t access_address) {
  // Disable radio
  radio_disable();

  // Enable the High Frequency clock on the processor. This is a pre-requisite
  // for the RADIO module. Without this clock, no communication is possible.
  NRF_CLOCK-&amp;gt;EVENTS_HFCLKSTARTED = 0;
  NRF_CLOCK-&amp;gt;TASKS_HFCLKSTART = 1;
  while (NRF_CLOCK-&amp;gt;EVENTS_HFCLKSTARTED == 0);

  // power should be one of: -30, -20, -16, -12, -8, -4, 0, 4
  NRF_RADIO-&amp;gt;TXPOWER = RADIO_TXPOWER_TXPOWER_Neg20dBm;//Pos4dBm;
  NRF_RADIO-&amp;gt;TXADDRESS = 0;

  /* Set 802154 data rate. */
  NRF_RADIO-&amp;gt;MODE = RADIO_MODE_MODE_Ieee802154_250Kbit;
  /* Listen on channel . */
  NRF_RADIO-&amp;gt;FREQUENCY = channel_to_freq(channel);

  /* set start frame delimiter */
   //NRF_RADIO-&amp;gt;RESERVED12[4] = IEEE802154_SFD;// Gets a read-only error on ths
  *(uint32_t *)(0x40001660) = IEEE802154_SFD;

  /* set MHR filters */
   NRF_RADIO-&amp;gt;MHRMATCHCONF = 0;         /* Search Pattern Configuration */
  //*(uint32_t *)(0x40001644) = 0;

   NRF_RADIO-&amp;gt;MHRMATCHMAS = 0xff0007ff; /* Pattern mask */
  //*(uint32_t *)(0x40001648) = 0xff0007ff;

  /* and set some fitting configuration */
  NRF_RADIO-&amp;gt;PCNF0 = ((8 &amp;lt;&amp;lt; RADIO_PCNF0_LFLEN_Pos) |
                      (RADIO_PCNF0_PLEN_32bitZero &amp;lt;&amp;lt; RADIO_PCNF0_PLEN_Pos) |
                      (RADIO_PCNF0_CRCINC_Include &amp;lt;&amp;lt; RADIO_PCNF0_CRCINC_Pos));

  NRF_RADIO-&amp;gt;PCNF1 = IEEE802154_FRAME_LEN_MAX;

  NRF_RADIO-&amp;gt;MODECNF0 |=
      RADIO_MODECNF0_RU_Fast; // Enable fast mode for radio ramp up

  // Enable CRC
  NRF_RADIO-&amp;gt;CRCCNF =
      ((RADIO_CRCCNF_LEN_Two &amp;lt;&amp;lt; RADIO_CRCCNF_LEN_Pos) |
       (RADIO_CRCCNF_SKIPADDR_Ieee802154 &amp;lt;&amp;lt; RADIO_CRCCNF_SKIPADDR_Pos));
  NRF_RADIO-&amp;gt;CRCPOLY = 0x11021;
  NRF_RADIO-&amp;gt;CRCINIT = 0;

  // DATAWHITE
  NRF_RADIO-&amp;gt;DATAWHITEIV = 0x40;

  NRF_RADIO-&amp;gt;PACKETPTR = (uint32_t)(rx_buffer);

  // configure CCA
  set_cca_threshold(CONFIG_IEEE802154_CCA_THRESH_DEFAULT);

  // Configure interrupts
  NRF_RADIO-&amp;gt;INTENSET = RADIO_INTENSET_END_Msk | RADIO_INTENSET_FRAMESTART_Msk |
                        // RADIO_INTENSET_CCAIDLE_Msk |
                        RADIO_INTENSET_CCABUSY_Msk;
  // Enable NVIC Interrupt for Radio
  NVIC_SetPriority(RADIO_IRQn, IRQ_PRIORITY_LOW);
  NVIC_ClearPendingIRQ(RADIO_IRQn);
  NVIC_EnableIRQ(RADIO_IRQn);

  // Enable receiver hardware
  NRF_RADIO-&amp;gt;EVENTS_READY = 0;
  NRF_RADIO-&amp;gt;TASKS_RXEN = 1;
  while (NRF_RADIO-&amp;gt;EVENTS_READY == 0);
  NRF_RADIO-&amp;gt;EVENTS_END = 0;
  NRF_RADIO-&amp;gt;TASKS_START = 1;
}

/**
 * Send raw data asynchronously.
 **/

void radio_send_custom(uint8_t *pBuffer, uint8_t channel) {

  /* No shortcuts on disable. */
  NRF_RADIO-&amp;gt;SHORTS = 0x0;
  /* Switch radio to TX. */
  radio_disable();

  for (size_t try_ = 0; try_ &amp;lt; MAX_RETRIES; try_++) 
  {
    /* Switch packet buffer to tx_buffer. */
    NRF_RADIO-&amp;gt;PACKETPTR = (uint32_t)pBuffer;
    NRF_RADIO-&amp;gt;INTENSET = (1 &amp;lt;&amp;lt; RADIO_INTENSET_END_Pos);
    NVIC_ClearPendingIRQ(RADIO_IRQn);
    NVIC_EnableIRQ(RADIO_IRQn);

    NRF_RADIO-&amp;gt;EVENTS_READY = 0;
    NRF_RADIO-&amp;gt;TASKS_RXEN = 1;
    Serial.write(&amp;quot;While 1 &amp;quot;);// NEEDS THIS DELAY
    while (!NRF_RADIO-&amp;gt;EVENTS_READY);

     NRF_RADIO-&amp;gt;EVENTS_CCAIDLE = 0;
    //*(uint32_t *)(0x40001144) = 0;
    NRF_RADIO-&amp;gt;TASKS_CCASTART = 1;
    //*(uint32_t *)(0x4000102c) = 1;

     Serial.write(&amp;quot;While 2 &amp;quot;); // Needs this delay
    // while(!(NRF_RADIO-&amp;gt;EVENTS_CCAIDLE || CCABUSY));
    while(!(NRF_RADIO-&amp;gt;EVENTS_CCAIDLE || NRF_RADIO-&amp;gt;EVENTS_CCABUSY));
    // wait for CCAIDLE or CCABUSY
    Serial.write(&amp;quot;While 3 &amp;quot;);
    while (!(*(uint32_t *)(0x40001144) || sig_ccabusy));//CCA_IDLE
    if (sig_ccabusy) 
    {
      Serial.println(&amp;quot;WARNING: retry for CCABUSY&amp;quot;);
      sig_ccabusy = false;
      delay(100);
      continue;
    }

    /* Transmit with max power. */
    NRF_RADIO-&amp;gt;TXPOWER =
        (RADIO_TXPOWER_TXPOWER_Pos4dBm &amp;lt;&amp;lt; RADIO_TXPOWER_TXPOWER_Pos);
    NRF_RADIO-&amp;gt;FREQUENCY = channel_to_freq(channel);

    // enable receiver
    NRF_RADIO-&amp;gt;EVENTS_READY = 0;
    NRF_RADIO-&amp;gt;TASKS_TXEN = 1;

     Serial.write(&amp;quot;While 4\r\n&amp;quot;);//needs this delay
    while (!NRF_RADIO-&amp;gt;EVENTS_READY);
    NRF_RADIO-&amp;gt;TASKS_START = 1;
    /* From now, radio will send data and notify the result to Radio_IRQHandler */
    return;
  }
  Serial.println(&amp;quot;ERROR: retry too many times&amp;quot;);
}

/**
 * Change radio from TX to RX, while keeping last configuration of
 *radio_send_custom or radio_set_sniff
 **/
void radio_tx_to_rx() {
  NRF_RADIO-&amp;gt;PACKETPTR = (uint32_t)(rx_buffer);
  NVIC_DisableIRQ(RADIO_IRQn);
  NRF_RADIO-&amp;gt;SHORTS = RADIO_SHORTS_READY_START_Msk;

  // NRF_RADIO-&amp;gt;INTENSET = 0;
  NVIC_ClearPendingIRQ(RADIO_IRQn);
  NVIC_EnableIRQ(RADIO_IRQn);

  NRF_RADIO-&amp;gt;EVENTS_DISABLED = 0;
  NRF_RADIO-&amp;gt;TASKS_DISABLE = 1;

  while (NRF_RADIO-&amp;gt;EVENTS_DISABLED == 0);

  NRF_RADIO-&amp;gt;EVENTS_READY = 0;
  NRF_RADIO-&amp;gt;TASKS_RXEN = 1;
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;//radio.h
/**
 * Radio module
 *
 * This module provides all the required functions to manage the nRF51822
 * transceiver.
 **/

#pragma once
#include &amp;lt;Arduino.h&amp;gt;

extern uint8_t *rx_buffer; /* Rx buffer used by RF to store packets. */

#define IRQ_PRIORITY_HIGHEST 0
#define IRQ_PRIORITY_HIGH 1
#define IRQ_PRIORITY_MEDIUM 2
#define IRQ_PRIORITY_LOW 3

#define RADIO_MODE_MODE_Ieee802154_250Kbit (15UL)


#define IEEE802154_FRAME_LEN_MAX (127U)   /**&amp;lt; maximum 802.15.4 frame length */
#define IEEE802154G_FRAME_LEN_MAX (2047U) /**&amp;lt; maximum 802.15.4g-2012 frame length */
#define IEEE802154_ACK_FRAME_LEN (5U)     /**&amp;lt; ACK frame length */
#define IEEE802154_FCS_LEN                  (2U)


#define RADIO_PCNF0_PLEN_32bitZero (2UL) /*!&amp;lt; 32-bit zero preamble - used for IEEE 802.15.4 */
#define RADIO_PCNF0_CRCINC_Include (1UL) /*!&amp;lt; LENGTH includes CRC */
#define RADIO_PCNF0_CRCINC_Pos (26UL) /*!&amp;lt; Position of CRCINC field. */
#define RADIO_CRCCNF_SKIPADDR_Ieee802154 (2UL) /*!&amp;lt; CRC calculation as per 802.15.4 standard. Starting at first byte after length field. */


#define RADIO_INTENSET_FRAMESTART_Pos (14UL) /*!&amp;lt; Position of FRAMESTART field. */
#define RADIO_INTENSET_FRAMESTART_Msk (0x1UL &amp;lt;&amp;lt; RADIO_INTENSET_FRAMESTART_Pos) /*!&amp;lt; Bit mask of FRAMESTART field. */
#define RADIO_INTENSET_CCAIDLE_Pos (17UL) /*!&amp;lt; Position of CCAIDLE field. */
#define RADIO_INTENSET_CCAIDLE_Msk (0x1UL &amp;lt;&amp;lt; RADIO_INTENSET_CCAIDLE_Pos) /*!&amp;lt; Bit mask of CCAIDLE field. */
#define RADIO_INTENSET_CCABUSY_Pos (18UL) /*!&amp;lt; Position of CCABUSY field. */
#define RADIO_INTENSET_CCABUSY_Msk (0x1UL &amp;lt;&amp;lt; RADIO_INTENSET_CCABUSY_Pos) /*!&amp;lt; Bit mask of CCABUSY field. */


#define ED_RSSISCALE        (4U)    /**&amp;lt; RSSI scale for internal HW value */
#define ED_RSSIOFFS         (-92)   /**&amp;lt; RSSI offset for internal HW value */

/**
 * @brief IEEE802.15.4 default value for CCA threshold (in dBm)
 */
#ifndef CONFIG_IEEE802154_CCA_THRESH_DEFAULT
#define CONFIG_IEEE802154_CCA_THRESH_DEFAULT       (-70)
#endif

/* Bits 15..8 : CCA energy busy threshold. Used in all the CCA modes except CarrierMode. */
#define RADIO_CCACTRL_CCAEDTHRES_Pos (8UL) /*!&amp;lt; Position of CCAEDTHRES field. */
#define RADIO_CCACTRL_CCAEDTHRES_Msk (0xFFUL &amp;lt;&amp;lt; RADIO_CCACTRL_CCAEDTHRES_Pos) /*!&amp;lt; Bit mask of CCAEDTHRES field. */

/**
 * @brief   Default start frame delimiter
 */
#define IEEE802154_SFD (0xa7)

// signal for CCABUSY
extern volatile uint8_t sig_ccabusy;
// max retries
#define MAX_RETRIES 5

// functions
uint8_t channel_to_freq(int channel);
void radio_disable(void);
void radio_set_sniff(int channel, uint32_t access_address);
void radio_send_custom(uint8_t *pBuffer, uint8_t channel);
void radio_tx_to_rx();&lt;/pre&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>