USB for the TWR-K21F120M

Finally my Freescale TWR-K21F120M board arrived 🙂

TWR-K21F120M on the bench

TWR-K21F120M on the bench

For the next generation of INTRO Zumo robots I’m evaluating a new ‘brain’: more processing power, more FLASH, more RAM, more of everything 😉

TWR-K21F120M Board

The board comes assembled, with one micro-B USB cable, a CR2025 coin cell battery and quickstart guide.

💡 Be very careful with these micro-B USB connectors! The cables lock into the connector, and some force is used to pull them off. As the connectors are SMD ones, they easily could get ripped of the board if you are not careful!

TWR-K21F120M Board Front Side

TWR-K21F120M Board Front Side

The board has 4 user LED’s, potentiometer, an accelerometer (MMA8451Q), extension sockets (TWRPI) two push buttons, and on the back side an SD card socket:

TWR-K21F120M Board Back Side

TWR-K21F120M Board Back Side

On the back side you see as well the S08JM60 CPU responsible for the onboard debug interface (OSJTAG).

💡 The OSJTAG is not very fast. I recommend to use an external TAP (P&E Universal Multilink or Segger J-Link) with the onboard JTAG header.

The heart of the board is the MK21FN1M0V12 in BGA package:



In a small and tiny package I get

  1. ARM Cortex-M4F with Hardware Floating Point Unit
  2. Up to 120 MHz
  3. 1 MB FLASH
  4. 128 KByte SRAM (splitted up in two 64 KByte areas)

💡 I probably will go with a QFN package, as even if I manufacture the new robot board outside, that BGA package is not very hobby friendly.

So plenty of space and performance for my bot :-).


The onboard OSJTAG interface features a serial-to-USB bridge. As I do not plan to add the OSJTAG debug interface on my board, and as I need a serial connection to my robot, my first concern was to bring up USB CDC on the K21F120M.

The port of the Freescale USB Baremetal stack I have has not supported that K21F120M yet, so I have extended the FSL_USB_Stack component for the K21F120M:

K21F120M in USB CDC stack component

K21F120M in USB CDC stack component


My USB CDC component uses ring buffers for Tx and Rx:

USB CDC with RingBuffers

USB CDC with RingBuffers

The implementation was limited to use a byte as buffer entry, and a buffer was only able to have up to 256 elements.

Now the component has been extended for different element size, increased buffer size and an option to have it using reentrant or non-reentrant access to the buffer:

RingBuffer Properties

RingBuffer Properties

RingBuffer Methods and Events

RingBuffer Methods and Events


USB CDC device is working fine now for the K21F120M, and I made a quick port of my robot application to the K21F120M which is looking good. The USB HID keyboard and mouse plus MSD Host should be working too, but I have not tested it yet. I’m still not decided on the CPU, and I need to have a look at the Kinetis K22 too, as that one even looks a better choice for what I’m looking for?

My current project and components are available on GitHub:


Happy Evaluating 🙂

17 thoughts on “USB for the TWR-K21F120M

  1. Pingback: Processor Expert Driver Suite V10.3 Available | MCU on Eclipse

  2. Here is how to make your own k22 freedom board.
    Take a FRDM-KL25Z board and replace the processor with a mk22fn1m0vlk12. Then connect PTB11 and PTC13 to Vcc, PTB10 and PTC12 to Gnd.


  3. Hi Erich, I testing MK22,
    question: CDC send String( “mystring”) = ok
    send Buffer( hex ) to pc, not send
    ex: CDC1_App_Task(mybuf, 16) or CDC1_SendDataBlock( mybuf, 16)<–not send


    • Hi Carlos,
      Hi Carlos,
      I do not have a MK22, but I tried with KL25Z (should not matter) and it works for me:
      CDC1_SendBlock((unsigned char*)”hello?\r\n”, sizeof(“hello?\r\n”)-1);
      Keep in mind that the buffer for
      CDC1_App_Task(mybuf, 16)
      must be different for the buffer you use for SendBlock().


  4. Hi Mr. Styger

    I am currently trying to integrate your USB-stack into my solution on TWR-K21F120M board.
    Unfortunately the code crashes (bus fault) at line 237 in USB0.c file. I think there is something wrong with the settings of the component. Can you send me the right ones?

    I am using Eclispe with PE-plugin and IAR for debugging. I do not have a license for Codewarrior, so I am not able to extract the settings from your example project.



  5. Pingback: USB CDC with the FRDM-K64F, finally! | MCU on Eclipse

  6. So I modified this project to attempt to test the USB on a custom K22 board, but I can’t get the cdc.inf file to install properly in Windows 7. I am able to build and flash the program onto the board, and run the board. The USB device shows up as ‘Unknown device’, and when I attempt to follow the steps to install the cdc.inf file, I can get to the point of selecting the .inf file. Windows then gives me an error stating that the folder does not contain a driver that is compatible with the device. It notes that if the folder does contain the driver for the device, ensure that the driver is compatible with x64 Windows. Any ideas?


    • Which version of my Processor Expert USB components are you using? I have found that for the K64F ( the MMU needs to be disabled. Same applies to the FRDM-K22F. So are you using the K22F which is on the FRDM-K22F board, or the ‘old’ K22F? I ask because even if they have the same name, they are *different*.
      The other point is that usually if the USB enumeration does not work, then there is a problem with the clock setup to the USB module.
      I have added the FRDM-K22F to the USB stack, but I have not released it yet (there was no time last week-end for a release).

      I hope this helps,


      • I’m using the latest from here: I’m actually using a K22FX512VMC12. I set up the clock as described in the K64F link. If it matters, I’m using KDSv1.1.1. I tried copying the components and source code into a KDS project (since I don’t have the CW libraries to link to), and I was able to get the device to work (albeit with the rather interesting behaviour of echoing back every character instead of every line). I’ll double-check the clock settings, since these threw me for a loop when I first tried this on the TWRK21.


        • Ok, so it seems that the USB stack is working. Are you following the Documentation\cdc.inf_readme.txt to the letter? The issue is that you need to manually select the driver, and *not* let Windows pick it. I have pasted the instructions below.

          readme for USB CDD driver installation (cdc.inf)

          This cdc.inf is based on following forum thread:

          For your windows system, you need to make sure drivers are correctly installed.
          For this go into your Windows Device manager. Your CDC device probably shows up as “JM CDC DEMO”.

          Update the driver and point to the generated cdc.inf file in the project documentation folder.

          Important: follow the steps below to install/update the driver:
          – Go to the Device manager and identify the device. Perform an ‘update driver’
          – IMPORTANT: Select ‘Browse my computer for driver software’
          – IMPORTANT: select ‘let me pick from a list of device drivers on my computer’
          – use ‘show all devices’, use ‘have disk’ and to browse to the cdc.inf file
          – this will show up as ‘Freescale CDC device’ 🙂

          See as well following post:


      • Ouch. I just realized that I erred in my previous post. When I tried copying the files and whatnot over from this project, I copied them into another project for the TWR-K21F120. That was what worked (with quirks). The K22 seems to get caught in the enumeration loop and never leave.


Leave a Reply to Erich Styger Cancel reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.