console - instructables

33
CONSOLE VHDL Project

Upload: others

Post on 29-Oct-2021

5 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CONSOLE - Instructables

CONSOLE VHDL Project 

 

Page 2: CONSOLE - Instructables

Content: ● STAGE I (Spartan 3 board) 

○ General block diagram ○ PS2 Controller 

■ PS2 description ■ Block diagram ■ Block diagram description 

○ FSM Controller ■ FSM description ■ FSM diagram 

○ VGA Controller ■ VGA description ■ Block diagram ■ Block diagram description 

○ Code 

● STAGE II (Zybo board) ○ General block diagram ○ Bit layout ○ AXI registers ○ Memory top module 

■ Memory top module diagram ■ ROM description ■ Shifter description ■ Memory register description 

○ Bitmap drawing logic ○ Priority multiplexer ○ Serial Peripheral Interface (SPI) 

■ SPI background ■ SPI clocking ■ SPI transactions 

    

Page 3: CONSOLE - Instructables

STAGE I 

 

 

Page 4: CONSOLE - Instructables

General block diagram 

  

PS2 Controller 

PS2 description:  

INTRODUCTION  PS2 port was introduced in IBM’s Personal Systed2 personal computers. It is a widely supported interface for a keyboard and mouse to communicate with the host. The PS2 port contains two wires for communication purposes. One wire is for data, which is transmitted in a serial stream. The other wire is for the clock information, which specifies when the data is valid and can be retrieved. The information is transmitted as an 1 1-bit “packet” that contains a start bit, 8 data bits, an odd parity bit, and a stop bit. Whereas the basic format of the packet is identical for a keyboard and a mouse, the interpretation for the data bits is different. The FPGA prototyping board has a PS2 port and acts as a host. We discuss the keyboard interface in this chapter and cover the mouse interface in Chapter 9. The communication of the PS2 port is bidirectional and the host can send a command to the keyboard or mouse to set certain parameters. For our purposes, the bidirectional communication is hardly required for the PS2 keyboard, and thus our discussion is limited to one direction, from the keyboard to the prototyping board.  

 

Page 5: CONSOLE - Instructables

 

Physical interface of a PS2 port  In addition to data and clock lines, the PS2 port includes connections for power (i.e., Vcc) and ground. The power is supplied by the host. In the original PS2 port, V,, is 5 V and the outputs of the data and clock lines are open-collector. However, most current keyboards and mice can work well with 3.3 V. For an older keyboard and mouse, the 5-V supply can be obtained by switching the 52 jumper on the S3 board. The FPGA should still function properly since its I/O pins can tolerate 5-V input.  

Device-to-host communication protocol PS2 device and its host communicate via packets. The basic timing diagram of transmitting a packet from a PS2 device to a host is shown in Figure 8.1, in which the data and clock signals are labeled ps2d and ps2c, respectively. The data is transmitted in a serial stream, and its format is similar to that of a UART. Transmission begins with a start bit, followed by 8 data bits and an odd parity bit, and ends with a stop bit. Unlike a UART, the clock information is carried in a separate clock signal, ps2c. The falling edge of the ps2c signal indicates that the corresponding bit in the ps2d line is valid and can be retrieved. The clock period of the ps2c signal is between 60 and 100 ps (i.e., 10 kHz to 16.7 kHz), and the ps2d signal is stable at least 5 ps before and after the falling edge of the ps2c signal.  

Overview of the scan code A keyboard consists of a matrix of keys and an embedded microcontroller that monitors (i.e., scans) the activities of the keys and sends scan code accordingly. Three types of key activities are observed: 

●  When a key is pressed, the make code of the key is transmitted. ●  When a key is held down continuously, a condition known as typematic, the make 

