First draft for mouse sensor
This commit is contained in:
parent
b5c607fa3a
commit
c356b98801
3 changed files with 107 additions and 0 deletions
|
@ -14,3 +14,5 @@ defmt = "0.3.2"
|
||||||
usb-device = "0.2.9"
|
usb-device = "0.2.9"
|
||||||
usbd-hid = "0.6.1"
|
usbd-hid = "0.6.1"
|
||||||
critical-section = "1.1.1"
|
critical-section = "1.1.1"
|
||||||
|
embedded-hal = "0.2.7"
|
||||||
|
fugit = "0.3.6"
|
||||||
|
|
17
src/main.rs
17
src/main.rs
|
@ -1,6 +1,8 @@
|
||||||
#![no_std]
|
#![no_std]
|
||||||
#![no_main]
|
#![no_main]
|
||||||
|
|
||||||
|
mod mouse_sensor;
|
||||||
|
|
||||||
// Ensure we halt the program on panic (if we don't mention this crate it won't
|
// Ensure we halt the program on panic (if we don't mention this crate it won't
|
||||||
// be linked)
|
// be linked)
|
||||||
use panic_halt as _;
|
use panic_halt as _;
|
||||||
|
@ -26,6 +28,8 @@ use usbd_hid::{
|
||||||
hid_class::HIDClass,
|
hid_class::HIDClass,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
use crate::mouse_sensor::MouseSensor;
|
||||||
|
|
||||||
/// The USB Device Driver (shared with the interrupt).
|
/// The USB Device Driver (shared with the interrupt).
|
||||||
static mut USB_DEVICE: Option<UsbDevice<UsbBus>> = None;
|
static mut USB_DEVICE: Option<UsbDevice<UsbBus>> = None;
|
||||||
|
|
||||||
|
@ -57,6 +61,19 @@ fn main() -> ! {
|
||||||
.ok()
|
.ok()
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
|
// The single-cycle I/O block controls our GPIO pins
|
||||||
|
let sio = hal::Sio::new(pac.SIO);
|
||||||
|
|
||||||
|
// Set the pins up according to their function on this particular board
|
||||||
|
let pins = rp_pico::Pins::new(
|
||||||
|
pac.IO_BANK0,
|
||||||
|
pac.PADS_BANK0,
|
||||||
|
sio.gpio_bank0,
|
||||||
|
&mut pac.RESETS,
|
||||||
|
);
|
||||||
|
|
||||||
|
let mouse_sensor = MouseSensor::new(pins, &mut pac.RESETS, pac.SPI0, &clocks);
|
||||||
|
|
||||||
unsafe {
|
unsafe {
|
||||||
USB_BUS = Some(UsbBusAllocator::new(UsbBus::new(
|
USB_BUS = Some(UsbBusAllocator::new(UsbBus::new(
|
||||||
pac.USBCTRL_REGS,
|
pac.USBCTRL_REGS,
|
||||||
|
|
88
src/mouse_sensor.rs
Normal file
88
src/mouse_sensor.rs
Normal file
|
@ -0,0 +1,88 @@
|
||||||
|
use cortex_m::prelude::_embedded_hal_spi_FullDuplex;
|
||||||
|
use embedded_hal::digital::v2::OutputPin;
|
||||||
|
use fugit::HertzU32;
|
||||||
|
use rp_pico::{
|
||||||
|
hal::{
|
||||||
|
clocks::ClocksManager,
|
||||||
|
gpio::{
|
||||||
|
self,
|
||||||
|
bank0::{Gpio2, Gpio3, Gpio4, Gpio5},
|
||||||
|
Function, Output, Pin, PushPull, Spi,
|
||||||
|
},
|
||||||
|
spi::{self, Enabled, Spi as SpiDevice},
|
||||||
|
Clock,
|
||||||
|
},
|
||||||
|
pac::{RESETS, SPI0},
|
||||||
|
Pins,
|
||||||
|
};
|
||||||
|
|
||||||
|
pub struct MouseSensor {
|
||||||
|
_sclk: Pin<Gpio2, Function<Spi>>,
|
||||||
|
_mosi: Pin<Gpio3, Function<Spi>>,
|
||||||
|
_miso: Pin<Gpio4, Function<Spi>>,
|
||||||
|
cs: Pin<Gpio5, Output<PushPull>>,
|
||||||
|
|
||||||
|
spi: SpiDevice<Enabled, SPI0, 8>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl MouseSensor {
|
||||||
|
const READ: u8 = 0x7F;
|
||||||
|
const WRITE: u8 = 0x80;
|
||||||
|
|
||||||
|
pub fn new(pins: Pins, resets: &mut RESETS, spio: SPI0, clocks: &ClocksManager) -> Self {
|
||||||
|
// These are implicitly used by the spi driver if they are in the correct mode
|
||||||
|
let _sclk = pins.gpio2.into_mode::<gpio::FunctionSpi>();
|
||||||
|
let _mosi = pins.gpio3.into_mode::<gpio::FunctionSpi>();
|
||||||
|
let _miso = pins.gpio4.into_mode::<gpio::FunctionSpi>();
|
||||||
|
let mut cs = pins.gpio5.into_push_pull_output();
|
||||||
|
|
||||||
|
// Create an SPI driver instance for the SPI0 device
|
||||||
|
let spi = spi::Spi::<_, _, 8>::new(spio);
|
||||||
|
|
||||||
|
// Exchange the uninitialised SPI driver for an initialised one
|
||||||
|
let spi = spi.init(
|
||||||
|
resets,
|
||||||
|
clocks.peripheral_clock.freq(),
|
||||||
|
HertzU32::from_raw(16_000_000u32),
|
||||||
|
&embedded_hal::spi::MODE_0,
|
||||||
|
);
|
||||||
|
|
||||||
|
cs.set_high().unwrap();
|
||||||
|
|
||||||
|
Self {
|
||||||
|
_sclk,
|
||||||
|
_mosi,
|
||||||
|
_miso,
|
||||||
|
cs,
|
||||||
|
|
||||||
|
spi,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn access<F, T>(&mut self, f: F) -> T
|
||||||
|
where
|
||||||
|
F: Fn(&mut Self) -> T,
|
||||||
|
{
|
||||||
|
self.cs.set_low().unwrap();
|
||||||
|
|
||||||
|
let res = f(self);
|
||||||
|
|
||||||
|
self.cs.set_high().unwrap();
|
||||||
|
|
||||||
|
res
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn read(&mut self, address: u8) -> u8 {
|
||||||
|
self.access(|me| {
|
||||||
|
me.spi.send(address & Self::READ).unwrap();
|
||||||
|
me.spi.read().unwrap()
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn write(&mut self, address: u8, value: u8) {
|
||||||
|
self.access(|me| {
|
||||||
|
me.spi.send(address | Self::WRITE).unwrap();
|
||||||
|
me.spi.send(value).unwrap();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue