mirror of
https://github.com/embassy-rs/embassy.git
synced 2024-11-25 00:02:28 +00:00
Add spi, i2c and fix usart
This commit is contained in:
parent
53cb84d3d6
commit
a5f754238b
21
examples/stm32u0/src/bin/i2c.rs
Normal file
21
examples/stm32u0/src/bin/i2c.rs
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
#![no_std]
|
||||||
|
#![no_main]
|
||||||
|
|
||||||
|
use defmt::*;
|
||||||
|
use embassy_executor::Spawner;
|
||||||
|
use embassy_stm32::i2c::I2c;
|
||||||
|
use embassy_stm32::time::Hertz;
|
||||||
|
use {defmt_rtt as _, panic_probe as _};
|
||||||
|
|
||||||
|
const ADDRESS: u8 = 0x5F;
|
||||||
|
const WHOAMI: u8 = 0x0F;
|
||||||
|
|
||||||
|
#[embassy_executor::main]
|
||||||
|
async fn main(_spawner: Spawner) {
|
||||||
|
let p = embassy_stm32::init(Default::default());
|
||||||
|
let mut i2c = I2c::new_blocking(p.I2C2, p.PB10, p.PB11, Hertz(100_000), Default::default());
|
||||||
|
|
||||||
|
let mut data = [0u8; 1];
|
||||||
|
unwrap!(i2c.blocking_write_read(ADDRESS, &[WHOAMI], &mut data));
|
||||||
|
info!("Whoami: {}", data[0]);
|
||||||
|
}
|
30
examples/stm32u0/src/bin/spi.rs
Normal file
30
examples/stm32u0/src/bin/spi.rs
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
#![no_std]
|
||||||
|
#![no_main]
|
||||||
|
|
||||||
|
use defmt::*;
|
||||||
|
use embassy_stm32::gpio::{Level, Output, Speed};
|
||||||
|
use embassy_stm32::spi::{Config, Spi};
|
||||||
|
use embassy_stm32::time::Hertz;
|
||||||
|
use {defmt_rtt as _, panic_probe as _};
|
||||||
|
|
||||||
|
#[cortex_m_rt::entry]
|
||||||
|
fn main() -> ! {
|
||||||
|
info!("Hello World!");
|
||||||
|
|
||||||
|
let p = embassy_stm32::init(Default::default());
|
||||||
|
|
||||||
|
let mut spi_config = Config::default();
|
||||||
|
spi_config.frequency = Hertz(1_000_000);
|
||||||
|
|
||||||
|
let mut spi = Spi::new_blocking(p.SPI3, p.PC10, p.PC12, p.PC11, spi_config);
|
||||||
|
|
||||||
|
let mut cs = Output::new(p.PE0, Level::High, Speed::VeryHigh);
|
||||||
|
|
||||||
|
loop {
|
||||||
|
let mut buf = [0x0Au8; 4];
|
||||||
|
cs.set_low();
|
||||||
|
unwrap!(spi.blocking_transfer_in_place(&mut buf));
|
||||||
|
cs.set_high();
|
||||||
|
info!("xfer {=[u8]:x}", buf);
|
||||||
|
}
|
||||||
|
}
|
@ -2,15 +2,9 @@
|
|||||||
#![no_main]
|
#![no_main]
|
||||||
|
|
||||||
use defmt::*;
|
use defmt::*;
|
||||||
use embassy_stm32::dma::NoDma;
|
|
||||||
use embassy_stm32::usart::{Config, Uart};
|
use embassy_stm32::usart::{Config, Uart};
|
||||||
use embassy_stm32::{bind_interrupts, peripherals, usart};
|
|
||||||
use {defmt_rtt as _, panic_probe as _};
|
use {defmt_rtt as _, panic_probe as _};
|
||||||
|
|
||||||
bind_interrupts!(struct Irqs {
|
|
||||||
USART2_LPUART2 => usart::InterruptHandler<peripherals::USART2>;
|
|
||||||
});
|
|
||||||
|
|
||||||
#[cortex_m_rt::entry]
|
#[cortex_m_rt::entry]
|
||||||
fn main() -> ! {
|
fn main() -> ! {
|
||||||
info!("Hello World!");
|
info!("Hello World!");
|
||||||
@ -18,7 +12,7 @@ fn main() -> ! {
|
|||||||
let p = embassy_stm32::init(Default::default());
|
let p = embassy_stm32::init(Default::default());
|
||||||
|
|
||||||
let config = Config::default();
|
let config = Config::default();
|
||||||
let mut usart = Uart::new(p.USART2, p.PA3, p.PA2, Irqs, NoDma, NoDma, config).unwrap();
|
let mut usart = Uart::new_blocking(p.USART2, p.PA3, p.PA2, config).unwrap();
|
||||||
|
|
||||||
unwrap!(usart.blocking_write(b"Hello Embassy World!\r\n"));
|
unwrap!(usart.blocking_write(b"Hello Embassy World!\r\n"));
|
||||||
info!("wrote Hello, starting echo");
|
info!("wrote Hello, starting echo");
|
||||||
|
Loading…
Reference in New Issue
Block a user