@@ -16,6 +16,7 @@ import {
1616 isDeviceProtocolSupportGitInfo ,
1717 LEFT_HALF_MODULE ,
1818 LogService ,
19+ ModuleSlotToI2cAddress ,
1920 ModuleSlotToId ,
2021 ModuleVersionInfo ,
2122 OLED_DISPLAY_HEIGHT ,
@@ -229,8 +230,8 @@ export class UhkOperations {
229230 await this . jumpToBootloaderModule ( module . slotId ) ;
230231 await this . device . close ( ) ;
231232
232- const moduleBricked = await this . waitForKbootIdle ( module . name ) ;
233- if ( ! moduleBricked ) {
233+ const connectedAddress = await this . waitForKbootIdle ( module . name , module . i2cAddress ) ;
234+ if ( ! connectedAddress ) {
234235 const msg = `[UhkOperations] Couldn't connect to the "${ module . name } ".` ;
235236 this . logService . error ( msg ) ;
236237 throw new Error ( msg ) ;
@@ -250,9 +251,9 @@ export class UhkOperations {
250251 let connected = false ;
251252 while ( new Date ( ) . getTime ( ) - startTime . getTime ( ) < 30000 ) {
252253 try {
253- this . logService . misc ( `[UhkOperations] Try to connect to the "${ module . name } "` ) ;
254+ this . logService . misc ( `[UhkOperations] Try to connect to the "${ module . name } " at I2C address 0x ${ connectedAddress . toString ( 16 ) } ` ) ;
254255 kboot = new KBoot ( usbPeripheral ) ;
255- await kboot . configureI2c ( module . i2cAddress ) ;
256+ await kboot . configureI2c ( connectedAddress ) ;
256257 await kboot . getProperty ( Properties . BootloaderVersion ) ;
257258 connected = true ;
258259 break ;
@@ -272,14 +273,14 @@ export class UhkOperations {
272273 await snooze ( 1000 ) ;
273274
274275 this . logService . misc ( `[UhkOperations] Flash erase all on "${ module . name } " keyboard` ) ;
275- await kboot . configureI2c ( module . i2cAddress ) ;
276+ await kboot . configureI2c ( connectedAddress ) ;
276277 await kboot . flashEraseAllUnsecure ( ) ;
277278
278279 this . logService . misc ( `[UhkOperations] Read "${ module . name } " firmware from file` ) ;
279280 const configData = fs . readFileSync ( firmwarePath ) ;
280281
281282 this . logService . misc ( '[UhkOperations] Write memory' ) ;
282- await kboot . configureI2c ( module . i2cAddress ) ;
283+ await kboot . configureI2c ( connectedAddress ) ;
283284 await kboot . writeMemory ( { startAddress : 0 , data : configData } ) ;
284285
285286 this . logService . misc ( `[UhkOperations] Reset "${ module . name } " keyboard` ) ;
@@ -483,16 +484,34 @@ export class UhkOperations {
483484 }
484485 }
485486
486- public async waitForKbootIdle ( moduleName : string ) : Promise < boolean > {
487+ public async waitForKbootIdle ( moduleName : string , assignedAddress : ModuleSlotToI2cAddress ) : Promise < number > {
488+ const DEFAULT_BOOTLOADER_I2C_ADDRESS = 0x10 ;
489+ const ADDRESS_SWITCH_INTERVAL_MS = 10000 ;
490+ const startTime = new Date ( ) ;
491+ let currentAddress : number = assignedAddress ;
492+
487493 while ( true ) {
488494 const buffer = await this . device . write ( Buffer . from ( [ UsbCommand . GetProperty , DevicePropertyIds . CurrentKbootCommand ] ) ) ;
489495 await this . device . close ( ) ;
490496
491497 if ( buffer [ 1 ] === 0 ) {
492- return true ;
498+ return currentAddress ;
499+ }
500+
501+ const elapsedMs = new Date ( ) . getTime ( ) - startTime . getTime ( ) ;
502+ const useDefaultAddress = Math . floor ( elapsedMs / ADDRESS_SWITCH_INTERVAL_MS ) % 2 === 1 ;
503+ const desiredAddress = useDefaultAddress ? DEFAULT_BOOTLOADER_I2C_ADDRESS : assignedAddress ;
504+
505+ if ( desiredAddress !== currentAddress ) {
506+ this . logService . misc ( `[DeviceOperation] Switching kboot ping address from 0x${ currentAddress . toString ( 16 ) } to 0x${ desiredAddress . toString ( 16 ) } ` ) ;
507+ await this . device . sendKbootCommandToModule ( desiredAddress as ModuleSlotToI2cAddress , KbootCommands . idle ) ;
508+ await this . device . close ( ) ;
509+ await this . device . sendKbootCommandToModule ( desiredAddress as ModuleSlotToI2cAddress , KbootCommands . ping , 100 ) ;
510+ await this . device . close ( ) ;
511+ currentAddress = desiredAddress ;
493512 }
494513
495- this . logService . misc ( `[DeviceOperation] Cannot ping the bootloader. Please remove the "${ moduleName } " module, and keep reconnecting it until you do not see this message anymore.` ) ;
514+ this . logService . misc ( `[DeviceOperation] Cannot ping the bootloader at address 0x ${ currentAddress . toString ( 16 ) } . Please remove the "${ moduleName } " module, and keep reconnecting it until you do not see this message anymore.` ) ;
496515
497516 await snooze ( 1000 ) ;
498517 }
0 commit comments