Skip to content
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

Looking for Comments: Adopting core::cell for dynamic borrowing in RTIC #2

Open
wants to merge 6 commits into
base: master
Choose a base branch
from

Conversation

HarryMakes
Copy link
Collaborator

(Note: the following write-up has not been edited or cleaned up.)

Summary

A total of 3 aspects should be discussed:

  1. Re-factoring SpiEth and smoltcp_phy::SmoltcpDevice for dynamic borrowing
  2. Applying RTIC to the current examples, which could benefit from dynamic borrowing
  3. Deciding the system timer: current SysTick timer, or monotonic timer based on RTIC's CYCCNT

Major modifications

When I was trying to implement this driver in an RTIC-based project (e.g. sinara-hw's Booster), I encountered the issue where the SPI controller SpiEth object was needed to be borrowed twice, because this SpiEth object and the smoltcp interface smoltcp::iface::EthernetInterface object were instantiated as "late resources" in the RTIC model. SpiEth was borrowed when instantiating EthernetInterface because my original implementation for the smoltcp::phy::Device trait is a struct that contains a mutable reference to the SpiEth object.

From what I've observed, when smoltcp is used in RTIC, the interface is more often related or bound to a register block in STM32 like ETHERNET_DMA. However, since the Tx and Rx buffers are accessible by a serial interface instead of DMA in our case, a custom class (SpiEth) binds to the interface instead, and done by the means of mutable reference instead of moving. This prevents me from instantiating the interface in [#init].

To avoid the restrictive borrowing rule at Rust compile-time, I decided to re-factor the smoltcp_phy::SmoltcpDevice struct such that it now contains a core::cell::RefCell mutable container wrapping the target SpiEth. This removes the need to extend SmoltcpDevice's lifetime to the SpiEth object. I also cleaned up my code to avoid using trait objects. The code now uses generics and trait bounds for static dispatch - although using RefCell to borrow means dynamic borrowing.

Minor modifications

When I was developing the RTIC examples, I also became aware of the CYCCNT monotonic timer provided by RTIC, whose mechanism is about "adding" clock cycles to a 32-bit clock value. By experimenting with the code, this method would bring indefinite delays, as demonstrated by the time-out feature of the port 1234. For example, the time-out could be delayed by more than 2 seconds:

[0.0s] Listening to port 1234 for echoing, time-out in 10s
[0.0s] Listening to port 4321 for greeting, please connect to the port
[11.389s] Received packet: Ok("asdasdasads\n")
[23.491s] Listening to port 1234 for echoing, time-out in 10s

Meanwhile, some other existing RTIC projects that uses smoltcp seem to ignore the delay and simply use CYCCNT. Therefore, it would be great to know whether or not SysTick is preferred over RTIC's monotonic timer.

@HarryMakes HarryMakes requested a review from a team December 9, 2020 07:27
Copy link

@astro astro left a comment

Choose a reason for hiding this comment

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

This looks like a really good idea to me. It seems to get rid of lifetime hacks.

src/smoltcp_phy.rs Outdated Show resolved Hide resolved
src/smoltcp_phy.rs Outdated Show resolved Hide resolved
src/smoltcp_phy.rs Outdated Show resolved Hide resolved
Cargo.toml Outdated Show resolved Hide resolved
@HarryMakes

This comment has been minimized.

@HarryMakes
Copy link
Collaborator Author

I just force-pushed this branch again to apply styling/spelling/simplification of the code in master (4ba5052) first, which has solved all merge conflicts. This latest push (eeea973) has been tested on STM32-H407, but further testing on other platforms are welcomed.

* lib.rs: fix transmission status checking (line 176)
* lib.rs: make RAW_FRAME_LENGTH_MAX the same for both RX and TX, for the purpose of setting MAMXFL in controller, as well as setting MTU for smoltcp device
* smoltcp_phy.rs: fix missing MTU definition
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants