/*! ---------------------------------------------------------------------------- * @file instance.h * @brief DecaWave header for application level instance * * @attention * * Copyright 2015 (c) DecaWave Ltd, Dublin, Ireland. * * All rights reserved. * * @author DecaWave */ #ifndef _INSTANCE_H_ #define _INSTANCE_H_ #ifdef __cplusplus extern "C" { #endif #include "deca_types.h" #include "deca_device_api.h" #include "deca_param_types.h" #include "port.h" #include "config.h" #include "stateMachine.h" #include "message.h" #include "event.h" // TEST anc for peroid and sucess rate #define ANC_RESP_TEST 0 #define REPORT2WIFI //#define REPORT2COM #if !defined(REPORT2COM) && !defined(REPORT2WIFI) #define REPORT2COM #endif /****************************************************************************************************************** ********************* NOTES on DW (MP) features/options *********************************************************** *******************************************************************************************************************/ //#define DEEP_SLEEP (1) //To enable deep-sleep set this to 1 #define DEEP_SLEEP (0) //To enable deep-sleep set this to 1 //DEEP_SLEEP mode can be used, for example, by a Tag instance to put the DW1000 into low-power deep-sleep mode: // when the Anchor is sending the range report back to the Tag, the Tag will enter sleep after a ranging exchange is finished // once it receives a report or times out, before the next poll message is sent (before next ranging exchange is started). #define CORRECT_RANGE_BIAS (1) // Compensate for small bias due to uneven accumulator growth at close up high power #define ANCTOANCTWR (0) //if set to 1 then anchor to anchor TWR will be done in the last slot /****************************************************************************************************************** ******************************************************************************************************************* *******************************************************************************************************************/ #define NUM_INST 1 #define SPEED_OF_LIGHT (299702547.0) // in m/s in air #define MASK_40BIT (0x00FFFFFFFFFF) // DW1000 counter is 40 bits #define MASK_TXDTS (0x00FFFFFFFE00) //The TX timestamp will snap to 8 ns resolution - mask lower 9 bits. #define SIG_RX_UNKNOWN 99 // Received an unknown frame // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! // NOTE: the maximum RX timeout is ~ 65ms // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! #define TOF_REPORT_NUL 0 #define TOF_REPORT_T2A 1 #define TOF_REPORT_A2A 2 #define INVALID_TOF (0xABCDFFFF) /****************************************************************************************************************** ******************************************************************************************************************* *******************************************************************************************************************/ typedef enum _rtx_stage_{ RTX_IDLE=0, RTX_RX, RTX_TX_THEN_RX, RTX_TX, }eRxTxStage; // TX power and PG delay configuration structure typedef struct { uint8_t PGdelay; //TX POWER //31:24 BOOST_0.125ms_PWR //23:16 BOOST_0.25ms_PWR-TX_SHR_PWR //15:8 BOOST_0.5ms_PWR-TX_PHR_PWR //7:0 DEFAULT_PWR-TX_DATA_PWR uint32_t txPwr[2]; // }tx_struct; typedef struct _tof_inform_ { uint16_t address; uint32_t value; }stTofInformation; typedef struct _tof_result_ { uint16_t address; double distanceRaw; double distanceBias; }stFofResult; typedef struct { //diagnostic counters/data, results and logging //stTofInformation tofOfTags[MAX_TAG_LIST_SIZE]; //this is an array which holds last ToF from particular tag (ID 0-7) //this is an array which holds last ToF to each anchor it should stTofInformation tofArray[MAX_ANCHOR_LIST_SIZE]; //contain 4 ToF to 4 anchors all relating to same range number sequence //uint32_t tofAnc[MAX_ANCHOR_LIST_SIZE]; //this is an array which holds last ToFs from particular anchors (0, 0-1, 0-2, 1-2) //this is an array which holds last ToFs of the Anchor to Anchor ranging //uint32_t tofArrayAnc[MAX_ANCHOR_LIST_SIZE]; //it contains 3 ToFs relating to same range number sequence (0, 0-1, 0-2, 1-2) //stFofResult tagdistance[MAX_ANCHOR_LIST_SIZE]; stFofResult distance[MAX_ANCHOR_LIST_SIZE]; }twr_result; typedef struct { //timeouts and delays int32_t tagSleepTime_ms; //in milliseconds - defines the nominal Tag sleep time period int32_t tagSleepRnd; //add an extra slot duration to sleep time to avoid collision before getting synced by anchor 0 uint32_t delayedReplyTime; // delayed reply time of ranging-init/response/final message int32_t tagSleepCorrection; int32_t tagSleepCorrection2; uint32_t longTermRangeCount ; //total number of ranges //64 bit timestamps //union of TX timestamps uint64_t tagPollRxTime ; // receive time of poll message union { uint64_t txTimeStamp ; // last tx timestamp uint64_t tagPollTxTime ; // tag's poll tx timestamp uint64_t anchorRespTxTime ; // anchor's reponse tx timestamp }txu; uint64_t tagFinalRxTime ; // receive time of poll message }tm_and_delays; //Application data #pragma pack(1) typedef struct _tag_cmd_ { uint8_t control; uint8_t command; uint8_t reservered1; uint8_t reservered2; uint8_t reservered3; uint8_t reservered4; }stTagControlCommand; typedef struct _tag_status_ { uint8_t status; uint8_t power; uint8_t heartRate; uint8_t reserved1; uint8_t reserved2; float txRssi; float txRssiFinal; }stTagStatus; #pragma pack() typedef struct _tag_range_inform_ { uint16_t address; uint8_t flag; uint8_t rangeNumA; int16_t slot; uint16_t rxRespsIdx; //index into the array below (current tag (4bits)/seq number(4bits)) int8_t rxResps[RESP_RECORD_COUNT]; uint32_t tickUpdate; uint32_t tofOfTags; stTagStatus tagStatus; stTagControlCommand tagCommand; }stTagRangeInformation; typedef struct { inst_setting setting; //setting event_queue evtQueue; //event queue - used to store DW1000 events as they are processed by the dw_isr/callback functions twr_result twrResult; //twr result record state_machine stateMachine; //state machine tm_and_delays tmAndDelays; //time and delays params message_frame msgFrame; //message frame stTagRangeInformation rangeInform[MAX_TAG_LIST_SIZE]; } instance_data_t ; typedef struct _inst_debug { uint32_t errFrameCount; uint8_t toBeHandled; uint32_t type; uint32_t ancPollCount; uint32_t ancRespCounts[MAX_ANCHOR_LIST_SIZE]; uint32_t andRespSentCount; uint32_t ancFinalCount; uint32_t ancRespFailsCounts[MAX_ANCHOR_LIST_SIZE]; uint32_t ancRespLateCount; uint32_t tofSuccess; uint32_t calculateFails; uint32_t rxTimeoutCount; }stInstDebug; extern volatile eRxTxStage trxStage; extern volatile stExtandCommand g_stExtandCommand; //------------------------------------------------------------------------------------------------------------- // // Functions used in logging/displaying range and status data // //------------------------------------------------------------------------------------------------------------- // function to calculate and report the Time of Flight to the GUI/display int reportTOF(int idx, stTofInformation* tofx); void clearDistTable(int idx); void setTagDist(int tidx, int aidx); double getTagDist(int idx); // clear the status/ranging data void instClearCounts(void) ; //------------------------------------------------------------------------------------------------------------- // // Functions used in driving/controlling the ranging application // //------------------------------------------------------------------------------------------------------------- void instClose(void); // Call init, then call config, then call run. call close when finished // initialise the instance (application) structures and DW1000 device int instance_init(void); // configure the instance and DW1000 device void instance_config(const instanceConfig_t *config, const sfConfig_t *sfconfig); void instUpdateFramePeroid(uint16_t period); // configure the MAC address void instSetAddress(uint16_t address) ; void instSetPanID(uint16_t id); void instProcessRXtimeout(instance_data_t *inst); int appState_TA_INIT(instance_data_t *inst, uint32_t *status, int message); int appState_TA_TX_WAIT_CONF(instance_data_t *inst, uint32_t *status, int message); int appState_TA_RXE_WAIT(instance_data_t *inst, uint32_t *status, int message); int appState_TA_RX_WAIT_DATA(instance_data_t *inst, uint32_t *status, int message); // called (periodically or from and interrupt) to process any outstanding TX/RX events and to drive the ranging application int instance_run(void) ; // returns indication of status report change int testAppRun(instance_data_t *inst, int message); // calls the DW1000 interrupt handler #define instance_process_irq(x) dwt_isr() //call device interrupt handler // configure TX/RX callback functions that are called from DW1000 ISR void instRXcallback(const dwt_callback_data_t *rxd); void instTXcallback(const dwt_callback_data_t *txd); // sets the Tag sleep delay time (the time Tag "sleeps" between each ranging attempt) void instSetTagSleepDelay(int rangingsleep); void instSetReplyDelay(int delayms); // get the DW1000 device ID (e.g. 0xDECA0130 for DW1000) uint32_t instReadDeviceID(void) ; // Return Device ID reg, enables validation of physical device presence double instGetIdist(int idx); double instGetIdistRaw(int idx); uint16_t instGetIdistAddress(int idx); int instGetIdist_mm(int idx); int instGetIdistRaw_mm(int idx); uint8_t instValidRanges(void); void instClearDistTableAll(); void instBack2Anchor(instance_data_t *inst); int instSendDlyPacket(instance_data_t *inst, int delayedTx); int instGetRNum(void); uint32_t instGetTagRangeNum(uint32_t address); int instGetLCount(void); int instNewRangeAncAdd(void); int instNewRangeTagAdd(void); int instWRangePollTim(void); int instNewRange(void); uint32_t instNewRangeTim(void); uint64_t convertMicroSec2DeviceTime (double microSec); double convertDeviceTime2Sec(int32_t dt); #define DWT_PRF_64M_RFDLY (514.462f) #define DWT_PRF_16M_RFDLY (513.9067f) extern const uint16_t rfDelays[2]; extern const uint16_t rfDelaysTREK[2]; extern const tx_struct txSpectrumConfig[8]; extern const tx_struct txManualSpectrumConfig[8]; extern const tx_struct maxSpectrumConfig[8]; extern volatile instance_data_t instance_data[NUM_INST] ; extern volatile instance_data_t* gInst; int instPeekEvent(void); void instSaveEvent(event_data_t newEvent, uint8_t eType); event_data_t instGetSavedEvent(void); void instPutEvent(event_data_t newEvent, uint8_t eType); event_data_t* instGetEvent(int x); void instClearEvents(void); // configure TX power void instConfigTXPower(uint32_t txpower); void instSetTXPower(void); // configure the antenna delays void instConfigAntennaDelays(uint16_t tx, uint16_t rx); void instSetAntennaDelays(void); uint16_t instTXAntennaDly(void); uint16_t instRXAntennaDly(void); int instStartTXtest(int framePeriod); uint32_t GetTofResult(uint32_t address); void SetTofResult(uint32_t addr, uint32_t value); void ClearTagTofResult(uint32_t address); uint32_t GetTagTofResult(uint32_t address); void SetTagTofResult(uint32_t addr, uint32_t value); void ClearTofResult(uint32_t address); void SaveTagCommand(uint8_t* buf, uint32_t len); int GetTagCommand(uint16_t sourceAddress, stTagControlCommand* cmd); int GetTagStatus(uint32_t address, stTagStatus* status); void SaveTagStatus(uint16_t address, uint8_t* messageData); int instRangeSlotUpdate(); #ifdef __cplusplus } #endif #endif