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

Subversion Repositories pmodad2driver

[/] [pmodad2driver/] [trunk/] [hw/] [sources/] [Top_PmodAD2Driver.vhd] - Blame information for rev 2

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 ldalmasso
------------------------------------------------------------------------
2
-- Engineer:    Dalmasso Loic
3
-- Create Date: 05/02/2025
4
-- Module Name: Top_PmodAD2Driver
5
-- Description:
6
--      Top Module including Pmod AD2 Driver for the 4 Channels of 12-bit Analog-to-Digital Converter AD7991.
7
--
8
-- WARNING: /!\ Require Pull-Up on SCL and SDA pins /!\
9
--
10
-- Ports
11
--              Input   -       i_sys_clock: System Input Clock
12
--              Input   -       i_reset: Module Reset ('0': No Reset, '1': Reset)
13
--              Output  -       o_led: ADC Value
14
--              In/Out  -       io_scl: I2C Serial Clock ('0'-'Z'(as '1') values, working with Pull-Up)
15
--              In/Out  -       io_sda: I2C Serial Data ('0'-'Z'(as '1') values, working with Pull-Up)
16
------------------------------------------------------------------------
17
 
18
LIBRARY IEEE;
19
USE IEEE.STD_LOGIC_1164.ALL;
20
USE IEEE.NUMERIC_STD.ALL;
21
 
22
ENTITY Top_PmodAD2Driver is
23
 
24
PORT(
25
        i_sys_clock: IN STD_LOGIC;
26
    i_reset: IN STD_LOGIC;
27
    o_led: OUT UNSIGNED(15 downto 0);
28
        io_scl: INOUT STD_LOGIC;
29
    io_sda: INOUT STD_LOGIC
30
);
31
 
32
END Top_PmodAD2Driver;
33
 
34
ARCHITECTURE Behavioral of Top_PmodAD2Driver is
35
 
36
------------------------------------------------------------------------
37
-- Component Declarations
38
------------------------------------------------------------------------
39
COMPONENT PmodAD2Driver is
40
 
41
    GENERIC(
42
        sys_clock: INTEGER := 100_000_000;
43
        i2c_clock: INTEGER range 1 to 400_000 := 100_000
44
    );
45
 
46
    PORT(
47
        i_sys_clock: IN STD_LOGIC;
48
        i_enable: IN STD_LOGIC;
49
        i_mode: IN STD_LOGIC;
50
        i_addr: IN UNSIGNED(6 downto 0);
51
        i_config_byte: IN UNSIGNED(7 downto 0);
52
        i_last_read: IN STD_LOGIC;
53
        o_adc_valid: OUT STD_LOGIC;
54
        o_adc_value: OUT UNSIGNED(15 downto 0);
55
        o_ready: OUT STD_LOGIC;
56
        io_scl: INOUT STD_LOGIC;
57
        io_sda: INOUT STD_LOGIC
58
    );
59
 
60
END COMPONENT;
61
 
62
------------------------------------------------------------------------
63
-- Signal Declarations
64
------------------------------------------------------------------------
65
-- Pmod States
66
TYPE pmodState is (IDLE, CONFIG, END_CONFIG, WAITING_READ, READ_ADC, END_READ);
67
signal state: pmodState := IDLE;
68
signal next_state: pmodState;
69
 
70
-- Read Timer (1 read every second)
71
constant CLOCK_DIV: INTEGER := 100_000_000;
72
signal read_timer: INTEGER range 0 to CLOCK_DIV-1 := 0;
73
signal read_enable: STD_LOGIC := '0';
74
 
75
-- Pmod AD2 Enable
76
signal pmodad2_enable: STD_LOGIC := '0';
77
 
78
-- Pmod AD2 Ready
79
signal pmodad2_ready: STD_LOGIC := '0';
80
 
81
-- Pmode AD2 Input Register
82
signal mode_reg: STD_LOGIC := '0';
83
 
84
-- Pmod AD2 Output Register
85
signal adc_valid_reg: STD_LOGIC := '0';
86
signal adc_value_reg: UNSIGNED(15 downto 0) := (others => '0');
87
 
88
------------------------------------------------------------------------
89
-- Module Implementation
90
------------------------------------------------------------------------
91
begin
92
 
93
        -----------------------
94
        -- Reset Read Timer --
95
        -----------------------
96
        process(i_sys_clock)
97
        begin
98
                if rising_edge(i_sys_clock) then
99
 
100
            -- Reset Read Timer
101
            if (i_reset = '1') or (read_timer = CLOCK_DIV-1) or (state = END_READ) then
