# HG changeset patch # User Paul Boddie # Date 1360368325 0 # Node ID e59502a48fbe4e6b8d46cdacd9ec10ccf880a224 # Parent 5f9455ebcd32722a35f0e1b98a245d3b96559ac2 Moved the reset functionality into separate functions. Added a function which waits for the oscillator interrupt notification and which uses half-duplex transfers because it will be used before host mode is enabled. diff -r 5f9455ebcd32 -r e59502a48fbe test.c --- a/test.c Fri Feb 08 22:50:08 2013 +0000 +++ b/test.c Sat Feb 09 00:05:25 2013 +0000 @@ -16,6 +16,7 @@ #include #include #include +#include /* Pin assignments: * @@ -44,12 +45,16 @@ #define MAX_REG_READ 0x00 #define MAX_REG_WRITE 0x02 +#define MAX_REG_USBIRQ 13 #define MAX_REG_USBCTL 15 #define MAX_REG_PINCTL 17 #define MAX_REG_REVISION 18 +#define MAX_REG_HIRQ 25 #define MAX_REG_MODE 27 #define MAX_REG_HRSL 31 +#define MAX_USBIRQ_OSCOKIRQ 1 + #define MAX_USBCTL_PWRDOWN 16 #define MAX_USBCTL_CHIPRES 32 @@ -60,6 +65,8 @@ #define MAX_PINCTL_FDUPSPI_HALF 0 #define MAX_PINCTL_FDUPSPI_FULL 16 +#define MAX_HIRQ_BUSEVENTIRQ 1 + #define MAX_MODE_PERIPHERAL 0 #define MAX_MODE_HOST 1 #define MAX_MODE_SEPIRQ_OFF 0 @@ -196,6 +203,51 @@ return result; } +void chipreset() +{ + printf("Resetting...\n"); + spi_begin(); + spi_sendrecv(max_reg_write(MAX_REG_USBCTL)); + spi_sendrecv(MAX_USBCTL_CHIPRES); + spi_end(); + + printf("Clearing the reset...\n"); + spi_begin(); + spi_sendrecv(max_reg_write(MAX_REG_USBCTL)); + spi_sendrecv(0); + spi_end(); +} + +uint8_t wait() +{ + uint8_t status = 0, oscillator = 0; + + /* Wait for the oscillator before performing USB activity. */ + + printf("Waiting...\n"); + + while (!oscillator) + { + spi_begin(); + spi_send(max_reg_read(MAX_REG_USBIRQ)); + oscillator = spi_recv(); + spi_end(); + + oscillator = oscillator & MAX_USBIRQ_OSCOKIRQ; + if (!oscillator) + usleep(3000); /* 3ms */ + } + + return status; +} + +void reset() +{ + SET(MAX_RESET); + nanosleep(&tRESET, NULL); + CLR(MAX_RESET); +} + int main(int argc, char *argv[]) { uint8_t status = 0, revision = 0, hrsl = 0; @@ -225,6 +277,9 @@ /* Initialise the MAX3421E. */ + reset(); + wait(); + /* Set full-duplex, interrupt signalling. */ printf("Setting pin control...\n"); @@ -234,22 +289,6 @@ spi_end(); printf("INT set to %d\n", PIN(MAX_INT)); - SET(MAX_RESET); - nanosleep(&tRESET, NULL); - CLR(MAX_RESET); - - printf("Resetting...\n"); - spi_begin(); - spi_sendrecv(max_reg_write(MAX_REG_USBCTL)); - spi_sendrecv(MAX_USBCTL_CHIPRES); - spi_end(); - - printf("Clearing the reset...\n"); - spi_begin(); - spi_sendrecv(max_reg_write(MAX_REG_USBCTL)); - spi_sendrecv(0); - spi_end(); - /* Set host mode. */ printf("Setting mode...\n"); @@ -270,6 +309,18 @@ printf("Revision = %x\n", revision); printf("INT set to %d\n", PIN(MAX_INT)); + /* Detect bus event. */ + + if (status & MAX_HIRQ_BUSEVENTIRQ) + { + printf("Clearing BUSEVENTIRQ...\n"); + spi_begin(); + status = spi_sendrecv(max_reg(MAX_REG_HIRQ)); + spi_sendrecv(MAX_HIRQ_BUSEVENTIRQ); + spi_end(); + printf("Status = %x\n", status); + } + printf("HRSL...\n"); spi_begin(); status = spi_sendrecv(max_reg_read(MAX_REG_HRSL));