Add basic examples
This commit is contained in:
parent
88793567c2
commit
a49a04e06f
|
@ -0,0 +1,39 @@
|
||||||
|
//! Serial interface echo server example
|
||||||
|
//!
|
||||||
|
//! In this example every received byte will be sent back to the sender. You can test this
|
||||||
|
//! example with serial terminal emulator like `minicom`.
|
||||||
|
|
||||||
|
#![deny(unsafe_code)]
|
||||||
|
#![deny(warnings)]
|
||||||
|
#![no_main]
|
||||||
|
#![no_std]
|
||||||
|
|
||||||
|
extern crate panic_semihosting;
|
||||||
|
|
||||||
|
use cortex_m_rt::entry;
|
||||||
|
//use cortex_m_semihosting::hprintln;
|
||||||
|
use hal::block;
|
||||||
|
use hal::prelude::*;
|
||||||
|
use hal::serial::{config::Config as SerialConfig, Serial};
|
||||||
|
|
||||||
|
#[entry]
|
||||||
|
fn main() -> ! {
|
||||||
|
let device = hal::stm32::Peripherals::take().unwrap();
|
||||||
|
|
||||||
|
let rcc = device.RCC.constrain();
|
||||||
|
let clocks = rcc.cfgr.sysclk(48.mhz()).freeze();
|
||||||
|
|
||||||
|
let gpioa = device.GPIOA.split();
|
||||||
|
let tx = gpioa.pa2.into_alternate_af7();
|
||||||
|
let rx = gpioa.pa3.into_alternate_af7();
|
||||||
|
let config = SerialConfig::default().baudrate(9_600.bps());
|
||||||
|
|
||||||
|
let serial = Serial::usart2(device.USART2, (tx, rx), config, clocks).unwrap();
|
||||||
|
let (mut tx, mut rx) = serial.split();
|
||||||
|
|
||||||
|
loop {
|
||||||
|
let byte = block!(rx.read()).unwrap();
|
||||||
|
//hprintln!("in: {}", byte).unwrap();
|
||||||
|
block!(tx.write(byte)).ok();
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,56 @@
|
||||||
|
//! Accelerometer via SPI example
|
||||||
|
//!
|
||||||
|
//! This example reads all axes from the accelerometer and outputs it via semihosting
|
||||||
|
//! debug output.
|
||||||
|
|
||||||
|
#![deny(unsafe_code)]
|
||||||
|
#![deny(warnings)]
|
||||||
|
#![no_main]
|
||||||
|
#![no_std]
|
||||||
|
|
||||||
|
extern crate panic_semihosting;
|
||||||
|
|
||||||
|
use cortex_m_rt::entry;
|
||||||
|
use cortex_m_semihosting::hprintln;
|
||||||
|
//use hal::block;
|
||||||
|
use hal::prelude::*;
|
||||||
|
use hal::spi::{Mode, Phase, Polarity, Spi};
|
||||||
|
|
||||||
|
#[entry]
|
||||||
|
fn main() -> ! {
|
||||||
|
let device = hal::stm32::Peripherals::take().unwrap();
|
||||||
|
|
||||||
|
let rcc = device.RCC.constrain();
|
||||||
|
let clocks = rcc.cfgr.freeze();
|
||||||
|
|
||||||
|
let gpioa = device.GPIOA.split();
|
||||||
|
let sck = gpioa.pa5.into_alternate_af5();
|
||||||
|
let miso = gpioa.pa6.into_alternate_af5();
|
||||||
|
let mosi = gpioa.pa7.into_alternate_af5();
|
||||||
|
let mode = Mode {
|
||||||
|
polarity: Polarity::IdleHigh,
|
||||||
|
phase: Phase::CaptureOnSecondTransition,
|
||||||
|
};
|
||||||
|
let mut spi = Spi::spi1(device.SPI1, (sck, miso, mosi), mode, 100.hz(), clocks);
|
||||||
|
|
||||||
|
let gpioe = device.GPIOE.split();
|
||||||
|
let mut cs = gpioe.pe3.into_push_pull_output();
|
||||||
|
|
||||||
|
// Init
|
||||||
|
cs.set_low();
|
||||||
|
let mut commands = [0x20, 0b01000111];
|
||||||
|
let _ = spi.transfer(&mut commands[..]).unwrap();
|
||||||
|
cs.set_high();
|
||||||
|
|
||||||
|
loop {
|
||||||
|
// Read
|
||||||
|
cs.set_low();
|
||||||
|
let mut commands = [(1 << 7) | (1 << 6) | 0x29, 0x0, 0x0, 0x0, 0x0, 0x0];
|
||||||
|
let result = spi.transfer(&mut commands[..]).unwrap();
|
||||||
|
let acc_x = result[1] as i8;
|
||||||
|
let acc_y = result[3] as i8;
|
||||||
|
let acc_z = result[5] as i8;
|
||||||
|
cs.set_high();
|
||||||
|
hprintln!("{}, {}, {}", acc_x, acc_y, acc_z).unwrap();
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue