diff options
| author | Drashna Jaelre <drashna@live.com> | 2020-10-26 23:09:11 -0700 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-10-27 17:09:11 +1100 |
| commit | 33074bcbadbafa3a359efda5a45a9412e4eca7d2 (patch) | |
| tree | 5682dd93d88aa91a91b2e111f1b2a1e5604d7633 /keyboards/ploopyco/pmw3360.c | |
| parent | 555b1640b26fa09ea5f6b5bc7ea07dc654a326f9 (diff) | |
| download | qmk_firmware-33074bcbadbafa3a359efda5a45a9412e4eca7d2.tar.gz qmk_firmware-33074bcbadbafa3a359efda5a45a9412e4eca7d2.zip | |
[Keyboard] Bug fixes and improvements to PloopyCo devices (#10573)
Co-authored-by: Ryan <fauxpark@gmail.com>
Diffstat (limited to 'keyboards/ploopyco/pmw3360.c')
| -rw-r--r-- | keyboards/ploopyco/pmw3360.c | 225 |
1 files changed, 225 insertions, 0 deletions
diff --git a/keyboards/ploopyco/pmw3360.c b/keyboards/ploopyco/pmw3360.c new file mode 100644 index 000000000..1bd03e8b9 --- /dev/null +++ b/keyboards/ploopyco/pmw3360.c | |||
| @@ -0,0 +1,225 @@ | |||
| 1 | /* Copyright 2020 Christopher Courtney, aka Drashna Jael're (@drashna) <drashna@live.com> | ||
| 2 | * Copyright 2019 Sunjun Kim | ||
| 3 | * Copyright 2020 Ploopy Corporation | ||
| 4 | * | ||
| 5 | * This program is free software: you can redistribute it and/or modify | ||
| 6 | * it under the terms of the GNU General Public License as published by | ||
| 7 | * the Free Software Foundation, either version 2 of the License, or | ||
| 8 | * (at your option) any later version. | ||
| 9 | * | ||
| 10 | * This program is distributed in the hope that it will be useful, | ||
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 13 | * GNU General Public License for more details. | ||
| 14 | * | ||
| 15 | * You should have received a copy of the GNU General Public License | ||
| 16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| 17 | */ | ||
| 18 | |||
| 19 | |||
| 20 | #include "pmw3360.h" | ||
| 21 | #include "pmw3360_firmware.h" | ||
| 22 | |||
| 23 | #ifdef CONSOLE_ENABLE | ||
| 24 | # include "print.h" | ||
| 25 | #endif | ||
| 26 | bool _inBurst = false; | ||
| 27 | |||
| 28 | #ifndef PMW_CPI | ||
| 29 | # define PMW_CPI 1600 | ||
| 30 | #endif | ||
| 31 | #ifndef SPI_DIVISOR | ||
| 32 | # define SPI_DIVISOR 2 | ||
| 33 | #endif | ||
| 34 | #ifndef ROTATIONAL_TRANSFORM_ANGLE | ||
| 35 | # define ROTATIONAL_TRANSFORM_ANGLE 0x00 | ||
| 36 | #endif | ||
| 37 | |||
| 38 | #ifdef CONSOLE_ENABLE | ||
| 39 | void print_byte(uint8_t byte) { dprintf("%c%c%c%c%c%c%c%c|", (byte & 0x80 ? '1' : '0'), (byte & 0x40 ? '1' : '0'), (byte & 0x20 ? '1' : '0'), (byte & 0x10 ? '1' : '0'), (byte & 0x08 ? '1' : '0'), (byte & 0x04 ? '1' : '0'), (byte & 0x02 ? '1' : '0'), (byte & 0x01 ? '1' : '0')); } | ||
| 40 | #endif | ||
| 41 | |||
| 42 | |||
| 43 | bool spi_start_adv(void) { | ||
| 44 | bool status = spi_start(SPI_SS_PIN, false, 3, SPI_DIVISOR); | ||
| 45 | wait_us(1); | ||
| 46 | return status; | ||
| 47 | } | ||
| 48 | |||
| 49 | void spi_stop_adv(void) { | ||
| 50 | wait_us(1); | ||
| 51 | spi_stop(); | ||
| 52 | } | ||
| 53 | |||
| 54 | spi_status_t spi_write_adv(uint8_t reg_addr, uint8_t data) { | ||
| 55 | if (reg_addr != REG_Motion_Burst) { | ||
| 56 | _inBurst = false; | ||
| 57 | } | ||
| 58 | |||
| 59 | spi_start_adv(); | ||
| 60 | // send address of the register, with MSBit = 1 to indicate it's a write | ||
| 61 | spi_status_t status = spi_write(reg_addr | 0x80); | ||
| 62 | status = spi_write(data); | ||
| 63 | |||
| 64 | // tSCLK-NCS for write operation | ||
| 65 | wait_us(20); | ||
| 66 | |||
| 67 | // tSWW/tSWR (=120us) minus tSCLK-NCS. Could be shortened, but is looks like a safe lower bound | ||
| 68 | wait_us(100); | ||
| 69 | spi_stop(); | ||
| 70 | return status; | ||
| 71 | } | ||
| 72 | |||
| 73 | uint8_t spi_read_adv(uint8_t reg_addr) { | ||
| 74 | spi_start_adv(); | ||
| 75 | // send adress of the register, with MSBit = 0 to indicate it's a read | ||
| 76 | spi_write(reg_addr & 0x7f); | ||
| 77 | |||
| 78 | uint8_t data = spi_read(); | ||
| 79 | |||
| 80 | // tSCLK-NCS for read operation is 120ns | ||
| 81 | wait_us(1); | ||
| 82 | |||
| 83 | // tSRW/tSRR (=20us) minus tSCLK-NCS | ||
| 84 | wait_us(19); | ||
| 85 | |||
| 86 | spi_stop(); | ||
| 87 | return data; | ||
| 88 | } | ||
| 89 | |||
| 90 | void pmw_set_cpi(uint16_t cpi) { | ||
| 91 | int cpival = constrain((cpi / 100) - 1, 0, 0x77); // limits to 0--119 | ||
| 92 | |||
| 93 | spi_start_adv(); | ||
| 94 | spi_write_adv(REG_Config1, cpival); | ||
| 95 | spi_stop(); | ||
| 96 | } | ||
| 97 | |||
| 98 | bool pmw_spi_init(void) { | ||
| 99 | spi_init(); | ||
| 100 | _inBurst = false; | ||
| 101 | |||
| 102 | spi_stop(); | ||
| 103 | spi_start_adv(); | ||
| 104 | spi_stop(); | ||
| 105 | |||
| 106 | spi_write_adv(REG_Shutdown, 0xb6); // Shutdown first | ||
| 107 | wait_ms(300); | ||
| 108 | |||
| 109 | spi_start_adv(); | ||
| 110 | wait_us(40); | ||
| 111 | spi_stop_adv(); | ||
| 112 | wait_us(40); | ||
| 113 | |||
| 114 | spi_write_adv(REG_Power_Up_Reset, 0x5a); | ||
| 115 | wait_ms(50); | ||
| 116 | |||
| 117 | spi_read_adv(REG_Motion); | ||
| 118 | spi_read_adv(REG_Delta_X_L); | ||
| 119 | spi_read_adv(REG_Delta_X_H); | ||
| 120 | spi_read_adv(REG_Delta_Y_L); | ||
| 121 | spi_read_adv(REG_Delta_Y_H); | ||
| 122 | |||
| 123 | pmw_upload_firmware(); | ||
| 124 | |||
| 125 | spi_stop_adv(); | ||
| 126 | |||
| 127 | wait_ms(10); | ||
| 128 | pmw_set_cpi(PMW_CPI); | ||
| 129 | |||
| 130 | wait_ms(1); | ||
| 131 | |||
| 132 | return pmw_check_signature(); | ||
| 133 | } | ||
| 134 | |||
| 135 | void pmw_upload_firmware(void) { | ||
| 136 | spi_write_adv(REG_Config2, 0x00); | ||
| 137 | |||
| 138 | spi_write_adv(REG_Angle_Tune, constrain(ROTATIONAL_TRANSFORM_ANGLE, -30, 30)); | ||
| 139 | |||
| 140 | spi_write_adv(REG_SROM_Enable, 0x1d); | ||
| 141 | |||
| 142 | wait_ms(10); | ||
| 143 | |||
| 144 | spi_write_adv(REG_SROM_Enable, 0x18); | ||
| 145 | |||
| 146 | spi_start_adv(); | ||
| 147 | spi_write(REG_SROM_Load_Burst | 0x80); | ||
| 148 | wait_us(15); | ||
| 149 | |||
| 150 | unsigned char c; | ||
| 151 | for (int i = 0; i < firmware_length; i++) { | ||
| 152 | c = (unsigned char)pgm_read_byte(firmware_data + i); | ||
| 153 | spi_write(c); | ||
| 154 | wait_us(15); | ||
| 155 | } | ||
| 156 | wait_us(200); | ||
| 157 | |||
| 158 | spi_read_adv(REG_SROM_ID); | ||
| 159 | |||
| 160 | spi_write_adv(REG_Config2, 0x00); | ||
| 161 | |||
| 162 | spi_stop(); | ||
| 163 | wait_ms(10); | ||
| 164 | } | ||
| 165 | |||
| 166 | bool pmw_check_signature(void) { | ||
| 167 | uint8_t pid = spi_read_adv(REG_Product_ID); | ||
| 168 | uint8_t iv_pid = spi_read_adv(REG_Inverse_Product_ID); | ||
| 169 | uint8_t SROM_ver = spi_read_adv(REG_SROM_ID); | ||
| 170 | return (pid == 0x42 && iv_pid == 0xBD && SROM_ver == 0x04); // signature for SROM 0x04 | ||
| 171 | } | ||
| 172 | |||
| 173 | report_pmw_t pmw_read_burst(void) { | ||
| 174 | if (!_inBurst) { | ||
| 175 | #ifdef CONSOLE_ENABLE | ||
| 176 | dprintf("burst on"); | ||
| 177 | #endif | ||
| 178 | spi_write_adv(REG_Motion_Burst, 0x00); | ||
| 179 | _inBurst = true; | ||
| 180 | } | ||
| 181 | |||
| 182 | spi_start_adv(); | ||
| 183 | spi_write(REG_Motion_Burst); | ||
| 184 | wait_us(35); // waits for tSRAD | ||
| 185 | |||
| 186 | report_pmw_t data; | ||
| 187 | data.motion = 0; | ||
| 188 | data.dx = 0; | ||
| 189 | data.mdx = 0; | ||
| 190 | data.dy = 0; | ||
| 191 | data.mdx = 0; | ||
| 192 | |||
| 193 | data.motion = spi_read(); | ||
| 194 | spi_write(0x00); // skip Observation | ||
| 195 | data.dx = spi_read(); | ||
| 196 | data.mdx = spi_read(); | ||
| 197 | data.dy = spi_read(); | ||
| 198 | data.mdy = spi_read(); | ||
| 199 | |||
| 200 | spi_stop(); | ||
| 201 | |||
| 202 | #ifdef CONSOLE_ENABLE | ||
| 203 | print_byte(data.motion); | ||
| 204 | print_byte(data.dx); | ||
| 205 | print_byte(data.mdx); | ||
| 206 | print_byte(data.dy); | ||
| 207 | print_byte(data.mdy); | ||
| 208 | dprintf("\n"); | ||
| 209 | #endif | ||
| 210 | |||
| 211 | data.isMotion = (data.motion & 0x80) != 0; | ||
| 212 | data.isOnSurface = (data.motion & 0x08) == 0; | ||
| 213 | data.dx |= (data.mdx << 8); | ||
| 214 | data.dx = data.dx * -1; | ||
| 215 | data.dy |= (data.mdy << 8); | ||
| 216 | // data.dy = data.dy * -1; | ||
| 217 | |||
| 218 | spi_stop(); | ||
| 219 | |||
| 220 | if (data.motion & 0b111) { // panic recovery, sometimes burst mode works weird. | ||
| 221 | _inBurst = false; | ||
| 222 | } | ||
| 223 | |||
| 224 | return data; | ||
| 225 | } | ||
