2021-12-13 21:39:13 -08:00
|
|
|
/**
|
|
|
|
**************************************************************************
|
|
|
|
* @file printer_class.c
|
|
|
|
* @brief usb printer class type
|
|
|
|
**************************************************************************
|
|
|
|
* Copyright notice & Disclaimer
|
|
|
|
*
|
2022-04-11 04:50:25 -07: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-13 21:39:13 -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 "printer_class.h"
|
|
|
|
#include "printer_desc.h"
|
|
|
|
|
|
|
|
/** @addtogroup AT32F435_437_middlewares_usbd_class
|
|
|
|
* @{
|
|
|
|
*/
|
2022-04-11 04:50:25 -07:00
|
|
|
|
2021-12-13 21:39:13 -08:00
|
|
|
/** @defgroup USB_printer_class
|
|
|
|
* @brief usb device class printer demo
|
|
|
|
* @{
|
2022-04-11 04:50:25 -07:00
|
|
|
*/
|
2021-12-13 21:39:13 -08:00
|
|
|
|
|
|
|
/** @defgroup USB_printer_class_private_functions
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
2022-03-03 03:49:19 -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-13 21:39:13 -08:00
|
|
|
|
2022-03-03 03:49:19 -08:00
|
|
|
ALIGNED_HEAD static uint8_t printer_device_id[PRINTER_DEVICE_ID_LEN] ALIGNED_TAIL=
|
2021-12-13 21:39:13 -08:00
|
|
|
{
|
|
|
|
0x00, 0x16,
|
|
|
|
'M', 'F', 'G',':','A','r','t','e', 'r', 'y' ,' ',
|
|
|
|
'C','M', 'D', ':', 'E', 'S', 'C', 'P', 'O', 'S',' ',
|
|
|
|
};
|
|
|
|
|
|
|
|
/* static variable */
|
2022-03-03 03:49:19 -08:00
|
|
|
printer_type printer_struct;
|
2021-12-13 21:39:13 -08:00
|
|
|
|
|
|
|
/* usb device class handler */
|
2022-04-11 04:50:25 -07:00
|
|
|
usbd_class_handler printer_class_handler =
|
2021-12-13 21:39:13 -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 03:49:19 -08:00
|
|
|
&printer_struct
|
2021-12-13 21:39:13 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief initialize usb custom hid endpoint
|
|
|
|
* @param udev: to the structure of usbd_core_type
|
2022-04-11 04:50:25 -07:00
|
|
|
* @retval status of usb_sts_type
|
2021-12-13 21:39:13 -08:00
|
|
|
*/
|
2022-03-03 03:49:19 -08:00
|
|
|
static usb_sts_type class_init_handler(void *udev)
|
2021-12-13 21:39:13 -08:00
|
|
|
{
|
|
|
|
usb_sts_type status = USB_OK;
|
|
|
|
usbd_core_type *pudev = (usbd_core_type *)udev;
|
2022-03-03 03:49:19 -08:00
|
|
|
printer_type *pprter = (printer_type *)pudev->class_handler->pdata;
|
2022-04-11 04:50:25 -07:00
|
|
|
|
2021-12-13 21:39:13 -08:00
|
|
|
/* open in endpoint */
|
2022-03-03 03:49:19 -08:00
|
|
|
usbd_ept_open(pudev, USBD_PRINTER_BULK_IN_EPT, EPT_BULK_TYPE, USBD_PRINTER_IN_MAXPACKET_SIZE);
|
2022-04-11 04:50:25 -07:00
|
|
|
|
2021-12-13 21:39:13 -08:00
|
|
|
/* open out endpoint */
|
2022-03-03 03:49:19 -08:00
|
|
|
usbd_ept_open(pudev, USBD_PRINTER_BULK_OUT_EPT, EPT_BULK_TYPE, USBD_PRINTER_OUT_MAXPACKET_SIZE);
|
2022-04-11 04:50:25 -07:00
|
|
|
|
2021-12-13 21:39:13 -08:00
|
|
|
/* set out endpoint to receive status */
|
2022-03-03 03:49:19 -08:00
|
|
|
usbd_ept_recv(pudev, USBD_PRINTER_BULK_OUT_EPT, pprter->g_rx_buff, USBD_PRINTER_OUT_MAXPACKET_SIZE);
|
2022-04-11 04:50:25 -07:00
|
|
|
|
2022-03-03 03:49:19 -08:00
|
|
|
pprter->g_tx_completed = 1;
|
|
|
|
pprter->g_printer_port_status = 0x18;
|
2022-04-11 04:50:25 -07:00
|
|
|
|
2021-12-13 21:39:13 -08:00
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief clear endpoint or other state
|
|
|
|
* @param udev: to the structure of usbd_core_type
|
2022-04-11 04:50:25 -07:00
|
|
|
* @retval status of usb_sts_type
|
2021-12-13 21:39:13 -08:00
|
|
|
*/
|
2022-03-03 03:49:19 -08:00
|
|
|
static usb_sts_type class_clear_handler(void *udev)
|
2021-12-13 21:39:13 -08:00
|
|
|
{
|
|
|
|
usb_sts_type status = USB_OK;
|
|
|
|
usbd_core_type *pudev = (usbd_core_type *)udev;
|
2022-04-11 04:50:25 -07:00
|
|
|
|
2021-12-13 21:39:13 -08:00
|
|
|
/* close in endpoint */
|
|
|
|
usbd_ept_close(pudev, USBD_PRINTER_BULK_IN_EPT);
|
2022-04-11 04:50:25 -07:00
|
|
|
|
2021-12-13 21:39:13 -08:00
|
|
|
/* close out endpoint */
|
|
|
|
usbd_ept_close(pudev, USBD_PRINTER_BULK_OUT_EPT);
|
2022-04-11 04:50:25 -07:00
|
|
|
|
2021-12-13 21:39:13 -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 04:50:25 -07:00
|
|
|
* @retval status of usb_sts_type
|
2021-12-13 21:39:13 -08:00
|
|
|
*/
|
2022-03-03 03:49:19 -08:00
|
|
|
static usb_sts_type class_setup_handler(void *udev, usb_setup_type *setup)
|
2021-12-13 21:39:13 -08:00
|
|
|
{
|
|
|
|
usb_sts_type status = USB_OK;
|
|
|
|
usbd_core_type *pudev = (usbd_core_type *)udev;
|
2022-03-03 03:49:19 -08:00
|
|
|
printer_type *pprter = (printer_type *)pudev->class_handler->pdata;
|
2021-12-13 21:39:13 -08:00
|
|
|
|
|
|
|
switch(setup->bmRequestType & USB_REQ_TYPE_RESERVED)
|
|
|
|
{
|
|
|
|
/* class request */
|
|
|
|
case USB_REQ_TYPE_CLASS:
|
|
|
|
switch(setup->bRequest)
|
|
|
|
{
|
|
|
|
case PRINTER_REQ_GET_DEVICE_ID:
|
2022-03-03 03:49:19 -08:00
|
|
|
usbd_ctrl_send(pudev, printer_device_id, PRINTER_DEVICE_ID_LEN);
|
2021-12-13 21:39:13 -08:00
|
|
|
break;
|
|
|
|
case PRINTER_REQ_GET_PORT_STATUS:
|
2022-03-03 03:49:19 -08:00
|
|
|
usbd_ctrl_send(pudev, (uint8_t *)&pprter->g_printer_port_status, 1);
|
2021-12-13 21:39:13 -08:00
|
|
|
break;
|
|
|
|
case PRINTER_REQ_GET_SOFT_RESET:
|
2022-03-03 03:49:19 -08:00
|
|
|
usbd_ctrl_recv(pudev, pprter->g_printer_data, USBD_PRINTER_OUT_MAXPACKET_SIZE);
|
2021-12-13 21:39:13 -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:
|
|
|
|
usbd_ctrl_unsupport(pudev);
|
|
|
|
break;
|
|
|
|
case USB_STD_REQ_GET_INTERFACE:
|
2022-03-03 03:49:19 -08:00
|
|
|
usbd_ctrl_send(pudev, (uint8_t *)&pprter->alt_setting, 1);
|
2021-12-13 21:39:13 -08:00
|
|
|
break;
|
|
|
|
case USB_STD_REQ_SET_INTERFACE:
|
2022-03-03 03:49:19 -08:00
|
|
|
pprter->alt_setting = setup->wValue;
|
2021-12-13 21:39:13 -08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
usbd_ctrl_unsupport(pudev);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief usb device endpoint 0 in status stage complete
|
|
|
|
* @param udev: to the structure of usbd_core_type
|
2022-04-11 04:50:25 -07:00
|
|
|
* @retval status of usb_sts_type
|
2021-12-13 21:39:13 -08:00
|
|
|
*/
|
2022-03-03 03:49:19 -08:00
|
|
|
static usb_sts_type class_ept0_tx_handler(void *udev)
|
2021-12-13 21:39:13 -08:00
|
|
|
{
|
|
|
|
usb_sts_type status = USB_OK;
|
2022-04-11 04:50:25 -07:00
|
|
|
|
2021-12-13 21:39:13 -08:00
|
|
|
/* ...user code... */
|
2022-04-11 04:50:25 -07:00
|
|
|
|
2021-12-13 21:39:13 -08:00
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief usb device endpoint 0 out status stage complete
|
|
|
|
* @param udev: usb device core handler type
|
2022-04-11 04:50:25 -07:00
|
|
|
* @retval status of usb_sts_type
|
2021-12-13 21:39:13 -08:00
|
|
|
*/
|
2022-03-03 03:49:19 -08:00
|
|
|
static usb_sts_type class_ept0_rx_handler(void *udev)
|
2021-12-13 21:39:13 -08:00
|
|
|
{
|
|
|
|
usb_sts_type status = USB_OK;
|
2022-04-11 04:50:25 -07:00
|
|
|
|
2021-12-13 21:39:13 -08:00
|
|
|
/* ...user code... */
|
2022-04-11 04:50:25 -07:00
|
|
|
|
2021-12-13 21:39:13 -08:00
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief usb device transmision complete handler
|
|
|
|
* @param udev: to the structure of usbd_core_type
|
|
|
|
* @param ept_num: endpoint number
|
2022-04-11 04:50:25 -07:00
|
|
|
* @retval status of usb_sts_type
|
2021-12-13 21:39:13 -08:00
|
|
|
*/
|
2022-03-03 03:49:19 -08:00
|
|
|
static usb_sts_type class_in_handler(void *udev, uint8_t ept_num)
|
2021-12-13 21:39:13 -08:00
|
|
|
{
|
|
|
|
usbd_core_type *pudev = (usbd_core_type *)udev;
|
2022-03-03 03:49:19 -08:00
|
|
|
printer_type *pprter = (printer_type *)pudev->class_handler->pdata;
|
2021-12-13 21:39:13 -08:00
|
|
|
usb_sts_type status = USB_OK;
|
2022-04-11 04:50:25 -07:00
|
|
|
|
2021-12-13 21:39:13 -08:00
|
|
|
/* ...user code...
|
|
|
|
trans next packet data
|
|
|
|
*/
|
|
|
|
usbd_flush_tx_fifo(pudev, ept_num);
|
2022-03-03 03:49:19 -08:00
|
|
|
pprter->g_tx_completed = 1;
|
2022-04-11 04:50:25 -07:00
|
|
|
|
2021-12-13 21:39:13 -08:00
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief usb device endpoint receive data
|
|
|
|
* @param udev: to the structure of usbd_core_type
|
|
|
|
* @param ept_num: endpoint number
|
2022-04-11 04:50:25 -07:00
|
|
|
* @retval status of usb_sts_type
|
2021-12-13 21:39:13 -08:00
|
|
|
*/
|
2022-03-03 03:49:19 -08:00
|
|
|
static usb_sts_type class_out_handler(void *udev, uint8_t ept_num)
|
2021-12-13 21:39:13 -08:00
|
|
|
{
|
|
|
|
usb_sts_type status = USB_OK;
|
2022-03-03 03:49:19 -08:00
|
|
|
usbd_core_type *pudev = (usbd_core_type *)udev;
|
|
|
|
printer_type *pprter = (printer_type *)pudev->class_handler->pdata;
|
2022-04-11 04:50:25 -07:00
|
|
|
|
2021-12-13 21:39:13 -08:00
|
|
|
/*set recv flag*/
|
2022-03-03 03:49:19 -08:00
|
|
|
pprter->g_rx_completed = 1;
|
2022-04-11 04:50:25 -07:00
|
|
|
|
2021-12-13 21:39:13 -08:00
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief usb device sof handler
|
|
|
|
* @param udev: to the structure of usbd_core_type
|
2022-04-11 04:50:25 -07:00
|
|
|
* @retval status of usb_sts_type
|
2021-12-13 21:39:13 -08:00
|
|
|
*/
|
2022-03-03 03:49:19 -08:00
|
|
|
static usb_sts_type class_sof_handler(void *udev)
|
2021-12-13 21:39:13 -08:00
|
|
|
{
|
|
|
|
usb_sts_type status = USB_OK;
|
2022-04-11 04:50:25 -07:00
|
|
|
|
2021-12-13 21:39:13 -08:00
|
|
|
/* ...user code... */
|
2022-04-11 04:50:25 -07:00
|
|
|
|
2021-12-13 21:39:13 -08:00
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief usb device event handler
|
|
|
|
* @param udev: to the structure of usbd_core_type
|
|
|
|
* @param event: usb device event
|
2022-04-11 04:50:25 -07:00
|
|
|
* @retval status of usb_sts_type
|
2021-12-13 21:39:13 -08:00
|
|
|
*/
|
2022-03-03 03:49:19 -08:00
|
|
|
static usb_sts_type class_event_handler(void *udev, usbd_event_type event)
|
2021-12-13 21:39:13 -08:00
|
|
|
{
|
|
|
|
usb_sts_type status = USB_OK;
|
|
|
|
switch(event)
|
|
|
|
{
|
|
|
|
case USBD_RESET_EVENT:
|
2022-04-11 04:50:25 -07:00
|
|
|
|
2021-12-13 21:39:13 -08:00
|
|
|
/* ...user code... */
|
2022-04-11 04:50:25 -07:00
|
|
|
|
2021-12-13 21:39:13 -08:00
|
|
|
break;
|
|
|
|
case USBD_SUSPEND_EVENT:
|
2022-04-11 04:50:25 -07:00
|
|
|
|
2021-12-13 21:39:13 -08:00
|
|
|
/* ...user code... */
|
2022-04-11 04:50:25 -07:00
|
|
|
|
2021-12-13 21:39:13 -08:00
|
|
|
break;
|
|
|
|
case USBD_WAKEUP_EVENT:
|
|
|
|
/* ...user code... */
|
2022-04-11 04:50:25 -07:00
|
|
|
|
2021-12-13 21:39:13 -08:00
|
|
|
break;
|
|
|
|
case USBD_INISOINCOM_EVENT:
|
|
|
|
break;
|
|
|
|
case USBD_OUTISOINCOM_EVENT:
|
|
|
|
break;
|
2022-04-11 04:50:25 -07:00
|
|
|
|
2021-12-13 21:39:13 -08:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief usb device class send data
|
|
|
|
* @param udev: to the structure of usbd_core_type
|
|
|
|
* @param send_data: send data buffer
|
|
|
|
* @param len: send length
|
2022-04-11 04:50:25 -07:00
|
|
|
* @retval error status
|
2021-12-13 21:39:13 -08:00
|
|
|
*/
|
|
|
|
error_status usb_printer_send_data(void *udev, uint8_t *send_data, uint16_t len)
|
|
|
|
{
|
|
|
|
error_status status = SUCCESS;
|
|
|
|
usbd_core_type *pudev = (usbd_core_type *)udev;
|
2022-03-03 03:49:19 -08:00
|
|
|
printer_type *pprter = (printer_type *)pudev->class_handler->pdata;
|
|
|
|
if(pprter->g_tx_completed)
|
2021-12-13 21:39:13 -08:00
|
|
|
{
|
2022-03-03 03:49:19 -08:00
|
|
|
pprter->g_tx_completed = 0;
|
2021-12-13 21:39:13 -08:00
|
|
|
usbd_ept_send(pudev, USBD_PRINTER_BULK_IN_EPT, send_data, len);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
status = ERROR;
|
|
|
|
}
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @}
|
2022-04-11 04:50:25 -07:00
|
|
|
*/
|
2021-12-13 21:39:13 -08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @}
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @}
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|