You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

FRAM_Controller.sv 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. `include "SPI.sv"
  2. module FRAM(
  3. input i_clk, //Module (Module CLock = SPI Clock)
  4. input i_nreset,
  5. input logic [19:0] i_adr, //Memorycell adress in FRAM
  6. input logic [7:0] i_data, //data to write
  7. output logic [7:0] o_data, //data to read
  8. input logic i_rw, //Read = 1, Write = 0
  9. input logic i_status, //If 1 Read Staut register
  10. input logic i_hbn, //If 1 FRAM will enter Hibernation Mode
  11. input logic i_cready, //Starts transmission
  12. output logic o_busy, //Indicates FRAM Busy
  13. // SPI Interface
  14. output o_SPI_Clk,
  15. input i_SPI_MISO,
  16. output o_SPI_MOSI,
  17. output o_SPI_CS_n
  18. );
  19. //FRAM SPI OP Codes
  20. //Write Enable Control
  21. localparam WREN = 8'h06; //Set Write enable latch
  22. localparam WRDI = 8'h04; //Reset write enable latch
  23. //Register Access
  24. localparam RDSR = 8'h05; //Read Status Register
  25. localparam WRSR = 8'h01; //Write Status Register
  26. //Memory Write
  27. localparam WRITE = 8'h02; //Write Memory Data
  28. //Memory Read
  29. localparam READ = 8'h03; //Read Memory Data
  30. localparam FSTRT = 8'h0B; //Fast read memory Data
  31. //Special Sector Memory Access
  32. localparam SSWR = 8'h42; //Spcial Sector Write
  33. localparam SSRD = 8'h4B; //Special Sector Read
  34. //Identification and serial Number
  35. localparam RDID = 8'h9F; //Read Device ID
  36. localparam RUID = 8'h4C; //Read Unique ID
  37. localparam WRSN = 8'hC2; //Write Serial Number
  38. localparam RDSN = 8'hC3; //Read Serial Number
  39. //Low Power Modes
  40. localparam DPD = 8'hBA; // Enter Deep Power-Down
  41. localparam HBN = 8'hB9; // Enter Hibernate Mode
  42. //end FRAM SPI OP Codes
  43. //Controller Specific
  44. logic [3:0] state;
  45. // SPI Specific
  46. parameter SPI_MODE = 0; // CPOL = 0, CPHA = 0
  47. parameter CLKS_PER_HALF_BIT = 2; // 25MHz
  48. parameter MAX_BYTES_PER_CS = 5; // 5 bytes max per chip select cycle
  49. parameter CS_INACTIVE_CLKS = 1; // Adds delay (1clk) between cycles
  50. logic [7:0] r_Master_TX_Byte = 0;
  51. logic r_Master_TX_DV = 1'b0;
  52. logic w_Master_TX_Ready;
  53. logic w_Master_RX_DV;
  54. logic [7:0] w_Master_RX_Byte;
  55. logic [$clog2(MAX_BYTES_PER_CS+1)-1:0] w_Master_RX_Count, r_Master_TX_Count = 3'h1; //Standard 1 Byte pro CS Cycle
  56. SPI_Master_With_Single_CS
  57. #(.SPI_MODE(SPI_MODE), //SPI Mode 0-3
  58. .CLKS_PER_HALF_BIT(CLKS_PER_HALF_BIT), //sets Frequency of SPI_CLK
  59. .MAX_BYTES_PER_CS(MAX_BYTES_PER_CS), //Maximum Bytes per CS Cycle
  60. .CS_INACTIVE_CLKS(CS_INACTIVE_CLKS) //Amount of Time holding CS Low befor next command
  61. ) SPI
  62. (
  63. // Control/Data Signals,
  64. .i_Rst_L(i_nreset), // FPGA Reset
  65. .i_Clk(i_clk), // FPGA Clock
  66. // TX (MOSI) Signals
  67. .i_TX_Count(r_Master_TX_Count), // Number of bytes per CS
  68. .i_TX_Byte(r_Master_TX_Byte), // Byte to transmit on MOSI
  69. .i_TX_DV(r_Master_TX_DV), // Data Valid Pulse with i_TX_Byte
  70. .o_TX_Ready(w_Master_TX_Ready), // Transmit Ready for Byte
  71. // RX (MISO) Signals
  72. .o_RX_Count(w_Master_RX_Count), // Index of RX'd byte
  73. .o_RX_DV(w_Master_RX_DV), // Data Valid pulse (1 clock cycle)
  74. .o_RX_Byte(w_Master_RX_Byte), // Byte received on MISO
  75. // SPI Interface
  76. .o_SPI_Clk(o_SPI_Clk),
  77. .i_SPI_MISO(i_SPI_MISO),
  78. .o_SPI_MOSI(o_SPI_MOSI),
  79. .o_SPI_CS_n(o_SPI_CS_n)
  80. );
  81. //end SPI Specific
  82. task SPI_SendByte(input [7:0] data);
  83. @(posedge i_clk);
  84. r_Master_TX_Byte <= data;
  85. r_Master_TX_DV <= 1'b1;
  86. @(posedge i_clk);
  87. r_Master_TX_DV <= 1'b0;
  88. @(posedge i_clk);
  89. @(posedge w_Master_TX_Ready);
  90. endtask //end SPI_SendByte
  91. //FRAM Tasks
  92. task FRAM_Write(input [19:0] adr, input [7:0] data); //vgl. Fig.11
  93. logic [7:0] value;
  94. value <= 8'h0;
  95. //Set Write Enable
  96. r_Master_TX_Count <= 3'b1; //1Byte Transaction
  97. SPI_SendByte(WREN);
  98. //Write to fram
  99. r_Master_TX_Count <= 3'h5; //5 Byte Transaction
  100. SPI_SendByte(WRITE); //OPCode
  101. SPI_SendByte({4'hF,adr[19:16]}); //Adress [23-16]
  102. SPI_SendByte(adr[15:8]); //Adress [15-8]
  103. SPI_SendByte(adr[7:0]); //Adress [7-0]
  104. SPI_SendByte(data); //Data [7:0]
  105. //Reset Write Disable and Verify
  106. do begin
  107. r_Master_TX_Count <= 3'b1; //1Byte Transaction
  108. SPI_SendByte(WRDI); //Set Write Disable
  109. FRAM_Read_Status(value); //Lese Status Register
  110. end while(((value & 8'h2) >> 1) != 0);
  111. endtask //end FRAM_Write
  112. task FRAM_Read(input [19:0] adr, output [7:0] data); //vgl. Fig12
  113. r_Master_TX_Count <= 3'h5; //5 Byte Transaction
  114. SPI_SendByte(READ); //Opcode
  115. SPI_SendByte({4'hF,adr[19:16]}); //Adress [23-16]
  116. SPI_SendByte(adr[15:8]); //Adress [15-8]
  117. SPI_SendByte(adr[7:0]); //Adress [7-0]
  118. SPI_SendByte(8'hAA); //Dummy Bits, read byte in w_Master_RX_Byte
  119. data = w_Master_RX_Byte;
  120. endtask //end FRAM_READ
  121. task FRAM_Read_Status(output [7:0] data); //vgl. Fig9
  122. r_Master_TX_Count <= 3'h2; //2 Byte Transaction
  123. SPI_SendByte(RDSR); //OpCode
  124. SPI_SendByte(8'hFD); //Dummy Bits, read byte in w_Master_RX_Byte
  125. data = w_Master_RX_Byte;
  126. endtask //FRAM_Read_Status
  127. task FRAM_Hibernation(); //vgl. Fig22
  128. r_Master_TX_Count <= 3'h1; //1 Byte Transaction
  129. SPI_SendByte(HBN);
  130. endtask //FRAM_Hibernation
  131. //end FRAM Tasks
  132. always @(posedge i_clk or negedge i_nreset) begin
  133. state[0] = i_cready;
  134. state[1] = i_hbn;
  135. state[2] = i_status;
  136. state[3] = i_rw;
  137. if(~i_nreset) begin //Modul Reset
  138. o_data <= 8'h00;
  139. end //end if
  140. if(w_Master_TX_Ready) begin
  141. case(state) inside
  142. 4'b??11: FRAM_Hibernation();
  143. 4'b?101: FRAM_Read_Status(o_data);
  144. 4'b1001: FRAM_Read(i_adr, o_data);
  145. 4'b0001: FRAM_Write(i_adr, i_data);
  146. default:;
  147. endcase //endcase
  148. end //endif
  149. end //end always
  150. assign o_busy = w_Master_TX_Ready;
  151. endmodule