Friday, April 14, 2023

Crkbd keymap from configurator json and using kb2040 microcontroller

 


I wanted to document this for anyone else who's not super familiar with qmk and wants to get a setup working with the kb2040.   Here are the steps I followed:

Design a layout on QMK Configurator

https://config.qmk.fm/#/crkbd/rev1/LAYOUT_split_3x6_3

Export the json, e.g. to ~/Downloads/my_custom_keymap.json

Setup QMK on your computer

sudo apt install python3-pip

pip3 install qmk

qmk setup

And setup the keymap

We copy the default keymap directory and replace keymap.c with one based on our json.

cd ~/qmk_firmware/keyboards/crkbd/keymaps

cp default foo

cd foo

mv keymap.c keymap.c.defaultl

qmk json2c ~/Downloads/my_custom_keymap.json > keymap.c

Then build the firmware and flash it!

The secret sauce here was "CONVERT_TO=kb2040" - I didn't have to modify anything else, it just did what was needed to make this compatible with the kb2040 microcontrollers.

qmk flash -kb crkbd -km foo -e CONVERT_TO=kb2040

Press the boot button on the mcu, plug it into usb, and it should flash.  You can run it again for the second half of the kb, or just drag the .df2 over to it.

Thursday, March 30, 2023

Installing VS Code on Raspberry Pi and Ubuntu

 The aarch64 build of VS Code is available in Raspberry Pi OS repositories to bi installed directly with "apt install code", but the Ubuntu repos only have x86 and x86_64 versions of code.  So we need to manually install code - once we install the .deb package, it adds the Microsoft repo for us to keep it up to date:

Look for and download the latest arm64 package here:
https://packages.microsoft.com/repos/code/pool/main/c/code/

Then install it with dpkg:
sudo dpkg --install ~/Downloads/code*arm64.deb

Afterward, apt will keep it up to date.

Thursday, March 02, 2023

First tests with the new Amp Ripper 4k

I've use the Amp Ripper 3000, from kickstart-design, in previous projects - it is one of the highest quality, most simple to use charger/boost converters available for powering Raspberry Pi and other 5v projects.  It's plug-and-play from charger to battery to Raspberry Pi with enough power to run the Pi 4 and has a low-voltage pin to signal when the battery gets low.  And its one of the few solutions supporting pass-through-charging so it's suitable for a UPS.

The Amp Ripper 4000 (Kickstarter link) builds on the 3k with loads of improvements:

  • Increased 4 amp maximum current supply for more demanding setups.
  • I2C reporting of charging/battery voltage, battery percentage, charge/discharge rates via a MAX17084 chip.
  • Several large VOUT solder connections.
  • Support for a wide range of charging voltages:  5-14v!  Charge from 12v automotive systems, solar, etc.
  • Battery temperature thermistor support.
  • Configurable charging current if you'r adventurous enough to replace a surface mount resistor.
I'll be testing the overall performance of the AR 4k and writing code to query battery values from its i2c.  See below for example code to use in your projects.  Testing goals:

  • Power performance - Does it run an Intel Compute Stick m3? (YES!)
  • Reading battery charge stats via i2c using a pi 4 and python.
  • Reading battery charge stats via i2c using a pi pico and micropython.
  • Verifying battery percentage estimates and high/low cutoff voltages.
This is my test setup - a 12ah (1s6p) li-ion pack.  I'll add a disconnect for the battery when I get some BT2.0 plugs in the mail, but for now it's directly soldered to the AR 4k board.  

Be careful about using plugs in line with your batteries that aren't rated for 5+ amp.  I've had issues in the past  using JT/JXT connectors and voltage drop across them. 

Raw Power Performance

I have an Intel Compute Stick M3 that is my benchmark for any USB 5v supply.  It's finicky and will bootloop if the supply can't keep up so generally it has to be run from a wall wart.  The AR3k couldn't run it without hangs. 

The AR4k is working flawlessly here.  I got a youtube video going in chrome and started playing Hollow Knight in Steam while monitoring with top and no hangs!  I alternated between charging discharging and let it run for a couple hours on battery with no issues.

Reading battery charge stats via i2c  

Using a Pi 4 and Python

Wiring Guide




Example Code For Reading Battery Voltage and Percent

The AR4k uses a MAX17048 Li-Ion fuel gauge chip.  It will need at least one charge/discharge cycle before it correctly reports battery "cell_percent".  I'll update this with more specifics as my current project evolves.  There is an easy to use library from Adafruit for this chip that works well with the AR4k.

This is working on the latest (as of March 2023) Raspberry PI OS 32 bit.  It should be the same for RP OS 64 bit, but you may need different libraries with Ubuntu or other distros.  

Setup:
$ pip3 install adafruit-circuitpython-max1704x i2c-tools
$ sudo usermod -a -G dialout your_acct_name
$ reboot

Verify you can see the i2c device.  You should see a 36 listed for the max17048 chip using i2cdetect:
$ i2cdetect -y 1
...
30: -- -- -- -- -- 36 -- ...
...

If that works, you should be able to query the battery voltage:
$ python
>>> import time
>>> import board
>>> import adafruit_max1704x
>>> i2c = board.I2C()
>>> max17 = adafruit_max1704x.MAX17048(i2c)
>>> time.sleep(1)
>>> print(max17.cell_voltage)
3.54625
>>> print(max17.cell_percent)
35.69921875
>>>

The time.sleep(1) is needed if you put this in a script.  You'll see cell_voltage reported and cell_percent both reported as "0.0" if you query them immediately after the max17= line.

Notes for Ubuntu 22.10

If you're using Ubuntu 22.10 on your Pi, there are a couple extra steps to get started - we need to install the raspberry pi gpio library and add our account to the dialout group for access to the /dev/i2c* devices:

$ apt install python3-pip python3-rpi.gpio
$ sudo usermod -a -G dialout your_acct_name
$ reboot

Enabling Safe-Shutdown on Low Voltage

This is a pretty standard feature intended to power off the pi before the battery is completely dead so that it can be shut down cleanly, avoiding file-system corruption from a hard-power-off.  

The MAX1704x can be set to toggle the INT pin at a configured voltage, or we can use the LBO pin to trigger at 3.3v.  We can trigger a shutdown of the Pi by connecting the pin to a GPIO pin and updating config.txt.  - Additional hardware is needed to actually turn off the PSU, so an update will be made here once I sort that out.

To enable auto shutdown on the pi, edit /boot/firmware/config.txt and add the following line at the bottom:

dtoverlay=gpio-shutdown,gpio_pin=4,active_low=1,gpio_pull=up,debounce=1000

NOTE - I need to verify this.  I'll update in a few days when this is confirmed to be exactly correct.

Implementing a kernel module to make the power supply and battery appear as a laptop battery for the Raspberry Pi

This is work in progress.  There is a