6502.org Forum  Projects  Code  Documents  Tools  Forum
It is currently Sat May 11, 2024 9:06 pm

All times are UTC




Post new topic Reply to topic  [ 4 posts ] 
Author Message
PostPosted: Tue Feb 16, 2016 12:33 am 
Offline

Joined: Tue May 05, 2009 2:49 pm
Posts: 108
I abhor asking for help on this, as I was doing much better on my Verilog, but the finer points of Verilog must be escaping me.

I've tracked down lots of examples, but all of them are these huge general purpose affairs that run at CLOCK/2 at best, and I'm really just looking for an unbuffered Mode 0 SPI that runs at CLK speed.

It's to tie some SPI EEPROM memory to an 65XX bus (Yes, I know about 65SPI, but again, this is a means to an end, and I am short on cells in the CPLD).

Code:
`ifndef _spi
`define _spi

module spi(clock, reset, load, d, q, ss, sck, miso, mosi);
input clock;
input reset;
input load;
input [7:0] d;
output [7:0] q;
output ss;
output sck;
input miso;
output mosi;

reg in;
reg [7:0] q;
initial q = 0;
reg [3:0]i;
wire run;
initial i[3:0] = 8;
assign ss = !(run);
assign mosi = q[7];
assign run = (!i[3]);
assign sck = clock & !ss;

always @(posedge clock)
begin
  in <= miso;
end

always @(negedge clock or posedge reset)
begin
  if(reset)
  begin
    q <= 0;
    i <= 8;
  end
  else if(load)
  begin
    q <= d;
    i <= 0;
  end
  else if(run)
  begin
    i <= i + 1;
    q <= {q[6:0],in};
  end
end

endmodule
`endif


Top
 Profile  
Reply with quote  
PostPosted: Tue Feb 16, 2016 7:48 am 
Offline

Joined: Sun Jun 29, 2014 5:42 am
Posts: 337
Jim,

I take it then this is not currently working?

I wonder if the issue is that SS is asserted/de-asserted every byte? In my experience, this is normally held active for the whole of a multi byte transaction.

Dave


Top
 Profile  
Reply with quote  
PostPosted: Tue Feb 16, 2016 1:43 pm 
Offline

Joined: Tue May 05, 2009 2:49 pm
Posts: 108
It is currently sending data, but I have it hooked up to a Logic Analyzer and the output gets out of sync. I can grab a snapshot later tonight.

Yes, SS on this SPI is not usable as-is, for the reason you note, but I have the SS going out of the CPLD tied to a regular IO pin, and I am bringing it low and high manually. I will remove it from the Verilog at some point.

Jim


Top
 Profile  
Reply with quote  
PostPosted: Sat Feb 20, 2016 3:08 pm 
Offline
User avatar

Joined: Mon Apr 23, 2012 12:28 am
Posts: 760
Location: Huntsville, AL
Jim:

I pulled your code from above and built a simple simulation. It appeared to be just fine. I surmised that you were using clock directly from a 6502, which means that the load signal would be asserted when clock goes low and deassert when clock returns high. Therefore, I complemented the polarity of sck, and the clock edges used in your code because I suspect that you may be having a race condition on your load pulse. The figure below is a simulation of your code with those simple changes. It shows a load pulse with a pulse width equal to 1/2 clock, and the transmission of two bytes: 0xA5 and 0xC3. In the simulation, I wired miso to be the complement of mosi. After transmission, the q register correctly shows the expected receive values: 0x5A and 0x3C. I've also attached the code for the module as I modified it and the corresponding simulation. Hope this helps.
Attachment:
File comment: Simulation of simple SPI module.
brain_spi.JPG
brain_spi.JPG [ 82.73 KiB | Viewed 1181 times ]

Code:
`timescale 1ns / 1ps

module spi(
    input   reset,
    input   clock,

    input   load,
    input   [7:0] d,
   
    output  ss,
    output  sck,
    output  mosi,
    input   miso,
   
    output  reg [7:0] q
);

reg     in;
reg     [3:0] i;
wire    run;

assign ss   = !(run);
assign mosi = q[7];
assign run  = (!i[3]);
assign sck  = ~clock & !ss;

always @(negedge clock)
begin
    in <= #1 miso;
end

always @(posedge clock or posedge reset)
begin
    if(reset) begin
        q <= #1 0;
        i <= #1 8;
    end else if(load) begin
        q <= #1 d;
        i <= #1 0;
    end else if(run) begin
        i <= #1 i + 1;
        q <= #1 {q[6:0], in};
    end
end

endmodule

Code:
`timescale 1ns / 1ps

////////////////////////////////////////////////////////////////////////////////
// Company:
// Engineer:
//
// Create Date:   08:13:54 02/20/2016
// Design Name:   spi
// Module Name:   C:/XProjects/ISE10.1i/Work/Src/tb_brain_spi.v
// Project Name:  Work
// Target Device: 
// Tool versions: 
// Description:
//
// Verilog Test Fixture created by ISE for module: spi
//
// Dependencies:
//
// Revision:
// Revision 0.01 - File Created
// Additional Comments:
//
////////////////////////////////////////////////////////////////////////////////

module tb_brain_spi;

   // Inputs
   reg reset;
   reg clock;
   reg load;
   reg [7:0] d;
   wire miso;

   // Outputs
   wire ss;
   wire sck;
   wire mosi;
   wire [7:0] q;

   // Instantiate the Unit Under Test (UUT)
   spi uut (
      .reset(reset),
      .clock(clock),
      .load(load),
      .d(d),
      .ss(ss),
      .sck(sck),
      .mosi(mosi),
      .miso(miso),
      .q(q)
   );

   initial begin
      // Initialize Inputs
      reset = 1;
      clock = 1;
      load  = 0;
      d     = 0;
//      miso  = 0;

      // Wait 100 ns for global reset to finish
      #101 reset = 0;
       
      // Add stimulus here
       
        @(negedge clock) #1;
        @(negedge clock) #1;
       
        @(negedge clock) #1 load = 1; d = 8'hA5;
        @(posedge clock) #1 load = 0; d = 8'h00;
       
        @(posedge ss);

        @(negedge clock) #1;
        @(negedge clock) #1;
       
        @(negedge clock) #1 load = 1; d = 8'hC3;
        @(posedge clock) #1 load = 0; d = 8'h00;
   end
   
always #5 clock = ~clock;

assign miso = ~q[7];
     
endmodule

_________________
Michael A.


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 7 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to: