Revision 26dead12 source/DW1000/v1/deca_instance_common_v1.c

View differences:

source/DW1000/v1/deca_instance_common_v1.c
202 202
//these are default antenna delays for EVB1000, these can be used if there is no calibration data in the DW1000,
203 203
//or instead of the calibration data
204 204
const uint16_t rfDelays[2] = {
205
        (uint16_t) ((DWT_PRF_16M_RFDLY/ 2.0) * 1e-9 / DWT_TIME_UNITS),//PRF 16
206
        (uint16_t) ((DWT_PRF_64M_RFDLY/ 2.0) * 1e-9 / DWT_TIME_UNITS)
205
        (uint16_t) (((double)DWT_PRF_16M_RFDLY/ 2.0) * 1e-9 / DWT_TIME_UNITS),//PRF 16
206
        (uint16_t) (((double)DWT_PRF_64M_RFDLY/ 2.0) * 1e-9 / DWT_TIME_UNITS)
207 207
};
208 208

  
209 209
//these are default TREK Tag/Anchor antenna delays
210 210
const uint16_t rfDelaysTREK[2] = {
211
        (uint16_t) ((514.83f/ 2.0) * 1e-9 / DWT_TIME_UNITS),//channel 2
212
        (uint16_t) ((514.65f/ 2.0) * 1e-9 / DWT_TIME_UNITS) //channel 5
211
        (uint16_t) (((double)514.83f/ 2.0) * 1e-9 / DWT_TIME_UNITS),//channel 2
212
        (uint16_t) (((double)514.65f/ 2.0) * 1e-9 / DWT_TIME_UNITS) //channel 5
213 213
};
214 214

  
215
//int instance_starttxtest(int framePeriod)
216
//{
217
//    //define some test data for the tx buffer
218
//    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";
219

  
220
//    //NOTE: SPI frequency must be < 3MHz
221
//    port_set_dw1000_slowrate();  //max SPI before PLLs configured is ~4M
222

  
223
//    // the value here 0x1000 gives a period of 32.82 ┬Ás
224
//    //this is setting 0x1000 as frame period (125MHz clock cycles) (time from Tx en - to next - Tx en)
225
//    dwt_configcontinuousframemode(framePeriod);
226

  
227
//    dwt_writetxdata(127, (uint8 *)  msg, 0) ;
228
//    dwt_writetxfctrl(127, 0, 0);
229

  
230
//    //to start the first frame - set TXSTRT
231
//    dwt_starttx(DWT_START_TX_IMMEDIATE);
232

  
233
//    //measure the power
234
//    //Spectrum Analyser set:
235
//    //FREQ to be channel default e.g. 3.9936 GHz for channel 2
236
//    //SPAN to 1GHz
237
//    //SWEEP TIME 1s
238
//    //RBW and VBW 1MHz
239
//    //measure channel power
240

  
241
//    return DWT_SUCCESS ;
242
//}
243 215

  
244 216
// -------------------------------------------------------------------------------------------------------------------
245 217
//      Data Definitions
......
251 223
static double inst_idist[MAX_ANCHOR_LIST_SIZE] ;
252 224
static double inst_idistraw[MAX_ANCHOR_LIST_SIZE] ;
253 225

  
254
// TODO: Instance_data declaration in .c. Not on .h (extern only)
255 226
instance_data_t instance_data[NUM_INST] ;
256 227

  
257
/* TODO: Custom data declarations */
258
typedef struct
228
typedef struct __attribute__((packed))
259 229
{
260 230
    uint32_t      deviceID ;
261 231
    uint8_t       chan;               // added chan here - used in the reading of acc
......
282 252
  uint64_t dt;
283 253
  long double dtime;
284 254

  
285
  dtime = (microsecu / (double) DWT_TIME_UNITS) / 1e6 ;
255
  dtime = (long double)((microsecu / (double) DWT_TIME_UNITS) / 1e6);
286 256

  
287 257
  dt =  (uint64_t) (dtime) ;
288 258

  
......
302 272
  double distance ;
303 273
  double distance_to_correct;
304 274
  double tof ;
305
  int32_t tofi ;
275
  int64_t tofi ;
306 276

  
307 277
  // check for negative results and accept them making them proper negative integers
308 278
  tofi = (int32_t) tofx ; // make it signed
......
311 281
  }
