SURF  1.0
UartTx.vhd
Go to the documentation of this file.
1 -------------------------------------------------------------------------------
2 -- File : UartTx.vhd
3 -- Company : SLAC National Accelerator Laboratory
4 -- Created : 2016-05-13
5 -- Last update: 2016-06-28
6 -------------------------------------------------------------------------------
7 -- Description: Uart Transmitter
8 -------------------------------------------------------------------------------
9 -- This file is part of 'SLAC Firmware Standard Library'.
10 -- It is subject to the license terms in the LICENSE.txt file found in the
11 -- top-level directory of this distribution and at:
12 -- https://confluence.slac.stanford.edu/display/ppareg/LICENSE.html.
13 -- No part of 'SLAC Firmware Standard Library', including this file,
14 -- may be copied, modified, propagated, or distributed except according to
15 -- the terms contained in the LICENSE.txt file.
16 -------------------------------------------------------------------------------
17 
18 library IEEE;
19 use ieee.std_logic_1164.all;
20 use ieee.std_logic_unsigned.all;
21 
22 use work.StdRtlPkg.all;
23 
24 --! @see entity
25  --! @ingroup protocols_uart
26 entity UartTx is
27  generic (
28  TPD_G : time := 1 ns);
29  port (
30  clk : in sl;
31  rst : in sl;
32  baud16x : in sl;
33  wrData : in slv(7 downto 0);
34  wrValid : in sl;
35  wrReady : out sl;
36  tx : out sl);
37 end entity UartTx;
38 
39 architecture RTL of UartTx is
40 
41  type StateType is (WAIT_DATA_S, SYNC_EN_16_S, WAIT_16_S, TX_BIT_S);
42 
43  type RegType is record
45  holdReg : slv(7 downto 0);
47  baud16xCount : slv(3 downto 0);
48  shiftReg : slv(9 downto 0);
49  shiftCount : slv(3 downto 0);
50  end record RegType;
51 
52  constant REG_INIT_C : RegType := (
53  wrReady => '0',
54  holdReg => (others => '0'),
55  txState => WAIT_DATA_S,
56  baud16xCount => (others => '0'),
57  shiftReg => (others => '1'),
58  shiftCount => (others => '0'));
59 
60  signal r : RegType := REG_INIT_C;
61  signal rin : RegType;
62 
63 begin -- architecture RTL
64 
65 
66  comb : process (baud16x, r, rst, wrData, wrValid) is
67  variable v : RegType;
68  begin
69  v := r;
70 
71  case r.txState is
72  -- Wait for new data to send
73  when WAIT_DATA_S =>
74  v.wrReady := '1';
75  if (wrValid = '1' and r.wrReady = '1') then
76  v.wrReady := '0';
77  v.holdReg := wrData;
78  v.txState := SYNC_EN_16_S;
79  end if;
80 
81  -- Wait for next baud16x to synchronize
82  -- Then load the shift reg
83  -- Bit 0 is the start bit, bit 9 is the stop bit
84  when SYNC_EN_16_S =>
85  if (baud16x = '1') then
86  v.baud16xCount := (others => '0');
87  v.shiftReg := '1' & r.holdReg & '0';
88  v.shiftCount := (others => '0');
89  v.txState := WAIT_16_S;
90  end if;
91 
92  -- Wait 16 baud_16x counts (the baud rate)
93  -- When shifted all bits, wait for next tx data
94  when WAIT_16_S =>
95  if (baud16x = '1') then
96  v.baud16xCount := r.baud16xCount + 1;
97  if (r.baud16xCount = 15) then
98  v.txState := TX_BIT_S;
99  if (r.shiftCount = 9) then
100  v.txState := WAIT_DATA_S;
101  end if;
102  end if;
103  end if;
104 
105  -- Shift to TX next bit, increment shift count
106  when TX_BIT_S =>
107  v.shiftReg := '0' & r.shiftReg(9 downto 1);
108  v.shiftCount := r.shiftCount + 1;
109  v.txState := WAIT_16_S;
110 
111  end case;
112 
113  if (rst = '1') then
114  v := REG_INIT_C;
115  end if;
116 
117  rin <= v;
118  wrReady <= r.wrReady;
119  tx <= r.shiftReg(0);
120 
121  end process;
122 
123  seq : process (clk) is
124  begin
125  if (rising_edge(clk)) then
126  r <= rin after TPD_G;
127  end if;
128  end process seq;
129 
130 
131 end architecture RTL;
in wrDataslv( 7 downto 0)
Definition: UartTx.vhd:33
TPD_Gtime := 1 ns
Definition: UartTx.vhd:28
in wrValidsl
Definition: UartTx.vhd:34
RegType rin
Definition: UartTx.vhd:61
std_logic sl
Definition: StdRtlPkg.vhd:28
slv( 9 downto 0) shiftReg
Definition: UartTx.vhd:48
StateType txState
Definition: UartTx.vhd:46
_library_ ieeeieee
Definition: UartRx.vhd:18
slv( 7 downto 0) holdReg
Definition: UartTx.vhd:45
out txsl
Definition: UartTx.vhd:36
sl wrReady
Definition: UartTx.vhd:44
RegType := REG_INIT_C r
Definition: UartTx.vhd:60
(WAIT_DATA_S,SYNC_EN_16_S,WAIT_16_S,TX_BIT_S) StateType
Definition: UartTx.vhd:41
slv( 3 downto 0) baud16xCount
Definition: UartTx.vhd:47
slv( 3 downto 0) shiftCount
Definition: UartTx.vhd:49
out wrReadysl
Definition: UartTx.vhd:35
in baud16xsl
Definition: UartTx.vhd:32
in rstsl
Definition: UartTx.vhd:31
in clksl
Definition: UartTx.vhd:30
RegType :=(wrReady => '0',holdReg =>( others => '0'),txState => WAIT_DATA_S,baud16xCount =>( others => '0'),shiftReg =>( others => '1'),shiftCount =>( others => '0')) REG_INIT_C
Definition: UartTx.vhd:52
std_logic_vector slv
Definition: StdRtlPkg.vhd:29