FP_Setup_Type.vhd


1
library IEEE;
2
use IEEE.STD_LOGIC_1164.ALL;
3
use ieee.numeric_std.ALL;
4
5
entity FP_Setup is
6
  generic
7
  (  ClockDiv    : integer := 7              -- Frequenzen: FP_DClk = Clock / ClockDiv
8
  );
9
  port
10
  (  Clock      : in std_logic;
11
    FP_DIn      : in std_logic;              -- SPI Data in
12
    FP_DClk      : out std_logic;            -- SPI Clock
13
    FP_DStb      : out std_logic;            -- SPI Data strobe
14
    FP_DOut      : out std_logic;            -- SPI Data out
15
    CY7_DIn      : in std_logic;              -- SPI Data in
16
    CY7_DClk    : in std_logic;              -- SPI Clock
17
    CY7_DStb    : in std_logic;              -- SPI Data strobe
18
    CY7_DOut    : out std_logic;            -- SPI Data out
19
    SampleRate    : out std_logic_vector (4 downto 0);  -- -> I2S_Master.vhd
20
    NoOfChannels  : out std_logic_vector (4 downto 0);  -- 0..31: 1..32 Channels used
21
    SelChannel    : out std_logic_vector (4 downto 0);  -- Listen to Channel 1..32
22
    SelMono      : out std_logic;            -- Listen to Channel Mono
23
    SetupChanged  : out std_logic              -- SampleRate or NoOfChannels changed
24
  );
25
end FP_Setup ;
26
27
architecture Default of FP_Setup is
28
29
  type   SLV_CY7_Bytes is array(integer range <>) of std_logic_vector(7 downto 0);
30
  signal Init        : boolean;
31
  signal ClockCount    : integer range 0 to ClockDiv;
32
  signal SPI_Status    : integer range 0 to 24;        -- 24 Data Bits + Strobe
33
  signal MuxCount      : std_logic_vector(2 downto 0);      -- Multiplex 8 7-Segment Digits
34
  signal StateTa1      : integer range 0 to 2;          -- 0: Ruhe, 1: Ta gedrückt, 2: Sw gedreht
35
  signal InputShift    : std_logic_vector(5 downto 0);
36
  signal InputReg      : std_logic_vector(5 downto 0);      -- Bit 5..0: Sw1, Sw2, Ta1, Ta2
37
  signal InputReg2    : std_logic_vector(5 downto 0);      -- Bit 5..0: Sw1, Sw2, Ta1, Ta2
38
  signal CY7_ClkDly    : std_logic_vector(1 to 2);              -- 
39
  signal CY7_DInDly    : std_logic_vector(1 to 2);              -- 
40
  signal CY7_StbDly    : std_logic_vector(1 to 2);              -- 
41
  signal CY7_InShift    : std_logic_vector(31 downto 0);    --
42
  signal CY7_InReg    : std_logic_vector(31 downto 0);    --
43
  signal CY7_OutShift    : std_logic_vector(31 downto 0);    --
44
  -- Einstellungen:
45
  signal SRate      : std_logic_vector(4 downto 0);      -- -> I2S_Master.vhd
46
  signal NChannels    : std_logic_vector(4 downto 0);      -- 0..31: Max. 32 Channels used
47
  signal SChannel      : std_logic_vector(4 downto 0);      -- 0..31: Listen to Channel 1..32
48
  signal SMono      : std_logic;              -- Listen to Channel Mono
49
  -- Für die Anzeige:
50
  signal NumVal32      : std_logic_vector(7 downto 0);      -- NChannels + 1, SChannel + 1, binär
51
  signal BCD_Val32    : std_logic_vector(7 downto 0);      -- NChannels + 1, SChannel + 1, BCD
52
  signal BCD_ValSR    : std_logic_vector(15 downto 0);    -- Sample Rate BCD
53
  signal DP_SR      : integer range 2 to 5;          -- Position des Dezimalpunkts SR
54
  signal BCD7SegIn    : std_logic_vector(3 downto 0);      -- Eingabe 7-Segment Encoder
55
  signal BCD7SegOut    : std_logic_vector(7 downto 0);      -- Ausgabe 7-Segment Encoder
56
57
begin
58
59
  CY7_Communication : process (Clock) begin
60
    -- Datenblatt: At both low and high speed in Mode 0, data on RXD0
61
    -- is sampled two CLKOUT cycles before the rising clock edge on TXD0.
62
    if Clock = '1' and Clock'event then
63
      CY7_ClkDly <= CY7_DClk & CY7_ClkDly(1);
