ARM:0018 STM32CubeMX hack – printer class device

This article is about printer class device for STM32F103 and STM32CubeMX.
Somehow there is NO printer device class example in whole internet. There ar CDC or HID examples copied from same STM32CubeMX example with minimal explanation, few of mass storage device samples (without real usability), some audio device. But there is no PRINTER CLASS example at all. Looks like that everybody is using printers and nobody is designing printers. Meanwhile, printer is very simple device- it receives some data from host and reports few bytes of status data. So here is my project- printer class device for STM32F103 MCU. Project is based of canned CDC example, with light modifications. I don’t know how to make CubeMX templates. So if you rebuild project using Cube software, some of files will be overwritten, some added. Do not rebuild project using cube.

What my project can do:

  • Connects to host computer (Windows, Mac, Linux) and enumerates as printer class device.
  • Printer reports 1248 id string. If string is properly coded, host OS recognizes printer model. Example printer is “text only” – no drivers needed.
  • Printer can “print”- example code just translate raw data using USART. Error state, “paper out” is send to host, but interpretation of data depens on OS and printer driver used.
  • Printer is fully “plug and play”.
  • I don’t have enough documentation about “MS descriptor”. Theoretically it is special descriptor request (0xEE), but somehow it is not working. Maybe it is dependant of OS and printer driver.

How to make clone of my project or incorporate to you existing desing. Start STM32CubeMX, configure all your pins and devices, select USB device and CDC class. Then, manually remove all the stuff related to CDC class and copy printer class files. There are minor changes (not very useful) in USB core library (mainly for MS Windows tricks), it is noted in comments as “levo”.

The only interesting file for end user is “printer_hardware.c” – it is few subroutines to pass control for USB device to physical printer software.  It is like interface for printer interface :).

Older version of software, for analysis only:

Download STM32CubeMX USB PRINTER CLASS demo code (source and compiled hex for bluepill STM32F103C8.

There was error is “plug & play” section. Small change, but it caused bigger change in other source code- printer response is not always same size as host request size. This caused buffer overrun and garbage reported to host. Also, there was error in 1284 string report- I forgot to add size of the report to the body of report (this is compatibility with LPT cable) :

/* ************************************************************************
Low-level function to report 1284 id string
Input: pointer to buffer
Returns: length of the string
************************************************************************ */
uint16_t printer_hardware_id(unsigned char * pbuf)
static unsigned char Id1284String[] = "00MFG:Generic;MDL:Generic_/_Text_Only;CMD:1284.4;CLS:PRINTER;DES:TTY;";

uint16_t length=sizeof(Id1284String);
Id1284String[0]=(length >>8) & 0xFF;
Id1284String[1]=length & 0xFF;
// IEEE 1284 device ID string (including length in the first two bytes in big endian format).
return length;

USB report
Better version of printer device source code:
New version of USB PRINTER device for STM32f103 (STM32CunbeMX) with proper lenght in printer internals description.

About Administrator

I am owner of this site.
This entry was posted in Anything, MCU and tagged , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *