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 242

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 217 jshamlet
  Rd_Data                    : out DATA_TYPE;
60 223 jshamlet
  Interrupt                  : out std_logic;
61
  --
62
  PWM_Out                    : out std_logic
63 214 jshamlet
);
64
end entity;
65
 
66
architecture behave of o8_pwm16 is
67
 
68 224 jshamlet
  alias Clock                is Open8_Bus.Clock;
69
  alias Reset                is Open8_Bus.Reset;
70
  alias uSec_Tick            is Open8_Bus.uSec_Tick;
71
 
72 217 jshamlet
  constant User_Addr         : std_logic_vector(15 downto 3) :=
73
                                Address(15 downto 3);
74 214 jshamlet
 
75 223 jshamlet
  alias  Comp_Addr           is Open8_Bus.Address(15 downto 3);
76 217 jshamlet
  signal Addr_Match          : std_logic := '0';
77 214 jshamlet
 
78 223 jshamlet
  alias  Reg_Addr            is Open8_Bus.Address(2 downto 0);
79 217 jshamlet
  signal Reg_Addr_q          : std_logic_vector(2 downto 0) := (others => '0');
80 214 jshamlet
 
81 217 jshamlet
  signal Wr_En               : std_logic := '0';
82
  signal Wr_Data_q           : DATA_TYPE := x"00";
83
  signal Rd_En               : std_logic := '0';
84 214 jshamlet
 
85 217 jshamlet
  signal PWM_Enable          : std_logic := '0';
86
  signal PWM_Period          : std_logic_vector(15 downto 0) := (others => '0');
87
  alias  PWM_Period_l        is PWM_Period(7 downto 0);
88
  alias  PWM_Period_u        is PWM_Period(15 downto 8);
89 214 jshamlet
 
90 217 jshamlet
  signal PWM_Width           : std_logic_vector(15 downto 0) := (others => '0');
91
  alias  PWM_Width_l         is PWM_Width(7 downto 0);
92
  alias  PWM_Width_u         is PWM_Width(15 downto 8);
93 214 jshamlet
 
94 217 jshamlet
  signal Period_Ctr          : std_logic_vector(15 downto 0) := (others => '0');
95
  signal Width_Ctr           : std_logic_vector(15 downto 0) := (others => '0');
96 214 jshamlet
 
97
begin
98
 
99 217 jshamlet
  Addr_Match                 <= '1' when Comp_Addr = User_Addr else '0';
100 214 jshamlet
 
101
  PWM_proc: process( Clock, Reset )
102
  begin
103
    if( Reset = Reset_Level )then
104 217 jshamlet
      Wr_Data_q              <= (others => '0');
105
      Reg_Addr_q             <= (others => '0');
106
      Wr_En                  <= '0';
107
      Rd_En                  <= '0';
108
      Rd_Data                <= x"00";
109
      Interrupt              <= '0';
110 214 jshamlet
 
111 217 jshamlet
      PWM_Enable             <= '0';
112
      PWM_Period             <= (others => '0');
113
      PWM_Width              <= (others => '0');
114 214 jshamlet
 
115 217 jshamlet
      Period_Ctr             <= (others => '0');
116
      Width_Ctr              <= (others => '0');
117
      PWM_Out                <= '0';
118 214 jshamlet
    elsif( rising_edge(Clock) )then
119 217 jshamlet
      Reg_Addr_q             <= Reg_Addr;
120 223 jshamlet
      Wr_Data_q              <= Open8_Bus.Wr_Data;
121
      Wr_En                  <= Addr_Match and Open8_Bus.Wr_En;
122 214 jshamlet
 
123
      if( Wr_En = '1' )then
124
        case( Reg_Addr_q )is
125
          when "000" =>
126 217 jshamlet
            PWM_Period_l     <= Wr_Data_q;
127 214 jshamlet
          when "001" =>
128 217 jshamlet
            PWM_Period_u     <= Wr_Data_q;
129 214 jshamlet
          when "010" =>
130 217 jshamlet
            PWM_Width_l      <= Wr_Data_q;
131 214 jshamlet
          when "011" =>
132 217 jshamlet
            PWM_Width_u      <= Wr_Data_q;
133 214 jshamlet
          when "100" | "101" | "110" | "111" =>
134 217 jshamlet
            PWM_Enable       <= Wr_Data_q(7);
135 214 jshamlet
          when others => null;
136
        end case;
137
      end if;
138
 
139 217 jshamlet
      Rd_Data                <= (others => '0');
140 223 jshamlet
      Rd_En                  <= Addr_Match and Open8_Bus.Rd_En;
141 214 jshamlet
      if( Rd_En = '1' )then
142
        case( Reg_Addr_q )is
143
          when "000" =>
144 217 jshamlet
            Rd_Data          <= PWM_Period_l;
145 214 jshamlet
          when "001" =>
146 217 jshamlet
            Rd_Data          <= PWM_Period_u;
147 214 jshamlet
          when "010" =>
148 217 jshamlet
            Rd_Data          <= PWM_Width_l;
149 214 jshamlet
          when "011" =>
150 217 jshamlet
            Rd_Data          <= PWM_Width_u;
151 214 jshamlet
          when "100" | "101" | "110" | "111" =>
152 217 jshamlet
            Rd_Data          <= PWM_Enable & "0000000";
153 214 jshamlet
          when others => null;
154
        end case;
155
      end if;
156
 
157 217 jshamlet
      Interrupt              <= '0';
158
      Period_Ctr             <= Period_Ctr - uSec_tick;
159
      Width_Ctr              <= Width_Ctr - uSec_tick;
160 214 jshamlet
 
161
      -- Stop the width counter from rolling over at 0
162
      if( or_reduce(Width_Ctr) = '0' )then
163 217 jshamlet
        Width_Ctr            <= (others => '0');
164 214 jshamlet
      end if;
165 217 jshamlet
 
166 214 jshamlet
      -- Reload both counters when period reaches 0
167
      if( or_reduce(Period_Ctr) = '0' )then
168 217 jshamlet
        Period_Ctr           <= PWM_Period;
169
        Width_Ctr            <= PWM_Width;
170
        Interrupt            <= '1';
171 214 jshamlet
      end if;
172
 
173
      -- Drive the output high as long as Width > 0 and PWM_Enable is high
174 217 jshamlet
      PWM_Out                <= or_reduce(Width_Ctr) and PWM_Enable;
175 214 jshamlet
 
176
      -- If the counter is disabled, reload the counters, and drive the output
177
      --  low.
178
      if( PWM_Enable = '0' )then
179 217 jshamlet
        Period_Ctr           <= PWM_Period;
180
        Width_Ctr            <= PWM_Width;
181
        Interrupt            <= '0';
182 214 jshamlet
      end if;
183
 
184
    end if;
185
  end process;
186
 
187
end architecture;

powered by: WebSVN 2.1.0

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