aboutsummaryrefslogtreecommitdiffstats
path: root/core/usb/usb.c
blob: f20f144276964ba1d96b6071044e72443ad94ca9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
/* *****************************************************************************
 * The MIT License
 *
 * Copyright (c) 2010 LeafLabs LLC.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 * ****************************************************************************/

/**
 *  @file usb.c
 *
 *  @brief usb-specific hardware setup, NVIC, clocks, and usb activities
 *  in the pre-attached state. includes some of the lower level callbacks 
 *  needed by the usb library, like suspend,resume,init,etc
 */

#include "usb.h"
#include "libmaple.h"
#include "usb_lib.h"
#include "gpio.h"
#include "usb_hardware.h"

#include "usb_config.h"
#include "usb_callbacks.h"
#include "usb_lib.h"

/* persistent usb structs */

volatile uint32 bDeviceState = UNCONNECTED;
volatile uint16_t wIstr = 0;
volatile bIntPackSOF  = 0;

DEVICE Device_Table = 
  {
    NUM_ENDPTS,
    1
  };

DEVICE_PROP Device_Property = 
  {
    usbInit,
    usbReset,
    usbStatusIn,
    usbStatusOut,
    usbDataSetup,
    usbNoDataSetup,
    usbGetInterfaceSetting,
    usbGetDeviceDescriptor,
    usbGetConfigDescriptor,
    usbGetStringDescriptor,
    NOP_Process,
    0,
    bMaxPacketSize
  };

USER_STANDARD_REQUESTS User_Standard_Requests = 
  {
    NOP_Process,
    usbSetConfiguration,
    NOP_Process,
    NOP_Process,
    NOP_Process,
    NOP_Process,
    NOP_Process,
    NOP_Process,
    usbSetDeviceAddress
  };

void (*pEpInt_IN[7])(void) =
{ 
  vcomDataTxCb,
  vcomManagementCb,
  NOP_Process,
  NOP_Process,
  NOP_Process,
  NOP_Process,
  NOP_Process,
};

void (*pEpInt_OUT[7])(void) =
{
  NOP_Process,
  NOP_Process,
  vcomDataRxCb,
  NOP_Process,
  NOP_Process,
  NOP_Process,
  NOP_Process,
};

struct
{
  volatile RESUME_STATE eState;
  volatile uint8 bESOFcnt;
} ResumeS;

void setupUSB (void) {
  gpio_set_mode(USB_DISC_BANK,
		USB_DISC_PIN,
		GPIO_MODE_OUTPUT_PP);

  /* setup the apb1 clock for USB */
  pRCC->APB1ENR |= 0x00800000;

  /* initialize the usb application */
  gpio_write_bit(USB_DISC_BANK,USB_DISC_PIN,0);  /* present ourselves to the host */
  USB_Init();  /* low level init routine provided by st lib */

}

void usbSuspend(void) {
  u16 wCNTR;
  wCNTR = _GetCNTR();
  wCNTR |= CNTR_FSUSP | CNTR_LPMODE;
  _SetCNTR(wCNTR);

  /* run any power reduction handlers */
  bDeviceState = SUSPENDED;
}

void usbResumeInit(void) {
  u16 wCNTR;

  /* restart any clocks that had been stopped */

  wCNTR = _GetCNTR();
  wCNTR &= (~CNTR_LPMODE);
  _SetCNTR(wCNTR);

  /* undo power reduction handlers here */
  _SetCNTR(ISR_MSK);

}

void usbResume(RESUME_STATE eResumeSetVal) {
  u16 wCNTR;

  if (eResumeSetVal != RESUME_ESOF)
    ResumeS.eState = eResumeSetVal;

  switch (ResumeS.eState)
    {
    case RESUME_EXTERNAL:
      usbResumeInit();
      ResumeS.eState = RESUME_OFF;
      break;
    case RESUME_INTERNAL:
      usbResumeInit();
      ResumeS.eState = RESUME_START;
      break;
    case RESUME_LATER:
      ResumeS.bESOFcnt = 2;
      ResumeS.eState = RESUME_WAIT;
      break;
    case RESUME_WAIT:
      ResumeS.bESOFcnt--;
      if (ResumeS.bESOFcnt == 0)
        ResumeS.eState = RESUME_START;
      break;
    case RESUME_START:
      wCNTR = _GetCNTR();
      wCNTR |= CNTR_RESUME;
      _SetCNTR(wCNTR);
      ResumeS.eState = RESUME_ON;
      ResumeS.bESOFcnt = 10;
      break;
    case RESUME_ON:
      ResumeS.bESOFcnt--;
      if (ResumeS.bESOFcnt == 0)
	{
	  wCNTR = _GetCNTR();
	  wCNTR &= (~CNTR_RESUME);
	  _SetCNTR(wCNTR);
	  ResumeS.eState = RESUME_OFF;
	}
      break;
    case RESUME_OFF:
    case RESUME_ESOF:
    default:
      ResumeS.eState = RESUME_OFF;
      break;
    }
}

RESULT usbPowerOn(void) {
  u16 wRegVal;

  wRegVal = CNTR_FRES;
  _SetCNTR(wRegVal);

  wInterrupt_Mask = 0;
  _SetCNTR(wInterrupt_Mask);
  _SetISTR(0);
  wInterrupt_Mask = CNTR_RESETM | CNTR_SUSPM | CNTR_WKUPM; /* the bare minimum */
  _SetCNTR(wInterrupt_Mask);

  return USB_SUCCESS;
}

RESULT usbPowerOff(void) {
  _SetCNTR(CNTR_FRES);
  _SetISTR(0);
  _SetCNTR(CNTR_FRES + CNTR_PDWN);

  /* note that all weve done here is powerdown the 
     usb peripheral. we have no disabled the clocks,
     pulled the usb_disc pin back up, or reset the
     application state machines */

  return USB_SUCCESS;
}

void usbEnbISR(void) {
  NVIC_InitTypeDef NVIC_InitStructure;


  NVIC_InitStructure.NVIC_IRQChannel = USB_LP_IRQ;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelCmd = TRUE;
  nvicInit(&NVIC_InitStructure);
}

void usbDsbISR(void) {
  NVIC_InitTypeDef NVIC_InitStructure;
  NVIC_InitStructure.NVIC_IRQChannel = USB_LP_IRQ;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelCmd = FALSE;
  nvicInit(&NVIC_InitStructure);
}

/* overloaded ISR routine, this is the main usb ISR */
void usb_lpIRQHandler(void) {
wIstr = _GetISTR();

/* go nuts with the preproc switches since this is an ISTR and must be FAST */
#if (ISR_MSK & ISTR_RESET)
if (wIstr & ISTR_RESET & wInterrupt_Mask)
  {
    _SetISTR((u16)CLR_RESET);
    Device_Property.Reset();
  }
#endif


#if (ISR_MSK & ISTR_DOVR)
if (wIstr & ISTR_DOVR & wInterrupt_Mask)
  {
    _SetISTR((u16)CLR_DOVR);
  }
#endif


#if (ISR_MSK & ISTR_ERR)
if (wIstr & ISTR_ERR & wInterrupt_Mask)
  {
    _SetISTR((u16)CLR_ERR);
  }
#endif


#if (ISR_MSK & ISTR_WKUP)
if (wIstr & ISTR_WKUP & wInterrupt_Mask)
  {
    _SetISTR((u16)CLR_WKUP);
    usbResume(RESUME_EXTERNAL);
  }
#endif

/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
#if (ISR_MSK & ISTR_SUSP)
if (wIstr & ISTR_SUSP & wInterrupt_Mask)
  {

    /* check if SUSPEND is possible */
    if (F_SUSPEND_ENABLED)
      {
	usbSuspend();
      }
    else
      {
	/* if not possible then resume after xx ms */
	usbResume(RESUME_LATER);
      }
    /* clear of the ISTR bit must be done after setting of CNTR_FSUSP */
    _SetISTR((u16)CLR_SUSP);
  }
#endif


#if (ISR_MSK & ISTR_SOF)
if (wIstr & ISTR_SOF & wInterrupt_Mask)
  {
    _SetISTR((u16)CLR_SOF);
    bIntPackSOF++;
  }
#endif


#if (ISR_MSK & ISTR_ESOF)
if (wIstr & ISTR_ESOF & wInterrupt_Mask)
  {
    _SetISTR((u16)CLR_ESOF);
    /* resume handling timing is made with ESOFs */
    usbResume(RESUME_ESOF); /* request without change of the machine state */
  }
#endif

/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
#if (ISR_MSK & ISTR_CTR)
if (wIstr & ISTR_CTR & wInterrupt_Mask)
  {
    /* servicing of the endpoint correct transfer interrupt */
    /* clear of the CTR flag into the sub */
    CTR_LP(); /* low priority ISR defined in the usb core lib */
  }
#endif
}

