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 |
23 |
zero_gravi |
-- pwm core counter --
|
94 |
2 |
zero_gravi |
signal pwm_cnt : std_ulogic_vector(7 downto 0);
|
95 |
|
|
|
96 |
|
|
begin
|
97 |
|
|
|
98 |
|
|
-- Access Control -------------------------------------------------------------------------
|
99 |
|
|
-- -------------------------------------------------------------------------------------------
|
100 |
|
|
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';
|
101 |
|
|
addr <= pwm_base_c(31 downto lo_abb_c) & addr_i(lo_abb_c-1 downto 2) & "00"; -- word aligned
|
102 |
|
|
rden <= acc_en and rden_i;
|
103 |
|
|
wren <= acc_en and wren_i;
|
104 |
|
|
|
105 |
|
|
|
106 |
|
|
-- Write access ---------------------------------------------------------------------------
|
107 |
|
|
-- -------------------------------------------------------------------------------------------
|
108 |
|
|
wr_access: process(clk_i)
|
109 |
|
|
begin
|
110 |
|
|
if rising_edge(clk_i) then
|
111 |
|
|
ack_o <= acc_en and (rden_i or wren_i);
|
112 |
|
|
-- write access --
|
113 |
|
|
if (wren = '1') then
|
114 |
|
|
if (addr = pwm_ctrl_addr_c) then -- control register
|
115 |
22 |
zero_gravi |
enable <= data_i(ctrl_enable_c);
|
116 |
|
|
prsc <= data_i(ctrl_prsc2_bit_c downto ctrl_prsc0_bit_c);
|
117 |
2 |
zero_gravi |
end if;
|
118 |
|
|
if (addr = pwm_duty_addr_c) then -- duty cycle register
|
119 |
|
|
for i in 0 to 3 loop
|
120 |
22 |
zero_gravi |
pwm_ch(i) <= data_i(7+i*8 downto 0+i*8);
|
121 |
2 |
zero_gravi |
end loop;
|
122 |
|
|
end if;
|
123 |
|
|
end if;
|
124 |
|
|
-- read access --
|
125 |
|
|
data_o <= (others => '0');
|
126 |
|
|
if (rden = '1') then
|
127 |
|
|
if (addr = pwm_ctrl_addr_c) then
|
128 |
|
|
data_o(ctrl_enable_c) <= enable;
|
129 |
|
|
data_o(ctrl_prsc2_bit_c downto ctrl_prsc0_bit_c) <= prsc;
|
130 |
|
|
else -- pwm_duty_addr_c
|
131 |
|
|
data_o(07 downto 00) <= pwm_ch(0);
|
132 |
|
|
data_o(15 downto 08) <= pwm_ch(1);
|
133 |
|
|
data_o(23 downto 16) <= pwm_ch(2);
|
134 |
|
|
data_o(31 downto 24) <= pwm_ch(3);
|
135 |
|
|
end if;
|
136 |
|
|
end if;
|
137 |
|
|
end if;
|
138 |
|
|
end process wr_access;
|
139 |
|
|
|
140 |
|
|
-- PWM clock select --
|
141 |
|
|
clkgen_en_o <= enable; -- enable clock generator
|
142 |
|
|
prsc_tick <= clkgen_i(to_integer(unsigned(prsc)));
|
143 |
|
|
|
144 |
|
|
|
145 |
|
|
-- PWM Core -------------------------------------------------------------------------------
|
146 |
|
|
-- -------------------------------------------------------------------------------------------
|
147 |
|
|
pwm_core: process(clk_i)
|
148 |
|
|
begin
|
149 |
|
|
if rising_edge(clk_i) then
|
150 |
|
|
-- pwm counter --
|
151 |
|
|
if (enable = '0') then
|
152 |
|
|
pwm_cnt <= (others => '0');
|
153 |
|
|
elsif (prsc_tick = '1') then
|
154 |
|
|
pwm_cnt <= std_ulogic_vector(unsigned(pwm_cnt) + 1);
|
155 |
|
|
end if;
|
156 |
|
|
-- channels --
|
157 |
|
|
for i in 0 to num_pwm_channels_c-1 loop
|
158 |
|
|
if (unsigned(pwm_cnt) >= unsigned(pwm_ch(i))) or (enable = '0') then
|
159 |
23 |
zero_gravi |
pwm_o(i) <= '0';
|
160 |
2 |
zero_gravi |
else
|
161 |
23 |
zero_gravi |
pwm_o(i) <= '1';
|
162 |
2 |
zero_gravi |
end if;
|
163 |
|
|
end loop; -- i, pwm channel
|
164 |
|
|
end if;
|
165 |
|
|
end process pwm_core;
|
166 |
|
|
|
167 |
|
|
|
168 |
|
|
end neorv32_pwm_rtl;
|