PDL for FM4  Version1.2
Peripheral Driverl Library for FM4
UsbHostHidCom.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 "usbhosthidcom.h"
60 
61 #if ((FM_PERIPHERAL_USB_HOST_ENABLED == ON) && (USBHOSTHIDCOM_ENABLED == ON))
62 
63 static stc_host_endpoint_t *EndpointOUT;
64 static stc_host_endpoint_t *EndpointIN;
65 static uint8_t u8EndpointIN, u8EndpointOUT;
67 static volatile boolean_t bDataSent = FALSE;
68 static volatile boolean_t bDataSending = FALSE;
69 static volatile boolean_t bDataReceived = FALSE;
70 static stc_usbn_t* pstcUsbHandle = NULL;
71 volatile static uint8_t u8Status = 0;
72 void (*UsbHostHidComDataSentHandler)(void) = 0;
73 void (*UsbHostHidComDataReceivedHandler)(uint8_t* pu8Buffer) = 0;
74 
75 static uint8_t pu8DataReceived[64];
76 
77 void UsbHostHidCom_DataReceivedHandler(stc_usbn_t* pstcUsb)
78 {
81  {
84  }
85 }
86 
87 void UsbHostHidCom_DataSentHandler(stc_usbn_t* pstcUsb)
88 {
89  bDataSent = TRUE;
92  {
94  bDataSent = FALSE;
95  }
96 }
97 
98 void UsbHostHidCom_SetDataSentHandler(void (*_UsbHostHidComDataSentHandler)(void))
99 {
100  UsbHostHidComDataSentHandler = _UsbHostHidComDataSentHandler;
101 }
102 
103 void UsbHostHidCom_SetDataReceivedHandler(void (*_UsbHostHidComDataReceivedHandler)(uint8_t* pu8Buffer))
104 {
105  UsbHostHidComDataReceivedHandler = _UsbHostHidComDataReceivedHandler;
106 }
107 
108 void UsbHostHidCom_InitiateSending(uint8_t* pu8Buffer)
109 {
110  bDataSent = FALSE;
111  bDataSending = TRUE;
112  UsbHost_TransferData(pstcUsbHandle,EndpointOUT,pu8Buffer, 64, UsbHostHidCom_DataSentHandler);
113 }
114 
115 void UsbHostHidCom_SendPolled(uint8_t* pu8Buffer)
116 {
117  bDataSent = FALSE;
118  bDataSending = TRUE;
119  UsbHost_TransferData(pstcUsbHandle,EndpointOUT,pu8Buffer, 64, UsbHostHidCom_DataSentHandler);
120  while(bDataSent == FALSE);
121 }
122 
124 {
125  while(bDataReceived == FALSE);
127  return pu8DataReceived;
128 }
129 
131 {
132  return bDataSent;
133 }
134 
136 {
137  return bDataSending;
138 }
139 
141 {
142  return bDataReceived;
143 }
144 
145 
146 
147 boolean_t UsbHostHidCom_InitHandler(stc_usbn_t* pstcUsb, uint8_t* pu8Configuration, uint32_t u32Length)
148 {
149  uint8_t* pu8Buffer;
150  uint8_t u8NumberOfInterfaces;
151  uint8_t u8NumberOfEndpoints;
152  uint8_t u8EndpointNumber;
153  uint8_t u8InterfaceNumber;
154 
155  if (pstcUsbHandle != NULL)
156  {
157  return FALSE;
158  }
159  pstcUsbHandle = pstcUsb;
160  bDataSent = FALSE;
163  u8NumberOfInterfaces=pu8Configuration[4];
164  for(u8InterfaceNumber=0;u8InterfaceNumber < u8NumberOfInterfaces;u8InterfaceNumber++)
165  {
166  pu8Buffer = UsbHost_GetUsbInterfaceDescriptor(pu8Configuration,u32Length,u8InterfaceNumber);
167  if (pu8Buffer == 0)
168  {
169  return FALSE;
170  }
171  u8NumberOfEndpoints = pu8Buffer[4];
172 
173  if ((pu8Buffer[5] == 0x03) && (pu8Buffer[6] == 0x00) && (pu8Buffer[7] == 0x0)) //Class & SubClass & Protocol
174  {
175  for(u8EndpointNumber=1;u8EndpointNumber<=u8NumberOfEndpoints;u8EndpointNumber++)
176  {
177  pu8Buffer = UsbHost_GetUsbEndpointDescriptor(pu8Configuration,u32Length,u8InterfaceNumber,u8EndpointNumber);
178  if (pu8Buffer[2] & USB_IN_DIRECTION)
179  {
180  u8EndpointIN = pu8Buffer[2];
181  u16MaxPackageSizeIN = (uint16_t)(pu8Buffer[4] + (pu8Buffer[5] << 8));
182  }
183  else
184  {
185  u8EndpointOUT = pu8Buffer[2];
186  u16MaxPackageSizeOUT = (uint16_t)(pu8Buffer[4] + (pu8Buffer[5] << 8));
187  }
188  if ((u8EndpointIN != 0) && (u8EndpointOUT != 0))
189  {
190  break;
191  }
192  }
193  if ((u8EndpointIN == 0) && (u8EndpointOUT == 0))
194  {
195 
196  return FALSE;
197  }
198 
199 
202  EndpointIN->pu8Buffer = pu8DataReceived;
203  EndpointIN->u32DataSize = 64;
204  break;
205  }
206  }
207  if ((u8EndpointIN == 0) || (u8EndpointOUT == 0))
208  {
209  return FALSE;
210  }
211  return TRUE;
212 }
213 
215 {
216  pstcUsbHandle = NULL;
217  bDataSent = FALSE;
220  u8Status = 0;
221  return TRUE;
222 }
223 
225 {
226  return (pstcUsbHandle != NULL);
227 }
228 
229 #endif