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 200

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

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

powered by: WebSVN 2.1.0

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