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

Subversion Repositories neorv32

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

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
    data_i      : in  std_ulogic_vector(31 downto 0); -- data in
53
    data_o      : out std_ulogic_vector(31 downto 0); -- data out
54
    ack_o       : out std_ulogic; -- transfer acknowledge
55
    -- clock generator --
56
    clkgen_en_o : out std_ulogic; -- enable clock generator
57
    clkgen_i    : in  std_ulogic_vector(07 downto 0);
58
    -- pwm output channels --
59
    pwm_o       : out std_ulogic_vector(03 downto 0)
60
  );
61
end neorv32_pwm;
62
 
63
architecture neorv32_pwm_rtl of neorv32_pwm is
64
 
65
  -- internal configuration --
66
  constant num_pwm_channels_c : natural := 4; -- number of PWM channels, fixed!
67
 
68
  -- IO space: module base address --
69
  constant hi_abb_c : natural := index_size_f(io_size_c)-1; -- high address boundary bit
70
  constant lo_abb_c : natural := index_size_f(pwm_size_c); -- low address boundary bit
71
 
72
  -- Control register bits --
73
  constant ctrl_enable_c    : natural := 0; -- r/w: PWM enable
74
  constant ctrl_prsc0_bit_c : natural := 1; -- r/w: prescaler select bit 0
75
  constant ctrl_prsc1_bit_c : natural := 2; -- r/w: prescaler select bit 1
76
  constant ctrl_prsc2_bit_c : natural := 3; -- r/w: prescaler select bit 2
77
 
78
  -- access control --
79
  signal acc_en : std_ulogic; -- module access enable
80
  signal addr   : std_ulogic_vector(31 downto 0); -- access address
81
  signal wren   : std_ulogic; -- write enable
82
  signal rden   : std_ulogic; -- read enable
83
 
84
  -- accessible regs --
85
  type pwm_ch_t is array (0 to num_pwm_channels_c-1) of std_ulogic_vector(7 downto 0);
86
  signal pwm_ch : pwm_ch_t; -- duty cycle (r/w)
87
  signal enable : std_ulogic; -- enable unit (r/w)
88
  signal prsc   : std_ulogic_vector(2 downto 0); -- clock prescaler (r/w)
89
 
90
  -- prescaler clock generator --
91
  signal prsc_tick : std_ulogic;
92
 
93
  -- pwm counter --
94
  signal pwm_cnt : std_ulogic_vector(7 downto 0);
95
  signal pwm_out : std_ulogic_vector(3 downto 0);
96
 
97
begin
98
 
99
  -- Access Control -------------------------------------------------------------------------
100
  -- -------------------------------------------------------------------------------------------
101
  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';
102
  addr   <= pwm_base_c(31 downto lo_abb_c) & addr_i(lo_abb_c-1 downto 2) & "00"; -- word aligned
103
  rden   <= acc_en and rden_i;
104
  wren   <= acc_en and wren_i;
105
 
106
 
107
  -- Write access ---------------------------------------------------------------------------
108
  -- -------------------------------------------------------------------------------------------
109
  wr_access: process(clk_i)
110
  begin
111
    if rising_edge(clk_i) then
112
      ack_o <= acc_en and (rden_i or wren_i);
113
      -- write access --
114
      if (wren = '1') then
115
        if (addr = pwm_ctrl_addr_c) then -- control register
116 22 zero_gravi
          enable <= data_i(ctrl_enable_c);
117
          prsc   <= data_i(ctrl_prsc2_bit_c downto ctrl_prsc0_bit_c);
118 2 zero_gravi
        end if;
119
        if (addr = pwm_duty_addr_c) then -- duty cycle register
120
          for i in 0 to 3 loop
121 22 zero_gravi
            pwm_ch(i) <= data_i(7+i*8 downto 0+i*8);
122 2 zero_gravi
          end loop;
123
        end if;
124
      end if;
125
      -- read access --
126
      data_o <= (others => '0');
127
      if (rden = '1') then
128
        if (addr = pwm_ctrl_addr_c) then
129
          data_o(ctrl_enable_c) <= enable;
130
          data_o(ctrl_prsc2_bit_c downto ctrl_prsc0_bit_c) <= prsc;
131
        else -- pwm_duty_addr_c
132
          data_o(07 downto 00) <= pwm_ch(0);
133
          data_o(15 downto 08) <= pwm_ch(1);
134
          data_o(23 downto 16) <= pwm_ch(2);
135
          data_o(31 downto 24) <= pwm_ch(3);
136
        end if;
137
      end if;
138
    end if;
139
  end process wr_access;
140
 
141
  -- PWM clock select --
142
  clkgen_en_o <= enable; -- enable clock generator
143
  prsc_tick   <= clkgen_i(to_integer(unsigned(prsc)));
144
 
145
 
146
  -- PWM Core -------------------------------------------------------------------------------
147
  -- -------------------------------------------------------------------------------------------
148
  pwm_core: process(clk_i)
149
  begin
150
    if rising_edge(clk_i) then
151
      -- pwm counter --
152
      if (enable = '0') then
153
        pwm_cnt <= (others => '0');
154
      elsif (prsc_tick = '1') then
155
        pwm_cnt <= std_ulogic_vector(unsigned(pwm_cnt) + 1);
156
      end if;
157
      -- channels --
158
      for i in 0 to num_pwm_channels_c-1 loop
159
        if (unsigned(pwm_cnt) >= unsigned(pwm_ch(i))) or (enable = '0') then
160
          pwm_out(i) <= '0';
161
        else
162
          pwm_out(i) <= '1';
163
        end if;
164
      end loop; -- i, pwm channel
165
    end if;
166
  end process pwm_core;
167
 
168
  -- output --
169
  pwm_o(0) <= pwm_out(0);
170
  pwm_o(1) <= pwm_out(1);
171
  pwm_o(2) <= pwm_out(2);
172
  pwm_o(3) <= pwm_out(3);
173
 
174
 
175
end neorv32_pwm_rtl;

powered by: WebSVN 2.1.0

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