-
Notifications
You must be signed in to change notification settings - Fork 10
/
soc_zephyr.py
146 lines (125 loc) · 5.66 KB
/
soc_zephyr.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#!/usr/bin/env python3
import os
import argparse
from migen import *
from litex.soc.integration.soc import *
from litex.soc.cores.i2s import *
from litex.soc.cores.gpio import *
from litex.soc.cores.pwm import PWM
from litex.soc.cores.spi import SPIMaster
from litex.soc.cores.bitbang import I2CMaster
from litex_boards.platforms import digilent_arty as arty_platform
from litex.soc.cores.gpio import GPIOOut, GPIOIn
from liteeth.phy.mii import LiteEthPHYMII
# Helpers ------------------------------------------------------------------------------------------
def platform_request_all(platform, name):
from litex.build.generic_platform import ConstraintError
r = []
while True:
try:
r += [platform.request(name, len(r))]
except ConstraintError:
break
if r == []:
raise ValueError
return r
# SoCZephyr -----------------------------------------------------------------------------------------
def SoCZephyr(soc_cls, **kwargs):
class _SoCZephyr(soc_cls):
csr_map = {**soc_cls.csr_map, **{
"ctrl": 0, # addr: 0xe0000000
"uart": 3, # addr: 0xe0001800
"spi": 4, # addr: 0xe0002000
"timer0": 5, # addr: 0xe0002800
"sdram": 6, # addr: 0xe0003000
"uartphy": 7, # addr: 0xe0004000
"mmcm": 9, # addr: 0xe0004800
"i2c0": 10, # addr: 0xe0005000
"rgb_led_r0": 14, # addr: 0xe0007000
"ethphy": 16, # addr: 0xe0008000
"ethmac": 19, # addr: 0xe0009800
"i2s_rx": 21, # addr: 0xe000a800
"i2s_tx": 22, # addr: 0xe000b000
"ddrphy": 23, # addr: 0xe000b800
"spiflash_core": 24, # addr: 0xe000c000
"spiflash_phy": 25, # addr: 0xe000c800
"watchdog0": 26, # addr: 0xe000d000
}}
interrupt_map = {**soc_cls.interrupt_map, **{
"timer0": 1,
"uart": 2,
"ethmac": 3,
"i2s_rx": 6,
"i2s_tx": 7,
"watchdog0": 8,
}}
mem_map_zephyr = {
"rom": 0x00000000,
"sram": 0x01000000,
"main_ram": 0x40000000,
"spiflash": 0x60000000,
"ethmac": 0xb0000000,
"i2s_rx": 0xb1000000,
"i2s_tx": 0xb2000000,
"csr": 0xe0000000,
}
def __init__(self, cpu_variant="standard", **kwargs):
soc_cls.__init__(self,
cpu_type="vexriscv",
cpu_variant=cpu_variant,
csr_data_width=32,
max_sdram_size=0x10000000, # Limit mapped SDRAM to 256MB for now
timer_uptime=True,
**kwargs)
soc_cls.mem_map.update(self.mem_map_zephyr)
def add_rgb_led(self):
rgb_led_pads = self.platform.request("rgb_led", 0)
setattr(self.submodules, "rgb_led_r0", PWM(getattr(rgb_led_pads, 'r')))
def add_i2c(self):
self.submodules.i2c0 = I2CMaster(self.platform.request("i2c", 0))
def add_i2s(self):
self.platform.add_extension(arty_platform._i2s_pmod_io)
i2s_mem_size = 0x40000
# i2s rx
self.submodules.i2s_rx = S7I2S(
pads=self.platform.request("i2s_rx"),
sample_width=24,
frame_format=I2S_FORMAT.I2S_STANDARD,
concatenate_channels=False,
toolchain=kwargs["toolchain"]
)
self.add_memory_region("i2s_rx", self.mem_map_zephyr["i2s_rx"], i2s_mem_size, type="io")
self.add_wb_slave(self.mem_regions["i2s_rx"].origin, self.i2s_rx.bus, i2s_mem_size)
# i2s tx
self.submodules.i2s_tx = S7I2S(
pads=self.platform.request("i2s_tx"),
sample_width=24,
frame_format=I2S_FORMAT.I2S_STANDARD,
master=True,
concatenate_channels=False,
toolchain=kwargs["toolchain"]
)
self.add_memory_region("i2s_tx", self.mem_map_zephyr["i2s_tx"], i2s_mem_size, type="io")
self.add_wb_slave(self.mem_regions["i2s_tx"].origin, self.i2s_tx.bus, i2s_mem_size)
self.comb += self.platform.request("i2s_rx_mclk").eq(self.cd_mmcm_clkout["i2s_rx"].clk)
self.comb += self.platform.request("i2s_tx_mclk").eq(self.cd_mmcm_clkout["i2s_tx"].clk)
def add_mmcm(self, freqs={}):
self.cd_mmcm_clkout = {}
self.submodules.mmcm = S7MMCM(speedgrade=-1)
self.mmcm.register_clkin(self.crg.cd_sys.clk, self.clk_freq)
self.add_constant("clkout_def_freq", int(self.clk_freq))
self.add_constant("clkout_def_phase", int(0))
self.add_constant("clkout_def_duty_num", int(50))
self.add_constant("clkout_def_duty_den", int(100))
self.add_constant("mmcm_lock_timeout", int(10))
self.add_constant("mmcm_drdy_timeout", int(10))
for n, key in enumerate(freqs):
self.cd_mmcm_clkout.update({key : ClockDomain(name="cd_mmcm_clkout{}".format(n))})
self.mmcm.create_clkout(self.cd_mmcm_clkout[key], freqs[key])
for n in range(len(freqs), 7):
key = "clk_{}".format(n)
self.cd_mmcm_clkout.update({key : ClockDomain(name="cd_mmcm_clkout{}".format(n))})
self.mmcm.create_clkout(self.cd_mmcm_clkout[key], self.clk_freq)
self.mmcm.expose_drp()
self.comb += self.mmcm.reset.eq(self.mmcm.drp_reset.re)
return _SoCZephyr(**kwargs)