2 use ieee.std_logic_1164.all;
3 use ieee.std_logic_arith.all;
4 use ieee.std_logic_unsigned.all;
6 --------------------------------------------------------------------------------
7 -- This entity is a generator of PWM signal with 16-bit MCU interface.
9 -- PWM bit width is configurable by PWM_WIDTH generic parameter and must be in
10 -- range of 1 to 16. Default is 16.
12 -- In practice, to generate PWM signal component must be fed by a counter vector
13 -- (pwm_cnt input) of appropriate width and also period! (One counter can be
14 -- used by several PWM generators and generated signals are then synchronized.)
16 -- The period of the input counter vector should be by 1 smaller than the count
17 -- of all possible PWM values, i.e. counter vector should never reaches the
18 -- maximum possible PWM value, and counting should start from 0. E.g. when full
19 -- range of PWM values is used, binary counter should be reset just one step
20 -- before it overflows. In such a case, when PWM value is maximal, output keeps
23 -- MCU interface consist of one r/w 16-bit register holding PWM value.
24 --------------------------------------------------------------------------------
28 ADDR : integer := 16#0160#;
29 PWM_WIDTH : integer := 16);
31 -- MCU peripheral interface
34 per_addr : in std_logic_vector (7 downto 0);
35 per_en : in std_logic;
36 per_wen : in std_logic_vector (1 downto 0);
37 per_din : in std_logic_vector (15 downto 0);
38 per_dout : out std_logic_vector (15 downto 0);
40 pwm_cnt : in std_logic_vector (PWM_WIDTH-1 downto 0);
44 --------------------------------------------------------------------------------
46 architecture behavioral of omsp_pwm is
48 signal pwm_value : std_logic_vector (15 downto 0);
50 signal pwm_sel : boolean;
52 --------------------------------------------------------------------------------
56 pwm_sel <= (per_addr = ADDR/2) and (per_en = '1');
58 per_dout <= pwm_value when pwm_sel else
62 -- Peripheral registers
66 pwm_value <= (others => '0');
68 elsif rising_edge(mclk) and pwm_sel then
69 if per_wen (0) = '1' then
70 pwm_value (7 downto 0) <= per_din (7 downto 0);
73 if per_wen (1) = '1' then
74 pwm_value (15 downto 8) <= per_din (15 downto 8);
80 -- Generation of the PWM signal
86 elsif mclk'event and mclk = '1' then
87 if pwm_cnt < pwm_value then