/* copies data out of sendBuf into the packet memory for 
   usb, but waits until any previous usb transmissions have 
   completed before doing this. It returns without waiting
   for its data to be sent. most efficient when 64 bytes are copied
   at a time. users responsible for not overflowing sendbuf 
   with len! if > 64 bytes are being sent, then the function
   will block at every 64 byte packet
*/
int16 usbSendBytes(uint8* sendBuf, uint16 len) {
/*   while (countTx != 0) { */
/*     if (reset_state == NDTR_NRTS) { */
/*       return 0; */
/*     } */
/*   }/\* wait for pipe to be clear *\/ */

  if (reset_state == NDTR_NRTS) {
    return -1; /* indicates to caller to stop trying, were not connected */
  }

  /* ideally we should wait here, but it gets stuck
     for some reason. countTx wont decrement when 
     theres no host side port reading the data, this is
     known, but even if we add the check for NDTR_NRTS it
     still gets stuck...*/
  if (countTx != 0) {
    return 0; /* indicated to caller to keep trying, were just busy */
  }

  uint16 sent = len; 

  while (len > VCOM_TX_EPSIZE) {
    countTx = VCOM_TX_EPSIZE;

    UserToPMABufferCopy(sendBuf,VCOM_TX_ADDR,VCOM_TX_EPSIZE);
    _SetEPTxCount(VCOM_TX_ENDP,VCOM_TX_EPSIZE);
    _SetEPTxValid(VCOM_TX_ENDP);

    while(countTx != 0);
    len     -= VCOM_TX_EPSIZE;
    sendBuf += VCOM_TX_EPSIZE;
  }

  if (len != 0) {
    countTx = len;
    UserToPMABufferCopy(sendBuf,VCOM_TX_ADDR,len);
    _SetEPTxCount(VCOM_TX_ENDP,len);
    _SetEPTxValid(VCOM_TX_ENDP);
  }
  
  return sent;
}

/* returns the number of available bytes are in the recv FIFO */
uint8 usbBytesAvailable(void) {
  return VCOM_RX_EPSIZE - maxNewBytes;
}

/* copies len bytes from the local recieve FIFO (not 
   usb packet buffer) into recvBuf and deq's the fifo. 
   will only copy the minimum of len or the available 
   bytes. returns the number of bytes copied */
uint8 usbReceiveBytes(uint8* recvBuf, uint8 len) {
  if (len > VCOM_RX_EPSIZE - maxNewBytes) {
    len = VCOM_RX_EPSIZE - maxNewBytes;
  }
  
  int i;
  for (i=0;i<len;i++) {
    recvBuf[i] = (uint8)(vcomBufferRx[(recvBufOut++)%VCOM_RX_EPSIZE]);
  }

  maxNewBytes += len;
  return len;
}

void usbSendHello(void) {
  char* myStr = "hello!";

  uint8 thisVal = 48 + usbBytesAvailable();
  char *line = "\n";
  while(usbSendBytes(&thisVal,1) == 0);
  while(usbSendBytes((uint8*)line,1) == 0);

  uint8 recv[64];
  usbReceiveBytes(&recv[0],1);
}