nrf-rs / nrf-hal

A Rust HAL for the nRF family of devices
Apache License 2.0
503 stars 139 forks source link

change done in nrf-usbd not synced in nrf-hal #342

Closed cnnblike closed 3 years ago

cnnblike commented 3 years ago

Hi, per investigation on my side, the master branch of nrf-hal repo should be tracking down the crates.io release. I think there might be some problem in these changes: https://github.com/nrf-rs/nrf-hal/commit/3ea49b7e5bfd1abbd67fa7c662a44ccf4db6713c https://github.com/nrf-rs/nrf-hal/commit/844b7823acefe46618c4521cfcaf1037014b585a

when tracking down the latest change from nrf-hal by "nrf52833-hal = {git = "https://github.com/nrf-rs/nrf-hal", branch = "master", features = ["rt"]}", I find that the lifetime hold of clock has been removed from nrf-usbd by https://github.com/nrf-rs/nrf-usbd/commit/4208283bb23d2def354cd03e4fb865c88759acd9, while the nrf-common-hal is still holding the ownership to clock.

this cause an issue, making the clock reference dropped before 'static usb_device leave. Cause this nrf-hal library not be able to use usb in rtic mode due to "clock dropped while its reference are asked to be static", reference code: https://gist.github.com/cnnblike/c6a486e19e516d932b0110f4accb39fd#file-main-rs-L39

cnnblike commented 3 years ago

or it could might be just my unfamiliar with the rust's lifetime management, the initial problem is like below:

error[E0597]: `clocks` does not live long enough
  --> src/main.rs:59:69
   |
7  | #[rtic::app(device = nrf52833_hal::pac, peripherals = true)]
   |                                                            - `clocks` dropped here while still borrowed
...
59 |         let usb_bus = Usbd::new(UsbPeripheral::new(ctx.device.USBD, &clocks));
   |                                 ------------------------------------^^^^^^^-
   |                                 |                                   |
   |                                 |                                   borrowed value does not live long enough
   |                                 argument requires that `clocks` is borrowed for `'static`

and

error[E0597]: `usb_bus` does not live long enough
  --> src/main.rs:60:38
   |
7  | #[rtic::app(device = nrf52833_hal::pac, peripherals = true)]
   |                                                            - `usb_bus` dropped here while still borrowed
...
60 |         let serial = SerialPort::new(&usb_bus);
   |                      ----------------^^^^^^^^-
   |                      |               |
   |                      |               borrowed value does not live long enough
   |                      argument requires that `usb_bus` is borrowed for `'static`
cnnblike commented 3 years ago

example repo: https://github.com/cnnblike/nrf52833dk

quick question: is it possible to use usb with rtic and nrf-hal? or I need to modify the lifetime to something other than 'static?

cnnblike commented 3 years ago

it should be resolve-able by utilizing the unsafe feature, resolving this. for any further reference, check https://github.com/cnnblike/nrf52833dk