当前位置:   article > 正文

IIC(I2C)总线 FPGA Verilog HDL_i2c fpga verilog hdl

i2c fpga verilog hdl

IIC(I2C)总线 FPGA Verilog HDL

配置文件:根据具体的IIC设备改一下时钟频率就可以产生正确的时钟波形

`define SYS_CLK  50_000_000
`define SCL_CLK  400_000

`define COUNT_MAX (`SYS_CLK/`SCL_CLK)

`define HALF  (`COUNT_MAX/2 -1)

`define H_HALF  (`HALF/2)
`define NEG     (`HALF +1)
`define L_HALF  (`H_HALF+NEG)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

RTL代码:

`include "config.v"
module I2C(
    clk,               //system clk 50MHZ
	rstn,              //active low
	data_in,           
    data_out,
	sda,
	scl,
	wr,               //wr=0   write; wr=1  read
	fail,
	req,
	address,
	wr_done,
	rd_done
);


    input clk;
	input rstn;
	input [7:0]data_in;
	input wr;
	input req;
	input [7:0] address;
	
	output [0:0]scl;
	output reg[7:0] data_out;
	
	inout sda;
	output fail;
	output rd_done;
	output wr_done;
	
	reg [7:0] clk_cnt;
	
	wire sda_in;
	reg sda_out;
	
	reg [29:0] state;
	
	reg [0:0]  sda_mode;
	wire clk_en;
	
	parameter IN   =0,
	          OUT  =1;
				 
				 
	parameter         idle      =30'b00_0000_0000_0000_0000_0000_0000_0001,
	                  start     =30'b00_0000_0000_0000_0000_0000_0000_0010,
					  mode0     =30'b00_0000_0000_0000_0000_0000_0000_0100,
					  mode1     =30'b00_0000_0000_0000_0000_0000_0000_1000,
					  mode2     =30'b00_0000_0000_0000_0000_0000_0001_0000,
					  mode3     =30'b00_0000_0000_0000_0000_0000_0010_0000,
					  mode4     =30'b00_0000_0000_0000_0000_0000_0100_0000,
					  mode5     =30'b00_0000_0000_0000_0000_0000_1000_0000,
					  mode6     =30'b00_0000_0000_0000_0000_0001_0000_0000,
					  mode7     =30'b00_0000_0000_0000_0000_0010_0000_0000,
					  ack_mode  =30'b00_0000_0000_0000_0000_0100_0000_0000,
					  addr0     =30'b00_0000_0000_0000_0000_1000_0000_0000,
					  addr1     =30'b00_0000_0000_0000_0001_0000_0000_0000,
					  addr2     =30'b00_0000_0000_0000_0010_0000_0000_0000,
					  addr3     =30'b00_0000_0000_0000_0100_0000_0000_0000,
					  addr4     =30'b00_0000_0000_0000_1000_0000_0000_0000,
					  addr5     =30'b00_0000_0000_0001_0000_0000_0000_0000,
					  addr6     =30'b00_0000_0000_0010_0000_0000_0000_0000,
					  addr7     =30'b00_0000_0000_0100_0000_0000_0000_0000,
					  ack_addr  =30'b00_0000_0000_1000_0000_0000_0000_0000,
					  bit0      =30'b00_0000_0001_0000_0000_0000_0000_0000,      
					  bit1      =30'b00_0000_0010_0000_0000_0000_0000_0000, 
					  bit2      =30'b00_0000_0100_0000_0000_0000_0000_0000, 
					  bit3      =30'b00_0000_1000_0000_0000_0000_0000_0000, 
					  bit4      =30'b00_0001_0000_0000_0000_0000_0000_0000, 
					  bit5      =30'b00_0010_0000_0000_0000_0000_0000_0000, 
					  bit6      =30'b00_0100_0000_0000_0000_0000_0000_0000, 
					  bit7      =30'b00_1000_0000_0000_0000_0000_0000_0000, 
					  ack_bit   =30'b01_0000_0000_0000_0000_0000_0000_0000,
					  stop      =30'b10_0000_0000_0000_0000_0000_0000_0000;    
					  
   //generate clk 400khz
	always@(posedge clk)
      if(!rstn)
		   clk_cnt <=8'd0;
		else if(clk_en)
		   begin
			   if(clk_cnt==`COUNT_MAX)
				  clk_cnt <=8'd0;
				else
				  clk_cnt <=clk_cnt + 1'b1;
			end
		else
		   clk_cnt <=8'd0;
			
	//wire neg=(clk_cnt==`NEG)?1:0;            //negedge of the scl_clk
	wire      h_half   =(clk_cnt==h_half);      
	wire      l_half   =(clk_cnt==l_half);  
   wire      scl_clk  =(clk_cnt <`HALF);
   assign    scl      =(clk_en&scl_clk)|(!clk_en);
	
	//control sda direction
    assign sda=(sda_mode==OUT)?sda_out:1'bz;
	assign sda_in=(sda_mode==IN)?sda:1'bz;
	wire    full=(clk_cnt==`COUNT_MAX);

   //state control	
	always@(posedge clk)
      if(!rstn)
	      state <=idle;
		else
	      case(state)
		   idle:
		      if(req)
			      state <=start;
				else
			      state <=state;
			start:   //at the same time start clock generator
			      if(full)
					   state <=mode0;
					else
					   state <=state;
			mode0:
			      if(full)
		            state <=mode1;
					else
					   state <=state;
			mode1:
			      if(full)
		            state <=mode2;
					else
					   state <=state;
			mode2:
			      if(full)
		            state <=mode3;
					else
					   state <=state;
			mode3:
		         if(full)
					  state <=mode4;
					else
					  state <=state;
			mode4:
		         if(full)
					  state <=mode5;
					else
					  state <=state;
			mode5:
			      if(full)
		            state <=mode6;
					else
					   state <=state;
			mode6:
		         if(full)
					  state <=mode7;
					else
					  state <=state;
			mode7:
			      if(full)
		            state <=ack_mode;	
					else
					   state <=state;
			ack_mode:
			      if(h_half)
					   begin
						   if(sda_in==1'b1)
							   state <=addr0;
							else
							   state <=stop;
						end
					else
					   state <=state;
			addr0:
			      if(full)
					   state <=addr1;
					else
					   state <=state;
			addr1: 
			      if(full)
					   state <=addr2;
					else
					   state <=state;
			addr2:
			      if(full)
					   state <=addr3;
					else
					   state <=state;
			addr3:  
			      if(full)
					   state <=addr4;
					else
					   state <=state;
			addr4:
			      if(full)
					   state <=addr5;
					else
					   state <=state;
			addr5:
			      if(full)
					   state <=addr6;
					else
					   state <=state;
			addr6:
			      if(full)
					   state <=addr7;
					else
					   state <=state;
			addr7:
			      if(full)
					   state <=ack_addr;
					else
					   state <=state;
		   ack_addr:
			      if(h_half)
					   begin
			            if(sda_in==1'b0)
					         state <=bit0;
					      else
					         state <=stop;
						end
				   else
					   state <=state;
		   bit0:
			      if(full)
					   state <=bit1;
					else
					   state <=bit2;
			bit1:
			      if(full)
					   state <=bit2;
					else
					   state <=state;
			bit2:
			      if(full)
					   state <=bit3;
					else
					   state <=state;
			bit3:
			      if(full)
					   state <=bit4;
					else
					   state <=state;
			bit4:
			      if(full)
					   state <=bit5;
					else
					   state <=state;
			bit5:
			      if(full)
					   state <=bit6;
					else
					   state <=state;
			bit6:
			      if(full)
					   state <=bit7;
					else
					   state <=state;			
			bit7:
			      if(full)
					   state <=ack_bit;
					else
					   state <=state;
			ack_bit:
			        if((!wr)&h_half)
					      begin
			               if(sda_in==1'b0)
					            state  <=stop;
					         else
					            state  <=stop;
							end
                    else if(wr&l_half)
                           state <=stop;					
					else
						   state <=state;
			stop:
			      if(full)
					   state <=idle;
					else
					   state <=state;
			default: state  <=stop;
		endcase
			
	always@(posedge clk)
	if(!rstn)
	begin
	    sda_mode <=OUT;
		sda_out  <=1'b1;
		data_out <=8'b0;
	end
	else
	    case(state)
		    idle:
			        begin
			        sda_mode <=OUT;
				     sda_out  <=1'b1;
			        end
		    start:
			        begin
			        sda_mode <=OUT;
				        if(l_half)
					    sda_out <=1'b0;
					    else
					    sda_out <=1'b1;
			        end
		    mode0:
			        begin
			        sda_mode <=OUT;
				        if(l_half)
					    sda_out <=1'b1;
					    else
					    sda_out <=sda_out;
			        end
		    mode1:
			        begin
			        sda_mode <=OUT;
				        if(l_half)
					    sda_out <=1'b0;
					    else
					    sda_out <=sda_out;
			        end
		    mode2:
			        begin
			        sda_mode <=OUT;
				        if(l_half)
					    sda_out <=1'b1;
					    else
					    sda_out <=sda_out;
			        end
		    mode3:
			        begin
			        sda_mode <=OUT;
				        if(l_half)
					    sda_out <=1'b0;
					    else
					    sda_out <=sda_out;
			        end
		    mode4:
			        begin
			        sda_mode <=OUT;
				        if(l_half)
					    sda_out <=1'b1;
					    else
					    sda_out <=sda_out;
			        end
		    mode5:
			        begin
			        sda_mode <=OUT;
				        if(l_half)
					    sda_out <=1'b0;
					    else
					    sda_out <=sda_out;
			        end
		    mode6:
			        begin
			        sda_mode <=OUT;
				        if(l_half)
					    sda_out <=1'b0;
					    else
					    sda_out <=sda_out;
			        end
		    mode7:
			        begin
			        sda_mode <=OUT;
				        if(l_half)
					    sda_out <=wr;
					    else
					    sda_out <=sda_out;
			        end
		    ack_mode:
			        begin
			        sda_mode <=IN;
			        end
		    addr0:
			        begin
			        sda_mode <=OUT;
				        if(l_half)
					    sda_out <=address[7];
					    else
					    sda_out <=sda_out;
			        end					
		    addr1:
			        begin
			        sda_mode <=OUT;
				        if(l_half)
					    sda_out <=address[6];
					    else
					    sda_out <=sda_out;
			        end	
		    addr2:
			        begin
			        sda_mode <=OUT;
				        if(l_half)
					    sda_out <=address[5];
					    else
					    sda_out <=sda_out;
			        end	
		    addr3:
			        begin
			        sda_mode <=OUT;
				        if(l_half)
					    sda_out <=address[4];
					    else
					    sda_out <=sda_out;
			        end	
		    addr4:
			        begin
			        sda_mode <=OUT;
				        if(l_half)
					    sda_out <=address[3];
					    else
					    sda_out <=sda_out;
			        end	
		    addr5:
			        begin
			        sda_mode <=OUT;
				        if(l_half)
					    sda_out <=address[2];
					    else
					    sda_out <=sda_out;
			        end	
		    addr6:
			        begin
			        sda_mode <=OUT;
				        if(l_half)
					    sda_out <=address[1];
					    else
					    sda_out <=sda_out;
			        end	
		    addr7:
			        begin
			        sda_mode <=OUT;
				        if(l_half)
					    sda_out <=address[0];
					    else
					    sda_out <=sda_out;
			        end						
		   ack_addr:
			        begin
			        sda_mode <=IN;
			        end	
            bit0:
                    if(wr)//read
					    begin
                            sda_mode     <=IN;
                            if(h_half)
					    	data_out[7]  <=sda_in;
							else
							data_out[7]  <=data_out[7];
                        end
                   	else//write
                        begin
                            sda_mode     <=OUT;
							   if(l_half)
							   sda_out   <=data_in[7];
							   else
							   sda_out   <=sda_out;
                        end						
            bit1:
                    if(wr)//read
					    begin
                            sda_mode     <=IN;
                            if(h_half)
					    	data_out[6]  <=sda_in;
							else
							data_out[6]  <=data_out[6];
                        end
                   	else//write
                        begin
                            sda_mode     <=OUT;
							   if(l_half)
							   sda_out   <=data_in[6];
							   else
							   sda_out   <=sda_out;
                        end	
            bit2:
                    if(wr)//read
					    begin
                            sda_mode     <=IN;
                            if(h_half)
					    	data_out[5]  <=sda_in;
							else
							data_out[5]  <=data_out[5];							
                        end
                   	else//write
                        begin
                            sda_mode     <=OUT;
							   if(l_half)
							   sda_out   <=data_in[5];
							   else
							   sda_out   <=sda_out;
                        end	
            bit3:
                    if(wr)//read
					    begin
                            sda_mode     <=IN;
                            if(h_half)
					    	data_out[4]  <=sda_in;
							else
							data_out[4]  <=data_out[4];							
                        end
                   	else//write
                        begin
                            sda_mode     <=OUT;
							   if(l_half)
							   sda_out   <=data_in[4];
							   else
							   sda_out   <=sda_out;
                        end	
            bit4:
                    if(wr)//read
					    begin
                            sda_mode     <=IN;
                            if(h_half)
					    	data_out[3]  <=sda_in;
							else
							data_out[3]  <=data_out[3];							
                        end
                   	else//write
                        begin
                            sda_mode     <=OUT;
							   if(l_half)
							   sda_out   <=data_in[3];
							   else
							   sda_out   <=sda_out;
                        end	
            bit5:
                    if(wr)//read
					    begin
                            sda_mode     <=IN;
                            if(h_half)
					    	data_out[2]  <=sda_in;
							else
							data_out[2]  <=data_out[2];							
                        end
                   	else//write
                        begin
                            sda_mode     <=OUT;
							   if(l_half)
							   sda_out   <=data_in[2];
							   else
							   sda_out   <=sda_out;
                        end	
            bit6:
                    if(wr)//read
					    begin
                            sda_mode     <=IN;
                            if(h_half)
					    	data_out[1]  <=sda_in;
							else
							data_out[1]  <=data_out[1];							
                        end
                   	else//write
                        begin
                            sda_mode     <=OUT;
							   if(l_half)
							   sda_out   <=data_in[1];
							   else
							   sda_out   <=sda_out;
                        end	
            bit7:
                    if(wr)//read
					    begin
                            sda_mode     <=IN;
                            if(h_half)
					    	data_out[0]  <=sda_in;
							else
							data_out[0]  <=data_out[0];							
                        end
                   	else//write
                        begin
                            sda_mode     <=OUT;
							   if(l_half)
							   sda_out   <=data_in[0];
							   else
							   sda_out   <=sda_out;
                        end	
            ack_bit:
						if(!wr)
                            sda_mode  <=IN;
						else
						    begin
							sda_mode  <=OUT;
							if(l_half)
							sda_out   <=1'b1;
							else
							sda_out   <=sda_out;
							end        									
            stop:
                if(h_half)//閹峰鐝
				    begin
					sda_mode    <=OUT;
					sda_out     <=1;
					end
				else if(!h_half)//閹峰缍
				    begin
					sda_mode    <=OUT;
					sda_out     <=0;					
					end
		    default:begin   
						sda_out   <=1'b1;
						sda_mode  <=OUT;
                        data_out  <=8'd0;						
			        end
	    endcase
								
   assign   clk_en       =(state==idle);
   assign   wr_done      =(state==ack_bit)&(h_half)&(sda_in==1'b0);
   assign   fail         =((state==ack_mode)&(h_half)&(sda_in!=1'b0))|
                          ((state==ack_addr)&(h_half)&(sda_in!=1'b0))|
                          ((state==ack_bit)&(h_half)&(sda_in!=1'b0));			 
   assign   rd_done     =(state==stop);	
endmodule 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236
  • 237
  • 238
  • 239
  • 240
  • 241
  • 242
  • 243
  • 244
  • 245
  • 246
  • 247
  • 248
  • 249
  • 250
  • 251
  • 252
  • 253
  • 254
  • 255
  • 256
  • 257
  • 258
  • 259
  • 260
  • 261
  • 262
  • 263
  • 264
  • 265
  • 266
  • 267
  • 268
  • 269
  • 270
  • 271
  • 272
  • 273
  • 274
  • 275
  • 276
  • 277
  • 278
  • 279
  • 280
  • 281
  • 282
  • 283
  • 284
  • 285
  • 286
  • 287
  • 288
  • 289
  • 290
  • 291
  • 292
  • 293
  • 294
  • 295
  • 296
  • 297
  • 298
  • 299
  • 300
  • 301
  • 302
  • 303
  • 304
  • 305
  • 306
  • 307
  • 308
  • 309
  • 310
  • 311
  • 312
  • 313
  • 314
  • 315
  • 316
  • 317
  • 318
  • 319
  • 320
  • 321
  • 322
  • 323
  • 324
  • 325
  • 326
  • 327
  • 328
  • 329
  • 330
  • 331
  • 332
  • 333
  • 334
  • 335
  • 336
  • 337
  • 338
  • 339
  • 340
  • 341
  • 342
  • 343
  • 344
  • 345
  • 346
  • 347
  • 348
  • 349
  • 350
  • 351
  • 352
  • 353
  • 354
  • 355
  • 356
  • 357
  • 358
  • 359
  • 360
  • 361
  • 362
  • 363
  • 364
  • 365
  • 366
  • 367
  • 368
  • 369
  • 370
  • 371
  • 372
  • 373
  • 374
  • 375
  • 376
  • 377
  • 378
  • 379
  • 380
  • 381
  • 382
  • 383
  • 384
  • 385
  • 386
  • 387
  • 388
  • 389
  • 390
  • 391
  • 392
  • 393
  • 394
  • 395
  • 396
  • 397
  • 398
  • 399
  • 400
  • 401
  • 402
  • 403
  • 404
  • 405
  • 406
  • 407
  • 408
  • 409
  • 410
  • 411
  • 412
  • 413
  • 414
  • 415
  • 416
  • 417
  • 418
  • 419
  • 420
  • 421
  • 422
  • 423
  • 424
  • 425
  • 426
  • 427
  • 428
  • 429
  • 430
  • 431
  • 432
  • 433
  • 434
  • 435
  • 436
  • 437
  • 438
  • 439
  • 440
  • 441
  • 442
  • 443
  • 444
  • 445
  • 446
  • 447
  • 448
  • 449
  • 450
  • 451
  • 452
  • 453
  • 454
  • 455
  • 456
  • 457
  • 458
  • 459
  • 460
  • 461
  • 462
  • 463
  • 464
  • 465
  • 466
  • 467
  • 468
  • 469
  • 470
  • 471
  • 472
  • 473
  • 474
  • 475
  • 476
  • 477
  • 478
  • 479
  • 480
  • 481
  • 482
  • 483
  • 484
  • 485
  • 486
  • 487
  • 488
  • 489
  • 490
  • 491
  • 492
  • 493
  • 494
  • 495
  • 496
  • 497
  • 498
  • 499
  • 500
  • 501
  • 502
  • 503
  • 504
  • 505
  • 506
  • 507
  • 508
  • 509
  • 510
  • 511
  • 512
  • 513
  • 514
  • 515
  • 516
  • 517
  • 518
  • 519
  • 520
  • 521
  • 522
  • 523
  • 524
  • 525
  • 526
  • 527
  • 528
  • 529
  • 530
  • 531
  • 532
  • 533
  • 534
  • 535
  • 536
  • 537
  • 538
  • 539
  • 540
  • 541
  • 542
  • 543
  • 544
  • 545
  • 546
  • 547
  • 548
  • 549
  • 550
  • 551
  • 552
  • 553
  • 554
  • 555
  • 556
  • 557
  • 558
  • 559
  • 560
  • 561
  • 562
  • 563
  • 564
  • 565
  • 566
  • 567
  • 568
  • 569
  • 570
  • 571
  • 572
  • 573
  • 574
  • 575
  • 576
  • 577
  • 578
  • 579
  • 580
  • 581
  • 582
  • 583
  • 584
  • 585
  • 586
  • 587
  • 588
  • 589
  • 590
  • 591
  • 592
  • 593
  • 594
  • 595
  • 596
  • 597
  • 598
  • 599
  • 600
  • 601
  • 602
  • 603
  • 604
  • 605
  • 606
  • 607
  • 608
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/599747
推荐阅读
相关标签
  

闽ICP备14008679号