64
      CY7_DInDly <= CY7_DIn & CY7_DInDly(1);
65
      CY7_StbDly <= CY7_DStb & CY7_StbDly(1);
66
      if CY7_ClkDly = "10" then
67
        CY7_InShift <= CY7_DInDly(2) & CY7_InShift(CY7_InShift'high downto 1);
68
      end if;
69
      if CY7_StbDly = "10"  then
70
        CY7_InReg <= CY7_InShift;
71
        CY7_OutShift <= CY7_InShift;
72
      end if;
73
      if CY7_ClkDly = "10" then
74
        CY7_OutShift <= '0' & CY7_OutShift(CY7_OutShift'high downto 1);
75
      end if;
76
      CY7_DOut <= CY7_OutShift(0);
77
    end if;
78
  end process CY7_Communication;
79
80
  SPI_and_Settings : process (Clock) begin
81
    if Clock = '1' and Clock'event then
82
      if not Init then
83
        NChannels <= (others => '1');
84
        Init <= true;
85
      end if;
86
      ClockCount <= ClockCount + 1;
87
      if ClockCount = ClockDiv / 2 then
88
        FP_DStb <= '0';
89
        FP_DClk <= '1';
90
      end if;
91
      if ClockCount = 0 then
92
        SetupChanged <= '0';
93
        if InputReg(5) = '0' then
94
          -- Taste Sw1 (Kanäle) gedrückt: Umschalten Mono/Stero bei Listen to Channel?
95
          if StateTa1 < 2 then
96
            StateTa1 <= 1;
97
          end if;
98
        else
99
          if StateTa1 = 1 then
100
            SMono <= not SMono;
101
          end if;
102
          StateTa1 <= 0;
103
        end if;
104
        if SPI_Status = 24 then
105
          -- Restart SPI-Tx
106
          SPI_Status <= 0;
107
          FP_DStb <= '1';
108
          MuxCount <= std_logic_vector(unsigned(MuxCount) + 1);          -- Mux Digit 0..7
109
        else
110
          -- Continue SPI-Tx
111
          SPI_Status <= SPI_Status + 1;
112
          FP_DClk <= '0';
113
          InputShift <= InputShift(4 downto 0) & FP_DIn;
114
          if SPI_Status = 8 then
115
            -- 1 Byte data input
116
            InputReg <= InputShift;
117
            InputReg2 <= InputReg;
118
            if InputReg(4) = '0' then
119
              -- Taste Sw2 (Sample Rate) gedrückt
120
              case InputReg2(1 downto 0) & InputReg(1 downto 0) is
121
                -- Sample Rate
122
                when "0100" | "1011" => if unsigned(SRate) > 0 then
123
                              SRate <= std_logic_vector(unsigned(SRate) - 1);
124
                              SetupChanged <= '1';
125
                              if unsigned(SRate) = 4 then
126
                                -- 128 kHz überspringen
127
                                SRate <= "00001";
128
                              elsif SRate(1 downto 0) = "00" then
129
                                -- 8, 16, 32, 64 kHz nicht mit 22 MHz Oszillator
130
                                SRate <= std_logic_vector(unsigned(SRate) - 2);
131
                              end if;
132
                            end if;
133
                when "0001" | "1110" => if unsigned(SRate) < 18 then
134
                              SRate <= std_logic_vector(unsigned(SRate) + 1);
135
                              SetupChanged <= '1';
136
                              if unsigned(SRate) = 1 then
137
                                -- 128 kHz überspringen
138
                                SRate <= "00100";
139
                              elsif SRate(1 downto 0) = "10" then
140
                                -- 8, 16, 32, 64 kHz nicht mit 22 MHz Oszillator
141
                                SRate <= std_logic_vector(unsigned(SRate) + 2);
142
                              end if;
143
                            end if;
144
                when others => null;
145
              end case;
146
            end if;
147
            if InputReg(5) = '0' then
148
              -- Taste Sw1 (Kanäle) gedrückt
149
              case InputReg2(3 downto 2) & InputReg(3 downto 2) is
150
                -- Number of Channels
151
                when "0100" | "1011" => if unsigned(NChannels) < 31 then 
152
                              NChannels <= std_logic_vector(unsigned(NChannels) + 1);
153
                              SetupChanged <= '1';
154
                            end if;
155
                            StateTa1 <= 2;        -- Keine Mono/Stereo-Umschaltung
156
                when "0001" | "1110" => if unsigned(NChannels) > 0 then 
157
                              NChannels <= std_logic_vector(unsigned(NChannels) - 1);
158
                              SetupChanged <= '1';
159
                              if SChannel > NChannels then 
160
                                SChannel <= NChannels;
161
                              end if;
162
                            end if;
163
                            StateTa1 <= 2;        -- Keine Mono/Stereo-Umschaltung
164
                when others => null;
165
              end case;
166
            else
167
              -- Taste Sw1 (Kanäle) gelöst
168
              case InputReg2(3 downto 2) & InputReg(3 downto 2) is
169
                -- Selected Channel
170
                when "0100" | "1011" =>  if SChannel < NChannels then 
171
                              SChannel <= std_logic_vector(unsigned(SChannel) + 1);
172
                            end if;
173
                when "0001" | "1110" => if unsigned(SChannel) > 0 then 
174
                              SChannel <= std_logic_vector(unsigned(SChannel) - 1);
175
                            end if;
176
                when others => null;
177
              end case;
178
            end if;
179
            if SChannel > NChannels then 
180
              SChannel <= NChannels;
181
            end if;
182
          end if;
183
        end if;
184
      end if;
185
    end if;
186
  end process SPI_and_Settings;
187
188
  with to_integer(unsigned(SRate)) select
189
  BCD_ValSR <=  x"1920" when 0,
190
          x"1764" when 1,
191
          x"f960" when 4,
192
          x"f882" when 5,
193
          x"f640" when 6,
194
          x"f480" when 8,
195
          x"f441" when 9,
196
          x"f320" when 10,
197
          x"f240" when 12,
198
          x"2205" when 13,
199
          x"f160" when 14,
200
          x"f120" when 16,
201
          x"1102" when 17,
202
          x"ff80" when others;
203
204
  with to_integer(unsigned(SRate)) select
205
    DP_SR <=   3 when 13 | 17,
206
          4 when others;
207
  
208
  NumVal32 <= "000" & std_logic_vector(unsigned(NChannels) + 1) when unsigned(MuxCount) < 4
209
       else "000" & std_logic_vector(unsigned(SChannel) + 1);
210
  
211
  BCD_Val32 <= std_logic_vector(unsigned(NumVal32) +  0) when unsigned(NumVal32) < 10
212
      else std_logic_vector(unsigned(NumVal32) +  6) when unsigned(NumVal32) < 20
213
      else std_logic_vector(unsigned(NumVal32) + 12) when unsigned(NumVal32) < 30
214
      else std_logic_vector(unsigned(NumVal32) + 18);
215
216
  BCD7SegIn <= BCD_Val32(3 downto 0)   when unsigned(MuxCount) = 1 or unsigned(MuxCount) = 7
217
      else BCD_ValSR(15 downto 12) when unsigned(MuxCount) = 2
218
      else BCD_ValSR(11 downto 8)  when unsigned(MuxCount) = 3
219
      else BCD_ValSR(7 downto 4)   when unsigned(MuxCount) = 4
220
      else BCD_ValSR(3 downto 0)   when unsigned(MuxCount) = 5
221
      else x"f"           when unsigned(BCD_Val32(7 downto 4)) = 0  -- 0 & 6
222
      else BCD_Val32(7 downto 4);          -- 0 & 6
223
224
  with to_integer(unsigned(BCD7SegIn)) select
225
    BCD7SegOut <=  x"fc" when 0,
226
            x"60" when 1,
227
            x"da" when 2,
228
            x"f2" when 3,
229
            x"66" when 4,
230
            x"b6" when 5,
231
            x"be" when 6,
232
            x"e0" when 7,
233
            x"fe" when 8,
234
            x"f6" when 9,
235
            x"00" when others;
236
237
  FP_DOut <=  SMono when (unsigned(MuxCount) = 7) and (SPI_Status = 9) else                -- Dez. Punkt für SelMono
238
        '1' when (to_integer(unsigned(MuxCount)) = DP_SR) and (SPI_Status = 9) else              -- Dez. Punkt für Sample Rate
239
        BCD7SegOut(SPI_Status - 9) when (SPI_Status >= 9) and (SPI_Status <= 16) else  --  8..15: Segm.-LEDs
240
        '0' when (SPI_Status) - 17 = to_integer(unsigned(not MuxCount)) else                  -- 16..23: Anode driver on
241
        '1';                                      --  0..7 and Anode driver off 
242
243
  SampleRate <= SRate;
244
  NoOfChannels <= NChannels;
245
  SelChannel <= SChannel;
246
  SelMono <= SMono;
247
248
end Default;