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

Subversion Repositories open8_urisc

[/] [open8_urisc/] [trunk/] [VHDL/] [o8_pwm16.vhd] - Blame information for rev 317

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

Line No. Rev Author Line
1 214 jshamlet
-- Copyright (c)2018, 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_pwm16
25
-- Description:  Provides a 16-bit standard PWM output with 1 uSec resolution,
26
--                as well as CPU interrupt on overflow. Note that the PWM
27
--                timers reload from registers on overflow, not on write
28
--
29
-- Register Map:
30
-- Offset  Bitfield Description                        Read/Write
31
--   0x00  AAAAAAAA Period (lower byte)                  (RW)
32
--   0x01  AAAAAAAA Period (upper byte)                  (RW)
33
--   0x02  AAAAAAAA Width (lower byte)                   (RW)
34
--   0x03  AAAAAAAA Width (upper byte)                   (RW)
35
--   0x04  A------- Timer Status                         (RW)
36
--                  A: Enabled on '1' / Disable on '0'
37
--
38
-- Revision History
39
-- Author          Date     Change
40
------------------ -------- ---------------------------------------------------
41
-- Seth Henry      04/25/18 Design Start
42
-- Seth Henry      04/10/20 Code cleanup and comments
43 224 jshamlet
-- Seth Henry      04/16/20 Modified to use Open8 bus record
44 214 jshamlet
 
45
library ieee;
46
  use ieee.std_logic_1164.all;
47
  use ieee.std_logic_unsigned.all;
48
  use ieee.std_logic_misc.all;
49
 
50
library work;
51
  use work.open8_pkg.all;
52
 
53
entity o8_pwm16 is
54
generic(
55 217 jshamlet
  Address                    : ADDRESS_TYPE
56 214 jshamlet
);
57
port(
58 223 jshamlet
  Open8_Bus                  : in  OPEN8_BUS_TYPE;
59 244 jshamlet
  Write_Qual                 : in  std_logic := '1';
60 217 jshamlet
  Rd_Data                    : out DATA_TYPE;
61 223 jshamlet
  Interrupt                  : out std_logic;
62
  --
63
  PWM_Out                    : out std_logic
64 214 jshamlet
);
65
end entity;
66
 
67
architecture behave of o8_pwm16 is
68
 
69 224 jshamlet
  alias Clock                is Open8_Bus.Clock;
70
  alias Reset                is Open8_Bus.Reset;
71
  alias uSec_Tick            is Open8_Bus.uSec_Tick;
72
 
73 217 jshamlet
  constant User_Addr         : std_logic_vector(15 downto 3) :=
74
                                Address(15 downto 3);
75 214 jshamlet
 
76 223 jshamlet
  alias  Comp_Addr           is Open8_Bus.Address(15 downto 3);
77 217 jshamlet
  signal Addr_Match          : std_logic := '0';
78 214 jshamlet
 
79 244 jshamlet
  alias  Reg_Sel_d           is Open8_Bus.Address(2 downto 0);
80
  signal Reg_Sel_q           : std_logic_vector(2 downto 0) := (others => '0');
81
  signal Wr_En_d             : std_logic := '0';
82
  signal Wr_En_q             : std_logic := '0';
83
  alias  Wr_Data_d           is Open8_Bus.Wr_Data;
84 217 jshamlet
  signal Wr_Data_q           : DATA_TYPE := x"00";
85 244 jshamlet
  signal Rd_En_d             : std_logic := '0';
86
  signal Rd_En_q             : std_logic := '0';
87 214 jshamlet
 
88 217 jshamlet
  signal PWM_Enable          : std_logic := '0';
89
  signal PWM_Period          : std_logic_vector(15 downto 0) := (others => '0');
90
  alias  PWM_Period_l        is PWM_Period(7 downto 0);
91
  alias  PWM_Period_u        is PWM_Period(15 downto 8);
92 214 jshamlet
 
93 217 jshamlet
  signal PWM_Width           : std_logic_vector(15 downto 0) := (others => '0');
94
  alias  PWM_Width_l         is PWM_Width(7 downto 0);
95
  alias  PWM_Width_u         is PWM_Width(15 downto 8);
96 214 jshamlet
 
97 217 jshamlet
  signal Period_Ctr          : std_logic_vector(15 downto 0) := (others => '0');
98
  signal Width_Ctr           : std_logic_vector(15 downto 0) := (others => '0');
99 214 jshamlet
 
100
begin
101
 
102 217 jshamlet
  Addr_Match                 <= '1' when Comp_Addr = User_Addr else '0';
103 244 jshamlet
  Wr_En_d                    <= Addr_Match and Open8_Bus.Wr_En and Write_Qual;
