Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Kingst la2016 driver fixup #131

Closed
wants to merge 9 commits into from
Closed

Kingst la2016 driver fixup #131

wants to merge 9 commits into from

Conversation

Planet911
Copy link
Contributor

The sigrok driver for the Kingst LA2016 is currently broken for many use cases, it doesn't work at all for me in PulseView.
This pr fixes a number of driver issues, mainly the fpga spi register addressing, and also adds support for older LA2016 devices.
It also adds support for LA1016 devices (old and new revisions) which have the same hardware as LA2016 devices but use different fpga bitstreams and are limited to 100MHz sampling.
This has all been tested on LA2016 and LA1016 devices, on linux host but I don't envisage any portability issues.

Fix the address of registers within the fpga which are used for contolling
input thresholds, sampling, triggering and bulk transfers. These addresses
were derived from observations of the usb bus and of the internal la2016
spi bus. See wiki for captures of spi bus activity.
There are, in fact, two pwm outputs which must both be configured to
control the dac output which offsets input signals. See the last two
pages of the schematic (on wiki) for details.
The OEM software communicates with device IC U10 to perform challenge-response
authentication, ensuring the device is genuine. We have no knowledge of the
obfuscation or encryption method used. Additionally it is a rolling code, so
the response changes even with same challenge. In any case, we can just ignore
this IC U10, it does not hinder sigrok support. Removing this code from
the initialisation routine. See schematic in wiki for more info on U10.
A magic number within the device eeprom identifies the device
type and hardware revision. Using this number, the correct
fpga bitstream is selected and loaded.
The Kingst LA2016 and LA1016 use the same hardware, just the coding
of authentication IC U10 is different. U10 limits the LA1016 to
using an FPGA bitstream that samples at a maximum of 100MHz. Other
than that, the devices operate identically and the driver just needs
to select the correct bitstream and limit the maximum sample rate
appropriately. This commit adds support for the existing two hardware
revisions of LA1016, which require different bitstreams.
@resonantworks
Copy link

@Planet911 I can't thank you enough for this contribution! For the first time my Kingst LA2016 is working flawlessly with sigrok after getting the device a few months back! I wasted so much time trying various methods and failing, but your branch works without any modification.

To test this, I've:

  1. Extracted the firmware using your matching sigrok-util PR (which I normally had to hack to get to work)
  2. Built this PR to create the library files
  3. Downloaded a nightly version of PulseView (0.5.0-git-3ce5dd9) and extracted the AppImage using --appimage-extract
  4. Overwrote the packaged libs with this PR's build
  5. Ran PulseView from the squashfs-root directory,

Thanks again so much!

@Planet911
Copy link
Contributor Author

@resonantworks That's great, thanks for the positive feedback, hopefully it will help get this pulled into mainline.

@stevenspmd
Copy link

@Planet911 is it possible for someone to kick off a PR build of pulseview for osx that includes these changes? I just picked up a LA1016 and I would like to give this PR a try . I tried building from source but am not familiar with this project and kept running into issues.

@matthew-brown-bluefruit
Copy link

I would also really appreciate a windows build to try it out since that's what I use at work.

I've tried following the MXE build instructions, but the patches are out of date and I couldn't see which version of MXE is being used. @uwehermann

@Planet911
Copy link
Contributor Author

@stevenspmd @matthew-brown-bluefruit
Sorry, I can't help with osx or windows builds. MXE got me confused too.

@matthew-brown-bluefruit
Copy link

@stevenspmd @matthew-brown-bluefruit
Sorry, I can't help with osx or windows builds. MXE got me confused too.

Glad it not just me ;)

@resonantworks
Copy link

resonantworks commented Jun 22, 2021

Managed to build it via MXE, but it unfortunately fails to run properly on Windows. Started PulseView, clicked Run, nothing happens, clicked Stop, hangs, disconnected USB, UI responsive again but no capture. This happens with the both the KingST driver and the WinUSB patched the Zadig supplied with the PulseView installer.

Attached is the log from the PulseView debug build console window.
pulseview-kingst2016-patch.txt

Here's how I got the MXE script to work on Ubuntu 20.04 (WSL):

  • Build requirements
sudo apt-get install git-core gcc g++ make autoconf autoconf-archive automake libtool pkg-config libglib2.0-dev libglibmm-2.4-dev libzip-dev libusb-1.0-0-dev libftdi1-dev libieee1284-3-dev libvisa-dev nettle-dev libavahi-client-dev libhidapi-dev check doxygen python-numpy python-dev python-gi-dev python-setuptools swig default-jdk asciidoctor
  • Clone and patch sigrok-util
cd ~ # Can put this somewhere else, but you'll need to use your path in the MXE patching below..
git clone git://sigrok.org/sigrok-util
cd sigrok-util
git remote add Planet911 [email protected]:Planet911/sigrok-util.git
git fetch Planet911
git merge Planet911/sigrok-fwextract-kingst-la2016-fixup-extraction
  • Set up MXE for x64 (takes bloody ages to finish..)
cd ~ # Needs to be in $HOME for this to work
git clone https://github.com/mxe/mxe.git mxe-git
cd mxe-git
git checkout build-2020-06-06
patch -p1 < ../sigrok-util/cross-compile/mingw/mxe_fixes.patch

sudo apt install autoconf automake autopoint bash bison bzip2 flex g++ g++-multilib gettext git gperf intltool libc6-dev-i386 libgdk-pixbuf2.0-dev libltdl-dev libssl-dev libtool-bin libxml-parser-perl lzip make openssl p7zip-full patch perl python ruby sed unzip wget xz-utils

make MXE_TARGETS=x86_64-w64-mingw32.static.posix MXE_PLUGIN_DIRS=plugins/examples/qt5-freeze gcc glib libzip libusb1 libftdi1 hidapi glibmm qtbase qtimageformats qtsvg qttranslations boost check gendef libieee1284 qtbase_CONFIGURE_OPTS='-no-sql-mysql'
  • Modify the sigrok-util/cross-compile/mingw/sigrok-cross-mingw script
...
TARGET="x86_64"
PARALLEL="-j 8" # Set to your number of cores
DEBUG=1
...
# libsigrok
#$GIT_CLONE $REPO_BASE/libsigrok
git clone [email protected]:sigrokproject/libsigrok.git
cd libsigrok
# Patch in KingST driver fix: Planet911:kingst-la2016-driver-fixup
git remote add Planet911 [email protected]:Planet911/libsigrok.git
git fetch Planet911
git merge --no-edit Planet911/kingst-la2016-driver-fixup
./autogen.sh
...
  • Build the x64 Windows target
cd ~/sigrok-util/cross-compile/mingw
./sigrok-cross-mingw
  • If using WSL, copy built installer out to Windows
cp build_debug_64/pulseview/contrib/pulseview-0.5.0-git-*-installer.exe /mnt/c/temp

Here's the built Windows installer (max attachment size 25M - github madness - unzip each one, then use 7zip to decompress the split archive - ugh!):
pulseview-0.5.0-git-cd38c84-installer.001.zip
pulseview-0.5.0-git-cd38c84-installer.002.zip
pulseview-0.5.0-git-cd38c84-installer.003.zip
pulseview-0.5.0-git-cd38c84-installer.004.zip

@Planet911
Copy link
Contributor Author

@resonantworks I tried your build (from the linked 7z archive) and I see the same thing here. The capture runs correctly but then bulk upload of capture data on EP6IN fails to start. Wireshark shows the kingst driver is writing the upload parameters correctly (over the control endpoint), it is just the EP6IN bulk which stalls. Wireshark doesn't show any bulk in request at that point. To me this smells like a USB endpoint issue, something needed to kickstart the EP6IN transfer on Windows.

@resonantworks
Copy link

@Planet911 Thanks for the update. I've got a 'Zaleae Logic 16' in the office, so I'll see what happens in PulseView on Windows with that later this week when go in.

@resonantworks
Copy link

@Planet911 I got the 'Zaleae Logic 16' to work with the build by using Zadig 2.5 to replace the driver with 'libusb-win32' (fails on 'WinUSB'), but can't seem to change the driver for the KingST LA2016 as it always resets back to 'WinUSB' after running PulseView.

@Planet911
Copy link
Contributor Author

Planet911 commented Jul 13, 2021

@resonantworks Using your excellent 'quick guide' above, I've built the Windows version of PulseView and (with this PR applied) my Kingst LA is now working fine in Win10. It's using the Kingst Windows driver but of course it's just a wrapper around the generic winusb driver. The only difference I can see from your build process is that I did a release build (DEBUG=0 in the sigrok-cross-mingw script). Build script attached, although changes are just as you describe above.
sigrok-cross-mingw.zip

Edit: One minor thing, I also had to 'apt-get install nsis' as it wasn't included above.

Edit2: Agggh, nope, not fixed, the issue seems to still be there intermittently. Hmm.

@resonantworks
Copy link

@Planet911, that's brilliant news! I'd love to see this get mainlined since it's such good hardware for the money (if/when the maintainers come out of their caves one day).

@Planet911
Copy link
Contributor Author

@resonantworks, someone from the sigrok mailing list has a working win64 build of pulseview with this PR's kingst support. It seems that their build of libusb-1.0.a works where mine doesn't, for some as yet unknown reason (dependencies or build environment, to be investigated). Anyway, here is the 64bit build of libusb-1.0.a which gives me a working PulseView on win64. Put it here:
~/mxe-git/usr/x86_64-w64-mingw32.static.posix/lib/libusb-1.0.a
Then try building PulseView again (no need to rebuild mxe).
libusb-1.0.zip

