Anthony DiGirolamo | e9a3ce6 | 2023-10-20 21:06:44 +0000 | [diff] [blame] | 1 | # Gonk |
| 2 | |
| 3 | [TOC] |
| 4 | |
| 5 | ## Getting Started |
| 6 | |
Anthony DiGirolamo | ed6e6d7 | 2024-09-27 20:34:05 +0000 | [diff] [blame] | 7 | > **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 DiGirolamo | e9a3ce6 | 2023-10-20 21:06:44 +0000 | [diff] [blame] | 11 | 1. Clone the repo |
| 12 | |
| 13 | ```sh |
Eric Holland | 28b171d | 2024-06-26 18:30:03 +0000 | [diff] [blame] | 14 | git clone https://pigweed.googlesource.com/gonk |
Anthony DiGirolamo | e9a3ce6 | 2023-10-20 21:06:44 +0000 | [diff] [blame] | 15 | ``` |
| 16 | |
| 17 | 2. 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 | |
| 26 | 3. 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 DiGirolamo | 87acfa9 | 2024-08-02 20:32:54 +0000 | [diff] [blame] | 34 | ## Capture ADC samples and plot |
| 35 | |
| 36 | 1. 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 | |
| 54 | 2. Start capturing ADC samples with: |
| 55 | |
| 56 | ```sh |
| 57 | gonk \ |
Anthony DiGirolamo | 17c57b4 | 2024-08-13 23:30:27 +0000 | [diff] [blame] | 58 | --bitstream-file DEFAULT \ |
Anthony DiGirolamo | 87acfa9 | 2024-08-02 20:32:54 +0000 | [diff] [blame] | 59 | --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 DiGirolamo | 17c57b4 | 2024-08-13 23:30:27 +0000 | [diff] [blame] | 65 | 3. Plot the logs from `gonk-csv-log.txt` with: |
Anthony DiGirolamo | 87acfa9 | 2024-08-02 20:32:54 +0000 | [diff] [blame] | 66 | |
| 67 | ```sh |
Anthony DiGirolamo | 17c57b4 | 2024-08-13 23:30:27 +0000 | [diff] [blame] | 68 | python tools/gonk_tools/plot.py -i gonk-csv-log.txt -o plot.svg |
Anthony DiGirolamo | 87acfa9 | 2024-08-02 20:32:54 +0000 | [diff] [blame] | 69 | ``` |
| 70 | |
| 71 | |
| 72 | ## Compilation Details |
Anthony DiGirolamo | e9a3ce6 | 2023-10-20 21:06:44 +0000 | [diff] [blame] | 73 | |
| 74 | Build for the host and device by running: |
| 75 | |
| 76 | ```sh |
| 77 | pw build |
| 78 | ``` |
| 79 | |
| 80 | This is mostly a shortcut with nice output for running `gn gen out/gn` and |
| 81 | `ninja -C out/gn`. |
| 82 | |
| 83 | The build commands are defined in: `//tools/gonk_tools/build_project.py`. |
| 84 | |
Anthony DiGirolamo | 87acfa9 | 2024-08-02 20:32:54 +0000 | [diff] [blame] | 85 | ## Gonk Verilog Build |
Anthony DiGirolamo | 63be565 | 2023-12-18 22:33:30 +0000 | [diff] [blame] | 86 | |
| 87 | The Verilog build requires the following to be installed on Linux: |
| 88 | |
| 89 | ```sh |
| 90 | sudo apt install fpga-icestorm nextpnr-ice40 yosys |
| 91 | ``` |
| 92 | |
Anthony DiGirolamo | 87acfa9 | 2024-08-02 20:32:54 +0000 | [diff] [blame] | 93 | Running `pw build` will run the FPGA toolchain if these commands are available on the `$PATH`: |
Anthony DiGirolamo | 63be565 | 2023-12-18 22:33:30 +0000 | [diff] [blame] | 94 | |
Anthony DiGirolamo | 87acfa9 | 2024-08-02 20:32:54 +0000 | [diff] [blame] | 95 | - `icepack` |
| 96 | - `icetime` |
| 97 | - `nextpnr-ice40` |
| 98 | - `yosys` |
Anthony DiGirolamo | 63be565 | 2023-12-18 22:33:30 +0000 | [diff] [blame] | 99 | |
| 100 | The bitstream files will be written with the `.bin` extenson under |
Anthony DiGirolamo | 899f4ee | 2023-12-20 21:45:02 +0000 | [diff] [blame] | 101 | `./out/gn/obj/fpga/*/*.bin` along with log output files. |
| 102 | |
| 103 | For example: |
| 104 | |
| 105 | ```sh |
| 106 | $ ls ./out/gn/obj/fpga/toplevel/ |
| 107 | nextpnr-log.txt |
| 108 | toplevel.asc |
| 109 | toplevel.bin |
| 110 | toplevel.json |
| 111 | toplevel_timing_report.json |
| 112 | toplevel_timing_report.txt |
| 113 | yosys-log.txt |
| 114 | ``` |
Anthony DiGirolamo | 63be565 | 2023-12-18 22:33:30 +0000 | [diff] [blame] | 115 | |
Anthony DiGirolamo | 87acfa9 | 2024-08-02 20:32:54 +0000 | [diff] [blame] | 116 | ## Gonk Firmware Build |
Anthony DiGirolamo | 1e343e3 | 2023-12-20 00:32:49 +0000 | [diff] [blame] | 117 | |
Anthony DiGirolamo | 87acfa9 | 2024-08-02 20:32:54 +0000 | [diff] [blame] | 118 | Flash the stm32f7 and launch the `gonk.py` script on a bitstream file. |
Anthony DiGirolamo | 1e343e3 | 2023-12-20 00:32:49 +0000 | [diff] [blame] | 119 | |
Anthony DiGirolamo | a597058 | 2024-05-07 21:19:13 +0000 | [diff] [blame] | 120 | ### Flash via DFU |
Anthony DiGirolamo | 1e343e3 | 2023-12-20 00:32:49 +0000 | [diff] [blame] | 121 | |
Anthony DiGirolamo | 1e343e3 | 2023-12-20 00:32:49 +0000 | [diff] [blame] | 122 | 1. Unplug gonk from USB and replug with MODE button held down. |
| 123 | |
Anthony DiGirolamo | 87acfa9 | 2024-08-02 20:32:54 +0000 | [diff] [blame] | 124 | 1. Run `gonk-flash` which uses `pyfu-usb`. The `--bin-file` argument is optional. |
Anthony DiGirolamo | 1e343e3 | 2023-12-20 00:32:49 +0000 | [diff] [blame] | 125 | |
| 126 | ```sh |
Anthony DiGirolamo | 87acfa9 | 2024-08-02 20:32:54 +0000 | [diff] [blame] | 127 | gonk-flash --bin-file ./out/gn/arduino_size_optimized/obj/applications/gonk/bin/gonk.elf |
Anthony DiGirolamo | 1e343e3 | 2023-12-20 00:32:49 +0000 | [diff] [blame] | 128 | ``` |
| 129 | |
Anthony DiGirolamo | cbdd610 | 2024-02-08 15:43:10 +0000 | [diff] [blame] | 130 | ### Alternative: Flash with BlackMagic Probe |
Anthony DiGirolamo | 8c54b05 | 2023-12-18 18:36:59 +0000 | [diff] [blame] | 131 | |
Anthony DiGirolamo | 87acfa9 | 2024-08-02 20:32:54 +0000 | [diff] [blame] | 132 | This is more useful if you are doing firmware debugging. |
| 133 | |
Anthony DiGirolamo | 8c54b05 | 2023-12-18 18:36:59 +0000 | [diff] [blame] | 134 | ```sh |
Anthony DiGirolamo | 87acfa9 | 2024-08-02 20:32:54 +0000 | [diff] [blame] | 135 | ./scripts/flash-with-blackmagic-probe.sh ./out/gn/arduino_size_optimized/obj/applications/gonk/bin/gonk.elf |
Anthony DiGirolamo | 8c54b05 | 2023-12-18 18:36:59 +0000 | [diff] [blame] | 136 | ``` |
| 137 | |
Anthony DiGirolamo | 7d7ff1a | 2024-03-05 22:30:07 +0000 | [diff] [blame] | 138 | ## Generating the Gonk Python Bundle |
| 139 | |
Anthony DiGirolamo | 87acfa9 | 2024-08-02 20:32:54 +0000 | [diff] [blame] | 140 | This is a distibutable python wheel that can be used without the Gonk |
| 141 | source checkout or compiling anything. |
| 142 | |
Anthony DiGirolamo | 7d7ff1a | 2024-03-05 22:30:07 +0000 | [diff] [blame] | 143 | On Linux with the FPGA toolchain available run: |
| 144 | |
| 145 | ```sh |
| 146 | pw build |
| 147 | ``` |
| 148 | |
| 149 | The zip file containing all the dependencies for the gonk python tooling is located in: |
| 150 | |
| 151 | ```sh |
| 152 | out/gn/obj/gonk_bundle.zip |
| 153 | ``` |
| 154 | |
| 155 | Inside are the Python wheels for `gonk_dist`, `gonk_firmware`, and all |
| 156 | third_party dependencies. |
| 157 | |
| 158 | ``` |
| 159 | gonk_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 DiGirolamo | 87acfa9 | 2024-08-02 20:32:54 +0000 | [diff] [blame] | 173 | ## Appendix |
Anthony DiGirolamo | 37a38b7 | 2023-12-20 22:47:45 +0000 | [diff] [blame] | 174 | |
Anthony DiGirolamo | 9d3e6b3 | 2024-08-13 23:33:11 +0000 | [diff] [blame] | 175 | ### CSV Log Format |
| 176 | |
| 177 | #### Fields |
| 178 | |
| 179 | 1. Host timestamp with format `%Y%m%d %H:%M:%S.%f` |
| 180 | 2. Delta microseconds (elapsed microseconds since the last ADC read) on the STM32 microcontroller side. |
| 181 | 3. 7 Voltage values |
| 182 | 4. 7 Current values |
| 183 | 5. 7 Power values |
| 184 | 6. Comment text |
| 185 | |
| 186 | Example separated into multiple lines (with an empty comment field) |
| 187 | ``` |
| 188 | 20240813 14:16:35.176433, |
| 189 | 283, |
| 190 | 0.8148437500000001, 0.8892578125, 5.212109375000001, 1.087109375, 0.8904296875000001, 3.3820312500000003, 1.8125, |
| 191 | 0.058447916666666676, 0.031546875, 0.13443125, 0.015902343750000002, 0.0009143518518518518, 0.16081250000000002, 0.004143750000000001, |
| 192 | 0.04762591959635418, 0.02805330505371094, 0.7006703784179689, 0.01728758697509766, 0.0008141660337094908, 0.5438729003906251, 0.007510546875000001, |
| 193 | |
| 194 | ``` |
| 195 | |
| 196 | #### GPIO assert events in the CSV |
| 197 | |
| 198 | When GPIOs are pulled LOW (to ground) a line in the CSV will appear |
| 199 | with delta_micros = 0 and empty measurement values. The Comment field will |
| 200 | contain the GPIO assert log message. |
| 201 | |
| 202 | Example: |
| 203 | |
| 204 | ``` |
| 205 | 20240813 14:12:32.930888, 0, |
| 206 | ,,,,,,, |
| 207 | ,,,,,,, |
| 208 | ,,,,,,, |
| 209 | Header pin assert: 2 |
| 210 | ``` |
| 211 | |
| 212 | |
Anthony DiGirolamo | 108aac3 | 2024-08-13 23:23:34 +0000 | [diff] [blame] | 213 | ### Compiling the FPGA Toolchain |
| 214 | |
| 215 | #### yosys |
| 216 | |
| 217 | See https://github.com/YosysHQ/yosys for more instructions. |
| 218 | |
| 219 | On Ubuntu/Debian install the deps: |
| 220 | ```sh |
| 221 | sudo 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 | |
| 227 | Activate the bootstrap environment `. ./bootstraph.sh` then compile yosys. |
| 228 | |
| 229 | ```sh |
| 230 | git clone https://github.com/YosysHQ/yosys |
| 231 | cd yosys |
| 232 | git submodule update --init |
| 233 | make config-clang |
| 234 | make -j8 |
| 235 | ``` |
| 236 | |
| 237 | Install it to a known directory and add it to your `$PATH`: |
| 238 | |
| 239 | ```sh |
| 240 | make DESTDIR=$HOME/ice40-fpga-tools install |
| 241 | export $PATH=$PATH:$HOME/ice40-fpga-tools/usr/local/bin |
| 242 | ``` |
| 243 | |
| 244 | #### fpga-icestorm |
| 245 | |
| 246 | TODO: Document compiling from source. |
| 247 | |
| 248 | #### nextpnr-ice40 |
| 249 | |
| 250 | TODO: Document compiling from source. |
| 251 | |
Anthony DiGirolamo | 87acfa9 | 2024-08-02 20:32:54 +0000 | [diff] [blame] | 252 | ### Updating STM32Cube |
Anthony DiGirolamo | e9a3ce6 | 2023-10-20 21:06:44 +0000 | [diff] [blame] | 253 | |
| 254 | Checkout the desired commits in each of these submodules: |
| 255 | |
| 256 | ``` |
| 257 | third_party/stm32cube_f7/cmsis_core |
| 258 | third_party/stm32cube_f7/cmsis_device |
| 259 | third_party/stm32cube_f7/hal_driver |
| 260 | ``` |
| 261 | |
| 262 | Then run from Gonk root: |
| 263 | |
| 264 | ```sh |
| 265 | python -m pw_stm32cube_build gen_file_list third_party/stm32cube_f7 |
| 266 | ``` |
Anthony DiGirolamo | 8c54b05 | 2023-12-18 18:36:59 +0000 | [diff] [blame] | 267 | |
Anthony DiGirolamo | 87acfa9 | 2024-08-02 20:32:54 +0000 | [diff] [blame] | 268 | ### Gonk Pin Maps |
Anthony DiGirolamo | 8c54b05 | 2023-12-18 18:36:59 +0000 | [diff] [blame] | 269 | |
Anthony DiGirolamo | 87acfa9 | 2024-08-02 20:32:54 +0000 | [diff] [blame] | 270 | #### Misc |
Anthony DiGirolamo | 8c54b05 | 2023-12-18 18:36:59 +0000 | [diff] [blame] | 271 | |
| 272 | | Net | STM32 Pin | STM32 Function | Function | |
Anthony DiGirolamo | 6edbdf6 | 2024-01-08 22:06:17 +0000 | [diff] [blame] | 273 | |--------|-----------|----------------|----------| |
Anthony DiGirolamo | 8c54b05 | 2023-12-18 18:36:59 +0000 | [diff] [blame] | 274 | | STATUS | PB13 | GPIO_Output | STAT LED | |
| 275 | |
Anthony DiGirolamo | 9d3e6b3 | 2024-08-13 23:33:11 +0000 | [diff] [blame] | 276 | ### Top 6 pin header (0.1inch pitch 2x3) |
| 277 | |
| 278 | Visual 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 DiGirolamo | 87acfa9 | 2024-08-02 20:32:54 +0000 | [diff] [blame] | 298 | ### SPI Flash Connection |
Anthony DiGirolamo | 8c54b05 | 2023-12-18 18:36:59 +0000 | [diff] [blame] | 299 | |
Anthony DiGirolamo | 6edbdf6 | 2024-01-08 22:06:17 +0000 | [diff] [blame] | 300 | | 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 DiGirolamo | 8c54b05 | 2023-12-18 18:36:59 +0000 | [diff] [blame] | 308 | |
Anthony DiGirolamo | 87acfa9 | 2024-08-02 20:32:54 +0000 | [diff] [blame] | 309 | ### FPGA Connection |
Anthony DiGirolamo | 8c54b05 | 2023-12-18 18:36:59 +0000 | [diff] [blame] | 310 | |
Anthony DiGirolamo | bf6f685 | 2024-01-25 22:51:27 +0000 | [diff] [blame] | 311 | | 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 | | | |