This program is for an autonomous robot for the competition at the Hochschule Luzern. We are one of the 32 teams. <a href="http://cruisingcrepe.wordpress.com/">http://cruisingcrepe.wordpress.com/</a> The postition control is based on this Documentation: Control of Wheeled Mobile Robots: An Experimental Overview from Alessandro De Luca, Giuseppe Oriolo, Marilena Vendittelli. For more information see here: <a href="http://www.dis.uniroma1.it/~labrob/pub/papers/Ramsete01.pdf">http://www.dis.uniroma1.it/~labrob/pub/papers/Ramsete01.pdf</a>
Fork of autonomousRobotAndroid by
Revision 12:235e318a414f, committed 2013-04-07
- Comitter:
- chrigelburri
- Date:
- Sun Apr 07 08:31:51 2013 +0000
- Parent:
- 11:775ebb69d5e1
- Child:
- 13:a7c30ee09bae
- Commit message:
- Kommentare nochmals verbessert android fehlt noch
Changed in this revision
--- a/Actuators/Hallsensor.cpp Fri Apr 05 10:58:42 2013 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,115 +0,0 @@ -#include "Hallsensor.h" - -Hallsensor::Hallsensor( - PinName hall1, - PinName hall2, - PinName hall3 -) - : hall1_(hall1), - hall2_(hall2), - hall3_(hall3) -{ - pulses_ = 0; - - //Workout what the current state is. - int h1 = hall1_.read(); - int h2 = hall2_.read(); - int h3 = hall3_.read(); - - // Set the PullUp Resistor - hall1_.mode(PullUp); - hall2_.mode(PullUp); - hall3_.mode(PullUp); - - //set interrupts on only hall 1-3 rise and fall. - hall1_.rise(this, &Hallsensor::encode); - hall1_.fall(this, &Hallsensor::encode); - hall2_.rise(this, &Hallsensor::encode); - hall2_.fall(this, &Hallsensor::encode); - hall3_.rise(this, &Hallsensor::encode); - hall3_.fall(this, &Hallsensor::encode); -} - -void Hallsensor::reset(void) -{ - pulses_ = 0; -} - -int Hallsensor::getPulses(void) -{ - return pulses_; -} - - -void Hallsensor::encode(void) -{ - // read the state - int h1 = hall1_.read(); - int h2 = hall2_.read(); - int h3 = hall3_.read(); - - //3-bit state. - currState_ = (h1 << 2) | (h2 << 1) | h3; - - if ((prevState_ == 0x5) && (currState_ == 0x4)) { - pulses_++; - prevState_ = currState_; - return; - } else if (prevState_ == 0x5 && currState_ == 0x1) { - pulses_--; - prevState_ = currState_; - return; - } - - if ((prevState_ == 0x4) && (currState_ == 0x6)) { - pulses_++; - prevState_ = currState_; - return; - } else if (prevState_ == 0x4 && currState_ == 0x5) { - pulses_--; - prevState_ = currState_; - return; - } - - if ((prevState_ == 0x6) && (currState_ == 0x2)) { - pulses_++; - prevState_ = currState_; - return; - } else if (prevState_ == 0x6 && currState_ == 0x4) { - pulses_--; - prevState_ = currState_; - return; - } - - if ((prevState_ == 0x2) && (currState_ == 0x3)) { - pulses_++; - prevState_ = currState_; - return; - } else if (prevState_ == 0x2 && currState_ == 0x6) { - pulses_--; - prevState_ = currState_; - return; - } - - if ((prevState_ == 0x3) && (currState_ == 0x1)) { - pulses_++; - prevState_ = currState_; - return; - } else if (prevState_ == 0x3 && currState_ == 0x2) { - pulses_--; - prevState_ = currState_; - return; - } - - if ((prevState_ == 0x1) && (currState_ == 0x5)) { - pulses_++; - prevState_ = currState_; - return; - } else if (prevState_ == 0x1 && currState_ == 0x3) { - pulses_--; - prevState_ = currState_; - return; - } - prevState_ = currState_; - -}
--- a/Actuators/Hallsensor.h Fri Apr 05 10:58:42 2013 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,74 +0,0 @@ -#ifndef HALLSENSOR_H -#define HALLSENSOR_H - -#include "mbed.h" - -/** - * @author Christian Burri - * - * @copyright Copyright © 2013 HSLU Pren Team #1 Cruising Crêpe - * All rights reserved. - * - * @brief - * - * Interface to count the Hallsensor input from a EC-Motor. - * - */ -class Hallsensor -{ - -private: - - /** - * @brief Update the pulse count. - * Called on every rising/falling edge of Hall 1-3. - * Reads the state of the channels and determines whether a pulse forward - * or backward has occured, updating the count appropriately. - */ - void encode(void); - - InterruptIn hall1_; - InterruptIn hall2_; - InterruptIn hall3_; - - int prevState_; - int currState_; - - volatile int pulses_; - -public: - - /** - * @brief Constructor of the class <code>Hallsensor</code>. - * - * Reads the current values on Hall1 , Hall2 and Hall3 to determine the - * initial state. - * Attaches the encode function to the rise/fall interrupt edges of - * Hall1, Hall2 and Hall3. - * @param hall1 mbed pin for Hall1 input. - * @param hall2 mbed pin for Hall2 input. - * @param hall3 mbed pin for Hall3 input. - */ - Hallsensor(PinName hall1, PinName hall2, PinName hall3); - - /** - * @brief Reset the encoder. - * Sets the pulses and revolutions count to zero. - */ - void reset(void); - - /** - * @brief Read the number of pulses recorded by the encoder. - * @return Number of pulses which have occured, given in [count] - */ - int getPulses(void); - - /** - * @brief Read the number of revolutions recorded by the encoder. - * @return Number of revolutions which have occured on the index channel. - */ - int getRevolutions(void); - -}; - -#endif /* Hallsensor_H */
--- a/Actuators/Hallsensor.lib Fri Apr 05 10:58:42 2013 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,1 +0,0 @@ -Actuators/Hallsensor#d5573b3c22ae
--- a/Actuators/MaxonESCON.cpp Fri Apr 05 10:58:42 2013 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,86 +0,0 @@ -#include "MaxonESCON.h" - -using namespace std; - -MaxonESCON::MaxonESCON( - PinName enb, - PinName isenb, - PinName pwm, - PinName actualSpeed, - Hallsensor *hall -) - : - _enb(enb), - _isenb(isenb), - _pwm(pwm), - _actualSpeed(actualSpeed), - _hall(hall) -{ - - _pwm = 0; - - // Initial condition of output enables - _enb = 0; - - // Set initial condition of PWM 2kHz - period(0.0005); - - // Set the pulses to zero - _pulses = 0; - - // Set the Pull Up Resistor - _isenb.mode(PullUp); -} - -void MaxonESCON::setVelocity(float speed) -{ - speed = speed / ESCON_SET_FACTOR * 60.0f; - if(speed > 1 ) { - _pwm = 0.89f; - } else if(speed < -1) { - _pwm = 0.11f; - } else { - _pwm = 0.4f*speed + (0.5f * (SET_SPEED_PATCH)); - } -} - -float MaxonESCON::getActualSpeed(void) -{ - return (_actualSpeed.read()* 2.0f - 1.0f *(GET_SPEED_PATCH)) * ESCON_GET_FACTOR / 60.0f; -} - -void MaxonESCON::period(float period) -{ - _pwm.period(period); -} - -void MaxonESCON::enable(bool enb) -{ - if(enb == true) { - _enb = 1; - } else { - _enb = 0; - } -} - -bool MaxonESCON::isEnabled() -{ - if(_isenb.read() == 1) { - return true; - } else { - return false; - } -} - -int MaxonESCON::getPulses(void) -{ - _pulses = _hall->getPulses(); - return _pulses; -} - -int MaxonESCON::setPulses(int setPos) -{ - _hall->reset(); - _pulses = _hall->getPulses(); - return _pulses; -}
--- a/Actuators/MaxonESCON.h Fri Apr 05 10:58:42 2013 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,101 +0,0 @@ -#ifndef _MAXON_ESCON_H_ -#define _MAXON_ESCON_H_ - -#include "Hallsensor.h" -#include "defines.h" - -/** - * @author Christian Burri - * - * @copyright Copyright © 2013 HSLU Pren Team #1 Cruising Crêpe - * All rights reserved. - * - * @brief - * - * This class implements the driver for the Maxon ESCON servo driver. - * For more information see the Datasheet: - * <a href="http://escon.maxonmotor.com">http://escon.maxonmotor.com</a> - */ -class MaxonESCON -{ - -private: - - /** @brief To Enable the amplifier */ - DigitalOut _enb; - /** @brief Duty Cycle to set the speed */ - PwmOut _pwm; - /** @brief Hallsensor Class */ - Hallsensor* _hall; - /** @brief Ready output from ESCON */ - DigitalIn _isenb; - /** @brief Actual speed from ESCON analog Output 1 */ - AnalogIn _actualSpeed; - /** @brief increment the Hallpattern */ - int _pulses; - -public: - - /** - * @brief Create a motor control object. - * @param enb DigitalOut, set high for enable - * @param isenb DigitalIn, high for enable - * @param pwm PwmOut pin, set the velocity - * @param actualSpeed AnalogIn filtered signal for ActualSpeed from Motor - * @param hall The object of the hallsensor from Motor - */ - MaxonESCON(PinName enb, - PinName isenb, - PinName pwm, - PinName actualSpeed, - Hallsensor *hall); - - /** - * @brief Set the speed of the motor with a pwm for 10%..90%. - * 50% PWM is 0rpm. - * Caclulate from [1/s] in [1/min] and the Factor of the ESCON. - * @param speed The speed of the motor as a normalised value, given in [1/s] - */ - void setVelocity(float speed); - - /** - * @brief Return the speed from ESCON. - * 0 rpm is defined in the Analog input as 1.65V - * @return speed of the motor, given in [1/s] - */ - float getActualSpeed(void); - - /** - * @brief Set the period of the pwm duty cycle. - * Wrapper for PwmOut::period() - * @param period Pwm duty cycle, given in [s]. - */ - void period(float period); - - /** - * @brief Set the Motor to a enable sate. - * @param enb <code>false</code> for disable <code>true</code> for enable. - */ - void enable(bool enb); - - /** - * @brief Tests if the servo drive is enabled. - * @return <code>true</code> if the drive is enabled, - * <code>false</code> otherwise. - */ - bool isEnabled(void); - - /** - * @brief Return the number of Pulses. - * @return Pulses, given in [count] - */ - int getPulses(void); - - /** - * @brief Set the Pulses of the Motor, given in [count] - * @return Pulses, given in [count] - */ - int setPulses(int setPos); -}; - -#endif
--- a/Actuators/MaxonESCON.lib Fri Apr 05 10:58:42 2013 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,1 +0,0 @@ -Actuators/MaxonESCON#343a651a9693
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ActuatorsSensor/Hallsensor.cpp Sun Apr 07 08:31:51 2013 +0000 @@ -0,0 +1,115 @@ +#include "Hallsensor.h" + +Hallsensor::Hallsensor( + PinName hall1, + PinName hall2, + PinName hall3 +) + : hall1_(hall1), + hall2_(hall2), + hall3_(hall3) +{ + pulses_ = 0; + + //Workout what the current state is. + int h1 = hall1_.read(); + int h2 = hall2_.read(); + int h3 = hall3_.read(); + + // Set the PullUp Resistor + hall1_.mode(PullUp); + hall2_.mode(PullUp); + hall3_.mode(PullUp); + + //set interrupts on only hall 1-3 rise and fall. + hall1_.rise(this, &Hallsensor::encode); + hall1_.fall(this, &Hallsensor::encode); + hall2_.rise(this, &Hallsensor::encode); + hall2_.fall(this, &Hallsensor::encode); + hall3_.rise(this, &Hallsensor::encode); + hall3_.fall(this, &Hallsensor::encode); +} + +void Hallsensor::reset(void) +{ + pulses_ = 0; +} + +int Hallsensor::getPulses(void) +{ + return pulses_; +} + + +void Hallsensor::encode(void) +{ + // read the state + int h1 = hall1_.read(); + int h2 = hall2_.read(); + int h3 = hall3_.read(); + + //3-bit state. + currState_ = (h1 << 2) | (h2 << 1) | h3; + + if ((prevState_ == 0x5) && (currState_ == 0x4)) { + pulses_++; + prevState_ = currState_; + return; + } else if (prevState_ == 0x5 && currState_ == 0x1) { + pulses_--; + prevState_ = currState_; + return; + } + + if ((prevState_ == 0x4) && (currState_ == 0x6)) { + pulses_++; + prevState_ = currState_; + return; + } else if (prevState_ == 0x4 && currState_ == 0x5) { + pulses_--; + prevState_ = currState_; + return; + } + + if ((prevState_ == 0x6) && (currState_ == 0x2)) { + pulses_++; + prevState_ = currState_; + return; + } else if (prevState_ == 0x6 && currState_ == 0x4) { + pulses_--; + prevState_ = currState_; + return; + } + + if ((prevState_ == 0x2) && (currState_ == 0x3)) { + pulses_++; + prevState_ = currState_; + return; + } else if (prevState_ == 0x2 && currState_ == 0x6) { + pulses_--; + prevState_ = currState_; + return; + } + + if ((prevState_ == 0x3) && (currState_ == 0x1)) { + pulses_++; + prevState_ = currState_; + return; + } else if (prevState_ == 0x3 && currState_ == 0x2) { + pulses_--; + prevState_ = currState_; + return; + } + + if ((prevState_ == 0x1) && (currState_ == 0x5)) { + pulses_++; + prevState_ = currState_; + return; + } else if (prevState_ == 0x1 && currState_ == 0x3) { + pulses_--; + prevState_ = currState_; + return; + } + prevState_ = currState_; + +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ActuatorsSensor/Hallsensor.h Sun Apr 07 08:31:51 2013 +0000 @@ -0,0 +1,74 @@ +#ifndef HALLSENSOR_H +#define HALLSENSOR_H + +#include "mbed.h" + +/** + * @author Christian Burri + * + * @copyright Copyright © 2013 HSLU Pren Team #1 Cruising Crêpe + * All rights reserved. + * + * @brief + * + * Interface to count the Hallsensor input from a EC-Motor. + * + */ +class Hallsensor +{ + +private: + + /** + * @brief Update the pulse count. + * Called on every rising/falling edge of Hall 1-3. + * Reads the state of the channels and determines whether a pulse forward + * or backward has occured, updating the count appropriately. + */ + void encode(void); + + InterruptIn hall1_; + InterruptIn hall2_; + InterruptIn hall3_; + + int prevState_; + int currState_; + + volatile int pulses_; + +public: + + /** + * @brief Constructor of the class <code>Hallsensor</code>. + * + * Reads the current values on Hall1 , Hall2 and Hall3 to determine the + * initial state. + * Attaches the encode function to the rise/fall interrupt edges of + * Hall1, Hall2 and Hall3. + * @param hall1 mbed pin for Hall1 input. + * @param hall2 mbed pin for Hall2 input. + * @param hall3 mbed pin for Hall3 input. + */ + Hallsensor(PinName hall1, PinName hall2, PinName hall3); + + /** + * @brief Reset the encoder. + * Sets the pulses and revolutions count to zero. + */ + void reset(void); + + /** + * @brief Read the number of pulses recorded by the encoder. + * @return Number of pulses which have occured, given in [count] + */ + int getPulses(void); + + /** + * @brief Read the number of revolutions recorded by the encoder. + * @return Number of revolutions which have occured on the index channel. + */ + int getRevolutions(void); + +}; + +#endif
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ActuatorsSensor/Hallsensor.lib Sun Apr 07 08:31:51 2013 +0000 @@ -0,0 +1,1 @@ +Actuators/Hallsensor#d5573b3c22ae
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ActuatorsSensor/MaxonESCON.cpp Sun Apr 07 08:31:51 2013 +0000 @@ -0,0 +1,86 @@ +#include "MaxonESCON.h" + +using namespace std; + +MaxonESCON::MaxonESCON( + PinName enb, + PinName isenb, + PinName pwm, + PinName actualSpeed, + Hallsensor *hall +) + : + _enb(enb), + _isenb(isenb), + _pwm(pwm), + _actualSpeed(actualSpeed), + _hall(hall) +{ + + _pwm = 0; + + // Initial condition of output enables + _enb = 0; + + // Set initial condition of PWM 2kHz + period(0.0005); + + // Set the pulses to zero + _pulses = 0; + + // Set the Pull Up Resistor + _isenb.mode(PullUp); +} + +void MaxonESCON::setVelocity(float speed) +{ + speed = speed / ESCON_SET_FACTOR * 60.0f; + if(speed > 1 ) { + _pwm = 0.89f; + } else if(speed < -1) { + _pwm = 0.11f; + } else { + _pwm = 0.4f*speed + (0.5f * (SET_SPEED_PATCH)); + } +} + +float MaxonESCON::getActualSpeed(void) +{ + return (_actualSpeed.read()* 2.0f - 1.0f *(GET_SPEED_PATCH)) * ESCON_GET_FACTOR / 60.0f; +} + +void MaxonESCON::period(float period) +{ + _pwm.period(period); +} + +void MaxonESCON::enable(bool enb) +{ + if(enb == true) { + _enb = 1; + } else { + _enb = 0; + } +} + +bool MaxonESCON::isEnabled() +{ + if(_isenb.read() == 1) { + return true; + } else { + return false; + } +} + +int MaxonESCON::getPulses(void) +{ + _pulses = _hall->getPulses(); + return _pulses; +} + +int MaxonESCON::setPulses(int setPos) +{ + _hall->reset(); + _pulses = _hall->getPulses(); + return _pulses; +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ActuatorsSensor/MaxonESCON.h Sun Apr 07 08:31:51 2013 +0000 @@ -0,0 +1,101 @@ +#ifndef _MAXON_ESCON_H_ +#define _MAXON_ESCON_H_ + +#include "Hallsensor.h" +#include "defines.h" + +/** + * @author Christian Burri + * + * @copyright Copyright © 2013 HSLU Pren Team #1 Cruising Crêpe + * All rights reserved. + * + * @brief + * + * This class implements the driver for the Maxon ESCON servo driver. + * For more information see the Datasheet: + * <a href="http://escon.maxonmotor.com">http://escon.maxonmotor.com</a> + */ +class MaxonESCON +{ + +private: + + /** @brief To Enable the amplifier */ + DigitalOut _enb; + /** @brief Duty Cycle to set the speed */ + PwmOut _pwm; + /** @brief Hallsensor Class */ + Hallsensor* _hall; + /** @brief Ready output from ESCON */ + DigitalIn _isenb; + /** @brief Actual speed from ESCON analog Output 1 */ + AnalogIn _actualSpeed; + /** @brief increment the Hallpattern */ + int _pulses; + +public: + + /** + * @brief Create a motor control object. + * @param enb DigitalOut, set high for enable + * @param isenb DigitalIn, high for enable + * @param pwm PwmOut pin, set the velocity + * @param actualSpeed AnalogIn filtered signal for ActualSpeed from Motor + * @param hall The object of the hallsensor from Motor + */ + MaxonESCON(PinName enb, + PinName isenb, + PinName pwm, + PinName actualSpeed, + Hallsensor *hall); + + /** + * @brief Set the speed of the motor with a pwm for 10%..90%. + * 50% PWM is 0rpm. + * Caclulate from [1/s] in [1/min] and the Factor of the ESCON. + * @param speed The speed of the motor as a normalised value, given in [1/s] + */ + void setVelocity(float speed); + + /** + * @brief Return the speed from ESCON. + * 0 rpm is defined in the Analog input as 1.65V + * @return speed of the motor, given in [1/s] + */ + float getActualSpeed(void); + + /** + * @brief Set the period of the pwm duty cycle. + * Wrapper for PwmOut::period() + * @param period Pwm duty cycle, given in [s]. + */ + void period(float period); + + /** + * @brief Set the Motor to a enable sate. + * @param enb <code>false</code> for disable <code>true</code> for enable. + */ + void enable(bool enb); + + /** + * @brief Tests if the servo drive is enabled. + * @return <code>true</code> if the drive is enabled, + * <code>false</code> otherwise. + */ + bool isEnabled(void); + + /** + * @brief Return the number of Pulses. + * @return Pulses, given in [count] + */ + int getPulses(void); + + /** + * @brief Set the Pulses of the Motor, given in [count] + * @return Pulses, given in [count] + */ + int setPulses(int setPos); +}; + +#endif
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ActuatorsSensor/MaxonESCON.lib Sun Apr 07 08:31:51 2013 +0000 @@ -0,0 +1,1 @@ +Actuators/MaxonESCON#343a651a9693
--- a/Android/AndroidAccessory/AndroidAccessory.cpp Fri Apr 05 10:58:42 2013 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,323 +0,0 @@ -#include <stdio.h> -#include <stdlib.h> -#include <string.h> - -#include "USBHost.h" -#include "AndroidAccessory.h" -#include "mbed.h" - -AndroidAccessory* _adk; - -void AdkreadCallback(int device, int endpoint, int status, u8* buf, int len, void* userData); -void AdkwriteCallback(int device, int endpoint, int status, u8* buf, int len, void* userData); - - - -AndroidAccessory::AndroidAccessory(int rbuffsize,int wbuffsize, - const char* manufacturer, - const char *model, - const char *description, - const char *version, - const char *uri, - const char *serial - ) { - - _adk=this; - - this->manufacturer=manufacturer; - this->model=model; - this->description=description; - this->version=version; - this->uri=uri; - this->serial=serial; - - u32 len; - u8* p=USBGetBuffer(&len); - if (len<(rbuffsize+wbuffsize+255)) { - error("buff size too big.please resize max=%d. currentSize=%d\r\n",len,(rbuffsize+wbuffsize+255)); - } - - _readbuff=p; - _readbuffsize=rbuffsize; - p+=rbuffsize; - _writebuff=p; - _writebuffsize=wbuffsize; - p+=wbuffsize; - _strbuff=p; - p+=255; - -} - - - -int AndroidAccessory::write(u8 *buff, int len) { - log("AndroidAccessory::write "); - // __disable_irq(); - int ret=USBBulkTransfer(_device,output_ep,buff,len,AdkwriteCallback,this); - // __enable_irq(); - log("--ret=%d \r\n",ret); - return ret; -} -int AndroidAccessory::writeNC(u8 *buff, int len) { - log("AndroidAccessory::write "); - // __disable_irq(); - int ret=USBBulkTransfer(_device,output_ep,buff,len); - // __enable_irq(); - log("--ret=%d \r\n",ret); - return ret; -} - - - -int AndroidAccessory::read(u8 *buff, int len) { - // if(_initok==false)return 0; - - log("AndroidAccessory::read "); - // __disable_irq(); - int ret=USBBulkTransfer(_device,input_ep|0x80,buff,len); - // __enable_irq(); - log("--ret=%d \r\n",ret); - return ret; -} - - -void AndroidAccessory::init(int device, int configuration, int interfaceNumber) { - - log("AndroidAccessory::init \r\n"); - -// _initok=false; - _device = device; - _configuration = configuration; - _interfaceNumber = interfaceNumber; - printf("device = %d configuration = %d interfaceNumber = %d\r\n", device, configuration, interfaceNumber); - int err; - - u8* buffer=_strbuff; - err = GetDescriptor(_device,DESCRIPTOR_TYPE_CONFIGURATION,0,buffer,4); - - if (err < 0) { - log("Failed to get descriptor\r\n"); - return; - } - - - int len = buffer[2] | (buffer[3] << 8); - if (len > 255) { - log("config descriptor too large\n"); - /* might want to truncate here */ - return; - } - err = GetDescriptor(_device,DESCRIPTOR_TYPE_CONFIGURATION,0,buffer,len); - u8* p = buffer; - input_ep=0; - output_ep=0; - EndpointDescriptor *epDesc; - while (p<(buffer+len)) { - u8 descLen = p[0]; - u8 descType = p[1]; - log("descLen=%d,descType=%d\r\n",descLen,descType); - switch (descType) { - case DESCRIPTOR_TYPE_CONFIGURATION: - log("config desc\r\n"); - break; - case DESCRIPTOR_TYPE_INTERFACE: - log("interface desc\r\n"); - break; - case DESCRIPTOR_TYPE_ENDPOINT: - epDesc=(EndpointDescriptor*)p; - if (!input_ep && (epDesc->bEndpointAddress& 0x80)) { - input_ep=epDesc->bEndpointAddress& 0x7f; - //PacketSize drop - log("input Endpoint address=%d,wMaxPacketSize=%d,bmAttributes=%d\r\n",input_ep,epDesc->wMaxPacketSize,epDesc->bmAttributes); - - } else if (!output_ep) { - output_ep=epDesc->bEndpointAddress& 0x7f; - //PacketSize drop - log("output Endpoint address=%d,wMaxPacketSize=%d,bmAttributes=%d\r\n",input_ep,epDesc->wMaxPacketSize,epDesc->bmAttributes); - } else { - //other - log("non input,output Endpoint address=%d,wMaxPacketSize=%d,bmAttributes=%d\r\n",input_ep,epDesc->wMaxPacketSize,epDesc->bmAttributes); - } - break; - default: - log("unkown desc type(%d) \r\n",descType); - } - p+=descLen; - } - - if (!(input_ep && output_ep)) { - log("can't find accessory endpoints\r\n"); - return; - } - - log("SetConfiguration\r\n"); - err = SetConfiguration(device,configuration); - if (err < 0) { - log("SetConfiguration error\r\n"); - return; - } - - - log("interrupt setup\r\n"); - //interrupt setup - if (_readbuff==NULL || _readbuffsize<=0) { - error("_readbuffer error please setup buffer call setReadBuffer function\r\n"); - } - - if (IO_PENDING!=USBBulkTransfer(_device,input_ep|0x80,_readbuff,_readbuffsize,AdkreadCallback,this)) - return; - - - log("setupDevice\r\n"); - this->setupDevice(); -// _initok=true; -} - - - -bool AndroidAccessory::switchDevice(int device) { - - if (1==getProtocol(device)) { - log("device supports protocol 1\r\n"); - - } else { - log("could not read device protocol version\r\n"); - return false; - } - - - sendString(device,ACCESSORY_STRING_MANUFACTURER,manufacturer); - sendString(device,ACCESSORY_STRING_MODEL,model); - sendString(device,ACCESSORY_STRING_DESCRIPTION,description); - sendString(device,ACCESSORY_STRING_VERSION,version); - sendString(device,ACCESSORY_STRING_URI,uri); - sendString(device,ACCESSORY_STRING_SERIAL,serial); - USBControlTransfer(device, - HOST_TO_DEVICE |REQUEST_TYPE_VENDOR|RECIPIENT_DEVICE, - ACCESSORY_START, - 0,//value - 0, //index - 0, - 0, - 0, - 0 ); - - wait_ms(4); - //reset usb host - USBInit(); - - return true; - -} - - -int AndroidAccessory::getProtocol(int device) { - s16 data=-1; - USBControlTransfer(device, - DEVICE_TO_HOST|REQUEST_TYPE_VENDOR|RECIPIENT_DEVICE, - ACCESSORY_GET_PROTOCOL, - 0,//value - 0, //index - (u8*)&data, - 2, - 0, - 0 ); - return data; - -} - -void AndroidAccessory::sendString(const char *str) { - sendString(_device,1,str); - -} - -void AndroidAccessory::sendString(int device, int index, const char *str) { - - LOG("send_string start(%d,%d,%s) %d \r\n",device,index,str,strlen(str)+1); - strcpy((char*)_strbuff,str); - //thankyou curryman san - USBControlTransfer(device, - HOST_TO_DEVICE|REQUEST_TYPE_VENDOR|RECIPIENT_DEVICE, - ACCESSORY_SEND_STRING, - 0,//value - index, - _strbuff, - strlen(str)+1 - ); - - LOG("send_string end(%d,%d,%s)\r\n",device,index,str); - -} - - -/** from USBHost load function. initialize Android device**/ -void OnLoadDevice(int device, DeviceDescriptor* deviceDesc, InterfaceDescriptor* interfaceDesc) { - printf("LoadDevice %d %02X:%02X:%02X\r\n",device,interfaceDesc->bInterfaceClass,interfaceDesc->bInterfaceSubClass,interfaceDesc->bInterfaceProtocol); - char s[128]; - - for (int i = 1; i < 3; i++) { - if (GetString(device,i,s,sizeof(s)) < 0) - break; - printf("%d: %s\r\n",i,s); - } - - //for android ADK - if ( ( deviceDesc->idVendor != 0x18D1 || - ( deviceDesc->idProduct != 0x2D00 && deviceDesc->idProduct != 0x2D01)) - &&_adk->switchDevice(device)) { - - printf(" try to change accmode.interfaceDesc->bInterfaceClass=%d\r\n",interfaceDesc->bInterfaceClass); - //1th root - //accmode_support=true; - printf("accessory mode ok.\r\n"); - return; - } - - if (deviceDesc->idVendor == 0x18D1 && - (deviceDesc->idProduct == 0x2D00 || deviceDesc->idProduct == 0x2D01)) { - //2th root - printf("connecting Android.\r\n"); - printf("idVender=%x idProduct=%x interfaceDesc->bInterfaceClass=%d\r\n",deviceDesc->idVendor,deviceDesc->idProduct,interfaceDesc->bInterfaceClass); - _adk->init(device,1,0); - //_AdkUSB.loop(); - return; - } -} - -void AdkreadCallback(int device, int endpoint, int status, u8* buf, int len, void* userData) { - log("AdkreadCallback(int device=%d, int endpoint=%x, int status=%d, u8* buf=%p, int len=%d, void* userData=%p)\r\n", - device,endpoint,status,buf,len,userData); -// __disable_irq(); - AndroidAccessory* t = (AndroidAccessory*)userData; - if (status!=0 && status!=8) { - log("adk end.\r\n"); - t->adkEnd(); -// __enable_irq(); - USBInit(); - return; - } - - - //virtual method run - t->callbackRead(buf,len); - - USBBulkTransfer(device, endpoint , buf, len, AdkreadCallback, userData); - -// wait_ms(4); -// __enable_irq(); -} - - - - -void AdkwriteCallback(int device, int endpoint, int status, u8* buf, int len, void* userData) { - - log("AdkwriteCallback(int device=%d, int endpoint=%x, int status=%d, u8* buf=%p, int len=%d, void* userData=%p)\r\n", - device,endpoint,status,buf,len,userData); - - AndroidAccessory* t = (AndroidAccessory*)userData; - t->callbackWrite(); - //wait_ms(4); - //USBBulkTransfer(device, endpoint , buf, len, AdkwriteCallback, userData); -} -
--- a/Android/AndroidAccessory/AndroidAccessory.h Fri Apr 05 10:58:42 2013 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,195 +0,0 @@ -/* mbed AndroidAccessory Library - * Created by p07gbar from work by Makoto Abe - * - */ -#ifndef ADK_H_INCLUDED -#define ADK_H_INCLUDED - -#include "mbed.h" -#include "USBHost.h" - - -//#define ADKLOG 1 -#if ADKLOG -#define LOG(...) printf(__VA_ARGS__) -#define Log(...) printf(__VA_ARGS__) -#define log(...) printf(__VA_ARGS__) - -#else -#define LOG(...) do {} while(0) -#define Log(...) do {} while(0) -#define log(...) do {} while(0) - -#endif - -#define ACCESSORY_STRING_MANUFACTURER 0 -#define ACCESSORY_STRING_MODEL 1 -#define ACCESSORY_STRING_DESCRIPTION 2 -#define ACCESSORY_STRING_VERSION 3 -#define ACCESSORY_STRING_URI 4 -#define ACCESSORY_STRING_SERIAL 5 - -#define ACCESSORY_GET_PROTOCOL 51 -#define ACCESSORY_SEND_STRING 52 -#define ACCESSORY_START 53 - - - -/** An AndroidAccessory control class - * - * It allows easy creation of a mbed android ADK accessory, with minimal low level fussing. - * Base code should have methods resetDevice(), setupDevice(), callbackRead(u8 *buff, int len) and callBackWrite() functions - * - */ - -class AndroidAccessory { -public: - - - -/** Create a AndroidAccessory object - * - * Create a AndroidAccessoryobject with specified buffer sizes and infomation - * - * @param rbuffsize The size of the read buffer - * @param wbuffsize The size of the write buffer - * @param manufacturer The manufacturer of the accessory - * @param model The model of the accessory - * @param description A short description of the accessory - * @param version The current version of the accessory - * @param uri Some data to go with the accessory (URL or more description) - * @param serial The serial number of the accessory - */ - AndroidAccessory(int rbuffsize,int wbuffsize, - const char* manufacturer, - const char *model, - const char *description, - const char *version, - const char *uri, - const char *serial - ); - - /** Init the device - * This is meant to be implimented by the user of the class - * - * @param device Device number - * @param configuration Configuration - * @param interfaceNumber Inteface number - */ - virtual void init(int device, int configuration, int interfaceNumber); - -/** Reset the device - * This is meant to be implimented by the user of the class - * - */ - virtual void resetDevice()=0; - -/** Setup the device - * This is meant to be implimented by the user of the class. Called when the device is first intialised - * - */ - virtual void setupDevice()=0; - - /** Callback on Read - * This is meant to be implimented by the user of the class. Called when some data has been read in. - * - * @param buff The buffered read in data - * @param len The length of the packet recived - * - */ - virtual int callbackRead(u8 *buff, int len)=0; - - /** Callback after Write - * This is meant to be implimented by the user of the class. Called when the write has been finished. - * - */ - virtual int callbackWrite()=0; - - /** Write over USB - * This sends the data in the buffer over USB in a packet - * - * @param buff The buffer to write out - * @param len The length of the packet to send - * - */ - int write(u8 *buff, int len); - -/** Write over USB - * This sends the data in the buffer over USB in a packet, sends _writebuff and _writebuffsize - * - */ - int write() { - return write(_writebuff,_writebuffsize); - } - - /** Write over USB with no callback - * This sends the data in the buffer over USB in a packet, waits until the packet is sent, rather than doing a callback - * - * @param buff The buffer to write out - * @param len The length of the packet to send - * - */ - int writeNC(u8 *buff, int len); - - /** Write over USB - * This sends the data in the buffer over USB in a packet, waits until the packet is sent, rather than doing a callback, sends _writebuff and _writebuffsize - * - */ - int writeNC() { - return writeNC(_writebuff,_writebuffsize); - } - - /** Read the buffer USB - * This sends the data in the buffer over USB in a packet, waits until the packet is sent, rather than doing a callback - * - * @param buff The buffer to read into - * @param len The length of the packet to read in - * - * @param returns The number of bytes read - * - */ - int read(u8 *buff, int len); - - - void adkEnd() { - // _initok=false; - resetDevice(); - }; //if connection close - bool switchDevice(int device); - - //buffer - u8* _readbuff; - int _readbuffsize; - u8* _writebuff; - int _writebuffsize; - u8* _strbuff;//255bytes; - void sendString(const char *str); - -private: - - void sendString(int device, int index, const char *str); - int getProtocol(int device); - - const char *manufacturer; - const char *model; - const char *description; - const char *version; - const char *uri; - const char *serial; - - //endpoints - int input_ep; - int output_ep; - - int _device; - int _configuration; - int _interfaceNumber; - - //bool _initok; - -}; - -extern AndroidAccessory* _adk; //declared in cpp - - -#endif \ No newline at end of file
--- a/Android/AndroidAccessory/USBHost/USBHost.cpp Fri Apr 05 10:58:42 2013 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,1093 +0,0 @@ - -/* -Copyright (c) 2010 Peter Barrett - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in -all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -THE SOFTWARE. -*/ - - - -#include "mbed.h" -#include "USBHost.h" -#ifndef USBHOST_LOG - - -// Config (default uses x bytes) -#define MAX_DEVICES 8 // Max number of devices -#define MAX_ENDPOINTS_TOTAL 16 // Max number of endpoints total -#define MAX_ENDPOINTS_PER_DEVICE 8 // Max number of endpoints for any one device - -//#define USBLOG 1 -#if USBLOG -#define LOG(...) printf(__VA_ARGS__) -#else -#define LOG(...) do {} while(0) -#endif - -// USB host structures - -#define USB_RAM_SIZE 16*1024 // AHB SRAM block 1 TODO MACHINE DEPENDENT -#define USB_RAM_BASE 0x2007C000 - -#define TOKEN_SETUP 0 -#define TOKEN_IN 1 -#define TOKEN_OUT 2 - -// Status flags from hub -#define PORT_CONNECTION 0 -#define PORT_ENABLE 1 -#define PORT_SUSPEND 2 -#define PORT_OVER_CURRENT 3 -#define PORT_RESET 4 -#define PORT_POWER 8 -#define PORT_LOW_SPEED 9 - -#define C_PORT_CONNECTION 16 -#define C_PORT_ENABLE 17 -#define C_PORT_SUSPEND 18 -#define C_PORT_OVER_CURRENT 19 -#define C_PORT_RESET 20 - -typedef struct { - u8 bm_request_type; - u8 b_request; - u16 w_value; - u16 w_index; - u16 w_length; -} Setup; - - -// Hub stuff is kept private just to keep api simple -int SetPortFeature(int device, int feature, int index); -int ClearPortFeature(int device, int feature, int index); -int SetPortPower(int device, int port); -int SetPortReset(int device, int port); -int GetPortStatus(int device, int port, u32* status); - -//=================================================================== -//=================================================================== -// Hardware defines - -// HcControl -#define PeriodicListEnable 0x00000004 -#define IsochronousEnable 0x00000008 -#define ControlListEnable 0x00000010 -#define BulkListEnable 0x00000020 -#define OperationalMask 0x00000080 -#define HostControllerFunctionalState 0x000000C0 - -// HcCommandStatus -#define HostControllerReset 0x00000001 -#define ControlListFilled 0x00000002 -#define BulkListFilled 0x00000004 - -// HcInterruptStatus Register -#define WritebackDoneHead 0x00000002 -#define StartofFrame 0x00000004 -#define ResumeDetected 0x00000008 -#define UnrecoverableError 0x00000010 -#define FrameNumberOverflow 0x00000020 -#define RootHubStatusChange 0x00000040 -#define OwnershipChange 0x00000080 -#define MasterInterruptEnable 0x80000000 - -// HcRhStatus -#define SetGlobalPower 0x00010000 -#define DeviceRemoteWakeupEnable 0x00008000 - -// HcRhPortStatus (hub 0, port 1) -#define CurrentConnectStatus 0x00000001 -#define PortEnableStatus 0x00000002 -#define PortSuspendStatus 0x00000004 -#define PortOverCurrentIndicator 0x00000008 -#define PortResetStatus 0x00000010 - -#define PortPowerStatus 0x00000100 -#define LowspeedDevice 0x00000200 -#define HighspeedDevice 0x00000400 - -#define ConnectStatusChange (CurrentConnectStatus << 16) -#define PortResetStatusChange (PortResetStatus << 16) - - -#define TD_ROUNDING (u32)0x00040000 -#define TD_SETUP (u32)0x00000000 -#define TD_IN (u32)0x00100000 -#define TD_OUT (u32)0x00080000 -#define TD_DELAY_INT(x) (u32)((x) << 21) -#define TD_TOGGLE_0 (u32)0x02000000 -#define TD_TOGGLE_1 (u32)0x03000000 -#define TD_CC (u32)0xF0000000 - -// HostController EndPoint Descriptor -typedef struct { - volatile u32 Control; - volatile u32 TailTd; - volatile u32 HeadTd; - volatile u32 Next; -} HCED; - -// HostController Transfer Descriptor -typedef struct { - volatile u32 Control; - volatile u32 CurrBufPtr; - volatile u32 Next; - volatile u32 BufEnd; -} HCTD; - -// Host Controller Communication Area -typedef struct { - volatile u32 InterruptTable[32]; - volatile u16 FrameNumber; - volatile u16 FrameNumberPad; - volatile u32 DoneHead; - volatile u8 Reserved[120]; -} HCCA; - -//==================================================================================== -//==================================================================================== - -class HostController; -class Endpoint; -class Device; - -// must be 3*16 bytes long -class Endpoint -{ -public: - HCED EndpointDescriptor; // Pointer to EndpointDescriptor == Pointer to Endpoint - HCTD TDHead; - - enum State - { - Free, - NotQueued, - Idle, - SetupQueued, - DataQueued, - StatusQueued, - CallbackPending - }; - - volatile u8 CurrentState; - u8 Flags; // 0x80 In, 0x03 mask endpoint type - - u16 Length; - u8* Data; - USBCallback Callback; // Must be a multiple of 16 bytes long - void* UserData; - - int Address() - { - int ep = (EndpointDescriptor.Control >> 7) & 0xF; - if (ep) - ep |= Flags & 0x80; - return ep; - } - - int Device() - { - return EndpointDescriptor.Control & 0x7F; - } - - int Status() - { - return (TDHead.Control >> 28) & 0xF; - } - - u32 Enqueue(u32 head) - { - if (CurrentState == NotQueued) - { - EndpointDescriptor.Next = head; - head = (u32)&EndpointDescriptor; - CurrentState = Idle; - } - return head; - } -}; - -class Device -{ -public: - u8 _endpointMap[MAX_ENDPOINTS_PER_DEVICE*2]; - u8 Hub; - u8 Port; - u8 Addr; - u8 Pad; - - // Only if this device is a hub - u8 HubPortCount; // nonzero if this is a hub - u8 HubInterruptData; - u8 HubMap; - u8 HubMask; - - int Flags; // 1 = Disconnected - - Setup SetupBuffer; - - // Allocate endpoint zero - int Init(DeviceDescriptor* d, int hub, int port, int addr, int lowSpeed) - { - Hub = hub; - Port = port; - Addr = addr; - Flags = lowSpeed; - memset(_endpointMap,0xFF,sizeof(_endpointMap)); - return 0; - } - - int SetEndpointIndex(int ep, int endpointIndex) - { - for (int i = 0; i < MAX_ENDPOINTS_PER_DEVICE*2; i += 2) - { - if (_endpointMap[i] == 0xFF) // Add endpoint to map - { - _endpointMap[i] = ep; - _endpointMap[i+1] = endpointIndex; - return 0; - } - } - return ERR_ENDPOINT_NONE_LEFT; - } - - int GetEndpointIndex(int ep) - { - for (int i = 0; i < MAX_ENDPOINTS_PER_DEVICE*2; i += 2) - { - if (_endpointMap[i] == ep) - return _endpointMap[i+1]; - if (_endpointMap[i] == 0xFF) - break; - } - return -1; - } -}; - -class HostController -{ -public: - HCCA CommunicationArea; - Endpoint Endpoints[MAX_ENDPOINTS_TOTAL]; // Multiple of 16 - - Endpoint EndpointZero; // For device enumeration - HCTD _commonTail; - Setup _setupZero; - - Device Devices[MAX_DEVICES]; - u32 _frameNumber; // 32 bit ms counter - - u8 _callbacksPending; // Endpoints with callbacks are pending, set from ISR via ProcessDoneQueue - u8 _rootHubStatusChange; // Root hub status has changed, set from ISR - u8 _unused0; - u8 _unused1; - - u8 _connectPending; // Reset has initiated a connect - u8 _connectCountdown; // Number of ms left after reset before we can connect - u8 _connectHub; // Will connect on this hub - u8 _connectPort; // ... and this port - - u8 SRAM[0]; // Start of free SRAM - - void Loop() - { - u16 elapsed = CommunicationArea.FrameNumber - (u16)_frameNumber; // extend to 32 bits - _frameNumber += elapsed; - - // Do callbacks, if any - while (_callbacksPending) - { - for (int i = 0; i < MAX_ENDPOINTS_TOTAL; i++) - { - Endpoint* endpoint = Endpoints + i; - if (endpoint->CurrentState == Endpoint::CallbackPending) - { - LOG("Sorting Callbacks %i\n\r",endpoint->CurrentState); - _callbacksPending--; - endpoint->CurrentState = Endpoint::Idle; - LOG("SatusChanged %i\n\r",endpoint->CurrentState); - endpoint->Callback(endpoint->Device(),endpoint->Address(),endpoint->Status(),endpoint->Data,endpoint->Length,endpoint->UserData); - } - } - } - - // Deal with changes on the root hub - if (_rootHubStatusChange) - { - u32 status = LPC_USB->HcRhPortStatus1; - _rootHubStatusChange = 0; - if (status >> 16) - { - HubStatusChange(0,1,status); - LPC_USB->HcRhPortStatus1 = status & 0xFFFF0000; // clear status changes - } - } - - // Connect after reset timeout - if (_connectCountdown) - { - if (elapsed >= _connectCountdown) - { - _connectCountdown = 0; - Connect(_connectHub,_connectPort & 0x7F,_connectPort & 0x80); - } else - _connectCountdown -= elapsed; - } - } - - // HubInterrupt - bitmap in dev->HubInterruptData - void HubInterrupt(int device) - { - Device* dev = &Devices[device-1]; - for (int i = 0; i < dev->HubPortCount; i++) - { - int port = i+1; - if (dev->HubInterruptData & (1 << port)) - { - u32 status = 0; - GetPortStatus(device,port,&status); - if (status >> 16) - { - if (_connectPending && (status & ConnectStatusChange)) - continue; // Don't connect again until previous device has been added and addressed - - HubStatusChange(device,port,status); - if (status & ConnectStatusChange) - ClearPortFeature(device,C_PORT_CONNECTION,port); - if (status & PortResetStatusChange) - ClearPortFeature(device,C_PORT_RESET,port); - } - } - } - } - - static void HubInterruptCallback(int device, int endpoint, int status, u8* data, int len, void* userData) - { - HostController* controller = (HostController*)userData; - if (status == 0) - controller->HubInterrupt(device); - USBInterruptTransfer(device,endpoint,data,1,HubInterruptCallback,userData); - } - - int InitHub(int device) - { - u8 buf[16]; - int r= USBControlTransfer(device,DEVICE_TO_HOST | REQUEST_TYPE_CLASS | RECIPIENT_DEVICE,GET_DESCRIPTOR,(DESCRIPTOR_TYPE_HUB << 8),0,buf,sizeof(buf)); - if (r < 0) - return ERR_HUB_INIT_FAILED; - - // turn on power on the hubs ports - Device* dev = &Devices[device-1]; - int ports = buf[2]; - dev->HubPortCount = ports; - for (int i = 0; i < ports; i++) - SetPortPower(device,i+1); - - // Enable hub change interrupts - return USBInterruptTransfer(device,0x81,&dev->HubInterruptData,1,HubInterruptCallback,this); - } - - int AddEndpoint(int device, int ep, int attributes, int maxPacketSize, int interval) - { - LOG("AddEndpoint D:%02X A:%02X T:%02X P:%04X I:%02X\r\n",device,ep,attributes,maxPacketSize,interval); - Device* dev = &Devices[device-1]; - Endpoint* endpoint = AllocateEndpoint(device,ep,attributes,maxPacketSize); - if (!endpoint) - return ERR_ENDPOINT_NONE_LEFT; - dev->SetEndpointIndex(ep,endpoint - Endpoints); - endpoint->EndpointDescriptor.Control |= dev->Flags; // Map in slow speed - return 0; // TODO ed->bInterval - } - - int AddEndpoint(int device, EndpointDescriptor* ed) - { - return AddEndpoint(device,ed->bEndpointAddress,ed->bmAttributes,ed->wMaxPacketSize,ed->bInterval); - } - - // allocate a endpoint - Endpoint* AllocateEndpoint(int device, int endpointAddress, int type, int maxPacketSize) - { - for (int i = 0; i < MAX_ENDPOINTS_TOTAL; i++) - { - Endpoint* ep = &Endpoints[i]; - if (ep->CurrentState == 0) - { - //LOG("Allocated endpoint %d to %02X:%02X\r\n",i,device,endpointAddress); - ep->Flags = (endpointAddress & 0x80) | (type & 3); - ep->CurrentState = Endpoint::NotQueued; - ep->EndpointDescriptor.Control = (maxPacketSize << 16) | ((endpointAddress & 0x7F) << 7) | device; - return ep; - } - } - return 0; - } - - Endpoint* GetEndpoint(int device, int ep) - { - if (device == 0) - { - //printf("WARNING: USING DEVICE 0\n"); - return &EndpointZero; - } - if (device > MAX_DEVICES) - return 0; - int i = Devices[device-1].GetEndpointIndex(ep); - if (i == -1) - return 0; - return Endpoints + i; - } - - int Transfer(Endpoint* endpoint, int token, u8* data, int len, int state) - { - //LOG("Transfer %02X T:%d Len:%d S:%d\r\n",endpoint->Address(),token,len,state); - - int toggle = 0; - if (endpoint->Address() == 0) - toggle = (token == TOKEN_SETUP) ? TD_TOGGLE_0 : TD_TOGGLE_1; - - if (token != TOKEN_SETUP) - token = (token == TOKEN_IN ? TD_IN : TD_OUT); - - HCTD* head = &endpoint->TDHead; - HCTD* tail = &_commonTail; - - head->Control = TD_ROUNDING | token | TD_DELAY_INT(0) | toggle | TD_CC; - head->CurrBufPtr = (u32)data; - head->BufEnd = (u32)(data + len - 1); - head->Next = (u32)tail; - - HCED* ed = &endpoint->EndpointDescriptor; - ed->HeadTd = (u32)head | (ed->HeadTd & 0x00000002); // carry toggle - ed->TailTd = (u32)tail; - - //HCTD* td = head; - //LOG("%04X TD %08X %08X %08X Next:%08X\r\n",CommunicationArea.FrameNumber,td->Control,td->CurrBufPtr,td->BufEnd,td->Next); - //LOG("%04X ED %08X %08X %08X\r\n",CommunicationArea.FrameNumber,ed->Control,ed->HeadTd,ed->TailTd); - - switch (endpoint->Flags & 3) - { - case ENDPOINT_CONTROL: - LPC_USB->HcControlHeadED = endpoint->Enqueue(LPC_USB->HcControlHeadED); // May change state NotQueued->Idle - endpoint->CurrentState = state; // Get in before an int - LPC_USB->HcCommandStatus = LPC_USB->HcCommandStatus | ControlListFilled; - LPC_USB->HcControl = LPC_USB->HcControl | ControlListEnable; - break; - - case ENDPOINT_BULK: - LPC_USB->HcBulkHeadED = endpoint->Enqueue(LPC_USB->HcBulkHeadED); - endpoint->CurrentState = state; - LPC_USB->HcCommandStatus = LPC_USB->HcCommandStatus | BulkListFilled; - LPC_USB->HcControl = LPC_USB->HcControl | BulkListEnable; - break; - - case ENDPOINT_INTERRUPT: - CommunicationArea.InterruptTable[0] = endpoint->Enqueue(CommunicationArea.InterruptTable[0]); - endpoint->CurrentState = state; - LPC_USB->HcControl |= PeriodicListEnable; - break; - } - return 0; - } - - // Remove an endpoint from an active queue - bool Remove(HCED* ed, volatile HCED** queue) - { - if (*queue == 0) - return false; - if (*queue == (volatile HCED*)ed) - { - *queue = (volatile HCED*)ed->Next; // At head of queue - return true; - } - - volatile HCED* head = *queue; - while (head) - { - if (head->Next == (u32)ed) - { - head->Next = ed->Next; - return true; - } - head = (volatile HCED*)head->Next; - } - return false; - } - - void Release(Endpoint* endpoint) - { - if (endpoint->CurrentState == Endpoint::NotQueued) - { - // Never event used it, nothing to do - } - else - { - HCED* ed = (HCED*)endpoint; - ed->Control |= 0x4000; // SKIP - switch (endpoint->Flags & 0x03) - { - case ENDPOINT_CONTROL: - Remove(ed,(volatile HCED**)&LPC_USB->HcControlHeadED); - break; - case ENDPOINT_BULK: - Remove(ed,(volatile HCED**)&LPC_USB->HcBulkHeadED); - break; - case ENDPOINT_INTERRUPT: - for (int i = 0; i < 32; i++) - Remove(ed,(volatile HCED**)&CommunicationArea.InterruptTable[i]); - break; - } - - u16 fn = CommunicationArea.FrameNumber; - while (fn == CommunicationArea.FrameNumber) - ; // Wait for next frame - - } - - // In theory, the endpoint is now dead. - // TODO: Will Callbacks ever be pending? BUGBUG - memset(endpoint,0,sizeof(Endpoint)); - } - - // Pop the last TD from the list - HCTD* Reverse(HCTD* current) - { - HCTD *result = NULL,*temp; - while (current) - { - temp = (HCTD*)current->Next; - current->Next = (u32)result; - result = current; - current = temp; - } - return result; - } - - // Called from interrupt... - // Control endpoints use a state machine to progress through the transfers - void ProcessDoneQueue(u32 tdList) - { - HCTD* list = Reverse((HCTD*)tdList); - while (list) - { - Endpoint* endpoint = (Endpoint*)(list-1); - list = (HCTD*)list->Next; - int ep = endpoint->Address(); - bool in = endpoint->Flags & 0x80; - int status = (endpoint->TDHead.Control >> 28) & 0xF; - - //LOG("ProcessDoneQueue %02X %08X\r\n",ep,endpoint->TDHead.Control); - - if (status != 0) - { - LOG("ProcessDoneQueue status %02X %d\r\n",ep,status); - endpoint->CurrentState = Endpoint::Idle; - } else { - switch (endpoint->CurrentState) - { - case Endpoint::SetupQueued: - if (endpoint->Length == 0) - Transfer(endpoint,in ? TOKEN_OUT : TOKEN_IN,0,0,Endpoint::StatusQueued); // Skip Data Phase - else - Transfer(endpoint,in ? TOKEN_IN : TOKEN_OUT,endpoint->Data,endpoint->Length, Endpoint::DataQueued); // Setup is done, now Data - break; - - case Endpoint::DataQueued: - if (endpoint->TDHead.CurrBufPtr) - endpoint->Length = endpoint->TDHead.CurrBufPtr - (u32)endpoint->Data; - - if (ep == 0) - Transfer(endpoint,in ? TOKEN_OUT : TOKEN_IN,0,0,Endpoint::StatusQueued); // Data is done, now Status, Control only - else - endpoint->CurrentState = Endpoint::Idle; - break; - - case Endpoint::StatusQueued: // Transaction is done - endpoint->CurrentState = Endpoint::Idle; - break; - } - } - - // Complete, flag if we need a callback - if (endpoint->Callback && endpoint->CurrentState == Endpoint::Idle) - { - endpoint->CurrentState = Endpoint::CallbackPending; - _callbacksPending++; - } - } - } - - // Hack to reset devices that don't want to connect - int AddDevice(int hub, int port, bool isLowSpeed) - { - int device = AddDeviceCore(hub,port,isLowSpeed); - if (device < 0) - { - LOG("========RETRY ADD DEVICE========\r\n"); // This will go for ever.. TODO power cycle root? - Disconnect(hub,port); // Could not read descriptor at assigned address, reset this port and try again - ResetPort(hub,port); // Cheap bluetooth dongles often need this on a hotplug - return -1; - } - return device; - } - - int AddDeviceCore(int hub, int port, bool isLowSpeed) - { - int lowSpeed = isLowSpeed ? 0x2000 : 0; - DeviceDescriptor desc; - EndpointZero.EndpointDescriptor.Control = (8 << 16) | lowSpeed; // MaxPacketSize == 8 - int r = GetDescriptor(0,DESCRIPTOR_TYPE_DEVICE,0,(u8*)&desc,8); - if (r < 0) - { - LOG("FAILED TO LOAD DESCRIPTOR FOR DEVICE 0\r\n"); - return r; - } - - EndpointZero.EndpointDescriptor.Control = (desc.bMaxPacketSize << 16) | lowSpeed; // Actual MaxPacketSize - r = GetDescriptor(0,DESCRIPTOR_TYPE_DEVICE,0,(u8*)&desc,sizeof(desc)); - if (r < 0) - return r; - - LOG("\nClass %02X found %04X:%04X\r\n",desc.bDeviceClass,desc.idVendor,desc.idProduct); - - // Now assign the device an address, move off EndpointZero - int device = 0; - for (int i = 0; i < MAX_DEVICES; i++) - { - if (Devices[i].Port == 0) - { - device = i+1; - break; - } - } - if (!device) - return ERR_DEVICE_NONE_LEFT; - - r = SetAddress(0,device); - if (r) - return r; - DelayMS(2); - - // Now at a nonzero address, create control endpoint - Device* dev = &Devices[device-1]; - dev->Init(&desc,hub,port,device,lowSpeed); - AddEndpoint(device,0,ENDPOINT_CONTROL,desc.bMaxPacketSize,0); - _connectPending = 0; - - // Verify this all works - r = GetDescriptor(device,DESCRIPTOR_TYPE_DEVICE,0,(u8*)&desc,sizeof(desc)); - if (r < 0) - return r; - - // Set to interface 0 by default - // Calls LoadDevice if interface is found - r = SetConfigurationAndInterface(device,1,0,&desc); - - if (desc.bDeviceClass == CLASS_HUB) - InitHub(device); // Handle hubs in this code - - return device; - } - - // Walk descriptors and create endpoints for a given device - // TODO configuration !=1, alternate settings etc. - int SetConfigurationAndInterface(int device, int configuration, int interfaceNumber, DeviceDescriptor* desc) - { - u8 buffer[255]; - int err = GetDescriptor(device,DESCRIPTOR_TYPE_CONFIGURATION,0,buffer,sizeof(buffer)); - if (err < 0) - return err; - - err = SetConfiguration(device,configuration); - if (err < 0) - return err; - - // Add the endpoints for this interface - int len = buffer[2] | (buffer[3] << 8); - u8* d = buffer; - u8* end = d + len; - InterfaceDescriptor* found = 0; - while (d < end) - { - if (d[1] == DESCRIPTOR_TYPE_INTERFACE) - { - InterfaceDescriptor* id = (InterfaceDescriptor*)d; - if (id->bInterfaceNumber == interfaceNumber) - { - found = id; - d += d[0]; - while (d < end && d[1] != DESCRIPTOR_TYPE_INTERFACE) - { - switch (d[1]) - { - case DESCRIPTOR_TYPE_ENDPOINT: - AddEndpoint(device,(EndpointDescriptor*)d); - break; - default: - LOG("Skipping descriptor %02X (%d bytes)\r\n",d[1],d[0]); - } - d += d[0]; - } - } - } - d += d[0]; - } - - if (!found) - return ERR_INTERFACE_NOT_FOUND; - OnLoadDevice(device,desc,found); - return 0; - } - - void Init() - { - LOG("USB INIT (Controller is %d bytes)\r\n",sizeof(*this)); - memset(this,0,sizeof(HostController)); - EndpointZero.CurrentState = Endpoint::NotQueued; - HWInit(&CommunicationArea); - DelayMS(10); - } - - void ResetPort(int hub, int port) - { - LOG("ResetPort Hub:%d Port:%d\r\n",hub,port); - _connectPending++; // Only reset/add 1 device at a time - if (hub == 0) - LPC_USB->HcRhPortStatus1 = PortResetStatus; // Reset Root Hub, port 1 - else - SetPortReset(hub,port); // or reset other hub - } - - void Disconnect(int hub, int port) - { - LOG("Disconnect Hub:%d Port:%d\r\n",hub,port); // Mark a device for destruction - for (int i = 0; i < MAX_DEVICES; i++) - { - Device* dev = Devices + i; - if (dev->Port == port && dev->Hub == hub) - { - // Disconnect everything that is attached to this device if it is a hub - for (int p = 0; p < dev->HubPortCount; p++) - Disconnect(i+1,p+1); - - // Now release endpoints - for (int j = 1; j < MAX_ENDPOINTS_PER_DEVICE*2; j += 2) - { - u8 endpointIndex = dev->_endpointMap[j]; - if (endpointIndex != 0xFF) - Release(Endpoints + endpointIndex); - } - dev->Port = 0; // Device is now free - dev->Flags = 0; - return; - } - } - } - - // called after reset - void Connect(int hub, int port, bool lowspeed) - { - LOG("Connect Hub:%d Port:%d %s\r\n",hub,port,lowspeed ? "slow" : "full"); - AddDevice(hub,port,lowspeed); - } - - // Called from interrupt - void HubStatusChange(int hub, int port, u32 status) - { - LOG("HubStatusChange Hub:%d Port:%d %08X\r\n",hub,port,status); - if (status & ConnectStatusChange) - { - if (status & CurrentConnectStatus) // Connecting - ResetPort(hub,port); // Reset to initiate connect (state machine?) - else - Disconnect(hub,port); - } - - if (status & PortResetStatusChange) - { - if (!(status & PortResetStatus)) - { - _connectCountdown = 200; // Schedule a connection in 200ms - if (status & LowspeedDevice) - port |= 0x80; - _connectHub = hub; - _connectPort = port; - } - } - } - - #define HOST_CLK_EN (1<<0) - #define PORTSEL_CLK_EN (1<<3) - #define AHB_CLK_EN (1<<4) - #define CLOCK_MASK (HOST_CLK_EN | PORTSEL_CLK_EN | AHB_CLK_EN) - - #define FRAMEINTERVAL (12000-1) // 1ms - #define DEFAULT_FMINTERVAL ((((6 * (FRAMEINTERVAL - 210)) / 7) << 16) | FRAMEINTERVAL) - - void DelayMS(int ms) - { - u16 f = ms + CommunicationArea.FrameNumber; - while (f != CommunicationArea.FrameNumber) - ; - } - - static void HWInit(HCCA* cca) - { - NVIC_DisableIRQ(USB_IRQn); - - // turn on power for USB - LPC_SC->PCONP |= (1UL<<31); - // Enable USB host clock, port selection and AHB clock - LPC_USB->USBClkCtrl |= CLOCK_MASK; - // Wait for clocks to become available - while ((LPC_USB->USBClkSt & CLOCK_MASK) != CLOCK_MASK) - ; - - // We are a Host - LPC_USB->OTGStCtrl |= 1; - LPC_USB->USBClkCtrl &= ~PORTSEL_CLK_EN; // we don't need port selection clock until we do OTG - - // configure USB pins - LPC_PINCON->PINSEL1 &= ~((3<<26)|(3<<28)); - LPC_PINCON->PINSEL1 |= ((1<<26)|(1<<28)); // USB D+/D- - - LPC_PINCON->PINSEL3 &= ~((3 << 6) | (3 << 22)); // USB_PPWR, USB_OVRCR - LPC_PINCON->PINSEL3 |= ((2 << 6) | (2 << 22)); - - LPC_PINCON->PINSEL4 &= ~(3 << 18); // USB_CONNECT - LPC_PINCON->PINSEL4 |= (1 << 18); - - // Reset OHCI block - LPC_USB->HcControl = 0; - LPC_USB->HcControlHeadED = 0; - LPC_USB->HcBulkHeadED = 0; - - LPC_USB->HcCommandStatus = HostControllerReset; - LPC_USB->HcFmInterval = DEFAULT_FMINTERVAL; - LPC_USB->HcPeriodicStart = FRAMEINTERVAL*90/100; - - LPC_USB->HcControl = (LPC_USB->HcControl & (~HostControllerFunctionalState)) | OperationalMask; - LPC_USB->HcRhStatus = SetGlobalPower; - - LPC_USB->HcHCCA = (u32)cca; - LPC_USB->HcInterruptStatus |= LPC_USB->HcInterruptStatus; - LPC_USB->HcInterruptEnable = MasterInterruptEnable | WritebackDoneHead | RootHubStatusChange | FrameNumberOverflow; - - NVIC_SetPriority(USB_IRQn, 0); - NVIC_EnableIRQ(USB_IRQn); - while (cca->FrameNumber < 10) - ; // 10ms delay before diving in - } -}; - -//==================================================================================== -//==================================================================================== -// Host controller instance and Interrupt handler - -static HostController _controller __attribute__((at(USB_RAM_BASE))); - -extern "C" void USB_IRQHandler(void) __irq; -void USB_IRQHandler (void) __irq -{ - u32 int_status = LPC_USB->HcInterruptStatus; - - if(int_status & UnrecoverableError) //Error - { - LOG("USB_IRQHandler UnrecoverableError Please reset\r\n"); - } - - - if (int_status & RootHubStatusChange) // Root hub status change - _controller._rootHubStatusChange++; // Just flag the controller, will be processed in USBLoop - - u32 head = 0; - if (int_status & WritebackDoneHead) - { - head = _controller.CommunicationArea.DoneHead; // Writeback Done - _controller.CommunicationArea.DoneHead = 0; - } - LPC_USB->HcInterruptStatus = int_status; - - if (head) - _controller.ProcessDoneQueue(head); // TODO - low bit can be set BUGBUG -} - -//==================================================================================== -//==================================================================================== -// API Methods - -void USBInit() -{ - return _controller.Init(); -} - -void USBLoop() -{ - - return _controller.Loop(); - -} - -u8* USBGetBuffer(u32* len) -{ - *len = USB_RAM_SIZE - sizeof(HostController); - return _controller.SRAM; -} - -static Setup* GetSetup(int device) -{ - if (device == 0) - return &_controller._setupZero; - - if (device < 1 || device > MAX_DEVICES) - return 0; - return &_controller.Devices[device-1].SetupBuffer; -} - -// Loop until IO on endpoint is complete -static int WaitIODone(Endpoint* endpoint) -{ - LOG("Waiting\n\r"); - if (endpoint->CurrentState == Endpoint::NotQueued) - return 0; - while (endpoint->CurrentState != Endpoint::Idle) - //LOG("Loopz"); - USBLoop(); // May generate callbacks, mount or unmount devices etc - int status = endpoint->Status(); - if (status == 0) - return endpoint->Length; - LOG("DATA SENT\n\r"); - return -status; -} - -int USBTransfer(int device, int ep, u8 flags, u8* data, int length, USBCallback callback, void* userData) -{ - Endpoint* endpoint = _controller.GetEndpoint(device,ep); - if (!endpoint) - return ERR_ENDPOINT_NOT_FOUND; - - WaitIODone(endpoint); - LOG("before ep=%x,callback=%p\r\n",endpoint,endpoint->Callback); - endpoint->Flags = flags; - endpoint->Data = data; - endpoint->Length = length; - endpoint->Callback = callback; - endpoint->UserData = userData; - LOG("ep=%x,callback=%p\r\n",ep,callback); - if (ep == 0) - _controller.Transfer(endpoint,TOKEN_SETUP,(u8*)GetSetup(device),8,Endpoint::SetupQueued); - else - _controller.Transfer(endpoint,flags & 0x80 ? TOKEN_IN : TOKEN_OUT,data,length,Endpoint::DataQueued); - if (callback) - return IO_PENDING; - return WaitIODone(endpoint); -} - -int USBControlTransfer(int device, int request_type, int request, int value, int index, u8* data, int length, USBCallback callback, void * userData) -{ - Setup* setup = GetSetup(device); - if (!setup) - return ERR_DEVICE_NOT_FOUND; - - // Async control calls may overwrite setup buffer of previous call, so we need to wait before setting up next call - WaitIODone(_controller.GetEndpoint(device,0)); - - setup->bm_request_type = request_type; - setup->b_request = request; - setup->w_value = value; - setup->w_index = index; - setup->w_length = length; - return USBTransfer(device,0,request_type & DEVICE_TO_HOST,data,length,callback,userData); -} - -int USBInterruptTransfer(int device, int ep, u8* data, int length, USBCallback callback, void* userData) -{ - return USBTransfer(device,ep,(ep & 0x80) | ENDPOINT_INTERRUPT,data,length,callback,userData); -} - -int USBBulkTransfer(int device, int ep, u8* data, int length, USBCallback callback, void* userData) -{ - return USBTransfer(device,ep,(ep & 0x80) | ENDPOINT_BULK,data,length,callback,userData); -} - -int GetDescriptor(int device, int descType,int descIndex, u8* data, int length) -{ - return USBControlTransfer(device,DEVICE_TO_HOST | RECIPIENT_DEVICE, GET_DESCRIPTOR,(descType << 8)|(descIndex), 0, data, length, 0); -} - -int GetString(int device, int index, char* dst, int length) -{ - u8 buffer[255]; - int le = GetDescriptor(device,DESCRIPTOR_TYPE_STRING,index,buffer,sizeof(buffer)); - if (le < 0) - return le; - if (length < 1) - return -1; - length <<= 1; - if (le > length) - le = length; - for (int j = 2; j < le; j += 2) - *dst++ = buffer[j]; - *dst = 0; - return (le>>1)-1; -} - -int SetAddress(int device, int new_addr) -{ - return USBControlTransfer(device,HOST_TO_DEVICE | RECIPIENT_DEVICE, SET_ADDRESS, new_addr, 0, 0, 0, 0); -} - -int SetConfiguration(int device, int configNum) -{ - return USBControlTransfer(device,HOST_TO_DEVICE | RECIPIENT_DEVICE, SET_CONFIGURATION, configNum, 0, 0, 0, 0); -} - -int SetInterface(int device, int ifNum, int altNum) -{ - return USBControlTransfer(device,HOST_TO_DEVICE | RECIPIENT_INTERFACE, SET_INTERFACE, altNum, ifNum, 0, 0, 0); -} - -// HUB stuff -int SetPortFeature(int device, int feature, int index) -{ - return USBControlTransfer(device,HOST_TO_DEVICE | REQUEST_TYPE_CLASS | RECIPIENT_OTHER,SET_FEATURE,feature,index,0,0); -} - -int ClearPortFeature(int device, int feature, int index) -{ - return USBControlTransfer(device,HOST_TO_DEVICE | REQUEST_TYPE_CLASS | RECIPIENT_OTHER,CLEAR_FEATURE,feature,index,0,0); -} - -int SetPortPower(int device, int port) -{ - int r = SetPortFeature(device,PORT_POWER,port); - _controller.DelayMS(20); // 80ms to turn on a hubs power... DESCRIPTOR? todo - return r; -} - -int SetPortReset(int device, int port) -{ - return SetPortFeature(device,PORT_RESET,port); -} - -int GetPortStatus(int device, int port, u32* status) -{ - return USBControlTransfer(device,DEVICE_TO_HOST | REQUEST_TYPE_CLASS | RECIPIENT_OTHER,GET_STATUS,0,port,(u8*)status,4); -} - -#endif \ No newline at end of file
--- a/Android/AndroidAccessory/USBHost/USBHost.h Fri Apr 05 10:58:42 2013 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,221 +0,0 @@ - -/* -Copyright (c) 2010 Peter Barrett - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in -all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -THE SOFTWARE. -*/ - - -#ifndef USBHOST_H -#define USBHOST_H - -//log mode on off -//#define USBHOST_LOG - - -#ifndef u8 -typedef unsigned char u8; -typedef unsigned short u16; -typedef unsigned long u32; - -typedef char s8; -typedef short s16; -typedef char s32; -#endif - -#define ENDPOINT_CONTROL 0 -#define ENDPOINT_ISOCRONOUS 1 -#define ENDPOINT_BULK 2 -#define ENDPOINT_INTERRUPT 3 - -#define DESCRIPTOR_TYPE_DEVICE 1 -#define DESCRIPTOR_TYPE_CONFIGURATION 2 -#define DESCRIPTOR_TYPE_STRING 3 -#define DESCRIPTOR_TYPE_INTERFACE 4 -#define DESCRIPTOR_TYPE_ENDPOINT 5 - -#define DESCRIPTOR_TYPE_HID 0x21 -#define DESCRIPTOR_TYPE_REPORT 0x22 -#define DESCRIPTOR_TYPE_PHYSICAL 0x23 -#define DESCRIPTOR_TYPE_HUB 0x29 - -enum USB_CLASS_CODE -{ - CLASS_DEVICE, - CLASS_AUDIO, - CLASS_COMM_AND_CDC_CONTROL, - CLASS_HID, - CLASS_PHYSICAL = 0x05, - CLASS_STILL_IMAGING, - CLASS_PRINTER, - CLASS_MASS_STORAGE, - CLASS_HUB, - CLASS_CDC_DATA, - CLASS_SMART_CARD, - CLASS_CONTENT_SECURITY = 0x0D, - CLASS_VIDEO = 0x0E, - CLASS_DIAGNOSTIC_DEVICE = 0xDC, - CLASS_WIRELESS_CONTROLLER = 0xE0, - CLASS_MISCELLANEOUS = 0xEF, - CLASS_APP_SPECIFIC = 0xFE, - CLASS_VENDOR_SPECIFIC = 0xFF -}; - -#define DEVICE_TO_HOST 0x80 -#define HOST_TO_DEVICE 0x00 -#define REQUEST_TYPE_STANDARD 0x00 -#define REQUEST_TYPE_CLASS 0x20 -#define REQUEST_TYPE_VENDOR 0x40 -#define RECIPIENT_DEVICE 0x00 -#define RECIPIENT_INTERFACE 0x01 -#define RECIPIENT_ENDPOINT 0x02 -#define RECIPIENT_OTHER 0x03 - -#define GET_STATUS 0 -#define CLEAR_FEATURE 1 -#define SET_FEATURE 3 -#define SET_ADDRESS 5 -#define GET_DESCRIPTOR 6 -#define SET_DESCRIPTOR 7 -#define GET_CONFIGURATION 8 -#define SET_CONFIGURATION 9 -#define GET_INTERFACE 10 -#define SET_INTERFACE 11 -#define SYNCH_FRAME 11 - -/* HID Request Codes */ -#define HID_REQUEST_GET_REPORT 0x01 -#define HID_REQUEST_GET_IDLE 0x02 -#define HID_REQUEST_GET_PROTOCOL 0x03 -#define HID_REQUEST_SET_REPORT 0x09 -#define HID_REQUEST_SET_IDLE 0x0A -#define HID_REQUEST_SET_PROTOCOL 0x0B - -/* HID Report Types */ -#define HID_REPORT_INPUT 0x01 -#define HID_REPORT_OUTPUT 0x02 -#define HID_REPORT_FEATURE 0x03 - - -// -5 is nak -/* -0010 ACK Handshake -1010 NAK Handshake -1110 STALL Handshake -0110 NYET (No Response Yet) -*/ - -#define IO_PENDING -100 -#define ERR_ENDPOINT_NONE_LEFT -101 -#define ERR_ENDPOINT_NOT_FOUND -102 -#define ERR_DEVICE_NOT_FOUND -103 -#define ERR_DEVICE_NONE_LEFT -104 -#define ERR_HUB_INIT_FAILED -105 -#define ERR_INTERFACE_NOT_FOUND -106 - -typedef struct -{ - u8 bLength; - u8 bDescriptorType; - u16 bcdUSB; - u8 bDeviceClass; - u8 bDeviceSubClass; - u8 bDeviceProtocol; - u8 bMaxPacketSize; - u16 idVendor; - u16 idProduct; - u16 bcdDevice; // version - u8 iManufacturer; - u8 iProduct; - u8 iSerialNumber; - u8 bNumConfigurations; -} DeviceDescriptor; // 16 bytes - -typedef struct -{ - u8 bLength; - u8 bDescriptorType; - u16 wTotalLength; - u8 bNumInterfaces; - u8 bConfigurationValue; // Value to use as an argument to select this configuration - u8 iConfiguration; // Index of String Descriptor describing this configuration - u8 bmAttributes; // Bitmap D7 Reserved, set to 1. (USB 1.0 Bus Powered),D6 Self Powered,D5 Remote Wakeup,D4..0 = 0 - u8 bMaxPower; // Maximum Power Consumption in 2mA units -} ConfigurationDescriptor; - -typedef struct -{ - u8 bLength; - u8 bDescriptorType; - u8 bInterfaceNumber; - u8 bAlternateSetting; - u8 bNumEndpoints; - u8 bInterfaceClass; - u8 bInterfaceSubClass; - u8 bInterfaceProtocol; - u8 iInterface; // Index of String Descriptor Describing this interface -} InterfaceDescriptor; - -typedef struct -{ - u8 bLength; - u8 bDescriptorType; - u8 bEndpointAddress; // Bits 0:3 endpoint, Bits 7 Direction 0 = Out, 1 = In (Ignored for Control Endpoints) - u8 bmAttributes; // Bits 0:1 00 = Control, 01 = Isochronous, 10 = Bulk, 11 = Interrupt - u16 wMaxPacketSize; - u8 bInterval; // Interval for polling endpoint data transfers. -} EndpointDescriptor; - -typedef struct { - u8 bLength; - u8 bDescriptorType; - u16 bcdHID; - u8 bCountryCode; - u8 bNumDescriptors; - u8 bDescriptorType2; - u16 wDescriptorLength; -} HIDDescriptor; - -//============================================================================ -//============================================================================ - - -void USBInit(); -void USBLoop(); -u8* USBGetBuffer(u32* len); - -// Optional callback for transfers, called at interrupt time -typedef void (*USBCallback)(int device, int endpoint, int status, u8* data, int len, void* userData); - -// Transfers -int USBControlTransfer(int device, int request_type, int request, int value, int index, u8* data, int length, USBCallback callback = 0, void* userData = 0); -int USBInterruptTransfer(int device, int ep, u8* data, int length, USBCallback callback = 0, void* userData = 0); -int USBBulkTransfer(int device, int ep, u8* data, int length, USBCallback callback = 0, void* userData = 0); - -// Standard Device methods -int GetDescriptor(int device, int descType, int descIndex, u8* data, int length); -int GetString(int device, int index, char* dst, int length); -int SetAddress(int device, int new_addr); -int SetConfiguration(int device, int configNum); -int SetInterface(int device, int ifNum, int altNum); - -// Implemented to notify app of the arrival of a device -void OnLoadDevice(int device, DeviceDescriptor* deviceDesc, InterfaceDescriptor* interfaceDesc); - -#endif \ No newline at end of file
--- a/Android/AndroidAccessory/USBHost/USBHost_log.cpp Fri Apr 05 10:58:42 2013 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,1175 +0,0 @@ - -/* -Copyright (c) 2010 Peter Barrett - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in -all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -THE SOFTWARE. -*/ - - -#include "mbed.h" -#include "USBHost.h" -//#define USBHOST_LOG -#ifdef USBHOST_LOG - -// Config (default uses x bytes) -#define MAX_DEVICES 8 // Max number of devices -#define MAX_ENDPOINTS_TOTAL 16 // Max number of endpoints total -#define MAX_ENDPOINTS_PER_DEVICE 8 // Max number of endpoints for any one device - -//#define USBLOG 1 -#if USBLOG -#define LOG(...) printf(__VA_ARGS__) -#else -#define LOG(...) do {} while(0) -#endif - -// USB host structures - -#define USB_RAM_SIZE 16*1024 // AHB SRAM block 1 TODO MACHINE DEPENDENT -#define USB_RAM_BASE 0x2007C000 - -#define TOKEN_SETUP 0 -#define TOKEN_IN 1 -#define TOKEN_OUT 2 - -// Status flags from hub -#define PORT_CONNECTION 0 -#define PORT_ENABLE 1 -#define PORT_SUSPEND 2 -#define PORT_OVER_CURRENT 3 -#define PORT_RESET 4 -#define PORT_POWER 8 -#define PORT_LOW_SPEED 9 - -#define C_PORT_CONNECTION 16 -#define C_PORT_ENABLE 17 -#define C_PORT_SUSPEND 18 -#define C_PORT_OVER_CURRENT 19 -#define C_PORT_RESET 20 - -typedef struct { - u8 bm_request_type; - u8 b_request; - u16 w_value; - u16 w_index; - u16 w_length; -} Setup; - - -// Hub stuff is kept private just to keep api simple -int SetPortFeature(int device, int feature, int index); -int ClearPortFeature(int device, int feature, int index); -int SetPortPower(int device, int port); -int SetPortReset(int device, int port); -int GetPortStatus(int device, int port, u32* status); - -//=================================================================== -//=================================================================== -// Hardware defines - -// HcControl -#define PeriodicListEnable 0x00000004 -#define IsochronousEnable 0x00000008 -#define ControlListEnable 0x00000010 -#define BulkListEnable 0x00000020 -#define OperationalMask 0x00000080 -#define HostControllerFunctionalState 0x000000C0 - -// HcCommandStatus -#define HostControllerReset 0x00000001 -#define ControlListFilled 0x00000002 -#define BulkListFilled 0x00000004 - -// HcInterruptStatus Register -#define WritebackDoneHead 0x00000002 -#define StartofFrame 0x00000004 -#define ResumeDetected 0x00000008 -#define UnrecoverableError 0x00000010 -#define FrameNumberOverflow 0x00000020 -#define RootHubStatusChange 0x00000040 -#define OwnershipChange 0x00000080 -#define MasterInterruptEnable 0x80000000 - -// HcRhStatus -#define SetGlobalPower 0x00010000 -#define DeviceRemoteWakeupEnable 0x00008000 - -// HcRhPortStatus (hub 0, port 1) -#define CurrentConnectStatus 0x00000001 -#define PortEnableStatus 0x00000002 -#define PortSuspendStatus 0x00000004 -#define PortOverCurrentIndicator 0x00000008 -#define PortResetStatus 0x00000010 - -#define PortPowerStatus 0x00000100 -#define LowspeedDevice 0x00000200 -#define HighspeedDevice 0x00000400 - -#define ConnectStatusChange (CurrentConnectStatus << 16) -#define PortResetStatusChange (PortResetStatus << 16) - - -#define TD_ROUNDING (u32)0x00040000 -#define TD_SETUP (u32)0x00000000 -#define TD_IN (u32)0x00100000 -#define TD_OUT (u32)0x00080000 -#define TD_DELAY_INT(x) (u32)((x) << 21) -#define TD_TOGGLE_0 (u32)0x02000000 -#define TD_TOGGLE_1 (u32)0x03000000 -#define TD_CC (u32)0xF0000000 - -// HostController EndPoint Descriptor -typedef struct { - volatile u32 Control; - volatile u32 TailTd; - volatile u32 HeadTd; - volatile u32 Next; -} HCED; - -// HostController Transfer Descriptor -typedef struct { - volatile u32 Control; - volatile u32 CurrBufPtr; - volatile u32 Next; - volatile u32 BufEnd; -} HCTD; - -// Host Controller Communication Area -typedef struct { - volatile u32 InterruptTable[32]; - volatile u16 FrameNumber; - volatile u16 FrameNumberPad; - volatile u32 DoneHead; - volatile u8 Reserved[120]; -} HCCA; - -//==================================================================================== -//==================================================================================== - -class HostController; -class Endpoint; -class Device; - -// must be 3*16 bytes long -class Endpoint -{ -public: - HCED EndpointDescriptor; // Pointer to EndpointDescriptor == Pointer to Endpoint - HCTD TDHead; - - enum State - { - Free, - NotQueued, - Idle, - SetupQueued, - DataQueued, - StatusQueued, - CallbackPending - }; - - volatile u8 CurrentState; - u8 Flags; // 0x80 In, 0x03 mask endpoint type - - u16 Length; - u8* Data; - USBCallback Callback; // Must be a multiple of 16 bytes long - void* UserData; - - int Address() - { - int ep = (EndpointDescriptor.Control >> 7) & 0xF; - if (ep) - ep |= Flags & 0x80; - return ep; - } - - int Device() - { - return EndpointDescriptor.Control & 0x7F; - } - - int Status() - { - return (TDHead.Control >> 28) & 0xF; - } - - u32 Enqueue(u32 head) - { - if (CurrentState == NotQueued) - { - EndpointDescriptor.Next = head; - head = (u32)&EndpointDescriptor; - CurrentState = Idle; - } - return head; - } -}; - -class Device -{ -public: - u8 _endpointMap[MAX_ENDPOINTS_PER_DEVICE*2]; - u8 Hub; - u8 Port; - u8 Addr; - u8 Pad; - - // Only if this device is a hub - u8 HubPortCount; // nonzero if this is a hub - u8 HubInterruptData; - u8 HubMap; - u8 HubMask; - - int Flags; // 1 = Disconnected - - Setup SetupBuffer; - - // Allocate endpoint zero - int Init(DeviceDescriptor* d, int hub, int port, int addr, int lowSpeed) - { - Hub = hub; - Port = port; - Addr = addr; - Flags = lowSpeed; - memset(_endpointMap,0xFF,sizeof(_endpointMap)); - return 0; - } - - int SetEndpointIndex(int ep, int endpointIndex) - { - for (int i = 0; i < MAX_ENDPOINTS_PER_DEVICE*2; i += 2) - { - if (_endpointMap[i] == 0xFF) // Add endpoint to map - { - _endpointMap[i] = ep; - _endpointMap[i+1] = endpointIndex; - return 0; - } - } - return ERR_ENDPOINT_NONE_LEFT; - } - - int GetEndpointIndex(int ep) - { - for (int i = 0; i < MAX_ENDPOINTS_PER_DEVICE*2; i += 2) - { - if (_endpointMap[i] == ep) - return _endpointMap[i+1]; - if (_endpointMap[i] == 0xFF) - break; - } - return -1; - } -}; - -class HostController -{ -public: - HCCA CommunicationArea; - Endpoint Endpoints[MAX_ENDPOINTS_TOTAL]; // Multiple of 16 - - Endpoint EndpointZero; // For device enumeration - HCTD _commonTail; - Setup _setupZero; - - Device Devices[MAX_DEVICES]; - u32 _frameNumber; // 32 bit ms counter - - u8 _callbacksPending; // Endpoints with callbacks are pending, set from ISR via ProcessDoneQueue - u8 _rootHubStatusChange; // Root hub status has changed, set from ISR - u8 _unused0; - u8 _unused1; - - u8 _connectPending; // Reset has initiated a connect - u8 _connectCountdown; // Number of ms left after reset before we can connect - u8 _connectHub; // Will connect on this hub - u8 _connectPort; // ... and this port - - u8 SRAM[0]; // Start of free SRAM - - void Loop() - { - // printf("Enter Loop \r\n"); - u16 elapsed = CommunicationArea.FrameNumber - (u16)_frameNumber; // extend to 32 bits - _frameNumber += elapsed; - - // Do callbacks, if any - while (_callbacksPending) - { - for (int i = 0; i < MAX_ENDPOINTS_TOTAL; i++) - { - Endpoint* endpoint = Endpoints + i; - if (endpoint->CurrentState == Endpoint::CallbackPending) - { - _callbacksPending--; - endpoint->CurrentState = Endpoint::Idle; - LOG("CurrentState Change Idle endpoint(%d) currentState =%d\r\n",endpoint->Address(),endpoint->CurrentState); - endpoint->Callback(endpoint->Device(),endpoint->Address(),endpoint->Status(),endpoint->Data,endpoint->Length,endpoint->UserData); - } - } - } - - // Deal with changes on the root hub - if (_rootHubStatusChange) - { - u32 status = LPC_USB->HcRhPortStatus1; - _rootHubStatusChange = 0; - if (status >> 16) - { - HubStatusChange(0,1,status); - LPC_USB->HcRhPortStatus1 = status & 0xFFFF0000; // clear status changes - } - } - - // Connect after reset timeout - if (_connectCountdown) - { - if (elapsed >= _connectCountdown) - { - _connectCountdown = 0; - Connect(_connectHub,_connectPort & 0x7F,_connectPort & 0x80); - } else - _connectCountdown -= elapsed; - } - - // printf("Outer Loop \r\n"); - } - - // HubInterrupt - bitmap in dev->HubInterruptData - void HubInterrupt(int device) - { - Device* dev = &Devices[device-1]; - for (int i = 0; i < dev->HubPortCount; i++) - { - int port = i+1; - if (dev->HubInterruptData & (1 << port)) - { - u32 status = 0; - GetPortStatus(device,port,&status); - if (status >> 16) - { - if (_connectPending && (status & ConnectStatusChange)) - continue; // Don't connect again until previous device has been added and addressed - - HubStatusChange(device,port,status); - if (status & ConnectStatusChange) - ClearPortFeature(device,C_PORT_CONNECTION,port); - if (status & PortResetStatusChange) - ClearPortFeature(device,C_PORT_RESET,port); - } - } - } - } - - static void HubInterruptCallback(int device, int endpoint, int status, u8* data, int len, void* userData) - { - HostController* controller = (HostController*)userData; - if (status == 0) - controller->HubInterrupt(device); - USBInterruptTransfer(device,endpoint,data,1,HubInterruptCallback,userData); - } - - int InitHub(int device) - { - u8 buf[16]; - int r= USBControlTransfer(device,DEVICE_TO_HOST | REQUEST_TYPE_CLASS | RECIPIENT_DEVICE,GET_DESCRIPTOR,(DESCRIPTOR_TYPE_HUB << 8),0,buf,sizeof(buf)); - if (r < 0) - return ERR_HUB_INIT_FAILED; - - // turn on power on the hubs ports - Device* dev = &Devices[device-1]; - int ports = buf[2]; - dev->HubPortCount = ports; - for (int i = 0; i < ports; i++) - SetPortPower(device,i+1); - - // Enable hub change interrupts - return USBInterruptTransfer(device,0x81,&dev->HubInterruptData,1,HubInterruptCallback,this); - } - - int AddEndpoint(int device, int ep, int attributes, int maxPacketSize, int interval) - { - LOG("AddEndpoint D:%02X A:%02X T:%02X P:%04X I:%02X\r\n",device,ep,attributes,maxPacketSize,interval); - Device* dev = &Devices[device-1]; - Endpoint* endpoint = AllocateEndpoint(device,ep,attributes,maxPacketSize); - if (!endpoint) - return ERR_ENDPOINT_NONE_LEFT; - dev->SetEndpointIndex(ep,endpoint - Endpoints); - endpoint->EndpointDescriptor.Control |= dev->Flags; // Map in slow speed - return 0; // TODO ed->bInterval - } - - int AddEndpoint(int device, EndpointDescriptor* ed) - { - return AddEndpoint(device,ed->bEndpointAddress,ed->bmAttributes,ed->wMaxPacketSize,ed->bInterval); - } - - // allocate a endpoint - Endpoint* AllocateEndpoint(int device, int endpointAddress, int type, int maxPacketSize) - { - for (int i = 0; i < MAX_ENDPOINTS_TOTAL; i++) - { - Endpoint* ep = &Endpoints[i]; - if (ep->CurrentState == 0) - { - //LOG("Allocated endpoint %d to %02X:%02X\r\n",i,device,endpointAddress); - ep->Flags = (endpointAddress & 0x80) | (type & 3); - ep->CurrentState = Endpoint::NotQueued; - ep->EndpointDescriptor.Control = (maxPacketSize << 16) | ((endpointAddress & 0x7F) << 7) | device; - return ep; - } - } - return 0; - } - - Endpoint* GetEndpoint(int device, int ep) - { - if (device == 0) - { - //printf("WARNING: USING DEVICE 0\n"); - return &EndpointZero; - } - if (device > MAX_DEVICES) - return 0; - int i = Devices[device-1].GetEndpointIndex(ep); - if (i == -1) - return 0; - return Endpoints + i; - } - - int Transfer(Endpoint* endpoint, int token, u8* data, int len, int state) - { - //Makoto edit comment out - // LOG("Transfer start\r\n"); - LOG("Transfer ep=%02X Token:%d Len:%d State:%d\r\n",endpoint->Address(),token,len,state); - - int toggle = 0; - if (endpoint->Address() == 0) - toggle = (token == TOKEN_SETUP) ? TD_TOGGLE_0 : TD_TOGGLE_1; - - if (token != TOKEN_SETUP) - token = (token == TOKEN_IN ? TD_IN : TD_OUT); - - HCTD* head = &endpoint->TDHead; - HCTD* tail = &_commonTail; - - head->Control = TD_ROUNDING | token | TD_DELAY_INT(0) | toggle | TD_CC; - head->CurrBufPtr = (u32)data; - head->BufEnd = (u32)(data + len - 1); - head->Next = (u32)tail; - - HCED* ed = &endpoint->EndpointDescriptor; - ed->HeadTd = (u32)head | (ed->HeadTd & 0x00000002); // carry toggle - ed->TailTd = (u32)tail; - - //HCTD* td = head; - //LOG("%04X TD %08X %08X %08X Next:%08X\r\n",CommunicationArea.FrameNumber,td->Control,td->CurrBufPtr,td->BufEnd,td->Next); - //LOG("%04X ED %08X %08X %08X\r\n",CommunicationArea.FrameNumber,ed->Control,ed->HeadTd,ed->TailTd); - - //LOG("Transfer run01\r\n"); - switch (endpoint->Flags & 3) - { - case ENDPOINT_CONTROL: - LOG("Transfer run ENDPOINT_CONTROL\r\n"); - LPC_USB->HcControlHeadED = endpoint->Enqueue(LPC_USB->HcControlHeadED); // May change state NotQueued->Idle - //LOG("Transfer run02\r\n"); - LOG("endpoint->CurrentState change %d->%d\r\n",endpoint->CurrentState,state); - endpoint->CurrentState = state; // Get in before an int - LPC_USB->HcCommandStatus = LPC_USB->HcCommandStatus | ControlListFilled; - LPC_USB->HcControl = LPC_USB->HcControl | ControlListEnable; - - break; - - case ENDPOINT_BULK: - LOG("Transfer run ENDPOINT_BULK\r\n"); - LPC_USB->HcBulkHeadED = endpoint->Enqueue(LPC_USB->HcBulkHeadED); - LOG("endpoint->CurrentState change %d->%d\r\n",endpoint->CurrentState,state); - endpoint->CurrentState = state; - LPC_USB->HcCommandStatus = LPC_USB->HcCommandStatus | BulkListFilled; - LPC_USB->HcControl = LPC_USB->HcControl | BulkListEnable; - break; - - case ENDPOINT_INTERRUPT: - LOG("Transfer run ENDPOINT_INTERRUPT\r\n"); - CommunicationArea.InterruptTable[0] = endpoint->Enqueue(CommunicationArea.InterruptTable[0]); - LOG("endpoint->CurrentState change %d->%d\r\n",endpoint->CurrentState,state); - endpoint->CurrentState = state; - LPC_USB->HcControl |= PeriodicListEnable; - break; - } - - LOG("Transfer end\r\n"); - return 0; - } - - // Remove an endpoint from an active queue - bool Remove(HCED* ed, volatile HCED** queue) - { - if (*queue == 0) - return false; - if (*queue == (volatile HCED*)ed) - { - *queue = (volatile HCED*)ed->Next; // At head of queue - return true; - } - - volatile HCED* head = *queue; - while (head) - { - if (head->Next == (u32)ed) - { - head->Next = ed->Next; - return true; - } - head = (volatile HCED*)head->Next; - } - return false; - } - - void Release(Endpoint* endpoint) - { - if (endpoint->CurrentState == Endpoint::NotQueued) - { - // Never event used it, nothing to do - } - else - { - HCED* ed = (HCED*)endpoint; - ed->Control |= 0x4000; // SKIP - switch (endpoint->Flags & 0x03) - { - case ENDPOINT_CONTROL: - Remove(ed,(volatile HCED**)&LPC_USB->HcControlHeadED); - break; - case ENDPOINT_BULK: - Remove(ed,(volatile HCED**)&LPC_USB->HcBulkHeadED); - break; - case ENDPOINT_INTERRUPT: - for (int i = 0; i < 32; i++) - Remove(ed,(volatile HCED**)&CommunicationArea.InterruptTable[i]); - break; - } - - u16 fn = CommunicationArea.FrameNumber; - while (fn == CommunicationArea.FrameNumber) - ; // Wait for next frame - - } - - // In theory, the endpoint is now dead. - // TODO: Will Callbacks ever be pending? BUGBUG - memset(endpoint,0,sizeof(Endpoint)); - } - - // Pop the last TD from the list - HCTD* Reverse(HCTD* current) - { - HCTD *result = NULL,*temp; - while (current) - { - temp = (HCTD*)current->Next; - current->Next = (u32)result; - result = current; - current = temp; - } - return result; - } - - // Called from interrupt... - // Control endpoints use a state machine to progress through the transfers - void ProcessDoneQueue(u32 tdList) - { - - LOG("<<ProcessDoneQueue enter>>\r\n"); - HCTD* list = Reverse((HCTD*)tdList); - while (list) - { - Endpoint* endpoint = (Endpoint*)(list-1); - list = (HCTD*)list->Next; - int ep = endpoint->Address(); - bool in = endpoint->Flags & 0x80; - int status = (endpoint->TDHead.Control >> 28) & 0xF; - - //LOG("ProcessDoneQueue %02X %08X\r\n",ep,endpoint->TDHead.Control); - - if (status != 0) - { - LOG("ProcessDoneQueue status %02X %d\r\n",ep,status); - LOG("CurrentState Change Idle endpoint(%d) currentState =%d\r\n",endpoint->Address(),endpoint->CurrentState); - endpoint->CurrentState = Endpoint::Idle; - - - } else { - switch (endpoint->CurrentState) - { - case Endpoint::SetupQueued: - if (endpoint->Length == 0) - { - LOG("ProcessDoneQueue endpoint->Length == 0 endpoint->CurrentState=%d\r\n",endpoint->CurrentState); - Transfer(endpoint,in ? TOKEN_OUT : TOKEN_IN,0,0,Endpoint::StatusQueued); // Skip Data Phase - } - else - { - LOG("ProcessDoneQueue endpoint->Length != 0 endpoint->CurrentState=%d\r\n",endpoint->CurrentState); - Transfer(endpoint,in ? TOKEN_IN : TOKEN_OUT,endpoint->Data,endpoint->Length, Endpoint::DataQueued); // Setup is done, now Data - } - break; - - case Endpoint::DataQueued: - if (endpoint->TDHead.CurrBufPtr) - endpoint->Length = endpoint->TDHead.CurrBufPtr - (u32)endpoint->Data; - - if (ep == 0) - { - LOG("ProcessDoneQueue ep == 0 endpoint->CurrentState=%d\r\n",endpoint->CurrentState); - Transfer(endpoint,in ? TOKEN_OUT : TOKEN_IN,0,0,Endpoint::StatusQueued); // Data is done, now Status, Control only - } - else - { - - LOG("ProcessDoneQueue ep != 0 endpoint->CurrentState=%d\r\n",endpoint->CurrentState); - endpoint->CurrentState = Endpoint::Idle; - LOG("CurrentState Change Idle endpoint(%d) currentState =%d\r\n",endpoint->Address(),endpoint->CurrentState); - } - break; - - case Endpoint::StatusQueued: // Transaction is done - LOG("CurrentState Change Idle endpoint(%d) currentState =%d\r\n",endpoint->Address(),endpoint->CurrentState); - endpoint->CurrentState = Endpoint::Idle; - break; - } - } - - // Complete, flag if we need a callback - if (endpoint->Callback && endpoint->CurrentState == Endpoint::Idle) - { - endpoint->CurrentState = Endpoint::CallbackPending; - _callbacksPending++; - } - } - LOG("<<ProcessDoneQueue out>>\r\n"); - } - - // Hack to reset devices that don't want to connect - int AddDevice(int hub, int port, bool isLowSpeed) - { - int device = AddDeviceCore(hub,port,isLowSpeed); - if (device < 0) - { - LOG("========RETRY ADD DEVICE========\r\n"); // This will go for ever.. TODO power cycle root? - Disconnect(hub,port); // Could not read descriptor at assigned address, reset this port and try again - ResetPort(hub,port); // Cheap bluetooth dongles often need this on a hotplug - return -1; - } - return device; - } - - int AddDeviceCore(int hub, int port, bool isLowSpeed) - { - int lowSpeed = isLowSpeed ? 0x2000 : 0; - DeviceDescriptor desc; - EndpointZero.EndpointDescriptor.Control = (8 << 16) | lowSpeed; // MaxPacketSize == 8 - int r = GetDescriptor(0,DESCRIPTOR_TYPE_DEVICE,0,(u8*)&desc,8); - if (r < 0) - { - LOG("FAILED TO LOAD DESCRIPTOR FOR DEVICE 0\r\n"); - return r; - } - - EndpointZero.EndpointDescriptor.Control = (desc.bMaxPacketSize << 16) | lowSpeed; // Actual MaxPacketSize - r = GetDescriptor(0,DESCRIPTOR_TYPE_DEVICE,0,(u8*)&desc,sizeof(desc)); - if (r < 0) - return r; - - LOG("\nClass %02X found %04X:%04X\r\n",desc.bDeviceClass,desc.idVendor,desc.idProduct); - - // Now assign the device an address, move off EndpointZero - int device = 0; - for (int i = 0; i < MAX_DEVICES; i++) - { - if (Devices[i].Port == 0) - { - device = i+1; - break; - } - } - if (!device) - return ERR_DEVICE_NONE_LEFT; - - r = SetAddress(0,device); - if (r) - return r; - DelayMS(2); - - // Now at a nonzero address, create control endpoint - Device* dev = &Devices[device-1]; - dev->Init(&desc,hub,port,device,lowSpeed); - AddEndpoint(device,0,ENDPOINT_CONTROL,desc.bMaxPacketSize,0); - _connectPending = 0; - - // Verify this all works - r = GetDescriptor(device,DESCRIPTOR_TYPE_DEVICE,0,(u8*)&desc,sizeof(desc)); - if (r < 0) - return r; - - // Set to interface 0 by default - // Calls LoadDevice if interface is found - r = SetConfigurationAndInterface(device,1,0,&desc); - - if (desc.bDeviceClass == CLASS_HUB) - InitHub(device); // Handle hubs in this code - - return device; - } - - // Walk descriptors and create endpoints for a given device - // TODO configuration !=1, alternate settings etc. - int SetConfigurationAndInterface(int device, int configuration, int interfaceNumber, DeviceDescriptor* desc) - { - u8 buffer[255]; - int err = GetDescriptor(device,DESCRIPTOR_TYPE_CONFIGURATION,0,buffer,sizeof(buffer)); - if (err < 0) - return err; - - err = SetConfiguration(device,configuration); - if (err < 0) - return err; - - // Add the endpoints for this interface - int len = buffer[2] | (buffer[3] << 8); - u8* d = buffer; - u8* end = d + len; - InterfaceDescriptor* found = 0; - while (d < end) - { - if (d[1] == DESCRIPTOR_TYPE_INTERFACE) - { - InterfaceDescriptor* id = (InterfaceDescriptor*)d; - if (id->bInterfaceNumber == interfaceNumber) - { - found = id; - d += d[0]; - while (d < end && d[1] != DESCRIPTOR_TYPE_INTERFACE) - { - switch (d[1]) - { - case DESCRIPTOR_TYPE_ENDPOINT: - AddEndpoint(device,(EndpointDescriptor*)d); - break; - default: - LOG("Skipping descriptor %02X (%d bytes)\r\n",d[1],d[0]); - } - d += d[0]; - } - } - } - d += d[0]; - } - - if (!found) - return ERR_INTERFACE_NOT_FOUND; - OnLoadDevice(device,desc,found); - return 0; - } - - void Init() - { - LOG("USB INIT (Controller is %d bytes)\r\n",sizeof(*this)); - memset(this,0,sizeof(HostController)); - EndpointZero.CurrentState = Endpoint::NotQueued; - HWInit(&CommunicationArea); - DelayMS(10); - } - - void ResetPort(int hub, int port) - { - LOG("ResetPort Hub:%d Port:%d\r\n",hub,port); - _connectPending++; // Only reset/add 1 device at a time - if (hub == 0) - LPC_USB->HcRhPortStatus1 = PortResetStatus; // Reset Root Hub, port 1 - else - SetPortReset(hub,port); // or reset other hub - } - - void Disconnect(int hub, int port) - { - LOG("Disconnect Hub:%d Port:%d\r\n",hub,port); // Mark a device for destruction - for (int i = 0; i < MAX_DEVICES; i++) - { - Device* dev = Devices + i; - if (dev->Port == port && dev->Hub == hub) - { - // Disconnect everything that is attached to this device if it is a hub - for (int p = 0; p < dev->HubPortCount; p++) - Disconnect(i+1,p+1); - - // Now release endpoints - for (int j = 1; j < MAX_ENDPOINTS_PER_DEVICE*2; j += 2) - { - u8 endpointIndex = dev->_endpointMap[j]; - if (endpointIndex != 0xFF) - Release(Endpoints + endpointIndex); - } - dev->Port = 0; // Device is now free - dev->Flags = 0; - return; - } - } - } - - // called after reset - void Connect(int hub, int port, bool lowspeed) - { - LOG("Connect Hub:%d Port:%d %s\r\n",hub,port,lowspeed ? "slow" : "full"); - AddDevice(hub,port,lowspeed); - } - - // Called from interrupt - void HubStatusChange(int hub, int port, u32 status) - { - LOG("HubStatusChange Hub:%d Port:%d %08X\r\n",hub,port,status); - if (status & ConnectStatusChange) - { - if (status & CurrentConnectStatus) // Connecting - ResetPort(hub,port); // Reset to initiate connect (state machine?) - else - Disconnect(hub,port); - } - - if (status & PortResetStatusChange) - { - if (!(status & PortResetStatus)) - { - _connectCountdown = 200; // Schedule a connection in 200ms - if (status & LowspeedDevice) - port |= 0x80; - _connectHub = hub; - _connectPort = port; - } - } - } - - #define HOST_CLK_EN (1<<0) - #define PORTSEL_CLK_EN (1<<3) - #define AHB_CLK_EN (1<<4) - #define CLOCK_MASK (HOST_CLK_EN | PORTSEL_CLK_EN | AHB_CLK_EN) - - #define FRAMEINTERVAL (12000-1) // 1ms - #define DEFAULT_FMINTERVAL ((((6 * (FRAMEINTERVAL - 210)) / 7) << 16) | FRAMEINTERVAL) - - void DelayMS(int ms) - { - u16 f = ms + CommunicationArea.FrameNumber; - while (f != CommunicationArea.FrameNumber) - ; - } - - static void HWInit(HCCA* cca) - { - NVIC_DisableIRQ(USB_IRQn); - - // turn on power for USB - LPC_SC->PCONP |= (1UL<<31); - // Enable USB host clock, port selection and AHB clock - LPC_USB->USBClkCtrl |= CLOCK_MASK; - // Wait for clocks to become available - while ((LPC_USB->USBClkSt & CLOCK_MASK) != CLOCK_MASK) - ; - - // We are a Host - LPC_USB->OTGStCtrl |= 1; - LPC_USB->USBClkCtrl &= ~PORTSEL_CLK_EN; // we don't need port selection clock until we do OTG - - // configure USB pins - LPC_PINCON->PINSEL1 &= ~((3<<26)|(3<<28)); - LPC_PINCON->PINSEL1 |= ((1<<26)|(1<<28)); // USB D+/D- - - LPC_PINCON->PINSEL3 &= ~((3 << 6) | (3 << 22)); // USB_PPWR, USB_OVRCR - LPC_PINCON->PINSEL3 |= ((2 << 6) | (2 << 22)); - - LPC_PINCON->PINSEL4 &= ~(3 << 18); // USB_CONNECT - LPC_PINCON->PINSEL4 |= (1 << 18); - - // Reset OHCI block - LPC_USB->HcControl = 0; - LPC_USB->HcControlHeadED = 0; - LPC_USB->HcBulkHeadED = 0; - - LPC_USB->HcCommandStatus = HostControllerReset; - LPC_USB->HcFmInterval = DEFAULT_FMINTERVAL; - LPC_USB->HcPeriodicStart = FRAMEINTERVAL*90/100; - - LPC_USB->HcControl = (LPC_USB->HcControl & (~HostControllerFunctionalState)) | OperationalMask; - LPC_USB->HcRhStatus = SetGlobalPower; - - LPC_USB->HcHCCA = (u32)cca; - LPC_USB->HcInterruptStatus |= LPC_USB->HcInterruptStatus; - LPC_USB->HcInterruptEnable = MasterInterruptEnable | WritebackDoneHead | RootHubStatusChange | FrameNumberOverflow; - - NVIC_SetPriority(USB_IRQn, 0); - NVIC_EnableIRQ(USB_IRQn); - while (cca->FrameNumber < 10) - ; // 10ms delay before diving in - } -}; - -//==================================================================================== -//==================================================================================== -// Host controller instance and Interrupt handler - -static HostController _controller __attribute__((at(USB_RAM_BASE))); - -extern "C" void USB_IRQHandler(void) __irq; -void USB_IRQHandler (void) __irq -{ -LOG("USB_IRQHandler start \r\n"); - u32 int_status = LPC_USB->HcInterruptStatus; - LOG("USB_IRQHandler int_status=%0x\r\n",int_status); - - if(int_status & UnrecoverableError) //Error - { - LOG("USB_IRQHandler UnrecoverableError Please reset\r\n"); - } - - if(int_status & MasterInterruptEnable) - { - LOG("USB_IRQHandler MasterInterruptEnable\r\n"); - } - - if(int_status &OwnershipChange) - { - LOG("USB_IRQHandler OwnershipChange\r\n"); - } - - if(int_status &FrameNumberOverflow) - { - LOG("USB_IRQHandler FrameNumberOverflow\r\n"); - } - if(int_status&ResumeDetected) - { - LOG("USB_IRQHandler ResumeDetected\r\n"); - } - if(int_status&StartofFrame) - { - LOG("USB_IRQHandler StartofFrame\r\n"); - } - - - if (int_status & RootHubStatusChange) // Root hub status change - { - LOG("USB_IRQHandler _rootHubStatusChange %0x->%0x \r\n", _controller._rootHubStatusChange, _controller._rootHubStatusChange+1); - _controller._rootHubStatusChange++; // Just flag the controller, will be processed in USBLoop - } - - u32 head = 0; - if (int_status & WritebackDoneHead) - { - head = _controller.CommunicationArea.DoneHead; // Writeback Done - _controller.CommunicationArea.DoneHead = 0; - LOG("USB_IRQHandler head=%0x\r\n",head); - } - LPC_USB->HcInterruptStatus = int_status; - - if (head) - _controller.ProcessDoneQueue(head); // TODO - low bit can be set BUGBUG -LOG("USB_IRQHandler end \r\n"); -} - -//==================================================================================== -//==================================================================================== -// API Methods - -void USBInit() -{ - return _controller.Init(); -} - -void USBLoop() -{ - return _controller.Loop(); -} - -u8* USBGetBuffer(u32* len) -{ - *len = USB_RAM_SIZE - sizeof(HostController); - return _controller.SRAM; -} - -static Setup* GetSetup(int device) -{ - if (device == 0) - return &_controller._setupZero; - - if (device < 1 || device > MAX_DEVICES) - return 0; - return &_controller.Devices[device-1].SetupBuffer; -} - -// Loop until IO on endpoint is complete -static int WaitIODone(Endpoint* endpoint) -{ - LOG("WaitIODone start\r\n"); - if (endpoint->CurrentState == Endpoint::NotQueued) - return 0; - while (endpoint->CurrentState != Endpoint::Idle) - { - LOG("WaitIODone goto USBloop endpoint->CurrentState =%d \r\n",endpoint->CurrentState); - USBLoop(); // May generate callbacks, mount or unmount devices etc - } - int status = endpoint->Status(); - - LOG("WaitIODone end\r\n"); - if (status == 0) - return endpoint->Length; - return -status; -} - -int USBTransfer(int device, int ep, u8 flags, u8* data, int length, USBCallback callback, void* userData) -{ - //LOG("USBTRANSFER start\r\n"); - Endpoint* endpoint = _controller.GetEndpoint(device,ep); - if (!endpoint) - { - LOG("USBTRANSFER error Endpoint not found!!\r\n"); - return ERR_ENDPOINT_NOT_FOUND; - } - - //LOG("USBTRANSFER run01\r\n"); - WaitIODone(endpoint); - endpoint->Flags = flags; - endpoint->Data = data; - endpoint->Length = length; - endpoint->Callback = callback; - endpoint->UserData = userData; - //LOG("USBTRANSFER run02\r\n"); - if (ep == 0) - { - LOG("USBTRANSFER ep == 0(Setup) ------------------------------\r\n"); - _controller.Transfer(endpoint,TOKEN_SETUP,(u8*)GetSetup(device),8,Endpoint::SetupQueued); - } - else - { - LOG("USBTRANSFER ep != 0\r\n"); - _controller.Transfer(endpoint,flags & 0x80 ? TOKEN_IN : TOKEN_OUT,data,length,Endpoint::DataQueued); - } - if (callback) - { - LOG("USBTRANSFER end io_pending\r\n"); - return IO_PENDING; - } - LOG("USBTRANSFER end wait io done \r\n"); - return WaitIODone(endpoint); -} - -int USBControlTransfer(int device, int request_type, int request, int value, int index, u8* data, int length, USBCallback callback, void * userData) -{ - Setup* setup = GetSetup(device); - if (!setup) - return ERR_DEVICE_NOT_FOUND; - - // Async control calls may overwrite setup buffer of previous call, so we need to wait before setting up next call - WaitIODone(_controller.GetEndpoint(device,0)); - - setup->bm_request_type = request_type; - setup->b_request = request; - setup->w_value = value; - setup->w_index = index; - setup->w_length = length; - LOG("USBControlTransfer(device=%d,reqtype=%d,req=%d,value=%d,index=%d,data=%p,len=%d,callbak=%p,udata=%p)\r\n", - device,request_type,request,value,index,data,length,callback,userData); - return USBTransfer(device,0,request_type & DEVICE_TO_HOST,data,length,callback,userData); -} - -int USBInterruptTransfer(int device, int ep, u8* data, int length, USBCallback callback, void* userData) -{ - return USBTransfer(device,ep,(ep & 0x80) | ENDPOINT_INTERRUPT,data,length,callback,userData); -} - -int USBBulkTransfer(int device, int ep, u8* data, int length, USBCallback callback, void* userData) -{ - return USBTransfer(device,ep,(ep & 0x80) | ENDPOINT_BULK,data,length,callback,userData); -} - -int GetDescriptor(int device, int descType,int descIndex, u8* data, int length) -{ - return USBControlTransfer(device,DEVICE_TO_HOST | RECIPIENT_DEVICE, GET_DESCRIPTOR,(descType << 8)|(descIndex), 0, data, length, 0); -} - -int GetString(int device, int index, char* dst, int length) -{ - u8 buffer[255]; - int le = GetDescriptor(device,DESCRIPTOR_TYPE_STRING,index,buffer,sizeof(buffer)); - if (le < 0) - return le; - if (length < 1) - return -1; - length <<= 1; - if (le > length) - le = length; - for (int j = 2; j < le; j += 2) - *dst++ = buffer[j]; - *dst = 0; - return (le>>1)-1; -} - -int SetAddress(int device, int new_addr) -{ - return USBControlTransfer(device,HOST_TO_DEVICE | RECIPIENT_DEVICE, SET_ADDRESS, new_addr, 0, 0, 0, 0); -} - -int SetConfiguration(int device, int configNum) -{ - return USBControlTransfer(device,HOST_TO_DEVICE | RECIPIENT_DEVICE, SET_CONFIGURATION, configNum, 0, 0, 0, 0); -} - -int SetInterface(int device, int ifNum, int altNum) -{ - return USBControlTransfer(device,HOST_TO_DEVICE | RECIPIENT_INTERFACE, SET_INTERFACE, altNum, ifNum, 0, 0, 0); -} - -// HUB stuff -int SetPortFeature(int device, int feature, int index) -{ - return USBControlTransfer(device,HOST_TO_DEVICE | REQUEST_TYPE_CLASS | RECIPIENT_OTHER,SET_FEATURE,feature,index,0,0); -} - -int ClearPortFeature(int device, int feature, int index) -{ - return USBControlTransfer(device,HOST_TO_DEVICE | REQUEST_TYPE_CLASS | RECIPIENT_OTHER,CLEAR_FEATURE,feature,index,0,0); -} - -int SetPortPower(int device, int port) -{ - int r = SetPortFeature(device,PORT_POWER,port); - _controller.DelayMS(20); // 80ms to turn on a hubs power... DESCRIPTOR? todo - return r; -} - -int SetPortReset(int device, int port) -{ - return SetPortFeature(device,PORT_RESET,port); -} - -int GetPortStatus(int device, int port, u32* status) -{ - return USBControlTransfer(device,DEVICE_TO_HOST | REQUEST_TYPE_CLASS | RECIPIENT_OTHER,GET_STATUS,0,port,(u8*)status,4); -} - -#endif \ No newline at end of file
--- a/Android/android.cpp Fri Apr 05 10:58:42 2013 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,190 +0,0 @@ -#include "android.h" - -using namespace std; - -AdkTerm::AdkTerm() : AndroidAccessory(INBL,OUTL, "ARM", "mbed", "mbed Terminal", "0.1", "http://www.mbed.org", "0000000012345678") -{ - -}; - - -void AdkTerm::setupDevice() -{ - settick = false; - for (int i = 0; i<OUTL; i++) { - buffer[i] = 0; - } - bcount = 0; - //n.attach(this,&AdkTerm::AttachTick,5); - //tick.attach(this,&AdkTerm::onTick,0.1); -} - -float AdkTerm::getx() -{ - return x/1000; -} - -float AdkTerm::gety() -{ - return y/1000; -} - -float AdkTerm::gett() -{ - return t * PI / 180; -} - -/*void AdkTerm::AttachTick() -{ - if(!settick)tick.attach(this,&AdkTerm::onTick,0.04); - settick = true; -} - -void AdkTerm::onTick() -{ - right = 1-Right; - left = 1-Left; - bool update = false; - int templ, tempr; - - - - templ = int(left * 10000); - tempr = int(right * 10000); - - - - if (abs(templ-tl)>170) { - update = true; - } - if (abs(tempr-tr)>170) { - update = true; - } - if (update) { - u8* wbuf = _writebuff; - - wbuf[0] = 'P'; - wbuf[1] = templ&0xFF; - wbuf[2] = (templ>>8) & 0xFF; - wbuf[3] = tempr&0xFF; - wbuf[4] = (tempr>>8) & 0xFF; - wbuf[5] = 0; - - this->write(wbuf,5); - } -}*/ - -void AdkTerm::resetDevice() -{ - for (int i = 0; i<OUTL; i++) { - buffer[i] = 0; - } - bcount = 0; -} - -void AdkTerm::Tokenize(const string& str, - vector<string>& tokens, - const string& delimiters /*= " "*/) -{ - // Skip delimiters at beginning. - string::size_type lastPos = str.find_first_not_of(delimiters, 0); - // Find first "non-delimiter". - string::size_type pos = str.find_first_of(delimiters, lastPos); - - while (string::npos != pos || string::npos != lastPos) { - // Found a token, add it to the vector. - tokens.push_back(str.substr(lastPos, pos - lastPos)); - // Skip delimiters. Note the "not_of" - lastPos = str.find_first_not_of(delimiters, pos); - // Find next "non-delimiter" - pos = str.find_first_of(delimiters, lastPos); - } -} - -int AdkTerm::callbackRead(u8 *buf, int len) -{ - // convert buffer (unsigned char) to string - std::string str(reinterpret_cast<char*>(buf), len); - - // new vector of strings - vector<string> tokens; - - // tokenize the string with the semicolon separator - Tokenize(str, tokens, ";"); - copy(tokens.begin(), tokens.end(), ostream_iterator<string>(cout, ", ")); - - - if(tokens.size() > 2) { - - //string to float - x = ::atof(tokens.at(0).c_str()); - y = ::atof(tokens.at(1).c_str()); - t = ::atof(tokens.at(2).c_str()); - - //pc.printf("Android x(%d): %f\n\r\n",len,x); - // pc.printf("Android y(%d): %f\n\r\n",len,y); - //pc.printf("Android t(%d): %f\n\r\n",len,t); - } else { - //pc.printf("Android sayys(%d): %s\n\r\n",len,str); - } - -// switch ( buf[0] ) { -// case 'x': -// if (buf[1] == ":"){pc.printf("X-Coordinate(%d): %s\n\r\n",len,buf)}; -// break; -// case 'y': -// if (buf[1] == ":"){pc.printf("Y-Coordinate(%d): %s\n\r\n",len,buf)}; -// break; -// case 't': -// if (buf[1] == ":"){pc.printf("Z-Coordinate(%d): %s\n\r\n",len,buf)}; -// break; -// default: -// pc.printf("Command not recognized (%d): %s\n\r\n\n\n",len,buf); -// } - - - //AttachTick(); - - return 0; -} - -// split: receives a char delimiter; returns a vector of strings -// By default ignores repeated delimiters, unless argument rep == 1. - -int AdkTerm::callbackWrite() -{ - ind = false; - return 0; -} - -void AdkTerm::serialIRQ() -{ - //buffer[bcount] = pc.getc(); - - - if (buffer[bcount] == '\n' || buffer[bcount] == '\r') { - u8* wbuf = _writebuff; - for (int i = 0; i<OUTL; i++) { - wbuf[i] = buffer[i]; - buffer[i] = 0; - } - //pc.printf("Sending: %s\n\r",wbuf); - ind = true; - this->write(wbuf,bcount); - bcount = 0; - } else { - if (buffer[bcount] != 0x08 && buffer[bcount] != 0x7F ) { - bcount++; - if (bcount == OUTL) { - bcount = 0; - } - } else { - bcount--; - } - } - -} - - -// gehört nicht mehr zur klasse für zum testen.......!!!!!!!! -
--- a/Android/android.h Fri Apr 05 10:58:42 2013 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,154 +0,0 @@ -#ifndef _ANDROID_H_ -#define _ANDROID_H_ - -#include "mbed.h" -#include "AndroidAccessory.h" -#include "defines.h" -#include <string> -#include <sstream> -#include <vector> -#include <iostream> -#include <stdlib.h> - -/** - * @author Arno Galliker - * - * @subsection LICENSE - * - * Copyright © 2013 HSLU Pren Team #1 Cruising Crêpe - * All rights reserved. - * - * @subsection DESCRIPTION - * - * This class implements communication with the android smartphone - * For more information see the Android ADK Cookbook: - * <a href="http://mbed.org/cookbook/mbed-with-Android-ADK">http://mbed.org/cookbook/mbed-with-Android-ADK</a> - * - * Originally created by p07gbar from work by Makoto Abe - */ - -#define OUTL 100 -#define INBL 100 - -class AdkTerm : public AndroidAccessory -{ - -private: - - /** Attaches a tick to send messages over the USB buffer in a certain interval */ - void AttachTick(); - - /** Method to call when a tick period is over */ - void onTick(); - - /** Char buffer with a size of OUTL */ - char buffer[OUTL]; - - /** Buffer count ??? */ - int bcount; - - /** Instance of ticker */ - Ticker tick; - - //float right,left,rl,ll; - //int tl,tr; - - //Timeout n; - - /** States if tick is on */ - bool settick; - - /** States if something is written to the buffer?? */ - bool ind; - - /** Desired position in meters for x-coordinate, given by android */ - float x; - - /** Desired position in meters for y-coordinate, given by android */ - float y; - - /** Desired position in degrees for theta, given by android */ - float t; - -public: - - /** - * Creates a <code>AdkTerm</code> object and initializes all private - * state variables. Constructor - */ - AdkTerm(); - - /** - * Sets initial configurations and clears the buffer - * - */ - void setupDevice(); - - /** - * Returns the desired position in meters for x-coordinate, given by android - * @return x-coordinate,given in [m] - */ - float getx(); - - /** - * Returns the desired position in meters for y-coordinate, given by android - * @return y-coordinate,given in [m] - */ - float gety(); - - /** - * Returns the esired position in degrees for theta, given by android - * @return y-coordinate,given in degrees [°] - */ - float gett(); - -private: - - /** - * - * - */ - //void AttachTick(); - - /** - * - * - */ - // void onTick(); - - /** - * Clears the buffer and bcount - * - */ - void resetDevice(); - - /** - * Takes an string, a vector of strings for the delimited tokens, and a with the - * @param str - * @param tokens - * @param delimiters - */ - void Tokenize(const string& str, vector<string>& tokens, const string& delimiters = " "); - - /** - * - * @param buf - * @param len - */ - int callbackRead(u8 *buf, int len); - - /** - * - * - */ - int callbackWrite(); - - /** - * - * - */ - void serialIRQ(); - -}; - -#endif
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/AndroidADKTerm/AndroidAccessory/AndroidAccessory.cpp Sun Apr 07 08:31:51 2013 +0000 @@ -0,0 +1,323 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#include "USBHost.h" +#include "AndroidAccessory.h" +#include "mbed.h" + +AndroidAccessory* _adk; + +void AdkreadCallback(int device, int endpoint, int status, u8* buf, int len, void* userData); +void AdkwriteCallback(int device, int endpoint, int status, u8* buf, int len, void* userData); + + + +AndroidAccessory::AndroidAccessory(int rbuffsize,int wbuffsize, + const char* manufacturer, + const char *model, + const char *description, + const char *version, + const char *uri, + const char *serial + ) { + + _adk=this; + + this->manufacturer=manufacturer; + this->model=model; + this->description=description; + this->version=version; + this->uri=uri; + this->serial=serial; + + u32 len; + u8* p=USBGetBuffer(&len); + if (len<(rbuffsize+wbuffsize+255)) { + error("buff size too big.please resize max=%d. currentSize=%d\r\n",len,(rbuffsize+wbuffsize+255)); + } + + _readbuff=p; + _readbuffsize=rbuffsize; + p+=rbuffsize; + _writebuff=p; + _writebuffsize=wbuffsize; + p+=wbuffsize; + _strbuff=p; + p+=255; + +} + + + +int AndroidAccessory::write(u8 *buff, int len) { + log("AndroidAccessory::write "); + // __disable_irq(); + int ret=USBBulkTransfer(_device,output_ep,buff,len,AdkwriteCallback,this); + // __enable_irq(); + log("--ret=%d \r\n",ret); + return ret; +} +int AndroidAccessory::writeNC(u8 *buff, int len) { + log("AndroidAccessory::write "); + // __disable_irq(); + int ret=USBBulkTransfer(_device,output_ep,buff,len); + // __enable_irq(); + log("--ret=%d \r\n",ret); + return ret; +} + + + +int AndroidAccessory::read(u8 *buff, int len) { + // if(_initok==false)return 0; + + log("AndroidAccessory::read "); + // __disable_irq(); + int ret=USBBulkTransfer(_device,input_ep|0x80,buff,len); + // __enable_irq(); + log("--ret=%d \r\n",ret); + return ret; +} + + +void AndroidAccessory::init(int device, int configuration, int interfaceNumber) { + + log("AndroidAccessory::init \r\n"); + +// _initok=false; + _device = device; + _configuration = configuration; + _interfaceNumber = interfaceNumber; + printf("device = %d configuration = %d interfaceNumber = %d\r\n", device, configuration, interfaceNumber); + int err; + + u8* buffer=_strbuff; + err = GetDescriptor(_device,DESCRIPTOR_TYPE_CONFIGURATION,0,buffer,4); + + if (err < 0) { + log("Failed to get descriptor\r\n"); + return; + } + + + int len = buffer[2] | (buffer[3] << 8); + if (len > 255) { + log("config descriptor too large\n"); + /* might want to truncate here */ + return; + } + err = GetDescriptor(_device,DESCRIPTOR_TYPE_CONFIGURATION,0,buffer,len); + u8* p = buffer; + input_ep=0; + output_ep=0; + EndpointDescriptor *epDesc; + while (p<(buffer+len)) { + u8 descLen = p[0]; + u8 descType = p[1]; + log("descLen=%d,descType=%d\r\n",descLen,descType); + switch (descType) { + case DESCRIPTOR_TYPE_CONFIGURATION: + log("config desc\r\n"); + break; + case DESCRIPTOR_TYPE_INTERFACE: + log("interface desc\r\n"); + break; + case DESCRIPTOR_TYPE_ENDPOINT: + epDesc=(EndpointDescriptor*)p; + if (!input_ep && (epDesc->bEndpointAddress& 0x80)) { + input_ep=epDesc->bEndpointAddress& 0x7f; + //PacketSize drop + log("input Endpoint address=%d,wMaxPacketSize=%d,bmAttributes=%d\r\n",input_ep,epDesc->wMaxPacketSize,epDesc->bmAttributes); + + } else if (!output_ep) { + output_ep=epDesc->bEndpointAddress& 0x7f; + //PacketSize drop + log("output Endpoint address=%d,wMaxPacketSize=%d,bmAttributes=%d\r\n",input_ep,epDesc->wMaxPacketSize,epDesc->bmAttributes); + } else { + //other + log("non input,output Endpoint address=%d,wMaxPacketSize=%d,bmAttributes=%d\r\n",input_ep,epDesc->wMaxPacketSize,epDesc->bmAttributes); + } + break; + default: + log("unkown desc type(%d) \r\n",descType); + } + p+=descLen; + } + + if (!(input_ep && output_ep)) { + log("can't find accessory endpoints\r\n"); + return; + } + + log("SetConfiguration\r\n"); + err = SetConfiguration(device,configuration); + if (err < 0) { + log("SetConfiguration error\r\n"); + return; + } + + + log("interrupt setup\r\n"); + //interrupt setup + if (_readbuff==NULL || _readbuffsize<=0) { + error("_readbuffer error please setup buffer call setReadBuffer function\r\n"); + } + + if (IO_PENDING!=USBBulkTransfer(_device,input_ep|0x80,_readbuff,_readbuffsize,AdkreadCallback,this)) + return; + + + log("setupDevice\r\n"); + this->setupDevice(); +// _initok=true; +} + + + +bool AndroidAccessory::switchDevice(int device) { + + if (1==getProtocol(device)) { + log("device supports protocol 1\r\n"); + + } else { + log("could not read device protocol version\r\n"); + return false; + } + + + sendString(device,ACCESSORY_STRING_MANUFACTURER,manufacturer); + sendString(device,ACCESSORY_STRING_MODEL,model); + sendString(device,ACCESSORY_STRING_DESCRIPTION,description); + sendString(device,ACCESSORY_STRING_VERSION,version); + sendString(device,ACCESSORY_STRING_URI,uri); + sendString(device,ACCESSORY_STRING_SERIAL,serial); + USBControlTransfer(device, + HOST_TO_DEVICE |REQUEST_TYPE_VENDOR|RECIPIENT_DEVICE, + ACCESSORY_START, + 0,//value + 0, //index + 0, + 0, + 0, + 0 ); + + wait_ms(4); + //reset usb host + USBInit(); + + return true; + +} + + +int AndroidAccessory::getProtocol(int device) { + s16 data=-1; + USBControlTransfer(device, + DEVICE_TO_HOST|REQUEST_TYPE_VENDOR|RECIPIENT_DEVICE, + ACCESSORY_GET_PROTOCOL, + 0,//value + 0, //index + (u8*)&data, + 2, + 0, + 0 ); + return data; + +} + +void AndroidAccessory::sendString(const char *str) { + sendString(_device,1,str); + +} + +void AndroidAccessory::sendString(int device, int index, const char *str) { + + LOG("send_string start(%d,%d,%s) %d \r\n",device,index,str,strlen(str)+1); + strcpy((char*)_strbuff,str); + //thankyou curryman san + USBControlTransfer(device, + HOST_TO_DEVICE|REQUEST_TYPE_VENDOR|RECIPIENT_DEVICE, + ACCESSORY_SEND_STRING, + 0,//value + index, + _strbuff, + strlen(str)+1 + ); + + LOG("send_string end(%d,%d,%s)\r\n",device,index,str); + +} + + +/** from USBHost load function. initialize Android device**/ +void OnLoadDevice(int device, DeviceDescriptor* deviceDesc, InterfaceDescriptor* interfaceDesc) { + printf("LoadDevice %d %02X:%02X:%02X\r\n",device,interfaceDesc->bInterfaceClass,interfaceDesc->bInterfaceSubClass,interfaceDesc->bInterfaceProtocol); + char s[128]; + + for (int i = 1; i < 3; i++) { + if (GetString(device,i,s,sizeof(s)) < 0) + break; + printf("%d: %s\r\n",i,s); + } + + //for android ADK + if ( ( deviceDesc->idVendor != 0x18D1 || + ( deviceDesc->idProduct != 0x2D00 && deviceDesc->idProduct != 0x2D01)) + &&_adk->switchDevice(device)) { + + printf(" try to change accmode.interfaceDesc->bInterfaceClass=%d\r\n",interfaceDesc->bInterfaceClass); + //1th root + //accmode_support=true; + printf("accessory mode ok.\r\n"); + return; + } + + if (deviceDesc->idVendor == 0x18D1 && + (deviceDesc->idProduct == 0x2D00 || deviceDesc->idProduct == 0x2D01)) { + //2th root + printf("connecting Android.\r\n"); + printf("idVender=%x idProduct=%x interfaceDesc->bInterfaceClass=%d\r\n",deviceDesc->idVendor,deviceDesc->idProduct,interfaceDesc->bInterfaceClass); + _adk->init(device,1,0); + //_AdkUSB.loop(); + return; + } +} + +void AdkreadCallback(int device, int endpoint, int status, u8* buf, int len, void* userData) { + log("AdkreadCallback(int device=%d, int endpoint=%x, int status=%d, u8* buf=%p, int len=%d, void* userData=%p)\r\n", + device,endpoint,status,buf,len,userData); +// __disable_irq(); + AndroidAccessory* t = (AndroidAccessory*)userData; + if (status!=0 && status!=8) { + log("adk end.\r\n"); + t->adkEnd(); +// __enable_irq(); + USBInit(); + return; + } + + + //virtual method run + t->callbackRead(buf,len); + + USBBulkTransfer(device, endpoint , buf, len, AdkreadCallback, userData); + +// wait_ms(4); +// __enable_irq(); +} + + + + +void AdkwriteCallback(int device, int endpoint, int status, u8* buf, int len, void* userData) { + + log("AdkwriteCallback(int device=%d, int endpoint=%x, int status=%d, u8* buf=%p, int len=%d, void* userData=%p)\r\n", + device,endpoint,status,buf,len,userData); + + AndroidAccessory* t = (AndroidAccessory*)userData; + t->callbackWrite(); + //wait_ms(4); + //USBBulkTransfer(device, endpoint , buf, len, AdkwriteCallback, userData); +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/AndroidADKTerm/AndroidAccessory/AndroidAccessory.h Sun Apr 07 08:31:51 2013 +0000 @@ -0,0 +1,195 @@ +/* mbed AndroidAccessory Library + * Created by p07gbar from work by Makoto Abe + * + */ +#ifndef ADK_H_INCLUDED +#define ADK_H_INCLUDED + +#include "mbed.h" +#include "USBHost.h" + + +//#define ADKLOG 1 +#if ADKLOG +#define LOG(...) printf(__VA_ARGS__) +#define Log(...) printf(__VA_ARGS__) +#define log(...) printf(__VA_ARGS__) + +#else +#define LOG(...) do {} while(0) +#define Log(...) do {} while(0) +#define log(...) do {} while(0) + +#endif + +#define ACCESSORY_STRING_MANUFACTURER 0 +#define ACCESSORY_STRING_MODEL 1 +#define ACCESSORY_STRING_DESCRIPTION 2 +#define ACCESSORY_STRING_VERSION 3 +#define ACCESSORY_STRING_URI 4 +#define ACCESSORY_STRING_SERIAL 5 + +#define ACCESSORY_GET_PROTOCOL 51 +#define ACCESSORY_SEND_STRING 52 +#define ACCESSORY_START 53 + + + +/** An AndroidAccessory control class + * + * It allows easy creation of a mbed android ADK accessory, with minimal low level fussing. + * Base code should have methods resetDevice(), setupDevice(), callbackRead(u8 *buff, int len) and callBackWrite() functions + * + */ + +class AndroidAccessory { +public: + + + +/** Create a AndroidAccessory object + * + * Create a AndroidAccessoryobject with specified buffer sizes and infomation + * + * @param rbuffsize The size of the read buffer + * @param wbuffsize The size of the write buffer + * @param manufacturer The manufacturer of the accessory + * @param model The model of the accessory + * @param description A short description of the accessory + * @param version The current version of the accessory + * @param uri Some data to go with the accessory (URL or more description) + * @param serial The serial number of the accessory + */ + AndroidAccessory(int rbuffsize,int wbuffsize, + const char* manufacturer, + const char *model, + const char *description, + const char *version, + const char *uri, + const char *serial + ); + + /** Init the device + * This is meant to be implimented by the user of the class + * + * @param device Device number + * @param configuration Configuration + * @param interfaceNumber Inteface number + */ + virtual void init(int device, int configuration, int interfaceNumber); + +/** Reset the device + * This is meant to be implimented by the user of the class + * + */ + virtual void resetDevice()=0; + +/** Setup the device + * This is meant to be implimented by the user of the class. Called when the device is first intialised + * + */ + virtual void setupDevice()=0; + + /** Callback on Read + * This is meant to be implimented by the user of the class. Called when some data has been read in. + * + * @param buff The buffered read in data + * @param len The length of the packet recived + * + */ + virtual int callbackRead(u8 *buff, int len)=0; + + /** Callback after Write + * This is meant to be implimented by the user of the class. Called when the write has been finished. + * + */ + virtual int callbackWrite()=0; + + /** Write over USB + * This sends the data in the buffer over USB in a packet + * + * @param buff The buffer to write out + * @param len The length of the packet to send + * + */ + int write(u8 *buff, int len); + +/** Write over USB + * This sends the data in the buffer over USB in a packet, sends _writebuff and _writebuffsize + * + */ + int write() { + return write(_writebuff,_writebuffsize); + } + + /** Write over USB with no callback + * This sends the data in the buffer over USB in a packet, waits until the packet is sent, rather than doing a callback + * + * @param buff The buffer to write out + * @param len The length of the packet to send + * + */ + int writeNC(u8 *buff, int len); + + /** Write over USB + * This sends the data in the buffer over USB in a packet, waits until the packet is sent, rather than doing a callback, sends _writebuff and _writebuffsize + * + */ + int writeNC() { + return writeNC(_writebuff,_writebuffsize); + } + + /** Read the buffer USB + * This sends the data in the buffer over USB in a packet, waits until the packet is sent, rather than doing a callback + * + * @param buff The buffer to read into + * @param len The length of the packet to read in + * + * @param returns The number of bytes read + * + */ + int read(u8 *buff, int len); + + + void adkEnd() { + // _initok=false; + resetDevice(); + }; //if connection close + bool switchDevice(int device); + + //buffer + u8* _readbuff; + int _readbuffsize; + u8* _writebuff; + int _writebuffsize; + u8* _strbuff;//255bytes; + void sendString(const char *str); + +private: + + void sendString(int device, int index, const char *str); + int getProtocol(int device); + + const char *manufacturer; + const char *model; + const char *description; + const char *version; + const char *uri; + const char *serial; + + //endpoints + int input_ep; + int output_ep; + + int _device; + int _configuration; + int _interfaceNumber; + + //bool _initok; + +}; + +extern AndroidAccessory* _adk; //declared in cpp + + +#endif \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/AndroidADKTerm/AndroidAccessory/USBHost/USBHost.cpp Sun Apr 07 08:31:51 2013 +0000 @@ -0,0 +1,1093 @@ + +/* +Copyright (c) 2010 Peter Barrett + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. +*/ + + + +#include "mbed.h" +#include "USBHost.h" +#ifndef USBHOST_LOG + + +// Config (default uses x bytes) +#define MAX_DEVICES 8 // Max number of devices +#define MAX_ENDPOINTS_TOTAL 16 // Max number of endpoints total +#define MAX_ENDPOINTS_PER_DEVICE 8 // Max number of endpoints for any one device + +//#define USBLOG 1 +#if USBLOG +#define LOG(...) printf(__VA_ARGS__) +#else +#define LOG(...) do {} while(0) +#endif + +// USB host structures + +#define USB_RAM_SIZE 16*1024 // AHB SRAM block 1 TODO MACHINE DEPENDENT +#define USB_RAM_BASE 0x2007C000 + +#define TOKEN_SETUP 0 +#define TOKEN_IN 1 +#define TOKEN_OUT 2 + +// Status flags from hub +#define PORT_CONNECTION 0 +#define PORT_ENABLE 1 +#define PORT_SUSPEND 2 +#define PORT_OVER_CURRENT 3 +#define PORT_RESET 4 +#define PORT_POWER 8 +#define PORT_LOW_SPEED 9 + +#define C_PORT_CONNECTION 16 +#define C_PORT_ENABLE 17 +#define C_PORT_SUSPEND 18 +#define C_PORT_OVER_CURRENT 19 +#define C_PORT_RESET 20 + +typedef struct { + u8 bm_request_type; + u8 b_request; + u16 w_value; + u16 w_index; + u16 w_length; +} Setup; + + +// Hub stuff is kept private just to keep api simple +int SetPortFeature(int device, int feature, int index); +int ClearPortFeature(int device, int feature, int index); +int SetPortPower(int device, int port); +int SetPortReset(int device, int port); +int GetPortStatus(int device, int port, u32* status); + +//=================================================================== +//=================================================================== +// Hardware defines + +// HcControl +#define PeriodicListEnable 0x00000004 +#define IsochronousEnable 0x00000008 +#define ControlListEnable 0x00000010 +#define BulkListEnable 0x00000020 +#define OperationalMask 0x00000080 +#define HostControllerFunctionalState 0x000000C0 + +// HcCommandStatus +#define HostControllerReset 0x00000001 +#define ControlListFilled 0x00000002 +#define BulkListFilled 0x00000004 + +// HcInterruptStatus Register +#define WritebackDoneHead 0x00000002 +#define StartofFrame 0x00000004 +#define ResumeDetected 0x00000008 +#define UnrecoverableError 0x00000010 +#define FrameNumberOverflow 0x00000020 +#define RootHubStatusChange 0x00000040 +#define OwnershipChange 0x00000080 +#define MasterInterruptEnable 0x80000000 + +// HcRhStatus +#define SetGlobalPower 0x00010000 +#define DeviceRemoteWakeupEnable 0x00008000 + +// HcRhPortStatus (hub 0, port 1) +#define CurrentConnectStatus 0x00000001 +#define PortEnableStatus 0x00000002 +#define PortSuspendStatus 0x00000004 +#define PortOverCurrentIndicator 0x00000008 +#define PortResetStatus 0x00000010 + +#define PortPowerStatus 0x00000100 +#define LowspeedDevice 0x00000200 +#define HighspeedDevice 0x00000400 + +#define ConnectStatusChange (CurrentConnectStatus << 16) +#define PortResetStatusChange (PortResetStatus << 16) + + +#define TD_ROUNDING (u32)0x00040000 +#define TD_SETUP (u32)0x00000000 +#define TD_IN (u32)0x00100000 +#define TD_OUT (u32)0x00080000 +#define TD_DELAY_INT(x) (u32)((x) << 21) +#define TD_TOGGLE_0 (u32)0x02000000 +#define TD_TOGGLE_1 (u32)0x03000000 +#define TD_CC (u32)0xF0000000 + +// HostController EndPoint Descriptor +typedef struct { + volatile u32 Control; + volatile u32 TailTd; + volatile u32 HeadTd; + volatile u32 Next; +} HCED; + +// HostController Transfer Descriptor +typedef struct { + volatile u32 Control; + volatile u32 CurrBufPtr; + volatile u32 Next; + volatile u32 BufEnd; +} HCTD; + +// Host Controller Communication Area +typedef struct { + volatile u32 InterruptTable[32]; + volatile u16 FrameNumber; + volatile u16 FrameNumberPad; + volatile u32 DoneHead; + volatile u8 Reserved[120]; +} HCCA; + +//==================================================================================== +//==================================================================================== + +class HostController; +class Endpoint; +class Device; + +// must be 3*16 bytes long +class Endpoint +{ +public: + HCED EndpointDescriptor; // Pointer to EndpointDescriptor == Pointer to Endpoint + HCTD TDHead; + + enum State + { + Free, + NotQueued, + Idle, + SetupQueued, + DataQueued, + StatusQueued, + CallbackPending + }; + + volatile u8 CurrentState; + u8 Flags; // 0x80 In, 0x03 mask endpoint type + + u16 Length; + u8* Data; + USBCallback Callback; // Must be a multiple of 16 bytes long + void* UserData; + + int Address() + { + int ep = (EndpointDescriptor.Control >> 7) & 0xF; + if (ep) + ep |= Flags & 0x80; + return ep; + } + + int Device() + { + return EndpointDescriptor.Control & 0x7F; + } + + int Status() + { + return (TDHead.Control >> 28) & 0xF; + } + + u32 Enqueue(u32 head) + { + if (CurrentState == NotQueued) + { + EndpointDescriptor.Next = head; + head = (u32)&EndpointDescriptor; + CurrentState = Idle; + } + return head; + } +}; + +class Device +{ +public: + u8 _endpointMap[MAX_ENDPOINTS_PER_DEVICE*2]; + u8 Hub; + u8 Port; + u8 Addr; + u8 Pad; + + // Only if this device is a hub + u8 HubPortCount; // nonzero if this is a hub + u8 HubInterruptData; + u8 HubMap; + u8 HubMask; + + int Flags; // 1 = Disconnected + + Setup SetupBuffer; + + // Allocate endpoint zero + int Init(DeviceDescriptor* d, int hub, int port, int addr, int lowSpeed) + { + Hub = hub; + Port = port; + Addr = addr; + Flags = lowSpeed; + memset(_endpointMap,0xFF,sizeof(_endpointMap)); + return 0; + } + + int SetEndpointIndex(int ep, int endpointIndex) + { + for (int i = 0; i < MAX_ENDPOINTS_PER_DEVICE*2; i += 2) + { + if (_endpointMap[i] == 0xFF) // Add endpoint to map + { + _endpointMap[i] = ep; + _endpointMap[i+1] = endpointIndex; + return 0; + } + } + return ERR_ENDPOINT_NONE_LEFT; + } + + int GetEndpointIndex(int ep) + { + for (int i = 0; i < MAX_ENDPOINTS_PER_DEVICE*2; i += 2) + { + if (_endpointMap[i] == ep) + return _endpointMap[i+1]; + if (_endpointMap[i] == 0xFF) + break; + } + return -1; + } +}; + +class HostController +{ +public: + HCCA CommunicationArea; + Endpoint Endpoints[MAX_ENDPOINTS_TOTAL]; // Multiple of 16 + + Endpoint EndpointZero; // For device enumeration + HCTD _commonTail; + Setup _setupZero; + + Device Devices[MAX_DEVICES]; + u32 _frameNumber; // 32 bit ms counter + + u8 _callbacksPending; // Endpoints with callbacks are pending, set from ISR via ProcessDoneQueue + u8 _rootHubStatusChange; // Root hub status has changed, set from ISR + u8 _unused0; + u8 _unused1; + + u8 _connectPending; // Reset has initiated a connect + u8 _connectCountdown; // Number of ms left after reset before we can connect + u8 _connectHub; // Will connect on this hub + u8 _connectPort; // ... and this port + + u8 SRAM[0]; // Start of free SRAM + + void Loop() + { + u16 elapsed = CommunicationArea.FrameNumber - (u16)_frameNumber; // extend to 32 bits + _frameNumber += elapsed; + + // Do callbacks, if any + while (_callbacksPending) + { + for (int i = 0; i < MAX_ENDPOINTS_TOTAL; i++) + { + Endpoint* endpoint = Endpoints + i; + if (endpoint->CurrentState == Endpoint::CallbackPending) + { + LOG("Sorting Callbacks %i\n\r",endpoint->CurrentState); + _callbacksPending--; + endpoint->CurrentState = Endpoint::Idle; + LOG("SatusChanged %i\n\r",endpoint->CurrentState); + endpoint->Callback(endpoint->Device(),endpoint->Address(),endpoint->Status(),endpoint->Data,endpoint->Length,endpoint->UserData); + } + } + } + + // Deal with changes on the root hub + if (_rootHubStatusChange) + { + u32 status = LPC_USB->HcRhPortStatus1; + _rootHubStatusChange = 0; + if (status >> 16) + { + HubStatusChange(0,1,status); + LPC_USB->HcRhPortStatus1 = status & 0xFFFF0000; // clear status changes + } + } + + // Connect after reset timeout + if (_connectCountdown) + { + if (elapsed >= _connectCountdown) + { + _connectCountdown = 0; + Connect(_connectHub,_connectPort & 0x7F,_connectPort & 0x80); + } else + _connectCountdown -= elapsed; + } + } + + // HubInterrupt - bitmap in dev->HubInterruptData + void HubInterrupt(int device) + { + Device* dev = &Devices[device-1]; + for (int i = 0; i < dev->HubPortCount; i++) + { + int port = i+1; + if (dev->HubInterruptData & (1 << port)) + { + u32 status = 0; + GetPortStatus(device,port,&status); + if (status >> 16) + { + if (_connectPending && (status & ConnectStatusChange)) + continue; // Don't connect again until previous device has been added and addressed + + HubStatusChange(device,port,status); + if (status & ConnectStatusChange) + ClearPortFeature(device,C_PORT_CONNECTION,port); + if (status & PortResetStatusChange) + ClearPortFeature(device,C_PORT_RESET,port); + } + } + } + } + + static void HubInterruptCallback(int device, int endpoint, int status, u8* data, int len, void* userData) + { + HostController* controller = (HostController*)userData; + if (status == 0) + controller->HubInterrupt(device); + USBInterruptTransfer(device,endpoint,data,1,HubInterruptCallback,userData); + } + + int InitHub(int device) + { + u8 buf[16]; + int r= USBControlTransfer(device,DEVICE_TO_HOST | REQUEST_TYPE_CLASS | RECIPIENT_DEVICE,GET_DESCRIPTOR,(DESCRIPTOR_TYPE_HUB << 8),0,buf,sizeof(buf)); + if (r < 0) + return ERR_HUB_INIT_FAILED; + + // turn on power on the hubs ports + Device* dev = &Devices[device-1]; + int ports = buf[2]; + dev->HubPortCount = ports; + for (int i = 0; i < ports; i++) + SetPortPower(device,i+1); + + // Enable hub change interrupts + return USBInterruptTransfer(device,0x81,&dev->HubInterruptData,1,HubInterruptCallback,this); + } + + int AddEndpoint(int device, int ep, int attributes, int maxPacketSize, int interval) + { + LOG("AddEndpoint D:%02X A:%02X T:%02X P:%04X I:%02X\r\n",device,ep,attributes,maxPacketSize,interval); + Device* dev = &Devices[device-1]; + Endpoint* endpoint = AllocateEndpoint(device,ep,attributes,maxPacketSize); + if (!endpoint) + return ERR_ENDPOINT_NONE_LEFT; + dev->SetEndpointIndex(ep,endpoint - Endpoints); + endpoint->EndpointDescriptor.Control |= dev->Flags; // Map in slow speed + return 0; // TODO ed->bInterval + } + + int AddEndpoint(int device, EndpointDescriptor* ed) + { + return AddEndpoint(device,ed->bEndpointAddress,ed->bmAttributes,ed->wMaxPacketSize,ed->bInterval); + } + + // allocate a endpoint + Endpoint* AllocateEndpoint(int device, int endpointAddress, int type, int maxPacketSize) + { + for (int i = 0; i < MAX_ENDPOINTS_TOTAL; i++) + { + Endpoint* ep = &Endpoints[i]; + if (ep->CurrentState == 0) + { + //LOG("Allocated endpoint %d to %02X:%02X\r\n",i,device,endpointAddress); + ep->Flags = (endpointAddress & 0x80) | (type & 3); + ep->CurrentState = Endpoint::NotQueued; + ep->EndpointDescriptor.Control = (maxPacketSize << 16) | ((endpointAddress & 0x7F) << 7) | device; + return ep; + } + } + return 0; + } + + Endpoint* GetEndpoint(int device, int ep) + { + if (device == 0) + { + //printf("WARNING: USING DEVICE 0\n"); + return &EndpointZero; + } + if (device > MAX_DEVICES) + return 0; + int i = Devices[device-1].GetEndpointIndex(ep); + if (i == -1) + return 0; + return Endpoints + i; + } + + int Transfer(Endpoint* endpoint, int token, u8* data, int len, int state) + { + //LOG("Transfer %02X T:%d Len:%d S:%d\r\n",endpoint->Address(),token,len,state); + + int toggle = 0; + if (endpoint->Address() == 0) + toggle = (token == TOKEN_SETUP) ? TD_TOGGLE_0 : TD_TOGGLE_1; + + if (token != TOKEN_SETUP) + token = (token == TOKEN_IN ? TD_IN : TD_OUT); + + HCTD* head = &endpoint->TDHead; + HCTD* tail = &_commonTail; + + head->Control = TD_ROUNDING | token | TD_DELAY_INT(0) | toggle | TD_CC; + head->CurrBufPtr = (u32)data; + head->BufEnd = (u32)(data + len - 1); + head->Next = (u32)tail; + + HCED* ed = &endpoint->EndpointDescriptor; + ed->HeadTd = (u32)head | (ed->HeadTd & 0x00000002); // carry toggle + ed->TailTd = (u32)tail; + + //HCTD* td = head; + //LOG("%04X TD %08X %08X %08X Next:%08X\r\n",CommunicationArea.FrameNumber,td->Control,td->CurrBufPtr,td->BufEnd,td->Next); + //LOG("%04X ED %08X %08X %08X\r\n",CommunicationArea.FrameNumber,ed->Control,ed->HeadTd,ed->TailTd); + + switch (endpoint->Flags & 3) + { + case ENDPOINT_CONTROL: + LPC_USB->HcControlHeadED = endpoint->Enqueue(LPC_USB->HcControlHeadED); // May change state NotQueued->Idle + endpoint->CurrentState = state; // Get in before an int + LPC_USB->HcCommandStatus = LPC_USB->HcCommandStatus | ControlListFilled; + LPC_USB->HcControl = LPC_USB->HcControl | ControlListEnable; + break; + + case ENDPOINT_BULK: + LPC_USB->HcBulkHeadED = endpoint->Enqueue(LPC_USB->HcBulkHeadED); + endpoint->CurrentState = state; + LPC_USB->HcCommandStatus = LPC_USB->HcCommandStatus | BulkListFilled; + LPC_USB->HcControl = LPC_USB->HcControl | BulkListEnable; + break; + + case ENDPOINT_INTERRUPT: + CommunicationArea.InterruptTable[0] = endpoint->Enqueue(CommunicationArea.InterruptTable[0]); + endpoint->CurrentState = state; + LPC_USB->HcControl |= PeriodicListEnable; + break; + } + return 0; + } + + // Remove an endpoint from an active queue + bool Remove(HCED* ed, volatile HCED** queue) + { + if (*queue == 0) + return false; + if (*queue == (volatile HCED*)ed) + { + *queue = (volatile HCED*)ed->Next; // At head of queue + return true; + } + + volatile HCED* head = *queue; + while (head) + { + if (head->Next == (u32)ed) + { + head->Next = ed->Next; + return true; + } + head = (volatile HCED*)head->Next; + } + return false; + } + + void Release(Endpoint* endpoint) + { + if (endpoint->CurrentState == Endpoint::NotQueued) + { + // Never event used it, nothing to do + } + else + { + HCED* ed = (HCED*)endpoint; + ed->Control |= 0x4000; // SKIP + switch (endpoint->Flags & 0x03) + { + case ENDPOINT_CONTROL: + Remove(ed,(volatile HCED**)&LPC_USB->HcControlHeadED); + break; + case ENDPOINT_BULK: + Remove(ed,(volatile HCED**)&LPC_USB->HcBulkHeadED); + break; + case ENDPOINT_INTERRUPT: + for (int i = 0; i < 32; i++) + Remove(ed,(volatile HCED**)&CommunicationArea.InterruptTable[i]); + break; + } + + u16 fn = CommunicationArea.FrameNumber; + while (fn == CommunicationArea.FrameNumber) + ; // Wait for next frame + + } + + // In theory, the endpoint is now dead. + // TODO: Will Callbacks ever be pending? BUGBUG + memset(endpoint,0,sizeof(Endpoint)); + } + + // Pop the last TD from the list + HCTD* Reverse(HCTD* current) + { + HCTD *result = NULL,*temp; + while (current) + { + temp = (HCTD*)current->Next; + current->Next = (u32)result; + result = current; + current = temp; + } + return result; + } + + // Called from interrupt... + // Control endpoints use a state machine to progress through the transfers + void ProcessDoneQueue(u32 tdList) + { + HCTD* list = Reverse((HCTD*)tdList); + while (list) + { + Endpoint* endpoint = (Endpoint*)(list-1); + list = (HCTD*)list->Next; + int ep = endpoint->Address(); + bool in = endpoint->Flags & 0x80; + int status = (endpoint->TDHead.Control >> 28) & 0xF; + + //LOG("ProcessDoneQueue %02X %08X\r\n",ep,endpoint->TDHead.Control); + + if (status != 0) + { + LOG("ProcessDoneQueue status %02X %d\r\n",ep,status); + endpoint->CurrentState = Endpoint::Idle; + } else { + switch (endpoint->CurrentState) + { + case Endpoint::SetupQueued: + if (endpoint->Length == 0) + Transfer(endpoint,in ? TOKEN_OUT : TOKEN_IN,0,0,Endpoint::StatusQueued); // Skip Data Phase + else + Transfer(endpoint,in ? TOKEN_IN : TOKEN_OUT,endpoint->Data,endpoint->Length, Endpoint::DataQueued); // Setup is done, now Data + break; + + case Endpoint::DataQueued: + if (endpoint->TDHead.CurrBufPtr) + endpoint->Length = endpoint->TDHead.CurrBufPtr - (u32)endpoint->Data; + + if (ep == 0) + Transfer(endpoint,in ? TOKEN_OUT : TOKEN_IN,0,0,Endpoint::StatusQueued); // Data is done, now Status, Control only + else + endpoint->CurrentState = Endpoint::Idle; + break; + + case Endpoint::StatusQueued: // Transaction is done + endpoint->CurrentState = Endpoint::Idle; + break; + } + } + + // Complete, flag if we need a callback + if (endpoint->Callback && endpoint->CurrentState == Endpoint::Idle) + { + endpoint->CurrentState = Endpoint::CallbackPending; + _callbacksPending++; + } + } + } + + // Hack to reset devices that don't want to connect + int AddDevice(int hub, int port, bool isLowSpeed) + { + int device = AddDeviceCore(hub,port,isLowSpeed); + if (device < 0) + { + LOG("========RETRY ADD DEVICE========\r\n"); // This will go for ever.. TODO power cycle root? + Disconnect(hub,port); // Could not read descriptor at assigned address, reset this port and try again + ResetPort(hub,port); // Cheap bluetooth dongles often need this on a hotplug + return -1; + } + return device; + } + + int AddDeviceCore(int hub, int port, bool isLowSpeed) + { + int lowSpeed = isLowSpeed ? 0x2000 : 0; + DeviceDescriptor desc; + EndpointZero.EndpointDescriptor.Control = (8 << 16) | lowSpeed; // MaxPacketSize == 8 + int r = GetDescriptor(0,DESCRIPTOR_TYPE_DEVICE,0,(u8*)&desc,8); + if (r < 0) + { + LOG("FAILED TO LOAD DESCRIPTOR FOR DEVICE 0\r\n"); + return r; + } + + EndpointZero.EndpointDescriptor.Control = (desc.bMaxPacketSize << 16) | lowSpeed; // Actual MaxPacketSize + r = GetDescriptor(0,DESCRIPTOR_TYPE_DEVICE,0,(u8*)&desc,sizeof(desc)); + if (r < 0) + return r; + + LOG("\nClass %02X found %04X:%04X\r\n",desc.bDeviceClass,desc.idVendor,desc.idProduct); + + // Now assign the device an address, move off EndpointZero + int device = 0; + for (int i = 0; i < MAX_DEVICES; i++) + { + if (Devices[i].Port == 0) + { + device = i+1; + break; + } + } + if (!device) + return ERR_DEVICE_NONE_LEFT; + + r = SetAddress(0,device); + if (r) + return r; + DelayMS(2); + + // Now at a nonzero address, create control endpoint + Device* dev = &Devices[device-1]; + dev->Init(&desc,hub,port,device,lowSpeed); + AddEndpoint(device,0,ENDPOINT_CONTROL,desc.bMaxPacketSize,0); + _connectPending = 0; + + // Verify this all works + r = GetDescriptor(device,DESCRIPTOR_TYPE_DEVICE,0,(u8*)&desc,sizeof(desc)); + if (r < 0) + return r; + + // Set to interface 0 by default + // Calls LoadDevice if interface is found + r = SetConfigurationAndInterface(device,1,0,&desc); + + if (desc.bDeviceClass == CLASS_HUB) + InitHub(device); // Handle hubs in this code + + return device; + } + + // Walk descriptors and create endpoints for a given device + // TODO configuration !=1, alternate settings etc. + int SetConfigurationAndInterface(int device, int configuration, int interfaceNumber, DeviceDescriptor* desc) + { + u8 buffer[255]; + int err = GetDescriptor(device,DESCRIPTOR_TYPE_CONFIGURATION,0,buffer,sizeof(buffer)); + if (err < 0) + return err; + + err = SetConfiguration(device,configuration); + if (err < 0) + return err; + + // Add the endpoints for this interface + int len = buffer[2] | (buffer[3] << 8); + u8* d = buffer; + u8* end = d + len; + InterfaceDescriptor* found = 0; + while (d < end) + { + if (d[1] == DESCRIPTOR_TYPE_INTERFACE) + { + InterfaceDescriptor* id = (InterfaceDescriptor*)d; + if (id->bInterfaceNumber == interfaceNumber) + { + found = id; + d += d[0]; + while (d < end && d[1] != DESCRIPTOR_TYPE_INTERFACE) + { + switch (d[1]) + { + case DESCRIPTOR_TYPE_ENDPOINT: + AddEndpoint(device,(EndpointDescriptor*)d); + break; + default: + LOG("Skipping descriptor %02X (%d bytes)\r\n",d[1],d[0]); + } + d += d[0]; + } + } + } + d += d[0]; + } + + if (!found) + return ERR_INTERFACE_NOT_FOUND; + OnLoadDevice(device,desc,found); + return 0; + } + + void Init() + { + LOG("USB INIT (Controller is %d bytes)\r\n",sizeof(*this)); + memset(this,0,sizeof(HostController)); + EndpointZero.CurrentState = Endpoint::NotQueued; + HWInit(&CommunicationArea); + DelayMS(10); + } + + void ResetPort(int hub, int port) + { + LOG("ResetPort Hub:%d Port:%d\r\n",hub,port); + _connectPending++; // Only reset/add 1 device at a time + if (hub == 0) + LPC_USB->HcRhPortStatus1 = PortResetStatus; // Reset Root Hub, port 1 + else + SetPortReset(hub,port); // or reset other hub + } + + void Disconnect(int hub, int port) + { + LOG("Disconnect Hub:%d Port:%d\r\n",hub,port); // Mark a device for destruction + for (int i = 0; i < MAX_DEVICES; i++) + { + Device* dev = Devices + i; + if (dev->Port == port && dev->Hub == hub) + { + // Disconnect everything that is attached to this device if it is a hub + for (int p = 0; p < dev->HubPortCount; p++) + Disconnect(i+1,p+1); + + // Now release endpoints + for (int j = 1; j < MAX_ENDPOINTS_PER_DEVICE*2; j += 2) + { + u8 endpointIndex = dev->_endpointMap[j]; + if (endpointIndex != 0xFF) + Release(Endpoints + endpointIndex); + } + dev->Port = 0; // Device is now free + dev->Flags = 0; + return; + } + } + } + + // called after reset + void Connect(int hub, int port, bool lowspeed) + { + LOG("Connect Hub:%d Port:%d %s\r\n",hub,port,lowspeed ? "slow" : "full"); + AddDevice(hub,port,lowspeed); + } + + // Called from interrupt + void HubStatusChange(int hub, int port, u32 status) + { + LOG("HubStatusChange Hub:%d Port:%d %08X\r\n",hub,port,status); + if (status & ConnectStatusChange) + { + if (status & CurrentConnectStatus) // Connecting + ResetPort(hub,port); // Reset to initiate connect (state machine?) + else + Disconnect(hub,port); + } + + if (status & PortResetStatusChange) + { + if (!(status & PortResetStatus)) + { + _connectCountdown = 200; // Schedule a connection in 200ms + if (status & LowspeedDevice) + port |= 0x80; + _connectHub = hub; + _connectPort = port; + } + } + } + + #define HOST_CLK_EN (1<<0) + #define PORTSEL_CLK_EN (1<<3) + #define AHB_CLK_EN (1<<4) + #define CLOCK_MASK (HOST_CLK_EN | PORTSEL_CLK_EN | AHB_CLK_EN) + + #define FRAMEINTERVAL (12000-1) // 1ms + #define DEFAULT_FMINTERVAL ((((6 * (FRAMEINTERVAL - 210)) / 7) << 16) | FRAMEINTERVAL) + + void DelayMS(int ms) + { + u16 f = ms + CommunicationArea.FrameNumber; + while (f != CommunicationArea.FrameNumber) + ; + } + + static void HWInit(HCCA* cca) + { + NVIC_DisableIRQ(USB_IRQn); + + // turn on power for USB + LPC_SC->PCONP |= (1UL<<31); + // Enable USB host clock, port selection and AHB clock + LPC_USB->USBClkCtrl |= CLOCK_MASK; + // Wait for clocks to become available + while ((LPC_USB->USBClkSt & CLOCK_MASK) != CLOCK_MASK) + ; + + // We are a Host + LPC_USB->OTGStCtrl |= 1; + LPC_USB->USBClkCtrl &= ~PORTSEL_CLK_EN; // we don't need port selection clock until we do OTG + + // configure USB pins + LPC_PINCON->PINSEL1 &= ~((3<<26)|(3<<28)); + LPC_PINCON->PINSEL1 |= ((1<<26)|(1<<28)); // USB D+/D- + + LPC_PINCON->PINSEL3 &= ~((3 << 6) | (3 << 22)); // USB_PPWR, USB_OVRCR + LPC_PINCON->PINSEL3 |= ((2 << 6) | (2 << 22)); + + LPC_PINCON->PINSEL4 &= ~(3 << 18); // USB_CONNECT + LPC_PINCON->PINSEL4 |= (1 << 18); + + // Reset OHCI block + LPC_USB->HcControl = 0; + LPC_USB->HcControlHeadED = 0; + LPC_USB->HcBulkHeadED = 0; + + LPC_USB->HcCommandStatus = HostControllerReset; + LPC_USB->HcFmInterval = DEFAULT_FMINTERVAL; + LPC_USB->HcPeriodicStart = FRAMEINTERVAL*90/100; + + LPC_USB->HcControl = (LPC_USB->HcControl & (~HostControllerFunctionalState)) | OperationalMask; + LPC_USB->HcRhStatus = SetGlobalPower; + + LPC_USB->HcHCCA = (u32)cca; + LPC_USB->HcInterruptStatus |= LPC_USB->HcInterruptStatus; + LPC_USB->HcInterruptEnable = MasterInterruptEnable | WritebackDoneHead | RootHubStatusChange | FrameNumberOverflow; + + NVIC_SetPriority(USB_IRQn, 0); + NVIC_EnableIRQ(USB_IRQn); + while (cca->FrameNumber < 10) + ; // 10ms delay before diving in + } +}; + +//==================================================================================== +//==================================================================================== +// Host controller instance and Interrupt handler + +static HostController _controller __attribute__((at(USB_RAM_BASE))); + +extern "C" void USB_IRQHandler(void) __irq; +void USB_IRQHandler (void) __irq +{ + u32 int_status = LPC_USB->HcInterruptStatus; + + if(int_status & UnrecoverableError) //Error + { + LOG("USB_IRQHandler UnrecoverableError Please reset\r\n"); + } + + + if (int_status & RootHubStatusChange) // Root hub status change + _controller._rootHubStatusChange++; // Just flag the controller, will be processed in USBLoop + + u32 head = 0; + if (int_status & WritebackDoneHead) + { + head = _controller.CommunicationArea.DoneHead; // Writeback Done + _controller.CommunicationArea.DoneHead = 0; + } + LPC_USB->HcInterruptStatus = int_status; + + if (head) + _controller.ProcessDoneQueue(head); // TODO - low bit can be set BUGBUG +} + +//==================================================================================== +//==================================================================================== +// API Methods + +void USBInit() +{ + return _controller.Init(); +} + +void USBLoop() +{ + + return _controller.Loop(); + +} + +u8* USBGetBuffer(u32* len) +{ + *len = USB_RAM_SIZE - sizeof(HostController); + return _controller.SRAM; +} + +static Setup* GetSetup(int device) +{ + if (device == 0) + return &_controller._setupZero; + + if (device < 1 || device > MAX_DEVICES) + return 0; + return &_controller.Devices[device-1].SetupBuffer; +} + +// Loop until IO on endpoint is complete +static int WaitIODone(Endpoint* endpoint) +{ + LOG("Waiting\n\r"); + if (endpoint->CurrentState == Endpoint::NotQueued) + return 0; + while (endpoint->CurrentState != Endpoint::Idle) + //LOG("Loopz"); + USBLoop(); // May generate callbacks, mount or unmount devices etc + int status = endpoint->Status(); + if (status == 0) + return endpoint->Length; + LOG("DATA SENT\n\r"); + return -status; +} + +int USBTransfer(int device, int ep, u8 flags, u8* data, int length, USBCallback callback, void* userData) +{ + Endpoint* endpoint = _controller.GetEndpoint(device,ep); + if (!endpoint) + return ERR_ENDPOINT_NOT_FOUND; + + WaitIODone(endpoint); + LOG("before ep=%x,callback=%p\r\n",endpoint,endpoint->Callback); + endpoint->Flags = flags; + endpoint->Data = data; + endpoint->Length = length; + endpoint->Callback = callback; + endpoint->UserData = userData; + LOG("ep=%x,callback=%p\r\n",ep,callback); + if (ep == 0) + _controller.Transfer(endpoint,TOKEN_SETUP,(u8*)GetSetup(device),8,Endpoint::SetupQueued); + else + _controller.Transfer(endpoint,flags & 0x80 ? TOKEN_IN : TOKEN_OUT,data,length,Endpoint::DataQueued); + if (callback) + return IO_PENDING; + return WaitIODone(endpoint); +} + +int USBControlTransfer(int device, int request_type, int request, int value, int index, u8* data, int length, USBCallback callback, void * userData) +{ + Setup* setup = GetSetup(device); + if (!setup) + return ERR_DEVICE_NOT_FOUND; + + // Async control calls may overwrite setup buffer of previous call, so we need to wait before setting up next call + WaitIODone(_controller.GetEndpoint(device,0)); + + setup->bm_request_type = request_type; + setup->b_request = request; + setup->w_value = value; + setup->w_index = index; + setup->w_length = length; + return USBTransfer(device,0,request_type & DEVICE_TO_HOST,data,length,callback,userData); +} + +int USBInterruptTransfer(int device, int ep, u8* data, int length, USBCallback callback, void* userData) +{ + return USBTransfer(device,ep,(ep & 0x80) | ENDPOINT_INTERRUPT,data,length,callback,userData); +} + +int USBBulkTransfer(int device, int ep, u8* data, int length, USBCallback callback, void* userData) +{ + return USBTransfer(device,ep,(ep & 0x80) | ENDPOINT_BULK,data,length,callback,userData); +} + +int GetDescriptor(int device, int descType,int descIndex, u8* data, int length) +{ + return USBControlTransfer(device,DEVICE_TO_HOST | RECIPIENT_DEVICE, GET_DESCRIPTOR,(descType << 8)|(descIndex), 0, data, length, 0); +} + +int GetString(int device, int index, char* dst, int length) +{ + u8 buffer[255]; + int le = GetDescriptor(device,DESCRIPTOR_TYPE_STRING,index,buffer,sizeof(buffer)); + if (le < 0) + return le; + if (length < 1) + return -1; + length <<= 1; + if (le > length) + le = length; + for (int j = 2; j < le; j += 2) + *dst++ = buffer[j]; + *dst = 0; + return (le>>1)-1; +} + +int SetAddress(int device, int new_addr) +{ + return USBControlTransfer(device,HOST_TO_DEVICE | RECIPIENT_DEVICE, SET_ADDRESS, new_addr, 0, 0, 0, 0); +} + +int SetConfiguration(int device, int configNum) +{ + return USBControlTransfer(device,HOST_TO_DEVICE | RECIPIENT_DEVICE, SET_CONFIGURATION, configNum, 0, 0, 0, 0); +} + +int SetInterface(int device, int ifNum, int altNum) +{ + return USBControlTransfer(device,HOST_TO_DEVICE | RECIPIENT_INTERFACE, SET_INTERFACE, altNum, ifNum, 0, 0, 0); +} + +// HUB stuff +int SetPortFeature(int device, int feature, int index) +{ + return USBControlTransfer(device,HOST_TO_DEVICE | REQUEST_TYPE_CLASS | RECIPIENT_OTHER,SET_FEATURE,feature,index,0,0); +} + +int ClearPortFeature(int device, int feature, int index) +{ + return USBControlTransfer(device,HOST_TO_DEVICE | REQUEST_TYPE_CLASS | RECIPIENT_OTHER,CLEAR_FEATURE,feature,index,0,0); +} + +int SetPortPower(int device, int port) +{ + int r = SetPortFeature(device,PORT_POWER,port); + _controller.DelayMS(20); // 80ms to turn on a hubs power... DESCRIPTOR? todo + return r; +} + +int SetPortReset(int device, int port) +{ + return SetPortFeature(device,PORT_RESET,port); +} + +int GetPortStatus(int device, int port, u32* status) +{ + return USBControlTransfer(device,DEVICE_TO_HOST | REQUEST_TYPE_CLASS | RECIPIENT_OTHER,GET_STATUS,0,port,(u8*)status,4); +} + +#endif \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/AndroidADKTerm/AndroidAccessory/USBHost/USBHost.h Sun Apr 07 08:31:51 2013 +0000 @@ -0,0 +1,221 @@ + +/* +Copyright (c) 2010 Peter Barrett + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. +*/ + + +#ifndef USBHOST_H +#define USBHOST_H + +//log mode on off +//#define USBHOST_LOG + + +#ifndef u8 +typedef unsigned char u8; +typedef unsigned short u16; +typedef unsigned long u32; + +typedef char s8; +typedef short s16; +typedef char s32; +#endif + +#define ENDPOINT_CONTROL 0 +#define ENDPOINT_ISOCRONOUS 1 +#define ENDPOINT_BULK 2 +#define ENDPOINT_INTERRUPT 3 + +#define DESCRIPTOR_TYPE_DEVICE 1 +#define DESCRIPTOR_TYPE_CONFIGURATION 2 +#define DESCRIPTOR_TYPE_STRING 3 +#define DESCRIPTOR_TYPE_INTERFACE 4 +#define DESCRIPTOR_TYPE_ENDPOINT 5 + +#define DESCRIPTOR_TYPE_HID 0x21 +#define DESCRIPTOR_TYPE_REPORT 0x22 +#define DESCRIPTOR_TYPE_PHYSICAL 0x23 +#define DESCRIPTOR_TYPE_HUB 0x29 + +enum USB_CLASS_CODE +{ + CLASS_DEVICE, + CLASS_AUDIO, + CLASS_COMM_AND_CDC_CONTROL, + CLASS_HID, + CLASS_PHYSICAL = 0x05, + CLASS_STILL_IMAGING, + CLASS_PRINTER, + CLASS_MASS_STORAGE, + CLASS_HUB, + CLASS_CDC_DATA, + CLASS_SMART_CARD, + CLASS_CONTENT_SECURITY = 0x0D, + CLASS_VIDEO = 0x0E, + CLASS_DIAGNOSTIC_DEVICE = 0xDC, + CLASS_WIRELESS_CONTROLLER = 0xE0, + CLASS_MISCELLANEOUS = 0xEF, + CLASS_APP_SPECIFIC = 0xFE, + CLASS_VENDOR_SPECIFIC = 0xFF +}; + +#define DEVICE_TO_HOST 0x80 +#define HOST_TO_DEVICE 0x00 +#define REQUEST_TYPE_STANDARD 0x00 +#define REQUEST_TYPE_CLASS 0x20 +#define REQUEST_TYPE_VENDOR 0x40 +#define RECIPIENT_DEVICE 0x00 +#define RECIPIENT_INTERFACE 0x01 +#define RECIPIENT_ENDPOINT 0x02 +#define RECIPIENT_OTHER 0x03 + +#define GET_STATUS 0 +#define CLEAR_FEATURE 1 +#define SET_FEATURE 3 +#define SET_ADDRESS 5 +#define GET_DESCRIPTOR 6 +#define SET_DESCRIPTOR 7 +#define GET_CONFIGURATION 8 +#define SET_CONFIGURATION 9 +#define GET_INTERFACE 10 +#define SET_INTERFACE 11 +#define SYNCH_FRAME 11 + +/* HID Request Codes */ +#define HID_REQUEST_GET_REPORT 0x01 +#define HID_REQUEST_GET_IDLE 0x02 +#define HID_REQUEST_GET_PROTOCOL 0x03 +#define HID_REQUEST_SET_REPORT 0x09 +#define HID_REQUEST_SET_IDLE 0x0A +#define HID_REQUEST_SET_PROTOCOL 0x0B + +/* HID Report Types */ +#define HID_REPORT_INPUT 0x01 +#define HID_REPORT_OUTPUT 0x02 +#define HID_REPORT_FEATURE 0x03 + + +// -5 is nak +/* +0010 ACK Handshake +1010 NAK Handshake +1110 STALL Handshake +0110 NYET (No Response Yet) +*/ + +#define IO_PENDING -100 +#define ERR_ENDPOINT_NONE_LEFT -101 +#define ERR_ENDPOINT_NOT_FOUND -102 +#define ERR_DEVICE_NOT_FOUND -103 +#define ERR_DEVICE_NONE_LEFT -104 +#define ERR_HUB_INIT_FAILED -105 +#define ERR_INTERFACE_NOT_FOUND -106 + +typedef struct +{ + u8 bLength; + u8 bDescriptorType; + u16 bcdUSB; + u8 bDeviceClass; + u8 bDeviceSubClass; + u8 bDeviceProtocol; + u8 bMaxPacketSize; + u16 idVendor; + u16 idProduct; + u16 bcdDevice; // version + u8 iManufacturer; + u8 iProduct; + u8 iSerialNumber; + u8 bNumConfigurations; +} DeviceDescriptor; // 16 bytes + +typedef struct +{ + u8 bLength; + u8 bDescriptorType; + u16 wTotalLength; + u8 bNumInterfaces; + u8 bConfigurationValue; // Value to use as an argument to select this configuration + u8 iConfiguration; // Index of String Descriptor describing this configuration + u8 bmAttributes; // Bitmap D7 Reserved, set to 1. (USB 1.0 Bus Powered),D6 Self Powered,D5 Remote Wakeup,D4..0 = 0 + u8 bMaxPower; // Maximum Power Consumption in 2mA units +} ConfigurationDescriptor; + +typedef struct +{ + u8 bLength; + u8 bDescriptorType; + u8 bInterfaceNumber; + u8 bAlternateSetting; + u8 bNumEndpoints; + u8 bInterfaceClass; + u8 bInterfaceSubClass; + u8 bInterfaceProtocol; + u8 iInterface; // Index of String Descriptor Describing this interface +} InterfaceDescriptor; + +typedef struct +{ + u8 bLength; + u8 bDescriptorType; + u8 bEndpointAddress; // Bits 0:3 endpoint, Bits 7 Direction 0 = Out, 1 = In (Ignored for Control Endpoints) + u8 bmAttributes; // Bits 0:1 00 = Control, 01 = Isochronous, 10 = Bulk, 11 = Interrupt + u16 wMaxPacketSize; + u8 bInterval; // Interval for polling endpoint data transfers. +} EndpointDescriptor; + +typedef struct { + u8 bLength; + u8 bDescriptorType; + u16 bcdHID; + u8 bCountryCode; + u8 bNumDescriptors; + u8 bDescriptorType2; + u16 wDescriptorLength; +} HIDDescriptor; + +//============================================================================ +//============================================================================ + + +void USBInit(); +void USBLoop(); +u8* USBGetBuffer(u32* len); + +// Optional callback for transfers, called at interrupt time +typedef void (*USBCallback)(int device, int endpoint, int status, u8* data, int len, void* userData); + +// Transfers +int USBControlTransfer(int device, int request_type, int request, int value, int index, u8* data, int length, USBCallback callback = 0, void* userData = 0); +int USBInterruptTransfer(int device, int ep, u8* data, int length, USBCallback callback = 0, void* userData = 0); +int USBBulkTransfer(int device, int ep, u8* data, int length, USBCallback callback = 0, void* userData = 0); + +// Standard Device methods +int GetDescriptor(int device, int descType, int descIndex, u8* data, int length); +int GetString(int device, int index, char* dst, int length); +int SetAddress(int device, int new_addr); +int SetConfiguration(int device, int configNum); +int SetInterface(int device, int ifNum, int altNum); + +// Implemented to notify app of the arrival of a device +void OnLoadDevice(int device, DeviceDescriptor* deviceDesc, InterfaceDescriptor* interfaceDesc); + +#endif \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/AndroidADKTerm/AndroidAccessory/USBHost/USBHost_log.cpp Sun Apr 07 08:31:51 2013 +0000 @@ -0,0 +1,1175 @@ + +/* +Copyright (c) 2010 Peter Barrett + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. +*/ + + +#include "mbed.h" +#include "USBHost.h" +//#define USBHOST_LOG +#ifdef USBHOST_LOG + +// Config (default uses x bytes) +#define MAX_DEVICES 8 // Max number of devices +#define MAX_ENDPOINTS_TOTAL 16 // Max number of endpoints total +#define MAX_ENDPOINTS_PER_DEVICE 8 // Max number of endpoints for any one device + +//#define USBLOG 1 +#if USBLOG +#define LOG(...) printf(__VA_ARGS__) +#else +#define LOG(...) do {} while(0) +#endif + +// USB host structures + +#define USB_RAM_SIZE 16*1024 // AHB SRAM block 1 TODO MACHINE DEPENDENT +#define USB_RAM_BASE 0x2007C000 + +#define TOKEN_SETUP 0 +#define TOKEN_IN 1 +#define TOKEN_OUT 2 + +// Status flags from hub +#define PORT_CONNECTION 0 +#define PORT_ENABLE 1 +#define PORT_SUSPEND 2 +#define PORT_OVER_CURRENT 3 +#define PORT_RESET 4 +#define PORT_POWER 8 +#define PORT_LOW_SPEED 9 + +#define C_PORT_CONNECTION 16 +#define C_PORT_ENABLE 17 +#define C_PORT_SUSPEND 18 +#define C_PORT_OVER_CURRENT 19 +#define C_PORT_RESET 20 + +typedef struct { + u8 bm_request_type; + u8 b_request; + u16 w_value; + u16 w_index; + u16 w_length; +} Setup; + + +// Hub stuff is kept private just to keep api simple +int SetPortFeature(int device, int feature, int index); +int ClearPortFeature(int device, int feature, int index); +int SetPortPower(int device, int port); +int SetPortReset(int device, int port); +int GetPortStatus(int device, int port, u32* status); + +//=================================================================== +//=================================================================== +// Hardware defines + +// HcControl +#define PeriodicListEnable 0x00000004 +#define IsochronousEnable 0x00000008 +#define ControlListEnable 0x00000010 +#define BulkListEnable 0x00000020 +#define OperationalMask 0x00000080 +#define HostControllerFunctionalState 0x000000C0 + +// HcCommandStatus +#define HostControllerReset 0x00000001 +#define ControlListFilled 0x00000002 +#define BulkListFilled 0x00000004 + +// HcInterruptStatus Register +#define WritebackDoneHead 0x00000002 +#define StartofFrame 0x00000004 +#define ResumeDetected 0x00000008 +#define UnrecoverableError 0x00000010 +#define FrameNumberOverflow 0x00000020 +#define RootHubStatusChange 0x00000040 +#define OwnershipChange 0x00000080 +#define MasterInterruptEnable 0x80000000 + +// HcRhStatus +#define SetGlobalPower 0x00010000 +#define DeviceRemoteWakeupEnable 0x00008000 + +// HcRhPortStatus (hub 0, port 1) +#define CurrentConnectStatus 0x00000001 +#define PortEnableStatus 0x00000002 +#define PortSuspendStatus 0x00000004 +#define PortOverCurrentIndicator 0x00000008 +#define PortResetStatus 0x00000010 + +#define PortPowerStatus 0x00000100 +#define LowspeedDevice 0x00000200 +#define HighspeedDevice 0x00000400 + +#define ConnectStatusChange (CurrentConnectStatus << 16) +#define PortResetStatusChange (PortResetStatus << 16) + + +#define TD_ROUNDING (u32)0x00040000 +#define TD_SETUP (u32)0x00000000 +#define TD_IN (u32)0x00100000 +#define TD_OUT (u32)0x00080000 +#define TD_DELAY_INT(x) (u32)((x) << 21) +#define TD_TOGGLE_0 (u32)0x02000000 +#define TD_TOGGLE_1 (u32)0x03000000 +#define TD_CC (u32)0xF0000000 + +// HostController EndPoint Descriptor +typedef struct { + volatile u32 Control; + volatile u32 TailTd; + volatile u32 HeadTd; + volatile u32 Next; +} HCED; + +// HostController Transfer Descriptor +typedef struct { + volatile u32 Control; + volatile u32 CurrBufPtr; + volatile u32 Next; + volatile u32 BufEnd; +} HCTD; + +// Host Controller Communication Area +typedef struct { + volatile u32 InterruptTable[32]; + volatile u16 FrameNumber; + volatile u16 FrameNumberPad; + volatile u32 DoneHead; + volatile u8 Reserved[120]; +} HCCA; + +//==================================================================================== +//==================================================================================== + +class HostController; +class Endpoint; +class Device; + +// must be 3*16 bytes long +class Endpoint +{ +public: + HCED EndpointDescriptor; // Pointer to EndpointDescriptor == Pointer to Endpoint + HCTD TDHead; + + enum State + { + Free, + NotQueued, + Idle, + SetupQueued, + DataQueued, + StatusQueued, + CallbackPending + }; + + volatile u8 CurrentState; + u8 Flags; // 0x80 In, 0x03 mask endpoint type + + u16 Length; + u8* Data; + USBCallback Callback; // Must be a multiple of 16 bytes long + void* UserData; + + int Address() + { + int ep = (EndpointDescriptor.Control >> 7) & 0xF; + if (ep) + ep |= Flags & 0x80; + return ep; + } + + int Device() + { + return EndpointDescriptor.Control & 0x7F; + } + + int Status() + { + return (TDHead.Control >> 28) & 0xF; + } + + u32 Enqueue(u32 head) + { + if (CurrentState == NotQueued) + { + EndpointDescriptor.Next = head; + head = (u32)&EndpointDescriptor; + CurrentState = Idle; + } + return head; + } +}; + +class Device +{ +public: + u8 _endpointMap[MAX_ENDPOINTS_PER_DEVICE*2]; + u8 Hub; + u8 Port; + u8 Addr; + u8 Pad; + + // Only if this device is a hub + u8 HubPortCount; // nonzero if this is a hub + u8 HubInterruptData; + u8 HubMap; + u8 HubMask; + + int Flags; // 1 = Disconnected + + Setup SetupBuffer; + + // Allocate endpoint zero + int Init(DeviceDescriptor* d, int hub, int port, int addr, int lowSpeed) + { + Hub = hub; + Port = port; + Addr = addr; + Flags = lowSpeed; + memset(_endpointMap,0xFF,sizeof(_endpointMap)); + return 0; + } + + int SetEndpointIndex(int ep, int endpointIndex) + { + for (int i = 0; i < MAX_ENDPOINTS_PER_DEVICE*2; i += 2) + { + if (_endpointMap[i] == 0xFF) // Add endpoint to map + { + _endpointMap[i] = ep; + _endpointMap[i+1] = endpointIndex; + return 0; + } + } + return ERR_ENDPOINT_NONE_LEFT; + } + + int GetEndpointIndex(int ep) + { + for (int i = 0; i < MAX_ENDPOINTS_PER_DEVICE*2; i += 2) + { + if (_endpointMap[i] == ep) + return _endpointMap[i+1]; + if (_endpointMap[i] == 0xFF) + break; + } + return -1; + } +}; + +class HostController +{ +public: + HCCA CommunicationArea; + Endpoint Endpoints[MAX_ENDPOINTS_TOTAL]; // Multiple of 16 + + Endpoint EndpointZero; // For device enumeration + HCTD _commonTail; + Setup _setupZero; + + Device Devices[MAX_DEVICES]; + u32 _frameNumber; // 32 bit ms counter + + u8 _callbacksPending; // Endpoints with callbacks are pending, set from ISR via ProcessDoneQueue + u8 _rootHubStatusChange; // Root hub status has changed, set from ISR + u8 _unused0; + u8 _unused1; + + u8 _connectPending; // Reset has initiated a connect + u8 _connectCountdown; // Number of ms left after reset before we can connect + u8 _connectHub; // Will connect on this hub + u8 _connectPort; // ... and this port + + u8 SRAM[0]; // Start of free SRAM + + void Loop() + { + // printf("Enter Loop \r\n"); + u16 elapsed = CommunicationArea.FrameNumber - (u16)_frameNumber; // extend to 32 bits + _frameNumber += elapsed; + + // Do callbacks, if any + while (_callbacksPending) + { + for (int i = 0; i < MAX_ENDPOINTS_TOTAL; i++) + { + Endpoint* endpoint = Endpoints + i; + if (endpoint->CurrentState == Endpoint::CallbackPending) + { + _callbacksPending--; + endpoint->CurrentState = Endpoint::Idle; + LOG("CurrentState Change Idle endpoint(%d) currentState =%d\r\n",endpoint->Address(),endpoint->CurrentState); + endpoint->Callback(endpoint->Device(),endpoint->Address(),endpoint->Status(),endpoint->Data,endpoint->Length,endpoint->UserData); + } + } + } + + // Deal with changes on the root hub + if (_rootHubStatusChange) + { + u32 status = LPC_USB->HcRhPortStatus1; + _rootHubStatusChange = 0; + if (status >> 16) + { + HubStatusChange(0,1,status); + LPC_USB->HcRhPortStatus1 = status & 0xFFFF0000; // clear status changes + } + } + + // Connect after reset timeout + if (_connectCountdown) + { + if (elapsed >= _connectCountdown) + { + _connectCountdown = 0; + Connect(_connectHub,_connectPort & 0x7F,_connectPort & 0x80); + } else + _connectCountdown -= elapsed; + } + + // printf("Outer Loop \r\n"); + } + + // HubInterrupt - bitmap in dev->HubInterruptData + void HubInterrupt(int device) + { + Device* dev = &Devices[device-1]; + for (int i = 0; i < dev->HubPortCount; i++) + { + int port = i+1; + if (dev->HubInterruptData & (1 << port)) + { + u32 status = 0; + GetPortStatus(device,port,&status); + if (status >> 16) + { + if (_connectPending && (status & ConnectStatusChange)) + continue; // Don't connect again until previous device has been added and addressed + + HubStatusChange(device,port,status); + if (status & ConnectStatusChange) + ClearPortFeature(device,C_PORT_CONNECTION,port); + if (status & PortResetStatusChange) + ClearPortFeature(device,C_PORT_RESET,port); + } + } + } + } + + static void HubInterruptCallback(int device, int endpoint, int status, u8* data, int len, void* userData) + { + HostController* controller = (HostController*)userData; + if (status == 0) + controller->HubInterrupt(device); + USBInterruptTransfer(device,endpoint,data,1,HubInterruptCallback,userData); + } + + int InitHub(int device) + { + u8 buf[16]; + int r= USBControlTransfer(device,DEVICE_TO_HOST | REQUEST_TYPE_CLASS | RECIPIENT_DEVICE,GET_DESCRIPTOR,(DESCRIPTOR_TYPE_HUB << 8),0,buf,sizeof(buf)); + if (r < 0) + return ERR_HUB_INIT_FAILED; + + // turn on power on the hubs ports + Device* dev = &Devices[device-1]; + int ports = buf[2]; + dev->HubPortCount = ports; + for (int i = 0; i < ports; i++) + SetPortPower(device,i+1); + + // Enable hub change interrupts + return USBInterruptTransfer(device,0x81,&dev->HubInterruptData,1,HubInterruptCallback,this); + } + + int AddEndpoint(int device, int ep, int attributes, int maxPacketSize, int interval) + { + LOG("AddEndpoint D:%02X A:%02X T:%02X P:%04X I:%02X\r\n",device,ep,attributes,maxPacketSize,interval); + Device* dev = &Devices[device-1]; + Endpoint* endpoint = AllocateEndpoint(device,ep,attributes,maxPacketSize); + if (!endpoint) + return ERR_ENDPOINT_NONE_LEFT; + dev->SetEndpointIndex(ep,endpoint - Endpoints); + endpoint->EndpointDescriptor.Control |= dev->Flags; // Map in slow speed + return 0; // TODO ed->bInterval + } + + int AddEndpoint(int device, EndpointDescriptor* ed) + { + return AddEndpoint(device,ed->bEndpointAddress,ed->bmAttributes,ed->wMaxPacketSize,ed->bInterval); + } + + // allocate a endpoint + Endpoint* AllocateEndpoint(int device, int endpointAddress, int type, int maxPacketSize) + { + for (int i = 0; i < MAX_ENDPOINTS_TOTAL; i++) + { + Endpoint* ep = &Endpoints[i]; + if (ep->CurrentState == 0) + { + //LOG("Allocated endpoint %d to %02X:%02X\r\n",i,device,endpointAddress); + ep->Flags = (endpointAddress & 0x80) | (type & 3); + ep->CurrentState = Endpoint::NotQueued; + ep->EndpointDescriptor.Control = (maxPacketSize << 16) | ((endpointAddress & 0x7F) << 7) | device; + return ep; + } + } + return 0; + } + + Endpoint* GetEndpoint(int device, int ep) + { + if (device == 0) + { + //printf("WARNING: USING DEVICE 0\n"); + return &EndpointZero; + } + if (device > MAX_DEVICES) + return 0; + int i = Devices[device-1].GetEndpointIndex(ep); + if (i == -1) + return 0; + return Endpoints + i; + } + + int Transfer(Endpoint* endpoint, int token, u8* data, int len, int state) + { + //Makoto edit comment out + // LOG("Transfer start\r\n"); + LOG("Transfer ep=%02X Token:%d Len:%d State:%d\r\n",endpoint->Address(),token,len,state); + + int toggle = 0; + if (endpoint->Address() == 0) + toggle = (token == TOKEN_SETUP) ? TD_TOGGLE_0 : TD_TOGGLE_1; + + if (token != TOKEN_SETUP) + token = (token == TOKEN_IN ? TD_IN : TD_OUT); + + HCTD* head = &endpoint->TDHead; + HCTD* tail = &_commonTail; + + head->Control = TD_ROUNDING | token | TD_DELAY_INT(0) | toggle | TD_CC; + head->CurrBufPtr = (u32)data; + head->BufEnd = (u32)(data + len - 1); + head->Next = (u32)tail; + + HCED* ed = &endpoint->EndpointDescriptor; + ed->HeadTd = (u32)head | (ed->HeadTd & 0x00000002); // carry toggle + ed->TailTd = (u32)tail; + + //HCTD* td = head; + //LOG("%04X TD %08X %08X %08X Next:%08X\r\n",CommunicationArea.FrameNumber,td->Control,td->CurrBufPtr,td->BufEnd,td->Next); + //LOG("%04X ED %08X %08X %08X\r\n",CommunicationArea.FrameNumber,ed->Control,ed->HeadTd,ed->TailTd); + + //LOG("Transfer run01\r\n"); + switch (endpoint->Flags & 3) + { + case ENDPOINT_CONTROL: + LOG("Transfer run ENDPOINT_CONTROL\r\n"); + LPC_USB->HcControlHeadED = endpoint->Enqueue(LPC_USB->HcControlHeadED); // May change state NotQueued->Idle + //LOG("Transfer run02\r\n"); + LOG("endpoint->CurrentState change %d->%d\r\n",endpoint->CurrentState,state); + endpoint->CurrentState = state; // Get in before an int + LPC_USB->HcCommandStatus = LPC_USB->HcCommandStatus | ControlListFilled; + LPC_USB->HcControl = LPC_USB->HcControl | ControlListEnable; + + break; + + case ENDPOINT_BULK: + LOG("Transfer run ENDPOINT_BULK\r\n"); + LPC_USB->HcBulkHeadED = endpoint->Enqueue(LPC_USB->HcBulkHeadED); + LOG("endpoint->CurrentState change %d->%d\r\n",endpoint->CurrentState,state); + endpoint->CurrentState = state; + LPC_USB->HcCommandStatus = LPC_USB->HcCommandStatus | BulkListFilled; + LPC_USB->HcControl = LPC_USB->HcControl | BulkListEnable; + break; + + case ENDPOINT_INTERRUPT: + LOG("Transfer run ENDPOINT_INTERRUPT\r\n"); + CommunicationArea.InterruptTable[0] = endpoint->Enqueue(CommunicationArea.InterruptTable[0]); + LOG("endpoint->CurrentState change %d->%d\r\n",endpoint->CurrentState,state); + endpoint->CurrentState = state; + LPC_USB->HcControl |= PeriodicListEnable; + break; + } + + LOG("Transfer end\r\n"); + return 0; + } + + // Remove an endpoint from an active queue + bool Remove(HCED* ed, volatile HCED** queue) + { + if (*queue == 0) + return false; + if (*queue == (volatile HCED*)ed) + { + *queue = (volatile HCED*)ed->Next; // At head of queue + return true; + } + + volatile HCED* head = *queue; + while (head) + { + if (head->Next == (u32)ed) + { + head->Next = ed->Next; + return true; + } + head = (volatile HCED*)head->Next; + } + return false; + } + + void Release(Endpoint* endpoint) + { + if (endpoint->CurrentState == Endpoint::NotQueued) + { + // Never event used it, nothing to do + } + else + { + HCED* ed = (HCED*)endpoint; + ed->Control |= 0x4000; // SKIP + switch (endpoint->Flags & 0x03) + { + case ENDPOINT_CONTROL: + Remove(ed,(volatile HCED**)&LPC_USB->HcControlHeadED); + break; + case ENDPOINT_BULK: + Remove(ed,(volatile HCED**)&LPC_USB->HcBulkHeadED); + break; + case ENDPOINT_INTERRUPT: + for (int i = 0; i < 32; i++) + Remove(ed,(volatile HCED**)&CommunicationArea.InterruptTable[i]); + break; + } + + u16 fn = CommunicationArea.FrameNumber; + while (fn == CommunicationArea.FrameNumber) + ; // Wait for next frame + + } + + // In theory, the endpoint is now dead. + // TODO: Will Callbacks ever be pending? BUGBUG + memset(endpoint,0,sizeof(Endpoint)); + } + + // Pop the last TD from the list + HCTD* Reverse(HCTD* current) + { + HCTD *result = NULL,*temp; + while (current) + { + temp = (HCTD*)current->Next; + current->Next = (u32)result; + result = current; + current = temp; + } + return result; + } + + // Called from interrupt... + // Control endpoints use a state machine to progress through the transfers + void ProcessDoneQueue(u32 tdList) + { + + LOG("<<ProcessDoneQueue enter>>\r\n"); + HCTD* list = Reverse((HCTD*)tdList); + while (list) + { + Endpoint* endpoint = (Endpoint*)(list-1); + list = (HCTD*)list->Next; + int ep = endpoint->Address(); + bool in = endpoint->Flags & 0x80; + int status = (endpoint->TDHead.Control >> 28) & 0xF; + + //LOG("ProcessDoneQueue %02X %08X\r\n",ep,endpoint->TDHead.Control); + + if (status != 0) + { + LOG("ProcessDoneQueue status %02X %d\r\n",ep,status); + LOG("CurrentState Change Idle endpoint(%d) currentState =%d\r\n",endpoint->Address(),endpoint->CurrentState); + endpoint->CurrentState = Endpoint::Idle; + + + } else { + switch (endpoint->CurrentState) + { + case Endpoint::SetupQueued: + if (endpoint->Length == 0) + { + LOG("ProcessDoneQueue endpoint->Length == 0 endpoint->CurrentState=%d\r\n",endpoint->CurrentState); + Transfer(endpoint,in ? TOKEN_OUT : TOKEN_IN,0,0,Endpoint::StatusQueued); // Skip Data Phase + } + else + { + LOG("ProcessDoneQueue endpoint->Length != 0 endpoint->CurrentState=%d\r\n",endpoint->CurrentState); + Transfer(endpoint,in ? TOKEN_IN : TOKEN_OUT,endpoint->Data,endpoint->Length, Endpoint::DataQueued); // Setup is done, now Data + } + break; + + case Endpoint::DataQueued: + if (endpoint->TDHead.CurrBufPtr) + endpoint->Length = endpoint->TDHead.CurrBufPtr - (u32)endpoint->Data; + + if (ep == 0) + { + LOG("ProcessDoneQueue ep == 0 endpoint->CurrentState=%d\r\n",endpoint->CurrentState); + Transfer(endpoint,in ? TOKEN_OUT : TOKEN_IN,0,0,Endpoint::StatusQueued); // Data is done, now Status, Control only + } + else + { + + LOG("ProcessDoneQueue ep != 0 endpoint->CurrentState=%d\r\n",endpoint->CurrentState); + endpoint->CurrentState = Endpoint::Idle; + LOG("CurrentState Change Idle endpoint(%d) currentState =%d\r\n",endpoint->Address(),endpoint->CurrentState); + } + break; + + case Endpoint::StatusQueued: // Transaction is done + LOG("CurrentState Change Idle endpoint(%d) currentState =%d\r\n",endpoint->Address(),endpoint->CurrentState); + endpoint->CurrentState = Endpoint::Idle; + break; + } + } + + // Complete, flag if we need a callback + if (endpoint->Callback && endpoint->CurrentState == Endpoint::Idle) + { + endpoint->CurrentState = Endpoint::CallbackPending; + _callbacksPending++; + } + } + LOG("<<ProcessDoneQueue out>>\r\n"); + } + + // Hack to reset devices that don't want to connect + int AddDevice(int hub, int port, bool isLowSpeed) + { + int device = AddDeviceCore(hub,port,isLowSpeed); + if (device < 0) + { + LOG("========RETRY ADD DEVICE========\r\n"); // This will go for ever.. TODO power cycle root? + Disconnect(hub,port); // Could not read descriptor at assigned address, reset this port and try again + ResetPort(hub,port); // Cheap bluetooth dongles often need this on a hotplug + return -1; + } + return device; + } + + int AddDeviceCore(int hub, int port, bool isLowSpeed) + { + int lowSpeed = isLowSpeed ? 0x2000 : 0; + DeviceDescriptor desc; + EndpointZero.EndpointDescriptor.Control = (8 << 16) | lowSpeed; // MaxPacketSize == 8 + int r = GetDescriptor(0,DESCRIPTOR_TYPE_DEVICE,0,(u8*)&desc,8); + if (r < 0) + { + LOG("FAILED TO LOAD DESCRIPTOR FOR DEVICE 0\r\n"); + return r; + } + + EndpointZero.EndpointDescriptor.Control = (desc.bMaxPacketSize << 16) | lowSpeed; // Actual MaxPacketSize + r = GetDescriptor(0,DESCRIPTOR_TYPE_DEVICE,0,(u8*)&desc,sizeof(desc)); + if (r < 0) + return r; + + LOG("\nClass %02X found %04X:%04X\r\n",desc.bDeviceClass,desc.idVendor,desc.idProduct); + + // Now assign the device an address, move off EndpointZero + int device = 0; + for (int i = 0; i < MAX_DEVICES; i++) + { + if (Devices[i].Port == 0) + { + device = i+1; + break; + } + } + if (!device) + return ERR_DEVICE_NONE_LEFT; + + r = SetAddress(0,device); + if (r) + return r; + DelayMS(2); + + // Now at a nonzero address, create control endpoint + Device* dev = &Devices[device-1]; + dev->Init(&desc,hub,port,device,lowSpeed); + AddEndpoint(device,0,ENDPOINT_CONTROL,desc.bMaxPacketSize,0); + _connectPending = 0; + + // Verify this all works + r = GetDescriptor(device,DESCRIPTOR_TYPE_DEVICE,0,(u8*)&desc,sizeof(desc)); + if (r < 0) + return r; + + // Set to interface 0 by default + // Calls LoadDevice if interface is found + r = SetConfigurationAndInterface(device,1,0,&desc); + + if (desc.bDeviceClass == CLASS_HUB) + InitHub(device); // Handle hubs in this code + + return device; + } + + // Walk descriptors and create endpoints for a given device + // TODO configuration !=1, alternate settings etc. + int SetConfigurationAndInterface(int device, int configuration, int interfaceNumber, DeviceDescriptor* desc) + { + u8 buffer[255]; + int err = GetDescriptor(device,DESCRIPTOR_TYPE_CONFIGURATION,0,buffer,sizeof(buffer)); + if (err < 0) + return err; + + err = SetConfiguration(device,configuration); + if (err < 0) + return err; + + // Add the endpoints for this interface + int len = buffer[2] | (buffer[3] << 8); + u8* d = buffer; + u8* end = d + len; + InterfaceDescriptor* found = 0; + while (d < end) + { + if (d[1] == DESCRIPTOR_TYPE_INTERFACE) + { + InterfaceDescriptor* id = (InterfaceDescriptor*)d; + if (id->bInterfaceNumber == interfaceNumber) + { + found = id; + d += d[0]; + while (d < end && d[1] != DESCRIPTOR_TYPE_INTERFACE) + { + switch (d[1]) + { + case DESCRIPTOR_TYPE_ENDPOINT: + AddEndpoint(device,(EndpointDescriptor*)d); + break; + default: + LOG("Skipping descriptor %02X (%d bytes)\r\n",d[1],d[0]); + } + d += d[0]; + } + } + } + d += d[0]; + } + + if (!found) + return ERR_INTERFACE_NOT_FOUND; + OnLoadDevice(device,desc,found); + return 0; + } + + void Init() + { + LOG("USB INIT (Controller is %d bytes)\r\n",sizeof(*this)); + memset(this,0,sizeof(HostController)); + EndpointZero.CurrentState = Endpoint::NotQueued; + HWInit(&CommunicationArea); + DelayMS(10); + } + + void ResetPort(int hub, int port) + { + LOG("ResetPort Hub:%d Port:%d\r\n",hub,port); + _connectPending++; // Only reset/add 1 device at a time + if (hub == 0) + LPC_USB->HcRhPortStatus1 = PortResetStatus; // Reset Root Hub, port 1 + else + SetPortReset(hub,port); // or reset other hub + } + + void Disconnect(int hub, int port) + { + LOG("Disconnect Hub:%d Port:%d\r\n",hub,port); // Mark a device for destruction + for (int i = 0; i < MAX_DEVICES; i++) + { + Device* dev = Devices + i; + if (dev->Port == port && dev->Hub == hub) + { + // Disconnect everything that is attached to this device if it is a hub + for (int p = 0; p < dev->HubPortCount; p++) + Disconnect(i+1,p+1); + + // Now release endpoints + for (int j = 1; j < MAX_ENDPOINTS_PER_DEVICE*2; j += 2) + { + u8 endpointIndex = dev->_endpointMap[j]; + if (endpointIndex != 0xFF) + Release(Endpoints + endpointIndex); + } + dev->Port = 0; // Device is now free + dev->Flags = 0; + return; + } + } + } + + // called after reset + void Connect(int hub, int port, bool lowspeed) + { + LOG("Connect Hub:%d Port:%d %s\r\n",hub,port,lowspeed ? "slow" : "full"); + AddDevice(hub,port,lowspeed); + } + + // Called from interrupt + void HubStatusChange(int hub, int port, u32 status) + { + LOG("HubStatusChange Hub:%d Port:%d %08X\r\n",hub,port,status); + if (status & ConnectStatusChange) + { + if (status & CurrentConnectStatus) // Connecting + ResetPort(hub,port); // Reset to initiate connect (state machine?) + else + Disconnect(hub,port); + } + + if (status & PortResetStatusChange) + { + if (!(status & PortResetStatus)) + { + _connectCountdown = 200; // Schedule a connection in 200ms + if (status & LowspeedDevice) + port |= 0x80; + _connectHub = hub; + _connectPort = port; + } + } + } + + #define HOST_CLK_EN (1<<0) + #define PORTSEL_CLK_EN (1<<3) + #define AHB_CLK_EN (1<<4) + #define CLOCK_MASK (HOST_CLK_EN | PORTSEL_CLK_EN | AHB_CLK_EN) + + #define FRAMEINTERVAL (12000-1) // 1ms + #define DEFAULT_FMINTERVAL ((((6 * (FRAMEINTERVAL - 210)) / 7) << 16) | FRAMEINTERVAL) + + void DelayMS(int ms) + { + u16 f = ms + CommunicationArea.FrameNumber; + while (f != CommunicationArea.FrameNumber) + ; + } + + static void HWInit(HCCA* cca) + { + NVIC_DisableIRQ(USB_IRQn); + + // turn on power for USB + LPC_SC->PCONP |= (1UL<<31); + // Enable USB host clock, port selection and AHB clock + LPC_USB->USBClkCtrl |= CLOCK_MASK; + // Wait for clocks to become available + while ((LPC_USB->USBClkSt & CLOCK_MASK) != CLOCK_MASK) + ; + + // We are a Host + LPC_USB->OTGStCtrl |= 1; + LPC_USB->USBClkCtrl &= ~PORTSEL_CLK_EN; // we don't need port selection clock until we do OTG + + // configure USB pins + LPC_PINCON->PINSEL1 &= ~((3<<26)|(3<<28)); + LPC_PINCON->PINSEL1 |= ((1<<26)|(1<<28)); // USB D+/D- + + LPC_PINCON->PINSEL3 &= ~((3 << 6) | (3 << 22)); // USB_PPWR, USB_OVRCR + LPC_PINCON->PINSEL3 |= ((2 << 6) | (2 << 22)); + + LPC_PINCON->PINSEL4 &= ~(3 << 18); // USB_CONNECT + LPC_PINCON->PINSEL4 |= (1 << 18); + + // Reset OHCI block + LPC_USB->HcControl = 0; + LPC_USB->HcControlHeadED = 0; + LPC_USB->HcBulkHeadED = 0; + + LPC_USB->HcCommandStatus = HostControllerReset; + LPC_USB->HcFmInterval = DEFAULT_FMINTERVAL; + LPC_USB->HcPeriodicStart = FRAMEINTERVAL*90/100; + + LPC_USB->HcControl = (LPC_USB->HcControl & (~HostControllerFunctionalState)) | OperationalMask; + LPC_USB->HcRhStatus = SetGlobalPower; + + LPC_USB->HcHCCA = (u32)cca; + LPC_USB->HcInterruptStatus |= LPC_USB->HcInterruptStatus; + LPC_USB->HcInterruptEnable = MasterInterruptEnable | WritebackDoneHead | RootHubStatusChange | FrameNumberOverflow; + + NVIC_SetPriority(USB_IRQn, 0); + NVIC_EnableIRQ(USB_IRQn); + while (cca->FrameNumber < 10) + ; // 10ms delay before diving in + } +}; + +//==================================================================================== +//==================================================================================== +// Host controller instance and Interrupt handler + +static HostController _controller __attribute__((at(USB_RAM_BASE))); + +extern "C" void USB_IRQHandler(void) __irq; +void USB_IRQHandler (void) __irq +{ +LOG("USB_IRQHandler start \r\n"); + u32 int_status = LPC_USB->HcInterruptStatus; + LOG("USB_IRQHandler int_status=%0x\r\n",int_status); + + if(int_status & UnrecoverableError) //Error + { + LOG("USB_IRQHandler UnrecoverableError Please reset\r\n"); + } + + if(int_status & MasterInterruptEnable) + { + LOG("USB_IRQHandler MasterInterruptEnable\r\n"); + } + + if(int_status &OwnershipChange) + { + LOG("USB_IRQHandler OwnershipChange\r\n"); + } + + if(int_status &FrameNumberOverflow) + { + LOG("USB_IRQHandler FrameNumberOverflow\r\n"); + } + if(int_status&ResumeDetected) + { + LOG("USB_IRQHandler ResumeDetected\r\n"); + } + if(int_status&StartofFrame) + { + LOG("USB_IRQHandler StartofFrame\r\n"); + } + + + if (int_status & RootHubStatusChange) // Root hub status change + { + LOG("USB_IRQHandler _rootHubStatusChange %0x->%0x \r\n", _controller._rootHubStatusChange, _controller._rootHubStatusChange+1); + _controller._rootHubStatusChange++; // Just flag the controller, will be processed in USBLoop + } + + u32 head = 0; + if (int_status & WritebackDoneHead) + { + head = _controller.CommunicationArea.DoneHead; // Writeback Done + _controller.CommunicationArea.DoneHead = 0; + LOG("USB_IRQHandler head=%0x\r\n",head); + } + LPC_USB->HcInterruptStatus = int_status; + + if (head) + _controller.ProcessDoneQueue(head); // TODO - low bit can be set BUGBUG +LOG("USB_IRQHandler end \r\n"); +} + +//==================================================================================== +//==================================================================================== +// API Methods + +void USBInit() +{ + return _controller.Init(); +} + +void USBLoop() +{ + return _controller.Loop(); +} + +u8* USBGetBuffer(u32* len) +{ + *len = USB_RAM_SIZE - sizeof(HostController); + return _controller.SRAM; +} + +static Setup* GetSetup(int device) +{ + if (device == 0) + return &_controller._setupZero; + + if (device < 1 || device > MAX_DEVICES) + return 0; + return &_controller.Devices[device-1].SetupBuffer; +} + +// Loop until IO on endpoint is complete +static int WaitIODone(Endpoint* endpoint) +{ + LOG("WaitIODone start\r\n"); + if (endpoint->CurrentState == Endpoint::NotQueued) + return 0; + while (endpoint->CurrentState != Endpoint::Idle) + { + LOG("WaitIODone goto USBloop endpoint->CurrentState =%d \r\n",endpoint->CurrentState); + USBLoop(); // May generate callbacks, mount or unmount devices etc + } + int status = endpoint->Status(); + + LOG("WaitIODone end\r\n"); + if (status == 0) + return endpoint->Length; + return -status; +} + +int USBTransfer(int device, int ep, u8 flags, u8* data, int length, USBCallback callback, void* userData) +{ + //LOG("USBTRANSFER start\r\n"); + Endpoint* endpoint = _controller.GetEndpoint(device,ep); + if (!endpoint) + { + LOG("USBTRANSFER error Endpoint not found!!\r\n"); + return ERR_ENDPOINT_NOT_FOUND; + } + + //LOG("USBTRANSFER run01\r\n"); + WaitIODone(endpoint); + endpoint->Flags = flags; + endpoint->Data = data; + endpoint->Length = length; + endpoint->Callback = callback; + endpoint->UserData = userData; + //LOG("USBTRANSFER run02\r\n"); + if (ep == 0) + { + LOG("USBTRANSFER ep == 0(Setup) ------------------------------\r\n"); + _controller.Transfer(endpoint,TOKEN_SETUP,(u8*)GetSetup(device),8,Endpoint::SetupQueued); + } + else + { + LOG("USBTRANSFER ep != 0\r\n"); + _controller.Transfer(endpoint,flags & 0x80 ? TOKEN_IN : TOKEN_OUT,data,length,Endpoint::DataQueued); + } + if (callback) + { + LOG("USBTRANSFER end io_pending\r\n"); + return IO_PENDING; + } + LOG("USBTRANSFER end wait io done \r\n"); + return WaitIODone(endpoint); +} + +int USBControlTransfer(int device, int request_type, int request, int value, int index, u8* data, int length, USBCallback callback, void * userData) +{ + Setup* setup = GetSetup(device); + if (!setup) + return ERR_DEVICE_NOT_FOUND; + + // Async control calls may overwrite setup buffer of previous call, so we need to wait before setting up next call + WaitIODone(_controller.GetEndpoint(device,0)); + + setup->bm_request_type = request_type; + setup->b_request = request; + setup->w_value = value; + setup->w_index = index; + setup->w_length = length; + LOG("USBControlTransfer(device=%d,reqtype=%d,req=%d,value=%d,index=%d,data=%p,len=%d,callbak=%p,udata=%p)\r\n", + device,request_type,request,value,index,data,length,callback,userData); + return USBTransfer(device,0,request_type & DEVICE_TO_HOST,data,length,callback,userData); +} + +int USBInterruptTransfer(int device, int ep, u8* data, int length, USBCallback callback, void* userData) +{ + return USBTransfer(device,ep,(ep & 0x80) | ENDPOINT_INTERRUPT,data,length,callback,userData); +} + +int USBBulkTransfer(int device, int ep, u8* data, int length, USBCallback callback, void* userData) +{ + return USBTransfer(device,ep,(ep & 0x80) | ENDPOINT_BULK,data,length,callback,userData); +} + +int GetDescriptor(int device, int descType,int descIndex, u8* data, int length) +{ + return USBControlTransfer(device,DEVICE_TO_HOST | RECIPIENT_DEVICE, GET_DESCRIPTOR,(descType << 8)|(descIndex), 0, data, length, 0); +} + +int GetString(int device, int index, char* dst, int length) +{ + u8 buffer[255]; + int le = GetDescriptor(device,DESCRIPTOR_TYPE_STRING,index,buffer,sizeof(buffer)); + if (le < 0) + return le; + if (length < 1) + return -1; + length <<= 1; + if (le > length) + le = length; + for (int j = 2; j < le; j += 2) + *dst++ = buffer[j]; + *dst = 0; + return (le>>1)-1; +} + +int SetAddress(int device, int new_addr) +{ + return USBControlTransfer(device,HOST_TO_DEVICE | RECIPIENT_DEVICE, SET_ADDRESS, new_addr, 0, 0, 0, 0); +} + +int SetConfiguration(int device, int configNum) +{ + return USBControlTransfer(device,HOST_TO_DEVICE | RECIPIENT_DEVICE, SET_CONFIGURATION, configNum, 0, 0, 0, 0); +} + +int SetInterface(int device, int ifNum, int altNum) +{ + return USBControlTransfer(device,HOST_TO_DEVICE | RECIPIENT_INTERFACE, SET_INTERFACE, altNum, ifNum, 0, 0, 0); +} + +// HUB stuff +int SetPortFeature(int device, int feature, int index) +{ + return USBControlTransfer(device,HOST_TO_DEVICE | REQUEST_TYPE_CLASS | RECIPIENT_OTHER,SET_FEATURE,feature,index,0,0); +} + +int ClearPortFeature(int device, int feature, int index) +{ + return USBControlTransfer(device,HOST_TO_DEVICE | REQUEST_TYPE_CLASS | RECIPIENT_OTHER,CLEAR_FEATURE,feature,index,0,0); +} + +int SetPortPower(int device, int port) +{ + int r = SetPortFeature(device,PORT_POWER,port); + _controller.DelayMS(20); // 80ms to turn on a hubs power... DESCRIPTOR? todo + return r; +} + +int SetPortReset(int device, int port) +{ + return SetPortFeature(device,PORT_RESET,port); +} + +int GetPortStatus(int device, int port, u32* status) +{ + return USBControlTransfer(device,DEVICE_TO_HOST | REQUEST_TYPE_CLASS | RECIPIENT_OTHER,GET_STATUS,0,port,(u8*)status,4); +} + +#endif \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/AndroidADKTerm/android.cpp Sun Apr 07 08:31:51 2013 +0000 @@ -0,0 +1,190 @@ +#include "android.h" + +using namespace std; + +AdkTerm::AdkTerm() : AndroidAccessory(INBL,OUTL, "ARM", "mbed", "mbed Terminal", "0.1", "http://www.mbed.org", "0000000012345678") +{ + +}; + + +void AdkTerm::setupDevice() +{ + settick = false; + for (int i = 0; i<OUTL; i++) { + buffer[i] = 0; + } + bcount = 0; + //n.attach(this,&AdkTerm::AttachTick,5); + //tick.attach(this,&AdkTerm::onTick,0.1); +} + +float AdkTerm::getx() +{ + return x/1000; +} + +float AdkTerm::gety() +{ + return y/1000; +} + +float AdkTerm::gett() +{ + return t * PI / 180; +} + +/*void AdkTerm::AttachTick() +{ + if(!settick)tick.attach(this,&AdkTerm::onTick,0.04); + settick = true; +} + +void AdkTerm::onTick() +{ + right = 1-Right; + left = 1-Left; + bool update = false; + int templ, tempr; + + + + templ = int(left * 10000); + tempr = int(right * 10000); + + + + if (abs(templ-tl)>170) { + update = true; + } + if (abs(tempr-tr)>170) { + update = true; + } + if (update) { + u8* wbuf = _writebuff; + + wbuf[0] = 'P'; + wbuf[1] = templ&0xFF; + wbuf[2] = (templ>>8) & 0xFF; + wbuf[3] = tempr&0xFF; + wbuf[4] = (tempr>>8) & 0xFF; + wbuf[5] = 0; + + this->write(wbuf,5); + } +}*/ + +void AdkTerm::resetDevice() +{ + for (int i = 0; i<OUTL; i++) { + buffer[i] = 0; + } + bcount = 0; +} + +void AdkTerm::Tokenize(const string& str, + vector<string>& tokens, + const string& delimiters /*= " "*/) +{ + // Skip delimiters at beginning. + string::size_type lastPos = str.find_first_not_of(delimiters, 0); + // Find first "non-delimiter". + string::size_type pos = str.find_first_of(delimiters, lastPos); + + while (string::npos != pos || string::npos != lastPos) { + // Found a token, add it to the vector. + tokens.push_back(str.substr(lastPos, pos - lastPos)); + // Skip delimiters. Note the "not_of" + lastPos = str.find_first_not_of(delimiters, pos); + // Find next "non-delimiter" + pos = str.find_first_of(delimiters, lastPos); + } +} + +int AdkTerm::callbackRead(u8 *buf, int len) +{ + // convert buffer (unsigned char) to string + std::string str(reinterpret_cast<char*>(buf), len); + + // new vector of strings + vector<string> tokens; + + // tokenize the string with the semicolon separator + Tokenize(str, tokens, ";"); + copy(tokens.begin(), tokens.end(), ostream_iterator<string>(cout, ", ")); + + + if(tokens.size() > 2) { + + //string to float + x = ::atof(tokens.at(0).c_str()); + y = ::atof(tokens.at(1).c_str()); + t = ::atof(tokens.at(2).c_str()); + + //pc.printf("Android x(%d): %f\n\r\n",len,x); + // pc.printf("Android y(%d): %f\n\r\n",len,y); + //pc.printf("Android t(%d): %f\n\r\n",len,t); + } else { + //pc.printf("Android sayys(%d): %s\n\r\n",len,str); + } + +// switch ( buf[0] ) { +// case 'x': +// if (buf[1] == ":"){pc.printf("X-Coordinate(%d): %s\n\r\n",len,buf)}; +// break; +// case 'y': +// if (buf[1] == ":"){pc.printf("Y-Coordinate(%d): %s\n\r\n",len,buf)}; +// break; +// case 't': +// if (buf[1] == ":"){pc.printf("Z-Coordinate(%d): %s\n\r\n",len,buf)}; +// break; +// default: +// pc.printf("Command not recognized (%d): %s\n\r\n\n\n",len,buf); +// } + + + //AttachTick(); + + return 0; +} + +// split: receives a char delimiter; returns a vector of strings +// By default ignores repeated delimiters, unless argument rep == 1. + +int AdkTerm::callbackWrite() +{ + ind = false; + return 0; +} + +void AdkTerm::serialIRQ() +{ + //buffer[bcount] = pc.getc(); + + + if (buffer[bcount] == '\n' || buffer[bcount] == '\r') { + u8* wbuf = _writebuff; + for (int i = 0; i<OUTL; i++) { + wbuf[i] = buffer[i]; + buffer[i] = 0; + } + //pc.printf("Sending: %s\n\r",wbuf); + ind = true; + this->write(wbuf,bcount); + bcount = 0; + } else { + if (buffer[bcount] != 0x08 && buffer[bcount] != 0x7F ) { + bcount++; + if (bcount == OUTL) { + bcount = 0; + } + } else { + bcount--; + } + } + +} + + +// gehört nicht mehr zur klasse für zum testen.......!!!!!!!! +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/AndroidADKTerm/android.h Sun Apr 07 08:31:51 2013 +0000 @@ -0,0 +1,154 @@ +#ifndef _ANDROID_H_ +#define _ANDROID_H_ + +#include "mbed.h" +#include "AndroidAccessory.h" +#include "defines.h" +#include <string> +#include <sstream> +#include <vector> +#include <iostream> +#include <stdlib.h> + +/** + * @author Arno Galliker + * + * @subsection LICENSE + * + * Copyright © 2013 HSLU Pren Team #1 Cruising Crêpe + * All rights reserved. + * + * @subsection DESCRIPTION + * + * This class implements communication with the android smartphone + * For more information see the Android ADK Cookbook: + * <a href="http://mbed.org/cookbook/mbed-with-Android-ADK">http://mbed.org/cookbook/mbed-with-Android-ADK</a> + * + * Originally created by p07gbar from work by Makoto Abe + */ + +#define OUTL 100 +#define INBL 100 + +class AdkTerm : public AndroidAccessory +{ + +private: + + /** Attaches a tick to send messages over the USB buffer in a certain interval */ + void AttachTick(); + + /** Method to call when a tick period is over */ + void onTick(); + + /** Char buffer with a size of OUTL */ + char buffer[OUTL]; + + /** Buffer count ??? */ + int bcount; + + /** Instance of ticker */ + Ticker tick; + + //float right,left,rl,ll; + //int tl,tr; + + //Timeout n; + + /** States if tick is on */ + bool settick; + + /** States if something is written to the buffer?? */ + bool ind; + + /** Desired position in meters for x-coordinate, given by android */ + float x; + + /** Desired position in meters for y-coordinate, given by android */ + float y; + + /** Desired position in degrees for theta, given by android */ + float t; + +public: + + /** + * Creates a <code>AdkTerm</code> object and initializes all private + * state variables. Constructor + */ + AdkTerm(); + + /** + * Sets initial configurations and clears the buffer + * + */ + void setupDevice(); + + /** + * Returns the desired position in meters for x-coordinate, given by android + * @return x-coordinate,given in [m] + */ + float getx(); + + /** + * Returns the desired position in meters for y-coordinate, given by android + * @return y-coordinate,given in [m] + */ + float gety(); + + /** + * Returns the esired position in degrees for theta, given by android + * @return y-coordinate,given in degrees [°] + */ + float gett(); + +private: + + /** + * + * + */ + //void AttachTick(); + + /** + * + * + */ + // void onTick(); + + /** + * Clears the buffer and bcount + * + */ + void resetDevice(); + + /** + * Takes an string, a vector of strings for the delimited tokens, and a with the + * @param str + * @param tokens + * @param delimiters + */ + void Tokenize(const string& str, vector<string>& tokens, const string& delimiters = " "); + + /** + * + * @param buf + * @param len + */ + int callbackRead(u8 *buf, int len); + + /** + * + * + */ + int callbackWrite(); + + /** + * + * + */ + void serialIRQ(); + +}; + +#endif
--- a/RobotControl/MotionState.h Fri Apr 05 10:58:42 2013 +0000 +++ b/RobotControl/MotionState.h Sun Apr 07 08:31:51 2013 +0000 @@ -53,7 +53,7 @@ /** * @brief Destructor of the Object to destroy the Object. - **/ + */ virtual ~MotionState(); /**
--- a/RobotControl/RobotControl.cpp Fri Apr 05 10:58:42 2013 +0000 +++ b/RobotControl/RobotControl.cpp Sun Apr 07 08:31:51 2013 +0000 @@ -31,6 +31,17 @@ } +float RobotControl::PiRange(float theta) +{ + if(theta <= -PI) { + return theta += 2*PI; + } else if (theta > PI) { + return theta -= 2*PI; + } else { + return theta; + } +} + void RobotControl::setEnable(bool enable) { motorControllerLeft->enable(enable); @@ -175,67 +186,61 @@ void RobotControl::run() { -// kann glaub wegggggg - if (isEnabled()) { - } else { + // When the Motor is note enable set the desired speed to the acutal speed. + // only used by setting the speed and omega via the WII control. + if (!isEnabled()) { speed = 0.0f; omega = 0.0f; Desired.setState(&Actual); } - ////bis hieerrrrr - /* position calculation */ - - /* Set the state of speed from Left und Right Wheel*/ + // position calculation + // Set the state of speed from Left und Right Wheel stateLeft.speed = motorControllerLeft->getActualSpeed() * 2.0f * WHEEL_RADIUS_LEFT * PI * GEAR; stateRight.speed = - motorControllerRight->getActualSpeed() * 2.0f * WHEEL_RADIUS_RIGHT * PI * GEAR ; - /* translational speed of the Robot (average) */ + //translational speed of the Robot (average) Actual.speed = (stateRight.speed + stateLeft.speed) / 2.0f; - /* rotational speed of the Robot */ + //rotational speed of the Robot Actual.omega = (stateRight.speed - stateLeft.speed) / WHEEL_DISTANCE; - /* rotational theta of the Robot integrate the omega with the time*/ + //rotational theta of the Robot integrate the omega with the time Actual.theta += Actual.omega * period; Actual.theta = PiRange(Actual.theta); - /* translational X and Y Position. integrate the speed with the time */ + //translational X and Y Position. integrate the speed with the time Actual.xposition += (Actual.speed * period * cos(Actual.theta)); Actual.yposition += (Actual.speed * period * sin(Actual.theta)); - /* motor control */ + //motor control if ( isEnabled() && ( getDistanceError() >= MIN_DISTANCE_ERROR ) ) { - /* postition control */ - + //postition control speed = K1 * getDistanceError() * cos( getThetaErrorToGoal() ); omega = K2 * getThetaErrorToGoal() + - K1 * ( ( sin(getThetaErrorToGoal()) * cos(getThetaErrorToGoal()) ) / - (getThetaErrorToGoal()) ) * ( getThetaErrorToGoal() + K3 * getThetaGoal() ); + K1 * + ( + ( + sin(getThetaErrorToGoal() ) * cos(getThetaErrorToGoal() ) + ) / getThetaErrorToGoal() + ) * + ( getThetaErrorToGoal() + + K3 * getThetaGoal() ); - motorControllerLeft->setVelocity( ( ( (2 * speed) - (WHEEL_DISTANCE * omega) ) / 2 ) / - (2 * WHEEL_RADIUS_LEFT * PI * GEAR) ); - motorControllerRight->setVelocity(-( ( (2 * speed) + (WHEEL_DISTANCE * omega) ) / 2) / - (2 * WHEEL_RADIUS_RIGHT * PI * GEAR) ); + motorControllerLeft->setVelocity( + ( ( (2 * speed) - (WHEEL_DISTANCE * omega) ) / 2 ) / + (2 * WHEEL_RADIUS_LEFT * PI * GEAR) + ); + motorControllerRight->setVelocity( + -( ( (2 * speed) + (WHEEL_DISTANCE * omega) ) / 2) / + (2 * WHEEL_RADIUS_RIGHT * PI * GEAR) + ); } else { - motorControllerLeft->setVelocity(0.0f); motorControllerRight->setVelocity(0.0f); - } } - -float RobotControl::PiRange(float theta) -{ - if(theta <= -PI) { - return theta += 2*PI; - } else if (theta > PI) { - return theta -= 2*PI; - } else { - return theta; - } -}
--- a/RobotControl/RobotControl.h Fri Apr 05 10:58:42 2013 +0000 +++ b/RobotControl/RobotControl.h Sun Apr 07 08:31:51 2013 +0000 @@ -21,6 +21,7 @@ * and writes these velocity values to the motor servo drives. * This class offers methods to enable or disable the controller, and to set * the desired x- and y-postion and the θ values of the robot. + * */ class RobotControl : public Task @@ -83,21 +84,6 @@ bool isEnabled(); /** - * @brief Sets the maximum acceleration value. - * @param acceleration the maximum acceleration value to use - * for the calculation of the motion trajectory, given in [m/s<SUP>2</SUP>] - */ - void setAcceleration(float acceleration); - - /** - * @brief Sets the maximum acceleration value of rotation. - * @param acceleration the maximum acceleration value - * to use for the calculation of the motion trajectory, - * given in [rad/s<SUP>2</SUP>] - */ - void setThetaAcceleration(float acceleration); - - /** * @brief Sets the desired translational speed of the robot. * @param speed the desired speed, given in [m/s] */
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/State/State.cpp Sun Apr 07 08:31:51 2013 +0000 @@ -0,0 +1,175 @@ +#include "State.h" + +using namespace std; + +// File +FILE *logp; + +// LocalFileSystem +LocalFileSystem local("local"); + +State::State(state_t* s, + RobotControl* robotControl, + MaxonESCON* motorControllerLeft, + MaxonESCON* motorControllerRight, + PinName batteryPin, + float period) + : Task(period) , + battery(batteryPin) +{ + /* get peripherals */ + this->s = s; + this->robotControl = robotControl; + this->motorControllerLeft = motorControllerLeft; + this->motorControllerRight = motorControllerRight; + this->battery =battery; + this->period = period; +} + +State::~State() {} + +void State::initPlotFile(void) +{ + logp = fopen("/local/plots.txt", "w"); // only write + if(logp == NULL) { + exit(1); + } else { + fprintf(logp, + "Time[ms]\t" + "BatteryVoltage[V]\t" + "NumberOfPulsesLeft\t" + "NumberOfPulsesRight\t" + "VelocityLeft[m/s]\t" + "VelocityRight[m/s]\t" + "VelocityCar[m/s]\t" //7 + "VelocityRotation[grad/s]\t" + "X-AxisCo-ordinate[m]\t" + "Y-AxisCo-ordinate[m]\t" + "X-AxisError[m]\t" + "X-AxisError[m]\t" + "AngleError[grad]\t" + "AngleCar[grad]\t" + "SetpointX-Axis[m]\t" //15 + "SetpointY-Axis[m]\t" + "SetpointAngel[grad]\t" //17 + "SetpointVelocitiy[m/s]\t" + "SetpointVelocitiyRotations[rad/s]\t" + "State\t" //20 + "distanceToGoal[m]\t" //21 + "angleToGoal[grad]\t" + "thetaFromTheGoal[grad]\n"); + } +} + + +void State::savePlotFile(state_t s) +{ + char buf[256]; + + sprintf(buf,"%d\t%f\t%d\t%d\t%f\t%f\t%f\t%f\t%f\t%f\t%f\t" + "%f\t%f\t%f\t%f\t%f\t%f\t%f\t%f\t%d\t%f\t%f\t%f", + s.millis, + s.voltageBattery, + s.leftPulses, + s.rightPulses, + s.leftVelocity, + s.rightVelocity, + s.velocity, + s.omega, + s.xAxis, + s.yAxis, + s.xAxisError, + s.yAxisError, + s.angleError, + s.angle, + s.setxAxis, + s.setyAxis, + s.setAngle, + s.setVelocity, + s.setOmega, + s.state, + s.rho, + s.lamda, + s.delta + ); + + if (logp) + fprintf(logp, buf); + fprintf(logp, "\n"); // new line +} + +void State::closePlotFile(void) +{ + fclose(logp); +} + +float State::readBattery() +{ + return battery.read()*BAT_MULTIPLICATOR; +} + +void State::setBatteryBit() +{ + if(s->voltageBattery < BAT_MIN) { + s->state |= STATE_UNDER; + } else { + s->state &= (~STATE_UNDER); + } +} + +void State::setEnableLeftBit() +{ + if(motorControllerLeft->isEnabled()) { + s->state &= (~STATE_LEFT); + } else { + s->state |= STATE_LEFT; + } +} + +void State::setEnableRightBit() +{ + if(motorControllerRight->isEnabled()) { + s->state &= (~STATE_RIGHT); + } else { + s->state |= STATE_RIGHT; + } +} + +void State::startTimerFromZero() +{ + timer.reset(); + timer.start(); +} + +void State::run() +{ + s->millis = timer.read_ms(); + s->voltageBattery = readBattery(); + s->leftPulses = - motorControllerLeft->getPulses(); + s->rightPulses = motorControllerRight->getPulses(); + s->leftVelocity = motorControllerLeft->getActualSpeed() * + 2.0f * WHEEL_RADIUS_LEFT * PI * GEAR; + s->rightVelocity = - motorControllerRight->getActualSpeed()* + 2.0f * WHEEL_RADIUS_RIGHT * PI * GEAR; + s->velocity = robotControl->getActualSpeed(); + s->omega = robotControl->getActualOmega(); + s->xAxis = robotControl->getxActualPosition(); + s->yAxis = robotControl->getyActualPosition(); + s->angle = robotControl->getActualTheta() * 180 / PI; + s->xAxisError = robotControl->getxPositionError(); + s->yAxisError = robotControl->getyPositionError(); + s->angleError = robotControl->getThetaError() * 180 / PI; + s->setxAxis = robotControl->getDesiredxPosition(); + s->setyAxis = robotControl->getDesiredyPosition(); + s->setAngle = robotControl->getDesiredTheta() * 180 / PI; + s->setVelocity = robotControl->getDesiredSpeed(); + s->setOmega = robotControl->getDesiredOmega(); + + s->rho = robotControl->getDistanceError(); + s->lamda = robotControl->getThetaErrorToGoal() * 180 / PI; + s->delta = robotControl->getThetaGoal() * 180 / PI; + + setBatteryBit(); + setEnableLeftBit(); + setEnableRightBit(); +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/State/State.h Sun Apr 07 08:31:51 2013 +0000 @@ -0,0 +1,100 @@ +#ifndef _STATE_H_ +#define _STATE_H_ + +#include "MaxonESCON.h" +#include "RobotControl.h" +#include "Task.h" +#include "defines.h" + +/** + * @author Christian Burri + * + * @copyright Copyright © 2013 HSLU Pren Team #1 Cruising Crêpe + * All rights reserved. + * + * @brief + * + * State is the main mechanism for communicating current realtime system state to + * the rest of the system for logging etc. + * + */ + +class State : public Task +{ + +private: + + state_t* s; + RobotControl* robotControl; + MaxonESCON* motorControllerLeft; + MaxonESCON* motorControllerRight; + AnalogIn battery; + Timer timer; + float period; + +public: + + /** + * @brief Creates a robot control object and initializes all private state variables. + * @param s struct to read and write the state + * @param robotControl Object to read the state + * @param motorControllerLeft a reference to the servo drive for the left motor + * @param motorControllerRight a reference to the servo drive for the right motor + * @param batteryPin mbed pin for analog Input Battery Voltage + * @param period the sampling period of the run loop of this controller, given in [s] + */ + State(state_t* s, + RobotControl* robotControl, + MaxonESCON* motorControllerLeft, + MaxonESCON* motorControllerRight, + PinName batteryPin, + float period); + + /** + * @brief Destructor of the Object to destroy the Object. + */ + virtual ~State(); + + /** + * @brief Initzialize the File. Open the File plots.txt and set the title at first line. + */ + void initPlotFile(void); + + /** + * @brief Close the File. + */ + void closePlotFile(void); + + /** + * @brief Return the Battery voltage + * state variables. + * @return Batterie Voltage [V] + */ + float readBattery(); + + /** + * @brief Starts the timer from zero. + * The timer is for the logging Time. + */ + void startTimerFromZero(); + + /** + * @brief Save the new state to a new line. + */ + void savePlotFile(state_t s); + + /** + * @brief Run method actualize every period. + */ + void run(); + +private: + + void setBatteryBit(); + + void setEnableLeftBit(); + + void setEnableRightBit(); +}; + +#endif
--- a/StateDefines/State.cpp Fri Apr 05 10:58:42 2013 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,175 +0,0 @@ -#include "State.h" - -using namespace std; - -// File -FILE *logp; - -// LocalFileSystem -LocalFileSystem local("local"); - -State::State(state_t* s, - RobotControl* robotControl, - MaxonESCON* motorControllerLeft, - MaxonESCON* motorControllerRight, - PinName batteryPin, - float period) - : Task(period) , - battery(batteryPin) -{ - /* get peripherals */ - this->s = s; - this->robotControl = robotControl; - this->motorControllerLeft = motorControllerLeft; - this->motorControllerRight = motorControllerRight; - this->battery =battery; - this->period = period; -} - -State::~State() {} - -void State::initPlotFile(void) -{ - logp = fopen("/local/plots.txt", "w"); // only write - if(logp == NULL) { - exit(1); - } else { - fprintf(logp, - "Time[ms]\t" - "BatteryVoltage[V]\t" - "NumberOfPulsesLeft\t" - "NumberOfPulsesRight\t" - "VelocityLeft[m/s]\t" - "VelocityRight[m/s]\t" - "VelocityCar[m/s]\t" //7 - "VelocityRotation[grad/s]\t" - "X-AxisCo-ordinate[m]\t" - "Y-AxisCo-ordinate[m]\t" - "X-AxisError[m]\t" - "X-AxisError[m]\t" - "AngleError[grad]\t" - "AngleCar[grad]\t" - "SetpointX-Axis[m]\t" //15 - "SetpointY-Axis[m]\t" - "SetpointAngel[grad]\t" //17 - "SetpointVelocitiy[m/s]\t" - "SetpointVelocitiyRotations[rad/s]\t" - "State\t" //20 - "distanceToGoal[m]\t" //21 - "angleToGoal[grad]\t" - "thetaFromTheGoal[grad]\n"); - } -} - - -void State::savePlotFile(state_t s) -{ - char buf[256]; - - sprintf(buf,"%d\t%f\t%d\t%d\t%f\t%f\t%f\t%f\t%f\t%f\t%f\t" - "%f\t%f\t%f\t%f\t%f\t%f\t%f\t%f\t%d\t%f\t%f\t%f", - s.millis, - s.voltageBattery, - s.leftPulses, - s.rightPulses, - s.leftVelocity, - s.rightVelocity, - s.velocity, - s.omega, - s.xAxis, - s.yAxis, - s.xAxisError, - s.yAxisError, - s.angleError, - s.angle, - s.setxAxis, - s.setyAxis, - s.setAngle, - s.setVelocity, - s.setOmega, - s.state, - s.rho, - s.lamda, - s.delta - ); - - if (logp) - fprintf(logp, buf); - fprintf(logp, "\n"); // new line -} - -void State::closePlotFile(void) -{ - fclose(logp); -} - -float State::readBattery() -{ - return battery.read()*BAT_MULTIPLICATOR; -} - -void State::setBatteryBit() -{ - if(s->voltageBattery < BAT_MIN) { - s->state |= STATE_UNDER; - } else { - s->state &= (~STATE_UNDER); - } -} - -void State::setEnableLeftBit() -{ - if(motorControllerLeft->isEnabled()) { - s->state &= (~STATE_LEFT); - } else { - s->state |= STATE_LEFT; - } -} - -void State::setEnableRightBit() -{ - if(motorControllerRight->isEnabled()) { - s->state &= (~STATE_RIGHT); - } else { - s->state |= STATE_RIGHT; - } -} - -void State::startTimerFromZero() -{ - timer.reset(); - timer.start(); -} - -void State::run() -{ - s->millis = timer.read_ms(); - s->voltageBattery = readBattery(); - s->leftPulses = - motorControllerLeft->getPulses(); - s->rightPulses = motorControllerRight->getPulses(); - s->leftVelocity = motorControllerLeft->getActualSpeed() * - 2.0f * WHEEL_RADIUS_LEFT * PI * GEAR; - s->rightVelocity = - motorControllerRight->getActualSpeed()* - 2.0f * WHEEL_RADIUS_RIGHT * PI * GEAR; - s->velocity = robotControl->getActualSpeed(); - s->omega = robotControl->getActualOmega(); - s->xAxis = robotControl->getxActualPosition(); - s->yAxis = robotControl->getyActualPosition(); - s->angle = robotControl->getActualTheta() * 180 / PI; - s->xAxisError = robotControl->getxPositionError(); - s->yAxisError = robotControl->getyPositionError(); - s->angleError = robotControl->getThetaError() * 180 / PI; - s->setxAxis = robotControl->getDesiredxPosition(); - s->setyAxis = robotControl->getDesiredyPosition(); - s->setAngle = robotControl->getDesiredTheta() * 180 / PI; - s->setVelocity = robotControl->getDesiredSpeed(); - s->setOmega = robotControl->getDesiredOmega(); - - s->rho = robotControl->getDistanceError(); - s->lamda = robotControl->getThetaErrorToGoal() * 180 / PI; - s->delta = robotControl->getThetaGoal() * 180 / PI; - - setBatteryBit(); - setEnableLeftBit(); - setEnableRightBit(); -}
--- a/StateDefines/State.h Fri Apr 05 10:58:42 2013 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,100 +0,0 @@ -#ifndef _STATE_H_ -#define _STATE_H_ - -#include "MaxonESCON.h" -#include "RobotControl.h" -#include "Task.h" -#include "defines.h" - -/** - * @author Christian Burri - * - * @copyright Copyright © 2013 HSLU Pren Team #1 Cruising Crêpe - * All rights reserved. - * - * @brief - * - * State is the main mechanism for communicating current realtime system state to - * the rest of the system for logging etc. - * - */ - -class State : public Task -{ - -private: - - state_t* s; - RobotControl* robotControl; - MaxonESCON* motorControllerLeft; - MaxonESCON* motorControllerRight; - AnalogIn battery; - Timer timer; - float period; - -public: - - /** - * @brief Creates a robot control object and initializes all private state variables. - * @param s struct to read and write the state - * @param robotControl Object to read the state - * @param motorControllerLeft a reference to the servo drive for the left motor - * @param motorControllerRight a reference to the servo drive for the right motor - * @param batteryPin mbed pin for analog Input Battery Voltage - * @param period the sampling period of the run loop of this controller, given in [s] - */ - State(state_t* s, - RobotControl* robotControl, - MaxonESCON* motorControllerLeft, - MaxonESCON* motorControllerRight, - PinName batteryPin, - float period); - - /** - * @brief Destructor of the Object to destroy the Object. - **/ - virtual ~State(); - - /** - * @brief Initzialize the File. Open the File plots.txt and set the title at first line. - */ - void initPlotFile(void); - - /** - * @brief Close the File. - */ - void closePlotFile(void); - - /** - * @brief Return the Battery voltage - * state variables. - * @return Batterie Voltage [V] - */ - float readBattery(); - - /** - * @brief Starts the timer from zero. - * The timer is for the logging Time. - */ - void startTimerFromZero(); - - /** - * @brief Save the new state to a new line. - */ - void savePlotFile(state_t s); - - /** - * @brief Run method actualize every period. - */ - void run(); - -private: - - void setBatteryBit(); - - void setEnableLeftBit(); - - void setEnableRightBit(); -}; - -#endif
--- a/StateDefines/defines.h Fri Apr 05 10:58:42 2013 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,266 +0,0 @@ -#ifndef _DEFINES_H_ -#define _DEFINES_H_ - -/*! \file defines.h -\brief All defines for the roboter you can see here. -*/ - -#include "mbed.h" - -/** - * @name Physical dimensions π; - */ -#define PI 3.141592654f -/*! @} */ - -/** - * @name maxon motor #339282 EC 45 flat 30W - * @{ - */ - -/** -* @brief Number of of pole pairs -*/ -#define POLE_PAIRS 8u - -/** -* @brief Gear on the motor 1/11.6f -*/ -#define GEAR 1/11.6f - -/** -* @brief Pulses per electrical step form the Hallsensor, have 6 steps -*/ -#define PULSES_PER_STEP 6u -/*! @} */ - -/** - * @name Physical Dimension of the car - * @{ - */ - -/** - * @brief Value for the diffrerenz between left an right, given in [m] - */ -#define WHEEL_RADIUS_DIFF 0.0000f - -/** - * @brief Radius of the left wheel, given in [m] - */ -#define WHEEL_RADIUS_LEFT 0.040280f - -/** - * @brief Radius of the left wheel, given in [m] - */ -#define WHEEL_RADIUS_RIGHT (WHEEL_RADIUS_LEFT - WHEEL_RADIUS_DIFF) - -/** - * @brief Distance of the wheel, given in [m] Greater --> turn more - */ -#define WHEEL_DISTANCE 0.2000f - -/** - * @brief Sets the start X-point, given in [m] - */ -#define START_X_OFFSET -0.8f - -/** - * @brief Sets the start Y-point, given in [m] - */ -#define START_Y_OFFSET 0.8f -/*! @} */ - -/** - * @name State Bits of the car - * @{ - */ - -/** - * @brief Bit0 = stop pressed - */ -#define STATE_STOP 1u - -/** - * @brief Bit1 = Undervoltage battery - */ -#define STATE_UNDER 2u - -/** - * @brief Bit2 = left ESCON in error state - */ -#define STATE_LEFT 4u - -/** - * @brief Bit3 = right ESCON in error state - */ -#define STATE_RIGHT 8u -/*! @} */ - -/** - * @name ESCON Constands - * @{ - */ - -/** - * @brief Speed Factor how set in the ESCON Studio - */ -#define ESCON_SET_FACTOR 1500.0f - -/** - * @brief Speed Factor how get in the ESCON Studio - */ -#define ESCON_GET_FACTOR 1600.4f - -/** - * @brief Error patch of the drift of Analog input and pwn output for set speed - */ -#define SET_SPEED_PATCH (1+0.00262f) - -/** - * @brief Error patch of the drift of Analog input and pwn output for get speed - */ -#define GET_SPEED_PATCH (1+0.0019f) -/*! @} */ - -/** - * @name position controller - * @{ - */ - -/** - * @brief Main Gain for k1, k2 and k3 - */ -#define GAIN 0.8f - -/** - * @brief Gain k1 default 1.0f - */ -#define K1 0.8f * GAIN - -/** - * @brief Gain k2 default 3.0f - */ -#define K2 3.0f * GAIN - -/** - * @brief Gain k3 default 2.0f - */ -#define K3 2.0f * GAIN - -/** - * @brief Min. Distance to switch the position controller off. - * Because when Distance Error goes to zero the ATAN2 is not define, given in [m] - */ -#define MIN_DISTANCE_ERROR 0.01f -/*! @} */ - -/** - * @name Batterie control Battery Type: 1SP1P LG-18650 - * * nominal voltage 3.6V - * * 5 batterys ==> 5 * 3.5V = 17.5V - * @{ - */ - -/** - * @brief Battery Multiplicator for the potential divider. - * - * R2 / (R1 + R2) = 0.153 R2= 10k , R1 = 1.8k 1/0.153 = 6.555 --> 3.3 * 6.555 = 21.6333333f - */ -#define BAT_MULTIPLICATOR 21.633333333f - -/** - * @brief minium operate voltage, given in [V] - */ -#define BAT_MIN 17.5f -/*! @} */ - -/** - * @name sampling rate for a Task Object - * @{ - */ - -/** - * @brief 1kHz Rate for Robot Control, given in [s] - */ -#define PERIOD_ROBOTCONTROL 0.001f - -/** - * @brief 1kHz Rate for State Objekt , given in [s] - */ -#define PERIOD_STATE 0.001f - -/** - * @brief 10Hz Rate for the Android communication , given in [s] - */ -#define PERIOD_ANDROID 0.1f -/*! @} */ - -/** - * @name Android Buffer Size for communication - * @{ - */ - -/** - * @brief Buffer Output - */ -#define OUTL 100 - -/** - * @brief Buffer Input - */ -#define INBL 100 -/*! @} */ - -/** - * @brief struct state - * structure containing system sensor data - */ -typedef struct state { - /** @brief millis Time [ms]*/ - int millis; - /** @brief Battery voltage [V] */ - float voltageBattery; - /** @brief Number of pulses left */ - int leftPulses; - /** @brief Number of pulses right */ - int rightPulses; - /** @brief Velocity left [m/s] */ - float leftVelocity; - /** @brief Velocity right [m/s] */ - float rightVelocity; - /** @brief Velocity of the car [m/s] */ - float velocity; - /** @brief Velocity rotation [°/s] */ - float omega; - /** @brief X-Axis from co-ordinate [m] */ - float xAxis; - /** @brief Y-Axis from co-ordinate [m] */ - float yAxis; - /** @brief X-Axis Error [m] */ - float xAxisError; - /** @brief X-Axis Error [m] */ - float yAxisError; - /** @brief Angle Error [°] */ - float angleError; - /** @brief Angle from Car [°] */ - float angle; - /** @brief Setpoint X-Axis [m] */ - float setxAxis; - /** @brief Setpoint Y-Axis [m] */ - float setyAxis; - /** @brief Setpoint Angel [°] */ - float setAngle; - /** @brief Setpoint velocitiy [m/s] */ - float setVelocity; - /** @brief Setpoint rotation velocitiy [rad/s] */ - float setOmega; - int state; - /** @brief distance to Goal */ - float rho; - /** @brief theta to goal */ - float lamda; - /** @brief theta from the goal */ - float delta; -} state_t; - -#endif
--- a/Task/Task.h Fri Apr 05 10:58:42 2013 +0000 +++ b/Task/Task.h Sun Apr 07 08:31:51 2013 +0000 @@ -54,24 +54,24 @@ virtual ~Task(); /** - * Gets the period of this task. - * @return the period in seconds. - */ + * Gets the period of this task. + * @return the period in seconds. + */ float getPeriod(); /** - * Starts this task. - */ + * Starts this task. + */ void start(); /** - * Stops this task. - */ + * Stops this task. + */ void stop(); /** - * This method needs to be implemented by a user task. - */ + * This method needs to be implemented by a user task. + */ virtual void run(); };
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/defines.h Sun Apr 07 08:31:51 2013 +0000 @@ -0,0 +1,266 @@ +#ifndef _DEFINES_H_ +#define _DEFINES_H_ + +/*! \file defines.h +\brief All defines for the roboter you can see here. +*/ + +#include "mbed.h" + +/** + * @name Physical dimensions π; + */ +#define PI 3.141592654f +/*! @} */ + +/** + * @name maxon motor #339282 EC 45 flat 30W + * @{ + */ + +/** +* @brief Number of of pole pairs +*/ +#define POLE_PAIRS 8u + +/** +* @brief Gear on the motor 1/11.6f +*/ +#define GEAR 1/11.6f + +/** +* @brief Pulses per electrical step form the Hallsensor, have 6 steps +*/ +#define PULSES_PER_STEP 6u +/*! @} */ + +/** + * @name Physical Dimension of the car + * @{ + */ + +/** + * @brief Value for the diffrerenz between left an right, given in [m] + */ +#define WHEEL_RADIUS_DIFF 0.0000f + +/** + * @brief Radius of the left wheel, given in [m] + */ +#define WHEEL_RADIUS_LEFT 0.040280f + +/** + * @brief Radius of the left wheel, given in [m] + */ +#define WHEEL_RADIUS_RIGHT (WHEEL_RADIUS_LEFT - WHEEL_RADIUS_DIFF) + +/** + * @brief Distance of the wheel, given in [m] Greater --> turn more + */ +#define WHEEL_DISTANCE 0.2000f + +/** + * @brief Sets the start X-point, given in [m] + */ +#define START_X_OFFSET -0.8f + +/** + * @brief Sets the start Y-point, given in [m] + */ +#define START_Y_OFFSET 0.8f +/*! @} */ + +/** + * @name State Bits of the car + * @{ + */ + +/** + * @brief Bit0 = stop pressed + */ +#define STATE_STOP 1u + +/** + * @brief Bit1 = Undervoltage battery + */ +#define STATE_UNDER 2u + +/** + * @brief Bit2 = left ESCON in error state + */ +#define STATE_LEFT 4u + +/** + * @brief Bit3 = right ESCON in error state + */ +#define STATE_RIGHT 8u +/*! @} */ + +/** + * @name ESCON Constands + * @{ + */ + +/** + * @brief Speed Factor how set in the ESCON Studio + */ +#define ESCON_SET_FACTOR 1500.0f + +/** + * @brief Speed Factor how get in the ESCON Studio + */ +#define ESCON_GET_FACTOR 1600.4f + +/** + * @brief Error patch of the drift of Analog input and pwn output for set speed + */ +#define SET_SPEED_PATCH (1+0.00262f) + +/** + * @brief Error patch of the drift of Analog input and pwn output for get speed + */ +#define GET_SPEED_PATCH (1+0.0019f) +/*! @} */ + +/** + * @name position controller + * @{ + */ + +/** + * @brief Main Gain for k1, k2 and k3 + */ +#define GAIN 0.8f + +/** + * @brief Gain k1 default 1.0f + */ +#define K1 0.8f * GAIN + +/** + * @brief Gain k2 default 3.0f + */ +#define K2 3.0f * GAIN + +/** + * @brief Gain k3 default 2.0f + */ +#define K3 2.0f * GAIN + +/** + * @brief Min. Distance to switch the position controller off. + * Because when Distance Error goes to zero the ATAN2 is not define, given in [m] + */ +#define MIN_DISTANCE_ERROR 0.01f +/*! @} */ + +/** + * @name Batterie control Battery Type: 1SP1P LG-18650 + * * nominal voltage 3.6V + * * 5 batterys ==> 5 * 3.5V = 17.5V + * @{ + */ + +/** + * @brief Battery Multiplicator for the potential divider. + * + * R2 / (R1 + R2) = 0.153 R2= 10k , R1 = 1.8k 1/0.153 = 6.555 --> 3.3 * 6.555 = 21.6333333f + */ +#define BAT_MULTIPLICATOR 21.633333333f + +/** + * @brief minium operate voltage, given in [V] + */ +#define BAT_MIN 17.5f +/*! @} */ + +/** + * @name sampling rate for a Task Object + * @{ + */ + +/** + * @brief 1kHz Rate for Robot Control, given in [s] + */ +#define PERIOD_ROBOTCONTROL 0.001f + +/** + * @brief 1kHz Rate for State Objekt , given in [s] + */ +#define PERIOD_STATE 0.001f + +/** + * @brief 10Hz Rate for the Android communication , given in [s] + */ +#define PERIOD_ANDROID 0.1f +/*! @} */ + +/** + * @name Android Buffer Size for communication + * @{ + */ + +/** + * @brief Buffer Output + */ +#define OUTL 100 + +/** + * @brief Buffer Input + */ +#define INBL 100 +/*! @} */ + +/** + * @brief struct state + * structure containing system sensor data + */ +typedef struct state { + /** @brief millis Time [ms]*/ + int millis; + /** @brief Battery voltage [V] */ + float voltageBattery; + /** @brief Number of pulses left */ + int leftPulses; + /** @brief Number of pulses right */ + int rightPulses; + /** @brief Velocity left [m/s] */ + float leftVelocity; + /** @brief Velocity right [m/s] */ + float rightVelocity; + /** @brief Velocity of the car [m/s] */ + float velocity; + /** @brief Velocity rotation [°/s] */ + float omega; + /** @brief X-Axis from co-ordinate [m] */ + float xAxis; + /** @brief Y-Axis from co-ordinate [m] */ + float yAxis; + /** @brief X-Axis Error [m] */ + float xAxisError; + /** @brief X-Axis Error [m] */ + float yAxisError; + /** @brief Angle Error [°] */ + float angleError; + /** @brief Angle from Car [°] */ + float angle; + /** @brief Setpoint X-Axis [m] */ + float setxAxis; + /** @brief Setpoint Y-Axis [m] */ + float setyAxis; + /** @brief Setpoint Angel [°] */ + float setAngle; + /** @brief Setpoint velocitiy [m/s] */ + float setVelocity; + /** @brief Setpoint rotation velocitiy [rad/s] */ + float setOmega; + int state; + /** @brief distance to Goal */ + float rho; + /** @brief theta to goal */ + float lamda; + /** @brief theta from the goal */ + float delta; +} state_t; + +#endif
--- a/main.cpp Fri Apr 05 10:58:42 2013 +0000 +++ b/main.cpp Sun Apr 07 08:31:51 2013 +0000 @@ -33,18 +33,28 @@ #include "RobotControl.h" //#include "android.h" -//Android +/** + * @name Communication + * @{ + */ + +/** + * @brief <code>adkTerm</code> object is for the communication with a smartphone. + * The operation system must be a android. + */ //AdkTerm adkTerm; +/*! @} */ /** * @name Hallsensor * @{ */ - - /** - * @brief <code>hallsensorLeft</code> object with pin15, pin17 and pin16 - */ + +/** +* @brief <code>hallsensorLeft</code> object with pin15, pin17 and pin16 +*/ Hallsensor hallLeft(p18, p17, p16); + /** * @brief <code>hallsensorLeft</code> object with pin17, pin28 and pin29 */ @@ -53,20 +63,24 @@ /** * @name Motors and Robot Control - * @{ + * @{ */ - - /** - * @brief <code>leftMotor</code> object with pin26, pin25, pin24, pin19 and <code>hallsensorLeft</code> object - */ + +/** +* @brief <code>leftMotor</code> object with pin26, pin25, pin24, +* pin19 and <code>hallsensorLeft</code> object +*/ MaxonESCON leftMotor(p26, p25, p24, p19, &hallLeft); + /** - * @brief <code>rightMotor</code> object with pin23, pin22, pin21, pin20 and <code>hallsensorRight</code> object + * @brief <code>rightMotor</code> object with pin23, pin22, pin21, + * pin20 and <code>hallsensorRight</code> object */ MaxonESCON rightMotor(p23, p22, p21, p20, &hallRight); /** - * @brief <code>robotControl</code> object with <code>leftMotor</code>, <code>rightMotor</code> and the sampling rate for the run method + * @brief <code>robotControl</code> object with <code>leftMotor</code>, + * <code>rightMotor</code> and the sampling rate for the run method */ RobotControl robotControl(&leftMotor, &rightMotor, PERIOD_ROBOTCONTROL); /*! @} */ @@ -75,31 +89,49 @@ * @name Logging & State * @{ */ - - /** - * @brief Define the struct for the State and the Logging - */ + +/** + * @brief Define the struct for the State and the Logging + */ state_t s; + /** - * @brief <code>state</code> object with <code>robotControl</code>, <code>rightMotor</code>, <code>leftMotor</code>, <code>battery</code>, and the sampling rate for the run method + * @brief <code>state</code> object with <code>robotControl</code>, + * <code>rightMotor</code>, <code>leftMotor</code>, <code>battery</code> + * and the sampling rate for the run method */ State state(&s, &robotControl, &leftMotor, &rightMotor, p15, PERIOD_STATE); /*! @} */ -// PC USB communications DAs wird danach gelöscht +// @todo PC USB communications DAs wird danach gelöscht //Serial pc(USBTX, USBRX); +/** +* @brief Main function. Start the Programm here. +*/ int main() { - - state.initPlotFile(); - state.startTimerFromZero(); - state.start(); - + + /** + * Initialze the filt PLOTS.txt, + * start the timer for the Logging to the file + * and start the Task for logging + **/ + state.initPlotFile(); + state.startTimerFromZero(); + state.start(); + + /** + * Clear all Errors of the ESCON Module, with a disabled to enable event + */ robotControl.setEnable(false); wait(0.1); robotControl.setEnable(true); wait(0.1); + + /** + * Set the startposition and start the Task for controlling the roboter. + */ robotControl.setAllToZero(0, 0, PI/2 ); // robotControl.setAllToZero(START_X_OFFSET, START_Y_OFFSET, PI/2 ); robotControl.start(); @@ -110,32 +142,32 @@ // wait(0.1); ////////////////////////////////////////// - - robotControl.setDesiredPositionAndAngle(0.0f, 1.0f, PI); - while(!(robotControl.getDistanceError() <= 0.1)) { - state.savePlotFile(s); - }; - robotControl.setDesiredPositionAndAngle(-1.00f, 1.0f, -PI/2); - while(!(robotControl.getDistanceError() <= 0.1)) { - state.savePlotFile(s); - }; + robotControl.setDesiredPositionAndAngle(0.0f, 1.0f, PI); + while(!(robotControl.getDistanceError() <= 0.1)) { + state.savePlotFile(s); + }; + + robotControl.setDesiredPositionAndAngle(-1.00f, 1.0f, -PI/2); + while(!(robotControl.getDistanceError() <= 0.1)) { + state.savePlotFile(s); + }; - robotControl.setDesiredPositionAndAngle(0.0f, -0.8f, PI/2); - while(!(robotControl.getDistanceError() <= 0.05)) { - state.savePlotFile(s); - }; + robotControl.setDesiredPositionAndAngle(0.0f, -0.8f, PI/2); + while(!(robotControl.getDistanceError() <= 0.05)) { + state.savePlotFile(s); + }; - robotControl.setDesiredPositionAndAngle(0.0f, -0.2f, PI/2); - while(!(robotControl.getDistanceError() <= 0.05)) { - state.savePlotFile(s); - }; + robotControl.setDesiredPositionAndAngle(0.0f, -0.2f, PI/2); + while(!(robotControl.getDistanceError() <= 0.05)) { + state.savePlotFile(s); + }; - robotControl.setDesiredPositionAndAngle(0.0, 0.0f, PI/2); - while(!(s.millis >= 32000)) { - state.savePlotFile(s); - }; - + robotControl.setDesiredPositionAndAngle(0.0, 0.0f, PI/2); + while(!(s.millis >= 32000)) { + state.savePlotFile(s); + }; + @@ -261,26 +293,29 @@ -/* + /* - pc.baud(460800); - pc.printf("Welcome to adkTerm\n\n\n\n\n\n\r"); - pc.printf("here we go... \n"); - adkTerm.setupDevice(); - pc.printf("Android Development Kit: start\r\n"); - USBInit(); - while (1) { - USBLoop(); - - pc.printf("Android x: %f ",adkTerm.getx()); - pc.printf("Android y: %f ",adkTerm.gety()); - pc.printf("Android t: %f\n",adkTerm.gett()); - robotControl.setDesiredPositionAndAngle(adkTerm.getx(), adkTerm.gety(), adkTerm.gett()); - } + pc.baud(460800); + pc.printf("Welcome to adkTerm\n\n\n\n\n\n\r"); + pc.printf("here we go... \n"); + adkTerm.setupDevice(); + pc.printf("Android Development Kit: start\r\n"); + USBInit(); + while (1) { + USBLoop(); + + pc.printf("Android x: %f ",adkTerm.getx()); + pc.printf("Android y: %f ",adkTerm.gety()); + pc.printf("Android t: %f\n",adkTerm.gett()); + robotControl.setDesiredPositionAndAngle(adkTerm.getx(), adkTerm.gety(), adkTerm.gett()); + } - + /** + * Close the File PLOTS.txt to read the file after with the computer and draw a diagramm + */ + /* state.savePlotFile(s); state.closePlotFile(); state.stop();