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 224

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 224 jshamlet
-- Seth Henry      04/16/20 Modified to use Open8 bus record
33 194 jshamlet
 
34 191 jshamlet
library ieee;
35
use ieee.std_logic_1164.all;
36
use ieee.std_logic_unsigned.all;
37
use ieee.std_logic_arith.all;
38
use ieee.std_logic_misc.all;
39
 
40
library work;
41
  use work.open8_pkg.all;
42
 
43
entity o8_max7221 is
44
generic(
45 224 jshamlet
  Bitclock_Frequency         : real := 5000000.0;
46
  Clock_Frequency            : real;
47 217 jshamlet
  Address                    : ADDRESS_TYPE
48 191 jshamlet
);
49
port(
50 223 jshamlet
  Open8_Bus                  : in  OPEN8_BUS_TYPE;
51 191 jshamlet
  --
52 217 jshamlet
  Mx_Data                    : out std_logic;
53
  Mx_Clock                   : out std_logic;
54
  MX_LDCSn                   : out std_logic
55 191 jshamlet
);
56
end entity;
57
 
58
architecture behave of o8_max7221 is
59
 
60 224 jshamlet
  alias Clock                is Open8_Bus.Clock;
61
  alias Reset                is Open8_Bus.Reset;
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 224 jshamlet
  constant BAUD_DLY_RATIO    : real := (Clock_Frequency / Bitclock_Frequency);
83
  constant BAUD_DLY_VAL      : integer := integer(BAUD_DLY_RATIO * 0.5);
84 217 jshamlet
  constant BAUD_DLY_WDT      : integer := ceil_log2(BAUD_DLY_VAL - 1);
85
  constant BAUD_DLY          : std_logic_vector :=
86
                         conv_std_logic_vector(BAUD_DLY_VAL - 1, BAUD_DLY_WDT);
87 191 jshamlet
 
88 217 jshamlet
  signal Baud_Cntr           : std_logic_vector( BAUD_DLY_WDT - 1 downto 0 )
89
                               := (others => '0');
90
  signal Baud_Tick           : std_logic;
91 191 jshamlet
 
92
  type IO_STATES is ( IDLE, SYNC_CLK, SCLK_L, SCLK_H, ADV_BIT, DONE );
93 217 jshamlet
  signal io_state            : IO_STATES;
94
  signal bit_cntr            : std_logic_vector(3 downto 0);
95
  signal tx_buffer           : std_logic_vector(15 downto 0);
96 191 jshamlet
 
97
begin
98
 
99 223 jshamlet
  FIFO_Wr_En                 <= Open8_Bus.Wr_En when Comp_Addr = User_Addr else
100
                                '0';
101
 
102
  FIFO_Wr_Data               <= Open8_Bus.Address(3 downto 0) &
103
                                Open8_Bus.Wr_Data;
104
 
105 217 jshamlet
  FIFO_Reset                 <= Reset when Reset_Level = '1' else (not Reset);
106 191 jshamlet
 
107
  U_FIFO : entity work.o8_max7221_fifo
108
  port map(
109 217 jshamlet
    aclr                     => FIFO_Reset,
110
    clock                    => Clock,
111
    data                     => FIFO_Wr_Data,
112
    rdreq                    => FIFO_Rd_En,
113
    wrreq                    => FIFO_Wr_En,
114
    empty                    => FIFO_Empty,
115
    q                        => FIFO_Rd_Data
116 191 jshamlet
  );
117
 
118
  tx_FSM: process( Clock, Reset )
119
  begin
120
    if( Reset = Reset_Level )then
121 217 jshamlet
      TX_Ctrl                <= IDLE;
122
      TX_En                  <= '0';
123
      FIFO_Rd_En             <= '0';
124 191 jshamlet
    elsif( rising_edge(Clock) )then
125 217 jshamlet
      TX_En                  <= '0';
126
      FIFO_Rd_En             <= '0';
127 191 jshamlet
 
128
      case( TX_Ctrl )is
129
        when IDLE =>
130
          if( FIFO_Empty = '0' )then
131 217 jshamlet
            FIFO_Rd_En       <= '1';
132
            TX_Ctrl          <= TX_BYTE;
133 191 jshamlet
          end if;
134
 
135
        when TX_BYTE =>
136 217 jshamlet
          TX_En              <= '1';
137
          TX_Ctrl            <= TX_START;
138 191 jshamlet
 
139
        when TX_START =>
140
          if( TX_Idle = '0' )then
141 217 jshamlet
            TX_Ctrl          <= TX_WAIT;
142 191 jshamlet
          end if;
143
 
144
        when TX_WAIT =>
145
          if( TX_Idle = '1' )then
146 217 jshamlet
            TX_Ctrl          <= IDLE;
147 191 jshamlet
          end if;
148
 
149
        when others => null;
150
      end case;
151
 
152
    end if;
153
  end process;
154
 
155
  Baud_Rate_proc: process( Clock, Reset )
156
  begin
157
    if( Reset = Reset_Level )then
158 217 jshamlet
      Baud_Cntr              <= (others => '0');
159
      Baud_Tick              <= '0';
160 191 jshamlet
    elsif( rising_edge( Clock ) )then
161 217 jshamlet
      Baud_Cntr              <= Baud_Cntr - 1;
162
      Baud_Tick              <= nor_reduce(Baud_Cntr);
163 191 jshamlet
      if( Baud_Cntr = 0 )then
164 217 jshamlet
        Baud_Cntr            <= BAUD_DLY;
165 191 jshamlet
      end if;
166
    end if;
167
  end process;
168
 
169
  io_FSM: process( Clock, Reset )
170
  begin
171
    if( Reset = Reset_Level )then
172 217 jshamlet
      io_state               <= IDLE;
173
      bit_cntr               <= (others => '0');
174
      tx_buffer              <= (others => '0');
175
      TX_Idle                <= '0';
176 191 jshamlet
 
177 217 jshamlet
      Mx_Clock               <= '0';
178
      Mx_Data                <= '0';
179
      MX_LDCSn               <= '0';
180 191 jshamlet
 
181
    elsif( rising_edge(Clock) )then
182
 
183 217 jshamlet
      TX_Idle                <= '0';
184
      Mx_Clock               <= '0';
185 191 jshamlet
 
186
      case( io_state )is
187
        when IDLE =>
188 217 jshamlet
          Mx_Data            <= '0';
189
          MX_LDCSn           <= '1';
190
          TX_Idle            <= '1';
191 191 jshamlet
          if( TX_En = '1' )then
192 217 jshamlet
            tx_buffer        <= "0000" & FIFO_Rd_Data;
193
            bit_cntr         <= (others => '1');
194
            io_state         <= SYNC_CLK;
195 191 jshamlet
          end if;
196
 
197
        when SYNC_CLK =>
198
          if( Baud_Tick = '1' )then
199 217 jshamlet
            io_state         <= SCLK_L;
200 191 jshamlet
          end if;
201
 
202
        when SCLK_L =>
203 217 jshamlet
          MX_LDCSn           <= '0';
204
          Mx_Data            <= tx_buffer(conv_integer(bit_cntr));
205 191 jshamlet
          if( Baud_Tick = '1' )then
206 217 jshamlet
            io_state         <= SCLK_H;
207 191 jshamlet
          end if;
208
 
209
        when SCLK_H =>
210 217 jshamlet
          Mx_Clock           <= '1';
211 191 jshamlet
          if( Baud_Tick = '1' )then
212 217 jshamlet
            bit_cntr         <= bit_cntr - 1;
213
            io_state         <= ADV_BIT;
214 191 jshamlet
          end if;
215
 
216
        when ADV_BIT =>
217 217 jshamlet
          io_state           <= SCLK_L;
218 191 jshamlet
          if( and_reduce(bit_cntr) = '1' )then
219 217 jshamlet
            io_state         <= DONE;
220 191 jshamlet
          end if;
221
 
222
        when DONE =>
223 217 jshamlet
          Mx_Data            <= '0';
224 191 jshamlet
          if( Baud_Tick = '1' )then
225 217 jshamlet
            io_state         <= IDLE;
226 191 jshamlet
          end if;
227
 
228
        when others => null;
229
      end case;
230
    end if;
231
  end process;
232
 
233
end architecture;

powered by: WebSVN 2.1.0

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