当前位置:   article > 正文

基于SPI实现stm32与fpga通信(三)_fpga实现stm32软核

fpga实现stm32软核

使用stm32与fpga之间的spi通信,stm32通过fpga控制DAC输出模拟电压,使用三线制。

使用FPGA驱动MS5541,采用3线制,输出时钟(20MHz),片选信号,输出信号,主机端时钟为100MHz,将时钟5分频,然后设置片选信号与并行转串行输出信号在20MHz时钟上升沿输出

inputclk
inputrst

input

start
input

i16_dac_data

output

o_dac_sclk

output

o_dac_cs

output

o_dac_MOSI

  1. module dac_core(
  2. input clk,
  3. input rst,
  4. input start,
  5. input [15:0] i16_dac_data,
  6. output wire o_dac_sclk,
  7. output reg o_dac_cs,
  8. output reg o_dac_mosi
  9. );
  10. reg [5:0] tri_cnt;
  11. reg [4:0] data_count;
  12. reg [15:0] i16_dac_data_reg;
  13. wire sclk_posedge;
  14. reg o_dac_sclk_buf;
  15. /**************************************************************************
  16. 100MHz 5分频
  17. **************************************************************************/
  18. reg [3:0] state1;
  19. reg [3:0] state2;
  20. always@(posedge clk)
  21. begin
  22. if(rst)
  23. begin
  24. state1 <= 4'b0000;
  25. end
  26. else
  27. begin
  28. case(state1)
  29. 4'b0000: state1 <= 4'b0010;
  30. 4'b0010: state1 <= 4'b0110;
  31. 4'b0110: state1 <= 4'b0001;
  32. 4'b0001: state1 <= 4'b0011;
  33. 4'b0011: state1 <= 4'b0000;
  34. default: state1 <= 4'b0000;
  35. endcase
  36. end
  37. end
  38. always@(negedge clk)
  39. begin
  40. if(rst)
  41. begin
  42. state2 <= 4'b0 ;
  43. end
  44. else
  45. begin
  46. case(state2)
  47. 4'b0000: state2 <= 4'b0010;
  48. 4'b0010: state2 <= 4'b0110;
  49. 4'b0110: state2 <= 4'b0001;
  50. 4'b0001: state2 <= 4'b0011;
  51. 4'b0011: state2 <= 4'b0000;
  52. default: state2 <= 4'b0000;
  53. endcase
  54. end
  55. end
  56. assign o_dac_sclk = state1[0] | state2[0];
  57. /******************************************************************************
  58. 输出时钟边沿
  59. ******************************************************************************/
  60. always@(posedge clk) begin
  61. if(rst) begin
  62. o_dac_sclk_buf <= 1'b0;
  63. end
  64. else begin
  65. o_dac_sclk_buf <= o_dac_sclk;
  66. end
  67. end
  68. assign sclk_posedge = (o_dac_sclk_buf == 1'b0 && o_dac_sclk == 1'b1);
  69. /**************************************************************************
  70. 状态转移
  71. **************************************************************************/
  72. reg [2:0] cnt;
  73. reg [2:0] state_cur;
  74. reg [2:0] state_nex;
  75. parameter SM01 = 3'b001,
  76. SM02 = 3'b010,
  77. SM03 = 3'b100;
  78. always@(posedge clk) begin
  79. if(rst) begin
  80. cnt <= 2'd0;
  81. end
  82. else begin
  83. if((state_cur == 3'd0 && cnt <3'd3)) begin
  84. cnt <= cnt + 1'b1;
  85. end
  86. else begin
  87. cnt <= 3'd0;
  88. end
  89. end
  90. end
  91. always@(*) begin
  92. if(rst) begin
  93. state_cur <= 3'd0;
  94. end
  95. else begin
  96. state_cur <= state_nex;
  97. end
  98. end
  99. always @(posedge clk) begin
  100. if (rst) begin
  101. o_dac_cs <= 1'b1;
  102. o_dac_mosi <= 1'b0;
  103. data_count <= 4'd0;
  104. i16_dac_data_reg<= i16_dac_data;
  105. state_nex <= 3'b0;
  106. tri_done <= 1'b0;
  107. end
  108. else begin
  109. case(state_cur)
  110. SM01: begin
  111. o_dac_cs <= 1'b1;
  112. o_dac_mosi <= 1'b0;
  113. tri_done <= 1'b0;
  114. if(cnt == 3'd3) begin
  115. state_nex <= SM02;
  116. i16_dac_data_reg<= i16_dac_data;
  117. end
  118. end
  119. SM02: begin
  120. tri_done <= 1'b1;
  121. if (start) begin
  122. if(o_dac_cs == 1'b1) begin
  123. o_dac_cs <= 1'b0;
  124. data_count <= 4'd0;
  125. o_dac_mosi <= i16_dac_data_reg[15];
  126. i16_dac_data_reg <= i16_dac_data_reg << 1;
  127. end
  128. else begin
  129. if(sclk_posedge == 1'b1)begin
  130. if (data_count < 4'd15) begin
  131. o_dac_mosi <= i16_dac_data_reg[15];
  132. i16_dac_data_reg <= i16_dac_data_reg << 1;
  133. data_count <= data_count + 4'd1;
  134. end
  135. else begin
  136. data_count <= 2'd0;
  137. o_dac_mosi <= 1'b0;
  138. state_nex <= SM03;
  139. o_dac_cs <= 1'b1;
  140. end
  141. end
  142. else begin
  143. o_dac_cs <= o_dac_cs;
  144. data_count <= data_count;
  145. o_dac_mosi <= o_dac_mosi;
  146. i16_dac_data_reg <= i16_dac_data_reg;
  147. end
  148. end
  149. end
  150. end
  151. SM03: begin
  152. state_nex <= SM01;
  153. end
  154. endcase
  155. end
  156. end
  157. endmodule

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/585905
推荐阅读
相关标签
  

闽ICP备14008679号