Skip to content

Ringbuffer smoltcp device implementation#1139

Draft
lmbollen wants to merge 83 commits intomainfrom
lucas/ringbuffer-smoltcp
Draft

Ringbuffer smoltcp device implementation#1139
lmbollen wants to merge 83 commits intomainfrom
lucas/ringbuffer-smoltcp

Conversation

@lmbollen
Copy link
Contributor

@lmbollen lmbollen commented Jan 8, 2026

This pull request adds the implementation of smoltcp's Device trait for a pair of TransmitRingbuffer and ReceiveRingbuffer alongside with a unit test that verifies whether we can connect, send and receive data.

I have to further sanity check if the current implementation for consuming the RxToken and TxToken is alright for now or if we can do better (thanks Claude), but at least we have a unit test that shows that the concept works.

The current device implementation uses the first 32 bits of the packet as identifier to differentiate between new and old packets. Incoming packets can be malformed on a physical level, so the implementation processes each packet twice.

TODOs:

  • Investigate whether we can use Ip as medium instead of Ethernet, this would probably require us to calculate and verify the checksum manually. (this would be an optimization)
  • Reconsider if we need to process each packet multiple times or if it would be better to just do it once
  • Take another look at the logic for consuming RxToken's
  • Take another look at the logic for consuming TxToken's
  • Reconsider if the current API for the ringbuffer / scatter / gather components has the desired form [[u8; 8]; SIZE]

@lmbollen lmbollen force-pushed the lucas/ringbuffer-smoltcp branch from fd5e77d to 4cb10d9 Compare January 9, 2026 11:34
@lmbollen lmbollen mentioned this pull request Jan 15, 2026
3 tasks
Copy link
Collaborator

@rslawson rslawson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mostly just did a linting pass since this is a draft PR. I'll have a proper look at this when it's moved out of draft (:

Comment on lines +294 to +296
loop {
continue;
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Personally I prefer just a loop {} and putting #[allow(clippy::empty_loop)] on the item it's in, but this works just as well.

let hal_rx_temp = ReceiveRingbuffer::new(scatter, 0);
find_alignment_offset(&hal_tx_temp, &hal_rx_temp)
};
writeln!(uart, " Alignment offset: {}", rx_offset).ok();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
writeln!(uart, " Alignment offset: {}", rx_offset).ok();
writeln!(uart, " Alignment offset: {rx_offset}").ok();

let tx_buffer = TransmitRingbuffer::new(gather);
let mut device = RingbufferDevice::new(rx_buffer, tx_buffer);
let mtu = device.mtu();
writeln!(uart, " MTU: {} bytes", mtu).ok();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
writeln!(uart, " MTU: {} bytes", mtu).ok();
writeln!(uart, " MTU: {mtu} bytes").ok();

iface.update_ip_addrs(|addrs| {
addrs.push(ip_addr).unwrap();
});
writeln!(uart, " IP: {}", ip_addr).ok();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
writeln!(uart, " IP: {}", ip_addr).ok();
writeln!(uart, " IP: {ip_addr}").ok();

let client = sockets.get_mut::<tcp::Socket>(client_handle);
if client.can_send() {
if let Ok(sent) = client.send_slice(test_data) {
writeln!(uart, " Sent {} bytes", sent).ok();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
writeln!(uart, " Sent {} bytes", sent).ok();
writeln!(uart, " Sent {sent} bytes").ok();

if server.can_recv() {
if let Ok(len) = server.recv_slice(&mut received_data) {
received_len = len;
writeln!(uart, " Received {} bytes", len).ok();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
writeln!(uart, " Received {} bytes", len).ok();
writeln!(uart, " Received {len} bytes").ok();

Comment on lines +195 to +200
writeln!(
uart,
" Expected {} bytes: {:?}",
test_data.len(),
test_data
)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
writeln!(
uart,
" Expected {} bytes: {:?}",
test_data.len(),
test_data
)
writeln!(
uart,
" Expected {} bytes: {test_data:?}",
test_data.len(),
)

test_data
)
.ok();
writeln!(uart, " Got {} bytes: {:?}", received_len, received_slice).ok();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
writeln!(uart, " Got {} bytes: {:?}", received_len, received_slice).ok();
writeln!(uart, " Got {received_len} bytes: {received_slice:?}").ok();

Comment on lines +106 to +111
writeln!(
uart,
"Step 5: Starting TCP server on port {}...",
SERVER_PORT
)
.ok();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
writeln!(
uart,
"Step 5: Starting TCP server on port {}...",
SERVER_PORT
)
.ok();
writeln!(
uart,
"Step 5: Starting TCP server on port {SERVER_PORT}...",
)
.ok();

Comment on lines +270 to +275
trace!(
"Transmitted packet: checksum {}, seq {}, total length {}",
crc,
self.seq_num,
total_len,
);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
trace!(
"Transmitted packet: checksum {}, seq {}, total length {}",
crc,
self.seq_num,
total_len,
);
trace!(
"Transmitted packet: checksum {crc}, seq {}, total length {total_len}",
self.seq_num,
);

@lmbollen lmbollen force-pushed the lucas/ringbuffer-smoltcp branch 21 times, most recently from 54c66c4 to 84a2cc6 Compare March 9, 2026 16:55
@lmbollen lmbollen force-pushed the lucas/ringbuffer-smoltcp branch 6 times, most recently from fdb988d to 33ba9af Compare March 12, 2026 08:00
@lmbollen lmbollen force-pushed the lucas/ringbuffer-smoltcp branch from 339d8e6 to a912dd6 Compare March 20, 2026 10:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants