memo.md (6065B)
1 ## Hardware Information 2 3 The YANG HHKB BLE controller design is similiar to hasu's 4 controller. Most pins are compatiable. 5 6 **MCU**: ATmega32U4 7 **Bluetooth**: MDBT40 (nRF51822-based), with Adafruit Bluefruit LE UART Friend firmware. 8 **Power**: 3.3V 9 **CPU Frequency**: 8MHz 10 **Bootloader**: Lufa MassStorage 11 12 ## Pin usage 13 14 | Description | HASU pin usage | YANG mod changed | 15 |:------------------------------------ | ---------------------- | -------------------------- | 16 | ~KEY: Lo(0) when key is pressed | PD7 input(with pullup) | | 17 | Hysteresis: Hi(1) if key was pressed | PB7 output | | 18 | Row selector bit0 | PB0 output | | 19 | Row selector bit1 | PB1 output | | 20 | Row selector bit2 | PB2 output | | 21 | Col selector bit0 | PB3 output | | 22 | Col selector bit1 | PB4 output | | 23 | Col selector bit2 | PB5 output | | 24 | Key unable | PB6 output | | 25 | Switch power | PD4 output | PD6 output (PMOS FET) | 26 | Bluetooth UART Rx | PC4 input | PD2 | 27 | Bluetooth UART Tx | PC5 output | PD3 | 28 | Bluetooth power | | PD5 output (low: power on) | 29 | LED 0 | | PF4 | 30 | LED 2 | | PF1 | 31 | LED 4 | | PF0 | 32 | Unused for PRO2 | PC6 | | 33 | Unused for PRO2 | PC7 | | 34 | Inner USB power | | PF7 | 35 36 ## How to flash LUFA MassStorage bootloader on Linux 37 38 The FAT filesystem on Linux very often cannot flush the write cache, 39 leading to broken firmware in the flash. 40 41 We can use `dd` to write to the virtual block storage directly to 42 bypass the vfs layer. 43 44 ``` 45 dd if=FLASH.bin of=<path of virtual block device> seek=4 46 ``` 47 48 Skip 4 sectors because the default sector size of the virtual device 49 and dd is 512 bytes and the emulated flash file starts at 5th sector. 50 51 ## How to find the path of the virtual block device 52 53 After the keyboard boots into flash mode, on Linux system you should 54 be able to find the block device in `dmesg` logs. 55 56 For exmaple if you type 57 58 ``` 59 sudo dmesg 60 ``` 61 62 You should find something like 63 64 ``` 65 [357885.143593] usb 1-1.4: USB disconnect, device number 24 66 [357885.627740] usb 1-1.4: new full-speed USB device number 25 using xhci_hcd 67 [357885.729486] usb 1-1.4: New USB device found, idVendor=03eb, idProduct=1962, bcdDevice= 0.01 68 [357885.729492] usb 1-1.4: New USB device strings: Mfr=0, Product=0, SerialNumber=0 69 [357885.745620] SCSI subsystem initialized 70 [357885.746712] usb-storage 1-1.4:1.0: USB Mass Storage device detected 71 [357885.746818] scsi host0: usb-storage 1-1.4:1.0 72 [357885.746919] usbcore: registered new interface driver usb-storage 73 [357885.747689] usbcore: registered new interface driver uas 74 [357886.766755] scsi 0:0:0:0: Direct-Access LUFA Bootloader 0.00 PQ: 0 ANSI: 0 75 [357886.773216] scsi 0:0:0:0: Attached scsi generic sg0 type 0 76 [357886.777474] sd 0:0:0:0: [sdx] 134 512-byte logical blocks: (68.6 kB/67.0 KiB) 77 [357886.780300] sd 0:0:0:0: [sdx] Write Protect is off 78 [357886.780302] sd 0:0:0:0: [sdx] Mode Sense: 00 00 00 00 79 [357886.783113] sd 0:0:0:0: [sdx] Asking for cache data failed 80 [357886.783114] sd 0:0:0:0: [sdx] Assuming drive cache: write through 81 [357886.842676] sdx: 82 [357886.859528] sd 0:0:0:0: [sdx] Attached SCSI removable disk 83 ``` 84 85 The `sdx` is the block device name and the full path is at `/dev/sdx` 86 The above flash command will become 87 88 ``` 89 dd if=FLASH.bin of=/dev/sdx seek=4 90 ``` 91 92 ## Adafruit Bluefruit LE UART configuraton 93 94 The default baud rate used by the firmware is 76800 although adafruit 95 do not recommend using higher baudrates than 9600 because the nRF51 96 UART can drop characters. 97 98 Double speed mode to get more accurate async reading because the F_CPU 99 speed is 8MHz. 100 101 ## Power saving mode design 102 103 Power saving is only enabled when USB is detached and using battery 104 power. Here we define several levels of power saving mode, each saves 105 more power but takes longer to resume operation. 106 107 1. Level 1: idle mode is activated after a short configurable time 108 (MATRIX_POWER_SAVE_TIMEOUT_MS) MCU is put into sleep mode and only 109 scan the matrix per 15ms. PORTB pins are set to input with pull-up 110 to save power. Sensing PCB is powered down between scans. 111 112 2. Level 2: after idling for longer (MATRIX_POWER_SAVE_TIMEOUT_L2_MS) 113 we entry this state. Matrix scan is skipped until the time lapses 114 900ms. 115 116 2. Level 3: sleep mode is activated after a longer timeout 117 (MATRIX_POWER_SAVE_TIMEOUT_L3_MS) Bluetooth module is powered down. 118 119 ## Battery reading 120 121 VBAT is connected to AIN6 pin on the MDBT40 module and the AREF pin is 122 the reference voltage. Doing a ADC with AT+HWDAC=6 will return the 123 difference between VBAT and VREF. 124 125 It seems when fully charged the ADC read is 550. Likely VREF is 3311mV 126 and the fully charged VBAT is thus 3861mV. 127 128 Enable battery service with AT+BLEBATTEN=1 first then we can update the 129 battery level by using AT+BLEBATTVAL=%d 130 131 ## References 132 133 * https://github.com/joric/qmk/wiki/hhkb_ble 134 * https://github.com/tomsmalley/custom-topre-guide 135 * https://github.com/abcminiuser/lufa/blob/master/Bootloaders/MassStorage/Lib/VirtualFAT.h