fx2/firmware/fw/device.c

128 lines
3.0 KiB
C

#include <fx2macros.h>
#include <delay.h>
#ifdef DEBUG_FIRMWARE
#include <stdio.h>
#else
#define printf(...)
#endif
//************************** Configuration Handlers *****************************
// change to support as many interfaces as you need
//volatile xdata BYTE interface=0;
//volatile xdata BYTE alt=0; // alt interface
// set *alt_ifc to the current alt interface for ifc
BOOL
handle_get_interface(BYTE ifc, BYTE * alt_ifc)
{
// *alt_ifc=alt;
return TRUE;
}
// return TRUE if you set the interface requested
// NOTE this function should reconfigure and reset the endpoints
// according to the interface descriptors you provided.
BOOL
handle_set_interface(BYTE ifc, BYTE alt_ifc)
{
printf("Set Interface.\n");
//interface=ifc;
//alt=alt_ifc;
return TRUE;
}
// handle getting and setting the configuration
// 1 is the default. If you support more than one config
// keep track of the config number and return the correct number
// config numbers are set int the dscr file.
//volatile BYTE config=1;
BYTE
handle_get_configuration()
{
return 1;
}
// NOTE changing config requires the device to reset all the endpoints
BOOL
handle_set_configuration(BYTE cfg)
{
printf("Set Configuration.\n");
//config=cfg;
return TRUE;
}
//******************* VENDOR COMMAND HANDLERS **************************
BOOL
handle_vendorcommand(BYTE cmd)
{
switch (cmd) {
// case VC_EPSTAT:
// break;
default:
printf ( "Need to implement vendor command: %02x\n", cmd );
}
/* not handled by handlers */
return FALSE;
}
//******************** INIT ***********************
void
main_init()
{
SYNCDELAY4; REVCTL = 0x03;
/* set register default values */
SYNCDELAY4; PINFLAGSAB = 0x00;
SYNCDELAY4; PINFLAGSCD = 0x00;
SYNCDELAY4; FIFOPINPOLAR = 0x00;
/* SLAVE FIFO (set bmIFCLKPOL to ensure better setup times) */
SYNCDELAY4; IFCONFIG = bmIFCLKSRC | bm3048MHZ | bmIFCLKOE | (bmIFCFG1 | bmIFCFG0);
/* only valid endpoints are 2/6 */
/* EP2 is DIR=OUT, TYPE=BULK, SIZE=512, BUF=2x (HOST->DEVICE) */
SYNCDELAY4; EP2CFG = 0xa2;
/* slave fifo configuration 8bit, autoout */
//SYNCDELAY4; EP2FIFOCFG &= ~bmWORDWIDE;
SYNCDELAY4; EP2FIFOCFG = bmAUTOOUT;
SYNCDELAY4; EP2AUTOINLENH = 0x00;
SYNCDELAY4; EP2AUTOINLENL = 0x00;
/* EP6 is DIR=IN, TYPE=BULK, SIZE=512, BUF=2x (DEVICE->HOST) */
SYNCDELAY4; EP6CFG = 0xe2;
/* slave fifo configuration 8bit, autoin, zero length */
//SYNCDELAY4; EP6FIFOCFG &= ~bmWORDWIDE;
SYNCDELAY4; EP6FIFOCFG = bmAUTOIN | bmZEROLENIN;
/* AUTOIN packet length (512byte) */
SYNCDELAY4; EP6AUTOINLENH = 0x02;
SYNCDELAY4; EP6AUTOINLENL = 0x00;
/* unused fifos */
SYNCDELAY4; EP4FIFOCFG = 0;
SYNCDELAY4; EP8FIFOCFG = 0;
/* deactive other endpoints */
SYNCDELAY4; EP1INCFG &= ~bmVALID;
SYNCDELAY4; EP1OUTCFG &= ~bmVALID;
SYNCDELAY4; EP4CFG &= ~bmVALID;
SYNCDELAY4; EP8CFG &= ~bmVALID;
/* Reset the FIFO */
SYNCDELAY4; FIFORESET = 0x80;
SYNCDELAY4; FIFORESET = 0x82;
SYNCDELAY4; FIFORESET = 0x84;
SYNCDELAY4; FIFORESET = 0x86;
SYNCDELAY4; FIFORESET = 0x00;
SYNCDELAY4;
printf("Initialization Done.\n");
}
void
main_loop()
{
// do some work
}