fix dual example for rp2350 conflict printf and cdc_printf

This commit is contained in:
hathach
2025-05-13 16:27:26 +07:00
parent ed087b9ed8
commit fe4446090e
3 changed files with 33 additions and 25 deletions

View File

@@ -190,7 +190,9 @@ void tuh_hid_umount_cb(uint8_t dev_addr, uint8_t instance) {
// look up new key in previous keys // look up new key in previous keys
static inline bool find_key_in_report(hid_keyboard_report_t const* report, uint8_t keycode) { static inline bool find_key_in_report(hid_keyboard_report_t const* report, uint8_t keycode) {
for (uint8_t i = 0; i < 6; i++) { for (uint8_t i = 0; i < 6; i++) {
if (report->keycode[i] == keycode) return true; if (report->keycode[i] == keycode) {
return true;
}
} }
return false; return false;
@@ -230,7 +232,9 @@ static void process_kbd_report(uint8_t dev_addr, hid_keyboard_report_t const* re
// TODO example skips key released // TODO example skips key released
} }
if (flush) tud_cdc_write_flush(); if (flush) {
tud_cdc_write_flush();
}
prev_report = *report; prev_report = *report;
} }

View File

@@ -78,6 +78,22 @@ static void print_device_info(uint8_t daddr, const tusb_desc_device_t* desc_devi
void led_blinking_task(void); void led_blinking_task(void);
void cdc_task(void); void cdc_task(void);
#define cdc_printf(...) \
do { \
char _tempbuf[256]; \
char* _bufptr = _tempbuf; \
uint32_t count = (uint32_t) sprintf(_tempbuf, __VA_ARGS__); \
while (count > 0) { \
uint32_t wr_count = tud_cdc_write(_bufptr, count); \
count -= wr_count; \
_bufptr += wr_count; \
if (count > 0){ \
tud_task(); \
tud_cdc_write_flush(); \
} \
} \
} while(0)
/*------------- MAIN -------------*/ /*------------- MAIN -------------*/
int main(void) { int main(void) {
board_init(); board_init();
@@ -160,22 +176,6 @@ void cdc_task(void) {
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
// Host Get device information // Host Get device information
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
#define cdc_printf(...) \
do { \
char _tempbuf[256]; \
char* _bufptr = _tempbuf; \
uint32_t count = (uint32_t) sprintf(_tempbuf, __VA_ARGS__); \
while (count > 0) { \
uint32_t wr_count = tud_cdc_write(_bufptr, count); \
count -= wr_count; \
_bufptr += wr_count; \
if (count > 0){ \
tud_task();\
tud_cdc_write_flush(); \
} \
} \
} while(0)
static void print_device_info(uint8_t daddr, const tusb_desc_device_t* desc_device) { static void print_device_info(uint8_t daddr, const tusb_desc_device_t* desc_device) {
// Get String descriptor using Sync API // Get String descriptor using Sync API
uint16_t serial[64]; uint16_t serial[64];
@@ -232,12 +232,12 @@ void tuh_enum_descriptor_device_cb(uint8_t daddr, tusb_desc_device_t const* desc
} }
void tuh_mount_cb(uint8_t daddr) { void tuh_mount_cb(uint8_t daddr) {
printf("mounted device %u\r\n", daddr); cdc_printf("mounted device %u\r\n", daddr);
is_print[daddr] = true; is_print[daddr] = true;
} }
void tuh_umount_cb(uint8_t daddr) { void tuh_umount_cb(uint8_t daddr) {
printf("unmounted device %u\r\n", daddr); cdc_printf("unmounted device %u\r\n", daddr);
is_print[daddr] = false; is_print[daddr] = false;
} }
@@ -249,7 +249,9 @@ void led_blinking_task(void) {
static bool led_state = false; static bool led_state = false;
// Blink every interval ms // Blink every interval ms
if (board_millis() - start_ms < blink_interval_ms) return; // not enough time if (board_millis() - start_ms < blink_interval_ms) {
return;// not enough time
}
start_ms += blink_interval_ms; start_ms += blink_interval_ms;
board_led_write(led_state); board_led_write(led_state);
@@ -300,7 +302,9 @@ static int _count_utf8_bytes(const uint16_t *buf, size_t len) {
} }
static void print_utf16(uint16_t *temp_buf, size_t buf_len) { static void print_utf16(uint16_t *temp_buf, size_t buf_len) {
if ((temp_buf[0] & 0xff) == 0) return; // empty if ((temp_buf[0] & 0xff) == 0) {
return;// empty
}
size_t utf16_len = ((temp_buf[0] & 0xff) - 2) / sizeof(uint16_t); size_t utf16_len = ((temp_buf[0] & 0xff) - 2) / sizeof(uint16_t);
size_t utf8_len = (size_t) _count_utf8_bytes(temp_buf + 1, utf16_len); size_t utf8_len = (size_t) _count_utf8_bytes(temp_buf + 1, utf16_len);
_convert_utf16le_to_utf8(temp_buf + 1, utf16_len, (uint8_t *) temp_buf, sizeof(uint16_t) * buf_len); _convert_utf16le_to_utf8(temp_buf + 1, utf16_len, (uint8_t *) temp_buf, sizeof(uint16_t) * buf_len);

View File

@@ -547,7 +547,7 @@ void tuh_task_ext(uint32_t timeout_ms, bool in_isr) {
// TODO better to have an separated queue for newly attached devices // TODO better to have an separated queue for newly attached devices
if (_usbh_data.enumerating_daddr == TUSB_INDEX_INVALID_8) { if (_usbh_data.enumerating_daddr == TUSB_INDEX_INVALID_8) {
// New device attached and we are ready // New device attached and we are ready
TU_LOG1("[%u:] USBH Device Attach\r\n", event.rhport); TU_LOG_USBH("[%u:] USBH Device Attach\r\n", event.rhport);
_usbh_data.enumerating_daddr = 0; // enumerate new device with address 0 _usbh_data.enumerating_daddr = 0; // enumerate new device with address 0
enum_new_device(&event); enum_new_device(&event);
} else { } else {
@@ -562,7 +562,7 @@ void tuh_task_ext(uint32_t timeout_ms, bool in_isr) {
break; break;
case HCD_EVENT_DEVICE_REMOVE: case HCD_EVENT_DEVICE_REMOVE:
TU_LOG1("[%u:%u:%u] USBH DEVICE REMOVED\r\n", event.rhport, event.connection.hub_addr, event.connection.hub_port); TU_LOG_USBH("[%u:%u:%u] USBH DEVICE REMOVED\r\n", event.rhport, event.connection.hub_addr, event.connection.hub_port);
if (_usbh_data.enumerating_daddr == 0 && if (_usbh_data.enumerating_daddr == 0 &&
event.rhport == _usbh_data.dev0_bus.rhport && event.rhport == _usbh_data.dev0_bus.rhport &&
event.connection.hub_addr == _usbh_data.dev0_bus.hub_addr && event.connection.hub_addr == _usbh_data.dev0_bus.hub_addr &&
@@ -1464,7 +1464,7 @@ static void process_enumeration(tuh_xfer_t* xfer) {
bool retry = (_usbh_data.enumerating_daddr != TUSB_INDEX_INVALID_8) && (failed_count < ATTEMPT_COUNT_MAX); bool retry = (_usbh_data.enumerating_daddr != TUSB_INDEX_INVALID_8) && (failed_count < ATTEMPT_COUNT_MAX);
if (retry) { if (retry) {
tusb_time_delay_ms_api(ATTEMPT_DELAY_MS); // delay a bit tusb_time_delay_ms_api(ATTEMPT_DELAY_MS); // delay a bit
TU_LOG1("Enumeration attempt %u/%u\r\n", failed_count+1, ATTEMPT_COUNT_MAX); TU_LOG_USBH("Enumeration attempt %u/%u\r\n", failed_count+1, ATTEMPT_COUNT_MAX);
retry = tuh_control_xfer(xfer); retry = tuh_control_xfer(xfer);
} }