This tutorial was extracted from Erich Styger blog http://mcuoneclipse.wordpress.com with his agreement.
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.
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:
With this, I can use the FSL_USB_HID_Mouse_Device component:
In that component, similar to the HID Keyboard device, I select the 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:
Using the USB HID Mouse Device Class
The component offers the following methods and events:
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.
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.