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

hal::sercom::uart::Config could potentially check for the correct sercom clock #668

Open
tgross35 opened this issue Jan 26, 2023 · 2 comments

Comments

@tgross35
Copy link
Contributor

I had a config like the following:

// types
// clocks: &mut GenericClockController,
// sercom: SERCOM0,
// mclk: &mut pac::MCLK,
// uart_rx: impl Into<UartRx>, // Pin<PA05, AlternateD>
// uart_tx: impl Into<UartTx>,// Pin<PA04, AlternateD>

let gclk0 = clocks.gclk0();
let clock = &clocks.sercom3_core(&gclk0).unwrap();
let baud = 9600.hz();
let pads = uart::Pads::default().rx(uart_rx.into()).tx(uart_tx.into());
uart::Config::new(mclk, sercom, pads, clock.freq())
    .baud(baud, BaudMode::Fractional(Oversampling::Bits16))
    .enable()

A very easy thing to miss is that clock is the wrong type - I used sercom3_core but should have used sercom0_core. The result was that within the call stack enable->self.registers.enable->usart.ctrlb.modify, the processor entered a trap since sercom0 wasn't configured.

It seems like it would be possible to provide a separate method to new that would take a reference to a sercom clock, to configure it properly. Or, maybe it would be good to add a debug_assert within enable to check that the appropriate clock is enabled.

@bradleyharden
Copy link
Contributor

@tgross35, yes, clock safety is coming soon with the merge of #450. I'm currently working to port that code to D21 and D11 chips. Once that is done, we can start using it elsewhere in the HAL, including the sercom module.

@tgross35
Copy link
Contributor Author

Oh, that’s awesome! Good to know

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

No branches or pull requests

2 participants