element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet & Tria Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • About Us
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      • Japan
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Vietnam
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Freedom development platform
  • Products
  • Dev Tools
  • Freedom development platform
  • More
  • Cancel
Freedom development platform
Documents Codewarrior Tutorial for FRDM-KL25Z: Using the Freedom as an USB Mouse Device
  • Blog
  • Forum
  • Documents
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Freedom development platform to participate - click to join for free!
Actions
  • Share
  • More
  • Cancel
Engagement
  • Author Author: FreescaleTools_and_Software
  • Date Created: 8 Aug 2013 1:43 PM Date Created
  • Last Updated Last Updated: 8 Aug 2013 2:22 PM
  • Views 586 views
  • Likes 0 likes
  • Comments 0 comments
Related
Recommended

Codewarrior Tutorial for FRDM-KL25Z: Using the Freedom as an USB Mouse Device

This tutorial was extracted from Erich Styger blog http://mcuoneclipse.wordpress.com with his agreement.

imageimage

   

I finally completed my project turning the FRDM-KL25ZFRDM-KL25Z board into a USB mouse device :-) . The form factor and the capabilities of the Freedom board makes it a great board for implementing it as a ‘custom mouse’. All what I need is the USB stack running on it and have it acting as USB HID Mouse device.

  

FRDM-KL25Z enumerated as USB HID Mouse Device

FRDM-KL25Z enumerated as USB HID Mouse Device

 

 

In this post I turned the FRDM-KL25Z into a USB HID keyboard device. Having the same thing, but as USB HID Mouse device is not much different. Although the FSL USB components had to be updated to make things really easy.

 

USB HID Mouse Project

Creation of a USB HID Mouse project is the same procedure as for the USB HID Keyboard project here: Make sure you set up the clock to 48 MHz and enable the bus clock for the USB block. You might copy or change the USB HID Keyboard project and change it to a USB HID Mouse project too. A link to my project is posted at the end of this article.

  

FSL USB HID Keyboard Device Component

In the FSL USB component, there is now a selection for the HID Mouse Device:

 

HID Mouse Device Selection

HID Mouse Device Selection

  

With this, I can use the FSL_USB_HID_Mouse_Device component:

 

FSL_USB_HID_Mouse_Device

FSL_USB_HID_Mouse_Device

 

In that component, similar to the HID Keyboard device, I select the CPU:

 

Selected Kinetis KL25 CPU

Selected Kinetis KL25 CPU

 

Optionally I can specify a string for the vendor and the device which will show up on the host. A buffer is used for the update messages sent to the host which can be configured as needed. It is just that this buffer needs to have 4 byte entries as I’m using 4 bytes for the USB messages:

 

Buffer settings for USB HID Mouse Device 

Buffer settings for USB HID Mouse Device

 

Using the USB HID Mouse Device Class

The component offers the following methods and events:

 

FSL_USB_Stack with FSL_USB_HID_Mouse_Device

FSL_USB_Stack with FSL_USB_HID_Mouse_Device

 

  • App_Task() needs to be called periodically by the application. The method sends the data from the buffer.
  • isEnumerated() return TRUE or FALSE to know if the mouse device has been enumerated on the USB bus.
  • Send() is used to add ‘raw’ data packets to the buffer to be sent. This allows greater flexibility.
  • Move() is using internally Send(), with a simpler interface. With Move() I can send a message that the mouse has moved, with x and y (delta) values.
  • Click() is used to send a mouse click message. It uses a parameter to tell which button(s) are pressed.
  • Init() initializes the module, and is called automatically from the FSL_USB_Stack component.

 

Example Project

To test the functionality, I’m using this source:

   

void APP_Run(void) {

  int cnt=0; /* counter to slow down LED blinking */

 

  for(;;) {

    WAIT1_Waitms(10);

    cnt++;

    if (HIDK2_App_Task()==ERR_BUSOFF) {

      if ((cnt%128)==0) { /* just slow down blinking */

        LEDG_Off();

        LEDR_Neg();

      }

    } else {

      if ((cnt%128)==0) { /* just slow down blinking */

        LEDR_Off();

        LEDG_Neg();

      }

      if (SW1_GetVal()==0) { /* button pressed */

        WAIT1_Waitms(100); /* wait for debouncing */

        if (SW1_GetVal()==0) { /* still pressed */

          //(void)HIDK2_Send(0, 8, 8); /* raw move message */

 

          //(void)HIDK2_Send(HIDK2_MOUSE_LEFT, 0, 0); /* send left mouse button */

          //(void)HIDK2_Send(0, 0, 0); /* send release button message */

 

          //(void)HIDK2_Move(-8, 0); /* moving the cursor up */

 

          //HIDK2_Click(HIDK2_MOUSE_LEFT); /* left mouse click */

 

          HIDK2_Click(HIDK2_MOUSE_RIGHT); /* right mouse click */

        }

        while(SW1_GetVal()==0) {} /* wait until button is released */

      }

    }

  }

}

  

  

:!: Depending on how Processor Expert names your components, you might need to use something like HIDM1_ instead of HIDK2_ as in my example.

 

 

The application is using the reset button on the FRDM-KL25Z board to send mouse messages. As long the board is not connected, it is blinking the red RGB LED. Once connected, it blinks green.

 

 

Enumerated HID Mouse Device in Windows 

Enumerated HID Mouse Device in Windows

  

Pressing the SW1 button will send one of the messages in the examples

   

HIDK2_Click(HIDK2_MOUSE_LEFT);

 

sends a click message, followed by a release event. This would be the same as sending the ‘raw’ date with

 

(void)HIDK2_Send(HIDK2_MOUSE_LEFT, 0, 0); /* send left mouse button */

(void)HIDK2_Send(0, 0, 0); /* send release button message */

 

With the Send() method, the first parameter is the state of the button, followed by x and y offsets for moves.

Moving the mouse is done with

 

(void)HIDK2_Move(3, 5); /* moving the cursor*/

 

which uses an x and a y offset.

 

Summary

An example project is attached to this post. Make sure you update your Processor Expert components with the zip file attached to this message (see here). I have now a simple starting point for using the KL25Z as a USB mouse device. It should be easy to add the accelerometer part to it, so I can have an ‘air mouse’. Let’s see if I’ll find time to add that functionality. Additionally, as the USB HID allows great flexibility, a generic USB HID device would be something I’m interested in. Or a USB composite device with USB HID Keyboard and USB HID mouse.

 

Happy Mousing :-)

Attachments:
ProcessorExpertComponents070813.zip
Freedom_USB_HID_Mouse.zip
  • project_example
  • cortex-m0+
  • freescale
  • freedom_board
  • hid
  • Cortex-M
  • kinetis
  • usb
  • cortex
  • frdm-kl25z
  • kinetis-l
  • cortex-m0
  • mouse
  • arm
  • freedom
  • Share
  • History
  • More
  • Cancel
  • Sign in to reply
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube