PDL for FM4  Version1.2
Peripheral Driverl Library for FM4
usbhost.h
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 /******************************************************************************/
60 #ifndef __USBHOST_H__
61 #define __USBHOST_H__
62 
95 
96 /* C binding of definitions if building with C++ compiler */
97 #ifdef __cplusplus
98 extern "C"
99 {
100 #endif
101 
102 /******************************************************************************/
103 /* Include files */
104 /******************************************************************************/
105 
106 #include "usb.h"
107 
108 #if (FM_PERIPHERAL_USB_HOST_ENABLED == ON)
109 
110 #define USBHOST_CONNECTION_DELAY 200000
111 #define MAX_HOST_ENDPOINTS 6
112 // Token mask in the HTOKEN register
113 #define HTOKEN_SETUP 0x10
114 #define HTOKEN_IN 0x20
115 #define HTOKEN_OUT 0x30
116 #define HTOKEN_SOF 0x40
117 #define HTOKEN_ISO_IN 0x50
118 #define HTOKEN_ISO_OUT 0x60
119 
120 #define CREATE_TOKEN(toggle, type, endpoint) ((toggle) << 7) | type | endpoint
121 
122 #define HERR_SOF_MASK 0x80
123 #define HERR_RCV_MASK 0x40
124 #define HERR_TOUT_MASK 0x20
125 #define HERR_CRC_MASK 0x10
126 #define HERR_TOGGLE_MASK 0x8
127 #define HERR_BITSTUFF_MASK 0x4
128 
129 #define USBHOST_SCHEDULER_COMPLETION 0x01
130 #define USBHOST_SCHEDULER_SOF 0x02
131 #define USBHOST_SCHEDULER_IDLE 0x03
132 #define USBHOST_SCHEDULER_BUSY 0x04
133 
134 #define USBHOST_SUCCESS 0
135 #define USBHOST_ERROR 1
136 
137 #define USBHOST_ENDPOINTSTATUS_INITIATE 0x01
138 #define USBHOST_ENDPOINTSTATUS_ACTIVE 0x02
139 #define USBHOST_ENDPOINTSTATUS_INUSE 0x04
140 #define USBHOST_ENDPOINTSTATUS_DATA 0x08
141 #define USBHOST_ENDPOINTSTATUS_STALL 0x10
142 
143 #define USBHOST_EPADDR0_OUT 0x00
144 #define USBHOST_EPADDR0_IN 0x80
145 
146 
147 #define HERR_ACK 0
148 #define HERR_NAK 0x01
149 #define HERR_STALL 0x02
150 #define HERR_NULL 0x03
151 
152 #define BM_REQUESTTYPE_DIROUT 0x00
153 #define BM_REQUESTTYPE_DIRIN 0x80
154 
155 #define BM_REQUESTTYPE_DIR 0x80
156 #define BM_REQUESTTYPE_TYPE 0x60
157 #define BM_REQUESTTYPE_RECEIVER 0x1F
158 
159 
160 #define USBHOST_STAGE_IDLE 1
161 #define USBHOST_STAGE_SETUP 2
162 #define USBHOST_STAGE_DATA 3
163 #define USBHOST_STAGE_STATUS 4
164 #define USBHOST_STAGE_HANDSHAKE 5
165 
166 #define USBHOST_DEVICE_IDLE 1
167 #define USBHOST_DEVICE_ADDRESSING 2
168 #define USBHOST_DEVICE_ADDRESSED 4
169 #define USBHOST_DEVICE_ENUMERATED 8
170 #define USBHOST_DEVICE_CONFIGURATING 16
171 #define USBHOST_DEVICE_CONFIGURED 32
172 
173 #ifndef IS_SET
174  #define IS_SET(x,y) ((x & y) > 0)
175 #endif
176 
177 #ifndef IS_CLEARED
178  #define IS_CLEARED(x,y) ((x & y) == 0)
179 #endif
180 
181 #ifndef SET_MASK
182  #define SET_MASK(x,y) x |= y
183 #endif
184 
185 #ifndef CLEAR_MASK
186  #define CLEAR_MASK(x,y) x &= ~y
187 #endif
188 
189 #ifndef TOGGLE_MASK
190  #define TOGGLE_MASK(x,y) SET_MASK(x,y); CLEAR_MASK(x,y)
191 #endif
192 #define ISOUT(x) (!ISIN(x))
193 #define ISIN(x) ((Handler->u8Address) & 0x80)
194 
195 #define USBHOST_RESETENDPOINTBUFFER(endpoint,buffer,datasize) (endpoint->u32DataSize) = datasize;\
196  (endpoint->pu8Buffer) = buffer;\
197  (endpoint->pu8BufferPosNextPackage) = buffer;\
198  (endpoint->pu8BufferPos) = buffer;\
199  if (endpoint->u32BufferSize < datasize) (endpoint->u32BufferSize) = datasize
200 
201 #define USB_DESCRIPTOR_LENGTH 0x00
202 #define USB_DESCRIPTOR_DESCRIPTORTYPE 0x01
203 #define USB_INTERFACE_DESCRIPTOR_INTERFACENUMBER 0x03
204 
205 #define USBH_STATUS_SUCCESS 0x0000
206 #define USBH_STATUS_ERROR 0x0001
207 #define USBH_STATUS_STALL 0x0008
208 #define USBH_STATUS_LENGTH 0x000D
209 
210 #define UsbHost_GetUsbInterfaceDescriptor(x,y,z) UsbHost_GetUsbEndpointDescriptor(x,y,z, 0)
211 
212 /*****************************************************************************/
213 /* Global type definitions ('typedef') */
214 /*****************************************************************************/
215 
216 
224 typedef struct stc_host_endpoint
225 {
226  uint8_t* pu8Buffer;
227  uint8_t* pu8BufferPos;
228  uint8_t* pu8BufferPosNextPackage;
229  uint32_t u32DataSize;
230  uint32_t u32BufferSize;
231  uint16_t u16FifoSize;
232  void (* CompletionHandler)(stc_usbn_t* pstcUsb);
233  boolean_t bToggle;
234  uint8_t u8LastToken;
235  volatile uint8_t u8Status;
236  volatile uint8_t u8Stage;
237  uint8_t u8Address;
238  uint8_t u8Interval;
239  uint8_t u8IntervalCount;
240  uint8_t u8Retries;
241  boolean_t bAbortTransfer;
242  boolean_t bIsochronous;
243 } stc_host_endpoint_t;
244 
252 typedef struct stc_usbhost_intern_data
253 {
254  stc_usbn_endpoint_data_t* pstcUsbHalEndpointOUT;
255  stc_usbn_endpoint_data_t* pstcUsbHalEndpointIN;
256  stc_host_endpoint_t* Ep0IN;
257  stc_host_endpoint_t* Ep0OUT;
258  stc_host_endpoint_t* ActiveEndpoint;
259  uint8_t u8DeviceStatus;
260  uint8_t u8DeviceAddress;
261  uint8_t u8EnumerationCounter;
262  stc_usb_request_t stcLastSetup;
263  stc_usb_request_t stcLastSetupBeforeStall;
264  uint8_t* pu8SetupData;
265  volatile uint32_t u32Timeout;
266  volatile uint32_t u32ConTimeout;
267  volatile uint32_t u32BusResetTimeout;
268  volatile uint32_t u32ConnectionDelay;
269  uint8_t u8CurrentEndpointPosition;
270  uint8_t u8EndpointListEnd;
271  volatile boolean_t bSchedulerLock;
272  volatile boolean_t bBusResetSend;
273  stc_host_endpoint_t astcHostEndpoints[MAX_HOST_ENDPOINTS];
274  uint8_t pu8Setup[8];
275  uint8_t u8SyncToken;
276  uint8_t u8SetupDelay;
277  uint8_t u8LastToken;
278  uint8_t u8SchedulerState;
279  stc_host_endpoint_t* pstcClearEndpoint;
280  volatile boolean_t bFullSpeed;
281  volatile uint32_t u32WaitForCompletionTimeout;
282  volatile boolean_t bSofToken;
283  uint8_t pu8GlobalBuffer[2000];
284  void (* StallCompletionHandler)(stc_usbn_t* pstcUsb);
285  void (* SetupCompletionHandler)(stc_usbn_t* pstcUsb, uint8_t* pu8Buffer, uint32_t u32DataSize);
286 } stc_usbhost_intern_data_t;
287 
295 typedef struct stc_usbhostn_instance_data
296 {
297  volatile stc_usbn_t* pstcInstance;
298  stc_usbhost_intern_data_t stcInternData;
299 } stc_usbhostn_instance_data_t;
300 
301 
302 
303 
304 
305 
306 
307 
308 uint8_t UsbHost_AddHostEndpoint(stc_usbn_t* pstcUsb, stc_host_endpoint_t** pstcHandler, uint8_t u8EndpointAddress, uint16_t u16FifoSize, uint8_t u8Interval, void(* CompletionHandler)(stc_usbn_t* pstcUsb));
309 
310 stc_host_endpoint_t* UsbHost_EndpointFromNumber(stc_usbn_t* pstcUsb, uint8_t u8EndpointNumber);
311 
312 void UsbHost_AddressDevice(stc_usbn_t* pstcUsb, uint8_t u8NewAddress);
313 
314 uint32_t UsbHost_ReceiveSetupData(stc_usbn_t* pstcUsb, uint8_t** pu8Buffer);
315 
316 void UsbHost_SetupRequestWithData(stc_usbn_t* pstcUsb, stc_usb_request_t* pstcSetup, uint8_t* pu8Data, void(*SetupCompletion)(stc_usbn_t* pstcUsb, uint8_t* pu8Buffer, uint32_t u32DataSize));
317 
318 void UsbHost_SetupRequest(stc_usbn_t* pstcUsb, stc_usb_request_t* pstcSetup, void(*SetupCompletion)(stc_usbn_t* pstcUsb, uint8_t* pu8Buffer, uint32_t u32DataSize));
319 
320 void UsbHost_ConnectionCallback(stc_usbn_t* pstcUsb);
321 
322 void UsbHost_ClearAllEndpoints(stc_usbn_t* pstcUsb);
323 
324 void UsbHost_DisconnectionCallback(stc_usbn_t* pstcUsb);
325 
326 void UsbHost_CompletionCallback(stc_usbn_t* pstcUsb);
327 
328 void UsbHost_SofCallback(stc_usbn_t* pstcUsb);
329 
330 void UsbHost_BusresetCallback(stc_usbn_t* pstcUsb);
331 
332 void UsbHost_InitCallback(stc_usbn_t* pstcUsb,struct stc_usb_config* pstcConfig);
333 
334 void UsbHost_TransferDataToFifo(stc_usbn_t* pstcUsb, uint8_t* pu8Buffer, uint16_t u16Size, uint16_t u16FifoSize);
335 
336 uint16_t UsbHost_TransferFifoToBuffer(stc_usbn_t* pstcUsb, uint8_t* pu8Buffer, uint32_t u32MaxBufferSize);
337 
338 void UsbHost_TransferData(stc_usbn_t* pstcUsb, stc_host_endpoint_t* Handler,uint8_t* pu8Buffer, uint32_t u32BufferSize, void (* CompletionHandler)(stc_usbn_t* pstcUsb));
339 
340 void UsbHost_Init(stc_usbn_t* pstcUsb, stc_usb_config_t* pstcUsbConfig);
341 
342 uint8_t UsbHost_GetDeviceStatus(stc_usbn_t* pstcUsb);
343 
344 void UsbHost_ClearEndpoint(stc_usbn_t* pstcUsb, stc_host_endpoint_t* pstcEndpoint);
345 
346 void UsbHost_AbortTransfer(stc_host_endpoint_t* pstcEndpoint);
347 
348 void UsbHost_ResetToggle(stc_host_endpoint_t* pstcEndpoint);
349 
350 void UsbHost_ClearEndpointCompletion(stc_usbn_t* pstcUsb, uint8_t* pu8Buffer, uint32_t u32DataSize);
351 
352 void UsbHost_ClearEndpointPoll(stc_usbn_t* pstcUsb, stc_host_endpoint_t* pstcEndpoint);
353 
354 void UsbHost_SetConfigurationDescriptor(stc_usbn_t* pstcUsb, uint8_t u8Configuration);
355 
356 uint8_t* UsbHost_GetUsbEndpointDescriptor(uint8_t* pu8Buffer, uint16_t u16Size, uint8_t u8InterfaceNumber, uint8_t u8EndpointNumber);
357 
358 void UsbHost_SchedulerInitiateEndpoint(stc_usbn_t* pstcUsb, stc_usbhost_intern_data_t* pstcUsbHostIntern, stc_host_endpoint_t* pstcEndpoint);
359 
360 void UsbHost_Tick(void);
361 
362 #ifdef __cplusplus
363 }
364 #endif
365 
366 
367 #endif /* (FM_PERIPHERAL_USB_HOST_ENABLED == ON) */
368 
369 
370 #endif