1 |
2 |
zero_gravi |
-- #################################################################################################
|
2 |
|
|
-- # << NEORV32 - Pulse Width Modulation Controller (PWM) >> #
|
3 |
|
|
-- # ********************************************************************************************* #
|
4 |
60 |
zero_gravi |
-- # Simple PWM controller with 8 bit resolution for the duty cycle and programmable base #
|
5 |
|
|
-- # frequency. The controller supports up to 60 PWM channels. #
|
6 |
2 |
zero_gravi |
-- # ********************************************************************************************* #
|
7 |
|
|
-- # BSD 3-Clause License #
|
8 |
|
|
-- # #
|
9 |
60 |
zero_gravi |
-- # Copyright (c) 2021, Stephan Nolting. All rights reserved. #
|
10 |
2 |
zero_gravi |
-- # #
|
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 |
60 |
zero_gravi |
generic (
|
47 |
62 |
zero_gravi |
NUM_CHANNELS : natural -- number of PWM channels (0..60)
|
48 |
60 |
zero_gravi |
);
|
49 |
2 |
zero_gravi |
port (
|
50 |
|
|
-- host access --
|
51 |
|
|
clk_i : in std_ulogic; -- global clock line
|
52 |
|
|
addr_i : in std_ulogic_vector(31 downto 0); -- address
|
53 |
|
|
rden_i : in std_ulogic; -- read enable
|
54 |
|
|
wren_i : in std_ulogic; -- write enable
|
55 |
|
|
data_i : in std_ulogic_vector(31 downto 0); -- data in
|
56 |
|
|
data_o : out std_ulogic_vector(31 downto 0); -- data out
|
57 |
|
|
ack_o : out std_ulogic; -- transfer acknowledge
|
58 |
|
|
-- clock generator --
|
59 |
|
|
clkgen_en_o : out std_ulogic; -- enable clock generator
|
60 |
|
|
clkgen_i : in std_ulogic_vector(07 downto 0);
|
61 |
|
|
-- pwm output channels --
|
62 |
60 |
zero_gravi |
pwm_o : out std_ulogic_vector(NUM_CHANNELS-1 downto 0)
|
63 |
2 |
zero_gravi |
);
|
64 |
|
|
end neorv32_pwm;
|
65 |
|
|
|
66 |
|
|
architecture neorv32_pwm_rtl of neorv32_pwm is
|
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 |
60 |
zero_gravi |
type pwm_ch_t is array (0 to NUM_CHANNELS-1) of std_ulogic_vector(7 downto 0);
|
86 |
2 |
zero_gravi |
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 |
60 |
zero_gravi |
type pwm_ch_rd_t is array (0 to 60-1) of std_ulogic_vector(7 downto 0);
|
91 |
|
|
signal pwm_ch_rd : pwm_ch_rd_t; -- duty cycle read-back
|
92 |
|
|
|
93 |
2 |
zero_gravi |
-- prescaler clock generator --
|
94 |
|
|
signal prsc_tick : std_ulogic;
|
95 |
|
|
|
96 |
23 |
zero_gravi |
-- pwm core counter --
|
97 |
2 |
zero_gravi |
signal pwm_cnt : std_ulogic_vector(7 downto 0);
|
98 |
|
|
|
99 |
|
|
begin
|
100 |
|
|
|
101 |
60 |
zero_gravi |
-- Sanity Checks --------------------------------------------------------------------------
|
102 |
|
|
-- -------------------------------------------------------------------------------------------
|
103 |
|
|
assert not (NUM_CHANNELS > 60) report "NEORV32 PROCESSOR CONFIG ERROR! <IO.PWM> invalid number of channels! Has to be 0..60.!" severity error;
|
104 |
|
|
|
105 |
|
|
|
106 |
2 |
zero_gravi |
-- Access Control -------------------------------------------------------------------------
|
107 |
|
|
-- -------------------------------------------------------------------------------------------
|
108 |
|
|
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';
|
109 |
|
|
addr <= pwm_base_c(31 downto lo_abb_c) & addr_i(lo_abb_c-1 downto 2) & "00"; -- word aligned
|
110 |
|
|
rden <= acc_en and rden_i;
|
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 |
66 |
zero_gravi |
ack_o <= rden or wren;
|
120 |
60 |
zero_gravi |
|
121 |
2 |
zero_gravi |
-- write access --
|
122 |
|
|
if (wren = '1') then
|
123 |
60 |
zero_gravi |
-- control register --
|
124 |
|
|
if (addr = pwm_ctrl_addr_c) then
|
125 |
22 |
zero_gravi |
enable <= data_i(ctrl_enable_c);
|
126 |
|
|
prsc <= data_i(ctrl_prsc2_bit_c downto ctrl_prsc0_bit_c);
|
127 |
2 |
zero_gravi |
end if;
|
128 |
60 |
zero_gravi |
-- duty cycle registers --
|
129 |
|
|
for i in 0 to NUM_CHANNELS-1 loop -- channel loop
|
130 |
|
|
if (addr(5 downto 2) = std_ulogic_vector(to_unsigned((i/4)+1, 4))) then -- 4 channels per register; add ctrl reg offset
|
131 |
|
|
pwm_ch(i) <= data_i((i mod 4)*8+7 downto (i mod 4)*8+0);
|
132 |
|
|
end if;
|
133 |
|
|
end loop;
|
134 |
2 |
zero_gravi |
end if;
|
135 |
60 |
zero_gravi |
|
136 |
2 |
zero_gravi |
-- read access --
|
137 |
|
|
data_o <= (others => '0');
|
138 |
|
|
if (rden = '1') then
|
139 |
60 |
zero_gravi |
case addr(5 downto 2) is
|
140 |
|
|
when x"0" => data_o(ctrl_enable_c) <= enable; data_o(ctrl_prsc2_bit_c downto ctrl_prsc0_bit_c) <= prsc;
|
141 |
|
|
when x"1" => if (NUM_CHANNELS > 0) then data_o <= pwm_ch_rd(3) & pwm_ch_rd(2) & pwm_ch_rd(1) & pwm_ch_rd(0); else NULL; end if;
|
142 |
|
|
when x"2" => if (NUM_CHANNELS > 0) then data_o <= pwm_ch_rd(7) & pwm_ch_rd(6) & pwm_ch_rd(5) & pwm_ch_rd(4); else NULL; end if;
|
143 |
|
|
when x"3" => if (NUM_CHANNELS > 0) then data_o <= pwm_ch_rd(11) & pwm_ch_rd(10) & pwm_ch_rd(9) & pwm_ch_rd(8); else NULL; end if;
|
144 |
|
|
when x"4" => if (NUM_CHANNELS > 0) then data_o <= pwm_ch_rd(15) & pwm_ch_rd(14) & pwm_ch_rd(13) & pwm_ch_rd(12); else NULL; end if;
|
145 |
|
|
when x"5" => if (NUM_CHANNELS > 0) then data_o <= pwm_ch_rd(19) & pwm_ch_rd(18) & pwm_ch_rd(17) & pwm_ch_rd(16); else NULL; end if;
|
146 |
|
|
when x"6" => if (NUM_CHANNELS > 0) then data_o <= pwm_ch_rd(23) & pwm_ch_rd(22) & pwm_ch_rd(21) & pwm_ch_rd(20); else NULL; end if;
|
147 |
|
|
when x"7" => if (NUM_CHANNELS > 0) then data_o <= pwm_ch_rd(27) & pwm_ch_rd(26) & pwm_ch_rd(25) & pwm_ch_rd(24); else NULL; end if;
|
148 |
|
|
when x"8" => if (NUM_CHANNELS > 0) then data_o <= pwm_ch_rd(31) & pwm_ch_rd(30) & pwm_ch_rd(29) & pwm_ch_rd(28); else NULL; end if;
|
149 |
|
|
when x"9" => if (NUM_CHANNELS > 0) then data_o <= pwm_ch_rd(35) & pwm_ch_rd(34) & pwm_ch_rd(33) & pwm_ch_rd(32); else NULL; end if;
|
150 |
|
|
when x"a" => if (NUM_CHANNELS > 0) then data_o <= pwm_ch_rd(39) & pwm_ch_rd(38) & pwm_ch_rd(37) & pwm_ch_rd(36); else NULL; end if;
|
151 |
|
|
when x"b" => if (NUM_CHANNELS > 0) then data_o <= pwm_ch_rd(43) & pwm_ch_rd(42) & pwm_ch_rd(41) & pwm_ch_rd(40); else NULL; end if;
|
152 |
|
|
when x"c" => if (NUM_CHANNELS > 0) then data_o <= pwm_ch_rd(47) & pwm_ch_rd(46) & pwm_ch_rd(45) & pwm_ch_rd(44); else NULL; end if;
|
153 |
|
|
when x"d" => if (NUM_CHANNELS > 0) then data_o <= pwm_ch_rd(51) & pwm_ch_rd(50) & pwm_ch_rd(49) & pwm_ch_rd(48); else NULL; end if;
|
154 |
|
|
when x"e" => if (NUM_CHANNELS > 0) then data_o <= pwm_ch_rd(55) & pwm_ch_rd(54) & pwm_ch_rd(53) & pwm_ch_rd(52); else NULL; end if;
|
155 |
|
|
when x"f" => if (NUM_CHANNELS > 0) then data_o <= pwm_ch_rd(59) & pwm_ch_rd(58) & pwm_ch_rd(57) & pwm_ch_rd(56); else NULL; end if;
|
156 |
|
|
when others => NULL;
|
157 |
|
|
end case;
|
158 |
2 |
zero_gravi |
end if;
|
159 |
|
|
end if;
|
160 |
|
|
end process wr_access;
|
161 |
|
|
|
162 |
60 |
zero_gravi |
-- duty cycle read-back --
|
163 |
|
|
pwm_dc_rd_gen: process(pwm_ch)
|
164 |
|
|
begin
|
165 |
|
|
pwm_ch_rd <= (others => (others => '0'));
|
166 |
|
|
for i in 0 to NUM_CHANNELS-1 loop
|
167 |
|
|
pwm_ch_rd(i) <= pwm_ch(i);
|
168 |
|
|
end loop;
|
169 |
|
|
end process pwm_dc_rd_gen;
|
170 |
|
|
|
171 |
2 |
zero_gravi |
-- PWM clock select --
|
172 |
|
|
clkgen_en_o <= enable; -- enable clock generator
|
173 |
|
|
prsc_tick <= clkgen_i(to_integer(unsigned(prsc)));
|
174 |
|
|
|
175 |
|
|
|
176 |
|
|
-- PWM Core -------------------------------------------------------------------------------
|
177 |
|
|
-- -------------------------------------------------------------------------------------------
|
178 |
|
|
pwm_core: process(clk_i)
|
179 |
|
|
begin
|
180 |
|
|
if rising_edge(clk_i) then
|
181 |
60 |
zero_gravi |
-- pwm base counter --
|
182 |
2 |
zero_gravi |
if (enable = '0') then
|
183 |
|
|
pwm_cnt <= (others => '0');
|
184 |
|
|
elsif (prsc_tick = '1') then
|
185 |
|
|
pwm_cnt <= std_ulogic_vector(unsigned(pwm_cnt) + 1);
|
186 |
|
|
end if;
|
187 |
60 |
zero_gravi |
|
188 |
2 |
zero_gravi |
-- channels --
|
189 |
60 |
zero_gravi |
for i in 0 to NUM_CHANNELS-1 loop
|
190 |
2 |
zero_gravi |
if (unsigned(pwm_cnt) >= unsigned(pwm_ch(i))) or (enable = '0') then
|
191 |
23 |
zero_gravi |
pwm_o(i) <= '0';
|
192 |
2 |
zero_gravi |
else
|
193 |
23 |
zero_gravi |
pwm_o(i) <= '1';
|
194 |
2 |
zero_gravi |
end if;
|
195 |
60 |
zero_gravi |
end loop;
|
196 |
2 |
zero_gravi |
end if;
|
197 |
|
|
end process pwm_core;
|
198 |
|
|
|
199 |
|
|
|
200 |
|
|
end neorv32_pwm_rtl;
|