add audio looprec project ported for sam3u-ek

This commit is contained in:
Harald Welte 2011-12-01 20:54:00 +01:00
parent 6d90963221
commit c862d700c6
4 changed files with 688 additions and 0 deletions

View File

@ -0,0 +1,239 @@
# ----------------------------------------------------------------------------
# ATMEL Microcontroller Software Support
# ----------------------------------------------------------------------------
# Copyright (c) 2008, Atmel Corporation
#
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# - Redistributions of source code must retain the above copyright notice,
# this list of conditions and the disclaimer below.
#
# Atmel's name may not be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
# DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
# OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
# EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# ----------------------------------------------------------------------------
# Makefile for compiling the usb-device-core-project project
#-------------------------------------------------------------------------------
# User-modifiable options
#-------------------------------------------------------------------------------
# Chip & board used for compilation
# (can be overriden by adding CHIP=chip and BOARD=board to the command-line)
CHIP = at91sam3u4
BOARD = at91sam3u-ek
# Trace level used for compilation
# (can be overriden by adding TRACE_LEVEL=#number to the command-line)
# TRACE_LEVEL_DEBUG 5
# TRACE_LEVEL_INFO 4
# TRACE_LEVEL_WARNING 3
# TRACE_LEVEL_ERROR 2
# TRACE_LEVEL_FATAL 1
# TRACE_LEVEL_NO_TRACE 0
TRACE_LEVEL = 4
# Optimization level, put in comment for debugging
OPTIMIZATION = -Os
# AT91 library directory
AT91LIB = ../../at91lib
# External library
EXT_LIBS= ../external_libs
# Output file basename
OUTPUT = usb-device-core-project-$(BOARD)-$(CHIP)
# Compile with chip specific features
include $(AT91LIB)/boards/$(BOARD)/$(CHIP)/chip.mak
# Compile for all memories available on the board (this sets $(MEMORIES))
include $(AT91LIB)/boards/$(BOARD)/board.mak
# Output directories
BIN = bin
OBJ = obj
#-------------------------------------------------------------------------------
# Tools
#-------------------------------------------------------------------------------
# Tool suffix when cross-compiling
CROSS_COMPILE = arm-none-eabi-
# Compilation tools
CC = $(CROSS_COMPILE)gcc
SIZE = $(CROSS_COMPILE)size
STRIP = $(CROSS_COMPILE)strip
OBJCOPY = $(CROSS_COMPILE)objcopy
# Flags
INCLUDES += -I$(AT91LIB)/boards/$(BOARD)
INCLUDES += -I$(AT91LIB)/peripherals
INCLUDES += -I$(AT91LIB)/components
INCLUDES += -I$(AT91LIB)/drivers
INCLUDES += -I$(AT91LIB)
INCLUDES += -I$(EXT_LIBS)
INCLUDES += -I$(EXT_LIBS)/cmsis
ifeq ($(CHIP_CORE), cortexm3)
TARGET_OPTS = -mcpu=cortex-m3 -mthumb
else
TARGET_OPTS =
endif
CFLAGS += $(TARGET_OPTS)
CFLAGS += -Wall -mlong-calls -ffunction-sections
CFLAGS += -g $(OPTIMIZATION) $(INCLUDES) -D$(CHIP) -DTRACE_LEVEL=$(TRACE_LEVEL)
ASFLAGS = $(TARGET_OPTS) -Wall -g $(OPTIMIZATION) $(INCLUDES) -D$(CHIP) -D__ASSEMBLY__
LDFLAGS = -g $(OPTIMIZATION) -nostartfiles $(TARGET_OPTS) -Wl,--gc-sections
#-------------------------------------------------------------------------------
# Files
#-------------------------------------------------------------------------------
# Directories where source files can be found
PERIPH = $(AT91LIB)/peripherals
BOARDS = $(AT91LIB)/boards
UTILITY = $(AT91LIB)/utility
COMP = $(AT91LIB)/components
DRIVER = $(AT91LIB)/drivers
USB = $(AT91LIB)/usb
VPATH += $(UTILITY)
VPATH += $(PERIPH)/dbgu
VPATH += $(PERIPH)/pio
VPATH += $(PERIPH)/irq
VPATH += $(PERIPH)/ssc
VPATH += $(PERIPH)/twi
VPATH += $(PERIPH)/pmc
VPATH += $(PERIPH)/cp15
VPATH += $(BOARDS)/$(BOARD)
VPATH += $(BOARDS)/$(BOARD)/$(CHIP)
VPATH += $(DRIVER)/twi
VPATH += $(PERIPH)/mci
VPATH += $(PERIPH)/dma
VPATH += $(DRIVER)/dmad
VPATH += $(EXT_LIBS)/cmsis
VPATH += $(USB)/device/audio-looprec
VPATH += $(USB)/device/core
VPATH += $(USB)/common/core
VPATH += $(USB)/common/audio
# Objects built from C source files
C_OBJECTS += main.o
C_OBJECTS += USBD_UDPHS.o
C_OBJECTS += USBDDriver.o
C_OBJECTS += USBDCallbacks_Initialized.o
C_OBJECTS += USBDCallbacks_Reset.o
#C_OBJECTS += USBDCallbacks_Resumed.o
#C_OBJECTS += USBDCallbacks_Suspended.o
C_OBJECTS += USBDDriverCb_CfgChanged.o
#C_OBJECTS += USBDDriverCb_IfSettingChanged.o
C_OBJECTS += USBInterfaceRequest.o
C_OBJECTS += USBFeatureRequest.o
C_OBJECTS += USBGenericRequest.o
C_OBJECTS += USBGetDescriptorRequest.o
C_OBJECTS += USBSetAddressRequest.o
C_OBJECTS += USBSetConfigurationRequest.o
C_OBJECTS += USBGenericDescriptor.o
C_OBJECTS += USBConfigurationDescriptor.o
C_OBJECTS += USBEndpointDescriptor.o
C_OBJECTS += dbgu.o
C_OBJECTS += pio.o
C_OBJECTS += pio_it.o
C_OBJECTS += ssc.o
C_OBJECTS += twi.o
C_OBJECTS += pmc.o
C_OBJECTS += led.o
C_OBJECTS += twid.o
C_OBJECTS += string.o
C_OBJECTS += stdio.o
C_OBJECTS += math.o
C_OBJECTS += trace.o
C_OBJECTS += board_memories.o
C_OBJECTS += board_lowlevel.o
C_OBJECTS += AUDGenericRequest.o AUDFeatureUnitRequest.o
C_OBJECTS += AUDDLoopRecChannel.o AUDDLoopRecDriver.o
C_OBJECTS += AUDDLoopRecDriverDescriptors.o
# Objects for different chips
ifeq ($(CHIP_CORE), cortexm3)
C_OBJECTS += nvic.o
C_OBJECTS += exceptions.o
C_OBJECTS += board_cstartup_gnu.o
C_OBJECTS += core_cm3.o
else
C_OBJECTS += aic.o
C_OBJECTS += cp15.o
endif
ifeq ($(CHIP_IP_MCI), MCI_DMA)
C_OBJECTS += dmad.o
C_OBJECTS += dma.o
C_OBJECTS += mci_hs.o
else
C_OBJECTS += mci.o
endif
# Objects built from Assembly source files
ifneq ($(CHIP_CORE), cortexm3)
ASM_OBJECTS += board_cstartup.o
ASM_OBJECTS += cp15_asm.o
endif
# Append OBJ and BIN directories to output filename
OUTPUT := $(BIN)/$(OUTPUT)
#-------------------------------------------------------------------------------
# Rules
#-------------------------------------------------------------------------------
all: $(BIN) $(OBJ) $(MEMORIES)
$(BIN) $(OBJ):
mkdir $@
define RULES
C_OBJECTS_$(1) = $(addprefix $(OBJ)/$(1)_, $(C_OBJECTS))
ASM_OBJECTS_$(1) = $(addprefix $(OBJ)/$(1)_, $(ASM_OBJECTS))
$(1): $$(ASM_OBJECTS_$(1)) $$(C_OBJECTS_$(1))
$(CC) $(LDFLAGS) -T"$(AT91LIB)/boards/$(BOARD)/$(CHIP)/$$@.lds" -o $(OUTPUT)-$$@.elf $$^
$(OBJCOPY) -O binary $(OUTPUT)-$$@.elf $(OUTPUT)-$$@.bin
$(SIZE) $$^ $(OUTPUT)-$$@.elf
$$(C_OBJECTS_$(1)): $(OBJ)/$(1)_%.o: %.c Makefile $(OBJ) $(BIN)
$(CC) $(CFLAGS) -D$(1) -c -o $$@ $$<
$$(ASM_OBJECTS_$(1)): $(OBJ)/$(1)_%.o: %.S Makefile $(OBJ) $(BIN)
$(CC) $(ASFLAGS) -D$(1) -c -o $$@ $$<
debug_$(1): $(1)
perl ../resources/gdb/debug.pl $(OUTPUT)-$(1).elf
endef
$(foreach MEMORY, $(MEMORIES), $(eval $(call RULES,$(MEMORY))))
clean:
-rm -f $(OBJ)/*.o $(BIN)/*.bin $(BIN)/*.elf

View File

@ -0,0 +1,449 @@
/* ----------------------------------------------------------------------------
* ATMEL Microcontroller Software Support
* ----------------------------------------------------------------------------
* Copyright (c) 2009, Atmel Corporation
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the disclaimer below.
*
* Atmel's name may not be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
* DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* ----------------------------------------------------------------------------
*/
/** \cond usb_audio_looprec
* \page usb_audio_looprec USB Audio Loopback-Recorder Example
*
* \section Purpose
*
* The USB Audio Loopback-Recorder Example will help you to get
* familiar with the USB Device Port(UDP) and DACC on AT91SAM microcontrollers.
* Also it can help you to be familiar with the USB Framework that is used for
* rapid development of USB-compliant class drivers such as USB Audio Device
* class.
*
* \section Requirements
*
* This package can be used with SAM9XX5 evaluation kits that have both
* UDP.
*
* \section Description
*
* The demo simulates an USB Desktop Speaker with Microphone which actually
* does not "speak out" but loop back the sound as microphone input.
*
* When an EK running this program connected to a host (PC for example), with
* USB cable, the EK appears as a desktop speaker for the host. Then the host
* can play sound through host software. The audio stream from the host is
* then sent to the EK. At the same time, the audio stream received is also
* sent back to host from EK for recording.
*
* \section Usage
*
* -# Build the program and download it inside the evaluation board. Please
* refer to the
* <a href="http://www.atmel.com/dyn/resources/prod_documents/doc6421.pdf">
* SAM-BA User Guide</a>, the
* <a href="http://www.atmel.com/dyn/resources/prod_documents/doc6310.pdf">
* GNU-Based Software Development</a> application note or to the
* <a href="ftp://ftp.iar.se/WWWfiles/arm/Guides/EWARM_UserGuide.ENU.pdf">
* IAR EWARM User Guide</a>, depending on your chosen solution.
* -# On the computer, open and configure a terminal application
* (e.g. HyperTerminal on Microsoft Windows) with these settings:
* - 115200 bauds
* - 8 bits of data
* - No parity
* - 1 stop bit
* - No flow control
* -# Start the application.
* -# In the terminal window, the following text should appear:
* \code
* -- USB Device Audio LoopREC Example xxx --
* -- AT91xxxxxx-xx
* -- Compiled: xxx xx xxxx xx:xx:xx --
* \endcode
* -# When connecting USB cable to windows, the LED blinks, and the host
* reports a new USB device attachment (if it's the first time you connect
* an audio speaker demo board to your host). You can find new
* "USB Composite Device" and "USB Audio Device" appear in the hardware
* device list.
* -# You can play sound in host side through the USB Audio Device.
* When playing sound, you can also record through the USB Audio Device on
* the host.
*
* \section References
* - usb_audio_looprec/main.c
* - ssc: SSC interface driver
* - usb: USB Framework, Audio Device Class driver and UDP interface driver
* - \ref usbd_framework
* - \ref usbd_api
* - \ref usbd_audio_rec_drv
*
*/
/**
* \file
*
* This file contains all the specific code for the
* usb_audio_looprec example.
*/
/*----------------------------------------------------------------------------
* Headers
*----------------------------------------------------------------------------*/
#include <board.h>
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
#include <stdbool.h>
#include <irq/irq.h>
#include <pio/pio.h>
#include <pio/pio_it.h>
#include <utility/trace.h>
#include <utility/led.h>
#include <usb/device/core/USBD.h>
#include <usb/device/audio-looprec/AUDDLoopRecDriver.h>
#include <usb/device/audio-looprec/AUDDLoopRecChannel.h>
/*----------------------------------------------------------------------------
* Definitions
*----------------------------------------------------------------------------*/
/** Number of available audio buffers. */
#define BUFFER_NUMBER 8
/** Size of one buffer in bytes. */
#define BUFFER_SIZE (AUDDLoopRecDriver_BYTESPERFRAME*2)
/// Use for power management
#define STATE_IDLE 0
/// The USB device is in suspend state
#define STATE_SUSPEND 4
/// The USB device is in resume state
#define STATE_RESUME 5
//------------------------------------------------------------------------------
// Internal variables
//------------------------------------------------------------------------------
/// State of USB, for suspend and resume
unsigned char USBState = STATE_IDLE;
/** Data buffers for receiving audio frames from the USB host. */
static uint8_t buffers[BUFFER_NUMBER][BUFFER_SIZE];
/** Number of samples stored in each data buffer. */
static uint32_t bufferSizes[BUFFER_NUMBER];
/** Next buffer in which USB data can be stored. */
static uint32_t inBufferIndex = 0;
/** Number of buffers that can be sent to the DAC. */
static volatile uint32_t numBuffersToSend = 0;
/** Current state of the playback stream interface. */
static volatile uint8_t isPlyActive = 0;
/** Current state of the record stream interface. */
static volatile uint8_t isRecActive = 0;
/*----------------------------------------------------------------------------
* VBus monitoring (optional)
*----------------------------------------------------------------------------*/
/** VBus pin instance. */
static const Pin pinVbus = PIN_USB_VBUS;
/**
* Handles interrupts coming from PIO controllers.
*/
static void ISR_Vbus(const Pin *pPin)
{
/* Check current level on VBus */
if (PIO_Get(&pinVbus))
{
TRACE_INFO("VBUS conn\n\r");
USBD_Connect();
}
else
{
TRACE_INFO("VBUS discon\n\r");
USBD_Disconnect();
}
}
/**
* Configures the VBus pin to trigger an interrupt when the level on that pin
* changes.
*/
static void VBus_Configure( void )
{
/* Configure PIO */
PIO_Configure(&pinVbus, 1);
PIO_ConfigureIt(&pinVbus, ISR_Vbus);
PIO_EnableIt(&pinVbus);
/* Check current level on VBus */
if (PIO_Get(&pinVbus))
{
/* if VBUS present, force the connect */
USBD_Connect();
}
else
{
TRACE_INFO("discon\n\r");
USBD_Disconnect();
}
}
/*----------------------------------------------------------------------------
* USB Power Control
*----------------------------------------------------------------------------*/
#ifdef PIN_USB_POWER_ENA
/** Power Enable A (MicroAB Socket) pin instance. */
static const Pin pinPOnA = PIN_USB_POWER_ENA;
#endif
#ifdef PIN_USB_POWER_ENB
/** Power Enable B (A Socket) pin instance. */
static const Pin pinPOnB = PIN_USB_POWER_ENB;
#endif
#ifdef PIN_USB_POWER_ENC
/** Power Enable C (A Socket) pin instance. */
static const Pin pinPOnC = PIN_USB_POWER_ENC;
#endif
/**
* Configures the Power Enable pin to disable self power.
*/
static void USBPower_Configure( void )
{
#ifdef PIN_USB_POWER_ENA
PIO_Configure(&pinPOnA, 1);
#endif
#ifdef PIN_USB_POWER_ENB
PIO_Configure(&pinPOnB, 1);
#endif
#ifdef PIN_USB_POWER_ENC
PIO_Configure(&pinPOnC, 1);
#endif
}
/*----------------------------------------------------------------------------
* Internal functions
*----------------------------------------------------------------------------*/
/**
* Invoked when a frame has been received.
*/
static void FrameReceived(uint32_t unused,
uint8_t status,
uint32_t transferred,
uint32_t remaining)
{
if (status == USBD_STATUS_SUCCESS)
{
/* Loopback! add this buffer to write list */
if (!isRecActive) {}
else
{
AUDDLoopRecDriver_Write(buffers[inBufferIndex],
AUDDLoopRecDriver_BYTESPERFRAME,
NULL, 0);
}
/* Update input status data */
bufferSizes[inBufferIndex] = transferred
/ AUDDLoopRecDriver_BYTESPERSAMPLE;
inBufferIndex = (inBufferIndex + 1) % BUFFER_NUMBER;
numBuffersToSend++;
}
else if (status == USBD_STATUS_ABORTED)
{
/* Error , ABORT, add NULL buffer */
bufferSizes[inBufferIndex] = 0;
inBufferIndex = (inBufferIndex + 1) % BUFFER_NUMBER;
numBuffersToSend++;
}
else
{
/* Packet is discarded */
}
/* Receive next packet */
AUDDLoopRecDriver_Read(buffers[inBufferIndex],
AUDDLoopRecDriver_BYTESPERFRAME,
(TransferCallback) FrameReceived,
0); // No optional argument
}
/*----------------------------------------------------------------------------
* Callbacks re-implementation
*----------------------------------------------------------------------------*/
//------------------------------------------------------------------------------
/// Invoked when the USB device leaves the Suspended state. By default,
/// configures the LEDs.
//------------------------------------------------------------------------------
void USBDCallbacks_Resumed(void)
{
// Initialize LEDs
LED_Configure(USBD_LEDPOWER);
LED_Set(USBD_LEDPOWER);
LED_Configure(USBD_LEDUSB);
LED_Clear(USBD_LEDUSB);
USBState = STATE_RESUME;
}
//------------------------------------------------------------------------------
/// Invoked when the USB device gets suspended. By default, turns off all LEDs.
//------------------------------------------------------------------------------
void USBDCallbacks_Suspended(void)
{
// Turn off LEDs
LED_Clear(USBD_LEDPOWER);
LED_Clear(USBD_LEDUSB);
USBState = STATE_SUSPEND;
}
/**
* Invoked when an audio channel get muted or unmuted. Mutes/unmutes the
* channel at the DAC level.
* \param mic Microphone/Speaker stream changed.
* \param channel Channel number that changed.
* \param muted Indicates the new mute status of the channel.
*/
void AUDDLoopRecChannel_MuteChanged(AUDDLoopRecChannel *channel,
uint8_t muted)
{
/* Speaker Master channel */
if (channel == AUDDLoopRecDriver_MASTERCHANNEL)
{
if (muted)
{
TRACE_WARNING("MuteMaster ");
}
else
{
TRACE_INFO("UnmuteMaster ");
}
}
}
#if 0
/**
* Invoked when an audio streaming interface setting changed.
* Audio stream is automatically reseted.
* Actually control streaming rate.
* \param mic Microphone/Speaker stream changed.
* \param newSetting New stream (interface) setting.
*/
void AUDDLoopRecDriver_StreamSettingChanged(uint8_t mic,
uint8_t newSetting)
{
/* Speaker stream */
if (!mic)
{
isPlyActive = (newSetting > 0);
}
else
{
isRecActive = (newSetting > 0);
}
}
#endif
/*----------------------------------------------------------------------------
* Exported functions
*----------------------------------------------------------------------------*/
extern void USBD_IrqHandler(void);
/**
* \brief usb_audio_looprec Application entry point.
*
* Starts the driver and waits for an audio input stream to forward to the DAC.
*/
int main(void)
{
volatile uint8_t usbConn = 0;
volatile uint8_t plyOn = 0, recOn = 0;
printf("-- USB Device Audio LoopREC Example %s --\n\r", SOFTPACK_VERSION);
printf("-- %s\n\r", BOARD_NAME);
printf("-- Compiled: %s %s --\n\r", __DATE__, __TIME__);
/* If they are present, configure Vbus & Wake-up pins */
PIO_InitializeInterrupts(0);
/* Initialize all USB power (off) */
USBPower_Configure();
/* Audio STREAM LED */
LED_Configure(USBD_LEDOTHER);
/* Configure USB & DACC interrupt priority */
//IRQ_ConfigureIT( ID_UDPHS, 6, USBD_IrqHandler );
/* USB audio driver initialization */
AUDDLoopRecDriver_Initialize();
/* connect if needed */
VBus_Configure();
/* Infinite loop */
while (1)
{
if (USBD_GetState() < USBD_STATE_CONFIGURED)
{
usbConn = 0;
continue;
}
if (plyOn)
{
if (isPlyActive == 0)
{
printf("plyE ");
plyOn = 0;
}
}
else if (isPlyActive)
{
/* Try to Start Reading the incoming audio stream */
AUDDLoopRecDriver_Read(buffers[inBufferIndex],
AUDDLoopRecDriver_BYTESPERFRAME,
(TransferCallback) FrameReceived,
0); // No optional argument
printf("plyS ");
plyOn = 1;
}
if (recOn)
{
if (isRecActive == 0)
{
printf("recE ");
recOn = 0;
}
}
else if (isRecActive)
{
printf("recS ");
recOn = 1;
}
}
}
/** \endcond */