code is transmitted repeatedly at a specific rate. By default, a PS2 keyboard transmits the make code about every 100 ms after a key has been held down for 0.5 second. The make code of the main part of a PS2 keyboard is shown in Figure 8.3. It is normally 1 byte wide and represented by two hexadecimal numbers. For example, the make code of the A key is I C . This code can be conveyed by one packet when transmitted. The make codes of a handful of special-purpose keys, which are known as the extended keys, can have 2 to 4 bytes. A few of these keys are shown in Figure 8.3. For example, the make code of the upper arrow on the right is EO 75. Multiple packets are needed for the transmission. The break codes of the regular keys consist of FO followed by the make code of the key. For example, the break code of the A key is FO 1C. 

Page 6: CONSOLE - Instructables

The PS2 keyboard transmits a sequence of codes according to the key activities. For example, when we press and release the A key, the keyboard first transmits its make code and then the break code: 1C FO 1C If we hold the key down for awhile before releasing it, the make code will be transmitted multiple times: 1C 1C 1 C . . . 1C FO 1 C Multiple keys can be pressed at the same time. For example, we can first press the s h i f t key (whose make code is 12) and then the A key, and release the A key and then release the s h i f t key. The transmitted code sequence follows the make and break codes of the two keys: 12 1 C FO 1 C FO 12 The previous sequence is how we normally obtain an uppercase A. Note that there is no special code to distinguish the lower- and uppercase keys. It is the responsibility of the host device to keep track of whether the shift key is pressed and to determine the case accordingly.  

Block diagram: 

 

FSM controller 

FSM description:   

INTRODUCTION An FSM (finite state machine) is used to model a system that transits among a finite number of internal states. The transitions depend on the current state and external input. Unlike a regular sequential circuit, the state transitions of an FSM do not exhibit a simple, repetitive pattern. Its next-state logic is usually constructed from scratch and is sometimes known as “random” logic. This is different from the next-state logic of a regular sequential circuit, which is composed mostly of “structured” components, such as incrementors and shifters. In this chapter, we provide an overview of the basic characteristics and representation of FSMs and discuss the derivation of HDL codes. In practice, the main application of an FSM 

Page 7: CONSOLE - Instructables

is to act as the controller of a large digital system, which examines the external commands and status and activates proper control signals to control operation of a data path, which is usually composed of regular sequential components. This is known as an FSMD (finite state machine with data path).  

Mealy and Moore outputs  The basic block diagram of an FSM is the same as that of a regular sequential circuit and is repeated in Figure 5.1. It consists of a state register, next-state logic, and output logic. An FSM is known as a Moore machine if the output is only a function of state, and is known as a Mealy machine if the output is a function of state and external input. Both types of output may exist in a complex FSM, and we simply refer to it as containing a Moore output and Mealy output. The Moore and Mealy outputs are similar but not identical. Understanding their subtle differences is the key for a controller design. The example in Section 5.3.1 illustrates the behaviors and constructions of the two types of outputs.  

  

FSM representation  An FSM is usually specified by an abstract state diagram or ASM chart (algorithmic state machine chart), both capturing the FSM's input, output, states, and transitions in a graphical representation. The two representations provide the same information. The FSM representation is more compact and better for simple applications. The ASM chart representation is somewhat like a flowchart and is more descriptive for applications with complex transition conditions and actions. State diagram A state diagram is composed of nodes, which represent states and are drawn as circles, and annotated transitional arcs. A single node and its transition arcs are shown in Figure 5.2(a). A logic expression expressed in terms of input signals is associated 

Page 8: CONSOLE - Instructables

with each transition arc and represents a specific condition. The arc is taken when the corresponding expression is evaluated t r u e . The Moore output values are placed inside the circle since they depend only on the current state. The Mealy output values are associated with the conditions of transition arcs since they depend on the current state and external input. To reduce clutter in the diagram, only asserted output values are listed. The output signal takes the default (Le., unasserted) value otherwise. A representative state diagram is shown in Figure 5.3(a). The FSM has four states, two external input signals (i.e., a and b), one Moore output signal (i.e., y l ) , and one Mealy output signal (i.e., yo). The yl signal is asserted when the FSM is in the s2 or s3 state. The yo signal is asserted when the FSM is in the SO state and the a and b signals are "1 1". ASM chart An ASM chart is composed of a network of ASM blocks. An ASM block consists of one state box and an optional network of decision boxes and conditional output boxes. A representative ASM block is shown in Figure 5.2(b). A state box represents a state in an FSM, and the asserted Moore output values are listed inside the box. Note that it has only one exit path. A decision box tests the input condition and determines which exit path to take. It has two exit paths, labeled T and F, which correspond to the t r u e and f a l s e values of the condition. A conditional output box lists asserted Mealy output values and is usually placed after a decision box. It indicates that the listed output signal can be activated only when the corresponding condition in the decision box is met.    

 

Page 9: CONSOLE - Instructables

   A state diagram can easily be converted to an ASM chart, and vice versa. The corresponding ASM chart of the previous FSM state diagram is shown in Figure 5.3(b).  

 

 

 

   

Page 10: CONSOLE - Instructables

FSM diagram:  

      

Page 11: CONSOLE - Instructables

 

VGA controller 

VGA description: 

Basic operation of a CRT  VGA (video graphics array) is a video display standard introduced in the late 1980s in IBM PCs and is widely supported by PC graphics hardware and monitors. The conceptual sketch of a monochrome CRT monitor is shown in Figure 12.1. The electron gun (cathode) generates a focused electron beam, which traverses a vacuum tube and eventually hits the phosphorescent screen, Light is emitted at the instant that electrons hit a phosphor dot on the screen. The intensity of the electron beam and the brightness of the dot are determined by the voltage level of the external video input signal, labeled mono in Figure 12.1. The mono signal is an analog signal whose voltage level is between 0 and 0.7 V. A vertical deflection coil and a horizontal deflection coil outside the tube produce magnetic fields to control how the electron beam travels and to determine where on the screen the electrons hit. In today’s monitors, the electron beam traverses (i.e., scans) the screen systematically in a fixed pattern, from left to right and from top to bottom, as shown in Figure 12.2   

 Figure 12.1 Conceptual diagram of a CRT monitor. 

 

Page 12: CONSOLE - Instructables

  

                  The monitor's internal oscillators and amplifiers generate sawtooth waveforms to control the two deflection coils. For example, the electron beam moves from the left edge to the right edge as the voltage applied to the horizontal deflection coil gradually increases. After reaching the right edge, the beam returns rapidly to the left edge (i.e., retraces) when the voltage changes to 0. The relationship between the sawtooth waveform and the scan is shown in Figure 12.4. Two external synchronization signals, hsync and vsync, control generation of the sawtooth waveforms. These signals are digital signals. The relationship between the hsync signal and the horizontal sawtooth is also shown in Figure 12.4. Note that the I' 1 and "0" periods of the hsync signal correspond to the rising and falling ramps of the sawtooth waveform. The basic operation of a color CRT is similar except that it has three electron beams, 

Page 13: CONSOLE - Instructables

which are projected to the red, green, and blue phosphor dots on the screen. The three dots are combined to form a pixel. We can adjust the voltage levels of the three video input signals to obtain the desired pixel color.  

VIdeo controller  A video controller generates the synchronization signals and outputs data pixels serially. A simplified block diagram of a VGA controller is shown in Figure 12.3. It contains a synchronization circuit, labeled vga-sync, and a pixel generation circuit. 

 The vga-sync circuit generates the timing and synchronization signals. The hsync and vsync signals are connected to the VGA port to control the horizontal and vertical scans of the monitor. The two signals are decoded from the internal counters, whose outputs are the pixel-x and pixel-y signals. The pixel_x and pixel_y signals indicate the relative positions of the scans and essentially specify the location of the current pixel. The vga-sync circuit also generates the video-on signal to indicate whether to enable or disable the display. The design of this circuit is discussed in Section 12.2. The pixel generation circuit generates the three video signals, which are collectively referred to as the rgb signal. A color value is obtained according to the current coordinates of the pixel (the pixel_x and pixel_y signals) and the external control and data signals. VGA SYNCHRONIZATION  The video synchronization circuit generates the hsync signal, which specifies the required 

Page 14: CONSOLE - Instructables

time to traverse (scan) a row, and the vsync signal, which specifies the required time to traverse (scan) the entire screen. Subsequent discussions are based on a 640-by-480 VGA screen with a 25-MHz pixel rate, which means that 25M pixels are processed in a second. Note that this resolution is also known as the VGA mode. The screen of a CRT monitor usually includes a small black border, as shown at the top of Figure 12.4. The middle rectangle is the visible portion. Note that the coordinate of the vertical axis increases downward. The coordinates of the top-left and bottom-right corners are (0,O) and (639,479), respectively.  

Horizontal synchronization  A detailed timing diagram of one horizontal scan is shown in Figure 12.4. A period of the hsync signal contains 800 pixels and can be divided into four regions:    

 Figure 12.4 Timing diagram of a horizontal scan. 

Page 15: CONSOLE - Instructables

 ●  Display: region where the pixels are actually displayed on the screen. The length of 

this region is 640 pixels. ● Retrace: region in which the electron beams return to the left edge. The video signal 

should be disabled (i.e., black), and the length of this region is 96 pixels. ●  Right border: region that forms the right border of the display region. It is also know 

as the front porch (i.e., porch before retrace). The video signal should be disabled, and the length of this region is 16 pixels. 

●  Left border: region that forms the left border of the display region. It is also know as the backporch (i.e., porch after retrace). The video signal should be disabled, and the length of this region is 48 pixels. Note that the lengths of the right and left borders may vary for different brands of monitors. The hsync signal can be obtained by a special mod-800 counter and a decoding circuit. The counts are marked on the top of the hsync signal in Figure 12.4. We intentionally start the counting from the beginning of the display region. This allows us to use the counter output as the horizontal (x-axis) coordinate. This output constitutes the pixel_x signal. The hsync signal goes low when the counter’s output is between 656 and 75 1. Note that the CRT monitor should be black in the right and left borders and during retrace. We use the h_video_on signal to indicate whether the current horizontal coordinate is in the displayable region. It is asserted only when the pixel count is smaller than 640.      

Page 16: CONSOLE - Instructables

Vertical synchronization  During the vertical scan, the electron beams move gradually from top to bottom and then return to the top. This corresponds to the time required to refresh the entire screen. The format of the vsync signal is similar to that of the hsync signal, as shown in Figure 12.5. The time unit of the movement is represented in terms of horizontal scan lines. A period of the vsync signal is 525 lines and can be divided into four regions: 

●  Display: region where the horizontal lines are actually displayed on the screen. The length of this region is 480 lines. 

●  Retrace: region that the electron beams return to the top of the screen. The video signal should be disabled, and the length of this region is 2 lines. 

●  Bottom border: region that forms the bottom border of the display region. It is also know as the frontporch (i.e., porch before retrace). The video signal should be disabled, and the length of this region is 10 lines. 

●  Top border: region that forms the top border of the display region. It is also know as the backporch (i,e,, porch after retrace). The video signal should be disabled, and the length of this region is 33 lines. As in the horizontal scan, the lengths of the top and bottom borders may vary for different brands of monitors. The vsync signal can be obtained by a special mod-525 counter and a decoding circuit. Again, we intentionally start counting from the beginning of the display region. This allows us to use the counter output as the vertical (y-axis) coordinate. This output constitutes the pixel-y signal. The vsync signal goes low when the line count is 490 or 491. As in the horizontal scan, we use the v-video-on signal to indicate whether the current vertical coordinate is in the displayable region. It is asserted only when the line count is smaller than 480.  

Page 17: CONSOLE - Instructables

Timing calculation of VGA synchronization signals 

 

