-
Notifications
You must be signed in to change notification settings - Fork 231
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
I haven't tested this because my atmega2560 is currently soldered and…
… doesn't have the right pins. Will fix that soon. If someone else can test would be great.
- Loading branch information
1 parent
597b073
commit 54990d0
Showing
1 changed file
with
58 additions
and
0 deletions.
There are no files selected for viewing
58 changes: 58 additions & 0 deletions
58
examples/atmega2560/src/bin/atmega2560-usart_spi-feedback.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
//! This example demonstrates how to set up a SPI interface and communicate | ||
//! over it. The physical hardware configuration consists of connecting a | ||
//! jumper directly from pin `PB2` to pin `PB3`. | ||
//! | ||
//! Run the program using `cargo run`. | ||
//! You should see it output the line `data: 42` repeatedly. | ||
//! If the output you see is `data: 255`, you may need to check your jumper. | ||
#![no_std] | ||
#![no_main] | ||
|
||
use atmega_hal::delay::Delay; | ||
use atmega_hal::usart::{Baudrate, Usart}; | ||
use atmega_hal::usart_spi; | ||
use embedded_hal::delay::DelayNs; | ||
use embedded_hal::spi::SpiBus; | ||
use panic_halt as _; | ||
|
||
// Define core clock. This can be used in the rest of the project. | ||
type CoreClock = atmega_hal::clock::MHz16; | ||
|
||
#[avr_device::entry] | ||
fn main() -> ! { | ||
let dp = atmega_hal::Peripherals::take().unwrap(); | ||
let pins = atmega_hal::pins!(dp); | ||
|
||
let mut delay = Delay::<crate::CoreClock>::new(); | ||
|
||
// set up serial interface for text output | ||
let mut serial = Usart::new( | ||
dp.USART0, | ||
pins.pe0, | ||
pins.pe1.into_output(), | ||
Baudrate::<crate::CoreClock>::new(57600), | ||
); | ||
|
||
// Create SPI interface. | ||
let (mut spi, _) = usart_spi::Usart1Spi::new( | ||
dp.SPI, | ||
pins.pd5.into_output(), | ||
pins.pd3.into_output(), | ||
pins.pd2.into_pull_up_input(), | ||
pins.pd4.into_output().downgrade(), | ||
spi::Settings::default(), | ||
); | ||
|
||
loop { | ||
// Send a byte | ||
let data_out: [u8; 1] = [42]; | ||
let mut data_in: [u8; 1] = [0]; | ||
// Send a byte | ||
// Because MISO is connected to MOSI, the read data should be the same | ||
spi.transfer(&mut data_in, &data_out).unwrap(); | ||
|
||
ufmt::uwriteln!(&mut serial, "data: {}\r", data_in[0]).unwrap(); | ||
delay.delay_ms(1000); | ||
} | ||
} |