55// pio-usb is required for rp2040 host
66#include " pio_usb.h"
77#include " Adafruit_TinyUSB.h"
8+ #include " pico/stdlib.h"
89
910// Pin D+ for host, D- = D+ + 1
1011#ifndef PIN_USB_HOST_DP
@@ -34,20 +35,14 @@ void loop() {
3435
3536void setup1 () {
3637
38+ // override tools menu CPU frequency setting
39+ set_sys_clock_khz (120'000 , true );
40+
3741#if 0
3842 while ( !Serial ) delay(10); // wait for native usb
3943 Serial.println("Core1 setup to run TinyUSB host with pio-usb");
4044#endif
4145
42- // Check for CPU frequency, must be multiple of 120Mhz for bit-banging USB
43- uint32_t cpu_hz = clock_get_hz (clk_sys);
44- if ( cpu_hz != 120000000UL && cpu_hz != 240000000UL ) {
45- while ( !Serial ) delay (10 ); // wait for native usb
46- Serial.printf (" Error: CPU Clock = %lu, PIO USB require CPU clock must be multiple of 120 Mhz\r\n " , cpu_hz);
47- Serial.printf (" Change your CPU Clock to either 120 or 240 Mhz in Menu->CPU Speed \r\n " );
48- while (1 ) delay (1 );
49- }
50-
5146#ifdef PIN_5V_EN
5247 pinMode (PIN_5V_EN, OUTPUT);
5348 digitalWrite (PIN_5V_EN, PIN_5V_EN_STATE);
@@ -68,9 +63,11 @@ void setup1() {
6863
6964int old_ascii = -1 ;
7065uint32_t repeat_timeout;
71- const uint32_t repeat_time = 150 ;
66+ // this matches Linux default of 500ms to first repeat, 1/20s thereafter
67+ const uint32_t default_repeat_time = 50 ;
68+ const uint32_t initial_repeat_time = 500 ;
7269
73- void send_ascii (uint8_t code) {
70+ void send_ascii (uint8_t code, uint32_t repeat_time=default_repeat_time ) {
7471 old_ascii = code;
7572 repeat_timeout = millis () + repeat_time;
7673 if (code > 32 && code < 127 ) {
@@ -86,9 +83,8 @@ void loop1()
8683 uint32_t now = millis ();
8784 uint32_t deadline = repeat_timeout - now;
8885 if (old_ascii >= 0 && deadline > INT32_MAX) {
89- repeat_timeout += repeat_time;
90- deadline = repeat_timeout - now;
9186 send_ascii (old_ascii);
87+ deadline = repeat_timeout - now;
9288 } else if (old_ascii < 0 ) {
9389 deadline = UINT32_MAX;
9490 }
@@ -133,7 +129,7 @@ const char * const lut[] = {
133129 " !@#$%^&*()" , /* 0 - shifted numeric keys */
134130 " \r\x1b\10\t -=[]\\ #;'`,./" , /* 1 - symbol keys */
135131 " \n\x1b\177\t _+{}|~:\" ~<>?" , /* 2 - shifted */
136- " \3\4\2\1 " , /* 3 - arrow keys RLDU */
132+ " \12\13\10\22 " , /* 3 - arrow keys RLDU */
137133 " /*-+\n 1234567890." , /* 4 - keypad w/numlock */
138134 " /*-+\n\xff\2\xff\4\xff\3\xff\1\xff\xff ." , /* 5 - keypad w/o numlock */
139135};
@@ -217,7 +213,7 @@ void process_event(uint8_t dev_addr, uint8_t instance, const hid_keyboard_report
217213 }
218214 if (ctrl) code &= 0x1f ;
219215 if (alt) code ^= 0x80 ;
220- send_ascii (code);
216+ send_ascii (code, initial_repeat_time );
221217 break ;
222218 }
223219 }
0 commit comments