104
  Rd_En_d                    <= Addr_Match and Open8_Bus.Rd_En;
105 214 jshamlet
 
106
  PWM_proc: process( Clock, Reset )
107
  begin
108
    if( Reset = Reset_Level )then
109 244 jshamlet
      Reg_Sel_q              <= "000";
110
      Wr_En_q                <= '0';
111
      Wr_Data_q              <= x"00";
112
      Rd_En_q                <= '0';
113
      Rd_Data                <= OPEN8_NULLBUS;
114
 
115 217 jshamlet
      Interrupt              <= '0';
116 214 jshamlet
 
117 217 jshamlet
      PWM_Enable             <= '0';
118
      PWM_Period             <= (others => '0');
119
      PWM_Width              <= (others => '0');
120 214 jshamlet
 
121 217 jshamlet
      Period_Ctr             <= (others => '0');
122
      Width_Ctr              <= (others => '0');
123
      PWM_Out                <= '0';
124 214 jshamlet
    elsif( rising_edge(Clock) )then
125 244 jshamlet
      Reg_Sel_q              <= Reg_Sel_d;
126 214 jshamlet
 
127 244 jshamlet
      Wr_En_q                <= Wr_En_d;
128
      Wr_Data_q              <= Wr_Data_d;
129
      if( Wr_En_q = '1' )then
130
        case( Reg_Sel_q )is
131 214 jshamlet
          when "000" =>
132 217 jshamlet
            PWM_Period_l     <= Wr_Data_q;
133 214 jshamlet
          when "001" =>
134 217 jshamlet
            PWM_Period_u     <= Wr_Data_q;
135 214 jshamlet
          when "010" =>
136 217 jshamlet
            PWM_Width_l      <= Wr_Data_q;
137 214 jshamlet
          when "011" =>
138 217 jshamlet
            PWM_Width_u      <= Wr_Data_q;
139 214 jshamlet
          when "100" | "101" | "110" | "111" =>
140 217 jshamlet
            PWM_Enable       <= Wr_Data_q(7);
141 214 jshamlet
          when others => null;
142
        end case;
143
      end if;
144
 
145 244 jshamlet
      Rd_En_q                <= Rd_En_d;
146
      Rd_Data                <= OPEN8_NULLBUS;
147
      if( Rd_En_q = '1' )then
148
        case( Reg_Sel_q )is
149 214 jshamlet
          when "000" =>
150 217 jshamlet
            Rd_Data          <= PWM_Period_l;
151 214 jshamlet
          when "001" =>
152 217 jshamlet
            Rd_Data          <= PWM_Period_u;
153 214 jshamlet
          when "010" =>
154 217 jshamlet
            Rd_Data          <= PWM_Width_l;
155 214 jshamlet
          when "011" =>
156 217 jshamlet
            Rd_Data          <= PWM_Width_u;
157 214 jshamlet
          when "100" | "101" | "110" | "111" =>
158 217 jshamlet
            Rd_Data          <= PWM_Enable & "0000000";
159 214 jshamlet
          when others => null;
160
        end case;
161
      end if;
162
 
163 217 jshamlet
      Interrupt              <= '0';
164
      Period_Ctr             <= Period_Ctr - uSec_tick;
165
      Width_Ctr              <= Width_Ctr - uSec_tick;
166 214 jshamlet
 
167
      -- Stop the width counter from rolling over at 0
168
      if( or_reduce(Width_Ctr) = '0' )then
169 217 jshamlet
        Width_Ctr            <= (others => '0');
170 214 jshamlet
      end if;
171 217 jshamlet
 
172 214 jshamlet
      -- Reload both counters when period reaches 0
173
      if( or_reduce(Period_Ctr) = '0' )then
174 217 jshamlet
        Period_Ctr           <= PWM_Period;
175
        Width_Ctr            <= PWM_Width;
176
        Interrupt            <= '1';
177 214 jshamlet
      end if;
178
 
179
      -- Drive the output high as long as Width > 0 and PWM_Enable is high
180 217 jshamlet
      PWM_Out                <= or_reduce(Width_Ctr) and PWM_Enable;
181 214 jshamlet
 
182
      -- If the counter is disabled, reload the counters, and drive the output
183
      --  low.
184
      if( PWM_Enable = '0' )then
185 217 jshamlet
        Period_Ctr           <= PWM_Period;
186
        Width_Ctr            <= PWM_Width;
187
        Interrupt            <= '0';
188 214 jshamlet
      end if;
189
 
190
    end if;
191
  end process;
192
 
193
end architecture;

powered by: WebSVN 2.1.0

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