@resonantworks
Copy link

Thanks for the updated @Planet911 ;). Doesn't surprise me at all due to the MXE stuff. I'll try take a look later when I get a free moment.

Comment on lines +44 to +51
/* usb vendor class control requests to the cypress FX2 microcontroller */
#define CMD_EEPROM 0xa2 /* ctrl_in reads, ctrl_out writes */
#define CMD_FPGA_INIT 0x50 /* used before and after FPGA bitstream loading */
#define CMD_FPGA_SPI 0x20 /* access registers in the FPGA over SPI bus, ctrl_in reads, ctrl_out writes */
#define CMD_FPGA_ENABLE 0x10
#define CMD_BULK_RESET 0x38 /* flush FX2 usb endpoint 6 IN fifos */
#define CMD_BULK_START 0x30 /* begin transfer of capture data via usb endpoint 6 IN */
#define CMD_KAUTH 0x60 /* communicate with authentication ic U10, not used */

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think these defines should be sorted by increasing hex values. I also think one space after '#define' instead of a tab is enough. While I have no strong opinion on the latter one, I think the sorting would be good to have because it makes future changes a lot easier.

Comment on lines +62 to +70
#define REG_RUN 0x00 /* read capture status, write capture start */
#define REG_PWM_EN 0x02 /* user pwm channels on/off */
#define REG_CAPT_MODE 0x03 /* set to 0x00 for capture to sdram, 0x01 bypass sdram for streaming */
#define REG_BULK 0x08 /* write start address and number of bytes for capture data bulk upload */
#define REG_SAMPLING 0x10 /* write capture config, read capture data location in sdram */
#define REG_TRIGGER 0x20 /* write level and edge trigger config */
#define REG_THRESHOLD 0x68 /* write two pwm configs to control input threshold dac */
#define REG_PWM1 0x70 /* write config for user pwm1 */
#define REG_PWM2 0x78 /* write config for user pwm2 */

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above. I think one space after '#define' will do.

@wsakernel
Copy link

wsakernel commented Sep 3, 2021

Disclaimer:I don't have this hardware and know nothing about it. Still, I want to support sigrok with code review.
From the high level perspective, the code looks sane and is in real good shape. Lots of sensible comments and easy to grasp additions. It just makes me wonder how this code ever worked in first place? Because the code is in good shape, and we have further users confirming that it works now, I am all for pulling this code into the master branch (well, after my minor request for a better sorting of defines has been applied). Thanks for this work!

Copy link

@wsakernel wsakernel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, let me just approve it. We can fix the sorting incrementally.

@stevenspmd
Copy link

Awesome. I have the LA1016. Is there an osx (or even a linux) build available?

@Planet911
Copy link
Contributor Author

Planet911 commented Sep 3, 2021

@wsakernel Thank you for approving, I'm still around and happy to tidy up the code. Please could you also pull the associated firmware extraction script changes I made, which are required to support these driver changes? See sigrok-util PR here:
sigrokproject/sigrok-util#7

@wsakernel
Copy link

Thanks for the pointer. Done now.

@wsakernel
Copy link

BTW do I get it right that the old code worked because of the old firmware? And this new code requires the new firmware? If so, is it possible to check for the old firmware and suggest an update to the user if it is found?

@Planet911
Copy link
Contributor Author

@wsakernel The manufacturer firmware hasn't changed functionality AFAIK, it's just that with my driver changes there are now four different hardware versions supported, each of which need different FPGA firmware. The update to the extraction code will now extract all four of those FPGA firmwares (it was just extracting one). Also the user had to change the firmware filename to match that expected by the old driver, fixed that too.

@wsakernel
Copy link

I wondered about firmware compatibility because when you remove the CTRL_* defines and replace them with the REG_* defines, there are some different hex values for the names, e.g. for BULK (from 0x10 to 0x08), SAMPLING, TRIGGER, THRESHOLD.
This is why I originally wondered how the code ever worked, assuming the old hex values were wrong. Then, I thought maybe the old value were correct for old firmware. The third option is, of course, me being lost in the details.

@Planet911
Copy link
Contributor Author

Planet911 commented Sep 4, 2021

@wsakernel Yes, I wondered how it ever worked too. It never has for me. I know of one person on the sigrok devel ML who had it working at least partially on sigrok-cli. AFAIK nobody had it working on PulseView. I bought the LA2016 because it was marked as supported by sigrok but I ended up doing quite a bit of work to get it functional.

@abraxa
Copy link
Member

abraxa commented Sep 10, 2021

Thank you all very much! It is always great to see users contribute but since I don't have the hardware in question myself, it's always hard to judge whether the changes are an improvement or not. That's why I appreciate when more people chime in than just the commit author as that allows me to better judge the situation.
I merged the commits as 0084954 and following. Again, thank you all!

@abraxa abraxa closed this Sep 10, 2021
@abraxa
Copy link
Member

abraxa commented Sep 10, 2021

By the way - the Windows and Linux container builds including this change will be available in roughly 24 hours while the OSX builds unfortunately need to be checked by the jenkins admin before they'll be updated again. My apologies.

@Planet911
Copy link
Contributor Author

Planet911 commented Sep 12, 2021

I have just tested the Linux nightly build of PulseView and the Kingst LA1016 and LA2016 are both working, which is great. However, neither of them work with the Windows nightly build. This looks like the libusb issue I mentioned above where I could only make a working Win build of PV using libusb built by someone else. I was hoping it was just my build process but it seems the sigrok jenkins build is the same. I don't know what the issue is and debugging is a PITA due to the cross compilation build process :-(

@abraxa
Copy link
Member

abraxa commented Sep 12, 2021

Thanks for letting me know, I wonder if it's related to sigrokproject/sigrok-util#9 - I'll see if I can provide you a build with this change to let you test it.

@Planet911
Copy link
Contributor Author

Hmm, no, I think the problem is something deeper.

@Planet911
Copy link
Contributor Author

OK, I found the problem. It's related to part of this sigrok mxe patch. Specifically, the lines which enable libusb RAW_IO policy:
++ if (!WinUSBX[sub_api].SetPipePolicy(winusb_handle, endpoint_address,
++ RAW_IO, sizeof(UCHAR), &policy)) {
++ usbi_dbg("failed to enable RAW_IO for endpoint %02X", endpoint_address);
++ }
By commenting out those lines (i.e. not applying that patch to libusb/os/windows_usb.c) my build of PulseView works on windows with the Kingst LA2016. I'll ask about this on the sigrok-devel mailing list.

@NRollo
Copy link

NRollo commented Sep 29, 2021

I have a LA2016 arriving in a couple of days. Any news on the SigRok PV Win10 compatibility with LA2016? I just want to know if it is worth using time on installing SigRok for this device now or if I should wait.

Thanks for your great work!

@stevenspmd
Copy link

stevenspmd commented Sep 29, 2021 via email

@Planet911
Copy link
Contributor Author

Planet911 commented Sep 29, 2021

@NRollo No progress yet. If your super-keen you could compile your own PV for Win and disable the libusb RAW_IO feature. As to a proper solution, RAW_IO will need to remain enabled (it has been in sigrok since 2016 or so) and so changes to the capture read buffer management in the LA2016 libsigrok driver is probably the way forward (ask libusb for 512 bytes at a time).

@NRollo
Copy link

NRollo commented Sep 29, 2021

@Planet911 Thanks for the prompt answer!
I think I would be on quite a long learning curve in order to setup the build environment, let alone making changes to the sigrok code.
But of cause, it could be interesting to know how it all fits together (as a start) - any documentation you could point me to?

@Planet911
Copy link
Contributor Author

@abraxa @wsakernel I have tracked down the issue with the windows build and created a new PR to fix it. It's a tiny PR so hopefully we can get this pulled relatively quickly, please!

@NRollo
Copy link

NRollo commented Oct 2, 2021

@Planet911 Great work!!
I will for sure try it out when it is released.

@abraxa
Copy link
Member

abraxa commented Oct 2, 2021

I merged @Planet911 's PR and updated nightlies should be available in about 9 hours.

@Planet911
Copy link
Contributor Author

@abraxa Thanks for the quick response, the Windows nightly is working now.
For Kingst users, I updated the wiki with a few known issues that are worth being aware of.

@Planet911
Copy link
Contributor Author

@abraxa I've developed open firmware for the fx2 mcu in this analyser. Can you create a new sigrok repository, perhaps named 'sigrok-firmware-kingst-la2016'? I'll then submit a PR where we can track testing and code review.

@Planet911
Copy link
Contributor Author

@abraxa I've developed open firmware for the fx2 mcu in this analyser. The firmware has a similar project structure to sigrok-firmware-fx2lafw but it doesn't really belong in any existing repository. How should I proceed to have this reviewed for inclusion in sigrok? Thanks.

@wsakernel
Copy link

@Planet911 From my experience with this project, such things (new repo) are best sorted out via IRC. Try to reach abraxa there. Or maybe open a new ticket here at libsigrok.

@Planet911
Copy link
Contributor Author

@resonantworks @stevenspmd @NRollo
Sorry to (ab)use this closed PR, but if any users of the LA2016 or LA1016 are listening, I would appreciate some help testing some open firmware for these devices. It's really easy, just replace the oem firmware with this one from my PR

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants