9
9
module Clash.Cores.SPI
10
10
( SPIMode (.. )
11
11
-- * SPI master
12
+ , SpiMasterIn (.. )
13
+ , SpiMasterOut (.. )
12
14
, spiMaster
13
15
-- * SPI slave
16
+ , SpiSlaveIn (.. )
17
+ , SpiSlaveOut (.. )
14
18
, SPISlaveConfig (.. )
15
19
, spiSlave
16
20
-- ** Vendor configured SPI slaves
@@ -27,6 +31,44 @@ import Clash.Sized.Internal.BitVector
27
31
import Clash.Cores.LatticeSemi.ICE40.IO
28
32
import Clash.Cores.LatticeSemi.ECP5.IO
29
33
34
+ -- | Input signals of an SPI subordinate.
35
+ data SpiSlaveIn (ds :: BiSignalDefault ) (dom :: Domain ) (misoLanes :: Nat ) (mosiLanes :: Nat ) =
36
+ SpiSlaveIn {
37
+ spiSlaveMosi :: " MOSI" ::: Signal dom (BitVector mosiLanes ),
38
+ -- ^ Master-out, slave-in
39
+ spiSlaveMisoIn :: " MISO" ::: BiSignalIn ds dom misoLanes ,
40
+ -- ^ Master-in, slave-out
41
+ spiSlaveSck :: " SCK" ::: Signal dom Bit ,
42
+ -- ^ Serial Clock
43
+ spiSlaveSs :: " SS" ::: Signal dom Bit
44
+ -- ^ Slave select
45
+ }
46
+
47
+ -- | Output signals of an SPI subordinate.
48
+ data SpiSlaveOut (ds :: BiSignalDefault ) (dom :: Domain ) (misoLanes :: Nat ) (mosiLanes :: Nat ) =
49
+ SpiSlaveOut {
50
+ spiSlaveMisoOut :: " MISO" ::: BiSignalOut ds dom misoLanes
51
+ -- ^ Master-in, slave-out
52
+ }
53
+
54
+ -- | Output signals of an SPI master.
55
+ data SpiMasterOut (dom :: Domain ) (misoLanes :: Nat ) (mosiLanes :: Nat ) =
56
+ SpiMasterOut {
57
+ spiMasterMosi :: " MOSI" ::: Signal dom (BitVector mosiLanes ),
58
+ -- ^ Master-out, slave-in
59
+ spiMasterSck :: " SCK" ::: Signal dom Bit ,
60
+ -- ^ Serial Clock
61
+ spiMasterSs :: " SS" ::: Signal dom Bit
62
+ -- ^ Slave select
63
+ }
64
+
65
+ -- | Input signals of an SPI master.
66
+ data SpiMasterIn (dom :: Domain ) (misoLanes :: Nat ) (mosiLanes :: Nat ) =
67
+ SpiMasterIn {
68
+ spiMasterMisoOut :: " MISO" ::: Signal dom (BitVector misoLanes )
69
+ -- ^ Master-in, slave-out
70
+ }
71
+
30
72
-- | SPI mode
31
73
--
32
74
-- * CPOL: Clock POLarity
@@ -184,35 +226,29 @@ spiSlave
184
226
. (HiddenClockResetEnable dom , KnownNat n , 1 <= n )
185
227
=> SPISlaveConfig ds dom
186
228
-- ^ Configure SPI mode and tri-state buffer
187
- -> Signal dom Bool
188
- -- ^ Serial Clock (SCLK)
189
- -> Signal dom Bit
190
- -- ^ Master Output Slave Input (MOSI)
191
- -> BiSignalIn ds dom 1
192
- -- ^ Master Input Slave Output (MISO)
193
- --
194
- -- Inout port connected to the tri-state buffer for the MISO
195
- -> Signal dom Bool
196
- -- ^ Slave select (SS)
229
+ -> SpiSlaveIn ds dom 1 1
230
+ -- ^ SPI interface
197
231
-> Signal dom (BitVector n )
198
- -- ^ Data to send from master to slave
232
+ -- ^ Data to send from slave to master.
199
233
--
200
234
-- Input is latched the moment slave select goes low
201
- -> ( BiSignalOut ds dom 1
235
+ -> ( SpiSlaveOut ds dom 1 1
202
236
, Signal dom Bool
203
237
, Signal dom (Maybe (BitVector n )))
204
238
-- ^ Parts of the tuple:
205
239
--
206
240
-- 1. The "out" part of the inout port of the MISO; used only for simulation.
207
241
--
208
- -- 2. (Maybe) the word send by the master
209
- spiSlave (SPISlaveConfig mode latch buf) sclk mosi bin ss din =
242
+ -- 2. the acknowledgement for the data sent from the master to the slave.
243
+ --
244
+ -- 2. (Maybe) the word sent by the master
245
+ spiSlave (SPISlaveConfig mode latch buf) (SpiSlaveIn mosi bin sclk ss) din =
210
246
let ssL = if latch then delay undefined ss else ss
211
247
mosiL = if latch then delay undefined mosi else mosi
212
248
sclkL = if latch then delay undefined sclk else sclk
213
- (miso, ack, dout) = spiCommon mode ssL mosiL sclkL din
214
- bout = buf bin (not <$> ssL) miso
215
- in (bout, ack, dout)
249
+ (miso, ack, dout) = spiCommon mode (bitToBool <$> ssL) ( head . bv2v <$> mosiL) (bitToBool <$> sclkL) din
250
+ bout = buf bin (not . bitToBool <$> ssL) miso
251
+ in (SpiSlaveOut bout, ack, dout)
216
252
217
253
-- | SPI master configurable in the SPI mode and clock divider
218
254
--
@@ -237,11 +273,8 @@ spiMaster
237
273
-> Signal dom (Maybe (BitVector n ))
238
274
-- ^ Data to send from master to slave, transmission starts when receiving
239
275
-- /Just/ a value
240
- -> Signal dom Bit
241
- -- ^ Master Input Slave Output (MISO)
242
- -> ( Signal dom Bool -- SCK
243
- , Signal dom Bit -- MOSI
244
- , Signal dom Bool -- SS
276
+ -> SpiMasterIn dom 1 1
277
+ -> ( SpiMasterOut dom 1 1
245
278
, Signal dom Bool -- Busy
246
279
, Signal dom Bool -- Acknowledge
247
280
, Signal dom (Maybe (BitVector n )) -- Data: Slave -> Master
@@ -254,15 +287,15 @@ spiMaster
254
287
-- 4. Busy signal indicating that a transmission is in progress, new words on
255
288
-- the data line will be ignored when /True/
256
289
-- 5. (Maybe) the word send from the slave to the master
257
- spiMaster mode fN fW din miso =
258
- let (mosi, ack, dout) = spiCommon mode ssL misoL sclkL
290
+ spiMaster mode fN fW din ( SpiMasterIn miso) =
291
+ let (mosi, ack, dout) = spiCommon mode ssL ( head . bv2v <$> misoL) sclkL
259
292
(fromMaybe undefined # <$> din)
260
293
latch = snatToInteger fN /= 1
261
294
ssL = if latch then delay undefined ss else ss
262
295
misoL = if latch then delay undefined miso else miso
263
296
sclkL = if latch then delay undefined sclk else sclk
264
297
(ss, sclk, busy) = spiGen mode fN fW din
265
- in (sclk, mosi, ss , busy, ack, dout)
298
+ in (SpiMasterOut (v2bv . singleton <$> mosi) (boolToBit <$> sclk) (boolToBit <$> ss) , busy, ack, dout)
266
299
267
300
-- | Generate slave select and SCK
268
301
spiGen
@@ -339,21 +372,12 @@ spiSlaveLatticeSBIO
339
372
-- Clock: 2*SCK < Core
340
373
--
341
374
-- * Set to /False/ when core clock is twice as fast, or as fast, as the SCK
342
- -> Signal dom Bool
343
- -- ^ Serial Clock (SCLK)
344
- -> Signal dom Bit
345
- -- ^ Master Output Slave Input (MOSI)
346
- -> BiSignalIn 'Floating dom 1
347
- -- ^ Master Input Slave Output (MISO)
348
- --
349
- -- Inout port connected to the tri-state buffer for the MISO
350
- -> Signal dom Bool
351
- -- ^ Slave select (SS)
375
+ -> SpiSlaveIn 'Floating dom 1 1
352
376
-> Signal dom (BitVector n )
353
377
-- ^ Data to send from slave to master
354
378
--
355
379
-- Input is latched the moment slave select goes low
356
- -> ( BiSignalOut 'Floating dom 1
380
+ -> ( SpiSlaveOut 'Floating dom 1 1
357
381
, Signal dom Bool
358
382
, Signal dom (Maybe (BitVector n )))
359
383
-- ^ Parts of the tuple:
@@ -385,21 +409,12 @@ spiSlaveLatticeBB
385
409
-- Clock: 2*SCK < Core
386
410
--
387
411
-- * Set to /False/ when core clock is twice as fast, or as fast, as the SCK
388
- -> Signal dom Bool
389
- -- ^ Serial Clock (SCLK)
390
- -> Signal dom Bit
391
- -- ^ Master Output Slave Input (MOSI)
392
- -> BiSignalIn 'Floating dom 1
393
- -- ^ Master Input Slave Output (MISO)
394
- --
395
- -- Inout port connected to the tri-state buffer for the MISO
396
- -> Signal dom Bool
397
- -- ^ Slave select (SS)
412
+ -> SpiSlaveIn 'Floating dom 1 1
398
413
-> Signal dom (BitVector n )
399
414
-- ^ Data to send from slave to master
400
415
--
401
416
-- Input is latched the moment slave select goes low
402
- -> ( BiSignalOut 'Floating dom 1
417
+ -> ( SpiSlaveOut 'Floating dom 1 1
403
418
, Signal dom Bool
404
419
, Signal dom (Maybe (BitVector n )))
405
420
-- ^ Parts of the tuple:
0 commit comments