USBD.c
Go to the documentation of this file.
1 /* ---------------------------------------------------------------------------- */
2 /* Atmel Microcontroller Software Support */
3 /* SAM Software Package License */
4 /* ---------------------------------------------------------------------------- */
5 /* Copyright (c) 2015, Atmel Corporation */
6 /* */
7 /* All rights reserved. */
8 /* */
9 /* Redistribution and use in source and binary forms, with or without */
10 /* modification, are permitted provided that the following condition is met: */
11 /* */
12 /* - Redistributions of source code must retain the above copyright notice, */
13 /* this list of conditions and the disclaimer below. */
14 /* */
15 /* Atmel's name may not be used to endorse or promote products derived from */
16 /* this software without specific prior written permission. */
17 /* */
18 /* DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR */
19 /* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF */
20 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE */
21 /* DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT, */
22 /* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT */
23 /* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, */
24 /* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF */
25 /* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING */
26 /* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, */
27 /* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
28 /* ---------------------------------------------------------------------------- */
29 
43 /*---------------------------------------------------------------------------
44  * Headers
45  *---------------------------------------------------------------------------*/
46 
47 #include "USBD.h"
48 #include "USBD_HAL.h"
49 
50 #include <USBLib_Trace.h>
51 
52 /*---------------------------------------------------------------------------
53  * Definitions
54  *---------------------------------------------------------------------------*/
55 
56 /*---------------------------------------------------------------------------
57  * Internal variables
58  *---------------------------------------------------------------------------*/
59 
61 static uint8_t deviceState;
63 static uint8_t previousDeviceState;
64 
65 uint8_t ForceFS = 0;
66 
67 /*---------------------------------------------------------------------------
68  * Internal Functions
69  *---------------------------------------------------------------------------*/
70 
71 /*---------------------------------------------------------------------------
72  * Exported functions
73  *---------------------------------------------------------------------------*/
74 
75 /*---------------------------------------------------------------------------
76  * USBD: Event handlers
77  *---------------------------------------------------------------------------*/
78 
84 {
85  /* Don't do anything if the device is already suspended */
87 
88  /* Switch to the Suspended state */
91 
92  /* Suspend HW interface */
94 
95  /* Invoke the User Suspended callback (Suspend System?) */
96  if (NULL != (void *)USBDCallbacks_Suspended)
98  }
99 }
100 
106 {
107  /* Don't do anything if the device was not suspended */
109  /* Active the device */
112 
114  /* Invoke the Resume callback */
115  if (NULL != (void *)USBDCallbacks_Resumed)
117  }
118  }
119 }
120 
127 {
128  /* The device enters the Default state */
130  /* Active the USB HW */
132  /* Only EP0 enabled */
133  USBD_HAL_ResetEPs(0xFFFFFFFF, USBD_STATUS_RESET, 0);
135 
136  /* Invoke the Reset callback */
137  if (NULL != (void *)USBDCallbacks_Reset)
139 }
140 
147 void USBD_RequestHandler(uint8_t bEndpoint,
148  const USBGenericRequest *pRequest)
149 {
150  if (bEndpoint != 0) {
151  TRACE_WARNING("EP%d request not supported, default EP only",
152  bEndpoint);
153  } else if (NULL != (void *)USBDCallbacks_RequestReceived)
155 }
156 
157 /*---------------------------------------------------------------------------
158  * USBD: Library interface
159  *---------------------------------------------------------------------------*/
160 
165 void USBD_ConfigureEndpoint(const USBEndpointDescriptor *pDescriptor)
166 {
167  USBD_HAL_ConfigureEP(pDescriptor);
168 }
169 
191 uint8_t USBD_Write(uint8_t bEndpoint,
192  const void *pData,
193  uint32_t dLength,
194  TransferCallback fCallback,
195  void *pArgument)
196 {
197  USBD_HAL_SetTransferCallback(bEndpoint, fCallback, pArgument);
198  return USBD_HAL_Write(bEndpoint, pData, dLength);
199 }
200 #if 0
201 
226 uint8_t USBD_MblWrite(uint8_t bEndpoint,
227  void *pMbl,
228  uint16_t wListSize,
229  uint8_t bCircList,
230  uint16_t wStartNdx,
231  MblTransferCallback fCallback,
232  void *pArgument)
233 {
234  Endpoint *pEndpoint = &(endpoints[bEndpoint]);
235  MblTransfer *pTransfer = (MblTransfer *) & (pEndpoint->transfer);
236  uint16_t i;
237 
238  /* EP0 is not suitable for Mbl */
239 
240  if (bEndpoint == 0)
241 
243 
244  /* Check that the endpoint is in Idle state */
245 
246  if (pEndpoint->state != UDP_ENDPOINT_IDLE)
247 
248  return USBD_STATUS_LOCKED;
249 
250  pEndpoint->state = UDP_ENDPOINT_SENDINGM;
251 
252  TRACE_DEBUG_WP("WriteM%d(0x%x,%d) ", bEndpoint, pMbl, wListSize);
253 
254  /* Start from first if not circled list */
255 
256  if (!bCircList) wStartNdx = 0;
257 
258  /* Setup the transfer descriptor */
259 
260  pTransfer->pMbl = (USBDTransferBuffer *)pMbl;
261  pTransfer->listSize = wListSize;
262  pTransfer->fCallback = fCallback;
263  pTransfer->pArgument = pArgument;
264  pTransfer->currBuffer = wStartNdx;
265  pTransfer->freedBuffer = 0;
266  pTransfer->pLastLoaded = &(((USBDTransferBuffer *)pMbl)[wStartNdx]);
267  pTransfer->circList = bCircList;
268  pTransfer->allUsed = 0;
269 
270  /* Clear all buffer */
271 
272  for (i = 0; i < wListSize; i ++) {
273 
274  pTransfer->pMbl[i].transferred = 0;
275  pTransfer->pMbl[i].buffered = 0;
276  pTransfer->pMbl[i].remaining = pTransfer->pMbl[i].size;
277  }
278 
279  /* Send the first packet */
280 
281  while ((UDP->UDP_CSR[bEndpoint]&UDP_CSR_TXPKTRDY) == UDP_CSR_TXPKTRDY);
282 
283  UDP_MblWriteFifo(bEndpoint);
284  SET_CSR(bEndpoint, UDP_CSR_TXPKTRDY);
285 
286  /* If double buffering is enabled and there is data remaining, */
287 
288  /* prepare another packet */
289 
290  if ((CHIP_USB_ENDPOINTS_BANKS(bEndpoint) > 1)
291  && (pTransfer->pMbl[pTransfer->currBuffer].remaining > 0))
292 
293  UDP_MblWriteFifo(bEndpoint);
294 
295  /* Enable interrupt on endpoint */
296 
297  UDP->UDP_IER = 1 << bEndpoint;
298 
299  return USBD_STATUS_SUCCESS;
300 }
301 #endif
302 
318 uint8_t USBD_Read(uint8_t bEndpoint,
319  void *pData,
320  uint32_t dLength,
321  TransferCallback fCallback,
322  void *pArgument)
323 {
324  USBD_HAL_SetTransferCallback(bEndpoint, fCallback, pArgument);
325  return USBD_HAL_Read(bEndpoint, pData, dLength);
326 }
327 #if 0
328 
336 uint8_t USBD_MblReuse(uint8_t bEndpoint,
337  uint8_t *pNewBuffer,
338  uint16_t wNewSize)
339 {
340  Endpoint *pEndpoint = &(endpoints[bEndpoint]);
341  MblTransfer *pTransfer = (MblTransfer *)&(pEndpoint->transfer);
342  USBDTransferBuffer *pBi = &(pTransfer->pMbl[pTransfer->freedBuffer]);
343 
344  TRACE_DEBUG_WP("MblReuse(%d), st%x, circ%d\n\r",
345  bEndpoint, pEndpoint->state, pTransfer->circList);
346 
347  /* Only for Multi-buffer-circle list */
348 
349  if (bEndpoint != 0
350  && (pEndpoint->state == UDP_ENDPOINT_RECEIVINGM
351  || pEndpoint->state == UDP_ENDPOINT_SENDINGM)
352  && pTransfer->circList) {
353  } else
354 
356 
357  /* Check if there is freed buffer */
358 
359  if (pTransfer->freedBuffer == pTransfer->currBuffer
360  && !pTransfer->allUsed)
361 
362  return USBD_STATUS_LOCKED;
363 
364  /* Update transfer information */
365 
366  if ((++ pTransfer->freedBuffer) == pTransfer->listSize)
367  pTransfer->freedBuffer = 0;
368 
369  if (pNewBuffer) {
370  pBi->pBuffer = pNewBuffer;
371  pBi->size = wNewSize;
372  }
373 
374  pBi->buffered = 0;
375  pBi->transferred = 0;
376  pBi->remaining = pBi->size;
377 
378  /* At least one buffer is not processed */
379 
380  pTransfer->allUsed = 0;
381  return USBD_STATUS_SUCCESS;
382 }
383 #endif
384 
388 void USBD_Halt(uint8_t bEndpoint)
389 {
390  USBD_HAL_Halt(bEndpoint, 1);
391 }
392 
397 void USBD_Unhalt(uint8_t bEndpoint)
398 {
399  USBD_HAL_Halt(bEndpoint, 0);
400 }
401 
407 uint8_t USBD_IsHalted(uint8_t bEndpoint)
408 {
409  return USBD_HAL_Halt(bEndpoint, 0xFF);
410 }
411 
416 uint8_t USBD_IsHighSpeed(void)
417 {
418  return USBD_HAL_IsHighSpeed();
419 }
420 
427 uint8_t USBD_Stall(uint8_t bEndpoint)
428 
429 {
430  return USBD_HAL_Stall(bEndpoint);
431 }
432 
437 void USBD_SetAddress(uint8_t address)
438 {
439  TRACE_INFO_WP("SetAddr(%d) ", address);
440 
441  USBD_HAL_SetAddress(address);
442 
443  if (address == 0)
445  else
447 }
448 
453 void USBD_SetConfiguration(uint8_t cfgnum)
454 {
455  TRACE_INFO_WP("SetCfg(%d) ", cfgnum);
456 
458 
459  if (cfgnum != 0)
461  else {
463  /* Reset all endpoints but Control 0 */
464  USBD_HAL_ResetEPs(0xFFFFFFFE, USBD_STATUS_RESET, 0);
465  }
466 }
467 
468 /*---------------------------------------------------------------------------
469  * USBD: Library API
470  *---------------------------------------------------------------------------*/
471 
476 {
477  /* Device is NOT suspended */
479  TRACE_INFO("USBD_RemoteWakeUp: Device is not suspended\n\r");
480  return;
481  }
482 
485 }
486 
490 void USBD_Connect(void)
491 {
492  TRACE_INFO(" Connect -");
494 }
495 
499 void USBD_Disconnect(void)
500 {
501  TRACE_INFO(" DisConnect -");
503 
504  /* Device returns to the Powered state */
505 
507 
509 
511 
513 }
514 
518 void USBD_Init(void)
519 {
520  TRACE_INFO_WP("USBD_Init\n\r");
521 
522  /* HW Layer Initialize */
523  USBD_HAL_Init();
524 
525  /* Device is in the Attached state */
528 
529  /* Upper Layer Initialize */
530  if (NULL != (void *)USBDCallbacks_Initialized)
532 }
533 
538 uint8_t USBD_GetState(void)
539 {
540  return deviceState;
541 }
542 
547 void USBD_Test(uint8_t bIndex)
548 {
549  USBD_HAL_Test(bIndex);
550 }
551 
552 
554 {
555  ForceFS = 1;
556 }
557 
static uint8_t deviceState
Definition: USBD.c:61
#define USBD_STATUS_INVALID_PARAMETER
Definition: USBD.h:112
#define USBD_STATE_POWERED
Definition: USBD.h:137
Buffer struct used for multi-buffer-listed transfer.
Definition: USBD.h:155
uint8_t USBD_HAL_Read(uint8_t bEndpoint, void *pData, uint32_t dLength)
Definition: USBD_HAL.c:1618
uint8_t USBD_HAL_Write(uint8_t bEndpoint, const void *pData, uint32_t dLength)
Definition: USBD_HAL.c:1429
union Endpoint::@32 transfer
#define USBD_STATE_SUSPENDED
Definition: USBD.h:133
void USBD_HAL_Init(void)
Definition: USBD_HAL.c:1734
#define USBD_STATUS_RESET
Definition: USBD.h:108
void USBD_HAL_Disconnect(void)
Disable Pull-up, disconnect.
Definition: USBD_HAL.c:1684
uint8_t USBD_HAL_IsHighSpeed(void)
Definition: USBD_HAL.c:1898
void USBD_ForceFullSpeed(void)
Definition: USBD.c:553
void USBD_RemoteWakeUp(void)
Definition: USBD.c:475
WEAK void USBDCallbacks_RequestReceived(const USBGenericRequest *request)
Definition: USBDCallbacks.c:83
void USBD_HAL_Test(uint8_t bIndex)
Definition: USBD_HAL.c:1943
uint8_t USBD_GetState(void)
Definition: USBD.c:538
void USBD_Unhalt(uint8_t bEndpoint)
Definition: USBD.c:397
uint8_t USBD_IsHalted(uint8_t bEndpoint)
Definition: USBD.c:407
#define TRACE_INFO(...)
Definition: USBLib_Trace.h:166
void USBD_SetAddress(uint8_t address)
Definition: USBD.c:437
USBDTransferBuffer * pMbl
Definition: USBD_HAL.c:159
uint8_t USBD_HAL_ConfigureEP(const USBEndpointDescriptor *pDescriptor)
Definition: USBD_HAL.c:1188
uint16_t listSize
Definition: USBD_HAL.c:157
void USBD_HAL_Connect(void)
Enable Pull-up, connect.
Definition: USBD_HAL.c:1635
WEAK void USBDCallbacks_Resumed(void)
Definition: USBDCallbacks.c:76
#define NULL
Definition: nm_bsp.h:52
void USBD_ResetHandler()
Definition: USBD.c:126
uint8_t USBD_Stall(uint8_t bEndpoint)
Definition: USBD.c:427
volatile uint8_t state
Definition: USBD_HAL.c:176
void USBD_SetConfiguration(uint8_t cfgnum)
Definition: USBD.c:453
void USBD_Connect(void)
Definition: USBD.c:490
#define USBD_STATE_DEFAULT
Definition: USBD.h:139
static Endpoint endpoints[CHIP_USB_NUMENDPOINTS]
Definition: USBD_HAL.c:207
uint8_t USBD_HAL_SetTransferCallback(uint8_t bEP, TransferCallback fCallback, void *pCbData)
Definition: USBD_HAL.c:1340
void USBD_HAL_RemoteWakeUp(void)
Definition: USBD_HAL.c:1694
uint8_t * pBuffer
Definition: USBD.h:157
uint8_t USBD_Read(uint8_t bEndpoint, void *pData, uint32_t dLength, TransferCallback fCallback, void *pArgument)
Definition: USBD.c:318
void USBD_RequestHandler(uint8_t bEndpoint, const USBGenericRequest *pRequest)
Definition: USBD.c:147
#define USBD_STATUS_LOCKED
Definition: USBD.h:102
uint8_t ForceFS
Definition: USBD.c:65
void USBD_ConfigureEndpoint(const USBEndpointDescriptor *pDescriptor)
Definition: USBD.c:165
WEAK void USBDCallbacks_Suspended(void)
Definition: USBDCallbacks.c:70
void USBD_ResumeHandler(void)
Definition: USBD.c:105
uint8_t USBD_Write(uint8_t bEndpoint, const void *pData, uint32_t dLength, TransferCallback fCallback, void *pArgument)
Definition: USBD.c:191
void USBD_HAL_ResetEPs(uint32_t bmEPs, uint8_t bStatus, uint8_t bKeepCfg)
Reset endpoints and disable them.Terminate transfer if there is any, with given status;Reset the endp...
Definition: USBD_HAL.c:1119
uint8_t USBD_IsHighSpeed(void)
Definition: USBD.c:416
WEAK void USBDCallbacks_Reset(void)
Definition: USBDCallbacks.c:62
void USBD_Disconnect(void)
Definition: USBD.c:499
uint16_t buffered
Definition: USBD.h:163
void USBD_SuspendHandler(void)
Definition: USBD.c:83
void USBD_HAL_Suspend(void)
Definition: USBD_HAL.c:1910
void * pArgument
Definition: USBD_HAL.c:151
void(* TransferCallback)(void *pArg, uint8_t status, uint32_t transferred, uint32_t remaining)
Definition: USBD.h:200
#define USBD_STATE_CONFIGURED
Definition: USBD.h:143
void USBD_Halt(uint8_t bEndpoint)
Definition: USBD.c:388
uint8_t USBD_HAL_Halt(uint8_t bEndpoint, uint8_t ctl)
Definition: USBD_HAL.c:1809
void USBD_Test(uint8_t bIndex)
Definition: USBD.c:547
#define USBD_STATE_ADDRESS
Definition: USBD.h:141
void USBD_HAL_SetAddress(uint8_t address)
Definition: USBD_HAL.c:1711
#define TRACE_WARNING(...)
Definition: USBLib_Trace.h:174
uint16_t remaining
Definition: USBD.h:165
uint8_t USBD_HAL_Stall(uint8_t bEP)
Definition: USBD_HAL.c:1779
#define USBD_STATUS_WRONG_STATE
Definition: USBD.h:114
uint16_t transferred
Definition: USBD.h:161
void USBD_HAL_Activate(void)
Definition: USBD_HAL.c:1922
void(* MblTransferCallback)(void *pArg, uint8_t status)
Definition: USBD.h:211
void USBD_Init(void)
Definition: USBD.c:518
#define CHIP_USB_ENDPOINTS_BANKS(ep)
uint16_t size
Definition: USBD.h:159
static uint8_t previousDeviceState
Definition: USBD.c:63
#define USBD_STATUS_SUCCESS
Definition: USBD.h:100
MblTransferCallback fCallback
Definition: USBD_HAL.c:149
#define TRACE_DEBUG_WP(...)
Definition: USBLib_Trace.h:162
#define TRACE_INFO_WP(...)
Definition: USBLib_Trace.h:167
WEAK void USBDCallbacks_Initialized(void)
Definition: USBDCallbacks.c:54
void USBD_HAL_SetConfiguration(uint8_t cfgnum)
Definition: USBD_HAL.c:1725


inertial_sense_ros
Author(s):
autogenerated on Sat Sep 19 2020 03:19:05