atmega_hal/
spi.rs

1//! SPI
2//!
3//! # Example
4//!
5//! Complete example source code can be found in the repository
6//! [`atmega2560-spi-feedback.rs`](https://github.com/Rahix/avr-hal/blob/main/examples/atmega2560/src/bin/atmega2560-spi-feedback.rs)
7//!
8//! ```
9//! let dp = atmega_hal::Peripherals::take().unwrap();
10//! let pins = atmega_hal::pins!(dp);
11//!
12//! let (mut spi, mut cs) = spi::Spi::new(
13//!     dp.SPI,
14//!     pins.pb1.into_output(),
15//!     pins.pb2.into_output(),
16//!     pins.pb3.into_pull_up_input(),
17//!     pins.pb0.into_output(),
18//!     spi::Settings::default(),
19//! );
20//!
21//! let data_out = b"Hello World!";
22//! let mut data_in = [0u8; 12];
23//!
24//! cs.set_low().unwrap();
25//! spi.transfer(&mut data_in, data_out).unwrap();
26//! cs.set_high().unwrap();
27//!
28//! ufmt::uwriteln!(&mut serial, "data: {:?}", data_in).unwrap();
29//! ```
30
31#[allow(unused_imports)]
32use crate::port;
33pub use avr_hal_generic::spi::*;
34
35#[cfg(any(
36    feature = "atmega128a",
37    feature = "atmega1280",
38    feature = "atmega2560",
39    feature = "atmega32u4"
40))]
41pub type Spi = avr_hal_generic::spi::Spi<
42    crate::Atmega,
43    crate::pac::SPI,
44    port::PB1,
45    port::PB2,
46    port::PB3,
47    port::PB0,
48>;
49#[cfg(any(
50    feature = "atmega128a",
51    feature = "atmega1280",
52    feature = "atmega2560",
53    feature = "atmega32u4"
54))]
55avr_hal_generic::impl_spi! {
56    hal: crate::Atmega,
57    peripheral: crate::pac::SPI,
58    sclk: port::PB1,
59    mosi: port::PB2,
60    miso: port::PB3,
61    cs: port::PB0,
62}
63
64#[cfg(any(
65    feature = "atmega168",
66    feature = "atmega328p",
67    feature = "atmega48p",
68    feature = "atmega8",
69    feature = "atmega88p"
70))]
71pub type Spi = avr_hal_generic::spi::Spi<
72    crate::Atmega,
73    crate::pac::SPI,
74    port::PB5,
75    port::PB3,
76    port::PB4,
77    port::PB2,
78>;
79#[cfg(any(
80    feature = "atmega168",
81    feature = "atmega328p",
82    feature = "atmega48p",
83    feature = "atmega8",
84    feature = "atmega88p"
85))]
86avr_hal_generic::impl_spi! {
87    hal: crate::Atmega,
88    peripheral: crate::pac::SPI,
89    sclk: port::PB5,
90    mosi: port::PB3,
91    miso: port::PB4,
92    cs: port::PB2,
93}
94
95#[cfg(feature = "atmega328pb")]
96pub type Spi0 = avr_hal_generic::spi::Spi<
97    crate::Atmega,
98    crate::pac::SPI0,
99    port::PB5,
100    port::PB3,
101    port::PB4,
102    port::PB2,
103>;
104#[cfg(feature = "atmega328pb")]
105avr_hal_generic::impl_spi! {
106    hal: crate::Atmega,
107    peripheral: crate::pac::SPI0,
108    sclk: port::PB5,
109    mosi: port::PB3,
110    miso: port::PB4,
111    cs: port::PB2,
112}
113#[cfg(feature = "atmega328pb")]
114pub type Spi1 = avr_hal_generic::spi::Spi<
115    crate::Atmega,
116    crate::pac::SPI1,
117    port::PC1,
118    port::PE3,
119    port::PC0,
120    port::PE2,
121>;
122#[cfg(feature = "atmega328pb")]
123avr_hal_generic::impl_spi! {
124    hal: crate::Atmega,
125    peripheral: crate::pac::SPI1,
126    sclk: port::PC1,
127    mosi: port::PE3,
128    miso: port::PC0,
129    cs: port::PE2,
130}
131
132#[cfg(any(feature = "atmega1284p", feature = "atmega32a"))]
133pub type Spi = avr_hal_generic::spi::Spi<
134    crate::Atmega,
135    crate::pac::SPI,
136    port::PB7,
137    port::PB5,
138    port::PB6,
139    port::PB4,
140>;
141#[cfg(any(feature = "atmega1284p", feature = "atmega32a"))]
142avr_hal_generic::impl_spi! {
143    hal: crate::Atmega,
144    peripheral: crate::pac::SPI,
145    sclk: port::PB7,
146    mosi: port::PB5,
147    miso: port::PB6,
148    cs: port::PB4,
149}