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_common_v1.c b/source/DW1000/v1/deca_instance_common_v1.c
index a2dab51..47f39ce 100755
--- a/source/DW1000/v1/deca_instance_common_v1.c
+++ b/source/DW1000/v1/deca_instance_common_v1.c
@@ -202,44 +202,16 @@ const tx_struct txSpectrumConfig[8] =
 //these are default antenna delays for EVB1000, these can be used if there is no calibration data in the DW1000,
 //or instead of the calibration data
 const uint16_t rfDelays[2] = {
-        (uint16_t) ((DWT_PRF_16M_RFDLY/ 2.0) * 1e-9 / DWT_TIME_UNITS),//PRF 16
-        (uint16_t) ((DWT_PRF_64M_RFDLY/ 2.0) * 1e-9 / DWT_TIME_UNITS)
+        (uint16_t) (((double)DWT_PRF_16M_RFDLY/ 2.0) * 1e-9 / DWT_TIME_UNITS),//PRF 16
+        (uint16_t) (((double)DWT_PRF_64M_RFDLY/ 2.0) * 1e-9 / DWT_TIME_UNITS)
 };
 
 //these are default TREK Tag/Anchor antenna delays
 const uint16_t rfDelaysTREK[2] = {
-        (uint16_t) ((514.83f/ 2.0) * 1e-9 / DWT_TIME_UNITS),//channel 2
-        (uint16_t) ((514.65f/ 2.0) * 1e-9 / DWT_TIME_UNITS) //channel 5
+        (uint16_t) (((double)514.83f/ 2.0) * 1e-9 / DWT_TIME_UNITS),//channel 2
+        (uint16_t) (((double)514.65f/ 2.0) * 1e-9 / DWT_TIME_UNITS) //channel 5
 };
 
-//int instance_starttxtest(int framePeriod)
-//{
-//    //define some test data for the tx buffer
-//    uint8 msg[127] = "The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the l";
-
-//    //NOTE: SPI frequency must be < 3MHz
-//    port_set_dw1000_slowrate();  //max SPI before PLLs configured is ~4M
-
-//    // the value here 0x1000 gives a period of 32.82 µs
-//    //this is setting 0x1000 as frame period (125MHz clock cycles) (time from Tx en - to next - Tx en)
-//    dwt_configcontinuousframemode(framePeriod);
-
-//    dwt_writetxdata(127, (uint8 *)  msg, 0) ;
-//    dwt_writetxfctrl(127, 0, 0);
-
-//    //to start the first frame - set TXSTRT
-//    dwt_starttx(DWT_START_TX_IMMEDIATE);
-
-//    //measure the power
-//    //Spectrum Analyser set:
-//    //FREQ to be channel default e.g. 3.9936 GHz for channel 2
-//    //SPAN to 1GHz
-//    //SWEEP TIME 1s
-//    //RBW and VBW 1MHz
-//    //measure channel power
-
-//    return DWT_SUCCESS ;
-//}
 
 // -------------------------------------------------------------------------------------------------------------------
 //      Data Definitions
@@ -251,11 +223,9 @@ static double inst_tdist[MAX_TAG_LIST_SIZE] ;
 static double inst_idist[MAX_ANCHOR_LIST_SIZE] ;
 static double inst_idistraw[MAX_ANCHOR_LIST_SIZE] ;
 
-// TODO: Instance_data declaration in .c. Not on .h (extern only)
 instance_data_t instance_data[NUM_INST] ;
 
-/* TODO: Custom data declarations */
-typedef struct
+typedef struct __attribute__((packed))
 {
     uint32_t      deviceID ;
     uint8_t       chan;               // added chan here - used in the reading of acc
@@ -282,7 +252,7 @@ uint64_t convertmicrosectodevicetimeu (double microsecu){
   uint64_t dt;
   long double dtime;
 
-  dtime = (microsecu / (double) DWT_TIME_UNITS) / 1e6 ;
+  dtime = (long double)((microsecu / (double) DWT_TIME_UNITS) / 1e6);
 
   dt =  (uint64_t) (dtime) ;
 
@@ -302,7 +272,7 @@ int reportTOF(int idx, uint32_t tofx){
   double distance ;
   double distance_to_correct;
   double tof ;
-  int32_t tofi ;
+  int64_t tofi ;
 
   // check for negative results and accept them making them proper negative integers
   tofi = (int32_t) tofx ; // make it signed
@@ -311,7 +281,7 @@ int reportTOF(int idx, uint32_t tofx){
   }
 
   // convert to seconds (as floating point)
-  tof = convertdevicetimetosec(tofi) ;          //this is divided by 4 to get single time of flight
+  tof = convertdevicetimetosec((int32_t)tofi) ;          //this is divided by 4 to get single time of flight
   inst_idistraw[idx] = distance = tof * SPEED_OF_LIGHT;
 
 #if (CORRECT_RANGE_BIAS == 1)
@@ -376,11 +346,11 @@ void instancecleardisttableall(void){
 // Set this instance role as the Tag, Anchor or Listener
 void instancesetrole(int inst_mode){
   // assume instance 0, for this
-  instance_data[0].mode =  inst_mode;                   // set the role
+  instance_data[0].mode =  (INST_MODE)inst_mode;                   // set the role
 }
 
 int instancegetrole(void){
-  return instance_data[0].mode;
+  return (int)instance_data[0].mode;
 }
 
 int instancenewrange(void){
@@ -397,7 +367,7 @@ int instancenewrangetagadd(void){
   return instance_data[0].newRangeTagAddress;
 }
 
-int instancenewrangetim(void){
+uint32_t instancenewrangetim(void){
   return instance_data[0].newRangeTime;
 }
 
@@ -447,8 +417,7 @@ int instance_init(DW1000Driver* drv){
   //dwt_softreset();
 
   //this initialises DW1000 and uses specified configurations from OTP/ROM
-  result = dwt_initialise(DWT_LOADUCODE, drv) ;    // TODO
-//  result = dwt_initialise(DWT_LOADUCODE) ;
+  result = dwt_initialise(DWT_LOADUCODE, drv);
 
   //this is platform dependent - only program if DW EVK/EVB
   dwt_setleds(3) ; //configure the GPIOs which control the leds on EVBs
@@ -768,44 +737,25 @@ void instance_txcallback(const dwt_cb_data_t *txd){
 
   dw_event.uTimeStamp = portGetTickCnt();
 
-//  chprintf((BaseSequentialStream *)&SD2, " TX callback! \r\n");
-
-//  if(txevent == DWT_SIG_TX_DONE) {
-    //uint32_t txtimestamp = 0;
-
-    //NOTE - we can only get TX good (done) while here
-    //dwt_readtxtimestamp((uint8*) &instance_data[instance].txu.txTimeStamp);
-
-    dwt_readtxtimestamp(txTimeStamp) ;
-    dw_event.timeStamp32l = (uint32_t)txTimeStamp[0] + ((uint32_t)txTimeStamp[1] << 8) + ((uint32_t)txTimeStamp[2] << 16) + ((uint32_t)txTimeStamp[3] << 24);
-    dw_event.timeStamp = txTimeStamp[4];
-    dw_event.timeStamp <<= 32;
-    dw_event.timeStamp += dw_event.timeStamp32l;
-    dw_event.timeStamp32h = ((uint32_t)txTimeStamp[4] << 24) + (dw_event.timeStamp32l >> 8);
+  dwt_readtxtimestamp(txTimeStamp) ;
+  dw_event.timeStamp32l = (uint32_t)txTimeStamp[0] + ((uint32_t)txTimeStamp[1] << 8) + ((uint32_t)txTimeStamp[2] << 16) + ((uint32_t)txTimeStamp[3] << 24);
+  dw_event.timeStamp = txTimeStamp[4];
+  dw_event.timeStamp <<= 32;
+  dw_event.timeStamp += dw_event.timeStamp32l;
+  dw_event.timeStamp32h = ((uint32_t)txTimeStamp[4] << 24) + (dw_event.timeStamp32l >> 8);
 
-    instance_data[instance].stopTimer = 0;
+  instance_data[instance].stopTimer = 0;
 
-    dw_event.rxLength = instance_data[instance].psduLength;
-    dw_event.type =  0;
-    dw_event.type_pend =  0;
-    dw_event.type_save = DWT_SIG_TX_DONE;
+  dw_event.rxLength = instance_data[instance].psduLength;
+  dw_event.type =  0;
+  dw_event.type_pend =  0;
+  dw_event.type_save = DWT_SIG_TX_DONE;
 
-    memcpy((uint8_t *)&dw_event.msgu.frame[0], (uint8_t *)&instance_data[instance].msg_f, instance_data[instance].psduLength);
+  memcpy((uint8_t *)&dw_event.msgu.frame[0], (uint8_t *)&instance_data[instance].msg_f, instance_data[instance].psduLength);
 
-    instance_putevent(dw_event, DWT_SIG_TX_DONE);
+  instance_putevent(dw_event, DWT_SIG_TX_DONE);
 
-    instance_data[instance].txMsgCount++;
-//  }
-//  else if(txevent == DWT_SIG_TX_AA_DONE)  {
-//    //auto ACK confirmation
-//    dw_event.rxLength = 0;
-//    dw_event.type =  0;
-//    dw_event.type_save = DWT_SIG_TX_AA_DONE;
-//
-//    instance_putevent(dw_event, DWT_SIG_TX_AA_DONE);
-//
-//    //printf("TX AA time %f ecount %d\n",convertdevicetimetosecu(instance_data[instance].txu.txTimeStamp), instance_data[instance].dweventCnt);
-//  }
+  instance_data[instance].txMsgCount++;
 
   instance_data[instance].monitor = 0;
 }
@@ -834,7 +784,7 @@ uint8_t tagrxreenable(uint16_t sourceAddress){
   case 2:
   default:
     if(instance_data[instance].responseTO > 0) {  //can get here as result of error frame so need to check
-      dwt_setrxtimeout((uint16_t)instance_data[instance].fwtoTime_sy * instance_data[instance].responseTO); //reconfigure the timeout
+      dwt_setrxtimeout((uint16_t)(instance_data[instance].fwtoTime_sy * instance_data[instance].responseTO)); //reconfigure the timeout
       dwt_rxenable(DWT_START_RX_IMMEDIATE) ;
       type_pend = DWT_SIG_RX_PENDING ;
     }
@@ -1056,7 +1006,7 @@ void ancprepareresponse2(uint16_t sourceAddress, uint8_t srcAddr_index, uint8_t
   instance_data[instance].msg_f.seqNum = instance_data[instance].frameSN++;
 
   //set the delayed rx on time (the final message will be sent after this delay)
-  dwt_setrxaftertxdelay(instance_data[instance].ancRespRxDelay);  //units are 1.0256us - wait for wait4respTIM before RX on (delay RX)
+  dwt_setrxaftertxdelay((uint32_t)instance_data[instance].ancRespRxDelay);  //units are 1.0256us - wait for wait4respTIM before RX on (delay RX)
 
   instance_data[instance].tagSleepCorrection = 0;
   instance_data[instance].msg_f.messageData[RES_TAG_SLP0] = 0 ;
@@ -1093,7 +1043,7 @@ void ancprepareresponse(uint16_t sourceAddress, uint8_t srcAddr_index, uint8_t f
 
   //we have our range - update the own mask entry...
   if(instance_data[instance].tof[tof_idx] != INVALID_TOF) { //check the last ToF entry is valid and copy into the current array
-    instance_data[instance].rxResponseMask = (0x1 << instance_data[instance].shortAdd_idx);
+    instance_data[instance].rxResponseMask = (uint8_t)(0x1 << instance_data[instance].shortAdd_idx);
     instance_data[instance].tofArray[instance_data[instance].shortAdd_idx] = instance_data[instance].tof[tof_idx];
   }
   else	{  //reset response mask
@@ -1101,7 +1051,7 @@ void ancprepareresponse(uint16_t sourceAddress, uint8_t srcAddr_index, uint8_t f
     instance_data[instance].rxResponseMask = 0;	//reset the mask of received responses when rx poll
   }
   //set the delayed rx on time (the final message will be sent after this delay)
-  dwt_setrxaftertxdelay(instance_data[instance].ancRespRxDelay);  //units are 1.0256us - wait for wait4respTIM before RX on (delay RX)
+  dwt_setrxaftertxdelay((uint32_t)instance_data[instance].ancRespRxDelay);  //units are 1.0256us - wait for wait4respTIM before RX on (delay RX)
 
   //if this is gateway anchor - calculate the slot period correction
   if(instance_data[instance].gatewayAnchor)  {
@@ -1187,7 +1137,7 @@ void instance_rxcallback(const dwt_cb_data_t *rxd){
 
   //if Listener then just report the received frame to the instance (application)
   if(rxd_event == DWT_SIG_RX_OKAY) { //Process good/known frame types
-    uint16_t sourceAddress = (((uint16_t)dw_event.msgu.frame[srcAddr_index+1]) << 8) + dw_event.msgu.frame[srcAddr_index];
+    uint16_t sourceAddress = (uint16_t)(((uint16_t)dw_event.msgu.frame[srcAddr_index+1]) << 8) + (uint16_t)(dw_event.msgu.frame[srcAddr_index]);
 
     if(instance_data[instance].mode != LISTENER)      {
       if(instance_data[instance].mode == TAG) //if tag got a good frame - this is probably a response, but could also be some other non-ranging frame
@@ -1251,7 +1201,7 @@ void instance_rxcallback(const dwt_cb_data_t *rxd){
           instance_data[instance].rxMsgCount++;
           return;
         }
-        instance_data[instance].rxRespsIdx = (int8_t) ((dw_event.msgu.frame[POLL_RNUM+fcode_index] & 0xf)
+        instance_data[instance].rxRespsIdx = (uint8_t) ((dw_event.msgu.frame[POLL_RNUM+fcode_index] & 0xf)
             + ((sourceAddress&0x7) << 4));
         instance_data[instance].rxResps[instance_data[instance].rxRespsIdx] = 0;
 
@@ -1352,7 +1302,7 @@ void instance_rxcallback(const dwt_cb_data_t *rxd){
           instance_data[instance].rxMsgCount++;
           return;
         }
-      break;  // TODO: fallthourgh for anchor
+      break;
 //        __attribute__ ((fallthrough));
 
         //if anchor fall into case below and process the frame
@@ -1376,7 +1326,7 @@ void instance_rxcallback(const dwt_cb_data_t *rxd){
 }
 
 
-/*TODO: Callback funtion for RX timeout (available from newer driver version) */
+/*! Callback funtion for RX timeout (available from newer driver version) */
 #pragma GCC optimize ("O3")
 void instance_rxtimeoutcallback(const dwt_cb_data_t *rxd){
 
@@ -1410,7 +1360,7 @@ void instance_rxtimeoutcallback(const dwt_cb_data_t *rxd){
 }
 
 
-/*TODO: Callback funtion for RX error (available from newer driver version) */
+/*! Callback funtion for RX error (available from newer driver version) */
 #pragma GCC optimize ("O3")
 void instance_rxerrorcallback(const dwt_cb_data_t *rxd) {
 
@@ -1503,7 +1453,7 @@ event_data_t* instance_getevent(int x){
   dw_event_g.type = instance_data[instance].dwevent[indexOut].type ;
 
 
-  instance_data[instance].dwevent[indexOut].gotit = x;
+  instance_data[instance].dwevent[indexOut].gotit = (uint8_t)x;
 
   //instance_data[instance].dwevent[indexOut].eventtimeclr = portGetTickCnt();
 
@@ -1555,8 +1505,6 @@ int instance_run(void){
     message = 0;
   }
 
-
-
   if(done == INST_DONE_WAIT_FOR_NEXT_EVENT_TO) { //we are in RX and need to timeout (Tag needs to send another poll if no Rx frame)
     if(instance_data[instance].mode == TAG) { //Tag (is either in RX or sleeping)
       int32_t nextPeriod ;
@@ -1664,7 +1612,6 @@ void instanceconfigtxpower(uint32_t txpower){
   instance_data[0].txPower = txpower ;
 
   instance_data[0].txPowerChanged = 1;
-
 }
 
 void instancesettxpower(void){
@@ -1709,7 +1656,7 @@ uint8_t instancevalidranges(void){
 #endif
 
 
-/*! TODO: the following Functions are added for user application (previously on the API )*/
+/*! The following Functions are added for user application (previously on the API )*/
 
 /*! ------------------------------------------------------------------------------------------------------------------
  * @fn dwt_getotptxpower()
