summaryrefslogtreecommitdiffstats
path: root/PIC/uart.vhd
blob: a0fad93659c981519745d1967825db40a5d3ccd6 (plain) (blame)
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;

-- TX: fire and forget (8E1)
-- RX: FIFO-backed (8E1), consumer reads via rd_en/rx_data/rx_empty

entity uart is
  generic (
    CLK_FREQ     : positive := 50_000_000;
    BAUD_RATE    : positive := 115_200;
    CLKS_PER_BIT : positive := CLK_FREQ / BAUD_RATE
  );
  port (
    clk            : in  std_logic;
    -- TX
    tx_should_send : in  std_logic;
    byte_in        : in  std_logic_vector(7 downto 0);
    tx_done        : out std_logic;
    baud_tick      : out std_logic;
    uart_tx        : out std_logic;
    -- RX
    uart_rx        : in  std_logic;
    rd_en          : in  std_logic;
    rx_data        : out std_logic_vector(7 downto 0);
    rx_empty       : out std_logic;
    rx_full        : out std_logic
  );
end entity uart;

architecture rtl of uart is

  signal baud_counter : std_logic_vector(15 downto 0)
    := (others => '0');
  signal baud_tick_i  : std_logic := '0';

  signal tx_out       : std_logic := '1';
  signal current_byte : std_logic_vector(7 downto 0) := (others => '0');
  signal tx_byte_latch : std_logic_vector(7 downto 0) := (others => '0');

  -- RX input synchronizer (2FF, idle state is '1')
  signal uart_rx_meta : std_logic := '1';
  signal uart_rx_sync : std_logic := '1';

  -- RX FIFO wiring
  signal fifo_wr_en : std_logic := '0';
  signal fifo_din   : std_logic_vector(7 downto 0)
    := (others => '0');
  signal fifo_full  : std_logic := '0';

begin

  uart_tx   <= tx_out;
  baud_tick <= baud_tick_i;

  rx_fifo : entity work.fifo
    generic map (DEPTH => 64, ADDR_BITS => 6, WIDTH => 8)
    port map (
      clk   => clk,
      wr_en => fifo_wr_en,
      rd_en => rd_en,
      din   => fifo_din,
      dout  => rx_data,
      full  => fifo_full,
      empty => rx_empty
    );

  rx_full <= fifo_full;

  uart_rx_synchronizer : process(clk)
  begin
    if rising_edge(clk) then
      uart_rx_meta <= uart_rx;
      uart_rx_sync <= uart_rx_meta;
    end if;
  end process uart_rx_synchronizer;

  uart_baud_timer : process(clk)
  begin
    if rising_edge(clk) then
      if unsigned(baud_counter) = CLKS_PER_BIT - 1 then
        baud_counter <= (others => '0');
        baud_tick_i  <= '1';
      else
        baud_counter <= std_logic_vector(unsigned(baud_counter) + 1);
        baud_tick_i  <= '0';
      end if;
    end if;
  end process uart_baud_timer;

  uart_transmit_byte : process(clk)
    type t_state is (idle, start, data, parity, stop);
    variable state       : t_state := idle;
    variable bit_index   : unsigned(2 downto 0);
    variable parity_value : std_logic;
  begin
    if rising_edge(clk) then
      tx_done <= '0';
      if baud_tick_i = '1' then
        case state is
          when idle =>
            if tx_should_send = '1' then
              tx_out        <= '0';           -- start bit immediately
              tx_byte_latch <= byte_in;       -- latch byte now
              parity_value  := '0';
              bit_index     := (others => '0');
              state         := data;
            end if;
          when start =>
            null;  -- unused, kept for completeness
          when data =>
            tx_out       <= tx_byte_latch(to_integer(bit_index));
            parity_value := parity_value xor tx_byte_latch(to_integer(bit_index));
            if bit_index = 7 then
              state := parity;
            else
              state := data;
            end if;
            bit_index := bit_index + 1;
          when parity =>
            tx_out <= parity_value;
            state  := stop;
          when stop =>
            tx_out  <= '1';
            tx_done <= '1';
            state   := idle;
        end case;
      end if;
    end if;
  end process uart_transmit_byte;

  uart_receive_byte : process(clk)
    type rx_state_t is (idle, start, data, parity, stop);
    variable state       : rx_state_t := idle;
    variable bit_index   : unsigned(2 downto 0);
    variable rx_byte     : std_logic_vector(7 downto 0);
    variable parity_val  : std_logic;
    variable rx_counter  : unsigned(15 downto 0);
  begin
    if rising_edge(clk) then
      fifo_wr_en <= '0';
      case state is
        when idle =>
          if uart_rx_sync = '0' then
            rx_counter := to_unsigned(CLKS_PER_BIT / 2, 16);
            state      := start;
          end if;
        when start =>
          if rx_counter = 0 then
            if uart_rx_sync = '0' then
              rx_counter := to_unsigned(CLKS_PER_BIT - 1, 16);
              bit_index  := (others => '0');
              parity_val := '0';
              state      := data;
            else
              state := idle;
            end if;
          else
            rx_counter := rx_counter - 1;
          end if;
        when data =>
          if rx_counter = 0 then
            rx_byte(to_integer(bit_index)) := uart_rx_sync;
            parity_val := parity_val xor uart_rx_sync;
            rx_counter := to_unsigned(CLKS_PER_BIT - 1, 16);
            if bit_index = 7 then
              state := parity;
            else
              bit_index := bit_index + 1;
            end if;
          else
            rx_counter := rx_counter - 1;
          end if;
        when parity =>
          if rx_counter = 0 then
            rx_counter := to_unsigned(CLKS_PER_BIT - 1, 16);
            state      := stop;
          else
            rx_counter := rx_counter - 1;
          end if;
        when stop =>
          if rx_counter = 0 then
            if uart_rx_sync = '1' and fifo_full = '0' then
              fifo_din   <= rx_byte;
              fifo_wr_en <= '1';
            end if;
            state := idle;
          else
            rx_counter := rx_counter - 1;
          end if;
      end case;
    end if;
  end process uart_receive_byte;

end architecture rtl;