102
                read_timer <= 0;
103
 
104
                        -- Increment Read Timer
105
                        elsif (state = WAITING_READ) then
106
                read_timer <= read_timer +1;
107
                        end if;
108
                end if;
109
        end process;
110
 
111
        -----------------
112
        -- Read Enable --
113
        -----------------
114
        process(i_sys_clock)
115
        begin
116
                if rising_edge(i_sys_clock) then
117
 
118
                        -- Read Enable
119
                        if (read_timer = CLOCK_DIV-1) then
120
                                read_enable <= '1';
121
                        else
122
                read_enable <= '0';
123
                        end if;
124
 
125
                end if;
126
        end process;
127
 
128
        ------------------------
129
        -- Pmod State Machine --
130
        ------------------------
131
    -- Pmod State
132
        process(i_sys_clock)
133
        begin
134
                if rising_edge(i_sys_clock) then
135
 
136
            -- Reset
137
            if (i_reset = '1') then
138
                state <= IDLE;
139
 
140
            -- Next State
141
                        else
142
                                state <= next_state;
143
                        end if;
144
 
145
                end if;
146
        end process;
147
 
148
    -- Pmod Next State
149
    process(state, pmodad2_ready, read_enable)
150
        begin
151
                case state is
152
                        when IDLE =>    if (pmodad2_ready = '1') then
153
                                next_state <= CONFIG;
154
                            else
155
                                next_state <= IDLE;
156
                                                        end if;
157
 
158
                        -- Configure ADC
159
                        when CONFIG =>  if (pmodad2_ready = '0') then
160
                                next_state <= END_CONFIG;
161
                            else
162
                                next_state <= CONFIG;
163
                                                        end if;
164
 
165
            -- End of Configuration
166
            when END_CONFIG =>
167
                            if (pmodad2_ready = '1') then
168
                                next_state <= WAITING_READ;
169
                            else
170
                                next_state <= END_CONFIG;
171
                            end if;
172
 
173
            -- Waiting Read ADC
174
            when WAITING_READ =>
175
                            if (read_enable = '1') then
176
                                next_state <= READ_ADC;
177
                            else
178
                                next_state <= WAITING_READ;
179
                            end if;
180
 
181
            -- Read ADC
182
            when READ_ADC =>
183
                            if (pmodad2_ready = '0') then
184
                                next_state <= END_READ;
185
                            else
186
                                next_state <= READ_ADC;
187
                            end if;
188
 
189
            -- End Read ADC
190
            when END_READ =>
191
                            if (pmodad2_ready = '1') then
192
                                next_state <= WAITING_READ;
193
                            else
194
                                next_state <= END_READ;
195
                            end if;
196
 
197
            when others => next_state <= IDLE;
198
        end case;
199
    end process;
200
 
201
        ---------------------
202
        -- Pmod AD2 Enable --
203
        ---------------------
204
    pmodad2_enable <= '0' when state = IDLE or state = WAITING_READ else '1';
205
 
206
        -------------------
207
        -- Pmod AD2 Mode --
208
        -------------------
209
    mode_reg <= '0' when state = CONFIG or state = END_CONFIG else '1';
210
 
211
    ----------------------------
212
        -- Pmod AD2 Digital Value --
213
        ----------------------------
214
        process(i_sys_clock)
215
        begin
216
 
217
                if rising_edge(i_sys_clock) then
218
 
219
            -- Config
220
            if (state = IDLE) then
221
                o_led <= "1010101010101010";
222
 
223
            -- ADC Value
224
            elsif (adc_valid_reg = '1') then
225
                o_led <= adc_value_reg;
226
            end if;
227
 
228
        end if;
229
    end process;
230
 
231
    ---------------------
232
        -- Pmod AD2 Driver --
233
        ---------------------
234
    inst_PmodAD2Driver: PmodAD2Driver
235
    generic map (
236
        sys_clock => 100_000_000,
237
        i2c_clock => 100_000)
238
 
239
    port map (
240
        i_sys_clock => i_sys_clock,
241
        i_enable => pmodad2_enable,
242
        i_mode => mode_reg,
243
        i_addr => "0101000",
244
        i_config_byte => "00010000",
245
        i_last_read => '1',
246
        o_adc_valid => adc_valid_reg,
247
        o_adc_value => adc_value_reg,
248
        o_ready => pmodad2_ready,
249
        io_scl => io_scl,
250
        io_sda => io_sda);
251
 
252
end Behavioral;

powered by: WebSVN 2.1.0

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