PDL for FM4  Version1.2
Peripheral Driverl Library for FM4
UsbDeviceHidCom.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 /*****************************************************************************/
56 #define __USBDEVICEHIDCOM_C__
57 
58 /******************************************************************************/
59 /* Include files */
60 /******************************************************************************/
61 
62 #include "UsbDeviceHidCom.h"
63 
64 #if ((FM_PERIPHERAL_USB_DEVICE_ENABLED == ON) && (USBDEVICEHIDCOM_ENABLED == ON))
65 /******************************************************************************/
66 /* Local type definitions ('typedef') */
67 /******************************************************************************/
68 
69 /******************************************************************************/
70 /* Local function prototypes ('static') */
71 /******************************************************************************/
72 
73 static void RxTxCallback(stc_usbn_t* pstcUsb, struct stc_usbn_endpoint_data* pstcEndpoint);
74 static void ConfCallback(stc_usbn_t* pstcUsb);
75 
76 /******************************************************************************/
77 /* Local variable definitions (declared in header file with 'extern') */
78 /******************************************************************************/
79 
80 static const uint8_t u8MaxSupportedEndpoints = 2;
83 static uint8_t ppu8Buffers[2][64];
84 static const uint32_t pu32BufferSizes[2] = {64,64};
90 static stc_usbn_t* pstcUsbHandle = NULL;
91 
92 /******************************************************************************/
93 /* Global variable definitions (declared in header file with 'extern') */
94 /******************************************************************************/
95 
96 /******************************************************************************/
97 /* Procedures / Functions */
98 /******************************************************************************/
99 
110 static void ConfCallback(stc_usbn_t* pstcUsb)
111 {
112  if (UsbDevice_GetStatus(pstcUsb) == UsbConfigured)
113  {
114  bReady = TRUE;
115  } else
116  {
117  bReady = FALSE;
118  }
119 }
120 
132 static void RxTxCallback(stc_usbn_t* pstcUsb, struct stc_usbn_endpoint_data* pstcEndpoint)
133 {
134  volatile uint8_t i;
135  volatile uint8_t u8EndpointIndex = 0xFF;
136 
137  if ((pstcEndpoint->u8EndpointAddress) & 0x80)
138  {
139  bDataSent = TRUE;
140  }
141  else
142  {
144  }
145 }
146 
156 void UsbDeviceHidCom_Init(stc_usbn_t* pstcUsb)
157 {
158  stc_usbdevice_class_config_t stcUsbClassConfig;
159  stc_usbdevice_endpoint_config_t stcEndpointConfig;
160  uint8_t* pu8Interface = NULL;
161  uint8_t u8InterfaceLength = 0;
162  uint8_t u8i = 0;
163  uint8_t u8NumEndpoints = 0;
164  pstcUsbHandle = pstcUsb;
165 
166  stcUsbClassConfig.u8InterfaceNumber = 0xFF; // do not use fix interface number, choose by class, subclass, protocol
167  stcUsbClassConfig.u8InterfaceClass = 0x03; // HID Class
168  stcUsbClassConfig.u8InterfaceSubClass = 0x00; // Custom Sub Class
169  stcUsbClassConfig.u8InterfaceProtocoll = 0x00; // Custom Protocol
170  stcUsbClassConfig.pfnSetupRequestCallback = NULL; // No setup requests handled
171  stcUsbClassConfig.pfnConnectionCallback = NULL; // No connection callback handled
172  stcUsbClassConfig.pfnDisconnectionCallback = NULL; // No disconnection callback handled
173  stcUsbClassConfig.pfnConfCallback = ConfCallback; // Callback for configuration set
174  UsbDevice_RegisterVendorClass(pstcUsb,&stcUsbClassConfig,&pu8Interface,&u8InterfaceLength);
175 
176  for(u8i = 0;u8i < u8InterfaceLength;)
177  {
178  u8i += pu8Interface[u8i];
179  if (USBDESCR_ENDPOINT == pu8Interface[u8i + 1])
180  {
181  stcEndpointConfig.u8EndpointAddress = pu8Interface[u8i + 2];
182  stcEndpointConfig.pfnRxTxCallback = RxTxCallback;
183  stcEndpointConfig.pstcEndpointBuffer = &(pstcEndpointBuffers[u8NumEndpoints]);
184  ppstcUsedEndpoints[u8NumEndpoints] = UsbDevice_SetupEndpoint(pstcUsb, &stcEndpointConfig);
185  if (((stcEndpointConfig.u8EndpointAddress) & 0x80) == 0)
186  {
187  pstcEndpointOUT = ppstcUsedEndpoints[u8NumEndpoints];
188  UsbDevice_ReceiveData(pstcUsb, ppstcUsedEndpoints[u8NumEndpoints], ppu8Buffers[u8NumEndpoints], pu32BufferSizes[u8NumEndpoints], UsbIRQ);
189  }
190  else
191  {
192  pstcEndpointIN = ppstcUsedEndpoints[u8NumEndpoints];
193  }
194  u8NumEndpoints++;
195  if (u8NumEndpoints > u8MaxSupportedEndpoints)
196  {
197  return;
198  }
199  }
200  }
201 
202 }
203 
216 {
217  if ((bReady == FALSE) || (bDataSent == FALSE))
218  {
219  return FALSE;
220  }
221  bDataSent = FALSE;
222  UsbDevice_SendData(pstcUsbHandle, pstcEndpointIN, pu8Buffer, 64, enMode);
223  if (enMode == UsbPOLL)
224  {
225  bDataSent = TRUE;
226  }
227  return TRUE;
228 }
229 
238 {
239  return !bDataSent;
240 }
241 
250 {
251  return bDataSent;
252 }
253 
263 uint32_t UsbDeviceHidCom_GetReceivedData(uint8_t* pu8OutBuffer)
264 {
265  if (bDataReceived == TRUE)
266  {
268  memcpy(pu8OutBuffer,pstcEndpointOUT->pstcEndpointBuffer->pu8Buffer,64);
269  UsbDevice_ReceiveData(pstcUsbHandle, pstcEndpointOUT, pstcEndpointOUT->pstcEndpointBuffer->pu8Buffer, pstcEndpointOUT->pstcEndpointBuffer->u32BufferSize, UsbIRQ);
270  return 64;
271  }
272  return 0;
273 }
274 #endif