Skip to content

Commit

Permalink
Improve uartTx
Browse files Browse the repository at this point in the history
Rewrite in style of uartRx, and don't waste an extra cycle on each baud
  • Loading branch information
acairncross committed Dec 31, 2020
1 parent 8ad14a9 commit d2ad8f9
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 11 deletions.
23 changes: 13 additions & 10 deletions src/Euphrates/UART.hs
Original file line number Diff line number Diff line change
Expand Up @@ -88,25 +88,28 @@ data TxBit n
uartTxT :: KnownNat n => Word32 -> Maybe (BitVector n) -> State (TxState n) (Bit, Bool)
uartTxT clocksPerBaud input = get >>= \case
TxIdle -> case input of
Just input' -> put (TxBit clocksPerBaud (TxStartBit input')) >> return (high, True)
Just input' -> put (TxBit 0 (TxStartBit input')) >> return (high, True)
Nothing -> return (high, False)
TxBit cnt txBit ->
TxBit cnt txBit -> do
let cnt1 = cnt + 1
let baudDone = cnt1 == clocksPerBaud
let cnt' = if baudDone then 0 else cnt1
case txBit of
TxStartBit datum -> do
put $ if cnt == 0
then TxBit clocksPerBaud (TxDataBit datum 0)
else TxBit (cnt - 1) txBit
put $ if baudDone
then TxBit cnt' (TxDataBit datum 0)
else TxBit cnt' txBit
return (low, True)
TxDataBit datum i -> do
put $ if cnt == 0
then TxBit clocksPerBaud $
put $ if baudDone
then TxBit cnt' $
if i == maxBound then TxStopBit else TxDataBit (rotateR datum 1) (i+1)
else TxBit (cnt - 1) (TxDataBit datum i)
else TxBit cnt' (TxDataBit datum i)
return (lsb datum, True)
TxStopBit -> do
put $ if cnt == 0
put $ if baudDone
then TxIdle
else TxBit (cnt - 1) TxStopBit
else TxBit cnt' TxStopBit
return (high, True)

uartTx
Expand Down
2 changes: 1 addition & 1 deletion test/Euphrates/Spec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ spec = do

describe "UART" $ do
let clocksPerBaud = 111 :: Natural
let clocksPerIdle = 13
let clocksPerIdle = 0
let baudDuration = clocksPerBaud * snatToNatural (clockPeriod @System)
let values = [12, 34, 56, 78] :: [BitVector 8]

Expand Down

0 comments on commit d2ad8f9

Please sign in to comment.