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

Utilising the atmega2560's "USART in SPI Mode" #561

Open
CoolSlimbo opened this issue Jun 22, 2024 · 10 comments · May be fixed by #562 or #588
Open

Utilising the atmega2560's "USART in SPI Mode" #561

CoolSlimbo opened this issue Jun 22, 2024 · 10 comments · May be fixed by #562 or #588
Labels
hal-generic Related to MCU generic parts of avr-hal help wanted Extra attention is needed

Comments

@CoolSlimbo
Copy link

CoolSlimbo commented Jun 22, 2024

According the the atmega2560 datasheet, its USART modules can be used in "SPI Mode," which essentially gives an extra bus (or four) to add SPI devices to it.
However, within the current implementation of avr_hal/atmega_hal, it's not plausible to use the USART modules in SPI mode.

Given this is hardware supported on the atmega's (versions of it), it would make sense to have a hardware implementation of it it in the HAL's.

Free to give this an attempt if given a suggestion in the right way for how to get started.


Related Issue:

Linked PR: #562

@Rahix
Copy link
Owner

Rahix commented Jun 23, 2024

Yes, this isn't supported yet.

If anyone is interested in working on a "USART as SPI" driver and needs guidance on how to do this, feel free to ping me!

@Rahix Rahix added help wanted Extra attention is needed hal-generic Related to MCU generic parts of avr-hal labels Jun 23, 2024
@CoolSlimbo
Copy link
Author

I'll gladly give this a shot in implementation, if you could aid a hand in the direction it needs to go in!

Currently, my confusion is just on how one would implement it, whilst also preventing usage of the USART for USART purposes.

@Rahix
Copy link
Owner

Rahix commented Jun 23, 2024

whilst also preventing usage of the USART for USART purposes.

This part is actually very simple. The UsartSpi driver constructor consumes the USART peripheral, just like the current UART driver consumes it. Due to Rust's ownership rules, this means only one of the two can ever exist:

    let dp = atmega_hal::Peripherals::take().unwrap();
    let pins = atmega_hal::pins!(dp);
    let mut serial = Usart::new(
        dp.USART0, // <- USART0 is moved into `serial` here
        pins.pe0,
        pins.pe1.into_output(),
        Baudrate::<crate::CoreClock>::new(57600),
    );

    let mut spi = UsartSpi::new(dp.USART0, ...);  // <- compiler error because USART0 was moved previously and is no longer available

@Rahix
Copy link
Owner

Rahix commented Jun 23, 2024

if you could aid a hand in the direction it needs to go in!

Generally, you'll need to add a usart_spi module in avr-hal-generic, similar in structure to the existing spi or usart modules. This defines the generic UsartSpi driver and a macro for the MCU-specific HALs. This macro is then instanciated in atmega-hal to implement USART as SPI for the available USART peripherals.

@CoolSlimbo
Copy link
Author

Roger on that. I do see that now, I was over-engineering that way to much...

I'll get to work on implementing the driver, however, would this preferred to be in the USART or SPI module?

@Rahix
Copy link
Owner

Rahix commented Jun 23, 2024

I'd create a completely new module for USART as SPI. usart_spi I guess.

@CoolSlimbo CoolSlimbo linked a pull request Jun 23, 2024 that will close this issue
@Rahix Rahix linked a pull request Jun 23, 2024 that will close this issue
@armandas
Copy link
Contributor

armandas commented Jul 24, 2024

@CoolSlimbo Thanks for taking this one! I was missing this feature and pondered working on it, but my Rust skill is still lacking for it...

I'll be happy to give it a test!

@CoolSlimbo
Copy link
Author

@armandas If you could, that would be splendid.

I had completely forgot about this due to school, and my lack of multi USART devices, so testing it out and closing this up for me (us?) would be great.

@armandas
Copy link
Contributor

@CoolSlimbo I just posted my results on the PR. There is a small compilation issue, so I think if you resolve it, the PR may be good ready for review.

@CoolSlimbo
Copy link
Author

Thank you.

As long as it runs the CI (which it should now), it'll be able to finally be merged!

@armandas armandas linked a pull request Oct 5, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
hal-generic Related to MCU generic parts of avr-hal help wanted Extra attention is needed
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants