1
|
/* ----------------------------------------------------------------------------
|
2
|
* ATMEL Microcontroller Software Support
|
3
|
* ----------------------------------------------------------------------------
|
4
|
* Copyright (c) 2009, Atmel Corporation
|
5
|
*
|
6
|
* All rights reserved.
|
7
|
*
|
8
|
* Redistribution and use in source and binary forms, with or without
|
9
|
* modification, are permitted provided that the following conditions are met:
|
10
|
*
|
11
|
* - Redistributions of source code must retain the above copyright notice,
|
12
|
* this list of conditions and the disclaimer below.
|
13
|
*
|
14
|
* Atmel's name may not be used to endorse or promote products derived from
|
15
|
* this software without specific prior written permission.
|
16
|
*
|
17
|
* DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR
|
18
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
19
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
|
20
|
* DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT,
|
21
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
22
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
|
23
|
* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
24
|
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
25
|
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
|
26
|
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
27
|
* ----------------------------------------------------------------------------
|
28
|
*/
|
29
|
/**
|
30
|
* \file
|
31
|
*
|
32
|
* This file contains all the specific code for the
|
33
|
* usb_fast_source example.
|
34
|
*/
|
35
|
|
36
|
/*----------------------------------------------------------------------------
|
37
|
* Headers
|
38
|
*----------------------------------------------------------------------------*/
|
39
|
|
40
|
#include <board.h>
|
41
|
|
42
|
#include <stdio.h>
|
43
|
#include <stdint.h>
|
44
|
#include <stdbool.h>
|
45
|
#include <stdbool.h>
|
46
|
|
47
|
#include <irq/irq.h>
|
48
|
#include <pio/pio.h>
|
49
|
#include <pio/pio_it.h>
|
50
|
#include <utility/trace.h>
|
51
|
#include <utility/led.h>
|
52
|
|
53
|
#include <usb/device/core/USBD.h>
|
54
|
|
55
|
#include <usb/device/dfu/dfu.h>
|
56
|
|
57
|
#include <fast_source.h>
|
58
|
|
59
|
/*----------------------------------------------------------------------------
|
60
|
* Definitions
|
61
|
*----------------------------------------------------------------------------*/
|
62
|
|
63
|
#if 0
|
64
|
/** Number of available audio buffers. */
|
65
|
#define BUFFER_NUMBER 8
|
66
|
/** Size of one buffer in bytes. */
|
67
|
#define BUFFER_SIZE (AUDDLoopRecDriver_BYTESPERFRAME*2)
|
68
|
#endif
|
69
|
|
70
|
/// Use for power management
|
71
|
#define STATE_IDLE 0
|
72
|
/// The USB device is in suspend state
|
73
|
#define STATE_SUSPEND 4
|
74
|
/// The USB device is in resume state
|
75
|
#define STATE_RESUME 5
|
76
|
|
77
|
//------------------------------------------------------------------------------
|
78
|
// Internal variables
|
79
|
//------------------------------------------------------------------------------
|
80
|
/// State of USB, for suspend and resume
|
81
|
unsigned char USBState = STATE_IDLE;
|
82
|
|
83
|
#if 0
|
84
|
/** Data buffers for receiving audio frames from the USB host. */
|
85
|
static uint8_t buffers[BUFFER_NUMBER][BUFFER_SIZE];
|
86
|
/** Number of samples stored in each data buffer. */
|
87
|
static uint32_t bufferSizes[BUFFER_NUMBER];
|
88
|
/** Next buffer in which USB data can be stored. */
|
89
|
static uint32_t inBufferIndex = 0;
|
90
|
/** Number of buffers that can be sent to the DAC. */
|
91
|
static volatile uint32_t numBuffersToSend = 0;
|
92
|
#endif
|
93
|
|
94
|
/** Current state of the playback stream interface. */
|
95
|
static volatile uint8_t isPlyActive = 0;
|
96
|
/** Current state of the record stream interface. */
|
97
|
static volatile uint8_t isRecActive = 0;
|
98
|
|
99
|
/*----------------------------------------------------------------------------
|
100
|
* VBus monitoring (optional)
|
101
|
*----------------------------------------------------------------------------*/
|
102
|
|
103
|
/** VBus pin instance. */
|
104
|
static const Pin pinVbus = PIN_USB_VBUS;
|
105
|
static const Pin pinPB = PIN_PUSHBUTTON_1;
|
106
|
|
107
|
/**
|
108
|
* Handles interrupts coming from PIO controllers.
|
109
|
*/
|
110
|
static void ISR_Vbus(const Pin *pPin)
|
111
|
{
|
112
|
/* Check current level on VBus */
|
113
|
if (PIO_Get(&pinVbus))
|
114
|
{
|
115
|
TRACE_INFO("VBUS conn\n\r");
|
116
|
USBD_Connect();
|
117
|
}
|
118
|
else
|
119
|
{
|
120
|
TRACE_INFO("VBUS discon\n\r");
|
121
|
USBD_Disconnect();
|
122
|
}
|
123
|
}
|
124
|
|
125
|
/**
|
126
|
* Configures the VBus pin to trigger an interrupt when the level on that pin
|
127
|
* changes.
|
128
|
*/
|
129
|
static void VBus_Configure( void )
|
130
|
{
|
131
|
/* Configure PIO */
|
132
|
PIO_Configure(&pinVbus, 1);
|
133
|
PIO_ConfigureIt(&pinVbus, ISR_Vbus);
|
134
|
PIO_EnableIt(&pinVbus);
|
135
|
|
136
|
/* Check current level on VBus */
|
137
|
if (PIO_Get(&pinVbus))
|
138
|
{
|
139
|
/* if VBUS present, force the connect */
|
140
|
USBD_Connect();
|
141
|
}
|
142
|
else
|
143
|
{
|
144
|
TRACE_INFO("discon\n\r");
|
145
|
USBD_Disconnect();
|
146
|
}
|
147
|
}
|
148
|
|
149
|
/*----------------------------------------------------------------------------
|
150
|
* USB Power Control
|
151
|
*----------------------------------------------------------------------------*/
|
152
|
|
153
|
#ifdef PIN_USB_POWER_ENA
|
154
|
/** Power Enable A (MicroAB Socket) pin instance. */
|
155
|
static const Pin pinPOnA = PIN_USB_POWER_ENA;
|
156
|
#endif
|
157
|
#ifdef PIN_USB_POWER_ENB
|
158
|
/** Power Enable B (A Socket) pin instance. */
|
159
|
static const Pin pinPOnB = PIN_USB_POWER_ENB;
|
160
|
#endif
|
161
|
#ifdef PIN_USB_POWER_ENC
|
162
|
/** Power Enable C (A Socket) pin instance. */
|
163
|
static const Pin pinPOnC = PIN_USB_POWER_ENC;
|
164
|
#endif
|
165
|
/**
|
166
|
* Configures the Power Enable pin to disable self power.
|
167
|
*/
|
168
|
static void USBPower_Configure( void )
|
169
|
{
|
170
|
#ifdef PIN_USB_POWER_ENA
|
171
|
PIO_Configure(&pinPOnA, 1);
|
172
|
#endif
|
173
|
#ifdef PIN_USB_POWER_ENB
|
174
|
PIO_Configure(&pinPOnB, 1);
|
175
|
#endif
|
176
|
#ifdef PIN_USB_POWER_ENC
|
177
|
PIO_Configure(&pinPOnC, 1);
|
178
|
#endif
|
179
|
}
|
180
|
|
181
|
/*----------------------------------------------------------------------------
|
182
|
* Internal functions
|
183
|
*----------------------------------------------------------------------------*/
|
184
|
|
185
|
#if 0
|
186
|
/**
|
187
|
* Invoked when a frame has been received.
|
188
|
*/
|
189
|
static void FrameReceived(uint32_t unused,
|
190
|
uint8_t status,
|
191
|
uint32_t transferred,
|
192
|
uint32_t remaining)
|
193
|
{
|
194
|
if (status == USBD_STATUS_SUCCESS)
|
195
|
{
|
196
|
/* Loopback! add this buffer to write list */
|
197
|
if (!isRecActive) {}
|
198
|
else
|
199
|
{
|
200
|
AUDDLoopRecDriver_Write(buffers[inBufferIndex],
|
201
|
AUDDLoopRecDriver_BYTESPERFRAME,
|
202
|
NULL, 0);
|
203
|
}
|
204
|
|
205
|
/* Update input status data */
|
206
|
bufferSizes[inBufferIndex] = transferred
|
207
|
/ AUDDLoopRecDriver_BYTESPERSAMPLE;
|
208
|
inBufferIndex = (inBufferIndex + 1) % BUFFER_NUMBER;
|
209
|
numBuffersToSend++;
|
210
|
|
211
|
}
|
212
|
else if (status == USBD_STATUS_ABORTED)
|
213
|
{
|
214
|
/* Error , ABORT, add NULL buffer */
|
215
|
bufferSizes[inBufferIndex] = 0;
|
216
|
inBufferIndex = (inBufferIndex + 1) % BUFFER_NUMBER;
|
217
|
numBuffersToSend++;
|
218
|
}
|
219
|
else
|
220
|
{
|
221
|
/* Packet is discarded */
|
222
|
}
|
223
|
|
224
|
/* Receive next packet */
|
225
|
AUDDLoopRecDriver_Read(buffers[inBufferIndex],
|
226
|
AUDDLoopRecDriver_BYTESPERFRAME,
|
227
|
(TransferCallback) FrameReceived,
|
228
|
0); // No optional argument
|
229
|
}
|
230
|
#endif
|
231
|
|
232
|
/*----------------------------------------------------------------------------
|
233
|
* Callbacks re-implementation
|
234
|
*----------------------------------------------------------------------------*/
|
235
|
|
236
|
//------------------------------------------------------------------------------
|
237
|
/// Invoked when the USB device leaves the Suspended state. By default,
|
238
|
/// configures the LEDs.
|
239
|
//------------------------------------------------------------------------------
|
240
|
void USBDCallbacks_Resumed(void)
|
241
|
{
|
242
|
// Initialize LEDs
|
243
|
LED_Configure(USBD_LEDPOWER);
|
244
|
LED_Set(USBD_LEDPOWER);
|
245
|
LED_Configure(USBD_LEDUSB);
|
246
|
LED_Clear(USBD_LEDUSB);
|
247
|
USBState = STATE_RESUME;
|
248
|
}
|
249
|
|
250
|
//------------------------------------------------------------------------------
|
251
|
/// Invoked when the USB device gets suspended. By default, turns off all LEDs.
|
252
|
//------------------------------------------------------------------------------
|
253
|
void USBDCallbacks_Suspended(void)
|
254
|
{
|
255
|
// Turn off LEDs
|
256
|
LED_Clear(USBD_LEDPOWER);
|
257
|
LED_Clear(USBD_LEDUSB);
|
258
|
USBState = STATE_SUSPEND;
|
259
|
}
|
260
|
|
261
|
|
262
|
|
263
|
/*----------------------------------------------------------------------------
|
264
|
* Exported functions
|
265
|
*----------------------------------------------------------------------------*/
|
266
|
|
267
|
extern void USBD_IrqHandler(void);
|
268
|
/**
|
269
|
* \brief usb_audio_looprec Application entry point.
|
270
|
*
|
271
|
* Starts the driver and waits for an audio input stream to forward to the DAC.
|
272
|
*/
|
273
|
int main(void)
|
274
|
{
|
275
|
volatile uint8_t usbConn = 0;
|
276
|
volatile uint8_t plyOn = 0, recOn = 0;
|
277
|
|
278
|
TRACE_CONFIGURE(DBGU_STANDARD, 115200, BOARD_MCK);
|
279
|
|
280
|
printf("-- USB Device Fast Audio Source %s --\n\r", SOFTPACK_VERSION);
|
281
|
printf("-- %s\n\r", BOARD_NAME);
|
282
|
printf("-- Compiled: %s %s --\n\r", __DATE__, __TIME__);
|
283
|
|
284
|
/* If they are present, configure Vbus & Wake-up pins */
|
285
|
PIO_InitializeInterrupts(0);
|
286
|
|
287
|
/* Initialize all USB power (off) */
|
288
|
USBPower_Configure();
|
289
|
|
290
|
/* Audio STREAM LED */
|
291
|
LED_Configure(USBD_LEDOTHER);
|
292
|
|
293
|
/* USB audio driver initialization */
|
294
|
fastsource_init();
|
295
|
|
296
|
/* connect if needed */
|
297
|
VBus_Configure();
|
298
|
|
299
|
PIO_Configure(&pinPB, 1);
|
300
|
|
301
|
static int state = 0;
|
302
|
|
303
|
/* Infinite loop */
|
304
|
while (1)
|
305
|
{
|
306
|
if (USBD_GetState() < USBD_STATE_CONFIGURED)
|
307
|
{
|
308
|
usbConn = 0;
|
309
|
continue;
|
310
|
}
|
311
|
if (state == 0) {
|
312
|
fastsource_start();
|
313
|
state = 1;
|
314
|
}
|
315
|
if (plyOn)
|
316
|
{
|
317
|
if (isPlyActive == 0)
|
318
|
{
|
319
|
printf("plyE ");
|
320
|
plyOn = 0;
|
321
|
}
|
322
|
}
|
323
|
else if (isPlyActive)
|
324
|
{
|
325
|
#if 0
|
326
|
/* Try to Start Reading the incoming audio stream */
|
327
|
AUDDLoopRecDriver_Read(buffers[inBufferIndex],
|
328
|
AUDDLoopRecDriver_BYTESPERFRAME,
|
329
|
(TransferCallback) FrameReceived,
|
330
|
0); // No optional argument
|
331
|
|
332
|
printf("plyS ");
|
333
|
plyOn = 1;
|
334
|
#endif
|
335
|
}
|
336
|
if (recOn)
|
337
|
{
|
338
|
if (isRecActive == 0)
|
339
|
{
|
340
|
printf("recE ");
|
341
|
recOn = 0;
|
342
|
}
|
343
|
}
|
344
|
else if (isRecActive)
|
345
|
{
|
346
|
printf("recS ");
|
347
|
recOn = 1;
|
348
|
}
|
349
|
|
350
|
#ifdef dfu
|
351
|
if (PIO_Get(&pinPB) == 0)
|
352
|
DFURT_SwitchToDFU();
|
353
|
#endif
|
354
|
}
|
355
|
}
|
356
|
/** \endcond */
|