commit 26dead12185b7721d47c45900bebd6dda6ff4ea7
Author: Cung Sang <csang@techfak.uni-bielefeld.de>
Date:   Tue Feb 12 11:20:39 2019 +0100

    DW1000 LLD updated

diff --git a/source/DW1000/v1/deca_instance_tag_anchor_v1.c b/source/DW1000/v1/deca_instance_tag_anchor_v1.c
index a2a415c..e6fb673 100755
--- a/source/DW1000/v1/deca_instance_tag_anchor_v1.c
+++ b/source/DW1000/v1/deca_instance_tag_anchor_v1.c
@@ -17,23 +17,10 @@
 
 #include <v1/deca_instance_v1.h>
 #include <string.h>
-#include<math.h>
+#include <math.h>
 #include "module.h"
 
 // -------------------------------------------------------------------------------------------------------------------
-//      Data Definitions
-// -------------------------------------------------------------------------------------------------------------------
-
-// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
-// NOTE: the maximum RX timeout is ~ 65ms
-// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
-
-
-// -------------------------------------------------------------------------------------------------------------------
-// Functions
-// -------------------------------------------------------------------------------------------------------------------
-
-// -------------------------------------------------------------------------------------------------------------------
 //
 // function to construct the message/frame header bytes
 //
@@ -61,7 +48,7 @@ int instancesenddlypacket(instance_data_t *inst, int delayedTx){
   }
 
   //begin delayed TX of frame
-  if (dwt_starttx(delayedTx | inst->wait4ack)){ // delayed start was too late
+  if (dwt_starttx((uint8_t)(delayedTx | inst->wait4ack))){ // delayed start was too late
     result = 1; //late/error
     inst->lateTX++;
   }
@@ -72,7 +59,6 @@ int instancesenddlypacket(instance_data_t *inst, int delayedTx){
   return result;                                              // state changes
 }
 
-
 int instance_calcranges(uint32_t *array, uint16_t size, int reportRange, uint8_t* mask){
   int i;
   int newRange = TOF_REPORT_NUL;
@@ -135,10 +121,6 @@ int testapprun(instance_data_t *inst, int message){
 
       sleep_mode = (DWT_LOADUCODE|DWT_PRESRV_SLEEP|DWT_CONFIG|DWT_TANDV);
 
-      /* TODO: No need anymore since the driver handle itself */
-//      if((dwt_getldotune() != 0)) //if we need to use LDO tune value from OTP kick it after sleep
-//        sleep_mode |= DWT_LOADOPSET;
-
       if(inst->configData.txPreambLength == DWT_PLEN_64)  //if using 64 length preamble then use the corresponding OPSet
         sleep_mode |= DWT_LOADOPSET;
 
@@ -255,7 +237,7 @@ int testapprun(instance_data_t *inst, int message){
         dwt_seteui(inst->eui64);
       }
 #else
-      Sleep(3); //to approximate match the time spent in the #if above  //TODO: 3ms default
+      Sleep(3); //to approximate match the time spent in the #if above
 #endif
 
       instancesetantennadelays(); //this will update the antenna delay if it has changed
@@ -426,7 +408,7 @@ int testapprun(instance_data_t *inst, int message){
           else {  //for anchor make the final half the delay ..... (this is ok, as A0 awaits 2 responses)
             tagCalculatedFinalTxTime =  (inst->txu.txTimeStamp + inst->pollTx2FinalTxDelayAnc) & MASK_TXDTS;
           }
-          inst->delayedReplyTime = tagCalculatedFinalTxTime >> 8; //high 32-bits
+          inst->delayedReplyTime = (uint32_t)(tagCalculatedFinalTxTime >> 8); //high 32-bits
           // Calculate Time Final message will be sent and write this field of Final message
           // Sending time will be delayedReplyTime, snapped to ~125MHz or ~250MHz boundary by
           // zeroing its low 9 bits, and then having the TX antenna delay added
@@ -451,9 +433,7 @@ int testapprun(instance_data_t *inst, int message){
         message = 0;
         //fall into the next case (turn on the RX)
       }
-
     }
-    // TODO: fall through to the next case statement in case of debugging
     break ; // end case TA_TX_WAIT_CONF
 
 
@@ -476,10 +456,9 @@ int testapprun(instance_data_t *inst, int message){
       // end case TA_RXE_WAIT, don't break, but fall through into the TA_RX_WAIT_DATA state to process it immediately.
       if(message == 0) break;
     }
-    // TODO: fall through to the next case statement in case of debugging
     break;
 
-    case TA_RX_WAIT_DATA :                                                                     // Wait RX data
+    case TA_RX_WAIT_DATA :   // Wait RX data
       //printf("TA_RX_WAIT_DATA %d", message) ;
       switch (message){
 
@@ -493,7 +472,7 @@ int testapprun(instance_data_t *inst, int message){
         //int srclen = 0;
         //int fctrladdr_len;
         uint8_t tof_idx  = 0;
-        uint8_t *messageData;
+        uint8_t *messageData = NULL;
 
         inst->stopTimer = 0; //clear the flag, as we have received a message
 
@@ -629,11 +608,6 @@ int testapprun(instance_data_t *inst, int message){
                 }
               }
             }
-            /*else
-                                {
-                                    //stay in RX wait for next frame...
-                                    inst->testAppState = TA_RXE_WAIT ;              // wait for next frame
-                                }*/
 
             if(fcode == RTLS_DEMO_MSG_ANCH_RESP) { //tag to anchor mode
               if(currentRangeNum == inst->rangeNum) { //these are the previous ranges...
@@ -692,7 +666,7 @@ int testapprun(instance_data_t *inst, int message){
             uint64_t tagFinalRxTime  = 0;
             uint64_t tagPollTxTime  = 0;
             uint64_t anchorRespRxTime  = 0;
-            uint64_t tof = INVALID_TOF;
+            int32_t tof = (int32_t)INVALID_TOF;
 
             double RaRbxDaDb = 0;
             double RbyDb = 0;
@@ -758,31 +732,31 @@ int testapprun(instance_data_t *inst, int message){
 
               RayDa = ((double)Ra + (double)Da);
 
-              tof = (int32_t) ( RaRbxDaDb/(RbyDb + RayDa) );
+              tof = (int32_t)(RaRbxDaDb/(RbyDb + RayDa));
             }
 
             //tag to anchor ranging
             if(RTLS_DEMO_MSG_TAG_FINAL == fcode) {
               inst->newRangeTagAddress = srcAddr[0] + ((uint16_t) srcAddr[1] << 8);
               //time-of-flight
-              inst->tof[inst->newRangeTagAddress & 0x7] = tof;
+              inst->tof[inst->newRangeTagAddress & 0x7] = (uint32_t)tof;
               //calculate all tag - anchor ranges... and report
               inst->newRange = instance_calcranges(&inst->tofArray[0], MAX_ANCHOR_LIST_SIZE, TOF_REPORT_T2A, &inst->rxResponseMask);
               inst->rxResponseMaskReport = inst->rxResponseMask; //copy the valid mask to report
               inst->rxResponseMask = 0;
               //we have our range - update the own mask entry...
-              if(tof != INVALID_TOF) { //check the last ToF entry is valid and copy into the current array
+              if(tof != (int32_t)INVALID_TOF) { //check the last ToF entry is valid and copy into the current array
                 setTagDist(srcAddr[0], inst->shortAdd_idx); //copy distance from this anchor to the tag into array
 
-                inst->rxResponseMask = (0x1 << inst->shortAdd_idx);
-                inst->tofArray[inst->shortAdd_idx] = tof;
+                inst->rxResponseMask = (uint8_t)(0x1 << inst->shortAdd_idx);
+                inst->tofArray[inst->shortAdd_idx] = (uint32_t)tof;
               }
               inst->newRangeTime = dw_event->uTimeStamp ;
             }
             else { //anchor to anchor ranging
               inst->newRangeTagAddress = srcAddr[0] + ((uint16_t) srcAddr[1] << 8);
               //time-of-flight
-              inst->tofAnc[tof_idx] = tof;
+              inst->tofAnc[tof_idx] = (uint32_t)tof;
             }
 
             //reset the response count
@@ -884,23 +858,23 @@ void instancesetreplydelay(int delayus) { //delay in us
   float msgdatalen = 0;
   float preamblelen = 0;
   int sfdlen = 0;
-  int x = 0;
+  float x = 0;
 
   //Set the RX timeouts based on the longest expected message - the Final message
   //Poll = 13, Response = 20, Final = 44 bytes
   //msgdatalen = TAG_FINAL_MSG_LEN + FRAME_CRTL_AND_ADDRESS_S + FRAME_CRC;
   msgdatalen = ANCH_RESPONSE_MSG_LEN + FRAME_CRTL_AND_ADDRESS_S + FRAME_CRC;
 
-  x = (int) ceil(msgdatalen*8/330.0f);
+  x = (float)ceil((double)(msgdatalen*8)/(double)330.0f);
 
   msgdatalen = msgdatalen*8 + x*48;
 
   //add some margin so we don't timeout too soon
   margin = 0; //(TAG_FINAL_MSG_LEN - TAG_POLL_MSG_LEN);
 
-  x = (int) ceil(margin*8/330.0f);
+  x = (float) ceil((double)(margin*8)/(double)330.0f);
 
-  margin = margin*8 + x*48;
+  margin = (int) (margin*8 + x*48);
 
   //assume PHR length is 172308ns for 110k and 21539ns for 850k/6.81M
   if(instance_data[instance].configData.dataRate == DWT_BR_110K) {
@@ -946,7 +920,7 @@ void instancesetreplydelay(int delayus) { //delay in us
     preamblelen = (sfdlen + preamblelen) * 1.01763f;
   }
 
-  respframe_sy = (16 + (int)((preamblelen + ((msgdatalen + margin)/1000.0))/ 1.0256)) ;
+  respframe_sy = (16 + (int)((double)((double)preamblelen + ((double)(msgdatalen + margin)/1000.0))/ 1.0256)) ;
 
   //this is the delay used for the delayed transmit (when sending the response, and final messages)
   instance_data[instance].pollTx2FinalTxDelay = convertmicrosectodevicetimeu (delayus);
@@ -959,7 +933,7 @@ void instancesetreplydelay(int delayus) { //delay in us
   //andhor 3 will have the delay set to 3 * fixedReplyDelayAnc and so on...
   //this delay depends on how quickly the tag can receive and process the message from previous anchor
   //(and also the frame length of course)
-  respframe = (int)(preamblelen + (msgdatalen/1000.0)); //length of response frame (micro seconds)
+  respframe = (int)((double)preamblelen + ((double)msgdatalen/1000.0)); //length of response frame (micro seconds)
   if(instance_data[instance].configData.dataRate == DWT_BR_110K) {
 
     //set the frame wait timeout time - total time the frame takes in symbols
@@ -967,7 +941,7 @@ void instancesetreplydelay(int delayus) { //delay in us
 
     instance_data[instance].fwtoTimeAnc_sy = respframe_sy; //add some margin so we don't timeout too soon
     instance_data[instance].fixedReplyDelayAnc = convertmicrosectodevicetimeu (respframe + RX_RESPONSE1_TURNAROUND_110K);
-    instance_data[instance].fixedReplyDelayAncP = (uint32_t) (((uint64_t) convertmicrosectodevicetimeu (preamblelen)) >> 8) + 16;
+    instance_data[instance].fixedReplyDelayAncP = (uint32_t) (((uint64_t) convertmicrosectodevicetimeu ((double)preamblelen)) >> 8) + 16;
 
     instance_data[instance].ancRespRxDelay = RX_RESPONSE1_TURNAROUND_110K ;
   }
@@ -978,11 +952,10 @@ void instancesetreplydelay(int delayus) { //delay in us
 
     instance_data[instance].fwtoTimeAnc_sy =  respframe_sy;
     instance_data[instance].fixedReplyDelayAnc = convertmicrosectodevicetimeu (respframe + RX_RESPONSE1_TURNAROUND_6M81);
-    instance_data[instance].fixedReplyDelayAncP = (uint32_t) (((uint64_t) convertmicrosectodevicetimeu (preamblelen)) >> 8) + 16;
+    instance_data[instance].fixedReplyDelayAncP = (uint32_t) (((uint64_t) convertmicrosectodevicetimeu ((double)preamblelen)) >> 8) + 16;
 
     instance_data[instance].ancRespRxDelay = RX_RESPONSE1_TURNAROUND_6M81 ;
   }
-
 }
 
 // -------------------------------------------------------------------------------------------------------------------
