3
3
//! See the parent module for documentation
4
4
5
5
use crate :: {
6
- gpio:: { self , Alternate } ,
6
+ gpio:: { self , alt :: quadspi as alt , Alternate } ,
7
7
rcc:: { rec, CoreClocks , ResetEnable } ,
8
8
stm32,
9
9
} ;
10
10
11
11
use super :: { Bank , Config , Qspi , SamplingEdge } ;
12
12
13
13
/// Used to indicate that an IO pin is not used by the QSPI interface.
14
- pub struct NoIo { }
14
+ pub use gpio :: NoPin as NoIo ;
15
15
16
16
/// Indicates a set of pins can be used for the QSPI interface on bank 1.
17
- pub trait PinsBank1 { }
18
- pub trait PinIo0Bank1 { }
19
- pub trait PinIo1Bank1 { }
20
- pub trait PinIo2Bank1 { }
21
- pub trait PinIo3Bank1 { }
17
+ pub trait PinsBank1 {
18
+ type AltPins ;
19
+ fn convert ( self ) -> Self :: AltPins ;
20
+ }
22
21
23
22
/// Indicates a set of pins can be used for the QSPI interface on bank 2.
24
- pub trait PinsBank2 { }
25
- pub trait PinSckBank2 { }
26
- pub trait PinIo0Bank2 { }
27
- pub trait PinIo1Bank2 { }
28
- pub trait PinIo2Bank2 { }
29
- pub trait PinIo3Bank2 { }
30
-
31
- pub trait PinSck { }
23
+ pub trait PinsBank2 {
24
+ type AltPins ;
25
+ fn convert ( self ) -> Self :: AltPins ;
26
+ }
32
27
33
28
impl < SCK , IO0 , IO1 , IO2 , IO3 > PinsBank1 for ( SCK , IO0 , IO1 , IO2 , IO3 )
34
29
where
35
- SCK : PinSck ,
36
- IO0 : PinIo0Bank1 ,
37
- IO1 : PinIo1Bank1 ,
38
- IO2 : PinIo2Bank1 ,
39
- IO3 : PinIo3Bank1 ,
30
+ SCK : Into < alt :: Clk > ,
31
+ IO0 : Into < alt :: Bk1Io0 > ,
32
+ IO1 : Into < alt :: Bk1Io1 > ,
33
+ IO2 : Into < alt :: Bk1Io2 > ,
34
+ IO3 : Into < alt :: Bk1Io3 > ,
40
35
{
36
+ type AltPins =
37
+ ( alt:: Clk , alt:: Bk1Io0 , alt:: Bk1Io1 , alt:: Bk1Io2 , alt:: Bk1Io3 ) ;
38
+ fn convert ( self ) -> Self :: AltPins {
39
+ (
40
+ self . 0 . into ( ) ,
41
+ self . 1 . into ( ) ,
42
+ self . 2 . into ( ) ,
43
+ self . 3 . into ( ) ,
44
+ self . 4 . into ( ) ,
45
+ )
46
+ }
41
47
}
42
48
43
49
impl < SCK , IO0 , IO1 , IO2 , IO3 > PinsBank2 for ( SCK , IO0 , IO1 , IO2 , IO3 )
44
50
where
45
- SCK : PinSck ,
46
- IO0 : PinIo0Bank2 ,
47
- IO1 : PinIo1Bank2 ,
48
- IO2 : PinIo2Bank2 ,
49
- IO3 : PinIo3Bank2 ,
51
+ SCK : Into < alt :: Clk > ,
52
+ IO0 : Into < alt :: Bk2Io0 > ,
53
+ IO1 : Into < alt :: Bk2Io1 > ,
54
+ IO2 : Into < alt :: Bk2Io2 > ,
55
+ IO3 : Into < alt :: Bk2Io3 > ,
50
56
{
57
+ type AltPins =
58
+ ( alt:: Clk , alt:: Bk2Io0 , alt:: Bk2Io1 , alt:: Bk2Io2 , alt:: Bk2Io3 ) ;
59
+ fn convert ( self ) -> Self :: AltPins {
60
+ (
61
+ self . 0 . into ( ) ,
62
+ self . 1 . into ( ) ,
63
+ self . 2 . into ( ) ,
64
+ self . 3 . into ( ) ,
65
+ self . 4 . into ( ) ,
66
+ )
67
+ }
51
68
}
52
69
53
- macro_rules! pins {
54
- ( Bank1 : [ IO0 : [ $( $IO0: ty) ,* ] IO1 : [ $( $IO1: ty) ,* ] IO2 : [ $( $IO2: ty) ,* ] IO3 : [ $( $IO3: ty) ,* ] ] ) => {
55
- $(
56
- impl PinIo0Bank1 for $IO0 { }
57
- ) *
58
- $(
59
- impl PinIo1Bank1 for $IO1 { }
60
- ) *
61
- $(
62
- impl PinIo2Bank1 for $IO2 { }
63
- ) *
64
- $(
65
- impl PinIo3Bank1 for $IO3 { }
66
- ) *
67
- } ;
68
-
69
- ( Bank2 : [ IO0 : [ $( $IO0: ty) ,* ] IO1 : [ $( $IO1: ty) ,* ] IO2 : [ $( $IO2: ty) ,* ] IO3 : [ $( $IO3: ty) ,* ] ] ) => {
70
- $(
71
- impl PinIo0Bank2 for $IO0 { }
72
- ) *
73
- $(
74
- impl PinIo1Bank2 for $IO1 { }
75
- ) *
76
- $(
77
- impl PinIo2Bank2 for $IO2 { }
78
- ) *
79
- $(
80
- impl PinIo3Bank2 for $IO3 { }
81
- ) *
82
- } ;
83
-
84
- ( SCK : [ $( $SCK: ty) ,* ] , Bank1 : $bank1: tt, Bank2 : $bank2: tt) => {
85
- $(
86
- impl PinSck for $SCK { }
87
- ) *
88
- pins!( Bank1 : $bank1) ;
89
- pins!( Bank2 : $bank2) ;
90
- } ;
91
- }
92
-
93
- pins ! {
94
- SCK : [
95
- gpio:: PB2 <Alternate <9 >>,
96
- gpio:: PF10 <Alternate <9 >>
97
- ] ,
98
- Bank1 : [
99
- IO0 : [
100
- gpio:: PC9 <Alternate <9 >>,
101
- gpio:: PD11 <Alternate <9 >>,
102
- gpio:: PF8 <Alternate <10 >>
103
- ]
104
- IO1 : [
105
- gpio:: PC10 <Alternate <9 >>,
106
- gpio:: PD12 <Alternate <9 >>,
107
- gpio:: PF9 <Alternate <10 >>,
108
- NoIo
109
- ]
110
- IO2 : [
111
- gpio:: PE2 <Alternate <9 >>,
112
- gpio:: PF7 <Alternate <9 >>,
113
- NoIo
114
- ]
115
- IO3 : [
116
- gpio:: PA1 <Alternate <9 >>,
117
- gpio:: PD13 <Alternate <9 >>,
118
- gpio:: PF6 <Alternate <9 >>,
119
- NoIo
120
- ]
121
- ] ,
122
- Bank2 : [
123
- IO0 : [
124
- gpio:: PE7 <Alternate <10 >>,
125
- gpio:: PF8 <Alternate <10 >>,
126
- gpio:: PH2 <Alternate <9 >>
127
- ]
128
- IO1 : [
129
- gpio:: PE8 <Alternate <10 >>,
130
- gpio:: PF9 <Alternate <10 >>,
131
- gpio:: PH3 <Alternate <9 >>,
132
- NoIo
133
- ]
134
- IO2 : [
135
- gpio:: PE9 <Alternate <10 >>,
136
- gpio:: PG9 <Alternate <9 >>,
137
- NoIo
138
- ]
139
- IO3 : [
140
- gpio:: PE10 <Alternate <10 >>,
141
- gpio:: PG14 <Alternate <9 >>,
142
- NoIo
143
- ]
144
- ]
145
- }
146
-
147
- pub trait QspiExt {
148
- fn bank1 < CONFIG , PINS > (
70
+ pub trait QspiExt : Sized {
71
+ fn bank1 (
149
72
self ,
150
- _pins : PINS ,
151
- config : CONFIG ,
73
+ pins : impl PinsBank1 ,
74
+ config : impl Into < Config > ,
152
75
clocks : & CoreClocks ,
153
76
prec : rec:: Qspi ,
154
- ) -> Qspi < stm32:: QUADSPI >
155
- where
156
- CONFIG : Into < Config > ,
157
- PINS : PinsBank1 ;
77
+ ) -> Qspi < stm32:: QUADSPI > {
78
+ let _pins = pins . convert ( ) ;
79
+ Self :: qspi_unchecked ( self , config , Bank :: One , clocks , prec )
80
+ }
158
81
159
- fn bank2 < CONFIG , PINS > (
82
+ fn bank2 (
160
83
self ,
161
- _pins : PINS ,
162
- config : CONFIG ,
84
+ pins : impl PinsBank2 ,
85
+ config : impl Into < Config > ,
163
86
clocks : & CoreClocks ,
164
87
prec : rec:: Qspi ,
165
- ) -> Qspi < stm32:: QUADSPI >
166
- where
167
- CONFIG : Into < Config > ,
168
- PINS : PinsBank2 ;
88
+ ) -> Qspi < stm32:: QUADSPI > {
89
+ let _pins = pins . convert ( ) ;
90
+ Self :: qspi_unchecked ( self , config , Bank :: Two , clocks , prec )
91
+ }
169
92
170
- fn qspi_unchecked < CONFIG > (
93
+ fn qspi_unchecked (
171
94
self ,
172
- config : CONFIG ,
95
+ config : impl Into < Config > ,
173
96
bank : Bank ,
174
97
clocks : & CoreClocks ,
175
98
prec : rec:: Qspi ,
176
- ) -> Qspi < stm32:: QUADSPI >
177
- where
178
- CONFIG : Into < Config > ;
99
+ ) -> Qspi < stm32:: QUADSPI > ;
179
100
}
180
101
181
102
impl Qspi < stm32:: QUADSPI > {
182
- pub fn qspi_unchecked < CONFIG > (
103
+ pub fn new_unchecked < CONFIG > (
183
104
regs : stm32:: QUADSPI ,
184
105
config : CONFIG ,
185
106
bank : Bank ,
@@ -206,30 +127,20 @@ impl Qspi<stm32::QUADSPI> {
206
127
207
128
// Clear all pending flags.
208
129
regs. fcr . write ( |w| {
209
- w. ctof ( )
210
- . set_bit ( )
211
- . csmf ( )
212
- . set_bit ( )
213
- . ctcf ( )
214
- . set_bit ( )
215
- . ctef ( )
216
- . set_bit ( )
130
+ w. ctof ( ) . set_bit ( ) ;
131
+ w. csmf ( ) . set_bit ( ) ;
132
+ w. ctcf ( ) . set_bit ( ) ;
133
+ w. ctef ( ) . set_bit ( )
217
134
} ) ;
218
135
219
136
// Configure the communication method for QSPI.
220
137
regs. ccr . write ( |w| unsafe {
221
- w. fmode ( )
222
- . bits ( 0 ) // indirect mode
223
- . dmode ( )
224
- . bits ( config. mode . reg_value ( ) )
225
- . admode ( )
226
- . bits ( config. mode . reg_value ( ) )
227
- . adsize ( )
228
- . bits ( 0 ) // Eight-bit address
229
- . imode ( )
230
- . bits ( 0 ) // No instruction phase
231
- . dcyc ( )
232
- . bits ( config. dummy_cycles )
138
+ w. fmode ( ) . bits ( 0 ) ; // indirect mode
139
+ w. dmode ( ) . bits ( config. mode . reg_value ( ) ) ;
140
+ w. admode ( ) . bits ( config. mode . reg_value ( ) ) ;
141
+ w. adsize ( ) . bits ( 0 ) ; // Eight-bit address
142
+ w. imode ( ) . bits ( 0 ) ; // No instruction phase
143
+ w. dcyc ( ) . bits ( config. dummy_cycles )
233
144
} ) ;
234
145
235
146
let spi_frequency = config. frequency . raw ( ) ;
@@ -250,12 +161,10 @@ impl Qspi<stm32::QUADSPI> {
250
161
//
251
162
// SSHIFT must not be set in DDR mode.
252
163
regs. cr . write ( |w| unsafe {
253
- w. prescaler ( )
254
- . bits ( divisor as u8 )
255
- . sshift ( )
256
- . bit ( config. sampling_edge == SamplingEdge :: Falling )
257
- . fthres ( )
258
- . bits ( config. fifo_threshold - 1 )
164
+ w. prescaler ( ) . bits ( divisor as u8 ) ;
165
+ w. sshift ( )
166
+ . bit ( config. sampling_edge == SamplingEdge :: Falling ) ;
167
+ w. fthres ( ) . bits ( config. fifo_threshold - 1 )
259
168
} ) ;
260
169
261
170
match bank {
@@ -275,44 +184,13 @@ impl Qspi<stm32::QUADSPI> {
275
184
}
276
185
277
186
impl QspiExt for stm32:: QUADSPI {
278
- fn bank1 < CONFIG , PINS > (
187
+ fn qspi_unchecked (
279
188
self ,
280
- _pins : PINS ,
281
- config : CONFIG ,
282
- clocks : & CoreClocks ,
283
- prec : rec:: Qspi ,
284
- ) -> Qspi < stm32:: QUADSPI >
285
- where
286
- CONFIG : Into < Config > ,
287
- PINS : PinsBank1 ,
288
- {
289
- Qspi :: qspi_unchecked ( self , config, Bank :: One , clocks, prec)
290
- }
291
-
292
- fn bank2 < CONFIG , PINS > (
293
- self ,
294
- _pins : PINS ,
295
- config : CONFIG ,
296
- clocks : & CoreClocks ,
297
- prec : rec:: Qspi ,
298
- ) -> Qspi < stm32:: QUADSPI >
299
- where
300
- CONFIG : Into < Config > ,
301
- PINS : PinsBank2 ,
302
- {
303
- Qspi :: qspi_unchecked ( self , config, Bank :: Two , clocks, prec)
304
- }
305
-
306
- fn qspi_unchecked < CONFIG > (
307
- self ,
308
- config : CONFIG ,
189
+ config : impl Into < Config > ,
309
190
bank : Bank ,
310
191
clocks : & CoreClocks ,
311
192
prec : rec:: Qspi ,
312
- ) -> Qspi < stm32:: QUADSPI >
313
- where
314
- CONFIG : Into < Config > ,
315
- {
316
- Qspi :: qspi_unchecked ( self , config, bank, clocks, prec)
193
+ ) -> Qspi < stm32:: QUADSPI > {
194
+ Qspi :: new_unchecked ( self , config, bank, clocks, prec)
317
195
}
318
196
}
0 commit comments