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

Subversion Repositories neorv32

[/] [neorv32/] [trunk/] [rtl/] [core/] [neorv32_pwm.vhd] - Blame information for rev 2

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

Line No. Rev Author Line
1 2 zero_gravi
-- #################################################################################################
2
-- # << NEORV32 - Pulse Width Modulation Controller (PWM) >>                                       #
3
-- # ********************************************************************************************* #
4
-- # Simple 4-channel PWM controller with 8 bit resolution for the duty cycle and programmable     #
5
-- # clock.                                                                                        #
6
-- # ********************************************************************************************* #
7
-- # BSD 3-Clause License                                                                          #
8
-- #                                                                                               #
9
-- # Copyright (c) 2020, Stephan Nolting. All rights reserved.                                     #
10
-- #                                                                                               #
11
-- # Redistribution and use in source and binary forms, with or without modification, are          #
12
-- # permitted provided that the following conditions are met:                                     #
13
-- #                                                                                               #
14
-- # 1. Redistributions of source code must retain the above copyright notice, this list of        #
15
-- #    conditions and the following disclaimer.                                                   #
16
-- #                                                                                               #
17
-- # 2. Redistributions in binary form must reproduce the above copyright notice, this list of     #
18
-- #    conditions and the following disclaimer in the documentation and/or other materials        #
19
-- #    provided with the distribution.                                                            #
20
-- #                                                                                               #
21
-- # 3. Neither the name of the copyright holder nor the names of its contributors may be used to  #
22
-- #    endorse or promote products derived from this software without specific prior written      #
23
-- #    permission.                                                                                #
24
-- #                                                                                               #
25
-- # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS   #
26
-- # OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF               #
27
-- # MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE    #
28
-- # COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,     #
29
-- # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE #
30
-- # GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED    #
31
-- # AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING     #
32
-- # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED  #
33
-- # OF THE POSSIBILITY OF SUCH DAMAGE.                                                            #
34
-- # ********************************************************************************************* #
35
-- # The NEORV32 Processor - https://github.com/stnolting/neorv32              (c) Stephan Nolting #
36
-- #################################################################################################
37
 
38
library ieee;
39
use ieee.std_logic_1164.all;
40
use ieee.numeric_std.all;
41
 
42
library neorv32;
43
use neorv32.neorv32_package.all;
44
 
45
entity neorv32_pwm is
46
  port (
47
    -- host access --
48
    clk_i       : in  std_ulogic; -- global clock line
49
    addr_i      : in  std_ulogic_vector(31 downto 0); -- address
50
    rden_i      : in  std_ulogic; -- read enable
51
    wren_i      : in  std_ulogic; -- write enable
52
    ben_i       : in  std_ulogic_vector(03 downto 0); -- byte write enable
53
    data_i      : in  std_ulogic_vector(31 downto 0); -- data in
54
    data_o      : out std_ulogic_vector(31 downto 0); -- data out
55
    ack_o       : out std_ulogic; -- transfer acknowledge
56
    -- clock generator --
57
    clkgen_en_o : out std_ulogic; -- enable clock generator
58
    clkgen_i    : in  std_ulogic_vector(07 downto 0);
59
    -- pwm output channels --
60
    pwm_o       : out std_ulogic_vector(03 downto 0)
61
  );
62
end neorv32_pwm;
63
 
64
architecture neorv32_pwm_rtl of neorv32_pwm is
65
 
66
  -- internal configuration --
67
  constant num_pwm_channels_c : natural := 4; -- number of PWM channels, fixed!
68
 
69
  -- IO space: module base address --
70
  constant hi_abb_c : natural := index_size_f(io_size_c)-1; -- high address boundary bit
71
  constant lo_abb_c : natural := index_size_f(pwm_size_c); -- low address boundary bit
72
 
73
  -- Control register bits --
74
  constant ctrl_enable_c    : natural := 0; -- r/w: PWM enable
75
  constant ctrl_prsc0_bit_c : natural := 1; -- r/w: prescaler select bit 0
76
  constant ctrl_prsc1_bit_c : natural := 2; -- r/w: prescaler select bit 1
77
  constant ctrl_prsc2_bit_c : natural := 3; -- r/w: prescaler select bit 2
78
 
79
  -- access control --
80
  signal acc_en : std_ulogic; -- module access enable
81
  signal addr   : std_ulogic_vector(31 downto 0); -- access address
82
  signal wren   : std_ulogic; -- write enable
83
  signal rden   : std_ulogic; -- read enable
