Skip to content

Conversation

@remiberthoz
Copy link

@remiberthoz remiberthoz commented Jan 4, 2026

Describe the PR
Implement a printer device driver for USB Printers. The example illustrates receiving data from a host, the device driver as-is is recognized by the generic printer drivers on linux (and I assume, Windows too).

This allows sending binary data to the device, with no specific drivers, using tools like lp -o raw.

Additional context
The example compiles and works on a Pi Pico.

I am not yet sure about integration of the changes in the build toolchain. I have not yet understood if I have to manually edit all the hw/{bsp,mcu}/ file to include the device driver in cmake source lists.

if (!_printer_fifo_cfg.tx_persistent) {
tu_fifo_clear(&p_printer->tx_ff);
}
// tu_fifo_set_overwritable(&p_printer->rx_ff, true);

Check notice

Code scanning / CodeQL

Commented-out code Note

This comment appears to contain commented-out code.
I apparently made a mistake when copying/renaming this file, file
content was that of printer_device.c.
@remiberthoz
Copy link
Author

Well I apparently made (at least) one major mistake when moving the code from my local project to the repository fork. The content of printer.h and printer_device.c is the same, leading to (part of) the compilation errors. I'll work on that and the other problems raised by the CI pipeline.

@remiberthoz
Copy link
Author

Ok, I'm realizing that I developed this locally against version 0.18.0 but tried to merge it in version 0.20.0. This is not the only problem but I am back on track. I'll see what was changed between the two releases and update the device driver accordingly.

@remiberthoz
Copy link
Author

remiberthoz commented Jan 7, 2026

In my terminal, compilation and upload of the printer_to_hid example now correctly yields :

$ cat helloworld.ps  # see helloworld.ps content
  %!PS
  8 selectfont
  (Hello world!) show

$ lp -d TinyUSB_Device -o raw ./helloworld.ps  # print helloworld.ps on the device
  request id is TinyUSB_Device-330 (1 file(s))

$   PS 8 selectfont  Hello world   show  # auto-typed by HID

The last line is automatically typed by the HID device. Only alphanumeric characters are handled by the example program, but other characters could be translated to HID keycodes too. The printer endpoint receives UTF8.


CircleCI has caught one remaining [[maybe_unused]] flag that I'll replace with a (void) (in the one-random-make-msp430-large-msp430-gcc test). I believe the other failing tests are not directly related to the printer device class or its example, but to the build toolchain that is not correctly adjusted yet.

@remiberthoz remiberthoz marked this pull request as ready for review January 8, 2026 17:56
@remiberthoz
Copy link
Author

Looking forward to your feedback!

I believe I’ve addressed the issues in the sections of code I added. If there’s anything else you'd like me to revisit or any concerns with the implementation, please let me know. I’m happy to make further adjustments.

@github-actions
Copy link

Size Difference Report

Because TinyUSB code size varies by port and configuration, the metrics below represent the averaged totals across all example builds.

Note: If there is no change, only one value is shown.

Changes >1% in size

file .text .bss size % diff
dcd_musb.c 2431 ➙ 2079 (-352) 160 2591 ➙ 2239 (-352) -13.6%
TOTAL 2431 ➙ 2079 (-352) 160 2591 ➙ 2239 (-352) -13.6%

Changes <1% in size

No entries.

