示例#1
0
void cph_deca_isr_configure() {
	cph_queue_init(&event_queue, sizeof(cph_deca_event_t), CPH_MAX_EVENTS, events);

//	dwt_setcallbacks(cph_deca_txcallback, cph_deca_rxcallback);

//	dwt_setinterrupt(
//			DWT_INT_TFRS | DWT_INT_RFCG
//					| ( DWT_INT_ARFE | DWT_INT_RFSL | DWT_INT_SFDT | DWT_INT_RPHE | DWT_INT_RFCE | DWT_INT_RFTO /*| DWT_INT_RXPTO*/),
//			1);
	dwt_setinterrupt(
			DWT_INT_TFRS | DWT_INT_RFCG
					| ( DWT_INT_RFTO ),
			1);
}
示例#2
0
void DWM1000_Tag::init() {
	INFO("HSPI");
//_________________________________________________INIT SPI ESP8266

	resetChip();
	initSpi();
	enableIsr();

	uint64_t eui = 0xF1F2F3F4F5F6F7F;
	dwt_seteui((uint8_t*) &eui);
	dwt_geteui((uint8_t*) &eui);
	LOG<< HEX << "EUID : "<< eui <<FLUSH;
	dwt_seteui((uint8_t*) &eui);
	dwt_geteui((uint8_t*) &eui);
	LOG<< HEX << "EUID : "<< eui <<FLUSH;

//	dwt_softreset();
	deca_sleep(100);

	while (dwt_initialise(DWT_LOADUCODE)) {
		LOG<< " dwt_initialise failed " << FLUSH;
	}
	LOG<< " dwt_initialise done." << FLUSH;
	while (dwt_configure(&config)) {
		LOG<< " dwt_configure failed " << FLUSH;
	}
	LOG<< " dwt_configure done." << FLUSH;

	uint32_t device_id = dwt_readdevid();
	LOG<< HEX << " device id : " << device_id <<FLUSH;

	dwt_setrxantennadelay(RX_ANT_DLY); /* Apply default antenna delay value. See NOTE 1 below. */
	dwt_settxantennadelay(TX_ANT_DLY);

	/* Set expected response's delay and timeout. See NOTE 4 and 5 below.
	 * As this example only handles one incoming frame with always the same delay and timeout, those values can be set here once for all. */
	dwt_setrxaftertxdelay(POLL_TX_TO_RESP_RX_DLY_UUS);
	dwt_setrxtimeout(RESP_RX_TIMEOUT_UUS);
	dwt_setinterrupt(DWT_INT_RFCG, 1);	// enable

	_count = 0;
}
示例#3
0
bool DWM1000_Anchor::dispatch(Msg& msg) {
	PT_BEGIN()
	PT_WAIT_UNTIL(msg.is(0, SIG_INIT));
	init();
	while (true) {

		WAIT_POLL: {
			dwt_setrxtimeout(0); /* Clear reception timeout to start next ranging process. */
			dwt_rxenable(0); /* Activate reception immediately. */
//			dwt_setinterrupt(DWT_INT_RFCG, 1);	// enable RXD interrupt

			while (true) { /* Poll for reception of a frame or error/timeout. See NOTE 7 below. */
				timeout(1000);/* This is the delay from the end of the frame transmission to the enable of the receiver, as programmed for the DW1000's wait for response feature. */
				clearInterrupt();
				PT_YIELD_UNTIL(timeout() || isInterruptDetected());
				status_reg = _status_reg;
				LOG<< HEX << " status reg.:" << status_reg << " ,interrupts : " << interruptCount << FLUSH;
				status_reg = dwt_read32bitreg(SYS_STATUS_ID);
				LOG<< HEX << " IRQ pin : " << digitalRead(D2) << " status_reg DWM1000 	" << status_reg << FLUSH;// PULL LOW

				if (status_reg & (SYS_STATUS_RXFCG | SYS_STATUS_ALL_RX_ERR))
					break;
			}
		}
		///____________________________________________________________________________

		if (status_reg & SYS_STATUS_RXFCG) {
			LOG<< " $ "<<FLUSH;
			uint32 frame_len;
			dwt_write32bitreg(SYS_STATUS_ID, SYS_STATUS_RXFCG); /* Clear good RX frame event in the DW1000 status register. */

			/* A frame has been received, read it into the local buffer. */
			frame_len = dwt_read32bitreg(RX_FINFO_ID) & RX_FINFO_RXFL_MASK_1023;
			if (frame_len <= RX_BUFFER_LEN) {
				dwt_readrxdata(rx_buffer, frame_len, 0);
			}

			/* Check that the frame is a poll sent by "DS TWR initiator" example.
			 * As the sequence number field of the frame is not relevant, it is cleared to simplify the validation of the frame. */
			rx_buffer[ALL_MSG_SN_IDX] = 0;
			if (memcmp(rx_buffer, rx_poll_msg, ALL_MSG_COMMON_LEN) == 0) {
				LOG<< " $$ "<<FLUSH;
				uint32 resp_tx_time;

				poll_rx_ts = get_rx_timestamp_u64(); /* Retrieve poll reception timestamp. */

				/* Set send time for response. See NOTE 8 below. */
				resp_tx_time = (poll_rx_ts
						+ (POLL_RX_TO_RESP_TX_DLY_UUS * UUS_TO_DWT_TIME)) >> 8;
				dwt_setdelayedtrxtime(resp_tx_time);

				/* Set expected delay and timeout for final message reception. */
				dwt_setrxaftertxdelay(RESP_TX_TO_FINAL_RX_DLY_UUS);
				dwt_setrxtimeout(FINAL_RX_TIMEOUT_UUS);

				/* Write and send the response message. See NOTE 9 below.*/
				tx_resp_msg[ALL_MSG_SN_IDX] = frame_seq_nb;
				dwt_writetxdata(sizeof(tx_resp_msg), tx_resp_msg, 0);
				dwt_writetxfctrl(sizeof(tx_resp_msg), 0);
				dwt_starttx(DWT_START_TX_DELAYED | DWT_RESPONSE_EXPECTED);

				/* We assume that the transmission is achieved correctly, now poll for reception of expected "final" frame or error/timeout.
				 * See NOTE 7 below. */
//				while (true) { /* Poll for reception of a frame or error/timeout. See NOTE 7 below. */
				timeout(10);
				dwt_setinterrupt(DWT_INT_RFCG, 1);// enable
				clearInterrupt();
//				PT_YIELD_UNTIL(timeout() || isInterruptDetected());
				status_reg = dwt_read32bitreg(SYS_STATUS_ID);
//				status_reg = _status_reg;
				LOG<< HEX << " status reg2:" << status_reg << FLUSH;
//					if (status_reg & (SYS_STATUS_RXFCG | SYS_STATUS_ALL_RX_ERR))
//						break;
//				}
//               while (!((status_reg = dwt_read32bitreg(SYS_STATUS_ID)) & (SYS_STATUS_RXFCG | SYS_STATUS_ALL_RX_ERR)))
//               { };

				/* Increment frame sequence number after transmission of the response message (modulo 256). */
				frame_seq_nb++;

				if (status_reg & SYS_STATUS_RXFCG) {
					LOG<< " $$$ "<<FLUSH;
					/* Clear good RX frame event and TX frame sent in the DW1000 status register. */
					dwt_write32bitreg(SYS_STATUS_ID,
							SYS_STATUS_RXFCG | SYS_STATUS_TXFRS);

					/* A frame has been received, read it into the local buffer. */
					frame_len = dwt_read32bitreg(
							RX_FINFO_ID) & RX_FINFO_RXFLEN_MASK;
					if (frame_len <= RX_BUF_LEN) {
						dwt_readrxdata(rx_buffer, frame_len, 0);
					}

					/* Check that the frame is a final message sent by "DS TWR initiator" example.
					 * As the sequence number field of the frame is not used in this example, it can be zeroed to ease the validation of the frame. */
					rx_buffer[ALL_MSG_SN_IDX] = 0;
					if (memcmp(rx_buffer, rx_final_msg, ALL_MSG_COMMON_LEN)
							== 0) {
						uint32 poll_tx_ts, resp_rx_ts, final_tx_ts;
						uint32 poll_rx_ts_32, resp_tx_ts_32, final_rx_ts_32;
						double Ra, Rb, Da, Db;
						int64 tof_dtu;

						/* Retrieve response transmission and final reception timestamps. */
						resp_tx_ts = get_tx_timestamp_u64();
						final_rx_ts = get_rx_timestamp_u64();

						/* Get timestamps embedded in the final message. */
						final_msg_get_ts(&rx_buffer[FINAL_MSG_POLL_TX_TS_IDX],
								&poll_tx_ts);
						final_msg_get_ts(&rx_buffer[FINAL_MSG_RESP_RX_TS_IDX],
								&resp_rx_ts);
						final_msg_get_ts(&rx_buffer[FINAL_MSG_FINAL_TX_TS_IDX],
								&final_tx_ts);

						/* Compute time of flight. 32-bit subtractions give correct answers even if clock has wrapped. See NOTE 10 below. */
						poll_rx_ts_32 = (uint32) poll_rx_ts;
						resp_tx_ts_32 = (uint32) resp_tx_ts;
						final_rx_ts_32 = (uint32) final_rx_ts;
						Ra = (double) (resp_rx_ts - poll_tx_ts);
						Rb = (double) (final_rx_ts_32 - resp_tx_ts_32);
						Da = (double) (final_tx_ts - resp_rx_ts);
						Db = (double) (resp_tx_ts_32 - poll_rx_ts_32);
						tof_dtu = (int64) ((Ra * Rb - Da * Db)
								/ (Ra + Rb + Da + Db));

						tof = tof_dtu * DWT_TIME_UNITS;
						distance = tof * SPEED_OF_LIGHT;

						/* Display computed distance on LCD. */
//						char dist_str[20];
//						sprintf(dist_str,"%3.2f", distance);
						//                      lcd_display_str(dist_str);
						LOG<< " distance : " << (float)distance << "m. " << FLUSH;
					}
				} else {
					/* Clear RX error events in the DW1000 status register. */
					dwt_write32bitreg(SYS_STATUS_ID, SYS_STATUS_ALL_RX_ERR);
				}
			}
		} else {
示例#4
0
int app_dw1000_init (
		int HACK_role,
		int HACK_EUI,
		void (*txcallback)(const dwt_callback_data_t *),
		void (*rxcallback)(const dwt_callback_data_t *)
		) {
	uint32_t devID;
	int err;

	// Start off DW1000 comms slow
	REG(SSI0_BASE + SSI_CR1) = 0;
	REG(SSI0_BASE + SSI_CPSR) = 8;
	REG(SSI0_BASE + SSI_CR1) |= SSI_CR1_SSE;

	// Reset the DW1000...for some reason
	dw1000_reset();

	// Make sure we can talk to the DW1000
	devID = dwt_readdevid();
	if (devID != DWT_DEVICE_ID) {
#ifdef DW_DEBUG
		printf("Could not read Device ID from the DW1000\r\n");
		printf("Possible the chip is asleep...\r\n");
#endif
		return -1;
	}

	// Select which of the three antennas on the board to use
	dw1000_choose_antenna(0);

	// Init the dw1000 hardware
	err = dwt_initialise(DWT_LOADUCODE    |
			DWT_LOADLDO      |
			DWT_LOADTXCONFIG |
			DWT_LOADXTALTRIM);
	if (err != DWT_SUCCESS) {
		return -1;
	}

	// Setup interrupts
	// Note: using auto rx re-enable so don't need to trigger on error frames
	dwt_setinterrupt(DWT_INT_TFRS |
			DWT_INT_RFCG |
			DWT_INT_SFDT |
			DWT_INT_RFTO |
			DWT_INT_RPHE |
			DWT_INT_RFCE |
			DWT_INT_RFSL |
			DWT_INT_RXPTO |
			DWT_INT_SFDT, 1);

	// Configure the callbacks from the dwt library
	dwt_setcallbacks(txcallback, rxcallback);

	// Set the parameters of ranging and channel and whatnot
	global_ranging_config.chan           = 2;
	global_ranging_config.prf            = DWT_PRF_64M;
	global_ranging_config.txPreambLength = DWT_PLEN_64;//DWT_PLEN_4096
	// global_ranging_config.txPreambLength = DWT_PLEN_256;
	global_ranging_config.rxPAC          = DWT_PAC8;
	global_ranging_config.txCode         = 9;  // preamble code
	global_ranging_config.rxCode         = 9;  // preamble code
	global_ranging_config.nsSFD          = 0;
	global_ranging_config.dataRate       = DWT_BR_6M8;
	global_ranging_config.phrMode        = DWT_PHRMODE_EXT; //Enable extended PHR mode (up to 1024-byte packets)
	global_ranging_config.smartPowerEn   = 1;
	global_ranging_config.sfdTO          = 64+8+1;//(1025 + 64 - 32);
	dwt_configure(&global_ranging_config, 0);//(DWT_LOADANTDLY | DWT_LOADXTALTRIM));
	dwt_setsmarttxpower(global_ranging_config.smartPowerEn);

	// Configure TX power
	{
		global_tx_config.PGdly = pgDelay[global_ranging_config.chan];
		global_tx_config.power = txPower[global_ranging_config.chan];
		dwt_configuretxrf(&global_tx_config);
	}

	/* All constants same anyway
	if(DW1000_ROLE_TYPE == TAG)
		dwt_xtaltrim(xtaltrim[0]);
	else
		dwt_xtaltrim(xtaltrim[ANCHOR_EUI]);
	*/
	dwt_xtaltrim(xtaltrim[0]);

	////TEST 1: XTAL trim calibration
	//dwt_configcwmode(global_ranging_config.chan);
	//dwt_xtaltrim(8);
	//while(1);

	//{
	//    //TEST 2: TX Power level calibration
	//    uint8_t msg[127] = "The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the l";
	//    dwt_configcontinuousframemode(0x1000);
	//    dwt_writetxdata(127, (uint8 *)  msg, 0) ;
	//    dwt_writetxfctrl(127, 0);
	//    dwt_starttx(DWT_START_TX_IMMEDIATE);
	//    while(1);
	//}

	// Configure the antenna delay settings
	{
		uint16_t antenna_delay;

		//Antenna delay not really necessary if we're doing an end-to-end calibration
		antenna_delay = 0;
		dwt_setrxantennadelay(antenna_delay);
		dwt_settxantennadelay(antenna_delay);
		//global_tx_antenna_delay = antenna_delay;

		//// Shift this over a bit for some reason. Who knows.
		//// instance_common.c:508
		//antenna_delay = dwt_readantennadelay(global_ranging_config.prf) >> 1;
		//if (antenna_delay == 0) {
		//    printf("resetting antenna delay\r\n");
		//    // If it's not in the OTP, use a magic value from instance_calib.c
		//    antenna_delay = ((DWT_PRF_64M_RFDLY/ 2.0) * 1e-9 / DWT_TIME_UNITS);
		//    dwt_setrxantennadelay(antenna_delay);
		//    dwt_settxantennadelay(antenna_delay);
		//}
		//global_tx_antenna_delay = antenna_delay;
		//printf("tx antenna delay: %u\r\n", antenna_delay);
	}

	// // Set the sleep delay. Not sure what this does actually.
	// instancesettagsleepdelay(POLL_SLEEP_DELAY, BLINK_SLEEP_DELAY);


	// Configure as either a tag or anchor

	if (HACK_role == ANCHOR) {
		uint8_t eui_array[8];

		// Enable frame filtering
		dwt_enableframefilter(DWT_FF_DATA_EN | DWT_FF_ACK_EN);

		dw1000_populate_eui(eui_array, HACK_EUI);
		dwt_seteui(eui_array);
		dwt_setpanid(DW1000_PANID);

		// We do want to enable auto RX
		dwt_setautorxreenable(1);
		// Let's do double buffering
		dwt_setdblrxbuffmode(0);
		// Disable RX timeout by setting to 0
		dwt_setrxtimeout(0);

		// Go for receiving
		dwt_rxenable(0);

	} else if (HACK_role == TAG) {
		uint8_t eui_array[8];

		// Allow data and ack frames
		dwt_enableframefilter(DWT_FF_DATA_EN | DWT_FF_ACK_EN);

		dw1000_populate_eui(eui_array, HACK_EUI);
		dwt_seteui(eui_array);
		dwt_setpanid(DW1000_PANID);

		// Do this for the tag too
		dwt_setautorxreenable(1);
		dwt_setdblrxbuffmode(1);
		dwt_enableautoack(5 /*ACK_RESPONSE_TIME*/);

		// Configure sleep
		{
			int mode = DWT_LOADUCODE    |
				DWT_PRESRV_SLEEP |
				DWT_CONFIG       |
				DWT_TANDV;
			if (dwt_getldotune() != 0) {
				// If we need to use LDO tune value from OTP kick it after sleep
				mode |= DWT_LOADLDO;
			}
			// NOTE: on the EVK1000 the DEEPSLEEP is not actually putting the
			// DW1000 into full DEEPSLEEP mode as XTAL is kept on
			dwt_configuresleep(mode, DWT_WAKE_CS | DWT_SLP_EN);
		}

	}

	// Make it fast
	REG(SSI0_BASE + SSI_CR1) = 0;
	REG(SSI0_BASE + SSI_CPSR) = 2;
	REG(SSI0_BASE + SSI_CR1) |= SSI_CR1_SSE;

	return 0;
}
void cs_listener_run(void) {
	uint8_t ts[8];
	uint32 chirp_ts;
	int64_t rcv_tag_ts = 0;
	uint32 sync_rate = cph_config->sender_period;
	uint8 functionCode = 0;
	double diff_val = 0;
	double diff_val_prev = 0;
	double diff_val_var = 0;
	double diff_ts =0;
	double adjusted_ts = 0;
	double relative_tag_ts = 0;

	int64_t rcv_curr_ts = 0;
	int64_t rcv_prev_ts = 0;
	int64_t rcv_diff_ts = 0;
	double rcv_diff = 0;

	int64_t blink_curr_ts = 0;
	int64_t blink_prev_ts = 0;
	int64_t blink_diff_ts = 0;
	double blink_diff = 0;

	double rcv_blink_diff = 0;

	uint32 rcv_interval;
	uint32 elapsed;
	uint8 count = 1;

	irq_init();
	pio_disable_interrupt(DW_IRQ_PIO, DW_IRQ_MASK);
	cph_deca_init_device();
	cph_deca_init_network(cph_config->panid, cph_config->shortid);

	printf("Device ID: %08X\r\n", dwt_readdevid());


	// Enable external sync
	uint32_t ec_ctrl;
	dwt_readfromdevice(EXT_SYNC_ID, EC_CTRL_OFFSET, 4, (uint8_t*) &ec_ctrl);
	ec_ctrl &= EC_CTRL_WAIT_MASK;			// clear WAIT field
	ec_ctrl |= EC_CTRL_OSTRM | (33 << 3);	// turn on OSTRM and put 33 in WAIT field
	dwt_writetodevice(EXT_SYNC_ID, EC_CTRL_OFFSET, 4, (uint8_t*) &ec_ctrl);


	dwt_setcallbacks(0, rxcallback);

	chirp_ts = cph_get_millis();
	dwt_setinterrupt( DWT_INT_TFRS | DWT_INT_RFCG | (DWT_INT_ARFE | DWT_INT_RFSL | DWT_INT_SFDT | DWT_INT_RPHE | DWT_INT_RFCE | DWT_INT_RFTO /*| DWT_INT_RXPTO*/), 1);
	pio_enable_interrupt(DW_IRQ_PIO, DW_IRQ_MASK);

	dwt_rxenable(0);

	while(1)
	{
		if(AWAKE_CHIRP) {
			elapsed = cph_get_millis() - chirp_ts;
			if(elapsed > CHIRP_PERIOD) {
				TRACE("Awake %d\r\n", count);
				count++;
				chirp_ts = cph_get_millis();
			}
		}

		if(irq_status == STATUS_RCV){
			functionCode = ((cph_deca_msg_header_t*)rx_buffer)->functionCode;

			switch(functionCode){
			case FUNC_CS_SYNC:
				dwt_readrxtimestamp(ts);
				TRACE("end   sys: ");
				for (int i = 4; i >= 0; i--) {
					TRACE("%02X", ts[i]);
				}
				TRACE("\r\n");
//				// Difference in RX timestamps
//				rcv_curr_ts = get_rx_timestamp();
//				rcv_diff_ts = rcv_curr_ts - rcv_prev_ts;
//				rcv_prev_ts = rcv_curr_ts;
//				rcv_diff = (int64_t)rcv_diff_ts * DWT_TIME_UNITS * 1000;
//
//				// Difference in TX timestamps
//				blink_curr_ts = ((int64_t)((cph_deca_msg_blink_t*)rx_buffer)->blinkTxTs) << 8;
//				blink_diff_ts = blink_curr_ts - blink_prev_ts;
//				blink_prev_ts = blink_curr_ts;
//				blink_diff = blink_diff_ts * DWT_TIME_UNITS * 1000;
//
//				rcv_blink_diff = rcv_diff - blink_diff;
//
////				TRACE("TS0: %08X \t TS1: %08X \t V: %08X \t %+.08f ms\r\n", blink_ts_prev, blink_ts, blink_ts_var, blink_diff);
//				TRACE("Bdiff: %+.08f ms \t Rdiff: %+.08f ms \t RBdiff: %+.08f ms\r\n", blink_diff, rcv_diff, rcv_blink_diff);

				break;
			case FUNC_CS_TAG:
				rcv_tag_ts = get_rx_timestamp();
				rcv_tag_ts = rcv_tag_ts - rcv_curr_ts;
				relative_tag_ts = DWU_to_MS(rcv_tag_ts);
//				TRACE("%02X : Raw Tag TS: %.08f   ", ((cph_deca_msg_header_t*)rx_buffer)->seq, relative_tag_ts);
				if(relative_tag_ts < 100) {
//					relative_tag_ts = ((relative_tag_ts - diff_ts) - (int)(relative_tag_ts - diff_ts)) * 1000000; //Truncates leading value, and converts to ns
					relative_tag_ts = ((relative_tag_ts - diff_ts) - (int)(relative_tag_ts - diff_ts)) * 1000; //Truncates leading value, and converts to μs
//					relative_tag_ts = (relative_tag_ts - diff_ts); //Account for offset from source clock in ms
					TRACE("%02X:%.08f\r\n", ((cph_deca_msg_header_t*)rx_buffer)->seq, relative_tag_ts);
				}
				break;
			case FUNC_CS_COORD:
				break;
			default:
				break;
			}
			irq_status = STATUS_EMPTY;
			dwt_rxenable(0);

		} else if (irq_status == STATUS_ERR) {
//			TRACE("INVALID LENGTH: %d\r\n", frame_len);
			irq_status = STATUS_EMPTY;
			dwt_rxenable(0);
		}
	}
}
示例#6
0
// -------------------------------------------------------------------------------------------------------------------
//
// the main instance state machine (for Tag instance mode only!)
//
// -------------------------------------------------------------------------------------------------------------------
//
int testapprun_tf(instance_data_t *inst, int message)
{

    switch (inst->testAppState)
    {
        case TA_INIT :
            // printf("TA_INIT") ;
            switch (inst->mode)
            {
                case TAG:
                {
                	int mode = 0;

                    dwt_enableframefilter(DWT_FF_DATA_EN | DWT_FF_ACK_EN); //allow data and ack frames;
                    inst->frameFilteringEnabled = 1 ;
                    dwt_setpanid(inst->panid);
                    dwt_seteui(inst->eui64);
            		inst->msg_f.panID[0] = (inst->panid) & 0xff;
                	inst->msg_f.panID[1] = inst->panid >> 8;

#if (DR_DISCOVERY == 1)
                    inst->mode = TAG_TDOA ;
                    inst->testAppState = TA_TXBLINK_WAIT_SEND;
                    memcpy(&inst->blinkmsg.tagID[0], &inst->eui64[0], BLINK_FRAME_SOURCE_ADDRESS);
#else
                    inst->testAppState = TA_TXPOLL_WAIT_SEND;
#endif

                    //can use RX auto re-enable when not logging/plotting errored frames
                    inst->rxautoreenable = 1;

                    dwt_setautorxreenable(inst->rxautoreenable); //not necessary to auto RX re-enable as the receiver is on for a short time (Tag knows when the response is coming)

                    //disable double buffer for a Tag - not needed....
                    dwt_setdblrxbuffmode(0); //enable/disable double RX buffer

                    //NOTE - Auto ACK only works if frame filtering is enabled!
                    dwt_enableautoack(ACK_RESPONSE_TIME); //wait for ACK_RESPONSE_TIME symbols (e.g. 5) before replying with the ACK

                    mode = (DWT_LOADUCODE|DWT_PRESRV_SLEEP|DWT_CONFIG|DWT_TANDV);

					if((dwt_getldotune() != 0)) //if we need to use LDO tune value from OTP kick it after sleep
							mode |= DWT_LOADLDO;

					if(inst->configData.txPreambLength == DWT_PLEN_64) //if using 64 length preamble then use the corresponding OPSet
						mode |= DWT_LOADOPSET;

                    //NOTE: on the EVK1000 the DEEPSLEEP is not actually putting the DW1000 into full DEEPSLEEP mode as XTAL is kept on
                    dwt_configuresleep(mode, DWT_WAKE_CS|DWT_SLP_EN); //configure the on wake parameters (upload the IC config settings)

                }
                break;
                default:
                break;
            }
            break; // end case TA_INIT

        case TA_SLEEP_DONE :
        {
        	event_data_t* dw_event = instance_getevent(20); //clear the event from the queue
			// waiting for timout from application to wakup IC
			if (dw_event->type != DWT_SIG_RX_TIMEOUT)
            {
                inst->done = INST_DONE_WAIT_FOR_NEXT_EVENT; //wait here for sleep timeout
                break;
            }

            inst->done = INST_NOT_DONE_YET;
            inst->instToSleep = 0;
            inst->testAppState = inst->nextState;
            inst->nextState = 0; //clear
            inst->canprintinfo = 0;
            //wake up from DEEP SLEEP
            {
                //wake up device from low power mode
                //NOTE - in the ARM  code just drop chip select for 200us
                port_SPIx_clear_chip_select();  //CS low

                instance_data[0].dwIDLE = 0; //reset

                setup_DW1000RSTnIRQ(1); //enable RSTn IRQ

                Sleep(1);   //200 us to wake up then waits 5ms for DW1000 XTAL to stabilise
                port_SPIx_set_chip_select();  //CS high
#if (DW_IDLE_CHK==1) //Wait (sleep) to give DW1000 time to get to IDLE state

                Sleep(5);

                //this is platform dependent - only program if DW EVK/EVB
                dwt_setleds(1);

                //MP bug - TX antenna delay needs reprogramming as it is not preserved
                dwt_settxantennadelay(inst->txantennaDelay) ;

                //set EUI as it will not be preserved unless the EUI is programmed and loaded from NVM
                /*if((inst->mode == TAG) || (inst->mode == TAG_TDOA))
                {
                    dwt_setpanid(inst->panid);
                    dwt_seteui(inst->eui64);
                }*/
#elif (DW_IDLE_CHK==2) //Use RSTn pin to notify the micro that DW1000 is in IDLE

                //need to poll to check when the DW1000 is in IDLE, the CPLL interrupt is not reliable
                while(instance_data[0].dwIDLE == 0); //wait for DW1000 to go to IDLE state RSTn pin to go high

                if(dwt_read32bitreg(0x0) != 0xDECA0130)
                {
                	//error?
                	int x = 0;
                }

                setup_DW1000RSTnIRQ(0); //disable RSTn IRQ
#else
                //need to poll to check when the DW1000 is in IDLE, the CPLL interrupt is not reliable
                while(dwt_read32bitreg(0x0) != 0xDECA0130);

                //Sleep(2);
#endif

                dwt_entersleepaftertx(0);
                dwt_setinterrupt(DWT_INT_TFRS, 1); //re-enable the TX/RX interrupts

            }
        }
            break;

        case TA_TXE_WAIT : //either go to sleep or proceed to TX a message
            // printf("TA_TXE_WAIT") ;
            //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->instToSleep)  //go to sleep before sending the next poll
                    )
            {
                //the app should put chip into low power state and wake up in tagSleepTime_ms time...
                //the app could go to *_IDLE state and wait for uP to wake it up...
                inst->done = INST_DONE_WAIT_FOR_NEXT_EVENT_TO; //don't sleep here but kick off the TagTimeoutTimer (instancetimer)
                inst->testAppState = TA_SLEEP_DONE;

                if(inst->nextState == TA_TXBLINK_WAIT_SEND)
                inst->canprintinfo = 1;

                //put device into low power mode
                dwt_entersleep(); //go to sleep

            }
            else //proceed to configuration and transmission of a frame
            {

				inst->testAppState = inst->nextState;
               	inst->nextState = 0; //clear
            }
            break ; // end case TA_TXE_WAIT

        case TA_TXBLINK_WAIT_SEND :
            {
                //blink frames with IEEE EUI-64 tag ID
                inst->blinkmsg.frameCtrl = 0xC5 ;
                inst->blinkmsg.seqNum = inst->frame_sn++;

                dwt_writetxdata((BLINK_FRAME_CRTL_AND_ADDRESS + FRAME_CRC), (uint8 *)  (&inst->blinkmsg), 0) ;  // write the frame data
                dwt_writetxfctrl((BLINK_FRAME_CRTL_AND_ADDRESS + FRAME_CRC), 0);

                //response will be sent after 500us (thus delay the receiver turn on by 290sym ~ 299us)
                //use delayed rx on (wait4resp timer) - this value is applied when the TX frame is done/sent, so this value can be written after TX is started
				dwt_setrxaftertxdelay(inst->rnginitW4Rdelay_sy);  //units are ~us - wait for wait4respTIM before RX on (delay RX)

                dwt_starttx(DWT_START_TX_IMMEDIATE | DWT_RESPONSE_EXPECTED); //always using immediate TX

                dwt_setrxtimeout(inst->fwtoTimeB_sy);  //units are us - wait for BLINKRX_FWTO_TIME after RX on before timing out
#if (DW_IDLE_CHK==2)
                //this is platform dependent - only program if DW EVK/EVB
                dwt_setleds(1);

                //MP bug - TX antenna delay needs reprogramming as it is not preserved
                dwt_settxantennadelay(inst->txantennaDelay) ;
#endif
                inst->sentSN = inst->blinkmsg.seqNum;
                inst->wait4ack = DWT_RESPONSE_EXPECTED; //Poll is coming soon after...

                inst->instToSleep = 1;
                inst->testAppState = TA_TX_WAIT_CONF ; // wait confirmation
                inst->previousState = TA_TXBLINK_WAIT_SEND ;
                inst->done = INST_DONE_WAIT_FOR_NEXT_EVENT; //will use RX FWTO to time out (set below)

            }
            break ; // end case TA_TXBLINK_WAIT_SEND

        case TA_TXPOLL_WAIT_SEND :
            {

#if (DR_DISCOVERY == 1)
                //NOTE the anchor address is set after receiving the ranging initialisation message
				inst->instToSleep = 1; //go to Sleep after this poll
#else
                //set destination address
                if(destaddress(inst))
                {
                    break;
                }
                //copy anchor address to short message structure
                inst->msg_f.destAddr[0] = inst->msg.destAddr[0];
                inst->msg_f.destAddr[1] = inst->msg.destAddr[1];

#endif
                inst->msg_f.messageData[FCODE] = RTLS_DEMO_MSG_TAG_POLLF;

    			inst->psduLength = TAG_POLL_F_MSG_LEN + FRAME_CRTL_AND_ADDRESS_S + FRAME_CRC + EXTRA_LENGTH;

				//set frame type (0-2), SEC (3), Pending (4), ACK (5), PanIDcomp(6)
                inst->msg_f.frameCtrl[0] = 0x41 /*PID comp*/;

                //short address for both
                inst->msg_f.frameCtrl[1] = 0x8 /*dest short address (16bits)*/ | 0x80 /*src short address (16bits)*/;

                inst->msg_f.seqNum = inst->frame_sn++;


                inst->wait4ack = DWT_RESPONSE_EXPECTED; //Response is coming after 275 us...
                //500 -> 485, 800 -> 765
                dwt_writetxfctrl(inst->psduLength, 0);

                //if the response is expected there is a 1ms timeout to stop RX if no response (ACK or other frame) coming
                dwt_setrxtimeout(inst->fwtoTime_sy);  //units are us - wait for 215us after RX on

                //use delayed rx on (wait4resp timer)
                dwt_setrxaftertxdelay(inst->fixedReplyDelay_sy);  //units are ~us - wait for wait4respTIM before RX on (delay RX)

                dwt_writetxdata(inst->psduLength, (uint8 *)  &inst->msg_f, 0) ;   // write the poll frame data

                //start TX of frame
                dwt_starttx(DWT_START_TX_IMMEDIATE | inst->wait4ack);
#if (DW_IDLE_CHK==2)                
                //this is platform dependent - only program if DW EVK/EVB
                dwt_setleds(1);

                //MP bug - TX antenna delay needs reprogramming as it is not preserved
                dwt_settxantennadelay(inst->txantennaDelay) ;
#endif          
                inst->sentSN = inst->msg_f.seqNum;

                //write the final function code
                inst->msg_f.messageData[FCODE] = RTLS_DEMO_MSG_TAG_FINALF;
                //increment the sequence number for the final message
                inst->msg_f.seqNum = inst->frame_sn;

                inst->testAppState = TA_TX_WAIT_CONF ;                                               // wait confirmation
                inst->previousState = TA_TXPOLL_WAIT_SEND ;
                inst->done = INST_DONE_WAIT_FOR_NEXT_EVENT; //will use RX FWTO to time out (set below)

                inst->responseRxNum = 0;

            }
            break;


        case TA_TX_WAIT_CONF :
           //printf("TA_TX_WAIT_CONF") ;
            {
            	//uint8 temp[5];
            	event_data_t* dw_event = instance_getevent(5); //get and clear this event

                //NOTE: Can get the ACK before the TX confirm event for the frame requesting the ACK
                //this happens because if polling the ISR the RX event will be processed 1st and then the TX event
                //thus the reception of the ACK will be processed before the TX confirmation of the frame that requested it.
                if(dw_event->type != DWT_SIG_TX_DONE) //wait for TX done confirmation
                {
                    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
                    	inst->gotTO = 1;
                    }
                    if(dw_event->type == SIG_RX_ACK)
                    {
                        inst->wait4ack = 0 ; //clear the flag as the ACK has been received
                        inst_processackmsg(inst, dw_event->msgu.rxackmsg.seqNum);
                        //printf("RX ACK in TA_TX_WAIT_CONF... wait for TX confirm before changing state\n");
                    }

                    inst->done = INST_DONE_WAIT_FOR_NEXT_EVENT;
                    break;

                }

                inst->done = INST_NOT_DONE_YET;

                if(inst->previousState == TA_TXFINAL_WAIT_SEND) //tag will do immediate receive when waiting for report (as anchor sends it without delay)
                {
#if (DR_DISCOVERY == 1)
                	//in Discovery mode anchor is not sending the report to tag go to sleep
                    inst->done = INST_DONE_WAIT_FOR_NEXT_EVENT_TO; //kick off the TagTimeoutTimer (instance timer) to initiate wakeup
                    inst->nextState = TA_TXPOLL_WAIT_SEND;
                    inst->testAppState = TA_TXE_WAIT; //we are going manually to sleep - change to TA_TXE_WAIT state
#else
                    //wait for report when non-Discovery mode
                    if(inst->wait4ack == 0)
                    	dwt_rxenable(0) ;               // turn receiver on,
#endif
                    break;
                }
                else if (inst->gotTO) //timeout
                {
                	inst_processrxtimeout(inst);
                	inst->gotTO = 0;
                }
                else
                {

                    if(inst->previousState == TA_TXPOLL_WAIT_SEND)
					{

						// write the final's frame control and address tx data (add CRC as the function will write length - 2)
						dwt_writetxdata((FRAME_CRTL_AND_ADDRESS_S + 1 + FRAME_CRC), (uint8 *)  &inst->msg_f, FINAL_MSG_OFFSET) ;   // write the final frame data

						dwt_entersleepaftertx(1);
						dwt_setinterrupt(DWT_INT_TFRS, 0); //disable all the interrupts (wont be able to enter sleep if interrupts are pending)

						inst->tagPollTxTime32l = dw_event->timeStamp32l;
						inst->relpyAddress[0] = inst->msg_f.destAddr[0];
						inst->relpyAddress[1] = inst->msg_f.destAddr[1];
						inst->canprintinfo = 2;
					}

                    if(inst->previousState == TA_TXRANGINGINIT_WAIT_SEND) //set frame control for the response message
					{
						dwt_writetxfctrl((ANCH_RESPONSE_F_MSG_LEN + FRAME_CRTL_AND_ADDRESS_S + FRAME_CRC + EXTRA_LENGTH), RESPONSE_MSG_OFFSET);
					}

                    inst->testAppState = TA_RX_WAIT_DATA ;              // wait for next frame
                    //turn RX on
                    if(inst->wait4ack == 0)
                    	dwt_rxenable(0) ;               // turn receiver on, immediate = 0/delayed = 1

                    inst->wait4ack = 0 ;
                    //dwt_readfromdevice(0x19, 0, 5, temp);
                    //sprintf((char*)&usbdata[20], "T2R%d %02x%02x%02x%02x%02x ", count, temp[4], temp[3], temp[2], temp[1], temp[0]);
                    //send_usbmessage(&usbdata[20], 16);

                    //count=0;
                }

            }

            break ; // end case TA_TX_WAIT_CONF

        case TA_RXE_WAIT :
			//printf("TA_RXE_WAIT") ;
			{
				// - with "fast" ranging - we only get here after frame timeout...
				//turn RX on
				instancerxon(inst, 0, 0) ;   // turn RX on, with/without delay

				inst->testAppState = TA_RX_WAIT_DATA;   // let this state handle it

				// end case TA_RXE_WAIT, don't break, but fall through into the TA_RX_WAIT_DATA state to process it immediately.
				if(message == 0) break;
			}

        case TA_RX_WAIT_DATA :                                                                     // Wait RX data
           //printf("TA_RX_WAIT_DATA") ;

            switch (message)
            {

                case SIG_RX_BLINK :
                {
                	instance_getevent(6); //get and clear this event
                    //else //not initiating ranging - continue to receive
                    {
                        inst->testAppState = TA_RX_WAIT_DATA ;              // wait for next frame
                        //turn RX on
                        dwt_rxenable(0) ;               // turn receiver on, immediate = 0/delayed = 1
                        inst->done = INST_NOT_DONE_YET;
                    }

                }
                break;

                case SIG_RX_ACK :
                {
					event_data_t* dw_event = instance_getevent(7); //get and clear this event
					inst_processackmsg(inst, dw_event->msgu.rxackmsg.seqNum);
                    //else we did not expect this ACK turn the RX on again
                    //only enable receiver when not using double buffering
                    inst->testAppState = TA_RX_WAIT_DATA ;              // wait for next frame
                    //turn RX on
                    dwt_rxenable(0) ;               // turn receiver on, immediate = 0/delayed = 1
                    inst->done = INST_NOT_DONE_YET;
                }
                break;

                case DWT_SIG_RX_OKAY :
                {
					event_data_t* dw_event = instance_getevent(8); //get and clear this event
					uint8  srcAddr[8] = {0,0,0,0,0,0,0,0};
					int fcode = 0;
					int fn_code = 0;
					int srclen = 0;
					int fctrladdr_len;
					uint8 *messageData;

					inst->stoptimer = 0; //clear the flag, as we have received a message

					// 16 or 64 bit addresses
					switch(dw_event->msgu.frame[1])
					{
						case 0xCC: //
							memcpy(&srcAddr[0], &(dw_event->msgu.rxmsg_ll.sourceAddr[0]), ADDR_BYTE_SIZE_L);
							fn_code = dw_event->msgu.rxmsg_ll.messageData[FCODE];
							messageData = &dw_event->msgu.rxmsg_ll.messageData[0];
							srclen = ADDR_BYTE_SIZE_L;
							fctrladdr_len = FRAME_CRTL_AND_ADDRESS_L;
							break;
						case 0xC8: //
							memcpy(&srcAddr[0], &(dw_event->msgu.rxmsg_sl.sourceAddr[0]), ADDR_BYTE_SIZE_L);
							fn_code = dw_event->msgu.rxmsg_sl.messageData[FCODE];
							messageData = &dw_event->msgu.rxmsg_sl.messageData[0];
							srclen = ADDR_BYTE_SIZE_L;
							fctrladdr_len = FRAME_CRTL_AND_ADDRESS_LS;
							break;
						case 0x8C: //
							memcpy(&srcAddr[0], &(dw_event->msgu.rxmsg_ls.sourceAddr[0]), ADDR_BYTE_SIZE_S);
							fn_code = dw_event->msgu.rxmsg_ls.messageData[FCODE];
							messageData = &dw_event->msgu.rxmsg_ls.messageData[0];
							srclen = ADDR_BYTE_SIZE_S;
							fctrladdr_len = FRAME_CRTL_AND_ADDRESS_LS;
							break;
						case 0x88: //
							memcpy(&srcAddr[0], &(dw_event->msgu.rxmsg_ss.sourceAddr[0]), ADDR_BYTE_SIZE_S);
							fn_code = dw_event->msgu.rxmsg_ss.messageData[FCODE];
							messageData = &dw_event->msgu.rxmsg_ss.messageData[0];
							srclen = ADDR_BYTE_SIZE_S;
							fctrladdr_len = FRAME_CRTL_AND_ADDRESS_S;
							break;
					}

					if((inst->ackexpected) && (inst->ackTO)) //ACK frame was expected but we got a good frame - treat as ACK timeout
					{
						//printf("got good frame instead of ACK in DWT_SIG_RX_OKAY - pretend TO\n");
						inst_processrxtimeout(inst);
						message = 0; //clear the message as we have processed the event
					}
                    else
                    {

                    	inst->ackexpected = 0; //clear this as we got good frame (but as not using ACK TO) we prob missed the ACK - check if it has been addressed to us

                    	fcode = fn_code; //tag has address filtering so if it received a frame it must be addressed to it

                        switch(fcode)
                        {
							case RTLS_DEMO_MSG_RNG_INIT:
							{
								if(inst->mode == TAG_TDOA) //only start ranging with someone if not ranging already
								{
									//double delay = rxrngmsg->messageData[RES_T1] + (rxrngmsg->messageData[RES_T2] << 8); //in ms

									inst->testAppState = TA_TXPOLL_WAIT_SEND ; // send next poll
									//remember the anchor address
									inst->msg_f.destAddr[0] =  srcAddr[0];
									inst->msg_f.destAddr[1] =  srcAddr[1];
									inst->msg_f.sourceAddr[0] = messageData[RES_R1];
									inst->msg_f.sourceAddr[1] = messageData[RES_R1+1];

									inst->tagShortAdd = (uint16)messageData[RES_R1] + ((uint16)messageData[RES_R2] << 8) ;
									dwt_setaddress16(inst->tagShortAdd);

									//instancesetreplydelay(delay); //

									inst->mode = TAG ;
									inst->rxTimeouts = 0; //reset timeout count
									inst->instToSleep = 0; //don't go to sleep - start ranging instead and then sleep after 1 range is done
									inst->done = INST_NOT_DONE_YET;
								}
							}
							break; //RTLS_DEMO_MSG_RNG_INITF

                            case RTLS_DEMO_MSG_ANCH_RESPF:
                            {
#if (TWSYMRANGE == 1)
								//need to write the delayed time before starting transmission
								inst->delayedReplyTime32 = ((uint32)dw_event->timeStamp32h + (uint32)inst->fixedFastReplyDelay32h) ;
								dwt_setdelayedtrxtime(inst->delayedReplyTime32) ;

								dwt_writetxfctrl((TAG_FINAL_F_MSG_LEN + FRAME_CRTL_AND_ADDRESS_S + FRAME_CRC), FINAL_MSG_OFFSET);

								if(dwt_starttx(DWT_START_TX_DELAYED))
								{
									//error - TX FAILED
									// initiate the re-transmission
									inst->testAppState = TA_TXE_WAIT ;
									inst->nextState = TA_TXPOLL_WAIT_SEND;

									dwt_entersleepaftertx(0);

									inst->wait4ack = 0; //clear the flag as the TX has
									inst->lateTX++;
									break; //exit this switch case...
								}
								else
								{
									rtd_t rtd;
									//calculate the difference between response rx and final tx
									//here we just need to subtract the low 32 bits as the response delay is < 32bits (actually it is < 26 bits)
									rtd.diffRmP = (uint32)dw_event->timeStamp32l - (uint32)inst->tagPollTxTime32l ;
									//calculate difference between final tx and response rx
									rtd.diffFmR = (uint32)inst->txantennaDelay + ((uint32)inst->fixedFastReplyDelay32h << 8) - ((uint32)dw_event->timeStamp32l & 0x1FF);
									//write the rest of the message (the two response time differences (low 32 bits)
									dwt_writetxdata((TAG_FINAL_F_MSG_LEN - 1 + FRAME_CRC), (uint8 *)  &rtd, (FINAL_MSG_OFFSET+FRAME_CRTL_AND_ADDRESS_S+1)) ;   // write the frame data

									inst->sentSN = inst->msg_f.seqNum;
									inst->previousState = TA_TXFINAL_WAIT_SEND;
									//if Tag is not waiting for report - it will go to sleep automatically after the final is sent
									inst->done = INST_DONE_WAIT_FOR_NEXT_EVENT_TO; //kick off the TagTimeoutTimer (instancetimer) to initiate wakeup
									inst->testAppState = TA_SLEEP_DONE; //we are going automatically to sleep so no TX confirm interrupt (next state will be TA_SLEEP_DONE)
									inst->canprintinfo = 1;
									inst->txmsgcount ++;
									inst->frame_sn++ ; //increment as final is sent
								}

								inst->respPSC = (dwt_read16bitoffsetreg(0x10, 2) >> 4);
								inst->wait4ack = 0; //no response
								inst->ackexpected = !ACK_REQUESTED ; //used to ignore unexpected ACK frames
								//inst->rxu.anchorRespRxTime = inst->rxu.rxTimeStamp ; //Response's Rx time

								inst->nextState = TA_TXPOLL_WAIT_SEND;
#else
								if(inst->responseRxNum == 0) // this is first response
								{
									dwt_setrxtimeout(5000); //~5ms
									//turn RX on
									dwt_rxenable(0) ;               // turn receiver on, immediate = 0/delayed = 1
									inst->anchResp1RxTime32l = dw_event->timeStamp32l;
									inst->responseRxNum++;
								}
								else // we have two responses and can calculate ToF
								{
									//the first response will be sent time X after reception of the poll, but as the tx time is snapped to 8ns
									//we need to account for the low 9 bits of poll rx time in the RTD calculation
									uint32 pollrxlowbits = (uint32)messageData[1] + (uint32)(messageData[2] << 8);

									//RTD = (RxResp1 - TxPoll) - (RxResp2 - RxResp1)
									//ToF = RTD/2 = RxResp1 - 0.5 * (TxPoll + RxResp2)

									inst->tof32 = ((uint32)inst->anchResp1RxTime32l - (uint32)inst->tagPollTxTime32l + pollrxlowbits) - ((uint32)dw_event->timeStamp32l - (uint32)inst->anchResp1RxTime32l);

									inst->tof32 <<= 1; //to make it compatible with reportTOF() which expects ToF*4

									reportTOF_f(inst);
                                    inst->newrange = 1;
                                    inst->testAppState = TA_TXE_WAIT ;
                                    inst->nextState = TA_TXPOLL_WAIT_SEND;
								}
#endif


                            }
                            break; //RTLS_DEMO_MSG_ANCH_RESPF

                            case RTLS_DEMO_MSG_ANCH_TOFRF:
                            {
                                    inst->tof32 = messageData[TOFR];
                                    inst->tof32 += (uint32)messageData[TOFR+1] << 8;
                                    inst->tof32 += (uint32)messageData[TOFR+2] << 16;
                                    inst->tof32 += (uint32)messageData[TOFR+3] << 24;

                                    if(dw_event->msgu.rxmsg_ss.seqNum != inst->lastReportSN)
                                    {
                                    	reportTOF_f(inst);
                                        inst->newrange = 1;
                                        inst->lastReportSN = dw_event->msgu.rxmsg_ss.seqNum;
                                        inst->newrangetagaddress = srcAddr[0] + ((uint16) srcAddr[1] << 8);
                                        inst->newrangeancaddress = inst->eui64[0] + ((uint16) inst->eui64[1] << 8);
                                    }

                                    inst->testAppState = TA_TXE_WAIT;
                                    inst->nextState = TA_TXPOLL_WAIT_SEND ; // send next poll

                            }
                            break; //RTLS_DEMO_MSG_ANCH_TOFRF

                            default:
                            {
                                //only enable receiver when not using double buffering
                                inst->testAppState = TA_RX_WAIT_DATA ;              // wait for next frame
                                //turn RX on
                                dwt_rxenable(0) ;               // turn receiver on, immediate = 0/delayed = 1

                            }
                            break;
                        } //end switch (rxmsg->functionCode)

                        if(dw_event->msgu.frame[0] & 0x20)
    					{
    						//as we only pass the received frame with the ACK request bit set after the ACK has been sent
    						instance_getevent(9); //get and clear the ACK sent event
    					}

                    }
                }
                break ;

                case DWT_SIG_RX_TIMEOUT :
                    //printf("PD_DATA_TIMEOUT") ;
                	instance_getevent(26); //get and clear this event
                	inst_processrxtimeout(inst);
                    message = 0; //clear the message as we have processed the event
                break ;

                case DWT_SIG_TX_AA_DONE: //ignore this event - just process the rx frame that was received before the ACK response
                case 0: //no event - wait in receive...
                {
                    //stay in Rx (fall-through from above state)
                    //if(DWT_SIG_TX_AA_DONE == message) printf("Got SIG_TX_AA_DONE in RX wait - ignore\n");
                    if(inst->done == INST_NOT_DONE_YET) inst->done = INST_DONE_WAIT_FOR_NEXT_EVENT;
                }
                break;

                default :
                {
                    //printf("\nERROR - Unexpected message %d ??\n", message) ;
                    //assert(0) ;                                             // Unexpected Primitive what is going on ?
                }
                break ;

            }
            break ; // end case TA_RX_WAIT_DATA

            default:
                //printf("\nERROR - invalid state %d - what is going on??\n", inst->testAppState) ;
            break;
    } // end switch on testAppState

    return inst->done;
} // end testapprun()
示例#7
0
// First (generic) init of the DW1000
dw1000_err_e dw1000_init () {

	// Do the STM setup that initializes pin and peripherals and whatnot.
	if (!_stm_dw1000_interface_setup) {
		setup();
	}

	// Reset the dw1000...for some reason
	dw1000_reset();
	uDelay(100);

	// Make sure we can talk to the DW1000
	uint32_t devID;
	devID = dwt_readdevid();
	if (devID != DWT_DEVICE_ID) {
		//if we can't talk to dw1000, return with an error
		uDelay(1000);
		return DW1000_COMM_ERR;
	}

	GPIO_WriteBit(STM_GPIO3_PORT, STM_GPIO3_PIN, Bit_SET);
	uDelay(1000);
	GPIO_WriteBit(STM_GPIO3_PORT, STM_GPIO3_PIN, Bit_RESET);

	// Choose antenna 0 as a default
	dw1000_choose_antenna(0);

	// Initialize the dw1000 hardware
	uint32_t err;
	err = dwt_initialise(DWT_LOADUCODE |
	                     DWT_LOADLDO |
	                     DWT_LOADTXCONFIG |
	                     DWT_LOADXTALTRIM);

	if (err != DWT_SUCCESS) {
		return DW1000_COMM_ERR;
	}

	// Configure interrupts and callbacks
	dwt_setinterrupt(0xFFFFFFFF, 0);
	dwt_setinterrupt(DWT_INT_TFRS |
	                 DWT_INT_RFCG |
	                 DWT_INT_RPHE |
	                 DWT_INT_RFCE |
	                 DWT_INT_RFSL |
	                 DWT_INT_RFTO |
	                 DWT_INT_RXPTO |
	                 DWT_INT_SFDT |
	                 DWT_INT_ARFE, 1);

	dwt_setcallbacks(txcallback, rxcallback);

	// Set the parameters of ranging and channel and whatnot
	global_ranging_config.chan           = 2;
	global_ranging_config.prf            = DWT_PRF_64M;
	global_ranging_config.txPreambLength = DWT_PLEN_64;
	global_ranging_config.rxPAC          = DWT_PAC8;
	global_ranging_config.txCode         = 9;  // preamble code
	global_ranging_config.rxCode         = 9;  // preamble code
	global_ranging_config.nsSFD          = 0;
	global_ranging_config.dataRate       = DWT_BR_6M8;
	global_ranging_config.phrMode        = DWT_PHRMODE_EXT; //Enable extended PHR mode (up to 1024-byte packets)
	global_ranging_config.smartPowerEn   = 1;
	global_ranging_config.sfdTO          = 64+8+1;//(1025 + 64 - 32);
#if DW1000_USE_OTP
	dwt_configure(&global_ranging_config, (DWT_LOADANTDLY | DWT_LOADXTALTRIM));
#else
	dwt_configure(&global_ranging_config, 0);
#endif
	dwt_setsmarttxpower(global_ranging_config.smartPowerEn);

	// Configure TX power based on the channel used
	global_tx_config.PGdly = pgDelay[global_ranging_config.chan];
	global_tx_config.power = txPower[global_ranging_config.chan];
	dwt_configuretxrf(&global_tx_config);

	// Need to set some radio properties. Ideally these would come from the
	// OTP memory on the DW1000
#if DW1000_USE_OTP == 0

	// This defaults to 8. Don't know why.
	dwt_xtaltrim(8);

	// Antenna delay we don't really care about so we just use 0
	dwt_setrxantennadelay(DW1000_ANTENNA_DELAY_RX);
	dwt_settxantennadelay(DW1000_ANTENNA_DELAY_TX);
#endif

	return DW1000_NO_ERR;
}
示例#8
0
void listener_run(void) {

	uint32_t announce_coord_ts = 0;
	uint32_t elapsed = 0;
	uint32_t last_ts = 0;
	uint32_t count = 0;

	irq_init();
	pio_disable_interrupt(DW_IRQ_PIO, DW_IRQ_MASK);



	// Setup DW1000
	dwt_txconfig_t txconfig;

	// Setup DECAWAVE
	reset_DW1000();
	spi_set_rate_low();
	dwt_initialise(DWT_LOADUCODE);
	spi_set_rate_high();

	dwt_configure(&cph_config->dwt_config);

	dwt_setpanid(0x4350);
	dwt_setaddress16(0x1234);

	// Clear CLKPLL_LL
	dwt_write32bitreg(SYS_STATUS_ID, 0x02000000);

	uint32_t id = dwt_readdevid();
	printf("Device ID: %08X\r\n", id);


#if 1

	dwt_setcallbacks(0, rxcallback);
	dwt_setinterrupt(
			DWT_INT_TFRS | DWT_INT_RFCG
					| (DWT_INT_ARFE | DWT_INT_RFSL | DWT_INT_SFDT | DWT_INT_RPHE | DWT_INT_RFCE | DWT_INT_RFTO /*| DWT_INT_RXPTO*/),
			1);

	pio_enable_interrupt(DW_IRQ_PIO, DW_IRQ_MASK);

	dwt_rxenable(0);

	while (1) {

		elapsed = cph_get_millis() - last_ts;
		if (elapsed > 5000) {
			printf("alive %d\r\n", count++);
			last_ts = cph_get_millis();
		}

		if (trx_signal == SIGNAL_RCV) {
			printf("[RCV] %d - ", frame_len);
			for (int i = 0; i < frame_len; i++) {
				printf("%02X ", rx_buffer[i]);
			}
			printf("\r\n");
			trx_signal = SIGNAL_EMPTY;
			dwt_rxenable(0);
		}
		else if(trx_signal == SIGNAL_ERR) {
			printf("ERROR: %08X\r\n", error_status_reg);
			trx_signal = SIGNAL_EMPTY;
			dwt_rxenable(0);
		}
		else if(trx_signal == SIGNAL_ERR_LEN) {
			printf("ERROR LENGTH: %08X\r\n", error_status_reg);
			trx_signal = SIGNAL_EMPTY;
			dwt_rxenable(0);
		}
	}

#else

	while (1) {

		/* Activate reception immediately. */
		dwt_rxenable(0);

		while (!((status_reg = dwt_read32bitreg(SYS_STATUS_ID)) & (SYS_STATUS_RXFCG | SYS_STATUS_ALL_RX_ERR))) {
		};

		if (status_reg & SYS_STATUS_RXFCG) {
			uint32 frame_len;

			dwt_write32bitreg(SYS_STATUS_ID, SYS_STATUS_RXFCG);

			frame_len = dwt_read32bitreg(RX_FINFO_ID) & RX_FINFO_RXFL_MASK_1023;
			if (frame_len <= MAXRXSIXZE) {
				dwt_readrxdata(rx_buffer, frame_len, 0);
			} else {
				frame_len = 0;
			}

			if (frame_len > 0) {
				printf("[RCV] ");
				for (int i = 0; i < frame_len; i++) {
					printf("%02X ", rx_buffer[i]);
				}
				printf("\r\n");
			} else {
				printf("ERROR: frame_len == %d\r\n", frame_len);
			}

		} else {

			printf("ERROR: dwt_rxenable has status of %08X\r\n", status_reg);
			/* Clear RX error events in the DW1000 status register. */
			dwt_write32bitreg(SYS_STATUS_ID, SYS_STATUS_ALL_RX_ERR | SYS_STATUS_CLKPLL_LL);
		}
	}
#endif

}
示例#9
0
bool DWM1000_Tag::dispatch(Msg& msg) {
	PT_BEGIN()
	PT_WAIT_UNTIL(msg.is(0, SIG_INIT));
	init();

	POLL_SEND: {
		while (true) {
			timeout(1000);				// delay  between POLL
			PT_YIELD_UNTIL(timeout());
			/* Write frame data to DW1000 and prepare transmission. See NOTE 7 below. */
			tx_poll_msg[ALL_MSG_SN_IDX] = frame_seq_nb;
			dwt_writetxdata(sizeof(tx_poll_msg), tx_poll_msg, 0);
			dwt_writetxfctrl(sizeof(tx_poll_msg), 0);

			/* Start transmission, indicating that a response is expected so that reception is enabled automatically after the frame is sent and the delay
			 * set by dwt_setrxaftertxdelay() has elapsed. */
			LOG<< " Start TXF " << FLUSH;
			dwt_starttx(DWT_START_TX_IMMEDIATE | DWT_RESPONSE_EXPECTED);// SEND POLL MSG
			dwt_setinterrupt(DWT_INT_TFRS, 0);
			dwt_setinterrupt(DWT_INT_RFCG, 1);	// enable
			clearInterrupt();
			_timeoutCounter = 0;

			/* We assume that the transmission is achieved correctly, poll for reception of a frame or error/timeout. See NOTE 8 below. */
			timeout(10);
			PT_YIELD_UNTIL(timeout() || isInterruptDetected());	// WAIT RESP MSG

			if (isInterruptDetected())
				LOG<< " INTERRUPT DETECTED " << FLUSH;

			status_reg = dwt_read32bitreg(SYS_STATUS_ID);
			LOG<< HEX <<" SYS_STATUS " << status_reg << FLUSH;
			if (status_reg == 0xDEADDEAD) {
				init();
			} else if (status_reg & SYS_STATUS_RXFCG)
				goto RESP_RECEIVED;
			else if (status_reg & SYS_STATUS_ALL_RX_ERR) {

				if (status_reg & SYS_STATUS_RXRFTO)
					INFO(" RX Timeout");
				else
					INFO(" RX error ");
				dwt_write32bitreg(SYS_STATUS_ID, SYS_STATUS_ALL_RX_ERR); /* Clear RX error events in the DW1000 status register. */

			}
		}

	}
	RESP_RECEIVED: {

		LOG<< " Received " <<FLUSH;

		frame_seq_nb++; /* Increment frame sequence number after transmission of the poll message (modulo 256). */
		uint32 frame_len;

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

		/* A frame has been received, read iCHANGEt into the local buffer. */
		frame_len =
		dwt_read32bitreg(RX_FINFO_ID) & RX_FINFO_RXFLEN_MASK;
		if (frame_len <= RX_BUF_LEN) {
			dwt_readrxdata(rx_buffer, frame_len, 0);
		}

		/* Check that the frame is the expected response from the companion "DS TWR responder" example.
		 * As the sequence number field of the frame is not relevant, it is cleared to simplify the validation of the frame. */
		rx_buffer[ALL_MSG_SN_IDX] = 0;
		if (memcmp(rx_buffer, rx_resp_msg, ALL_MSG_COMMON_LEN) == 0) {	// CHECK RESP MSG
			uint32 final_tx_time;

			/* Retrieve poll transmission and response reception timestamp. */
			poll_tx_ts = get_tx_timestamp_u64();
			resp_rx_ts = get_rx_timestamp_u64();

			/* Compute final message transmission time. See NOTE 9 below. */
			final_tx_time = (resp_rx_ts
					+ (RESP_RX_TO_FINAL_TX_DLY_UUS * UUS_TO_DWT_TIME))
			>> 8;
			dwt_setdelayedtrxtime(final_tx_time);

			/* Final TX timestamp is the transmission time we programmed plus the TX antenna delay. */
			final_tx_ts = (((uint64) (final_tx_time & 0xFFFFFFFE)) << 8)
			+ TX_ANT_DLY;

			/* Write all timestamps in the final message. See NOTE 10 below. */
			final_msg_set_ts(&tx_final_msg[FINAL_MSG_POLL_TX_TS_IDX],
					poll_tx_ts);
			final_msg_set_ts(&tx_final_msg[FINAL_MSG_RESP_RX_TS_IDX],
					resp_rx_ts);
			final_msg_set_ts(&tx_final_msg[FINAL_MSG_FINAL_TX_TS_IDX],
					final_tx_ts);

			/* Write and send final message. See NOTE 7 below. */
			tx_final_msg[ALL_MSG_SN_IDX] = frame_seq_nb;
			dwt_writetxdata(sizeof(tx_final_msg), tx_final_msg, 0);
			dwt_writetxfctrl(sizeof(tx_final_msg), 0);
			dwt_starttx(DWT_START_TX_DELAYED);				// SEND FINAL MSG

			/* Poll DW1000 until TX frame sent event set. See NOTE 8 below. */
			timeout(10);
			PT_YIELD_UNTIL((dwt_read32bitreg(SYS_STATUS_ID) & SYS_STATUS_TXFRS) || timeout());;
			/* Clear TXFRS event. */
			dwt_write32bitreg(SYS_STATUS_ID, SYS_STATUS_TXFRS);

			/* Increment frame sequence number after transmission of the final message (modulo 256). */
			frame_seq_nb++;
		} else {