USB Component  Version 6.3
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 MCBSTM32F400) 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 MCBSTM32F400 board with the STM32F407IGHx device. In the Manage Run-Time Environment window, select the following components:

  • USB:Device:HID:1
  • CMSIS Driver:USB Device (API):Full-speed
  • Board Support:Joystick (API):Joystick (Variant MCBSTM32F400)

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 C File (.c) and enter Mouse in the Name box.
  • Copy the following code into the Mouse.c file:
    /*------------------------------------------------------------------------------
    MDK Middleware - Component ::USB:Device
    Copyright (c) 2004-2014 ARM Germany GmbH. All rights reserved.
    *------------------------------------------------------------------------------
    Name: Mouse.c
    Purpose: USB Device Human Interface Device example program
    *----------------------------------------------------------------------------*/
    #include "cmsis_os.h"
    #include "rl_usb.h"
    #include "usb_hid.h"
    #include "Board_Joystick.h"
    #include "stm32f4xx_hal.h"
    extern uint32_t os_time;
    uint32_t HAL_GetTick(void) {
    return os_time;
    }
    /* System Clock Configuration */
    void SystemClock_Config(void) {
    RCC_OscInitTypeDef RCC_OscInitStruct;
    RCC_ClkInitTypeDef RCC_ClkInitStruct;
    /* Enable Power Control clock */
    __PWR_CLK_ENABLE();
    /* The voltage scaling allows optimizing the power consumption when the
    device is clocked below the maximum system frequency (see datasheet). */
    __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
    /* Enable HSE Oscillator and activate PLL with HSE as source */
    RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
    RCC_OscInitStruct.HSEState = RCC_HSE_ON;
    RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
    RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
    RCC_OscInitStruct.PLL.PLLM = 25;
    RCC_OscInitStruct.PLL.PLLN = 336;
    RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
    RCC_OscInitStruct.PLL.PLLQ = 7;
    HAL_RCC_OscConfig(&RCC_OscInitStruct);
    /* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2
    clocks dividers */
    RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_PCLK1 |
    RCC_CLOCKTYPE_PCLK2;
    RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
    RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
    RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
    RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
    HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5);
    }
    int main (void) {
    uint32_t state, state_ex = 0;
    uint8_t mouse_in_report[4];
    bool update;
    HAL_Init();
    SystemClock_Config();
    Joystick_Initialize();
    USBD_Initialize (0); /* USB Device 0 Initialization */
    USBD_Connect (0); /* USB Device 0 Connect */
    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
    • USB OTG Full-speed
    • I2C1 (Inter-integrated Circuit Interface 1) [Driver_I2C1] (for the Joystick connected to I2C1)
      • Change I2C1_SCL Pin to PB8 and
      • I2C1_SDA Pin to PB9
      • Enable DMA Rx and
      • DMA Tx
  • 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 168000000

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
  • 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 USBFS 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.