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

Subversion Repositories neorv32

[/] [neorv32/] [trunk/] [rtl/] [core/] [neorv32_pwm.vhd] - Diff between revs 23 and 60

Go to most recent revision | Show entire file | Details | Blame | View Log

Rev 23 Rev 60
Line 1... Line 1...
-- #################################################################################################
-- #################################################################################################
-- # << NEORV32 - Pulse Width Modulation Controller (PWM) >>                                       #
-- # << NEORV32 - Pulse Width Modulation Controller (PWM) >>                                       #
-- # ********************************************************************************************* #
-- # ********************************************************************************************* #
-- # Simple 4-channel PWM controller with 8 bit resolution for the duty cycle and programmable     #
-- # Simple PWM controller with 8 bit resolution for the duty cycle and programmable base          #
-- # clock.                                                                                        #
-- # frequency. The controller supports up to 60 PWM channels.                                     #
-- # ********************************************************************************************* #
-- # ********************************************************************************************* #
-- # BSD 3-Clause License                                                                          #
-- # BSD 3-Clause License                                                                          #
-- #                                                                                               #
-- #                                                                                               #
-- # Copyright (c) 2020, Stephan Nolting. All rights reserved.                                     #
-- # Copyright (c) 2021, Stephan Nolting. All rights reserved.                                     #
-- #                                                                                               #
-- #                                                                                               #
-- # Redistribution and use in source and binary forms, with or without modification, are          #
-- # Redistribution and use in source and binary forms, with or without modification, are          #
-- # permitted provided that the following conditions are met:                                     #
-- # permitted provided that the following conditions are met:                                     #
-- #                                                                                               #
-- #                                                                                               #
-- # 1. Redistributions of source code must retain the above copyright notice, this list of        #
-- # 1. Redistributions of source code must retain the above copyright notice, this list of        #
Line 41... Line 41...
 
 
library neorv32;
library neorv32;
use neorv32.neorv32_package.all;
use neorv32.neorv32_package.all;
 
 
entity neorv32_pwm is
entity neorv32_pwm is
 
  generic (
 
    NUM_CHANNELS : natural := 4 -- number of PWM channels (0..60)
 
  );
  port (
  port (
    -- host access --
    -- host access --
    clk_i       : in  std_ulogic; -- global clock line
    clk_i       : in  std_ulogic; -- global clock line
    addr_i      : in  std_ulogic_vector(31 downto 0); -- address
    addr_i      : in  std_ulogic_vector(31 downto 0); -- address
    rden_i      : in  std_ulogic; -- read enable
    rden_i      : in  std_ulogic; -- read enable
Line 54... Line 57...
    ack_o       : out std_ulogic; -- transfer acknowledge
    ack_o       : out std_ulogic; -- transfer acknowledge
    -- clock generator --
    -- clock generator --
    clkgen_en_o : out std_ulogic; -- enable clock generator
    clkgen_en_o : out std_ulogic; -- enable clock generator
    clkgen_i    : in  std_ulogic_vector(07 downto 0);
    clkgen_i    : in  std_ulogic_vector(07 downto 0);
    -- pwm output channels --
    -- pwm output channels --
    pwm_o       : out std_ulogic_vector(03 downto 0)
    pwm_o       : out std_ulogic_vector(NUM_CHANNELS-1 downto 0)
  );
  );
end neorv32_pwm;
end neorv32_pwm;
 
 
architecture neorv32_pwm_rtl of neorv32_pwm is
architecture neorv32_pwm_rtl of neorv32_pwm is
 
 
  -- internal configuration --
 
  constant num_pwm_channels_c : natural := 4; -- number of PWM channels, fixed!
 
 
 
  -- IO space: module base address --
  -- IO space: module base address --
  constant hi_abb_c : natural := index_size_f(io_size_c)-1; -- high address boundary bit
  constant hi_abb_c : natural := index_size_f(io_size_c)-1; -- high address boundary bit
  constant lo_abb_c : natural := index_size_f(pwm_size_c); -- low address boundary bit
  constant lo_abb_c : natural := index_size_f(pwm_size_c); -- low address boundary bit
 
 
  -- Control register bits --
  -- Control register bits --
