Hello everyone, I have a problem. When I try to receive a message, “if” doesn’t work, because a status of message is incorrect. Here is my code. I will be grateful for any help.
*A variable of the message status is “status_reg”.
#include "deca_device_api.h"
#include "deca_regs.h"
#include "stdio.h"
#include "mem_manager.h"
// Example application name and version to display on LCD screen.
#define APP_NAME "RX DIAG v1.0"
#define SIZE_OF_CPLX_IN_BYTES 4
typedef struct {
int16 real;
int16 imag;
}
cplx_value_t;
// Size of packets parts
#define CONTROL_SIZE 1
#define SEQ_NUM_SIZE 1
#define STATE_SIZE 1
#define FCS_SIZE 2
#define SOURCE_ADDRESS_SIZE 8
#define TX_TIME_SIZE 5
#define CTRL_SIZE (CONTROL_SIZE + SEQ_NUM_SIZE) //2 bytes
#define CRTL_AND_ADDRESS_SIZE (CTRL_SIZE + SOURCE_ADDRESS_SIZE) //10 bytes
// Frame Control Field
#define FCF_COMPATIBLE 0xC5 // For compatible
#define FCF_TX_TS 0xFA // For tx_ts
#define FCF_SHORT 0x5A // For short
// Massage with tx time stamp and tag state
typedef struct
{
uint8 frameCtrl; // frame control 00
uint8 seqNum; // sequence number 01
uint8 tagID[SOURCE_ADDRESS_SIZE]; // Tag ID 02-09 64 bit
uint8 txTime[TX_TIME_SIZE]; // Tx Time Stamp 10-15 40 bit
uint8 rxTime[TX_TIME_SIZE]; // Rx Time Stamp
uint8 state; // State 16
uint8 fcs[FCS_SIZE]; // Frame Check Sequence 17-18 16 bit
} blink_msg_tx_ts_t;
// Total: 18 byte
// Default communication configuration. We use here EVK1000's default mode (mode 3).
static dwt_config_t config = {
2, // Channel number.
DWT_PRF_64M, // Pulse repetition frequency.
DWT_PLEN_1024, // Preamble length. Used in TX only.
DWT_PAC32, // Preamble acquisition chunk size. Used in RX only.
9, // TX preamble code. Used in TX only.
9, // RX preamble code. Used in RX only.
1, // 0 to use standard SFD, 1 to use non-standard SFD.
DWT_BR_850K, // Data rate.
DWT_PHRMODE_STD, // PHY header mode.
(1025 + 64 - 32) // SFD timeout (preamble length + 1 + SFD length - PAC size). Used in RX only.
};
// Buffer to store received frame. See NOTE 1 below.
#define FRAME_LEN_MAX 127
static uint8 rx_buffer[FRAME_LEN_MAX];
// Hold copy of status register state here for reference, so reader can examine it at a breakpoint.
static uint32 status_reg = 0;
// Hold copy of frame length of frame received (if good), so reader can examine it at a breakpoint.
static uint16 frame_len = 0;
// Hold copy of event counters so that it can be examined at a debug breakpoint.
static dwt_deviceentcnts_t event_cnt;
// Hold copy of diagnostics data so that it can be examined at a debug breakpoint.
static dwt_rxdiag_t rx_diag;
blink_msg_tx_ts_t msg_txts;
int main(void)
{
// Start with board specific hardware init.
peripherals_init();
//nrf_mem_init();
// Display application name on LCD.
//lcd_display_str(APP_NAME);
reset_DW1000();
// spi_set_rate_low();
port_set_dw1000_slowrate();
if (dwt_initialise(DWT_LOADUCODE) == DWT_ERROR)
{
// lcd_display_str("INIT FAILED");
while (1)
{ };
}
// spi_set_rate_high();
port_set_dw1000_fastrate();
// Configure DW1000.
dwt_configure(&config);
// Activate event counters.
dwt_configeventcounters(1);
char string[256];
// Loop forever receiving frames.
while (1)
{
int i;
// TESTING BREAKPOINT LOCATION #1
// Clear local RX buffer, rx_diag structure and accumulator values to avoid having leftovers from previous receptions This is not necessary
// but is included here to aid reading the data for each new frame.
// This is a good place to put a breakpoint. Here (after first time through the loop) the local status register will be set for last event
// and if a good receive has happened the data buffer will have the data in it, and frame_len will be set to the length of the RX frame. All
// diagnostics data will also be available.
for (i = 0 ; i < FRAME_LEN_MAX; i++ )
{
rx_buffer[i] = 1;
}
msg_txts.frameCtrl=0;
msg_txts.seqNum=0;
for (i = 0 ; i < SOURCE_ADDRESS_SIZE; i++ )
msg_txts.tagID[SOURCE_ADDRESS_SIZE]=0;
for (i = 0 ; i < TX_TIME_SIZE; i++ )
msg_txts.txTime[TX_TIME_SIZE]=0;
msg_txts.state=0;
for (i = 0 ; i < FCS_SIZE; i++ )
msg_txts.fcs[FCS_SIZE]=0;
// Activate reception immediately. See NOTE 4 below.
dwt_rxenable(DWT_START_RX_IMMEDIATE);
// Poll until a frame is properly received or an error/timeout occurs. See NOTE 5 below.
// STATUS register is 5 bytes long but, as the event we are looking at is in the first byte of the register, we can use this simplest API
// function to access it.
while (!((status_reg = dwt_read32bitreg(SYS_STATUS_ID)) & (SYS_STATUS_RXFCG | SYS_STATUS_ALL_RX_ERR)))
{ };
if (status_reg & SYS_STATUS_RXFCG)
{
// A frame has been received, copy it to our local buffer.
frame_len = dwt_read32bitreg(RX_FINFO_ID) & RX_FINFO_RXFL_MASK_1023;
if (frame_len <= FRAME_LEN_MAX)
{
dwt_readrxdata(rx_buffer, frame_len, 0);
dwt_readrxtimestamp(msg_txts.rxTime);
}
int i;
memset(string, 0, 256);
msg_txts.frameCtrl=rx_buffer[0];
msg_txts.seqNum=rx_buffer[1];
for (i = 2 ; i < 10; i++ )
msg_txts.tagID[i-2]=rx_buffer[i];
for (i = 10 ; i < 15; i++ )
msg_txts.txTime[i-10]=rx_buffer[i];
msg_txts.state=rx_buffer[15];
for (i = 16 ; i < 18; i++ )
msg_txts.fcs[i-16]=rx_buffer[i];
//dwt_readrxtimestamp(msg_txts.rxTime);
sprintf(string, "frameCtrl:%d seqNum:%d tadID:%d %d %d %d %d %d %d %d txTime:%d %d %d %d %d rxTime:%d %d %d %d %d state:%d fcs:%d %d\n",msg_txts.frameCtrl, msg_txts.seqNum, msg_txts.tagID[0], msg_txts.tagID[1], msg_txts.tagID[2], msg_txts.tagID[3], msg_txts.tagID[4], msg_txts.tagID[5], msg_txts.tagID[6], msg_txts.tagID[7], msg_txts.txTime[0], msg_txts.txTime[1], msg_txts.txTime[2], msg_txts.txTime[3], msg_txts.txTime[4], msg_txts.rxTime[0], msg_txts.rxTime[1], msg_txts.rxTime[2], msg_txts.rxTime[3], msg_txts.rxTime[4], msg_txts.state, msg_txts.fcs[0], msg_txts.fcs[1]);
deca_uart_transmit(string);
dwt_write32bitreg(SYS_STATUS_ID, SYS_STATUS_RXFCG);
}
else
{
// Clear RX error events in the DW1000 status register.
dwt_write32bitreg(SYS_STATUS_ID, SYS_STATUS_ALL_RX_ERR);
// Reset RX to properly reinitialise LDE operation.
dwt_rxreset();
}
// Read event counters. See NOTE 7.
dwt_readeventcounters(&event_cnt);
}
//nrf_free(cplx_buf);
}