Recently I acquired a small macropad to start my keebin adventures. The hardware looks very solid, but the software is quite abysmal. Windows only, and numerous sources indicate there is malware in it as well. So as any reader here would do, I opened it up and to my delight there was a CH552 running the show, exactly the MCU I've been eyeing for a while now to find a project for!
This project will detail my findings getting to know this chip, and has the goal to end up with a firmware that provides two things:
1) Presents itself as a HID keyboard to the PC connected to it, with all buttons active (either hardcoded, or preferably configurable at runtime)
2) Provides UART access over USB to configure the pad
I don't know if both goals are possible simultaneous, further study will show.
The linked github repository now has a very early version of the firmware. It should be compiled with ch55xduino, just like the other code snippets in the other logs. It is not possible yet to configure which keys are send, it his hardcoded (but straightforward to change) like this:
Left key: '1'
Middle key: '2'
Right key: '3'
Press knob: 's'
Turn knob clockwise: 'r'
Turn knob counterclockwise: 'l'
Next up is to make this easily configurable, without reprogramming the firmware. I'm planning to have the values on the internal eeprom, with an easy way to change them.
A couple things that came up when creating this:
The USB HID code disables the automatic jump-to-bootloader in ch55xduino, so every time you want to reflash the ch552, you'll have to open up the keypad and connect the testpads connected to P3.6.
The ch55xduino IDE changed the bootloader access method from P1.5 to GND, to P3.6 to 3v3. This is unfortunate, because programming the chip using P1.5 would be possible without opening up the pad since the right key is connected to the pin adjacent to P1.5, and pulls that to GND when pressed.
Ch55xduino has examples where you can use both USB CDC and USB HID together (comport + keyboard), but that does not seem to work on my ch552. Only the HID shows up when I connect it to my linux machine.
The jump-to-bootloader function does not seem to work, which is odd because I use exactly the same code as the USB CDC of ch55xduino, which does work properly also on my chip. So more research is needed here.
Bootloader access is also needed to write to the EEPROM, which will be used to reconfigure which characters are returned on keypress.
Debounce is not yet implemented, although I did not notice any bouncing yet.
The rotary encoder is quite straightforward to read out, the internet is full of examples. After some tryouts the pins were found to be connected to P3.0 and P3.1, and the switch that can be activated by pressing the knob is on P3.3. The two outputs of the encoder just go high one after another, depending on which direction the knob is turned:
There is no debouncing in the code above, so you might get some extra readings when pressing the knob.
This means the whole pad is figured out now (in record time, at least for me), next steps will be to write the USB keyboard code. And some nice way to configure the keys, LEDs and encoder without having to reprogram the ch552 with the Arduino IDE. Onward!
So that was quick! Hot on the heels of the key mapping to the pins, it turns out that the RGB LEDs are WS2812 connected to P3.4, and CH55xduino has some example code for that already!
#include<WS2812.h>#define NUM_LEDS 3#define COLOR_PER_LEDS 3#define NUM_BYTES (NUM_LEDS*COLOR_PER_LEDS)
__xdata uint8_t ledData[NUM_BYTES];
voidsetup(){
pinMode(34, OUTPUT);
}
voidloop(){
for (uint8_t i = 0; i < NUM_LEDS; i++) {
set_pixel_for_GRB_LED(ledData, i, 255, 0, 0);
neopixel_show_P3_4(ledData, NUM_BYTES);
delay(100);
}
for (uint8_t i = 0; i < NUM_LEDS; i++) {
set_pixel_for_GRB_LED(ledData, i, 0, 255, 0);
neopixel_show_P3_4(ledData, NUM_BYTES);
delay(100);
}
for (uint8_t i = 0; i < NUM_LEDS; i++) {
set_pixel_for_GRB_LED(ledData, i, 0, 0, 255);
neopixel_show_P3_4(ledData, NUM_BYTES);
delay(100);
}
}
So only the rotary knob is left to figure out, and after that the HID stuff (but CH55xduino has plenty of examples on that). Pins left are P1.4 P1.5 P3.0 P3.1 P3.2 and P3.3.
This will be my quickest project ever by a long shot.
After a lot of headaches why I couldn't get to the bootloader, you can see in the previous log I finally succeeded. So next step: get some code running on it.
As I did not want to fiddle with some clip all the time, I decided to go with the Arduino port by Deqing:
Not only is it a breeze to upload new firmware with this, it also comes with a ton of example code! The only downside I can think of is that it takes up quite a chunk of available space, but since this macropad should only monitor the buttons, and light the LEDs, I guess it will be fine.
In my great wisdom I decided to screw the whole thing together without tracing what connects to which pins, so that will be trial and error. For the next log. I already trial-and-errored the 3 keys:
Connecting the pad to a PC has it show up in lsusb like this:
ID 1189:8890AcerCommunications & Multimedia
The keyboard ships with Windows software to do a first time programming of the buttons (links added at the end of this log), after which it should be usable on any device (I did not test this though, I opted for first opening it up).
After unscrewing the bottom which is very hacker friendly, it showed its internals which is just a CH552 and some passives + USB-C connector. The board looks very well assembled with the MCU easily accessible for reprogramming.
To reprogram the chip there are several flashers available, I picked isp55e0 from the AUR. I've read somewhere that the chip should be in DFU mode, and to achieve that I have to boot it while holding D+ at 3v3, which means connecting pin 12 and 16 together. And this board has nice test pads placed next to each other for this! So I guess these pads are there for exactly that reason.
On the other hand, connecting USB while holding D+ at 3v3 sounds like a bad idea to me, so I did not try that yet. I'll do further research on how to flash them first, and report on this in a new log.
EDIT: actually, CH55xduino https://hackaday.io/project/172143/instructions indicates it is really as easy as connecting D+ to Vcc (5V in their case), and then connect USB. Let's try! and expect a new log soon.
EDIT2: so the super easy way of getting into the bootloader (D+ to 3v3 using the pads) is not working, probably because the USB is in the way. Luckily, The Rabid Inventor wrote that there is another way, pull pin 3 to ground, and that works! I can access the bootloader now, and reprogram the chip. Woohoo!
i have the 6 button non-rgb and the 6 button 3 layer bluetooth version of these macropads. both use a version of the ch552 and the same terrible software to program it.
i've had both apart to do the same preliminary exploration and dug a little into the same idea of seeing if i could get something - anything - better than just keypresses and more toward real macro's or even just binding to launching a small python script using something like F13-24 or other usually unbound available hid standard keys and additional software - i didn't get anywhere with it really.
i did find some interesting data sheets and some revealing chinese webpages on the devices but it's just too far out of my wheelhouse to make much of it
no i didn't - i did see the ch55xduino stuff and found a couple of flash utils a few months ago but wasn't brave enough to possibly brick my new toys - i'm not great with programming arduino and not much better with micro/circuit python
i'm more at a 'i can fiollolw instructions and make minor modifications' kinda level since i'm just getting back into this kinda fun after 20 some years - lots of catching up to do
The ch552 seems very difficult to brick, once you have the arduino core on it it's basically free play. We can discuss this further via PM here on had.io if you like, those macropads are very hacker friendly, much more than I anticipated.
i have the 6 button non-rgb and the 6 button 3 layer bluetooth version of these macropads. both use a version of the ch552 and the same terrible software to program it.
i've had both apart to do the same preliminary exploration and dug a little into the same idea of seeing if i could get something - anything - better than just keypresses and more toward real macro's or even just binding to launching a small python script using something like F13-24 or other usually unbound available hid standard keys and additional software - i didn't get anywhere with it really.
i did find some interesting data sheets and some revealing chinese webpages on the devices but it's just too far out of my wheelhouse to make much of it