-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUART.hs
More file actions
46 lines (36 loc) · 1.59 KB
/
UART.hs
File metadata and controls
46 lines (36 loc) · 1.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE RecordWildCards #-}
module UART where
import CLaSH.Prelude
-- import qualified Data.List as L
data TXState = TXState { txDataReg :: BitVector 8
, txEmpty :: Bool
, txCount :: BitVector 4
, txOut :: Bit
, clockDividerCount :: BitVector 13
} deriving (Show)
divider :: BitVector 13
divider = 5207 -- 50E6/9600 -1
uartInitialState :: TXState
uartInitialState = TXState 0 True 0 1 0
txUart :: Signal (BitVector 8, Bool, Bool) -> Signal (Bit, Bool)
txUart = txRun `mealy` uartInitialState
txRun :: TXState -> (BitVector 8, Bool, Bool) -> (TXState, (Bit, Bool))
txRun st@TXState{..} (txData, txLoadData, txEnable) = (st', (out, txEmpty'')) where
(st', out, txEmpty'') = case clockDividerCount of
_ | clockDividerCount == divider -> st'' where
txDataReg' = if txLoadData then txData else txDataReg
txEmpty' = case txLoadData of
True -> False
_ | txCount == 9 -> True
_ -> txEmpty
txOut' = if not txEnable then 0 else case txCount of
0 -> 0
9 -> 1
_ -> txDataReg ! (txCount -1)
txCount' = case txCount of
9 -> 0
_ -> txCount + 1
st'' = (st{clockDividerCount = 0, txDataReg = txDataReg', txEmpty = txEmpty', txOut = txOut', txCount = txCount'}, txOut', txEmpty')
_ -> st'' where
st'' = (st{clockDividerCount = clockDividerCount+1}, txOut, txEmpty)