Skip to content

Commit 7ce6e4a

Browse files
Speed up software encoder
1 parent ef9d780 commit 7ce6e4a

File tree

2 files changed

+37
-20
lines changed

2 files changed

+37
-20
lines changed

examples/test_ll/test_ll.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11

22
#include <stdio.h>
33
#include <string.h>
4+
#include <strings.h>
45

56
#include "hardware/gpio.h"
67
#include "hardware/sync.h"
@@ -125,6 +126,23 @@ int main() {
125126

126127
root->connected = false;
127128
}
129+
130+
{
131+
printf("\nTest 5: Software Encode Speed\n");
132+
uint8_t buffer[64];
133+
uint8_t encoded_data[64 * 2 * 7 / 6 + 2];
134+
for (size_t i = 0; i < sizeof(buffer); i++) {
135+
buffer[i] = i;
136+
}
137+
138+
absolute_time_t start = get_absolute_time();
139+
for (int i = 0; i < 1000; i++) {
140+
pio_usb_ll_encode_tx_data(buffer, sizeof(buffer), encoded_data);
141+
}
142+
absolute_time_t end = get_absolute_time();
143+
int64_t diff = absolute_time_diff_us(start, end);
144+
printf("%f us (64bytes packet)", diff / 1000.0f);
145+
}
128146
}
129147
}
130148

src/pio_usb.c

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)