Bare metal radio transmit and send on nrf5340

Dear forum,

I am testing out the radio on the nrf5340 using two evaluation board connected to a SES setup, but the receiving board only gets garbage and the checksum is never passed.

Not sure if the issue is on the sender or reciever side.

The code is borrowed from NordicSnippets/examples at master · andenore/NordicSnippets and modified to run on the 5340. I strongly suspect I did something wrong in my translation.

1. Set up a project for the network core (new project _> target Network Core)

2. Change NRF_CLOCK to NRF_CLOCK_NS and NRF_RADIO to NRF_RADIO_NS everywhere

3. Compile and run transmitter example on one board and reciever on another.

Here is the code for the receiving board

void radio_rx(void) {

  // Packet receive buffer
  volatile uint8_t packet[16];
  
  // Start HFCLK from crystal oscillator. The radio needs crystal to function correctly.
  NRF_CLOCK_NS->TASKS_HFCLKSTART = 1;
  while (NRF_CLOCK_NS->EVENTS_HFCLKSTARTED == 0);
  
  // Configure radio with 2Mbit Nordic proprietary mode
  NRF_RADIO_NS->MODE = RADIO_MODE_MODE_Nrf_2Mbit << RADIO_MODE_MODE_Pos;
  
  // Configure packet with no S0,S1 or Length fields and 8-bit preamble.
  NRF_RADIO_NS->PCNF0 = (0 << RADIO_PCNF0_LFLEN_Pos) |
                     (0 << RADIO_PCNF0_S0LEN_Pos) |
                     (0 << RADIO_PCNF0_S1LEN_Pos) | 
                     (RADIO_PCNF0_S1INCL_Automatic << RADIO_PCNF0_S1INCL_Pos) |
                     (RADIO_PCNF0_PLEN_8bit << RADIO_PCNF0_PLEN_Pos);
  
  // Configure static payload length of 16 bytes. 3 bytes address, little endian with whitening enabled.
  NRF_RADIO_NS->PCNF1 =  (16 << RADIO_PCNF1_MAXLEN_Pos) |
                      (16 << RADIO_PCNF1_STATLEN_Pos) |
                      (2  << RADIO_PCNF1_BALEN_Pos) | 
                      (RADIO_PCNF1_ENDIAN_Little << RADIO_PCNF1_ENDIAN_Pos) |
                      (RADIO_PCNF1_WHITEEN_Enabled << RADIO_PCNF1_WHITEEN_Pos);
  
  // initialize whitening value
  NRF_RADIO_NS->DATAWHITEIV = 0x55;
  
  // Configure address Prefix0 + Base0
  NRF_RADIO_NS->BASE0   = 0x0000BABE;
  NRF_RADIO_NS->PREFIX0 = 0x41 << RADIO_PREFIX0_AP0_Pos;
  
  // Use logical address 0 (BASE0 + PREFIX0 byte 0)
  NRF_RADIO_NS->RXADDRESSES = RADIO_RXADDRESSES_ADDR0_Enabled << RADIO_RXADDRESSES_ADDR0_Pos;
  
  // Initialize CRC (two bytes)
  NRF_RADIO_NS->CRCCNF = (RADIO_CRCCNF_LEN_Two << RADIO_CRCCNF_LEN_Pos) |
                      (RADIO_CRCCNF_SKIPADDR_Skip << RADIO_CRCCNF_SKIPADDR_Pos);
  NRF_RADIO_NS->CRCPOLY = 0x0000AAAA;
  NRF_RADIO_NS->CRCINIT = 0x12345678;
  
  // Enable fast rampup, new in nRF52
  NRF_RADIO_NS->MODECNF0 = (RADIO_MODECNF0_DTX_B0 << RADIO_MODECNF0_DTX_Pos) |
                        (RADIO_MODECNF0_RU_Fast << RADIO_MODECNF0_RU_Pos);
  
  // receiving packets at 2400MHz
  NRF_RADIO_NS->FREQUENCY = 0 << RADIO_FREQUENCY_FREQUENCY_Pos;
  
  // Configure address of the packet and logic address to use
  NRF_RADIO_NS->PACKETPTR = (uint32_t)&packet[0];
  
  // Configure shortcuts to start as soon as READY event is received, and disable radio as soon as packet is received.
  NRF_RADIO_NS->SHORTS = (RADIO_SHORTS_READY_START_Enabled << RADIO_SHORTS_READY_START_Pos) |
                      (RADIO_SHORTS_END_DISABLE_Enabled << RADIO_SHORTS_END_DISABLE_Pos);
  
  // Continually receive packet
  while (1)
  {
    NRF_RADIO_NS->TASKS_RXEN = 1;
    while (NRF_RADIO_NS->EVENTS_DISABLED == 0);
    NRF_RADIO_NS->EVENTS_DISABLED = 0;

    
    if (NRF_RADIO_NS->EVENTS_CRCOK)
    {

    printf("Message: ");
    printf("%.*s", 16, packet);
    printf(" \n");

      __NOP();
    }
  }
}

