Ejemplo n.º 1
0
/*! ------------------------------------------------------------------------------------------------------------------
 * Function: decamutexon()
 *
 * Description: This function should disable interrupts. This is called at the start of a critical section
 * It returns the irq state before disable, this value is used to re-enable in decamutexoff call
 *
 * Note: The body of this function is defined in deca_mutex.c and is platform specific
 *
 * input parameters:	
 *
 * output parameters
 *
 * returns the state of the DW1000 interrupt
 */
decaIrqStatus_t decamutexon(void)           
{
	decaIrqStatus_t s = port_GetEXT_IRQStatus();

	if(s) {
		port_DisableEXT_IRQ(); //disable the external interrupt line
	}
	return s ;   // return state before disable, value is used to re-enable in decamutexoff call
}
Ejemplo n.º 2
0
/*! ------------------------------------------------------------------------------------------------------------------
 * Function: decamutexon()
 *
 * Description: This function should disable interrupts. This is called at the start of a critical section
 * It returns the irq state before disable, this value is used to re-enable in decamutexoff call
 *
 * Note: The body of this function is defined in deca_mutex.c and is platform specific
 *
 * input parameters:	
 *
 * output parameters
 *
 * returns the state of the DW1000 interrupt
 */
decaIrqStatus_t decamutexon(void)           
{
	decaIrqStatus_t s = port_GetEXT_IRQStatus();

	if(s) {
		//HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_5);
		port_DisableEXT_IRQ(); //disable the external interrupt line
	}
	return s ;   // return state before disable, value is used to re-enable in decamutexoff call
}
Ejemplo n.º 3
0
int process_usbmessage(void)
{
	int result = 0;
	switch(application_mode)
	{
		case STAND_ALONE:
		{
			if(local_buff_length == 5)
			{
				//d (from "deca")
				if((local_buff[0] == 100) && (result == 0)) //d (from "deca")
				{
					if(local_buff[4] == 63)
					{
						int i = sizeof(SOFTWARE_VER_STRINGUSB);
						//change mode to  USB_TO_SPI and send a reply "y"
						tx_buff[0] = 121;
						memcpy(&tx_buff[1], SOFTWARE_VER_STRINGUSB, i);
						tx_buff[i+2] = 0;
						tx_buff_length = i + 2;
						application_mode = USB_TO_SPI;
						result = 2;
						//led_off(LED_ALL);
						led_on(LED_PC7); //turn on LED to indicate connection to PC application
					}
				}
			}
		}
		break;


		case USB_TO_SPI:
		{
			//first byte specifies the SPI speed and SPI read/write operation
			//bit 0 = 1 for write, 0 for read
			//bit 1 = 1 for high speed, 0 for low speed

			//<STX>   <ETX>
			//
			//to read from the device (e.g. 4 bytes), total length is = 1 + 1 + length_of_command (2) + length_of_data_to_read (2) + header length (1/2) + 1
			//
			//to write to the device (e.g. 4 bytes),  total length is = 1 + 1 + length_of_command (2) + length_of_data_to_write (2) + header length (1/2) + data length + 1
			//
			//LBS comes first:   0x2, 0x2, 0x7, 0x0, 0x04, 0x00, 0x3

			if(local_buff_length)
			{
				//0x2 = STX - start of SPI transaction data
				if(local_buff[0] == 0x2)
				{
					//configure SPI speed
					configSPIspeed(((local_buff[1]>>1) & 0x1));

					if((local_buff[1] & 0x1) == 0) //SPI read
					{
						int msglength = local_buff[2] + (local_buff[3]<<8);
						int datalength = local_buff[4] + (local_buff[5]<<8);

						//led_on(LED_PC6);
						tx_buff[0] = 0x2;
						tx_buff[datalength+2] = 0x3;

						//max data we can read in a single SPI transaction is 4093 as the USB/VCP tx buffer is only 4096 bytes long
						if((local_buff[msglength-1] != 0x3) || (datalength > 4093))
						{
							tx_buff[1] = 0x1; // if no ETX (0x3) indicate error
						}
						else
						{
							// do the read from the SPI
							readfromspi(msglength-7, &local_buff[6], datalength, &tx_buff[2]);  // result is stored in the buffer

							tx_buff[1] = 0x0; // no error
						}

						tx_buff_length = datalength + 3;
						result = 2;
					}

					if((local_buff[1] & 0x1) == 1) //SPI write
					{
						int msglength = local_buff[2] + (local_buff[3]<<8);
						int datalength = local_buff[4] + (local_buff[5]<<8);
						int headerlength = msglength - 7 - datalength;

						if(local_buff_length == msglength) //we got the whole message (sent from the PC)
						{
							local_buff_offset = 0;

							led_off(LED_PC6);
							tx_buff[0] = 0x2;
							tx_buff[2] = 0x3;

							if(local_buff[msglength-1] != 0x3)
							{
								tx_buff[1] = 0x1; // if no ETX (0x3) indicate error
							}
							else
							{
								// do the write to the SPI
								writetospi(headerlength, &local_buff[6], datalength, &local_buff[6+headerlength]);  // result is stored in the buffer

								tx_buff[1] = 0x0; // no error
							}

							tx_buff_length = 3;
							result = 2;
						}
						else //wait for the whole message
						{
							led_on(LED_PC6);
						}
					}
				}

				if((local_buff[0] == 100) && (result == 0)) //d (from "deca")
				{
					if(local_buff[4] == 63)
					{
						int i = sizeof(SOFTWARE_VER_STRINGUSB);
						//change mode to  USB_TO_SPI and send a reply "y"
						tx_buff[0] = 121;
						memcpy(&tx_buff[1], SOFTWARE_VER_STRINGUSB, i);
						tx_buff[i+2] = 0;
						tx_buff_length = i + 2;
						application_mode = USB_TO_SPI;
						result = 2;
						//led_off(LED_ALL);
						led_on(LED_PC7); //turn on LED to indicate connection to PC application
					}

				}

				if((local_buff[0] == 114) && (result == 0)) //r - flush the USB buffers...
				{
					DCD_EP_Flush(&USB_OTG_dev, CDC_IN_EP);
					result = 0;
				}
			}
		}
		break;

		case USB_PRINT_ONLY:
			if(local_buff_length && (result == 0))
			{
				if((local_buff[0] == 0x5) && (local_buff[5] == 0x5))
				{
					uint16 txantennadelay = local_buff[1] + (local_buff[2]<<8);
					uint16 rxantennadelay = local_buff[3] + (local_buff[4]<<8);
					instanceconfigantennadelays(txantennadelay, rxantennadelay);
				}
				if((local_buff[0] == 0x7) && (local_buff[5] == 0x7))
				{
					//not used in EVK
				}
				if((local_buff[0] == 0x6) && (local_buff[2] == 0x6))
				{
					uint8 switchS1 = local_buff[1];

					//disable DW1000 IRQ
					port_DisableEXT_IRQ(); //disable IRQ until we configure the device
					//turn DW1000 off
					dwt_forcetrxoff();
					//re-configure the instance
					inittestapplication(switchS1);
					//save the new setting
					s1configswitch = switchS1;
					//set the LDC
					setLCDline1(switchS1);
					//enable DW1000 IRQ
					port_EnableEXT_IRQ(); //enable IRQ before starting

					ranging = 0;

				}
				//d (from "deca")
				if(local_buff[0] == 100) //d (from "deca")
				{
					if(local_buff[4] == 63)
					{
						int i = sizeof(SOFTWARE_VER_STRINGUSB);
						//send a reply "n"
						tx_buff[0] = 110;
						memcpy(&tx_buff[1], SOFTWARE_VER_STRINGUSB, i);
						tx_buff[i+2] = 0;
						tx_buff_length = i + 2;
						result = 2;
					}
					if(local_buff[4] == 36) //"$"
					{
						//send a reply "n"
						tx_buff[0] = 110;
						memcpy(&tx_buff[1], version, version_size);
						tx_buff[version_size+1] = s1configswitch & 0xff;
						tx_buff[version_size+2] = '\r';
						tx_buff[version_size+3] = '\n';
						tx_buff_length = version_size + 4;
						result = 2;
					}
					if(local_buff[4] == 33) //"!"
					{
						//send back the DW1000 partID and lotID
						uint32 partID = dwt_getpartid();
						uint32 lotID = dwt_getlotid();
						tx_buff[0] = 110;
						memcpy(&tx_buff[1], &partID, 4);
						memcpy(&tx_buff[5], &lotID, 4);
						tx_buff[9] = '\r';
						tx_buff[10] = '\n';
						tx_buff_length = 11;
						result = 2;
					}
				}
			}
			break;

		default:
			break;
	}
Ejemplo n.º 4
0
portTASK_FUNCTION(task_decawave, pvParameters) {
	UNUSED(pvParameters);

	int i = 0;
	int toggle = 1;
	int ranging = 0;
	uint8 dataseq[40];
	double range_result = 0;
	double avg_result = 0;
	uint8 dataseq1[40];
	uint8 command = 0x0;

	led_off(LED_ALL); //turn off all the LEDs

	peripherals_init();

	spi_peripheral_init();

	Sleep(1000); //wait for LCD to power on

	printf("DECAWAVE        \r\n");
	printf(SOFTWARE_VER_STRING);
	printf("\r\n");

	Sleep(1000);

	port_DisableEXT_IRQ(); //disable ScenSor IRQ until we configure the device

	printf("DECAWAVE  RANGE\r\n");

	led_off(LED_ALL);

	int testresult = inittestapplication();
	if (testresult < 0) {
		led_on(LED_ALL); //to display error....
		printf("ERROR\r\n");
		printf("INIT FAIL %d\r\n", testresult);
		for (;;) {
		}
	}

	//sleep for 5 seconds displaying "Decawave"
	i = 30;
	while (i--) {
		if (i & 1)
			led_off(LED_ALL);
		else
			led_on(LED_ALL);

		Sleep(200);
	}
	i = 0;
	led_off(LED_ALL);

	if (is_tag) {
		instance_mode = TAG;
		printf("TAG\r\n");
	} else {
		instance_mode = ANCHOR;
		printf("ANCHOR\r\n");
#if (DR_DISCOVERY == 1)
		printf("DR_DISCOVER == 1\r\n");
#else
		printf("DR_DISCOVER == 0\r\n");
#endif
	}

	if (instance_mode == TAG) {
		//if TA_SW1_2 is on use fast ranging (fast 2wr)
		if (use_fast2wr) {
			printf("Fast Tag Ranging\r\n");
		} else {
			printf("TAG BLINK %llX\r\n", instance_get_addr());
		}
	} else {
		printf("AWAITING POLL\r\n");
	}

	port_EnableEXT_IRQ(); //enable ScenSor IRQ before starting

	// main loop
	while (1) {

		//ERIC: Delay irq handling...will this work?
		uint32_t bail = 0;
		if (irq_set) {
			do {
				if (bail++ > 2000) {
					printf("BAIL!\r\n");
					Sleep(10);
					for (;;) {
					}
				}
				instance_process_irq(0);
			} while (port_CheckEXT_IRQ() == 1);
			irq_set = 0x00;
		}

		instance_run();

		if (instancenewrange()) {
			ranging = 1;
			//send the new range information to LCD and/or USB
			range_result = instance_get_idist();
#if (DR_DISCOVERY == 0)
			if(instance_mode == ANCHOR)
#endif
			avg_result = instance_get_adist();
			//set_rangeresult(range_result);

			printf("LAST: %4.2f m   ", range_result);
#if (DR_DISCOVERY == 0)
			if(instance_mode == ANCHOR)
			printf("AVG8: %4.2f m", avg_result);
			else
			printf("%llx", instance_get_anchaddr());
#else
			printf("AVG8: %4.2f m\r\n", avg_result);
#endif
		}

		if (ranging == 0) {
			if (instance_mode != ANCHOR) {
				if (instancesleeping()) {
					if (toggle) {
						printf("AWAITING RESPONSE\r\n");
					} else {
						toggle = 1;
						printf("TAG BLINK %llX\r\n", instance_get_addr());
					}
				}

				if (instanceanchorwaiting() == 2) {
					ranging = 1;
					printf("RANGING WITH %016llX\r\n", instance_get_anchaddr());
				}
			} else {
				if (instanceanchorwaiting()) {
					toggle += 2;

					if (toggle > 300000) {
						if (toggle & 0x1) {
							toggle = 0;
							printf("AWAITING POLL\r\n");
						} else {
							toggle = 1;
#if (DR_DISCOVERY == 1)
							printf("DISCOVERY MODE ");
#else
							printf("NON DISCOVERY ");
#endif
							printf("%llX\r\n", instance_get_addr());
						}
//						print_status();
					}

				} else if (instanceanchorwaiting() == 2) {
					printf("RANGING WITH %llX", instance_get_tagaddr());
				}
			}
		}
	}
}