OpenCores
URL https://opencores.org/ocsvn/artec_dongle_ii_fpga/artec_dongle_ii_fpga/trunk

Subversion Repositories artec_dongle_ii_fpga

[/] [artec_dongle_ii_fpga/] [trunk/] [src/] [spi_eeprom/] [spi_master.vhd] - Blame information for rev 9

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 9 nuubik
------------------------------------------------------------------
2
-- Universal dongle board source code
3
-- 
4
-- Copyright (C) 2008 Artec Design <jyrit@artecdesign.ee>
5
-- 
6
-- This source code is free hardware; you can redistribute it and/or
7
-- modify it under the terms of the GNU Lesser General Public
8
-- License as published by the Free Software Foundation; either
9
-- version 2.1 of the License, or (at your option) any later version.
10
-- 
11
-- This source code is distributed in the hope that it will be useful,
12
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
13
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
-- Lesser General Public License for more details.
15
-- 
16
-- You should have received a copy of the GNU Lesser General Public
17
-- License along with this library; if not, write to the Free Software
18
-- Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19
-- 
20
-- 
21
-- The complete text of the GNU Lesser General Public License can be found in 
22
-- the file 'lesser.txt'.
23
 
24
 
25
library IEEE;
26
use IEEE.std_logic_1164.all;
27
use IEEE.std_logic_unsigned.all;
28
use IEEE.std_logic_arith.all;
29
 
30
entity spi_if is
31
  port (
32
    clk       : in  std_logic;
33
    reset_n   : in  std_logic;
34
        --------------------------
35
        -- EEPROM signals
36
        ee_do      : out std_logic;
37
        ee_di      : in  std_logic;
38
        ee_hold_n  : out std_logic;
39
        ee_cs_n    : out std_logic;
40
        ee_clk     : out std_logic;
41
        ee_wrp_n   : out std_logic;     --write protect signal active low
42
        -- Mem bus
43
    mem_addr  : in std_logic_vector(23 downto 0);
44
    mem_do    : out std_logic_vector(15 downto 0);
45
    mem_di    : in  std_logic_vector(15 downto 0);
46
 
47
    mem_wr    : in  std_logic;  --write not read signal
48
    mem_val   : in  std_logic;
49
    mem_ack   : out std_logic
50
 
51
    );
52
end spi_if;
53
 
54
 
55
architecture RTL of spi_if is
56
  type state_type is (RESETs,SPI_CYCLEs,WAITs);
57
  signal CS : state_type;
58
 
59
  signal spi_cnt    : std_logic_vector(4 downto 0);
60
  signal spi_shiftr : std_logic_vector(0 to 23);
61
  signal spi_wren_done : std_logic;
62
 
63
 
64
  constant SPI_READ    : std_logic_vector(0 to 2):="011";
65
  constant SPI_WRITE   : std_logic_vector(0 to 2):="010";
66
  constant SPI_SET_WEN : std_logic_vector(0 to 2):="110";
67
  constant SPI_CLR_WEN : std_logic_vector(0 to 2):="100";
68
 
69
 
70
begin
71
 
72
 
73
ee_do <= spi_shiftr(0);
74
 
75
SPI_SM: process (clk, reset_n)
76
begin  -- process READ
77
        if reset_n='0' then
78
                ee_cs_n <='1';
79
                CS <= RESETs;
80
                ee_clk <='0';
81
                ee_wrp_n <='1';  --active low write protect
82
                ee_hold_n <='1';
83
                spi_wren_done <='0';
84
                spi_cnt <=(others=>'0');
85
    elsif clk'event and clk = '1' then    -- rising clock edge
86
 
87
                case CS is
88
                        when RESETs =>
89
                                 mem_ack <='0';
90
                                 ee_cs_n <= (not mem_val);                 --chipselect 4 spi
91
                                 fl_we_n <= (not (mem_val and mem_wr));  --write enable 4 flash
92
                                 if spi_wren_done ='0' then
93
                                        spi_cnt <= "01111";  --only 8 bit command needs to be sent
94
                                        spi_shiftr(0 to 3) <="0000";
95
                                        spi_shiftr(4) <= '0';
96
                                        spi_shiftr(5 to 7) <= SPI_SET_WEN;
97
                                        CS <= SPI_CYCLEs;
98
                                 elsif mem_val='1' and mem_wr = '0' then --READ
99
                                        spi_cnt <=(others=>'0');
100
                                        spi_shiftr(0 to 3) <="0000";
101
                                        spi_shiftr(4) <= mem_addr(8);
102
                                        spi_shiftr(5 to 7) <= SPI_READ;
103
                                        spi_shiftr(8 to 15) <= mem_addr(7 downto 0);
104
                                        CS <= SPI_CYCLEs;
105
                                 elsif mem_val='1' and mem_wr = '1' then --WRITE
106
                                        spi_cnt <=(others=>'0');
107
                                        spi_shiftr(0 to 3) <="0000";
108
                                        spi_shiftr(4) <= mem_addr(8);
109
                                        spi_shiftr(5 to 7) <= SPI_WRITE;
110
                                        spi_shiftr(8 to 15) <= mem_addr(7 downto 0);
111
                                        spi_shiftr(16 to 23) <= mem_di(7 downto 0);
112
                                        CS <= SPI_CYCLEs;
113
                                 end if;   --elsif mem_cmd
114
                        when SPI_CYCLEs =>
115
                                if spi_cnt < 24 then
116
                                        ee_clk <= not ee_clk;
117
                                elsif
118
                                        mem_do <= x"00"&spi_shiftr(16 to 23); --this may be done always as this is don't care to all but read
119
                                        ee_clk <= '0';
120
                                        if spi_wren_done ='0' then
121
                                                spi_wren_done <='1';
122
                                                CS <= RESETs;
123
                                        else
124
                                                mem_ack <='1';
125
                                                CS <= WAITs;
126
                                        end if;
127
                                end if;
128
 
129
                                if ee_clk='1' then
130
                                        spi_shiftr <= spi_shiftr(1 to 23)&ee_di;
131
                                        spi_cnt <= spi_cnt + 1;
132
                                end if;
133
                        when WAITs =>
134
                                if      mem_val='0' then -- wait untill val is removed
135
                                        mem_ack <='0';
136
                                        CS <= RESETs;
137
                                end if;
138
 
139
                end case;
140
 
141
  end if;                               --system
142
end process SPI_SM;
143
 
144
 
145
 
146
 
147 2 nuubik
end RTL;

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.