@@ -405,18 +405,19 @@ uint8_t __no_inline_not_in_flash_func(pio_usb_ll_encode_tx_data)(
405405 uint8_t const * buffer , uint8_t buffer_len , uint8_t * encoded_data ) {
406406 uint16_t bit_idx = 0 ;
407407 int current_state = 1 ;
408- int bit_stuffing = 0 ;
408+ int bit_stuffing = 6 ;
409409 for (int idx = 0 ; idx < buffer_len ; idx ++ ) {
410+ uint8_t byte = buffer [idx ];
410411 for (int b = 0 ; b < 8 ; b ++ ) {
411- uint8_t byte_idx = bit_idx >> 3 ;
412+ uint8_t byte_idx = bit_idx >> 2 ;
412413 encoded_data [byte_idx ] <<= 2 ;
413- if (buffer [ idx ] & (1 << b )) {
414+ if (byte & (1 << b )) {
414415 if (current_state ) {
415416 encoded_data [byte_idx ] |= PIO_USB_TX_ENCODED_DATA_K ;
416417 } else {
417418 encoded_data [byte_idx ] |= PIO_USB_TX_ENCODED_DATA_J ;
418419 }
419- bit_stuffing ++ ;
420+ bit_stuffing -- ;
420421 } else {
421422 if (current_state ) {
422423 encoded_data [byte_idx ] |= PIO_USB_TX_ENCODED_DATA_J ;
@@ -425,13 +426,13 @@ uint8_t __no_inline_not_in_flash_func(pio_usb_ll_encode_tx_data)(
425426 encoded_data [byte_idx ] |= PIO_USB_TX_ENCODED_DATA_K ;
426427 current_state = 1 ;
427428 }
428- bit_stuffing = 0 ;
429+ bit_stuffing = 6 ;
429430 }
430431
431- bit_idx += 2 ;
432- byte_idx = bit_idx >> 3 ;
432+ bit_idx ++ ;
433433
434- if (bit_stuffing == 6 ) {
434+ if (bit_stuffing == 0 ) {
435+ byte_idx = bit_idx >> 2 ;
435436 encoded_data [byte_idx ] <<= 2 ;
436437
437438 if (current_state ) {
@@ -441,32 +442,30 @@ uint8_t __no_inline_not_in_flash_func(pio_usb_ll_encode_tx_data)(
441442 encoded_data [byte_idx ] |= PIO_USB_TX_ENCODED_DATA_K ;
442443 current_state = 1 ;
443444 }
444- bit_stuffing = 0 ;
445-
446- bit_idx += 2 ;
445+ bit_stuffing = 6 ;
446+ bit_idx ++ ;
447447 }
448448 }
449449 }
450450
451- uint8_t byte_idx = bit_idx >> 3 ;
451+ uint8_t byte_idx = bit_idx >> 2 ;
452452 encoded_data [byte_idx ] <<= 2 ;
453453 encoded_data [byte_idx ] |= PIO_USB_TX_ENCODED_DATA_SE0 ;
454- bit_idx += 2 ;
455- byte_idx = bit_idx >> 3 ;
454+ bit_idx ++ ;
456455
456+ byte_idx = bit_idx >> 2 ;
457457 encoded_data [byte_idx ] <<= 2 ;
458458 encoded_data [byte_idx ] |= PIO_USB_TX_ENCODED_DATA_COMP ;
459- bit_idx += 2 ;
460- byte_idx = bit_idx >> 3 ;
459+ bit_idx ++ ;
461460
462461 do {
462+ byte_idx = bit_idx >> 2 ;
463463 encoded_data [byte_idx ] <<= 2 ;
464464 encoded_data [byte_idx ] |= PIO_USB_TX_ENCODED_DATA_K ;
465- bit_idx += 2 ;
466- byte_idx = bit_idx >> 3 ;
467- } while ((bit_idx & 0x07 ) != 0 );
465+ bit_idx ++ ;
466+ } while (bit_idx & 0x07 );
468467
469- byte_idx = bit_idx >> 3 ;
468+ byte_idx = bit_idx >> 2 ;
470469 return byte_idx ;
471470}
472471
0 commit comments