Editing single-sided examples into multi-sided?

Hello everyone,

I am actually trying to use dwm1001 to develop a system which contains multi tags and multi anchors. To achive that i tried to create a protocol with hand-shaking. Tags will broadcasting their IDs with some other informations. When an anchor gets that massage, it responds that massage with its own ID, the tag’s ID and some other informations. After this hand-shake will be complated. Both tag and anchor know each other’s ID and they start to communicate for ranging.

It is actually very simple if DWM supports this RF communication but the moment I have changed anything with the examples everything goes crazy. :smiley: I could not even get any data most of the time and when i did some of the information were different then i sent.

These are my codes: First one is the anchor. There are two tasks one of them for hand shaking and other one is for ranging.

void hand_shake_run(void)
{
	/* Activate reception immediately. */
  dwt_rxenable(DWT_START_RX_IMMEDIATE);

  /* Poll for reception of a frame or error/timeout. See NOTE 5 below. */
  while (!((status_reg = dwt_read32bitreg(SYS_STATUS_ID)) & (SYS_STATUS_RXFCG | SYS_STATUS_ALL_RX_TO | SYS_STATUS_ALL_RX_ERR)))
  {};
		
	if (status_reg & SYS_STATUS_RXFCG)
  {	
		uint32 frame_len;

		/* Clear good RX frame event in the DW1000 status register. */
		dwt_write32bitreg(SYS_STATUS_ID, SYS_STATUS_RXFCG);

		/* A frame has been received, read it into the local buffer. */
		frame_len = dwt_read32bitreg(RX_FINFO_ID) & RX_FINFO_RXFLEN_MASK;
		 
		if (frame_len == RX_HAND_SHAKE_LEN)
		{
			/* Read Rx Data of UWB */
			dwt_readrxdata(rx_buffer, RX_HAND_SHAKE_LEN, 0);
			if(memcmp(&rx_buffer[RX_HS_MSG_HS], hand_shake_code, HS_CODE_LEN) == 0)
			{
				/* Chech the ID of the incoming massage if it is alraedy hand shaked */
				if(check_ID(rx_buffer) == false)
				{
					int ret;
					
					/* Add new ID to hand shaked ID buffer */
					memcpy(&handShakedDevices[handShakedDeviceCount].id, rx_buffer, ID_LEN);

					/* Clear Tx Buffer */
					memset(tx_buffer, 0 ,MAX_BUFFER_LEN);
					
					/* Prepare  hand shake response package */
					memcpy(tx_buffer, &handShakedDevices[handShakedDeviceCount].id, ID_LEN);
					memcpy(&tx_buffer[TX_HS_MSG_ID], deviceID, ID_LEN);
					memcpy(&tx_buffer[TX_HS_MSG_HS], hand_shake_code, HS_CODE_LEN);
					
					/* Write package data to DW1000 and prepare transmission */
					dwt_write32bitreg(SYS_STATUS_ID, SYS_STATUS_TXFRS);
					dwt_writetxdata(TX_HS_LEN, tx_buffer, 0); 
					dwt_writetxfctrl(TX_HS_LEN, 0, 1); 
					
					/* Start transmission, indicating that a response is expected so that reception is enabled automatically after the frame is sent and the delay
					* set by dwt_setrxaftertxdelay() has elapsed. */
					ret = dwt_starttx(DWT_START_TX_DELAYED);
					
					/* If dwt_starttx() returns an error, abandon this ranging exchange and proceed to the next one. */
					if (ret == DWT_SUCCESS)
					{
						/* Poll DW1000 until TX frame sent event set. */
						while (!(dwt_read32bitreg(SYS_STATUS_ID) & SYS_STATUS_TXFRS))
						{};

						/* Clear TXFRS event. */
						dwt_write32bitreg(SYS_STATUS_ID, SYS_STATUS_TXFRS);
					}
					else
					{
						/* Reset RX to properly reinitialise LDE operation. */
						dwt_rxreset();
					}
					handShakedDeviceCount++;
					handShaked = true;
				}
			}
		}
	}
}
void anchor_main_run(void)
{			
	uint8 counter_poll;
	
	for(counter_poll = 0; counter_poll < handShakedDeviceCount; counter_poll++)
	{
		/* Activate reception immediately. */
		dwt_rxenable(DWT_START_RX_IMMEDIATE);
		
		status_reg = dwt_read32bitreg(SYS_STATUS_ID);
		/* Clear Tx Buffer */
		memset(tx_buffer, 0 ,MAX_BUFFER_LEN);
			
		/* Write frame data to poll buffer and prepare transmission.*/
		memcpy(tx_buffer, &handShakedDevices[counter_poll].id, ID_LEN);
		memcpy(&tx_buffer[TX_REG_MSG_ID], deviceID, ID_LEN);
		memcpy(&tx_buffer[TX_REG_MSG_STAT], &handShakedDevices[counter_poll].isWashNeeded, STAT_LEN);
					
		/* Read to Tx Status Register that Tx is ready to send */
		dwt_write32bitreg(SYS_STATUS_ID, SYS_STATUS_TXFRS);
			
		/* Send poll buffer to tx buffer */
		dwt_writetxdata(TX_HS_LEN, tx_buffer, 0); 
		dwt_writetxfctrl(TX_HS_LEN, 0, 1); 

		/* Start transmission, indicating that a response is expected so that reception is enabled automatically after the frame is sent and the delay
		* set by dwt_setrxaftertxdelay() has elapsed. */
		dwt_starttx(DWT_START_TX_IMMEDIATE | DWT_RESPONSE_EXPECTED);
	
		/* We assume that the transmission is achieved correctly, poll for reception of a frame or error/timeout. See NOTE 4 below. */
		while (!((status_reg = dwt_read32bitreg(SYS_STATUS_ID)) & (SYS_STATUS_RXFCG | SYS_STATUS_ALL_RX_TO | SYS_STATUS_ALL_RX_ERR)))
		{};
			
		if (status_reg & SYS_STATUS_RXFCG)
		{		
			uint32 frame_len;
				
			if(frame_len == RX_REG_LEN)
			{
				/* Read Rx Data of UWB */
				dwt_readrxdata(rx_buffer, RX_REG_LEN, 0);
				
				if(memcmp(rx_buffer, deviceID, ID_LEN) == 0)
				{
					if(check_ID(rx_buffer) == true)
					{
						uint32 poll_tx_ts, resp_rx_ts, poll_rx_ts, resp_tx_ts;
						int32 rtd_init, rtd_resp;
						float clockOffsetRatio ;
						uint8 targetID[ID_LEN + 1] = {0};
						
						memcpy(targetID, &rx_buffer[RX_REG_MSG_ID], ID_LEN);
						/* Retrieve poll transmission and response reception timestamps. See NOTE 5 below. */
						poll_tx_ts = dwt_readtxtimestamplo32();
						resp_rx_ts = dwt_readrxtimestamplo32();

						/* Read carrier integrator value and calculate clock offset ratio. See NOTE 7 below. */
						clockOffsetRatio = dwt_readcarrierintegrator() * (FREQ_OFFSET_MULTIPLIER * HERTZ_TO_PPM_MULTIPLIER_CHAN_5 / 1.0e6) ;

						/* Get timestamps embedded in response message. */
						resp_msg_get_ts(&rx_buffer[RX_REG_MSG_POLL], &poll_rx_ts);
						resp_msg_get_ts(&rx_buffer[RX_REG_MSG_RESP], &resp_tx_ts);

						/* Compute time of flight and distance, using clock offset ratio to correct for differing local and remote clock rates */
						rtd_init = resp_rx_ts - poll_tx_ts;
						rtd_resp = resp_tx_ts - poll_rx_ts;

						tof = ((rtd_init - rtd_resp * (1.0f - clockOffsetRatio)) / 2.0f) * DWT_TIME_UNITS; // Specifying 1.0f and 2.0f are floats to clear warning 
						distance = tof * SPEED_OF_LIGHT;

						printf("Distance : %f\r\n", distance);
						printf("ID : %s\r\n", targetID);
					}
				}
			}
		}
		else
		{
			/* Clear RX error/timeout events in the DW1000 status register. */
			dwt_write32bitreg(SYS_STATUS_ID, SYS_STATUS_ALL_RX_TO | SYS_STATUS_ALL_RX_ERR);

			/* Reset RX to properly reinitialise LDE operation. */
			dwt_rxreset();
		}
	}
}

this is tag

void hand_shake_run(void)
{
		/* Clear Tx Buffer */
	memset(tx_buffer, 0 ,MAX_BUFFER_LEN);
			
	/* Write frame data to poll buffer and prepare transmission.*/
	memcpy(tx_buffer, deviceID, ID_LEN);
	memcpy(&tx_buffer[TX_HS_MSG_HS], hand_shake_code, HS_CODE_LEN);
					
	/* Read to Tx Status Register that Tx is ready to send */
	dwt_write32bitreg(SYS_STATUS_ID, SYS_STATUS_TXFRS);
			
	/* Send poll buffer to tx buffer */
	dwt_writetxdata(TX_HS_LEN, tx_buffer, 0); 
	dwt_writetxfctrl(TX_HS_LEN, 0, 1); 

  /* Start transmission, indicating that a response is expected so that reception is enabled automatically after the frame is sent and the delay
  * set by dwt_setrxaftertxdelay() has elapsed. */
  dwt_starttx(DWT_START_TX_IMMEDIATE | DWT_RESPONSE_EXPECTED);
	
  /* We assume that the transmission is achieved correctly, poll for reception of a frame or error/timeout. See NOTE 4 below. */
  while (!((status_reg = dwt_read32bitreg(SYS_STATUS_ID)) & (SYS_STATUS_RXFCG | SYS_STATUS_ALL_RX_TO | SYS_STATUS_ALL_RX_ERR)))
  {};
  if (status_reg & SYS_STATUS_RXFCG)
  {	
    uint32 frame_len;
		
    /* Clear good RX frame event in the DW1000 status register. */
    dwt_write32bitreg(SYS_STATUS_ID, SYS_STATUS_RXFCG);

    /* A frame has been received, read it into the local buffer if it's length is equel the RX_BUFFER_LEN */
    frame_len = dwt_read32bitreg(RX_FINFO_ID) & RX_FINFO_RXFL_MASK_1023;
    if (frame_len == RX_HS_LEN)
    {		
			/* Read Rx data into the buffer */
      dwt_readrxdata(rx_buffer, RX_HS_LEN, 0);
			
			/* Check if the target ID is belongs to this device */
			if(memcmp(rx_buffer, deviceID, ID_LEN) == 0)
			{
				/* Check the sending device ID is not already one of the hand shaked device */
				if(check_ID(&rx_buffer[RX_REG_MSG_ID]) == false)
				{
					/* Check if the hand shake code is correct */
					if(memcmp(&rx_buffer[RX_HS_MSG_HS], hand_shake_code, HS_CODE_LEN) == 0)
					{
						/* Copy the sending device ID to hand shaked device ID buffer */
							memcpy(handShakedDevices[handShakedDeviceCount].id, &rx_buffer[RX_REG_MSG_ID], ID_LEN);
						  handShakedDeviceCount++;
							handShaked = true;
					}	
				}
			}	
		}
	}
}
int tag_main_run(void)
{		
  /* Activate reception immediately. */
  dwt_rxenable(DWT_START_RX_IMMEDIATE);

  /* Poll for reception of a frame or error/timeout. See NOTE 5 below. */
  while (!((status_reg = dwt_read32bitreg(SYS_STATUS_ID)) & (SYS_STATUS_RXFCG | SYS_STATUS_ALL_RX_TO | SYS_STATUS_ALL_RX_ERR)))
  {};

    #if 0	  // Include to determine the type of timeout if required.
    int temp = 0;
    // (frame wait timeout and preamble detect timeout)
    if(status_reg & SYS_STATUS_RXRFTO )
    temp =1;
    else if(status_reg & SYS_STATUS_RXPTO )
    temp =2;
    #endif

  if (status_reg & SYS_STATUS_RXFCG)
  {
    uint32 frame_len;
		if(frame_len == RX_REG_LEN)
		{
			uint32 resp_tx_time;
			int ret;
				
			/* Read Rx data into the buffer */
			dwt_readrxdata(rx_buffer, RX_REG_LEN, 0);
				
			/* Check if the target ID is belongs to this device */
			if(memcmp(rx_buffer, deviceID, ID_LEN) == 0)
			{
				/* Check the sending device ID is not already one of the hand shaked device */
				if(check_ID(&rx_buffer[RX_REG_MSG_ID]) == false)
				{				
					if((rx_buffer[RX_REG_MSG_STAT] == false) || (rx_buffer[RX_REG_MSG_STAT] == true))
					{
						/* Copy the new status to device status */
						memcpy(&isWashNeeded, &rx_buffer[RX_REG_MSG_STAT], STAT_LEN);
					}
				}
				/* Retrieve poll reception timestamp. */
				poll_rx_ts = get_rx_timestamp_u64();

				/* Compute final message transmission time. See NOTE 7 below. */
				resp_tx_time = (poll_rx_ts + (POLL_RX_TO_RESP_TX_DLY_UUS * UUS_TO_DWT_TIME)) >> 8;
				dwt_setdelayedtrxtime(resp_tx_time);

				/* Response TX timestamp is the transmission time we programmed plus the antenna delay. */
				resp_tx_ts = (((uint64)(resp_tx_time & 0xFFFFFFFEUL)) << 8) + TX_ANT_DLY;

				/* Write all timestamps in the final message. See NOTE 8 below. */
				resp_msg_set_ts(&tx_buffer[TX_REG_MSG_POLL], poll_rx_ts);
				resp_msg_set_ts(&tx_buffer[TX_REG_MSG_RESP], resp_tx_ts);

				/* Write and send the response message. */
				memcpy(tx_buffer, &rx_buffer[RX_REG_MSG_ID], ID_LEN);
				memcpy(&tx_buffer[RX_REG_MSG_ID], deviceID, ID_LEN);
				dwt_writetxdata(TX_REG_LEN, tx_buffer, 0); /* Zero offset in TX buffer. See Note 5 below.*/
				dwt_writetxfctrl(TX_REG_LEN, 0, 1); /* Zero offset in TX buffer, ranging. */
				ret = dwt_starttx(DWT_START_TX_IMMEDIATE);

				/* If dwt_starttx() returns an error, abandon this ranging exchange and proceed to the next one. */
				if (ret == DWT_SUCCESS)
				{
					/* Poll DW1000 until TX frame sent event set. See NOTE 5 below. */
					while (!(dwt_read32bitreg(SYS_STATUS_ID) & SYS_STATUS_TXFRS))
					{};

					/* Clear TXFRS event. */
					dwt_write32bitreg(SYS_STATUS_ID, SYS_STATUS_TXFRS);
				}
				else
				{
					/* Reset RX to properly reinitialise LDE operation. */
					dwt_rxreset();
				}
			}
		}
  }
  else
  {
    /* Clear RX error events in the DW1000 status register. */
    dwt_write32bitreg(SYS_STATUS_ID, SYS_STATUS_ALL_RX_ERR);

    /* Reset RX to properly reinitialise LDE operation. */
    dwt_rxreset();
  }
	return(1);
}