790 #include #include #include "mpu.h" /* * Global variable holding the MPU-401 base address * Has to be set before calling any function if different from the * default 0x330 */ int mpu_base = 0x330; /* mpu_wait_drr(): ** wait for MPU ready to receive a command, with timeout. */ int mpu_wait_drr() { long counter; int stat; for (counter = 0xFFFFL; counter != 0; --counter) { if ((stat = inp(mpu_base + MPUP_STAT) & MPUPS_DRR) == 0) break; } return (stat); } /* mpu_wait_dsr(): ** wait for MPU ready to send data, with timeout. */ int mpu_wait_dsr() { long counter; int stat; for (counter = 0xFFFFL; counter != 0; --counter) { if ((stat = inp(mpu_base + MPUP_STAT) & MPUPS_DSR) == 0) break; } return (stat); } /* mpu_dsr(): ** check if data waiting. */ int mpu_dsr() { return (inp(mpu_base + MPUP_STAT) & MPUPS_DSR); } /* mpu_put(byte): ** put out a single byte command & wait for the ack. */ void mpu_put(unsigned char cmd) { long counter; int ack; (void) mpu_wait_drr(); outp(mpu_base + MPUP_COMD, cmd); for (counter = 0xFFL; counter != 0; --counter) { if (mpu_wait_dsr()) { /* timed out */ fprintf(stderr, "DSR timeout\n"); break; } if (mpu_dget() == CMD_ACK) break; } } /* mpu_dput(data): ** put out a single data byte */ void mpu_dput(unsigned char data) { if (mpu_wait_drr()) { fprintf(stderr, "DRR timeout\n"); return; } outp(mpu_base + MPUP_DATA, data); } /* mpu_dget(): ** get data byte if available, else return -1 */ int mpu_dget() { int data = -1; if (! mpu_dsr()) { asm cli data = inp(mpu_base + MPUP_DATA); asm sti } return (data); } /* mpu_exit_uart(): ** exit UART mode (by sending a reset) */ void mpu_exit_uart(void) { (void) mpu_wait_drr(); outp(mpu_base + MPUP_COMD, CMD_RESET); } . 0