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

Subversion Repositories smbus_controller

[/] [smbus_controller/] [trunk/] [hw/] [sources/] [SMBusController.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: 18/11/2024
4
-- Module Name: SMBusController
5
-- Description:
6
--      System Management Bus (SMBus) Controller, compatible with all 15 commands (from SMBus Speficiation 3.3.1)
7
--              Features:
8
--                      - Controller-Transmitter with Read/Write Operations
9
--                      - Command Byte activation/deactivation
10
--                      - PEC (Packet Error Code) activation/deactivation
11
--                      - Configurable Read/Write Data length
12
--                      - Bus Busy Detection
13
--                      - Bus Timeout Detection
14
--                      - Clock Stretching Detection
15
--                      - Multimaster (arbitration)
16
--                      - SMBus Classes (100kHz, 400kHz, 1MHz)
17
--
18
-- WARNING: /!\ Require Pull-Up on SMBCLK and SMBDAT pins /!\
19
--
20
-- Usage:
21
--      The Ready signal indicates no operation is on going and the SMBus Controller is waiting operation.
22
--              The Busy signal indicates operation is on going.
23
--      Reset input can be trigger at any time to reset the SMBus Controller to the IDLE state.
24
--              1. Set all necessary inputs
25
--                      * Mode (Write only, Read only or Write-then-Read)
26
--                      * SMBus Slave Address Delay
27
--                      * Enable/Disable SMBus Command to Write
28
--                      * Enable/Disable SMBus PEC (Packet Error Code)
29
--                      * Set the number of byte to Write (in Write-only and  Write-then-Read modes)
30
--                      * Set the number of byte to Read (in Read-only and  Write-then-Read modes)
31
--                      * Set SMBus Command Value (SMBus Commande enable)
32
--                      * Set SMBus Data to Write
33
--      2. Asserts Start input. The Ready signal is de-asserted and the Busy signal is asserted.
34
--              3. SMBus Controller re-asserts the Ready signal at the end of transmission (Controller is ready for a new transmission)
35
--              4. The Data Read value is available when its validity signal is asserted
36
--              5. If an Error occur during transmission, the Error signal is asserterd
37
--
38
-- Generics
39
--              INPUT_CLOCK_FREQ: Module Input Clock Frequency
40
--              SMBUS_CLOCK_FREQ: SMBus Serial Clock Frequency
41
--              SMBUS_CLASS: SMBus Class (100kHz, 400kHz, 1MHz)
42
--              MAX_DATA_BIT_LENGTH: Maximum Length of the SMBus Data in bits
43
--
44
-- Ports
45
--              Input   -       i_clock: Module Input Clock
46
--              Input   -       i_reset: Reset ('0': No Reset, '1': Reset)
47
--              Input   -       i_start: Start SMBus Transmission ('0': No Start, '1': Start)
48
--              Input   -       i_mode: Operation Mode ("00": Write-Only, "11": Read-Only, "01": Write-then-Read)
49
--              Input   -       i_slave_addr: SMBus Slave Address (7 bits)
50
--              Input   -       i_cmd_enable: Command Byte Enable ('0': Disable, '1': Enable)
51
--              Input   -       i_pec_enable: Packet Error Code Enable ('0': Disable, '1': Enable)
52
--              Input   -       i_data_write_byte_number: Number of Data Bytes to Write
53
--              Input   -       i_data_read_byte_number: Number of Data Bytes to Read
54
--              Input   -       i_cmd: Command Value to Write
55
--              Input   -       i_data_write: Data Value to Write
56
--              Output  -       o_data_read: Read Data Value
57
--              Output  -       o_data_read_valid: Validity of the Read Data Value ('0': Not Valid, '1': Valid)
58
--              Output  -       o_ready: Ready State of SMBus Controller ('0': Not Ready, '1': Ready)
59
--              Output  -       o_error: Error State of SMBus Controller ('0': No Error, '1': Error)
60
--              Output  -       o_busy: Busy State of SMBus Controller ('0': Not Busy, '1': Busy)
61
--              In/Out  -       io_smbclk: SMBus Serial Clock ('0'-'Z'(as '1') values, working with Pull-Up)
62
--              In/Out  -       io_smbdat: SMBus Serial Data ('0'-'Z'(as '1') values, working with Pull-Up)
63
------------------------------------------------------------------------
64
 
65
LIBRARY IEEE;
66
USE IEEE.STD_LOGIC_1164.ALL;
67
USE IEEE.NUMERIC_STD.ALL;
68
 
69
ENTITY SMBusController is
70
 
71
GENERIC(
72
        INPUT_CLOCK_FREQ: INTEGER := 12_000_000;
73
        SMBUS_CLOCK_FREQ: INTEGER := 100_000;
74
        SMBUS_CLASS: INTEGER := 100_000;
75
        MAX_DATA_BIT_LENGTH: INTEGER := 8
76
);
77
 
78
PORT(
79
        i_clock: IN STD_LOGIC;
80
    i_reset: IN STD_LOGIC;
81
        i_start: IN STD_LOGIC;
82
        i_mode: IN STD_LOGIC_VECTOR(1 downto 0);
83
        i_slave_addr: IN STD_LOGIC_VECTOR(6 downto 0);
84
        i_cmd_enable: IN STD_LOGIC;
85
        i_pec_enable: IN STD_LOGIC;
86
        i_data_write_byte_number: IN INTEGER range 0 to MAX_DATA_BIT_LENGTH/8;
87
        i_data_read_byte_number: IN INTEGER range 0 to MAX_DATA_BIT_LENGTH/8;
88
        i_cmd: IN STD_LOGIC_VECTOR(7 downto 0);
89
        i_data_write: IN STD_LOGIC_VECTOR(MAX_DATA_BIT_LENGTH-1 downto 0);
90
        o_data_read: OUT STD_LOGIC_VECTOR(MAX_DATA_BIT_LENGTH-1 downto 0);
91
        o_data_read_valid: OUT STD_LOGIC;
92
        o_ready: OUT STD_LOGIC;
93
        o_error: OUT STD_LOGIC;
94
        o_busy: OUT STD_LOGIC;
95
        io_smbclk: INOUT STD_LOGIC;
96
        io_smbdat: INOUT STD_LOGIC
97
);
98
 
99
END SMBusController;
100
 
101
ARCHITECTURE Behavioral of SMBusController is
102
 
103
------------------------------------------------------------------------
104
-- Component Declarations
105
------------------------------------------------------------------------
106
COMPONENT SMBusAnalyzer is
107
 
108
        GENERIC(
109
                INPUT_CLOCK_FREQ: INTEGER := 12_000_000;
110
                SMBUS_CLASS: INTEGER := 100_000
111
        );
112
 
113
        PORT(
114
                i_clock: IN STD_LOGIC;
115
                i_reset: IN STD_LOGIC;
116
                i_smbclk_controller: IN STD_LOGIC;
117
                i_smbclk_line: IN STD_LOGIC;
118
                i_smbdat_controller: IN STD_LOGIC;
119
                i_smbdat_line: IN STD_LOGIC;
120
                o_smbus_busy: OUT STD_LOGIC;
121
                o_smbus_timeout: OUT STD_LOGIC;
122
                o_smbus_arbitration: OUT STD_LOGIC;
123
                o_smbclk_stretching: OUT STD_LOGIC
124
        );
125
 
126
END COMPONENT;
127
 
128
COMPONENT SMBusPec is
129
 
130
        PORT(
131
                i_clock: IN STD_LOGIC;
132
                i_reset: IN STD_LOGIC;
133
                i_enable: IN STD_LOGIC;
134
                i_data: IN STD_LOGIC_VECTOR(7 downto 0);
135
                o_pec: OUT STD_LOGIC_VECTOR(7 downto 0)
136
        );
137
 
138
END COMPONENT;
139
 
140
------------------------------------------------------------------------
141
-- Constant Declarations
142
------------------------------------------------------------------------
143
-- SMBus Clock Dividers
144
constant CLOCK_DIV: INTEGER := INPUT_CLOCK_FREQ / SMBUS_CLOCK_FREQ;
145
constant CLOCK_DIV_1_4: INTEGER := CLOCK_DIV /4;
146
constant CLOCK_DIV_3_4: INTEGER := CLOCK_DIV - CLOCK_DIV_1_4;
147
 
148
-- SMBus Controller Modes ("00": Write-Only, "11": Read-Only, "01": Write-then-Read)
149
constant SMBUS_WRITE_ONLY_MODE: STD_LOGIC_VECTOR(1 downto 0) := "00";
150
constant SMBUS_READ_ONLY_MODE: STD_LOGIC_VECTOR(1 downto 0) := "11";
151
constant SMBUS_WRITE_THEN_READ_MODE: STD_LOGIC_VECTOR(1 downto 0) := "01";
152
 
153
-- SMBus Modes ('0': Write, '1': Read)
154
constant SMBUS_WRITE_MODE: STD_LOGIC := '0';
155
constant SMBUS_READ_MODE: STD_LOGIC := '1';
156
 
157
-- SMBus Bit Counter End (8-bit per cycle)
158
constant SMBUS_BIT_COUNTER_END: UNSIGNED(3 downto 0) := "0111";
159
 
160
-- SMBus SMBDAT Left Shift Value
161
constant LEFT_SHIFT_EMPTY_VALUE: STD_LOGIC := '0';
162
 
163
-- SMBus IDLE ('Z' with Pull-Up)
164
constant TRANSMISSION_IDLE: STD_LOGIC := 'Z';
165
 
166
-- SMBus Transmission Don't Care Bit
167
constant TRANSMISSION_DONT_CARE_BIT: STD_LOGIC := '1';
168
 
169
-- SMBus Transmission Start Bit
170
constant TRANSMISSION_START_BIT: STD_LOGIC := '0';
171
 
172
-- SMBus Transmission ACK Bit
173
constant TRANSMISSION_ACK_BIT: STD_LOGIC := '0';
174
 
175
-- SMBus Transmission NACK Bit
176
constant TRANSMISSION_NACK_BIT: STD_LOGIC := '1';
177
 
178
------------------------------------------------------------------------
179
-- Signal Declarations
180
------------------------------------------------------------------------
181
-- SMBus Controller States
182
TYPE smbusState is (IDLE, START_TX,
183
                                        WRITE_SLAVE_ADDR_W, WRITE_CMD_VALUE, WRITE_REG_VALUE, WRITE_PEC_VALUE,
184
                                        RE_START_TX,
185
                                        WRITE_SLAVE_ADDR_R, READ_REG_VALUE, READ_PEC_VALUE,
186
                                        STOP_TX);
187
signal state: smbusState := IDLE;
188
signal next_state: smbusState;
189
 
190
-- SMBus Clock Management
191
signal clock_divider: INTEGER range 0 to CLOCK_DIV-1 := 0;
192
signal clock_enable: STD_LOGIC := '0';
193
signal clock_enable_1_4: STD_LOGIC := '0';
194
signal clock_enable_3_4: STD_LOGIC := '0';
195
 
196
-- SMBus Bit Counter (8 bits per phass + 1 bit ACK/ACK)
197
signal bit_counter: UNSIGNED(3 downto 0) := (others => '0');
198
signal bit_counter_end: STD_LOGIC := '0';
199
 
200
-- SMBus Bit/Byte Counter
201
signal byte_counter: INTEGER range 0 to MAX_DATA_BIT_LENGTH/8 := 0;
202
signal byte_counter_end: STD_LOGIC := '0';
203
 
204
-- SMBus SMBCLK & SMBDAT IOs
205
signal smbclk_in: STD_LOGIC := '0';
206
signal smbclk_out: STD_LOGIC := '0';
207
signal smbclk_out_reg: STD_LOGIC := '0';
208
signal smbdat_in: STD_LOGIC := '0';
209
signal smbdat_in_reg: STD_LOGIC_VECTOR(MAX_DATA_BIT_LENGTH-1 downto 0) := (others => '0');
210
signal smbdat_in_valid: STD_LOGIC := '0';
211
signal smbdat_out: STD_LOGIC := '0';
212
signal smbdat_out_reg: STD_LOGIC_VECTOR(MAX_DATA_BIT_LENGTH-1 downto 0) := (others => '0');
213
 
214
-- SMBus PEC Signals
215
signal smbus_pec_reset: STD_LOGIC := '0';
216
signal smbus_pec_enable: STD_LOGIC := '0';
217
signal smbus_pec_input: STD_LOGIC_VECTOR(7 downto 0) := (others => '0');
218
signal smbus_pec_value: STD_LOGIC_VECTOR(7 downto 0) := (others => '0');
219
signal smbus_pec_received: STD_LOGIC_VECTOR(7 downto 0) := (others => '0');
220
 
221
-- SMBus Analyzer Signals
222
signal smbus_busy: STD_LOGIC := '0';
223
signal smbus_timeout: STD_LOGIC := '0';
224
signal smbus_arbitration: STD_LOGIC := '0';
225
signal smbclk_stretching: STD_LOGIC := '0';
226
 
227
-- SMBus Error
228
signal smbus_error: STD_LOGIC := '0';
229
 
230
------------------------------------------------------------------------
231
-- Module Implementation
232
------------------------------------------------------------------------
233
begin
234
 
235
        -------------------------
236
        -- SMBus Clock Divider --
237
        -------------------------
238
        process(i_clock)
239
        begin
240
                if rising_edge(i_clock) then
241
 
242
                        -- Reset Clock Divider
243
                        if (i_reset = '1') or (clock_divider = CLOCK_DIV-1) then
244
                                clock_divider <= 0;
245
 
246
                        -- Increment Clock Divider (Waiting no SMBCLK Stretching)
247
                        elsif (smbclk_stretching = '0') then
248
                                clock_divider <= clock_divider +1;
249
                        end if;
250
                end if;
251
        end process;
252
 
253
        -------------------------
254
        -- SMBus Clock Enables --
255
        -------------------------
256
        process(i_clock)
257
        begin
258
                if rising_edge(i_clock) then
259
 
260
                        -- SMBCLK Stretching (Waiting no SMBCLK Stretching)
261
                        if (smbclk_stretching = '0') then
262
 
263
                                -- Clock Enable
264
                                if (clock_divider = CLOCK_DIV-1) then
265
                                        clock_enable <= '1';
266
                                else
267
                                        clock_enable <= '0';
268
                                end if;
269
 
270
                                -- Clock Enable (1/4)
271
                                if (clock_divider = CLOCK_DIV_1_4-1) then
272
                                        clock_enable_1_4 <= '1';
273
                                else
274
                                        clock_enable_1_4 <= '0';
275
                                end if;
276
 
277
                                -- Clock Enable (3/4)
278
                                if (clock_divider = CLOCK_DIV_3_4-1) then
279
                                        clock_enable_3_4 <= '1';
280
                                else
281
                                        clock_enable_3_4 <= '0';
282
                                end if;
283
 
284
                        -- SMBCLK Stretching (Disable all Clock Enables)
285
                        else
286
                                clock_enable <= '0';
287
                                clock_enable_1_4 <= '0';
288
                                clock_enable_3_4 <= '0';
289
 
290
                        end if;
291
                end if;
292
        end process;
293
 
294
        -------------------------
295
        -- SMBus State Machine --
296
        -------------------------
297
    -- SMBus State
298
        process(i_clock)
299
        begin
300
                if rising_edge(i_clock) then
301
 
302
                        -- Reset State
303
                        if (i_reset = '1') then
304
                                state <= IDLE;
305
 
306
                        -- Next State (when Clock Enable)
307
                        elsif (clock_enable = '1') then
308
                                state <= next_state;
309
                        end if;
310
                end if;
311
        end process;
312
 
313
    -- SMBus Next State
314
        process(state, i_start, i_mode, i_cmd_enable, i_pec_enable, i_data_write_byte_number, i_data_read_byte_number, smbus_busy, smbus_error, smbus_arbitration, bit_counter_end, byte_counter_end)
315
        begin
316
 
317
                case state is
318
                        when IDLE =>    if (i_start = '1') and (smbus_busy = '0') then
319
                                                                next_state <= START_TX;
320
                                                        else
321
                                                                next_state <= IDLE;
322
                                                        end if;
323
 
324
                        -- Start Transmission
325
                        when START_TX =>
326
                                                        -- Read-Only Mode
327
                                                        if (i_mode = SMBUS_READ_ONLY_MODE) then
328
                                                                next_state <= WRITE_SLAVE_ADDR_R;
329
 
330
                                                        -- Write-Only or Write-then-Read Modes
331
                                                        else
332
                                                                next_state <= WRITE_SLAVE_ADDR_W;
333
                                                        end if;
334
 
335
                        -- Write Slave Address (Write Mode)
336
                        when WRITE_SLAVE_ADDR_W =>
337
                                                        -- Error
338
                                                        if (smbus_error = '1') then
339
                                                                next_state <= STOP_TX;
340
 
341
                                                        -- End of Write Slave Addr Cycle
342
                                                        elsif (bit_counter_end = '1') then
343
 
344
                                                                -- Write Command
345
                                                                if (i_cmd_enable = '1') then
346
                                                                        next_state <= WRITE_CMD_VALUE;
347
 
348
                                                                -- No Byte to Write
349
                                                                elsif (i_data_write_byte_number = 0) then
350
                                                                        next_state <= STOP_TX;
351
 
352
                                                                -- Write Value
353
                                                                else
354
                                                                        next_state <= WRITE_REG_VALUE;
355
                                                                end if;
356
 
357
                                                        -- Master Loses Arbitration (during Write Cycle)
358
                                                        elsif (smbus_arbitration = '0') then
359
                                                                next_state <= IDLE;
360
 
361
                                                        else
362
                                                                next_state <= WRITE_SLAVE_ADDR_W;
363
                                                        end if;
364
 
365
                        -- Write Command Value (Write Mode)
366
                        when WRITE_CMD_VALUE =>
367
                                                        -- Error
368
                                                        if (smbus_error = '1') then
369
                                                                next_state <= STOP_TX;
370
 
371
                                                        -- End of Write Command Value Cycle
372
                                                        elsif (bit_counter_end = '1') then
373
 
374
                                                                -- At least 1 Byte to Write
375
                                                                if (i_data_write_byte_number /= 0) then
376
                                                                        next_state <= WRITE_REG_VALUE;
377
 
378
                                                                -- Write-then-Read Mode
379
                                                                elsif (i_mode = SMBUS_WRITE_THEN_READ_MODE) then
380
                                                                        next_state <= RE_START_TX;
381
 
382
                                                                -- End of Transmission (Should NOT Happen)
383
                                                                else
384
                                                                        next_state <= STOP_TX;
385
                                                                end if;
386
 
387
                                                        -- Master Loses Arbitration (during Write Cycle)
388
                                                        elsif (smbus_arbitration = '0') then
389
                                                                next_state <= IDLE;
390
 
391
                                                        else
392
                                                                next_state <= WRITE_CMD_VALUE;
393
                                                        end if;
394
 
395
                        -- Write Register Value (Write Mode)
396
                        when WRITE_REG_VALUE =>
397
                                                        -- Error
398
                                                        if (smbus_error = '1') then
399
                                                                next_state <= STOP_TX;
400
 
401
                                                        -- End of Write Value Cycle
402
                                                        elsif (byte_counter_end = '1') then
403
 
404
                                                                -- Write-then-Read Mode
405
                                                                if (i_mode = SMBUS_WRITE_THEN_READ_MODE) then
406
                                                                        next_state <= RE_START_TX;
407
 
408
                                                                -- Write PEC Value
409
                                                                elsif (i_pec_enable = '1') then
410
                                                                        next_state <= WRITE_PEC_VALUE;
411
 
412
                                                                -- Stop Transmission
413
                                                                else
414
                                                                        next_state <= STOP_TX;
415
                                                                end if;
416
 
417
                                                        -- Master Loses Arbitration (during Write Cycle)
418
                                                        elsif (bit_counter_end = '0') and (smbus_arbitration = '0') then
419
                                                                next_state <= IDLE;
420
 
421
                                                        else
422
                                                                next_state <= WRITE_REG_VALUE;
423
                                                        end if;
424
 
425
                        -- Write PEC Value (Write Mode)
426
                        when WRITE_PEC_VALUE =>
427
                                                        -- End of Write PEC Value Cycle (Stop Transmission)
428
                                                        if (bit_counter_end = '1') then
429
                                                                next_state <= STOP_TX;
430
 
431
                                                        -- Master Loses Arbitration (during Write Cycle)
432
                                                        elsif (smbus_arbitration = '0') then
433
                                                                next_state <= IDLE;
434
 
435
                                                        else
436
                                                                next_state <= WRITE_PEC_VALUE;
437
                                                        end if;
438
 
439
                        -- Re-Start Transmission
440
                        when RE_START_TX => next_state <= WRITE_SLAVE_ADDR_R;
441
 
442
                        -- Write Slave Address (Read Mode)
443
                        when WRITE_SLAVE_ADDR_R =>
444
                                                        -- Error
445
                                                        if (smbus_error = '1') then
446
                                                                next_state <= STOP_TX;
447
 
448
                                                        -- End of Write Slave Addr Cycle
449
                                                        elsif (bit_counter_end = '1') then
450
 
451
                                                                -- No Byte to Write
452
                                                                if (i_data_read_byte_number = 0) then
453
                                                                        next_state <= STOP_TX;
454
                                                                else
455
                                                                        next_state <= READ_REG_VALUE;
456
                                                                end if;
457
 
458
                                                        -- Master Loses Arbitration (during Write Cycle)
459
                                                        elsif (smbus_arbitration = '0') then
460
                                                                next_state <= IDLE;
461
 
462
                                                        else
463
                                                                next_state <= WRITE_SLAVE_ADDR_R;
464
                                                        end if;
465
 
466
                        -- Read Register Value (Read Mode)
467
                        when READ_REG_VALUE =>
468
                                                        -- End of Read Value Cycle
469
                                                        if (byte_counter_end = '1') then
470
 
471
                                                                -- Read PEC Value
472
                                                                if (i_pec_enable = '1') then
473
                                                                        next_state <= READ_PEC_VALUE;
474
 
475
                                                                -- Stop Transmission
476
                                                                else
477
                                                                        next_state <= STOP_TX;
478
                                                                end if;
479
 
480
                                                        else
481
                                                                next_state <= READ_REG_VALUE;
482
                                                        end if;
483
 
484
                        -- Read PEC Value (Read Mode)
485
                        when READ_PEC_VALUE =>
486
                                                        -- End of Read PEC Value Cycle
487
                                                        if (bit_counter_end = '1') then
488
                                                                next_state <= STOP_TX;
489
                                                        else
490
                                                                next_state <= READ_PEC_VALUE;
491
                                                        end if;
492
 
493
                        -- Stop Transmission
494
                        when others => next_state <= IDLE;
495
                end case;
496
        end process;
497
 
498
        -----------------------
499
        -- SMBus Bit Counter --
500
        -----------------------
501
        process(i_clock)
502
        begin
503
                if rising_edge(i_clock) then
504
 
505
                        -- Clock Enable
506
                        if (clock_enable = '1') then
507
 
508
                                -- Reset Counter
509
                                if (state = IDLE) or (state = START_TX) or (state = RE_START_TX) or (state = STOP_TX) or (bit_counter_end = '1') then
510
                                        bit_counter <= (others => '0');
511
 
512
                                -- Increment Counter
513
                                else
514
                                        bit_counter <= bit_counter +1;
515
                                end if;
516
                        end if;
517
                end if;
518
    end process;
519
 
520
        -- Bit Counter End
521
        bit_counter_end <= bit_counter(3);
522
 
523
        ------------------------
524
        -- SMBus Byte Counter --
525
        ------------------------
526
        process(i_clock)
527
        begin
528
                if rising_edge(i_clock) then
529
 
530
                        -- Clock Enable
531
                        if (clock_enable = '1') then
532
 
533
                                -- Reset Byte Counter
534
                                if (state = START_TX) or (state = RE_START_TX) then
535
                                        byte_counter <= 0;
536
 
537
                                -- Increment Byte Counter (bii counter to 7 for anticipation)
538
                                elsif ((state = WRITE_REG_VALUE) or (state = READ_REG_VALUE)) and (bit_counter = SMBUS_BIT_COUNTER_END) then
539
                                        byte_counter <= byte_counter +1;
540
                                end if;
541
                        end if;
542
                end if;
543
        end process;
544
 
545
        -- Byte Counter End
546
        byte_counter_end <= '1' when (state = WRITE_REG_VALUE) and (byte_counter = i_data_write_byte_number) else
547
                                                '1' when (state = READ_REG_VALUE) and (byte_counter = i_data_read_byte_number) else
548
                                                '0';
549
 
550
        ----------------------------------
551
        -- SMBus SMBCLK Output Register --
552
        ----------------------------------
553
        process(i_clock)
554
        begin
555
                if rising_edge(i_clock) then
556
 
557
                        -- SMBCLK High ('Z')
558
                        if (clock_enable_1_4 = '1') or (state = IDLE) then
559
                                smbclk_out_reg <= '1';
560
 
561
                        -- SMBCLK Low ('0')
562
                        elsif (clock_enable_3_4 = '1') and (state /= RE_START_TX) and (state /= STOP_TX) then
563
                                smbclk_out_reg <= '0';
564
                        end if;
565
                end if;
566
        end process;
567
        smbclk_out <= smbclk_out_reg;
568
 
569
        ---------------------------------
570
        -- SMBus SMBDAT Write Register --
571
        ---------------------------------
572
        process(i_clock)
573
        begin
574
                if rising_edge(i_clock) then
575
 
576
                        -- Clock Enable
577
                        if (clock_enable = '1') then
578
 
579
                                -- Load Slave Address (Write/Read Mode)
580
                                if (state = START_TX) then
581
 
582
                                        if (i_mode = SMBUS_READ_ONLY_MODE) then
583
                                                -- Load Slave Address (Read Mode)
584
                                                smbdat_out_reg(MAX_DATA_BIT_LENGTH-1 downto MAX_DATA_BIT_LENGTH-8) <= i_slave_addr & SMBUS_READ_MODE;
585
                                        else
586
                                                -- Load Slave Address (Write Mode)
587
                                                smbdat_out_reg(MAX_DATA_BIT_LENGTH-1 downto MAX_DATA_BIT_LENGTH-8) <= i_slave_addr & SMBUS_WRITE_MODE;
588
                                        end if;
589
 
590
                                -- Load Slave Address (Read Mode)
591
                                elsif (state = RE_START_TX) then
592
                                        smbdat_out_reg(MAX_DATA_BIT_LENGTH-1 downto MAX_DATA_BIT_LENGTH-8) <= i_slave_addr & SMBUS_READ_MODE;
593
 
594
                                -- Load Write Command value / Write Register Value
595
                                elsif (state = WRITE_SLAVE_ADDR_W) and (bit_counter_end = '1') then
596
 
597
                                        -- Load Write Command value
598
                                        if (i_cmd_enable = '1') then
599
                                                smbdat_out_reg(MAX_DATA_BIT_LENGTH-1 downto MAX_DATA_BIT_LENGTH-8) <= i_cmd;
600
 
601
                                        -- Load Write Register Value
602
                                        else
603
                                                smbdat_out_reg <= i_data_write;
604
                                        end if;
605
 
606
                                -- Load Write Register Value
607
                                elsif (state = WRITE_CMD_VALUE) and (bit_counter_end = '1') then
608
                                        smbdat_out_reg <= i_data_write;
609
 
610
                                -- Load Write PEC Value
611
                                elsif (state = WRITE_REG_VALUE) and (i_pec_enable = '1') and (byte_counter_end = '1') then
612
                                        smbdat_out_reg(MAX_DATA_BIT_LENGTH-1 downto MAX_DATA_BIT_LENGTH-8) <= smbus_pec_value;
613
 
614
                                -- Left-Shift
615
                                else
616
                                        smbdat_out_reg <= smbdat_out_reg(MAX_DATA_BIT_LENGTH-2 downto 0) & LEFT_SHIFT_EMPTY_VALUE;
617
                                end if;
618
                        end if;
619
                end if;
620
        end process;
621
 
622
        -------------------------------
623
        -- SMBus SMBDAT Output Value --
624
        -------------------------------
625
        process(state, bit_counter_end, byte_counter_end, smbdat_out_reg)
626
        begin
627
                -- Start & Stop Transmission
628
                if (state = START_TX) or (state = STOP_TX) then
629
                        smbdat_out <= TRANSMISSION_START_BIT;
630
 
631
                -- Read Cycles
632
                elsif (state = READ_REG_VALUE) or (state = READ_PEC_VALUE) then
633
 
634
                        -- Last Read Cycle
635
                        if (byte_counter_end = '1') then
636
                                smbdat_out <= TRANSMISSION_NACK_BIT;
637
 
638
                        -- End of Read Phase
639
                        elsif (bit_counter_end = '1') then
640
                                smbdat_out <= TRANSMISSION_ACK_BIT;
641
 
642
                        -- Read In-Progress
643
                        else
644
                                smbdat_out <= TRANSMISSION_DONT_CARE_BIT;
645
                        end if;
646
 
647
                -- IDLE / Re-Start Cycles / End of Write Cycles
648
                elsif (state = IDLE) or (state = RE_START_TX) or (bit_counter_end = '1') then
649
                        smbdat_out <= TRANSMISSION_DONT_CARE_BIT;
650
 
651
                -- Write Cycles
652
                else
653
                        smbdat_out <= smbdat_out_reg(7);
654
                end if;
655
        end process;
656
 
657
        -------------------------------
658
        -- SMBus SMBCLK & SMBDAT IOs --
659
        -------------------------------
660
        -- SMBus SMBCLK Input (for Simulation only: '0' when io_smbclk = '0' else '1')
661
        smbclk_in <= io_smbclk;
662
 
663
        -- SMBus SMBCLK Output ('0' or 'Z' values)
664
        io_smbclk <= '0' when smbclk_out = '0' else TRANSMISSION_IDLE;
665
 
666
        -- SMBus SMBDAT Input (for Simulation only: '0' when io_smbdat = '0' else '1')
667
        smbdat_in <= '0' when io_smbdat = '0' else '1';
668
 
669
        -- SMBus SMBDAT Output ('0' or 'Z' values)
670
        io_smbdat <= '0' when smbdat_out = '0' else TRANSMISSION_IDLE;
671
 
672
        --------------------
673
        -- SMBus Analyzer --
674
        --------------------
675
        inst_smbusAnalyzer: SMBusAnalyzer
676
                generic map (
677
                        INPUT_CLOCK_FREQ => INPUT_CLOCK_FREQ,
678
                        SMBUS_CLASS => SMBUS_CLASS)
679
 
680
                port map (
681
                        i_clock => i_clock,
682
                        i_reset => i_reset,
683
                        i_smbclk_controller => smbclk_out,
684
                        i_smbclk_line => smbclk_in,
685
                        i_smbdat_controller => smbdat_out,
686
                        i_smbdat_line => smbdat_in,
687
                        o_smbus_busy => smbus_busy,
688
                        o_smbus_timeout => smbus_timeout,
689
                        o_smbus_arbitration => smbus_arbitration,
690
                        o_smbclk_stretching => smbclk_stretching);
691
 
692
        ----------------------
693
        -- SMBus Read Value --
694
        ----------------------
695
        process(i_clock)
696
        begin
697
                if rising_edge(i_clock) then
698
 
699
                        -- Clock Enable
700
                        if (clock_enable = '1') then
701
 
702
                                -- Not End of Read Register Value Phase
703
                                if (state = READ_REG_VALUE) and (bit_counter_end = '0') then
704
                                        smbdat_in_reg <= smbdat_in_reg(MAX_DATA_BIT_LENGTH-2 downto 0) & smbdat_in;
705
                                end if;
706
                        end if;
707
                end if;
708
        end process;
709
        o_data_read <= smbdat_in_reg;
710
 
711
        ----------------------------
712
        -- SMBus Read Value Valid --
713
        ----------------------------
714
        process(i_clock)
715
        begin
716
                if rising_edge(i_clock) then
717
 
718
                        -- End of Transmission
719
                        if (state = STOP_TX) then
720
 
721
                                -- Read Value with PEC
722
                                if (i_pec_enable = '1') then
723
 
724
                                        -- Verify PEC (Read Value Valid only if PEC OK)
725
                                        if (smbus_pec_value = smbus_pec_received) then
726
                                                smbdat_in_valid <= '1';
727
                                        else
728
                                                smbdat_in_valid <= '0';
729
                                        end if;
730
 
731
                                -- Read Value without PEC
732
                                else
733
                                        smbdat_in_valid <= '1';
734
                                end if;
735
 
736
                        -- Disable Read Value Valid (New cycle)
737
                        elsif (state /= IDLE) then
738
                                smbdat_in_valid <= '0';
739
                        end if;
740
 
741
                end if;
742
        end process;
743
        o_data_read_valid <= smbdat_in_valid;
744
 
745
        --------------------------
746
        -- SMBus PEC Controller --
747
        --------------------------
748
        -- SMBus PEC Reset (when Start Transmission)
749
        smbus_pec_reset <= '1' when state = START_TX else '0';
750
 
751
        -- SMBus PEC Enable Handler
752
        process(i_clock)
753
        begin
754
                if rising_edge(i_clock) then
755
 
756
                        -- Clock Enable & PEC Enable & Write / Read Cycles
757
                        if (clock_enable_1_4 = '1') and (i_pec_enable = '1') and (
758
                                (state = WRITE_SLAVE_ADDR_W) or (state = WRITE_CMD_VALUE) or (state = WRITE_REG_VALUE) or
759
                                (state = WRITE_SLAVE_ADDR_R) or (state = READ_REG_VALUE)) and (bit_counter_end = '1') then
760
 
761
                                smbus_pec_enable <= '1';
762
                        else
763
                                smbus_pec_enable <= '0';
764
                        end if;
765
                end if;
766
        end process;
767
 
768
        -- SMBus PEC Input Handler
769
        process(i_clock)
770
        begin
771
                if rising_edge(i_clock) then
772
 
773
                        -- Clock Enable
774
                        if (clock_enable = '1') then
775
 
776
                                -- Write Cycles
777
                                if (state = WRITE_SLAVE_ADDR_W) or (state = WRITE_CMD_VALUE) or (state = WRITE_REG_VALUE) or (state = WRITE_SLAVE_ADDR_R) then
778
                                        smbus_pec_input <= smbus_pec_input(6 downto 0) & smbdat_out;
779
 
780
                                -- Read Cycles
781
                                elsif (state = READ_REG_VALUE) then
782
                                        smbus_pec_input <= smbus_pec_input(6 downto 0) & smbdat_in;
783
                                end if;
784
                        end if;
785
                end if;
786
        end process;
787
 
788
        -- SMBus Received PEC Handler
789
        process(i_clock)
790
        begin
791
                if rising_edge(i_clock) then
792
 
793
                        -- Clock Enable
794
                        if (clock_enable = '1') then
795
 
796
                                -- Not End of Read PEC Value Phase
797
                                if (i_pec_enable = '1') and (bit_counter_end = '0') then
798
                                        smbus_pec_received <= smbus_pec_received(6 downto 0) & smbdat_in;
799
                                end if;
800
                        end if;
801
                end if;
802
        end process;
803
 
804
        ------------------------
805
        -- SMBus PEC Instance --
806
        ------------------------
807
        inst_smbusPec: SMBusPec
808
                port map (
809
                        i_clock => i_clock,
810
                        i_reset => smbus_pec_reset,
811
                        i_enable => smbus_pec_enable,
812
                        i_data => smbus_pec_input,
813
                        o_pec => smbus_pec_value);
814
 
815
        ------------------------
816
        -- SMBus Ready Status --
817
        ------------------------
818
        o_ready <= '1' when state = IDLE and smbus_busy = '0' else '0';
819
 
820
        ------------------------
821
        -- SMBus Busy Status --
822
        ------------------------
823
        o_busy <= smbus_busy;
824
 
825
        ------------------------
826
        -- SMBus Error Status --
827
        ------------------------
828
        process(i_clock)
829
        begin
830
                if rising_edge(i_clock) then
831
 
832
                        -- Disable Error Flag (New cycle)
833
                        if (state = START_TX) then
834
                                smbus_error <= '0';
835
 
836
                        -- Timeout
837
                        elsif (smbus_timeout = '1') then
838
                                smbus_error <= '1';
839
 
840
                        -- Write Cycles
841
                        elsif (state = WRITE_SLAVE_ADDR_W) or (state = WRITE_CMD_VALUE) or (state = WRITE_REG_VALUE) or (state = WRITE_PEC_VALUE) or (state = WRITE_SLAVE_ADDR_R) then
842
 
843
                                -- No ACK at the End of Write Cycle
844
                                if (bit_counter_end = '1') and (smbdat_in /= TRANSMISSION_ACK_BIT) then
845
                                        smbus_error <= '1';
846
                                end if;
847
 
848
                        -- Received PEC Error (only in Read or Write-then-Read modes)
849
                        elsif (state = STOP_TX) and (i_pec_enable = '1') and (smbus_pec_value /= smbus_pec_received) then
850
                                smbus_error <= '1';
851
                        end if;
852
                end if;
853
        end process;
854
        o_error <= smbus_error;
855
 
856
end Behavioral;

powered by: WebSVN 2.1.0

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