and for transmission

void radio_tx(void)
{
  // Packet to send
  uint8_t packet[16] = "demopacket";
  
  // Start HFCLK from crystal oscillator. The radio needs crystal to function correctly.
  NRF_CLOCK_NS->TASKS_HFCLKSTART = 1;
  while (NRF_CLOCK_NS->EVENTS_HFCLKSTARTED == 0);
  
  // Configure radio with 2Mbit Nordic proprietary mode
  NRF_RADIO_NS->MODE = RADIO_MODE_MODE_Nrf_2Mbit << RADIO_MODE_MODE_Pos;
  
  // Configure packet with no S0,S1 or Length fields and 8-bit preamble.
  NRF_RADIO_NS->PCNF0 = (0 << RADIO_PCNF0_LFLEN_Pos) |
                     (0 << RADIO_PCNF0_S0LEN_Pos) |
                     (0 << RADIO_PCNF0_S1LEN_Pos) | 
                     (RADIO_PCNF0_S1INCL_Automatic << RADIO_PCNF0_S1INCL_Pos) |
                     (RADIO_PCNF0_PLEN_8bit << RADIO_PCNF0_PLEN_Pos);
  
  // Configure static payload length of 16 bytes. 3 bytes address, little endian with whitening enabled.
  NRF_RADIO_NS->PCNF1 =  (16 << RADIO_PCNF1_MAXLEN_Pos) |
                      (16 << RADIO_PCNF1_STATLEN_Pos) |
                      (2  << RADIO_PCNF1_BALEN_Pos) | 
                      (RADIO_PCNF1_ENDIAN_Little << RADIO_PCNF1_ENDIAN_Pos) |
                      (RADIO_PCNF1_WHITEEN_Enabled << RADIO_PCNF1_WHITEEN_Pos);
  
  // initialize whitening value
  NRF_RADIO_NS->DATAWHITEIV = 0x55;
  
  // Configure address Prefix0 + Base0
  NRF_RADIO_NS->BASE0   = 0x0000BABE;
  NRF_RADIO_NS->PREFIX0 = 0x41 << RADIO_PREFIX0_AP0_Pos;
  
  // Use logical address 0 (BASE0 + PREFIX0 byte 0)
  NRF_RADIO_NS->TXADDRESS = 0 << RADIO_TXADDRESS_TXADDRESS_Pos;
  
  // Initialize CRC (two bytes)
  NRF_RADIO_NS->CRCCNF = (RADIO_CRCCNF_LEN_Two << RADIO_CRCCNF_LEN_Pos) |
                      (RADIO_CRCCNF_SKIPADDR_Skip << RADIO_CRCCNF_SKIPADDR_Pos);
  NRF_RADIO_NS->CRCPOLY = 0x0000AAAA;
  NRF_RADIO_NS->CRCINIT = 0x12345678;
  
  // Enable fast rampup, new in nRF52
  NRF_RADIO_NS->MODECNF0 = (RADIO_MODECNF0_DTX_B0 << RADIO_MODECNF0_DTX_Pos) |
                        (RADIO_MODECNF0_RU_Fast << RADIO_MODECNF0_RU_Pos);
                        
  // 0dBm output power, sending packets at 2400MHz
  NRF_RADIO_NS->TXPOWER = RADIO_TXPOWER_TXPOWER_0dBm << RADIO_TXPOWER_TXPOWER_Pos;
  NRF_RADIO_NS->FREQUENCY = 0 << RADIO_FREQUENCY_FREQUENCY_Pos;
  
  // Configure address of the packet and logic address to use
  NRF_RADIO_NS->PACKETPTR = (uint32_t)&packet[0];
  
  // Configure shortcuts to start as soon as READY event is received, and disable radio as soon as packet is sent.
  NRF_RADIO_NS->SHORTS = (RADIO_SHORTS_READY_START_Enabled << RADIO_SHORTS_READY_START_Pos) |
                      (RADIO_SHORTS_END_DISABLE_Enabled << RADIO_SHORTS_END_DISABLE_Pos);
  
  // Continuously send the same packet
  while (1)
  {
    NRF_RADIO_NS->TASKS_TXEN = 1;
    while (NRF_RADIO_NS->EVENTS_DISABLED == 0);
    NRF_RADIO_NS->EVENTS_DISABLED = 0;
  }
}

Any help appreciated

Related