| 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 |
3 |
AlexRayne |
-- ôîðìèðóåò ïîñëåäîâàòåëüíîñòü âõîæäåíèÿ è âûõîäà èç ïîñûëêè ñèãíàëîâ nSS è SCK:
|
| 14 |
|
|
-- ïîñûëêà îáîçíà÷àåòñÿ àêòèâíûì nSS('0'), íà ñòàðòå ïîñûëêè SCK='1' ïîëòàêòà
|
| 15 |
2 |
AlexRayne |
-- çàãðóæàþòñÿ áèòû ïî ïåðåäíåìó ôðîíòó 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 |
3 |
AlexRayne |
|
| 25 |
|
|
-- SDLen, SDMax:
|
| 26 |
|
|
-- sets len of short spi sequence for poweroff purposes short (SDLen) and maximum (SDMax) length
|
| 27 |
|
|
-- QuietLen:
|
| 28 |
|
|
-- requred TimeOut before start
|
| 29 |
|
|
-- Start:
|
| 30 |
|
|
--Start lock on rising CLK, and changes ignores during transmition. if one still high after transmition
|
| 31 |
|
|
-- ends, then new frame starts after QuietLen timeout if ContinueStart not active
|
| 32 |
|
|
-- ContinueStart:
|
| 33 |
|
|
-- if false then spi produce controling sequense of xfer entry and inter-frame pause
|
| 34 |
|
|
-- else spi start new frame xfer immeidate after completing current frame
|
| 35 |
|
|
-- ShutDown
|
| 36 |
|
|
-- locks by high level, after Shuting down complete new SutDown sequence can be forced by Start
|
| 37 |
|
|
-- if one activate during transmition, then it forces current frame to close if it can (beetween SDLen..SDMax bits)
|
| 38 |
|
|
-- or generate short shutdown frame after completing current frae else
|
| 39 |
|
|
-- Ready:
|
| 40 |
|
|
-- rising edge of ready can be used for loading DQ data to dest.
|
| 41 |
|
|
-- Shift:
|
| 42 |
|
|
-- shift clock for internal data register intended to expand load logic to parallel loading registers,
|
| 43 |
|
|
-- to make a multi chanel reciever
|
| 44 |
|
|
-- Sleeping
|
| 45 |
|
|
-- State of ADC power mode - is it shutdowned.
|
| 46 |
2 |
AlexRayne |
--------------------------------------------------------------------
|
| 47 |
|
|
-- $Log$
|
| 48 |
|
|
--------------------------------------------------------------------
|
| 49 |
|
|
|
| 50 |
|
|
LIBRARY ieee;
|
| 51 |
|
|
USE ieee.std_logic_1164.all;
|
| 52 |
|
|
|
| 53 |
|
|
-- Entity Declaration
|
| 54 |
|
|
ENTITY AdcRecv IS
|
| 55 |
|
|
-- {{ALTERA_IO_BEGIN}} DO NOT REMOVE THIS LINE!
|
| 56 |
|
|
GENERIC(
|
| 57 |
|
|
SPILen : positive := 16;
|
| 58 |
|
|
DataLen : positive := 16;
|
| 59 |
|
|
DataOffset : natural := 0;
|
| 60 |
|
|
SDLen : natural := 1;
|
| 61 |
|
|
SDMax : natural := 10;
|
| 62 |
|
|
QuietLen : natural := 1
|
| 63 |
|
|
);
|
| 64 |
|
|
PORT
|
| 65 |
|
|
(
|
| 66 |
|
|
CLK : IN STD_LOGIC;
|
| 67 |
|
|
Start : IN STD_LOGIC;
|
| 68 |
|
|
ContinueStart : in STD_LOGIC := '0';
|
| 69 |
|
|
ShutDown: IN STD_LOGIC;
|
| 70 |
|
|
reset : IN STD_LOGIC;
|
| 71 |
|
|
|
| 72 |
|
|
SDI : IN STD_LOGIC;
|
| 73 |
|
|
SCK : OUT STD_LOGIC;
|
| 74 |
|
|
nSS : OUT STD_LOGIC;
|
| 75 |
|
|
|
| 76 |
|
|
DQ : OUT std_logic_vector(DataLen-1 downto 0);--STD_LOGIC_2D(Chanels-1 downto 0, DataLen-1 downto 0);
|
| 77 |
|
|
Ready : OUT STD_LOGIC;
|
| 78 |
3 |
AlexRayne |
Shift : OUT STD_LOGIC;
|
| 79 |
|
|
Sleeping : OUT STD_LOGIC
|
| 80 |
2 |
AlexRayne |
);
|
| 81 |
|
|
-- {{ALTERA_IO_END}} DO NOT REMOVE THIS LINE!
|
| 82 |
|
|
|
| 83 |
|
|
END AdcRecv;
|
| 84 |
|
|
|
| 85 |
|
|
|
| 86 |
|
|
-- Architecture Body
|
| 87 |
|
|
|
| 88 |
|
|
ARCHITECTURE BEH OF AdcRecv IS
|
| 89 |
|
|
signal SS : std_logic;
|
| 90 |
|
|
signal Data : std_logic_vector(DataLen-1 downto 0);
|
| 91 |
|
|
signal iSCK : std_logic;
|
| 92 |
|
|
signal iReady : std_logic;
|
| 93 |
|
|
|
| 94 |
|
|
subtype BitIndex is natural range 0 to SPILen-1;
|
| 95 |
|
|
signal BitNo : BitIndex;
|
| 96 |
|
|
|
| 97 |
|
|
subtype QuietIndex is natural range 0 to QuietLen;
|
| 98 |
|
|
signal QuietCnt : QuietIndex;
|
| 99 |
|
|
signal QuietOk : std_logic;
|
| 100 |
|
|
|
| 101 |
|
|
signal isLastBit : std_logic;
|
| 102 |
|
|
signal isLastDataBit: std_logic;
|
| 103 |
|
|
signal isFirstBit : std_logic;
|
| 104 |
|
|
signal Transfer : std_logic := '0';
|
| 105 |
|
|
signal PrepTransfer : std_logic := '0';
|
| 106 |
|
|
signal ReceiveWindow : std_logic := '0';
|
| 107 |
|
|
|
| 108 |
|
|
type States is ( stSerLoading, stQuietCheck); --stReady,
|
| 109 |
|
|
signal FSMState : States;
|
| 110 |
|
|
signal NextState : States;
|
| 111 |
|
|
|
| 112 |
|
|
signal SDEnough : std_logic;
|
| 113 |
|
|
signal SDDone : std_logic;
|
| 114 |
|
|
signal NeedSD : std_logic;
|
| 115 |
|
|
signal Enable : std_logic;
|
| 116 |
|
|
|
| 117 |
|
|
begin
|
| 118 |
|
|
BitCounter : process(CLK, reset, Enable, Transfer, isLastBit, FSMState) is begin
|
| 119 |
|
|
if (reset = '1') or (FSMState = stQuietCheck) then -- (Enable = '0') then
|
| 120 |
|
|
BitNo <= 0;
|
| 121 |
|
|
else
|
| 122 |
|
|
if falling_edge(CLK) then
|
| 123 |
|
|
if isLastBit = '1' then
|
| 124 |
|
|
BitNo <= 0;
|
| 125 |
|
|
else
|
| 126 |
|
|
if Transfer = '1' then
|
| 127 |
|
|
BitNo <= BitNo+1;
|
| 128 |
|
|
end if;
|
| 129 |
|
|
end if;
|
| 130 |
|
|
end if;
|
| 131 |
|
|
end if;
|
| 132 |
|
|
end process;
|
| 133 |
|
|
|
| 134 |
|
|
isFirstBit <= '1' when (BitNo = 0) else '0';
|
| 135 |
|
|
isLastBit <= '1' when (BitNo = SPILen-1) else '0';
|
| 136 |
|
|
isLastDataBit <= '1'when (BitNo = DataOffset + DataLen-1) else '0';
|
| 137 |
|
|
|
| 138 |
|
|
ReceiveWindow <= '1' when (BitNo >= DataOffset) and (BitNo <= DataOffset + DataLen-1)
|
| 139 |
|
|
else '0';
|
| 140 |
|
|
|
| 141 |
|
|
SDEnough <= '1' when (BitNo >= SDLen) and (BitNo < SDMax) else '0';
|
| 142 |
|
|
|
| 143 |
|
|
SDmonitor : process(SS, enable, iSCK, NeedSD, SDEnough, Reset) is begin
|
| 144 |
|
|
if (reset = '1') or (iSCK = '0') then
|
| 145 |
|
|
SDDone <= '0';
|
| 146 |
|
|
elsif falling_edge(enable) then
|
| 147 |
|
|
SDDone <= SDEnough;
|
| 148 |
|
|
end if;
|
| 149 |
|
|
end process;
|
| 150 |
3 |
AlexRayne |
|
| 151 |
|
|
Sleeping <= SDDone;
|
| 152 |
2 |
AlexRayne |
|
| 153 |
|
|
Qsafer: if QuietLen > 1 generate
|
| 154 |
|
|
QuietOk <= '1' when (QuietCnt >= QuietLen) else '0';
|
| 155 |
|
|
|
| 156 |
|
|
QuietCounter: process(FSMState, reset, CLK, QuietOk) is begin
|
| 157 |
|
|
if (reset = '1')
|
| 158 |
|
|
or (FSMState = stSerLoading)
|
| 159 |
|
|
then
|
| 160 |
|
|
QuietCnt <= 0;
|
| 161 |
|
|
else
|
| 162 |
|
|
if rising_edge(CLK) then
|
| 163 |
|
|
if QuietOk = '0' then
|
| 164 |
|
|
QuietCnt <= QuietCnt+1;
|
| 165 |
|
|
end if;
|
| 166 |
|
|
end if;
|
| 167 |
|
|
end if;
|
| 168 |
|
|
end process;
|
| 169 |
|
|
end generate;
|
| 170 |
|
|
|
| 171 |
|
|
EmptyQsafer: if QuietLen <= 1 generate
|
| 172 |
|
|
QuietOk <= '1';
|
| 173 |
|
|
end generate;
|
| 174 |
|
|
|
| 175 |
|
|
EnableReg: process(Start, NeedSD, iReady, NextState, FSMState, CLK, Reset) is begin
|
| 176 |
|
|
if (reset = '1') then
|
| 177 |
|
|
Enable <= '0';
|
| 178 |
|
|
elsif rising_edge(CLK) then
|
| 179 |
|
|
if (iReady and (Start or NeedSD)) = '1' then
|
| 180 |
|
|
Enable <= '1';
|
| 181 |
|
|
else
|
| 182 |
|
|
if (FSMState = stSerLoading) and (NextState /= stSerLoading) then
|
| 183 |
|
|
Enable <= '0';
|
| 184 |
|
|
end if;
|
| 185 |
|
|
end if;
|
| 186 |
|
|
end if;
|
| 187 |
|
|
end process;
|
| 188 |
|
|
|
| 189 |
|
|
SDRequest: process(ShutDown, SDDone, Reset) is begin
|
| 190 |
|
|
if (Reset = '1') or (SDDone = '1') then
|
| 191 |
|
|
NeedSD <= '0';
|
| 192 |
|
|
elsif (ShutDown = '1') and (SDDone = '0') then
|
| 193 |
|
|
NeedSD <= '1';
|
| 194 |
|
|
end if;
|
| 195 |
|
|
end process;
|
| 196 |
|
|
|
| 197 |
|
|
-- NeedSD <= ShutDown and not SDDone;
|
| 198 |
|
|
-- NeedSD <= '1' when ShutDown and not SDDone else
|
| 199 |
|
|
-- '0' when ;
|
| 200 |
|
|
|
| 201 |
|
|
|
| 202 |
|
|
FSMStepper : process(NextState, CLK, Reset) is begin
|
| 203 |
|
|
if reset = '1' then
|
| 204 |
|
|
FSMState <= stQuietCheck;
|
| 205 |
|
|
elsif falling_edge(CLK) then
|
| 206 |
|
|
FSMState <= NextState;
|
| 207 |
|
|
end if;
|
| 208 |
|
|
end process;
|
| 209 |
|
|
|
| 210 |
|
|
FSM : process(FSMState, CLK, QuietOk, isLastBit, ContinueStart
|
| 211 |
|
|
, ShutDown, SDEnough, Start, NeedSD, Reset, Enable)
|
| 212 |
|
|
is begin
|
| 213 |
|
|
case FSMState is
|
| 214 |
|
|
when stSerLoading =>
|
| 215 |
|
|
if (ShutDown = '1') and (SDEnough = '1') then
|
| 216 |
|
|
NextState <= stQuietCheck;
|
| 217 |
|
|
elsif (isLastBit = '1') then
|
| 218 |
|
|
if ContinueStart = '0' then
|
| 219 |
|
|
NextState <= stQuietCheck;
|
| 220 |
|
|
else
|
| 221 |
|
|
NextState <= stSerLoading;--stReady;
|
| 222 |
|
|
end if;
|
| 223 |
|
|
else
|
| 224 |
|
|
NextState <= stSerLoading;
|
| 225 |
|
|
end if;
|
| 226 |
|
|
when stQuietCheck =>
|
| 227 |
|
|
if (QuietOk = '1') then
|
| 228 |
|
|
if ((Enable = '1') or (NeedSD = '1')) then
|
| 229 |
|
|
NextState <= stSerLoading;
|
| 230 |
|
|
else
|
| 231 |
|
|
NextState <= stQuietCheck;--stReady;
|
| 232 |
|
|
end if;
|
| 233 |
|
|
else
|
| 234 |
|
|
NextState <= stQuietCheck;
|
| 235 |
|
|
end if;
|
| 236 |
|
|
when others =>
|
| 237 |
|
|
NextState <= stQuietCheck;
|
| 238 |
|
|
end case;
|
| 239 |
|
|
end process;
|
| 240 |
|
|
|
| 241 |
|
|
Transfer <= '1' when (FSMState = stSerLoading) else '0';
|
| 242 |
|
|
-- SS must contain gap with '1' about 1/2cycle on SCK at start and end of frames
|
| 243 |
|
|
SS <= Transfer or Enable;
|
| 244 |
|
|
iSCK <= CLK or not Enable; --when (FSMState = stSerLoading) else '1';
|
| 245 |
|
|
|
| 246 |
|
|
nSS <= not SS;
|
| 247 |
|
|
SCK <= iSCK;
|
| 248 |
|
|
|
| 249 |
|
|
DataCell : process (Data, CLK, reset, ReceiveWindow, SS) is begin
|
| 250 |
|
|
if reset = '1' then
|
| 251 |
|
|
Data <= (others => '0');
|
| 252 |
|
|
elsif rising_edge(CLK) then
|
| 253 |
|
|
if (ReceiveWindow = '1') and (SS = '1') then
|
| 254 |
|
|
Data(Data'high downto 1) <= Data(Data'high-1 downto 0);
|
| 255 |
|
|
Data(0) <= SDI;
|
| 256 |
|
|
end if;
|
| 257 |
|
|
end if;
|
| 258 |
|
|
end process;
|
| 259 |
|
|
|
| 260 |
|
|
DQ <= Data;
|
| 261 |
|
|
Shift <= CLK and ReceiveWindow;
|
| 262 |
|
|
|
| 263 |
|
|
readyMoitor: process(CLK, FSMState, isFirstbit, isLastDataBit, Reset) is begin
|
| 264 |
|
|
if (reset = '1') or (FSMState = stQuietCheck) then
|
| 265 |
|
|
iready <= '1';
|
| 266 |
|
|
elsif (FSMState = stSerLoading) and (isFirstbit = '1') and (CLK = '1') then
|
| 267 |
|
|
iready <= '0';
|
| 268 |
|
|
elsif falling_edge(CLK) then
|
| 269 |
|
|
if isLastDataBit = '1' then
|
| 270 |
|
|
iready <= '1';
|
| 271 |
|
|
end if;
|
| 272 |
|
|
end if;
|
| 273 |
|
|
end process;
|
| 274 |
|
|
|
| 275 |
|
|
Ready <= iReady;
|
| 276 |
|
|
end architecture BEH;
|