nanopynq 0.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (109) hide show
  1. nanopynq-0.1.0/LICENSE +21 -0
  2. nanopynq-0.1.0/PKG-INFO +430 -0
  3. nanopynq-0.1.0/README.md +406 -0
  4. nanopynq-0.1.0/THIRD_PARTY_NOTICES.md +21 -0
  5. nanopynq-0.1.0/licenses/esp32-camera-LICENSE +202 -0
  6. nanopynq-0.1.0/licenses/gowin-ddr3-LICENSE +201 -0
  7. nanopynq-0.1.0/licenses/openmv-ov2640-LICENSE +28 -0
  8. nanopynq-0.1.0/pyproject.toml +91 -0
  9. nanopynq-0.1.0/pyproject.toml.orig +76 -0
  10. nanopynq-0.1.0/src/nanopynq/__init__.py +4 -0
  11. nanopynq-0.1.0/src/nanopynq/__main__.py +72 -0
  12. nanopynq-0.1.0/src/nanopynq/boards.py +44 -0
  13. nanopynq-0.1.0/src/nanopynq/browser.py +92 -0
  14. nanopynq-0.1.0/src/nanopynq/build/__init__.py +0 -0
  15. nanopynq-0.1.0/src/nanopynq/build/collect.py +302 -0
  16. nanopynq-0.1.0/src/nanopynq/build/crosscheck.py +400 -0
  17. nanopynq-0.1.0/src/nanopynq/build/design.py +291 -0
  18. nanopynq-0.1.0/src/nanopynq/build/gowin.py +77 -0
  19. nanopynq-0.1.0/src/nanopynq/camera.py +120 -0
  20. nanopynq-0.1.0/src/nanopynq/data/99-nanopynq.rules +5 -0
  21. nanopynq-0.1.0/src/nanopynq/data/boards.yaml +42 -0
  22. nanopynq-0.1.0/src/nanopynq/data/designs/tangmega138kpro-base/base.fs +21889 -0
  23. nanopynq-0.1.0/src/nanopynq/data/designs/tangmega138kpro-base/base.npq +69 -0
  24. nanopynq-0.1.0/src/nanopynq/data/designs/tangmega138kpro-base/build.json +55 -0
  25. nanopynq-0.1.0/src/nanopynq/data/designs/tangmega138kpro-ddr3/base.fs +4 -0
  26. nanopynq-0.1.0/src/nanopynq/data/designs/tangmega138kpro-ddr3/base.npq +144 -0
  27. nanopynq-0.1.0/src/nanopynq/data/designs/tangmega138kpro-ddr3/build.json +81 -0
  28. nanopynq-0.1.0/src/nanopynq/data/designs/tangnano20k-base/base.fs +1360 -0
  29. nanopynq-0.1.0/src/nanopynq/data/designs/tangnano20k-base/base.npq +68 -0
  30. nanopynq-0.1.0/src/nanopynq/data/designs/tangnano20k-base/build.json +47 -0
  31. nanopynq-0.1.0/src/nanopynq/data/designs/tangnano20k-sdram/base.fs +1360 -0
  32. nanopynq-0.1.0/src/nanopynq/data/designs/tangnano20k-sdram/base.npq +133 -0
  33. nanopynq-0.1.0/src/nanopynq/data/designs/tangnano20k-sdram/build.json +63 -0
  34. nanopynq-0.1.0/src/nanopynq/data/designs/tangnano4k-base/base.fs +510 -0
  35. nanopynq-0.1.0/src/nanopynq/data/designs/tangnano4k-base/manifest.yaml +11 -0
  36. nanopynq-0.1.0/src/nanopynq/data/designs/tangnano4k-camera/base.fs +766 -0
  37. nanopynq-0.1.0/src/nanopynq/data/designs/tangnano4k-camera/manifest.yaml +14 -0
  38. nanopynq-0.1.0/src/nanopynq/data/designs/tangnano4k-camstream/base.fs +766 -0
  39. nanopynq-0.1.0/src/nanopynq/data/designs/tangnano4k-camstream/manifest.yaml +14 -0
  40. nanopynq-0.1.0/src/nanopynq/data/designs/tangnano9k-base/base.fs +728 -0
  41. nanopynq-0.1.0/src/nanopynq/data/designs/tangnano9k-base/base.npq +69 -0
  42. nanopynq-0.1.0/src/nanopynq/data/designs/tangnano9k-base/build.json +55 -0
  43. nanopynq-0.1.0/src/nanopynq/doctor.py +116 -0
  44. nanopynq-0.1.0/src/nanopynq/drivers/__init__.py +1 -0
  45. nanopynq-0.1.0/src/nanopynq/drivers/capture.py +39 -0
  46. nanopynq-0.1.0/src/nanopynq/drivers/ddr3.py +71 -0
  47. nanopynq-0.1.0/src/nanopynq/drivers/dvp.py +40 -0
  48. nanopynq-0.1.0/src/nanopynq/drivers/frame_ring.py +66 -0
  49. nanopynq-0.1.0/src/nanopynq/drivers/gpio.py +43 -0
  50. nanopynq-0.1.0/src/nanopynq/drivers/i2c.py +46 -0
  51. nanopynq-0.1.0/src/nanopynq/drivers/sdram.py +92 -0
  52. nanopynq-0.1.0/src/nanopynq/errors.py +83 -0
  53. nanopynq-0.1.0/src/nanopynq/gowin.py +214 -0
  54. nanopynq-0.1.0/src/nanopynq/ip.py +39 -0
  55. nanopynq-0.1.0/src/nanopynq/ipdesc/gpio_in.toml +7 -0
  56. nanopynq-0.1.0/src/nanopynq/ipdesc/gpio_out.toml +7 -0
  57. nanopynq-0.1.0/src/nanopynq/ipdesc/pwm_timer.toml +10 -0
  58. nanopynq-0.1.0/src/nanopynq/ipdesc/sysinfo.toml +8 -0
  59. nanopynq-0.1.0/src/nanopynq/manifest.py +79 -0
  60. nanopynq-0.1.0/src/nanopynq/npq.py +515 -0
  61. nanopynq-0.1.0/src/nanopynq/overlay.py +227 -0
  62. nanopynq-0.1.0/src/nanopynq/program.py +69 -0
  63. nanopynq-0.1.0/src/nanopynq/py.typed +0 -0
  64. nanopynq-0.1.0/src/nanopynq/resolve.py +89 -0
  65. nanopynq-0.1.0/src/nanopynq/rtl/base_regs.v +52 -0
  66. nanopynq-0.1.0/src/nanopynq/rtl/base_soc.v +28 -0
  67. nanopynq-0.1.0/src/nanopynq/rtl/bus_cdc.v +63 -0
  68. nanopynq-0.1.0/src/nanopynq/rtl/cam_fb.v +219 -0
  69. nanopynq-0.1.0/src/nanopynq/rtl/capture.v +99 -0
  70. nanopynq-0.1.0/src/nanopynq/rtl/clk_meter.v +82 -0
  71. nanopynq-0.1.0/src/nanopynq/rtl/ddr3/ddr3_calib.sv +226 -0
  72. nanopynq-0.1.0/src/nanopynq/rtl/ddr3/ddr3_cmd.sv +215 -0
  73. nanopynq-0.1.0/src/nanopynq/rtl/ddr3/ddr3_ctrl.sv +247 -0
  74. nanopynq-0.1.0/src/nanopynq/rtl/ddr3/ddr3_init.sv +126 -0
  75. nanopynq-0.1.0/src/nanopynq/rtl/ddr3/ddr3_phy_2to1.sv +381 -0
  76. nanopynq-0.1.0/src/nanopynq/rtl/ddr3/ddr3_phy_slow.sv +319 -0
  77. nanopynq-0.1.0/src/nanopynq/rtl/ddr3_arb.v +100 -0
  78. nanopynq-0.1.0/src/nanopynq/rtl/ddr3_port.v +62 -0
  79. nanopynq-0.1.0/src/nanopynq/rtl/ddr3_test.v +190 -0
  80. nanopynq-0.1.0/src/nanopynq/rtl/dvp_frontend.v +92 -0
  81. nanopynq-0.1.0/src/nanopynq/rtl/fb_display.v +61 -0
  82. nanopynq-0.1.0/src/nanopynq/rtl/frame_ring.v +278 -0
  83. nanopynq-0.1.0/src/nanopynq/rtl/hdmi_testpat.v +87 -0
  84. nanopynq-0.1.0/src/nanopynq/rtl/hyperram_ctrl.v +104 -0
  85. nanopynq-0.1.0/src/nanopynq/rtl/i2c_master.v +95 -0
  86. nanopynq-0.1.0/src/nanopynq/rtl/jtag_bridge.v +70 -0
  87. nanopynq-0.1.0/src/nanopynq/rtl/pwm_timer.v +64 -0
  88. nanopynq-0.1.0/src/nanopynq/rtl/regbus_gpio.v +33 -0
  89. nanopynq-0.1.0/src/nanopynq/rtl/sdram_arb.v +56 -0
  90. nanopynq-0.1.0/src/nanopynq/rtl/sdram_ctrl.v +142 -0
  91. nanopynq-0.1.0/src/nanopynq/rtl/sdram_port.v +61 -0
  92. nanopynq-0.1.0/src/nanopynq/rtl/sdram_test.v +152 -0
  93. nanopynq-0.1.0/src/nanopynq/rtl/spi_master.v +69 -0
  94. nanopynq-0.1.0/src/nanopynq/rtl/sysinfo.v +27 -0
  95. nanopynq-0.1.0/src/nanopynq/rtl/tmds_encoder.v +54 -0
  96. nanopynq-0.1.0/src/nanopynq/rtl/uart_rx.v +47 -0
  97. nanopynq-0.1.0/src/nanopynq/rtl/uart_tx.v +41 -0
  98. nanopynq-0.1.0/src/nanopynq/rtl/uartbone_bridge.v +102 -0
  99. nanopynq-0.1.0/src/nanopynq/rtl/uflash_reader.v +59 -0
  100. nanopynq-0.1.0/src/nanopynq/rtl/video_timing.v +34 -0
  101. nanopynq-0.1.0/src/nanopynq/sensors/__init__.py +1 -0
  102. nanopynq-0.1.0/src/nanopynq/sensors/ov2640.py +309 -0
  103. nanopynq-0.1.0/src/nanopynq/transport/__init__.py +0 -0
  104. nanopynq-0.1.0/src/nanopynq/transport/d2xx.py +181 -0
  105. nanopynq-0.1.0/src/nanopynq/transport/jtag.py +63 -0
  106. nanopynq-0.1.0/src/nanopynq/transport/jtagbridge.py +89 -0
  107. nanopynq-0.1.0/src/nanopynq/transport/mpsse.py +330 -0
  108. nanopynq-0.1.0/src/nanopynq/transport/serialport.py +48 -0
  109. nanopynq-0.1.0/src/nanopynq/transport/uartbone.py +56 -0
