1
1
use core:: convert:: Infallible ;
2
2
3
- use embedded_hal:: spi:: { ErrorType , Operation , SpiBus , SpiDevice } ;
3
+ use embedded_hal:: {
4
+ delay:: DelayNs ,
5
+ spi:: { ErrorType , Operation , SpiBus , SpiDevice } ,
6
+ } ;
4
7
use riscv:: interrupt;
5
8
6
9
use super :: { PinCS , Pins , PinsNoCS , SharedBus , SpiConfig , SpiX } ;
7
10
8
11
/// SPI shared device abstraction
9
- pub struct SpiSharedDevice < ' bus , SPI , PINS , CS > {
12
+ pub struct SpiSharedDevice < ' bus , SPI , PINS , CS , D > {
10
13
bus : & ' bus SharedBus < SPI , PINS > ,
11
14
cs : CS ,
12
15
config : SpiConfig ,
16
+ delay : D ,
13
17
}
14
18
15
- impl < ' bus , SPI , PINS , CS > SpiSharedDevice < ' bus , SPI , PINS , CS >
19
+ impl < ' bus , SPI , PINS , CS , D > SpiSharedDevice < ' bus , SPI , PINS , CS , D >
16
20
where
17
21
SPI : SpiX ,
18
22
PINS : PinsNoCS < SPI > ,
19
23
CS : PinCS < SPI > ,
24
+ D : DelayNs ,
20
25
{
21
26
/// Create shared [SpiSharedDevice] using the existing [SharedBus]
22
27
/// and given [SpiConfig]. The config gets cloned.
23
- pub fn new ( bus : & ' bus SharedBus < SPI , PINS > , cs : CS , config : & SpiConfig ) -> Self
28
+ pub fn new ( bus : & ' bus SharedBus < SPI , PINS > , cs : CS , config : & SpiConfig , delay : D ) -> Self
24
29
where
25
30
PINS : PinsNoCS < SPI > ,
26
31
{
27
32
Self {
28
33
bus,
29
34
cs,
30
35
config : config. clone ( ) ,
36
+ delay,
31
37
}
32
38
}
33
39
@@ -55,20 +61,22 @@ where
55
61
}
56
62
}
57
63
58
- impl < SPI , PINS , CS > ErrorType for SpiSharedDevice < ' _ , SPI , PINS , CS >
64
+ impl < SPI , PINS , CS , D > ErrorType for SpiSharedDevice < ' _ , SPI , PINS , CS , D >
59
65
where
60
66
SPI : SpiX ,
61
67
PINS : Pins < SPI > ,
62
68
CS : PinCS < SPI > ,
69
+ D : DelayNs ,
63
70
{
64
71
type Error = Infallible ;
65
72
}
66
73
67
- impl < SPI , PINS , CS > SpiDevice for SpiSharedDevice < ' _ , SPI , PINS , CS >
74
+ impl < SPI , PINS , CS , D > SpiDevice for SpiSharedDevice < ' _ , SPI , PINS , CS , D >
68
75
where
69
76
SPI : SpiX ,
70
77
PINS : Pins < SPI > ,
71
78
CS : PinCS < SPI > ,
79
+ D : DelayNs ,
72
80
{
73
81
fn transaction ( & mut self , operations : & mut [ Operation < ' _ , u8 > ] ) -> Result < ( ) , Infallible > {
74
82
interrupt:: free ( || {
@@ -79,12 +87,12 @@ where
79
87
bus. start_frame ( ) ;
80
88
for operation in operations {
81
89
match operation {
82
- Operation :: Read ( words) => bus. read ( words) ,
83
- Operation :: Write ( words) => bus. write ( words) ,
84
- Operation :: Transfer ( read, write) => bus. transfer ( read, write) ,
85
- Operation :: TransferInPlace ( words) => bus. transfer_in_place ( words) ,
86
- Operation :: DelayNs ( _ns ) => Ok ( ( ) ) , // TODO: NOOP?
87
- } ? ;
90
+ Operation :: Read ( words) => bus. read ( words) ? ,
91
+ Operation :: Write ( words) => bus. write ( words) ? ,
92
+ Operation :: Transfer ( read, write) => bus. transfer ( read, write) ? ,
93
+ Operation :: TransferInPlace ( words) => bus. transfer_in_place ( words) ? ,
94
+ Operation :: DelayNs ( ns ) => self . delay . delay_ns ( * ns ) ,
95
+ } ;
88
96
}
89
97
bus. end_frame ( ) ;
90
98
0 commit comments