• 1

    ..

  • 2

    ...

  • 3

    ...

Saturday 28 September 2013

SPI MASTER SLAVE Verilog Code - SPI Working Modes of Operation - Applications - Advantages Disadvantages


SPI means Serial Pheripheral Interface.
The Serial Peripheral Interface Bus or SPI bus is a synchronous serial data link de facto standard, named by Motorola, that operates in full duplex mode. Devices communicate in master/slave mode where the master device initiates the data frame. Multiple slave devices are allowed with individual slave select (chip select) lines. Sometimes SPI is called a four-wire serial bus, contrasting with three-, two-, and one-wire serial buses. SPI is often referred to as SSI (Synchronous Serial Interface).
Data transmission:
A typical hardware setup using two shift registers to form an inter-chip circular buffer

To begin a communication, the bus master first configures the clock, using a frequency less than or equal to the maximum frequency the slave device supports. Such frequencies are commonly in the range of 10 kHz–100 MHz.
The master then transmits the logic 0 for the desired chip over the chip select line. A logic 0 is transmitted because the chip select line is active low, meaning its off state is a logic 1; on is asserted with a logic 0. If a waiting period is required (such as for analog-to-digital conversion), then the master must wait for at least that period of time before starting to issue clock cycles.
During each SPI clock cycle, a full duplex data transmission occurs:

  •  the master sends a bit on the MOSI line; the slave reads it from that same line 
  •   the slave sends a bit on the MISO line; the master reads it from that same line
Advantages:
  • Full duplex communication
  • Higher throughput than I²C or SMBus
  • Complete protocol flexibility for the bits transferred
    • Not limited to 8-bit words
    • Arbitrary choice of message size, content, and purpose
  • Extremely simple hardware interfacing
    • Typically lower power requirements than I²C or SMBus due to less circuitry (including pull up resistors)
    • No arbitration or associated failure modes
    • Slaves use the master's clock, and don't need precision oscillators
    • Slaves don't need a unique address — unlike I²C or GPIB or SCSI
    • Transceivers are not needed
  • Uses only four pins on IC packages, and wires in board layouts or connectors, much fewer than parallel interfaces
  • At most one unique bus signal per device (chip select); all others are shared
  • Signals are unidirectional allowing for easy Galvanic isolation
  • Not limited to any maximum clock speed, enabling potentially high throughput
Disadvantages:
  • Requires more pins on IC packages than I²C, even in the three-wire variant
  • No in-band addressing; out-of-band chip select signals are required on shared buses
  • No hardware flow control by the slave (but the master can delay the next clock edge to slow the transfer rate)
  • No hardware slave acknowledgment (the master could be transmitting to nowhere and not knowing it)
  • Supports only one master device
  • No error-checking protocol is defined
  • Generally prone to noise spikes causing faulty communication
  • Without a formal standard, validating conformance is not possible
  • Only handles short distances compared to RS-232, RS-485, or CAN-bus
  • Many existing variations, making it difficult to find development tools like host adapters that support those variations
  • SPI does not support hot plugging (dynamically adding nodes).
Applications:
The board real estate savings compared to a parallel I/O bus are significant, and have earned SPI a solid role in embedded systems. That is true for most system-on-a-chip processors, both with higher end 32-bit processors such as those using ARM, MIPS, or PowerPC and with other microcontrollers such as the AVR, PIC, and MSP430. These chips usually include SPI controllers capable of running in either master or slave mode. In-system programmable AVR controllers (including blank ones) can be programmed using an SPI interface.
Chip or FPGA based designs sometimes use SPI to communicate between internal components; on-chip real estate can be as costly as its on-board cousin.
The full-duplex capability makes SPI very simple and efficient for single master/single slave applications. Some devices use the full-duplex mode to implement an efficient, swift data stream for applications such as digital audio, digital signal processing, or telecommunications channels, but most off-the-shelf chips stick to half-duplex request/response protocols.
SPI is used to talk to a variety of peripherals, such as
  • Sensors: temperature, pressure, ADC, touchscreens, video game controllers
  • Control devices: audio codecs, digital potentiometers, DAC
  • Camera lenses: Canon EF lens mount
  • Communications: Ethernet, USB, USART, CAN, IEEE 802.15.4, IEEE 802.11, handheld video games
  • Memory: flash and EEPROM
  • Real-time clocks
  • LCD, sometimes even for managing image data
  • Any MMC or SD card (including SDIO variant)

