# SPI Master (VHDL)

**URL:** <https://forum.digikey.com/t/spi-master-vhdl/12717>\
**Category:** Logic Design\
**Tags:** tech-tips\
**Created:** [March 16, 2021, 12:34am UTC](https://forum.digikey.com/t/spi-master-vhdl/12717 "2021-03-16T00:34:33Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Scott\_1767](https://avatars.discourse-cdn.com/v4/letter/s/d26b3c/32.png) [@Scott\_1767](https://forum.digikey.com/u/Scott_1767)\
**Post date:** [March 16, 2021, 12:34am UTC](https://forum.digikey.com/t/spi-master-vhdl/12717/1 "2021-03-16T00:34:33Z")

</div>

[Logic Home](https://forum.digikey.com/t/logic-home-table-of-contents/12507)

 

# Code Download

Version 1.2: [spi\_master.vhd](https://forum.digikey.com/uploads/short-url/alQWwVonijaM7wKNzSebFtNRHhn.vhd) (8.9 KB)  
SCLK immediately assumes the value of the CPOL input when the bus is idle

Version 1.1: [spi\_master\_v1\_1.vhd](https://forum.digikey.com/uploads/short-url/hov2zOFRYOoQl6gnZHGjl6I3Hl5.vhd) (8.8 KB)  
Corrected ModelSim simulation error (explicitly reset clk\_toggles signal)

Version 1.0: [spi\_master\_v1\_0.vhd](https://forum.digikey.com/uploads/short-url/lknJtVl6XisM034Y4DzaTzsDV63.vhd) (17.0 KB)  
Initial Public Release

# Features

- VHDL source code of a Serial Peripheral Interface (SPI) master component
- Configurable number of slaves
- Configurable data width
- Selectable polarity and phase
- Selectable speed
- Continuous Mode for lengthy data streams

# Introduction

This details an SPI master component for use in CPLDs and FPGAs, written in VHDL. The component was designed using Quartus II, version 9.0. Resource requirements depend on the implementation (i.e. the desired number of slaves and data width). Figure 1 illustrates a typical example of the SPI master integrated into a system.

 ![spi_master_block](https://global.discourse-cdn.com/digikey/original/2X/4/41f13bd7b84e29faa47c6fbfcaadf76b65219a54.jpeg)

**Figure 1.** Example Application

# 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.

 ![spi_timing_diagram](https://global.discourse-cdn.com/digikey/original/2X/8/8cf1910277283f7b2125542b96812aa55be14daa.jpeg)

**Figure 2.** SPI Timing Diagram

# Port Descriptions

Table 1 describes the SPI master’s ports. The number of slaves is declared in the ENTITY by the GENERIC parameter _slaves_, and the transmit and receive data bus widths are declared by the GENERIC parameter _d\_width_.

**Table 1.** Port Descriptions

 ![port_descriptions](https://global.discourse-cdn.com/digikey/original/2X/4/41250ade38e3181469bafcc9a72b87851bcbe26b.jpeg)

# 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 1.

 ![equation_1_numbered](https://global.discourse-cdn.com/digikey/original/2X/8/867b160fe37afbef8883770ca48fc6d9c8b8b152.jpeg)

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.

# Polarity and Phase

The _enable_ pin latches in the standard logic values of _cpol_ and _cpha_ at the start of each transaction. This allows communication with individual slaves using independent SPI modes. If all slaves require the same mode, _cpol_ and _cpha_ can simply be tied to the corresponding logic levels.

# 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 3 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.

 ![transaction_timing_diagram_web](https://global.discourse-cdn.com/digikey/original/2X/f/ffd1097ec330567b59bd0c1fb15ae587dd6f1951.jpeg)

**Figure 3.** Typical Transaction Timing Diagram

# Continuous Mode

During the final receive bit, the SPI master reads the _cont_ flag. If low, the component completes the communication as described above. If high, the component latches in data on the _tx\_data_ port and seamlessly continues the transmission with the new data once the current data is exhausted. When the last bit of the current data finishes (i.e. when the first bit of new data appears on the mosi port), the component outputs the first block of received data on the _rx\_data_ port and deasserts the _busy_ signal for one _clock_ cycle to indicate this receive data is available. The receive data remains on the _rx\_data_ port until the new data communication completes and the component presents the successive block of receive data.

Figure 4 depicts the timing diagram for a transaction using continuous mode. This SPI master is instantiated with four slaves and a two bit data width. Once the component latches the initial data “10,” the _cont_ port remains high and the _tx\_data_ port presents the new data “01.” After the component latches the new data, it sets the _busy_ port low, which signals that the _cont_ and _tx\_data_ ports can be cleared and that the first receive data “01” is present on the _rx\_data_ port. When the transaction completes, the _busy_ signal again deasserts, and the new _rx\_data_ “10” is available.

 ![transaction_timing_diagram_cont_web](https://global.discourse-cdn.com/digikey/original/2X/8/8af6a82605e0020e39251015d1fa9bea8a9af948.jpeg)

**Figure 4.** Transaction Timing Diagram with Continuous Mode

Continuous Mode can transact very long data streams by being perpetually implemented. Tying the _cont_ port low disables continuous mode.

# Reset

The _reset\_n_ input port must have a logic high for the SPI master component to operate. A low signal on this port asynchronously resets the component. During reset, the component holds the _busy_ port and all _ss\_n_ outputs high. The _mosi_ output assumes a high impedance state, and the _rx\_data_ output port clears. Once released from reset, the _busy_ port deasserts on the following _clock_, indicating the SPI master’s readiness to communicate.

# Conclusion

This SPI master is a flexible programmable logic component that accommodates communication with a variety of slaves via a single parallel interface. It allows communication with a user specified number of slaves, which may require independent SPI modes, data widths, and serial clock speeds.

# Related Topics

[SPI Slave (VHDL)](https://www.digikey.com/eewiki/pages/viewpage.action?pageId=7569477)

[SPI 3-Wire Master (VHDL)](https://www.digikey.com/eewiki/pages/viewpage.action?pageId=27754638)

[SPI to I2C Bridge (VHDL)](https://www.digikey.com/eewiki/pages/viewpage.action?pageId=11042934)

[Accelerometer ADXL345 Pmod Controller (VHDL)](https://www.digikey.com/eewiki/pages/viewpage.action?pageId=90243412) – This design uses the SPI Master component described on this page to communicate with an Analog Devices ADXL345 Accelerometer. It also demonstrates the use of the Continuous Mode feature of this SPI Master.

[Accelerometer ADXL362 Pmod Controller (VHDL)](https://www.digikey.com/eewiki/pages/viewpage.action?pageId=107216903) – This design uses the SPI Master component described on this page to communicate with an Analog Devices ADXL362 Accelerometer. It also demonstrates the use of the Continuous Mode feature of this SPI Master.

[Ambient Light Sensor Pmod Controller (VHDL)](https://www.digikey.com/eewiki/pages/viewpage.action?pageId=78087312) – This design uses the SPI Master component described on this page to communicate with a Texas Instruments ADC081S021 analog-to-digital converter to gather data from Vishay Semiconductor’s TEMT6000X01 ambient light sensor.

[ADC AD7476A Pmod Controller (VHDL)](https://www.digikey.com/eewiki/pages/viewpage.action?pageId=90243836) – This design uses a version of this SPI Master component that has been modified to include a second MISO data line. With 2 data input lines, the design communicates with (receives data from) 2 separate Analog Devices AD7476A 12-bit ADCs simultaneously.

[DAC AD5541A Pmod Controller (VHDL)](https://forum.digikey.com/t/dac-ad5541a-pmod-controller-vhdl/15020) - This design uses the SPI Master above to communicate with an Analog Devices AD5541A single-channel, 16-bit digital-to-analog converter.

[DAC AD5628 Pmod Controller (VHDL)](https://www.digikey.com/eewiki/pages/viewpage.action?pageId=95552142) - This design uses the SPI Master above to communicate with an Analog Devices AD5628 12-bit digital-to-analog converter. It configures the converter and sends commands, addresses, and data to control the converter’s 8 channels.

[DAC AD7303 Pmod Controller (VHDL)](https://forum.digikey.com/t/dac-ad7303-pmod-controller-vhdl/19420) - This design uses a version of this SPI Master component that has been modified to include a second MOSI data line. The design controls 2 separate Analog Devices AD7303 dual-channel, 8-bit DACs simultaneously.

[DAC DAC121S101 Pmod Controller (VHDL)](https://www.digikey.com/eewiki/pages/viewpage.action?pageId=101023893) - This design uses a version of this SPI Master component that has been modified to include a second MOSI data line. With the 2 data output lines, the design sends data to 2 separate Texas Instruments DAC121S101 12-bit digital-to-analog converters simultaneously.

[Digital Potentiometer AD5160 Pmod Controller (VHDL)](https://forum.digikey.com/t/digital-potentiometer-ad5160-pmod-controller-vhdl/16550) - This design uses the SPI Master above to control the resistance of an Analog Devices AD5160 digital pot.

[Gyro L3G4200D Pmod Controller (VHDL)](https://forum.digikey.com/t/gyro-l3g4200d-pmod-controller-vhdl/23030) - This design uses the SPI Master above to configure and retrieve information from an ST Microelectronics L3G4200D Gyro. It also demonstrates the use of the Continuous Mode feature of this SPI Master.

[Joystick JSTK2 Pmod Controller (VHDL)](https://forum.digikey.com/t/joystick-jstk2-pmod-controller-vhdl/28986) - This design uses the SPI Master above to send commands and retrieve data from a PIC16F1618 microcontroller embedded in a joystick module.

[NAV Pmod Controller (VHDL)](https://forum.digikey.com/t/nav-pmod-controller-vhdl/24615) - This navigation design uses the SPI Master above to retrieve data from an ST Microelectronics LPS25HB pressure sensor and LSM9DS1 iNEMO inertial module (3-axis accelerometer, gyro, and magnetometer), providing a total of 10 degrees of freedom.

[Thermocouple Pmod Controller (VHDL)](https://www.digikey.com/eewiki/pages/viewpage.action?pageId=101023745) - This design uses the SPI Master above to retrieve information from a Maxim MAX31855 thermocouple-to-digital converter.

---

<div class="post-metadata">

**Author:** ![winpeerapong](https://avatars.discourse-cdn.com/v4/letter/w/b5e925/32.png) [@winpeerapong](https://forum.digikey.com/u/winpeerapong)\
**Post date:** [August 8, 2024, 7:23am UTC](https://forum.digikey.com/t/spi-master-vhdl/12717/2 "2024-08-08T07:23:08Z")

</div>

To implement code spi master, I tried to use it with my code but no any data exit to output. Do you have an example of spi test? In order I need to measure signal of output port mosi, clk and cs

---

<div class="post-metadata">

**Author:** ![Steve\_Fellman](https://avatars.discourse-cdn.com/v4/letter/s/c77e96/32.png) [@Steve\_Fellman](https://forum.digikey.com/u/Steve_Fellman)\
**Post date:** [August 8, 2024, 11:51am UTC](https://forum.digikey.com/t/spi-master-vhdl/12717/3 "2024-08-08T11:51:34Z")

</div>

Hello,  
Welcome to the DigiKey TechForum. I’m not able to assist with this, but perhaps one of our engineers may be able to offer guidance.

---

<div class="post-metadata">

**Author:** ![RobertCNelson](https://sea2.discourse-cdn.com/digikey/user_avatar/forum.digikey.com/robertcnelson/32/12_2.png) [@RobertCNelson](https://forum.digikey.com/u/RobertCNelson)\
**Post date:** [August 8, 2024, 3:57pm UTC](https://forum.digikey.com/t/spi-master-vhdl/12717/4 "2024-08-08T15:57:31Z")

</div>

Hi @winpeerapong the author of this SPI Master (VHDL) design, used physical pmod devices attached to the fpga board. Sadly he has retired, what hardware do you currently have attached to your SPI design?

Regards,

---

<div class="post-metadata">

**Author:** ![winpeerapong](https://avatars.discourse-cdn.com/v4/letter/w/b5e925/32.png) [@winpeerapong](https://forum.digikey.com/u/winpeerapong)\
**Post date:** [August 9, 2024, 1:15am UTC](https://forum.digikey.com/t/spi-master-vhdl/12717/5 "2024-08-09T01:15:09Z")

</div>

I mean testbenches for your code
