1.//****************************************************************************//3.//# @Date: 2019-05-19 21:06:324.//# @Last Modified by: zlk5.//# @WeChat Official Account: OpenFPGA6.//# @Last Modified time: 2019-05-19 21:31:518.//# @Modification History: 2019-05-19 20:58:199.//# Date By Version Change Description:10.//# ========================================================================= #11.//# 2019-05-19 20:58:1912.//# ========================================================================= #15.//****************************************************************************//19. output RTC_NRST,RTC_SCLK,23. input [7:0]iAddr,iData,26. parameter FCLK = 6'd25, FHALF = 6'd12; // 2Mhz,(1/2Mhz)/(1/50Mhz) FCLK 为一个周期27. parameter FF_Write = 6'd16, FF_Read = 6'd32;//FHALF 为半周期31. reg [7:0]D1,T; //D1 为暂存读取结果 T 为伪函数的操作空间32. reg rRST, rSCLK, rSIO;33. reg isQ,isDone; //isQ 为 IO 的控制输出35. always @ ( posedge CLOCK or negedge RST_n )39. { i,Go } <= { 6'd0,6'd0 };40. { D1,T } <= { 8'd0,8'd0 };41. { rRST, rSCLK, rSIO } <= 3'b000;42. { isQ, isDone } <= 2'b00;44./***********************************************************************45.下面步骤是写一个字节的伪函数。步骤 0 拉高片选,准备访问字节,并且进入伪函数。47.步骤 2 拉低片选,步骤 3~4 则是用来产生完成信号。48.***********************************************************************/53. begin { rRST,rSCLK } <= 2'b10; T <= iAddr; i <= FF_Write; Go <= i + 1'b1; end56. begin T <= iData; i <= FF_Write; Go <= i + 1'b1; end59. begin { rRST,rSCLK } <= 2'b00; i <= i + 1'b1; end62. begin isDone <= 1'b1; i <= i + 1'b1; end65. begin isDone <= 1'b0; i <= 6'd0; end69. 16,17,18,19,20,21,22,23:74. if( C1 == 0 ) rSCLK <= 1'b0;75. else if( C1 == FHALF ) rSCLK <= 1'b1;77. if( C1 == FCLK -1) begin C1 <= 6'd0; i <= i + 1'b1; end78. else C1 <= C1 + 1'b1;85./***********************************************************************86.以下内容是读操作,下面步骤是写一个字节的伪函数,87.步骤 0 拉高使能,准备访问字节并且进入写函数。89.步骤 2 拉低使能之余,也将读取结果暂存至 D。91.***********************************************************************/96. begin { rRST,rSCLK } <= 2'b10; T <= iAddr; i <= FF_Write; Go <= i + 1'b1; end99. begin i <= FF_Read; Go <= i + 1'b1; end102. begin { rRST,rSCLK } <= 2'b00; D1 <= T; i <= i + 1'b1; end105. begin isDone <= 1'b1; i <= i + 1'b1; end108. begin isDone <= 1'b0; i <= 6'd0; end110. /*********************/112. 16,17,18,19,20,21,22,23:117. if( C1 == 0 ) rSCLK <= 1'b0;118. else if( C1 == FHALF ) rSCLK <= 1'b1;120. if( C1 == FCLK -1) begin C1 <= 6'd0; i <= i + 1'b1; end121. else C1 <= C1 + 1'b1;127. /*********************/129. 32,33,34,35,36,37,38,39:133. if( C1 == 0 ) rSCLK <= 1'b0;134. else if( C1 == FHALF ) begin rSCLK <= 1'b1; T[i-32] <= RTC_DATA; end136. if( C1 == FCLK -1) begin C1 <= 6'd0; i <= i + 1'b1; end137. else C1 <= C1 + 1'b1;144./***********************************************************************145.以下内容为相关输出驱动声明,其中 rSIO 驱动 RTC_DATA, D 驱动 oData 。146.***********************************************************************/147. assign { RTC_NRST,RTC_SCLK } = { rRST,rSCLK };148. assign RTC_DATA = isQ ? rSIO : 1'bz;149. assign oDone = isDone;
|