Modes of Operation:
MODE
SETUP
SAMPLE
0
falling
rising
1
rising
falling
2
rising
falling
3
falling
rising

          In the SPI communication, there can be 2 or more devices and 4 modes of operation depending of the clock phase & polarity. Only one master device & a number of slave exits on the bus which are controlled by Master. So simply there are two devices namely MASTER DEVICE and SLAVE DEVICE.

       The Master module in mode 3 is designed using Verilog as a FSM (finite state machine), with 3 states namely IDLE, SEND & FINISH. And the Slave module is simple like shift register. These are designed and tested in Xilinx & ModelSim.


SPI MODE 3:
          CHANGE DATA @ NEGEDGE
          read data @posedge
The SPI Mode 3 waveform is

Let us see the MASTER MODULE here. (Slave module & joining Master-Slave in upcoming postings)
       In IDLE state, it waits till the data is available and then proceeds to LOAD state where the direction, data are stored into transmit register. In SEND state, the data is shifted out & parallely sampled from DIN and after completing it goes to FINISH state putting received data into receive buffer & sets Done flag.
    The block diagram of MASTER MODULE is as follows,


 RSTB-active low asyn reset, CLK-clock, T_RB=0-rx  1-TX, mlb=0-LSB 1st 1-msb 1st
 START=1- starts data transmission cdiv 0=clk/4 1=/8   2=/16  3=/32

The SPI MASTER module code is as follows:
   STATE MACHINE:
//state transistion
always@(negedge clk or negedge rstb) begin
 if(rstb==0)
   cur<=finish;
 else
   cur<=nxt;
 end
//FSM I/O
always @(start or cur or nbit) begin
                    nxt=cur;
                    clr=0; 
                    shift=0;//ss=0;
                    case(cur)
                             idle:begin
                                      if(start==1)
                                  begin
                                                                    case (cdiv)
                                                                             2’b00: mid=2;
                                                                             2’b01: mid=4;
                                                                             2’b10: mid=8;
                                                                             2’b11: mid=16;
                                                                    endcase
                                                          shift=1;
                                                          done=1’b0;
                                                          nxt=send;  
                                                          end
                           end //idle
                             send:begin
                                      ss=0;
                                      if(nbit!=8)
                                                begin shift=1; end
                                      else begin
                                                          rdata=rreg;done=1’b1;
                                                          nxt=finish;
                                                end
                                      end//send
                             finish:begin
                                                shift=0;
                                                ss=1;
                                                clr=1;
                                                nxt=idle;
                                       end
                             default: nxt=finish;
      endcase
    end//always
CLOCK GENERATOR BLOCK:
always@(negedge clk or posedge clr) begin
  if(clr==1)
                   begin cnt=0; sck=1; end
  else begin
          if(shift==1) begin
                   cnt=cnt+1;
            if(cnt==mid) begin
                    sck=~sck;
                   cnt=0;
                   end //mid
          end //shift
 end //rst
end
Tx BLOCK:
always@(negedge sck or posedge clr) begin
 if(clr==1) begin
            treg=8’hFF;  dout=1; 
  end 
 else begin
                   if(nbit==0) begin //load data into TREG
                             treg=tdat; dout=mlb?treg[7]:treg[0];
                   end //nbit_if
                   else begin
                             if(mlb==0) //LSB first, shift right
                                      begin treg={1’b1,treg[7:1]}; dout=treg[0]; end
                             else//MSB first shift LEFT
                                      begin treg={treg[6:0],1’b1}; dout=treg[7]; end
                   end
 end //rst
end
Rx BLOCK:
always@(posedge sck or posedge clr ) begin // or negedge rstb
 if(clr==1)  begin
                             nbit=0;  rreg=8’hFF;  end
    else begin
                     if(mlb==0) //LSB first, din@msb -> right shift
                             begin  rreg={din,rreg[7:1]};  end
                     else  //MSB first, din@lsb -> left shift
                             begin  rreg={rreg[6:0],din};  end
                     nbit=nbit+1;
 end //rst
end

SIMULATION OUPTUT OF MASTER:


If you enjoyed this post plz let us know your views via comments.
This helps us to do much more better.
Thankyou.

