initial commit

This commit is contained in:
2013-10-02 10:43:54 +02:00
commit f3e562f835
92 changed files with 7400 additions and 0 deletions

1
firmware/lib/.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
fx2.lib

40
firmware/lib/Makefile Normal file
View File

@@ -0,0 +1,40 @@
# Copyright (C) 2009 Ubixum, Inc.
#
# This library 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.1 of the License, or (at your option) any later version.
#
# This library 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 library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
AS8051 ?= sdcc-sdas8051
SOURCES = serial.c i2c.c delay.c setupdat.c gpif.c eputils.c $(wildcard interrupts/*.c)
FX2_OBJS = $(patsubst %.c,%.rel, $(SOURCES)) usbav.rel int4av.rel
INCLUDES = -I../include
SDCC = sdcc-sdcc -mmcs51 $(SDCCFLAGS)
LIBS = fx2.lib
all: $(LIBS)
$(LIBS): $(FX2_OBJS)
sdcc-sdcclib fx2.lib $?
usbav.rel: usbav.a51
$(AS8051) -logs usbav.a51
int4av.rel: int4av.a51
$(AS8051) -logs int4av.a51
%.rel: %.c
$(SDCC) $(INCLUDES) -c $< -o $@
clean:
rm -f *.{asm,ihx,lnk,lst,map,mem,rel,rst,sym,adb,cdb,lib}
rm -f interrupts/*.{asm,ihx,lnk,lst,map,mem,rel,rst,sym,adb,dcb,lib}

74
firmware/lib/delay.c Normal file
View File

@@ -0,0 +1,74 @@
/**
* Copyright (C) 2009 Ubixum, Inc.
*
* This library 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.1 of the License, or (at your option) any later version.
*
* This library 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 library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
**/
#include <fx2regs.h>
#include <fx2macros.h>
#include <delay.h>
void
delay(WORD millis)
{
/**
* It takes 12 crystal pulses to make 1 machine cycle (8051.com)
* ez-usb trm 1.13
* 83.3 ns at 48mhz per instruction cycle
* (assume 166.6ns at 24mhz)
* (assume 333.3ns at 12mhz)
* ez-usb trm 12.1
* Includes the cycles for each instruction
**/
WORD loop_count;
volatile WORD count; // NOTE perhaps use different solutions w/ out volatile
// set count to the number of times we need to
// go around a loop for 1 millisecond
// then do that loop millis times. (1000 us=1ms)
// 48mhz: 1000 us / (17 cycles * 83.3 ns / cycle / 1000 ns/us) = 706
// 24mhz: 353
// 12mhz: 177
// recalculate if the number of cycles changes
// like if you change the loop below
loop_count = CPUFREQ == CLK_12M ? 177 : CPUFREQ == CLK_24M ? 353 : 706;
// sdcc generated assembly
/* cycles code
; delay.c:31: do {
00101$:
; delay.c:32: } while ( --count );
2 dec _delay_count_1_1
2 mov a,#0xff
4 cjne a,_delay_count_1_1,00121$
2 dec (_delay_count_1_1 + 1)
00121$:
2 mov a,_delay_count_1_1
2 orl a,(_delay_count_1_1 + 1)
3 jnz 00101$
Total 17
*/
do {
count = loop_count;
do {
} while (--count);
} while (--millis);
}

64
firmware/lib/eputils.c Normal file
View File

@@ -0,0 +1,64 @@
/**
* Copyright (C) 2009 Ubixum, Inc.
*
* This library 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.1 of the License, or (at your option) any later version.
*
* This library 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 library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
**/
#include <eputils.h>
#include <fx2regs.h>
#ifdef DEBUG_EPUTILS
#include <stdio.h>
#else
#define printf(...)
#endif
void
readep0(BYTE * dst, WORD len)
{
WORD read = 0; // n bytes read
BYTE c, avail;
while (read < len) {
EP0BCH = 0;
// NOTE need syncdelay?
EP0BCL = 0; // re-arm ep so host can send more
while (EP0CS & bmEPBUSY);
avail = EP0BCL; // max size fits in one byte (64 bytes)
for (c = 0; c < avail; ++c)
dst[read + c] = EP0BUF[c];
read += avail;
}
}
void
writeep0(BYTE * src, WORD len)
{
WORD written = 0;
BYTE c;
while (written < len) {
while (EP0CS & bmEPBUSY); // wait
for (c = 0; c < 64 && written < len; ++c) {
EP0BUF[c] = src[written++];
}
EP0BCH = 0;
EP0BCL = c;
printf("Write %d bytes\n", c);
}
}

94
firmware/lib/fx2.mk Normal file
View File

@@ -0,0 +1,94 @@
# common make targets for compiling fx2 firmware
#
# In your Makefile, define:
# SOURCES: list of c files to compile
# A51_SOURCES: list of any a51 files.
# DEPS: list of any depedancies (like auto-generated header files) that need
# generated prior to compiling. You must provide the target definition
# for any DEPS you define.
# BASENAME: name of your firmware file, i.e., myfirmware, but not myfirmware.c
#
# Leave these alone or redefine as necessary to customize firmware.
# (Redefine after including this makefile)
# VID vendor id
# PID product id
# LIBS optional additional libraries to link with the firmware.
# SDCC build/link options
# CODE_SIZE: Default --code-size 0x3c00
# XRAM_SIZE: Default --xram-size 0x0200
# XRAM_LOC: Default --xram-loc 0x3c00
# BUILDDIR: build directory (default build)
# These two can be changed to be blank if no device descriptor is being used.
# DSCR_AREA: Default -Wl"-b DSCR_AREA=0x3e00"
# INT2JT: Default -Wl"-b INT2JT=0x3f00"
#
# Provided targets:
#
# default target: creates $(BASENAME).ihx
# bix: creates $(BASENAME).bix
# iic: creates $(BASENAME).iic
# load: uses fx2load to load firmware.bix onto the development board
# (You can customize VID/PID if you need to load the firmware onto a device that has different vendor and product id
# The default is 0x04b4, 0x8613
# clean: delete all the temp files.
ASM = sdcc-sdas8051
CC = sdcc-sdcc
VID ?= 0x04b4
PID ?= 0x2342
DSCR_AREA ?= -Wl"-b DSCR_AREA=0x3e00"
INT2JT ?= -Wl"-b INT2JT=0x3f00"
CODE_SIZE ?= --code-size 0x3c00
XRAM_SIZE ?= --xram-size 0x0200
XRAM_LOC ?= --xram-loc 0x3c00
BUILDDIR ?= build
CFLAGS = -mmcs51 $(CODE_SIZE) $(XRAM_SIZE) $(XRAM_LOC) $(DSCR_AREA) $(INT2JT)
FX2LIBDIR?=$(dir $(lastword $(MAKEFILE_LIST)))../
RELS=$(addprefix $(BUILDDIR)/, $(addsuffix .rel, $(notdir $(basename $(SOURCES) $(A51_SOURCES)))))
.PHONY: all ihx iic bix load clean clean-all
all: ihx
ihx: $(BUILDDIR)/$(BASENAME).ihx
bix: $(BUILDDIR)/$(BASENAME).bix
iic: $(BUILDDIR)/$(BASENAME).iic
$(FX2LIBDIR)/lib/fx2.lib: $(FX2LIBDIR)/lib/*.c $(FX2LIBDIR)/lib/*.a51
$(MAKE) -C $(FX2LIBDIR)/lib
$(BUILDDIR):
mkdir -p $(BUILDDIR)
# can't use default target %.rel because there is no way
# to differentiate the dependency. (Is it %.rel: %.c or %.a51)
$(BUILDDIR)/$(BASENAME).ihx: $(BUILDDIR) $(SOURCES) $(A51_SOURCES) $(FX2LIBDIR)/lib/fx2.lib $(DEPS)
for a in $(A51_SOURCES); do \
cp $$a $(BUILDDIR)/; \
cd $(BUILDDIR) && $(ASM) -logs `basename $$a` && cd ..; \
done
for s in $(SOURCES); do \
THISREL=$$(basename `echo "$$s" | sed -e 's/\.c$$/\.rel/'`); \
$(CC) $(CFLAGS) -c -I $(FX2LIBDIR)/include $$s -o $(BUILDDIR)/$$THISREL ; \
done
$(CC) $(CFLAGS) -o $@ $(RELS) fx2.lib -L $(FX2LIBDIR)/lib $(LIBS)
$(BUILDDIR)/$(BASENAME).bix: $(BUILDDIR)/$(BASENAME).ihx
objcopy -I ihex -O binary $< $@
$(BUILDDIR)/$(BASENAME).iic: $(BUILDDIR)/$(BASENAME).ihx
$(FX2LIBDIR)/utils/ihx2iic.py -v $(VID) -p $(PID) $< $@
load: $(BUILDDIR)/$(BASENAME).bix
#fx2load -v $(VID) -p $(PID) $(BUILDDIR)/$(BASENAME).bix
fx2load -v 0x1443 -p 0x0007 $(BUILDDIR)/$(BASENAME).bix
#fxload -vvv -t fx2 -D /dev/bus/usb/002/003 -I $(BUILDDIR)/$(BASENAME).bix
#fxload -vvv -t fx2 -D /dev/bus/usb/002/003 -I build/firmware.ihx
clean:
rm -f $(BUILDDIR)/*.{asm,ihx,lnk,lst,map,mem,rel,rst,sym,adb,cdb,bix}
clean-all: clean
$(MAKE) -C $(FX2LIBDIR)/lib clean

198
firmware/lib/gpif.c Normal file
View File

@@ -0,0 +1,198 @@
/**
* Copyright (C) 2009 Ubixum, Inc.
*
* This library 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.1 of the License, or (at your option) any later version.
*
* This library 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 library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
**/
#include <fx2regs.h>
#include <fx2macros.h>
#include <delay.h>
#include <gpif.h>
#define SYNCDELAY SYNCDELAY4
void
gpif_init(BYTE * wavedata, BYTE * initdata)
{
BYTE i;
// Registers which require a synchronization delay, see section 15.14
// FIFORESET FIFOPINPOLAR
// INPKTEND OUTPKTEND
// EPxBCH:L REVCTL
// GPIFTCB3 GPIFTCB2
// GPIFTCB1 GPIFTCB0
// EPxFIFOPFH:L EPxAUTOINLENH:L
// EPxFIFOCFG EPxGPIFFLGSEL
// PINFLAGSxx EPxFIFOIRQ
// EPxFIFOIE GPIFIRQ
// GPIFIE GPIFADRH:L
// UDMACRCH:L EPxGPIFTRIG
// GPIFTRIG
// Note: The pre-REVE EPxGPIFTCH/L register are affected, as well...
// ...these have been replaced by GPIFTC[B3:B0] registers
// 8051 doesn't have access to waveform memories 'til
// the part is in GPIF mode.
// IFCLKSRC=1 , FIFOs executes on internal clk source
// xMHz=1 , 48MHz internal clk rate
// IFCLKOE=0 , Don't drive IFCLK pin signal at 48MHz
// IFCLKPOL=0 , Don't invert IFCLK pin signal from internal clk
// ASYNC=1 , master samples asynchronous
// GSTATE=1 , Drive GPIF states out on PORTE[2:0], debug WF
// IFCFG[1:0]=10, FX2 in GPIF master mode IFCONFIG
IFCONFIG &= ~0x03; // turn off IFCFG[1:0]
IFCONFIG |= 0x02; // set's IFCFG[1:0] to 10 to put in GPIF master mode.
GPIFABORT = 0xFF; // abort any waveforms pending
GPIFREADYCFG = initdata[0];
GPIFCTLCFG = initdata[1];
GPIFIDLECS = initdata[2];
GPIFIDLECTL = initdata[3];
GPIFWFSELECT = initdata[5];
GPIFREADYSTAT = initdata[6];
// use dual autopointer feature...
AUTOPTRSETUP = 0x07; // inc both pointers,
// ...warning: this introduces pdata hole(s)
// ...at E67B (XAUTODAT1) and E67C (XAUTODAT2)
// source
AUTOPTRH1 = MSB((WORD) wavedata);
AUTOPTRL1 = LSB((WORD) wavedata);
// destination
AUTOPTRH2 = 0xE4;
AUTOPTRL2 = 0x00;
// transfer
for (i = 0x00; i < 128; i++) {
EXTAUTODAT2 = EXTAUTODAT1;
}
// Configure GPIF Address pins, output initial value,
// these instructions don't do anything on the
// smaller chips (e.g., 56 pin model only has ports a,b,d)
PORTCCFG = 0xFF; // [7:0] as alt. func. GPIFADR[7:0]
OEC = 0xFF; // and as outputs
PORTECFG |= 0x80; // [8] as alt. func. GPIFADR[8]
OEE |= 0x80; // and as output
// ...OR... tri-state GPIFADR[8:0] pins
// PORTCCFG = 0x00; // [7:0] as port I/O
// OEC = 0x00; // and as inputs
// PORTECFG &= 0x7F; // [8] as port I/O
// OEE &= 0x7F; // and as input
// GPIF address pins update when GPIFADRH/L written
SYNCDELAY; //
GPIFADRH = 0x00; // bits[7:1] always 0
SYNCDELAY; //
GPIFADRL = 0x00; // point to PERIPHERAL address 0x0000
// set the initial flowstates to be all 0 in case flow states are not used
FLOWSTATE = 0;
FLOWLOGIC = 0;
FLOWEQ0CTL = 0;
FLOWEQ1CTL = 0;
FLOWHOLDOFF = 0;
FLOWSTB = 0;
FLOWSTBEDGE = 0;
FLOWSTBHPERIOD = 0;
}
void
gpif_setflowstate(BYTE * flowstates, BYTE bank)
{
BYTE base = 9 * bank;
FLOWSTATE = flowstates[base];
FLOWLOGIC = flowstates[base + 1];
FLOWEQ0CTL = flowstates[base + 2];
FLOWEQ1CTL = flowstates[base + 3];
FLOWHOLDOFF = flowstates[base + 4];
FLOWSTB = flowstates[base + 5];
FLOWSTBEDGE = flowstates[base + 6];
FLOWSTBHPERIOD = flowstates[base + 7];
}
void
gpif_set_tc32(DWORD tc)
{
GPIFTCB3 = MSB(MSW(tc));
SYNCDELAY;
GPIFTCB2 = LSB(MSW(tc));
SYNCDELAY;
GPIFTCB1 = MSB(LSW(tc));
SYNCDELAY;
GPIFTCB0 = LSB(LSW(tc));
}
void
gpif_set_tc16(WORD tc)
{
GPIFTCB1 = MSB(tc);
SYNCDELAY;
GPIFTCB0 = LSB(tc);
}
void
gpif_single_read16(WORD * res, WORD len)
{
BYTE c;
while (!(GPIFTRIG & 0x80)); // wait done
// dummy read to trigger real read
res[0] = XGPIFSGLDATLX;
for (c = 0; c < len; ++c) {
while (!(GPIFTRIG & 0x80)); // wait done
// real read
res[c] = GPIFSGLDATH << 8;
// whether or not to do another transfer is controlled by GPIFSGLDATLNOX or ..DATLX
res[c] |= c == len - 1 ? GPIFSGLDATLNOX : GPIFSGLDATLX;
}
}
void
gpif_single_write16(WORD * dat, WORD len)
{
BYTE c;
for (c = 0; c < len; ++c) {
while (!(GPIFTRIG & 0x80));
XGPIFSGLDATH = MSB(dat[c]);
XGPIFSGLDATLX = LSB(dat[c]);
}
}
void
gpif_fifo_read(GPIF_EP_NUM ep_num)
{
while (!(GPIFTRIG & 0x80)); // wait until things are finished
GPIFTRIG = GPIFTRGRD | ep_num;
}
void
gpif_fifo_write(GPIF_EP_NUM ep_num)
{
while (!(GPIFTRIG & 0x80)); // wait until things are finished
GPIFTRIG = ep_num; // R/W=0, E[1:0] = ep_num
}

313
firmware/lib/i2c.c Normal file
View File

@@ -0,0 +1,313 @@
/**
* Copyright (C) 2009 Ubixum, Inc.
*
* This library 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.1 of the License, or (at your option) any later version.
*
* This library 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 library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
**/
#include <stdio.h> // NOTE this needs deleted
#include <fx2regs.h>
#include <fx2macros.h>
#include <i2c.h>
#include <delay.h>
//#define DEBUG_I2C 1
#ifdef DEBUG_I2C
#define i2c_printf(...) printf(__VA_ARGS__)
#else
#define i2c_printf(...)
#endif
volatile __xdata BOOL cancel_i2c_trans;
#define CHECK_I2C_CANCEL() if (cancel_i2c_trans) return FALSE
/**
*
1. Set START=1. If BERR=1, start timer*.
2. Write the 7-bit peripheral address and the direction bit (0 for a write) to I2DAT.
3. Wait for DONE=1 or for timer to expire*. If BERR=1, go to step 1.
4. If ACK=0, go to step 9.
5. Load I2DAT with a data byte.
6. Wait for DONE=1*. If BERR=1, go to step 1.
7. If ACK=0, go to step 9.
8. Repeat steps 5-7 for each byte until all bytes have been transferred.
9. Set STOP=1. Wait for STOP = 0 before initiating another transfer.
**/
BOOL
i2c_write(BYTE addr, WORD len, BYTE * addr_buf, WORD len2, BYTE * data_buf)
{
WORD cur_byte;
WORD total_bytes = len + len2; // NOTE overflow error?
BYTE retry_count = 2; // two tries to write address/read ack
cancel_i2c_trans = FALSE;
//BOOL wait=FALSE; // use timer if needed
// 1. Set START=1. If BERR=1, start timer*.
step1:
CHECK_I2C_CANCEL();
cur_byte = 0;
I2CS |= bmSTART;
if (I2CS & bmBERR) {
i2c_printf("Woops.. need to do the timer\n");
delay(10); // way too long probably
goto step1;
}
// 2. Write the 7-bit peripheral address and the direction bit (0 for a write) to I2DAT.
I2DAT = addr << 1;
// 3. Wait for DONE=1 or for timer to expire*. If BERR=1, go to step 1.
while (!(I2CS & bmDONE) && !cancel_i2c_trans);
CHECK_I2C_CANCEL();
if (I2CS & bmBERR) {
i2c_printf("bmBERR, going to step 1\n");
goto step1;
}
// 4. If ACK=0, go to step 9.
if (!(I2CS & bmACK)) {
I2CS |= bmSTOP;
while ((I2CS & bmSTOP) && !cancel_i2c_trans);
CHECK_I2C_CANCEL();
--retry_count;
if (!retry_count) {
i2c_printf("No ack after writing address.! Fail\n");
return FALSE;
}
delay(10);
goto step1;
}
// 8. Repeat steps 5-7 for each byte until all bytes have been transferred.
while (cur_byte < total_bytes) {
// 5. Load I2DAT with a data byte.
I2DAT =
cur_byte <
len ? addr_buf[cur_byte] : data_buf[cur_byte - len];
++cur_byte;
// 6. Wait for DONE=1*. If BERR=1, go to step 1.
while (!(I2CS & bmDONE) && !cancel_i2c_trans);
CHECK_I2C_CANCEL();
if (I2CS & bmBERR) {
i2c_printf("bmBERR on byte %d. Going to step 1\n",
cur_byte - 1);
goto step1;
//return FALSE;
}
// 7. If ACK=0, go to step 9.
if (!(I2CS & bmACK)) {
I2CS |= bmSTOP;
while ((I2CS & bmSTOP) && !cancel_i2c_trans);
i2c_printf("No Ack after byte %d. Fail\n", cur_byte - 1);
return FALSE;
}
}
// 9. Set STOP=1. Wait for STOP = 0 before initiating another transfer.
//real step 9
I2CS |= bmSTOP;
while ((I2CS & bmSTOP) && !cancel_i2c_trans);
CHECK_I2C_CANCEL();
return TRUE;
}
/*
trm 13.4.4
1. Set START=1. If BERR = 1, start timer*.
2. Write the 7-bit peripheral address and the direction bit (1 for a read) to I2DAT.
3. Wait for DONE=1 or for timer to expire*. If BERR=1, go to step 1.
4. If ACK=0, set STOP=1 and go to step 15.
5. Read I2DAT to initiate the first burst of nine SCL pulses to clock in the first byte from the slave.
Discard the value that was read from I2DAT.
6. Wait for DONE=1. If BERR=1, go to step 1.
7. Read the just-received byte of data from I2DAT. This read also initiates the next read transfer.
8. Repeat steps 6 and 7 for each byte until ready to read the second-to-last byte.
9. Wait for DONE=1. If BERR=1, go to step 1.
10. Before reading the second-to-last I2DAT byte, set LASTRD=1.
11. Read the second-to-last byte from I2DAT. With LASTRD=1, this initiates the final byte read on
the bus.
12. Wait for DONE=1. If BERR=1, go to step 1.
13. Set STOP=1.
14. Read the final byte from I2DAT immediately (the next instruction) after setting the STOP bit. By
reading I2DAT while the "stop" condition is being generated, the just-received data byte will be
retrieved without initiating an extra read transaction (nine more SCL pulses) on the I²Cbus.
15. Wait for STOP = 0 before initiating another transfer
*/
/*
* timer should be at least as long as longest start-stop interval on the bus
serial clock for i2c bus runs at 100khz by default and can run at 400khz for devices that support it
start-stop interval is about 9 serial clock cycles
400KHZ bit 0=100khz, 1=400khz
how many cycles at XTAL cycles/second = 9 cycles at 400k (or 100k) cycles/second
timeout = n i2c cycles / I2C cycles/sec = timeout seconds
timeout seconds * XTAL cycles/sec = XTAL cycles
9 / 400 (or 100) * (XTAL)
*/
BOOL
i2c_read(BYTE addr, WORD len, BYTE * buf)
{
BYTE tmp;
WORD cur_byte;
cancel_i2c_trans = FALSE;
//WORD timeout_cycles = (WORD)(9.0 * XTAL / I2CFREQ );
// 1. Set START=1. If BERR = 1, start timer*.
start:
CHECK_I2C_CANCEL();
cur_byte = 0;
I2CS |= bmSTART;
if (I2CS & bmBERR) {
i2c_printf("Woops, step1 BERR, need to do timeout\n");
delay(10); // NOTE way too long
goto start;
}
// 2. Write the 7-bit peripheral address and the direction bit (1 for a read) to I2DAT.
I2DAT = (addr << 1) | 1; // last 1 for read
// 3. Wait for DONE=1 or for timer to expire*. If BERR=1, go to step 1.
while (!(I2CS & bmDONE) && !cancel_i2c_trans);
CHECK_I2C_CANCEL();
if (I2CS & bmBERR)
goto start;
// 4. If ACK=0, set STOP=1 and go to step 15.
if (!(I2CS & bmACK)) {
I2CS |= bmSTOP;
while ((I2CS & bmSTOP) && !cancel_i2c_trans);
return FALSE;
}
// with only one byte to read, this needs set here.
// (In this case, the tmp read is the 2nd to last read)
if (len == 1)
I2CS |= bmLASTRD;
// 5. Read I2DAT to initiate the first burst of nine SCL pulses to clock in the first byte from the slave.
// Discard the value that was read from I2DAT.
tmp = I2DAT; // discard read
while (len > cur_byte + 1) { // reserve last byte read for after the loop
// 6. Wait for DONE=1. If BERR=1, go to step 1.
// 9. Wait for DONE=1. If BERR=1, go to step 1.
while (!(I2CS & bmDONE) && !cancel_i2c_trans);
CHECK_I2C_CANCEL();
if (I2CS & bmBERR)
goto start;
// 10. Before reading the second-to-last I2DAT byte, set LASTRD=1.
if (len == cur_byte + 2) // 2nd to last byte
I2CS |= bmLASTRD;
// 7. Read the just-received byte of data from I2DAT. This read also initiates the next read transfer.
// 11. Read the second-to-last byte from I2DAT. With LASTRD=1, this initiates the final byte read on
// the bus.
buf[cur_byte++] = I2DAT;
// 8. Repeat steps 6 and 7 for each byte until ready to read the second-to-last byte.
}
//12. Wait for DONE=1. If BERR=1, go to step 1.
while (!(I2CS & bmDONE) && !cancel_i2c_trans);
CHECK_I2C_CANCEL();
if (I2CS & bmBERR)
goto start;
// 13. Set STOP=1.
I2CS |= bmSTOP;
// 14. Read the final byte from I2DAT immediately (the next instruction) after setting the STOP bit. By
// reading I2DAT while the "stop" condition is being generated, the just-received data byte will be
// retrieved without initiating an extra read transaction (nine more SCL pulses) on the I²Cbus.
buf[cur_byte] = I2DAT; // use instead of buffer addressing so next instruction reads I2DAT
while ((I2CS & bmSTOP) && !cancel_i2c_trans);
CHECK_I2C_CANCEL();
return TRUE;
}
BOOL
eeprom_write(BYTE prom_addr, WORD addr, WORD length, BYTE * buf)
{
BYTE addr_len = 0;
// 1st bytes of buffer are address and next byte is value
BYTE data_buffer[3];
WORD cur_byte = 0;
#ifdef DEBUG_I2C
if (EEPROM_TWO_BYTE) {
i2c_printf("Two Byte EEProm Address detected.\n");
} else {
i2c_printf("Single Byte EEProm address detected.\n");
}
#endif
while (cur_byte < length) {
addr_len = 0;
if (EEPROM_TWO_BYTE) {
data_buffer[addr_len++] = MSB(addr);
}
data_buffer[addr_len++] = LSB(addr);
data_buffer[addr_len++] = buf[cur_byte++];
i2c_printf("%02x ", data_buffer[addr_len - 1]);
if (!i2c_write(prom_addr, addr_len, data_buffer, 0, NULL))
return FALSE;
++addr; // next byte goes to next address
}
return TRUE;
}
BOOL
eeprom_read(BYTE prom_addr, WORD addr, WORD length, BYTE * buf)
{
BYTE eeprom_addr[2];
BYTE addr_len = 0;
if (EEPROM_TWO_BYTE)
eeprom_addr[addr_len++] = MSB(addr);
eeprom_addr[addr_len++] = LSB(addr);
// write the address we want to read to the prom
//printf ("Starting Addr Write with addr len %d\n", addr_len);
if (!i2c_write(prom_addr, addr_len, eeprom_addr, 0, NULL))
return FALSE;
//printf ( "Starting read\n" );
if (!i2c_read(prom_addr, length, buf))
return FALSE;
return TRUE;
}

28
firmware/lib/int4av.a51 Normal file
View File

@@ -0,0 +1,28 @@
; Copyright (C) 2010 Ubixum, Inc.
;
; This library 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.1 of the License, or (at your option) any later version.
;
; This library 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 library; if not, write to the Free Software
; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
.module INT4AV ; jump table for int4AV (GPIF & endpoint interrupts)
.globl _INT4JT
.area INT4AV (ABS,OVR)
.org 0x53 ; where INT4 jumps to
_INT4AV = #. + 2
ljmp _INT2JT ; the addr gets replaced so this really goes to int4jt locations
.area INT4JT ( CODE )
_INT4JT: ; doesn't do anything but forces this module to be linked in if gpif macro used.

View File

@@ -0,0 +1,24 @@
/**
* Copyright (C) 2010 Ubixum, Inc.
*
* This library 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.1 of the License, or (at your option) any later version.
*
* This library 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 library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
**/
#include <autovector.h>
void
ep0ack_isr()
__interrupt EP0ACK_ISR {
}

View File

@@ -0,0 +1,24 @@
/**
* Copyright (C) 2010 Ubixum, Inc.
*
* This library 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.1 of the License, or (at your option) any later version.
*
* This library 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 library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
**/
#include <autovector.h>
void
ep0in_isr()
__interrupt EP0IN_ISR {
}

View File

@@ -0,0 +1,24 @@
/**
* Copyright (C) 2010 Ubixum, Inc.
*
* This library 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.1 of the License, or (at your option) any later version.
*
* This library 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 library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
**/
#include <autovector.h>
void
ep0out_isr()
__interrupt EP0OUT_ISR {
}

View File

@@ -0,0 +1,24 @@
/**
* Copyright (C) 2010 Ubixum, Inc.
*
* This library 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.1 of the License, or (at your option) any later version.
*
* This library 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 library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
**/
#include <autovector.h>
void
ep0ping_isr()
__interrupt EP0PING_ISR {
}

View File

@@ -0,0 +1,24 @@
/**
* Copyright (C) 2010 Ubixum, Inc.
*
* This library 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.1 of the License, or (at your option) any later version.
*
* This library 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 library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
**/
#include <autovector.h>
void
ep1in_isr()
__interrupt EP1IN_ISR {
}

View File

@@ -0,0 +1,24 @@
/**
* Copyright (C) 2010 Ubixum, Inc.
*
* This library 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.1 of the License, or (at your option) any later version.
*
* This library 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 library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
**/
#include <autovector.h>
void
ep1out_isr()
__interrupt EP1OUT_ISR {
}

View File

@@ -0,0 +1,24 @@
/**
* Copyright (C) 2010 Ubixum, Inc.
*
* This library 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.1 of the License, or (at your option) any later version.
*
* This library 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 library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
**/
#include <autovector.h>
void
ep1ping_isr()
__interrupt EP1PING_ISR {
}

View File

@@ -0,0 +1,24 @@
/**
* Copyright (C) 2010 Ubixum, Inc.
*
* This library 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.1 of the License, or (at your option) any later version.
*
* This library 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 library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
**/
#include <autovector.h>
void
ep2_isr()
__interrupt EP2_ISR {
}

View File

@@ -0,0 +1,24 @@
/**
* Copyright (C) 2010 Ubixum, Inc.
*
* This library 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.1 of the License, or (at your option) any later version.
*
* This library 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 library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
**/
#include <autovector.h>
void
ep2ef_isr()
__interrupt EP2EF_ISR {
}

View File

@@ -0,0 +1,24 @@
/**
* Copyright (C) 2010 Ubixum, Inc.
*
* This library 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.1 of the License, or (at your option) any later version.
*
* This library 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 library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
**/
#include <autovector.h>
void
ep2ff_isr()
__interrupt EP2FF_ISR {
}

View File

@@ -0,0 +1,24 @@
/**
* Copyright (C) 2010 Ubixum, Inc.
*
* This library 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.1 of the License, or (at your option) any later version.
*
* This library 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 library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
**/
#include <autovector.h>
void
ep2isoerr_isr()
__interrupt EP2ISOERR_ISR {
}

View File

@@ -0,0 +1,24 @@
/**
* Copyright (C) 2010 Ubixum, Inc.
*
* This library 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.1 of the License, or (at your option) any later version.
*
* This library 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 library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
**/
#include <autovector.h>
void
ep2pf_isr()
__interrupt EP2PF_ISR {
}

View File

@@ -0,0 +1,24 @@
/**
* Copyright (C) 2010 Ubixum, Inc.
*
* This library 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.1 of the License, or (at your option) any later version.
*
* This library 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 library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
**/
#include <autovector.h>
void
ep2ping_isr()
__interrupt EP2PING_ISR {
}

View File

@@ -0,0 +1,24 @@
/**
* Copyright (C) 2010 Ubixum, Inc.
*
* This library 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.1 of the License, or (at your option) any later version.
*
* This library 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 library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
**/
#include <autovector.h>
void
ep4_isr()
__interrupt EP4_ISR {
}

View File

@@ -0,0 +1,24 @@
/**
* Copyright (C) 2010 Ubixum, Inc.
*
* This library 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.1 of the License, or (at your option) any later version.
*
* This library 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 library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
**/
#include <autovector.h>
void
ep4ef_isr()
__interrupt EP4EF_ISR {
}

View File

@@ -0,0 +1,24 @@
/**
* Copyright (C) 2010 Ubixum, Inc.
*
* This library 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.1 of the License, or (at your option) any later version.
*
* This library 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 library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
**/
#include <autovector.h>
void
ep4ff_isr()
__interrupt EP4FF_ISR {
}

View File

@@ -0,0 +1,24 @@
/**
* Copyright (C) 2010 Ubixum, Inc.
*
* This library 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.1 of the License, or (at your option) any later version.
*
* This library 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 library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
**/
#include <autovector.h>
void
ep4isoerr_isr()
__interrupt EP4ISOERR_ISR {
}

View File

@@ -0,0 +1,24 @@
/**
* Copyright (C) 2010 Ubixum, Inc.
*
* This library 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.1 of the License, or (at your option) any later version.
*
* This library 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 library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
**/
#include <autovector.h>
void
ep4pf_isr()
__interrupt EP4PF_ISR {
}

View File

@@ -0,0 +1,24 @@
/**
* Copyright (C) 2010 Ubixum, Inc.
*
* This library 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.1 of the License, or (at your option) any later version.
*
* This library 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 library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
**/
#include <autovector.h>
void
ep4ping_isr()
__interrupt EP4PING_ISR {
}

View File

@@ -0,0 +1,24 @@
/**
* Copyright (C) 2010 Ubixum, Inc.
*
* This library 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.1 of the License, or (at your option) any later version.
*
* This library 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 library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
**/
#include <autovector.h>
void
ep6_isr()
__interrupt EP6_ISR {
}

View File

@@ -0,0 +1,24 @@
/**
* Copyright (C) 2010 Ubixum, Inc.
*
* This library 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.1 of the License, or (at your option) any later version.
*
* This library 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 library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
**/
#include <autovector.h>
void
ep6ef_isr()
__interrupt EP6EF_ISR {
}

View File

@@ -0,0 +1,24 @@
/**
* Copyright (C) 2010 Ubixum, Inc.
*
* This library 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.1 of the License, or (at your option) any later version.
*
* This library 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 library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
**/
#include <autovector.h>
void
ep6ff_isr()
__interrupt EP6FF_ISR {
}

View File

@@ -0,0 +1,24 @@
/**
* Copyright (C) 2010 Ubixum, Inc.
*
* This library 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.1 of the License, or (at your option) any later version.
*
* This library 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 library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
**/
#include <autovector.h>
void
ep6isoerr_isr()
__interrupt EP6ISOERR_ISR {
}

View File

@@ -0,0 +1,24 @@
/**
* Copyright (C) 2010 Ubixum, Inc.
*
* This library 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.1 of the License, or (at your option) any later version.
*
* This library 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 library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
**/
#include <autovector.h>
void
ep6pf_isr()
__interrupt EP6PF_ISR {
}

View File

@@ -0,0 +1,24 @@
/**
* Copyright (C) 2010 Ubixum, Inc.
*
* This library 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.1 of the License, or (at your option) any later version.
*
* This library 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 library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
**/
#include <autovector.h>
void
ep6ping_isr()
__interrupt EP6PING_ISR {
}

View File

@@ -0,0 +1,24 @@
/**
* Copyright (C) 2010 Ubixum, Inc.
*
* This library 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.1 of the License, or (at your option) any later version.
*
* This library 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 library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
**/
#include <autovector.h>
void
ep8_isr()
__interrupt EP8_ISR {
}

View File

@@ -0,0 +1,24 @@
/**
* Copyright (C) 2010 Ubixum, Inc.
*
* This library 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.1 of the License, or (at your option) any later version.
*
* This library 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 library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
**/
#include <autovector.h>
void
ep8ef_isr()
__interrupt EP8EF_ISR {
}

View File

@@ -0,0 +1,24 @@
/**
* Copyright (C) 2010 Ubixum, Inc.
*
* This library 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.1 of the License, or (at your option) any later version.
*
* This library 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 library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
**/
#include <autovector.h>
void
ep8ff_isr()
__interrupt EP8FF_ISR {
}

View File

@@ -0,0 +1,24 @@
/**
* Copyright (C) 2010 Ubixum, Inc.
*
* This library 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.1 of the License, or (at your option) any later version.
*
* This library 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 library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
**/
#include <autovector.h>
void
ep8isoerr_isr()
__interrupt EP8ISOERR_ISR {
}

View File

@@ -0,0 +1,24 @@
/**
* Copyright (C) 2010 Ubixum, Inc.
*
* This library 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.1 of the License, or (at your option) any later version.
*
* This library 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 library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
**/
#include <autovector.h>
void
ep8pf_isr()
__interrupt EP8PF_ISR {
}

View File

@@ -0,0 +1,24 @@
/**
* Copyright (C) 2010 Ubixum, Inc.
*
* This library 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.1 of the License, or (at your option) any later version.
*
* This library 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 library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
**/
#include <autovector.h>
void
ep8ping_isr()
__interrupt EP8PING_ISR {
}

View File

@@ -0,0 +1,24 @@
/**
* Copyright (C) 2010 Ubixum, Inc.
*
* This library 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.1 of the License, or (at your option) any later version.
*
* This library 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 library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
**/
#include <autovector.h>
void
errlimit_isr()
__interrupt ERRLIMIT_ISR {
}

View File

@@ -0,0 +1,24 @@
/**
* Copyright (C) 2010 Ubixum, Inc.
*
* This library 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.1 of the License, or (at your option) any later version.
*
* This library 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 library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
**/
#include <autovector.h>
void
gpifdone_isr()
__interrupt GPIFDONE_ISR {
}

View File

@@ -0,0 +1,24 @@
/**
* Copyright (C) 2010 Ubixum, Inc.
*
* This library 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.1 of the License, or (at your option) any later version.
*
* This library 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 library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
**/
#include <autovector.h>
void
gpifwf_isr()
__interrupt GPIFWF_ISR {
}

View File

@@ -0,0 +1,24 @@
/**
* Copyright (C) 2010 Ubixum, Inc.
*
* This library 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.1 of the License, or (at your option) any later version.
*
* This library 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 library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
**/
#include <autovector.h>
void
hispeed_isr()
__interrupt HISPEED_ISR {
}

View File

@@ -0,0 +1,24 @@
/**
* Copyright (C) 2010 Ubixum, Inc.
*
* This library 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.1 of the License, or (at your option) any later version.
*
* This library 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 library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
**/
#include <autovector.h>
void
ibn_isr()
__interrupt IBN_ISR {
}

View File

@@ -0,0 +1,24 @@
/**
* Copyright (C) 2010 Ubixum, Inc.
*
* This library 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.1 of the License, or (at your option) any later version.
*
* This library 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 library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
**/
#include <autovector.h>
void
sof_isr()
__interrupt SOF_ISR {
}

View File

@@ -0,0 +1,24 @@
/**
* Copyright (C) 2010 Ubixum, Inc.
*
* This library 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.1 of the License, or (at your option) any later version.
*
* This library 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 library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
**/
#include <autovector.h>
void
spare_isr()
__interrupt RESERVED_ISR {
}

View File

@@ -0,0 +1,24 @@
/**
* Copyright (C) 2010 Ubixum, Inc.
*
* This library 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.1 of the License, or (at your option) any later version.
*
* This library 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 library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
**/
#include <autovector.h>
void
sudav_isr()
__interrupt SUDAV_ISR {
}

View File

@@ -0,0 +1,24 @@
/**
* Copyright (C) 2010 Ubixum, Inc.
*
* This library 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.1 of the License, or (at your option) any later version.
*
* This library 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 library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
**/
#include <autovector.h>
void
suspend_isr()
__interrupt SUSPEND_ISR {
}

View File

@@ -0,0 +1,24 @@
/**
* Copyright (C) 2010 Ubixum, Inc.
*
* This library 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.1 of the License, or (at your option) any later version.
*
* This library 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 library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
**/
#include <autovector.h>
void
sutok_isr()
__interrupt SUTOK_ISR {
}

View File

@@ -0,0 +1,24 @@
/**
* Copyright (C) 2010 Ubixum, Inc.
*
* This library 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.1 of the License, or (at your option) any later version.
*
* This library 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 library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
**/
#include <autovector.h>
void
usbreset_isr()
__interrupt USBRESET_ISR {
}

102
firmware/lib/serial.c Normal file
View File

@@ -0,0 +1,102 @@
/**
* Copyright (C) 2009 Ubixum, Inc.
*
* This library 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.1 of the License, or (at your option) any later version.
*
* This library 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 library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
**/
#include <fx2regs.h>
#include <fx2macros.h>
#include <serial.h>
/**
* using the comp port implies that timer 2 will be used as
* a baud rate generator. (Don't use timer 2)
**/
void
sio0_init(WORD baud_rate)
__critical
{ // baud_rate max should be 57600 since int=2 bytes
WORD hl; // hl value for reload
BYTE mult; // multiplier for clock speed
DWORD tmp; // scratch for mult/divide
// 0 = 12mhz, 1=24mhz, 2=48mhz
mult = CPUFREQ == CLK_12M ? 1 : CPUFREQ == CLK_24M ? 2 : 4; // since only 3 clock speeds, fast switch instead of doing 2^clock speed pow(2,clkspd)
// set the clock rate
// use clock 2
RCLK = 1;
TCLK = 1;
// RCAP2H:L = 0xFFFF - CLKOUT / 32 x baud_rate
// in order to round to nearest value..
// tmp * 2 // double
// tmp / rate // do the divide
// tmp + 1 // add one (which is like adding 1/2)
// tmp / 2 // back to original rounded
tmp = mult * 375000L * 2;
tmp /= baud_rate;
tmp += 1;
tmp /= 2;
hl = 0xFFFF - (WORD) tmp;
RCAP2H = MSB(hl);
// seems that the 24/48mhz calculations are always one less than suggested values
// trm table 14-16
RCAP2L = LSB(hl) + (mult > 0 ? 1 : 0);
TR2 = 1; // start the timer
// set up the serial port
SM0 = 0;
SM1 = 1; // serial mode 1 (asyncronous)
SM2 = 0; // has to do with receiving
REN = 1; // to enable receiving
PCON |= 0x80; // SET SMOD0, baud rate doubler
TI = 1; // we send initial byte
}
char
getchar()
{
char c;
while (!RI);
c = SBUF0;
RI = 0;
return c;
}
void
_transchar(char c)
{
while (!TI); // wait for TI=1
TI = 0;
SBUF0 = c;
}
void
putchar(char c)
{
if (c == '\n')
_transchar('\r'); // transmit \r\n
_transchar(c);
if (c == '\r')
_transchar('\n'); // transmit \r\n
}

396
firmware/lib/setupdat.c Normal file
View File

@@ -0,0 +1,396 @@
/**
* Copyright (C) 2009 Ubixum, Inc.
*
* This library 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.1 of the License, or (at your option) any later version.
*
* This library 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 library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
**/
//#define DEBUG_SETUPDAT
#ifdef DEBUG_SETUPDAT
#include <stdio.h> // NOTE this needs deleted
#else
#define printf(...)
#define NULL (void*)0;
#endif
#include <fx2regs.h>
#include <fx2macros.h>
#include <eputils.h>
#include <setupdat.h>
extern BOOL handle_vendorcommand(BYTE cmd);
extern BOOL handle_set_configuration(BYTE cfg);
extern BOOL handle_get_interface(BYTE ifc, BYTE * alt_ifc);
extern BOOL handle_set_interface(BYTE ifc, BYTE alt_ifc);
extern BYTE handle_get_configuration();
extern BOOL handle_set_configuration(BYTE cfg);
extern void handle_reset_ep(BYTE ep);
/**
* Predefs for handlers
**/
// GET_STATUS,
BOOL handle_get_status();
// CLEAR_FEATURE,
BOOL handle_clear_feature();
// 0x02 is reserved
// SET_FEATURE=0x03,
BOOL handle_set_feature();
// 0x04 is reserved
// SET_ADDRESS=0x05, // this is handled by EZ-USB core unless RENUM=0
// GET_DESCRIPTOR,
void handle_get_descriptor();
// SET_DESCRIPTOR,
// GET_CONFIGURATION, // handled by callback
// SET_CONFIGURATION, // handled by callback
// GET_INTERFACE, // handled by callback
// SET_INTERFACE, // handled by callback
// SYNC_FRAME // not yet implemented
/*
TRM 2.2
Setup Token ->
data transfer ->
handshake
*/
void
handle_setupdata()
{
//printf ( "Handle setupdat: %02x\n", SETUPDAT[1] );
switch (SETUPDAT[1]) {
case GET_STATUS:
if (!handle_get_status())
STALLEP0();
break;
case CLEAR_FEATURE:
if (!handle_clear_feature()) {
STALLEP0();
}
break;
case SET_FEATURE:
if (!handle_set_feature()) {
STALLEP0();
}
break;
case GET_DESCRIPTOR:
handle_get_descriptor();
break;
case GET_CONFIGURATION:
EP0BUF[0] = handle_get_configuration();
EP0BCH = 0;
EP0BCL = 1;
break;
case SET_CONFIGURATION:
// user callback
if (!handle_set_configuration(SETUPDAT[2])) {
STALLEP0();
}
break;
case GET_INTERFACE:
{
BYTE alt_ifc;
if (!handle_get_interface(SETUPDAT[4], &alt_ifc)) {
STALLEP0();
} else {
EP0BUF[0] = alt_ifc;
EP0BCH = 0;
EP0BCL = 1;
}
}
break;
case SET_INTERFACE:
// user callback
if (!handle_set_interface(SETUPDAT[4], SETUPDAT[2])) {
STALLEP0();
}
break;
default:
if (!handle_vendorcommand(SETUPDAT[1])) {
printf("Unhandled Vendor Command: %02x\n", SETUPDAT[1]);
STALLEP0();
}
}
// do the handshake
EP0CS |= bmHSNAK;
}
__xdata BYTE *
ep_addr(BYTE ep)
{ // bit 8 of ep_num is the direction
BYTE ep_num = ep & ~0x80; // mask the direction
switch (ep_num) {
case 0:
return &EP0CS;
case 1:
return ep & 0x80 ? &EP1INCS : &EP1OUTCS;
case 2:
return &EP2CS;
case 4:
return &EP4CS;
case 6:
return &EP6CS;
case 8:
return &EP8CS;
default:
return NULL;
}
}
// Get status has three request types
#define GS_DEVICE 0x80
#define GS_INTERFACE 0x81
#define GS_ENDPOINT 0x82
volatile BOOL self_powered = FALSE;
volatile BOOL remote_wakeup_allowed = FALSE;
BOOL
handle_get_status()
{
switch (SETUPDAT[0]) {
// case 0: // sometimes we get a 0 status too
case GS_INTERFACE:
EP0BUF[0] = 0;
EP0BUF[1] = 0;
EP0BCH = 0;
EP0BCL = 2;
break;
case GS_DEVICE:
// two byte response
// byte 0 bit 0 = self powered bit 1 = remote wakeup
EP0BUF[0] = (remote_wakeup_allowed << 1) | self_powered;
// byte 1 = 0
EP0BUF[1] = 0;
EP0BCH = 0;
EP0BCL = 2;
break;
case GS_ENDPOINT:
{
__xdata BYTE *pep = ep_addr(SETUPDAT[4]);
if (!pep)
return FALSE;
// byte 0 bit 0 = stall bit
EP0BUF[0] = *pep & bmEPSTALL ? 1 : 0;
EP0BUF[1] = 0;
EP0BCH = 0;
EP0BCL = 2;
}
break;
default:
printf("Unexpected Get Status: %02x\n", SETUPDAT[0]);
return FALSE;
}
return TRUE;
}
#define GF_DEVICE 0
#define GF_ENDPOINT 2
BOOL
handle_clear_feature()
{
//printf ( "Clear Feature\n" );
switch (SETUPDAT[0]) {
case GF_DEVICE:
if (SETUPDAT[2] == 1) {
remote_wakeup_allowed = FALSE;
break;
}
return FALSE;
case GF_ENDPOINT:
if (SETUPDAT[2] == 0) { // ep stall feature
__xdata BYTE *pep = ep_addr(SETUPDAT[4]);
printf("unstall endpoint %02X\n", SETUPDAT[4]);
*pep &= ~bmEPSTALL;
RESETTOGGLE(SETUPDAT[4]);
} else {
printf("unsupported ep feature %02x", SETUPDAT[2]);
return FALSE;
}
break;
default:
return handle_vendorcommand(SETUPDAT[1]);
}
return TRUE;
}
BOOL
handle_set_feature()
{
printf("Set Feature %02x\n", SETUPDAT[0]);
switch (SETUPDAT[0]) {
case GF_DEVICE:
if (SETUPDAT[2] == 2)
break; // this is TEST_MODE and we simply need to return the handshake
if (SETUPDAT[2] == 1) {
remote_wakeup_allowed = TRUE;
break;
}
return FALSE;
case GF_ENDPOINT:
if (SETUPDAT[2] == 0) { // ep stall feature
// set TRM 2.3.2
// stall and endpoint
__xdata BYTE *pep = ep_addr(SETUPDAT[4]);
printf("Stall ep %d\n", SETUPDAT[4]);
if (!pep) {
return FALSE;
}
*pep |= bmEPSTALL;
// should now reset data toggles
// write ep+dir to TOGCTL
RESETTOGGLE(SETUPDAT[4]);
// restore stalled ep to default condition
// NOTE
//handle_reset_ep(SETUPDAT[4]);
} else {
printf("unsupported ep feature %02x\n", SETUPDAT[2]);
return FALSE;
}
break;
default:
return handle_vendorcommand(SETUPDAT[1]);
}
return TRUE;
}
/* these are devined in dscr.asm
and need to be customized then
linked in by the firmware manually */
extern __code WORD dev_dscr;
extern __code WORD dev_qual_dscr;
extern __code WORD highspd_dscr;
extern __code WORD fullspd_dscr;
extern __code WORD dev_strings;
WORD pDevConfig = (WORD) & fullspd_dscr;
WORD pOtherConfig = (WORD) & highspd_dscr;
void
handle_hispeed(BOOL highspeed)
{
__critical {
printf("Hi Speed or reset Interrupt\n");
if (highspeed) {
pDevConfig = (WORD) & highspd_dscr;
pOtherConfig = (WORD) & fullspd_dscr;
} else {
pDevConfig = (WORD) & fullspd_dscr;
pOtherConfig = (WORD) & highspd_dscr;
}
}
}
/**
* Handle:
* Device Descriptor
* Device Qualifier
* Configuration
* String
* Other-Speed
**/
void
handle_get_descriptor()
{
//printf ( "Get Descriptor\n" );
switch (SETUPDAT[3]) {
case DSCR_DEVICE_TYPE:
printf("Get Device Config\n");
SUDPTRH = MSB((WORD) & dev_dscr);
SUDPTRL = LSB((WORD) & dev_dscr);
break;
case DSCR_CONFIG_TYPE:
// get the config descriptor
printf("Get Config Descriptor\n");
SUDPTRH = MSB(pDevConfig);
SUDPTRL = LSB(pDevConfig);
break;
case DSCR_STRING_TYPE:
//printf ( "Get String Descriptor idx: %d\n", SETUPDAT[2] );
{
STRING_DSCR *pStr = (STRING_DSCR *) & dev_strings;
// pStr points to string 0
BYTE idx = SETUPDAT[2];
BYTE cur = 0; // current check
do {
if (idx == cur++)
break;
//printf ( "Length of pStr: %d\n", pStr->dsc_len );
//printf ( "pstr: %04x to ", pStr );
pStr =
(STRING_DSCR *) ((BYTE *) pStr +
pStr->dsc_len);
//printf ( "%04x\n" , pStr );
if (pStr->dsc_type != DSCR_STRING_TYPE)
pStr = NULL;
} while (pStr && cur <= idx);
if (pStr) {
/* BYTE i;
//printf ( "found str: '");
for (i=0;i<pStr->dsc_len-2;++i) {
printf ( i%2==0?"%c":"%02x", *((BYTE*)(&pStr->pstr)+i));
} printf ( "\n"); */
SUDPTRH = MSB((WORD) pStr);
SUDPTRL = LSB((WORD) pStr);
//SUDPTRH = MSB((WORD)&dev_strings);
//SUDPTRL = LSB((WORD)&dev_strings);
} else {
STALLEP0();
}
}
break;
case DSCR_DEVQUAL_TYPE:
printf("Get Device Qualifier Descriptor\n");
// assumes this is a high speed capable device
SUDPTRH = MSB((WORD) & dev_qual_dscr);
SUDPTRL = LSB((WORD) & dev_qual_dscr);
break;
case DSCR_OTHERSPD_TYPE:
printf("Other Speed Descriptor\n");
SUDPTRH = MSB(pOtherConfig);
SUDPTRL = LSB(pOtherConfig);
break;
default:
printf("Unhandled Get Descriptor: %02x\n", SETUPDAT[3]);
STALLEP0();
}
}

125
firmware/lib/usbav.a51 Normal file
View File

@@ -0,0 +1,125 @@
; Copyright (C) 2010 Ubixum, Inc.
;
; This library 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.1 of the License, or (at your option) any later version.
;
; This library 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 library; if not, write to the Free Software
; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
.module INT2AV ; jump table for usb auto vector
.globl _INT2JT ; defined as global so this assembly gets included in project
.area INT2AV (ABS,OVR)
.org 0x43 ; this is where USBINT ( interrupt 8 ) jumps to
_INT2AV = #. + 2 ; two bytes for ljmp (auto set by INT2IVEC)
ljmp _INT2JT
; INT2 Jump Table
.area INT2JT ( CODE )
;.org 0x1A00 ; needs to be on a page boundary
_INT2JT:
ljmp _sudav_isr
.db 0
ljmp _sof_isr
.db 0
ljmp _sutok_isr
.db 0
ljmp _suspend_isr
.db 0
ljmp _usbreset_isr
.db 0
ljmp _hispeed_isr
.db 0
ljmp _ep0ack_isr
.db 0
ljmp _spare_isr
.db 0
ljmp _ep0in_isr
.db 0
ljmp _ep0out_isr
.db 0
ljmp _ep1in_isr
.db 0
ljmp _ep1out_isr
.db 0
ljmp _ep2_isr
.db 0
ljmp _ep4_isr
.db 0
ljmp _ep6_isr
.db 0
ljmp _ep8_isr
.db 0
ljmp _ibn_isr
.db 0
ljmp _spare_isr
.db 0
ljmp _ep0ping_isr
.db 0
ljmp _ep1ping_isr
.db 0
ljmp _ep2ping_isr
.db 0
ljmp _ep4ping_isr
.db 0
ljmp _ep6ping_isr
.db 0
ljmp _ep8ping_isr
.db 0
ljmp _errlimit_isr
.db 0
ljmp _spare_isr
.db 0
ljmp _spare_isr
.db 0
ljmp _spare_isr
.db 0
ljmp _ep2isoerr_isr
.db 0
ljmp _ep4isoerr_isr
.db 0
ljmp _ep6isoerr_isr
.db 0
ljmp _ep8isoerr_isr
.db 0
; INT4JT
ljmp _ep2pf_isr
.db 0
ljmp _ep4pf_isr
.db 0
ljmp _ep6pf_isr
.db 0
ljmp _ep8pf_isr
.db 0
ljmp _ep2ef_isr
.db 0
ljmp _ep4ef_isr
.db 0
ljmp _ep6ef_isr
.db 0
ljmp _ep8ef_isr
.db 0
ljmp _ep2ff_isr
.db 0
ljmp _ep4ff_isr
.db 0
ljmp _ep6ff_isr
.db 0
ljmp _ep8ff_isr
.db 0
ljmp _gpifdone_isr
.db 0
ljmp _gpifwf_isr
.db 0