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

Subversion Repositories neo430

[/] [neo430/] [trunk/] [neo430/] [rtl/] [core/] [neo430_pwm.vhd] - Blame information for rev 198

Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

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