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

Subversion Repositories pmodad2driver

[/] [pmodad2driver/] [trunk/] [hw/] [sources/] [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: 07/02/2025
4
-- Module Name: PmodAD2Driver
5
-- Description:
6
--      Pmod AD2 Driver for the 4 Channels of 12-bit Analog-to-Digital Converter AD7991. The communication with the ADC uses the I2C protocol.
7
--      User can specifies the I2C Clock Frequency (up to 400 kHz with the Fast Mode).
8
--
9
-- WARNING: /!\ Require Pull-Up on SCL and SDA pins /!\
10
--
11
-- Usage:
12
--              User specifies inputs: I2C mode (i_mode), ADC Slave Address (i_addr), Configuration Byte (i_config_byte, write mode only) and the Last Read Cycle trigger (i_last_read, read mode only)
13
--              The transmission begin when the i_enable signal is set to '1'.
14
--              When started, the PmodAD2Driver executes the complete operation cycle (configurations or ADC conversions) independently of the new i_enable signal value (the i_enable signal can be reset).
15
--              At the end of the operation cycle, if the i_enable signal is still set to '1', the PmodAD2Driver executes the operation again with the current inputs.
16
--              The o_ready signal (set to '1') indicates the PmodAD2Driver is ready to process new operation. The o_ready signal is set to '0' to acknowledge the receipt.
17
--              The o_ready signal is set to '0' to acknowledge the receipt.
18
--              In Write mode, the PmodAD2Driver writes the Configuration byte into the ADC register and stop the transmission.
19
--              In Read mode, the PmodAD2Driver always reads 2-byte ADC conversion values channel-by-channel (according to ADC configuration).
20
--              The ADC value (o_adc_value) is available when its validity signal (o_adc_valid) is asserted.
21
--              In Read mode, while the i_last_read is NOT set to '1', the PmodAD2Driver execute the 2-byte ADC conversion value.
22
--              When the i_last_read is set to '1', the PmodAD2Driver ends the 2-byte ADC conversion value and return to IDLE state, and waits for the i_enable signal is set to '1'.
23
--
24
--              ADC AD7991 has 2 I2C Addresses:
25
--              AD7991-0: 010 1000
26
--              AD7991-1: 010 1001
27
--
28
--              Configuration Register (8-bit Write Only)
29
--              | D7  | D6  | D5  | D4  |   D3    |  D2  |       D1        |      D0      | Bit
30
--              | CH3 | CH2 | CH1 | CH0 | REF_SEL | FLTR | Bit Trial delay | Sample delay | Description
31
--              |  1  |  1      |  1  |  1      |    0    |  0   |               0                 |      0       | Default Value
32
--
33
--              Configuration Register MSB Description
34
--              | D7 | D6 | D5 | D4 | Analog Input Channel
35
--              | 0  | 0  | 0  | 0  | No channel selected
36
--              | 0  | 0  | 0  | 1  | Convert on VIN0
37
--              | 0  | 0  | 1  | 0  | Convert on VIN1
38
--              | 0  | 0  | 1  | 1  | Sequence between VIN0 and VIN1
39
--              | 0  | 1  | 0  | 0  | Convert on VIN2
40
--              | 0  | 1  | 0  | 1  | Sequence between VIN0 and VIN2
41
--              | 0  | 1  | 1  | 0  | Sequence between VIN1 and VIN2
42
--              | 0  | 1  | 1  | 1  | Sequence among VIN0, VIN1, and VIN2
43
--              | 1  | 0  | 0  | 0  | Convert on VIN3
44
--              | 1  | 0  | 0  | 1  | Sequence between VIN0 and VIN3
45
--              | 1  | 0  | 1  | 0  | Sequence between VIN1 and VIN3
46
--              | 1  | 0  | 1  | 1  | Sequence among VIN0, VIN1, and VIN3
47
--              | 1  | 1  | 0  | 0  | Sequence between VIN2 and VIN3
48
--              | 1  | 1  | 0  | 1  | Sequence among VIN0, VIN2, and VIN3
49
--              | 1  | 1  | 1  | 0  | Sequence among VIN1, VIN2, and VIN3
50
--              | 1  | 1  | 1  | 1  | Sequence among VIN0, VIN1, VIN2, and VIN3
51
--
52
--              Conversion Result Register (16-bit Read Only)
53
--              | D15 | D14 |  D13  |  D12  | D11 | D10 | D9 | D8 | D7 | D6 | D5 | D4 | D3 | D2 | D1 | D0 |
54
--              |  0  |  0  | CHID1 | CHID0 | MSB | B10 | B9 | B8 | B7 | B6 | B5 | B4 | B3 | B2 | B1 | B0 |
55
--
56
-- Generics
57
--              sys_clock: System Input Clock Frequency (Hz)
58
--      i2c_clock: I2C Serial Clock Frequency (Standard Mode: 100 kHz, Fast Mode: 400 kHz)
59
-- Ports
60
--              Input   -       i_sys_clock: System Input Clock
61
--              Input   -       i_enable: Module Enable ('0': Disable, '1': Enable)
62
--              Input   -       i_mode: Read or Write Mode ('0': Write, '1': Read)
63
--              Input   -       i_addr: ADC Address (7 bits)
64
--              Input   -       i_config_byte: ADC Configuration Byte (8 bits)
65
--              Input   -       i_last_read: Indicates the Last Read Operation ('0': Continue Read Cycle, '1': Last Read Cycle)
66
--              Output  -       o_adc_valid: ADC Read Value Valid ('0': Not Valid, '1': Valid)
67
--              Output  -       o_adc_value: ADC Read Value
68
--              Output  -       o_ready: ADC Ready Status ('0': NOT Ready, '1': Ready)
69
--              In/Out  -       io_scl: I2C Serial Clock ('0'-'Z'(as '1') values, working with Pull-Up)
70
--              In/Out  -       io_sda: I2C Serial Data ('0'-'Z'(as '1') values, working with Pull-Up)
71
------------------------------------------------------------------------
72
 
73
LIBRARY IEEE;
74
USE IEEE.STD_LOGIC_1164.ALL;
75
USE IEEE.NUMERIC_STD.ALL;
76
 
77
ENTITY PmodAD2Driver is
78
 
79
GENERIC(
80
        sys_clock: INTEGER := 100_000_000;
81
        i2c_clock: INTEGER range 1 to 400_000 := 100_000
82
);
83
 
84
PORT(
85
        i_sys_clock: IN STD_LOGIC;
86
    i_enable: IN STD_LOGIC;
87
        i_mode: IN STD_LOGIC;
88
        i_addr: IN UNSIGNED(6 downto 0);
89
    i_config_byte: IN UNSIGNED(7 downto 0);
90
        i_last_read: IN STD_LOGIC;
91
    o_adc_valid: OUT STD_LOGIC;
92
        o_adc_value: OUT UNSIGNED(15 downto 0);
93
    o_ready: OUT STD_LOGIC;
94
        io_scl: INOUT STD_LOGIC;
95
        io_sda: INOUT STD_LOGIC
96
);
97
 
98
END PmodAD2Driver;
99
 
100
ARCHITECTURE Behavioral of PmodAD2Driver is
101
 
102
------------------------------------------------------------------------
103
-- Component Declarations
104
------------------------------------------------------------------------
105
COMPONENT I2CBusAnalyzer is
106
        PORT(
107
                i_clock: IN STD_LOGIC;
108
                i_scl_master: IN STD_LOGIC;
109
                i_scl_line: IN STD_LOGIC;
110
                i_sda_master: IN STD_LOGIC;
111
                i_sda_line: IN STD_LOGIC;
112
                o_bus_busy: OUT STD_LOGIC;
113
                o_bus_arbitration: OUT STD_LOGIC;
114
                o_scl_stretching: OUT STD_LOGIC
115
        );
116
END COMPONENT;
117
 
118
------------------------------------------------------------------------
119
-- Constant Declarations
120
------------------------------------------------------------------------
121
-- I2C Clock Dividers
122
constant CLOCK_DIV: INTEGER := sys_clock / i2c_clock;
123
constant CLOCK_DIV_X2_1_4: INTEGER := CLOCK_DIV /4;
124
constant CLOCK_DIV_X2_3_4: INTEGER := CLOCK_DIV - CLOCK_DIV_X2_1_4;
125
 
126
-- I2C IDLE ('Z' with Pull-Up)
127
constant TRANSMISSION_IDLE: STD_LOGIC := 'Z';
128
 
129
-- I2C Transmission Don't Care Bit
130
constant TRANSMISSION_DONT_CARE_BIT: STD_LOGIC := '1';
131
 
132
-- I2C Transmission Start Bit
133
constant TRANSMISSION_START_BIT: STD_LOGIC := '0';
134
 
135
-- I2C Transmission ACK Bit
136
constant TRANSMISSION_ACK_BIT: STD_LOGIC := '0';
137
 
138
-- I2C Transmission NACK Bit
139
constant TRANSMISSION_NACK_BIT: STD_LOGIC := '1';
140
 
141
-- I2C Modes ('0': Write, '1': Read)
142
constant I2C_WRITE_MODE: STD_LOGIC := '0';
143
 
144
------------------------------------------------------------------------
145
-- Signal Declarations
146
------------------------------------------------------------------------
147
-- Pmod A2 Registers
148
signal enable_reg: STD_LOGIC := '0';
149
signal mode_reg: STD_LOGIC := '0';
150
signal data_write_reg: UNSIGNED(15 downto 0) := (others => '0');
151
signal data_read_reg: UNSIGNED(15 downto 0) := (others => '0');
152
signal data_read_reg_valid: STD_LOGIC := '0';
153
 
154
-- I2C Master States
155
TYPE i2cState is (      IDLE, START_TX,
156
                                        WRITE_SLAVE_ADDR, SLAVE_ADDR_ACK,
157
                                        WRITE_BYTE, WRITE_BYTE_ACK,
158
                                        READ_BYTE_1, READ_BYTE_1_ACK, READ_BYTE_2, READ_BYTE_2_ACK,
159
                                        READ_BYTE_2_NO_ACK, STOP_TX);
160
signal state: i2cState := IDLE;
161
signal next_state: i2cState;
162
 
163
-- I2C Clock Divider
164
signal i2c_clock_divider: INTEGER range 0 to CLOCK_DIV-1 := 0;
165
signal i2c_clock_enable: STD_LOGIC := '0';
166
signal i2c_clock_enable_rising: STD_LOGIC := '0';
167
signal i2c_clock_enable_falling: STD_LOGIC := '0';
168
 
169
-- I2C Transmission Bit Counter (8 bits per cycle)
170
signal bit_counter: UNSIGNED(2 downto 0) := (others => '0');
171
signal bit_counter_end: STD_LOGIC := '0';
172
 
173
-- I2C SCL
174
signal scl_reg_out: STD_LOGIC := '1';
175
 
176
-- I2C SDA
177
signal sda_in_reg: STD_LOGIC := '1';
178
signal sda_out: STD_LOGIC := '1';
179
 
180
------------------------------------------------------------------------
181
-- Module Implementation
182
------------------------------------------------------------------------
183
begin
184
 
185
        --------------------------------------
186
        -- Pmod AD2 Enable & Mode Registers --
187
        --------------------------------------
188
        process(i_sys_clock)
189
        begin
190
 
191
                if rising_edge(i_sys_clock) then
192
 
193
            -- Load Inputs
194
            if (state = IDLE) then
195
                                enable_reg <= i_enable;
196
                                mode_reg <= i_mode;
197
            end if;
198
 
199
        end if;
200
    end process;
201
 
202
        -----------------------
203
        -- I2C Clock Divider --
204
        -----------------------
205
        process(i_sys_clock)
206
        begin
207
                if rising_edge(i_sys_clock) then
208
 
209
                        -- Reset I2C Clock Divider
210
                        if (enable_reg = '0') or (i2c_clock_divider = CLOCK_DIV-1) then
211
                                i2c_clock_divider <= 0;
212
 
213
                        -- Increment I2C Clock Divider
214
                        else
215
                                i2c_clock_divider <= i2c_clock_divider +1;
216
                        end if;
217
                end if;
218
        end process;
219
 
220
        -----------------------
221
        -- I2C Clock Enables --
222
        -----------------------
223
        process(i_sys_clock)
224
        begin
225
                if rising_edge(i_sys_clock) then
226
 
227
                        -- I2C Clock Enable
228
                        if (i2c_clock_divider = CLOCK_DIV-1) then
229
                                i2c_clock_enable <= '1';
230
                        else
231
                                i2c_clock_enable <= '0';
232
                        end if;
233
 
234
                        -- I2C Clock Rising
235
                        if (i2c_clock_divider = CLOCK_DIV_X2_1_4-1) then
236
                                i2c_clock_enable_rising <= '1';
237
                        else
238
                                i2c_clock_enable_rising <= '0';
239
                        end if;
240
 
241
                        -- I2C Clock Falling
242
                        if (i2c_clock_divider = CLOCK_DIV_X2_3_4-1) then
243
                                i2c_clock_enable_falling <= '1';
244
                        else
245
                                i2c_clock_enable_falling <= '0';
246
                        end if;
247
 
248
                end if;
249
        end process;
250
 
251
        -----------------------
252
        -- I2C State Machine --
253
        -----------------------
254
    -- I2C State
255
        process(i_sys_clock)
256
        begin
257
                if rising_edge(i_sys_clock) then
258
 
259
                        -- Next State (When I2C Clock Enable)
260
                        if (i2c_clock_enable = '1') then
261
                                state <= next_state;
262
                        end if;
263
 
264
                end if;
265
        end process;
266
 
267
        -- I2C Next State
268
        process(state, enable_reg, bit_counter_end, sda_in_reg, mode_reg, i_last_read)
269
        begin
270
                case state is
271
                        when IDLE =>    if (enable_reg = '1') then
272
                                next_state <= START_TX;
273
                            else
274
                                next_state <= IDLE;
275
                                                        end if;
276
 
277
                        -- Start Transmission
278
                        when START_TX => next_state <= WRITE_SLAVE_ADDR;
279
 
280
                        -- Write Slave Address
281
                        when WRITE_SLAVE_ADDR =>
282
                                                        -- End of Write Slave Address Cycle
283
                                                        if (bit_counter_end = '1') then
284
                                                                next_state <= SLAVE_ADDR_ACK;
285
 
286
                                                        else
287
                                                                next_state <= WRITE_SLAVE_ADDR;
288
                                                        end if;
289
 
290
                        -- Slave Address ACK
291
                        when SLAVE_ADDR_ACK =>
292
                                                        -- Slave ACK Error or Stop Command
293
                                                        if (sda_in_reg /= TRANSMISSION_ACK_BIT) then
294
                                                                next_state <= STOP_TX;
295
 
296
                                                        -- Write Mode
297
                                                        elsif (mode_reg = I2C_WRITE_MODE) then
298
                                                                next_state <= WRITE_BYTE;
299
 
300
                                                        -- Read Mode
301
                                                        else
302
                                                                next_state <= READ_BYTE_1;
303
                                                        end if;
304
 
305
                        -- Write Byte
306
                        when WRITE_BYTE =>
307
                                                        -- End of Write Byte Cycle
308
                                                        if (bit_counter_end = '1') then
309
                                                                next_state <= WRITE_BYTE_ACK;
310
 
311
                                                        else
312
                                                                next_state <= WRITE_BYTE;
313
                                                        end if;
314
 
315
                        -- Write Byte ACK (Independ of Slave ACK/No ACK)
316
                        when WRITE_BYTE_ACK => next_state <= STOP_TX;
317
 
318
                        -- Read Byte 1
319
                        when READ_BYTE_1 =>
320
                                                        -- End of Read Byte 1 Cycle
321
                                                        if (bit_counter_end = '1') then
322
                                                                next_state <= READ_BYTE_1_ACK;
323
                                                        else
324
                                                                next_state <= READ_BYTE_1;
325
                                                        end if;
326
 
327
                        -- Read Byte 1 ACK
328
                        when READ_BYTE_1_ACK => next_state <= READ_BYTE_2;
329
 
330
                        -- Read Byte 2
331
                        when READ_BYTE_2 =>
332
                                                        -- End of Read Byte 2 Cycle
333
                                                        if (bit_counter_end = '1') then
334
 
335
                                                                -- End of Read Cycles (NO ACK)
336
                                                                if (i_last_read = '1') then
337
                                                                        next_state <= READ_BYTE_2_NO_ACK;
338
 
339
                                                                -- New Read Cycles (ACK)
340
                                                                else
341
                                                                        next_state <= READ_BYTE_2_ACK;
342
                                                                end if;
343
 
344
                                                        else
345
                                                                next_state <= READ_BYTE_2;
346
                                                        end if;
347
 
348
                        -- Read Byte 2 ACK
349
                        when READ_BYTE_2_ACK => next_state <= READ_BYTE_1;
350
 
351
                        -- End of Read Cycles
352
                        when READ_BYTE_2_NO_ACK => next_state <= STOP_TX;
353
 
354
                        -- End of Transmission
355
                        when others => next_state <= IDLE;
356
                end case;
357
        end process;
358
 
359
        ---------------------
360
        -- I2C Bit Counter --
361
        ---------------------
362
        process(i_sys_clock)
363
        begin
364
                if rising_edge(i_sys_clock) then
365
 
366
                        -- I2C Clock Enable
367
                        if (i2c_clock_enable = '1') then
368
 
369
                                -- Increment I2C Bit Counter
370
                                if (state = WRITE_SLAVE_ADDR) or (state = WRITE_BYTE) or (state = READ_BYTE_1) or (state = READ_BYTE_2) then
371
                                        bit_counter <= bit_counter +1;
372
 
373
                                -- Reset I2C Bit Counter
374
                                else
375
                                        bit_counter <= (others => '0');
376
                                end if;
377
                        end if;
378
                end if;
379
    end process;
380
 
381
        -- I2C Bit Counter End
382
        bit_counter_end <= bit_counter(2) and bit_counter(1) and bit_counter(0);
383
 
384
        --------------------
385
        -- Pmod AD2 Ready --
386
        --------------------
387
    o_ready <= '1' when (state = IDLE) else '0';
388
 
389
        -----------------------------
390
        -- I2C SCL Output Register --
391
        -----------------------------
392
        process(i_sys_clock)
393
        begin
394
                if rising_edge(i_sys_clock) then
395
 
396
                        -- SCL High ('Z')
397
                        if (i2c_clock_enable_rising = '1') or (state = IDLE) then
398
                                scl_reg_out <= '1';
399
 
400
                        -- SCL Low ('0')
401
                        elsif (i2c_clock_enable_falling = '1') and (state /= STOP_TX) then
402
                                scl_reg_out <= '0';
403
                        end if;
404
                end if;
405
        end process;
406
 
407
        --------------------
408
        -- I2C SCL Output --
409
        --------------------
410
        -- ('0' or 'Z' values)
411
        io_scl <= '0' when scl_reg_out = '0' else TRANSMISSION_IDLE;
412
 
413
        -----------------------------
414
        -- I2C Data Write Register --
415
        -----------------------------
416
        process(i_sys_clock)
417
        begin
418
                if rising_edge(i_sys_clock) then
419
 
420
                        -- Load Inputs
421
            if (state = IDLE) then
422
                                data_write_reg <= i_addr & i_mode & i_config_byte;
423
 
424
                        -- I2C Clock Enable
425
                        elsif (i2c_clock_enable = '1') then
426
 
427
                                -- Left-Shift (when Write Slave Address or Write Byte)
428
                                if (state = WRITE_SLAVE_ADDR) or (state = WRITE_BYTE) then
429
                                        data_write_reg <= data_write_reg(14 downto 0) & data_write_reg(15);
430
                                end if;
431
                        end if;
432
                end if;
433
        end process;
434
 
435
        --------------------------
436
        -- I2C SDA Output Value --
437
        --------------------------
438
        process(state, i_last_read, data_write_reg)
439
        begin
440
                -- Start & Stop Transmission
441
                if (state = START_TX) or (state = STOP_TX) then
442
                        sda_out <= TRANSMISSION_START_BIT;
443
 
444
                -- Read Cycles ACK
445
                elsif (state = READ_BYTE_1_ACK) or (state = READ_BYTE_2_ACK) then
446
                        sda_out <= TRANSMISSION_ACK_BIT;
447
 
448
                -- Read Cycle NO ACK
449
                elsif (state = READ_BYTE_2_NO_ACK) then
450
                        sda_out <= TRANSMISSION_NACK_BIT;
451
 
452
                -- Write Slave Address or Write Byte Cycles
453
                elsif (state = WRITE_SLAVE_ADDR) or (state = WRITE_BYTE) then
454
                        sda_out <= data_write_reg(15);
455
 
456
                -- IDLE, Slave Address ACK, Write Byte ACK, Read Byte 1/2
457
                else
458
                        sda_out <= TRANSMISSION_DONT_CARE_BIT;
459
                end if;
460
        end process;
461
 
462
        --------------------
463
        -- I2C SDA Output --
464
        --------------------
465
        -- ('0' or 'Z' values)
466
        io_sda <= '0' when sda_out = '0' else TRANSMISSION_IDLE;
467
 
468
        -------------------
469
        -- I2C SDA Input --
470
        -------------------
471
        process(i_sys_clock)
472
        begin
473
                if rising_edge(i_sys_clock) then
474
 
475
                        -- I2C Clock Enable Rising Edge
476
                        if (i2c_clock_enable_rising = '1') then
477
                                sda_in_reg <= io_sda;
478
                        end if;
479
 
480
                end if;
481
        end process;
482
 
483
        --------------------
484
        -- Pmod AD2 Value --
485
        --------------------
486
        process(i_sys_clock)
487
        begin
488
                if rising_edge(i_sys_clock) then
489
 
490
                        -- I2C Clock Enable
491
                        if (i2c_clock_enable = '1') then
492
 
493
                                -- Read SDA Input
494
                                if (state = READ_BYTE_1) or (state = READ_BYTE_2) then
495
                                        data_read_reg <= data_read_reg(14 downto 0) & sda_in_reg;
496
                                end if;
497
 
498
                        end if;
499
                end if;
500
        end process;
501
        o_adc_value <= data_read_reg;
502
 
503
        --------------------------
504
        -- Pmod AD2 Valid Value --
505
        --------------------------
506
        process(i_sys_clock)
507
        begin
508
                if rising_edge(i_sys_clock) then
509
 
510
                        -- I2C Clock Enable
511
                        if (i2c_clock_enable = '1') then
512
 
513
                                -- Disable ADC Value Valid (New 2-byte Read Cycle)
514
                                if (state = START_TX) or (state = READ_BYTE_1) then
515
                                        data_read_reg_valid <= '0';
516
 
517
                                -- Enable ADC Value Valid Data (End of 2-byte Read Cycle)
518
                                elsif (state = READ_BYTE_2_ACK) or (state = READ_BYTE_2_NO_ACK) then
519
                                        data_read_reg_valid <= '1';
520
                                end if;
521
                        end if;
522
                end if;
523
        end process;
524
        o_adc_valid <= data_read_reg_valid;
525
 
526
end Behavioral;

powered by: WebSVN 2.1.0

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