Skip to content

Commit 7d5af18

Browse files
committed
Add comments to i2c core
1 parent 28485f6 commit 7d5af18

File tree

6 files changed

+121
-24
lines changed

6 files changed

+121
-24
lines changed

clash-cores/src/Clash/Cores/I2C.hs

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,29 +21,60 @@ import Clash.Cores.I2C.ByteMaster
2121
, PortName "write"
2222
, PortName "ackIn"
2323
, PortName "din"
24-
, PortName "i2cI"]
24+
, PortProduct "i2c"
25+
[ PortName "sda"
26+
, PortName "sdaEn"]
27+
]
2528
, t_output = PortProduct ""
2629
[ PortName "dout"
2730
, PortName "hostAck"
2831
, PortName "busy"
2932
, PortName "al"
3033
, PortName "ackOut"
31-
, PortProduct "" [PortName "i2cO_clk"]
32-
]
34+
, PortProduct "i2cO"
35+
[ PortName "scl"
36+
, PortName "sclOEn"
37+
, PortName "sda"
38+
, PortName "sdaOEn"
39+
]]
3340
}) #-}
41+
-- | Core for I2C communication
3442
i2c ::
43+
-- | Input Clock
3544
Clock System ->
45+
-- | Low level reset
3646
Reset System ->
47+
-- | Statemachine reset
3748
Signal System Bool ->
49+
-- | BitMaster enable
3850
Signal System Bool ->
51+
-- | Clock divider
3952
Signal System (Unsigned 16) ->
53+
-- | Start signal
4054
Signal System Bool ->
55+
-- | Stop signal
4156
Signal System Bool ->
57+
-- | Read signal
4258
Signal System Bool ->
59+
-- | Write signal
4360
Signal System Bool ->
61+
-- | Ack signal
4462
Signal System Bool ->
63+
-- | Input data
4564
Signal System (BitVector 8) ->
65+
-- | I2C input signals (SCL, SDA)
4666
Signal System (Bit, Bit) ->
67+
-- |
68+
-- 1. Received data
69+
-- 2. Command acknowledgement
70+
-- 3. I2C bus busy
71+
-- 4. Arbitration lost
72+
-- 5. I2C slave acknowledgement
73+
-- 6. Outgoing I2C signals
74+
-- 6.1 SCL
75+
-- 6.2 SCL Output enable`
76+
-- 6.3 SDA
77+
-- 6.4 SDA Output enable
4778
( Signal System (BitVector 8)
4879
, Signal System Bool
4980
, Signal System Bool

clash-cores/src/Clash/Cores/I2C/BitMaster.hs

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,16 @@ import Clash.Cores.I2C.BitMaster.BusCtrl
1313
import Clash.Cores.I2C.BitMaster.StateMachine
1414
import Clash.Cores.I2C.Types
1515

16+
-- | Internal state of the I2C BitMaster.
17+
--
18+
-- It includes the bus status controller, bit-level state machine, and various control signals and counters.
19+
-- The '_busState' manages the overall status of the I2C bus.
20+
-- The '_stateMachine' handles the bit-level I2C operations.
21+
-- The '_dout' holds the data to be sent out on the I2C bus.
22+
-- The '_dsclOen' is a delayed version of the SCL output enable signal.
23+
-- The '_clkEn' enables the clock for the state machine.
24+
-- The '_slaveWait' indicates if the slave is pulling the SCL line low, causing the master to wait.
25+
-- The '_cnt' is a counter used for clock division.
1626
data BitMasterS
1727
= BitS
1828
{ _busState :: BusStatusCtrl
@@ -27,7 +37,19 @@ data BitMasterS
2737

2838
makeLenses ''BitMasterS
2939

40+
41+
-- | 5-tuple containing the input interface for the BitMaster.
42+
-- 1. Resets the internal state when asserted
43+
-- 2. Enables or disables the BitMaster
44+
-- 3. Used for clock division
45+
-- 4. Carries command and data in signals
46+
-- 5. Contains the SCL and SDA input signals
3047
type BitMasterI = (Bool,Bool,Unsigned 16,BitCtrlSig,I2CIn)
48+
49+
-- | 3-tuple containing the output interface for the BitMaster.
50+
-- 1. Carries command acknowledgment and other flags
51+
-- 2. Indicates if the BitMaster is currently busy
52+
-- 3. Contains the SCL and SDA output signals
3153
type BitMasterO = (BitRespSig,Bool,I2COut)
3254

3355
{-# ANN bitMaster
@@ -51,7 +73,11 @@ type BitMasterO = (BitRespSig,Bool,I2COut)
5173
, PortName "al"
5274
, PortName "dout" ]
5375
, PortName "busy"
54-
, PortName "i2cO"
76+
, PortProduct "i2c"
77+
[ PortName "sda"
78+
, PortName "sdaEn"
79+
, PortName "scl"
80+
, PortName "sclEn" ]
5581
]
5682
}) #-}
5783
bitMaster

clash-cores/src/Clash/Cores/I2C/BitMaster/BusCtrl.hs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import Control.Monad.State
1010
import Clash.Cores.I2C.BitMaster.StateMachine
1111
import Clash.Cores.I2C.Types
1212

13+
-- | Bus status control state.
1314
data BusStatusCtrl
1415
= BusStatusCtrl
1516
{ _sI2C :: I2CIn -- synchronized SCL and SDA
@@ -44,6 +45,8 @@ busStartState
4445

4546
-- See: https://github.com/clash-lang/clash-compiler/pull/2511
4647
{-# CLASH_OPAQUE busStatusCtrl #-}
48+
-- | Low level bus status controller that monitors the state of the bus and performs
49+
-- glitch filtering. It detects start conditions, stop conditions and arbitration loss.
4750
busStatusCtrl :: Bool
4851
-> Bool
4952
-> Unsigned 16

clash-cores/src/Clash/Cores/I2C/BitMaster/StateMachine.hs

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,38 +9,49 @@ import Control.Monad.State
99

1010
import Clash.Cores.I2C.Types
1111

12+
-- | States for bit-level I2C operations.
1213
data BitStateMachine
13-
= Idle
14-
| Start (Index 5)
15-
| Stop (Index 4)
16-
| Read (Index 4)
17-
| Write (Index 4)
14+
= Idle -- ^ Idle state
15+
| Start (Index 5) -- ^ Start condition state
16+
| Stop (Index 4) -- ^ Stop condition state
17+
| Read (Index 4) -- ^ Read operation state
18+
| Write (Index 4) -- ^ Write operation state
1819
deriving (Eq, Generic, NFDataX)
1920

21+
-- | Defines the state machine with control and status registers.
2022
data StateMachine
2123
= StateMachine
22-
{ _sclOen :: Bool -- i2c clock output enable register
23-
, _sdaOen :: Bool -- i2c data output enable register
24-
, _sdaChk :: Bool -- check SDA status (multi-master arbiter)
25-
, _cmdAck :: Bool -- command completed
26-
, _bitStateM :: BitStateMachine -- State Machine
24+
{ _sclOen :: Bool -- ^ Enables SCL output
25+
, _sdaOen :: Bool -- ^ Enables SDA output
26+
, _sdaChk :: Bool -- ^ Checks SDA status
27+
, _cmdAck :: Bool -- ^ Acknowledges command completion
28+
, _bitStateM :: BitStateMachine -- ^ Current state of the bit-level state machine
2729
} deriving (Generic, NFDataX)
2830

2931
makeLenses ''StateMachine
3032

31-
{-# INLINE stateMachineStart #-}
33+
-- | Initial state of the state machine.
3234
stateMachineStart :: StateMachine
3335
stateMachineStart
3436
= StateMachine
35-
{ _sclOen = True
36-
, _sdaOen = True
37-
, _sdaChk = False
38-
, _cmdAck = False
39-
, _bitStateM = Idle
37+
{ _sclOen = True -- ^ SCL output enabled by default
38+
, _sdaOen = True -- ^ SDA output enabled by default
39+
, _sdaChk = False -- ^ SDA status check disabled by default
40+
, _cmdAck = False -- ^ Command acknowledgment flag set to false
41+
, _bitStateM = Idle -- ^ Initial state set to Idle
4042
}
4143

4244
-- See: https://github.com/clash-lang/clash-compiler/pull/2511
4345
{-# CLASH_OPAQUE bitStateMachine #-}
46+
-- | Bit level I2C state machine that manages transitions between various states from
47+
-- 'StateMachine' based on the input parameters and the current state.
48+
--
49+
-- * In the 'Start' state, the function initiates the start condition on the I2C bus.
50+
-- * In the 'Stop' state, it initiates the stop condition, releasing the bus.
51+
-- * In the 'Read' state, it reads a bit from the slave device.
52+
-- * In the 'Write' state, it writes a bit to the slave device.
53+
--
54+
-- The function ensures that the state transitions are compliant with the I2C protocol.
4455
bitStateMachine :: Bool
4556
-> Bool
4657
-> Bool

clash-cores/src/Clash/Cores/I2C/ByteMaster.hs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,22 @@ data ByteMasterS
3030

3131
makeLenses ''ByteMasterS
3232

33+
-- |
34+
-- 1. Statemachine reset
35+
-- 2. Start
36+
-- 3. Stop
37+
-- 4. Read
38+
-- 5. Write
39+
-- 6. Acknowledge
40+
-- 7. Data in
41+
-- 8. Bitmaster response
3342
type ByteMasterI = (Bool,Bool,Bool,Bool,Bool,Bool,BitVector 8,BitRespSig)
43+
44+
-- |
45+
-- 1. Acknowledge for I2C controller
46+
-- 2. I2C acknowledgement
47+
-- 3. Data output
48+
-- 4 Bitmaster control signals
3449
type ByteMasterO = (Bool,Bool,BitVector 8,BitCtrlSig)
3550

3651
{-# ANN byteMaster
@@ -56,6 +71,10 @@ type ByteMasterO = (Bool,Bool,BitVector 8,BitCtrlSig)
5671
, PortName "bitCtrl"
5772
]
5873
}) #-}
74+
-- | Byte level controller, takes care of correctly executing i2c communication
75+
-- based on the supplied control signals. It should be instantiated alongside 'bitMaster'.
76+
-- The outgoing bitCtrl' controls the 'bitMaster' whose 'bitResp' should be supplied
77+
-- as last input.
5978
byteMaster
6079
:: Clock System
6180
-> Reset System

clash-cores/src/Clash/Cores/I2C/Types.hs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,18 @@ module Clash.Cores.I2C.Types where
22

33
import Clash.Prelude
44

5+
-- | I2C commands: start, stop, read, write, and no-op.
56
data I2CCommand = I2Cstart | I2Cstop | I2Cwrite | I2Cread | I2Cnop
67
deriving (Eq, Ord, Generic, NFDataX)
78

8-
type BitCtrlSig = (I2CCommand,Bit)
9-
type BitRespSig = (Bool,Bool,Bit)
9+
-- | Bit-level I2C control signals (Command, Bit).
10+
type BitCtrlSig = (I2CCommand, Bit)
1011

11-
type I2CIn = (Bit,Bit)
12-
type I2COut = (Bit,Bool,Bit,Bool)
12+
-- | Bit-level I2C response signals (Ack, Busy, Bit).
13+
type BitRespSig = (Bool, Bool, Bit)
14+
15+
-- | I2C input signals (SCL, SDA).
16+
type I2CIn = (Bit, Bit)
17+
18+
-- | I2C output signals (SCL, SCL enable, SDA, SDA enable).
19+
type I2COut = (Bit, Bool, Bit, Bool)

0 commit comments

Comments
 (0)