summaryrefslogtreecommitdiffstats
path: root/fpga/hw-v2/src/usbrx/toplevel/usbrx_pwm.vhd
blob: c71e3a23ee2fbb6270acb1eb400f01a6e6286684 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
---------------------------------------------------------------------------------------------------
-- Filename    : usbrx_pwm.vhd
-- Project     : OsmoSDR FPGA Firmware
-- Purpose     : PWM Generator
---------------------------------------------------------------------------------------------------

-----------------------------------------------------------------------------------
-- Copyright (C) 2012 maintech GmbH, Otto-Hahn-Str. 15, 97204 Hoechberg, Germany --
-- written by Matthias Kleffel                                                   --
--                                                                               --
-- This program is free software; you can redistribute it and/or modify          --
-- it under the terms of the GNU General Public License as published by          --
-- the Free Software Foundation as version 3 of the License, or                  --
--                                                                               --
-- This program is distributed in the hope that it will be useful,               --
-- but WITHOUT ANY WARRANTY; without even the implied warranty of                --
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the                  --
-- GNU General Public License V3 for more details.                               --
--                                                                               --
-- You should have received a copy of the GNU General Public License             --
-- along with this program. If not, see <http://www.gnu.org/licenses/>.          --
-----------------------------------------------------------------------------------

library ieee;
	use ieee.std_logic_1164.all;
	use ieee.numeric_std.all;
library work;
	use work.all;
	use work.mt_toolbox.all;
	use work.usbrx.all;

entity usbrx_pwm is
	port(
		-- common
		clk       : in  std_logic;
		reset     : in  std_logic;

		-- config
		config    : in  usbrx_pwm_config_t;
		
		-- PWM output
		pwm0      : out std_logic;
		pwm1      : out std_logic
	);
end usbrx_pwm;

architecture rtl of usbrx_pwm is

	-- status
	signal counter0 : unsigned(15 downto 0);
	signal counter1 : unsigned(15 downto 0);
	signal out0     : std_logic;
	signal out1     : std_logic;
	
begin
	
	process(clk)
	begin
		if rising_edge(clk) then
			-- update counter #0
			counter0 <= counter0 - 1;
			if counter0 = 0 then
				counter0 <= config.freq0;
			end if;
			
			-- update counter #1
			counter1 <= counter1 - 1;
			if counter1 = 0 then
				counter1 <= config.freq1;
			end if;
			
			-- update output #0
			if counter0 < config.duty0
				then out0 <= '1';
				else out0 <= '0';
			end if;
			
			-- update output #1
			if counter1 < config.duty1
				then out1 <= '1';
				else out1 <= '0';
			end if;
			
			-- output register
			pwm0 <= out0;
			pwm1 <= out1;
				
			-- handle reset
			if reset='1' then
				counter0 <= (others=>'0');
				counter1 <= (others=>'0');
				out0     <= '0';
				out1     <= '0';
				pwm0     <= '0';
				pwm1     <= '0';
			end if;
		end if;
	end process;
	
end rtl;