problem in adding time sync features in ESB

Dear all,

[nrf sdk version 17.02 ; Broad:nrf52840DK]

I am now combing the time sync code into ESB protocol.In the original time sync source code,time slot API is used in order to let esb work cocurrently with BLE protocol.

But in my case,I remove that part.I used esb_ptx to send data packet and using an app timer to trigger program sending sync packet each 30ms.

The method for taking timestamp to sync is one main difference  from the source code shown below.

esb_ptx modification

typedef struct
{
    uint8_t message_id;    //recogonize sync_pkt or urllc_payload
   
} urllc_header;
//sync packet format
typedef struct
{
    urllc_header header;
    int32_t  timer_val;
    uint32_t counter_val;
   
} sync_pkt_t;
//data packet format
typedef struct
{
    urllc_header header;
    int32_t seq_num;
    uint32_t time_stamp;                            //time stamp to evaluate latency
    uint8_t data[URLLC_PAYLOAD_LENGTH];

} urllc_payload;

void take_sync_timer_val(sync_pkt_t* p_pkt)
{
    uint32_t sync_timer_val;
    uint32_t count_timer_val;
    uint32_t  peer_count;
    timers_capture(&sync_timer_val, &count_timer_val, &peer_count);
    p_pkt->counter_val = count_timer_val;
    p_pkt->timer_val = sync_timer_val;
}
// timer handler for sending sync packet periodically
static void repeated_timer_handler(void * p_context)
{

    tx_payload.length = sizeof(sync_pkt_t);               //set packet length
    tx_payload.noack = true;                              //sync packet is not required ack
    sync_pkt_t *p_pkt;
    p_pkt = (sync_pkt_t*)malloc(sizeof(sync_pkt_t));
    p_pkt->header.message_id = SYNC_PKT;                  //rx used meassage id to tell whethere the packet is for sync or data transmit
    take_sync_timer_val(p_pkt); 
    memcpy(tx_payload.data,p_pkt,sizeof(sync_pkt_t));
    if (nrf_esb_write_payload(&tx_payload) == NRF_SUCCESS)
    {
         NRF_LOG_INFO("Sending sync packet succeed");
    }        
    else
    {
         NRF_LOG_WARNING("Sending sync failed");
    }
    free(p_pkt);
}

In esb_prx,Rx will receive two types of packet,one is data packet with URLLC_DATA_PKT(0) as meassage_id,the other is sync packet with SYNC_PKT(1) as meassage

id.In RADIO_IRQHandler(),Rx first see the meassage_id,if it equals to SYNC_PKT,then the program follow the compensate procedure as the time sync code,I only modified the

Tx_CHAIN_DELAY to 2800.If Rx received the packet with URLLC_DATA_PKT as meassage_id,then the program follows the precedure in esb_rx.

esb_prx modification

void RADIO_IRQHandler() {
  
    if (NRF_RADIO->EVENTS_READY && (NRF_RADIO->INTENSET & RADIO_INTENSET_READY_Msk)) {
      NRF_RADIO->EVENTS_READY = 0;
       //DEBUG_PIN_SET(DEBUGPIN1);
    }

    if (NRF_RADIO->EVENTS_END && (NRF_RADIO->INTENSET & RADIO_INTENSET_END_Msk)) {
       NRF_RADIO->EVENTS_END = 0;
       //DEBUG_PIN_SET(DEBUGPIN2);
      // Call the correct on_radio_end function, depending on the current protocol state
      if (on_radio_end) {
        on_radio_end();
      }
    }

    if (NRF_RADIO->EVENTS_DISABLED && (NRF_RADIO->INTENSET & RADIO_INTENSET_DISABLED_Msk)) {

      NRF_RADIO->EVENTS_DISABLED = 0;
      uint8_t message_id;
      message_id = m_rx_payload_buffer[2];
      switch (message_id){
      case SYNC_PKT:
            {
            
              bool adjustment_procedure_started;
              sync_pkt_t *p_pkt;
              p_pkt = (sync_pkt_t*)&m_rx_payload_buffer[2];
              adjustment_procedure_started = sync_timer_offset_compensate(p_pkt);
              /*if (adjustment_procedure_started) {
                p_pkt = tx_buf_get();
              }*/
              clear_events_restart_rx();
            }
            break;

      case URLLC_DATA_PKT:
            if (on_radio_disabled) 
            {
              on_radio_disabled();
            }
            
            break;

      }

    }

  }

