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 219

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 217 jshamlet
  Bus_Address                : in  ADDRESS_TYPE;
54
  Wr_Enable                  : in  std_logic;
55
  Wr_Data                    : in  DATA_TYPE;
56 191 jshamlet
  --
57 217 jshamlet
  Mx_Data                    : out std_logic;
58
  Mx_Clock                   : out std_logic;
59
  MX_LDCSn                   : out std_logic
60 191 jshamlet
);
61
end entity;
62
 
63
architecture behave of o8_max7221 is
64
 
65 217 jshamlet
  signal FIFO_Reset          : std_logic;
66 191 jshamlet
 
67 217 jshamlet
  constant User_Addr         : std_logic_vector(15 downto 4) :=
68
                                 Address(15 downto 4);
69
  alias  Comp_Addr           is Bus_Address(15 downto 4);
70 191 jshamlet
 
71 217 jshamlet
  signal FIFO_Wr_En          : std_logic;
72
  signal FIFO_Wr_Data        : std_logic_vector(11 downto 0);
73 191 jshamlet
 
74 217 jshamlet
  signal FIFO_Rd_En          : std_logic;
75
  signal FIFO_Empty          : std_logic;
76
  signal FIFO_Rd_Data        : std_logic_vector(11 downto 0);
77 191 jshamlet
 
78
  type TX_CTRL_STATES is (IDLE, TX_BYTE, TX_START, TX_WAIT );
79 217 jshamlet
  signal TX_Ctrl             : TX_CTRL_STATES;
80 191 jshamlet
 
81 217 jshamlet
  signal TX_En               : std_logic;
82
  signal TX_Idle             : std_logic;
83 191 jshamlet
 
84 217 jshamlet
  constant BAUD_DLY_VAL      : integer := integer((Sys_Freq / Bit_Rate)/ 2.0);
85
  constant BAUD_DLY_WDT      : integer := ceil_log2(BAUD_DLY_VAL - 1);
86
  constant BAUD_DLY          : std_logic_vector :=
87
                         conv_std_logic_vector(BAUD_DLY_VAL - 1, BAUD_DLY_WDT);
88 191 jshamlet
 
89 217 jshamlet
  signal Baud_Cntr           : std_logic_vector( BAUD_DLY_WDT - 1 downto 0 )
90
                               := (others => '0');
91
  signal Baud_Tick           : std_logic;
92 191 jshamlet
 
93
  type IO_STATES is ( IDLE, SYNC_CLK, SCLK_L, SCLK_H, ADV_BIT, DONE );
94 217 jshamlet
  signal io_state            : IO_STATES;
95
  signal bit_cntr            : std_logic_vector(3 downto 0);
96
  signal tx_buffer           : std_logic_vector(15 downto 0);
97 191 jshamlet
 
98
begin
99
 
100 217 jshamlet
  FIFO_Wr_En                 <= Wr_Enable when Comp_Addr = User_Addr else '0';
101
  FIFO_Wr_Data               <= Bus_Address(3 downto 0) & Wr_Data;
102
  FIFO_Reset                 <= Reset when Reset_Level = '1' else (not Reset);
103 191 jshamlet
 
104
  U_FIFO : entity work.o8_max7221_fifo
105
  port map(
106 217 jshamlet
    aclr                     => FIFO_Reset,
107
    clock                    => Clock,
108
    data                     => FIFO_Wr_Data,
109
    rdreq                    => FIFO_Rd_En,
110
    wrreq                    => FIFO_Wr_En,
111
    empty                    => FIFO_Empty,
112
    q                        => FIFO_Rd_Data
113 191 jshamlet
  );
114
 
115
  tx_FSM: process( Clock, Reset )
116
  begin
117
    if( Reset = Reset_Level )then
118 217 jshamlet
      TX_Ctrl                <= IDLE;
119
      TX_En                  <= '0';
120
      FIFO_Rd_En             <= '0';
121 191 jshamlet
    elsif( rising_edge(Clock) )then
122 217 jshamlet
      TX_En                  <= '0';
123
      FIFO_Rd_En             <= '0';
124 191 jshamlet
 
125
      case( TX_Ctrl )is
126
        when IDLE =>
127
          if( FIFO_Empty = '0' )then
128 217 jshamlet
            FIFO_Rd_En       <= '1';
