# HG changeset patch # User Paul Boddie # Date 1360605988 0 # Node ID 046d502b211b1c3c9e3f41ac2b9052b2bf34fd15 # Parent 36c36cd4406705976ad08d793364bd588b7713a7 Introduced some device connection (and disconnection) handling. diff -r 36c36cd44067 -r 046d502b211b test.c --- a/test.c Mon Feb 11 17:48:21 2013 +0000 +++ b/test.c Mon Feb 11 18:06:28 2013 +0000 @@ -220,7 +220,7 @@ return timeout; } -uint8_t samplebus() +uint8_t samplebusready() { uint8_t result; @@ -229,6 +229,49 @@ return !(result & MAX_HCTL_SAMPLEBUS); } +void samplebus() +{ + max_write(MAX_REG_HCTL, MAX_HCTL_SAMPLEBUS); + while (!samplebusready()); +} + +void devicechanged() +{ + uint8_t hrsl, mode; + + hrsl = max_read(MAX_REG_HRSL, NULL); + mode = max_read(MAX_REG_MODE, NULL); + + if ((hrsl & MAX_HRSL_JSTATUS) && (hrsl & MAX_HRSL_KSTATUS)) + { + printf("Bad device status.\n"); + } + else if (!(hrsl & MAX_HRSL_JSTATUS) && !(hrsl & MAX_HRSL_KSTATUS)) + { + printf("Device disconnected.\n"); + } + else + { + printf("Device connected.\n"); + + /* Low speed device when J and lowspeed have the same level. + Since J and K should have opposing levels, K can be tested when + lowspeed is low. */ + + if (((hrsl & MAX_HRSL_JSTATUS) && (mode & MAX_MODE_LOWSPEED)) || + ((hrsl & MAX_HRSL_KSTATUS) && !(mode & MAX_MODE_LOWSPEED))) + { + printf("Device is low speed.\n"); + max_write(MAX_REG_MODE, MAX_MODE_HOST | MAX_MODE_SEPIRQ_OFF | MAX_MODE_DMPULLDN | MAX_MODE_DPPULLDN | MAX_MODE_LOWSPEED); + } + else + { + printf("Device is full speed.\n"); + max_write(MAX_REG_MODE, MAX_MODE_HOST | MAX_MODE_SEPIRQ_OFF | MAX_MODE_DMPULLDN | MAX_MODE_DPPULLDN); + } + } +} + void shutdown(int signum) { printf("Closing...\n"); @@ -315,7 +358,7 @@ status = max_read(MAX_REG_HIRQ, NULL); if (status & MAX_HIRQ_CONDETIRQ) - printf("Device connected.\n"); + devicechanged(); if (status & MAX_HIRQ_SUSDNIRQ) printf("Suspend done.\n"); if (status & MAX_HIRQ_BUSEVENTIRQ)