{"id":49405642,"url":"https://github.com/dgatf/usb_library_rp2040","last_synced_at":"2026-04-28T21:01:09.598Z","repository":{"id":347834237,"uuid":"859291538","full_name":"dgatf/usb_library_rp2040","owner":"dgatf","description":"A fast and lightweight USB device library for RP2040.","archived":false,"fork":false,"pushed_at":"2026-04-23T17:27:47.000Z","size":92,"stargazers_count":7,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"main","last_synced_at":"2026-04-23T19:26:24.621Z","etag":null,"topics":["library","rp2040","usb"],"latest_commit_sha":null,"homepage":"","language":"C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/dgatf.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2024-09-18T12:20:14.000Z","updated_at":"2026-04-23T17:24:09.000Z","dependencies_parsed_at":null,"dependency_job_id":null,"html_url":"https://github.com/dgatf/usb_library_rp2040","commit_stats":null,"previous_names":["dgatf/usb_library_rp2040"],"tags_count":3,"template":false,"template_full_name":null,"purl":"pkg:github/dgatf/usb_library_rp2040","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dgatf%2Fusb_library_rp2040","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dgatf%2Fusb_library_rp2040/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dgatf%2Fusb_library_rp2040/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dgatf%2Fusb_library_rp2040/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dgatf","download_url":"https://codeload.github.com/dgatf/usb_library_rp2040/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dgatf%2Fusb_library_rp2040/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":32399010,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-28T19:38:08.556Z","status":"ssl_error","status_checked_at":"2026-04-28T19:37:55.688Z","response_time":56,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["library","rp2040","usb"],"created_at":"2026-04-28T21:01:00.350Z","updated_at":"2026-04-28T21:01:09.591Z","avatar_url":"https://github.com/dgatf.png","language":"C","funding_links":[],"categories":[],"sub_categories":[],"readme":"# USB Device Library for RP2040\n\nA fast and lightweight USB device library for the RP2040.\n\n## Features\n\n- Supports control, bulk, isochronous, and interrupt transfers\n- Supports fixed-length transfers\n- Around 1.1 MB/s for bulk transfers in the included benchmark\n- Supports up to 32 endpoints\n- Supports single and double buffering\n- Interrupt-driven\n\nCompatible with the [Raspberry Pi Pico SDK](https://raspberrypi.github.io/pico-sdk-doxygen/).\n\n## Usage\n\nTo use the library:\n\n- Add `usb.h`, `usb.c`, `usb_common.h`, `usb_config.h`, and `usb_config.c` to your project.\n- Add the required libraries (`pico_stdlib`, `hardware_irq`) to your `CMakeLists.txt`. See [`src/CMakeLists.txt`](src/CMakeLists.txt).\n- Configure endpoints, handlers, and buffers in `usb_config.h` and `usb_config.c`. Do not modify the EP0 endpoints.\n- Use `bInterval` to adjust the polling interval: `0` = default, `1` = fastest, `16` = slowest.\n- Each endpoint must have a valid `data_buffer`.\n- Endpoint callbacks are called once when the transfer completes.\n- All transfers are finite and length-bounded by the `len` value passed to `usb_init_transfer()`. This does not apply to EP0.\n- Applications that need continuous data transfer should chain fixed-length transfers at application level.\n- Isochronous packet size 1024 cannot be used, because the RP2040 hardware limit is 1023 bytes.\n- `wMaxPacketSize` must be a multiple of 64.\n- Double buffering can be used with `wMaxPacketSize` values of 64, 128, 256, and 512. Sizes 128, 256, and 512 are supported only for isochronous transfers.\n- For maximum bulk transfer speed, use `double_buffer = true` and `bInterval = 1`.\n- For maximum isochronous transfer speed, use `double_buffer = false`, `bInterval = 1`, and `wMaxPacketSize = 960`.\n\n## API\n\n### `void usb_device_init(void)`\n\nInitializes the USB peripheral in device mode.\n\n### `bool usb_is_configured(void)`\n\nReturns `true` if the device has been configured by the host.\n\n### `bool usb_init_transfer(uint8_t addr, uint len)`\n\nStarts a fixed-length transfer on the endpoint address.\n\nParameters:  \n`addr` - endpoint address  \n`len` - transfer length\n\nReturns `true` if the transfer was started.\n\n### `void usb_cancel_transfer(uint8_t addr)`\n\nCancels the active transfer on the endpoint address.\n\nParameters:  \n`addr` - endpoint address\n\n### `uint8_t usb_get_address(void)`\n\nReturns the current USB device address.\n\n### `uint8_t *usb_get_endpoint_buffer(uint8_t addr)`\n\nReturns the configured endpoint data buffer.\n\nParameters:  \n`addr` - endpoint address\n\n### `uint usb_get_endpoint_buffer_size(uint8_t addr)`\n\nReturns the configured endpoint data buffer size.\n\nParameters:  \n`addr` - endpoint address\n\n### `void usb_set_endpoint_buffer(uint8_t addr, uint8_t *buf)`\n\nSets the data buffer for the specified endpoint.\n\nParameters:\n`addr` - endpoint address\n`buf` - pointer to the data buffer\n\n### `bool usb_is_busy(uint8_t addr)`\n\nReturns `true` if the endpoint currently has an active transfer.\n\nParameters:  \n`addr` - endpoint address\n\n## Callback Functions\n\n### `void control_transfer_handler(uint8_t *buf, volatile struct usb_setup_packet *pkt, uint8_t stage)`\n\nCalled when a setup packet is received on EP0. It is invoked in three stages: setup, data, and status.\n\nParameters:  \n`buf` - EP0 data buffer  \n`pkt` - setup packet  \n`stage` - `STAGE_SETUP`, `STAGE_DATA`, or `STAGE_STATUS`\n\n### `void ep_handler(uint8_t *buf, uint16_t len)`\n\nEndpoint callback.\n\nCalled once when the endpoint transfer completes.\n\nFor OUT endpoints, `buf` points to the endpoint data buffer containing the received data.  \nFor IN endpoints, `buf` points to the endpoint data buffer that was transmitted.  \n`len` is the number of bytes transferred.\n\nParameters:  \n`buf` - endpoint data buffer  \n`len` - number of transferred bytes\n\n## TinyUSB Comparison\n\nComparing the output of [`usb_speed_test.py`](utils/usb_speed_test.py) for both implementations, this library and TinyUSB (commit dated [2024-09-17](https://github.com/hathach/tinyusb/tree/f4dd1764849d005a2e44d51f62428aeaf2513804)), the following results were obtained:\n\n\u003cp align=\"center\"\u003e\u003cimg src=\"./utils/comparison.png\" width=\"600\"\u003e\u003cbr\u003e\u003c/p\u003e\n\n### This library\n\n```text\nRequest REQ_EP0_OUT. Size: 4096 bytes. Speed: 520 kB/s\nRequest REQ_EP0_IN. Size: 4096 bytes. Speed: 429 kB/s\nRequest REQ_EP1_OUT. Size: 40000 bytes. Speed: 1093 kB/s\nRequest REQ_EP2_IN. Size: 40000 bytes. Speed: 1109 kB/s\n```\n\n### TinyUSB\n\n```text\nRequest REQ_EP0_OUT. Size: 4096 bytes. Speed: 481 kB/s\nRequest REQ_EP0_IN. Size: 4096 bytes. Speed: 641 kB/s\nRequest REQ_EP1_OUT. Size: 30000 bytes. Speed: 500 kB/s\nRequest REQ_EP2_IN. Size: 30000 bytes. Speed: 631 kB/s\n```\n\n### Conclusion\n\n```text\nEP0 OUT:      +8.11%\nEP0 IN:      -33.07%\nBULK OUT:   +118.60%\nBULK IN:     +75.75%\n```\n\nIn this benchmark, the library outperforms TinyUSB for bulk transfers. Isochronous and interrupt transfers are not supported by TinyUSB.\n\nResults may vary slightly depending on transfer size and endpoint configuration.\n\n## Limitations\n\n- USB host mode is not supported. If you need host support, use TinyUSB.\n- USB classes are not implemented. You must implement the class yourself or use TinyUSB.\n- Only one configuration and one interface are currently supported.\n\n## References\n\n- [RP2040 Datasheet](https://datasheets.raspberrypi.com/rp2040/rp2040-datasheet.pdf)\n- [RP2040 SDK](https://raspberrypi.github.io/pico-sdk-doxygen/)\n- [Example: dev_lowlevel](https://github.com/raspberrypi/pico-examples/tree/master/usb/device/dev_lowlevel)\n- [USB in a Nutshell](https://www.beyondlogic.org/usbnutshell/usb1.shtml)","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdgatf%2Fusb_library_rp2040","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdgatf%2Fusb_library_rp2040","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdgatf%2Fusb_library_rp2040/lists"}