Line 80... Line 80...
  signal addr   : std_ulogic_vector(31 downto 0); -- access address
  signal addr   : std_ulogic_vector(31 downto 0); -- access address
  signal wren   : std_ulogic; -- write enable
  signal wren   : std_ulogic; -- write enable
  signal rden   : std_ulogic; -- read enable
  signal rden   : std_ulogic; -- read enable
 
 
  -- accessible regs --
  -- accessible regs --
  type pwm_ch_t is array (0 to num_pwm_channels_c-1) of std_ulogic_vector(7 downto 0);
  type pwm_ch_t is array (0 to NUM_CHANNELS-1) of std_ulogic_vector(7 downto 0);
  signal pwm_ch : pwm_ch_t; -- duty cycle (r/w)
  signal pwm_ch : pwm_ch_t; -- duty cycle (r/w)
  signal enable : std_ulogic; -- enable unit (r/w)
  signal enable : std_ulogic; -- enable unit (r/w)
  signal prsc   : std_ulogic_vector(2 downto 0); -- clock prescaler (r/w)
  signal prsc   : std_ulogic_vector(2 downto 0); -- clock prescaler (r/w)
 
 
 
  type pwm_ch_rd_t is array (0 to 60-1) of std_ulogic_vector(7 downto 0);
 
  signal pwm_ch_rd : pwm_ch_rd_t; -- duty cycle read-back
 
 
  -- prescaler clock generator --
  -- prescaler clock generator --
  signal prsc_tick : std_ulogic;
  signal prsc_tick : std_ulogic;
 
 
  -- pwm core counter --
  -- pwm core counter --
  signal pwm_cnt : std_ulogic_vector(7 downto 0);
  signal pwm_cnt : std_ulogic_vector(7 downto 0);
 
 
begin
begin
 
 
 
  -- Sanity Checks --------------------------------------------------------------------------
 
  -- -------------------------------------------------------------------------------------------
 
  assert not (NUM_CHANNELS > 60) report "NEORV32 PROCESSOR CONFIG ERROR! <IO.PWM> invalid number of channels! Has to be 0..60.!" severity error;
 
 
 
 
  -- Access Control -------------------------------------------------------------------------
  -- Access Control -------------------------------------------------------------------------
  -- -------------------------------------------------------------------------------------------
  -- -------------------------------------------------------------------------------------------
  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';
  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';
  addr   <= pwm_base_c(31 downto lo_abb_c) & addr_i(lo_abb_c-1 downto 2) & "00"; -- word aligned
  addr   <= pwm_base_c(31 downto lo_abb_c) & addr_i(lo_abb_c-1 downto 2) & "00"; -- word aligned
  rden   <= acc_en and rden_i;
  rden   <= acc_en and rden_i;
Line 107... Line 115...
  -- -------------------------------------------------------------------------------------------
  -- -------------------------------------------------------------------------------------------
  wr_access: process(clk_i)
  wr_access: process(clk_i)
  begin
  begin
    if rising_edge(clk_i) then
    if rising_edge(clk_i) then
      ack_o <= acc_en and (rden_i or wren_i);
      ack_o <= acc_en and (rden_i or wren_i);
 
 
      -- write access --
      -- write access --
      if (wren = '1') then
      if (wren = '1') then
        if (addr = pwm_ctrl_addr_c) then -- control register
        -- control register --
 
        if (addr = pwm_ctrl_addr_c) then
          enable <= data_i(ctrl_enable_c);
          enable <= data_i(ctrl_enable_c);
          prsc   <= data_i(ctrl_prsc2_bit_c downto ctrl_prsc0_bit_c);
          prsc   <= data_i(ctrl_prsc2_bit_c downto ctrl_prsc0_bit_c);
        end if;
        end if;
        if (addr = pwm_duty_addr_c) then -- duty cycle register
        -- duty cycle registers --
          for i in 0 to 3 loop
        for i in 0 to NUM_CHANNELS-1 loop -- channel loop
            pwm_ch(i) <= data_i(7+i*8 downto 0+i*8);
          if (addr(5 downto 2) = std_ulogic_vector(to_unsigned((i/4)+1, 4))) then -- 4 channels per register; add ctrl reg offset
          end loop;
            pwm_ch(i) <= data_i((i mod 4)*8+7 downto (i mod 4)*8+0);
        end if;
        end if;
 
        end loop;
      end if;
      end if;
 
 
      -- read access --
      -- read access --
      data_o <= (others => '0');
      data_o <= (others => '0');
      if (rden = '1') then
      if (rden = '1') then
        if (addr = pwm_ctrl_addr_c) then
        case addr(5 downto 2) is
          data_o(ctrl_enable_c) <= enable;
          when x"0"   => data_o(ctrl_enable_c) <= enable; data_o(ctrl_prsc2_bit_c downto ctrl_prsc0_bit_c) <= prsc;
          data_o(ctrl_prsc2_bit_c downto ctrl_prsc0_bit_c) <= prsc;
          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;
        else -- pwm_duty_addr_c
          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;
          data_o(07 downto 00) <= pwm_ch(0);
          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;
          data_o(15 downto 08) <= pwm_ch(1);
          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;
          data_o(23 downto 16) <= pwm_ch(2);
          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;
          data_o(31 downto 24) <= pwm_ch(3);
          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;
        end if;
          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;
 
          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;
 
          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;
 
          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;
 
          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;
 
          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;
 
          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;
 
          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;
 
          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;
 
          when others => NULL;
 
        end case;
      end if;
      end if;
    end if;
    end if;
  end process wr_access;
  end process wr_access;
 
 
 
  -- duty cycle read-back --
 
  pwm_dc_rd_gen: process(pwm_ch)
 
  begin
 
    pwm_ch_rd <= (others => (others => '0'));
 
    for i in 0 to NUM_CHANNELS-1 loop
 
      pwm_ch_rd(i) <= pwm_ch(i);
 
    end loop;
 
  end process pwm_dc_rd_gen;
 
 
  -- PWM clock select --
  -- PWM clock select --
  clkgen_en_o <= enable; -- enable clock generator
  clkgen_en_o <= enable; -- enable clock generator
  prsc_tick   <= clkgen_i(to_integer(unsigned(prsc)));
  prsc_tick   <= clkgen_i(to_integer(unsigned(prsc)));
 
 
 
 
  -- PWM Core -------------------------------------------------------------------------------
  -- PWM Core -------------------------------------------------------------------------------
  -- -------------------------------------------------------------------------------------------
  -- -------------------------------------------------------------------------------------------
  pwm_core: process(clk_i)
  pwm_core: process(clk_i)
  begin
  begin
    if rising_edge(clk_i) then
    if rising_edge(clk_i) then
      -- pwm counter --
      -- pwm base counter --
      if (enable = '0') then
      if (enable = '0') then
        pwm_cnt <= (others => '0');
        pwm_cnt <= (others => '0');
      elsif (prsc_tick = '1') then
      elsif (prsc_tick = '1') then
        pwm_cnt <= std_ulogic_vector(unsigned(pwm_cnt) + 1);
        pwm_cnt <= std_ulogic_vector(unsigned(pwm_cnt) + 1);
      end if;
      end if;
 
 
      -- channels --
      -- channels --
      for i in 0 to num_pwm_channels_c-1 loop
      for i in 0 to NUM_CHANNELS-1 loop
 
--if (pwm_cnt = pwm_ch(i)) or (pwm_ch(i) = x"00") or (enable = '0') then
 
--  pwm_o(i) <= '0';
 
--elsif (pwm_cnt = x"00") then
 
--  pwm_o(i) <= '1';
 
--end if;
        if (unsigned(pwm_cnt) >= unsigned(pwm_ch(i))) or (enable = '0') then
        if (unsigned(pwm_cnt) >= unsigned(pwm_ch(i))) or (enable = '0') then
          pwm_o(i) <= '0';
          pwm_o(i) <= '0';
        else
        else
          pwm_o(i) <= '1';
          pwm_o(i) <= '1';
        end if;
        end if;
      end loop; -- i, pwm channel
      end loop;
    end if;
    end if;
  end process pwm_core;
  end process pwm_core;
 
 
 
 
end neorv32_pwm_rtl;
end neorv32_pwm_rtl;

powered by: WebSVN 2.1.0

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