📄 usbprinterdriver.c
字号:
/* ----------------------------------------------------------------------------
* ATMEL Microcontroller Software Support
* ----------------------------------------------------------------------------
* Copyright (c) 2008, Atmel Corporation
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the disclaimer below.
*
* Atmel's name may not be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
* DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* ----------------------------------------------------------------------------
*/
/*
Title: USBPrinterDriver implementation
About: Purpose
Implementation of the USBPrinterDriver class methods.
*/
//------------------------------------------------------------------------------
// Headers
//------------------------------------------------------------------------------
#include "USBPrinterDriver.h"
#include "USBPrinterDriverDescriptors.h"
#include <utility/trace.h>
#include <utility/assert.h>
#include <usb/device/core/USBDDriver.h>
//------------------------------------------------------------------------------
// Types
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
/// USB driver for a CDC class implementing a virtual COM serial connection.
//------------------------------------------------------------------------------
typedef struct {
/// Standard USBDDriver instance.
USBDDriver usbdDriver;
} USBPrinterDriver;
//------------------------------------------------------------------------------
// Internal variables
//------------------------------------------------------------------------------
/// Static instance of the CDC serial driver.
static USBPrinterDriver usbPrinterDriver;
//------------------------------------------------------------------------------
// Optional RequestReceived() callback re-implementation
//------------------------------------------------------------------------------
#if !defined(NOAUTOCALLBACK)
//------------------------------------------------------------------------------
/// Re-implemented callback, invoked when a new USB Request is received.
//------------------------------------------------------------------------------
void USBDCallbacks_RequestReceived(const USBGenericRequest *request)
{
USBPrinterDriver_RequestHandler(request);
}
#endif
//------------------------------------------------------------------------------
// Exported functions
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
/// Initializes the USB Device CDC serial driver & USBD Driver.
//------------------------------------------------------------------------------
void USBPrinterDriver_Initialize()
{
TRACE_INFO("USBPrinterDriver_Initialize\n\r");
// Initialize the standard driver
USBDDriver_Initialize(&(usbPrinterDriver.usbdDriver),
&usbPrinterDriverDescriptors,
0); // Multiple settings for interfaces not supported
// Initialize the USB driver
USBD_Init();
}
//------------------------------------------------------------------------------
/// Handles CDC-specific SETUP requests. Should be called from a
/// re-implementation of USBDCallbacks_RequestReceived() method.
/// \param Pointer to a USBGenericRequest instance.
//------------------------------------------------------------------------------
void USBPrinterDriver_RequestHandler(const USBGenericRequest *request)
{
TRACE_INFO_WP("NewReq ");
// Handle the request
switch (USBGenericRequest_GetRequest(request)) {
case USBPrinterGenericRequest_UpdataReday:
USBPrinter_HandleUpdataReday();
break;
case USBPrinterGenericRequest_UpdataStart:
USBPrinter_HandleUpdataStart();
break;
default:
USBDDriver_RequestHandler(&(usbPrinterDriver.usbdDriver), request);
break;
}
}
//------------------------------------------------------------------------------
/// Receives data from the host through the virtual COM port created by
/// the CDC device serial driver. This function behaves like USBD_Read.
/// \param data Pointer to the data buffer to put received data.
/// \param size Size of the data buffer in bytes.
/// \param callback Optional callback function to invoke when the transfer
/// finishes.
/// \param argument Optional argument to the callback function.
/// \return USBD_STATUS_SUCCESS if the read operation has been started normally;
/// otherwise, the corresponding error code.
//------------------------------------------------------------------------------
unsigned char USBPrinterDriver_Read(void *data,
unsigned int size,
TransferCallback callback,
void *argument)
{
return USBD_Read(USBPrinterDriverDescriptors_DATAOUT,
data,
size,
callback,
argument);
}
//------------------------------------------------------------------------------
/// Sends a data buffer through the virtual COM port created by the CDC
/// device serial driver. This function behaves exactly like USBD_Write.
/// \param data Pointer to the data buffer to send.
/// \param size Size of the data buffer in bytes.
/// \param callback Optional callback function to invoke when the transfer
/// finishes.
/// \param argument Optional argument to the callback function.
/// \return USBD_STATUS_SUCCESS if the read operation has been started normally;
/// otherwise, the corresponding error code.
//------------------------------------------------------------------------------
unsigned char USBPrinterDriver_Write(void *data,
unsigned int size,
TransferCallback callback,
void *argument)
{
return USBD_Write(USBPrinterDriverDescriptors_DATAIN,
data,
size,
callback,
argument);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -