OpenCores
URL https://opencores.org/ocsvn/neorv32/neorv32/trunk

Subversion Repositories neorv32

[/] [neorv32/] [trunk/] [rtl/] [core/] [neorv32_twi.vhd] - Blame information for rev 35

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 zero_gravi
-- #################################################################################################
2 6 zero_gravi
-- # << NEORV32 - Two-Wire Interface Controller (TWI) >>                                           #
3 2 zero_gravi
-- # ********************************************************************************************* #
4
-- # Supports START and STOP conditions, 8 bit data + ACK/NACK transfers and clock stretching.     #
5 6 zero_gravi
-- # Supports ACKs by the constroller. No multi-controller support and no peripheral mode support  #
6
-- # yet. Interrupt: TWI_transfer_done                                                             #
7 2 zero_gravi
-- # ********************************************************************************************* #
8
-- # BSD 3-Clause License                                                                          #
9
-- #                                                                                               #
10
-- # Copyright (c) 2020, Stephan Nolting. All rights reserved.                                     #
11
-- #                                                                                               #
12
-- # Redistribution and use in source and binary forms, with or without modification, are          #
13
-- # permitted provided that the following conditions are met:                                     #
14
-- #                                                                                               #
15
-- # 1. Redistributions of source code must retain the above copyright notice, this list of        #
16
-- #    conditions and the following disclaimer.                                                   #
17
-- #                                                                                               #
18
-- # 2. Redistributions in binary form must reproduce the above copyright notice, this list of     #
19
-- #    conditions and the following disclaimer in the documentation and/or other materials        #
20
-- #    provided with the distribution.                                                            #
21
-- #                                                                                               #
22
-- # 3. Neither the name of the copyright holder nor the names of its contributors may be used to  #
23
-- #    endorse or promote products derived from this software without specific prior written      #
24
-- #    permission.                                                                                #
25
-- #                                                                                               #
26
-- # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS   #
27
-- # OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF               #
28
-- # MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE    #
29
-- # COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,     #
30
-- # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE #
31
-- # GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED    #
32
-- # AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING     #
33
-- # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED  #
34
-- # OF THE POSSIBILITY OF SUCH DAMAGE.                                                            #
35
-- # ********************************************************************************************* #
36
-- # The NEORV32 Processor - https://github.com/stnolting/neorv32              (c) Stephan Nolting #
37
-- #################################################################################################
38
 
39
library ieee;
40
use ieee.std_logic_1164.all;
41
use ieee.numeric_std.all;
42
 
43
library neorv32;
44
use neorv32.neorv32_package.all;
45
 
46
entity neorv32_twi is
47
  port (
48
    -- host access --
49
    clk_i       : in  std_ulogic; -- global clock line
50
    addr_i      : in  std_ulogic_vector(31 downto 0); -- address
51
    rden_i      : in  std_ulogic; -- read enable
52
    wren_i      : in  std_ulogic; -- write enable
53
    data_i      : in  std_ulogic_vector(31 downto 0); -- data in
54
    data_o      : out std_ulogic_vector(31 downto 0); -- data out
55
    ack_o       : out std_ulogic; -- transfer acknowledge
56
    -- clock generator --
57
    clkgen_en_o : out std_ulogic; -- enable clock generator
58
    clkgen_i    : in  std_ulogic_vector(07 downto 0);
59
    -- com lines --
60
    twi_sda_io  : inout std_logic; -- serial data line
61
    twi_scl_io  : inout std_logic; -- serial clock line
62
    -- interrupt --
63
    twi_irq_o   : out std_ulogic -- transfer done IRQ
64
  );
65
end neorv32_twi;
66
 
67
architecture neorv32_twi_rtl of neorv32_twi is
68
 
69
  -- IO space: module base address --
70
  constant hi_abb_c : natural := index_size_f(io_size_c)-1; -- high address boundary bit
71
  constant lo_abb_c : natural := index_size_f(twi_size_c); -- low address boundary bit
72
 
73
  -- control reg bits --
74
  constant ctrl_twi_en_c     : natural := 0; -- r/w: TWI enable
75
  constant ctrl_twi_start_c  : natural := 1; -- -/w: Generate START condition
76
  constant ctrl_twi_stop_c   : natural := 2; -- -/w: Generate STOP condition
77 35 zero_gravi
  constant ctrl_twi_irq_en_c : natural := 3; -- r/w: Enable transmission done interrupt
78 2 zero_gravi
  constant ctrl_twi_prsc0_c  : natural := 4; -- r/w: CLK prsc bit 0
79
  constant ctrl_twi_prsc1_c  : natural := 5; -- r/w: CLK prsc bit 1
80
  constant ctrl_twi_prsc2_c  : natural := 6; -- r/w: CLK prsc bit 2
81 6 zero_gravi
  constant ctrl_twi_mack_c   : natural := 7; -- r/w: generate ACK by controller for transmission
82 35 zero_gravi
  constant ctrl_twi_cksten_c : natural := 8; -- r/w: enable clock stretching by peripheral
83 2 zero_gravi
  --
84
  constant ctrl_twi_ack_c    : natural := 30; -- r/-: Set if ACK received
85
  constant ctrl_twi_busy_c   : natural := 31; -- r/-: Set if TWI unit is busy
86
 
87
  -- access control --
88
  signal acc_en : std_ulogic; -- module access enable
89
  signal addr   : std_ulogic_vector(31 downto 0); -- access address
90
  signal wr_en  : std_ulogic; -- word write enable
91
  signal rd_en  : std_ulogic; -- read enable
92
 
93
  -- twi clocking --
94
  signal twi_clk        : std_ulogic;
95
  signal twi_phase_gen  : std_ulogic_vector(3 downto 0);
96
  signal twi_clk_phase  : std_ulogic_vector(3 downto 0);
97
 
98
  -- twi clock stretching --
99
  signal twi_clk_halt : std_ulogic;
100
 
101
  -- twi transceiver core --
102 35 zero_gravi
  signal ctrl         : std_ulogic_vector(8 downto 0); -- unit's control register
103 2 zero_gravi
  signal arbiter      : std_ulogic_vector(2 downto 0);
104
  signal twi_bitcnt   : std_ulogic_vector(3 downto 0);
105
  signal twi_rtx_sreg : std_ulogic_vector(8 downto 0); -- main rx/tx shift reg
106
 
107
  -- tri-state I/O --
108
  signal twi_sda_i_ff0, twi_sda_i_ff1 : std_ulogic; -- sda input sync
109
  signal twi_scl_i_ff0, twi_scl_i_ff1 : std_ulogic; -- sda input sync
110
  signal twi_sda_i,     twi_sda_o     : std_ulogic;
111
  signal twi_scl_i,     twi_scl_o     : std_ulogic;
112
 
113
begin
114
 
115
  -- Access Control -------------------------------------------------------------------------
116
  -- -------------------------------------------------------------------------------------------
117
  acc_en <= '1' when (addr_i(hi_abb_c downto lo_abb_c) = twi_base_c(hi_abb_c downto lo_abb_c)) else '0';
118
  addr   <= twi_base_c(31 downto lo_abb_c) & addr_i(lo_abb_c-1 downto 2) & "00"; -- word aligned
119
  wr_en  <= acc_en and wren_i;
120
  rd_en  <= acc_en and rden_i;
121
 
122
 
123
  -- Read/Write Access ----------------------------------------------------------------------
124
  -- -------------------------------------------------------------------------------------------
125
  rw_access: process(clk_i)
126
  begin
127
    if rising_edge(clk_i) then
128
      ack_o <= acc_en and (rden_i or wren_i);
129
      -- write access --
130
      if (wr_en = '1') then
131
        if (addr = twi_ctrl_addr_c) then
