Skip to content

Commit 32d4b32

Browse files
committed
Improve header definitions
- Define MIN_OVERHEAD - Rename `MAX_PAYLOAD` to `MIN_MAX_PAYLOAD` to namespace the def - Define `MIN_MAX_PACKET_SIZE` to explicitly provide the maximum packet size that MIN will send. This is useful for declaring buffers when the UART tx implementation uses a buffer (DMA). Signed-off-by: Nick Brook <[email protected]>
1 parent 40315e7 commit 32d4b32

File tree

2 files changed

+27
-10
lines changed

2 files changed

+27
-10
lines changed

target/min.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

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

1414
// Special protocol bytes
1515
enum {
@@ -504,11 +504,11 @@ static void rx_byte(struct min_context *self, uint8_t byte)
504504
crc32_step(&self->rx_checksum, byte);
505505
if (self->rx_frame_length > 0) {
506506
// Can reduce the RAM size by compiling limits to frame sizes
507-
if (self->rx_frame_length <= MAX_PAYLOAD) {
507+
if (self->rx_frame_length <= MIN_MAX_PAYLOAD) {
508508
self->rx_frame_state = RECEIVING_PAYLOAD;
509509
} else {
510510
// Frame dropped because it's longer than any frame we can buffer
511-
min_debug_print("Dropping frame because length %d > MAX_PAYLOAD %d", self->rx_frame_length, MAX_PAYLOAD);
511+
min_debug_print("Dropping frame because length %d > MIN_MAX_PAYLOAD %d", self->rx_frame_length, MIN_MAX_PAYLOAD);
512512
self->rx_frame_state = SEARCHING_FOR_SOF;
513513
}
514514
} else {

target/min.h

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,29 @@
7070
#define TRANSPORT_PROTOCOL
7171
#endif
7272

73-
#ifndef MAX_PAYLOAD
74-
#define MAX_PAYLOAD (255U)
73+
// Number of bytes needed for a frame with a given payload length, excluding stuff bytes
74+
// 3 header bytes, ID/control byte, length byte, seq byte, 4 byte CRC, EOF byte
75+
#define MIN_OVERHEAD 11
76+
77+
#if defined(MAX_PAYLOAD)
78+
// for backwards compatibility
79+
#define MIN_MAX_PAYLOAD MAX_PAYLOAD
80+
#endif
81+
82+
#ifdef MIN_MAX_PAYLOAD
83+
#define MIN_MAX_PACKET_SIZE (MIN_MAX_PAYLOAD + MIN_OVERHEAD)
84+
#elif defined(MIN_MAX_PACKET_SIZE)
85+
#if (MIN_MAX_PACKET_SIZE < MIN_OVERHEAD)
86+
#error "MIN_MAX_PACKET_SIZE must be greater than MIN_OVERHEAD"
87+
#endif
88+
#define MIN_MAX_PAYLOAD (MIN_MAX_PACKET_SIZE - MIN_OVERHEAD)
89+
#else
90+
#define MIN_MAX_PAYLOAD 255
91+
#define MIN_MAX_PACKET_SIZE (MIN_MAX_PAYLOAD + MIN_OVERHEAD)
92+
#endif
93+
94+
#if (MIN_MAX_PAYLOAD > 255)
95+
#error "MIN frame payloads can be no bigger than 255 bytes"
7596
#endif
7697

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

88-
#if (MAX_PAYLOAD > 255)
89-
#error "MIN frame payloads can be no bigger than 255 bytes"
90-
#endif
91-
92109
// Indices into the frames FIFO are uint8_t and so can't have more than 256 frames in a FIFO
93110
#if (TRANSPORT_FIFO_MAX_FRAMES > 256)
94111
#error "Transport FIFO frames cannot exceed 256"
@@ -143,7 +160,7 @@ struct min_context {
143160
#ifdef TRANSPORT_PROTOCOL
144161
struct transport_fifo transport_fifo; // T-MIN queue of outgoing frames
145162
#endif
146-
uint8_t rx_frame_payload_buf[MAX_PAYLOAD]; // Payload received so far
163+
uint8_t rx_frame_payload_buf[MIN_MAX_PAYLOAD]; // Payload received so far
147164
uint32_t rx_frame_checksum; // Checksum received over the wire
148165
struct crc32_context rx_checksum; // Calculated checksum for receiving frame
149166
struct crc32_context tx_checksum; // Calculated checksum for sending frame

0 commit comments

Comments
 (0)