Files
AT32F403A_407_Firmware_Library/middlewares/usbd_class/mouse/mouse_class.c

374 lines
9.9 KiB
C
Raw Normal View History

2021-12-14 11:22:00 +08:00
/**
**************************************************************************
* @file mouse_class.c
* @brief usb hid mouse class type
**************************************************************************
* Copyright notice & Disclaimer
*
2022-04-11 19:22:17 +08:00
* The software Board Support Package (BSP) that is made available to
* download from Artery official website is the copyrighted work of Artery.
* Artery authorizes customers to use, copy, and distribute the BSP
* software and its related documentation for the purpose of design and
* development in conjunction with Artery microcontrollers. Use of the
2021-12-14 11:22:00 +08:00
* software is governed by this copyright notice and the following disclaimer.
*
* THIS SOFTWARE IS PROVIDED ON "AS IS" BASIS WITHOUT WARRANTIES,
* GUARANTEES OR REPRESENTATIONS OF ANY KIND. ARTERY EXPRESSLY DISCLAIMS,
* TO THE FULLEST EXTENT PERMITTED BY LAW, ALL EXPRESS, IMPLIED OR
* STATUTORY OR OTHER WARRANTIES, GUARANTEES OR REPRESENTATIONS,
* INCLUDING BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
*
**************************************************************************
*/
#include "usbd_core.h"
#include "mouse_class.h"
#include "mouse_desc.h"
/** @addtogroup AT32F403A_407_middlewares_usbd_class
* @{
*/
2022-04-11 19:22:17 +08:00
2021-12-14 11:22:00 +08:00
/** @defgroup USB_mouse_class
* @brief usb device mouse demo
* @{
2022-04-11 19:22:17 +08:00
*/
2021-12-14 11:22:00 +08:00
/** @defgroup USB_mouse_class_private_functions
* @{
*/
2022-03-03 19:28:16 +08:00
static usb_sts_type class_init_handler(void *udev);
static usb_sts_type class_clear_handler(void *udev);
static usb_sts_type class_setup_handler(void *udev, usb_setup_type *setup);
static usb_sts_type class_ept0_tx_handler(void *udev);
static usb_sts_type class_ept0_rx_handler(void *udev);
static usb_sts_type class_in_handler(void *udev, uint8_t ept_num);
static usb_sts_type class_out_handler(void *udev, uint8_t ept_num);
static usb_sts_type class_sof_handler(void *udev);
static usb_sts_type class_event_handler(void *udev, usbd_event_type event);
2021-12-14 11:22:00 +08:00
2022-03-03 19:28:16 +08:00
mouse_type mouse_struct;
2021-12-14 11:22:00 +08:00
/* usb device class handler */
2022-04-11 19:22:17 +08:00
usbd_class_handler mouse_class_handler =
2021-12-14 11:22:00 +08:00
{
class_init_handler,
class_clear_handler,
class_setup_handler,
class_ept0_tx_handler,
class_ept0_rx_handler,
class_in_handler,
class_out_handler,
class_sof_handler,
class_event_handler,
2022-03-03 19:28:16 +08:00
&mouse_struct
2021-12-14 11:22:00 +08:00
};
/**
* @brief initialize usb endpoint
* @param udev: to the structure of usbd_core_type
2022-04-11 19:22:17 +08:00
* @retval status of usb_sts_type
2021-12-14 11:22:00 +08:00
*/
2022-03-03 19:28:16 +08:00
static usb_sts_type class_init_handler(void *udev)
2021-12-14 11:22:00 +08:00
{
usb_sts_type status = USB_OK;
usbd_core_type *pudev = (usbd_core_type *)udev;
2022-04-11 19:22:17 +08:00
2021-12-14 11:22:00 +08:00
#ifndef USB_EPT_AUTO_MALLOC_BUFFER
/* use user define buffer address */
2022-03-03 19:28:16 +08:00
usbd_ept_buf_custom_define(pudev, USBD_MOUSE_IN_EPT, EPT1_TX_ADDR);
2021-12-14 11:22:00 +08:00
#endif
2022-04-11 19:22:17 +08:00
2021-12-14 11:22:00 +08:00
/* open hid in endpoint */
2022-03-03 19:28:16 +08:00
usbd_ept_open(pudev, USBD_MOUSE_IN_EPT, EPT_INT_TYPE, USBD_MOUSE_IN_MAXPACKET_SIZE);
2022-04-11 19:22:17 +08:00
2021-12-14 11:22:00 +08:00
return status;
}
/**
* @brief clear endpoint or other state
* @param udev: to the structure of usbd_core_type
2022-04-11 19:22:17 +08:00
* @retval status of usb_sts_type
2021-12-14 11:22:00 +08:00
*/
2022-03-03 19:28:16 +08:00
static usb_sts_type class_clear_handler(void *udev)
2021-12-14 11:22:00 +08:00
{
usb_sts_type status = USB_OK;
usbd_core_type *pudev = (usbd_core_type *)udev;
2022-04-11 19:22:17 +08:00
2021-12-14 11:22:00 +08:00
/* close hid in endpoint */
2022-03-03 19:28:16 +08:00
usbd_ept_close(pudev, USBD_MOUSE_IN_EPT);
2022-04-11 19:22:17 +08:00
2021-12-14 11:22:00 +08:00
return status;
}
/**
* @brief usb device class setup request handler
* @param udev: to the structure of usbd_core_type
* @param setup: setup packet
2022-04-11 19:22:17 +08:00
* @retval status of usb_sts_type
2021-12-14 11:22:00 +08:00
*/
2022-03-03 19:28:16 +08:00
static usb_sts_type class_setup_handler(void *udev, usb_setup_type *setup)
2021-12-14 11:22:00 +08:00
{
usb_sts_type status = USB_OK;
usbd_core_type *pudev = (usbd_core_type *)udev;
2022-03-03 19:28:16 +08:00
mouse_type *pmouse = (mouse_type *)pudev->class_handler->pdata;
2021-12-14 11:22:00 +08:00
uint16_t len;
uint8_t *buf;
switch(setup->bmRequestType & USB_REQ_TYPE_RESERVED)
{
/* class request */
case USB_REQ_TYPE_CLASS:
switch(setup->bRequest)
{
case HID_REQ_SET_PROTOCOL:
2022-03-03 19:28:16 +08:00
pmouse->hid_protocol = (uint8_t)setup->wValue;
2021-12-14 11:22:00 +08:00
break;
case HID_REQ_GET_PROTOCOL:
2022-03-03 19:28:16 +08:00
usbd_ctrl_send(pudev, (uint8_t *)&pmouse->hid_protocol, 1);
2021-12-14 11:22:00 +08:00
break;
case HID_REQ_SET_IDLE:
2022-03-03 19:28:16 +08:00
pmouse->hid_set_idle = (uint8_t)(setup->wValue >> 8);
2021-12-14 11:22:00 +08:00
break;
case HID_REQ_GET_IDLE:
2022-03-03 19:28:16 +08:00
usbd_ctrl_send(pudev, (uint8_t *)&pmouse->hid_set_idle, 1);
2021-12-14 11:22:00 +08:00
break;
case HID_REQ_SET_REPORT:
2022-03-03 19:28:16 +08:00
pmouse->hid_state = HID_REQ_SET_REPORT;
usbd_ctrl_recv(pudev, pmouse->hid_set_report, setup->wLength);
2021-12-14 11:22:00 +08:00
break;
default:
usbd_ctrl_unsupport(pudev);
break;
}
break;
/* standard request */
case USB_REQ_TYPE_STANDARD:
switch(setup->bRequest)
{
case USB_STD_REQ_GET_DESCRIPTOR:
if(setup->wValue >> 8 == HID_REPORT_DESC)
{
2022-03-03 19:28:16 +08:00
len = MIN(USBD_MOUSE_SIZ_REPORT_DESC, setup->wLength);
buf = (uint8_t *)g_usbd_mouse_report;
2021-12-14 11:22:00 +08:00
}
else if(setup->wValue >> 8 == HID_DESCRIPTOR_TYPE)
{
len = MIN(9, setup->wLength);
2022-03-03 19:28:16 +08:00
buf = (uint8_t *)g_mouse_usb_desc;
2021-12-14 11:22:00 +08:00
}
usbd_ctrl_send(pudev, (uint8_t *)buf, len);
break;
case USB_STD_REQ_GET_INTERFACE:
2022-03-03 19:28:16 +08:00
usbd_ctrl_send(pudev, (uint8_t *)&pmouse->alt_setting, 1);
2021-12-14 11:22:00 +08:00
break;
case USB_STD_REQ_SET_INTERFACE:
2022-03-03 19:28:16 +08:00
pmouse->alt_setting = setup->wValue;
2021-12-14 11:22:00 +08:00
break;
2023-08-08 19:27:03 +08:00
case USB_STD_REQ_CLEAR_FEATURE:
break;
case USB_STD_REQ_SET_FEATURE:
break;
2021-12-14 11:22:00 +08:00
default:
usbd_ctrl_unsupport(pudev);
break;
}
break;
default:
usbd_ctrl_unsupport(pudev);
break;
}
return status;
}
/**
* @brief usb device class endpoint 0 in status stage complete
* @param udev: to the structure of usbd_core_type
2022-04-11 19:22:17 +08:00
* @retval status of usb_sts_type
2021-12-14 11:22:00 +08:00
*/
2022-03-03 19:28:16 +08:00
static usb_sts_type class_ept0_tx_handler(void *udev)
2021-12-14 11:22:00 +08:00
{
usb_sts_type status = USB_OK;
2022-04-11 19:22:17 +08:00
2021-12-14 11:22:00 +08:00
/* ...user code... */
2022-04-11 19:22:17 +08:00
2021-12-14 11:22:00 +08:00
return status;
}
/**
* @brief usb device class endpoint 0 out status stage complete
* @param udev: to the structure of usbd_core_type
2022-04-11 19:22:17 +08:00
* @retval status of usb_sts_type
2021-12-14 11:22:00 +08:00
*/
2022-03-03 19:28:16 +08:00
static usb_sts_type class_ept0_rx_handler(void *udev)
2021-12-14 11:22:00 +08:00
{
usb_sts_type status = USB_OK;
usbd_core_type *pudev = (usbd_core_type *)udev;
2022-03-03 19:28:16 +08:00
mouse_type *pmouse = (mouse_type *)pudev->class_handler->pdata;
2021-12-14 11:22:00 +08:00
uint32_t recv_len = usbd_get_recv_len(pudev, 0);
/* ...user code... */
2022-03-03 19:28:16 +08:00
if( pmouse->hid_state == HID_REQ_SET_REPORT)
2021-12-14 11:22:00 +08:00
{
/* hid buffer process */
2022-03-03 19:28:16 +08:00
pmouse->hid_state = 0;
2021-12-14 11:22:00 +08:00
}
2022-04-11 19:22:17 +08:00
2021-12-14 11:22:00 +08:00
return status;
}
/**
* @brief usb device class transmision complete handler
* @param udev: to the structure of usbd_core_type
* @param ept_num: endpoint number
2022-04-11 19:22:17 +08:00
* @retval status of usb_sts_type
2021-12-14 11:22:00 +08:00
*/
2022-03-03 19:28:16 +08:00
static usb_sts_type class_in_handler(void *udev, uint8_t ept_num)
2021-12-14 11:22:00 +08:00
{
usb_sts_type status = USB_OK;
2022-04-11 19:22:17 +08:00
2021-12-14 11:22:00 +08:00
/* ...user code...
trans next packet data
*/
2022-04-11 19:22:17 +08:00
2021-12-14 11:22:00 +08:00
return status;
}
/**
* @brief usb device class endpoint receive data
* @param udev: to the structure of usbd_core_type
* @param ept_num: endpoint number
2022-04-11 19:22:17 +08:00
* @retval status of usb_sts_type
2021-12-14 11:22:00 +08:00
*/
2022-03-03 19:28:16 +08:00
static usb_sts_type class_out_handler(void *udev, uint8_t ept_num)
2021-12-14 11:22:00 +08:00
{
usb_sts_type status = USB_OK;
2022-04-11 19:22:17 +08:00
2021-12-14 11:22:00 +08:00
return status;
}
/**
* @brief usb device class sof handler
* @param udev: to the structure of usbd_core_type
2022-04-11 19:22:17 +08:00
* @retval status of usb_sts_type
2021-12-14 11:22:00 +08:00
*/
2022-03-03 19:28:16 +08:00
static usb_sts_type class_sof_handler(void *udev)
2021-12-14 11:22:00 +08:00
{
usb_sts_type status = USB_OK;
2022-04-11 19:22:17 +08:00
2021-12-14 11:22:00 +08:00
/* ...user code... */
2022-04-11 19:22:17 +08:00
2021-12-14 11:22:00 +08:00
return status;
}
/**
* @brief usb device class event handler
* @param udev: to the structure of usbd_core_type
* @param event: usb device event
2022-04-11 19:22:17 +08:00
* @retval status of usb_sts_type
2021-12-14 11:22:00 +08:00
*/
2022-03-03 19:28:16 +08:00
static usb_sts_type class_event_handler(void *udev, usbd_event_type event)
2021-12-14 11:22:00 +08:00
{
usb_sts_type status = USB_OK;
2022-03-03 19:28:16 +08:00
usbd_core_type *pudev = (usbd_core_type *)udev;
mouse_type *pmouse = (mouse_type *)pudev->class_handler->pdata;
2021-12-14 11:22:00 +08:00
switch(event)
{
case USBD_RESET_EVENT:
2022-04-11 19:22:17 +08:00
2021-12-14 11:22:00 +08:00
/* ...user code... */
2022-04-11 19:22:17 +08:00
2021-12-14 11:22:00 +08:00
break;
case USBD_SUSPEND_EVENT:
2022-03-03 19:28:16 +08:00
pmouse->hid_suspend_flag = 1;
2021-12-14 11:22:00 +08:00
/* ...user code... */
2022-04-11 19:22:17 +08:00
2021-12-14 11:22:00 +08:00
break;
case USBD_WAKEUP_EVENT:
/* ...user code... */
2022-04-11 19:22:17 +08:00
2021-12-14 11:22:00 +08:00
break;
default:
break;
}
return status;
}
/**
* @brief usb device class send report
* @param udev: to the structure of usbd_core_type
* @param report: report buffer
* @param len: report length
2022-04-11 19:22:17 +08:00
* @retval status of usb_sts_type
2021-12-14 11:22:00 +08:00
*/
2022-03-03 19:28:16 +08:00
usb_sts_type usb_mouse_class_send_report(void *udev, uint8_t *report, uint16_t len)
2021-12-14 11:22:00 +08:00
{
usb_sts_type status = USB_OK;
usbd_core_type *pudev = (usbd_core_type *)udev;
if(usbd_connect_state_get(pudev) == USB_CONN_STATE_CONFIGURED)
2022-03-03 19:28:16 +08:00
usbd_ept_send(pudev, USBD_MOUSE_IN_EPT, report, len);
2022-04-11 19:22:17 +08:00
2021-12-14 11:22:00 +08:00
return status;
}
/**
* @brief usb device class report function
* @param udev: to the structure of usbd_core_type
* @param op: operation
2022-04-11 19:22:17 +08:00
* @retval none
2021-12-14 11:22:00 +08:00
*/
void usb_hid_mouse_send(void *udev, uint8_t op)
{
2022-03-03 19:28:16 +08:00
usbd_core_type *pudev = (usbd_core_type *)udev;
mouse_type *pmouse = (mouse_type *)pudev->class_handler->pdata;
2021-12-14 11:22:00 +08:00
int8_t posx = 0, posy = 0, button = 0;
switch(op)
{
case LEFT_BUTTON:
button = 0x01;
break;
2022-04-11 19:22:17 +08:00
2021-12-14 11:22:00 +08:00
case RIGHT_BUTTON:
button = 0x2;
break;
2022-04-11 19:22:17 +08:00
2021-12-14 11:22:00 +08:00
case UP_MOVE:
posy -= MOVE_STEP;
break;
2022-04-11 19:22:17 +08:00
2021-12-14 11:22:00 +08:00
case DOWN_MOVE:
posy += MOVE_STEP;
break;
2022-04-11 19:22:17 +08:00
2021-12-14 11:22:00 +08:00
case LEFT_MOVE:
2022-04-11 19:22:17 +08:00
posx -= MOVE_STEP;
2021-12-14 11:22:00 +08:00
break;
2022-04-11 19:22:17 +08:00
2021-12-14 11:22:00 +08:00
case RIGHT_MOVE:
posx += MOVE_STEP;
break;
2022-04-11 19:22:17 +08:00
2021-12-14 11:22:00 +08:00
default:
break;
}
2022-03-03 19:28:16 +08:00
pmouse->mouse_buffer[0] = button;
pmouse->mouse_buffer[1] = posx;
pmouse->mouse_buffer[2] = posy;
2022-04-11 19:22:17 +08:00
2022-03-03 19:28:16 +08:00
usb_mouse_class_send_report(udev, pmouse->mouse_buffer, 4);
2021-12-14 11:22:00 +08:00
}
/**
* @}
2022-04-11 19:22:17 +08:00
*/
2021-12-14 11:22:00 +08:00
/**
* @}
*/
/**
* @}
*/