Skip to content

Commit 0f47197

Browse files
Last static variable and use builtin_ctx === ffs
1 parent 08741c5 commit 0f47197

File tree

2 files changed

+9
-26
lines changed

2 files changed

+9
-26
lines changed

cores/rp2040/USB.cpp

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -65,21 +65,11 @@ uint8_t _picotool_itf_num;
6565

6666
int usb_hid_poll_interval __attribute__((weak)) = 10;
6767

68-
// GCC doesn't seem to have builtin_ffs here
69-
int USBClass::ffs(uint32_t v) {
70-
for (auto i = 0; i < 32; i++) {
71-
if (v & (1 << i)) {
72-
return i;
73-
}
74-
}
75-
return 0;
76-
}
77-
7868
uint8_t USBClass::registerEndpointIn() {
7969
if (!_endpointIn) {
8070
return 0; // ERROR, out of EPs
8171
}
82-
int firstFree = ffs(_endpointIn);
72+
int firstFree = __builtin_ctz(_endpointIn);
8373
_endpointIn &= ~(1 << firstFree);
8474
return 0x80 + firstFree;
8575
}
@@ -92,7 +82,7 @@ uint8_t USBClass::registerEndpointOut() {
9282
if (!_endpointOut) {
9383
return 0; // ERROR, out of EPs
9484
}
95-
int firstFree = ffs(_endpointOut);
85+
int firstFree = __builtin_ctz(_endpointOut);
9686
_endpointOut &= ~(1 << firstFree);
9787
return firstFree;
9888
}
@@ -439,7 +429,6 @@ void __freertos_usb_task(void *param) {
439429
}
440430
}
441431
#else
442-
static int __usb_task_irq;
443432
static void usb_irq() {
444433
// if the mutex is already owned, then we are in user code
445434
// in this file which will do a tud_task itself, so we'll just do nothing
@@ -451,7 +440,7 @@ static void usb_irq() {
451440
}
452441

453442
static int64_t timer_task(__unused alarm_id_t id, __unused void *user_data) {
454-
irq_set_pending(__usb_task_irq);
443+
irq_set_pending(USB.usbTaskIRQ);
455444
return USB_TASK_INTERVAL;
456445
}
457446
#endif
@@ -503,11 +492,6 @@ void USBClass::connect() {
503492
#endif
504493
}
505494

506-
507-
508-
509-
510-
511495
void USBClass::begin() {
512496
if (tusb_inited()) {
513497
// Already called
@@ -527,9 +511,9 @@ void USBClass::begin() {
527511
xTaskCreate(__freertos_usb_task, "USB", 256, 0, configMAX_PRIORITIES - 2, &usbTask);
528512
vTaskCoreAffinitySet(usbTask, 1 << 0);
529513
#else
530-
__usb_task_irq = user_irq_claim_unused(true);
531-
irq_set_exclusive_handler(__usb_task_irq, usb_irq);
532-
irq_set_enabled(__usb_task_irq, true);
514+
usbTaskIRQ = user_irq_claim_unused(true);
515+
irq_set_exclusive_handler(usbTaskIRQ, usb_irq);
516+
irq_set_enabled(usbTaskIRQ, true);
533517
add_alarm_in_us(USB_TASK_INTERVAL, timer_task, nullptr, true);
534518
#endif
535519
}

cores/rp2040/USB.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,9 @@ class USBClass {
8989
#ifdef __FREERTOS
9090
// Should probably use a semaphore or something, but this works for now
9191
volatile bool initted = false;
92+
#else
93+
// The user IRQ for the USB "task"
94+
int usbTaskIRQ;
9295
#endif
9396

9497
private:
@@ -107,9 +110,6 @@ class USBClass {
107110
struct Entry *next;
108111
} Entry;
109112

110-
// Find-first-set in a 32b quantity. Not fast, but doesn't need to be
111-
int ffs(uint32_t v);
112-
113113
// Add or remove Entry in a linked list, keeping things ordered by ordering
114114
uint8_t addEntry(Entry **head, int interfaces, const uint8_t *descriptor, size_t len, int ordering, uint32_t vidMask);
115115
void removeEntry(Entry **head, unsigned int localid);
@@ -161,4 +161,3 @@ class USBClass {
161161
extern USBClass USB;
162162

163163
#endif
164-

0 commit comments

Comments
 (0)