HDL Implementation  The function of the vga-sync circuit is discussed in Section 12.1.3. If the frequency of the system clock is 25 MHz, the circuit can be implemented by two special counters: a mod-800 counter to keep track of the horizontal scan and a mod-525 counter to keep track of the vertical scan. Since our designs generally use the 50-MHz oscillator of the prototyping board, the system clock rate is twice the pixel rate. Instead of creating a separate 25-MHz clock domain and violating the synchronous design methodology, we can generate a 25-MHz enable tick to enable or pause the counting. The tick is also routed to the p-tick port as an output signal to coordinate operation of the pixel generation circuit. The HDL code is shown in Listing 12.1. It consists of a mod-2 counter to generate the 25-MHz enable tick and two counters for the horizontal and vertical scans. We use two status signals, h-end and v-end, to indicate completion of the horizontal and vertical scans. The values of various regions of the horizontal and vertical scans are defined as constants. They can be easily modified if a different resolution or refresh rate is used. To remove potential glitches, output buffers are inserted for the hsync and vsync signals. This leads 

Page 18: CONSOLE - Instructables

to a one-clock-cycle delay. We should add a similar buffer for the rgb signal in the pixel generation circuit to compensate for the delay.  

OVERVIEW OF THE PIXEL GENERATION CIRCUIT  The pixel generation circuit generates the 3-bit rgb signal for the VGA port. The external control and data signals specify the content of the screen, and the pixel-x and pixel-y signals from the vga-sync circuit provide the current coordinates of the pixel. For our discussion purposes, we divided this circuit into three broad categories: 

●  Bit-mapped scheme ●  Tile-mapped scheme ●  Object-mapped scheme 

In a bit-mapped scheme, a video memory is used to store the data to be displayed on the screen. Each pixel of the screen is mapped directly to a memory word, and the pixel-x and pixel-y signals form the address. A graphics processing circuit continuously updates the screen and writes relevant data to the video memory. A retrieval circuit continuously reads the video memory and routes the data to the rgb signal. This is the scheme used in today’s high-performance video controller. For 640-by-480 resolution, there are about 3 10k (i.e., 640*480) pixels on a screen. This translates to 310k memory bits for a monochrome display and 930k memory bits (i.e., 3 bits per pixel) for a 3-bit color display. A bit-mapped example is discussed in Section 12.5. To reduce the memory requirement, one alternative is to use a tile-mapped scheme. In this scheme, we group a collection of bits to form a tile and treat each tile as a display unit. For example. we can define an 8-by-8 square of pixels (i.e., 64 pixels) as a tile. The 640-by-480 pixel-oriented screen becomes an 80-by-60 tile-oriented screen. Only 4800 (i.e., 8060) words are needed for the tile memory. The number of bits in a word depends on the number of tile patterns. For example, if there are 32 tile patterns, each word should contain 5 bits, and the size of the tile memory is about 24k bits (i.e., 5*4800). The tile-mapped scheme usually requires a ROM to store the tile patterns. We call it pattern memory. Assume that monochrome patterns are used in the previous example. Each 8-by- 8 tile pattern requires 64 bits, and the entire 32 patterns need 2K (i.e., 8*8*32) bits. The overall memory requirement is about 26k bits, which is much smaller than the 310k bits of the bit-mapped scheme. The text display discussed in Chapter 13 is based on this scheme. For some applications, the video display can be very simple and contains only a few objects. Instead of wasting memory to store a mostly blank screen, we can generate these objects using simple object generation circuits. We call this approach an object-mapped scheme. An object-mapped example is discussed in Section 12.4. The three schemes can be mixed together to generate a full screen. For example, we can use a bit-mapped scheme to generate the background and use an object-mapped scheme to produce the main objects. We can also use a bit-mapped scheme for one portion of a screen and tile-mapped text for another part of the screen. 

Page 19: CONSOLE - Instructables

  

GRAPHIC GENERATION WITH AN OBJECT-MAPPED SCHEME  The conceptual diagram of an object-mapped pixel generation circuit that contains three objects is shown in Figure 12.6. The diagram consists of three object generation circuits and a special selecting and routing circuit, labeled rgb mux. An object generation circuit performs the following tasks: 

●  It keeps the coordinates of the current object and compares it with the current scan location provided by the pixel-x and pixel-y signals. 

●  If the current scan location falls within the region, it asserts the obj_i_on  signal to indicate that the current scan location is within the region of the ith object and the object should be “turned on.” 

●  It specifies the desired color in the obj_i_rgb  signal. The rgb mux circuit performs multiplexing according to an internal prioritizing scheme. It examines various obj_i_on signals and determines which obj_i_rgb signal is to be routed to the rgb output. The prioritizing scheme prioritizes the order of the displays when multiple obj_i _on signals are asserted at the same time. It corresponds to selecting an object for the foreground.     

Page 20: CONSOLE - Instructables

Block Diagram: 

Full VGA block Diagram 

Pixel generator Diagram 

 

Page 21: CONSOLE - Instructables

Game1_generator Diagram 

   

Block diagram description:  

Full VGA Diagram:  

● Debounce circuit - These circuits are used to filter through the fluctuations of the signal caused by the switch transitions. This ensures us that there won’t be any glitches during the program’s execution. The method used here involves waiting for the arrival of the rising edge, memorizing the value then waiting the minimum of 20ms before reading the input signal again. 

● VGA_sync - This part of the circuit is explained in detail in section VGA Description. 

● Pixel generators - This block is used to display the content on the screen. The inside of this block and its function in detail is explained below. 

Page 22: CONSOLE - Instructables

● FSM - This finite-state machine controls which screen(“menu”) is shown. It actively monitors the pattern of pressed buttons and on the basis of that sends appropriate signals to Pixel generator controlling the multiplexer inside. The machine also controls the colour of the selected text depending on the state it is in. The diagram for the FSM can be seen below.  

 

  

Pixel Generator Diagram:  

● Game1_generator - This generator is used to display the game named Breakout or as herein enumerated : I. More about this generator is explained below. 

● Interface_generator -This generator displays the menu screen with the following options: Games, Settings, Credits, Quit. For now Games and Credits are the only ones that are functional. 

● Game_interface - This generator displays the screen with four games where the user can choose which to play. In this phase one game exists. 

Page 23: CONSOLE - Instructables

● Credits_generator - This generator displays the names of students who worked on the project. 

Diagram Game1_generator:  

● main_bar - This block generates the movable paddle(bar) to bounce the ball upward. This block contains the paddles positions as output signals and connects them to the ball block. The reason for this is to detect a collision between the ball and the paddle(bar). 

● ball - This block foremost controls the ball, its movement and its bouncing. This block also contains the coordinates of all thirty bricks and on the basis of that the bouncing conditions are written. It is important to note that the bouncing conditions have four scenarios : when the ball hits the top, bottom, left and right side of brick. Consequently a logic was made to first detect if the ball has been hit and then which side was hit, which means that for the ball's movement it doesn’t matter which brick was hit.   

● bricks - This block consists of two parts. First part is a block that generates thirty bricks and the second part is a modified comparator. This comparator quantizes brick hits using a certain logic. The block then sends the information on which side of which brick was hit back to the ball block. 

● register - This register serves as a logic that would prevent the ball to hit the same brick two times. It’s not only important to remove the brick off the screen, but also its invisible edges have to disappear too. The output of this circuit is always ‘1’ until the ball hits a certain brick, when that happens it sends a ‘0’ signal for the brick that it hit. Ones this ‘0’ signal arrives at the input, the output of the register for that brick becomes ‘0’ forever. The only way to set the output  (setting bricks) to ‘1’ is to reset.  

● and -  This block helps remove any remnants of the brick after it has been hit (like collision conditions etc.) so the ball wouldn’t hit the sam brick more than once.  

● mux - This is a very specific type of a multiplexer that works a little atypically. He is mainly responsible for displaying and dyeing each of these components (ball, tiles, bricks).  

● dec - This decoder converts the bits to adjust them for the connection to the screen.   ● d_ff - This flip-flop was inserted to eliminate possible glitches. 

 

 

Code The code can be found on the following link:   goo.gl/3Y5IS ** Promeniti     

Page 24: CONSOLE - Instructables

STAGE II  

    

Page 25: CONSOLE - Instructables

General block diagram 

 

 AXI registers  

   

Page 26: CONSOLE - Instructables

Bit layout  

  Memory top module 

  ● Memory top module diagram 

 

 

Page 27: CONSOLE - Instructables

● ROM description  

ROM contains a 2N patterns and each requires  8 * 8 bits. Pattern is a bit-map of object.  

 

Page 28: CONSOLE - Instructables

 ● Shifter description 

 Shifter is used to rotate the content of rom memory by 180 degrees adjusting it for future drawing. 

 ● Memory register description 

 

Memory register is used to copy the content of ROM in its memory and make them available to the rest of the blocks (parts of system).    

Page 29: CONSOLE - Instructables

Bitmap drawing logic 

  In the bit-mapped scheme, we can scale a pattern to larger sizes by “enlarging” the screen pixels.  Magnifying have realized by increasing the bit-map from 8x8 (size "00") to 16x16 (size "01"), 32x32 (size "10") or 64x64 (size "11") and a slowdown clock enable respectively 2, 4 and 8 times. 

 Priority multiplexer  

     

Page 30: CONSOLE - Instructables

Serial Peripheral Interface (SPI)  ● SPI background 

 An SPI communication scheme is a full-duplex data link, using four wires.  The master initiates the transaction by pulling the Slave Select (SS) wire low.  A Serial Clock (SCLK) line, driven by the master, provides a synchronous clock source.  The master transmits data via the Master Out, Slave In (MOSI) line and receives data via the Master In, Slave Out (MISO) line. 

A master can communicates with multiple slaves via a variety of techniques.  In the most common configuration, each slave has an independent SS line but shares the SCLK, MISO, and MOSI lines with the other slaves.  Each slave ignores the shared lines when its SS line is not pulled low.  This topology is shown in Figure 1 above. 

SPI has four modes of operation, based on two parameters:  clock polarity (CPOL) and clock phase (CPHA).  Master and slave must use the same mode to communicate articulately.  If CPOL is zero, then SCLK is normally low, and the first clock edge is a rising edge.  If CPOL is one, SCLK is normally high, and the first clock edge is a falling edge. CPHA defines the data alignment.  If CPHA is zero, then the first data bit is written on the SS falling edge and read on the first SCLK edge.  If CPHA is one, data is written on the first SCLK edge and read on the second SCLK edge.  The timing diagram in Figure 2 depicts the four SPI modes. 

 

 

    

Page 31: CONSOLE - Instructables

 ● SPI clocking 

 The clock and clk_div inputs define the frequency of SCLK (i.e. the SPI data rate).  clock is the system clock used to operate the synchronous logic inside the component.  The clk_div integer input allows the user to set the relative speed at which the current transaction occurs.  clk_div is the number of clock periods between SCLK transitions, as described by equation:  

  

When the clk_div port is set to 1, the SCLK frequency is half the clock frequency and is set at the maximum achievable data rate.  The enable pin latches the value of clk_div into the component to begin each transaction, so it is possible to adjust the data rate for individual slaves. 

Setting the clk_div port to a constant value permanently sets the data rate.  If clk_div is set to 0, the component assumes a value of 1.  Therefore, tying the clk_div port low configures the component to always operate at maximum speed.  

    

Page 32: CONSOLE - Instructables

 ● SPI transactions 

 A low logic level on the busy output port indicates that the component is ready to accept a command.  The component latches the settings, address, and data for a transaction on the first rising edge of clock where the enable input is asserted.  On the following clock, the component asserts the busy signal and begins performing the transaction.  Once complete, the component outputs the received data on the rx_data port.  This data remains on the port until the component receives new data from a subsequent transaction.  The component sets busy low to notify the user when the data is available, and the component is immediately ready for another instruction. 

Figure shows the timing diagram for a typical transaction.  This SPI master is instantiated with four slaves and a four bit data width.  It transmits the data “1001” to slave 2, which operates in SPI mode 3 (CPOL = 1, CPHA = 1).  The master receives the “1010” data. 

 

  

  

Page 33: CONSOLE - Instructables