USB Component  Version 6.6
MDK-Professional Middleware for USB Device and Host
 All Data Structures Functions Variables Enumerations Enumerator Groups Pages
USB Mouse

The USB Mouse example application shows how to control the mouse pointer of a host PC with a microcontroller device using USB Device HID.

The following picture shows an exemplary connection of the development board (in this case a MCB1800) to a host PC. Using the joystick on the development board you can move the mouse pointer on the screen. Pressing the joystick down will issue a left-click action.

mouse_dev_example_setup.png

Create the "USB Mouse" Project

Create a new project in MDK. In this example, we are using the MCB1800 board with the NXP LPC1857 device. In the Manage Run-Time Environment window, select the following components:

  • USB:Device:HID:1
  • Keil::CMSIS Driver:USB Device:USB0
  • Board Support:Joystick (API):Joystick (Variant MCB1800)

Click the Resolve button and then OK. Your Project should look like this:

usb_mouse_example_proj_window.png
USB Mouse Project Structure

Source Files

  • Right-click on Source Group 1 and select Add New Item to Group 'Source Group 1'....
  • Click on User Code Template and select CMSIS:CMSIS-RTOS "main" function.
  • Make the following changes to the main.c file:
    • In line 7, right-click and select Insert '#include file' and then rl_usb.h
    • Next, right-click and select Insert '#include file' and then Board_Joystick.h
    • Copy the following variable definitions to the main function (before the osKernelInitialize() function):
        uint32_t state, state_ex = 0;
        uint8_t  mouse_in_report[4];
        bool     update;
    • Initialize the joystick and the USB device right after the osKernelInitialize() function:
        Joystick_Initialize();
       
        USBD_Initialize    (0);               /* USB Device 0 Initialization        */
        USBD_Connect       (0);               /* USB Device 0 Connect               */
    • After the osKernelStart add this while loop:
        while (1) {                           /* Loop forever                       */
          state  = Joystick_GetState();
          update = 0;
          mouse_in_report[0] = 0;
          mouse_in_report[1] = 0;
          mouse_in_report[2] = 0;
          mouse_in_report[3] = 0;
       
          if ((state ^ state_ex) & JOYSTICK_CENTER) {
            mouse_in_report[0] = (state & JOYSTICK_CENTER) ? 1 : 0;   /* Left Click */
            update   = 1;
            state_ex = state;
          }
          if (state & JOYSTICK_LEFT  ) { mouse_in_report[1] = (uint8_t)(-4); update = 1; } /* X Left  */
          if (state & JOYSTICK_RIGHT ) { mouse_in_report[1] =            4 ; update = 1; } /* X Right */
          if (state & JOYSTICK_UP    ) { mouse_in_report[2] = (uint8_t)(-4); update = 1; } /* Y Up    */
          if (state & JOYSTICK_DOWN  ) { mouse_in_report[2] =            4 ; update = 1; } /* Y Down  */
       
          if (update) {
            USBD_HID_GetReportTrigger(0, 0, mouse_in_report, 4);
          }
        }
  • The complete main.c file should look like this:
    /*----------------------------------------------------------------------------
    CMSIS-RTOS 'main' function template
    *---------------------------------------------------------------------------*/
    #define osObjectsPublic // define objects in main module
    #include "osObjects.h" // RTOS object definitions
    #include "rl_usb.h" // Keil.MDK-Pro::USB:CORE
    #include "Board_Joystick.h" // ::Board Support:Joystick
    /*
    main: initialize and start the system
    */
    int main (void) {
    uint32_t state, state_ex = 0;
    uint8_t mouse_in_report[4];
    bool update;
    osKernelInitialize (); // initialize CMSIS-RTOS
    // initialize peripherals here
    Joystick_Initialize();
    USBD_Initialize (0); /* USB Device 0 Initialization */
    // create 'thread' functions that start executing,
    // example: tid_name = osThreadCreate (osThread(name), NULL);
    osKernelStart (); // start thread execution
    while (1) { /* Loop forever */
    state = Joystick_GetState();
    update = 0;
    mouse_in_report[0] = 0;
    mouse_in_report[1] = 0;
    mouse_in_report[2] = 0;
    mouse_in_report[3] = 0;
    if ((state ^ state_ex) & JOYSTICK_CENTER) {
    mouse_in_report[0] = (state & JOYSTICK_CENTER) ? 1 : 0; /* Left Click */
    update = 1;
    state_ex = state;
    }
    if (state & JOYSTICK_LEFT ) { mouse_in_report[1] = (uint8_t)(-4); update = 1; } /* X Left */
    if (state & JOYSTICK_RIGHT ) { mouse_in_report[1] = 4 ; update = 1; } /* X Right */
    if (state & JOYSTICK_UP ) { mouse_in_report[2] = (uint8_t)(-4); update = 1; } /* Y Up */
    if (state & JOYSTICK_DOWN ) { mouse_in_report[2] = 4 ; update = 1; } /* Y Down */
    if (update) {
    USBD_HID_GetReportTrigger(0, 0, mouse_in_report, 4);
    }
    }
    }
  • Right-click on Source Group 1 and select Add New Item to Group 'Source Group 1'....
  • Click on User Code Template and select the USB Device HID Mouse template.
  • Click Add to copy the file USBD_User_HID_Mouse_0.c to the project.

Before building the project, you need to edit these configuration files:

  • Under Device, double-click RTE_Device.h and enable
    • USB0 Controller [Driver_USBD0 and Driver_USBH0]
    • I2C0 (Inter-integrated Circuit Interface 0) [Driver_I2C0] (for the Joystick connected to I2C0)
  • Under USB, double-click USBD_Config_HID_0.h and enable
    • Use User Provided HID Report Descriptor and set
    • User Provided HID Report Descriptor Size to 52
    • Set the Maximum Input Report Size (in bytes) to 4 as this is the size of report that is sent for a mouse position change and button presses from the main function.
  • Under CMSIS, double-click RTX_Conf_CM.c and set
    • Default Thread stack size to 512
    • Main Thread stack size to 512
    • Number of threads with user-provided stack size to 4
    • Total stack size [bytes] for threads with user-provided stack size to 2048
    • RTOS Kernel Timer input clock frequency [Hz] to 180000000

Before building and downloading the project to the target, make sure that the correct debugger is set in the Options for Target dialog (ALT + F7). You may then build and download the example project to the evaluation board using the µVision commands:

  • Project –> Build target (F7)
  • Flash   –> Download (F8)
  • Debug –> Start/Stop Debug Session (Ctrl + F5)
  • Debug –> Run (F5)

After these steps, the project should start executing on your evaluation kit. In case of errors, refer to the Evaluation Board User's Guide for configuration information.

Using the "USB Mouse" Project

Hardware Setup

  • Verify all jumper settings on the target hardware.
  • Connect the development board to a host PC attaching a Micro-USB cable to the USB0 port. Observe how it is recognized as a USB HID device with the mouse protocol:
hid_compliant_mouse.png
  • Play around with the joystick and see how the mouse moves on the screen.