osciliscope results
the results looks fine at the begining,the fine states probably will remain for two minutes and after that,the wave started to glide slowly(shown in the below vedio),it seems RX didn't receive sync packet anymore.
by the way,I think there are bugs in my radio_irqhandler() in esb_prx,could anyone help me please,thanks a lot!!
 
Best Regards,
Hao.
 
Parents
  • Hello,

    I have not worked with this unofficial example before (although I have seen it referenced). Therefore, it is difficult to say exactly what is wrong only by looking at a few snippets. Did you test the unmodified version before? Searching through thes from the github repository, which I assume you have used. I am not able to understand what changes you did, and why.

    E.g. the RADIO_IRQHandler() that you modified, was it the one in nrf_esb.c, or the one in time_sync.c? The reason I ask is that I don't see any particular similarities with it any of them, so that is why I am not sure what you are trying to do. 

    I assume that the example is sending the time sync messages regularly. Do you do that as well? And the drift between the devices, do they reset when the time sync messages are received or do they keep drifting?

    BR,

    Edvin

  • Hi,thanks to your quick reply

    1.the RADIO_IRQHandler() is in the nrf_esb.c,the original one doesn't have the time sync part,I add the compesation part which is sync_timer_offset_compensate(pkt) into it.In other words,I move part of time_sync.c code into nrf_esb.c.

    2.As mentioned above,esb_tx is sending sync packet regularly (every 30 ms),and the drift is reset when the time sync messages are received (at the beginning of the vedio),but the drift keep driftin for long and then become flat. 

    I will do more testing to figure it out,I just wonder is there any problems in RADIO_IRQHandler() because I do some modification and also I am not really familiar with the radio.

    Best Regards,

    Hao

  • HaoHuang said:
    I just wonder is there any problems in RADIO_IRQHandler()

    Not that I am aware of. The Radio IRQ Handler is widely used in almost all of our applications, also by our Bluetooth Stack.

    Did you test the unmodified version from the guide? The one with Bluetooth? It doesn't actually use bluetooth, it only supports running bluetooth next to it. 

    BR,

    Edvin

Reply
  • HaoHuang said:
    I just wonder is there any problems in RADIO_IRQHandler()

    Not that I am aware of. The Radio IRQ Handler is widely used in almost all of our applications, also by our Bluetooth Stack.

    Did you test the unmodified version from the guide? The one with Bluetooth? It doesn't actually use bluetooth, it only supports running bluetooth next to it. 

    BR,

    Edvin

Children
  • Hi,

    I already test the unmofidied one,and that works normally.I need some features in ESB,mean while I need the timers on tx,rx to be synchrnized,so I do the combination.

    I add the below snippet in RADIO_IRQHandler() of esb.c to doing sync procedure.I don't know if this is right.

    uint8_t message_id;
          message_id = m_rx_payload_buffer[2];
          switch (message_id){
          case SYNC_PKT:
                {
                
                  bool adjustment_procedure_started;
                  sync_pkt_t *p_pkt;
                  p_pkt = (sync_pkt_t*)&m_rx_payload_buffer[2];
                  adjustment_procedure_started = sync_timer_offset_compensate(p_pkt);
                  /*if (adjustment_procedure_started) {
                    p_pkt = tx_buf_get();
                  }*/
                  clear_events_restart_rx();
                }
                break;

Related