--===========================================================================--
|
--===========================================================================--
|
-- --
|
-- --
|
-- Synthesizable Serial Peripheral Interface Master --
|
-- Synthesizable Serial Peripheral Interface Master --
|
-- --
|
-- --
|
--===========================================================================--
|
--===========================================================================--
|
--
|
--
|
-- File name : spi-master.vhd
|
-- File name : spi-master.vhd
|
--
|
--
|
-- Entity name : spi-master
|
-- Entity name : spi-master
|
--
|
--
|
-- Purpose : Implements a SPI Master Controller
|
-- Purpose : Implements a SPI Master Controller
|
--
|
--
|
-- Dependencies : ieee.std_logic_1164
|
-- Dependencies : ieee.std_logic_1164
|
-- ieee.std_logic_arith
|
|
-- ieee.std_logic_unsigned
|
-- ieee.std_logic_unsigned
|
-- ieee.numeric_std
|
|
-- unisim.vcomponents
|
|
--
|
--
|
-- Author : Hans Huebner
|
-- Author : Hans Huebner
|
--
|
--
|
-- Email : hans@huebner.org
|
-- Email : hans@huebner.org
|
--
|
--
|
-- Web : http://opencores.org/project,system09
|
-- Web : http://opencores.org/project,system09
|
--
|
--
|
-- Description : This core implements a SPI master interface.
|
-- Description : This core implements a SPI master interface.
|
-- Transfer size is 4, 8, 12 or 16 bits.
|
-- Transfer size is 4, 8, 12 or 16 bits.
|
-- The SPI clock is 0 when idle, sampled on
|
-- The SPI clock is 0 when idle, sampled on
|
-- the rising edge of the SPI clock.
|
-- the rising edge of the SPI clock.
|
-- The SPI clock is derived from the bus clock input
|
-- The SPI clock is derived from the bus clock input
|
-- divided by 2, 4, 8 or 16.
|
-- divided by 2, 4, 8 or 16.
|
--
|
--
|
-- clk, reset, cs, rw, addr, data_in, data_out and irq
|
-- clk, reset, cs, rw, addr, data_in, data_out and irq
|
-- represent the System09 bus interface.
|
-- represent the System09 bus interface.
|
-- spi_clk, spi_mosi, spi_miso and spi_cs_n are the
|
-- spi_clk, spi_mosi, spi_miso and spi_cs_n are the
|
-- standard SPI signals meant to be routed off-chip.
|
-- standard SPI signals meant to be routed off-chip.
|
--
|
--
|
-- The SPI core provides for four register addresses
|
-- The SPI core provides for four register addresses
|
-- that the CPU can read or writen to:
|
-- that the CPU can read or writen to:
|
--
|
--
|
-- Base + $00 -> DL: Data Low LSB
|
-- Base + $00 -> DL: Data Low LSB
|
-- Base + $01 -> DH: Data High MSB
|
-- Base + $01 -> DH: Data High MSB
|
-- Base + $02 -> CS: Command/Status
|
-- Base + $02 -> CS: Command/Status
|
-- Base + $03 -> CO: Config
|
-- Base + $03 -> CO: Config
|
--
|
--
|
-- CS: Write bits:
|
-- CS: Write bits:
|
--
|
--
|
-- CS[0] START : Start transfer
|
-- CS[0] START : Start transfer
|
-- CS[1] END : Deselect device after transfer
|
-- CS[1] END : Deselect device after transfer
|
-- (or immediately if START = '0')
|
-- (or immediately if START = '0')
|
-- CS[2] IRQEN : Generate IRQ at end of transfer
|
-- CS[2] IRQEN : Generate IRQ at end of transfer
|
-- CS[6:4] SPIAD : SPI device address
|
-- CS[6:4] SPIAD : SPI device address
|
--
|
--
|
-- CS: Read bits
|
-- CS: Read bits
|
--
|
--
|
-- CS[0] BUSY : Currently transmitting data
|
-- CS[0] BUSY : Currently transmitting data
|
--
|
--
|
-- CO: Write bits
|
-- CO: Write bits
|
--
|
--
|
-- CO[1:0] DIVIDE: SPI clock divisor,
|
-- CO[1:0] DIVIDE: SPI clock divisor,
|
-- 00=clk/2,
|
-- 00=clk/2,
|
-- 01=clk/4,
|
-- 01=clk/4,
|
-- 10=clk/8,
|
-- 10=clk/8,
|
-- 11=clk/16
|
-- 11=clk/16
|
-- CO[3:2] LENGTH: Transfer length,
|
-- CO[3:2] LENGTH: Transfer length,
|
-- 00= 4 bits,
|
-- 00= 4 bits,
|
-- 01= 8 bits,
|
-- 01= 8 bits,
|
-- 10=12 bits,
|
-- 10=12 bits,
|
-- 11=16 bits
|
-- 11=16 bits
|
--
|
--
|
-- Copyright (C) 2009 - 2010 Hans Huebner
|
-- Copyright (C) 2009 - 2010 Hans Huebner
|
--
|
--
|
-- This program is free software: you can redistribute it and/or modify
|
-- This program is free software: you can redistribute it and/or modify
|
-- it under the terms of the GNU General Public License as published by
|
-- it under the terms of the GNU General Public License as published by
|
-- the Free Software Foundation, either version 3 of the License, or
|
-- the Free Software Foundation, either version 3 of the License, or
|
-- (at your option) any later version.
|
-- (at your option) any later version.
|
--
|
--
|
-- This program is distributed in the hope that it will be useful,
|
-- This program is distributed in the hope that it will be useful,
|
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
|
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
|
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
-- GNU General Public License for more details.
|
-- GNU General Public License for more details.
|
--
|
--
|
-- You should have received a copy of the GNU General Public License
|
-- You should have received a copy of the GNU General Public License
|
-- along with this program. If not, see <http://www.gnu.org/licenses/>.
|
-- along with this program. If not, see <http://www.gnu.org/licenses/>.
|
--
|
--
|
--
|
--
|
--===========================================================================--
|
--===========================================================================--
|
-- --
|
-- --
|
-- Revision History --
|
-- Revision History --
|
-- --
|
-- --
|
--===========================================================================--
|
--===========================================================================--
|
--
|
--
|
-- Version Author Date Description
|
-- Version Author Date Description
|
--
|
--
|
-- 0.1 Hans Huebner 23 February 2009 SPI bus master for System09
|
-- 0.1 Hans Huebner 23 February 2009 SPI bus master for System09
|
-- 0.2 John Kent 16 June 2010 Added GPL notice
|
-- 0.2 John Kent 16 June 2010 Added GPL notice
|
--
|
--
|
--
|
--
|
|
|
library ieee;
|
library ieee;
|
use ieee.std_logic_1164.all;
|
use ieee.std_logic_1164.all;
|
use ieee.std_logic_unsigned.all;
|
use ieee.std_logic_unsigned.all;
|
|
|
entity spi_master is
|
entity spi_master is
|
port (
|
port (
|
--
|
--
|
-- CPU Interface Signals
|
-- CPU Interface Signals
|
--
|
--
|
clk : in std_logic;
|
clk : in std_logic;
|
reset : in std_logic;
|
reset : in std_logic;
|
cs : in std_logic;
|
cs : in std_logic;
|
rw : in std_logic;
|
rw : in std_logic;
|
addr : in std_logic_vector(1 downto 0);
|
addr : in std_logic_vector(1 downto 0);
|
data_in : in std_logic_vector(7 downto 0);
|
data_in : in std_logic_vector(7 downto 0);
|
data_out : out std_logic_vector(7 downto 0);
|
data_out : out std_logic_vector(7 downto 0);
|
irq : out std_logic;
|
irq : out std_logic;
|
--
|
--
|
-- SPI Interface Signals
|
-- SPI Interface Signals
|
--
|
--
|
spi_miso : in std_logic;
|
spi_miso : in std_logic;
|
spi_mosi : out std_logic;
|
spi_mosi : out std_logic;
|
spi_clk : out std_logic;
|
spi_clk : out std_logic;
|
spi_cs_n : out std_logic_vector(7 downto 0)
|
spi_cs_n : out std_logic_vector(7 downto 0)
|
);
|
);
|
end;
|
end;
|
|
|
architecture rtl of spi_master is
|
architecture rtl of spi_master is
|
|
|
-- State type of the SPI transfer state machine
|
-- State type of the SPI transfer state machine
|
type state_type is (s_idle, s_running);
|
type state_type is (s_idle, s_running);
|
signal state : state_type;
|
signal state : state_type;
|
-- Shift register
|
-- Shift register
|
signal shift_reg : std_logic_vector(15 downto 0);
|
signal shift_reg : std_logic_vector(15 downto 0);
|
-- Buffer to hold data to be sent
|
-- Buffer to hold data to be sent
|
signal spi_data_buf : std_logic_vector(15 downto 0);
|
signal spi_data_buf : std_logic_vector(15 downto 0);
|
-- Start transmission flag
|
-- Start transmission flag
|
signal start : std_logic;
|
signal start : std_logic;
|
-- Number of bits transfered
|
-- Number of bits transfered
|
signal count : std_logic_vector(3 downto 0);
|
signal count : std_logic_vector(3 downto 0);
|
-- Buffered SPI clock
|
-- Buffered SPI clock
|
signal spi_clk_buf : std_logic;
|
signal spi_clk_buf : std_logic;
|
-- Buffered SPI clock output
|
-- Buffered SPI clock output
|
signal spi_clk_out : std_logic;
|
signal spi_clk_out : std_logic;
|
-- Previous SPI clock state
|
-- Previous SPI clock state
|
signal prev_spi_clk : std_logic;
|
signal prev_spi_clk : std_logic;
|
-- Number of clk cycles-1 in this SPI clock period
|
-- Number of clk cycles-1 in this SPI clock period
|
signal spi_clk_count : std_logic_vector(2 downto 0);
|
signal spi_clk_count : std_logic_vector(2 downto 0);
|
-- SPI clock divisor
|
-- SPI clock divisor
|
signal spi_clk_divide : std_logic_vector(1 downto 0);
|
signal spi_clk_divide : std_logic_vector(1 downto 0);
|
-- SPI transfer length
|
-- SPI transfer length
|
signal transfer_length : std_logic_vector(1 downto 0);
|
signal transfer_length : std_logic_vector(1 downto 0);
|
-- Flag to indicate that the SPI slave should be deselected after the current
|
-- Flag to indicate that the SPI slave should be deselected after the current
|
-- transfer
|
-- transfer
|
signal deselect : std_logic;
|
signal deselect : std_logic;
|
-- Flag to indicate that an IRQ should be generated at the end of a transfer
|
-- Flag to indicate that an IRQ should be generated at the end of a transfer
|
signal irq_enable : std_logic;
|
signal irq_enable : std_logic;
|
-- Internal chip select signal, will be demultiplexed through the cs_mux
|
-- Internal chip select signal, will be demultiplexed through the cs_mux
|
signal spi_cs : std_logic;
|
signal spi_cs : std_logic;
|
-- Current SPI device address
|
-- Current SPI device address
|
signal spi_addr : std_logic_vector(2 downto 0);
|
signal spi_addr : std_logic_vector(2 downto 0);
|
begin
|
begin
|
|
|
-- Read CPU bus into internal registers
|
-- Read CPU bus into internal registers
|
cpu_write : process(clk, reset)
|
cpu_write : process(clk, reset)
|
begin
|
begin
|
if reset = '1' then
|
if reset = '1' then
|
deselect <= '0';
|
deselect <= '0';
|
irq_enable <= '0';
|
irq_enable <= '0';
|
start <= '0';
|
start <= '0';
|
spi_clk_divide <= "11";
|
spi_clk_divide <= "11";
|
transfer_length <= "11";
|
transfer_length <= "11";
|
spi_data_buf <= (others => '0');
|
spi_data_buf <= (others => '0');
|
elsif falling_edge(clk) then
|
elsif falling_edge(clk) then
|
start <= '0';
|
start <= '0';
|
if cs = '1' and rw = '0' then
|
if cs = '1' and rw = '0' then
|
case addr is
|
case addr is
|
when "00" =>
|
when "00" =>
|
spi_data_buf(7 downto 0) <= data_in;
|
spi_data_buf(7 downto 0) <= data_in;
|
when "01" =>
|
when "01" =>
|
spi_data_buf(15 downto 8) <= data_in;
|
spi_data_buf(15 downto 8) <= data_in;
|
when "10" =>
|
when "10" =>
|
start <= data_in(0);
|
start <= data_in(0);
|
deselect <= data_in(1);
|
deselect <= data_in(1);
|
irq_enable <= data_in(2);
|
irq_enable <= data_in(2);
|
spi_addr <= data_in(6 downto 4);
|
spi_addr <= data_in(6 downto 4);
|
when "11" =>
|
when "11" =>
|
spi_clk_divide <= data_in(1 downto 0);
|
spi_clk_divide <= data_in(1 downto 0);
|
transfer_length <= data_in(3 downto 2);
|
transfer_length <= data_in(3 downto 2);
|
when others =>
|
when others =>
|
null;
|
null;
|
end case;
|
end case;
|
end if;
|
end if;
|
end if;
|
end if;
|
end process;
|
end process;
|
|
|
-- Provide data for the CPU to read
|
-- Provide data for the CPU to read
|
cpu_read : process(shift_reg, addr, state, deselect, start)
|
cpu_read : process(shift_reg, addr, state, deselect, start)
|
begin
|
begin
|
data_out <= (others => '0');
|
data_out <= (others => '0');
|
case addr is
|
case addr is
|
when "00" =>
|
when "00" =>
|
data_out <= shift_reg(7 downto 0);
|
data_out <= shift_reg(7 downto 0);
|
when "01" =>
|
when "01" =>
|
data_out <= shift_reg(15 downto 8);
|
data_out <= shift_reg(15 downto 8);
|
when "10" =>
|
when "10" =>
|
if state = s_idle then
|
if state = s_idle then
|
data_out(0) <= '0';
|
data_out(0) <= '0';
|
else
|
else
|
data_out(0) <= '1';
|
data_out(0) <= '1';
|
end if;
|
end if;
|
data_out(1) <= deselect;
|
data_out(1) <= deselect;
|
when others =>
|
when others =>
|
null;
|
null;
|
end case;
|
end case;
|
end process;
|
end process;
|
|
|
spi_cs_n <= "11111110" when spi_addr = "000" and spi_cs = '1' else
|
spi_cs_n <= "11111110" when spi_addr = "000" and spi_cs = '1' else
|
"11111101" when spi_addr = "001" and spi_cs = '1' else
|
"11111101" when spi_addr = "001" and spi_cs = '1' else
|
"11111011" when spi_addr = "010" and spi_cs = '1' else
|
"11111011" when spi_addr = "010" and spi_cs = '1' else
|
"11110111" when spi_addr = "011" and spi_cs = '1' else
|
"11110111" when spi_addr = "011" and spi_cs = '1' else
|
"11101111" when spi_addr = "100" and spi_cs = '1' else
|
"11101111" when spi_addr = "100" and spi_cs = '1' else
|
"11011111" when spi_addr = "101" and spi_cs = '1' else
|
"11011111" when spi_addr = "101" and spi_cs = '1' else
|
"10111111" when spi_addr = "110" and spi_cs = '1' else
|
"10111111" when spi_addr = "110" and spi_cs = '1' else
|
"01111111" when spi_addr = "111" and spi_cs = '1' else
|
"01111111" when spi_addr = "111" and spi_cs = '1' else
|
"11111111";
|
"11111111";
|
|
|
-- SPI transfer state machine
|
-- SPI transfer state machine
|
spi_proc : process(clk, reset)
|
spi_proc : process(clk, reset)
|
begin
|
begin
|
if reset = '1' then
|
if reset = '1' then
|
count <= (others => '0');
|
count <= (others => '0');
|
shift_reg <= (others => '0');
|
shift_reg <= (others => '0');
|
prev_spi_clk <= '0';
|
prev_spi_clk <= '0';
|
spi_clk_out <= '0';
|
spi_clk_out <= '0';
|
spi_cs <= '0';
|
spi_cs <= '0';
|
state <= s_idle;
|
state <= s_idle;
|
irq <= 'Z';
|
irq <= 'Z';
|
elsif falling_edge(clk) then
|
elsif falling_edge(clk) then
|
prev_spi_clk <= spi_clk_buf;
|
prev_spi_clk <= spi_clk_buf;
|
irq <= 'Z';
|
irq <= 'Z';
|
case state is
|
case state is
|
when s_idle =>
|
when s_idle =>
|
if start = '1' then
|
if start = '1' then
|
count <= (others => '0');
|
count <= (others => '0');
|
shift_reg <= spi_data_buf;
|
shift_reg <= spi_data_buf;
|
spi_cs <= '1';
|
spi_cs <= '1';
|
state <= s_running;
|
state <= s_running;
|
elsif deselect = '1' then
|
elsif deselect = '1' then
|
spi_cs <= '0';
|
spi_cs <= '0';
|
end if;
|
end if;
|
when s_running =>
|
when s_running =>
|
if prev_spi_clk = '1' and spi_clk_buf = '0' then
|
if prev_spi_clk = '1' and spi_clk_buf = '0' then
|
spi_clk_out <= '0';
|
spi_clk_out <= '0';
|
count <= count + "0001";
|
count <= count + "0001";
|
shift_reg <= shift_reg(14 downto 0) & spi_miso;
|
shift_reg <= shift_reg(14 downto 0) & spi_miso;
|
if ((count = "0011" and transfer_length = "00")
|
if ((count = "0011" and transfer_length = "00")
|
or (count = "0111" and transfer_length = "01")
|
or (count = "0111" and transfer_length = "01")
|
or (count = "1011" and transfer_length = "10")
|
or (count = "1011" and transfer_length = "10")
|
or (count = "1111" and transfer_length = "11")) then
|
or (count = "1111" and transfer_length = "11")) then
|
if deselect = '1' then
|
if deselect = '1' then
|
spi_cs <= '0';
|
spi_cs <= '0';
|
end if;
|
end if;
|
if irq_enable = '1' then
|
if irq_enable = '1' then
|
irq <= '1';
|
irq <= '1';
|
end if;
|
end if;
|
state <= s_idle;
|
state <= s_idle;
|
end if;
|
end if;
|
elsif prev_spi_clk = '0' and spi_clk_buf = '1' then
|
elsif prev_spi_clk = '0' and spi_clk_buf = '1' then
|
spi_clk_out <= '1';
|
spi_clk_out <= '1';
|
end if;
|
end if;
|
when others =>
|
when others =>
|
null;
|
null;
|
end case;
|
end case;
|
end if;
|
end if;
|
end process;
|
end process;
|
|
|
-- Generate SPI clock
|
-- Generate SPI clock
|
spi_clock_gen : process(clk, reset)
|
spi_clock_gen : process(clk, reset)
|
begin
|
begin
|
if reset = '1' then
|
if reset = '1' then
|
spi_clk_count <= (others => '0');
|
spi_clk_count <= (others => '0');
|
spi_clk_buf <= '0';
|
spi_clk_buf <= '0';
|
elsif falling_edge(clk) then
|
elsif falling_edge(clk) then
|
if state = s_running then
|
if state = s_running then
|
if ((spi_clk_divide = "00")
|
if ((spi_clk_divide = "00")
|
or (spi_clk_divide = "01" and spi_clk_count = "001")
|
or (spi_clk_divide = "01" and spi_clk_count = "001")
|
or (spi_clk_divide = "10" and spi_clk_count = "011")
|
or (spi_clk_divide = "10" and spi_clk_count = "011")
|
or (spi_clk_divide = "11" and spi_clk_count = "111")) then
|
or (spi_clk_divide = "11" and spi_clk_count = "111")) then
|
spi_clk_buf <= not spi_clk_buf;
|
spi_clk_buf <= not spi_clk_buf;
|
spi_clk_count <= (others => '0');
|
spi_clk_count <= (others => '0');
|
else
|
else
|
spi_clk_count <= spi_clk_count + "001";
|
spi_clk_count <= spi_clk_count + "001";
|
end if;
|
end if;
|
else
|
else
|
spi_clk_buf <= '0';
|
spi_clk_buf <= '0';
|
end if;
|
end if;
|
end if;
|
end if;
|
end process;
|
end process;
|
|
|
spi_mosi_mux : process(shift_reg, transfer_length)
|
spi_mosi_mux : process(shift_reg, transfer_length)
|
begin
|
begin
|
case transfer_length is
|
case transfer_length is
|
when "00" =>
|
when "00" =>
|
spi_mosi <= shift_reg(3);
|
spi_mosi <= shift_reg(3);
|
when "01" =>
|
when "01" =>
|
spi_mosi <= shift_reg(7);
|
spi_mosi <= shift_reg(7);
|
when "10" =>
|
when "10" =>
|
spi_mosi <= shift_reg(11);
|
spi_mosi <= shift_reg(11);
|
when "11" =>
|
when "11" =>
|
spi_mosi <= shift_reg(15);
|
spi_mosi <= shift_reg(15);
|
when others =>
|
when others =>
|
null;
|
null;
|
end case;
|
end case;
|
end process;
|
end process;
|
|
|
spi_clk <= spi_clk_out;
|
spi_clk <= spi_clk_out;
|
|
|
end rtl;
|
end rtl;
|
|
|