PDL for FM4  Version1.2
Peripheral Driverl Library for FM4
UsbDeviceHidJoystick.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 /******************************************************************************/
53 /*****************************************************************************/
54 /* Include files */
55 /*****************************************************************************/
56 #include "UsbDeviceHidJoystick.h"
57 
58 #if ((FM_PERIPHERAL_USB_DEVICE_ENABLED == ON) && (USBDEVICEHIDJOYSTICK_ENABLED == ON))
59 
60 /*****************************************************************************/
61 /* Local pre-processor symbols/macros ('#define') */
62 /*****************************************************************************/
63 
64 static boolean_t SendData(uint8_t* pu8Buffer, uint32_t u32DataSize, en_usbsend_mode_t enMode);
65 
66 /*****************************************************************************/
67 /* Global variable definitions (declared in header file with 'extern') */
68 /*****************************************************************************/
69 
70 /*****************************************************************************/
71 /* Local type definitions ('typedef') */
72 /*****************************************************************************/
73 
74 
75 
76 /*****************************************************************************/
77 /* Local variable definitions ('static') */
78 /*****************************************************************************/
79 
80 static uint8_t DataSentFlags = 0; //Status for sent data
81 static uint8_t pu8JoystickData[4] = {0,0,0,0};
84 static stc_usbn_t* pstcUsbHandle = NULL;
86 /*****************************************************************************/
87 /* Local function prototypes ('static') */
88 /*****************************************************************************/
89 
90 
101 static void ConfCallback(stc_usbn_t* pstcUsb)
102 {
103  if (UsbDevice_GetStatus(pstcUsb) == UsbConfigured)
104  {
105  bReady = TRUE;
106  } else
107  {
108  bReady = FALSE;
109  }
110 }
111 
112 /*****************************************************************************/
113 /* Function implementation - global ('extern') and local ('static') */
114 /*****************************************************************************/
115 
116 void UsbDeviceHidJoystick_Init(stc_usbn_t* pstcUsb)
117 {
118  stc_usbdevice_class_config_t stcUsbClassConfig;
119  stc_usbdevice_endpoint_config_t stcEndpointConfig;
120  uint8_t* pu8Interface = NULL;
121  uint8_t u8InterfaceLength = 0;
122  uint8_t u8i = 0;
123  uint8_t u8NumEndpoints = 0;
124  pstcUsbHandle = pstcUsb;
125 
126  stcUsbClassConfig.u8InterfaceNumber = 0xFF; // do not use fix interface number, choose by class, subclass, protocol
127  stcUsbClassConfig.u8InterfaceClass = 0x03; // HID Class
128  stcUsbClassConfig.u8InterfaceSubClass = 0x00; // Custom Sub Class
129  stcUsbClassConfig.u8InterfaceProtocoll = 0x00; // Custom Protocol
130  stcUsbClassConfig.pfnSetupRequestCallback = NULL; // setup requests handled
131  stcUsbClassConfig.pfnConnectionCallback = NULL; // No connection callback handled
132  stcUsbClassConfig.pfnDisconnectionCallback = NULL; // No disconnection callback handled
133  stcUsbClassConfig.pfnConfCallback = ConfCallback; // Callback for configuration set
134  UsbDevice_RegisterVendorClass(pstcUsb,&stcUsbClassConfig,&pu8Interface,&u8InterfaceLength);
135 
136  for(u8i = 0;u8i < u8InterfaceLength;)
137  {
138  u8i += pu8Interface[u8i];
139  if (USBDESCR_ENDPOINT == pu8Interface[u8i + 1])
140  {
141  stcEndpointConfig.u8EndpointAddress = pu8Interface[u8i + 2];
142  if (((stcEndpointConfig.u8EndpointAddress) & 0x80) == 0)
143  {
144  /* NOT USED */
145  }
146  else
147  {
148  stcEndpointConfig.pstcEndpointBuffer = &stcEndpointBufferIN;
149  pstcEndpointIN = UsbDevice_SetupEndpoint(pstcUsb, &stcEndpointConfig);
150  }
151  u8NumEndpoints++;
152  }
153  }
154 
155 }
156 
167 static boolean_t SendData(uint8_t* pu8Buffer, uint32_t u32DataSize, en_usbsend_mode_t enMode)
168 {
170  {
171  DataSentFlags &= ~(1<<1);
172 
173  UsbDevice_SendData(pstcUsbHandle, pstcEndpointIN, pu8Buffer, u32DataSize, enMode);
174  if (enMode == UsbPOLL)
175  {
176  DataSentFlags |= (1<<1);
177  }
178  return TRUE;
179  }
180  return FALSE;
181 }
182 
183 
191 {
192  pu8JoystickData[0] = (uint8_t)iThrottle;
193  SendData((uint8_t*)pu8JoystickData,4,UsbPOLL);
194 }
195 
196 
204 {
205  pu8JoystickData[1] = (uint8_t)iX;
206  SendData((uint8_t*)pu8JoystickData,4,UsbPOLL);
207 }
208 
209 
217 {
218  pu8JoystickData[2] = (uint8_t)iY;
219  SendData((uint8_t*)pu8JoystickData,4,UsbPOLL);
220 }
221 
222 
232 void UsbDeviceHidJoystick_ChangeXY(int8_t iX, int8_t iY)
233 {
234  pu8JoystickData[1] = (uint8_t)iX;
235  pu8JoystickData[2] = (uint8_t)iY;
236  SendData((uint8_t*)pu8JoystickData,4,UsbPOLL);
237 }
238 
239 
247 {
248  if (bButtonValue)
249  {
250  pu8JoystickData[3] |= (1 << 4);
251  }
252  else
253  {
254  pu8JoystickData[3] &= ~(1 << 4);
255  }
256  SendData((uint8_t*)pu8JoystickData,4,UsbPOLL);
257 }
258 
259 
267 {
268  if (bButtonValue)
269  {
270  pu8JoystickData[3] |= (1 << 5);
271  }
272  else
273  {
274  pu8JoystickData[3] &= ~(1 << 5);
275  }
276  SendData((uint8_t*)pu8JoystickData,4,UsbPOLL);
277 }
278 
279 
287 {
288  if (bButtonValue)
289  {
290  pu8JoystickData[3] |= (1 << 6);
291  }
292  else
293  {
294  pu8JoystickData[3] &= ~(1 << 6);
295  }
296  SendData((uint8_t*)pu8JoystickData,4,UsbPOLL);
297 }
298 
299 
307 {
308  if (bButtonValue)
309  {
310  pu8JoystickData[3] |= (1 << 6);
311  }
312  else
313  {
314  pu8JoystickData[3] &= ~(1 << 6);
315  }
316  SendData((uint8_t*)pu8JoystickData,4,UsbPOLL);
317 }
318 
319 
326 {
327  pu8JoystickData[3] = (pu8JoystickData[3] & 0xF0) | 0;
328 
329  SendData((uint8_t*)pu8JoystickData,4,UsbPOLL);
330 }
331 
332 
339 {
340  pu8JoystickData[3] = (pu8JoystickData[3] & 0xF0) | 1;
341 
342  SendData((uint8_t*)pu8JoystickData,4,UsbPOLL);
343 }
344 
345 
352 {
353  pu8JoystickData[3] = (pu8JoystickData[3] & 0xF0) | 2;
354 
355  SendData((uint8_t*)pu8JoystickData,4,UsbPOLL);
356 }
357 
358 
365 {
366  pu8JoystickData[3] = (pu8JoystickData[3] & 0xF0) | 3;
367 
368  SendData((uint8_t*)pu8JoystickData,4,UsbPOLL);
369 }
370 
371 
378 {
379  pu8JoystickData[3] = (pu8JoystickData[3] & 0xF0) | 7;
380 
381  SendData((uint8_t*)pu8JoystickData,4,UsbPOLL);
382 }
383 
392 {
393  return bReady;
394 }
395 #endif