* 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
+ * License along with this library; if not, see <http://www.gnu.org/licenses/>.
*/
#include <fx2macros.h>
#include <delay.h>
#include <setupdat.h>
+#define SET_ANALOG_MODE()
+
+#define SET_COUPLING(x)
+
+#define SET_CALIBRATION_PULSE(x)
+
+/* Toggle the 1kHz calibration pin, only accurate up to ca. 8MHz. */
+#define TOGGLE_CALIBRATION_PIN() PA7 = !PA7
+
+#define LED_CLEAR() PC0 = 1; PC1 = 1;
+#define LED_GREEN() PC0 = 1; PC1 = 0;
+#define LED_RED() PC0 = 0; PC1 = 1;
+
+/* CTLx pin index (IFCLK, ADC clock input). */
+#define CTL_BIT 2
+
+#define OUT0 ((1 << CTL_BIT) << 4) /* OEx = 1, CTLx = 0 */
+#define OE_CTL (((1 << CTL_BIT) << 4) | (1 << CTL_BIT)) /* OEx = CTLx = 1 */
+
/* Change to support as many interfaces as you need. */
-BYTE altiface = 0;
+static BYTE altiface = 0;
-volatile WORD ledcounter = 0;
+static volatile WORD ledcounter = 0;
-volatile __bit dosud = FALSE;
-volatile __bit dosuspend = FALSE;
+static volatile __bit dosud = FALSE;
+static volatile __bit dosuspend = FALSE;
extern __code BYTE highspd_dscr;
extern __code BYTE fullspd_dscr;
void timer2_isr(void) __interrupt TF2_ISR
{
- PA7 = !PA7;
- if (ledcounter) {
- if (--ledcounter == 0) {
- /* Clear LED. */
- PC0 = 1;
- PC1 = 1;
- }
- }
+ TOGGLE_CALIBRATION_PIN();
+
+ if (ledcounter && (--ledcounter == 0))
+ LED_CLEAR();
+
TF2 = 0;
}
* both channels and then we mask it out to only affect the channel currently
* requested.
*/
-BOOL set_voltage(BYTE channel, BYTE val)
+static BOOL set_voltage(BYTE channel, BYTE val)
{
BYTE bits, mask;
return TRUE;
}
-BOOL set_numchannels(BYTE numchannels)
+/**
+ * Each LSB in the nibble of the byte controls the coupling per channel.
+ *
+ * Setting PE3 disables AC coupling capacitor on CH0.
+ * Setting PE0 disables AC coupling capacitor on CH1.
+ */
+static void set_coupling(BYTE coupling_cfg)
+{
+ if (coupling_cfg & 0x01)
+ IOE |= 0x08;
+ else
+ IOE &= ~0x08;
+
+ if (coupling_cfg & 0x10)
+ IOE |= 0x01;
+ else
+ IOE &= ~0x01;
+}
+
+static BOOL set_numchannels(BYTE numchannels)
{
if (numchannels == 1 || numchannels == 2) {
BYTE fifocfg = 7 + numchannels;
return FALSE;
}
-void clear_fifo(void)
+static void clear_fifo(void)
{
GPIFABORT = 0xff;
SYNCDELAY3;
FIFORESET = 0;
}
-void stop_sampling(void)
+static void stop_sampling(void)
{
GPIFABORT = 0xff;
SYNCDELAY3;
INPKTEND = (altiface == 0) ? 6 : 2;
}
-void start_sampling(void)
+static void start_sampling(void)
{
int i;
+ SET_ANALOG_MODE();
+
clear_fifo();
for (i = 0; i < 1000; i++);
GPIFTCB0 = 0;
GPIFTRIG = (altiface == 0) ? 6 : 4;
- /* Set green LED, don't clear LED. */
+ /* Set green LED, don't clear LED afterwards (ledcounter = 0). */
+ LED_GREEN();
ledcounter = 0;
- PC0 = 1;
- PC1 = 0;
}
-void select_interface(BYTE alt)
+static void select_interface(BYTE alt)
{
const BYTE *pPacketSize = \
- (USBCS & bmHSM ? &highspd_dscr : &fullspd_dscr)
+ ((USBCS & bmHSM) ? &highspd_dscr : &fullspd_dscr)
+ (9 + (16 * alt) + 9 + 4);
altiface = alt;
}
}
-const struct samplerate_info {
+static const struct samplerate_info {
BYTE rate;
BYTE wait0;
BYTE wait1;
BYTE out0;
BYTE ifcfg;
} samplerates[] = {
- { 48,0x80, 0, 3, 0, 0x00, 0xea },
- { 30,0x80, 0, 3, 0, 0x00, 0xaa },
- { 24, 1, 0, 2, 1, 0x40, 0xca },
- { 16, 1, 1, 2, 0, 0x40, 0xca },
- { 12, 2, 1, 2, 0, 0x40, 0xca },
- { 8, 3, 2, 2, 0, 0x40, 0xca },
- { 4, 6, 5, 2, 0, 0x40, 0xca },
- { 2, 12, 11, 2, 0, 0x40, 0xca },
- { 1, 24, 23, 2, 0, 0x40, 0xca },
- { 50, 48, 47, 2, 0, 0x40, 0xca },
- { 20, 120, 119, 2, 0, 0x40, 0xca },
- { 10, 240, 239, 2, 0, 0x40, 0xca },
+ { 48, 0x80, 0, 3, 0, 0x00, 0xea },
+ { 30, 0x80, 0, 3, 0, 0x00, 0xaa },
+ { 24, 1, 0, 2, 1, OUT0, 0xca },
+ { 16, 1, 1, 2, 0, OUT0, 0xca },
+ { 12, 2, 1, 2, 0, OUT0, 0xca },
+ { 8, 3, 2, 2, 0, OUT0, 0xca },
+ { 4, 6, 5, 2, 0, OUT0, 0xca },
+ { 2, 12, 11, 2, 0, OUT0, 0xca },
+ { 1, 24, 23, 2, 0, OUT0, 0xca },
+ { 50, 48, 47, 2, 0, OUT0, 0xca },
+ { 20, 120, 119, 2, 0, OUT0, 0xca },
+ { 10, 240, 239, 2, 0, OUT0, 0xca },
};
-BOOL set_samplerate(BYTE rate)
+static BOOL set_samplerate(BYTE rate)
{
BYTE i = 0;
IFCONFIG = samplerates[i].ifcfg;
AUTOPTRSETUP = 7;
- AUTOPTRH2 = 0xE4;
+ AUTOPTRH2 = 0xE4; /* 0xE400: GPIF waveform descriptor 0. */
AUTOPTRL2 = 0x00;
/*
* The program for low-speed, e.g. 1 MHz, is:
- * wait 24, CTL2=0, FIFO
- * wait 23, CTL2=1
- * jump 0, CTL2=1
+ * wait 24, CTLx=0, FIFO
+ * wait 23, CTLx=1
+ * jump 0, CTLx=1
*
* The program for 24 MHz is:
- * wait 1, CTL2=0, FIFO
- * jump 0, CTL2=1
+ * wait 1, CTLx=0, FIFO
+ * jump 0, CTLx=1
*
* The program for 30/48 MHz is:
- * jump 0, CTL2=Z, FIFO, LOOP
+ * jump 0, CTLx=Z, FIFO, LOOP
+ *
+ * (CTLx is device-dependent, could be e.g. CTL0 or CTL2.)
*/
+ /* LENGTH / BRANCH 0-7 */
EXTAUTODAT2 = samplerates[i].wait0;
EXTAUTODAT2 = samplerates[i].wait1;
EXTAUTODAT2 = 1;
EXTAUTODAT2 = 0;
EXTAUTODAT2 = 0;
+ /* OPCODE 0-7 */
EXTAUTODAT2 = samplerates[i].opc0;
EXTAUTODAT2 = samplerates[i].opc1;
- EXTAUTODAT2 = 1;
+ EXTAUTODAT2 = 1; /* DATA=0 DP=1 */
EXTAUTODAT2 = 0;
EXTAUTODAT2 = 0;
EXTAUTODAT2 = 0;
EXTAUTODAT2 = 0;
EXTAUTODAT2 = 0;
+ /* OUTPUT 0-7 */
EXTAUTODAT2 = samplerates[i].out0;
- EXTAUTODAT2 = 0x44;
- EXTAUTODAT2 = 0x44;
- EXTAUTODAT2 = 0x00;
- EXTAUTODAT2 = 0x00;
- EXTAUTODAT2 = 0x00;
- EXTAUTODAT2 = 0x00;
- EXTAUTODAT2 = 0x00;
+ EXTAUTODAT2 = OE_CTL;
+ EXTAUTODAT2 = OE_CTL;
+ EXTAUTODAT2 = 0;
+ EXTAUTODAT2 = 0;
+ EXTAUTODAT2 = 0;
+ EXTAUTODAT2 = 0;
+ EXTAUTODAT2 = 0;
+ /* LOGIC FUNCTION 0-7 */
EXTAUTODAT2 = 0;
EXTAUTODAT2 = 0;
EXTAUTODAT2 = 0;
return TRUE;
}
+static BOOL set_calibration_pulse(BYTE fs)
+{
+ switch (fs) {
+ case 0: // 100Hz
+ RCAP2L = -10000 & 0xff;
+ RCAP2H = (-10000 & 0xff00) >> 8;
+ return TRUE;
+ case 1: // 1kHz
+ RCAP2L = -1000 & 0xff;
+ RCAP2H = (-1000 & 0xff00) >> 8;
+ return TRUE;
+ case 10: // 1kHz
+ RCAP2L = (BYTE)(-100 & 0xff);
+ RCAP2H = 0xff;
+ return TRUE;
+ case 50: // 50kHz
+ RCAP2L = (BYTE)(-20 & 0xff);
+ RCAP2H = 0xff;
+ return TRUE;
+ default:
+ return FALSE;
+ }
+}
+
/* Set *alt_ifc to the current alt interface for ifc. */
BOOL handle_get_interface(BYTE ifc, BYTE *alt_ifc)
{
{
stop_sampling();
- /* Set red LED. */
- PC0 = 0;
- PC1 = 1;
+ /* Set red LED, clear after timeout. */
+ LED_RED();
ledcounter = 1000;
- switch (cmd) {
- case 0xe0:
- case 0xe1:
+ /* Clear EP0BCH/L for each valid command. */
+ if (cmd >= 0xe0 && cmd <= 0xe6) {
EP0BCH = 0;
EP0BCL = 0;
while (EP0CS & bmEPBUSY);
+ }
+
+ switch (cmd) {
+ case 0xe0:
+ case 0xe1:
set_voltage(cmd - 0xe0, EP0BUF[0]);
return TRUE;
case 0xe2:
- EP0BCH = 0;
- EP0BCL = 0;
- while (EP0CS & bmEPBUSY);
set_samplerate(EP0BUF[0]);
return TRUE;
case 0xe3:
- EP0BCH = 0;
- EP0BCL = 0;
- while (EP0CS & bmEPBUSY);
if (EP0BUF[0] == 1)
start_sampling();
return TRUE;
case 0xe4:
- EP0BCH = 0;
- EP0BCL = 0;
- while (EP0CS & bmEPBUSY);
set_numchannels(EP0BUF[0]);
return TRUE;
+ case 0xe5:
+ SET_COUPLING(EP0BUF[0]);
+ return TRUE;
+ case 0xe6:
+ SET_CALIBRATION_PULSE(EP0BUF[0]);
+ return TRUE;
}
return FALSE; /* Not handled by handlers. */
}
-void init(void)
+static void init(void)
{
EP4CFG = 0;
EP8CFG = 0;
+ SET_ANALOG_MODE();
+
/* In idle mode tristate all outputs. */
- GPIFIDLECTL = 0x00;
- GPIFCTLCFG = 0x80;
+ GPIFIDLECTL = 0x00; /* Don't enable CTL0-5 outputs. */
+ GPIFCTLCFG = 0x80; /* TRICTL=1. CTL0-2: CMOS outputs, tri-statable. */
GPIFWFSELECT = 0x00;
GPIFREADYSTAT = 0x00;
select_interface(0);
}
-void main(void)
+static void main(void)
{
/* Save energy. */
SETCPUFREQ(CLK_12M);
/* Init timer2. */
RCAP2L = -500 & 0xff;
- RCAP2H = (-500 >> 8) & 0xff;
+ RCAP2H = (-500 & 0xff00) >> 8;
T2CON = 0;
ET2 = 1;
TR2 = 1;
- RENUMERATE();
+ RENUMERATE_UNCOND();
+ PORTECFG = 0;
PORTCCFG = 0;
PORTACFG = 0;
+ OEE = 0xff;
OEC = 0xff;
- OEA = 0x80;
+ OEA = 0xff;
while (TRUE) {
if (dosud) {
dosuspend = FALSE;
do {
/* Make sure ext wakeups are cleared. */
- WAKEUPCS |= bmWU|bmWU2;
+ WAKEUPCS |= bmWU | bmWU2;
SUSPEND = 1;
PCON |= 1;
__asm