129
            TX_Ctrl          <= TX_BYTE;
130 191 jshamlet
          end if;
131
 
132
        when TX_BYTE =>
133 217 jshamlet
          TX_En              <= '1';
134
          TX_Ctrl            <= TX_START;
135 191 jshamlet
 
136
        when TX_START =>
137
          if( TX_Idle = '0' )then
138 217 jshamlet
            TX_Ctrl          <= TX_WAIT;
139 191 jshamlet
          end if;
140
 
141
        when TX_WAIT =>
142
          if( TX_Idle = '1' )then
143 217 jshamlet
            TX_Ctrl          <= IDLE;
144 191 jshamlet
          end if;
145
 
146
        when others => null;
147
      end case;
148
 
149
    end if;
150
  end process;
151
 
152
  Baud_Rate_proc: process( Clock, Reset )
153
  begin
154
    if( Reset = Reset_Level )then
155 217 jshamlet
      Baud_Cntr              <= (others => '0');
156
      Baud_Tick              <= '0';
157 191 jshamlet
    elsif( rising_edge( Clock ) )then
158 217 jshamlet
      Baud_Cntr              <= Baud_Cntr - 1;
159
      Baud_Tick              <= nor_reduce(Baud_Cntr);
160 191 jshamlet
      if( Baud_Cntr = 0 )then
161 217 jshamlet
        Baud_Cntr            <= BAUD_DLY;
162 191 jshamlet
      end if;
163
    end if;
164
  end process;
165
 
166
  io_FSM: process( Clock, Reset )
167
  begin
168
    if( Reset = Reset_Level )then
169 217 jshamlet
      io_state               <= IDLE;
170
      bit_cntr               <= (others => '0');
171
      tx_buffer              <= (others => '0');
172
      TX_Idle                <= '0';
173 191 jshamlet
 
174 217 jshamlet
      Mx_Clock               <= '0';
175
      Mx_Data                <= '0';
176
      MX_LDCSn               <= '0';
177 191 jshamlet
 
178
    elsif( rising_edge(Clock) )then
179
 
180 217 jshamlet
      TX_Idle                <= '0';
181
      Mx_Clock               <= '0';
182 191 jshamlet
 
183
      case( io_state )is
184
        when IDLE =>
185 217 jshamlet
          Mx_Data            <= '0';
186
          MX_LDCSn           <= '1';
187
          TX_Idle            <= '1';
188 191 jshamlet
          if( TX_En = '1' )then
189 217 jshamlet
            tx_buffer        <= "0000" & FIFO_Rd_Data;
190
            bit_cntr         <= (others => '1');
191
            io_state         <= SYNC_CLK;
192 191 jshamlet
          end if;
193
 
194
        when SYNC_CLK =>
195
          if( Baud_Tick = '1' )then
196 217 jshamlet
            io_state         <= SCLK_L;
197 191 jshamlet
          end if;
198
 
199
        when SCLK_L =>
200 217 jshamlet
          MX_LDCSn           <= '0';
201
          Mx_Data            <= tx_buffer(conv_integer(bit_cntr));
202 191 jshamlet
          if( Baud_Tick = '1' )then
203 217 jshamlet
            io_state         <= SCLK_H;
204 191 jshamlet
          end if;
205
 
206
        when SCLK_H =>
207 217 jshamlet
          Mx_Clock           <= '1';
208 191 jshamlet
          if( Baud_Tick = '1' )then
209 217 jshamlet
            bit_cntr         <= bit_cntr - 1;
210
            io_state         <= ADV_BIT;
211 191 jshamlet
          end if;
212
 
213
        when ADV_BIT =>
214 217 jshamlet
          io_state           <= SCLK_L;
215 191 jshamlet
          if( and_reduce(bit_cntr) = '1' )then
216 217 jshamlet
            io_state         <= DONE;
217 191 jshamlet
          end if;
218
 
219
        when DONE =>
220 217 jshamlet
          Mx_Data            <= '0';
221 191 jshamlet
          if( Baud_Tick = '1' )then
222 217 jshamlet
            io_state         <= IDLE;
223 191 jshamlet
          end if;
224
 
225
        when others => null;
226
      end case;
227
    end if;
228
  end process;
229
 
230
end architecture;

powered by: WebSVN 2.1.0

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