312 282

  
313 283
  // convert to seconds (as floating point)
314
  tof = convertdevicetimetosec(tofi) ;          //this is divided by 4 to get single time of flight
284
  tof = convertdevicetimetosec((int32_t)tofi) ;          //this is divided by 4 to get single time of flight
315 285
  inst_idistraw[idx] = distance = tof * SPEED_OF_LIGHT;
316 286

  
317 287
#if (CORRECT_RANGE_BIAS == 1)
......
376 346
// Set this instance role as the Tag, Anchor or Listener
377 347
void instancesetrole(int inst_mode){
378 348
  // assume instance 0, for this
379
  instance_data[0].mode =  inst_mode;                   // set the role
349
  instance_data[0].mode =  (INST_MODE)inst_mode;                   // set the role
380 350
}
381 351

  
382 352
int instancegetrole(void){
383
  return instance_data[0].mode;
353
  return (int)instance_data[0].mode;
384 354
}
385 355

  
386 356
int instancenewrange(void){
......
397 367
  return instance_data[0].newRangeTagAddress;
398 368
}
399 369

  
400
int instancenewrangetim(void){
370
uint32_t instancenewrangetim(void){
401 371
  return instance_data[0].newRangeTime;
402 372
}
403 373

  
......
447 417
  //dwt_softreset();
448 418

  
449 419
  //this initialises DW1000 and uses specified configurations from OTP/ROM
450
  result = dwt_initialise(DWT_LOADUCODE, drv) ;    // TODO
451
//  result = dwt_initialise(DWT_LOADUCODE) ;
420
  result = dwt_initialise(DWT_LOADUCODE, drv);
452 421

  
453 422
  //this is platform dependent - only program if DW EVK/EVB
454 423
  dwt_setleds(3) ; //configure the GPIOs which control the leds on EVBs
......
768 737

  
769 738
  dw_event.uTimeStamp = portGetTickCnt();
770 739

  
771
//  chprintf((BaseSequentialStream *)&SD2, " TX callback! \r\n");
772

  
773
//  if(txevent == DWT_SIG_TX_DONE) {
774
    //uint32_t txtimestamp = 0;
775

  
776
    //NOTE - we can only get TX good (done) while here
777
    //dwt_readtxtimestamp((uint8*) &instance_data[instance].txu.txTimeStamp);
778

  
779
    dwt_readtxtimestamp(txTimeStamp) ;
780
    dw_event.timeStamp32l = (uint32_t)txTimeStamp[0] + ((uint32_t)txTimeStamp[1] << 8) + ((uint32_t)txTimeStamp[2] << 16) + ((uint32_t)txTimeStamp[3] << 24);
781
    dw_event.timeStamp = txTimeStamp[4];
782
    dw_event.timeStamp <<= 32;
783
    dw_event.timeStamp += dw_event.timeStamp32l;
784
    dw_event.timeStamp32h = ((uint32_t)txTimeStamp[4] << 24) + (dw_event.timeStamp32l >> 8);
740
  dwt_readtxtimestamp(txTimeStamp) ;
741
  dw_event.timeStamp32l = (uint32_t)txTimeStamp[0] + ((uint32_t)txTimeStamp[1] << 8) + ((uint32_t)txTimeStamp[2] << 16) + ((uint32_t)txTimeStamp[3] << 24);
742
  dw_event.timeStamp = txTimeStamp[4];
743
  dw_event.timeStamp <<= 32;
744
  dw_event.timeStamp += dw_event.timeStamp32l;
745
  dw_event.timeStamp32h = ((uint32_t)txTimeStamp[4] << 24) + (dw_event.timeStamp32l >> 8);
785 746

  
786
    instance_data[instance].stopTimer = 0;
747
  instance_data[instance].stopTimer = 0;
787 748

  
788
    dw_event.rxLength = instance_data[instance].psduLength;
789
    dw_event.type =  0;
790
    dw_event.type_pend =  0;
791
    dw_event.type_save = DWT_SIG_TX_DONE;
749
  dw_event.rxLength = instance_data[instance].psduLength;
750
  dw_event.type =  0;
751
  dw_event.type_pend =  0;
752
  dw_event.type_save = DWT_SIG_TX_DONE;
792 753

  
793
    memcpy((uint8_t *)&dw_event.msgu.frame[0], (uint8_t *)&instance_data[instance].msg_f, instance_data[instance].psduLength);
754
  memcpy((uint8_t *)&dw_event.msgu.frame[0], (uint8_t *)&instance_data[instance].msg_f, instance_data[instance].psduLength);
794 755

  
795
    instance_putevent(dw_event, DWT_SIG_TX_DONE);
756
  instance_putevent(dw_event, DWT_SIG_TX_DONE);
796 757

  
797
    instance_data[instance].txMsgCount++;
798
//  }
799
//  else if(txevent == DWT_SIG_TX_AA_DONE)  {
800
//    //auto ACK confirmation
801
//    dw_event.rxLength = 0;
802
//    dw_event.type =  0;
803
//    dw_event.type_save = DWT_SIG_TX_AA_DONE;
804
//
805
//    instance_putevent(dw_event, DWT_SIG_TX_AA_DONE);
806
//
807
//    //printf("TX AA time %f ecount %d\n",convertdevicetimetosecu(instance_data[instance].txu.txTimeStamp), instance_data[instance].dweventCnt);
808
//  }
758
  instance_data[instance].txMsgCount++;
809 759

  
810 760
  instance_data[instance].monitor = 0;
811 761
}
......
834 784
  case 2:
835 785
  default:
836 786
    if(instance_data[instance].responseTO > 0) {  //can get here as result of error frame so need to check
837
      dwt_setrxtimeout((uint16_t)instance_data[instance].fwtoTime_sy * instance_data[instance].responseTO); //reconfigure the timeout
787
      dwt_setrxtimeout((uint16_t)(instance_data[instance].fwtoTime_sy * instance_data[instance].responseTO)); //reconfigure the timeout
838 788
      dwt_rxenable(DWT_START_RX_IMMEDIATE) ;
839 789
      type_pend = DWT_SIG_RX_PENDING ;
840 790
    }
......
1056 1006
  instance_data[instance].msg_f.seqNum = instance_data[instance].frameSN++;
1057 1007

  
1058 1008
  //set the delayed rx on time (the final message will be sent after this delay)
1059
  dwt_setrxaftertxdelay(instance_data[instance].ancRespRxDelay);  //units are 1.0256us - wait for wait4respTIM before RX on (delay RX)
1009
  dwt_setrxaftertxdelay((uint32_t)instance_data[instance].ancRespRxDelay);  //units are 1.0256us - wait for wait4respTIM before RX on (delay RX)
1060 1010

  
1061 1011
  instance_data[instance].tagSleepCorrection = 0;
1062 1012
  instance_data[instance].msg_f.messageData[RES_TAG_SLP0] = 0 ;
......
1093 1043

  
1094 1044
  //we have our range - update the own mask entry...
1095 1045
  if(instance_data[instance].tof[tof_idx] != INVALID_TOF) { //check the last ToF entry is valid and copy into the current array
1096
    instance_data[instance].rxResponseMask = (0x1 << instance_data[instance].shortAdd_idx);
1046
    instance_data[instance].rxResponseMask = (uint8_t)(0x1 << instance_data[instance].shortAdd_idx);
1097 1047
    instance_data[instance].tofArray[instance_data[instance].shortAdd_idx] = instance_data[instance].tof[tof_idx];
1098 1048
  }
1099 1049
  else	{  //reset response mask
......
1101 1051
    instance_data[instance].rxResponseMask = 0;	//reset the mask of received responses when rx poll
1102 1052
  }
1103 1053
  //set the delayed rx on time (the final message will be sent after this delay)
1104
  dwt_setrxaftertxdelay(instance_data[instance].ancRespRxDelay);  //units are 1.0256us - wait for wait4respTIM before RX on (delay RX)
