void DWM1000_Anchor::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); if (dwt_initialise(DWT_LOADUCODE)) { LOG<< " dwt_initialise failed " << FLUSH; } else LOG<< " dwt_initialise done." << FLUSH; if (dwt_configure(&config)) { LOG<< " dwt_configure failed " << FLUSH; } else LOG<< " dwt_configure done." << FLUSH; uint32_t device_id = dwt_readdevid(); uint32_t part_id = dwt_getpartid(); uint32_t lot_id = dwt_getlotid(); LOG<< HEX << " device id : " << device_id << ", part id : " << part_id << ", lot_id :" << lot_id <<FLUSH; /* Apply default antenna delay value. See NOTE 1 below. */ dwt_setrxantennadelay(RX_ANT_DLY); 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_initialise(DWT_LOADUCODE); // Configure the callbacks from the dwt library dwt_setcallbacks(txcallback, rxcallback); _count = 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; }