1212#![ no_std]
1313#![ no_main]
1414
15+ use embedded_hal:: spi:: SpiDevice ;
16+ use embedded_hal_bus:: spi:: ExclusiveDevice ;
1517// Ensure we halt the program on panic (if we don't mention this crate it won't
1618// be linked)
1719use panic_halt as _;
@@ -20,7 +22,6 @@ use panic_halt as _;
2022use rp235x_hal as hal;
2123
2224// Some things we need
23- use embedded_hal_0_2:: prelude:: * ;
2425use hal:: clocks:: Clock ;
2526use hal:: fugit:: RateExtU32 ;
2627
@@ -60,6 +61,8 @@ fn main() -> ! {
6061 )
6162 . unwrap ( ) ;
6263
64+ let mut timer = hal:: Timer :: new_timer0 ( pac. TIMER0 , & mut pac. RESETS , & clocks) ;
65+
6366 // The single-cycle I/O block controls our GPIO pins
6467 let sio = hal:: Sio :: new ( pac. SIO ) ;
6568
@@ -78,34 +81,25 @@ fn main() -> ! {
7881 let spi_bus = hal:: spi:: Spi :: < _ , _ , _ , 8 > :: new ( pac. SPI0 , ( spi_mosi, spi_miso, spi_sclk) ) ;
7982
8083 // Exchange the uninitialised SPI driver for an initialised one
81- let mut spi_bus = spi_bus. init (
84+ let spi_bus = spi_bus. init (
8285 & mut pac. RESETS ,
8386 clocks. peripheral_clock . freq ( ) ,
8487 16 . MHz ( ) ,
8588 embedded_hal:: spi:: MODE_0 ,
8689 ) ;
8790
91+ let spi_cs = pins. gpio8 . into_function :: < hal:: gpio:: FunctionSioOutput > ( ) ;
92+ let mut spi = ExclusiveDevice :: new ( spi_bus, spi_cs, & mut timer) . unwrap ( ) ;
93+
8894 // Write out 0, ignore return value
89- if spi_bus . write ( & [ 0 ] ) . is_ok ( ) {
95+ if spi . write ( & [ 0 ] ) . is_ok ( ) {
9096 // SPI write was successful
9197 } ;
9298
93- // write 50, then check the return
94- let send_success = spi_bus. send ( 50 ) ;
95- match send_success {
96- Ok ( _) => {
97- // We succeeded, check the read value
98- if let Ok ( _x) = spi_bus. read ( ) {
99- // We got back `x` in exchange for the 0x50 we sent.
100- } ;
101- }
102- Err ( _) => todo ! ( ) ,
103- }
104-
10599 // Do a read+write at the same time. Data in `buffer` will be replaced with
106100 // the data read from the SPI device.
107101 let mut buffer: [ u8 ; 4 ] = [ 1 , 2 , 3 , 4 ] ;
108- let transfer_success = spi_bus . transfer ( & mut buffer) ;
102+ let transfer_success = spi . transfer_in_place ( & mut buffer) ;
109103 #[ allow( clippy:: single_match) ]
110104 match transfer_success {
111105 Ok ( _) => { } // Handle success
0 commit comments