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 213

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

powered by: WebSVN 2.1.0

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