Skip to content

Commit 3c55481

Browse files
committed
Merge branch 'hardware'
2 parents c83df8a + f338aab commit 3c55481

16 files changed

+728
-56
lines changed

ftdi.py

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
from pyftdi.ftdi import Ftdi
2+
from pyftdi.spi import (SpiController)
3+
from time import sleep
4+
5+
Ftdi.show_devices()
6+
7+
spi = SpiController()
8+
spi.configure('ftdi://ftdi:232h:FT4RZWM0/1')
9+
slave = spi.get_port(cs=0, freq = 12E4, mode=0)
10+
11+
def writeToSlave(out: bytes):
12+
global spi
13+
global slave
14+
15+
slave.write(out)
16+
17+
class _Getch:
18+
"""Gets a single character from standard input. Does not echo to the screen."""
19+
def __init__(self):
20+
try:
21+
self.impl = _GetchWindows()
22+
except ImportError:
23+
self.impl = _GetchUnix()
24+
25+
def __call__(self): return self.impl()
26+
27+
28+
class _GetchUnix:
29+
def __init__(self):
30+
import tty, sys
31+
32+
def __call__(self):
33+
import sys, tty, termios
34+
fd = sys.stdin.fileno()
35+
old_settings = termios.tcgetattr(fd)
36+
try:
37+
tty.setraw(sys.stdin.fileno())
38+
ch = sys.stdin.read(1)
39+
finally:
40+
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
41+
return ch
42+
43+
44+
class _GetchWindows:
45+
def __init__(self):
46+
import msvcrt
47+
48+
def __call__(self):
49+
import msvcrt
50+
return msvcrt.getch()
51+
52+
53+
getch = _Getch()
54+
55+
curValue = 0
56+
curMode = "LED"
57+
58+
print("Now in LED mode")
59+
60+
while True:
61+
if curMode == "LED":
62+
c = ord(getch())
63+
64+
if c == ord('q'):
65+
spi.close()
66+
quit()
67+
elif c == ord('t'):
68+
curMode = "TAPS"
69+
print("Now in Taps mode")
70+
continue
71+
72+
n = c - ord('0')
73+
a = c - ord('a')
74+
75+
newNibble = 0
76+
77+
if n in range(0, 10):
78+
print(n)
79+
newNibble = n
80+
elif a in range(0, 7):
81+
a = a + 10
82+
print(a)
83+
newNibble = a
84+
else:
85+
continue
86+
87+
curValue = ((curValue << 4) & 0xF0) | newNibble
88+
89+
writeToSlave([0x01, curValue])
90+
elif curMode == "TAPS":
91+
line = input()
92+
93+
if line == "q":
94+
spi.close()
95+
quit()
96+
elif line == "l":
97+
curMode = "LED"
98+
print("Now in LED mode")
99+
continue
100+
101+
coefficient = int(line)
102+
103+
if coefficient not in range(0, 256):
104+
print("Value must be between 0 and 255")
105+
continue
106+
107+
writeToSlave([0x02, coefficient])
108+
109+
110+
111+
112+

public/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,4 @@
2727
<noscript>You need to enable JavaScript to run this app.</noscript>
2828
<div id="root"></div>
2929
</body>
30-
</html>
30+
</html>

public/main.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ function createWindow() {
4343
) {
4444
return true;
4545
}
46-
return false;
4746
});
4847

4948
win.webContents.session.on(
@@ -164,4 +163,4 @@ if (process.env.NODE_ENV !== "production") {
164163
},
165164
],
166165
});
167-
}
166+
}

