Speed improvements

This commit is contained in:
Jan Käberich 2020-10-03 21:56:09 +02:00
parent 5e00b2a7f8
commit 6bc6b1d202
18 changed files with 130 additions and 108 deletions

View file

@ -42,10 +42,12 @@ void Communication::Input(const uint8_t *buf, uint16_t len) {
}
} while (handled_len > 0);
}
bool Communication::Send(Protocol::PacketInfo packet) {
#include "Hardware.hpp"
bool Communication::Send(const Protocol::PacketInfo &packet) {
// DEBUG1_HIGH();
uint16_t len = Protocol::EncodePacket(packet, outputBuffer,
sizeof(outputBuffer));
// DEBUG1_LOW();
return usb_transmit(outputBuffer, len);
// if (hUsbDeviceFS.dev_state == USBD_STATE_CONFIGURED) {
// uint16_t len = Protocol::EncodePacket(packet, outputBuffer,

View file

@ -8,11 +8,11 @@
namespace Communication {
using Callback = void(*)(Protocol::PacketInfo);
using Callback = void(*)(const Protocol::PacketInfo&);
void SetCallback(Callback cb);
void Input(const uint8_t *buf, uint16_t len);
bool Send(Protocol::PacketInfo packet);
bool Send(const Protocol::PacketInfo &packet);
bool SendWithoutPayload(Protocol::PacketType type);
}

View file

@ -152,18 +152,26 @@ static Protocol::Datapoint DecodeDatapoint(uint8_t *buf) {
}
static int16_t EncodeDatapoint(Protocol::Datapoint d, uint8_t *buf,
uint16_t bufSize) {
Encoder e(buf, bufSize);
e.add<float>(d.real_S11);
e.add<float>(d.imag_S11);
e.add<float>(d.real_S21);
e.add<float>(d.imag_S21);
e.add<float>(d.real_S12);
e.add<float>(d.imag_S12);
e.add<float>(d.real_S22);
e.add<float>(d.imag_S22);
e.add<uint64_t>(d.frequency);
e.add<uint16_t>(d.pointNum);
return e.getSize();
// Special case, bypassing the encoder for speed optimizations.
// The datapoint is only ever encoded on the device and the
// Protocol::Datapoint struct is setup without any padding between
// the variables. In this case it is allowed to simply copy its
// content into the buffer. Compared to using the encoder, this
// saves approximately 40us for each datapoint
memcpy(buf, &d, sizeof(d));
return sizeof(d);
// Encoder e(buf, bufSize);
// e.add<float>(d.real_S11);
// e.add<float>(d.imag_S11);
// e.add<float>(d.real_S21);
// e.add<float>(d.imag_S21);
// e.add<float>(d.real_S12);
// e.add<float>(d.imag_S12);
// e.add<float>(d.real_S22);
// e.add<float>(d.imag_S22);
// e.add<uint64_t>(d.frequency);
// e.add<uint16_t>(d.pointNum);
// return e.getSize();
}
static Protocol::SweepSettings DecodeSweepSettings(uint8_t *buf) {
@ -484,15 +492,25 @@ uint16_t Protocol::DecodeBuffer(uint8_t *buf, uint16_t len, PacketInfo *info) {
return data - buf;
}
/* The complete frame has been received, check checksum */
uint32_t crc = *(uint32_t*) &data[length - 4];
uint32_t compare = CRC32(0, data, length - 4);
if(crc != compare) {
// CRC mismatch, remove header
data += 1;
info->type = PacketType::None;
return data - buf;
}
// /* The complete frame has been received, check checksum */
// auto type = (PacketType) data[3];
// uint32_t crc = *(uint32_t*) &data[length - 4];
// if(type != PacketType::Datapoint) {
// uint32_t compare = CRC32(0, data, length - 4);
// if(crc != compare) {
// // CRC mismatch, remove header
// data += 1;
// info->type = PacketType::None;
// return data - buf;
// }
// } else {
// // Datapoint has the CRC set to zero
// if(crc != 0x00000000) {
// data += 1;
// info->type = PacketType::None;
// return data - buf;
// }
// }
// Valid packet, extract packet info
info->type = (PacketType) data[3];
@ -544,12 +562,12 @@ uint16_t Protocol::DecodeBuffer(uint8_t *buf, uint16_t len, PacketInfo *info) {
return data - buf + length;
}
uint16_t Protocol::EncodePacket(PacketInfo packet, uint8_t *dest, uint16_t destsize) {
int16_t payload_size = 0;
uint16_t Protocol::EncodePacket(const PacketInfo &packet, uint8_t *dest, uint16_t destsize) {
int16_t payload_size = 0;
switch (packet.type) {
case PacketType::Datapoint:
payload_size = EncodeDatapoint(packet.datapoint, &dest[4], destsize - 8);
break;
break;
case PacketType::SweepSettings:
payload_size = EncodeSweepSettings(packet.settings, &dest[4], destsize - 8);
break;
@ -600,7 +618,14 @@ uint16_t Protocol::EncodePacket(PacketInfo packet, uint8_t *dest, uint16_t dests
memcpy(&dest[1], &overall_size, 2);
dest[3] = (int) packet.type;
// Calculate checksum
uint32_t crc = CRC32(0, dest, overall_size - 4);
uint32_t crc = 0x00000000;
if(packet.type == PacketType::Datapoint) {
// CRC calculation takes about 18us which is the bulk of the time required to encode and transmit a datapoint.
// Skip CRC for data points to optimize throughput
crc = 0x00000000;
} else {
crc = CRC32(0, dest, overall_size - 4);
}
memcpy(&dest[overall_size - 4], &crc, 4);
return overall_size;
}

View file

@ -173,6 +173,6 @@ using PacketInfo = struct _packetinfo {
uint32_t CRC32(uint32_t crc, const void *data, uint32_t len);
uint16_t DecodeBuffer(uint8_t *buf, uint16_t len, PacketInfo *info);
uint16_t EncodePacket(PacketInfo packet, uint8_t *dest, uint16_t destsize);
uint16_t EncodePacket(const PacketInfo &packet, uint8_t *dest, uint16_t destsize);
}