Skip to content

Commit

Permalink
Merge pull request #34 from paddatrapper/audio-data
Browse files Browse the repository at this point in the history
Audio data transfer
  • Loading branch information
mithro committed Aug 27, 2017
2 parents 63e0b50 + a60cc3f commit 0b2b68c
Show file tree
Hide file tree
Showing 15 changed files with 719 additions and 1,050 deletions.
6 changes: 4 additions & 2 deletions audio/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,12 @@ LINUXHEADERSDIR := ../third_party/fx2lib-linux-headers
COMMON_DIR := ../common
INCS := -I$(LINUXHEADERSDIR)

CFLAGS := -DDSCR_AREA=0x3e00 --code-size 0x3c00 --xram-size 0x0200
# Audio descriptors require more space, so using 3d00 rather than the normal
# 0x3c00
CFLAGS := -DDSCR_AREA=0x3d00 --code-size 0x3c00 --xram-size 0x0200
CFLAGS += --xram-loc 0x3c00 -Wl"-b INT2JT=0x3f00"

CC_SRCS := descriptors.c firmware.c version_data.c
CC_SRCS := descriptors.c debug.c audiodata.c firmware.c version_data.c

include $(COMMON_DIR)/common.mk

Expand Down
1 change: 1 addition & 0 deletions audio/TODO
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
capture
http://www.usb.org/developers/docs/whitepapers/iadclasscode_r10.pdf
http://www.usb.org/developers/docs/InterfaceAssociationDescriptor_ecn.pdf
* Add more supported frequencies (descriptors.h descripts.highspeed.format)
126 changes: 126 additions & 0 deletions audio/audiodata.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
// Copyright (C) 2017 Kyle Robbertze <krobbertze@gmail.com>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

/** \file audiodata.c
* Contains definitions for USB audio communication between the FX2 chip and
* the host
*/

#ifdef DEBUG
#include <stdio.h>
#include "debug.h"
#else
#define printf(...)
#endif

#include <fx2regs.h>
#include <delay.h>
#include <eputils.h>

#include "audiodata.h"

#define SYNCDELAY SYNCDELAY4

/**
* Returns the configuration. We only have one configuration.
*/
BYTE handle_get_configuration() {
return 1;
}

/**
* Sets the configuration. Successful if setting it to cfg 1, otherwise fail
* as the descriptors only provide one configuration.
*/
BOOL handle_set_configuration(BYTE cfg) {
return cfg==1 ? TRUE : FALSE;
}

/* The current alternative setting */
BYTE alt_setting = 0;
/**
* Returns the interface currently in use.
*/
BOOL handle_get_interface(BYTE ifc, BYTE* alt_ifc) {
printf("Get Interface\n");
if (ifc == 0 || ifc == 1) {
*alt_ifc = alt_setting;
return TRUE;
}
return FALSE;
}

/**
* Sets the interface in use.
* 0,0 - Default control
* 1,0 - Streaming, no endpoint. This is used for when the device is not
* streaming
* 1,1 - Streaming with endpoint 8.
* See TRM section 2.3.7
* http://www.cypress.com/file/126446/download#G5.1043536
*/
BOOL handle_set_interface(BYTE ifc, BYTE alt_ifc) {
printf("Set interface %d to alt: %d\n", ifc, alt_ifc);

if (ifc == 0 && alt_ifc == 0) {
alt_setting = 0;
/*
* Restore endpoints to default condition with valid bit cleared
* (invalid, bulk, out, double)
*/
EP2CFG = 0x7F;
SYNCDELAY; EP4CFG = 0x7F;
SYNCDELAY; EP6CFG = 0x7F;
SYNCDELAY; EP8CFG = 0x7F;
SYNCDELAY; RESETFIFO(0x02);
return TRUE;
} else if (ifc == 1 && alt_ifc == 0) {
alt_setting = 0;
EP2CFG = 0x7F;
SYNCDELAY; EP4CFG = 0x7F;
SYNCDELAY; EP6CFG = 0x7F;
SYNCDELAY; EP8CFG = 0x7F;
SYNCDELAY; RESETFIFO(0x02);
/* reset toggles */
SYNCDELAY; RESETTOGGLE(0x82);
return TRUE;
} else if (ifc == 1 && alt_ifc == 1) {
alt_setting = 1;
/* Reset audio streaming endpoint */
EP8CFG = (bmVALID | bmDIR | bmTYPE0);
SYNCDELAY; EP2CFG = 0x7F;
SYNCDELAY; EP4CFG = 0x7F;
SYNCDELAY; EP6CFG = 0x7F;
SYNCDELAY; RESETFIFO(0x02);
SYNCDELAY; RESETTOGGLE(0x82);
return TRUE;
}
return FALSE;
}

/**
* Descriptor requests are handled by fx2lib.
*/
BOOL handle_get_descriptor() {
printf ( "Get Descriptor\n" );
return FALSE;
}

/**
* There are no vendor commands to handle
*/
BOOL handle_vendorcommand(BYTE cmd) {
return FALSE;
}
33 changes: 33 additions & 0 deletions audio/audiodata.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright (C) 2017 Kyle Robbertze <krobbertze@gmail.com>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

/** \file audiodata.h
* Contains definitions for USB audio communication between the FX2 chip and
* the host
*/

#ifndef AUDIO_DATA_H
#define AUDIO_DATA_H

