blob: e0515de57b428692b1658c23e2cba2bb6400a5b4 [file] [log] [blame] [view]
Anthony DiGirolamoe9a3ce62023-10-20 21:06:44 +00001# Gonk
2
3[TOC]
4
5## Getting Started
6
Anthony DiGirolamoed6e6d72024-09-27 20:34:05 +00007> **NOTE:** If you are using a `gonk_tools.zip` bundle follow the instructions in
8> the `//tools` directory:
9> https://pigweed.googlesource.com/gonk/+/refs/heads/main/tools/
10
Anthony DiGirolamoe9a3ce62023-10-20 21:06:44 +0000111. Clone the repo
12
13 ```sh
Eric Holland28b171d2024-06-26 18:30:03 +000014 git clone https://pigweed.googlesource.com/gonk
Anthony DiGirolamoe9a3ce62023-10-20 21:06:44 +000015 ```
16
172. Source `bootstrap.sh` to download all compilers and tooling into the
18 `environment` directory:
19
20 ```sh
21 . ./bootstrap.sh
22 ```
23
24 This should init all git submodules for you.
25
263. From here on the Pigweed environment is activated. You can activate the
27 environment in a new shell without re-running bootstrap by sourcing
28 `activate.sh`
29
30 ```sh
31 . ./activate.sh
32 ```
33
Anthony DiGirolamo87acfa92024-08-02 20:32:54 +000034## Capture ADC samples and plot
35
361. First bootstrap, run `pw build` and flash gonk using DFU.
37
38 ```sh
39 . ./bootstrap.sh
40 ```
41
42 ```sh
43 pw build
44 ```
45
46 Unplug gonk from USB and replug with MODE button held down.
47
48 Run `gonk-flash` on the MCU binary. This uses `pyfu-usb`.
49
50 ```sh
51 gonk-flash ./out/gn/arduino_size_optimized/obj/applications/gonk/gonk.bin
52 ```
53
542. Start capturing ADC samples with:
55
56 ```sh
57 gonk \
Anthony DiGirolamo17c57b42024-08-13 23:30:27 +000058 --bitstream-file DEFAULT \
Anthony DiGirolamo87acfa92024-08-02 20:32:54 +000059 --log-to-stderr
60 ```
61
62 - Press `Enter` to stop or start ADC continuous updates. It will begin automatically on startup.
63 - Press `ctrl-c` to quit.
64
Anthony DiGirolamo17c57b42024-08-13 23:30:27 +0000653. Plot the logs from `gonk-csv-log.txt` with:
Anthony DiGirolamo87acfa92024-08-02 20:32:54 +000066
67 ```sh
Anthony DiGirolamo17c57b42024-08-13 23:30:27 +000068 python tools/gonk_tools/plot.py -i gonk-csv-log.txt -o plot.svg
Anthony DiGirolamo87acfa92024-08-02 20:32:54 +000069 ```
70
71
72## Compilation Details
Anthony DiGirolamoe9a3ce62023-10-20 21:06:44 +000073
74Build for the host and device by running:
75
76```sh
77pw build
78```
79
80This is mostly a shortcut with nice output for running `gn gen out/gn` and
81`ninja -C out/gn`.
82
83The build commands are defined in: `//tools/gonk_tools/build_project.py`.
84
Anthony DiGirolamo87acfa92024-08-02 20:32:54 +000085## Gonk Verilog Build
Anthony DiGirolamo63be5652023-12-18 22:33:30 +000086
87The Verilog build requires the following to be installed on Linux:
88
89```sh
90sudo apt install fpga-icestorm nextpnr-ice40 yosys
91```
92
Anthony DiGirolamo87acfa92024-08-02 20:32:54 +000093Running `pw build` will run the FPGA toolchain if these commands are available on the `$PATH`:
Anthony DiGirolamo63be5652023-12-18 22:33:30 +000094
Anthony DiGirolamo87acfa92024-08-02 20:32:54 +000095- `icepack`
96- `icetime`
97- `nextpnr-ice40`
98- `yosys`
Anthony DiGirolamo63be5652023-12-18 22:33:30 +000099
100The bitstream files will be written with the `.bin` extenson under
Anthony DiGirolamo899f4ee2023-12-20 21:45:02 +0000101`./out/gn/obj/fpga/*/*.bin` along with log output files.
102
103For example:
104
105```sh
106$ ls ./out/gn/obj/fpga/toplevel/
107nextpnr-log.txt
108toplevel.asc
109toplevel.bin
110toplevel.json
111toplevel_timing_report.json
112toplevel_timing_report.txt
113yosys-log.txt
114```
Anthony DiGirolamo63be5652023-12-18 22:33:30 +0000115
Anthony DiGirolamo87acfa92024-08-02 20:32:54 +0000116## Gonk Firmware Build
Anthony DiGirolamo1e343e32023-12-20 00:32:49 +0000117
Anthony DiGirolamo87acfa92024-08-02 20:32:54 +0000118Flash the stm32f7 and launch the `gonk.py` script on a bitstream file.
Anthony DiGirolamo1e343e32023-12-20 00:32:49 +0000119
Anthony DiGirolamoa5970582024-05-07 21:19:13 +0000120### Flash via DFU
Anthony DiGirolamo1e343e32023-12-20 00:32:49 +0000121
Anthony DiGirolamo1e343e32023-12-20 00:32:49 +00001221. Unplug gonk from USB and replug with MODE button held down.
123
Anthony DiGirolamo87acfa92024-08-02 20:32:54 +00001241. Run `gonk-flash` which uses `pyfu-usb`. The `--bin-file` argument is optional.
Anthony DiGirolamo1e343e32023-12-20 00:32:49 +0000125
126 ```sh
Anthony DiGirolamo87acfa92024-08-02 20:32:54 +0000127 gonk-flash --bin-file ./out/gn/arduino_size_optimized/obj/applications/gonk/bin/gonk.elf
Anthony DiGirolamo1e343e32023-12-20 00:32:49 +0000128 ```
129
Anthony DiGirolamocbdd6102024-02-08 15:43:10 +0000130### Alternative: Flash with BlackMagic Probe
Anthony DiGirolamo8c54b052023-12-18 18:36:59 +0000131
Anthony DiGirolamo87acfa92024-08-02 20:32:54 +0000132This is more useful if you are doing firmware debugging.
133
Anthony DiGirolamo8c54b052023-12-18 18:36:59 +0000134```sh
Anthony DiGirolamo87acfa92024-08-02 20:32:54 +0000135./scripts/flash-with-blackmagic-probe.sh ./out/gn/arduino_size_optimized/obj/applications/gonk/bin/gonk.elf
Anthony DiGirolamo8c54b052023-12-18 18:36:59 +0000136```
137
Anthony DiGirolamo7d7ff1a2024-03-05 22:30:07 +0000138## Generating the Gonk Python Bundle
139
Anthony DiGirolamo87acfa92024-08-02 20:32:54 +0000140This is a distibutable python wheel that can be used without the Gonk
141source checkout or compiling anything.
142
Anthony DiGirolamo7d7ff1a2024-03-05 22:30:07 +0000143On Linux with the FPGA toolchain available run:
144
145```sh
146pw build
147```
148
149The zip file containing all the dependencies for the gonk python tooling is located in:
150
151```sh
152out/gn/obj/gonk_bundle.zip
153```
154
155Inside are the Python wheels for `gonk_dist`, `gonk_firmware`, and all
156third_party dependencies.
157
158```
159gonk_bundle
160├── python_wheels
161│ ├── appdirs-1.4.4-py2.py3-none-any.whl
162│ ├── astroid-3.0.1-py3-none-any.whl
163│ ├── ...
164│ ├── gonk_dist-0.0.1+20240305140627-py3-none-any.whl
165│ ├── gonk_firmware-0.0.1+20240305140542-py3-none-any.whl
166│ ├── ...
167│ └── wheel-0.40.0-py3-none-any.whl
168├── requirements.txt
169├── setup.bat
170└── setup.sh
171```
172
Anthony DiGirolamo87acfa92024-08-02 20:32:54 +0000173## Appendix
Anthony DiGirolamo37a38b72023-12-20 22:47:45 +0000174
Anthony DiGirolamo9d3e6b32024-08-13 23:33:11 +0000175### CSV Log Format
176
177#### Fields
178
1791. Host timestamp with format `%Y%m%d %H:%M:%S.%f`
1802. Delta microseconds (elapsed microseconds since the last ADC read) on the STM32 microcontroller side.
1813. 7 Voltage values
1824. 7 Current values
1835. 7 Power values
1846. Comment text
185
186Example separated into multiple lines (with an empty comment field)
187```
18820240813 14:16:35.176433,
189283,
1900.8148437500000001, 0.8892578125, 5.212109375000001, 1.087109375, 0.8904296875000001, 3.3820312500000003, 1.8125,
1910.058447916666666676, 0.031546875, 0.13443125, 0.015902343750000002, 0.0009143518518518518, 0.16081250000000002, 0.004143750000000001,
1920.04762591959635418, 0.02805330505371094, 0.7006703784179689, 0.01728758697509766, 0.0008141660337094908, 0.5438729003906251, 0.007510546875000001,
193
194```
195
196#### GPIO assert events in the CSV
197
198When GPIOs are pulled LOW (to ground) a line in the CSV will appear
199with delta_micros = 0 and empty measurement values. The Comment field will
200contain the GPIO assert log message.
201
202Example:
203
204```
20520240813 14:12:32.930888, 0,
206,,,,,,,
207,,,,,,,
208,,,,,,,
209Header pin assert: 2
210```
211
212
Anthony DiGirolamo108aac32024-08-13 23:23:34 +0000213### Compiling the FPGA Toolchain
214
215#### yosys
216
217See https://github.com/YosysHQ/yosys for more instructions.
218
219On Ubuntu/Debian install the deps:
220```sh
221sudo apt-get install build-essential clang lld bison flex \
222 libreadline-dev gawk tcl-dev libffi-dev git \
223 graphviz xdot pkg-config python3 libboost-system-dev \
224 libboost-python-dev libboost-filesystem-dev zlib1g-dev
225```
226
227Activate the bootstrap environment `. ./bootstraph.sh` then compile yosys.
228
229```sh
230git clone https://github.com/YosysHQ/yosys
231cd yosys
232git submodule update --init
233make config-clang
234make -j8
235```
236
237Install it to a known directory and add it to your `$PATH`:
238
239```sh
240make DESTDIR=$HOME/ice40-fpga-tools install
241export $PATH=$PATH:$HOME/ice40-fpga-tools/usr/local/bin
242```
243
244#### fpga-icestorm
245
246TODO: Document compiling from source.
247
248#### nextpnr-ice40
249
250TODO: Document compiling from source.
251
Anthony DiGirolamo87acfa92024-08-02 20:32:54 +0000252### Updating STM32Cube
Anthony DiGirolamoe9a3ce62023-10-20 21:06:44 +0000253
254Checkout the desired commits in each of these submodules:
255
256```
257third_party/stm32cube_f7/cmsis_core
258third_party/stm32cube_f7/cmsis_device
259third_party/stm32cube_f7/hal_driver
260```
261
262Then run from Gonk root:
263
264```sh
265python -m pw_stm32cube_build gen_file_list third_party/stm32cube_f7
266```
Anthony DiGirolamo8c54b052023-12-18 18:36:59 +0000267
Anthony DiGirolamo87acfa92024-08-02 20:32:54 +0000268### Gonk Pin Maps
Anthony DiGirolamo8c54b052023-12-18 18:36:59 +0000269
Anthony DiGirolamo87acfa92024-08-02 20:32:54 +0000270#### Misc
Anthony DiGirolamo8c54b052023-12-18 18:36:59 +0000271
272| Net | STM32 Pin | STM32 Function | Function |
Anthony DiGirolamo6edbdf62024-01-08 22:06:17 +0000273|--------|-----------|----------------|----------|
Anthony DiGirolamo8c54b052023-12-18 18:36:59 +0000274| STATUS | PB13 | GPIO_Output | STAT LED |
275
Anthony DiGirolamo9d3e6b32024-08-13 23:33:11 +0000276### Top 6 pin header (0.1inch pitch 2x3)
277
278Visual header pin positions:
279
280```
281 +-------------
282+------+--------+-----+ |
283| 2 | Ground | 6 | | USB-C
284+------+--------+-----+ | Port
285| 1 | 3 | VCC | |
286+------+--------+-----+ +-------------
287```
288
289| Header Pin | Net | STM32 Pin |
290|------------|-------------|-----------|
291| 1 | SPI_HDR_IO3 | PB0 |
292| 2 | SPI_HDR_IO1 | PA2 |
293| 3 | SPI_HDR_IO2 | PA3 |
294| 4 | GND | |
295| 5 | VCC_GONK | |
296| 6 | SPI_HDR_IO0 | PA1 |
297
Anthony DiGirolamo87acfa92024-08-02 20:32:54 +0000298### SPI Flash Connection
Anthony DiGirolamo8c54b052023-12-18 18:36:59 +0000299
Anthony DiGirolamo6edbdf62024-01-08 22:06:17 +0000300| Net | FPGA IO# | STM32 Pin | STM32 Function | Flash Pin |
301|--------------|----------|-----------|----------------|------------|
302| ICE_SPI_SS | 71 | PD2 | GPIO_Output | S# |
303| ICE_SPI_MISO | 68 | PB4 | SPI1_MISO | DQ1 |
304| ICE_SPI_MOSI | 67 | PB5 | SPI1_MOSI | DQ0 |
305| ICE_SPI_SCK | 70 | PB3 | SPI1_SCK | C |
306| FLASH_HOLD | 63 | PC11 | GPIO_Output | HOLD#/DQ3 |
307| FLASH_WP | 64 | PC12 | GPIO_Output | W#/VPP/DQ2 |
Anthony DiGirolamo8c54b052023-12-18 18:36:59 +0000308
Anthony DiGirolamo87acfa92024-08-02 20:32:54 +0000309### FPGA Connection
Anthony DiGirolamo8c54b052023-12-18 18:36:59 +0000310
Anthony DiGirolamobf6f6852024-01-25 22:51:27 +0000311| Net | Function | FPGA IO# | STM32 Pin | STM32 Function | Notes |
312|-------------------|----------|----------|-----------|----------------|---------------------|
313| FPGA_IO_SPARE_0_2 | rst_i | 135 | PA0 | GPIO_Output | Active high |
314| FPGA_IO_SPARE_0_0 | mode_i | 137 | PB11 | GPIO_Output | FPGA operation mode |
315| DSPI_CS | valid_o | 75 | PB6 | GPIO_Output | Data/Transfer Valid |
316| FPGA_IO_SPARE_1_1 | miso_i | 101 | PC2 | SPI1_MISO | |
317| FPGA_IO_SPARE_1_0 | mosi_i | 99 | PC3 | SPI1_MOSI | |
318| I2C_O_SDA | cs_n | 79 | PB9 | SPI1_NSS | |
319| FPGA_IO_SPARE_0_1 | sclk_i | 136 | PB10 | SPI1_SCK | |
320| FPGA_IO_SPARE_2_0 | | 49 | PA7 | | |
321| FPGA_IO_SPARE_2_1 | | 48 | PA6 | | |
322| FPGA_IO_SPARE_2_2 | | 47 | PA5 | | |
323| FPGA_IO_SPARE_2_3 | | 45 | PA4 | | |