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

Subversion Repositories open8_urisc

[/] [open8_urisc/] [trunk/] [VHDL/] [o8_max7221.vhd] - Blame information for rev 223

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

Line No. Rev Author Line
1 213 jshamlet
-- Copyright (c)2020 Jeremy Seth Henry
2 194 jshamlet
-- All rights reserved.
3
--
4
-- Redistribution and use in source and binary forms, with or without
5
-- modification, are permitted provided that the following conditions are met:
6
--     * Redistributions of source code must retain the above copyright
7
--       notice, this list of conditions and the following disclaimer.
8
--     * Redistributions in binary form must reproduce the above copyright
9
--       notice, this list of conditions and the following disclaimer in the
10
--       documentation and/or other materials provided with the distribution,
11
--       where applicable (as part of a user interface, debugging port, etc.)
12
--
13
-- THIS SOFTWARE IS PROVIDED BY JEREMY SETH HENRY ``AS IS'' AND ANY
14
-- EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15
-- WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16
-- DISCLAIMED. IN NO EVENT SHALL JEREMY SETH HENRY BE LIABLE FOR ANY
17
-- DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18
-- (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19
-- LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
20
-- ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21
-- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22
-- THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23
--
24
-- VHDL Units :  o8_max7221
25
-- Description:  Provides a memory mapped SPI interface to the max7221 LED
26
--                controller/driver.
27 213 jshamlet
--
28
-- Revision History
29
-- Author          Date     Change
30
------------------ -------- ---------------------------------------------------
31
-- Seth Henry      01/22/20 Design Start
32 194 jshamlet
 
33 191 jshamlet
library ieee;
34
use ieee.std_logic_1164.all;
35
use ieee.std_logic_unsigned.all;
36
use ieee.std_logic_arith.all;
37
use ieee.std_logic_misc.all;
38
 
39
library work;
40
  use work.open8_pkg.all;
41
 
42
entity o8_max7221 is
43
generic(
44 217 jshamlet
  Bit_Rate                   : real := 5000000.0;
45
  Sys_Freq                   : real;
46
  Reset_Level                : std_logic;
47
  Address                    : ADDRESS_TYPE
48 191 jshamlet
);
49
port(
50 217 jshamlet
  Clock                      : in  std_logic;
51
  Reset                      : in  std_logic;
52 191 jshamlet
  --
53 223 jshamlet
  Open8_Bus                  : in  OPEN8_BUS_TYPE;
54 191 jshamlet
  --
55 217 jshamlet
  Mx_Data                    : out std_logic;
56
  Mx_Clock                   : out std_logic;
57
  MX_LDCSn                   : out std_logic
58 191 jshamlet
);
59
end entity;
60
 
61
architecture behave of o8_max7221 is
62
 
63 217 jshamlet
  signal FIFO_Reset          : std_logic;
64 191 jshamlet
 
65 217 jshamlet
  constant User_Addr         : std_logic_vector(15 downto 4) :=
66
                                 Address(15 downto 4);
67 223 jshamlet
  alias  Comp_Addr           is Open8_Bus.Address(15 downto 4);
68 191 jshamlet
 
69 217 jshamlet
  signal FIFO_Wr_En          : std_logic;
70
  signal FIFO_Wr_Data        : std_logic_vector(11 downto 0);
71 191 jshamlet
 
72 217 jshamlet
  signal FIFO_Rd_En          : std_logic;
73
  signal FIFO_Empty          : std_logic;
74
  signal FIFO_Rd_Data        : std_logic_vector(11 downto 0);
75 191 jshamlet
 
76
  type TX_CTRL_STATES is (IDLE, TX_BYTE, TX_START, TX_WAIT );
77 217 jshamlet
  signal TX_Ctrl             : TX_CTRL_STATES;
78 191 jshamlet
 
79 217 jshamlet
  signal TX_En               : std_logic;
80
  signal TX_Idle             : std_logic;
81 191 jshamlet
 
82 217 jshamlet
  constant BAUD_DLY_VAL      : integer := integer((Sys_Freq / Bit_Rate)/ 2.0);
83
  constant BAUD_DLY_WDT      : integer := ceil_log2(BAUD_DLY_VAL - 1);
84
  constant BAUD_DLY          : std_logic_vector :=
85
                         conv_std_logic_vector(BAUD_DLY_VAL - 1, BAUD_DLY_WDT);
86 191 jshamlet
 
87 217 jshamlet
  signal Baud_Cntr           : std_logic_vector( BAUD_DLY_WDT - 1 downto 0 )
88
                               := (others => '0');
89
  signal Baud_Tick           : std_logic;
90 191 jshamlet
 
91
  type IO_STATES is ( IDLE, SYNC_CLK, SCLK_L, SCLK_H, ADV_BIT, DONE );
92 217 jshamlet
  signal io_state            : IO_STATES;
93
  signal bit_cntr            : std_logic_vector(3 downto 0);
94
  signal tx_buffer           : std_logic_vector(15 downto 0);
95 191 jshamlet
 
96
begin
97
 
98 223 jshamlet
  FIFO_Wr_En                 <= Open8_Bus.Wr_En when Comp_Addr = User_Addr else
99
                                '0';
100
 
101
  FIFO_Wr_Data               <= Open8_Bus.Address(3 downto 0) &
102
                                Open8_Bus.Wr_Data;
103
 
104 217 jshamlet
  FIFO_Reset                 <= Reset when Reset_Level = '1' else (not Reset);
105 191 jshamlet
 
106
  U_FIFO : entity work.o8_max7221_fifo
107
  port map(
108 217 jshamlet
    aclr                     => FIFO_Reset,
109
    clock                    => Clock,
110
    data                     => FIFO_Wr_Data,
111
    rdreq                    => FIFO_Rd_En,
112
    wrreq                    => FIFO_Wr_En,
113
    empty                    => FIFO_Empty,
114
    q                        => FIFO_Rd_Data
115 191 jshamlet
  );
116
 
117
  tx_FSM: process( Clock, Reset )
118
  begin
119
    if( Reset = Reset_Level )then
120 217 jshamlet
      TX_Ctrl                <= IDLE;
121
      TX_En                  <= '0';
122
      FIFO_Rd_En             <= '0';
123 191 jshamlet
    elsif( rising_edge(Clock) )then
124 217 jshamlet
      TX_En                  <= '0';
125
      FIFO_Rd_En             <= '0';
126 191 jshamlet
 
127
      case( TX_Ctrl )is
128
        when IDLE =>
129
          if( FIFO_Empty = '0' )then
130 217 jshamlet
            FIFO_Rd_En       <= '1';
131
            TX_Ctrl          <= TX_BYTE;
132 191 jshamlet
          end if;
133
 
134
        when TX_BYTE =>
135 217 jshamlet
          TX_En              <= '1';
136
          TX_Ctrl            <= TX_START;
137 191 jshamlet
 
138
        when TX_START =>
139
          if( TX_Idle = '0' )then
140 217 jshamlet
            TX_Ctrl          <= TX_WAIT;
141 191 jshamlet
          end if;
142
 
143
        when TX_WAIT =>
144
          if( TX_Idle = '1' )then
145 217 jshamlet
            TX_Ctrl          <= IDLE;
146 191 jshamlet
          end if;
147
 
148
        when others => null;
149
      end case;
150
 
151
    end if;
152
  end process;
153
 
154
  Baud_Rate_proc: process( Clock, Reset )
155
  begin
156
    if( Reset = Reset_Level )then
157 217 jshamlet
      Baud_Cntr              <= (others => '0');
158
      Baud_Tick              <= '0';
159 191 jshamlet
    elsif( rising_edge( Clock ) )then
160 217 jshamlet
      Baud_Cntr              <= Baud_Cntr - 1;
161
      Baud_Tick              <= nor_reduce(Baud_Cntr);
162 191 jshamlet
      if( Baud_Cntr = 0 )then
163 217 jshamlet
        Baud_Cntr            <= BAUD_DLY;
164 191 jshamlet
      end if;
165
    end if;
166
  end process;
167
 
168
  io_FSM: process( Clock, Reset )
169
  begin
170
    if( Reset = Reset_Level )then
171 217 jshamlet
      io_state               <= IDLE;
172
      bit_cntr               <= (others => '0');
173
      tx_buffer              <= (others => '0');
174
      TX_Idle                <= '0';
175 191 jshamlet
 
176 217 jshamlet
      Mx_Clock               <= '0';
177
      Mx_Data                <= '0';
178
      MX_LDCSn               <= '0';
179 191 jshamlet
 
180
    elsif( rising_edge(Clock) )then
181
 
182 217 jshamlet
      TX_Idle                <= '0';
183
      Mx_Clock               <= '0';
184 191 jshamlet
 
185
      case( io_state )is
186
        when IDLE =>
187 217 jshamlet
          Mx_Data            <= '0';
188
          MX_LDCSn           <= '1';
189
          TX_Idle            <= '1';
190 191 jshamlet
          if( TX_En = '1' )then
191 217 jshamlet
            tx_buffer        <= "0000" & FIFO_Rd_Data;
192
            bit_cntr         <= (others => '1');
193
            io_state         <= SYNC_CLK;
194 191 jshamlet
          end if;
195
 
196
        when SYNC_CLK =>
197
          if( Baud_Tick = '1' )then
198 217 jshamlet
            io_state         <= SCLK_L;
199 191 jshamlet
          end if;
200
 
201
        when SCLK_L =>
202 217 jshamlet
          MX_LDCSn           <= '0';
203
          Mx_Data            <= tx_buffer(conv_integer(bit_cntr));
204 191 jshamlet
          if( Baud_Tick = '1' )then
205 217 jshamlet
            io_state         <= SCLK_H;
206 191 jshamlet
          end if;
207
 
208
        when SCLK_H =>
209 217 jshamlet
          Mx_Clock           <= '1';
210 191 jshamlet
          if( Baud_Tick = '1' )then
211 217 jshamlet
            bit_cntr         <= bit_cntr - 1;
212
            io_state         <= ADV_BIT;
213 191 jshamlet
          end if;
214
 
215
        when ADV_BIT =>
216 217 jshamlet
          io_state           <= SCLK_L;
217 191 jshamlet
          if( and_reduce(bit_cntr) = '1' )then
218 217 jshamlet
            io_state         <= DONE;
219 191 jshamlet
          end if;
220
 
221
        when DONE =>
222 217 jshamlet
          Mx_Data            <= '0';
223 191 jshamlet
          if( Baud_Tick = '1' )then
224 217 jshamlet
            io_state         <= IDLE;
225 191 jshamlet
          end if;
226
 
227
        when others => null;
228
      end case;
229
    end if;
230
  end process;
231
 
232
end architecture;

powered by: WebSVN 2.1.0

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