#include <audio.h>
#include <fx2types.h>

BYTE handle_get_configuration();
BOOL handle_set_configuration(BYTE cfg);
BOOL handle_get_interface(BYTE ifc, BYTE* alt_ifc);
BOOL handle_set_interface(BYTE ifc, BYTE alt_ifc);
BOOL handle_get_descriptor();

#endif // AUDIO_DATA_H
170 changes: 170 additions & 0 deletions audio/debug.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
// Copyright (C) 2009-2012 Chris McClelland
// Copyright (C) 2017 Kyle Robbertze <krobbertze@gmail.com>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

/** \file debug.c
* Implements writing to serial for debugging purposes
*/
#include <fx2regs.h>
#include "debug.h"

#define PD3 0xB3
/* Serial write uses pin D3 */
__sbit __at PD3 USART;
#define BAUD 32

/**
* Initialises the usart interface. It supports output from the FX2 on pin D3
*/
void usart_init(void) {
USART = 1;
/* Enable output on pin D3 */
OED |= bmBIT3;
}

/**
* Bit banging serial output
*/
void usart_send_byte(BYTE c) {
(void)c; /* argument passed in DPL */
__asm
mov a, dpl
mov r1, #9
clr c
loop:
mov _USART, c
rrc a
mov r0, #BAUD
djnz r0, .
nop
djnz r1, loop

;; Stop bit
setb _USART
mov r0, #BAUD
djnz r0, .
__endasm;
}

/**
* Send a byte encoded as hexadecimal
*/
void usart_send_byte_hex(BYTE byte) {
__xdata BYTE ch;
ch = (byte >> 4) & 0x0F;
ch += (ch < 10 ) ? '0' : 'A' - 10;
usart_send_byte(ch);
ch = byte & 0x0F;
ch += (ch < 10 ) ? '0' : 'A' - 10;
usart_send_byte(ch);
}

/**
* Send a word width of data as hexadecimal
*/
void usart_send_word_hex(WORD word) {
__xdata BYTE ch;
ch = (word >> 12) & 0x0F;
ch += (ch < 10 ) ? '0' : 'A' - 10;
usart_send_byte(ch);
ch = (word >> 8) & 0x0F;
ch += (ch < 10 ) ? '0' : 'A' - 10;
usart_send_byte(ch);
ch = (word >> 4) & 0x0F;
ch += (ch < 10 ) ? '0' : 'A' - 10;
usart_send_byte(ch);
ch = (word >> 0) & 0x0F;
ch += (ch < 10 ) ? '0' : 'A' - 10;
usart_send_byte(ch);
}

/**
* Send a long word as hexadecimal
*/
void usart_send_long_hex(DWORD word) {
__xdata BYTE ch;
ch = (word >> 28) & 0x0F;
ch += (ch < 10 ) ? '0' : 'A' - 10;
usart_send_byte(ch);
ch = (word >> 24) & 0x0F;
ch += (ch < 10 ) ? '0' : 'A' - 10;
usart_send_byte(ch);
ch = (word >> 20) & 0x0F;
ch += (ch < 10 ) ? '0' : 'A' - 10;
usart_send_byte(ch);
ch = (word >> 16) & 0x0F;
ch += (ch < 10 ) ? '0' : 'A' - 10;
usart_send_byte(ch);
ch = (word >> 12) & 0x0F;
ch += (ch < 10 ) ? '0' : 'A' - 10;
usart_send_byte(ch);
ch = (word >> 8) & 0x0F;
ch += (ch < 10 ) ? '0' : 'A' - 10;
usart_send_byte(ch);
ch = (word >> 4) & 0x0F;
ch += (ch < 10 ) ? '0' : 'A' - 10;
usart_send_byte(ch);
ch = (word >> 0) & 0x0F;
ch += (ch < 10 ) ? '0' : 'A' - 10;
usart_send_byte(ch);
}

/**
* Send a string using bit banging output. Either \n or \r will send both
* \n and \r for ease of programming
*/
void usart_send_string(const char *s) {
while (*s) {
switch (*s) {
case '\r':
case '\n':
usart_send_newline();
break;
default:
usart_send_byte(*s);
}
*s++;
}
}

/**
* Ease of use for sending a new line.
*/
void usart_send_newline(void) {
usart_send_byte('\n');
usart_send_byte('\r');
}

/**
* The function that printf uses to send characters. This allows printf to
* be used for debug messages.
*/
void putchar(char c) {
switch (c) {
case '\r':
case '\n':
usart_send_newline();
break;
default:
usart_send_byte(c);
}
}

/**
* Not implemented
*/
char getchar(void) {
return '0';
}
36 changes: 36 additions & 0 deletions audio/debug.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright (C) 2009-2012 Chris McClelland
// Copyright (C) 2017 Kyle Robbertze <krobbertze@gmail.com>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

/** \file debug.h
* Details writing to serial for debugging purposes
*/

#ifndef DEBUG_H
#define DEBUG_H

#include <fx2types.h>

void usart_init(void);
void usart_send_byte(BYTE c);
void usart_send_byte_hex(BYTE byte);
void usart_send_word_hex(WORD word);
void usart_send_long_hex(DWORD word);
void usart_send_string(const char *s);
void usart_send_newline(void);
void putchar(char c);
char getchar(void);

#endif

0 comments on commit 0b2b68c

Please sign in to comment.