From f92a21f0bd82699bd5b443bee5fb01f341a07359 Mon Sep 17 00:00:00 2001
From: Nate Meyer <nate.devel@gmail.com>
Date: Fri, 16 Aug 2024 10:00:54 -0400
Subject: [PATCH] Add LiteX setup guide to docs.  Update Litex firmware
 submodule

---
 Docs/LiteX.md  | 120 +++++++++++++++++++++++++++++++++++++++++++++++++
 Firmware/LiteX |   2 +-
 README.md      |   8 ++++
 3 files changed, 129 insertions(+), 1 deletion(-)
 create mode 100644 Docs/LiteX.md

diff --git a/Docs/LiteX.md b/Docs/LiteX.md
new file mode 100644
index 00000000..c937ca3a
--- /dev/null
+++ b/Docs/LiteX.md
@@ -0,0 +1,120 @@
+# LiteX Build and Setup Guide
+
+## Gateware
+
+The FPGA gateware for the Thunderscope is supported by the [ThunderScope Litex Repo](https://github.com/EEVengers/thunderscope_litex).
+
+### Build
+
+1. Install Vivado, Python3, and LiteX.
+
+2. Ensure Vivado is in your current path
+
+3. Clone the thunderscope_litex repo and follow the instruction in the README.md to build the gateware and generate the software driver.
+
+4. `./thunderscope.py --variant=<variant> --driver --build`
+
+Available Variants:
+
+| Label   | Description |
+| :-----: | :---------- |
+| `a200t` | Trenz A200T Module |
+| `a100t` | Trenz A100T Module |
+| `a50t`  | Custom Module |
+
+### Flash
+
+1. Connect the JTAG debugger to the Thunderscope
+
+2. `./thunderscope.py --variant=<variant> --flash`
+
+## LitePCIe Driver
+
+### Linux
+
+The Linux driver for the LiteX build is generated by the litex gateware project.
+
+```sh
+$ cd software/kernel
+$ make
+$ sudo ./init.sh
+```
+
+### Windows
+
+The Windows driver for the LiteX build is available from the [LitePCIe Windows Driver Repo](https://github.com/NateMeyer/litepcie_driver_win)
+
+1. Install the prerequisites as described in the driver project's README, including either the Windows WDK and KMDF or the EWDK command line tools.
+
+2. After building the litex gateware, copy the csr and soc header files from the Linux driver to the Windows driver
+
+```sh
+> cp ./thunderscope_litex/software/kernel/csr.h ./litepcie_driver_win/litepciedrv/public_h/csr.h
+> cp ./thunderscope_litex/software/kernel/soc.h ./litepcie_driver_win/litepciedrv/public_h/soc.h
+```
+
+3. Build the LitePCIe Driver project by opening the .vcxproj file in Visual Studio, Or launching the EWDK build environment.
+
+```cmd
+> C:\EWDK\LaunchBuildEnv.cmd
+> msbuild litepciedrv.vcxproj /p:Configuration=Release /p:Platform=x64
+```
+
+The Driver file is found in the `litepciedrv/x64/Release/litepciedrv` folder.
+
+4. To install the driver, verify your PC is in TESTSIGNING mode.  In an administrator command prompt, run the command `Bcdedit.exe -set TESTSIGNING ON` and reboot the system.
+
+    When Windows is in TESTSIGNING mode, ensure the Thunderscope is connected to the PC.  Open the Device Manager and locate the Thunderscope in the "Multifunction Devices" category.  Right-Click the Thunderscope device and select "Update Driver".  Tell Windows to manualy install the driver from the litepciedrv build folder noted above.
+
+## TsLitex Library
+
+The libtslitex library provides control over the Thunderscope input channels and Sample DMA.  Clone the [libtslitex Repo](https://github.com/NateMeyer/libtslitex) [Current latest is the `ts_calibration` branch]
+
+Libtslitex uses the CMake build environment.
+
+1. Create a build folder
+
+```
+> mkdir build && cd build
+```
+
+2. Configure CMake
+
+```
+> cmake ..
+```
+
+3. Build with CMake
+
+```
+> cmake --build . --config=Release
+```
+
+4. There is a test application built to `build/artifacts/example/Release/` and the libraries are found in the `build/Release` folder
+
+## TS.NET
+
+Management of Data buffers and data triggering is provided by the TS.NET project.
+
+1. Clone the [TS.NET Repo](https://github.com/macaba/TS.NET)
+
+2. Build the TS.NET.Engine project using the included build script.
+
+```cmd
+> cd build-scripts
+> powershell -ExecutionPolicy Bypass -File "TS.NET.Engine (win-x64).ps1"
+```
+
+3. Copy the tslitex library file into the TS.NET.Engine build directory
+
+```cmd
+> cp libtslitex/build/Release/tslitex.dll TS.NET/builds/win-x64/TS.NET.Engine/0.1.0/tslitex.dll
+```
+
+4. Change the configured HardwareDriver in the thunderscope.yaml file to LiteX
+
+```yaml
+HardwareDriver: LiteX
+```
+
+You should now be able to run TS.NET.Engine and connect to the Thunderscope.
\ No newline at end of file
diff --git a/Firmware/LiteX b/Firmware/LiteX
index 16df2dd0..0fd27d5b 160000
--- a/Firmware/LiteX
+++ b/Firmware/LiteX
@@ -1 +1 @@
-Subproject commit 16df2dd00c18d12339a02ab383af28648190522f
+Subproject commit 0fd27d5b484345f7099cfb4d1c819a2de5bc5ac3
diff --git a/README.md b/README.md
index aa426b37..302a2691 100644
--- a/README.md
+++ b/README.md
@@ -21,6 +21,8 @@ Catch up with these videos on the project:
 
 ## Quick Start Guide 
 
+### XDMA
+
 These are instructions for the XDMA gateware, which is currently the most stable gateware and driver.
 
 1. Install the XDMA driver for [Linux](https://github.com/EEVengers/ThunderScope/tree/master/Software/xdma_driver_linux) or [Windows](https://github.com/EEVengers/ThunderScope/tree/master/Software/xdma_driver_win_src_2018_2)
@@ -40,6 +42,12 @@ These are instructions for the XDMA gateware, which is currently the most stable
 4. Run TS.NET.Engine and ngscopeclient
 5. Add ThunderScope in ngscopeclient under Add -> Oscilloscope with "thunderscope" Driver, "Twinlan" Transport and "Localhost:5025:5026" for the Path
 
+### LiteX
+
+Follow the instructions in the LiteX guide to build the gateware and software for running LiteX firmware on the Thunderscope.
+
+[LiteX Build and Setup Guide](Docs/LiteX.md)
+
 ## Troubleshooting 
 
 If you are getting under ~1000MB/s throughput on Linux, try turning off VT-d (Intel) or IOMMU/AMD-Vi (AMD) in BIOS or using "intel_iommu=off" in GRUB (Intel)