Skip to content

Commit

Permalink
Merge pull request #30 from desowin/update-bitstream
Browse files Browse the repository at this point in the history
Update to latest FPGA bitstream
  • Loading branch information
matwey authored Apr 27, 2023
2 parents b472206 + d0192dc commit 04aaeda
Show file tree
Hide file tree
Showing 9 changed files with 64 additions and 58 deletions.
10 changes: 5 additions & 5 deletions include/decoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ struct packet_decoder {
struct decoder_ops ops;
void* user_data;

uint64_t cumulative_ts;
int ts_byte;
int ts_length;
enum packet_decoder_state {
NEED_PACKET_MAGIC,
NEED_PACKET_FLAGS_LO,
NEED_PACKET_FLAGS_HI,
NEED_PACKET_FLAGS,
NEED_PACKET_LENGTH_LO,
NEED_PACKET_LENGTH_HI,
NEED_PACKET_TIMESTAMP_LO,
NEED_PACKET_TIMESTAMP_ME,
NEED_PACKET_TIMESTAMP_HI,
NEED_PACKET_TIMESTAMP,
NEED_PACKET_DATA
} state;

Expand Down
16 changes: 14 additions & 2 deletions include/openvizsla.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ struct
#endif
ov_packet {
uint8_t magic;
uint16_t flags;
uint8_t flags;
uint16_t size;
uint32_t timestamp : 24;
uint64_t timestamp;
uint8_t data[];
};
#ifdef _MSC_VER
Expand All @@ -42,6 +42,18 @@ enum ov_usb_speed {
OV_HIGH_SPEED = 0x48
};

#define OV_FLAGS_HF0_ERR 0x01
#define OV_FLAGS_HF0_OVF 0x02
#define OV_FLAGS_HF0_TRUNC 0x08
#define OV_FLAGS_HF0_FIRST 0x10
#define OV_FLAGS_HF0_LAST 0x20

#define OV_MAX_PACKET_SIZE 1027

static inline uint16_t ov_packet_captured_size(struct ov_packet* p) {
return (p->flags & OV_FLAGS_HF0_TRUNC) ? OV_MAX_PACKET_SIZE : p->size;
}

OPENVIZSLA_EXPORT struct ov_device* ov_new(const char* firmware_filename);
OPENVIZSLA_EXPORT int ov_open(struct ov_device* ov);
OPENVIZSLA_EXPORT void ov_free(struct ov_device* ov);
Expand Down
Binary file modified ov3.fwpkg
Binary file not shown.
4 changes: 1 addition & 3 deletions src/cha.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@
#define ftdi_tcioflush(x) ftdi_usb_purge_buffers(x)
#endif

#define PACKET_FLAGS_HF0_LAST 0x20

struct cha_loop_packet_callback_state {
size_t count;
ov_packet_decoder_callback user_callback;
Expand Down Expand Up @@ -504,7 +502,7 @@ static void cha_loop_packet_callback(void* data, struct ov_packet* packet) {
if (loop->max_count > 0 && loop->count++ >= loop->max_count)
loop->state = COUNT_LIMIT;

if (packet->flags & PACKET_FLAGS_HF0_LAST)
if (packet->flags & OV_FLAGS_HF0_LAST)
loop->state = END_OF_STREAM;
}

Expand Down
44 changes: 24 additions & 20 deletions src/decoder.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ int packet_decoder_init(struct packet_decoder* pd, struct ov_packet* p, size_t s
pd->buf_actual_length = 0;
pd->buf_length = size;
pd->error_str = NULL;
pd->cumulative_ts = 0;
pd->ts_byte = 0;
pd->ts_length = 0;
pd->state = NEED_PACKET_MAGIC;

return 0;
}
Expand All @@ -28,43 +32,43 @@ int packet_decoder_proc(struct packet_decoder* pd, uint8_t* buf, size_t size) {
assert(pd->buf_actual_length == 0);
assert(pd->buf_length > 0);

if (*buf != 0xa0) {
if (*buf == 0xa1) {
/* Discard magic filler */
buf++;
break;
}
if ((*buf != 0xa0) && (*buf != 0xa2)) {
pd->error_str = "Wrong packet magic";
return -1;
}

pd->packet->magic = *(buf++);
pd->state = NEED_PACKET_FLAGS_LO;
pd->state = NEED_PACKET_FLAGS;
} break;
case NEED_PACKET_FLAGS_LO: {
case NEED_PACKET_FLAGS: {
pd->packet->flags = *(buf++);
pd->state = NEED_PACKET_FLAGS_HI;
} break;
case NEED_PACKET_FLAGS_HI: {
pd->packet->flags = (*(buf++) << 8) | pd->packet->flags;
pd->state = NEED_PACKET_LENGTH_LO;
} break;
case NEED_PACKET_LENGTH_LO: {
pd->packet->size = *(buf++);
pd->state = NEED_PACKET_LENGTH_HI;
} break;
case NEED_PACKET_LENGTH_HI: {
pd->packet->size = (*(buf++) << 8) | pd->packet->size;
pd->state = NEED_PACKET_TIMESTAMP_LO;
pd->packet->size = ((*buf & 0x1f) << 8) | pd->packet->size;
pd->ts_length = (((*(buf++)) & 0xe0) >> 5) + 1;
pd->ts_byte = 0;
pd->packet->timestamp = 0;
pd->state = NEED_PACKET_TIMESTAMP;

// FIXME: check available buffer size
} break;
case NEED_PACKET_TIMESTAMP_LO: {
pd->packet->timestamp = *(buf++);
pd->state = NEED_PACKET_TIMESTAMP_ME;
} break;
case NEED_PACKET_TIMESTAMP_ME: {
pd->packet->timestamp |= *(buf++) << 8;
pd->state = NEED_PACKET_TIMESTAMP_HI;
} break;
case NEED_PACKET_TIMESTAMP_HI: {
pd->packet->timestamp = (*(buf++) << 16) | pd->packet->timestamp;
pd->state = NEED_PACKET_DATA;
case NEED_PACKET_TIMESTAMP: {
pd->packet->timestamp |= ((uint64_t)*(buf++)) << (8 * (pd->ts_byte++));
if (pd->ts_byte >= pd->ts_length) {
pd->cumulative_ts += pd->packet->timestamp;
pd->packet->timestamp = pd->cumulative_ts;
pd->state = NEED_PACKET_DATA;
}
} break;
case NEED_PACKET_DATA: {
const size_t required_length = pd->packet->size - pd->buf_actual_length;
Expand Down
20 changes: 10 additions & 10 deletions test/decoder.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,22 +32,22 @@ void frame_teardown() {
}

START_TEST (test_packet_decoder1) {
char inp[] = {0xa0,0,0,0x01,0,0xc4,0xcc,0x96,0x5a};
char inp[] = {0xa0,0,0x01,0xe0,0xc4,0xcc,0x96,0xa0,0xe0,0x00,0x00,0x01,0x5a};
ck_assert_int_eq(packet_decoder_proc(&pd, inp, sizeof(inp)), sizeof(inp));
ck_assert_int_eq(pd.state, NEED_PACKET_MAGIC);
ck_assert_int_eq(p.packet.size, 1);
ck_assert_int_eq(p.packet.timestamp, 0x96ccc4);
ck_assert_int_eq(memcmp(p.packet.data, inp+8, p.packet.size), 0);
ck_assert_int_eq(p.packet.timestamp, 0x010000e0a096ccc4);
ck_assert_int_eq(memcmp(p.packet.data, inp+sizeof(inp)-1, p.packet.size), 0);
}
END_TEST

START_TEST (test_packet_decoder2) {
char inp[] = {0xa0,0,0,0x01,0,0xc4,0xcc,0x96,0x5a,0xa0};
char inp[] = {0xa0,0,0x01,0,0xc4,0x5a,0xa0};
ck_assert_int_eq(packet_decoder_proc(&pd, inp, sizeof(inp)), sizeof(inp)-1);
ck_assert_int_eq(pd.state, NEED_PACKET_MAGIC);
ck_assert_int_eq(p.packet.size, 1);
ck_assert_int_eq(p.packet.timestamp, 0x96ccc4);
ck_assert_int_eq(memcmp(p.packet.data, inp+8, p.packet.size), 0);
ck_assert_int_eq(p.packet.timestamp, 0xc4);
ck_assert_int_eq(memcmp(p.packet.data, inp+sizeof(inp)-2, p.packet.size), 0);
}
END_TEST

Expand All @@ -58,20 +58,20 @@ START_TEST (test_packet_decoder3) {
END_TEST

START_TEST (test_packet_decoder4) {
char inp[] = {0xa0,0,0,0x03,0,0xac,0x6c,0xa5,0x69,0x83,0xe0};
char inp[] = {0xa0,0,0x03,0x40,0xac,0x6c,0xa5,0x69,0x83,0xe0};
ck_assert_int_eq(packet_decoder_proc(&pd, inp, sizeof(inp)), sizeof(inp));
ck_assert_int_eq(pd.state, NEED_PACKET_MAGIC);
ck_assert_int_eq(p.packet.size, 3);
ck_assert_int_eq(p.packet.timestamp, 0xa56cac);
ck_assert_int_eq(memcmp(p.packet.data, inp+8, p.packet.size), 0);
ck_assert_int_eq(memcmp(p.packet.data, inp+sizeof(inp)-3, p.packet.size), 0);
}
END_TEST

START_TEST (test_packet_decoder5) {
char inp1[] = {0xa0,0,0,0x03,0,0xac};
char inp1[] = {0xa0,0,0x03,0x40,0xac};
char inp2[] = {0x6c,0xa5,0x69,0x83,0xe0};
ck_assert_int_eq(packet_decoder_proc(&pd, inp1, sizeof(inp1)), sizeof(inp1));
ck_assert_int_eq(pd.state, NEED_PACKET_TIMESTAMP_ME);
ck_assert_int_eq(pd.state, NEED_PACKET_TIMESTAMP);
ck_assert_int_eq(p.packet.size, 3);
ck_assert_int_eq(packet_decoder_proc(&pd, inp2, sizeof(inp2)), sizeof(inp2));
ck_assert_int_eq(pd.state, NEED_PACKET_MAGIC);
Expand Down
2 changes: 1 addition & 1 deletion test/fwpkg.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ START_TEST (test_fwpkg_size1) {
ret = fwpkg_init_from_file(&fwpkg, PROJECT_ROOT "/ov3.fwpkg");
ck_assert_int_eq(ret, 0);
ck_assert_uint_eq(fwpkg_map_size(&fwpkg), 2080);
ck_assert_uint_eq(fwpkg_bitstream_size(&fwpkg), 340972);
ck_assert_uint_eq(fwpkg_bitstream_size(&fwpkg), 340692);
fwpkg_destroy(&fwpkg);
}
END_TEST
Expand Down
23 changes: 7 additions & 16 deletions tools/ovextcap/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ struct handler_data {
FILE* out; /**< Output file. Set to NULL if capture stopped from Wireshark (broken pipe). */
FILE* debug; /**< Debug output file. NULL if not writing to debug file. */
uint32_t utc_ts; /**< Seconds since epoch */
uint32_t last_ov_ts; /**< Last seen OpenVizsla timestamp. Used to detect overflows */
uint32_t ts_offset; /**< Timestamp offset in 1/OV_TIMESTAMP_FREQ_HZ units */
uint64_t last_ov_ts; /**< Last received packet timestamp */

bool filter_naks; /**< True if NAKs should be filtered */
bool filter_sofs; /**< True if uninteresting SOFs should be filtered */
Expand Down Expand Up @@ -281,28 +281,19 @@ static void packet_handler(struct ov_packet* packet, void* user_data) {
/* Increment timestamp based on the 60 MHz 24-bit counter value.
* Convert remaining clocks to nanoseconds: 1 clk = 1 / 60 MHz = 16.(6) ns
*/
uint64_t clks;
if (packet->timestamp < data->last_ov_ts) {
data->ts_offset += (1 << 24);
}
uint64_t diff_ts;
diff_ts = packet->timestamp - data->last_ov_ts;
data->last_ov_ts = packet->timestamp;
clks = data->ts_offset + packet->timestamp;
if (clks >= OV_TIMESTAMP_FREQ_HZ) {
data->utc_ts += 1;
data->ts_offset -= OV_TIMESTAMP_FREQ_HZ;
clks -= OV_TIMESTAMP_FREQ_HZ;
}
nsec = (clks * 17) - (clks / 3);
data->utc_ts += (data->ts_offset + diff_ts) / OV_TIMESTAMP_FREQ_HZ;
data->ts_offset = (data->ts_offset + diff_ts) % OV_TIMESTAMP_FREQ_HZ;
nsec = (data->ts_offset * 17) - (data->ts_offset / 3);

/* Only write actual USB packets */
if (packet->size > 0) {
struct pcap_packet pkt = {
.ts_sec = data->utc_ts,
.ts_nsec = nsec,
/* TODO: Modify OV firmware to provide info how long was truncated packet
* Currently this code lies that all packets are fully captured...
*/
.incl_len = packet->size,
.incl_len = ov_packet_captured_size(packet),
.orig_len = packet->size,
.captured = packet->data,
};
Expand Down
3 changes: 2 additions & 1 deletion tools/sample/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <inttypes.h>
#include <ftdi.h>
#include <signal.h>

#include <openvizsla.h>

static void packet_handler(struct ov_packet* packet, void* data) {
printf("[%04x] Received %d bytes at %d:", packet->flags, packet->size, packet->timestamp);
printf("[%02x] Received %d bytes at %" PRId64 ":", packet->flags, packet->size, packet->timestamp);
for (int i = 0; i < packet->size; ++i)
printf(" %02x", packet->data[i]);
printf("\n");
Expand Down

0 comments on commit 04aaeda

Please sign in to comment.