132 22 zero_gravi
          ctrl <= data_i(ctrl'left downto 0);
133 2 zero_gravi
        end if;
134
      end if;
135
      -- read access --
136
      data_o <= (others => '0');
137
      if (rd_en = '1') then
138
        if (addr = twi_ctrl_addr_c) then
139
          data_o(ctrl_twi_en_c)     <= ctrl(ctrl_twi_en_c);
140
          data_o(ctrl_twi_irq_en_c) <= ctrl(ctrl_twi_irq_en_c);
141
          data_o(ctrl_twi_prsc0_c)  <= ctrl(ctrl_twi_prsc0_c);
142
          data_o(ctrl_twi_prsc1_c)  <= ctrl(ctrl_twi_prsc1_c);
143
          data_o(ctrl_twi_prsc2_c)  <= ctrl(ctrl_twi_prsc2_c);
144
          data_o(ctrl_twi_mack_c)   <= ctrl(ctrl_twi_mack_c);
145 35 zero_gravi
          data_o(ctrl_twi_cksten_c) <= ctrl(ctrl_twi_cksten_c);
146 2 zero_gravi
          --
147
          data_o(ctrl_twi_ack_c)    <= not twi_rtx_sreg(0);
148
          data_o(ctrl_twi_busy_c)   <= arbiter(1) or arbiter(0);
149
        else -- twi_rtx_addr_c =>
150
          data_o(7 downto 0)        <= twi_rtx_sreg(8 downto 1);
151
 
152
        end if;
153
      end if;
154
    end if;
155
  end process rw_access;
156
 
157
 
158
  -- Clock Generation -----------------------------------------------------------------------
159
  -- -------------------------------------------------------------------------------------------
160
  -- clock generator enable --
161
  clkgen_en_o <= ctrl(ctrl_twi_en_c);
162
 
163
  -- main twi clock select --
164
  twi_clk <= clkgen_i(to_integer(unsigned(ctrl(ctrl_twi_prsc2_c downto ctrl_twi_prsc0_c))));
165
 
166
  -- generate four non-overlapping clock ticks at twi_clk/4 --
167
  clock_phase_gen: process(clk_i)
168
  begin
169
    if rising_edge(clk_i) then
170
      if (arbiter(2) = '0') or (arbiter = "100") then -- offline or idle
171
        twi_phase_gen <= "0001"; -- make sure to start with a new phase, 0,1,2,3 stepping
172
      elsif (twi_clk = '1') and (twi_clk_halt = '0') then -- enabled and no clock stretching detected
173
        twi_phase_gen <= twi_phase_gen(2 downto 0) & twi_phase_gen(3); -- shift left
174
      end if;
175
    end if;
176
  end process clock_phase_gen;
177
 
178
  twi_clk_phase(0) <= twi_phase_gen(0) and twi_clk; -- first step
179
  twi_clk_phase(1) <= twi_phase_gen(1) and twi_clk;
180
  twi_clk_phase(2) <= twi_phase_gen(2) and twi_clk;
181
  twi_clk_phase(3) <= twi_phase_gen(3) and twi_clk; -- last step
182
 
183
 
184
  -- TWI Transceiver ------------------------------------------------------------------------
185
  -- -------------------------------------------------------------------------------------------
186
  twi_rtx_unit: process(clk_i)
187
  begin
188
    if rising_edge(clk_i) then
189
      -- input synchronizer & sampler --
190
      twi_sda_i_ff0 <= twi_sda_i;
191
      twi_sda_i_ff1 <= twi_sda_i_ff0;
192
      twi_scl_i_ff0 <= twi_scl_i;
193
      twi_scl_i_ff1 <= twi_scl_i_ff0;
194
 
195
      -- defaults --
196
      twi_irq_o  <= '0';
197
      arbiter(2) <= ctrl(ctrl_twi_en_c); -- still activated?
198
 
199
      -- serial engine --
200
      -- TWI bus signals are set/sampled using 4 clock phases
201
      case arbiter is
202
 
203 6 zero_gravi
        when "100" => -- IDLE: waiting for requests, bus might be still claimed by this controller if no STOP condition was generated
204 2 zero_gravi
          twi_bitcnt <= (others => '0');
205
          if (wr_en = '1') then
206
            if (addr = twi_ctrl_addr_c) then
207
              if (data_i(ctrl_twi_start_c) = '1') then -- issue START condition
208
                arbiter(1 downto 0) <= "01";
209
              elsif (data_i(ctrl_twi_stop_c) = '1') then  -- issue STOP condition
210
                arbiter(1 downto 0) <= "10";
211
              end if;
212
            elsif (addr = twi_rtx_addr_c) then -- start a data transmission
213 6 zero_gravi
              -- one bit extra for ack, issued by controller if ctrl_twi_mack_c is set,
214
              -- sampled from peripheral if ctrl_twi_mack_c is cleared
215 22 zero_gravi
              twi_rtx_sreg <= data_i(7 downto 0) & (not ctrl(ctrl_twi_mack_c));
216
              arbiter(1 downto 0) <= "11";
217 2 zero_gravi
            end if;
218
          end if;
219
 
220
        when "101" => -- START: generate START condition
221
          if (twi_clk_phase(0) = '1') then
222
            twi_sda_o <= '1';
223
          elsif (twi_clk_phase(1) = '1') then
224
            twi_sda_o <= '0';
225
          end if;
226
 
227
          if (twi_clk_phase(0) = '1') then
228
            twi_scl_o <= '1';
229
          elsif (twi_clk_phase(3) = '1') then
230
            twi_scl_o <= '0';
231
            arbiter(1 downto 0) <= "00"; -- go back to IDLE
232
          end if;
233
 
234
        when "110" => -- STOP: generate STOP condition
235
          if (twi_clk_phase(0) = '1') then
236
            twi_sda_o <= '0';
237
          elsif (twi_clk_phase(3) = '1') then
238
            twi_sda_o <= '1';
239
            arbiter(1 downto 0) <= "00"; -- go back to IDLE
240
          end if;
241
 
242
          if (twi_clk_phase(0) = '1') then
243
            twi_scl_o <= '0';
244
          elsif (twi_clk_phase(1) = '1') then
245
            twi_scl_o <= '1';
246
          end if;
247
 
248
        when "111" => -- TRANSMISSION: transmission in progress
249
          if (twi_clk_phase(0) = '1') then
250
            twi_bitcnt   <= std_ulogic_vector(unsigned(twi_bitcnt) + 1);
251
            twi_scl_o    <= '0';
252
            twi_sda_o    <= twi_rtx_sreg(8); -- MSB first
253
          elsif (twi_clk_phase(1) = '1') then -- first half + second half of valid data strobe
254
            twi_scl_o    <= '1';
255
          elsif (twi_clk_phase(3) = '1') then
256
            twi_rtx_sreg <= twi_rtx_sreg(7 downto 0) & twi_sda_i_ff1; -- sample and shift left
257
            twi_scl_o    <= '0';
258
          end if;
259
 
260
          if (twi_bitcnt = "1010") then -- 8 data bits + 1 bit for ACK + 1 tick delay
261
            arbiter(1 downto 0) <= "00"; -- go back to IDLE
262
            twi_irq_o <= ctrl(ctrl_twi_irq_en_c); -- fire IRQ if enabled
263
          end if;
264
 
265
        when others => -- "0--" OFFLINE: TWI deactivated
266
          twi_sda_o <= '1';
267
          twi_scl_o <= '1';
268
          arbiter   <= ctrl(ctrl_twi_en_c) & "00"; -- stay here, go to idle when activated
269
 
270
      end case;
271
    end if;
272
  end process twi_rtx_unit;
273
 
274
 
275
  -- Clock Stretching Detector --------------------------------------------------------------
276
  -- -------------------------------------------------------------------------------------------
277 35 zero_gravi
  clock_stretching: process(ctrl, arbiter, twi_scl_o, twi_scl_i_ff1)
278 2 zero_gravi
  begin
279 6 zero_gravi
    -- clock stretching by the peripheral can happen at "any time"
280 35 zero_gravi
    if (arbiter(2) = '1') and              -- module enabled
281
       (ctrl(ctrl_twi_cksten_c) = '1') and -- clock stretching enabled
282
       (twi_scl_o = '1') and               -- controller wants to pull scl high
283
       (twi_scl_i_ff1 = '0') then          -- but scl is pulled low by peripheral
284 2 zero_gravi
      twi_clk_halt <= '1';
285
    else
286
      twi_clk_halt <= '0';
287
    end if;
288
  end process clock_stretching;
289
 
290
 
291
  -- Tri-State Driver -----------------------------------------------------------------------
292
  -- -------------------------------------------------------------------------------------------
293
  -- SDA and SCL need to be of type std_logic to be correctly resolved in simulation
294
  twi_sda_io <= '0' when (twi_sda_o = '0') else 'Z';
295
  twi_scl_io <= '0' when (twi_scl_o = '0') else 'Z';
296
 
297
  -- read-back --
298
  twi_sda_i <= std_ulogic(twi_sda_io);
299
  twi_scl_i <= std_ulogic(twi_scl_io);
300
 
301
 
302
end neorv32_twi_rtl;

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.