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

Subversion Repositories spiadc

[/] [spiadc/] [trunk/] [adcrecv.vhd] - Blame information for rev 2

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 AlexRayne
-- synthesis library lib
2
 
3
--------------------------------------------------------------------
4
-- Project : SPI receivers master 
5
-- Author : AlexRayne
6
-- Date : 2009.03.16.03
7
-- File : 
8
-- Design  : 
9
--------------------------------------------------------------------
10
-- Description : (win1251) SPI ìàñòåð-ïðèåìíèê ñ ìèíèìàëüíûìè çàòðàòàìè ðåñóðñîâ.
11
--      è âîçìîæíîñòüþ âûäà÷è shut-down ïîñûëêè. Ïðåíàçíà÷åíî äëÿ çàãðóçêè ÀÖÏ AD747x.
12
--      Ìîæåò çàãðóæàòü íàñòðàèâàåìóþ ÷àñòü SPI ïîñëåäîâàòåëüíîñòè (êóñîê).
13
--              ôîðìèðóåò ïîñëåäîâàòåëüíîñòü âõîæäåíèÿ è âûõîäà èç ïîñûëêè ñèãíàëîâ nSS è SCK:
14
--                  ïîñëå ïîñûëêà îáîçíà÷àåòñÿ àêòèâíûì nSS('0'), íà ñòàðòå ïîñûëêè SCK='1' ïîëòàêòà
15
--          çàãðóæàþòñÿ áèòû ïî ïåðåäíåìó ôðîíòó SCK, ïîñëåäíèé áèò ïîñûëêè íåèìååò çàäíåãî
16
--           ôðîíòà, SCK = '1' âñå ïàññèâíîå âðåìÿ.
17
 
18
-- Description : SPI master-receiver minimalistic costs
19
--      intended for loading ADC AD747x, capable produce shut-down frames.
20
--      can load tunable part of frame. generate entry/exit sequences on nSS, SCK:
21
--          activate nSS='0' on frame transfer, SCK='1' for half clock cycle at frame start,
22
--          data loads on rising front SCK, last frame bit have no falling edge SCK,
23
--          SCK='1' durung inactive period.   
24
--------------------------------------------------------------------
25
-- $Log$
26
--------------------------------------------------------------------
27
 
28
LIBRARY ieee;
29
USE ieee.std_logic_1164.all;
30
 
31
--  Entity Declaration
32
ENTITY AdcRecv IS
33
        -- {{ALTERA_IO_BEGIN}} DO NOT REMOVE THIS LINE!
34
    GENERIC(
35
        SPILen      : positive  := 16;
36
        DataLen     : positive  := 16;
37
        DataOffset  : natural   := 0;
38
        -- ShutDownLen sets len of short spi sequence for poweroff purposes
39
        SDLen       : natural   := 1;
40
        SDMax       : natural   := 10;
41
        -- requred TimeOut before start 
42
        QuietLen    : natural   := 1
43
    );
44
        PORT
45
        (
46
        CLK     : IN STD_LOGIC;
47
        Start   : IN STD_LOGIC;
48
 
49
        -- if false then spi produce controling sequense of xfer entry and inter-frame pause
50
        -- else spi start new frame xfer immeidate after completing current frame
51
        ContinueStart : in STD_LOGIC := '0';
52
        ShutDown: IN STD_LOGIC;
53
        reset   : IN STD_LOGIC;
54
 
55
        SDI     : IN STD_LOGIC;
56
        SCK     : OUT STD_LOGIC;
57
        nSS     : OUT STD_LOGIC;
58
 
59
        DQ      : OUT std_logic_vector(DataLen-1 downto 0);--STD_LOGIC_2D(Chanels-1 downto 0, DataLen-1 downto 0);
60
        -- rising edge of ready can be used for loading DQ data to dest.
61
        Ready   : OUT STD_LOGIC;
62
        -- used to expand load logic to parallel loading registers, to make a multi chanel reciever
63
        Shift   : OUT STD_LOGIC
64
        );
65
        -- {{ALTERA_IO_END}} DO NOT REMOVE THIS LINE!
66
 
67
END AdcRecv;
68
 
69
 
70
--  Architecture Body
71
 
72
ARCHITECTURE BEH OF AdcRecv IS
73
    signal SS           : std_logic;
74
    signal Data         : std_logic_vector(DataLen-1 downto 0);
75
    signal iSCK                 : std_logic;
76
    signal iReady               : std_logic;
77
 
78
    subtype BitIndex is natural range 0 to SPILen-1;
79
    signal BitNo        : BitIndex;
80
 
81
    subtype QuietIndex is natural range 0 to QuietLen;
82
    signal  QuietCnt    : QuietIndex;
83
    signal  QuietOk     : std_logic;
84
 
85
    signal isLastBit    : std_logic;
86
    signal isLastDataBit: std_logic;
87
    signal isFirstBit   : std_logic;
88
    signal Transfer     : std_logic := '0';
89
    signal PrepTransfer : std_logic := '0';
90
    signal ReceiveWindow : std_logic := '0';
91
 
92
    type States is ( stSerLoading, stQuietCheck); --stReady,
93
    signal FSMState : States;
94
    signal NextState : States;
95
 
96
    signal SDEnough     : std_logic;
97
    signal SDDone       : std_logic;
98
    signal NeedSD       : std_logic;
99
    signal Enable       : std_logic;
100
 
101
begin
102
    BitCounter : process(CLK, reset, Enable, Transfer, isLastBit, FSMState) is begin
103
        if (reset = '1') or (FSMState = stQuietCheck) then -- (Enable = '0') then 
104
            BitNo <= 0;
105
        else
106
            if falling_edge(CLK) then
107
                                if isLastBit = '1' then
108
                                        BitNo <= 0;
109
                                else
110
                                        if Transfer = '1' then
111
                        BitNo <= BitNo+1;
112
                                        end if;
113
                end if;
114
            end if;
115
        end if;
116
    end process;
117
 
118
        isFirstBit <= '1' when (BitNo = 0) else '0';
119
    isLastBit <= '1' when (BitNo = SPILen-1) else '0';
120
    isLastDataBit <= '1'when (BitNo = DataOffset + DataLen-1) else '0';
121
 
122
    ReceiveWindow <= '1' when (BitNo >= DataOffset) and (BitNo <= DataOffset + DataLen-1)
123
                else '0';
124
 
125
    SDEnough <= '1' when (BitNo >= SDLen) and (BitNo < SDMax) else '0';
126
 
127
    SDmonitor : process(SS, enable, iSCK, NeedSD, SDEnough, Reset) is begin
128
       if (reset = '1') or (iSCK = '0') then
129
            SDDone      <= '0';
130
       elsif falling_edge(enable) then
131
            SDDone      <= SDEnough;
132
        end if;
133
    end process;
134
 
135
    Qsafer: if QuietLen > 1 generate
136
                QuietOk <= '1' when (QuietCnt >= QuietLen) else '0';
137
 
138
        QuietCounter: process(FSMState, reset, CLK, QuietOk) is begin
139
                        if (reset = '1')
140
                                or (FSMState = stSerLoading)
141
                        then
142
                                QuietCnt <= 0;
143
                        else
144
                                if rising_edge(CLK) then
145
                                        if QuietOk = '0' then
146
                                                QuietCnt <= QuietCnt+1;
147
                                        end if;
148
                                end if;
149
                        end if;
150
                end process;
151
        end generate;
152
 
153
    EmptyQsafer: if QuietLen <= 1 generate
154
                QuietOk <= '1';
155
        end generate;
156
 
157
        EnableReg: process(Start, NeedSD, iReady, NextState, FSMState, CLK, Reset) is begin
158
                if (reset = '1') then
159
                        Enable <= '0';
160
                elsif rising_edge(CLK) then
161
                        if (iReady and (Start or NeedSD)) = '1' then
162
                                Enable <= '1';
163
                        else
164
                                if (FSMState = stSerLoading) and (NextState /= stSerLoading) then
165
                                        Enable <= '0';
166
                                end if;
167
                        end if;
168
                end if;
169
        end process;
170
 
171
        SDRequest: process(ShutDown, SDDone, Reset) is begin
172
                if (Reset = '1') or (SDDone = '1') then
173
                        NeedSD <= '0';
174
                elsif (ShutDown = '1') and (SDDone = '0') then
175
                        NeedSD <= '1';
176
                end if;
177
        end process;
178
 
179
--      NeedSD <= ShutDown and not SDDone;
180
--                      NeedSD <= '1' when ShutDown and not SDDone else
181
--                                              '0' when ;
182
 
183
 
184
        FSMStepper : process(NextState, CLK, Reset) is begin
185
                if reset = '1' then
186
                        FSMState <= stQuietCheck;
187
                elsif falling_edge(CLK) then
188
                        FSMState <= NextState;
189
                end if;
190
        end process;
191
 
192
    FSM : process(FSMState, CLK, QuietOk, isLastBit, ContinueStart
193
                 , ShutDown, SDEnough, Start, NeedSD, Reset, Enable)
194
    is begin
195
            case FSMState is
196
                when stSerLoading =>
197
                        if (ShutDown = '1') and (SDEnough = '1') then
198
                            NextState <= stQuietCheck;
199
                        elsif (isLastBit = '1') then
200
                            if ContinueStart = '0' then
201
                                NextState <= stQuietCheck;
202
                            else
203
                                NextState <= stSerLoading;--stReady;
204
                            end if;
205
                        else
206
                            NextState <= stSerLoading;
207
                        end if;
208
                when stQuietCheck =>
209
                    if (QuietOk = '1') then
210
                                                if ((Enable = '1') or (NeedSD = '1')) then
211
                                                        NextState <= stSerLoading;
212
                                                else
213
                                                        NextState <= stQuietCheck;--stReady;
214
                                                end if;
215
                    else
216
                        NextState <= stQuietCheck;
217
                    end if;
218
                when others =>
219
                    NextState <= stQuietCheck;
220
            end case;
221
    end process;
222
 
223
        Transfer <= '1' when (FSMState = stSerLoading) else '0';
224
        -- SS must contain gap with '1' about 1/2cycle on SCK at start and end of frames
225
    SS <= Transfer or Enable;
226
    iSCK   <= CLK or not Enable; --when (FSMState = stSerLoading) else '1';
227
 
228
    nSS <= not SS;
229
    SCK <= iSCK;
230
 
231
    DataCell : process (Data, CLK, reset, ReceiveWindow, SS) is begin
232
            if reset = '1' then
233
                Data <= (others => '0');
234
            elsif rising_edge(CLK) then
235
                if (ReceiveWindow = '1') and (SS = '1') then
236
                    Data(Data'high downto 1)  <= Data(Data'high-1 downto 0);
237
                    Data(0)                   <= SDI;
238
                end if;
239
            end if;
240
    end process;
241
 
242
    DQ <= Data;
243
    Shift <= CLK and ReceiveWindow;
244
 
245
        readyMoitor: process(CLK, FSMState, isFirstbit, isLastDataBit, Reset) is begin
246
                if (reset = '1') or (FSMState = stQuietCheck) then
247
                        iready <= '1';
248
                elsif (FSMState = stSerLoading) and (isFirstbit = '1') and (CLK = '1') then
249
                        iready <= '0';
250
                elsif falling_edge(CLK) then
251
                        if isLastDataBit = '1' then
252
                                iready <= '1';
253
                        end if;
254
                end if;
255
    end process;
256
 
257
        Ready <= iReady;
258
end architecture BEH;

powered by: WebSVN 2.1.0

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