No changes
file .text .rodata .data .bss size % diff
audio_device.c 2849 0 1248 1673 4518 +0.0%
cdc_device.c 1328 16 19 661 1988 +0.0%
cdc_host.c 6596 487 15 1539 8357 +0.0%
dcd_ch32_usbfs.c 1472 0 0 2444 3916 +0.0%
dcd_ch32_usbhs.c 1648 0 0 448 2096 +0.0%
dcd_ci_fs.c 1925 0 0 1290 3215 +0.0%
dcd_ci_hs.c 1762 0 0 1280 2530 +0.0%
dcd_da146xx.c 3067 0 0 144 3211 +0.0%
dcd_dwc2.c 4101 24 0 263 4387 +0.0%
dcd_eptri.c 2270 0 0 259 2529 +0.0%
dcd_khci.c 1953 0 0 1290 3243 +0.0%
dcd_lpc17_40.c 1470 0 0 648 1794 +0.0%
dcd_lpc_ip3511.c 1463 0 0 264 1639 +0.0%
dcd_mm32f327x_otg.c 1478 0 0 1290 2768 +0.0%
dcd_msp430x5xx.c 1796 0 0 176 1972 +0.0%
dcd_nrf5x.c 2919 0 0 292 3211 +0.0%
dcd_nuc120.c 1093 0 0 78 1171 +0.0%
dcd_nuc121.c 1167 0 0 101 1268 +0.0%
dcd_nuc505.c 0 0 1529 157 1686 +0.0%
dcd_rp2040.c 859 20 604 655 2138 +0.0%
dcd_rusb2.c 2917 0 0 156 3073 +0.0%
dcd_samd.c 1032 0 0 266 1298 +0.0%
dcd_samg.c 1319 0 0 72 1391 +0.0%
dcd_stm32_fsdev.c 2556 0 0 291 2847 +0.0%
dfu_device.c 744 28 712 183 926 +0.0%
dfu_rt_device.c 156 0 134 0 156 +0.0%
dwc2_common.c 582 29 0 0 597 +0.0%
ecm_rndis_device.c 1037 0 1 2272 3310 +0.0%
ehci.c 2761 0 0 5970 7537 +0.0%
fsdev_common.c 180 0 0 0 180 +0.0%
hcd_ch32_usbfs.c 2484 0 0 498 2982 +0.0%
hcd_ci_hs.c 190 0 0 0 190 +0.0%
hcd_dwc2.c 4893 30 0 512 5434 +0.0%
hcd_khci.c 2442 0 0 449 2891 +0.0%
hcd_musb.c 3073 0 0 157 3230 +0.0%
hcd_pio_usb.c 262 0 240 0 502 +0.0%
hcd_rp2040.c 976 73 416 384 1849 +0.0%
hcd_rusb2.c 2923 0 0 245 3168 +0.0%
hcd_samd.c 2220 0 0 324 2544 +0.0%
hcd_stm32_fsdev.c 3282 0 1 420 3703 +0.0%
hid_device.c 1118 44 997 115 1233 +0.0%
hid_host.c 1206 0 0 1250 2456 +0.0%
hub.c 1235 8 8 29 1268 +0.0%
midi_device.c 1127 0 991 589 1714 +0.0%
midi_host.c 1353 7 7 3740 5097 +0.0%
msc_device.c 2503 108 2274 538 3041 +0.0%
msc_host.c 1589 0 0 394 1984 +0.0%
mtp_device.c 1689 22 1449 579 2275 +0.0%
ncm_device.c 1514 28 1408 5830 7358 +0.0%
ohci.c 1942 0 0 2414 4356 +0.0%
rp2040_usb.c 172 75 718 4 969 +0.0%
rusb2_common.c 160 0 16 0 176 +0.0%
tusb.c 416 0 359 3 418 +0.0%
tusb_fifo.c 843 0 477 0 838 +0.0%
typec_stm32.c 820 8 2 12 842 +0.0%
usbc.c 420 2 20 166 608 +0.0%
usbd.c 3177 57 89 276 3518 +0.0%
usbd_control.c 523 0 474 78 600 +0.0%
usbh.c 4374 60 63 954 5415 +0.0%
usbtmc_device.c 2176 24 69 291 2500 +0.0%
vendor_device.c 624 0 530 464 1087 +0.0%
video_device.c 4391 5 1851 472 4855 +0.0%
TOTAL 110617 1155 16721 45349 158053 +0.0%

@HiFiPhile
Copy link
Collaborator

Hi, thank you for adding printer driver ! I've just tested that it's working on Windows 11 with a manual driver install, I'll leave my reviews below.

@remiberthoz
Copy link
Author

Thank you, I am looking forward to feedback. The tests I could perform on Windows indeed reveal that detection of the printer is not 100% automatic, and there is a need to "install" the printer (same on linux) from the configuration menu. At least there is no need to use admin privileges, download or execute dependencies, which is sufficient for my use case.

In the interest of TinyUSB as a whole, however, it is interesting to see if that can be improved. I suppose the printer description, which is currently hardcoded here : https://github.com/hathach/tinyusb/pull/3444/files#diff-d649fb44fa8e4a9383e3d84eefb7efd336196be0c8dc66028dd949b45b531437R247 can be adjusted. Switching to a config-defined value is an obvious improvement.

I am also trying to understand what the printer device is supposed to respond to the host on the BULK IN endpoint.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants