PDL for FM4  Version1.2
Peripheral Driverl Library for FM4
UsbDevicePrinter.c
Go to the documentation of this file.
1 /*******************************************************************************
2 * Copyright (C) 2013 Spansion LLC. All Rights Reserved.
3 *
4 * This software is owned and published by:
5 * Spansion LLC, 915 DeGuigne Dr. Sunnyvale, CA 94088-3453 ("Spansion").
6 *
7 * BY DOWNLOADING, INSTALLING OR USING THIS SOFTWARE, YOU AGREE TO BE BOUND
8 * BY ALL THE TERMS AND CONDITIONS OF THIS AGREEMENT.
9 *
10 * This software contains source code for use with Spansion
11 * components. This software is licensed by Spansion to be adapted only
12 * for use in systems utilizing Spansion components. Spansion shall not be
13 * responsible for misuse or illegal use of this software for devices not
14 * supported herein. Spansion is providing this software "AS IS" and will
15 * not be responsible for issues arising from incorrect user implementation
16 * of the software.
17 *
18 * SPANSION MAKES NO WARRANTY, EXPRESS OR IMPLIED, ARISING BY LAW OR OTHERWISE,
19 * REGARDING THE SOFTWARE (INCLUDING ANY ACOOMPANYING WRITTEN MATERIALS),
20 * ITS PERFORMANCE OR SUITABILITY FOR YOUR INTENDED USE, INCLUDING,
21 * WITHOUT LIMITATION, THE IMPLIED WARRANTY OF MERCHANTABILITY, THE IMPLIED
22 * WARRANTY OF FITNESS FOR A PARTICULAR PURPOSE OR USE, AND THE IMPLIED
23 * WARRANTY OF NONINFRINGEMENT.
24 * SPANSION SHALL HAVE NO LIABILITY (WHETHER IN CONTRACT, WARRANTY, TORT,
25 * NEGLIGENCE OR OTHERWISE) FOR ANY DAMAGES WHATSOEVER (INCLUDING, WITHOUT
26 * LIMITATION, DAMAGES FOR LOSS OF BUSINESS PROFITS, BUSINESS INTERRUPTION,
27 * LOSS OF BUSINESS INFORMATION, OR OTHER PECUNIARY LOSS) ARISING FROM USE OR
28 * INABILITY TO USE THE SOFTWARE, INCLUDING, WITHOUT LIMITATION, ANY DIRECT,
29 * INDIRECT, INCIDENTAL, SPECIAL OR CONSEQUENTIAL DAMAGES OR LOSS OF DATA,
30 * SAVINGS OR PROFITS,
31 * EVEN IF SPANSION HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
32 * YOU ASSUME ALL RESPONSIBILITIES FOR SELECTION OF THE SOFTWARE TO ACHIEVE YOUR
33 * INTENDED RESULTS, AND FOR THE INSTALLATION OF, USE OF, AND RESULTS OBTAINED
34 * FROM, THE SOFTWARE.
35 *
36 * This software may be replicated in part or whole for the licensed use,
37 * with the restriction that this Disclaimer and Copyright notice must be
38 * included with each copy of this software, whether used in part or whole,
39 * at all times.
40 */
41 /******************************************************************************/
55 /*****************************************************************************/
56 /* Include files */
57 /*****************************************************************************/
58 
59 #include "UsbDevicePrinter.h"
60 
61 #if ((FM_PERIPHERAL_USB_DEVICE_ENABLED == ON) && (USBDEVICEPRINTER_ENABLED == ON))
62 
63 
64 /*****************************************************************************/
65 /* Local pre-processor symbols/macros ('#define') */
66 /*****************************************************************************/
67 
68 
69 
70 /*****************************************************************************/
71 /* Global variable definitions (declared in header file with 'extern') */
72 /*****************************************************************************/
73 
74 /*****************************************************************************/
75 /* Local type definitions ('typedef') */
76 /*****************************************************************************/
77 
78 
79 
80 /*****************************************************************************/
81 /* Local variable definitions ('static') */
82 /*****************************************************************************/
83 
84 static uint8_t DataReceivedFlags = 0; //Status for received data
85 static uint8_t DataSentFlags = 0; //Status for sent data
86 
87 
88 static uint32_t u32LastReceivedSize; // last received size
89 static stc_usbn_endpoint_buffer_t stcEndpointBufferOUT; //struct for custom buffer
90 static uint8_t pu8IngoingBuffer[BUFFER_SIZE_ENDPOINTOUT]; // custom buffer
92 
94 stc_usbn_endpoint_buffer_t stcEpBufferIN; //struct for custom buffer
96 static stc_usbn_t* pstcUsbHandle = NULL;
97 
100 
101 
102 /*****************************************************************************/
103 /* Local function prototypes ('static') */
104 /*****************************************************************************/
105 
106 static void ClassSetupRequest(stc_usbn_t* pstcUsb, stc_usb_request_t* pstcSetup);
107 
108 static void UsbDevicePrinter_RxCallback(stc_usbn_t* pstcUsb, struct stc_usbn_endpoint_data* pstcEndpoint);
109 
110 static void UsbDevicePrinter_TxCallback(stc_usbn_t* pstcUsb, struct stc_usbn_endpoint_data* pstcEndpoint);
111 
112 
113 /*****************************************************************************/
114 /* Function implementation - global ('extern') and local ('static') */
115 /*****************************************************************************/
116 
124 void UsbDevicePrinter_Init(stc_usbn_t* pstcUsb)
125 {
126  stc_usbdevice_class_config_t stcUsbClassConfig;
127  stc_usbdevice_endpoint_config_t stcEndpointConfig;
128  uint8_t* pu8Interface = NULL;
129  uint8_t u8InterfaceLength = 0;
130  uint8_t u8i = 0;
131  uint8_t u8NumEndpoints = 0;
132  pstcUsbHandle = pstcUsb;
133 
134  stcUsbClassConfig.u8InterfaceNumber = 0xFF;
135  stcUsbClassConfig.u8InterfaceClass = 0x07;
136  stcUsbClassConfig.u8InterfaceSubClass = 0x01;
137  stcUsbClassConfig.u8InterfaceProtocoll = 0x02;
138  stcUsbClassConfig.pfnSetupRequestCallback = ClassSetupRequest;
139  stcUsbClassConfig.pfnConnectionCallback = NULL;
140  stcUsbClassConfig.pfnDisconnectionCallback = NULL;
141  stcUsbClassConfig.pfnConfCallback = NULL;
142  UsbDevice_RegisterVendorClass(pstcUsb,&stcUsbClassConfig,&pu8Interface,&u8InterfaceLength);
143 
144  for(u8i = 0;u8i < u8InterfaceLength;)
145  {
146  u8i += pu8Interface[u8i];
147  if (USBDESCR_ENDPOINT == pu8Interface[u8i + 1])
148  {
149  stcEndpointConfig.u8EndpointAddress = pu8Interface[u8i + 2];
150  if (((stcEndpointConfig.u8EndpointAddress) & 0x80) == 0)
151  {
152  stcEndpointConfig.pfnRxTxCallback = UsbDevicePrinter_RxCallback;
153  stcEndpointConfig.pstcEndpointBuffer = &stcEndpointBufferOUT;
154  pstcEndpointOUT = UsbDevice_SetupEndpoint(pstcUsb, &stcEndpointConfig);
155 
157  }
158  else
159  {
160  stcEndpointConfig.pfnRxTxCallback = UsbDevicePrinter_TxCallback;
161  stcEndpointConfig.pstcEndpointBuffer = &stcEndpointBufferIN;
162  pstcEndpointIN = UsbDevice_SetupEndpoint(pstcUsb, &stcEndpointConfig);
163  }
164  u8NumEndpoints++;
165  }
166  }
167 }
168 
177 {
178  pstnReceivedData = pstnCallback;
179 }
180 
189 {
190  pstnSentData = pstnCallback;
191 }
192 
202 static void ClassSetupRequest(stc_usbn_t* pstcUsb, stc_usb_request_t* pstcSetup)
203 {
204 
205 }
206 
215 static void UsbDevicePrinter_RxCallback(stc_usbn_t* pstcUsb, struct stc_usbn_endpoint_data* pstcEndpoint)
216 {
217  uint8_t* pu8Buffer = pstcEndpoint->pstcEndpointBuffer->pu8Buffer;
218  uint32_t u32DataSize = pstcEndpoint->pstcEndpointBuffer->u32DataSize;
219  u32LastReceivedSize = u32DataSize;
220  DataReceivedFlags |= (1<<2); // setting data received flag
221  if (pstnReceivedData != NULL)
222  {
223  pstnReceivedData(pu8Buffer,u32DataSize);
224  }
225  //Add your code here to process the received buffer
227 }
228 
229 
236 uint32_t UsbDevicePrinter_GetReceivedData(uint8_t** ppu8Buffer)
237 {
238  if ((DataReceivedFlags & (1<<2)) == 0)
239  {
240  return 0; // nothing to receive
241  }
242  DataReceivedFlags -= (1<<2);
243  *ppu8Buffer = pstcEndpointOUT->pstcEndpointBuffer->pu8Buffer;
244  return u32LastReceivedSize;
245 }
246 
258 boolean_t UsbDevicePrinter_SendData(stc_usbn_t* pstcUsb, uint8_t* pu8Buffer, uint32_t u32DataSize, en_usbsend_mode_t enMode)
259 {
260  if (UsbDevice_GetStatus(pstcUsb) == UsbConfigured)
261  {
262  DataSentFlags &= ~(1<<1);
263 
264  UsbDevice_SendData(pstcUsb, pstcEndpointIN, pu8Buffer, u32DataSize, enMode);
265  if (enMode == UsbPOLL)
266  {
267  DataSentFlags |= (1<<1);
268  }
269  return TRUE;
270  }
271  return FALSE;
272 }
273 
281 static void UsbDevicePrinter_TxCallback(stc_usbn_t* pstcUsb, struct stc_usbn_endpoint_data* pstcEndpoint)
282 {
283  DataSentFlags |= (1<<1);
284  if (pstnSentData != NULL)
285  {
286  pstnSentData();
287  }
288 }
295 {
296  if ((DataSentFlags & (1<<1)) > 0)
297  {
298  return 1;
299  }
300  else
301  {
302  return 0;
303  }
304 }
305 
306 #endif