From ecd6bbb26aecc2f749675398ce0318e8ab40959a Mon Sep 17 00:00:00 2001 From: David <stierint@hotmail.com> Date: Mon, 26 Oct 2020 19:51:41 -0400 Subject: [PATCH] timestamp datatype fixes. complete misc TODOs --- DecaRanging.coproj | 20 +- src/application/application_definitions.h | 36 +- src/application/dw_main.c | 35 +- src/application/instance.c | 537 +--------- src/application/instance.h | 56 +- src/application/instance_common.c | 720 ++----------- src/application/tdma_handler.c | 1160 +-------------------- src/application/tdma_handler.h | 10 +- src/decadriver/deca_device.c | 172 +-- src/decadriver/deca_regs.h | 7 +- src/platform/port.c | 52 +- src/platform/port.h | 1 - 12 files changed, 241 insertions(+), 2565 deletions(-) diff --git a/DecaRanging.coproj b/DecaRanging.coproj index 6c9e7cf..ad9877d 100644 --- a/DecaRanging.coproj +++ b/DecaRanging.coproj @@ -105,8 +105,8 @@ <File name="src/platform/deca_spi.h" path="src/platform/deca_spi.h" type="1"/> <File name="Libraries/STM32_USB_Device_Library/Core/src/usbd_ioreq.c" path="Libraries/STM32_USB_Device_Library/Core/src/usbd_ioreq.c" type="1"/> <File name="Libraries/STM32_USB_Device_Library/Class/cdc/src/usbd_cdc_core.c" path="Libraries/STM32_USB_Device_Library/Class/cdc/src/usbd_cdc_core.c" type="1"/> - <File name="Libraries/STM32_USB_Device_Library/Class/cdc/inc/usbd_cdc_core.h" path="Libraries/STM32_USB_Device_Library/Class/cdc/inc/usbd_cdc_core.h" type="1"/> <File name="src/usb/usbd_desc.h" path="src/usb/usbd_desc.h" type="1"/> + <File name="Libraries/STM32_USB_Device_Library/Class/cdc/inc/usbd_cdc_core.h" path="Libraries/STM32_USB_Device_Library/Class/cdc/inc/usbd_cdc_core.h" type="1"/> <File name="src/platform/deca_spi.c" path="src/platform/deca_spi.c" type="1"/> <File name="src/usb" path="" type="2"/> <File name="src/decadriver/deca_version.h" path="src/decadriver/deca_version.h" type="1"/> @@ -137,12 +137,12 @@ <File name="Libraries/CMSIS/CM3/DeviceSupport/ST/STM32F10x/system_stm32f10x.h" path="Libraries/CMSIS/CM3/DeviceSupport/ST/STM32F10x/system_stm32f10x.h" type="1"/> <File name="src/decadriver/deca_device.c" path="src/decadriver/deca_device.c" type="1"/> <File name="Libraries/STM32F10x_StdPeriph_Driver/src/stm32f10x_exti.c" path="Libraries/STM32F10x_StdPeriph_Driver/src/stm32f10x_exti.c" type="1"/> - <File name="Libraries/STM32F10x_StdPeriph_Driver/inc/stm32f10x_adc.h" path="Libraries/STM32F10x_StdPeriph_Driver/inc/stm32f10x_adc.h" type="1"/> <File name="Libraries/STM32_USB_Device_Library/Core/inc/usbd_req.h" path="Libraries/STM32_USB_Device_Library/Core/inc/usbd_req.h" type="1"/> + <File name="Libraries/STM32F10x_StdPeriph_Driver/inc/stm32f10x_adc.h" path="Libraries/STM32F10x_StdPeriph_Driver/inc/stm32f10x_adc.h" type="1"/> <File name="Libraries/STM32F10x_StdPeriph_Driver/src/stm32f10x_rtc.c" path="Libraries/STM32F10x_StdPeriph_Driver/src/stm32f10x_rtc.c" type="1"/> <File name="Libraries/STM32F10x_StdPeriph_Driver/src/stm32f10x_fsmc.c" path="Libraries/STM32F10x_StdPeriph_Driver/src/stm32f10x_fsmc.c" type="1"/> - <File name="Libraries/STM32_USB_OTG_Driver/inc/usb_otg.h" path="Libraries/STM32_USB_OTG_Driver/inc/usb_otg.h" type="1"/> <File name="src/usb/usb_conf.h" path="src/usb/usb_conf.h" type="1"/> + <File name="Libraries/STM32_USB_OTG_Driver/inc/usb_otg.h" path="Libraries/STM32_USB_OTG_Driver/inc/usb_otg.h" type="1"/> <File name="Libraries/STM32F10x_StdPeriph_Driver/src/stm32f10x_rcc.c" path="Libraries/STM32F10x_StdPeriph_Driver/src/stm32f10x_rcc.c" type="1"/> <File name="Libraries/STM32_USB_Device_Library/Core/src/usbd_req.c" path="Libraries/STM32_USB_Device_Library/Core/src/usbd_req.c" type="1"/> <File name="src/usb/usbd_conf.h" path="src/usb/usbd_conf.h" type="1"/> @@ -163,26 +163,26 @@ <File name="Libraries/STM32_USB_Device_Library/Core/src" path="" type="2"/> <File name="Libraries/STM32F10x_StdPeriph_Driver/src/stm32f10x_i2c.c" path="Libraries/STM32F10x_StdPeriph_Driver/src/stm32f10x_i2c.c" type="1"/> <File name="Libraries/STM32F10x_StdPeriph_Driver/src/misc.c" path="Libraries/STM32F10x_StdPeriph_Driver/src/misc.c" type="1"/> - <File name="Libraries/STM32_USB_OTG_Driver/src/usb_hcd.c" path="Libraries/STM32_USB_OTG_Driver/src/usb_hcd.c" type="1"/> <File name="Libraries/STM32F10x_StdPeriph_Driver/inc/stm32f10x_cec.h" path="Libraries/STM32F10x_StdPeriph_Driver/inc/stm32f10x_cec.h" type="1"/> + <File name="Libraries/STM32_USB_OTG_Driver/src/usb_hcd.c" path="Libraries/STM32_USB_OTG_Driver/src/usb_hcd.c" type="1"/> <File name="src/application/main.c" path="src/main.c" type="1"/> <File name="src/platform/deca_sleep.c" path="src/platform/deca_sleep.c" type="1"/> - <File name="Libraries/CMSIS/CM3/CoreSupport" path="" type="2"/> <File name="Libraries/STM32_USB_OTG_Driver/inc/usb_defines.h" path="Libraries/STM32_USB_OTG_Driver/inc/usb_defines.h" type="1"/> - <File name="src/decadriver/deca_param_types.h" path="src/decadriver/deca_param_types.h" type="1"/> - <File name="src/compiler" path="" type="2"/> + <File name="Libraries/CMSIS/CM3/CoreSupport" path="" type="2"/> <File name="Libraries/STM32_USB_OTG_Driver/src/usb_dcd_int.c" path="Libraries/STM32_USB_OTG_Driver/src/usb_dcd_int.c" type="1"/> + <File name="src/compiler" path="" type="2"/> + <File name="src/decadriver/deca_param_types.h" path="src/decadriver/deca_param_types.h" type="1"/> <File name="src/usb/deca_usb_bsp_evk1000.c" path="src/usb/deca_usb_bsp_evk1000.c" type="1"/> <File name="Libraries/CMSIS/CM3/DeviceSupport/ST/STM32F10x/startup/startup_stm32f10x_cl.S" path="Libraries/CMSIS/CM3/DeviceSupport/ST/STM32F10x/startup/startup_stm32f10x_cl.S" type="1"/> - <File name="src/platform/stm32f10x_conf.h" path="src/platform/stm32f10x_conf.h" type="1"/> <File name="Libraries/STM32_USB_OTG_Driver/inc/usb_bsp.h" path="Libraries/STM32_USB_OTG_Driver/inc/usb_bsp.h" type="1"/> + <File name="src/platform/stm32f10x_conf.h" path="src/platform/stm32f10x_conf.h" type="1"/> <File name="Libraries/CMSIS/CM3" path="" type="2"/> <File name="Libraries" path="" type="2"/> <File name="src/decadriver/deca_types.h" path="src/decadriver/deca_types.h" type="1"/> <File name="src/usb/deca_usb.h" path="src/usb/deca_usb.h" type="1"/> <File name="Libraries/STM32F10x_StdPeriph_Driver/inc" path="" type="2"/> - <File name="Libraries/STM32_USB_Device_Library/Core/inc/usbd_def.h" path="Libraries/STM32_USB_Device_Library/Core/inc/usbd_def.h" type="1"/> <File name="Libraries/STM32F10x_StdPeriph_Driver/inc/stm32f10x_spi.h" path="Libraries/STM32F10x_StdPeriph_Driver/inc/stm32f10x_spi.h" type="1"/> + <File name="Libraries/STM32_USB_Device_Library/Core/inc/usbd_def.h" path="Libraries/STM32_USB_Device_Library/Core/inc/usbd_def.h" type="1"/> <File name="Libraries/STM32F10x_StdPeriph_Driver/inc/stm32f10x_iwdg.h" path="Libraries/STM32F10x_StdPeriph_Driver/inc/stm32f10x_iwdg.h" type="1"/> <File name="src/platform/stm32f10x_it.c" path="src/platform/stm32f10x_it.c" type="1"/> <File name="Libraries/CMSIS/CM3/DeviceSupport/ST/STM32F10x/system_stm32f10x.c" path="Libraries/CMSIS/CM3/DeviceSupport/ST/STM32F10x/system_stm32f10x.c" type="1"/> @@ -216,8 +216,8 @@ <File name="Libraries/CMSIS/CM3/DeviceSupport/ST/STM32F10x/startup" path="" type="2"/> <File name="Libraries/STM32_USB_OTG_Driver/inc/usb_core.h" path="Libraries/STM32_USB_OTG_Driver/inc/usb_core.h" type="1"/> <File name="Libraries/STM32F10x_StdPeriph_Driver/src/stm32f10x_bkp.c" path="Libraries/STM32F10x_StdPeriph_Driver/src/stm32f10x_bkp.c" type="1"/> - <File name="Libraries/STM32F10x_StdPeriph_Driver/src/stm32f10x_gpio.c" path="Libraries/STM32F10x_StdPeriph_Driver/src/stm32f10x_gpio.c" type="1"/> <File name="src/application/dw_main.c" path="src/application/dw_main.c" type="1"/> + <File name="Libraries/STM32F10x_StdPeriph_Driver/src/stm32f10x_gpio.c" path="Libraries/STM32F10x_StdPeriph_Driver/src/stm32f10x_gpio.c" type="1"/> <File name="src/platform" path="" type="2"/> <File name="Libraries/STM32F10x_StdPeriph_Driver/src/stm32f10x_tim.c" path="Libraries/STM32F10x_StdPeriph_Driver/src/stm32f10x_tim.c" type="1"/> <File name="Libraries/STM32F10x_StdPeriph_Driver/inc/stm32f10x_i2c.h" path="Libraries/STM32F10x_StdPeriph_Driver/inc/stm32f10x_i2c.h" type="1"/> diff --git a/src/application/application_definitions.h b/src/application/application_definitions.h index cc4c2c9..426da17 100644 --- a/src/application/application_definitions.h +++ b/src/application/application_definitions.h @@ -24,7 +24,6 @@ #define MASK_40BIT (0x00FFFFFFFFFF) // DW1000 counter is 40 bits #define MASK_42BIT (0x000003FFFFFFFFFF) //stm32 microsecond timestamps are 42 bits #define MASK_TXDTS (0x00FFFFFFFE00) //The TX timestamp will snap to 8 ns resolution - mask lower 9 bits. -#define SYS_MASK_VAL (DWT_INT_TFRS | DWT_INT_RFCG | DWT_INT_RXOVRR | DWT_INT_ARFE | DWT_INT_RFSL | DWT_INT_SFDT | DWT_INT_RPHE | DWT_INT_RFCE | DWT_INT_RFTO) #define DELAY_CALIB (0) // when set to 1 - the LCD display will show information used for TX/RX delay calibration #define SET_TXRX_DELAY (0) //when set to 1 - the DW1000 RX and TX delays are set to the TX_DELAY and RX_DELAY defines @@ -78,13 +77,13 @@ enum #define RTLS_DEMO_MSG_SYNC (0x22) // Inform other UWBs to sync their TDMA frame start times //lengths including the Decaranging Message Function Code byte -#define TAG_POLL_MSG_LEN 1 // FunctionCode(1), -#define ANCH_RESPONSE_MSG_LEN 15 //TODO shorten! // FunctionCode(1), RespOption (1), OptionParam(2), Number of Tags(1), Measured_TOF_Time(6), Time Till next window reserved for catching a blink message (4) +#define TAG_POLL_MSG_LEN 1 // FunctionCode(1) +#define ANCH_RESPONSE_MSG_LEN 1 // FunctionCode(1) #define TAG_FINAL_MSG_LEN 16 // FunctionCode(1), Poll_TxTime(5), Resp_RxTime(5), Final_TxTime(5) #define RANGINGINIT_MSG_LEN 1 // FunctionCode(1) -#define RNG_REPORT_MSG_LEN_SHORT 9 // FunctionCode(1), time of flight (6), short address (2) -#define RNG_REPORT_MSG_LEN_LONG 15 // FunctionCode(1), time of flight (6), long address (8) -#define SYNC_MSG_LEN 8 // FunctionCode (1), framelength (1), time since frame start (6) +#define RNG_REPORT_MSG_LEN_SHORT 9 // FunctionCode(1), time of flight (6), short address (2) +#define RNG_REPORT_MSG_LEN_LONG 15 // FunctionCode(1), time of flight (6), long address (8) +#define SYNC_MSG_LEN 8 // FunctionCode(1), framelength (1), time since frame start (6) #define MAX_MAC_MSG_DATA_LEN (TAG_FINAL_MSG_LEN) //max message len of the above @@ -108,9 +107,6 @@ enum #define FRAME_CRTL_AND_ADDRESS_L (FRAME_DEST_ADDRESS_L + FRAME_SOURCE_ADDRESS_L + FRAME_CTRLP) //21 bytes for 64-bit addresses) #define FRAME_CRTL_AND_ADDRESS_S (FRAME_DEST_ADDRESS_S + FRAME_SOURCE_ADDRESS_S + FRAME_CTRLP) //9 bytes for 16-bit addresses) #define FRAME_CRTL_AND_ADDRESS_LS (FRAME_DEST_ADDRESS_L + FRAME_SOURCE_ADDRESS_S + FRAME_CTRLP) //15 bytes for 1 16-bit address and 1 64-bit address) -//#define MAX_USER_PAYLOAD_STRING_LL (STANDARD_FRAME_SIZE-FRAME_CRTL_AND_ADDRESS_L-TAG_FINAL_MSG_LEN-FRAME_CRC) //127 - 21 - 16 - 2 = 88 -//#define MAX_USER_PAYLOAD_STRING_SS (STANDARD_FRAME_SIZE-FRAME_CRTL_AND_ADDRESS_S-TAG_FINAL_MSG_LEN-FRAME_CRC) //127 - 9 - 16 - 2 = 100 -//#define MAX_USER_PAYLOAD_STRING_LS (STANDARD_FRAME_SIZE-FRAME_CRTL_AND_ADDRESS_LS-TAG_FINAL_MSG_LEN-FRAME_CRC) //127 - 15 - 16 - 2 = 94 #define MAX_USER_PAYLOAD_STRING_LL (STANDARD_FRAME_SIZE-FRAME_CRTL_AND_ADDRESS_L-FRAME_CRC) //127 - 21 - 2 = 104 #define MAX_USER_PAYLOAD_STRING_SS (STANDARD_FRAME_SIZE-FRAME_CRTL_AND_ADDRESS_S-FRAME_CRC) //127 - 9 - 2 = 116 #define MAX_USER_PAYLOAD_STRING_LS (STANDARD_FRAME_SIZE-FRAME_CRTL_AND_ADDRESS_LS-FRAME_CRC) //127 - 15 - 2 = 110 @@ -177,14 +173,13 @@ enum // Function code byte offset (valid for all message types). #define FCODE 0 // Function code is 1st byte of messageData -//INF message byte offsets //TODO remove the +6 -#define TDMA_TSFS 1+6 // offset to put time since TDMA frame start in the INF message -#define TDMA_TSFS_REBASE 7+6 // offset to put whether the receiving UWB needs to rebase its TDMA frame to the transmitted TSFS -#define TDMA_NUMN 8+6 // offset to put the number of this UWB's neighbors in the INF message -#define TDMA_NUMH 9+6 // offset to put the number of this UWB's hidden neighbors in the INF message -#define TDMA_FRAMELENGTH 10+6 // offset to put this UWB's TDMA framelength in the INF message -#define TDMA_NUMS 11+6 // offset to put the number of this UWB's TDMA slot assignments in the INF message -//TODO remove the +6 from above! +//INF message byte offsets +#define TDMA_TSFS 1 // offset to put time since TDMA frame start in the INF message +#define TDMA_TSFS_REBASE 7 // offset to put whether the receiving UWB needs to rebase its TDMA frame to the transmitted TSFS +#define TDMA_NUMN 8 // offset to put the number of this UWB's neighbors in the INF message +#define TDMA_NUMH 9 // offset to put the number of this UWB's hidden neighbors in the INF message +#define TDMA_FRAMELENGTH 10 // offset to put this UWB's TDMA framelength in the INF message +#define TDMA_NUMS 11 // offset to put the number of this UWB's TDMA slot assignments in the INF message // Final message byte offsets. #define PTXT 1 @@ -268,7 +263,7 @@ enum #define BLINK_PERIOD_RAND_MS 200 -#define RANGE_INIT_RAND 2000 //in DW1000 device time +#define RANGE_INIT_RAND 2000 //in DW1000 device time //TODO tune this number #define RX_CHECK_ON_PERIOD 200 //TODO modify @@ -317,9 +312,8 @@ typedef enum inst_states TA_TXBLINK_WAIT_SEND, //10 TA_TXRANGINGINIT_WAIT_SEND, //11 TA_TX_SELECT, //12 - TA_MODE_SELECT, //13 - TA_TXREPORT_WAIT_SEND, //14 - TA_TXSUG_WAIT_SEND //15 + TA_TXREPORT_WAIT_SEND, //13 + TA_TXSUG_WAIT_SEND //14 } INST_STATES; diff --git a/src/application/dw_main.c b/src/application/dw_main.c index 7e5e421..4a4572a 100644 --- a/src/application/dw_main.c +++ b/src/application/dw_main.c @@ -27,7 +27,6 @@ extern int usb_init(void); extern void usb_printconfig(int, uint8*, int); extern void send_usbmessage(uint8*, int); - // "1234567890123456" - 16 bytes long LCD #define SOFTWARE_VER_STRING "TDMA Version 1.0" // #define SWS1_TXSPECT_MODE 0x80 //Continuous TX spectrum mode @@ -140,10 +139,6 @@ instanceConfig_t chConfig[8] ={ } }; - -//uint32 inittestapplication(uint8 s1switch); - - int decarangingmode(uint8 mode_switch) { int mode = 0; @@ -205,14 +200,11 @@ uint32 inittestapplication(uint8 mode_switch) if(mode_switch & SWS1_ANC_MODE) { -// instance_mode = ANCHOR; - led_on(LED_PC6); } else { -// instance_mode = TAG; led_on(LED_PC7); } @@ -310,8 +302,6 @@ int dw_main(void) int toggle = 0; int toggle_counter = 0; int toggle_step = 5; -// uint8 dataseq[LCD_BUFF_LEN]; -// bool new_range = FALSE; uint8 command = 0x0; led_off(LED_ALL); //turn off all the LEDs @@ -354,7 +344,7 @@ int dw_main(void) writetoLCD(1, 0, dataseq); memcpy(dataseq, (const uint8 *) "GGRG UWB RANGING", LCD_BUFF_LEN); writetoLCD(40, 1, dataseq); //send some data - memcpy(dataseq, (const uint8 *) SOFTWARE_VER_STRING, LCD_BUFF_LEN); // Also set at line #26 (TODO Should make this from single value !!!) + memcpy(dataseq, (const uint8 *) SOFTWARE_VER_STRING, LCD_BUFF_LEN); writetoLCD(16, 1, dataseq); //send some data } @@ -422,20 +412,6 @@ int dw_main(void) memset(dataseq, ' ', LCD_BUFF_LEN); } -// if(enableLCD == TRUE) -// { -// -// memcpy(&dataseq[0], (const uint8 *) " DISCOVERY MODE ", LCD_BUFF_LEN); -// writetoLCD(LCD_BUFF_LEN, 1, dataseq); //send some data -// sprintf((char*)&dataseq[0], "%llX", instance_get_addr()); -// writetoLCD(LCD_BUFF_LEN, 1, dataseq); //send some data -// -// command = 0x2 ; //return cursor home -// writetoLCD( 1, 0, &command); -// } - - - if(enableLCD == TRUE) { memset(dataseq, ' ', LCD_BUFF_LEN); @@ -444,10 +420,6 @@ int dw_main(void) port_EnableEXT_IRQ(); - //TODO remove - instance_data_t* inst1 = instance_get_local_structure_ptr(0); - inst1->testTimer = portGetTickCntMicro(); - // main loop while(1) { @@ -455,7 +427,7 @@ int dw_main(void) //TODO reenable optimization in the compiler settings!!! instance_data_t* inst = instance_get_local_structure_ptr(0); canSleep = instance_run(); //run the state machine!!! - instance_mode = inst->mode; //TODO modify how the rest of this works with DISCOVER, TAG, and ANCHOR! + instance_mode = inst->mode; if(instancenewrange()) { @@ -596,7 +568,6 @@ int dw_main(void) dataseq[0] = 0x2 ; //return cursor home writetoLCD( 1, 0, dataseq); - //TODO only update the display if something has changed! if(toggle_counter <= toggle_step) { if(toggle == 2) @@ -620,8 +591,6 @@ int dw_main(void) toggle_counter = 0; } - - //TODO only update the display if something has changed! if(updateLCD == TRUE) { if(toggle == 1) diff --git a/src/application/instance.c b/src/application/instance.c index 1311237..826254a 100644 --- a/src/application/instance.c +++ b/src/application/instance.c @@ -167,49 +167,15 @@ void instanceconfigframeheader(instance_data_t *inst) // void instanceconfigmessages(instance_data_t *inst) { - //initialize ranging message(s) + //initialize ranging message //set source address into the message structure -// for(int i=0; i<UWB_LIST_SIZE; i++) //TODO only have 1 msg? or one Tag msg, one Anchor msg? -// { -// memcpy(&inst->msg[i].sourceAddr[0], &inst->eui64[0], inst->addrByteSize); -// } - memcpy(&inst->msg.sourceAddr[0], &inst->eui64[0], inst->addrByteSize); - //initialize RNG_INIT message //set source address into the message structure memcpy(&inst->rng_initmsg.sourceAddr[0], &inst->eui64[0], inst->addrByteSize); inst->rng_initmsg.messageData[FCODE] = RTLS_DEMO_MSG_RNG_INIT; - -// ////////////////////////////////////////////////////////////////////////////// -// //TODO remove this -// -// //these bytes not used, zero them out. -// inst->rng_initmsg.messageData[RNG_INIT_TAG_SHORT_ADDR_LO] = 0x00; -// inst->rng_initmsg.messageData[RNG_INIT_TAG_SHORT_ADDR_HI] = 0x00; -// -// -// -// uint16 resp_dly_us, resp_dly; -// // First response delay to send is anchor's response delay. -// resp_dly_us = ANC_TURN_AROUND_TIME_US + inst->frameLengths_us[POLL]; -// resp_dly = ((RESP_DLY_UNIT_US << RESP_DLY_UNIT_SHIFT) & RESP_DLY_UNIT_MASK) -// + ((resp_dly_us << RESP_DLY_VAL_SHIFT) & RESP_DLY_VAL_MASK); -// inst->rng_initmsg.messageData[RNG_INIT_ANC_RESP_DLY_LO] = resp_dly & 0xFF; //TODO remove -// inst->rng_initmsg.messageData[RNG_INIT_ANC_RESP_DLY_HI] = (resp_dly >> 8) & 0xFF; //TODO remove -// // Second response delay to send is tag's response delay. -// resp_dly_us = TAG_TURN_AROUND_TIME_US + inst->frameLengths_us[RESP]; -// resp_dly = ((RESP_DLY_UNIT_US << RESP_DLY_UNIT_SHIFT) & RESP_DLY_UNIT_MASK) -// + ((resp_dly_us << RESP_DLY_VAL_SHIFT) & RESP_DLY_VAL_MASK); -// inst->rng_initmsg.messageData[RNG_INIT_TAG_RESP_DLY_LO] = resp_dly & 0xFF; -// inst->rng_initmsg.messageData[RNG_INIT_TAG_RESP_DLY_HI] = (resp_dly >> 8) & 0xFF; -// -// //////////////////////////////////////////////////////////////////////////////////////// - - - //configure INF message uint16 broadcast_address = BROADCAST_ADDRESS; memcpy(&inst->inf_msg.sourceAddr[0], &inst->eui64[0], inst->addrByteSize); @@ -221,7 +187,6 @@ void instanceconfigmessages(instance_data_t *inst) memcpy(&inst->report_msg.destAddr[0], &broadcast_address, 2); inst->report_msg.messageData[FCODE] = RTLS_DEMO_MSG_RNG_REPORT; //message function code (specifies if message is a poll, response or other...) - //configure SYNC message memcpy(&inst->sync_msg.sourceAddr[0], &inst->eui64[0], inst->addrByteSize); memcpy(&inst->sync_msg.destAddr[0], &broadcast_address, 2); @@ -229,7 +194,6 @@ void instanceconfigmessages(instance_data_t *inst) //configure BLINK message memcpy(&inst->blinkmsg.tagID[0], &inst->eui64[0], ADDR_BYTE_SIZE_L); - } @@ -249,15 +213,8 @@ void instancerxon(instance_data_t *inst, int delayed, uint64 delayedReceiveTime) dwt_setdelayedtrxtime(dtime) ; } -// inst->lateRX -= dwt_rxenable(delayed) ; //- as when fails -1 is returned // turn receiver on, immediate/delayed - -// uint32 time_now = portGetTickCnt(); int dwt_rx_enable_return = dwt_rxenable(delayed); -// uint8 debug_msg[100]; -// int n = sprintf((char*)&debug_msg[0], "RX_ENABLE time_now: %lu, rx_enable: %i", time_now, dwt_rx_enable_return); -// send_usbmessage(&debug_msg[0], n); -// usb_run(); - inst->lateRX -= dwt_rx_enable_return; + inst->lateRX -= dwt_rx_enable_return; } // end instancerxon() @@ -300,7 +257,6 @@ const char* get_inst_states_string(enum inst_states state) case TA_TXBLINK_WAIT_SEND : return "TA_TXBLINK_WAIT_SEND"; case TA_TXRANGINGINIT_WAIT_SEND : return "TA_TXRANGINGINIT_WAIT_SEND"; case TA_TX_SELECT : return "TA_TX_SELECT"; - case TA_MODE_SELECT : return "TA_MODE_SELECT"; case TA_TXREPORT_WAIT_SEND : return "TA_TXREPORT_WAIT_SEND"; case TA_TXSUG_WAIT_SEND : return "TA_TXSUG_WAIT_SEND"; default: return "NONE"; @@ -313,7 +269,6 @@ const char* get_instanceModes_string(enum instanceModes mode) switch (mode) { case DISCOVERY : return "DISCOVERY"; -// case CONNECTION_PENDING : return "CONNECTION_PENDING"; case TAG : return "TAG"; case ANCHOR : return "ANCHOR"; case NUM_MODES : return "NUM_MODES"; @@ -382,11 +337,10 @@ char* get_msg_fcode_string(int fcode) } } -//void send_statetousb(instance_data_t *inst) void send_statetousb(instance_data_t *inst, struct TDMAHandler *tdma_handler) { - int usbdebugdata_size = sprintf((char*)&usbdebugdata[0], "%s , %s , %s , %s", get_inst_states_string(inst->testAppState), get_inst_states_string(inst->previousState), get_inst_states_string(inst->nextState), get_instanceModes_string(inst->mode)); + usbdebugdata_size = sprintf((char*)&usbdebugdata[0], "%s , %s , %s , %s", get_inst_states_string(inst->testAppState), get_inst_states_string(inst->previousState), get_inst_states_string(inst->nextState), get_instanceModes_string(inst->mode)); if (memcmp(usbdebugdataprev, usbdebugdata, usbdebugdata_size) != 0 || usbdebugdata_size != usbdebugdataprev_size) { @@ -394,19 +348,12 @@ void send_statetousb(instance_data_t *inst, struct TDMAHandler *tdma_handler) usb_run(); usbdebugdataprev_size = usbdebugdata_size; memcpy(usbdebugdataprev, usbdebugdata, usbdebugdata_size); - -// int num_neighbors = instfindnumneighbors(inst); -// uint8 debug_msg[150]; -// int n = sprintf((char*)&debug_msg[0], "mode: %s, num_neighbors %d, discovery_mode %s", get_instanceModes_string(inst->mode), num_neighbors, get_discovery_modes_string(tdma_handler->discovery_mode)); -// send_usbmessage(&debug_msg[0], n); -// usb_run(); - } } void send_rxmsgtousb(char *data) { - int usbrxdebugdata_size = sprintf((char*)&usbrxdebugdata[0], "%s", data); + usbrxdebugdata_size = sprintf((char*)&usbrxdebugdata[0], "%s", data); if (memcmp(usbrxdebugdataprev, usbrxdebugdata, usbrxdebugdata_size) != 0 || usbrxdebugdata_size != usbrxdebugdataprev_size) { @@ -419,7 +366,7 @@ void send_rxmsgtousb(char *data) void send_txmsgtousb(char *data) { - int usbtxdebugdata_size = sprintf((char*)&usbtxdebugdata[0], "TX message: %s", data); + usbtxdebugdata_size = sprintf((char*)&usbtxdebugdata[0], "TX message: %s", data); send_usbmessage(&usbtxdebugdata[0], usbtxdebugdata_size); usb_run(); @@ -435,20 +382,6 @@ int testapprun(instance_data_t *inst, struct TDMAHandler *tdma_handler, int mess { int done = INST_NOT_DONE_YET; -//// uint16 time_now_tim3 = (uint16)portGetTIM3(); -// uint64 time_now_us = portGetTickCntMicro(); -// uint64 deltaT = get_dt64(inst->testTimer, time_now_us); -// if(deltaT >= 500000) -// { -// uint8 debug_msg[200]; -// int n = sprintf((char*)&debug_msg[0], "%llu, %llu, %llu", inst->testTimer, time_now_us, deltaT); -//// int n = sprintf((char*)&debug_msg[0], "xxx, %llu, %u", inst->testTimer, time_now_tim3); -// send_usbmessage(&debug_msg[0], n); -// usb_run(); -// inst->testTimer = time_now_us; -// } - - if(tdma_handler->slot_transition(tdma_handler)) { message = 0; @@ -456,15 +389,6 @@ int testapprun(instance_data_t *inst, struct TDMAHandler *tdma_handler, int mess tdma_handler->check_discovery_mode_expiration(tdma_handler); - //NOTE: temporary debug code - if(inst->testAppState != inst->lastState){ - inst->currentStateStartTime = portGetTickCnt(); - } - inst->lastState = inst->testAppState; - -// send_statetousb(inst, tdma_handler); - - switch (inst->testAppState) { case TA_INIT : @@ -543,17 +467,10 @@ int testapprun(instance_data_t *inst, struct TDMAHandler *tdma_handler, int mess default: break; } - break; // end case TA_INIT - } - case TA_MODE_SELECT : - { - //TODO maybe this will be used to transition between DISCOVERY/ANCHOR/TAG modes? - break; - } + break; + }// end case TA_INIT case TA_TX_SELECT : { -// send_statetousb(inst); - //select a TX action, return TRUE if we should move on to another state if(tdma_handler->tx_select(tdma_handler) == TRUE) { @@ -565,14 +482,10 @@ int testapprun(instance_data_t *inst, struct TDMAHandler *tdma_handler, int mess done = INST_DONE_WAIT_FOR_NEXT_EVENT; } - break; // end case TA_TX_SELECT - } + break; + }// end case TA_TX_SELECT case TA_SLEEP_DONE : { - // if (inst->mode == ANCHOR) - // { -// send_statetousb(inst); - // } event_data_t* dw_event = instance_getevent(10); //clear the event from the queue // waiting for timeout from application to wakeup IC if (dw_event->type != DWT_SIG_RX_TIMEOUT) @@ -606,17 +519,17 @@ int testapprun(instance_data_t *inst, struct TDMAHandler *tdma_handler, int mess instancesetantennadelays(); #endif //TODO should this be here? or only if DEEP_SLEEP is enabled (above) putting only above for now -// instancesetantennadelays(); //this will update the antenna delay if it has changed break; } case TA_TXE_WAIT : //either go to sleep or proceed to TX a message { + //TODO rework sleep functionality + //should sleep between blinks? + //should blink when not own slot? + //should sleep after everything is done in own slot? + //does sleep still allow RX? - // if (inst->mode == ANCHOR) - // { - // send_statetousb(inst); - // } //if we are scheduled to go to sleep before next sending then sleep first. // if(((inst->nextState == TA_TXPOLL_WAIT_SEND) // || (inst->nextState == TA_TXBLINK_WAIT_SEND) || (inst->nextState == TA_TX_SELECT)) @@ -663,7 +576,7 @@ int testapprun(instance_data_t *inst, struct TDMAHandler *tdma_handler, int mess { int flength = (BLINK_FRAME_CRTL_AND_ADDRESS + FRAME_CRC); -// //blink frames with IEEE EUI-64 tag ID + //blink frames with IEEE EUI-64 tag ID inst->blinkmsg.seqNum = inst->frameSN++; //using wait for response to do delayed receive @@ -678,6 +591,7 @@ int testapprun(instance_data_t *inst, struct TDMAHandler *tdma_handler, int mess dwt_writetxdata(flength, (uint8 *) (&inst->blinkmsg), 0) ; // write the frame data dwt_writetxfctrl(flength, 0, 1); + //TODO standardize the transmission pass/fail handling as much as possible... if(dwt_starttx(DWT_START_TX_IMMEDIATE | inst->wait4ack) == 0) //always using immediate TX and enable delayed RX { uint8 debug_msg[100]; @@ -688,12 +602,12 @@ int testapprun(instance_data_t *inst, struct TDMAHandler *tdma_handler, int mess inst->blink_start = portGetTickCnt(); inst->timeofTx = portGetTickCnt(); + inst->txDoneTimeoutDuration = BLINK_SLEEP_DELAY + 20; //NOTE timeout duration found experimentally, may need to be changed if the delays in instance.h are modified tdma_handler->last_blink_time = portGetTickCnt(); tdma_handler->blinkPeriodRand = (uint32)rand()%BLINK_PERIOD_RAND_MS; } - inst->goToSleep = 1; //go to Sleep after this blink inst->testAppState = TA_TX_WAIT_CONF ; // wait confirmation //TODO move this into the if statement above? inst->previousState = TA_TXBLINK_WAIT_SEND ; //TODO move this into the ... @@ -742,45 +656,12 @@ int testapprun(instance_data_t *inst, struct TDMAHandler *tdma_handler, int mess uint32 time_now = portGetTickCnt(); inst->timeofTx = time_now; + inst->txDoneTimeoutDuration = RNG_INIT_REPLY_DLY_MS + 20; //NOTE timeout duration found experimentally, may need to be changed if the delays in instance.h are modified tdma_handler->set_discovery_mode(tdma_handler, WAIT_INF_INIT, time_now); //inst->monitor = 1; } - - - - -// dwt_setrxtimeout((uint16)0);//no timeout (keep RX on until instructed otherwise) -// dwt_setrxaftertxdelay((uint32)0);//immediately go to rx after tx - - -// dwt_writetxdata(psduLength, (uint8 *) &inst->rng_initmsg, 0) ; // write the frame data -// dwt_writetxfctrl(psduLength, 0, 1); -// if(dwt_starttx(DWT_START_TX_IMMEDIATE | inst->wait4ack) == 0) //always using immediate TX and enable delayed RX -// { -// inst->testAppState = TA_TX_WAIT_CONF ; // wait confirmation -// inst->previousState = TA_TXRANGINGINIT_WAIT_SEND ; -// done = INST_DONE_WAIT_FOR_NEXT_EVENT; //no timeout -// -// uint32 time_now = portGetTickCnt(); -// inst->timeofTx = time_now; -// tdma_handler->set_discovery_mode(tdma_handler, WAIT_INF_INIT, time_now); -// } -// else -// { -// inst->testAppState = TA_RXE_WAIT ; // wait to receive a new blink or poll message -// inst->wait4ack = 0; //clear the flag as the TX has failed the TRX is off -// uint8 debug_msg[100]; -// sprintf((char *)&debug_msg, "RTLS_DEMO_MSG_RNG_INIT -> TX ERROR"); -// send_txmsgtousb((char *)&debug_msg); -// usb_run(); -// } - - - - - - break; + break; } case TA_TXINF_WAIT_SEND : { @@ -799,7 +680,6 @@ int testapprun(instance_data_t *inst, struct TDMAHandler *tdma_handler, int mess uint8 fcode; memcpy(&fcode, &inst->inf_msg.messageData[FCODE], sizeof(uint8)); - dwt_writetxdata(psduLength, (uint8 *) &inst->inf_msg, 0) ; // write the frame data dwt_writetxfctrl(psduLength, 0, 1); if(dwt_starttx(DWT_START_TX_IMMEDIATE | inst->wait4ack) == 0) @@ -811,28 +691,15 @@ int testapprun(instance_data_t *inst, struct TDMAHandler *tdma_handler, int mess tdma_handler->set_discovery_mode(tdma_handler, EXIT, portGetTickCnt()); inst->mode = ANCHOR; - -// uint8 debug_msg[100]; -// int n = sprintf((char *)&debug_msg, "TX_INF_SUG,%llX,NULL", instance_get_addr()); -// send_usbmessage(&debug_msg[0], n); -// usb_run(); - -// //turn on RX after sending INF_SUG -// //set the delayed rx on time (the response message will be sent after this delay) -// dwt_setrxaftertxdelay(inst->txToRxDelayTag_sy); //TODO fix this! remove *0 //TODO will this mess up TX callback?? -// dwt_setrxtimeout((uint16)inst->fwtoTime_sy); //TODO fix this! remove *2 - //turn on RX after sending INF_SUG dwt_setrxtimeout((uint16)0);//no timeout (keep RX on until instructed otherwise) dwt_setrxaftertxdelay((uint32)0);//immediately go to rx after tx } else { -// inst->nextState = TA_TXPOLL_WAIT_SEND; - inst->nextState = TA_RXE_WAIT;//TODO remember this is for testing INF as last message + inst->nextState = TA_RXE_WAIT; } - if(fcode == RTLS_DEMO_MSG_INF_INIT || fcode == RTLS_DEMO_MSG_INF_SUG || fcode == RTLS_DEMO_MSG_INF_UPDATE) @@ -841,12 +708,8 @@ int testapprun(instance_data_t *inst, struct TDMAHandler *tdma_handler, int mess memcpy(&inst->inf_msg.messageData[FCODE], &fcode, sizeof(uint8)); } -// if(tdma_handler->rebase_pending == TRUE) -// { -// tdma_handler->rebase_tx = TRUE; -// } - inst->timeofTx = portGetTickCnt(); + inst->txDoneTimeoutDuration = 200; //NOTE timeout duration found experimentally, may need to be changed if the delays in instance.h are modified inst->testAppState = TA_TX_WAIT_CONF ; //TODO should only do this if we don't have a problem with the INF send... inst->previousState = TA_TXINF_WAIT_SEND ; done = INST_DONE_WAIT_FOR_NEXT_EVENT; //will use RX FWTO to time out (set below) @@ -856,7 +719,6 @@ int testapprun(instance_data_t *inst, struct TDMAHandler *tdma_handler, int mess { if(fcode == RTLS_DEMO_MSG_INF_SUG) { - //TODO think of a way to keep trying to send INF_SUG tdma_handler->set_discovery_mode(tdma_handler, WAIT_SEND_SUG, portGetTickCnt()); inst->nextState = TA_RXE_WAIT; } @@ -881,8 +743,6 @@ int testapprun(instance_data_t *inst, struct TDMAHandler *tdma_handler, int mess psduLength = TAG_POLL_MSG_LEN + FRAME_CRTL_AND_ADDRESS_S + FRAME_CRC; #endif -// Sleep(2); - //set the delayed rx on time (the response message will be sent after this delay) dwt_setrxaftertxdelay(inst->txToRxDelayTag_sy*0); //TODO fix this! remove *0 dwt_setrxtimeout((uint16)inst->fwtoTime_sy*2); //TODO fix this! remove *2 @@ -905,29 +765,8 @@ int testapprun(instance_data_t *inst, struct TDMAHandler *tdma_handler, int mess done = INST_DONE_WAIT_FOR_NEXT_EVENT; //will use RX FWTO to time out (set below) inst->range_start = portGetTickCnt(); - - -// //test between 0CD6 and 118C -// uint16 source_addr = 0x0CD6; -// uint16 my_addr = inst->uwbShortAdd; -// if(memcmp(&my_addr, &source_addr, sizeof(uint16))==0) -// { -// -// -// -// uint16 dest_addr = (uint16)instance_get_uwbaddr(inst->uwbToRangeWith); -// uint16 test_addr = 0x118C; -// if(memcmp(&test_addr, &dest_addr, sizeof(uint16))==0) -// { -// uint8 debug_msg[100]; -// int n = sprintf((char *)&debug_msg, "range_start,xxxx,xxxx"); -// send_usbmessage(&debug_msg[0], n); -// usb_run(); -// } -// } - - inst->timeofTx = portGetTickCnt(); + inst->txDoneTimeoutDuration = 200; //NOTE timeout duration found experimentally, may need to be changed if the delays in instance.h are modified break; } @@ -960,42 +799,13 @@ int testapprun(instance_data_t *inst, struct TDMAHandler *tdma_handler, int mess if(instancesendpacket(psduLength, DWT_START_TX_DELAYED | inst->wait4ack, inst->delayedReplyTime)) { -// // initiate the re-transmission -// inst->testAppState = TA_TXE_WAIT ; -// //inst->nextState = TA_TXPOLL_WAIT_SEND ; //TODO should this go to TX_SELECT instead? -//// inst->testAppState = TA_TX_SELECT; //TODO reset next and previous? -// inst->nextState = TA_TX_SELECT; //TODO reset next and previous? + inst->previousState = TA_INIT; + inst->nextState = TA_INIT; inst->testAppState = TA_RXE_WAIT; inst->wait4ack = 0; //clear the flag as the TX has failed the TRX is off inst->lateTX++; - -// uint8 debug_msg[100]; -// sprintf((char *)&debug_msg, "RTLS_DEMO_MSG_TAG_FINAL->late tx,%04X,xxxx",inst->uwbShortAdd); -// send_txmsgtousb((char *)&debug_msg); -// usb_run(); -// -// uint32 dt = 0; -// -// uint32 dwt_time_now = dwt_readsystimestamphi32(); -// -// if(inst->delayedReplyTime > dwt_time_now) -// { -// dt = dwt_getdt(dwt_time_now, inst->delayedReplyTime); -// -// } -// else -// { -// dt = dwt_getdt(inst->delayedReplyTime, dwt_time_now); -// } -// -// double dtf = convertdevicetimetosec(dt); -// int n = sprintf((char*)&debug_msg[0], "time_now: %lu, replytime: %lu, dts: %f", dwt_time_now, inst->delayedReplyTime, dtf); -// send_usbmessage(&debug_msg[0], n); -// usb_run(); - - } else { @@ -1005,38 +815,14 @@ int testapprun(instance_data_t *inst, struct TDMAHandler *tdma_handler, int mess done = INST_DONE_WAIT_FOR_NEXT_EVENT; //will use RX FWTO to time out (set below) inst->timeofTx = portGetTickCnt(); + inst->txDoneTimeoutDuration = 200; //NOTE timeout duration found experimentally, may need to be changed if the delays in instance.h are modified inst->monitor = 1; - -// uint8 debug_msg[100]; -// sprintf((char *)&debug_msg, "RTLS_DEMO_MSG_TAG_FINAL -> success!"); -// send_txmsgtousb((char *)&debug_msg); -// usb_run(); } break; } case TA_TXREPORT_WAIT_SEND : { - //set the delayed rx on time (the response message will be sent after this delay) -// dwt_setrxaftertxdelay(inst->txToRxDelayTag_sy*0); //TODO fix this! remove *0 -// dwt_setrxtimeout((uint16)inst->fwtoTime_sy*2); //TODO fix this! remove *2 -// dwt_setrxaftertxdelay((uint16)0); -// dwt_setrxtimeout((uint16)0); - -// dwt_writetxfctrl(psduLength, 0, 1); -// if(dwt_starttx(DWT_START_TX_IMMEDIATE) == 0){ -// // uint8 debug_msg[100]; -// // int n = sprintf((char *)&debug_msg, "TX_INF,%llX, psdu: %d ", instance_get_addr(), psduLength); -// // send_usbmessage(&debug_msg[0], n); -// // usb_run(); -// } -// -// inst->testAppState = TA_TX_WAIT_CONF ; // wait confirmation -// inst->previousState = TA_TXINF_WAIT_SEND ; -// done = INST_DONE_WAIT_FOR_NEXT_EVENT; //will use RX FWTO to time out (set below) -// -// inst->timeofTx = portGetTickCnt(); - int psduLength = 0; inst->report_msg.seqNum = inst->frameSN++; @@ -1047,12 +833,8 @@ int testapprun(instance_data_t *inst, struct TDMAHandler *tdma_handler, int mess psduLength = RNG_REPORT_MSG_LEN_SHORT + FRAME_CRTL_AND_ADDRESS_S + FRAME_CRC; #endif - //TODO if tof not zero, set tof, else memset 0 // Write calculated TOF into response message - //TODO #define messageData offsets -// memcpy(&inst->msg[inst->uwbToRangeWith].messageData[1], &inst->tof[inst->uwbToRangeWith], 6); //TODO fix number of bytes... -// dwt_writetxdata(psduLength, (uint8 *) &inst->msg[inst->uwbToRangeWith], 0) ; // write the frame data - memcpy(&inst->report_msg.messageData[REPORT_TOF], &inst->tof[inst->uwbToRangeWith], 6); //TODO fix number of bytes... + memcpy(&inst->report_msg.messageData[REPORT_TOF], &inst->tof[inst->uwbToRangeWith], 6); memcpy(&inst->report_msg.messageData[REPORT_ADDR], &inst->uwbList[inst->uwbToRangeWith], inst->addrByteSize); dwt_writetxdata(psduLength, (uint8 *) &inst->report_msg, 0) ; // write the frame data @@ -1064,16 +846,12 @@ int testapprun(instance_data_t *inst, struct TDMAHandler *tdma_handler, int mess } else { -// uint8 debug_msg[100]; -// int n = sprintf((char *)&debug_msg, "TX_RNG_REPORT,xxxx,%llX",instance_get_addr()); -// send_usbmessage(&debug_msg[0], n); -// usb_run(); - inst->testAppState = TA_TX_WAIT_CONF; // wait confirmation inst->previousState = TA_TXREPORT_WAIT_SEND; done = INST_DONE_WAIT_FOR_NEXT_EVENT; //will use RX FWTO to time out (set below) inst->timeofTx = portGetTickCnt(); + inst->txDoneTimeoutDuration = 200; //NOTE timeout duration found experimentally, may need to be changed if the delays in instance.h are modified inst->monitor = 1; } @@ -1081,8 +859,6 @@ int testapprun(instance_data_t *inst, struct TDMAHandler *tdma_handler, int mess } case TA_TX_WAIT_CONF : { - //TODO look at this state again. it seems to sometimes be problematic - event_data_t* dw_event = instance_getevent(11); //get and clear this event //NOTE: Can get the ACK before the TX confirm event for the frame requesting the ACK @@ -1092,49 +868,19 @@ int testapprun(instance_data_t *inst, struct TDMAHandler *tdma_handler, int mess { if(dw_event->type == DWT_SIG_RX_TIMEOUT) //got RX timeout - i.e. did not get the response (e.g. ACK) { - //we need to wait for SIG_TX_DONE and then process the timeout and re-send the frame if needed -// uint8 debug_msg[100]; -// int n = sprintf((char *)&debug_msg, "DWT_SIG_RX_TIMEOUT"); -// send_usbmessage(&debug_msg[0], n); -// usb_run(); inst->gotTO = 1; } - done = INST_DONE_WAIT_FOR_NEXT_EVENT; - - //TODO maybe move out of this if statement??? //sometimes the DW1000 tx callback (TXFRS) fails to trigger and the the SYS_STATE register //reads IDLE for for PMSC, RX, and TX so we need another way to timeout since RX FWTO won't be triggered. -// uint32 dt = get_dt32(inst->timeofTx, portGetTickCnt()); + uint32 dt = get_dt32(inst->timeofTx, portGetTickCnt()); -// if( inst->previousState == TA_TXFINAL_WAIT_SEND || -// inst->previousState == TA_TXPOLL_WAIT_SEND || -// inst->previousState == TA_TXRESPONSE_WAIT_SEND || -// inst->previousState == TA_TXINF_WAIT_SEND) -// { -// //NOTE timeout duration found experimentally, may need to be changed if the delays in instance.h are modified -// if(dt > 200) -// { -// inst->gotTO = 1; -// //inst_processtxrxtimeout(inst); -// } -// } -// else if(inst->previousState == TA_TXBLINK_WAIT_SEND){ //TODO put this back above??? -// if(dt > BLINK_SLEEP_DELAY + 20) -// { -// inst->gotTO = 1; -// //inst_processtxrxtimeout(inst); -// -// } -// } -// else if(inst->previousState == TA_TXRANGINGINIT_WAIT_SEND) -// { -// if(dt > RNG_INIT_REPLY_DLY_MS + 20) -// { -// //inst_processtxrxtimeout(inst); -// inst->gotTO = 1; -// } -// } + if(dt > inst->txDoneTimeoutDuration) //duration set at time of tx + { + inst->gotTO = 1; + } + + done = INST_DONE_WAIT_FOR_NEXT_EVENT; if(inst->gotTO == 0) { @@ -1147,19 +893,6 @@ int testapprun(instance_data_t *inst, struct TDMAHandler *tdma_handler, int mess if (inst->gotTO) //timeout { -// uint32 mask = dwt_read32bitreg(SYS_MASK_ID); - - -// uint8 debug_msg[100]; -// int n = sprintf((char *)&debug_msg, "inst->gotTO, SYS_MASK: %lu", mask); -// send_usbmessage(&debug_msg[0], n); -// usb_run(); - // send_txmsgtousb("(2nd) got TO in TA_TX_WAIT_CONF"); - -// uint8 debug_msg[100]; -// int n = sprintf((char *)&debug_msg, "inst_processtxrxtimeout(inst) after inst->gotTO"); -// send_usbmessage(&debug_msg[0], n); -// usb_run(); inst_processtxrxtimeout(inst); inst->gotTO = 0; inst->wait4ack = 0 ; //clear this @@ -1197,20 +930,12 @@ int testapprun(instance_data_t *inst, struct TDMAHandler *tdma_handler, int mess tagCalculatedFinalTxTime = tagCalculatedFinalTxTime + inst->txAntennaDelay; tagCalculatedFinalTxTime &= MASK_40BIT; -// // Write Calculated TX time field of Final message -// memcpy(&(inst->msg[inst->uwbToRangeWith].messageData[FTXT]), (uint8 *)&tagCalculatedFinalTxTime, 5); -// // Write Poll TX time field of Final message -// memcpy(&(inst->msg[inst->uwbToRangeWith].messageData[PTXT]), (uint8 *)&inst->txu.tagPollTxTime, 5); // Write Calculated TX time field of Final message memcpy(&(inst->msg.messageData[FTXT]), (uint8 *)&tagCalculatedFinalTxTime, 5); // Write Poll TX time field of Final message memcpy(&(inst->msg.messageData[PTXT]), (uint8 *)&inst->txu.tagPollTxTime, 5); } -// else if(inst->previousState == TA_TXRANGINGINIT_WAIT_SEND) -// { -// inst->mode = ANCHOR; -// } inst->testAppState = TA_RXE_WAIT ; // After sending, tag expects response/report, anchor waits to receive a final/new poll @@ -1220,20 +945,9 @@ int testapprun(instance_data_t *inst, struct TDMAHandler *tdma_handler, int mess }// end case TA_TX_WAIT_CONF case TA_RXE_WAIT : { - // if (inst->mode == ANCHOR) - // { - // send_statetousb(inst); - // } - if(inst->wait4ack == 0) //if this is set the RX will turn on automatically after TX { //turn RX on - -// uint8 debug_msg[100]; -// int n = sprintf((char*)&debug_msg[0], "instancerxon called from case TA_RXE_WAIT :"); -// send_usbmessage(&debug_msg[0], n); -// usb_run(); - instancerxon(inst, 0, 0) ; // turn RX on, without delay } else @@ -1247,18 +961,6 @@ int testapprun(instance_data_t *inst, struct TDMAHandler *tdma_handler, int mess inst->testAppState = TA_RX_WAIT_DATA; // let this state handle it inst->rxCheckOnTime = portGetTickCnt() + RX_CHECK_ON_PERIOD; - -// //see if tx is actually enabled -// //read SYS_STATE, getting second byte -// uint8 regval = dwt_read8bitoffsetreg(SYS_STATE_ID,1); -// //get the first 5 bytes -// regval &= 0x1F; -// uint8 debug_msg[200]; -// int n = sprintf((char *)&debug_msg, "RX STATUS: regval %u", regval); -// send_usbmessage(&debug_msg[0], n); -// usb_run(); - - // 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) { @@ -1274,14 +976,11 @@ int testapprun(instance_data_t *inst, struct TDMAHandler *tdma_handler, int mess { event_data_t* dw_event = instance_getevent(12); //get and clear this event -// if(inst->mode == ANCHOR) if(inst->mode == DISCOVERY) { - //inst->mode = ANCHOR; //TODO instead switch to anchor after sending out the ANCH_RESP - - inst->canPrintInfo = 1; + inst->canPrintInfo = 1; - //TODO add a small random number to this to reduce chance of collisions + //add a small random number to this to reduce chance of collisions inst->delayedReplyTime = (dw_event->timeStamp + inst->rnginitReplyDelay + (uint32)rand()%RANGE_INIT_RAND) >> 8 ; // time we should send the blink response //set destination address @@ -1342,66 +1041,21 @@ int testapprun(instance_data_t *inst, struct TDMAHandler *tdma_handler, int mess { //NOTE: WAIT_RNG_INIT checked in RX callback -// send_rxmsgtousb("RX process: DWT_SIG_RX_OKAY-RTLS_DEMO_MSG_RNG_INIT"); uint8 debug_msg[100]; -// int n = sprintf((char *)&debug_msg, "BLINK_COMPLETE,%llX,%llX", instance_get_addr(), instance_get_uwbaddr(inst->uwbToRangeWith)); int n = sprintf((char *)&debug_msg, "BLINK_COMPLETE,%04llX,%04llX", instance_get_addr(), instance_get_uwbaddr(inst->uwbToRangeWith)); send_usbmessage(&debug_msg[0], n); usb_run(); -// uint32 final_reply_delay_us; -// uint32 resp_dly[RESP_DLY_NB]; -// int i; - tdma_handler->build_new_network(tdma_handler); - //build the initial TDMA -// tdma_handler->uwbFrameStartTimes[0] = portGetTickCnt() - tdma_handler->slotDuration;//TODO handle timer wrapping... -// tdma_handler->lastSlotStartTime = portGetTickCnt(); -// tdma_handler->uwbListTDMAInfo[0].framelength = (uint8)MIN_FRAMELENGTH; -// -// -// //todo: make this a handler function -// //new -// tdma_handler->assign_slot(&tdma_handler->uwbListTDMAInfo[0], 1); -// tdma_handler->assign_slot(&tdma_handler->uwbListTDMAInfo[inst->uwbToRangeWith], 2); -//// tdma_handler->uwblist_assign_slot(tdma_handler, 0, 2); -// tdma_handler->assign_slot(&tdma_handler->uwbListTDMAInfo[0], 3); - - //TODO build slotAssignment array - -// bool assigned0 = tdma_handler->slot_assigned(tdma_handler, 0); -// bool assigned1 = tdma_handler->slot_assigned(tdma_handler, 1); -// bool assigned2 = tdma_handler->slot_assigned(tdma_handler, 2); -// bool assigned3 = tdma_handler->slot_assigned(tdma_handler, 3); -// -// uint8 debug_msg[100]; -// int n = sprintf((char*)&debug_msg[0], "ass0: %u, ass1: %u, ass2: %u, ass3: %u,", assigned0, assigned1, assigned2, assigned3); -// send_usbmessage(&debug_msg[0], n); -// usb_run(); - - -// tdma_handler->usb_dump_tdma(tdma_handler); - -// inst->inf_msg.messageData[FCODE] = RTLS_DEMO_MSG_INF_INIT; tdma_handler->populate_inf_msg(tdma_handler, RTLS_DEMO_MSG_INF_INIT); uint32 time_now = portGetTickCnt(); tdma_handler->set_discovery_mode(tdma_handler, EXIT, time_now); - inst->buildFrameTime = time_now; -// n = sprintf((char *)&debug_msg, "BUILD FRAME,time_now: %lu, framestart: %lu, slotduration: %lu", portGetTickCnt(), tdma_handler->frameStartTime, tdma_handler->slotDuration); -// send_usbmessage(&debug_msg[0], n); -// usb_run(); - -// inst->testAppState = TA_TXE_WAIT; inst->testAppState = TA_TX_SELECT; -// inst->testAppState = TA_TXINF_WAIT_SEND; -// inst->nextState = TA_TXPOLL_WAIT_SEND ; // send next poll -// inst->nextState = TA_TXINF_WAIT_SEND ; // send next poll - inst->mode = TAG; - //inst->responseTimeouts = 0; //reset timeout count + inst->mode = TAG; inst->goToSleep = 0; //don't go to sleep - start ranging instead and then sleep after 1 range is done or poll times out inst->instanceTimerTimeSaved = inst->instanceTimerTime = portGetTickCnt(); //set timer base @@ -1461,11 +1115,11 @@ int testapprun(instance_data_t *inst, struct TDMAHandler *tdma_handler, int mess if(tdma_handler->discovery_mode == WAIT_INF_REG) //treat INF_UPDATE and INF_SUG the same { - //synchronize the frames //TODO don't need to sync frame at this point...? + //synchronize the frames tdma_handler->frame_sync(tdma_handler, dw_event, framelength, timeSinceFrameStart_us, srcIndex, FS_ADOPT); -// //initialize collection of tdma info, clear any previously stored info + //initialize collection of tdma info, clear any previously stored info tdma_handler->process_inf_msg(tdma_handler, messageData, srcIndex, CLEAR_ALL_COPY); -// //set discovery mode to COLLECT_INF_REG + //set discovery mode to COLLECT_INF_REG tdma_handler->set_discovery_mode(tdma_handler, COLLECT_INF_REG, time_now); } else if(tdma_handler->discovery_mode == COLLECT_INF_REG) @@ -1484,8 +1138,6 @@ int testapprun(instance_data_t *inst, struct TDMAHandler *tdma_handler, int mess } else if(inst->mode == ANCHOR || inst->mode == TAG) { - //TODO in here (or in RX callback) I have to think about when to adopt the new TDMA and also what INF messages to accept or reject, if a modified TDMA exists but we havent had a chance to TX it in an INF_UPDATE yet. - //if we are a TAG or ANCHOR //1.) sync our frame start time to the local network //2.) check for and adopt any tdma changes, sending an INF_UPDATE or INF_REG accordingly @@ -1499,7 +1151,7 @@ int testapprun(instance_data_t *inst, struct TDMAHandler *tdma_handler, int mess if(tdma_modified) { //only repopulate the INF message if there was a modification to the TDMA configuration - tdma_handler->populate_inf_msg(tdma_handler, RTLS_DEMO_MSG_INF_UPDATE);//TODO populate at slot start while waiting for buffer to expire + tdma_handler->populate_inf_msg(tdma_handler, RTLS_DEMO_MSG_INF_UPDATE); } } @@ -1513,11 +1165,6 @@ int testapprun(instance_data_t *inst, struct TDMAHandler *tdma_handler, int mess //NOTE: discovery mode WAIT_INF_INIT checked in RX callback //process the INF packet -// uint8 debug_msg[100]; -// int n = sprintf((char*)&debug_msg[0], "process RTLS_DEMO_MSG_INF :"); -// send_usbmessage(&debug_msg[0], n); -// usb_run(); - uint32 time_now = portGetTickCnt(); uint8 srcIndex = instgetuwblistindex(inst, &srcAddr[0], inst->addrByteSize); @@ -1638,7 +1285,7 @@ int testapprun(instance_data_t *inst, struct TDMAHandler *tdma_handler, int mess if(reportTOF(inst, inst->newRangeUWBIndex)==0) { inst->newRange = 1; - inst->ranging = 1; +// inst->ranging = 1; inst->canPrintInfo = 1; } } @@ -1649,37 +1296,13 @@ int testapprun(instance_data_t *inst, struct TDMAHandler *tdma_handler, int mess inst->newRangeTagAddress = instance_get_uwbaddr(inst->uwbToRangeWith); inst->newRangeAncAddress = instance_get_addr(); - - -// char debug_msg[100]; -// n = sprintf((char*)&debug_msg[0], "TAG_FINAL %i", *node->index); -// send_usbmessage(&debug_msg[0], n); -// usb_run(); inst->testAppState = TA_TXREPORT_WAIT_SEND; - //TODO this changes! we need to send RNG_REPORT -// inst->testAppState = TA_RXE_WAIT ; -// inst->previousState = TA_INIT; -// inst->nextState = TA_INIT; -// inst->uwbToRangeWith = 255; -// -// dwt_setrxaftertxdelay(0); -// instancesetantennadelays(); //this will update the antenna delay if it has changed - - - //char debug_msg[100]; -// n = sprintf((char*)&debug_msg[0], "TAG_FINAL %d", inst->uwbToRangeWith); -// send_usbmessage(&debug_msg[0], n); -// usb_run(); - - break; } //RTLS_DEMO_MSG_TAG_FINAL case RTLS_DEMO_MSG_RNG_REPORT: { -// send_rxmsgtousb("RX process: DWT_SIG_RX_OKAY-RTLS_DEMO_MSG_RNG_REPORT "); - uint8 tag_index = instgetuwblistindex(inst, &messageData[REPORT_ADDR], inst->addrByteSize); uint8 anchor_index = instgetuwblistindex(inst, &srcAddr[0], inst->addrByteSize); @@ -1690,7 +1313,6 @@ int testapprun(instance_data_t *inst, struct TDMAHandler *tdma_handler, int mess inst->tof[anchor_index] = 0; //copy previously calculated ToF - //TODO #define messageData offsets memcpy(&inst->tof[anchor_index], &messageData[REPORT_TOF], 6); inst->newRangeAncAddress = instance_get_uwbaddr(anchor_index); @@ -1702,7 +1324,7 @@ int testapprun(instance_data_t *inst, struct TDMAHandler *tdma_handler, int mess if(reportTOF(inst, inst->newRangeUWBIndex)==0) { inst->newRange = 1; - inst->ranging = 1; +// inst->ranging = 1; inst->canPrintInfo = 1; } } @@ -1740,10 +1362,6 @@ int testapprun(instance_data_t *inst, struct TDMAHandler *tdma_handler, int mess } //RTLS_DEMO_MSG_RNG_REPORT default: { -// if(inst->mode == ANCHOR) -// { -// send_rxmsgtousb("RX process: DWT_SIG_RX_OKAY-default "); -// } inst->testAppState = TA_RXE_WAIT ; // wait for next frame dwt_setrxaftertxdelay(0); @@ -1758,36 +1376,21 @@ int testapprun(instance_data_t *inst, struct TDMAHandler *tdma_handler, int mess { if(tdma_handler->discovery_mode == WAIT_RNG_INIT || tdma_handler->discovery_mode == WAIT_INF_INIT) { -// tdma_handler->discovery_mode = WAIT_INF_REG; uint32 time_now = portGetTickCnt(); tdma_handler->set_discovery_mode(tdma_handler, WAIT_INF_REG, time_now); } - -// send_txmsgtousb("RX process: DWT_SIG_RX_TIMEOUT "); + //TODO remove if/else chain below int n; if(inst->previousState == TA_TXBLINK_WAIT_SEND) { -// if(tdma_handler->discovery_mode == WAIT_RNG_INIT) -// { -// tdma_handler->discovery_mode = WAIT_INF_REG; -// } -// tdma_handler->waitForRngInit = FALSE; - - uint8 debug_msg[100]; -// n = sprintf((char *)&debug_msg, "TX_BLINK_TIMEOUT,%llX,NULL", instance_get_addr()); n = sprintf((char *)&debug_msg, "TX_BLINK_TIMEOUT,%04llX,NULL", instance_get_addr()); send_usbmessage(&debug_msg[0], n); usb_run(); } else if(inst->previousState == TA_TXRANGINGINIT_WAIT_SEND) { -// if(tdma_handler->discovery_mode == WAIT_INF_INIT) -// { -// tdma_handler->discovery_mode = WAIT_INF_REG; -// } -// tdma_handler->waitForInf = FALSE; } else if(inst->previousState == TA_TXINF_WAIT_SEND) { @@ -1824,7 +1427,6 @@ int testapprun(instance_data_t *inst, struct TDMAHandler *tdma_handler, int mess case 0: default: { - //TODO maybe also check the events to see if nothing is upcoming that will handle this for us! //check if RX is on every so often. Turn it on if it isn't. uint32 time_now = portGetTickCnt(); if(time_now >= inst->rxCheckOnTime) @@ -1838,10 +1440,6 @@ int testapprun(instance_data_t *inst, struct TDMAHandler *tdma_handler, int mess if(regval == 0){//RX IDLE dwt_forcetrxoff(); instancerxon(inst, 0, 0); -// uint8 debug_msg[200]; -// int n = sprintf((char *)&debug_msg, "RX IDLE, RESET: regval %u", regval); -// send_usbmessage(&debug_msg[0], n); -// usb_run(); } } @@ -2028,7 +1626,7 @@ void instance_init_timings(void) resp_dly_us = ANC_TURN_AROUND_TIME_US + inst->frameLengths_us[POLL]; resp_dly = ((RESP_DLY_UNIT_US << RESP_DLY_UNIT_SHIFT) & RESP_DLY_UNIT_MASK) + ((resp_dly_us << RESP_DLY_VAL_SHIFT) & RESP_DLY_VAL_MASK); - inst->resp_dly[RESP_DLY_ANC] = resp_dly & 0xFFFF; //TODO make sure that we only want the 16 lowest bits + inst->resp_dly[RESP_DLY_ANC] = resp_dly & 0xFFFF; // Second response delay to send is tag's response delay. @@ -2036,7 +1634,7 @@ void instance_init_timings(void) resp_dly_us = TAG_TURN_AROUND_TIME_US + inst->frameLengths_us[RESP]; resp_dly = ((RESP_DLY_UNIT_US << RESP_DLY_UNIT_SHIFT) & RESP_DLY_UNIT_MASK) + ((resp_dly_us << RESP_DLY_VAL_SHIFT) & RESP_DLY_VAL_MASK); - inst->resp_dly[RESP_DLY_TAG] = resp_dly & 0xFFFF; //TODO make sure that we only want the 16 lowest bits + inst->resp_dly[RESP_DLY_TAG] = resp_dly & 0xFFFF; for (int i = 0; i < RESP_DLY_NB; i++) @@ -2112,23 +1710,7 @@ uint32 instance_getmessageduration_us(int data_length_bytes) uint64 instance_get_addr(void) //get own address { - - //TODO should this instead operate on uwbList[0]??? - - instance_data_t* inst = instance_get_local_structure_ptr(0); - uint64 x = 0; - x |= (uint64) inst->eui64[0]; - x |= (uint64) inst->eui64[1] << 8; -#if (USING_64BIT_ADDR == 1) - x |= (uint64) inst->eui64[2] << 16; - x |= (uint64) inst->eui64[3] << 24; - x |= (uint64) inst->eui64[4] << 32; - x |= (uint64) inst->eui64[5] << 40; - x |= (uint64) inst->eui64[6] << 48; - x |= (uint64) inst->eui64[7] << 56; -#endif - - return (x); + return instance_get_uwbaddr(0); } uint64 instance_get_uwbaddr(uint8 uwb_index) //get uwb address by index @@ -2175,7 +1757,7 @@ uint32 get_dt32(uint32 t1, uint32 t2) { return t2 - t1; } - else//TODO maybe rework this... should should instead check if one number is greater than a threshold... somehow spit out error if used incorrectly??? + else { //handle timestamp roleover return 4294967295 - t1 + t2; @@ -2218,34 +1800,17 @@ uint64 get_dt64(uint64 t1, uint64 t2) { return t2 - t1; } - else//TODO maybe rework this... should should instead check if one number is greater than a threshold... somehow spit out error if used incorrectly??? + else { - //handle timestamp roleover + //handle timestamp rollover return 4294967295999 - t1 + t2; } } -//TODO remove -//get the time difference between two between two 16-bit unsigned timestamps -//t1 is the first timestamp -//t2 is the second timetamp that occured after t1 -//uint16 get_dt16(uint16 t1, uint16 t2) -//{ -// if(t2 >= t1) -// { -// return t2 - t1; -// } -// else//TODO maybe rework this... should should instead check if one number is greater than a threshold... somehow spit out error if used incorrectly??? -// { -// //handle timestamp roleover -// return 0xFFFF - t1 + t2; -// } -//} - //add a duration to a 64 bit timestamp. This function handles number wrapping uint64 timestamp_add64(uint64 timestamp, uint64 duration) { - uint32 to_wrap = (uint64)4294967295999 - timestamp; + uint64 to_wrap = (uint64)4294967295999 - timestamp; if(duration > to_wrap) { return to_wrap + duration; diff --git a/src/application/instance.h b/src/application/instance.h index d827e63..49e676e 100644 --- a/src/application/instance.h +++ b/src/application/instance.h @@ -25,7 +25,7 @@ extern "C" { #include "tdma_handler.h" -typedef struct +typedef struct //TODO go through and see what can be removed { INST_MODE mode; //instance mode (tag or anchor) INST_STATES testAppState ; //state machine - current state @@ -72,18 +72,17 @@ typedef struct //message structures used for transmitted messages #if (USING_64BIT_ADDR == 1) - srd_msg_dlsl rng_initmsg ; // ranging init message (destination long, source long) - srd_ext_msg_dlsl msg; // simple 802.15.4 frame structure (used for tx message) - using long addresses - srd_ext_msg_dssl inf_msg; // extended inf message containing frame lengths and slot assignments - srd_ext_msg_dssl report_msg; // extended report message containing the calculated range - srd_ext_msg_dssl sync_msg; // extended message indicating the need to resync TDMA frame - //TODO make sure the dssl are correct! (ds for broadcast?) + srd_msg_dlsl rng_initmsg ; // ranging init message (destination long, source long) + srd_ext_msg_dlsl msg; // simple 802.15.4 frame structure (used for tx message) - using long addresses + srd_ext_msg_dssl inf_msg; // extended inf message containing frame lengths and slot assignments + srd_ext_msg_dssl report_msg; // extended report message containing the calculated range + srd_ext_msg_dssl sync_msg; // extended message indicating the need to resync TDMA frame #else - srd_msg_dlss rng_initmsg ; // ranging init message (destination long, source short) - srd_ext_msg_dsss msg; // simple 802.15.4 frame structure (used for tx message) - using short addresses - srd_ext_msg_dsss inf_msg; // extended inf message containing frame lengths and slot assignments - srd_ext_msg_dsss report_msg; // extended report message containing the calculated range - srd_ext_msg_dsss sync_msg; // extended message indicating the need to resync TDMA frame + srd_msg_dlss rng_initmsg ; // ranging init message (destination long, source short) + srd_ext_msg_dsss msg; // simple 802.15.4 frame structure (used for tx message) - using short addresses + srd_ext_msg_dsss inf_msg; // extended inf message containing frame lengths and slot assignments + srd_ext_msg_dsss report_msg; // extended report message containing the calculated range + srd_ext_msg_dsss sync_msg; // extended message indicating the need to resync TDMA frame #endif iso_IEEE_EUI64_blink_msg blinkmsg ; // frame structure (used for tx blink message) @@ -127,13 +126,6 @@ typedef struct int lateTX; int lateRX; -// double adist[RTD_MED_SZ] ; -// double adist4[4] ; -// double longTermRangeSum ; -// int longTermRangeCount ; -// int tofIndex ; -// int tofCount ; - uint8 newRangeUWBIndex; //index for most recent ranging exchange int newRange; uint64 newRangeAncAddress; //anchor address for most recent ranging exchange @@ -151,19 +143,6 @@ typedef struct uint8 uwbListLen ; uint8 uwbList[UWB_LIST_SIZE][8]; //index 0 reserved for self, rest for other tracked uwbs -// uint8 uwbListType[UWB_LIST_SIZE]; //UWB_LIST_SELF, UWB_LIST_NEIGHBOR, UWB_LIST_HIDDEN, UWB_LIST_INACTIVE - -// uint8 uwbNumActive[UWB_LIST_SIZE]; //number of TAGs each tracked ANCHOR is actively ranging with. //TODO remove? - - // keep track of when final messages so we can drop uwbs that we haven't communicated with in a while -// uint32 lastCommTimeStamp[UWB_LIST_SIZE]; //TODO move into tdma_handler? -// uint32 lastHiddenTimeStamp[UWB_LIST_SIZE]; -// uint32 lastTwiceHiddenTimeStamp[UWB_LIST_SIZE]; -// uint8 uwbTimeout[UWB_LIST_SIZE] ; //TODO remove and use list type instead - -// uint32 lastRangeTimeStamp[UWB_LIST_SIZE]; - -// uint8 time_till_next_reported[UWB_LIST_SIZE]; //used to keep track of whether we reported the RX_ACCEPT node. 0 if no, 1 if yes. uint32 blink_start; uint32 range_start; @@ -178,19 +157,12 @@ typedef struct uint8 dweventIdxIn; uint8 dweventPeek; uint8 monitor; - uint32 timeofTx; + uint32 timeofTx; //TODO make sure this is set for every single TX + uint32 txDoneTimeoutDuration; uint8 smartPowerEn; - uint32 currentStateStartTime; - INST_STATES lastState; - uint32 rxCheckOnTime; - uint32 buildFrameTime; - - uint8 ranging; - - uint64 testTimer; uint16 timerCounter; @@ -207,7 +179,6 @@ int reportTOF(instance_data_t *inst, uint8 uwb_index); // clear the status/ranging data void instanceclearcounts(void) ; void instclearuwblist(void); -// void instsetuwbtorangewith(int uwbID); int instaddactivateuwbinlist(instance_data_t *inst, uint8 *uwbAddr); int instcheckactiveuwbinlist(instance_data_t *inst, uint8 *uwbAddr); int instfindfirstactiveuwbinlist(instance_data_t *inst, uint8 startindex); @@ -215,7 +186,6 @@ int instfindnumactiveuwbinlist(instance_data_t *inst); int instfindnumneighbors(instance_data_t *inst); int instfindnumhidden(instance_data_t *inst); int instgetuwblistindex(instance_data_t *inst, uint8 *uwbAddr, uint8 addrByteSize); -void instremoveuwb(instance_data_t *inst, uint8 uwb_index); void instance_readaccumulatordata(void); diff --git a/src/application/instance_common.c b/src/application/instance_common.c index 3874e13..a2f1522 100644 --- a/src/application/instance_common.c +++ b/src/application/instance_common.c @@ -148,18 +148,9 @@ int reportTOF(instance_data_t *inst, uint8 uwb_index) int64 tofi ; // check for negative results and accept them making them proper negative integers - tofi = inst->tof[uwb_index] ; // make it signed + tofi = inst->tof[uwb_index] ; // make it signed -// memcpy(&instance_data[instance].msg[uwb_index].messageData[TOFR], &instance_data[instance].tof[uwb_index], sizeof(int64)); - -// int64 mtof = 0; -// memcpy(&mtof, &tofi, sizeof(int64)); -// uint8 debug_msg[100]; -// int n = sprintf((char*)&debug_msg[0], "reported TOF: %lld, mtof %lld", tofi, mtof); -// send_usbmessage(&debug_msg[0], n); -// usb_run(); - - if (tofi > 0x007FFFFFFFFF) // MP counter is 40 bits, close up TOF may be negative + if (tofi > 0x007FFFFFFFFF) // MP counter is 40 bits, close up TOF may be negative { tofi -= 0x010000000000 ; // subtract fill 40 bit range to make it negative } @@ -216,76 +207,6 @@ int instgetuwblistindex(instance_data_t *inst, uint8 *uwbAddr, uint8 addrByteSiz char uwbChar[2]; memcpy(&uwbChar[0], &uwbAddr[0], 2); - //TODO remove the following! - bool match = FALSE; - char test_addr[2] = {0x95, 0x15}; - if(memcmp(&uwbChar[0], &test_addr[0], 2) == 0) - { - match = TRUE; - } - test_addr[0] = 0x50; - test_addr[1] = 0x59; - if(memcmp(&uwbChar[0], &test_addr[0], 2) == 0) - { - match = TRUE; - } - test_addr[0] = 0x36; - test_addr[1] = 0x18; - if(memcmp(&uwbChar[0], &test_addr[0], 2) == 0) - { - match = TRUE; - } - test_addr[0] = 0x39; - test_addr[1] = 0x16; - if(memcmp(&uwbChar[0], &test_addr[0], 2) == 0) - { - match = TRUE; - } - test_addr[0] = 0x27; - test_addr[1] = 0x59; - if(memcmp(&uwbChar[0], &test_addr[0], 2) == 0) - { - match = TRUE; - } - test_addr[0] = 0x20; - test_addr[1] = 0x1B; - if(memcmp(&uwbChar[0], &test_addr[0], 2) == 0) - { - match = TRUE; - } - test_addr[0] = 0x8C; - test_addr[1] = 0x11; - if(memcmp(&uwbChar[0], &test_addr[0], 2) == 0) - { - match = TRUE; - } - test_addr[0] = 0x1D; - test_addr[1] = 0x14; - if(memcmp(&uwbChar[0], &test_addr[0], 2) == 0) - { - match = TRUE; - } - test_addr[0] = 0x8C; - test_addr[1] = 0x19; - if(memcmp(&uwbChar[0], &test_addr[0], 2) == 0) - { - match = TRUE; - } - test_addr[0] = 0xD6; - test_addr[1] = 0x0C; - if(memcmp(&uwbChar[0], &test_addr[0], 2) == 0) - { - match = TRUE; - } - - if(match == FALSE) - { - uint8 debug_msg[100]; - int n = sprintf((char*)&debug_msg[0], "match not found"); - send_usbmessage(&debug_msg[0], n); - usb_run(); - } - //add the new UWB to the list, if not already there and there is space for(uint8 i=0; i<UWB_LIST_SIZE; i++) { @@ -307,189 +228,6 @@ int instgetuwblistindex(instance_data_t *inst, uint8 *uwbAddr, uint8 addrByteSiz return 255; } -// ------------------------------------------------------------------------------------------------------------------- -// -// function to remove the UWB at a given index from our list. Take care when calling this inside of a loop iterating over the uwbList! -// -// ------------------------------------------------------------------------------------------------------------------- -// return index of UWB -void instremoveuwb(instance_data_t *inst, uint8 uwb_index) -{ - if(uwb_index > 0 && uwb_index < inst->uwbListLen) - { - if(inst->uwbToRangeWith == uwb_index) - { - uwb_index = 255; - } - else if(inst->uwbToRangeWith > uwb_index && inst->uwbToRangeWith < inst->uwbListLen) - { - inst->uwbToRangeWith--; - } - - if(inst->newRange == 1) - { - if(inst->newRangeUWBIndex == uwb_index) - { - uwb_index = 0; - inst->newRange = 0; - } - else if(inst->newRangeUWBIndex > uwb_index && inst->newRangeUWBIndex < inst->uwbListLen) - { - inst->newRangeUWBIndex--; - } - } - - - for(int i=uwb_index; i<inst->uwbListLen-1; i++) - { - memcpy(&inst->uwbList[i][0], &inst->uwbList[i+1][0], inst->addrByteSize); - inst->tof[i] = inst->tof[i+1]; - inst->idistance[i] = inst->idistance[i+1]; - inst->idistanceraw[i] = inst->idistanceraw[i]; - } - uint8 blank[8] = {0, 0, 0, 0, 0, 0, 0, 0}; - memcpy(&inst->uwbList[inst->uwbListLen-1][0], &blank[0], 8); - inst->tof[inst->uwbListLen-1] = 0; - inst->idistance[inst->uwbListLen-1] = 0; - inst->idistanceraw[inst->uwbListLen-1] = 0; - - //update the tdma_handler before reducing the size of the uwbList - tdma_handler.remove_uwbinfo(&tdma_handler, uwb_index); - - inst->uwbListLen--; - } -} - - -// ------------------------------------------------------------------------------------------------------------------- -//// -//// function to add new UWBs to the uwb list or remove timeout status from UWBs already in uwb list -//// -//// ------------------------------------------------------------------------------------------------------------------- -//// return 1 if UWB added to list or removed from timeout status -//// return 0 if UWB not added list or removed from timeout status -//int instaddactivateuwbinlist(instance_data_t *inst, uint8 *uwbAddr) -//{ -// uint8 i; -// uint8 blank[8] = {0, 0, 0, 0, 0, 0, 0, 0}; -// -// //add the new UWB to the list, if not already there and there is space -// for(i=1; i<UWB_LIST_SIZE; i++)//0 reserved for self. timeout status not used for self -// { -// if(memcmp(&inst->uwbList[i][0], &uwbAddr[0], inst->addrByteSize) != 0) -// { -// if(memcmp(&inst->uwbList[i][0], &blank[0], inst->addrByteSize) == 0) //blank entry -// { -// memcpy(&inst->uwbList[i][0], &uwbAddr[0], inst->addrByteSize) ; -// inst->uwbListLen = i + 1 ; -// inst->uwbToRangeWith = i; -// inst->uwbTimeout[i] = 0; -// return 1; -// } -// } -// else -// { -// if(inst->uwbTimeout[i]) -// { -// //uwb has timed out, wake it up -// inst->uwbToRangeWith = i; -// inst->uwbTimeout[i] = 0; -// return 1; -// } -// else -// { -// //we already have this uwb in the list and it has not timed out -// break; -// } -// } -// } -// -// return 0; -//} - -//// ------------------------------------------------------------------------------------------------------------------- -//// -//// function to check if a UWB is already in our list and not in a timeout status -//// -//// ------------------------------------------------------------------------------------------------------------------- -//// return index if UWB in list and not timed out -//// return 255 if UWB not in list or is but has timed out -//int instcheckactiveuwbinlist(instance_data_t *inst, uint8 *uwbAddr) -//{ -// uint8 i; -// uint8 blank[8] = {0, 0, 0, 0, 0, 0, 0, 0}; -// -// //add the new UWB to the list, if not already there and there is space -// for(i=1; i<UWB_LIST_SIZE; i++)//0 reserved for self, timeout not used for self -// { -// if(memcmp(&inst->uwbList[i][0], &uwbAddr[0], inst->addrByteSize) == 0) -// { -// if(inst->uwbTimeout[i]) -// { -// //UWB in list, but timed out -// break; -// } -// else -// { -// return i; //we already have this UWB in the list -// } -// } -// else -// { -// if(memcmp(&inst->uwbList[i][0], &blank[0], 8) == 0) //blank entry -// { -// break; -// } -// } -// } -// -// return 255; -//} - -//// ------------------------------------------------------------------------------------------------------------------- -//// -//// function to find the first UWB in our list that is not in a timeout status, starting with the given index -//// -//// ------------------------------------------------------------------------------------------------------------------- -//// return index for first UWB in list that is not timed out -//// return 255 if all UWBs in list (from the starting index) are timed out -//int instfindfirstactiveuwbinlist(instance_data_t *inst, uint8 startindex) -//{ -// uint8 i; -// -// for(i=startindex; i<inst->uwbListLen; i++) -// { -// if(!inst->uwbTimeout[i]) -// { -// return i; -// } -// } -// -// return 255; -//} - - - -//// ------------------------------------------------------------------------------------------------------------------- -//// -//// function to find the number of UWBs in our list that are not in a timeout status -//// -//// ------------------------------------------------------------------------------------------------------------------- -//int instfindnumactiveuwbinlist(instance_data_t *inst) -//{ -// uint8 num = 0; -// -// for(int i=1; i<inst->uwbListLen; i++) //0 reserved for self, timeout status not applicable -// { -// if(!inst->uwbTimeout[i]) -// { -// num++; -// } -// } -// -// return num; -//} - // ------------------------------------------------------------------------------------------------------------------- // // function to find the number of neighbor UWBs in our list that are not in a timeout status @@ -501,7 +239,6 @@ int instfindnumneighbors(instance_data_t *inst) for(int i=1; i<inst->uwbListLen; i++)// 0 reserved for self, cant be neighbor { -// if(inst->uwbListType[i] == UWB_LIST_NEIGHBOR) if(tdma_handler.uwbListTDMAInfo[i].connectionType == UWB_LIST_NEIGHBOR) { num++; @@ -522,7 +259,6 @@ int instfindnumhidden(instance_data_t *inst) for(int i=1; i<inst->uwbListLen; i++)//0 reserved for self, cannot be hidden { -// if(inst->uwbListType[i] == UWB_LIST_HIDDEN) if(tdma_handler.uwbListTDMAInfo[i].connectionType == UWB_LIST_HIDDEN) { num++; @@ -561,16 +297,6 @@ int instancegetrole(void) return instance_data[0].mode; } -int instanceisranging(void) -{ - if(instance_data[0].ranging == 1) - { - return 1; - } - - return 0; -} - int instancenewrange(void) { if(instance_data[0].newRange) @@ -608,6 +334,7 @@ int instancesleeping(void) return 0 ; } + // ------------------------------------------------------------------------------------------------------------------- // function to clear counts/averages/range values // @@ -620,18 +347,11 @@ void instanceclearcounts(void) dwt_configeventcounters(1); //enable and clear - NOTE: the counters are not preserved when in DEEP SLEEP instance_data[instance].frameSN = 0; - -// instance_data[instance].tofCount = 0 ; -// instance_data[instance].tofIndex = 0 ; - instance_data[instance].txmsgcount = 0; instance_data[instance].rxmsgcount = 0; instance_data[instance].lateTX = 0; instance_data[instance].lateRX = 0; -// instance_data[instance].longTermRangeSum = 0; -// instance_data[instance].longTermRangeCount = 0; - instclearuwbList(); @@ -649,11 +369,9 @@ int instance_init(void) int result; instance_data[instance].mode = DISCOVERY; - instance_data[instance].ranging = 0; +// instance_data[instance].ranging = 0; instance_data[instance].goToSleep = 0; -// instance_data[instance].tofIndex = 0; -// instance_data[instance].tofCount = 0; for(uint8 i=0; i<UWB_LIST_SIZE; i++) { instance_data[instance].tof[i] = 0; @@ -693,11 +411,9 @@ int instance_init(void) dwt_geteui(instance_data[instance].eui64); instance_data[instance].canPrintInfo = 0; - instance_data[instance].clockOffset = 0; instance_data[instance].monitor = 0; -// dwt_setdblrxbuffmode(1); return 0 ; } @@ -705,7 +421,6 @@ int instance_init(void) // // Return the Device ID register value, enables higher level validation of physical device presence // - uint32 instancereaddeviceid(void) { return dwt_readdevid() ; @@ -844,16 +559,14 @@ void instance_config(instanceConfig_t *config) instance_data[instance].configData.txPreambLength = config->preambleLen ; instance_data[instance].configData.rxPAC = config->pacSize ; instance_data[instance].configData.nsSFD = config->nsSFD ; -// instance_data[instance].configData.phrMode = DWT_PHRMODE_STD ; instance_data[instance].configData.phrMode = DWT_PHRMODE_EXT ; - instance_data[instance].configData.sfdTO = config->sfdTO; + instance_data[instance].configData.sfdTO = config->sfdTO; //configure the channel parameters dwt_configure(&instance_data[instance].configData) ; //NOTE: For EVK1000 the OTP stores calibrated antenna and TX power values for configuration modes 3 and 5, - //check if to use the antenna delay calibration values as read from the OTP if(dwt_otprevision() <= 1) //in revision 0, 1 of EVB1000/EVK1000 { @@ -985,42 +698,17 @@ int instance_get_rxl(void) //get number of late Tx frames void inst_processtxrxtimeout(instance_data_t *inst) { - - - // send_statetousb(inst); - -// uint8 debug_msg[100]; -// int n = sprintf((char*)&debug_msg[0], "inst_processrxtimeout(), uwbToRangeWith: %i, uwbListlen: %i", inst->uwbToRangeWith, inst->uwbListLen); -// send_usbmessage(&debug_msg[0], n); -// usb_run(); - -// tdma_handler.waitForInf = FALSE; -// tdma_handler.waitForRngInit = FALSE; - - if(inst->mode == DISCOVERY) { inst->wait4ack = 0; inst->uwbToRangeWith = 255; -// uint32 maxFrameDuration = tdma_handler.maxFramelength*tdma_handler.slotDuration; if(tdma_handler.discovery_mode == WAIT_RNG_INIT || tdma_handler.discovery_mode == WAIT_INF_INIT) { -// tdma_handler.discovery_mode = WAIT_INF_REG; uint32 time_now = portGetTickCnt(); tdma_handler.set_discovery_mode(&tdma_handler, WAIT_INF_REG, time_now); } -// if(inst->previousState == TA_TXBLINK_WAIT_SEND) -// { -// tdma_handler.waitForRngInit = FALSE; -// } -// else if(inst->previousState == TA_TXRANGINGINIT_WAIT_SEND) -// { -// tdma_handler.waitForInf = FALSE; -// } - - //TODO should I keep/remove this...? if(tdma_handler.check_blink(&tdma_handler) == TRUE) { inst->testAppState = TA_TX_SELECT; @@ -1029,15 +717,6 @@ void inst_processtxrxtimeout(instance_data_t *inst) { inst->testAppState = TA_RXE_WAIT; } -// uint32 maxFrameDuration = tdma_handler.maxFramelength*tdma_handler.slotDuration; -// if(portGetTickCnt() - tdma_handler.discoveryStartTime > maxFrameDuration) //TODO handle number wrapping! -// { -// inst->testAppState = TA_TX_SELECT ; -// } -// else -// { -// inst->testAppState = TA_RXE_WAIT; -// } dwt_setrxtimeout(0); } @@ -1050,21 +729,7 @@ void inst_processtxrxtimeout(instance_data_t *inst) } else //if(inst->mode == TAG) { - // initiate the re-transmission of the poll that was not responded to inst->testAppState = TA_TX_SELECT ; -// uint8 debug_msg[100]; -// int n = sprintf((char*)&debug_msg[0], "inst_processtxrxtimeout"); -// send_usbmessage(&debug_msg[0], n); -// usb_run(); -// if(inst->previousState == TA_TXBLINK_WAIT_SEND) -// { -// inst->uwbToRangeWith = instfindfirstactiveuwbinlist(inst, 0); -// } -// else if(inst->previousState == TA_TXPOLL_WAIT_SEND) -// { -// uint8 idx = inst->uwbToRangeWith + 1; -// inst->uwbToRangeWith = instfindfirstactiveuwbinlist(inst, idx); -// } } inst->previousState = TA_INIT; @@ -1078,7 +743,7 @@ void instance_txcallback(const dwt_cb_data_t *txd) uint8 txTimeStamp[5] = {0, 0, 0, 0, 0}; event_data_t dw_event; - //NOTE - we can only get TX good (done) while here + //NOTE: we can only get TX good (done) while here dwt_readtxtimestamp(txTimeStamp) ; dw_event.timeStamp32l = (uint32)txTimeStamp[0] + ((uint32)txTimeStamp[1] << 8) + ((uint32)txTimeStamp[2] << 16) + ((uint32)txTimeStamp[3] << 24); dw_event.timeStamp = txTimeStamp[4]; @@ -1089,12 +754,6 @@ void instance_txcallback(const dwt_cb_data_t *txd) dw_event.rxLength = 0; dw_event.typeSave = dw_event.type = DWT_SIG_TX_DONE ; -// uint32 dt = portGetTickCnt() - instance_data[instance].timeofTx; -// char debug_msg[100]; -// int n = sprintf((char*)&debug_msg[0], "TX CALLBACK: DWT_SIG_TX_DONE. dt: %lu ", dt); -// send_usbmessage((uint8*)&debug_msg[0], n); -// usb_run(); - instance_putevent(dw_event); #if (DEEP_SLEEP == 1) @@ -1109,17 +768,6 @@ void instance_rxtimeoutcallback(const dwt_cb_data_t *rxd) { event_data_t dw_event; -// int instance = 0; -// uint32 response_time = portGetTickCnt() - instance_data[instance].range_start; - -// uint8 debug_msg[200]; -// int n = 0; -// n = sprintf((char*)&debug_msg[0], "RX TIMEOUT CALLBACK");// duration: %lu", response_time); -// send_usbmessage(&debug_msg[0], n); -// usb_run(); - - - dw_event.typeSave = dw_event.type = DWT_SIG_RX_TIMEOUT; dw_event.rxLength = 0; dw_event.timeStamp = 0; @@ -1131,13 +779,6 @@ void instance_rxtimeoutcallback(const dwt_cb_data_t *rxd) void instance_rxerrorcallback(const dwt_cb_data_t *rxd) { -// uint8 debug_msg[200]; -// int n = 0; -// n = sprintf((char*)&debug_msg[0], "RX ERROR CALLBACK"); -// send_usbmessage(&debug_msg[0], n); -// usb_run(); - - int instance = 0; //error caused by automatic frame filtering, ignore it and immediately turn rx back on @@ -1146,12 +787,8 @@ void instance_rxerrorcallback(const dwt_cb_data_t *rxd) return; } - -// tdma_handler.waitForInf = FALSE; -// tdma_handler.waitForRngInit = FALSE; if(tdma_handler.discovery_mode == WAIT_RNG_INIT || tdma_handler.discovery_mode == WAIT_INF_INIT) { -// tdma_handler.discovery_mode = WAIT_INF_REG; uint32 time_now = portGetTickCnt(); tdma_handler.set_discovery_mode(&tdma_handler, WAIT_INF_REG, time_now); } @@ -1170,12 +807,7 @@ void instance_rxerrorcallback(const dwt_cb_data_t *rxd) } else { -// uint8 debug_msg[100]; -// int n = sprintf((char*)&debug_msg[0], "instancerxon called from case instance_rxerrorcallback :"); -// send_usbmessage(&debug_msg[0], n); -// usb_run(); - - instance_data[instance].uwbToRangeWith = 255; + instance_data[instance].uwbToRangeWith = 255; instancerxon(&instance_data[instance], 0, 0); //immediate enable if anchor or listener } @@ -1192,22 +824,9 @@ void instance_rxgoodcallback(const dwt_cb_data_t *rxd) uint8 rxd_event = 0; uint8 fcode_index = 0; event_data_t dw_event; - - -// int num_neighbors = instfindnumactiveneighbors(&instance_data[instance]); -// uint8 debug_msg[150]; -// int n = sprintf((char*)&debug_msg[0], "RX CALLBACK, mode: %s, num_neighbors %d, discovery_mode %s", get_instanceModes_string(instance_data[instance].mode), num_neighbors, get_discovery_modes_string(tdma_handler.discovery_mode)); -// send_usbmessage(&debug_msg[0], n); -// usb_run(); - -// uint8 debug_msg[150]; -// int n = sprintf((char*)&debug_msg[0], "RX CALLBACK"); -// send_usbmessage(&debug_msg[0], n); -// usb_run(); - uint32 time_now = portGetTickCnt(); - uint32 time_now_us = portGetTickCntMicro(); + uint64 time_now_us = portGetTickCntMicro(); //if we got a frame with a good CRC - RX OK rxd_event = DWT_SIG_RX_OKAY; @@ -1222,11 +841,6 @@ void instance_rxgoodcallback(const dwt_cb_data_t *rxd) if(rxd->datalength == 12) { rxd_event = DWT_SIG_RX_BLINK; - -// uint8 debug_msg[150]; -// int n = sprintf((char*)&debug_msg[0], "RX CALLBACK: DWT_SIG_RX_BLINK"); -// send_usbmessage(&debug_msg[0], n); -// usb_run(); } else rxd_event = SIG_RX_UNKNOWN; @@ -1310,10 +924,6 @@ void instance_rxgoodcallback(const dwt_cb_data_t *rxd) if(memcmp(&instance_data[instance].uwbList[0][0], &dw_event.msgu.frame[destAddr_index], instance_data[instance].addrByteSize) != 0) { -// uint8 debug_msg[150]; -// int n = sprintf((char*)&debug_msg[0], "RX Message not addressed to me"); -// send_usbmessage(&debug_msg[0], n); -// usb_run(); rxd_event = SIG_RX_UNKNOWN; } } @@ -1325,21 +935,9 @@ void instance_rxgoodcallback(const dwt_cb_data_t *rxd) dw_event.msgu.frame[fcode_index] != RTLS_DEMO_MSG_SYNC) { rxd_event = SIG_RX_UNKNOWN; -// uint8 debug_msg[150]; -// int n = sprintf((char*)&debug_msg[0], "not INIT/POLL/RESP/FINAL"); -// send_usbmessage(&debug_msg[0], n); -// usb_run(); } } -// if(rxd_event == SIG_RX_UNKNOWN){ -// uint8 debug_msg[150]; -// int n = sprintf((char*)&debug_msg[0], "RX CALLBACK: SIG_RX_UNKNOWN"); -// send_usbmessage(&debug_msg[0], n); -// usb_run(); -// } - - //ANCHOR RX //first check if the address is already tracked. //if not, add it. //then get the index for that address @@ -1363,13 +961,12 @@ void instance_rxgoodcallback(const dwt_cb_data_t *rxd) tdma_handler.uwbListTDMAInfo[uwb_index].lastCommNeighbor = time_now; } - - -// uint8 debug_msg[100]; -//// int n = sprintf((char*)&debug_msg[0], "RX CALLBACK RECEIVED: BLINK uwb_index: %d, uwbToRangeWith: %d ", uwb_index, instance_data[instance].uwbToRangeWith); -// int n = sprintf((char*)&debug_msg[0], "RX CALLBACK RECEIVED: BLINK,%llX,xxxx", instance_get_addr()); -// send_usbmessage(&debug_msg[0], n); -// usb_run(); + //TODO remove + uint8 debug_msg[100]; +// int n = sprintf((char*)&debug_msg[0], "RX CALLBACK RECEIVED: BLINK uwb_index: %d, uwbToRangeWith: %d ", uwb_index, instance_data[instance].uwbToRangeWith); + int n = sprintf((char*)&debug_msg[0], "RX CALLBACK RECEIVED: BLINK,%llX,xxxx", instance_get_addr()); + send_usbmessage(&debug_msg[0], n); + usb_run(); } else if(rxd_event == DWT_SIG_RX_OKAY) { @@ -1378,39 +975,64 @@ void instance_rxgoodcallback(const dwt_cb_data_t *rxd) if(uwb_index > 0 && uwb_index < UWB_LIST_SIZE) { - //TODO maybe do this somewhere else... tdma_handler.uwbListTDMAInfo[uwb_index].connectionType = UWB_LIST_NEIGHBOR; tdma_handler.uwbListTDMAInfo[uwb_index].lastCommNeighbor = time_now; } - -// uint8 debug_msg[100]; -//// int n = sprintf((char*)&debug_msg[0], "RX CB RX: DWT_SIG_RX_OKAY-%s uwb_index %d, uwbToRangeWith: %d ", get_msg_fcode_string(dw_event.msgu.frame[fcode_index]), uwb_index, instance_data[instance].uwbToRangeWith); -// uint16 my_addr = instance_data[instance].uwbShortAdd; -////// uint64 my_addr = (uint64)instance_data[instance].uwbShortAdd; -//// uint16 *addr_ptr = &instance_data[instance].uwbShortAdd; -//// int n = sprintf((char*)&debug_msg[0], "RX CB RX: DWT_SIG_RX_OKAY-%s,%u,xxxx,%p", get_msg_fcode_string(dw_event.msgu.frame[fcode_index]), my_addr, (void *) addr_ptr); -//// int n = sprintf((char*)&debug_msg[0], "RX CB RX: DWT_SIG_RX_OKAY-%s,%u,xxxx", get_msg_fcode_string(dw_event.msgu.frame[fcode_index]), my_addr); + //TODO remove + uint8 debug_msg[100]; + uint16 my_addr = instance_data[instance].uwbShortAdd; // int n = sprintf((char*)&debug_msg[0], "RX CB RX: DWT_SIG_RX_OKAY-%s,%04X,uwb_index: %04d, uwb_trw: %04d", get_msg_fcode_string(dw_event.msgu.frame[fcode_index]), my_addr,uwb_index, instance_data[instance].uwbToRangeWith); -//// int n = sprintf((char*)&debug_msg[0], "%u,%p", my_addr, (void *)addr_ptr); // send_usbmessage(&debug_msg[0], n); // usb_run(); + int n = 0; + + if(dw_event.msgu.frame[fcode_index] == RTLS_DEMO_MSG_RNG_INIT ) + { + n = sprintf((char*)&debug_msg[0], "RX CB RX: DWT_SIG_RX_OKAY-%s,%04X,uwb_index: %04d, uwb_trw: %04d", get_msg_fcode_string(dw_event.msgu.frame[fcode_index]), my_addr,uwb_index, instance_data[instance].uwbToRangeWith); + } + else if(dw_event.msgu.frame[fcode_index] == RTLS_DEMO_MSG_TAG_POLL ) + { + n = sprintf((char*)&debug_msg[0], "RX CB RX: DWT_SIG_RX_OKAY-%s,%04X,uwb_index: %04d, uwb_trw: %04d", get_msg_fcode_string(dw_event.msgu.frame[fcode_index]), my_addr,uwb_index, instance_data[instance].uwbToRangeWith); + } + else if(dw_event.msgu.frame[fcode_index] == RTLS_DEMO_MSG_ANCH_RESP) + { + n = sprintf((char*)&debug_msg[0], "RX CB RX: DWT_SIG_RX_OKAY-%s,%04X,uwb_index: %04d, uwb_trw: %04d", get_msg_fcode_string(dw_event.msgu.frame[fcode_index]), my_addr,uwb_index, instance_data[instance].uwbToRangeWith); + } + else if(dw_event.msgu.frame[fcode_index] == RTLS_DEMO_MSG_TAG_FINAL) + { + n = sprintf((char*)&debug_msg[0], "RX CB RX: DWT_SIG_RX_OKAY-%s,%04X,uwb_index: %04d, uwb_trw: %04d", get_msg_fcode_string(dw_event.msgu.frame[fcode_index]), my_addr,uwb_index, instance_data[instance].uwbToRangeWith); + } + else if(dw_event.msgu.frame[fcode_index] == RTLS_DEMO_MSG_INF_REG) + { + n = sprintf((char*)&debug_msg[0], "RX CB RX: DWT_SIG_RX_OKAY-%s,%04X,uwb_index: %04d, uwb_trw: %04d", get_msg_fcode_string(dw_event.msgu.frame[fcode_index]), my_addr,uwb_index, instance_data[instance].uwbToRangeWith); + } + else if(dw_event.msgu.frame[fcode_index] == RTLS_DEMO_MSG_INF_INIT) + { + n = sprintf((char*)&debug_msg[0], "RX CB RX: DWT_SIG_RX_OKAY-%s,%04X,uwb_index: %04d, uwb_trw: %04d", get_msg_fcode_string(dw_event.msgu.frame[fcode_index]), my_addr,uwb_index, instance_data[instance].uwbToRangeWith); + } + else if(dw_event.msgu.frame[fcode_index] == RTLS_DEMO_MSG_INF_SUG) + { + n = sprintf((char*)&debug_msg[0], "RX CB RX: DWT_SIG_RX_OKAY-%s,%04X,uwb_index: %04d, uwb_trw: %04d", get_msg_fcode_string(dw_event.msgu.frame[fcode_index]), my_addr,uwb_index, instance_data[instance].uwbToRangeWith); + } + else if(dw_event.msgu.frame[fcode_index] == RTLS_DEMO_MSG_INF_UPDATE) + { + n = sprintf((char*)&debug_msg[0], "RX CB RX: DWT_SIG_RX_OKAY-%s,%04X,uwb_index: %04d, uwb_trw: %04d", get_msg_fcode_string(dw_event.msgu.frame[fcode_index]), my_addr,uwb_index, instance_data[instance].uwbToRangeWith); + } + else if(dw_event.msgu.frame[fcode_index] == RTLS_DEMO_MSG_RNG_REPORT) + { + n = sprintf((char*)&debug_msg[0], "RX CB RX: DWT_SIG_RX_OKAY-%s,%04X,uwb_index: %04d, uwb_trw: %04d", get_msg_fcode_string(dw_event.msgu.frame[fcode_index]), my_addr,uwb_index, instance_data[instance].uwbToRangeWith); + } + else if(dw_event.msgu.frame[fcode_index] == RTLS_DEMO_MSG_SYNC) + { + n = sprintf((char*)&debug_msg[0], "RX CB RX: DWT_SIG_RX_OKAY-%s,%04X,uwb_index: %04d, uwb_trw: %04d", get_msg_fcode_string(dw_event.msgu.frame[fcode_index]), my_addr,uwb_index, instance_data[instance].uwbToRangeWith); + } - } - -// if(uwb_index > UWB_LIST_SIZE - 1 || uwb_index <= 0) -// { -// instance_data_t *inst = &instance_data[instance]; + send_usbmessage(&debug_msg[0], n); + usb_run(); -// uint8 debug_msg[100]; -// uint16 my_addr = instance_data[instance].uwbShortAdd; -// int n = sprintf((char*)&debug_msg[0], "uwb_index:%u of %u",uwb_index, instance_data[instance].uwbListLen); -// send_usbmessage(&debug_msg[0], n); -// usb_run(); - //TODO solve this problem. other, (incorrect) addresses are making it into our UWB list for some reason... - //think i took care of it, it was incorrect indexing in create/read INF messages -// } + } bool accept_inf = FALSE; //check if the incoming message indicates that we should range with the source UWB or just accept and process the message @@ -1424,7 +1046,6 @@ void instance_rxgoodcallback(const dwt_cb_data_t *rxd) { if(dw_event.msgu.frame[fcode_index] == RTLS_DEMO_MSG_TAG_POLL) { - //TODO would be good to check if the UWB sending this message was supposed to be ranging in this timeslot instance_data[instance].uwbToRangeWith = uwb_index; } else if(dw_event.msgu.frame[fcode_index] == RTLS_DEMO_MSG_INF_REG || dw_event.msgu.frame[fcode_index] == RTLS_DEMO_MSG_INF_SUG || dw_event.msgu.frame[fcode_index] == RTLS_DEMO_MSG_INF_UPDATE)//only anchor if already received INF_INIT or collected regular INF messages and joined with SUG message @@ -1434,17 +1055,11 @@ void instance_rxgoodcallback(const dwt_cb_data_t *rxd) } else if (instance_data[instance].mode == DISCOVERY) { - //TODO use switch case? if(dw_event.msgu.frame[fcode_index] == RTLS_DEMO_MSG_RNG_INIT) { if(tdma_handler.discovery_mode == WAIT_RNG_INIT) { - //only accept RNG_INIT if no other active neighbors exist (and we are waiting for RNG_INIT) - int num_active = instfindnumneighbors(&instance_data[instance]); - if(num_active <= 1) - { - instance_data[instance].uwbToRangeWith = uwb_index; - } + instance_data[instance].uwbToRangeWith = uwb_index; } } else if(dw_event.msgu.frame[fcode_index] == RTLS_DEMO_MSG_INF_INIT) @@ -1469,48 +1084,25 @@ void instance_rxgoodcallback(const dwt_cb_data_t *rxd) } else if(rxd_event == DWT_SIG_RX_BLINK) { - //only accept BLINK if in DISCOVERY mode and no other active neighbors exist - //and not waiting for inf or rng_init + //only accept BLINK if in DISCOVERY mode and waiting for INF_REG if(instance_data[instance].mode == DISCOVERY) { if(tdma_handler.discovery_mode == WAIT_INF_REG) { - int num_neighbors = instfindnumneighbors(&instance_data[instance]); -// uint8 debug_msg[100]; -// int n = sprintf((char*)&debug_msg[0], "num_neighbors %d", num_neighbors); -// send_usbmessage(&debug_msg[0], n); -// usb_run(); - -// if(num_neighbors <= 1) //TODO i don't think I want this -// { - instance_data[instance].uwbToRangeWith = uwb_index; -// } + instance_data[instance].uwbToRangeWith = uwb_index; } } } } } - -// n = sprintf((char*)&debug_msg[0], "uwb_index %d, uwbToRangeWith %d", uwb_index, instance_data[instance].uwbToRangeWith); -// send_usbmessage(&debug_msg[0], n); -// usb_run(); - - int place_event = 0; - if(rxd_event == DWT_SIG_RX_OKAY && uwb_index != 255) { //always accept. if(dw_event.msgu.frame[fcode_index] == RTLS_DEMO_MSG_RNG_REPORT || dw_event.msgu.frame[fcode_index] == RTLS_DEMO_MSG_SYNC) { -// uint8 debug_msg[100]; -// int n = sprintf((char*)&debug_msg[0], "RX CALLBACK ACCEPTED: RNG_REPORT <- uwb_index %d", uwb_index); -// send_usbmessage(&debug_msg[0], n); -// usb_run(); -// instance_data[instance].lastCommTimeStamp[uwb_index] = time_now; -// instance_data[instance].uwbTimeout[uwb_index] = 0; place_event = 1; } } @@ -1518,20 +1110,9 @@ void instance_rxgoodcallback(const dwt_cb_data_t *rxd) if(accept_inf == TRUE) { place_event = 1; - - - if(dw_event.msgu.frame[fcode_index] == RTLS_DEMO_MSG_INF_SUG) - { -// uint8 debug_msg[100]; -// int n = sprintf((char*)&debug_msg[0], "RX CB: DWT_SIG_RX_OKAY-%s, %llX, xxxx", get_msg_fcode_string(dw_event.msgu.frame[fcode_index]), instance_get_addr()); -// send_usbmessage(&debug_msg[0], n); -// usb_run(); - } - } else if(uwb_index != 255 && instance_data[instance].uwbToRangeWith == uwb_index) { - if(rxd_event == DWT_SIG_RX_OKAY) { //process RTLS_DEMO_MSG_TAG_POLL immediately. @@ -1559,7 +1140,7 @@ void instance_rxgoodcallback(const dwt_cb_data_t *rxd) instance_data[instance].msg.seqNum = instance_data[instance].frameSN++; - + //TODO //set the delayed rx on time (the final message will be sent after this delay) // dwt_setrxaftertxdelay((uint32)instance_data[instance].txToRxDelayAnc_sy); //units are 1.0256us - wait for wait4respTIM before RX on (delay RX) dwt_setrxaftertxdelay(0); //units are 1.0256us - wait for wait4respTIM before RX on (delay RX) @@ -1585,37 +1166,9 @@ void instance_rxgoodcallback(const dwt_cb_data_t *rxd) { dw_event.typePend = DWT_SIG_TX_PENDING ; // exit this interrupt and notify the application/instance that TX is in progress. instance_data[instance].timeofTx = time_now; + instance_data[instance].txDoneTimeoutDuration = 200; //NOTE timeout duration found experimentally, may need to be changed if the delays in instance.h are modified } - - instance_data[instance].tof[uwb_index] = 0; //clear ToF .. //TODO does this need to be here? - } -// else if(dw_event.msgu.frame[fcode_index] == RTLS_DEMO_MSG_ANCH_RESP) -// { -// -// } -// else if(dw_event.msgu.frame[fcode_index] == RTLS_DEMO_MSG_RNG_INIT) -// { -// -// } -// else if(dw_event.msgu.frame[fcode_index] == RTLS_DEMO_MSG_TAG_FINAL) -// { -// -// } -// else if(dw_event.msgu.frame[fcode_index] == RTLS_DEMO_MSG_RNG_REPORT) -// { -// instance_data[instance].lastCommTimeStamp[uwb_index] = portGetTickCnt(); -// instance_data[instance].uwbTimeout[uwb_index] = 0; -// uint8 debug_msg[100]; -// int n = sprintf((char*)&debug_msg[0], "RX CALLBACK ACCEPTED: RNG_REPORT <- uwb_index %d", uwb_index); -// send_usbmessage(&debug_msg[0], n); -// usb_run(); -// } -// else if(dw_event.msgu.frame[fcode_index] == RTLS_DEMO_MSG_SYNC) -// { -// -// } - place_event = 1; } @@ -1625,24 +1178,8 @@ void instance_rxgoodcallback(const dwt_cb_data_t *rxd) } } - // TODO figure out a better way to do this, I'd like to keep it where it was if possible - // doing it here because it needs to be toggled before toggling the rx buffer pointer - // Toggle the Host side Receive Buffer Pointer -// dwt_write8bitoffsetreg(SYS_CTRL_ID, SYS_CTRL_HRBT_OFFSET, 1); - -// uint8 buff = dwt_read8bitoffsetreg(SYS_STATUS_ID, 3); -// uint8 debug_msg[100]; -// int n = sprintf((char*)&debug_msg[0], "RX BUFFER TOGGLE: %d", buff); -// send_usbmessage(&debug_msg[0], n); -// usb_run(); - if(place_event) { -// uint8 debug_msg[100]; -// int n = sprintf((char*)&debug_msg[0], "event placed"); -// send_usbmessage(&debug_msg[0], n); -// usb_run(); - instance_putevent(dw_event); #if (DEEP_SLEEP == 1) @@ -1654,21 +1191,10 @@ void instance_rxgoodcallback(const dwt_cb_data_t *rxd) } else { - // instance_rxerrorcallback(rxd); - -// uint8 debug_msg[100]; -// int n = sprintf((char*)&debug_msg[0], "instancerxon called from !event_placed :"); -// send_usbmessage(&debug_msg[0], n); -// usb_run(); - instancerxon(&instance_data[instance], 0, 0); //immediately reenable RX } } - - - - int instance_peekevent(void) { int instance = 0; @@ -1725,7 +1251,9 @@ event_data_t* instance_getevent(int x) instance_data[instance].dweventIdxOut++; if(MAX_EVENT_NUMBER == instance_data[instance].dweventIdxOut) //wrap the counter + { instance_data[instance].dweventIdxOut = 0; + } instance_data[instance].dweventPeek = instance_data[instance].dweventIdxOut; //set the new peek value @@ -1759,108 +1287,25 @@ int instance_run(void) while(done == INST_NOT_DONE_YET) { -// uint64 time_now_us = portGetTickCntMicro(); done = testapprun(&instance_data[instance], &tdma_handler, message) ; // run the communications application -// uint64 duration = get_dt64(time_now_us, portGetTickCntMicro()); -// uint8 debug_msg[100]; -// int n = sprintf((char *)&debug_msg, "TAR, duration, %llu", duration); -// send_usbmessage(&debug_msg[0], n); -// usb_run(); - - //we've processed message message = 0; } uint32 time_now = portGetTickCnt(); - //check timeouts. handle case where we timeout and no longer have any neighbors + + //check timeouts. if(tdma_handler.check_timeouts(&tdma_handler, time_now)) { - tdma_handler.tdma_free_all_slots(&tdma_handler); - - instance_data[instance].mode = DISCOVERY; //TODO clear the TDMA slot information! - tdma_handler.discoveryStartTime = portGetTickCnt(); + //handle case where we timeout and no longer have any neighbors + instance_data[instance].mode = DISCOVERY; tdma_handler.enter_discovery_mode(&tdma_handler); inst_processtxrxtimeout(&instance_data[instance]); instance_data[instance].canPrintInfo = 0; - instance_data[instance].ranging = 0; } -// //check if lastCommTimeStamp has expired for any of the uwbs in our list -// for(int i=1; i < instance_data[instance].uwbListLen; i++)//0 reserved for self, timeout not applicable -// { -// //TODO could also have a timer to put into DISCOVERY if we haven't spoken to anyone in a while -// uint32 time_now = portGetTickCnt(); -// -// -// uint32 delta_t = get_dt32(instance_data[instance].lastCommTimeStamp[i], time_now); -// if(delta_t > UWB_COMM_TIMEOUT) //TODO handle number wrapping -// { -//// if(instance_data[instance].uwbListType[i] == UWB_LIST_NEIGHBOR) //what about hidden? -// if(tdma_handler.uwbListTDMAInfo[i].connectionType == UWB_LIST_NEIGHBOR) //what about hidden? -// { -//// instance_data[instance].uwbListType[i] = UWB_LIST_INACTIVE; //TODO release TDMA slots as well -// tdma_handler.uwbListTDMAInfo[i].connectionType = UWB_LIST_INACTIVE; //TODO release TDMA slots as well -// instance_data[instance].uwbTimeout[i] = 1; -// -// //NEW -// //if no more active neighbors exist, transition to DISCOVERY -// uint8 numNeighbors = instfindnumactiveneighbors(&instance_data[instance]); -// if(numNeighbors <= 0) -// { -// tdma_handler.tdma_free_all_slots(&tdma_handler); -// -// -// -//// uint8 debug_msg[100]; -//// int n = sprintf((char *)&debug_msg, "inst_processtxrxtimeout(inst) after free_all_slots"); -//// send_usbmessage(&debug_msg[0], n); -//// usb_run(); -// -// instance_data[instance].mode = DISCOVERY; //TODO clear the TDMA slot information! -// tdma_handler.discoveryStartTime = portGetTickCnt(); -// tdma_handler.enter_discovery_mode(&tdma_handler); -// inst_processtxrxtimeout(&instance_data[instance]); -// -// instance_data[instance].canPrintInfo = 0; -// instance_data[instance].ranging = 0; -// } -// else -// { -// //TODO check if able to reconfigure tdma assignements. shrink frame size, reassign emptied slots, etc -// tdma_handler.uwblist_free_slots(&tdma_handler, i); -// } -// -// -//// uint8 debug_msg[100]; -//// int n = sprintf((char*)&debug_msg[0], "TIMEOUT: uwb %i", i); -//// send_usbmessage(&debug_msg[0], n); -//// usb_run(); -// } -// -// if(instance_data[instance].uwbToRangeWith == i) -// { -// instance_data[instance].uwbToRangeWith = 255; -// //NOTE this might need to be changed for TAG operations -// if(instance_data[instance].mode == ANCHOR || instance_data[instance].mode == DISCOVERY) //TODO maybe send to TA_MODE_SELECT? -// { -// instance_data[instance].testAppState = TA_RXE_WAIT; -// } -// else -// { -//// uint8 debug_msg[100]; -//// int n = sprintf((char*)&debug_msg[0], "instance_run(void) TIMEOUT"); -//// send_usbmessage(&debug_msg[0], n); -//// usb_run(); -// instance_data[instance].testAppState = TA_TX_SELECT; -// } -// } -// } -// -// } - 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 == DISCOVERY) @@ -1884,12 +1329,6 @@ int instance_run(void) { if(instance_data[instance].instanceTimerTime < portGetTickCnt()) { -// uint8 debug_msg[200]; -// int n = 0; -// n = sprintf((char*)&debug_msg[0], "instance timer expired"); -// send_usbmessage(&debug_msg[0], n); -// usb_run(); - event_data_t dw_event; instance_data[instance].instanceTimerEnabled = 0; dw_event.rxLength = 0; @@ -1933,16 +1372,3 @@ uint16 instancerxantdly(void) } #endif - - -/* ========================================================== - -Notes: - -Previously code handled multiple instances in a single console application - -Now have changed it to do a single instance only. With minimal code changes...(i.e. kept [instance] index but it is always 0. - -Windows application should call instance_init() once and then in the "main loop" call instance_run(). - -*/ diff --git a/src/application/tdma_handler.c b/src/application/tdma_handler.c index 6da5ec2..b9b688c 100644 --- a/src/application/tdma_handler.c +++ b/src/application/tdma_handler.c @@ -14,7 +14,7 @@ static bool slot_transition(struct TDMAHandler *this) if(inst->mode == TAG || inst->mode == ANCHOR || - (inst->mode == DISCOVERY && (this->discovery_mode == WAIT_SEND_SUG)))// || this->discovery_mode == COLLECT_INF_REG)))//TODO this may be out of date + (inst->mode == DISCOVERY && (this->discovery_mode == WAIT_SEND_SUG || this->discovery_mode == COLLECT_INF_REG)))//TODO this may be out of date { uint64 time_now_us = portGetTickCntMicro(); uint64 timeSinceSlotStart64 = get_dt64(this->lastSlotStartTime64, time_now_us); @@ -30,7 +30,6 @@ static bool slot_transition(struct TDMAHandler *this) //we have transitioned into the next slot. //get the slot number and set the start time appropriately -// uint64 timeSinceFrameStart64 = get_dt64(this->uwbFrameStartTimes64[0], time_now_us); uint64 timeSinceFrameStart64 = get_dt64(this->uwbListTDMAInfo[0].frameStartTime, time_now_us); //TODO this->uwbListTDMAInfo[0].frameStartTime is sometimes getting set to a time in the future... figure out! @@ -43,25 +42,17 @@ static bool slot_transition(struct TDMAHandler *this) // send_usbmessage(&debug_msg[0], n); // usb_run(); - //TODO think about adopting rebase AFTER all neighbors have had a chance to TX the rebase in their INF message as well. -// this->rebase_pending = FALSE; -// this->rebase_tx = FALSE; -// this->uwbFrameStartTimes64[0] = this->rebase_frameStartTime64; -// timeSinceFrameStart64 = get_dt64(this->uwbFrameStartTimes64[0], time_now_us); } while(timeSinceFrameStart64 >= frameDuration64) { -// this->uwbFrameStartTimes64[0] = timestamp_add64(this->uwbFrameStartTimes64[0], frameDuration64); this->uwbListTDMAInfo[0].frameStartTime = timestamp_add64(this->uwbListTDMAInfo[0].frameStartTime, frameDuration64); timeSinceFrameStart64 -= frameDuration64; } -// this->lastFST = this->uwbFrameStartTimes64[0]; this->lastFST = this->uwbListTDMAInfo[0].frameStartTime; uint8 slot = timeSinceFrameStart64/(this->slotDuration_us); //integer division rounded down -// this->lastSlotStartTime64 = this->uwbFrameStartTimes64[0] + (uint64)(this->slotDuration_us*slot); this->lastSlotStartTime64 = this->uwbListTDMAInfo[0].frameStartTime + (uint64)(this->slotDuration_us*slot); @@ -70,31 +61,9 @@ static bool slot_transition(struct TDMAHandler *this) // send_usbmessage(&debug_msg[0], n); // usb_run(); - -// n = sprintf((char*)&debug_msg[0], "&this->slotAssignments[slot]: %u, &inst->uwbShortAdd %u :", this->slotAssignments[slot], inst->uwbShortAdd); -// send_usbmessage(&debug_msg[0], n); -// usb_run(); -// -// if(this->slot_assigned(this, slot) == TRUE) -// { -// -// } - -// struct TDMAInfo *info = &this->uwbListTDMAInfo[0]; -// bool assigned0 = this->slot_assigned(info, 0); -// bool assigned1 = this->slot_assigned(info, 1); -// bool assigned2 = this->slot_assigned(info, 2); -// bool assigned3 = this->slot_assigned(info, 3); -// -// uint8 debug_msg[100]; -// int n = sprintf((char*)&debug_msg[0], "NEW SLOT %u, ass0: %u, ass1: %u, ass2: %u, ass3: %u,", slot, assigned0, assigned1, assigned2, assigned3); -// send_usbmessage(&debug_msg[0], n); -// usb_run(); - - if(inst->mode != DISCOVERY) { - if(this->slot_assigned(&this->uwbListTDMAInfo[0], slot) == TRUE) //TODO left off here, for some reason it fails when I try this... + if(this->slot_assigned(&this->uwbListTDMAInfo[0], slot) == TRUE) { inst->mode = TAG; @@ -124,17 +93,10 @@ static bool slot_transition(struct TDMAHandler *this) return transition; } -//static void frame_sync(struct TDMAHandler *this, uint8 *messageData, uint16 rxLength, uint8 srcIndex, FRAME_SYNC_MODE mode) -//static void frame_sync(struct TDMAHandler *this, event_data_t *dw_event, uint8 *messageData, uint8 srcIndex, FRAME_SYNC_MODE mode) static void frame_sync(struct TDMAHandler *this, event_data_t *dw_event, uint8 framelength, uint64 timeSinceFrameStart_us, uint8 srcIndex, FRAME_SYNC_MODE mode) { instance_data_t *inst = instance_get_local_structure_ptr(0); -// uint8 debug_msg[100]; -// int n = sprintf((char*)&debug_msg[0], "frame sync"); -// send_usbmessage(&debug_msg[0], n); -// usb_run(); - //do not process erroneous tsfs //can happen if frame start time is shifted ahead of time_now_us in transmitting UWB if(timeSinceFrameStart_us > 1000000000 || timeSinceFrameStart_us == 0) @@ -142,20 +104,12 @@ static void frame_sync(struct TDMAHandler *this, event_data_t *dw_event, uint8 f return; } - uint8 sys_time_arr[5] = {0, 0, 0, 0, 0}; dwt_readsystime(sys_time_arr); uint64 dwt_time_now = 0; dwt_time_now = (uint64)sys_time_arr[0] + ((uint64)sys_time_arr[1] << 8) + ((uint64)sys_time_arr[2] << 16) + ((uint64)sys_time_arr[3] << 24) + ((uint64)sys_time_arr[4] << 32); uint64 time_now_us = portGetTickCntMicro(); -// uint8 framelength; -// uint64 timeSinceFrameStart_us = 0; - -// memcpy(&framelength, &messageData[TDMA_FRAMELENGTH], sizeof(uint8)); -// //timeSinceFrameStart in message -// memcpy(&timeSinceFrameStart_us, &messageData[TDMA_TSFS], 6); - //time from message to tx //assuming zero since we use DWT_START_TX_IMMEDIATE @@ -166,7 +120,7 @@ static void frame_sync(struct TDMAHandler *this, event_data_t *dw_event, uint8 f //NOTE: assuming zero since difference for speed of light travel time over 10cm and 100m is negligible for frame sync purposes //rx antenna delay - //NOTE: we won't use this, because the antenna delay is captured by the RX timestamp + //NOTE: antenna delay is captured by the RX timestamp //time from rx timestamp to now uint64 rxfs_process_delay = dwt_getdt(dw_event->timeStamp, dwt_time_now); @@ -174,27 +128,18 @@ static void frame_sync(struct TDMAHandler *this, event_data_t *dw_event, uint8 f uint64 txrx_delay = (uint64)(convertdevicetimetosec(tx_antenna_delay + rxfs_process_delay)*1000000.0) + inst->storePreLen_us; uint64 hisTimeSinceFrameStart_us = timeSinceFrameStart_us + txrx_delay; -// this->uwbFrameStartTimes64[srcIndex] = timestamp_subtract64(time_now_us, hisTimeSinceFrameStart_us); //TODO consider applying the diff! - this->uwbListTDMAInfo[srcIndex].frameStartTime = timestamp_subtract64(time_now_us, hisTimeSinceFrameStart_us); //TODO consider applying the diff! + this->uwbListTDMAInfo[srcIndex].frameStartTime = timestamp_subtract64(time_now_us, hisTimeSinceFrameStart_us); if(mode == FS_COLLECT) { return; } - else if(mode == FS_ADOPT) //TODO this might not be right! incoming framelength not always the same as ours! + else if(mode == FS_ADOPT) { -// this->uwbFrameStartTimes64[0] = this->uwbFrameStartTimes64[srcIndex]; -// this->uwbListTDMAInfo[0].frameStartTime = this->uwbListTDMAInfo[srcIndex].frameStartTime; -// uint8 slot = hisTimeSinceFrameStart_us/this->slotDuration_us; //integer division rounded down -// this->lastSlotStartTime64 = this->uwbFrameStartTimes64[0] + (uint64)((this->slotDuration_us)*slot); -// this->lastSlotStartTime64 = this->uwbListTDMAInfo[0].frameStartTime + (uint64)((this->slotDuration_us)*slot); - this->uwbListTDMAInfo[0].frameStartTime = this->uwbListTDMAInfo[srcIndex].frameStartTime;//NOTE gets processed further at end of function } - else// if(mode == FS_AVERAGE || mode == FS_EVAL) //TODO consider using a weighted average. perhaps use threshholds to determine if instead we should just use the ADOPT logic + else// if(mode == FS_AVERAGE || mode == FS_EVAL) { -// uint64 myFramelengthDuration_us = this->uwbListTDMAInfo[0].framelength*this->slotDuration_us; -// uint64 myTimeSinceFrameStart_us = get_dt64(this->uwbFrameStartTimes64[0], time_now_us); uint64 myTimeSinceFrameStart_us = get_dt64(this->uwbListTDMAInfo[0].frameStartTime, time_now_us); //SELF VS INCOMING @@ -224,7 +169,6 @@ static void frame_sync(struct TDMAHandler *this, event_data_t *dw_event, uint8 f else { diff_us = min_framelengthDuration - diff_tsfs_mod; - //subtract } } else @@ -235,7 +179,6 @@ static void frame_sync(struct TDMAHandler *this, event_data_t *dw_event, uint8 f if(diff_tsfs_mod <= 0.5*min_framelengthDuration) { diff_us = diff_tsfs_mod; - //subtract } else { @@ -261,33 +204,25 @@ static void frame_sync(struct TDMAHandler *this, event_data_t *dw_event, uint8 f if(diff_add == TRUE) { -// uint8 debug_msg[100]; -// int n = sprintf((char*)&debug_msg[0], "add %llu", diff_us); -// send_usbmessage(&debug_msg[0], n); -// usb_run(); - -// this->uwbFrameStartTimes64[0] = timestamp_add64(this->uwbFrameStartTimes64[0], diff_us/div); this->uwbListTDMAInfo[0].frameStartTime = timestamp_add64(this->uwbListTDMAInfo[0].frameStartTime, diff_us/div); -// this->lastSlotStartTime64 = timestamp_add64(this->lastSlotStartTime64, diff_us/div); } else { -// uint8 debug_msg[100]; -// int n = sprintf((char*)&debug_msg[0], "subtract %llu", diff_us); -// send_usbmessage(&debug_msg[0], n); -// usb_run(); -// this->uwbFrameStartTimes64[0] = timestamp_subtract64(this->uwbFrameStartTimes64[0], diff_us/div); this->uwbListTDMAInfo[0].frameStartTime = timestamp_subtract64(this->uwbListTDMAInfo[0].frameStartTime, diff_us/div); -// this->lastSlotStartTime64 = timestamp_subtract64(this->lastSlotStartTime64, diff_us/div); } } uint64 myFrameDuration = this->slotDuration_us*this->uwbListTDMAInfo[0].framelength; - uint64 myTimeSinceFrameStart_us = get_dt64(this->uwbListTDMAInfo[0].frameStartTime, time_now_us); //TODO what if added to be past time_now??? + uint64 myTimeSinceFrameStart_us = get_dt64(this->uwbListTDMAInfo[0].frameStartTime, time_now_us); if(myTimeSinceFrameStart_us > 100000000) { - //if this is a very large number, then the frame start time was likely moved head of time_now_us. - this->lastSlotStartTime64 = this->uwbListTDMAInfo[0].frameStartTime - (uint64)(this->slotDuration_us); + //if this is a very large number, then the frame start time was likely moved ahead of time_now_us. + while(this->uwbListTDMAInfo[0].frameStartTime > time_now_us) + { + this->uwbListTDMAInfo[0].frameStartTime = timestamp_subtract64(this->uwbListTDMAInfo[0].frameStartTime, myFrameDuration); + } + + myTimeSinceFrameStart_us = get_dt64(this->uwbListTDMAInfo[0].frameStartTime, time_now_us); } else { @@ -296,10 +231,10 @@ static void frame_sync(struct TDMAHandler *this, event_data_t *dw_event, uint8 f this->uwbListTDMAInfo[0].frameStartTime = timestamp_add64(this->uwbListTDMAInfo[0].frameStartTime, myFrameDuration); myTimeSinceFrameStart_us -= myFrameDuration; } - - uint8 slot = hisTimeSinceFrameStart_us/this->slotDuration_us; //integer division rounded down - this->lastSlotStartTime64 = this->uwbListTDMAInfo[0].frameStartTime + (uint64)(this->slotDuration_us*slot); } + + uint8 slot = myTimeSinceFrameStart_us/this->slotDuration_us; //integer division rounded down + this->lastSlotStartTime64 = timestamp_add64(this->uwbListTDMAInfo[0].frameStartTime, (uint64)(this->slotDuration_us*slot)); } @@ -307,7 +242,6 @@ static bool tx_sync_msg(struct TDMAHandler *this) { instance_data_t *inst = instance_get_local_structure_ptr(0); uint64 time_now_us = portGetTickCntMicro(); -// uint64 myTimeSinceFrameStart_us = get_dt64(this->uwbFrameStartTimes64[0], time_now_us); uint64 myTimeSinceFrameStart_us = get_dt64(this->uwbListTDMAInfo[0].frameStartTime, time_now_us); memcpy(&inst->sync_msg.messageData[SYNC_FRAMELENGTH], &this->uwbListTDMAInfo[0].framelength, sizeof(uint8)); memcpy(&inst->sync_msg.messageData[SYNC_TSFS], &myTimeSinceFrameStart_us, 6); @@ -336,209 +270,18 @@ static bool tx_sync_msg(struct TDMAHandler *this) inst->previousState = inst->testAppState; inst->testAppState = TA_TX_WAIT_CONF; // wait confirmation + inst->timeofTx = portGetTickCnt(); + inst->txDoneTimeoutDuration = 200; //NOTE timeout duration found experimentally, may need to be changed if the delays in instance.h are modified +// inst->monitor = 1; return TRUE; } } - - -// -////static void frame_sync(struct TDMAHandler *this, uint8 *messageData, uint16 rxLength, uint8 srcIndex, FRAME_SYNC_MODE mode) -//static void frame_sync(struct TDMAHandler *this, event_data_t *dw_event, uint8 *messageData, uint8 srcIndex, FRAME_SYNC_MODE mode) -//{ -// //ignore frame sync if we have a rebase pending -//// if(this->rebase_pending == TRUE) -//// { -//// return; -//// } -// -// instance_data_t *inst = instance_get_local_structure_ptr(0); -// -// uint8 sys_time_arr[5] = {0, 0, 0, 0, 0}; -// dwt_readsystime(sys_time_arr); -// uint64 dwt_time_now = (uint64)sys_time_arr[0] + ((uint64)sys_time_arr[1] << 8) + ((uint64)sys_time_arr[2] << 16) + ((uint64)sys_time_arr[3] << 24) + ((uint64)sys_time_arr[4] << 32); -// uint64 time_now_us = portGetTickCntMicro(); -// -// uint8 framelength; -// uint32 timeSinceFrameStart64 = 0; -//// bool rebase = FALSE; -// -// memcpy(&framelength, &messageData[TDMA_FRAMELENGTH], sizeof(uint8)); -// //timeSinceFrameStart in message -// memcpy(&timeSinceFrameStart64, &messageData[TDMA_TSFS], sizeof(uint64)); -//// memcpy(&rebase, &messageData[TDMA_TSFS_REBASE], sizeof(bool)); -// -// //time from message to tx -// //assuming zero since we use DWT_START_TX_IMMEDIATE -// -// //tx antenna delay -// uint64 tx_antenna_delay = (uint64)inst->txAntennaDelay; -// -// //time for xmission (only count once, happens on both sides near simultaneously) -// //easiest way to check would be to see if it is the same as the defines for other standard messages... -// inst->frameLengths_us[INF] = instance_getmessageduration_us(dw_event->rxLength); //TODO should maybe make sure extended framelength cannot overflow a uint32 -// -// //time to propagate -// //NOTE: assuming zero since difference for speed of light travel time over 10cm and 100m is negligible for frame sync purposes -// -// //rx antenna delay -// //NOTE: we won't use this, because the antenna delay is captured by the RX timestamp -// -// //time from rx timestamp to now -// uint64 rxfs_process_delay = dwt_getdt(dw_event->timeStamp, dwt_time_now); -// -// uint64 txrx_delay = (uint64)(convertdevicetimetosec(tx_antenna_delay + rxfs_process_delay)*1000000.0) + inst->storePreLen_us; -// -// uint64 hisTimeSinceFrameStart_us = timeSinceFrameStart64 + txrx_delay; -// this->uwbFrameStartTimes64[srcIndex] = timestamp_subtract64(time_now_us, hisTimeSinceFrameStart_us); //TODO consider applying the diff! -// -// if(mode == FS_ADOPT) -// { -// this->uwbFrameStartTimes64[0] = this->uwbFrameStartTimes64[srcIndex]; -// uint8 slot = hisTimeSinceFrameStart_us/this->slotDuration_us; //integer division rounded down -// this->lastSlotStartTime64 = this->uwbFrameStartTimes64[0] + (uint64)((this->slotDuration_us)*slot); -// } -// else if(mode == FS_COLLECT) -// { -// return; -// } -// else if(mode == FS_AVERAGE) //TODO consider using a weighted average. perhaps use threshholds to determine if instead we should just use the ADOPT logic -// { -//// if(this->discovery_mode == COLLECT_INF_REG) -//// { -//// //TODO handle the case where we are collecting from two different UWB networks that are not synchronized -//// //in case there are multiple subnetworks... only collect average frame start times for one of them... -//// //send out INF_SUG in that 0th frame with rebase to the other subnetwork's frame. but what if there are two other networks? -//// //instead send out INF fug in that 0th frame with rebase to that frame. Hopefully the other networks will get the message. -//// //if not, the discrepency will eventually be taken care of the ANCHOR and TAG logic, regarless of how many subnetworks there are. -//// //sync to the largest subnetwork... -//// } -// -// -//// if(rebase == TRUE) -//// { -//// this->rebase_pending = TRUE; -//// this->rebase_tx = FALSE; -//// this->rebase_frameStartTime64 = this->uwbFrameStartTimes64[srcIndex]; -//// return; -//// } -// -// uint64 myFramelengthDuration_us = this->uwbListTDMAInfo[0].framelength*this->slotDuration_us; -// uint64 myTimeSinceFrameStart_us = get_dt64(this->uwbFrameStartTimes64[0], time_now_us); -// -// uint64 diff_us = 0; -// bool diff_add = FALSE; -// uint64 threshold = this->slotStartDelay_us; //TODO make a permanent variable for this threshold -// -// //TODO make sure this can't/doesn't happen -// //myTimeSinceFrameStart is longer than the FramelengthDuration!!! -//// if(myTimeSinceFrameStart > myFramelengthDuration) -//// { -//// uint8 debug_msg[100]; -//// int n = sprintf((char *)&debug_msg, "myTimeSinceFrameStart > myTimeSinceFrameStart!");//,%llX, psdu: %d ", instance_get_addr(), psduLength); -//// send_usbmessage(&debug_msg[0], n); -//// usb_run(); -//// } -// -// if(this->uwbListTDMAInfo[0].framelength <= framelength) -// { -// uint64 hisTimeSinceFrameStartMod_us = hisTimeSinceFrameStart_us%myFramelengthDuration_us; -// -// if(myTimeSinceFrameStart_us > hisTimeSinceFrameStartMod_us) -// { -// diff_us = myTimeSinceFrameStart_us - hisTimeSinceFrameStartMod_us; -// diff_add = TRUE; -// -//// uint64 diff_us = myTimeSinceFrameStart_us - hisTimeSinceFrameStartMod_us; -//// this->uwbFrameStartTimes64[0] = timestamp_add64(this->uwbFrameStartTimes64[0], diff_us/2); -//// this->lastSlotStartTime64 = timestamp_add64(this->lastSlotStartTime64, diff_us/2); -// } -// else -// { -// diff_us = hisTimeSinceFrameStartMod_us - myTimeSinceFrameStart_us; -// diff_add = FALSE; -//// uint64 diff_us = hisTimeSinceFrameStartMod_us - myTimeSinceFrameStart_us; -//// this->uwbFrameStartTimes64[0] = timestamp_subtract64(this->uwbFrameStartTimes64[0], diff_us/2); -//// this->lastSlotStartTime64 = timestamp_subtract64(this->lastSlotStartTime64, diff_us/2); -// } -// } -// else -// { -// uint64 hisFramelengthDuration_us = framelength*this->slotDuration_us; -// uint64 myTimeSinceFrameStartMod_us = myTimeSinceFrameStart_us%hisFramelengthDuration_us; -// -// -//// if(timeSinceFrameStart > hisFramelengthDuration) -//// { -//// uint8 debug_msg[100]; -//// int n = sprintf((char *)&debug_msg, "timeSinceFrameStart > hisFramelengthDuration!"); -//// send_usbmessage(&debug_msg[0], n); -//// usb_run(); -//// } -// -// if(hisTimeSinceFrameStart_us > myTimeSinceFrameStartMod_us) -// { -// diff_us = hisTimeSinceFrameStart_us - myTimeSinceFrameStartMod_us; -// diff_add = FALSE; -// -//// uint64 diff_us = hisTimeSinceFrameStart_us - myTimeSinceFrameStartMod_us; -//// this->uwbFrameStartTimes64[0] = timestamp_subtract64(this->uwbFrameStartTimes64[0], diff_us/2); -//// this->lastSlotStartTime64 = timestamp_subtract64(this->lastSlotStartTime64, diff_us/2); -// } -// else -// { -// diff_us = myTimeSinceFrameStartMod_us - hisTimeSinceFrameStart_us; -// diff_add = TRUE; -// -//// uint64 diff_us = myTimeSinceFrameStartMod_us - hisTimeSinceFrameStart_us; -//// this->uwbFrameStartTimes64[0] = timestamp_add64(this->uwbFrameStartTimes64[0], diff_us/2); -//// this->lastSlotStartTime64 = timestamp_add64(this->lastSlotStartTime64, diff_us/2); -// } -// } -// -// -// //if the difference is too large, we need to rebase our frame start time -//// if(diff_us > threshold) -//// { -//// this->rebase_pending = TRUE; -//// this->rebase_tx = FALSE; -//// this->rebase_frameStartTime64 = this->uwbFrameStartTimes64[srcIndex]; -//// } -//// else -//// { -// if(diff_add == TRUE) -// { -// this->uwbFrameStartTimes64[0] = timestamp_add64(this->uwbFrameStartTimes64[0], diff_us/2); -// this->lastSlotStartTime64 = timestamp_add64(this->lastSlotStartTime64, diff_us/2); -// } -// else -// { -// this->uwbFrameStartTimes64[0] = timestamp_subtract64(this->uwbFrameStartTimes64[0], diff_us/2); -// this->lastSlotStartTime64 = timestamp_subtract64(this->lastSlotStartTime64, diff_us/2); -// } -//// } -// } -// -//} - - static bool tx_select(struct TDMAHandler *this) { -// uint8 debug_msg[100]; -// int n = sprintf((char *)&debug_msg, "tx_select,"); -// send_usbmessage(&debug_msg[0], n); -// usb_run(); - instance_data_t *inst = instance_get_local_structure_ptr(0); - uint32 time_now = portGetTickCnt(); //TODO come back and perhaps use only one or the other - uint32 time_now_us = portGetTickCntMicro(); //TODO see time_now - - //DISCOVERY pauses for BLINK_DELAY <-added -// if(this->waitForInf == TRUE || this->waitForRngInit == TRUE) -// { -// return FALSE; -// } + uint64 time_now_us = portGetTickCntMicro(); int uwb_index = 255; @@ -550,7 +293,7 @@ static bool tx_select(struct TDMAHandler *this) { //time to blink uwb_index = 255; - this->set_discovery_mode(this, WAIT_RNG_INIT, time_now); + this->set_discovery_mode(this, WAIT_RNG_INIT, portGetTickCnt()); } else { @@ -559,61 +302,18 @@ static bool tx_select(struct TDMAHandler *this) inst->testAppState = TA_RXE_WAIT; return TRUE; } - -// //start blinking if enough time has passed since entering DISCOVERY mode -// uint32 timeSinceDiscoverStart = get_dt32(this->discoveryStartTime, time_now); -// -// if(timeSinceDiscoverStart > this->waitInfDuration) -// { -// //enforce blink period -// uint32 timeSinceLastBlink = get_dt32(this->last_blink_time, time_now); -// -// // uint8 debug_msg[100]; -// // int n = sprintf((char *)&debug_msg, "timeSinceLastBlink %lu, BLINK_PERIOD_MS %lu ", timeSinceLastBlink, (uint32)BLINK_PERIOD_MS); -// // send_usbmessage(&debug_msg[0], n); -// // usb_run(); -// -// if(timeSinceLastBlink > (uint32)BLINK_PERIOD_MS + this->blinkPeriodRand) -// { -// //time to blink -// uwb_index = 255; -// this->set_discovery_mode(this, WAIT_RNG_INIT, time_now); -// } -// else -// { -// //not time to blink yet, keep waiting for RNG_INIT -// inst->wait4ack = 0; -// inst->testAppState = TA_RXE_WAIT; -// return TRUE; -// } -// } -// else -// { -// //shouldn't be in this mode, should be listening for INF messages -// inst->wait4ack = 0; -// inst->testAppState = TA_RXE_WAIT; -// return TRUE; -// } } else if(this->discovery_mode == SEND_SUG) { -// uint8 debug_msg[100]; -// int n = sprintf((char *)&debug_msg, "discovery send sug"); -// send_usbmessage(&debug_msg[0], n); -// usb_run(); - //get time since slot start and make sure that's greater than delay uint64 timeSinceSlotStart = get_dt64(this->lastSlotStartTime64, time_now_us); - //make sure that we are in slot 0 - if(timeSinceSlotStart <= this->slotStartDelay_us) { uwb_index = -1; } else { - //TODO figure out how to make sure we send our SUG packet at the right time... inst->wait4ack = 0; inst->testAppState = TA_TXINF_WAIT_SEND; inst->uwbToRangeWith = (uint8)255; @@ -651,7 +351,7 @@ static bool tx_select(struct TDMAHandler *this) { if(this->uwbListTDMAInfo[i].connectionType == UWB_LIST_NEIGHBOR) { - uint32 timeSinceRange = get_dt32(this->uwbListTDMAInfo[i].lastRange, time_now); + uint32 timeSinceRange = get_dt32(this->uwbListTDMAInfo[i].lastRange, portGetTickCnt()); if(timeSinceOldestRange == 0) { @@ -699,12 +399,6 @@ static bool tx_select(struct TDMAHandler *this) return TRUE; } -// debug_msg[100]; -// n = sprintf((char *)&debug_msg, "selected index %i ", uwb_index); -// send_usbmessage(&debug_msg[0], n); -// usb_run(); - - if(uwb_index < 1) //set to -1 when waiting for the slotStartDelay to pass { //do nothing @@ -778,7 +472,6 @@ static void populate_inf_msg(struct TDMAHandler *this, uint8 inf_msg_type) int num_neighbors = instfindnumneighbors(inst); int num_hidden = instfindnumhidden(inst); -// uint32 time_now = portGetTickCnt(); //fcode int msgDataIndex = FCODE; @@ -786,16 +479,7 @@ static void populate_inf_msg(struct TDMAHandler *this, uint8 inf_msg_type) //time since frame start - //populated directly before being sent -// msgDataIndex = TDMA_TSFS; -// uint32 timeSinceFrameStart = get_dt32(this->uwbFrameStartTimes[0], time_now); //TODO handle number wrapping -// memcpy(&inst->inf_msg.messageData[msgDataIndex], &timeSinceFrameStart, sizeof(uint32)); - - -// uint8 debug_msg[100]; -// int n = sprintf((char*)&debug_msg[0], "TX MSG_INF timeSinceFrameStart: %lu", timeSinceFrameStart); -// send_usbmessage(&debug_msg[0], n); -// usb_run(); + //populated immediately before being sent //number of neighbors msgDataIndex = TDMA_NUMN; @@ -805,7 +489,6 @@ static void populate_inf_msg(struct TDMAHandler *this, uint8 inf_msg_type) msgDataIndex = TDMA_NUMH; memcpy(&inst->inf_msg.messageData[msgDataIndex], &num_hidden, sizeof(uint8)); - //self framelength msgDataIndex = TDMA_FRAMELENGTH; memcpy(&inst->inf_msg.messageData[msgDataIndex], &this->uwbListTDMAInfo[0].framelength, sizeof(uint8)); @@ -825,7 +508,6 @@ static void populate_inf_msg(struct TDMAHandler *this, uint8 inf_msg_type) //neighbor address, framelength, number of slots, and slot assignments for(int i = 1; i < inst->uwbListLen; i++) //slot 0 reserved for self { -// if(inst->uwbListType[i] == UWB_LIST_NEIGHBOR) if(this->uwbListTDMAInfo[i].connectionType == UWB_LIST_NEIGHBOR) { struct TDMAInfo *info = &this->uwbListTDMAInfo[i]; @@ -854,7 +536,6 @@ static void populate_inf_msg(struct TDMAHandler *this, uint8 inf_msg_type) //hidden address, framelength, number of slots, and slot assignments for(int i = 1; i < inst->uwbListLen; i++) //slot 0 reserved for self { -// if(inst->uwbListType[i] == UWB_LIST_HIDDEN) if(this->uwbListTDMAInfo[i].connectionType == UWB_LIST_HIDDEN) { struct TDMAInfo *info = &this->uwbListTDMAInfo[i]; @@ -892,11 +573,8 @@ static void populate_inf_msg(struct TDMAHandler *this, uint8 inf_msg_type) static void update_inf_tsfs(struct TDMAHandler *this) { - //TODO check if tsfs is greater than frameDuration, update if so - instance_data_t *inst = instance_get_local_structure_ptr(0); uint64 time_now_us = portGetTickCntMicro(); - int msgDataIndex = TDMA_TSFS; uint64 timeSinceFrameStart64 = get_dt64(this->uwbListTDMAInfo[0].frameStartTime, time_now_us); uint64 frameDuration = this->slotDuration_us*this->uwbListTDMAInfo[0].framelength; @@ -915,11 +593,11 @@ static void update_inf_tsfs(struct TDMAHandler *this) timeSinceFrameStart64 = 0; } - memcpy(&inst->inf_msg.messageData[msgDataIndex], &timeSinceFrameStart64, 6); - + memcpy(&inst->inf_msg.messageData[TDMA_TSFS], &timeSinceFrameStart64, 6); } +//TODO check below for accuracy //Procedure for processing INF SUG, INF REG, and INF UPDATE //1. Check for differences with locally stored TDMA assignment information // (a) exit if none exist @@ -940,12 +618,6 @@ static bool process_inf_msg(struct TDMAHandler *this, uint8 *messageData, uint8 { //NOTE: this function does not handle TDMA deconflict -// uint8 debug_msg[100]; -// int n = sprintf((char*)&debug_msg[0], "process INF"); -// send_usbmessage(&debug_msg[0], n); -// usb_run(); - - bool tdma_modified = FALSE; uint32 time_now = portGetTickCnt(); @@ -964,7 +636,6 @@ static bool process_inf_msg(struct TDMAHandler *this, uint8 *messageData, uint8 safeAssign = TRUE; } - uint8 inf_msg_type; memcpy(&inf_msg_type, &messageData[FCODE], sizeof(uint8)); @@ -994,7 +665,6 @@ static bool process_inf_msg(struct TDMAHandler *this, uint8 *messageData, uint8 int msgDataIndex = TDMA_NUMS + 1; - //TODO have deconflict mode??? bool uwbListInMsg[UWB_LIST_SIZE]; for(int i = 0; i < inst->uwbListLen; i++) { @@ -1043,7 +713,8 @@ static bool process_inf_msg(struct TDMAHandler *this, uint8 *messageData, uint8 if(mode == CLEAR_LISTED_COPY) { - this->free_slots(info); //do after cheking framelength because framelength will be reset + //do after cheking framelength because framelength will be reset + this->free_slots(info); } info->framelength = MAX(framelength, info->framelength); @@ -1055,14 +726,8 @@ static bool process_inf_msg(struct TDMAHandler *this, uint8 *messageData, uint8 msgDataIndex++; this->assign_slot(info, slot, safeAssign); - -// if(this->assign_slot(info, slot) == TRUE) -// { -// tdma_modified = TRUE; //TODO this is not right, it should somehow compare before and after... -// } } - for(int i = 0; i < numNeighbors; i++) { uint8 address[8] = {0, 0, 0, 0, 0, 0, 0, 0}; @@ -1072,21 +737,17 @@ static bool process_inf_msg(struct TDMAHandler *this, uint8 *messageData, uint8 uint8 uwb_index = instgetuwblistindex(inst, &address[0], inst->addrByteSize); if(uwb_index != 0) { -// if(inst->uwbListType[uwb_index] == UWB_LIST_INACTIVE || inst->uwbListType[uwb_index] == UWB_LIST_TWICE_HIDDEN) if(this->uwbListTDMAInfo[uwb_index].connectionType == UWB_LIST_INACTIVE || this->uwbListTDMAInfo[uwb_index].connectionType == UWB_LIST_TWICE_HIDDEN) { -// inst->uwbListType[uwb_index] = UWB_LIST_HIDDEN; this->uwbListTDMAInfo[uwb_index].connectionType = UWB_LIST_HIDDEN; } -// inst->lastHiddenTimeStamp[uwb_index] = time_now; this->uwbListTDMAInfo[uwb_index].lastCommHidden = time_now; } info = &this->uwbListTDMAInfo[uwb_index]; uwbListInMsg[uwb_index] = TRUE; - memcpy(&framelength, &messageData[msgDataIndex], sizeof(uint8)); msgDataIndex++; memcpy(&numSlots, &messageData[msgDataIndex], sizeof(uint8)); @@ -1120,7 +781,8 @@ static bool process_inf_msg(struct TDMAHandler *this, uint8 *messageData, uint8 if(mode == CLEAR_LISTED_COPY) { - this->free_slots(info); //do after checking framelength because framelength reset + //do after checking framelength because framelength reset + this->free_slots(info); } info->framelength = MAX(framelength, info->framelength); @@ -1132,10 +794,6 @@ static bool process_inf_msg(struct TDMAHandler *this, uint8 *messageData, uint8 msgDataIndex++; this->assign_slot(info, slot, safeAssign); -// if(this->assign_slot(info, slot) == TRUE) -// { -// tdma_modified = TRUE; //TODO also not right! need to check modification -// } } } @@ -1148,14 +806,11 @@ static bool process_inf_msg(struct TDMAHandler *this, uint8 *messageData, uint8 uint8 uwb_index = instgetuwblistindex(inst, &address[0], inst->addrByteSize); if(uwb_index != 0)//0 reserved for self { -// if(inst->uwbListType[uwb_index] == UWB_LIST_INACTIVE) if(this->uwbListTDMAInfo[uwb_index].connectionType == UWB_LIST_INACTIVE) { -// inst->uwbListType[uwb_index] = UWB_LIST_TWICE_HIDDEN; this->uwbListTDMAInfo[uwb_index].connectionType = UWB_LIST_TWICE_HIDDEN; } -// inst->lastTwiceHiddenTimeStamp[uwb_index] = time_now; this->uwbListTDMAInfo[uwb_index].lastCommTwiceHidden = time_now; } @@ -1195,27 +850,21 @@ static bool process_inf_msg(struct TDMAHandler *this, uint8 *messageData, uint8 if(mode == CLEAR_LISTED_COPY) { - this->free_slots(info); //do after checking for difference because will reset framelength as well + //do after checking for difference because will reset framelength as well + this->free_slots(info); } info->framelength = MAX(framelength, info->framelength); msgDataIndex = msgDataIndexSave; -// int msgDataIndex = msgDataIndexSave; for(int s = 0; s < numSlots; s++) { memcpy(&slot, &messageData[msgDataIndex], sizeof(uint8)); msgDataIndex++; this->assign_slot(info, slot, safeAssign); -// if(this->assign_slot(info, slot) == TRUE) //the only problem i see with this is that it does not give me a good way to isolate which ones were or weren't modified, this is a problem for deconflict logic... -// { -// tdma_modified = TRUE; -// } } } - - //handle deconflict??? if(mode == CLEAR_LISTED_COPY) { //deconflict uncopied against copied. (excluding self) @@ -1225,11 +874,6 @@ static bool process_inf_msg(struct TDMAHandler *this, uint8 *messageData, uint8 { if((uwbListInMsg[i] == FALSE && uwbListInMsg[j] == TRUE) || (uwbListInMsg[i] == TRUE && uwbListInMsg[j] == FALSE)) { - -// if((inst->uwbListType[i] == UWB_LIST_NEIGHBOR && inst->uwbListType[j] == UWB_LIST_NEIGHBOR) || -// (inst->uwbListType[i] == UWB_LIST_NEIGHBOR && inst->uwbListType[j] == UWB_LIST_HIDDEN) || -// (inst->uwbListType[j] == UWB_LIST_NEIGHBOR && inst->uwbListType[i] == UWB_LIST_NEIGHBOR) || -// (inst->uwbListType[j] == UWB_LIST_NEIGHBOR && inst->uwbListType[i] == UWB_LIST_HIDDEN)) if((this->uwbListTDMAInfo[i].connectionType == UWB_LIST_NEIGHBOR && this->uwbListTDMAInfo[j].connectionType == UWB_LIST_NEIGHBOR) || (this->uwbListTDMAInfo[i].connectionType == UWB_LIST_NEIGHBOR && this->uwbListTDMAInfo[j].connectionType == UWB_LIST_HIDDEN) || (this->uwbListTDMAInfo[j].connectionType == UWB_LIST_NEIGHBOR && this->uwbListTDMAInfo[i].connectionType == UWB_LIST_NEIGHBOR) || @@ -1258,530 +902,9 @@ static bool process_inf_msg(struct TDMAHandler *this, uint8 *messageData, uint8 } } -// uint32 deltat = get_dt32(time_now, portGetTickCnt()); - -// if(deltat > 1) -// { -// uint8 debug_msg[100]; -// int n = sprintf((char *)&debug_msg, "process_inf_time:%lu,%04X,xxxx", deltat, inst->uwbShortAdd); -// send_usbmessage(&debug_msg[0], n); -// usb_run(); -// } - return tdma_modified; } - -static bool check_tdma_diff(struct TDMAHandler *this, uint8 *messageData, uint8 *srcIndex) -{ - return TRUE; - -// struct TDMAInfo info = &this->uwbListTDMAInfo[srcIndex]; -// -// if(info->framelength != framelength || info->slotsLength != numSlots) -// { -// return TRUE; -// } -// -// for(int s = 0; s < numSlots; s++) -// { -// memcpy(&slot, &messageData[msgDataIndex], 1); -// msgDataIndex++; -// -// if(this->slot_assigned(info, slot) == FALSE) -// { -// return TRUE; -// } -// } -// -// for(int i = 0; i < numNeighbors; i++) -// { -// memcpy(&address, &messageData[msgDataIndex], inst->addrByteSize); -// msgDataIndex += inst->addrByteSize; -// -// uint8 uwb_index = instgetuwblistindex(inst, &address[0], inst->addrByteSize); -// info = &this->uwbListTDMAInfo[uwb_index]; -// -// memcpy(&framelength, &messageData[msgDataIndex], 1); -// msgDataIndex++; -// -// memcpy(&numSlots, &messageData[msgDataIndex], 1); -// msgDataIndex++; -// -// if(info->framelength != framelength || info->slotsLength != numSlots) -// { -// return TRUE; -// } -// -// for(int s = 0; s < numSlots; s++) -// { -// memcpy(&slot, &messageData[msgDataIndex], 1); -// msgDataIndex++; -// -// if(this->slot_assigned(info, slot) == FALSE) -// { -// return TRUE; -// } -// } -// } -// -// for(int i = 0; i < numHidden; i++) -// { -// memcpy(&address, &messageData[msgDataIndex], inst->addrByteSize); -// msgDataIndex += inst->addrByteSize; -// -// uint8 uwb_index = instgetuwblistindex(inst, &address[0], inst->addrByteSize); -// -// info = &this->uwbListTDMAInfo[uwb_index]; -// -// memcpy(&framelength, &messageData[msgDataIndex], 1); -// msgDataIndex++; -// -// memcpy(&numSlots, &messageData[msgDataIndex], 1); -// msgDataIndex++; -// -// if(info->framelength != framelength || info->slotsLength != numSlots) -// { -// return TRUE; -// } -// -// for(int s = 0; s < numSlots; s++) -// { -// memcpy(&slot, &messageData[msgDataIndex], 1); -// msgDataIndex++; -// -// if(this->slot_assigned(info, slot) == FALSE) -// { -// return TRUE; -// } -// } -// } -// -// -// return FALSE; -} - - - -//TODO add conflict detection/resolution!!! -//static void populate_sug_msg(struct TDMAHandler *this) -//{ -// instance_data_t *inst = instance_get_local_structure_ptr(0); -// -// //if I'm here, i should clear my own assignment information -// if(this->mySlots != NULL && this->mySlotsLength != 0) -// { -// this->mySlotsLength = 0; -// free(this->mySlots); -// this->mySlots = NULL; -// } -// uint8 unassigned = 255; -// for(int i = 0; i < this->maxFramelength; i++) -// { -// memcpy(&this->slotAssignments[i], &unassigned, sizeof(uint8)); -// } -// this->framelength = MIN_FRAMELENGTH; -// -// -// -// -// inst->inf_msg.messageData[FCODE] = RTLS_DEMO_MSG_INF_SUG; -// int msgDataIndex = FCODE + 1; -// -// int num_neighbors = instfindnumactiveneighbors(inst); -// uint32 time_now = portGetTickCnt(); -// -// -// //TODO build framelength after creating slot assignments... -//// //framelength -//// memcpy(&inst->inf_msg.messageData[msgDataIndex], &this->framelength, 1); -// msgDataIndex++; -// -// uint32 timeSinceFrameStart = get_dt32(this->frameStartTime, time_now); //TODO handle number wrapping -// memcpy(&inst->inf_msg.messageData[msgDataIndex], &timeSinceFrameStart, sizeof(uint32)); -// msgDataIndex += sizeof(timeSinceFrameStart); -// -//// uint8 debug_msg[100]; -//// int n = sprintf((char*)&debug_msg[0], "TX MSG_INF timeSinceFrameStart: %lu", timeSinceFrameStart); -//// send_usbmessage(&debug_msg[0], n); -//// usb_run(); -// -// -// //number of neighbors -// memcpy(&inst->inf_msg.messageData[msgDataIndex], &num_neighbors, 1); -// msgDataIndex++; -// -// //neighbor addresses -// for(int i = 0; i < inst->uwbListLen; i++) -// { -// if(inst->uwbListType[i] == UWB_LIST_NEIGHBOR) -// { -// memcpy(&inst->inf_msg.messageData[msgDataIndex], &inst->uwbList[i][0], inst->addrByteSize); -// msgDataIndex += inst->addrByteSize; -// } -// } -// -// //neighbor framelength -// for(int i = 0; i < inst->uwbListLen; i++) -// { -// if(inst->uwbListType[i] == UWB_LIST_NEIGHBOR) -// { -// memcpy(&inst->inf_msg.messageData[msgDataIndex], &this->uwbFramelengths[i], 1); -// msgDataIndex++; -// } -// } -// -// //addresses in each TDMA slot -//// for(int i = 0; i < this->framelength; i++) -//// { -//// memcpy(&inst->inf_msg.messageData[msgDataIndex], &this->slotAssignments[i], 2); -//// -//// msgDataIndex += 2; -//// } -// -// //TODO check for conflicts at some point -// -// //use the slotAssignements array to combine the collected information -// //note: try to keep framelength as small as possible -// //note: if we have to increase the framelength, start over! -// -//// uint8 mfl = this->framelength; -//// -//// uint8 debug_msg[100]; -//// int n = sprintf((char*)&debug_msg[0], "this->framelength: %u", this->framelength); -//// send_usbmessage(&debug_msg[0], n); -//// usb_run(); -// -// //getting stuck in here... somehow this->framelength is getting set to 0 -// for(int i = 0; i < inst->uwbListLen; i++) -// { -// if(inst->uwbListType[i] != UWB_LIST_INACTIVE) -// { -// uint8 framelength_i = this->uwbFramelengths[i]; -// for(int j = 0; j < this->uwbListSlotsLengths[i]; j++) -// { -// //get slot -// uint8 slot_j; -// bool double_frame = FALSE; -// memcpy(&slot_j, &this->uwbListSlots[i][j], 1); -// -// -// //check if slot is taken -// if(slot_j >= this->framelength) -// { -// uint8 mod_slot = slot_j%this->framelength; -// if(mod_slot == 0) -// { -// //slots not allowed to be assigned to the zeroth slot -// double_frame = TRUE; -// } -// else -// { -//// if(memcmp(&this->slotAssignments[mod_slot], &zero, sizeof(uint8)) == 0) -// if(memcmp(&this->slotAssignments[mod_slot], &unassigned, sizeof(uint8)) == 0) -// { -// //slot not assigned -// memcpy(&this->slotAssignments[mod_slot], &i, sizeof(uint8)); -// } -// else if(memcmp(&this->slotAssignments[mod_slot], &i, sizeof(uint8)) != 0) -// { -// //already assigned to another UWB, -// //double the frame and start over!(?) should I instead consider framelengths and such? -// double_frame = TRUE; -// } -// } -// -// } -// else if(slot_j < this->framelength) -// { -// while(slot_j < this->framelength) -// { -// if(memcmp(&this->slotAssignments[slot_j], &unassigned, sizeof(uint8)) == 0) -// { -// //slot not assigned -// memcpy(&this->slotAssignments[slot_j], &i, sizeof(uint8)); -// } -// else if(memcmp(&this->slotAssignments[slot_j], &i, sizeof(uint8)) != 0) -// { -// //already assigned to another UWB, -// //double the frame and start over!(?) should I instead consider framelengths and such? -// double_frame = TRUE; -// break; -// } -// slot_j += framelength_i; -// } -// } -// -// if(double_frame == TRUE) -// { -// this->framelength *= 2; -// i = 0; -// j = 0; -// } -// -// // uint16 uwbShortAdd = address64to16(&inst->uwbList[i][0]); -// // uint8 index = msgDataIndex + 2*this->uwbListSlots[i][j]; -// // memcpy(&inst->inf_msg.messageData[msgDataIndex + (int)(inst->addrByteSize*this->uwbListSlots[i][j])], &uwbShortAdd, 2); -// } -// } -// -// } -// -// //if we are here, then we should've successfully combined the INF message information -// -// -// -// bool assigned = FALSE; -// while(assigned == FALSE) -// { -// //first try GU (Get Unassigned) slot -// for(int i = 1; i < this->framelength; i++)//start at i = 1 because 0 is a reserved slot -// { -//// if(memcmp(&this->slotAssignments[i], &zero, sizeof(uint8)) == 0) -// if(memcmp(&this->slotAssignments[i], &unassigned, sizeof(uint8)) == 0) -// { -// //empty slot, assign -// this->assign_slot(&this->myTDMAInfo, i); -// assigned = TRUE; -// } -// } -// -// //no open slots for GU -// //next try RMA (Release Multiple Assigned) slot -// if(assigned == FALSE) -// { -// uint8 most_slots = 1; -// uint8 most_slots_index = 255; -// uint8 release_slot = 255; -// uint8 assign_slot = 255; -// for(int i = 0; i < inst->uwbListLen; i++) -// { -// //TODO do I only need to consider neighbors or also hidden? -// if(inst->uwbListType[i] != UWB_LIST_INACTIVE) -// { -// uint8 uwbSlotsLen; -// memcpy(&uwbSlotsLen, &this->uwbListSlotsLengths[i], sizeof(uint8)); -// if(uwbSlotsLen > most_slots) -// { -// bool slot_found = FALSE; -// //check if the slot to be released can be assigned within our framelength -// for(int j = 0; j < uwbSlotsLen; j++) -// { -// uint8 slot; -// memcpy(&slot, &this->uwbListSlots[i][j], sizeof(uint8)); -// uint8 mod_slot = slot%this->framelength; -// -// if(mod_slot != 0 && memcmp(&this->slotAssignments[mod_slot], &i, sizeof(uint8)) == 0) -// { -// release_slot = slot; -// assign_slot = mod_slot; -// slot_found = TRUE; -// break; -// } -// } -// -// if(slot_found == TRUE) -// { -// most_slots = uwbSlotsLen; -// most_slots_index = i; -// } -// } -// } -// } -// -// if(most_slots_index != 255) //what about self? 254? -// { -// this->assign_slot(&this->myTDMAInfo, assign_slot); -// this->uwblist_free_slot(this, most_slots_index, release_slot); -// memcpy(&this->slotAssignments[assign_slot], &this->slotAssingmentSelfIndex, sizeof(uint8)); -// -// assigned = TRUE; -// } -// } -// -// //no slots released via RMA -// //DF (Double Frame), and assign via GU -// if(assigned == FALSE) -// { -// memcpy(&this->slotAssignments[(int)this->framelength], &this->slotAssingmentSelfIndex, sizeof(uint8)); -// -// for(int i = 1; i < this->framelength; i++) -// { -// memcpy(&this->slotAssignments[i + (int)this->framelength], &this->slotAssignments[i], sizeof(uint8)); -// } -// this->framelength *= 2; -// -// assigned = TRUE; -// } -// } -// -// for(int i = 0; i < this->framelength; i++) -// { -// //TODO handle the case where we are assigned to the slot -// uint8 slot_index; -// memcpy(&slot_index, &this->slotAssignments[i], sizeof(uint8)); -// memcpy(&inst->inf_msg.messageData[msgDataIndex], &inst->uwbList[slot_index], sizeof(inst->addrByteSize)); -// msgDataIndex += sizeof(inst->addrByteSize); -// } -// -// memcpy(&inst->inf_msg.messageData[FCODE + 1], &this->framelength, 1); -// -// dwt_setrxtimeout((uint16)0); -//// this->usb_dump_tdma(this); -// -// -//} - - -//void process_sug_msg(struct TDMAHandler *this, uint8 *messageData, uint8 *srcAddr) -//{ -// //how to compare and integrate changes??? -// //first add any addresses to my list -// instance_data_t *inst = instance_get_local_structure_ptr(0); -// -// uint8 srcIndex = instgetuwblistindex(inst, &srcAddr[0], inst->addrByteSize); -// inst->uwbListType[srcIndex] = UWB_LIST_NEIGHBOR; -// uint8 neighborAddressN[8] = {0, 0, 0, 0, 0, 0, 0, 0}; -// uint8 neighborFramelengthN; -// uint8 slotAddress[8] = {0, 0, 0, 0, 0, 0, 0, 0}; -// uint8 blankAddress[8] = {0, 0, 0, 0, 0, 0, 0, 0}; -// uint32 timeSinceFrameStart; -// -//// if(tdma_handler->discovery_mode == WAIT_INF_REG) -//// { -//// tdma_handler->tdma_free_all_slots(tdma_handler); -//// } -// -// int msgDataIndex = FCODE + 1; -// memcpy(&tdma_handler->uwbFramelengths[srcIndex], &messageData[msgDataIndex], 1); -// msgDataIndex++; -// -// //TODO use range to help sync frame start time -// memcpy(&timeSinceFrameStart, &messageData[msgDataIndex], sizeof(timeSinceFrameStart)); -// msgDataIndex += sizeof(timeSinceFrameStart); -// -// -// memcpy(&srcNumNeighbors, &messageData[msgDataIndex], 1); -// msgDataIndex++; -// -// int fl_index_offset = (int)(inst->addrByteSize*srcNumNeighbors); -// for(int n = 0; n < srcNumNeighbors; n++) -// { -// int framelength_index = msgDataIndex + fl_index_offset; -// memcpy(&neighborAddressN, &messageData[msgDataIndex], inst->addrByteSize); -// memcpy(&neighborFramelengthN, &messageData[framelength_index], sizeof(neighborFramelengthN)); -// -// msgDataIndex += (int)inst->addrByteSize; -// -// uint8 uwb_index = instgetuwblistindex(inst, &neighborAddressN[0], inst->addrByteSize); -// if(inst->uwbListType[uwb_index] != UWB_LIST_NEIGHBOR) -// { -// inst->uwbListType[uwb_index] = UWB_LIST_HIDDEN; -// } -// -// tdma_handler->uwbFramelengths[uwb_index] = neighborFramelengthN; -// } -// msgDataIndex += sizeof(uint8)*srcNumNeighbors; -// -// -// //now process the slot assignments -// for(int i = 0; i < tdma_handler->uwbFramelengths[srcIndex]-1; i++)//zeroth slot not included in INF -// { -// uint8 slot = i + 1; -// memcpy(&slotAddress, &messageData[msgDataIndex], inst->addrByteSize); -// //see who the address belongs to -//#if (USING_64BIT_ADDR==0) -// if(memcmp(&inst->uwbShortAdd, &slotAddress, inst->addrByteSize) == 0) -//#else -// if(memcmp(&inst->eui64[0], &slotAddress, inst->addrByteSize) == 0) -//#endif -// { -// //assignment is self -// } -// else -// { -// //assignment in UWB list if not unassigned -// if(memcmp(&inst->eui64[0], &blankAddress, inst->addrByteSize) != 0) -// { -// //find in our list (or add as hidden) -// uint8 uwb_index = instgetuwblistindex(inst, &slotAddress[0], inst->addrByteSize); -// if(inst->uwbListType[uwb_index] != UWB_LIST_NEIGHBOR) -// { -// inst->uwbListType[uwb_index] = UWB_LIST_HIDDEN; -// } -// -// //what to do with the information??? -// //TODO left off here!!! -//// tdma_handler->uwblist_assign_slot(tdma_handler, uwb_index, slot); -// } -// -// -// -// -// } -// -// msgDataIndex += (int)inst->addrByteSize; -// } -// -// -// //account for the delay between transmission and reception of the INF message -// //TODO make this a function of the data rate as well! -// //TODO experimentally found this number to be +- 1 millisecond for some reason. figure out why -// //experimentally found formula is 0.079667x + 0.85611 -// uint32 txrx_delay = (uint32)(0.079667*(float)(dw_event->rxLength) + 0.85611); //TODO make this function of message length! -// -// -// tdma_handler->uwbFrameStartTimes[srcIndex] = time_now - (timeSinceFrameStart + txrx_delay); -// -// -// uint32 myFramelengthDuration = tdma_handler->framelength*tdma_handler->slotDuration; -// uint32 myTimeSinceFrameStart = get_dt32(tdma_handler->frameStartTime, time_now); -// -// //NEW -// if(tdma_handler->framelength <= tdma_handler->uwbFramelengths[srcIndex]) -// { -// uint32 timeSinceFrameStartMod = (timeSinceFrameStart + txrx_delay)%myFramelengthDuration; -// -// if(myTimeSinceFrameStart > timeSinceFrameStartMod) -// { -// uint32 diff = myTimeSinceFrameStart - timeSinceFrameStartMod; -// tdma_handler->frameStartTime += diff/2; -// tdma_handler->lastSlotStartTime += diff/2; -// } -// else -// { -// uint32 diff = timeSinceFrameStartMod - myTimeSinceFrameStart; -// tdma_handler->frameStartTime -= diff/2; -// tdma_handler->lastSlotStartTime -= diff/2; -// -// } -// } -// else -// { -// uint32 hisFramelengthDuration = tdma_handler->uwbFramelengths[srcIndex]*tdma_handler->slotDuration; -// uint32 myTimeSinceFrameStartMod = (myTimeSinceFrameStart - txrx_delay)%hisFramelengthDuration; -// -// if(timeSinceFrameStart > myTimeSinceFrameStartMod) -// { -// uint32 diff = timeSinceFrameStart - myTimeSinceFrameStartMod; -// tdma_handler->frameStartTime -= diff/2; -// tdma_handler->lastSlotStartTime -= diff/2; -// } -// else -// { -// uint32 diff = myTimeSinceFrameStartMod - timeSinceFrameStart; -// tdma_handler->frameStartTime += diff/2; -// tdma_handler->lastSlotStartTime += diff/2; -// } -// } -// -// -// -// -//} - - - static bool poll_delay(struct TDMAHandler *this, uint32 time_now_offset, uint32 offset) { bool delay = FALSE; @@ -1854,6 +977,7 @@ static bool assign_slot(struct TDMAInfo *info, uint8 slot, bool safeAssign) return retval; } +//TODO check below and clean up description //finding and assigning a slot works according to the following: //1.) Set framelength to 4 //2.) Get Unassigned Slots (GU) @@ -1960,11 +1084,11 @@ static void find_assign_slot(struct TDMAHandler *this) //RMA //find UWB with greatest number of slot assignments - uint8 max_assignments = 0; //TODO may need uint16? + uint8 max_assignments = 0; uint8 max_uwb_index = 255; for(uint8 u = 1; u < inst->uwbListLen; u++)//0 reserved for self { - uint8 slotsLength = this->uwbListTDMAInfo[u].slotsLength; //TODO may need uint16? + uint8 slotsLength = this->uwbListTDMAInfo[u].slotsLength; if(info->framelength > this->uwbListTDMAInfo[u].framelength && this->uwbListTDMAInfo[u].slotsLength != 0) { slotsLength *= info->framelength/this->uwbListTDMAInfo[u].framelength; @@ -1984,7 +1108,7 @@ static void find_assign_slot(struct TDMAHandler *this) uint8 mod_slot = slot % info->framelength; this->assign_slot(info, mod_slot, TRUE); - this->deconflict_uwb_pair(this, info, &this->uwbListTDMAInfo[max_uwb_index]); //TODO consider handling differently (could maybe be more efficient?) + this->deconflict_uwb_pair(this, info, &this->uwbListTDMAInfo[max_uwb_index]); assignment_made = TRUE; } @@ -2001,23 +1125,16 @@ static void find_assign_slot(struct TDMAHandler *this) static void build_new_network(struct TDMAHandler *this) { instance_data_t *inst = instance_get_local_structure_ptr(0); -// uint32 time_now = portGetTickCnt(); - uint32 time_now_us = portGetTickCntMicro(); + uint64 time_now_us = portGetTickCntMicro(); bool safeAssign = FALSE; //clear all tdma information this->tdma_free_all_slots(this); //build the initial TDMA -// this->uwbFrameStartTimes_stm32[0] = time_now - this->slotDuration_ms;//TODO handle timer wrapping... -// this->lastSlotStartTime_stm32 = time_now; - - //build the initial TDMA -// this->uwbFrameStartTimes64[0] = timestamp_subtract64(time_now_us, this->slotDuration_us); this->uwbListTDMAInfo[0].frameStartTime = timestamp_subtract64(time_now_us, this->slotDuration_us); this->lastSlotStartTime64 = time_now_us; - this->uwbListTDMAInfo[0].framelength = (uint8)MIN_FRAMELENGTH; this->uwbListTDMAInfo[inst->uwbToRangeWith].framelength = (uint8)MIN_FRAMELENGTH; @@ -2038,19 +1155,13 @@ static bool deconflict_slot_assignments(struct TDMAHandler *this) //first deconflict slots in neighbor, hidden, and twice hidden for(int i = 1; i < inst->uwbListLen; i++)//0 reserved for self { -// if(inst->uwbListType[i] != UWB_LIST_INACTIVE) if(this->uwbListTDMAInfo[i].connectionType != UWB_LIST_INACTIVE) { for(int j = i+1; j < inst->uwbListLen; j++) { -// if(inst->uwbListType[j] != UWB_LIST_INACTIVE && j != i) if(this->uwbListTDMAInfo[j].connectionType != UWB_LIST_INACTIVE && j != i) { //first check if their list type requires deconflicting -// if((inst->uwbListType[i] == UWB_LIST_NEIGHBOR && inst->uwbListType[j] == UWB_LIST_TWICE_HIDDEN) || -// (inst->uwbListType[j] == UWB_LIST_NEIGHBOR && inst->uwbListType[i] == UWB_LIST_TWICE_HIDDEN) || -// (inst->uwbListType[i] == UWB_LIST_TWICE_HIDDEN && inst->uwbListType[j] == UWB_LIST_TWICE_HIDDEN) || -// (inst->uwbListType[i] == UWB_LIST_HIDDEN && inst->uwbListType[j] == UWB_LIST_HIDDEN)) if((this->uwbListTDMAInfo[i].connectionType == UWB_LIST_NEIGHBOR && this->uwbListTDMAInfo[j].connectionType == UWB_LIST_TWICE_HIDDEN) || (this->uwbListTDMAInfo[j].connectionType == UWB_LIST_NEIGHBOR && this->uwbListTDMAInfo[i].connectionType == UWB_LIST_TWICE_HIDDEN) || (this->uwbListTDMAInfo[i].connectionType == UWB_LIST_TWICE_HIDDEN && this->uwbListTDMAInfo[j].connectionType == UWB_LIST_TWICE_HIDDEN) || @@ -2080,11 +1191,9 @@ static bool deconflict_slot_assignments(struct TDMAHandler *this) continue; } - //next deconflict slots between self and neighbor, hidden, and twice hidden for(int i = 1; i < inst->uwbListLen; i++)//0 reserved for self { -// if(inst->uwbListType[i] != UWB_LIST_INACTIVE) if(this->uwbListTDMAInfo[i].connectionType != UWB_LIST_INACTIVE) { if(this->deconflict_uwb_pair(this, &this->uwbListTDMAInfo[0], &this->uwbListTDMAInfo[i])) @@ -2110,84 +1219,6 @@ static bool deconflict_slot_assignments(struct TDMAHandler *this) return conflict; } - -//bool indexed_deconflict_slot_assignments(struct TDMAHandler *this, bool selfDeconflict, bool *uwbListDeconflict) -//{ -// instance_data_t *inst = instance_get_local_structure_ptr(0); -// bool conflict = FALSE; -// -// while(TRUE) -// { -// bool conflict_this_iter = FALSE; -// //first deconflict slots in neighbor, hidden, and twice hidden -// for(int i = 0; i < inst->uwbListLen; i++) -// { -// if(inst->uwbListType[i] != UWB_LIST_INACTIVE) -// { -// for(int j = i+1; j < inst->uwbListLen; j++) -// { -// if(inst->uwbListType[j] != UWB_LIST_INACTIVE && j != i) -// { -// //first check if their list type requires deconflicting -// if((inst->uwbListType[i] == UWB_LIST_NEIGHBOR && inst->uwbListType[j] == UWB_LIST_TWICE_HIDDEN) || -// (inst->uwbListType[j] == UWB_LIST_NEIGHBOR && inst->uwbListType[i] == UWB_LIST_TWICE_HIDDEN) || -// (inst->uwbListType[i] == UWB_LIST_TWICE_HIDDEN && inst->uwbListType[j] == UWB_LIST_TWICE_HIDDEN) || -// (inst->uwbListType[i] == UWB_LIST_HIDDEN && inst->uwbListType[j] == UWB_LIST_HIDDEN)) -// { -// continue; -// } -// -// if(this->deconflict_uwb_pair(this, &this->uwbListTDMAInfo[i], &this->uwbListTDMAInfo[j])) -// { -// conflict = TRUE; -// conflict_this_iter = TRUE; -// break; -// } -// } -// } -// } -// -// if(conflict_this_iter) -// { -// break; -// } -// } -// -// if(conflict_this_iter) -// { -// continue; -// } -// -// -// //next deconflict slots between self and neighbor, hidden, and twice hidden -// for(int i = 0; i < inst->uwbListLen; i++) -// { -// if(inst->uwbListType[i] != UWB_LIST_INACTIVE) -// { -// if(this->deconflict_uwb_pair(this, &this->myTDMAInfo, &this->uwbListTDMAInfo[i])) -// { -// conflict = TRUE; -// conflict_this_iter = TRUE; -// } -// } -// if(conflict_this_iter) -// { -// break; -// } -// } -// -// if(conflict_this_iter) -// { -// continue; -// } -// -// break; //no conflicts found this iteration, break out of while loop -// } -// -// return conflict; -//} - - //return true if a conflict was found static bool deconflict_uwb_pair(struct TDMAHandler *this, struct TDMAInfo *info_a, struct TDMAInfo *info_b) { @@ -2207,7 +1238,6 @@ static bool deconflict_uwb_pair(struct TDMAHandler *this, struct TDMAInfo *info_ uint8 slot_sb; memcpy(&slot_sb, &info_b->slots[sb], 1); - //check if slot is taken if(slot_sa >= info_b->framelength) { @@ -2264,10 +1294,9 @@ static bool deconflict_uwb_pair(struct TDMAHandler *this, struct TDMAInfo *info_ return conflict; } - -//TODO assign reference in class struct!!! same with others written today static void deconflict_slot_pair(struct TDMAHandler *this, struct TDMAInfo *info_a, struct TDMAInfo *info_b, uint8 slot_idx_a, uint8 slot_idx_b) { + //TODO double check for accruacy! //procedure for deconflicting slots (PDS) //1.) delete a conflicting slot // applicable if all but node with fewest slots has more than one slot assignment @@ -2359,9 +1388,6 @@ static bool self_conflict(struct TDMAHandler *this) for(int b = 1; b < inst->uwbListLen; b++) { -// if(inst->uwbListType[b] == UWB_LIST_NEIGHBOR || -// inst->uwbListType[b] == UWB_LIST_HIDDEN || -// inst->uwbListType[b] == UWB_LIST_TWICE_HIDDEN) if(this->uwbListTDMAInfo[b].connectionType == UWB_LIST_NEIGHBOR || this->uwbListTDMAInfo[b].connectionType == UWB_LIST_HIDDEN || this->uwbListTDMAInfo[b].connectionType == UWB_LIST_TWICE_HIDDEN) @@ -2447,7 +1473,6 @@ static void free_slot(struct TDMAInfo *info, uint8 slot) } } - return; } @@ -2469,10 +1494,6 @@ static void uwblist_free_slots(struct TDMAHandler *this, uint8 uwb_index) if(uwb_index >= inst->uwbListLen) { //out of bounds! -// uint8 debug_msg[100]; -// int n = sprintf((char*)&debug_msg[0], "uwblist_free_slots: uwb_index %u out of bounds", uwb_index); -// send_usbmessage(&debug_msg[0], n); -// usb_run(); return; } @@ -2499,12 +1520,8 @@ static void tdma_free_all_slots(struct TDMAHandler *this) } -//TODO static void enter_discovery_mode(struct TDMAHandler *this) { -// instance_data_t *inst = instance_get_local_structure_ptr(0); -// inst->inf_msg.messageData[FCODE] = RTLS_DEMO_MSG_INF_INIT; - uint32 time_now = portGetTickCnt(); this->discoveryStartTime = time_now; this->last_blink_time = time_now; @@ -2519,12 +1536,6 @@ static void set_discovery_mode(struct TDMAHandler *this, DISCOVERY_MODE discover this->discovery_mode_start_time = time_now; this->discovery_mode = discovery_mode; - -// uint8 debug_msg[100]; -// int n = sprintf((char*)&debug_msg[0], "set_discovery_mode: %s", get_discovery_modes_string(discovery_mode)); -// send_usbmessage(&debug_msg[0], n); -// usb_run(); - switch (discovery_mode)//TODO make sure all modes are captured here... { case WAIT_INF_REG: @@ -2554,20 +1565,12 @@ static void set_discovery_mode(struct TDMAHandler *this, DISCOVERY_MODE discover } case WAIT_SEND_SUG: { -// uint8 debug_msg[200]; -// int n = sprintf((char*)&debug_msg[0], "SET WAIT_SEND_SUG start"); -// send_usbmessage(&debug_msg[0], n); -// usb_run(); - //find common frame start time among neighbors instance_data_t *inst = instance_get_local_structure_ptr(0); - //TODO need to modify this. - //need to discriminate between different subnetworks. //count the number of UWBs that belong to each subnetwork //select the subnetwork with the largest number of UWBs //find the common frame start time among the UWBs in that network - //if subnetworks do indeed exist, set the appropriate rebase variables here and in our inf_message //keep track of which number each UWB belongs to //then keep track of which was selected so we can iterate over it later in this function @@ -2577,24 +1580,19 @@ static void set_discovery_mode(struct TDMAHandler *this, DISCOVERY_MODE discover uint8 sub_network_members[UWB_LIST_SIZE-1] = {0}; //cannot be more subnetworks than other UWBs uint64 sub_network_tsfs[UWB_LIST_SIZE-1] = {0}; uint8 sub_network_base_framelength[UWB_LIST_SIZE-1] = {0}; - //TODO handle number wrapping uint64 time_now_us = portGetTickCntMicro(); - uint64 tcommon; + uint64 tcommon = 0; uint64 shortestFrameDuration = this->maxFramelength*this->slotDuration_us; - for(int i=1; i < inst->uwbListLen; i++) //zero reserved for self { -// if(inst->uwbListType[i] != UWB_LIST_NEIGHBOR) if(this->uwbListTDMAInfo[i].connectionType != UWB_LIST_NEIGHBOR) { - continue; //TODO consider a way to account for hidden and twice hidden to determine which network is larger + continue; } struct TDMAInfo *info_i = &this->uwbListTDMAInfo[i]; -// uint64 framelengthDuration_us = info_i->framelength*this->slotDuration_us; -// uint64 timeSinceFrameStart_us = get_dt64(this->uwbFrameStartTimes64[i], time_now_us); uint64 timeSinceFrameStart_us = get_dt64(this->uwbListTDMAInfo[i].frameStartTime, time_now_us); if(timeSinceFrameStart_us > 100000000) @@ -2603,8 +1601,6 @@ static void set_discovery_mode(struct TDMAHandler *this, DISCOVERY_MODE discover continue; } - //if tsfs > framelengthDuration_us, reduce it by framelengthDuration_us - //test the ith UWB against the jth subnetwork for(int j=0; j < num_sub_networks; j++) { @@ -2672,11 +1668,6 @@ static void set_discovery_mode(struct TDMAHandler *this, DISCOVERY_MODE discover } } -// debug_msg[200]; -// n = sprintf((char*)&debug_msg[0], "checkpoint 1"); -// send_usbmessage(&debug_msg[0], n); -// usb_run(); - //now select the subnetwork with the greatest number of uwbs uint8 max_num = 0; for(int i=0; i < num_sub_networks; i++) @@ -2688,19 +1679,17 @@ static void set_discovery_mode(struct TDMAHandler *this, DISCOVERY_MODE discover } } - uint64 tnext[max_num]; //TODO zero? - uint8 neighborIndices[max_num]; //TODO zero? - uint64 latest_tnext; + uint64 tnext[max_num]; + uint8 neighborIndices[max_num]; + uint64 latest_tnext = 0; uint8 nidx = 0; uint64 slotDuration_us = this->slotDuration_us; for(int i = 1; i < inst->uwbListLen; i++)//0 reserved for self { -// if(inst->uwbListType[i] == UWB_LIST_NEIGHBOR && sub_network_membership[i] == sub_network_selected) if(this->uwbListTDMAInfo[i].connectionType == UWB_LIST_NEIGHBOR && sub_network_membership[i] == sub_network_selected) { neighborIndices[nidx] = i; -// tnext[nidx] = this->uwbFrameStartTimes64[i]; tnext[nidx] = this->uwbListTDMAInfo[i].frameStartTime; while(time_now_us > tnext[nidx]) //TODO handle number wrapping... { @@ -2715,11 +1704,6 @@ static void set_discovery_mode(struct TDMAHandler *this, DISCOVERY_MODE discover } } -// debug_msg[200]; -// n = sprintf((char*)&debug_msg[0], "checkpoint 2"); -// send_usbmessage(&debug_msg[0], n); -// usb_run(); - tcommon = tnext[0]; latest_tnext = tcommon; bool converged = FALSE; @@ -2748,18 +1732,8 @@ static void set_discovery_mode(struct TDMAHandler *this, DISCOVERY_MODE discover } } - -// if(num_sub_networks > 1) -// { -// //need indicate rebase in inf message so other networks will have a chance to rebase to the network selected by this UWB -// this->rebase_pending = TRUE; -// this->rebase_tx = FALSE; -// this->rebase_frameStartTime64 = latest_tnext; -// } - - //expire as the beginning of the common frame start time - this->discovery_mode_duration = (uint32)(get_dt64(time_now_us, latest_tnext)/1000); //TODO make duration in us? + this->discovery_mode_duration = (uint32)(get_dt64(time_now_us, latest_tnext)/1000); this->discovery_mode_expires = TRUE; this->deconflict_slot_assignments(this); @@ -2771,14 +1745,11 @@ static void set_discovery_mode(struct TDMAHandler *this, DISCOVERY_MODE discover //back-track the frame start time so we can inform the need to rebase //and keep in sync with the subnetwork we initially chose to sync with -// this->uwbFrameStartTimes64[0] = latest_tnext; this->uwbListTDMAInfo[0].frameStartTime = latest_tnext; //TODO handle number wrapping uint64 myFrameDuration = this->uwbListTDMAInfo[0].framelength*this->slotDuration_us; -// while(this->uwbFrameStartTimes64[0] > time_now_us) while(this->uwbListTDMAInfo[0].frameStartTime > time_now_us) { -// this->uwbFrameStartTimes64[0] -= myFrameDuration; this->uwbListTDMAInfo[0].frameStartTime -= myFrameDuration; } @@ -2786,13 +1757,12 @@ static void set_discovery_mode(struct TDMAHandler *this, DISCOVERY_MODE discover uint8 slot = myTimeSinceFrameStart/this->slotDuration_us; //integer division rounded down this->lastSlotStartTime64 = this->uwbListTDMAInfo[0].frameStartTime + (uint64)(this->slotDuration_us*slot); -// this->lastSlotStartTime64 = this->uwbFrameStartTimes64[0]; - -// debug_msg[200]; -// n = sprintf((char*)&debug_msg[0], "SET WAIT_SEND_SUG end"); -// send_usbmessage(&debug_msg[0], n); -// usb_run(); - + break; + } + case SEND_SUG: + { + this->discovery_mode_duration = this->slotStartDelay_us*2;//TODO make sure this works as intended + this->discovery_mode_expires = TRUE; break; } case EXIT: @@ -2828,26 +1798,10 @@ static void check_discovery_mode_expiration(struct TDMAHandler *this) if(this->discovery_mode == COLLECT_INF_REG) { new_mode = WAIT_SEND_SUG; -// inst->testAppState = TA_TX_SELECT; inst->testAppState = TA_RXE_WAIT; //still collect RNG_REPORT messages while we wait to send our SUG message - - //NOTE moved to set_discovery_mode for WAIT_SEND_SUG - //TODO remove below - //deconflict gathered tdma info -// this->deconflict_slot_assignments(this); -// //assign self slot -// this->find_assign_slot(this); -// //construct SUG packet -// this->populate_inf_msg(this, RTLS_DEMO_MSG_INF_SUG); - - //TODO how to actually send SUG message? TA_TX_SELECT? - //could go directly to TA_TXSUG_WAIT_SEND and delay until start of next frame... - //that could remove the need for extra logic in TA_TX_SELECT... } else if(this->discovery_mode == WAIT_SEND_SUG) { -// this->uwbFrameStartTimes64[0] = portGetTickCntMicro(); -// this->lastSlotStartTime64 = this->uwbFrameStartTimes64[0]; inst->testAppState = TA_TX_SELECT; new_mode = SEND_SUG; } @@ -3168,9 +2122,6 @@ static void usb_dump_tdma(struct TDMAHandler *this) static struct TDMAHandler new(){ struct TDMAHandler ret = {}; - ret.slotDuration_ms = 50; //TODO should be a function of the data rate and the max number of UWBs - ret.slotDuration_us = ret.slotDuration_ms*1000; - ret.slot_transition = &slot_transition; ret.frame_sync = &frame_sync; ret.tx_sync_msg = &tx_sync_msg; @@ -3180,7 +2131,6 @@ static struct TDMAHandler new(){ ret.populate_inf_msg = &populate_inf_msg; ret.update_inf_tsfs = &update_inf_tsfs; ret.process_inf_msg = &process_inf_msg; - ret.check_tdma_diff = &check_tdma_diff; ret.poll_delay = &poll_delay; ret.slot_assigned = &slot_assigned; @@ -3205,11 +2155,11 @@ static struct TDMAHandler new(){ ret.deconflict_slot_pair = &deconflict_slot_pair; ret.self_conflict = &self_conflict; - uint32 time_now_us = portGetTickCntMicro(); - //TODO create a function to clear the frame information! - //and have it called in enter_discovery function! + ret.slotDuration_ms = 50; //TODO should be a function of the data rate and the max number of UWBs + ret.slotDuration_us = ret.slotDuration_ms*1000; + uint64 time_now_us = portGetTickCntMicro(); ret.maxFramelength = (uint8)MIN_FRAMELENGTH; while(ret.maxFramelength < (int)UWB_LIST_SIZE + 1) @@ -3219,7 +2169,6 @@ static struct TDMAHandler new(){ for(int i = 0; i < UWB_LIST_SIZE; i++) { -// ret.uwbListTDMAInfo[i].uwbIndex = i; ret.uwbListTDMAInfo[i].framelength = (uint8)MIN_FRAMELENGTH; ret.uwbListTDMAInfo[i].slots = NULL; ret.uwbListTDMAInfo[i].slotsLength = 0; @@ -3241,9 +2190,6 @@ static struct TDMAHandler new(){ ret.slotStartDelay_us = 4000; ret.frameSyncThreshold_us = 2*ret.slotStartDelay_us; ret.infMessageLength = 0; - ret.rebase_pending = FALSE; - ret.rebase_tx = FALSE; - ret.rebase_frameStartTime64 = time_now_us; ret.enter_discovery_mode(&ret); // ret.collectInfDuration = ret.maxFramelength*ret.slotDuration; diff --git a/src/application/tdma_handler.h b/src/application/tdma_handler.h index 0f2bfad..29aa932 100644 --- a/src/application/tdma_handler.h +++ b/src/application/tdma_handler.h @@ -7,7 +7,6 @@ struct TDMAInfo { -// uint8 uwbIndex; //TODO remove if unused uint8 connectionType; //UWB_LIST_SELF, UWB_LIST_NEIGHBOR, UWB_LIST_HIDDEN, UWB_LIST_INACTIVE uint32 lastCommNeighbor; //milliseconds uint32 lastCommHidden; //milliseconds @@ -45,8 +44,6 @@ struct TDMAHandler struct TDMAInfo uwbListTDMAInfo[UWB_LIST_SIZE]; - //TODO use smaller data types where possible - uint64 lastFST; uint64 lastSlotStartTime64; uint32 slotDuration_ms; //TODO make variable in duration based on UWB_LIST_SIZE @@ -55,11 +52,7 @@ struct TDMAHandler bool firstPollSentThisSlot; bool firstPollComplete; bool secondPollSentThisSlot; - bool rebase_pending; - bool rebase_tx; - uint64 rebase_frameStartTime64; //TODO rename 64 to us! - //TODO can probably use a smaller data type... uint64 slotStartDelay_us; //time between slot start and transmission within that slot uint64 frameSyncThreshold_us; @@ -86,7 +79,6 @@ struct TDMAHandler bool (*check_blink)(struct TDMAHandler *this); void (*populate_inf_msg)(struct TDMAHandler *this, uint8 inf_msg_type); bool (*process_inf_msg)(struct TDMAHandler *this, uint8 *messageData, uint8 srcIndex, INF_PROCESS_MODE mode); - bool (*check_tdma_diff)(struct TDMAHandler *this, uint8 *messageData, uint8 *srcAddr); bool (*poll_delay)(struct TDMAHandler *this, uint32 time_now_offset, uint32 offset); void (*enter_discovery_mode)(struct TDMAHandler *this); void (*set_discovery_mode)(struct TDMAHandler *this, DISCOVERY_MODE mode, uint32 time_now); @@ -105,7 +97,7 @@ struct TDMAHandler //run through all uwb pairs - bool (*deconflict_slot_assignments)(struct TDMAHandler *this); //TODO implement + bool (*deconflict_slot_assignments)(struct TDMAHandler *this); //run through each slot of two uwbs bool (*deconflict_uwb_pair)(struct TDMAHandler *this, struct TDMAInfo *info_a, struct TDMAInfo *info_b); //deconflict two specific slots diff --git a/src/decadriver/deca_device.c b/src/decadriver/deca_device.c index d8cf0f9..a398c88 100644 --- a/src/decadriver/deca_device.c +++ b/src/decadriver/deca_device.c @@ -2213,17 +2213,7 @@ uint8 dwt_checkirq(void) */ void dwt_isr(void) { - - uint32 status = dw1000local.cbData.status = dwt_read32bitreg(SYS_STATUS_ID); // Read status register low 32bits -// uint8 status_high = dwt_read8bitoffsetreg(SYS_STATUS_ID, 4); -// uint32 mask = dwt_read32bitreg(SYS_MASK_ID); - -// uint8 debug_msg[50]; -// int n = sprintf((char*)&debug_msg[0], "dwt_isr SYS_STATUS: %lu", status); -// send_usbmessage(&debug_msg[0], n); -// usb_run(); - uint8 callback_triggered = 0; @@ -2313,22 +2303,6 @@ void dwt_isr(void) { callback_triggered = 1; -// uint32 and_status = status & SYS_STATUS_ALL_RX_TO; -// uint8 debug_msg[100]; -// int n = 0; -// n = sprintf((char*)&debug_msg[0], "rx_timeout!!! and_status: %lu", and_status); -// send_usbmessage(&debug_msg[0], n); -// usb_run(); - -// if(and_status & SYS_STATUS_AFFREJ){ -// uint32 sys_cfg_reg = dwt_read32bitreg(SYS_CFG_ID); -// n = sprintf((char*)&debug_msg[0], "rx_timeout!!! sys_cfg_reg: %lu", sys_cfg_reg); -// send_usbmessage(&debug_msg[0], n); -// usb_run(); -// } - - - dwt_write32bitreg(SYS_STATUS_ID, SYS_STATUS_RXRFTO); // Clear RX timeout event bits dw1000local.wait4resp = 0; @@ -2351,24 +2325,6 @@ void dwt_isr(void) { callback_triggered = 1; -// uint32 and_status = status & SYS_STATUS_ALL_RX_ERR; -// uint8 debug_msg[100]; -// int n = 0; -// n = sprintf((char*)&debug_msg[0], "rx_error!!! and_status: %lu", and_status); -// send_usbmessage(&debug_msg[0], n); -// usb_run(); -// -// n = sprintf((char*)&debug_msg[0], "rx_error!!! sys_status: %lu", status); -// send_usbmessage(&debug_msg[0], n); -// usb_run(); -// -// if(and_status & SYS_STATUS_AFFREJ){ -// uint32 sys_cfg_reg = dwt_read32bitreg(SYS_CFG_ID); -// n = sprintf((char*)&debug_msg[0], "rx_error!!! sys_cfg_reg: %lu", sys_cfg_reg); -// send_usbmessage(&debug_msg[0], n); -// usb_run(); -// } - dwt_write32bitreg(SYS_STATUS_ID, SYS_STATUS_ALL_RX_ERR); // Clear RX error event bits dw1000local.wait4resp = 0; @@ -2386,18 +2342,15 @@ void dwt_isr(void) } } - // + //IRQ line high but no callbacks triggered. if(callback_triggered == 0) { - //clear status register - uint32 SYS_STATUS_ALL_INT = 1073217534; //TODO cleanup - dwt_write32bitreg(SYS_STATUS_ID, SYS_STATUS_ALL_INT); - -// uint8 debug_msg[50]; -// int n = sprintf((char*)&debug_msg[0], "unsubscribed callback"); -// send_usbmessage(&debug_msg[0], n); -// usb_run(); + //clear and reset SYS_MASK register + dwt_setinterrupt(SYS_MASK_MASK_32, 0); //disable all + dwt_setinterrupt(SYS_MASK_VAL, 1); + //clear SYS_STATUS register + dwt_write32bitreg(SYS_STATUS_ID, SYS_STATUS_MASK_32); } } @@ -2687,11 +2640,6 @@ int dwt_starttx(uint8 mode) } else { -// uint8 debug_msg[150]; -// int n = sprintf((char*)&debug_msg[0], "SYS_STATUS: %u", checkTxOK); -// send_usbmessage(&debug_msg[0], n); -// usb_run(); - // I am taking DSHP set to Indicate that the TXDLYS was set too late for the specified DX_TIME. // Remedial Action - (a) cancel delayed send temp = (uint8)SYS_CTRL_TRXOFF; // This assumes the bit is in the lowest byte @@ -2726,113 +2674,21 @@ int dwt_starttx(uint8 mode) */ void dwt_forcetrxoff(void) { - uint32 SYS_MASK_VAL = 605278336; - uint32 SYS_MASK_RESERVED = 0xC0080001; - uint32 SYS_STATUS_ALL_INT= 1073217534; - decaIrqStatus_t stat ; - uint32 mask; - - mask = dwt_read32bitreg(SYS_MASK_ID) ; // Read set interrupt mask -// if(mask != SYS_MASK_VAL){ -// uint8 mismatch = 1; -// } + dwt_setinterrupt(SYS_MASK_MASK_32, 0); // Clear interrupt mask - so we don't get any unwanted events // Need to beware of interrupts occurring in the middle of following read modify write cycle // We can disable the radio, but before the status is cleared an interrupt can be set (e.g. the // event has just happened before the radio was disabled) // thus we need to disable interrupt during this operation stat = decamutexon() ; - - - mask &= SYS_MASK_RESERVED; - //this is writing to the reserved bits!!! - //dwt_write32bitreg(SYS_MASK_ID, 0) ; // Clear interrupt mask - so we don't get any unwanted events - - dwt_write32bitreg(SYS_MASK_ID, mask) ; // Clear interrupt mask - so we don't get any unwanted events - dwt_write8bitoffsetreg(SYS_CTRL_ID, SYS_CTRL_OFFSET, (uint8)SYS_CTRL_TRXOFF) ; // Disable the radio + decamutexoff(stat) ; - // Forcing Transceiver off - so we do not want to see any new events that may have happened - dwt_write32bitreg(SYS_STATUS_ID, SYS_STATUS_ALL_INT); - dwt_syncrxbufptrs(); - - mask |= SYS_MASK_VAL; //set out desired sys_mask values - dwt_write32bitreg(SYS_MASK_ID, mask); - - -// // Reset the IC (might be needed if not getting here from POWER ON) -// dwt_softreset(); -// -// //we can enable any configuration loading from OTP/ROM on initialization -// result = dwt_initialise(DWT_LOADUCODE) ; -// -// //this is platform dependent - only program if DW EVK/EVB -// dwt_setleds(3) ; //configure the GPIOs which control the leds on EVBs -// -// if (DWT_SUCCESS != result) -// { -// return (-1) ; // device initialize has failed -// } -// -// //enable TX, RX states on GPIOs 6 and 5 -// dwt_setlnapamode(1,1); -// -// instanceclearcounts() ; -// -// instance_data[instance].sleepingEabled = 1; -// instance_data[instance].panID = 0xdeca ; -// instance_data[instance].wait4ack = 0; -// instance_data[instance].instanceTimerEnabled = 0; -// -// instance_clearevents(); -// -// dwt_geteui(instance_data[instance].eui64); -// -// instance_data[instance].canPrintInfo = 0; -// -// instance_data[instance].clockOffset = 0; -// instance_data[instance].monitor = 0; -// -// dwt_setdblrxbuffmode(1); - - - - - - uint32 new_mask = dwt_read32bitreg(SYS_MASK_ID); - new_mask &= SYS_MASK_VAL; - -// uint8 mismatch = 0; - -// while(new_mask != SYS_MASK_VAL){ -// if(new_mask != SYS_MASK_VAL){ -//// dwt_write32bitreg(SYS_MASK_ID, mask); -//// new_mask = dwt_read32bitreg(SYS_MASK_ID); -//// new_mask &= SYS_MASK_VAL; -// -// mismatch = 1; -// } - -// if(mismatch == 1){ -// uint8 debug_msg[100]; -// int n = sprintf((char*)&debug_msg[0], "mismatch occurred!"); -// send_usbmessage(&debug_msg[0], n); -// usb_run(); -// } - -// if(new_mask != mask){ -// uint8 mismatch = 1; -// } -// if(new_mask != mask){ -// uint8 mismatch = 1; -// } - - - // Enable/restore interrupts again... - decamutexoff(stat) ; - dw1000local.wait4resp = 0; + // Enable/restore interrupts again... + dwt_setinterrupt(SYS_MASK_VAL, 1); + dw1000local.wait4resp = 0; } // end deviceforcetrxoff() @@ -3115,12 +2971,6 @@ void dwt_setinterrupt(uint32 bitmask, uint8 enable) // Need to beware of interrupts occurring in the middle of following read modify write cycle stat = decamutexon() ; -// C0080001 -// uint32 reserved_mask = 0xC0080001; -// mask &= reserved_mask; //preserve reserved bits -// mask |= SYS_MASK_VAL; //set out desired sys_mask values -// dwt_write32bitreg(SYS_MASK_ID, mask); - mask = dwt_read32bitreg(SYS_MASK_ID) ; // Read register if(enable) diff --git a/src/decadriver/deca_regs.h b/src/decadriver/deca_regs.h index fc0791d..5e6e819 100644 --- a/src/decadriver/deca_regs.h +++ b/src/decadriver/deca_regs.h @@ -242,6 +242,10 @@ extern "C" { #define SYS_MASK_MTXBERR 0x10000000UL /* Mask Transmit Buffer Error event */ #define SYS_MASK_MAFFREJ 0x20000000UL /* Mask Automatic Frame Filtering rejection event */ +/* default SYS_MASK settings that will be used in the application */ +#define SYS_MASK_VAL (DWT_INT_TFRS | DWT_INT_RFCG | DWT_INT_RXOVRR | DWT_INT_ARFE | DWT_INT_RFSL | DWT_INT_SFDT | DWT_INT_RPHE | DWT_INT_RFCE | DWT_INT_RFTO) + + /****************************************************************************//** * @brief Bit definitions for register SYS_STATUS **/ @@ -249,7 +253,8 @@ extern "C" { #define SYS_STATUS_OFFSET 0x00 #define SYS_STATUS_LEN (5) /* Note 40 bit register */ /*masks */ -#define SYS_STATUS_MASK_32 0xFFF7FFFFUL /* System event Status Register access mask (all unused fields should always be writen as zero) */ +//#define SYS_STATUS_MASK_32 0xFFF7FFFFUL /* System event Status Register access mask (all unused fields should always be writen as zero) */ +#define SYS_STATUS_MASK_32 0x3FF7FFFF /* System event Status Register access mask (all unused fields should always be writen as zero) */ /*offset 0 */ #define SYS_STATUS_IRQS 0x00000001UL /* Interrupt Request Status READ ONLY */ #define SYS_STATUS_CPLOCK 0x00000002UL /* Clock PLL Lock */ diff --git a/src/platform/port.c b/src/platform/port.c index cd81730..c8df270 100644 --- a/src/platform/port.c +++ b/src/platform/port.c @@ -67,29 +67,19 @@ unsigned long long portGetTickCntMicro(void) uint64_t major_incr2 = (uint64_t)time32_incr; uint32_t minor_incr2 = SysTick->VAL; + unsigned long long microsec; + + //72 comes from SystemCoreClock / CLOCKS_PER_SEC * 1000 if(major_incr1 == major_incr2) { - //72 comes from SystemCoreClock / CLOCKS_PER_SEC * 1000 - return major_incr1*1000 + 1000 - (uint64_t)(minor_incr1/72); + microsec = major_incr1*1000 + 1000 - (uint64_t)(minor_incr1/72); } else { - //72 comes from SystemCoreClock / CLOCKS_PER_SEC * 1000 - return major_incr2*1000 + 1000 - (uint64_t)(minor_incr2/72); + microsec = major_incr2*1000 + 1000 - (uint64_t)(minor_incr2/72); } -// __asm__ volatile("dmb"); -// //72 comes from SystemCoreClock / CLOCKS_PER_SEC * 1000 -// return time64_incr*1000 + 1000 - (uint64_t)(SysTick->VAL/72); -// -//// major = SysTickMajor; -//// minor = SysTick->VAL; -//// __asm__ volatile("dmb"); -//// if (major != SysTickMajor) -//// return SysTickMajor - SysTick->Val; -//// return major - minor; -// - + return microsec; } uint16_t portGetTIM3() @@ -171,32 +161,6 @@ int peripherals_init (void) SysTick_Configuration(); NVIC_Configuration(); - - //TODO remove - -// /* make sure the peripheral is clocked */ -//// RCC_APB1PeriphClockCmd (RCC_APB1Periph_TIM3, ENABLE); -// RCC_ClocksTypeDef RCC_Clocks; -// RCC_GetClocksFreq (&RCC_Clocks); -// uint32_t multiplier; -// if (RCC_Clocks.PCLK1_Frequency == RCC_Clocks.SYSCLK_Frequency) { -// multiplier = 1; -// } else { -// multiplier = 2; -// } -// uint32_t TIM3CLK_Frequency = multiplier * RCC_Clocks.PCLK1_Frequency; -// uint32_t TIM3COUNTER_Frequency = 1000;//1000000; -// uint16_t prescaler = (TIM3CLK_Frequency / TIM3COUNTER_Frequency) - 1; -// uint16_t reload = 0xFFFF;//(25) - 1; // Tevt = 25 us -// TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; -// /* set everything back to default values */ -// TIM_TimeBaseStructInit (&TIM_TimeBaseStructure); -// /* only changes from the defaults are needed */ -// TIM_TimeBaseStructure.TIM_Period = reload; -// TIM_TimeBaseStructure.TIM_Prescaler = prescaler; -// TIM_TimeBaseInit (TIM3, &TIM_TimeBaseStructure); -// -// TIM_Cmd(TIM3, ENABLE); return 0; } @@ -351,10 +315,6 @@ int RCC_Configuration(void) RCC_APB2Periph_GPIOE | RCC_APB2Periph_AFIO, ENABLE); - /* Enable TIM3 clock */ //TODO remove -// RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); - - return 0; } diff --git a/src/platform/port.h b/src/platform/port.h index 44e7c8c..aff6f3f 100644 --- a/src/platform/port.h +++ b/src/platform/port.h @@ -150,7 +150,6 @@ typedef enum void Sleep(uint32_t Delay); unsigned long portGetTickCnt(void); unsigned long long portGetTickCntMicro(void); -//uint16_t portGetTIM3(); //TODO remove #define S1_SWITCH_ON (1) #define S1_SWITCH_OFF (0) -- GitLab