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

Subversion Repositories open8_urisc

[/] [open8_urisc/] [trunk/] [VHDL/] [o8_pwm_adc.vhd] - Blame information for rev 241

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 241 jshamlet
-- Copyright (c)2020 Jeremy Seth Henry
2
-- All rights reserved.
3
--
4
-- Redistribution and use in source and binary forms, with or without
5
-- modification, are permitted provided that the following conditions are met:
6
--     * Redistributions of source code must retain the above copyright
7
--       notice, this list of conditions and the following disclaimer.
8
--     * Redistributions in binary form must reproduce the above copyright
9
--       notice, this list of conditions and the following disclaimer in the
10
--       documentation and/or other materials provided with the distribution,
11
--       where applicable (as part of a user interface, debugging port, etc.)
12
--
13
-- THIS SOFTWARE IS PROVIDED BY JEREMY SETH HENRY ``AS IS'' AND ANY
14
-- EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15
-- WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16
-- DISCLAIMED. IN NO EVENT SHALL JEREMY SETH HENRY BE LIABLE FOR ANY
17
-- DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18
-- (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19
-- LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
20
-- ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21
-- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
22
-- SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23
--
24
-- VHDL Entity: pwm_adc
25
-- Description: Integrates a PWM input to return the approximate duty cycle
26
--              Uses a 1kB block ram as storage for a rolling integrator that
27
--               acts as a simple successive-approximation ADC.
28
--
29
-- Revision History
30
-- Author          Date     Change
31
------------------ -------- ---------------------------------------------------
32
-- Seth Henry      05/07/20 Design Start
33
 
34
library ieee;
35
  use ieee.std_logic_1164.all;
36
  use ieee.std_logic_unsigned.all;
37
 
38
library work;
39
  use work.open8_pkg.all;
40
 
41
entity o8_pwm_adc is
42
generic(
43
  Address                    : ADDRESS_TYPE
44
);
45
port(
46
  Open8_Bus                  : in  OPEN8_BUS_TYPE;
47
  Rd_Data                    : out DATA_TYPE;
48
  --
49
  PWM_In                     : in  std_logic
50
);
51
end entity;
52
 
53
architecture behave of o8_pwm_adc is
54
 
55
  alias Clock                is Open8_Bus.Clock;
56
  alias Reset                is Open8_Bus.Reset;
57
  alias uSec_Tick            is Open8_Bus.uSec_Tick;
58
 
59
  constant User_Addr         : std_logic_vector(15 downto 0) := Address(15 downto 0);
60
  alias  Comp_Addr           is Open8_Bus.Address(15 downto 0);
61
 
62
  signal Addr_Match          : std_logic := '0';
63
  signal Rd_En               : std_logic := '0';
64
 
65
  signal Sample              : DATA_TYPE := x"00";
66
  signal RAM_Addr            : std_logic_vector(9 downto 0) := (others => '0');
67
  signal RAM_Data            : DATA_TYPE := x"00";
68
  signal Accumulator         : std_logic_vector(17 downto 0) := (others => '0');
69
  signal Average             : DATA_TYPE := x"00";
70
begin
71
 
72
  Addr_Match                 <= Open8_Bus.Rd_En when Comp_Addr = User_Addr else '0';
73
 
74
  io_reg: process( Clock, Reset )
75
  begin
76
    if( Reset = Reset_Level )then
77
      Rd_En                  <= '0';
78
      Rd_Data                <= OPEN8_NULLBUS;
79
 
80
    elsif( rising_edge( Clock ) )then
81
 
82
      Rd_En                  <= Addr_Match and Open8_Bus.Rd_En;
83
      Rd_Data                <= OPEN8_NULLBUS;
84
 
85
      if( Rd_En = '1' )then
86
        Rd_Data              <= Average;
87
      end if;
88
    end if;
89
  end process;
90
 
91
  -- PWM input is binary, so the sample swings between 0x00 and 0xFF
92
  Sample                     <= (others => PWM_In);
93
 
94
  U_DP : entity work.o8_pwm_adc_ram
95
  port map(
96
                address                  => RAM_Addr,
97
                clock                    => Clock,
98
                data                     => Sample,
99
                wren                     => '1',
100
                q                        => RAM_Data
101
        );
102
 
103
  ADC_proc: process( Clock, Reset )
104
  begin
105
    if( Reset = Reset_Level )then
106
      RAM_Addr               <= (others => '0');
107
      Accumulator            <= (others => '0');
108
      Average                <= (others => '0');
109
    elsif( rising_edge(Clock) )then
110
      RAM_Addr               <= RAM_Addr + 1;
111
      Accumulator            <= Accumulator + ("0000000000" & RAM_Data);
112
      if( RAM_Addr = 0 )then
113
        Accumulator          <= (others => '0');
114
        Average              <= Accumulator(17 downto 10);
115
      end if;
116
    end if;
117
  end process;
118
 
119
end architecture;

powered by: WebSVN 2.1.0

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