nanopynq-0.1.0/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 FabricGate Contributors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,430 @@
1
+ Metadata-Version: 2.4
2
+ Name: nanopynq
3
+ Version: 0.1.0
4
+ Summary: PYNQ-style control of open-toolchain FPGA boards (Sipeed Tang Nano) over USB (UART or JTAG)
5
+ License-Expression: MIT
6
+ License-File: LICENSE
7
+ License-File: THIRD_PARTY_NOTICES.md
8
+ License-File: licenses/esp32-camera-LICENSE
9
+ License-File: licenses/gowin-ddr3-LICENSE
10
+ License-File: licenses/openmv-ov2640-LICENSE
11
+ Requires-Dist: libusb-package>=1.0.30
12
+ Requires-Dist: pyserial>=3.5
13
+ Requires-Dist: pyusb>=1.3
14
+ Requires-Dist: pyyaml>=6
15
+ Requires-Dist: yowasp-yosys==0.70.0.0.post1259 ; extra == 'build'
16
+ Requires-Dist: yowasp-nextpnr-himbaechel-gowin==0.11.1.0.post826 ; extra == 'build'
17
+ Requires-Dist: fabricgate>=0.7 ; extra == 'fabricgate'
18
+ Requires-Python: >=3.11
19
+ Project-URL: Homepage, https://github.com/fabricgate/nanopynq
20
+ Project-URL: Issues, https://github.com/fabricgate/nanopynq/issues
21
+ Provides-Extra: build
22
+ Provides-Extra: fabricgate
23
+ Description-Content-Type: text/markdown
24
+
25
+ # nanopynq
26
+
27
+ PYNQ-style Jupyter control of cheap Gowin FPGA boards over USB, using only open tools.
28
+ First target: Sipeed Tang Nano 9K (talks over the onboard USB-UART). The Tang Nano 4K has no
29
+ USB-UART wired to the FPGA; its base design talks over the same JTAG link that programs it
30
+ (Gowin GW_JTAG through the onboard debugger), so it also needs only the one USB cable.
31
+ The Tang Nano 20K base design talks over GW_JTAG the same way. The Tang Mega 138K Pro base design talks
32
+ over its onboard USB-UART, like the 9K.
33
+
34
+ ```python
35
+ from nanopynq import Overlay
36
+ ol = Overlay("base", board="tangnano9k")
37
+ ol.leds[0].on()
38
+ ol.buttons[0].read()
39
+ ```
40
+
41
+ ## Getting Started
42
+
43
+ Requirements: Python 3.11 or newer. nanopynq loads the design into the FPGA's SRAM itself, over the
44
+ JTAG interface of the board's onboard USB debugger; openFPGALoader is optional (see
45
+ [Using openFPGALoader](#using-openfpgaloader)).
46
+
47
+ ### Linux
48
+
49
+ ```sh
50
+ python -m venv .venv && . .venv/bin/activate
51
+ pip install nanopynq
52
+ # USB permissions (JTAG and UART): install the udev rule shipped with nanopynq, then re-plug the board.
53
+ # (`python -m nanopynq doctor` prints this command with the rule's path if the rule is missing.)
54
+ sudo cp "$(python -c 'import nanopynq, pathlib; print(pathlib.Path(nanopynq.__file__).parent / "data" / "99-nanopynq.rules")')" /etc/udev/rules.d/ && sudo udevadm control --reload-rules && sudo udevadm trigger
55
+ # Serial port permissions: join the dialout group, then log in again.
56
+ sudo usermod -aG dialout $USER
57
+ python -m nanopynq doctor
58
+ ```
59
+
60
+ ### Windows
61
+
62
+ ```powershell
63
+ python -m venv .venv; .venv\Scripts\Activate.ps1
64
+ pip install nanopynq
65
+ python -m nanopynq doctor
66
+ ```
67
+
68
+ No driver change is needed when Windows' FTDI driver is on the board (it was on the PCs this was tried
69
+ on; a PC without it is untested): nanopynq then talks to the debugger's JTAG through FTDI's D2XX library
70
+ (`ftd2xx.dll`, installed with that driver; nanopynq does not ship it). This is checked on the Tang Nano 9K
71
+ and 20K; the 4K is not checked yet. After Zadig (the driver of only "Interface 0" replaced with WinUSB,
72
+ Interface 1, the UART, left alone) nanopynq opens the board with libusb instead; Zadig is needed for the
73
+ WebUSB page (`examples/webusb`) and for openFPGALoader.
74
+
75
+ D2XX is FTDI's software, and FTDI's licence for it covers products that use FTDI's own chips; the boards'
76
+ debuggers (BL702, BL616) are FT2232-compatible chips from another maker. If that matters to you, run Zadig
77
+ as above: nanopynq then opens the board with libusb and never through D2XX (it still loads `ftd2xx.dll`
78
+ to look for a board when libusb finds one it cannot open, for example while another program holds it).
79
+
80
+ ### Check your setup
81
+
82
+ ```sh
83
+ python -m nanopynq doctor [--board tangnano9k]
84
+ ```
85
+
86
+ Each check prints `OK <name>` or `NG <name>: <next step>`, and the exit code is 1 if any
87
+ required check is `NG`. The checks are: Python version, the debugger's JTAG USB interface (every board
88
+ is programmed over it), the board's serial port (Tang Nano 9K, which talks over the UART), the udev
89
+ rule (Linux only) or a note on the drivers (Windows only), openFPGALoader (only for a board set up to use
90
+ it), and the optional build tools (`pip install nanopynq[build]`), whose `NG` does not change the exit
91
+ code.
92
+
93
+ ### Using openFPGALoader
94
+
95
+ nanopynq does not need [openFPGALoader](https://github.com/trabucayre/openFPGALoader), but it is still
96
+ the tool for writing the flash, and you can load the SRAM with it instead of nanopynq's own sequence
97
+ (for example if that fails on your board):
98
+
99
+ ```python
100
+ from nanopynq import Overlay
101
+ from nanopynq.program import program_sram_openfpgaloader
102
+
103
+ ol = Overlay("base", board="tangnano9k", programmer=program_sram_openfpgaloader)
104
+ ```
105
+
106
+ openFPGALoader is taken from `PATH`, or from the environment variable `NANOPYNQ_OPENFPGALOADER`.
107
+ Install it with `sudo apt install openfpgaloader` (Linux) or, in an MSYS2 UCRT64 shell,
108
+ `pacman -S mingw-w64-ucrt-x86_64-openFPGALoader` (Windows).
109
+
110
+ ### Blink an LED
111
+
112
+ Open `examples/01_blink.ipynb` in Jupyter and run it from the top. It runs the four lines
113
+ above, then blinks an LED.
114
+
115
+ If `Overlay(...)` raises `BoardNotResponding`, unplug and re-plug the USB cable, then run
116
+ `Overlay("base", board="tangnano9k")` again: it programs the board first and then opens the port.
117
+ (Re-plugging cuts power and erases the design from SRAM, so `download=False` would not help here.
118
+ Use `download=False` only to reopen the port within the same power cycle.)
119
+
120
+ `Overlay(...)` checks that the board runs the design it opens: it reads the design ID (`sys.design_id`) and
121
+ compares it with the design's `base.npq` before touching any other register. If another design is on the board
122
+ it raises `DesignMismatch`; with `download=False`, run `Overlay(...)` again with the default `download=True` to
123
+ program this one. Designs that have only a `manifest.yaml` (not rebuilt yet) cannot be checked, so
124
+ `download=False` warns and goes on.
125
+
126
+ If `Overlay(...)` raises `PortOpenError`, on Windows call `ol.close()` on the previous `ol` (or restart
127
+ the kernel); on Linux join the `dialout` group (see above). `with Overlay(...) as ol:` closes the port
128
+ for you.
129
+
130
+ ### Tang Nano 4K
131
+
132
+ ```python
133
+ ol = Overlay("base", board="tangnano4k") # programs over JTAG, then talks over JTAG
134
+ ol.leds[0].on() # the 4K has one LED (pin 10)
135
+ ```
136
+
137
+ `examples/02_led_tangnano4k.ipynb` walks through on / off / toggle, blinking, following a button and
138
+ the raw register, then closes the link.
139
+
140
+ ### Tang Nano 4K camera
141
+
142
+ ```python
143
+ from nanopynq import Overlay
144
+ from nanopynq.camera import Camera
145
+ ol = Overlay("camera", board="tangnano4k")
146
+ cam = Camera(ol.cam_i2c, ol.cam_capture, ol.cam_dvp)
147
+ cam.start()
148
+ cam.snapshot() # 160x120 grayscale, shown in the notebook
149
+ ```
150
+
151
+ `examples/03_camera_tangnano4k.ipynb` walks through it. The camera overlay is built from general IPs
152
+ (I2C master, DVP front end, frame capture buffer) and the OV2640 driver ported from Espressif's
153
+ esp32-camera (Apache-2.0 and MIT parts, see `THIRD_PARTY_NOTICES.md`). Still pictures, about 0.6 s each.
154
+
155
+ ### Tang Nano 4K camera stream
156
+
157
+ ```python
158
+ ol = Overlay("camstream", board="tangnano4k")
159
+ cam = Camera(ol.cam_i2c, None, ol.cam_dvp, ring=ol.cam_ring)
160
+ cam.start()
161
+ for frame in cam.stream(20): # about 2 frames/s over JTAG
162
+ ...
163
+ frames = cam.record(50) # 50 frames at the sensor's rate (about 8.7/s), then read
164
+ ```
165
+
166
+ The camstream overlay keeps frames in the 8 MB PSRAM (HyperRAM) inside the FPGA package: a ring of
167
+ 256 slots of 32 KB that takes every frame at the sensor's rate and drops new ones when it is full
168
+ (`ol.cam_ring.dropped`). `examples/04_camera_stream_tangnano4k.ipynb` walks through it.
169
+
170
+ Programming and talking use the debugger's one JTAG interface (on Windows through FTDI's driver or, after
171
+ Zadig, WinUSB; see above). Close a previous `ol` (`ol.close()`) before running `Overlay(...)` again, or
172
+ the board's JTAG interface is still in use and cannot be opened. If the onboard debugger stops answering, unplug and re-plug
173
+ the USB cable and run `Overlay(...)` again.
174
+
175
+ ### Tang Nano 20K
176
+
177
+ ```python
178
+ ol = Overlay("base", board="tangnano20k") # programs over JTAG, then talks over JTAG
179
+ ol.leds[0].on() # 6 LEDs (pins 15-20)
180
+ ol.buttons[0].read() # S1 (pin 88); buttons[1] is S2 (pin 87). 1 while pressed
181
+ ```
182
+
183
+ The drivers are as for the 4K (no change needed on Windows; Zadig optional). The 20K's debugger (BL616)
184
+ is a USB high-speed device; nanopynq reads its packet size from the device, so nothing else changes.
185
+
186
+ Designs built with `nanopynq build` (such as `examples/tangnano20k_chaser`) talk over the debugger's UART
187
+ instead (FPGA pins 69 / 70, its second serial port). `Overlay` takes the link from the design's `.npq`, so the
188
+ call is the same: `Overlay("examples/tangnano20k_chaser/dist")`.
189
+
190
+ ### Tang Nano 20K SDRAM
191
+
192
+ ```python
193
+ with Overlay("sdram", board="tangnano20k") as ol: # programs the SRAM only, never the flash
194
+ ol.sdram.write(0, [1, 2, 3]) # 8 MB window, byte offsets, 32-bit words
195
+ ol.sdram.read(0, 3) # -> [1, 2, 3]
196
+ ol.sdram_test.run(passes=1) # whole-SDRAM self-test: SdramTestResult(passes, errors, first_error)
197
+ ```
198
+
199
+ The onboard 8 MB SDRAM is a plain memory window on the bus. `ol.sdram_test.run()` writes a pattern, reads it
200
+ back and compares, inside the FPGA (about 0.7 s for all 8 MB), (`run()` blocks; the
201
+ self-test registers can also be started directly, as `hw_check.py` step 5 does, and the host may then
202
+ use `ol.sdram` outside the tested range while it runs). Host transfers over JTAG run at about 16 000 words/s for lists (single-word calls are much
203
+ slower). The check on real hardware is `tools/p0/sdram20k/hw_check.py`; results are in `reports/report_sdram20k.md`.
204
+
205
+ ### Tang Mega 138K Pro
206
+
207
+ ```python
208
+ ol = Overlay("base", board="tangmega138kpro") # programs over JTAG, then talks over the UART
209
+ ol.leds[0].on() # 6 LEDs on the Dock
210
+ ol.buttons[0].read() # S1 on the Dock; buttons[1] is S2. 1 while pressed
211
+ ```
212
+
213
+ For the Tang Mega 138K Pro only (the SOM on the Pro Dock): the non-Pro Tang Mega 138K and the Tang Console
214
+ 138K have the same FPGA, so nanopynq cannot tell them apart, but their pins differ. Power the Dock from its
215
+ 12 V DC jack (USB does not power it), and switch it off before plugging or unplugging anything.
216
+
217
+ The drivers are as for the 9K (no change needed on Windows; Zadig optional). A load takes about 3-4 s. This
218
+ board's debugger (BL616) clocks data sent LSB first at only about 0.5 MHz, so nanopynq sends it MSB first
219
+ (`msb_first` in `boards.yaml`; `reports/report_tangmega138kpro.md`).
220
+
221
+ ### Tang Mega 138K Pro DDR3
222
+
223
+ ```python
224
+ ol = Overlay("ddr3", board="tangmega138kpro") # about 17 s: the 35 MB bitstream does not compress
225
+ ol.ddr3.write(0, [1, 2, 3]) # 1 GiB window, byte offsets, 32-bit words
226
+ ol.ddr3_test.run() # whole-DDR3 self-test: SdramTestResult(...)
227
+ ol.ddr3_test.speeds # (write, read) MB/s of that run
228
+ ```
229
+
230
+ The onboard DDR3 (1 GiB, 32-bit) runs from the 50 MHz board clock, CK 50 MHz in DLL-off mode, with
231
+ e-karge's controller and 2to1 PHY ([e-karge/gowin-ddr3](https://github.com/e-karge/gowin-ddr3), vendored in
232
+ `src/nanopynq/rtl/ddr3/`; nanopynq adds a write of a whole 8-word burst to the controller). The self-test moves
233
+ one 8-word (32-byte) burst per access: about 69 MB/s writing and 57 MB/s reading inside the FPGA, so the
234
+ self-test of the whole 1 GiB takes about 35 s. `ol.ddr3_test.calib` shows the calibration; after `calib_fail`,
235
+ power-cycle the Dock and load again. The host window moves one word per access over the 115200 baud UART, so it
236
+ is for checks, not for bulk data. A circuit of your own reaches the DDR3 as master b of `ddr3_arb` in place of
237
+ `ddr3_test`: each access writes or reads the 8 words of an aligned burst (`b_wdata` / `b_rd_data` 256 bits, word 0
238
+ in the low bits); to write fewer words, enable only their bytes in `b_wmask` (4 bits per word). See
239
+ `gateware/boards/tangmega138kpro/ddr3_top.v` and the comment in `src/nanopynq/rtl/ddr3_arb.v`.
240
+
241
+ The design needs forks of Apicula and nextpnr (registers in the IO blocks, e-karge's `HAS_5A_IDES` for the IDDR
242
+ that read DQ, and his GW5A IODELAY with a dynamic delay for the read calibration; `tools/build_base.py` takes them
243
+ from `DDR3_OVL`, `DDR3_CHIPDB` and `DDR3_NEXTPNR`). Without `HAS_5A_IDES` (as in Apicula master) every IDDR reads 0
244
+ and the calibration fails; nextpnr main stops at the dynamic delay. `build.json` records the fork commits; results are in
245
+ `reports/report_ddr3_138k_survey.md`.
246
+
247
+ ### In the browser (Web Serial)
248
+
249
+ `examples/webserial/index.html` drives a board from a web page, without Python. Open the file directly
250
+ (`file://` works, no web server needed) in a Chromium-based browser (Chrome, Edge): other browsers have no
251
+ Web Serial. The page does not program the board, so program the design first (for example with
252
+ `Overlay("base", board="tangnano9k")`, then `ol.close()`), and the page can use only designs that talk over
253
+ UART (the Tang Nano 9K base).
254
+
255
+ 1. Under "Design file", choose the `.npq` of the design on the board. For the 9K base it is
256
+ `src/nanopynq/data/designs/tangnano9k-base/base.npq` in this repository. The page checks the file and
257
+ shows the rule and the place if it is broken.
258
+ 2. Press Connect and choose the board's port (the debugger shows two; if there is no answer, try the
259
+ other one). The page reads the design ID from the board and stops, with the reason, if the board runs
260
+ another design or a design without an ID.
261
+ 3. LEDs, buttons and registers are built from the `.npq`.
262
+
263
+ ## Building your own gateware
264
+
265
+ `pip install nanopynq[build]` installs the YoWASP Yosys and nextpnr-himbaechel-gowin toolchains. With them,
266
+ `nanopynq build` turns a design folder into a bitstream and its metadata file, with open-source tools only.
267
+ For now it builds for the boards that talk over UART: the Tang Nano 9K, the Tang Mega 138K Pro, and the Tang Nano
268
+ 20K over its debugger's UART (FPGA pins 69 / 70; the 20K's bundled designs talk over JTAG instead). The 4K talks only
269
+ over JTAG, which needs tools pip does not install.
270
+
271
+ A design folder holds your Verilog, a constraint file and `nanopynq.toml`.
272
+ `examples/tangnano9k_pwm` in this repository is one: the base design plus a PWM timer on LED 5.
273
+ `examples/tangnano9k_adder` is one with an IP of your own: an adder (`adder.v`) whose registers are written whole in `nanopynq.toml`.
274
+ `examples/tangnano9k_chaser` is another: a chaser (`chaser.v`) that keeps running a light along the LEDs once started.
275
+ `examples/tangnano9k_button_counter` counts presses of the two buttons (`button_counter.v`, with the contact bounce filtered out).
276
+ `examples/tangnano9k_crc32` computes the CRC-32 of bytes written to it (`crc32.v`), the same value as Python's `zlib.crc32`.
277
+ `examples/tangnano20k_pwm`, `_chaser`, `_button_counter` and `_crc32` are the same four on the Tang Nano 20K: their own `top.v` and pins, the same RTL (the 9K examples' files, or the shared `pwm_timer.v`).
278
+
279
+ ```toml
280
+ name = "pwm" # the output files: dist/pwm.fs, dist/pwm.npq
281
+ board = "tangnano9k"
282
+ top = "top" # optional (default "top"); the top takes a DESIGN_ID parameter
283
+ seed = 1 # optional (default 1): the place-and-route seed
284
+ # Read in this order. The set of files and their order change the bitstream, so list only what you use.
285
+ # nanopynq:<file> is nanopynq's shared RTL (the UART bridge, sysinfo, GPIO, pwm_timer, ...).
286
+ sources = [
287
+ "nanopynq:base_regs.v", "nanopynq:pwm_timer.v", "nanopynq:regbus_gpio.v", "nanopynq:sysinfo.v",
288
+ "nanopynq:uart_rx.v", "nanopynq:uart_tx.v", "nanopynq:uartbone_bridge.v",
289
+ "top.v",
290
+ ]
291
+ constraints = "tangnano9k.cst"
292
+
293
+ # The IPs (spec 5.3) are collected from the Verilog; here only what it does not hold (see below).
294
+ [ips.pwm]
295
+ parameters = { clock_hz = 27000000 }
296
+ ```
297
+
298
+ ```
299
+ $ nanopynq build examples/tangnano9k_pwm # or: cd examples/tangnano9k_pwm && nanopynq build
300
+ examples/tangnano9k_pwm/dist
301
+ pwm.fs, pwm.npq, build.json
302
+ ```
303
+
304
+ The `ips` (the IPs as the host sees them, the same fields as in a `.npq`) are collected from your Verilog.
305
+ The build elaborates the design with yosys and makes an IP of every instance of a module nanopynq describes
306
+ (`src/nanopynq/ipdesc/`: `sysinfo`, `regbus_gpio` as `gpio_out` or `gpio_in`, `pwm_timer`). Its base is the
307
+ `BASE` parameter the module decodes its addresses with, so it cannot drift from the circuit, and its registers
308
+ come from the description. The IP's name is the instance attribute `(* npq_name = "pwm" *)`, else the instance
309
+ name; `base_regs` names its instances `sys`, `leds` and `buttons`. The attributes do not change the bitstream.
310
+ `[ips.<name>]` in `nanopynq.toml` adds to the collected IP of that name: `parameters` the Verilog does not
311
+ hold (`pwm_timer`'s `clock_hz`; the build stops if one is missing), and `instance = "timer"` to give that
312
+ instance this name without touching the Verilog. An entry with a `base` is an IP written whole, as in a
313
+ `.npq` (`registers`, `addr_range`, ...): it takes the place of the collected IP of the same name and of every collected
314
+ IP in its address range, so a `nanopynq.toml` with every IP written by hand builds as before. Instances whose
315
+ names repeat (a `generate` loop) need `instance =` or entries written by hand, and a module of your own that only
316
+ shares its name with one nanopynq describes is left out unless `nanopynq.toml` names it (the build says so). Write your own IPs this way (descriptions of
317
+ your own modules are not read yet). The build prints the IPs and where each came from, and writes them to
318
+ `build/ips.json`; copy them into `nanopynq.toml` to pin them.
319
+
320
+ The build checks the `ips` (a broken one stops with the rule and the place, e.g. `V9 at ips.pwm.base`),
321
+ computes the design ID from them, builds it into the bitstream (the top's `DESIGN_ID`, handed down to
322
+ `sysinfo`), and writes `dist/`: the bitstream, the metadata file (`.npq`, JSON) and `build.json` (the
323
+ sources with their sha256, the tool versions and the seed). With the same sources and the tool versions
324
+ recorded in `build.json`, the `.fs` and `.npq` come out the same byte for byte.
325
+ Intermediate files and the log of a failed tool go to `build/`. The link (UART, 115200 baud) comes from the
326
+ board. Unknown keys in `nanopynq.toml` are errors.
327
+
328
+ Before synthesis, the build checks the `ips` against your Verilog, with yosys only. It runs on up to 8 cores;
329
+ for the example it takes about 5 s on 8 cores, 8 s on 4, 14 s on 2 and 26 s on one, and it grows with the
330
+ number of registers and the size of the design.
331
+ It puts a test bus master in place of the UART bridge and checks, over the first clocks after configuration:
332
+
333
+ - every register in `ips` has a slave that can answer at its address: there is some value of the inputs for
334
+ which a read does not return `0xdeadbeef` (error if there is none). This is not a proof for all inputs: a
335
+ register that answers only while, say, a button is held passes,
336
+ - no address outside the registers answers, before or after a write to it, inside an IP's range or outside
337
+ all of them. This one is proved for all inputs (error; only a warning inside an IP with `streams`, whose
338
+ data window answers between the registers; an IP with a window but no `streams` gets errors),
339
+ - an `rw` register reads back what was written, masked to its width; an `ro` one does not, and reads 0
340
+ above its width (warnings: an `rw` register may read something else, like an SPI data register).
341
+
342
+ Errors stop the build before synthesis; fix the `ips` or the RTL. They name every register without a slave,
343
+ but only one answering address per IP range (and one outside all ranges), so build again after a fix. A
344
+ failed build leaves none of its files (`<name>.fs`, `<name>.npq`, `build.json`) in `dist/`, so an older design
345
+ cannot be loaded by mistake. Warnings are shown and the build goes on. `build.json` records whether the check
346
+ ran and its number of warnings. The check's files (`design.il`, one log per proof) are in `build/crosscheck/`.
347
+
348
+ The check needs a top that:
349
+
350
+ 1. holds exactly one instance of nanopynq's `uartbone_bridge` (directly or through `base_soc`, not a renamed
351
+ copy), with the slaves on its bus,
352
+ 2. reads `0xdeadbeef` at unmapped addresses (`base_regs` does),
353
+ 3. ends its reset within about `depth` - 10 clocks of configuration; an input pin that feeds the reset must
354
+ be pinned with `[check] inputs`,
355
+ 4. uses no names starting with `npqchk`.
356
+
357
+ A top that breaks 1 or 3 stops with "cannot check"; one that breaks 2 stops with errors about addresses
358
+ that answer. A register that reads `0xdeadbeef` right after reset is reported as missing. It does not see: an `ro` register written wider than it
359
+ is (or narrower, when its high bits stay 0 for the first clocks, like a counter), `wo` registers beyond their
360
+ address, two slaves answering the same address, and what the values mean. A slave that answers only after
361
+ an earlier write is reported as missing. `nanopynq build --no-crosscheck` builds without the check and
362
+ says so; the `.npq` does not record it. The optional `[check]` table sets it up:
363
+
364
+ ```toml
365
+ [check]
366
+ inputs = { btn = 0 } # top inputs held at a value during the check (default: none; others are free)
367
+ depth = 32 # clocks after configuration the check covers (default 32)
368
+ ```
369
+
370
+ `dist/` is a design folder for `Overlay`:
371
+
372
+ ```python
373
+ from nanopynq import Overlay
374
+
375
+ ol = Overlay("examples/tangnano9k_pwm/dist") # programs pwm.fs, checks the design ID
376
+ ol.pwm.write(0x4, 27_000_000) # period: 1 s at 27 MHz
377
+ ol.pwm.write(0x8, 13_500_000) # duty: half of it
378
+ ol.pwm.write(0x0, 1) # enable: LED 5 blinks once a second
379
+ ```
380
+
381
+ The bundled designs are built with `tools/build_base.py`, which writes `base.fs`, `base.npq` and
382
+ `build.json` (also the commit) into `src/nanopynq/data/designs/<board>-<design>`.
383
+ The Tang Nano 9K and Tang Mega 138K Pro base designs are built with the pip tools.
384
+ The Tang Nano 4K base design cannot be rebuilt with the pip tools yet. GW_JTAG on the GW1NS-4
385
+ is in Apicula master (YosysHQ/apicula#578) but not in a released Apicula / nextpnr.
386
+ `tools/build_base.py --board tangnano4k` rebuilds it with Apicula master and a nextpnr chipdb
387
+ regenerated from it (see `tools/p0/gwjtag4k/README.md`).
388
+ The Tang Nano 20K base design also uses GW_JTAG, which released Apicula has for the GW2A-18C:
389
+ `tools/build_base.py --board tangnano20k` rebuilds it with the oss-cad-suite tools (see the script's docstring).
390
+
391
+ ## Designs from FabricGate
392
+
393
+ [FabricGate](https://github.com/harurun78/fabricgate) distributes designs and checks their digests; nanopynq
394
+ reads the `.npq` and checks the design ID as for any other design. A registered design is used in one of two ways.
395
+
396
+ Pull it with the `fabricgate` CLI and pass the folder:
397
+
398
+ ```sh
399
+ fabricgate pull <namespace>/<design>:<version> --platform tangnano9k/nanopynq --output pwm
400
+ ```
401
+
402
+ ```python
403
+ ol = Overlay("pwm") # the folder holds FabricGate's manifest.yaml next to the .fs and .npq; only the .npq is read
404
+ ```
405
+
406
+ Or let `Overlay` pull it, with `pip install "nanopynq[fabricgate]"` (without it, this raises `ImportError`
407
+ saying so):
408
+
409
+ ```python
410
+ ol = Overlay("fabricgate://<namespace>/<design>:<version>", board="tangnano9k")
411
+ ```
412
+
413
+ - `board` is required: it picks the platform (`tangnano9k/nanopynq`).
414
+ - The registry is the one the `fabricgate` CLI uses: `FG_REGISTRY`, else `registry` in
415
+ `~/.fabricgate/config.yaml`, else the production registry.
416
+ - The first call pulls the design into
417
+ `~/.cache/nanopynq/fabricgate/<registry>/<namespace>/<design>/<version>/<board>` (under
418
+ `$XDG_CACHE_HOME` if set), where `<registry>` is the registry URL as a folder name, e.g.
419
+ `registry.fabricgate.dev_api_v1`. Later calls use that folder without asking the registry; delete it to pull
420
+ again.
421
+ - If the registry says the version is yanked or deprecated, the call warns (`UserWarning`) with its reason, also
422
+ when the folder is reused. A version yanked after its first pull is not noticed until the folder is deleted.
423
+ - The extra installs FabricGate 0.7 or later, the first release that reads nanopynq designs carrying a `.npq`.
424
+
425
+ To publish your own design, see `docs/publish_fabricgate.md`.
426
+
427
+ ## License
428
+
429
+ nanopynq is released under the MIT License (`LICENSE`). The example designs under `examples/` carry their own
430
+ MIT `LICENSE`. Files taken from other projects keep their licenses: see `THIRD_PARTY_NOTICES.md` and `licenses/`.