Skip to content

Improve header definitions #58

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions target/min.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

// Number of bytes needed for a frame with a given payload length, excluding stuff bytes
// 3 header bytes, ID/control byte, length byte, seq byte, 4 byte CRC, EOF byte
#define ON_WIRE_SIZE(p) ((p) + 11U)
#define ON_WIRE_SIZE(p) ((p) + MIN_OVERHEAD)

// Special protocol bytes
enum {
Expand Down Expand Up @@ -504,11 +504,11 @@ static void rx_byte(struct min_context *self, uint8_t byte)
crc32_step(&self->rx_checksum, byte);
if (self->rx_frame_length > 0) {
// Can reduce the RAM size by compiling limits to frame sizes
if (self->rx_frame_length <= MAX_PAYLOAD) {
if (self->rx_frame_length <= MIN_MAX_PAYLOAD) {
self->rx_frame_state = RECEIVING_PAYLOAD;
} else {
// Frame dropped because it's longer than any frame we can buffer
min_debug_print("Dropping frame because length %d > MAX_PAYLOAD %d", self->rx_frame_length, MAX_PAYLOAD);
min_debug_print("Dropping frame because length %d > MIN_MAX_PAYLOAD %d", self->rx_frame_length, MIN_MAX_PAYLOAD);
self->rx_frame_state = SEARCHING_FOR_SOF;
}
} else {
Expand Down
33 changes: 26 additions & 7 deletions target/min.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,31 @@
#define TRANSPORT_PROTOCOL
#endif

#ifndef MAX_PAYLOAD
#define MAX_PAYLOAD (255U)
// Number of bytes needed for a frame with a given payload length, excluding stuff bytes
// 3 header bytes, ID/control byte, length byte, seq byte, 4 byte CRC, EOF byte
#define MIN_OVERHEAD 11

#if defined(MAX_PAYLOAD)
// for backwards compatibility
#define MIN_MAX_PAYLOAD MAX_PAYLOAD
#endif

#ifdef MIN_MAX_PACKET_SIZE
#if (MIN_MAX_PACKET_SIZE < MIN_OVERHEAD)
#error "MIN_MAX_PACKET_SIZE must be greater than MIN_OVERHEAD"
#endif
// Subtract overhead and max potential stuff bytes
#define MIN_MAX_PAYLOAD (2 * (MIN_MAX_PACKET_SIZE - MIN_OVERHEAD + 1) / 3)
#else
#ifndef MIN_MAX_PAYLOAD
#define MIN_MAX_PAYLOAD 255
#endif
// Add overhead and max potential stuff bytes
#define MIN_MAX_PACKET_SIZE (MIN_MAX_PAYLOAD + MIN_OVERHEAD + (MIN_MAX_PAYLOAD / 2))
#endif

#if (MIN_MAX_PAYLOAD > 255)
#error "MIN frame payloads can be no bigger than 255 bytes"
#endif

// Powers of two for FIFO management. Default is 16 frames in the FIFO, total of 1024 bytes for frame data
Expand All @@ -85,10 +108,6 @@
#define TRANSPORT_FIFO_MAX_FRAMES (1U << TRANSPORT_FIFO_SIZE_FRAMES_BITS)
#define TRANSPORT_FIFO_MAX_FRAME_DATA (1U << TRANSPORT_FIFO_SIZE_FRAME_DATA_BITS)

#if (MAX_PAYLOAD > 255)
#error "MIN frame payloads can be no bigger than 255 bytes"
#endif

// Indices into the frames FIFO are uint8_t and so can't have more than 256 frames in a FIFO
#if (TRANSPORT_FIFO_MAX_FRAMES > 256)
#error "Transport FIFO frames cannot exceed 256"
Expand Down Expand Up @@ -143,7 +162,7 @@ struct min_context {
#ifdef TRANSPORT_PROTOCOL
struct transport_fifo transport_fifo; // T-MIN queue of outgoing frames
#endif
uint8_t rx_frame_payload_buf[MAX_PAYLOAD]; // Payload received so far
uint8_t rx_frame_payload_buf[MIN_MAX_PAYLOAD]; // Payload received so far
uint32_t rx_frame_checksum; // Checksum received over the wire
struct crc32_context rx_checksum; // Calculated checksum for receiving frame
struct crc32_context tx_checksum; // Calculated checksum for sending frame
Expand Down