84
 
85
  -- accessible regs --
86
  type pwm_ch_t is array (0 to num_pwm_channels_c-1) of std_ulogic_vector(7 downto 0);
87
  signal pwm_ch : pwm_ch_t; -- duty cycle (r/w)
88
  signal enable : std_ulogic; -- enable unit (r/w)
89
  signal prsc   : std_ulogic_vector(2 downto 0); -- clock prescaler (r/w)
90
 
91
  -- prescaler clock generator --
92
  signal prsc_tick : std_ulogic;
93
 
94
  -- pwm counter --
95
  signal pwm_cnt : std_ulogic_vector(7 downto 0);
96
  signal pwm_out : std_ulogic_vector(3 downto 0);
97
 
98
begin
99
 
100
  -- Access Control -------------------------------------------------------------------------
101
  -- -------------------------------------------------------------------------------------------
102
  acc_en <= '1' when (addr_i(hi_abb_c downto lo_abb_c) = pwm_base_c(hi_abb_c downto lo_abb_c)) else '0';
103
  addr   <= pwm_base_c(31 downto lo_abb_c) & addr_i(lo_abb_c-1 downto 2) & "00"; -- word aligned
104
  rden   <= acc_en and rden_i;
105
  wren   <= acc_en and wren_i;
106
 
107
 
108
  -- Write access ---------------------------------------------------------------------------
109
  -- -------------------------------------------------------------------------------------------
110
  wr_access: process(clk_i)
111
  begin
112
    if rising_edge(clk_i) then
113
      ack_o <= acc_en and (rden_i or wren_i);
114
      -- write access --
115
      if (wren = '1') then
116
        if (addr = pwm_ctrl_addr_c) then -- control register
117
          if (ben_i(0) = '1') then
118
            enable <= data_i(ctrl_enable_c);
119
            prsc   <= data_i(ctrl_prsc2_bit_c downto ctrl_prsc0_bit_c);
120
          end if;
121
        end if;
122
        if (addr = pwm_duty_addr_c) then -- duty cycle register
123
          for i in 0 to 3 loop
124
            if (ben_i(i) = '1') then
125
              pwm_ch(i) <= data_i(7+i*8 downto 0+i*8);
126
            end if;
127
          end loop;
128
        end if;
129
      end if;
130
      -- read access --
131
      data_o <= (others => '0');
132
      if (rden = '1') then
133
        if (addr = pwm_ctrl_addr_c) then
134
          data_o(ctrl_enable_c) <= enable;
135
          data_o(ctrl_prsc2_bit_c downto ctrl_prsc0_bit_c) <= prsc;
136
        else -- pwm_duty_addr_c
137
          data_o(07 downto 00) <= pwm_ch(0);
138
          data_o(15 downto 08) <= pwm_ch(1);
139
          data_o(23 downto 16) <= pwm_ch(2);
140
          data_o(31 downto 24) <= pwm_ch(3);
141
        end if;
142
      end if;
143
    end if;
144
  end process wr_access;
145
 
146
  -- PWM clock select --
147
  clkgen_en_o <= enable; -- enable clock generator
148
  prsc_tick   <= clkgen_i(to_integer(unsigned(prsc)));
149
 
150
 
151
  -- PWM Core -------------------------------------------------------------------------------
152
  -- -------------------------------------------------------------------------------------------
153
  pwm_core: process(clk_i)
154
  begin
155
    if rising_edge(clk_i) then
156
      -- pwm counter --
157
      if (enable = '0') then
158
        pwm_cnt <= (others => '0');
159
      elsif (prsc_tick = '1') then
160
        pwm_cnt <= std_ulogic_vector(unsigned(pwm_cnt) + 1);
161
      end if;
162
      -- channels --
163
      for i in 0 to num_pwm_channels_c-1 loop
164
        if (unsigned(pwm_cnt) >= unsigned(pwm_ch(i))) or (enable = '0') then
165
          pwm_out(i) <= '0';
166
        else
167
          pwm_out(i) <= '1';
168
        end if;
169
      end loop; -- i, pwm channel
170
    end if;
171
  end process pwm_core;
172
 
173
  -- output --
174
  pwm_o(0) <= pwm_out(0);
175
  pwm_o(1) <= pwm_out(1);
176
  pwm_o(2) <= pwm_out(2);
177
  pwm_o(3) <= pwm_out(3);
178
 
179
 
180
end neorv32_pwm_rtl;

powered by: WebSVN 2.1.0

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