How am I supposed to use the USB component on an MKL26z32

Showing results for 
Show  only  | Search instead for 
Did you mean: 

How am I supposed to use the USB component on an MKL26z32

Contributor II

How am I supposed to use the USB peripheral on an MKL26z32 when the generated code for an empty(!) project with USB HID in Kinetis Design studio already overflows the available Flash of 32k by 3k?

I generated an empty project in KDS and after building that "empty" project for the first time it generated 10k code and after enabling -Os and -flto it was still 8k.

Then I added the usb hid device component, configured it for "mouse" and it overflowed the available flash.

What is the purpose of the USB-Peripheral in the KL26z32 if it can't be used with Kinetis IDE software? What are my Options now? Should I spend the time to learn the lowest levels of USB communication and write my own USB driver from scratch, thereby reducing the needed size from 35k to probably less than 5k? Or maybe has someone else already written a bare bones minimal driver for this peripheral together with a simple demo app, preferrably completely IDE-agnostic (plain and simple makefile project)? Does something like that exist?

Labels (2)
3 Replies

Contributor II

I can now answer my own question, and maybe others might find this helpful too. There is a way to use the USB in lightweight bare metal projects if one does not use any of the Kinetis SDK and Design Studio/PE components. If all the tons of abstraction layers and indirections and support for features one does not want to use are left out then a working bare metal generic USB HID device driver can be written from scratch in a very straightforward and astonishingly simple way that will result in an USB-enabled hello world project with less than 2500 Bytes :smileyalert: flash requirements.


I took Kevin Cuzner's excellent work here Teensy 3.1 bare metal: Writing a USB driver | Projects & Libraries as a starting point, ported it to the KL25z FRDM board I had lying around here (It should later be possible to port this to the KL26 easily), refactored and rewrote parts of it and added a bidirectional endpoint 1.

The main obstacles I encountered were the miserable and incomplete documentation of the Kinetis USB periperal and a complete lack of any official example code (code that demonstrates the usage of the peripheral itself and not code that merely demonstrates the incredible ability of Freescale engineers to build monstrous Layers upon Layers upon Layers of convoluted abstraction and indirection that becomes evermore obfuscated and bloated with every new version), so quite some reverse engineering and trial and error had to be done to understand the USB peripheral and still not all questions are answered.

The resulting example project I have published here: GitHub - prof7bit/frdm-kl25z-minimal-usb-hid: minimal USB HID implementation for FRDM-KL25Z demo boa... 

NXP Employee
NXP Employee

Hi Bernd Kreuss 

Please make sure that you clean up your project before you rebuild it with the optimization settings (right click in project folder->Clean Project). I make a simple usb-mouse project without optimizations and it was 35K but after enabling -Os, cleaning it up, and rebuilding it, the size reduce to 20K.


Hope this helps
Have a great day,
Jorge Alcala

Note: If this post answers your question, please click the Correct Answer button. Thank you!

0 Kudos

Specialist V

Hi Bernd

uTasker KL26 framework = 6'362 bytes of Flash (scheduler, general system drivers, etc.)
uTasker KL26 framework with USB mouse = 13'531 bytes of Flash (FS USB driver + HID mouse class and operation)
uTasker KL26 framework with USB-CDC application = 14'083 bytes of Flash (FS USB driver + CDC class and operation)
uTasker KL26 framework with USB-CDC application and HID MOUSE (composite) = 14'563 bytes of Flash (FS USB driver + CDC class and operation + HID class and mouse operation)
uTasker KL26 framework with USB-CDC application and HID MOUSE and keyboard (composite) = 15'431 bytes of Flash (FS USB driver + CDC class and operation + HID class and mouse + keyboard operation)

Tested on the Teensy LC and Freedom boards (setup for 32k/4k memory) /

Built with make file (GCC with -Os) [also builds with KDS, CW, S32 Design Studio, IAR, Keil, Atollic, Rowley, Green Hills, CooCox]

Flash consumption (on 32k/4k part) is not a problem generally but the SRAM may be a little tight together with further application requirements. The NXP examples probably are not designed for efficiency and potentially will be worse for SRAM consumption ((?) the more critical part) because larger Flash/RAM chips are available that are more interesting for sales.

The above references are industrially proven in various products (> 5 years) and thus robust, efficient, documented and can be simulated in the uTasker Kinetis simulator for further analysis, extension etc. With professional personal support if desired or FREE if not (but still with basic supported in this forum).


Kinetis for professionals:

0 Kudos