Close

[C] Building CircuitPython

A project log for WK-50 Trackball Keyboard

Reverse-engineering a bat-shaped, hot-swappable keyboard with a 38mm trackball and RGB-backlit encoder.

kelvinakelvinA 08/25/2024 at 12:410 Comments

Context

So I found the below video which incentivised me to try porting KMK instead of VIAL as I originally intended. It's a configurator that was unveiled to the public on the last day of May called pog.

I haven't looked into pog but it sounds like it's VIAL with more of the benefits and less of the drawbacks, so understandably, now that I've got the power of the RP2040 instead of the ProMicro, I wanted to try it first. I also heard that KMK was recommended for handwired boards and tinkering, so I assumed it would be a good fit for a keyboard I'm trying to reverse engineer.

Well, the first step is to install CircuitPython. Okay... just got to download that and-- oh, I need to install it for a specific board? There's even other keyboards here:

I'm going to skip the pitfalls (such as using "git submodule update --tinit --recursive" and then having to backtrack when I saw it was installing things I'd never need) and have a more smooth-sailing-sounding, step-by-step tutorial. Part of the reason is because, on the CircuitPython wiki at the time of writing:

It's possible, but tricky, to build in other environments such as CygWin or MinGW: we may cover how to use these in the future.

Which made me feel kind of salty because I had just finished setting up the board pins (before I realised I had to build CircuitPython first) and saw this screenshot:

So it's obviously and totally possible! 

I also want to avoid using WSL. Sounds like mounting and copying files from the Ubuntu to Windows side and back again. (Steve Jobs' opinion of the stylus comes to mind).

Circuitpython on MSYS2

It turns out that the install is kind of like the days when people said "there's an app for that", but here it's an MSYS package.

Forked and then cloned CircuitPython. I put it in a C:\mcu folder, thus my repo is in C:\mcu\circuitpython.

Before opening MSYS, navigate to C:\msys64, open mingw64.ini and uncomment

MSYS2_PATH_TYPE=inherit

so that MSYS can run Git for Windows (assuming git is present in the system PATH); I was getting a FileNotFoundError when trying to get submodules after the setup. The alternative probably is installing git via pacman, though I hear that it runs slower. I know the QMK MSYS has it's own version of git installed, so it's likely performant enough.

Open MSYS2 MINGw64. I followed the manual setup list.

Installed make, cmake and gettext.

pacman -S make
pacman -S cmake
pacman -S gettext

Installed python3-pip and make a virtual python environment. 

pacman -S python3-pip
python -m venv .py
source .py/bin/activate
python -m pip install --upgrade pip

Installed rust, as I got an error about it will occur when trying to run requirements-doc.txt later.

pacman -S mingw-w64-x86_64-rust

I tried to run requirements-dev.txt and didn't work fully. I found this when searching in the repo:

# requirements_dev.txt doesn't install on windows. (with msys2 python)

Looking at the error log and seeing as it stalled when trying to install cryptography, I navigated to C:\mcu\circuitpython opened requirements-dev.txt and commented it out. 

# for mbedtls certificate store
# version limit due to espressif
# cryptography<36.1,>=2.1.4

Instead, I installed its MSYS package. (I assumed I don't need to worry about the version for an RP2040).

pacman -S mingw-w64-x86_64-python-cryptography

Installed pip dependencies.

cd /c/mcu/circuitpython
pip3 install --upgrade -r requirements-dev.txt
pip3 install --upgrade -r requirements-doc.txt

Installed the ARM gcc toolchain.

pacman -S mingw-w64-x86_64-arm-none-eabi-gcc

Now I could get the submodules for the RP2040:

cd ports/raspberrypi
make fetch-port-submodules

Since I've never used it before, I had to install pre-commit.

pip install pre-commit

Then I could install it into the circuitpython folder

cd ../..
pre-commit install

Yet things were still broken

I was getting a "Cannot determine version" error for either of the following commands:

make -C mpy-cross

cd ports/raspberrypi
make BOARD=raspberry_pi_pico

 I asked in the Adafruit discord and it was suggested to run

python -m pdb py/makeversionhdr.py test_file.h

Which now meant that I had to start learning the python debugger in real time. I used this tutorial to learn my way around. With this, I was able to find a clue:

I added "CalledProcessError" into git_tag, which was originally "".

I then changed this to "9.1.2", but then I got a different error:

At least I know where the trickiness lies now.

Admitting defeat and setting up Circuitpython via WSL2

Installing (Ubuntu 22.04) was kind-of straightforward, just following links and inputting commands.

The CircuitPython docs didn't immediately mention how to actually go about installing the ARM toolchain and so I used wget to download the .tar.xz i needed and then install using this tutorial. It was only after successfully doing that when I saw the CircuitPython method, under the Cortex-A information.

Even though I don't think anything would stop me, I still made a python environment and added it into .bashrc via VS Code. I didn't know Microsoft had VS Code integration, which certainly makes WSL easier to work with.

I had a requirements-doc.txt error that was unrelated to having rust or not, and I just ignored it.

And the raspberry_pi_pico firmware compiled fine.

Configuring the WK-50

I grabbed the USB IDs from Device Manager and then named the pins, remembering that I'm looking at the board from the underside and that the U5 transistor column is actually Col13 (starting from Col0).

I then went back to look at other backlit keyboards and copied their neopixel reset code. I also changed the pin name from ARGB to NEOPIXEL to keep consistency.

Finally, to get it out of WSL:
cp build-wk-50/firmware.uf2 /mnt/c/mcu

Discussions