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

UsartSpi Support #588

Open
wants to merge 27 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
6c8737f
Inital implimentation. Adds in on USART0 only.
CoolSlimbo Jun 23, 2024
47217be
Implimented atmega328p support. Also untested, because I don't have a…
CoolSlimbo Jun 23, 2024
82bd66e
Reoragnise the `usart_spi` into its own module. + Docs
CoolSlimbo Jun 23, 2024
6a0f531
Rejig the docs.
CoolSlimbo Jun 23, 2024
f4052df
Atmega1280 and 2560 UsartSpi Implimentations
CoolSlimbo Jun 23, 2024
a129b83
Add 48p Normal Usart, idk what it was missing
CoolSlimbo Jun 23, 2024
d540e01
Added some more.
CoolSlimbo Jun 23, 2024
9101e4f
Fixed issue regarding CI.
CoolSlimbo Jun 23, 2024
cc0623c
Mark bit writes as unsafe.
CoolSlimbo Jun 23, 2024
053d840
Optimise my laziness (remove the inital implimentation, only macros f…
CoolSlimbo Jun 24, 2024
3495f87
Finalise the remaining USART's (turns out I was right at the end). In…
CoolSlimbo Jun 24, 2024
54ca978
Added some docs to make it less confusing.
CoolSlimbo Jun 27, 2024
028c617
I haven't tested this because my atmega2560 is currently soldered and…
CoolSlimbo Jun 27, 2024
3bf65ea
Adjust example to properly run.
CoolSlimbo Jul 24, 2024
0733da7
Update avr-hal-generic/src/usart_spi.rs w/ armandas clock rates
CoolSlimbo Jul 26, 2024
e5e95e1
Add dummy pin (figure out how to dynamic it)
CoolSlimbo Jul 26, 2024
d758625
Seriously... how do I do dynamic tho?
CoolSlimbo Jul 26, 2024
06a6c3d
Fix CS issue.
armandas Oct 4, 2024
c8dc190
Add a separate constructor for USART based SPI.
armandas Oct 4, 2024
6e1da6a
Fix examples.
armandas Oct 4, 2024
9ddd8f7
Fix clippy warnings.
armandas Oct 4, 2024
4d75862
Fix typos
armandas Oct 4, 2024
e1c3b9d
Fix doc
armandas Oct 4, 2024
e0c2f1a
Update comments to address feedback in #562
armandas Oct 5, 2024
d3f276b
Fix build error in for atmega1284p.
armandas Oct 10, 2024
4461c07
Add example, as per #591
armandas Oct 12, 2024
269eac1
Update example link.
armandas Oct 14, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Inital implimentation. Adds in on USART0 only.
Needs to be tested. Will test via Arudino UNO when I move them ig
CoolSlimbo authored and Rahix committed Jan 6, 2025

Verified

This commit was signed with the committer’s verified signature.
broonie Mark Brown
commit 6c8737f4d1b885af4a8a93c24d4b4e618a843a2d
1 change: 1 addition & 0 deletions avr-hal-generic/src/lib.rs
Original file line number Diff line number Diff line change
@@ -20,6 +20,7 @@ pub mod port;
pub mod simple_pwm;
pub mod spi;
pub mod usart;
pub mod usart_spi;
pub mod wdt;

/// Prelude containing all HAL traits
92 changes: 92 additions & 0 deletions avr-hal-generic/src/usart_spi.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
//! MSPIM Implimentation
use crate::spi;
use core::marker::PhantomData;

// This module just impliments a macro for SpiOps, since underlyingly, the Spi type can still be used since it just needs SpiOps

pub type UsartSpi<H, USART, SCLKPIN, MOSIPIN, MISOPIN, CSPIN> =
spi::Spi<H, USART, SCLKPIN, MOSIPIN, MISOPIN, CSPIN>;

// Impliment SpiOps trait for USART
#[macro_export]
macro_rules! impl_usart_spi {
(
hal: $HAL:ty,
peripheral: $USART_SPI:ty,
register_suffix: $n:expr,
sclk: $sclkpin:ty,
mosi: $mosipin:ty,
miso: $misopin:ty,
cs: $cspin:ty,
) => {
$crate::paste::paste! {
impl $crate::spi::SpiOps<$HAL, $sclkpin, $mosipin, $misopin, $cspin> for $USART_SPI {
fn raw_setup(&mut self, settings: &Settings) {
use $crate::hal::spi;

// Setup control registers
// We start by setting the UBBRn to 0
self.[<ubrr $n>].write(|w| w.bits(0));

// We have to translate the character size register into the 2 bits which are the MSB/LSB and the phase
// 5 Bit Char = MSB and 1st
// 6 Bit Char = MSB and 2nd
// 7 Bit Char = LSB and 1st
// 8 Bit Char = LSB and 2nd
self.[<ucsr $n c>].write(|w| {
w.[<umsel $n>]().spi_master();

match settings.data_order {
DataOrder::MostSignificantFirst => match settings.mode.phase {
spi::Phase::CaptureOnFirstTransition => w.[<ucsz $n>]().chr5(),
spi::Phase::CaptureOnSecondTransition => w.[<ucsz $n>]().chr6(),
},
DataOrder::LeastSignificantFirst => match settings.mode.phase {
spi::Phase::CaptureOnFirstTransition => w.[<ucsz $n>]().chr7(),
spi::Phase::CaptureOnSecondTransition => w.[<ucsz $n>]().chr8(),
},
};

match settings.mode.polarity {
spi::Polarity::IdleLow => w.[<ucpol $n>]().clear_bit(),
spi::Polarity::IdleHigh => w.[<ucpol $n>]().set_bit(),
}
});

// Enable receiver and transmitter, and also the rec interrupt.
self.[<ucsr $n b>].write(|w| w
.[<txen $n>]().set_bit()
.[<rxen $n>]().set_bit()
.[<rxcie $n>]().set_bit()
);

// Set the baudrate of the UBRRn, idk what it should be set to, so for now, it'll be set to 0
self.[<ubrr $n>].write(|w| w.bits(0));
}

fn raw_release(&mut self) {
// Probably a better way to "release" the SPI interface, but from the datasheet, this is what they suggest, so ig it works
self.[<ucsr $n c>].write(|w| w.[<umsel $n>]().usart_async());
}

fn raw_check_iflag(&self) -> bool {
self.[<ucsr $n a>].read().[<rxc $n>]().bit_is_set()
}

fn raw_read(&self) -> u8 {
self.[<udr $n>].read().bits()
}

fn raw_write(&mut self, byte: u8) {
self.[<udr $n>].write(|w| unsafe { w.bits(byte) });
}

fn raw_transaction(&mut self, byte: u8) -> u8 {
self.raw_write(byte);
while !self.raw_check_iflag() {}
self.raw_read()
}
}
}
};
}
30 changes: 30 additions & 0 deletions mcu/atmega-hal/src/spi.rs
Original file line number Diff line number Diff line change
@@ -61,6 +61,36 @@ avr_hal_generic::impl_spi! {
cs: port::PB0,
}

#[cfg(any(
feature = "atmega128a",
feature = "atmega1280",
feature = "atmega2560",
feature = "atmega32u4"
))]
pub type Usart0Spi = avr_hal_generic::usart_spi::UsartSpi<
crate::Atmega,
crate::pac::USART0,
port::PE2,
port::PE1,
port::PE0,
port::Dynamic,
>;
#[cfg(any(
feature = "atmega128a",
feature = "atmega1280",
feature = "atmega2560",
feature = "atmega32u4"
))]
avr_hal_generic::impl_usart_spi! {
hal: crate::Atmega,
peripheral: crate::pac::USART0,
register_suffix: 0,
sclk: port::PE2,
mosi: port::PE1,
miso: port::PE0,
cs: port::Dynamic,
}

#[cfg(any(
feature = "atmega168",
feature = "atmega328p",