vhdl/AdderTree.vhd

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
library ieee;
2+
use ieee.std_logic_1164.all;
3+
use ieee.numeric_std.all;
4+
use ieee.math_real.all;
5+
use work.ClariFi.all;
6+
7+
entity AdderTree is
8+
generic (
9+
WIDTH : natural
10+
; NUM_OPERANDS : natural
11+
);
12+
port (
13+
operands : in RegArray(0 to NUM_OPERANDS-1)(WIDTH-1 downto 0)
14+
; sum : out signed(WIDTH + natural(ceil(log2(real(NUM_OPERANDS)))) - 1 downto 0)
15+
);
16+
end entity;
17+
18+
architecture arch of AdderTree is
19+
signal inner_operands : RegArray(0 to (NUM_OPERANDS / 2) + (NUM_OPERANDS mod 2) - 1)(WIDTH downto 0);
20+
-- signal odd_dummy_operands : RegArray(0 to operands'high + 1)(WIDTH downto 0);
21+
-- signal odd_dummy_sum : signed(sum'high downto 0);
22+
begin
23+
-- sum <= ('0' & inner_operands(0)) + inner_operands(1);
24+
25+
odd_transform: if NUM_OPERANDS mod 2 = 1 generate
26+
inner_operands(inner_operands'high) <= '0' & operands(operands'high);
27+
end generate;
28+
29+
base_even: if NUM_OPERANDS = 2 generate
30+
sum <= ('0' & operands(0)) + operands(1);
31+
end generate;
32+
33+
-- base_odd: if NUM_OPERANDS = 3 generate
34+
-- sum <= ('0' & (('0' & operands(0)) + operands(1))) + operands(2);
35+
-- end generate;
36+
37+
inner_adders: if NUM_OPERANDS > 2 generate
38+
recurse: for i in 0 to inner_operands'high - (NUM_OPERANDS mod 2) generate
39+
inner_operands(i) <= ('0' & operands(i * 2)) + operands(i * 2 + 1);
40+
end generate;
41+
42+
uLayerAdder: entity work.AdderTree
43+
generic map (
44+
WIDTH => WIDTH + 1
45+
, NUM_OPERANDS => inner_operands'length
46+
)
47+
port map (
48+
operands => inner_operands
49+
, sum => sum
50+
);
51+
end generate;
52+
end architecture;

vhdl/BasicFIR.vhd

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,29 @@
1-
library ieee;
2-
use ieee.std_logic_1164.all;
3-
use ieee.numeric_std.all;
1+
-- library ieee;
2+
-- use ieee.std_logic_1164.all;
3+
-- use ieee.numeric_std.all;
44

5-
package ClariFi is
6-
type CoeffiecientArray is array(natural range <>) of integer;
7-
type RegArray is array(natural range <>) of signed;
8-
type SLVArray is array(natural range <>) of std_logic_vector;
9-
end package;
5+
-- package ClariFi is
6+
-- type CoeffiecientArray is array(natural range <>) of integer;
7+
-- type RegArray is array(natural range <>) of signed;
8+
-- type SLVArray is array(natural range <>) of std_logic_vector;
9+
-- end package;
1010

1111
library ieee;
1212
use ieee.std_logic_1164.all;
1313
use ieee.numeric_std.all;
14+
use IEEE.math_real.all;
1415
use work.ClariFi.all;
1516

1617
entity BasicFIR is
1718
generic (
1819
RESOLUTION : natural
1920
; NUM_TAPS : natural
20-
; COEFFICIENTS : CoeffiecientArray(0 to NUM_TAPS-1)
21+
-- ; COEFFICIENTS : CoeffiecientArray(0 to NUM_TAPS-1)
2122
);
2223
port (
2324
clk, rst, en : in std_logic
2425
; sample : in std_logic_vector(RESOLUTION-1 downto 0)
26+
; coefficients : RegArray(0 to NUM_TAPS-1)
2527
; filtered : out std_logic_vector(RESOLUTION-1 downto 0)
2628
);
2729
end entity;
@@ -30,7 +32,7 @@ architecture SingleOrder of BasicFIR is
3032
signal taps : RegArray(0 to NUM_TAPS-1)(RESOLUTION-1 downto 0);
3133
signal multiplies : RegArray(0 to NUM_TAPS-1)((RESOLUTION*2)-1 downto 0);
3234
signal multiplySLVs : SLVArray(0 to NUM_TAPS-1)((RESOLUTION*2)-1 downto 0);
33-
signal accumulator : signed((RESOLUTION*2)-1 downto 0);
35+
signal accumulator : signed((RESOLUTION * 2) + natural(ceil(log2(real(NUM_TAPS)))) - 1 downto 0);
3436
begin
3537
LoadTaps: process(clk, rst, en)
3638
begin
@@ -44,30 +46,40 @@ begin
4446
end loop;
4547
end if;
4648
end process;
47-
49+
4850
uMultipliers: for i in 0 to NUM_TAPS-1 generate
4951
uMultiplier: entity work.SignedMultDSP
5052
port map (
5153
CLK => clk
5254
, A => std_logic_vector(taps(i))
53-
, B => std_logic_vector(to_signed(COEFFICIENTS(i), RESOLUTION))
55+
, B => std_logic_vector(COEFFICIENTS(i))
5456
, P => multiplySLVs(i)
5557
);
5658

5759
multiplies(i) <= signed(multiplySLVs(i));
5860
end generate;
5961

60-
process (multiplies)
61-
variable acc : signed(accumulator'range) := (others => '0');
62-
begin
63-
acc := (others => '0');
62+
-- proc_accumulator: process (multiplies)
63+
-- variable acc : signed(accumulator'range) := (others => '0');
64+
-- begin
65+
-- acc := (others => '0');
6466

65-
for i in 0 to NUM_TAPS-1 loop
66-
acc := acc + multiplies(i);
67-
end loop;
67+
-- for i in 0 to NUM_TAPS-1 loop
68+
-- acc := acc + multiplies(i);
69+
-- end loop;
6870

69-
accumulator <= acc;
70-
end process;
71+
-- accumulator <= acc;
72+
-- end process;
73+
74+
uAdderTree: entity work.AdderTree
75+
generic map (
76+
WIDTH => RESOLUTION*2
77+
, NUM_OPERANDS => NUM_TAPS
78+
)
79+
port map (
80+
operands => multiplies
81+
, sum => accumulator
82+
);
7183

7284
filtered <= std_logic_vector(accumulator(accumulator'high downto accumulator'high - (RESOLUTION - 1)));
7385
end architecture;

vhdl/CS4272.vhd

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
-- Controller for the CS4272-DZZ (DAC and ADC)
2+
-- Assuming a sample frequency of 44.1 kHz
3+
-- Note that unless we have Fs > 50 KHz, pins M0 and M1
4+
-- should both be 0 (can just connect to DGND).
5+
-- CTRL = x"10" & RegAddr & Data
6+
7+
library ieee;
8+
use ieee.std_logic_1164.all;
9+
use ieee.numeric_std.all;
10+
11+
entity CS4272 is
12+
generic (
13+
WIDTH : natural
14+
);
15+
port (
16+
clk : in std_logic;
17+
rst : in std_logic;
18+
--go : in std_logic;
19+
en : in std_logic;
20+
DIN : in std_logic_vector(WIDTH-1 downto 0); -- Digital in
21+
AIN : in std_logic_vector(WIDTH-1 downto 0); -- Analog in
22+
CS : out std_logic; -- Connect to pin 13
23+
DOUT : out std_logic_vector(WIDTH-1 downto 0);
24+
AOUT : out std_logic_vector(WIDTH-1 downto 0);
25+
CTRL : out std_logic_vector(23 downto 0); -- Control signal for configuration
26+
converting : out std_logic;
27+
done : out std_logic
28+
);
29+
end entity;
30+
31+
architecture ADC_DAC of CS4272 is
32+
33+
type StateType is (S_Ready, S_DACControl, S_Converting, S_Done);
34+
signal state_r, next_state : StateType;
35+
signal ADC_data_r, DAC_data_r : std_logic_vector(WIDTH-1 downto 0);
36+
37+
begin
38+
process (clk, rst)
39+
begin
40+
if (rst = '1') then
41+
state_r <= S_Ready;
42+
ADC_data_r <= (others => '0');
43+
DAC_data_r <= (others => '0');
44+
elsif (rising_edge(clk)) then
45+
state_r <= next_state;
46+
end if;
47+
end process;
48+
49+
AIN <= ADC_data_r;
50+
DIN <= DAC_data_r;
51+
52+
process (state_r, go, en, DIN, AIN)
53+
begin
54+
done <= '0';
55+
converting <= '0';
56+
next_state <= state_r;
57+
58+
case state_r is
59+
when S_Ready =>
60+
CS <= '0';
61+
CTRL <= x"10" & x"07" & x"02"; -- Chip address, write, set CPEN, clear PDN
62+
next_state <= S_DACControl; -- we should always do configuration first regardless of go
63+
64+
when S_DACControl =>
65+
CS <= '0';
66+
-- Auto-Mute off, fast roll off, De-Emphasis Filter off, Ramp-up/down off, no inversion
67+
-- The default is x"00" anyways but in case we want to enable any option later
68+
CTRL <= x"10" & x"02" & x"00";
69+
next_state <= S_ADCControl;
70+
71+
when S_ADCControl =>
72+
CS <= '0';
73+
-- Dither16 is off (when we're using 8-bits resolution), 24-bit LJ, No mutes, High pass filter enabled (default)
74+
CTRL <= x"10" & x"06" & x"00";
75+
if (en = '1') -- when do we start converting
76+
next_state <= S_Converting;
77+
end if;
78+
79+
when S_Converting =>
80+
CS <= '1';
81+
converting <= '1';
82+
ADC_data_r <= AIN; -- Connect pins appropriately
83+
DAC_data_r <= DIN;
84+
next_state <= S_Done;
85+
86+
when S_Done =>
87+
CS <= '1';
88+
done <= '1'; -- you can get the data now
89+
if (en = '1')
90+
done <= '0'; -- another conversion
91+
next_state <= S_Converting; -- just convert, registers are already configured
92+
end if;
93+
end case;
94+
end process;
95+
end architecture;

0 commit comments

Comments
 (0)