PDL for FM4  Version1.2
Peripheral Driverl Library for FM4
UsbDeviceCmsisDap.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 /******************************************************************************/
50 #define __USBDEVICECMSISDAP_C__
51 
52 /******************************************************************************/
53 /* Include files */
54 /******************************************************************************/
55 
56 #include "UsbDeviceCmsisDap.h"
57 
58 #if ((FM_PERIPHERAL_USB_DEVICE_ENABLED == ON) && (USBDEVICECMSISDAP_ENABLED == ON))
59 
60 #include "DAP_config.h"
61 #include "DAP.h"
62 
63 /******************************************************************************/
64 /* Local type definitions ('typedef') */
65 /******************************************************************************/
66 
67 /******************************************************************************/
68 /* Local function prototypes ('static') */
69 /******************************************************************************/
70 
71 static void RxTxCallback(stc_usbn_t* pstcUsb, struct stc_usbn_endpoint_data* pstcEndpoint);
72 static void ConfCallback(stc_usbn_t* pstcUsb);
73 extern uint32_t DAP_ProcessVendorCommand(uint8_t *request, uint8_t *response);
74 
75 /******************************************************************************/
76 /* Local variable definitions (declared in header file with 'extern') */
77 /******************************************************************************/
78 
79 #if (64 != DAP_PACKET_SIZE)
80 #error "USB HID Output Report Size must match DAP Packet Size"
81 #endif
82 #if (64 != DAP_PACKET_SIZE)
83 #error "USB HID Input Report Size must match DAP Packet Size"
84 #endif
85 
86 static volatile uint8_t USB_RequestFlag; // Request Buffer Usage Flag
87 static volatile uint32_t USB_RequestIn; // Request Buffer In Index
88 static volatile uint32_t USB_RequestOut; // Request Buffer Out Index
89 
90 static volatile uint8_t USB_ResponseIdle; // Response Buffer Idle Flag
91 static volatile uint8_t USB_ResponseFlag; // Response Buffer Usage Flag
92 static volatile uint32_t USB_ResponseIn; // Response Buffer In Index
93 static volatile uint32_t USB_ResponseOut; // Response Buffer Out Index
94 
95 
96 //static const uint8_t u8MaxSupportedEndpoints = 2;
99 
100 static uint8_t USB_Request [DAP_PACKET_COUNT][DAP_PACKET_SIZE]; // Request Buffer
101 static uint8_t USB_Response[DAP_PACKET_COUNT][DAP_PACKET_SIZE]; // Response Buffer
102 uint8_t pu8BufferOUT[64];
103 uint8_t pu8BufferIN[64];
104 
107 
111 static stc_usbn_t* pstcUsbHandle = NULL;
112 
113 /******************************************************************************/
114 /* Global variable definitions (declared in header file with 'extern') */
115 /******************************************************************************/
116 
117 /******************************************************************************/
118 /* Procedures / Functions */
119 /******************************************************************************/
120 
131 static void ConfCallback(stc_usbn_t* pstcUsb)
132 {
133  if (UsbDevice_GetStatus(pstcUsb) == UsbConfigured)
134  {
135  DAP_Setup(); // DAP Setup
137  bDataSent = TRUE;
138  USB_RequestFlag = 0;
139  USB_RequestIn = 0;
140  USB_RequestOut = 0;
141  USB_ResponseIdle = 1;
142  USB_ResponseFlag = 0;
143  USB_ResponseIn = 0;
144  USB_ResponseOut = 0;
145  bReady = TRUE;
146  LED_CONNECTED_OUT(1); // Turn on Debugger Connected LED
147  Delayms(50); // Wait for 500ms
148  LED_RUNNING_OUT(1); // Turn on Target Running LED
149  Delayms(50); // Wait for 500ms
150  LED_CONNECTED_OUT(0); // Turn off Debugger Connected LED
151  Delayms(50); // Wait for 500ms
152  LED_RUNNING_OUT(0); // Turn off Target Running LED
153  } else
154  {
155  bReady = FALSE;
156  }
157 }
158 
170 static void RxTxCallback(stc_usbn_t* pstcUsb, struct stc_usbn_endpoint_data* pstcEndpoint)
171 {
172  volatile uint8_t i;
173  volatile uint8_t u8EndpointIndex = 0xFF;
174 
175  if ((pstcEndpoint->u8EndpointAddress) & 0x80)
176  {
177  bDataSent = TRUE;
178  }
179  else
180  {
181 
182  if (pu8BufferOUT[0] == ID_DAP_TransferAbort) {
183  DAP_TransferAbort = 1;
184  return;
185  }
187  return; // Discard packet when buffer is full
188  }
189  // Store data into request packet buffer
190  memcpy(USB_Request[USB_RequestIn], pstcEndpoint->pstcEndpointBuffer->pu8Buffer, 64);
191  UsbDevice_ReceiveData(pstcUsb, pstcEndpoint, pstcEndpoint->pstcEndpointBuffer->pu8Buffer, 64, UsbIRQ);
192  USB_RequestIn++;
193  if (USB_RequestIn == DAP_PACKET_COUNT) {
194  USB_RequestIn = 0;
195  }
196  if (USB_RequestIn == USB_RequestOut)
197  {
198  USB_RequestFlag = 1;
199  }
201  }
202 }
203 
213 void UsbDeviceCmsisDap_Init(stc_usbn_t* pstcUsb)
214 {
215  stc_usbdevice_class_config_t stcUsbClassConfig;
216  stc_usbdevice_endpoint_config_t stcEndpointConfig;
217  uint8_t* pu8Interface = NULL;
218  uint8_t u8InterfaceLength = 0;
219  uint8_t u8i = 0;
220  //uint8_t u8NumEndpoints = 0;
221  pstcUsbHandle = pstcUsb;
222 
223  pstcEndpointIN = NULL;
224  pstcEndpointOUT = NULL;
225 
226  USB_RequestFlag = 0;
227  USB_RequestIn = 0;
228  USB_RequestOut = 0;
229  USB_ResponseIdle = 1;
230  USB_ResponseFlag = 0;
231  USB_ResponseIn = 0;
232  USB_ResponseOut = 0;
233 
234  stcUsbClassConfig.u8InterfaceNumber = 0xFF; // do not use fix interface number, choose by class, subclass, protocol
235  stcUsbClassConfig.u8InterfaceClass = 0x03; // HID Class
236  stcUsbClassConfig.u8InterfaceSubClass = 0x00; // Custom Sub Class
237  stcUsbClassConfig.u8InterfaceProtocoll = 0x00; // Custom Protocol
238  stcUsbClassConfig.pfnSetupRequestCallback = NULL; // No setup requests handled
239  stcUsbClassConfig.pfnConnectionCallback = NULL; // No connection callback handled
240  stcUsbClassConfig.pfnDisconnectionCallback = NULL; // No disconnection callback handled
241  stcUsbClassConfig.pfnConfCallback = ConfCallback; // Callback for configuration set
242  UsbDevice_RegisterVendorClass(pstcUsb,&stcUsbClassConfig,&pu8Interface,&u8InterfaceLength);
243 
244  for(u8i = 0;u8i < u8InterfaceLength;)
245  {
246  u8i += pu8Interface[u8i];
247  if (USBDESCR_ENDPOINT == pu8Interface[u8i + 1])
248  {
249  stcEndpointConfig.u8EndpointAddress = pu8Interface[u8i + 2];
250  stcEndpointConfig.pfnRxTxCallback = RxTxCallback;
251 
252  if (((stcEndpointConfig.u8EndpointAddress) & 0x80) == 0)
253  {
254  stcEndpointConfig.pstcEndpointBuffer = &stcEndpointBufferOUT;
255  pstcEndpointOUT = UsbDevice_SetupEndpoint(pstcUsb, &stcEndpointConfig);
256  UsbDevice_ReceiveData(pstcUsb, pstcEndpointOUT, pu8BufferOUT, 64, UsbIRQ);
257  }
258  else
259  {
260  stcEndpointConfig.pstcEndpointBuffer = &stcEndpointBufferIN;
261  pstcEndpointIN = UsbDevice_SetupEndpoint(pstcUsb, &stcEndpointConfig);
262  }
263  if ((pstcEndpointIN != NULL) && (pstcEndpointOUT != NULL))
264  {
265  return;
266  }
267  }
268  }
269 
270 }
271 
284 {
285  if ((bReady == FALSE) || (bDataSent == FALSE))
286  {
287  return FALSE;
288  }
289  bDataSent = FALSE;
290  UsbDevice_SendData(pstcUsbHandle, pstcEndpointIN, pu8Buffer, 64, enMode);
292 
293  USB_ResponseOut++;
294  if (USB_ResponseOut == DAP_PACKET_COUNT) {
295  USB_ResponseOut = 0;
296  }
298  USB_ResponseFlag = 0;
299  }
300  return TRUE;
301  } else {
302  USB_ResponseIdle = 1;
303  }
304 
305  if (enMode == UsbPOLL)
306  {
307  bDataSent = TRUE;
308  }
309  return TRUE;
310 }
311 
313 {
314  uint32_t n;
315 
316  // Process pending requests
318 
319  // Process DAP Command and prepare response
320  DAP_ProcessCommand(USB_Request[USB_RequestOut], USB_Response[USB_ResponseIn]);
321  DAP_ProcessVendorCommand(USB_Request[USB_RequestOut], USB_Response[USB_ResponseIn]);
322  // Update request index and flag
323  n = USB_RequestOut + 1;
324  if (n == DAP_PACKET_COUNT) {
325  n = 0;
326  }
327  USB_RequestOut = n;
328  if (USB_RequestOut == USB_RequestIn) {
329  USB_RequestFlag = 0;
330  }
331 
332  if (USB_ResponseIdle) {
333  // Request that data is send back to host
334  USB_ResponseIdle = 0;
335  #if USB_USES_DMA == ON
337  #else
338  UsbDeviceCmsisDap_Send(USB_Response[USB_ResponseOut],UsbIRQ);
339  #endif
340  } else {
341  // Update response index and flag
342  n = USB_ResponseIn + 1;
343  if (n == DAP_PACKET_COUNT) {
344  n = 0;
345  }
346  USB_ResponseIn = n;
347  if (USB_ResponseIn == USB_ResponseOut) {
348  USB_ResponseFlag = 1;
349  }
350  }
351  }
352 }
353 
362 {
363  return !bDataSent;
364 }
365 
374 {
375  return bDataSent;
376 }
377 
387 uint32_t UsbDeviceCmsisDap_GetReceivedData(uint8_t* pu8OutBuffer)
388 {
389  if (bDataReceived == TRUE)
390  {
392  memcpy(pu8OutBuffer,pstcEndpointOUT->pstcEndpointBuffer->pu8Buffer,64);
393  UsbDevice_ReceiveData(pstcUsbHandle, pstcEndpointOUT, pstcEndpointOUT->pstcEndpointBuffer->pu8Buffer, pstcEndpointOUT->pstcEndpointBuffer->u32BufferSize, UsbIRQ);
394  return 64;
395  }
396  return 0;
397 }
398 
399 #endif