1054
  dwt_setrxaftertxdelay((uint32_t)instance_data[instance].ancRespRxDelay);  //units are 1.0256us - wait for wait4respTIM before RX on (delay RX)
1105 1055

  
1106 1056
  //if this is gateway anchor - calculate the slot period correction
1107 1057
  if(instance_data[instance].gatewayAnchor)  {
......
1187 1137

  
1188 1138
  //if Listener then just report the received frame to the instance (application)
1189 1139
  if(rxd_event == DWT_SIG_RX_OKAY) { //Process good/known frame types
1190
    uint16_t sourceAddress = (((uint16_t)dw_event.msgu.frame[srcAddr_index+1]) << 8) + dw_event.msgu.frame[srcAddr_index];
1140
    uint16_t sourceAddress = (uint16_t)(((uint16_t)dw_event.msgu.frame[srcAddr_index+1]) << 8) + (uint16_t)(dw_event.msgu.frame[srcAddr_index]);
1191 1141

  
1192 1142
    if(instance_data[instance].mode != LISTENER)      {
1193 1143
      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 1201
          instance_data[instance].rxMsgCount++;
1252 1202
          return;
1253 1203
        }
1254
        instance_data[instance].rxRespsIdx = (int8_t) ((dw_event.msgu.frame[POLL_RNUM+fcode_index] & 0xf)
1204
        instance_data[instance].rxRespsIdx = (uint8_t) ((dw_event.msgu.frame[POLL_RNUM+fcode_index] & 0xf)
1255 1205
            + ((sourceAddress&0x7) << 4));
1256 1206
        instance_data[instance].rxResps[instance_data[instance].rxRespsIdx] = 0;
1257 1207

  
......
1352 1302
          instance_data[instance].rxMsgCount++;
1353 1303
          return;
1354 1304
        }
1355
      break;  // TODO: fallthourgh for anchor
1305
      break;
1356 1306
//        __attribute__ ((fallthrough));
1357 1307

  
1358 1308
        //if anchor fall into case below and process the frame
......
1376 1326
}
1377 1327

  
1378 1328

  
1379
/*TODO: Callback funtion for RX timeout (available from newer driver version) */
1329
/*! Callback funtion for RX timeout (available from newer driver version) */
1380 1330
#pragma GCC optimize ("O3")
1381 1331
void instance_rxtimeoutcallback(const dwt_cb_data_t *rxd){
1382 1332

  
......
1410 1360
}
1411 1361

  
1412 1362

  
1413
/*TODO: Callback funtion for RX error (available from newer driver version) */
1363
/*! Callback funtion for RX error (available from newer driver version) */
1414 1364
#pragma GCC optimize ("O3")
1415 1365
void instance_rxerrorcallback(const dwt_cb_data_t *rxd) {
1416 1366

  
......
1503 1453
  dw_event_g.type = instance_data[instance].dwevent[indexOut].type ;
1504 1454

  
1505 1455

  
1506
  instance_data[instance].dwevent[indexOut].gotit = x;
1456
  instance_data[instance].dwevent[indexOut].gotit = (uint8_t)x;
1507 1457

  
1508 1458
  //instance_data[instance].dwevent[indexOut].eventtimeclr = portGetTickCnt();
1509 1459

  
......
1555 1505
    message = 0;
1556 1506
  }
1557 1507

  
1558

  
1559

  
1560 1508
  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)
1561 1509
    if(instance_data[instance].mode == TAG) { //Tag (is either in RX or sleeping)
1562 1510
      int32_t nextPeriod ;
......
1664 1612
  instance_data[0].txPower = txpower ;
1665 1613

  
1666 1614
  instance_data[0].txPowerChanged = 1;
1667

  
1668 1615
}
1669 1616

  
1670 1617
void instancesettxpower(void){
......
1709 1656
#endif
1710 1657

  
1711 1658

  
1712
/*! TODO: the following Functions are added for user application (previously on the API )*/
1659
/*! The following Functions are added for user application (previously on the API )*/
1713 1660

  
1714 1661
/*! ------------------------------------------------------------------------------------------------------------------
1715 1662
 * @fn dwt_getotptxpower()

Also available in: Unified diff