Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

USB with the Freescale FRDM-K22F Board

DZone's Guide to

USB with the Freescale FRDM-K22F Board

· IoT Zone
Free Resource

The FRDM-K22F is one of the latest members of the Freedom board families: 512 KByte Flash, 128 KB RAM and the usual Freedom board components on it. Unfortunately, Freescale decided not to populate the micro-SD card connector on the board, so from this perspective the FRDM-K64F is more value for the money. But the board has USB, so this makes it still interesting. And this is what this post is about: Adding USB to the FRDM-K22F board in a few minutes…

Freescale FRDM-K22F Board

Freescale FRDM-K22F Board

Software Update

I’m using Kinetis Design Studio in this post with Processor Expert components.

Components

Components

You need the “Processor Expert Support for Kinetis K22F/KV31F @120/100MHz Service Pack” installed. You get that update with the menu Help > Install New software and use the

http://freescale.com/lgfiles/updates/Eclipse/KDS

update site:

Installed K22F Service Pack

Installed K22F Service Pack

Additionally, you need the latest McuOnEclipse Processor Expert components from SourceForge (https://sourceforge.net/projects/mcuoneclipse/files/PEx%20Components/), see “McuOnEclipse Releases on SourceForge“.

Project Creation

Create a new project for Processor Expert in Eclipse and for the MK22FN512xxx12device:

New Project for MK22FN512

New Project for MK22FN512

Do *not* select the Kinetis SDK option, only select Processor Expert:

Processor Expert Project

Processor Expert Project

Clock Configuration

The K22 can run up to 120 MHz. I configure it to maximum speed, similar to the K64F (see “FRDM-K64F at Maximum Speed of 120 MHz“). I have the 8 MHz external crystal enabled:

Enabled External 8 MHz Crystal

Enabled External 8 MHz Crystal

MCG is set to PEE with a PLL output clock of 120 MHz:

PEE with 120 MHz Clock

PEE with 120 MHz Clock

System clocks are set to the maximum possible values:

System Clocks

System Clocks

USB Configuration

Add the FSL_USB_Stack component to the project. I’m using here a USB CDC Device class. Configure it to use Init_USB_OTG_VAR0 and enable USB CDC Device class:

USB Component Configuration

USB Component Configuration

Enable the clock gate and have it to use the PLL/FLL clock with the clock divider output as clock source. To build the needed 48 MHz USB sampling clock from the 120 MHz PLL clock, use a multiplier of 2 with a divider of 5:

USB Init Configuration

USB Init Configuration

Finally, select the K22FN120 as CPU in the USB CDC sub component:

USB CPU Selected

USB CPU Selected

That’s it!

Application Code

What is missing is now the application code. It can be very simple as this:

#include <stdio.h>
static uint8_t cdc_buffer[USB1_DATA_BUFF_SIZE];
static uint8_t in_buffer[USB1_DATA_BUFF_SIZE];
 
static void CDC_Run(void) {
  int i, cnt = 0;
  uint32_t val = 0;
  unsigned char buf[16];
 
  for(;;) {
    while(CDC1_App_Task(cdc_buffer, sizeof(cdc_buffer))==ERR_BUSOFF) {
      /* device not enumerated */
      LED1_Neg(); LED2_Off();
      WAIT1_Waitms(10);
    }
    LED1_Off(); LED2_Neg();
    if (CDC1_GetCharsInRxBuf()!=0) {
      i = 0;
      while(   i<sizeof(in_buffer)-1
            && CDC1_GetChar(&in_buffer[i])==ERR_OK
           )
      {
        i++;
      }
      in_buffer[i] = '\0';
      (void)CDC1_SendString((unsigned char*)"echo: ");
      (void)CDC1_SendString(in_buffer);
      UTIL1_strcpy(buf, sizeof(buf), (unsigned char*)"val: ");
      UTIL1_strcatNum32u(buf, sizeof(buf), val);
      UTIL1_strcat(buf, sizeof(buf), (unsigned char*)"\r\n");
      (void)CDC1_SendString(buf);
      val++;
    } else {
      WAIT1_Waitms(10);
      cnt++;
      if ((cnt%1024)==0) { /* from time to time, write some text */
        (void)CDC1_SendString((unsigned char*)"Type some text and it will echo.\r\n");
        CDC1_SendBlock((unsigned char*)"hello?\r\n", sizeof("hello?\r\n")-1);
      }
    }
  }
}

Summary

The K22F is the latest addition to the list of supported USB microcontroller in the FSL_USB_Stack component. The example project featuring USB CDC is available onGitHub.

Happy USBing!

Topics:

Published at DZone with permission of Erich Styger, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}