Thursday 26 September 2013

IMAGE PROCESSING IN VERILOG - ADD IMAGES IN VERILOG - MATLAB XILINX MODELSIM

In previous postings, we have seen how to Convert an Image into Text file for processing in HDL (verilog, VHDL) Now let us see how to process the text converted image in Verilog. We have implemented this in Xilinx and Modelsim.

The flow is simple
1. Read the text file
2. Convert to hexadecimal/binary
3. Pass to adder
4. Get the sum/output
5. Write to text file.

After that using matlab function, convert the output text file back to image.

The verilog codes are as follows,,
MAIN MODULE:
module imaddr(rstb,clk,en,a,b,sum,car);
 input rstb,clk,en;
 input [7:0] a,b;
 output reg [7:0] sum;
 output reg car;
always @ (negedge clk or negedge rstb) begin
  if(rstb==0)
   begin sum=0; car=0; end
  else begin
       if(en==1)
          begin {car,sum}=(a+b); end //en if
    end //else end
end
endmodule


TEST BENCH:
File Read:
    $readmemh("y_out.txt", hexad);
The image pixel value is read into "hexad" variable & then it is converted to decimal for processing.


Passing pixel values to IMAdder
//store output value @negedge since UUT is NEGEDGE sensitive ie, UUT produces output @ NegEdge
always @ (negedge clk) begin
  if(rstb==1 && en==1 && w!=(n+1)) begin
        if(w!=n) begin //check if count reaches max (image size)
              a<=decim[w]; b<=decim2[w]; //put values for UUT
        out[w]=sum; //store UUT output to array
        $fwrite(outfile,"%d\n",sum);
        $display("\n%d",sum);
        w=w+1; //increment count
           end
        else begin a<=a; b<=b; end
    end
  else if(w==(n+1) && done==0) begin done=1;    $display("Img Adding complete..."); end


CLICK HERE TO DOWNLOAD:   IMADDER.v    TESTBENCH.v


If you enjoyed or have any doubts regarding this post plz let us know your views via comments or visit our FORUM

This helps us to do much more better.
Thankyou.



VCD Description - Export VCD in Xilinx Modelsim - Example

         VCD means Value Change Dump. VCD is an ASCII-based format for dumpfiles generated by EDA logic simulation tools. The standard, four-value VCD format was defined along with the Verilog hardware description language by the IEEE Standard 1364-1995 in 1995. An Extended VCD format defined six years later in the IEEE Standard 1364-2001 supports the logging of signal strength and directionality. The simple and yet compact structure of the VCD format has allowed its use to become ubiquitous and to spread into non-Verilog tools such as the VHDL simulator GHDL and various kernel tracers. A limitation of the format is that it is unable to record the values in memories.



         VCD is in common use for Verilog designs, and is controlled by VCD system task calls in the Verilog source code. ModelSim provides command equivalents for these system tasks and extends VCD support to SystemC and VHDL designs. The ModelSim commands can be used on VHDL, Verilog, SystemC, or mixed designs.
        Extended VCD supports VHDL and Verilog ports in a mixed-language design containing SystemC, however Extended VCD does not support SystemC ports in these desings.
 
 
VCD FILE CREATION IN XILINX ISIM:
Enter the following script in ISIM Command Prompt at the end of simulation
Syntax:
         vcd dumpfile adder_dump.vcd
         vcd dumpvars –m adder

 Example for an adder,
        vcd dumpfile adder_dump.vcd
        vcd dumpvars –m adder

VCD FILE CREATION IN MODELSIM:
There are two flows in ModelSim for creating a VCD file.
·         One flow produces a four-state VCD file with variable changes in 0, 1, x, and z with no strength information.
·         The other flow produces an extended VCD file with variable changes in all states and strength information and port driver data.
Both flows will also capture port driver changes unless filtered out with optional command-line arguments.

        Enter the following script in VSIM Command Line at the end of simulation
Syntax four-state VCD export:
vcd file <filename>
vcd add <signals>
run

Syntax Extended VCD export:
vcd dumpports -file <filename> <signals>
run

Example for an adder,
vcd file addr.vcd
vcd add Test_Bench/UUT/*
run
vcd dumpports -file addr2.vcd Test_Bench/UUT/*
run



Search Here...