diff --git a/packages/usb/jump-to-module-bootloader.js b/packages/usb/jump-to-module-bootloader.js new file mode 100755 index 00000000..f7b7b075 --- /dev/null +++ b/packages/usb/jump-to-module-bootloader.js @@ -0,0 +1,14 @@ +#!/usr/bin/env node +const uhk = require('./uhk'); +const program = require('commander'); + +program + .usage(`moduleSlot`) + .parse(process.argv) + +const moduleSlot = program.args[0]; +const moduleSlotId = uhk.checkModuleSlot(moduleSlot, uhk.moduleSlotToId); +const device = uhk.getUhkDevice(); +let transfer = new Buffer([uhk.usbCommands.jumpToModuleBootloader, moduleSlotId]); +device.write(uhk.getTransferData(transfer)); +const response = Buffer.from(device.readSync()); diff --git a/packages/usb/jump-to-slave-bootloader.js b/packages/usb/jump-to-slave-bootloader.js deleted file mode 100755 index a8636adc..00000000 --- a/packages/usb/jump-to-slave-bootloader.js +++ /dev/null @@ -1,7 +0,0 @@ -#!/usr/bin/env node -const uhk = require('./uhk'); - -const device = uhk.getUhkDevice(); -let transfer = new Buffer([uhk.usbCommands.jumpToSlaveBootloader, 1]); -device.write(uhk.getTransferData(transfer)); -const response = Buffer.from(device.readSync()); diff --git a/packages/usb/send-kboot-command-to-module.js b/packages/usb/send-kboot-command-to-module.js new file mode 100755 index 00000000..55172792 --- /dev/null +++ b/packages/usb/send-kboot-command-to-module.js @@ -0,0 +1,38 @@ +#!/usr/bin/env node +const uhk = require('./uhk'); +const program = require('commander'); +const path = require('path'); + +program + .usage(`command [moduleSlot] + + command: ${Object.keys(uhk.kbootCommands).join(' | ')} + moduleSlot: ${Object.keys(uhk.moduleSlotToI2cAddress).join(' | ')} + moduleSlot is always required, except for the idle command.`) + .parse(process.argv) + +const kbootCommand = program.args[0]; +if (!kbootCommand) { + console.log(`No command provided.`); + console.log(`Valid commands: ${Object.keys(uhk.kbootCommands).join(', ')}`); + process.exit(1); +} + +const kbootCommandId = uhk.kbootCommands[kbootCommand]; +if (kbootCommandId === undefined) { + console.log(`Invalid command "${kbootCommand}" provided.`); + console.log(`Valid commands: ${Object.keys(uhk.kbootCommands).join(', ')}`); + process.exit(1); +} + +const moduleSlot = program.args[1]; + +let i2cAddress; +if (kbootCommand !== 'idle') { + i2cAddress = uhk.checkModuleSlot(moduleSlot, uhk.moduleSlotToI2cAddress); +} + +const device = uhk.getUhkDevice(); +let transfer = new Buffer([uhk.usbCommands.sendKbootCommand, kbootCommandId, parseInt(i2cAddress)]); +device.write(uhk.getTransferData(transfer)); +const response = Buffer.from(device.readSync()); diff --git a/packages/usb/send-kboot-command-to-slave.js b/packages/usb/send-kboot-command-to-slave.js deleted file mode 100755 index aa5c87cc..00000000 --- a/packages/usb/send-kboot-command-to-slave.js +++ /dev/null @@ -1,35 +0,0 @@ -#!/usr/bin/env node -const uhk = require('./uhk'); -const path = require('path'); - -function printUsage() { - const scriptFilename = path.basename(process.argv[1]); - const commands = Object.keys(uhk.kbootCommands).join(' | '); - console.log( -`Usage: ${scriptFilename} command i2cAddress -command: ${commands} -i2cAddress is not needed for the idle command`); -} - -const kbootCommand = process.argv[2]; -if (!kbootCommand) { - console.log(`No command provided`); - process.exit(1); -} - -const kbootCommandId = uhk.kbootCommands[kbootCommand]; -if (!kbootCommandId) { - console.log(`Invalid command provided`); - process.exit(1); -} - -const i2cAddress = process.argv[3]; -if (kbootCommand !== 'idle' && !i2cAddress) { - console.log(`No i2cAddress provided`); - process.exit(1); -} - -const device = uhk.getUhkDevice(); -let transfer = new Buffer([uhk.usbCommands.sendKbootCommand, kbootCommandId, parseInt(i2cAddress)]); -device.write(uhk.getTransferData(transfer)); -const response = Buffer.from(device.readSync()); diff --git a/packages/usb/shared.js b/packages/usb/shared.js index f37b5138..ece62199 100644 --- a/packages/usb/shared.js +++ b/packages/usb/shared.js @@ -35,7 +35,7 @@ function getBlhostCmd(pid) { break; } - return `${__dirname}/blhost/${blhostPath} --usb 0x1d50,${pid}`; + return `${__dirname}/blhost/${blhostPath} --usb 0x1d50,0x${pid.toString(16)}`; } function execRetry(command) { diff --git a/packages/usb/uhk.js b/packages/usb/uhk.js index c1da2200..c5044ae1 100755 --- a/packages/usb/uhk.js +++ b/packages/usb/uhk.js @@ -49,6 +49,7 @@ exports = module.exports = moduleExports = { getUhkDevice, getBootloaderDevice, getTransferData, + checkModuleSlot, usbCommands: { getProperty: 0, reenumerate: 1, @@ -63,7 +64,7 @@ exports = module.exports = moduleExports = { readUserConfig: 15, getKeyboardState: 16, readDebugInfo: 17, - jumpToSlaveBootloader: 18, + jumpToModuleBootloader: 18, sendKbootCommand: 19, }, enumerationModes: { @@ -78,6 +79,12 @@ exports = module.exports = moduleExports = { '2': 0x6122, '3': 0x6123, }, + enumerationNameToProductId: { + bootloader: 0x6120, + buspal: 0x6121, + normalKeyboard: 0x6122, + compatibleKeyboard: 0x6123, + }, vendorId: 0x1D50, systemPropertyIds: { usbProtocolVersion: 0, @@ -99,9 +106,14 @@ exports = module.exports = moduleExports = { reset: 2, }, moduleSlotToI2cAddress: { - leftHalf: 0x10, - leftAddon: 0x20, - rightAddon: 0x30, + leftHalf: '0x10', + leftAddon: '0x20', + rightAddon: '0x30', + }, + moduleSlotToId: { + leftHalf: 1, + leftAddon: 2, + rightAddon: 3, }, leftLedDriverAddress: 0b1110100, rightLedDriverAddress: 0b1110111, @@ -144,3 +156,20 @@ function writeLog(prefix, buffer) { } console.log(prefix + bufferToString(buffer)) } + +function checkModuleSlot(moduleSlot, mapping) { + const mapped = mapping[moduleSlot]; + + if (moduleSlot == undefined) { + console.log(`No moduleSlot specified.`); + process.exit(1); + } + + if (mapped == undefined) { + console.log(`Invalid moduleSlot "${moduleSlot}" specified.`); + console.log(`Valid module slots are: ${Object.keys(mapping).join(', ')}.`); + process.exit(1); + } + + return mapped; +} diff --git a/packages/usb/update-master-firmware.js b/packages/usb/update-device-firmware.js similarity index 76% rename from packages/usb/update-master-firmware.js rename to packages/usb/update-device-firmware.js index 4a4632f1..6f5588be 100755 --- a/packages/usb/update-master-firmware.js +++ b/packages/usb/update-device-firmware.js @@ -1,4 +1,5 @@ #!/usr/bin/env node +const uhk = require('./uhk'); const program = require('commander'); require('shelljs/global'); require('./shared') @@ -7,12 +8,12 @@ const extension = '.hex'; config.fatal = true; program - .usage(`update-master-firmware `) + .usage(`firmwareImage${extension}`) .parse(process.argv) const firmwareImage = program.args[0]; const usbDir = `${__dirname}`; -const blhost = getBlhostCmd(0x6120); +const blhost = getBlhostCmd(uhk.enumerationNameToProductId.bootloader); checkFirmwareImage(firmwareImage, extension); @@ -24,4 +25,4 @@ exec(`${blhost} flash-image ${firmwareImage}`); exec(`${blhost} reset`); config.verbose = false; -echo('Firmware updated successfully'); +echo('Firmware updated successfully.'); diff --git a/packages/usb/update-slave-firmware.js b/packages/usb/update-module-firmware.js similarity index 56% rename from packages/usb/update-slave-firmware.js rename to packages/usb/update-module-firmware.js index b1de57f2..2778174f 100755 --- a/packages/usb/update-slave-firmware.js +++ b/packages/usb/update-module-firmware.js @@ -2,43 +2,36 @@ const uhk = require('./uhk'); const program = require('commander'); require('shelljs/global'); -require('./shared') +require('./shared'); const extension = '.bin'; config.fatal = true; program - .usage(`update-slave-firmware `) + .usage(`moduleSlot firmwareImage${extension}`) .parse(process.argv) let moduleSlot = program.args[0]; -let i2cAddress = uhk.moduleSlotToI2cAddress[moduleSlot]; - -if (!i2cAddress) { - echo(`Invalid module slot specified.`); - echo(`Valid slots are: ${Object.keys(uhk.moduleSlotToI2cAddress).join(', ')}.`); - exit(1); -} - -i2cAddress = `0x${i2cAddress.toString(16)}`; +const i2cAddress = uhk.checkModuleSlot(moduleSlot, uhk.moduleSlotToI2cAddress); const firmwareImage = program.args[1]; checkFirmwareImage(firmwareImage, extension); const usbDir = `${__dirname}`; -const blhostUsb = getBlhostCmd(0x6121); +const blhostUsb = getBlhostCmd(uhk.enumerationNameToProductId.buspal); const blhostBuspal = `${blhostUsb} --buspal i2c,${i2cAddress},100k`; config.verbose = true; -exec(`${usbDir}/send-kboot-command-to-slave.js ping ${i2cAddress}`); -exec(`${usbDir}/jump-to-slave-bootloader.js`); +exec(`${usbDir}/send-kboot-command-to-module.js ping ${moduleSlot}`); +exec(`${usbDir}/jump-to-module-bootloader.js ${moduleSlot}`); exec(`${usbDir}/reenumerate.js buspal`); execRetry(`${blhostBuspal} get-property 1`); exec(`${blhostBuspal} flash-erase-all-unsecure`); exec(`${blhostBuspal} write-memory 0x0 ${firmwareImage}`); exec(`${blhostUsb} reset`); exec(`${usbDir}/reenumerate.js normalKeyboard`); -execRetry(`${usbDir}/send-kboot-command-to-slave.js reset ${i2cAddress}`); +execRetry(`${usbDir}/send-kboot-command-to-module.js reset ${moduleSlot}`); +exec(`${usbDir}/send-kboot-command-to-module.js idle`); config.verbose = false; echo('Firmware updated successfully.');