This tutorial was extracted from Erich Styger blog http://mcuoneclipse.wordpress.com with his agreement.
In “A shell for the Freedom board” I used the UART-to-USB OpenSDA capability of the Freescale Freedom board: The KL25Z processor uses the OpenSDA K20 microprocessor as Serial-to-USB converter. But this only works because of the P&E OpenSDA USB CDC (Communication Device Class) implementation. If I create my board without OpenSDA, I need a different approach: I want to do USB CDC with the KL25Z.
For several months I’m using successfully my Processor Expert FSL_USB_CDC component since early this year, in several projects and with different boards. What is missing is support for the Kinetis KL25Z. So this is what this tutorial is about. It takes you maybe around 30 minutes, and you will have USB support with your Freedom board. As for myself, after learning all the knowledge needed, it takes me less than 10 minutes.
I’m using CodeWarrior for MCU10.3 with my Processor Expert components. If you followed my earlier Freedom Board tutorials, then I hope you are familiar with loading more user components. Otherwise, have a look at the section “Additional Processor Expert Component Installation” in this post. In this tutorial I’m using following components:
- FSL_USB_Stack: Freescale USB Stack as Processor Expert component.
- Wait: Universal realtime waiting functions.
- Optional: LED: Universal LED driver.
Step 1: Creating the Project (or reuse an existing one)
In this tutorial I start from a clean sheet and create a new project. Of course it is possible to add USB CDC support to any of the other tutorials I have published for the Freedom KL25Z board, e.g. Tutorial: Enlightening the Freedom KL25Z Board. In that case, you can skip this step.
To create a new project, I select the menu File > New > Bareboard Project and give it a project name :
New Bareboard Project
Next is to select the device:
I’m going to debug the board with OpenSDA only, so I deselect the default P&E Multilink, and select Open Source SDA:
The next dialog is about the language and build tools options: I go with the defaults:
Language and Build Tools Options
In the next dialog I make sure I select Processor Expert:
Processor Expert Project Option
Pressing Finish will create the project:
Step 2: Adding the Components
The next step is to add my other component(s): I select the component(s) in the Components Library view and add it to my project. If I already have the Wait component in my project, of course no need to add it again.
Selecting and Adding Components
This will add the components to my project. And the USB component shows a red (x) telling me that it needs to be configured:
Need to configure the FSL_USB_Stack
I need to component Inspector to configure the properties.
If I inspect the properties of the USB component, it shows me that I need to set up the CPU and the USBInit property:
Need to configure component
So I select the CPU for my KL25Z board, and select Init_USB_OTG_VAR0 to initialize the USB peripheral for it:
USB component configured for KL25Z
The next thing is to inspect the USBInit component. And it is good to pay close attention to the warnings:
Warnings in USBInit
Enabling the Clock gateensures that the USB module is clocked. And as I have no external clock pin, I select to use the PLL/FLL clock source:
Enabled Clock Gate and configured clock source
The inspector informs me about a very important thing: the USB module requires a 48 MHz clock. So I need to make sure that this is configured properly.
Warning: Failing to configure the USB clock properly is probably the most common source of failure. For myself I can count endless hours debugging and inspecting my projects, and in many cases I made a mistake in the clock configuration. Unlike many other USB enabled devices, the Freescale ones need an external crystal, and that crystal has to be used to generate a very stable and accurate clock signal. Otherwise it will *not* work.
Step 3: Configuring the Clock
According to the FRDM-KL25Z Schematics (SPF-27556_D).pdf have a 8 MHz crystal on the board:
8 MHz Crystal on FRDM-KL25Z
IMPORTANT: I’m using in this post the ‘white’ pre-production Freedom Board. The black production board board has several differences (see Completing the FRDM-KL25Z Board). An important difference is that the black board has the 1.0MOhm resistor R25 nearby the 8 MHz: with that resistor *not* populated, the clock will *not* run in high gain mode. So for the black Freedom board with that resistor *not* populated, the clock mode has to be in ‘low power’ mode.
The configuration of the clock is in the CPU component. For this I select the CPU to configure the properties:
CPU component to configure the clocks
I can have multiple oscillator settings, but here I just need one. So I enable the ‘System oscillator 0′ setting, and configure it to use an 8 MHz external crystal in High gain mode (for the white pre-production board which has the R25 (see above) populated, but in Low Power mode for the black production board:
Setting 8 MHz external crystal with High Gain (White board only!!!)
As the black Freedom board has no R25 populated, the Oscillator operating mode has to be ‘Low power’ instead:
Low Power Oscillator Mode required if R25 not populated (black Freedom board)
Next is to select the PEE (PLL Engaged External) clock mode: in this mode, the PLL is using the external reference clock. Remember that the USB block needs an 48 MHz PLL signal? So I need to configure it to 96 MHz as it will be divided by a factor of 2 until it reaches the USB block:
PEE MCG Mode
With my clock changes, the core clock is not set correctly:
Error for core clock
With my base clock of 8 MHz, I can set the CPU core clock to 48 MHz and a Bus clock of 24 MHz:
Core and Bus Clock
Now I could think that everything is just fine. But there is one possible glitch, and maybe this is even a silicon problem (?): I need to *disable* the Internal and External reference clock, otherwise my board will crash during clock configuration. So I need to *disable* the reference clocks:
Internal and External Reference Clock disabled
Checking my USB module again, it shows that it is now properly clocked with 48 MHz:
USB with 48 MHz PLL clock
Step 4: Adding Application Code
I keep things very simple here: no RTOS. All what I add in Processor Expert is a simple loop-back which does an echo of what I type into the terminal window.
To show the current enumeration status, I’m using my code from my earlier Tutorial: Enlightening the Freedom KL25Z Board.
I’m adding the function
CDC_Run() to the
ProcessorExpert.c as below and call it from
Step 5: Running the Application
Now I can generate code for my drivers, e.g right click on the ProcessorExpert.pe file and selecting "Generate Processor Expert Code":
Generate Processor Expert Code
Followed by building the sources (menu Project > Build Project ). And then download/debug it.
With the USB plug disconnected, the red LED is on:
USB disconnected: red LED on
With connecting the USB cable to the KL25Z, it enumerates the device and the green LED is on:
USB enumerated: green LED on
Additionally it has been enumerated as device on my host machine:
Freescale CDC Device
In case it is the first time you use such a CDC device: the driver .inf file is generated in the ‘Documentation’ folder (for details see USB CDC, reloaded):
cdc.inf driver files
I connect to the virtual CDC COM port with a terminal program. And the application will echo what I send to it:
Terminal Connection with Echo
Setting up and using an USB stack is definitely not a trivial task, as a lot of settings needs to work correctly. I hope that with the above information it should be much easier to do this now for the Freescale FRDM-KL25Z board. Using Processor Expert makes things definitely a lot easier. While the above project is bare-metal (no RTOS), it is easy to add an RTOS to it. That’s actually how I use it like in the Freedom Shell project.
The above project can be downloaded here or from the link on http://www.steinerberg.com/EmbeddedComponents/Examples/Examples.htm