V18.
Dependencies: FATFileSystem mbed-rtos
Fork of USBHost by
Revision 32:085a38dec954, committed 2016-10-07
- Comitter:
- JuanManuelAmador
- Date:
- Fri Oct 07 10:37:00 2016 +0000
- Parent:
- 31:220cd93c9a5f
- Child:
- 33:f546501aafeb
- Commit message:
- V18
Changed in this revision
--- a/USBHost3GModule/IUSBHostSerial.h Mon Jun 01 11:01:25 2015 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,95 +0,0 @@ -/* IUSBHostSerial.h */ -/* Copyright (c) 2010-2012 mbed.org, MIT License -* -* 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 IUSBHOSTSERIAL_H_ -#define IUSBHOSTSERIAL_H_ - -/** - * Generic interface to abstract 3G dongles' impl - */ - -#include "USBHostConf.h" - -#ifdef USBHOST_3GMODULE - -#include "IUSBHostSerialListener.h" - -// This is needed by some versions of GCC -#undef putc -#undef getc - -class IUSBHostSerial { -public: - - enum IrqType { - RxIrq, - TxIrq - }; - - /* - * Get a char from the dongle's serial interface - */ - virtual int getc() = 0; - - /* - * Put a char to the dongle's serial interface - */ - virtual int putc(int c) = 0; - - /* - * Read a packet from the dongle's serial interface, to be called after multiple getc() calls - */ - virtual int readPacket() = 0; - - /* - * Write a packet to the dongle's serial interface, to be called after multiple putc() calls - */ - virtual int writePacket() = 0; - - /** - * Check the number of bytes available. - * - * @returns the number of bytes available - */ - virtual int readable() = 0; - - /** - * Check the free space in output. - * - * @returns the number of bytes available - */ - virtual int writeable() = 0; - - /** - * Attach a handler to call when a packet is received / when a packet has been transmitted. - * - * @param pListener instance of the listener deriving from the IUSBHostSerialListener - */ - virtual void attach(IUSBHostSerialListener* pListener) = 0; - - /** - * Enable or disable readable/writeable callbacks - */ - virtual void setupIrq(bool en, IrqType irq = RxIrq) = 0; - -}; - -#endif /* USBHOST_3GMODULE */ - -#endif /* IUSBHOSTSERIAL_H_ */
--- a/USBHost3GModule/IUSBHostSerialListener.h Mon Jun 01 11:01:25 2015 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,37 +0,0 @@ -/* IUSBHostSerialListener.h */ -/* Copyright (c) 2010-2012 mbed.org, MIT License -* -* 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 IUSBHOSTSERIALLISTENER_H_ -#define IUSBHOSTSERIALLISTENER_H_ - -#include "USBHostConf.h" - -#ifdef USBHOST_3GMODULE - -class IUSBHostSerialListener -{ -public: - virtual void readable() = 0; //Called when new data is available - virtual void writeable() = 0; //Called when new space is available -}; - -#endif /* USBHOST_3GMODULE */ - -#endif /* IUSBHOSTSERIALLISTENER_H_ */
--- a/USBHost3GModule/WANDongle.cpp Mon Jun 01 11:01:25 2015 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,235 +0,0 @@ -/* Copyright (c) 2010-2012 mbed.org, MIT License -* -* 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 "USBHostConf.h" - -#ifdef USBHOST_3GMODULE - -#include "dbg.h" -#include <stdint.h> -#include "rtos.h" - -#include "WANDongle.h" -#include "WANDongleInitializer.h" - -WANDongle::WANDongle() : m_pInitializer(NULL), m_serialCount(0), m_totalInitializers(0) -{ - host = USBHost::getHostInst(); - init(); -} - - -bool WANDongle::connected() { - return dev_connected; -} - -bool WANDongle::tryConnect() -{ - //FIXME should run on USB thread - - USB_DBG("Trying to connect device"); - - if (dev_connected) { - USB_DBG("Device is already connected!"); - return true; - } - - m_pInitializer = NULL; - - //Protect from concurrent access from USB thread - USBHost::Lock lock(host); - - for (int i = 0; i < MAX_DEVICE_CONNECTED; i++) - { - if ((dev = host->getDevice(i)) != NULL) - { - m_pInitializer = NULL; //Will be set in setVidPid callback - - USB_DBG("Enumerate"); - int ret = host->enumerate(dev, this); - if(ret) - { - return false; - } - - USB_DBG("Device has VID:%04x PID:%04x", dev->getVid(), dev->getPid()); - - if(m_pInitializer) //If an initializer has been found - { - USB_DBG("m_pInitializer=%p", m_pInitializer); - USB_DBG("m_pInitializer->getSerialVid()=%04x", m_pInitializer->getSerialVid()); - USB_DBG("m_pInitializer->getSerialPid()=%04x", m_pInitializer->getSerialPid()); - if ((dev->getVid() == m_pInitializer->getSerialVid()) && (dev->getPid() == m_pInitializer->getSerialPid())) - { - USB_DBG("The dongle is in virtual serial mode"); - host->registerDriver(dev, 0, this, &WANDongle::init); - m_serialCount = m_pInitializer->getSerialPortCount(); - if( m_serialCount > WANDONGLE_MAX_SERIAL_PORTS ) - { - m_serialCount = WANDONGLE_MAX_SERIAL_PORTS; - } - for(int j = 0; j < m_serialCount; j++) - { - USB_DBG("Connecting serial port #%d", j+1); - USB_DBG("Ep %p", m_pInitializer->getEp(dev, j, false)); - USB_DBG("Ep %p", m_pInitializer->getEp(dev, j, true)); - m_serial[j].connect( dev, m_pInitializer->getEp(dev, j, false), m_pInitializer->getEp(dev, j, true) ); - } - - USB_DBG("Device connected"); - - dev_connected = true; - - - return true; - } - else if ((dev->getVid() == m_pInitializer->getMSDVid()) && (dev->getPid() == m_pInitializer->getMSDPid())) - { - USB_DBG("Vodafone K3370 dongle detected in MSD mode"); - //Try to switch - if( m_pInitializer->switchMode(dev) ) - { - USB_DBG("Switched OK"); - return false; //Will be connected on a next iteration - } - else - { - USB_ERR("Could not switch mode"); - return false; - } - } - } //if() - } //if() - } //for() - return false; -} - -bool WANDongle::disconnect() -{ - dev_connected = false; - for(int i = 0; i < WANDONGLE_MAX_SERIAL_PORTS; i++) - { - m_serial[i].disconnect(); - } - return true; -} - -int WANDongle::getDongleType() -{ - if( m_pInitializer != NULL ) - { - return m_pInitializer->getType(); - } - else - { - return WAN_DONGLE_TYPE_UNKNOWN; - } -} - -IUSBHostSerial& WANDongle::getSerial(int index) -{ - return m_serial[index]; -} - -int WANDongle::getSerialCount() -{ - return m_serialCount; -} - -//Private methods -void WANDongle::init() -{ - m_pInitializer = NULL; - dev_connected = false; - for(int i = 0; i < WANDONGLE_MAX_SERIAL_PORTS; i++) - { - m_serial[i].init(host); - } -} - - -/*virtual*/ void WANDongle::setVidPid(uint16_t vid, uint16_t pid) -{ - WANDongleInitializer* initializer; - - for(int i = 0; i < m_totalInitializers; i++) - { - initializer = m_Initializers[i]; - USB_DBG("initializer=%p", initializer); - USB_DBG("initializer->getSerialVid()=%04x", initializer->getSerialVid()); - USB_DBG("initializer->getSerialPid()=%04x", initializer->getSerialPid()); - if ((dev->getVid() == initializer->getSerialVid()) && (dev->getPid() == initializer->getSerialPid())) - { - USB_DBG("The dongle is in virtual serial mode"); - m_pInitializer = initializer; - break; - } - else if ((dev->getVid() == initializer->getMSDVid()) && (dev->getPid() == initializer->getMSDPid())) - { - USB_DBG("Dongle detected in MSD mode"); - m_pInitializer = initializer; - break; - } - initializer++; - } //for - if(m_pInitializer) - { - m_pInitializer->setVidPid(vid, pid); - } -} - -/*virtual*/ bool WANDongle::parseInterface(uint8_t intf_nb, uint8_t intf_class, uint8_t intf_subclass, uint8_t intf_protocol) //Must return true if the interface should be parsed -{ - if(m_pInitializer) - { - return m_pInitializer->parseInterface(intf_nb, intf_class, intf_subclass, intf_protocol); - } - else - { - return false; - } -} - -/*virtual*/ bool WANDongle::useEndpoint(uint8_t intf_nb, ENDPOINT_TYPE type, ENDPOINT_DIRECTION dir) //Must return true if the endpoint will be used -{ - if(m_pInitializer) - { - return m_pInitializer->useEndpoint(intf_nb, type, dir); - } - else - { - return false; - } -} - - -bool WANDongle::addInitializer(WANDongleInitializer* pInitializer) -{ - if (m_totalInitializers >= WANDONGLE_MAX_INITIALIZERS) - return false; - m_Initializers[m_totalInitializers++] = pInitializer; - return true; -} - -WANDongle::~WANDongle() -{ - for(int i = 0; i < m_totalInitializers; i++) - delete m_Initializers[i]; -} - -#endif /* USBHOST_3GMODULE */
--- a/USBHost3GModule/WANDongle.h Mon Jun 01 11:01:25 2015 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,108 +0,0 @@ -/* Copyright (c) 2010-2012 mbed.org, MIT License -* -* 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 WANDONGLE_H -#define WANDONGLE_H - -#include "USBHostConf.h" - -#ifdef USBHOST_3GMODULE - -#include "USBHost.h" -#include "IUSBHostSerial.h" - -#include "rtos.h" - -#include "WANDongleSerialPort.h" -#include "WANDongleInitializer.h" -#include "IUSBEnumerator.h" - -#define WANDONGLE_MAX_OUTEP_SIZE 64 -#define WANDONGLE_MAX_INEP_SIZE 64 - -/** A class to use a WAN (3G/LTE) access dongle - * - */ -class WANDongle : public IUSBEnumerator { -public: - /* - * Constructor - * - * @param rootdir mount name - */ - WANDongle(); - - /* - * Destructor - */ - virtual ~WANDongle(); - - /* - * Check if a serial port device is connected - * - * @return true if a serial device is connected - */ - bool connected(); - - /* - * Try to connect device - * - * * @return true if connection was successful - */ - bool tryConnect(); - - /* - * Disconnect device - * - * * @return true if disconnection was successful - */ - bool disconnect(); - - int getDongleType(); - - IUSBHostSerial& getSerial(int index); - int getSerialCount(); - bool addInitializer(WANDongleInitializer* pInitializer); - - //From IUSBEnumerator - - virtual void setVidPid(uint16_t vid, uint16_t pid); - - virtual bool parseInterface(uint8_t intf_nb, uint8_t intf_class, uint8_t intf_subclass, uint8_t intf_protocol); //Must return true if the interface should be parsed - - virtual bool useEndpoint(uint8_t intf_nb, ENDPOINT_TYPE type, ENDPOINT_DIRECTION dir); //Must return true if the endpoint will be used - -protected: - USBHost * host; - USBDeviceConnected * dev; - bool dev_connected; - - WANDongleInitializer* m_pInitializer; - - void init(); - - WANDongleSerialPort m_serial[WANDONGLE_MAX_SERIAL_PORTS]; - int m_serialCount; - - int m_totalInitializers; - WANDongleInitializer* m_Initializers[WANDONGLE_MAX_INITIALIZERS]; -}; - -#endif /* USBHOST_3GMODULE */ - -#endif
--- a/USBHost3GModule/WANDongleInitializer.h Mon Jun 01 11:01:25 2015 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,73 +0,0 @@ -/* Copyright (c) 2010-2012 mbed.org, MIT License -* -* 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 WANDONGLEINITIALIZER_H -#define WANDONGLEINITIALIZER_H - -#include "USBHostConf.h" - -#ifdef USBHOST_3GMODULE - -#include <stdint.h> - -#include "USBHost.h" -#include "IUSBEnumerator.h" - -// [TODO] move these declarations to a proper place -#define WANDONGLE_MAX_SERIAL_PORTS 2 -#define WANDONGLE_MAX_INITIALIZERS 6 - -#define WAN_DONGLE_TYPE_UNKNOWN (-1) - -class WANDongleInitializer : public IUSBEnumerator -{ -protected: - WANDongleInitializer(USBHost* pHost) { m_pHost = pHost; } - USBHost* m_pHost; - uint8_t m_serialIntfMap[WANDONGLE_MAX_SERIAL_PORTS]; - -public: - virtual ~WANDongleInitializer() {} - virtual uint16_t getMSDVid() = 0; - virtual uint16_t getMSDPid() = 0; - - virtual uint16_t getSerialVid() = 0; - virtual uint16_t getSerialPid() = 0; - - virtual bool switchMode(USBDeviceConnected* pDev) = 0; - - virtual USBEndpoint* getEp(USBDeviceConnected* pDev, int serialPortNumber, bool tx) { - return pDev->getEndpoint(m_serialIntfMap[serialPortNumber], BULK_ENDPOINT, tx ? OUT : IN, 0); - } - - virtual int getSerialPortCount() = 0; - - virtual void setVidPid(uint16_t vid, uint16_t pid) = 0; - - virtual bool parseInterface(uint8_t intf_nb, uint8_t intf_class, uint8_t intf_subclass, uint8_t intf_protocol) = 0; //Must return true if the interface should be parsed - - virtual bool useEndpoint(uint8_t intf_nb, ENDPOINT_TYPE type, ENDPOINT_DIRECTION dir) = 0; //Must return true if the endpoint will be used - - virtual int getType() = 0; - - virtual uint8_t getSerialIntf(int index) { return m_serialIntfMap[index]; } -}; - -#endif /* USBHOST_3GMODULE */ - -#endif
--- a/USBHost3GModule/WANDongleSerialPort.cpp Mon Jun 01 11:01:25 2015 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,340 +0,0 @@ -/* Copyright (c) 2010-2012 mbed.org, MIT License -* -* 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 "USBHostConf.h" - -#ifdef USBHOST_3GMODULE - -#define __DEBUG__ 0 -#ifndef __MODULE__ -#define __MODULE__ "WANDongleSerialPort.cpp" -#endif - -#include "dbg.h" -#include <stdint.h> -#include "rtos.h" - -#include "WANDongleSerialPort.h" - -WANDongleSerialPort::WANDongleSerialPort() : cb_tx_en(false), cb_rx_en(false), listener(NULL) -{ - reset(); -} - -void WANDongleSerialPort::init(USBHost* pHost) -{ - host = pHost; -} - -void WANDongleSerialPort::reset() -{ - tx_mtx.lock(); - rx_mtx.lock(); - - bulk_in = NULL; - bulk_out = NULL; - - buf_out_len = 0; - max_out_size = 0; - lock_tx = false; - cb_tx_pending = false; - - buf_in_len = 0; - buf_in_read_pos = 0; - lock_rx = false; - cb_rx_pending = false; - - tx_mtx.unlock(); - rx_mtx.unlock(); -} - -int WANDongleSerialPort::readPacket() -{ - USB_DBG("Read packet on %p", this); - rx_mtx.lock(); - if(lock_rx) - { - USB_ERR("Fail"); - rx_mtx.unlock(); - return -1; - } - - if( bulk_in == NULL ) - { - USB_WARN("Port is disconnected"); - rx_mtx.unlock(); - return -1; - } - - lock_rx = true; //Receiving - rx_mtx.unlock(); -// USB_DBG("readPacket"); - //lock_rx.lock(); - USB_TYPE res = host->bulkRead(dev, (USBEndpoint *)bulk_in, buf_in, ((USBEndpoint *)bulk_in)->getSize(), false); //Queue transfer - if(res != USB_TYPE_PROCESSING) - { - //lock_rx.unlock(); - USB_ERR("host->bulkRead() returned %d", res); - Thread::wait(100); - return -1; - } - return 0; -} - -int WANDongleSerialPort::writePacket() -{ - tx_mtx.lock(); - if(lock_tx) - { - USB_ERR("Fail"); - tx_mtx.unlock(); - return -1; - } - - if( bulk_out == NULL ) - { - USB_WARN("Port is disconnected"); - tx_mtx.unlock(); - return -1; - } - - lock_tx = true; //Transmitting - tx_mtx.unlock(); -// USB_DBG("writePacket"); - - //lock_tx.lock(); - USB_TYPE res = host->bulkWrite(dev, (USBEndpoint *)bulk_out, buf_out, buf_out_len, false); //Queue transfer - if(res != USB_TYPE_PROCESSING) - { - //lock_tx.unlock(); - USB_ERR("host->bulkWrite() returned %d", res); - Thread::wait(100); - return -1; - } - return 0; -} - -int WANDongleSerialPort::putc(int c) -{ - tx_mtx.lock(); - if(!lock_tx) - { - if(buf_out_len < max_out_size) - { - buf_out[buf_out_len] = (uint8_t)c; - buf_out_len++; - } - } - else - { - USB_ERR("CAN'T WRITE!"); - } - tx_mtx.unlock(); - return c; -} - -int WANDongleSerialPort::getc() -{ - rx_mtx.lock(); - int c = 0; - if(!lock_rx) - { - if(buf_in_read_pos < buf_in_len) - { - c = (int)buf_in[buf_in_read_pos]; - buf_in_read_pos++; - } - } - else - { - USB_ERR("CAN'T READ!"); - } - rx_mtx.unlock(); - return c; -} - -int WANDongleSerialPort::readable() -{ - rx_mtx.lock(); - if (lock_rx) - { - rx_mtx.unlock(); - return 0; - } - - /* if( !lock_rx.trylock() ) - { - return 0; - }*/ - int res = buf_in_len - buf_in_read_pos; - //lock_rx.unlock(); - rx_mtx.unlock(); - return res; -} - -int WANDongleSerialPort::writeable() -{ - tx_mtx.lock(); - if (lock_tx) - { - tx_mtx.unlock(); - return 0; - } - - /*if( !lock_tx.trylock() ) - { - return 0; - }*/ - int res = max_out_size - buf_out_len; - tx_mtx.unlock(); - //lock_tx.unlock(); - return res; -} - -void WANDongleSerialPort::attach(IUSBHostSerialListener* pListener) -{ - if(pListener == NULL) - { - setupIrq(false, RxIrq); - setupIrq(false, TxIrq); - } - listener = pListener; - if(pListener != NULL) - { - setupIrq(true, RxIrq); - setupIrq(true, TxIrq); - } -} - -void WANDongleSerialPort::setupIrq(bool en, IrqType irq /*= RxIrq*/) -{ - switch(irq) - { - case RxIrq: - rx_mtx.lock(); - cb_rx_en = en; - if(en && cb_rx_pending) - { - cb_rx_pending = false; - rx_mtx.unlock(); - listener->readable(); //Process the interrupt that was raised - } - else - { - rx_mtx.unlock(); - } - break; - case TxIrq: - tx_mtx.lock(); - cb_tx_en = en; - if(en && cb_tx_pending) - { - cb_tx_pending = false; - tx_mtx.unlock(); - listener->writeable(); //Process the interrupt that was raised - } - else - { - tx_mtx.unlock(); - } - break; - } -} - - -void WANDongleSerialPort::connect( USBDeviceConnected* pDev, USBEndpoint* pInEp, USBEndpoint* pOutEp ) -{ - dev = pDev; - bulk_in = pInEp; - bulk_out = pOutEp; - max_out_size = bulk_out->getSize(); - if( max_out_size > WANDONGLE_MAX_OUTEP_SIZE ) - { - max_out_size = WANDONGLE_MAX_OUTEP_SIZE; - } - bulk_in->attach(this, &WANDongleSerialPort::rxHandler); - bulk_out->attach(this, &WANDongleSerialPort::txHandler); - readPacket(); //Start receiving data -} - -void WANDongleSerialPort::disconnect( ) -{ - reset(); -} - -//Private methods - - -void WANDongleSerialPort::rxHandler() -{ - if (((USBEndpoint *) bulk_in)->getState() == USB_TYPE_IDLE) //Success - { - buf_in_read_pos = 0; - buf_in_len = ((USBEndpoint *) bulk_in)->getLengthTransferred(); //Update length - //lock_rx.unlock(); - rx_mtx.lock(); - lock_rx = false; //Transmission complete - if(cb_rx_en) - { - rx_mtx.unlock(); - listener->readable(); //Call handler from the IRQ context - //readPacket() should be called by the handler subsequently once the buffer has been emptied - } - else - { - cb_rx_pending = true; //Queue the callback - rx_mtx.unlock(); - } - - } - else //Error, try reading again - { - //lock_rx.unlock(); - USB_DBG("Trying again"); - readPacket(); - } -} - -void WANDongleSerialPort::txHandler() -{ - if (((USBEndpoint *) bulk_out)->getState() == USB_TYPE_IDLE) //Success - { - tx_mtx.lock(); - buf_out_len = 0; //Reset length - lock_tx = false; //Transmission complete - //lock_tx.unlock(); - if(cb_tx_en) - { - tx_mtx.unlock(); - listener->writeable(); //Call handler from the IRQ context - //writePacket() should be called by the handler subsequently once the buffer has been filled - } - else - { - cb_tx_pending = true; //Queue the callback - tx_mtx.unlock(); - } - } - else //Error, try reading again - { - //lock_tx.unlock(); - writePacket(); - } -} - -#endif /* USBHOST_3GMODULE */
--- a/USBHost3GModule/WANDongleSerialPort.h Mon Jun 01 11:01:25 2015 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,133 +0,0 @@ -/* Copyright (c) 2010-2012 mbed.org, MIT License -* -* 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 WANDONGLESERIALPORT_H -#define WANDONGLESERIALPORT_H - -#include "USBHostConf.h" - -#ifdef USBHOST_3GMODULE - -#include "USBHost.h" -#include "IUSBHostSerial.h" - -#include "rtos.h" - - -#define WANDONGLE_MAX_OUTEP_SIZE 64 -#define WANDONGLE_MAX_INEP_SIZE 64 - -/** A class to use a WAN (3G/LTE) access dongle - * - */ -class WANDongleSerialPort : public IUSBHostSerial { -public: - /* - * Constructor - * - */ - WANDongleSerialPort(); - - void init( USBHost* pHost ); - - void connect( USBDeviceConnected* pDev, USBEndpoint* pInEp, USBEndpoint* pOutEp ); - - void disconnect( ); - - /* - * Get a char from the dongle's serial interface - */ - virtual int getc(); - - /* - * Put a char to the dongle's serial interface - */ - virtual int putc(int c); - - /* - * Read a packet from the dongle's serial interface, to be called after multiple getc() calls - */ - virtual int readPacket(); - - /* - * Write a packet to the dongle's serial interface, to be called after multiple putc() calls - */ - virtual int writePacket(); - - /** - * Check the number of bytes available. - * - * @returns the number of bytes available - */ - virtual int readable(); - - /** - * Check the free space in output. - * - * @returns the number of bytes available - */ - virtual int writeable(); - - /** - * Attach a handler to call when a packet is received / when a packet has been transmitted. - * - * @param pListener instance of the listener deriving from the IUSBHostSerialListener - */ - virtual void attach(IUSBHostSerialListener* pListener); - - /** - * Enable or disable readable/writeable callbacks - */ - virtual void setupIrq(bool en, IrqType irq = RxIrq); - - -protected: - USBEndpoint * bulk_in; - USBEndpoint * bulk_out; - USBHost * host; - USBDeviceConnected * dev; - - uint8_t buf_out[WANDONGLE_MAX_OUTEP_SIZE]; - volatile uint32_t buf_out_len; - uint32_t max_out_size; - volatile bool lock_tx; - volatile bool cb_tx_en; - volatile bool cb_tx_pending; - Mutex tx_mtx; - - uint8_t buf_in[WANDONGLE_MAX_INEP_SIZE]; - volatile uint32_t buf_in_len; - volatile uint32_t buf_in_read_pos; - volatile bool lock_rx; - volatile bool cb_rx_en; - volatile bool cb_rx_pending; - Mutex rx_mtx; - - IUSBHostSerialListener* listener; - - void reset(); - - void rxHandler(); - void txHandler(); - -}; - -#endif /* USBHOST_3GMODULE */ - -#endif -
--- a/USBHostHID/USBHostKeyboard.cpp Mon Jun 01 11:01:25 2015 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,184 +0,0 @@ -/* mbed USBHost Library - * Copyright (c) 2006-2013 ARM Limited - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "USBHostKeyboard.h" - -#if USBHOST_KEYBOARD - -static uint8_t keymap[4][0x39] = { - { 0, 0, 0, 0, 'a', 'b' /*0x05*/, - 'c', 'd', 'e', 'f', 'g' /*0x0a*/, - 'h', 'i', 'j', 'k', 'l'/*0x0f*/, - 'm', 'n', 'o', 'p', 'q'/*0x14*/, - 'r', 's', 't', 'u', 'v'/*0x19*/, - 'w', 'x', 'y', 'z', '1'/*0x1E*/, - '2', '3', '4', '5', '6'/*0x23*/, - '7', '8', '9', '0', 0x0A /*enter*/, /*0x28*/ - 0x1B /*escape*/, 0x08 /*backspace*/, 0x09/*tab*/, 0x20/*space*/, '-', /*0x2d*/ - '=', '[', ']', '\\', '#', /*0x32*/ - ';', '\'', 0, ',', '.', /*0x37*/ - '/'}, - - /* CTRL MODIFIER */ - { 0, 0, 0, 0, 0, 0 /*0x05*/, - 0, 0, 0, 0, 0 /*0x0a*/, - 0, 0, 0, 0, 0/*0x0f*/, - 0, 0, 0, 0, 0/*0x14*/, - 0, 0, 0, 0, 0/*0x19*/, - 0, 0, 0, 0, 0/*0x1E*/, - 0, 0, 0, 0, 0/*0x23*/, - 0, 0, 0, 0, 0 /*enter*/, /*0x28*/ - 0, 0, 0, 0, 0, /*0x2d*/ - 0, 0, 0, 0, 0, /*0x32*/ - 0, 0, 0, 0, 0, /*0x37*/ - 0}, - - /* SHIFT MODIFIER */ - { 0, 0, 0, 0, 'A', 'B' /*0x05*/, - 'C', 'D', 'E', 'F', 'G' /*0x0a*/, - 'H', 'I', 'J', 'K', 'L'/*0x0f*/, - 'M', 'N', 'O', 'P', 'Q'/*0x14*/, - 'R', 'S', 'T', 'U', 'V'/*0x19*/, - 'W', 'X', 'Y', 'Z', '!'/*0x1E*/, - '@', '#', '$', '%', '^'/*0x23*/, - '&', '*', '(', ')', 0, /*0x28*/ - 0, 0, 0, 0, 0, /*0x2d*/ - '+', '{', '}', '|', '~', /*0x32*/ - ':', '"', 0, '<', '>', /*0x37*/ - '?'}, - - /* ALT MODIFIER */ - { 0, 0, 0, 0, 0, 0 /*0x05*/, - 0, 0, 0, 0, 0 /*0x0a*/, - 0, 0, 0, 0, 0/*0x0f*/, - 0, 0, 0, 0, 0/*0x14*/, - 0, 0, 0, 0, 0/*0x19*/, - 0, 0, 0, 0, 0/*0x1E*/, - 0, 0, 0, 0, 0/*0x23*/, - 0, 0, 0, 0, 0 /*enter*/, /*0x28*/ - 0, 0, 0, 0, 0, /*0x2d*/ - 0, 0, 0, 0, 0, /*0x32*/ - 0, 0, 0, 0, 0, /*0x37*/ - 0} - -}; - - -USBHostKeyboard::USBHostKeyboard() { - host = USBHost::getHostInst(); - init(); -} - - -void USBHostKeyboard::init() { - dev = NULL; - int_in = NULL; - report_id = 0; - onKey = NULL; - onKeyCode = NULL; - dev_connected = false; - keyboard_intf = -1; - keyboard_device_found = false; -} - -bool USBHostKeyboard::connected() { - return dev_connected; -} - - -bool USBHostKeyboard::connect() { - - if (dev_connected) { - return true; - } - - for (uint8_t i = 0; i < MAX_DEVICE_CONNECTED; i++) { - if ((dev = host->getDevice(i)) != NULL) { - - if (host->enumerate(dev, this)) - break; - - if (keyboard_device_found) { - int_in = dev->getEndpoint(keyboard_intf, INTERRUPT_ENDPOINT, IN); - - if (!int_in) - break; - - USB_INFO("New Keyboard device: VID:%04x PID:%04x [dev: %p - intf: %d]", dev->getVid(), dev->getPid(), dev, keyboard_intf); - dev->setName("Keyboard", keyboard_intf); - host->registerDriver(dev, keyboard_intf, this, &USBHostKeyboard::init); - - int_in->attach(this, &USBHostKeyboard::rxHandler); - host->interruptRead(dev, int_in, report, int_in->getSize(), false); - - dev_connected = true; - return true; - } - } - } - init(); - return false; -} - -void USBHostKeyboard::rxHandler() { - int len = int_in->getLengthTransferred(); - int index = (len == 9) ? 1 : 0; - int len_listen = int_in->getSize(); - uint8_t key = 0; - if (len == 8 || len == 9) { - uint8_t modifier = (report[index] == 4) ? 3 : report[index]; - len_listen = len; - key = keymap[modifier][report[index + 2]]; - if (key && onKey) { - (*onKey)(key); - } - if ((report[index + 2] || modifier) && onKeyCode) { - (*onKeyCode)(report[index + 2], modifier); - } - } - if (dev && int_in) - host->interruptRead(dev, int_in, report, len_listen, false); -} - -/*virtual*/ void USBHostKeyboard::setVidPid(uint16_t vid, uint16_t pid) -{ - // we don't check VID/PID for keyboard driver -} - -/*virtual*/ bool USBHostKeyboard::parseInterface(uint8_t intf_nb, uint8_t intf_class, uint8_t intf_subclass, uint8_t intf_protocol) //Must return true if the interface should be parsed -{ - if ((keyboard_intf == -1) && - (intf_class == HID_CLASS) && - (intf_subclass == 0x01) && - (intf_protocol == 0x01)) { - keyboard_intf = intf_nb; - return true; - } - return false; -} - -/*virtual*/ bool USBHostKeyboard::useEndpoint(uint8_t intf_nb, ENDPOINT_TYPE type, ENDPOINT_DIRECTION dir) //Must return true if the endpoint will be used -{ - if (intf_nb == keyboard_intf) { - if (type == INTERRUPT_ENDPOINT && dir == IN) { - keyboard_device_found = true; - return true; - } - } - return false; -} - -#endif
--- a/USBHostHID/USBHostKeyboard.h Mon Jun 01 11:01:25 2015 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,102 +0,0 @@ -/* mbed USBHost Library - * Copyright (c) 2006-2013 ARM Limited - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef USBHOSTKEYBOARD_H -#define USBHOSTKEYBOARD_H - -#include "USBHostConf.h" - -#if USBHOST_KEYBOARD - -#include "USBHost.h" - -/** - * A class to communicate a USB keyboard - */ -class USBHostKeyboard : public IUSBEnumerator { -public: - - /** - * Constructor - */ - USBHostKeyboard(); - - /** - * Try to connect a keyboard device - * - * @return true if connection was successful - */ - bool connect(); - - /** - * Check if a keyboard is connected - * - * @returns true if a keyboard is connected - */ - bool connected(); - - /** - * Attach a callback called when a keyboard event is received - * - * @param ptr function pointer - */ - inline void attach(void (*ptr)(uint8_t key)) { - if (ptr != NULL) { - onKey = ptr; - } - } - - /** - * Attach a callback called when a keyboard event is received - * - * @param ptr function pointer - */ - inline void attach(void (*ptr)(uint8_t keyCode, uint8_t modifier)) { - if (ptr != NULL) { - onKeyCode = ptr; - } - } - -protected: - //From IUSBEnumerator - virtual void setVidPid(uint16_t vid, uint16_t pid); - virtual bool parseInterface(uint8_t intf_nb, uint8_t intf_class, uint8_t intf_subclass, uint8_t intf_protocol); //Must return true if the interface should be parsed - virtual bool useEndpoint(uint8_t intf_nb, ENDPOINT_TYPE type, ENDPOINT_DIRECTION dir); //Must return true if the endpoint will be used - -private: - USBHost * host; - USBDeviceConnected * dev; - USBEndpoint * int_in; - uint8_t report[9]; - int keyboard_intf; - bool keyboard_device_found; - - bool dev_connected; - - void rxHandler(); - - void (*onKey)(uint8_t key); - void (*onKeyCode)(uint8_t key, uint8_t modifier); - - int report_id; - - void init(); - -}; - -#endif - -#endif
--- a/USBHostHID/USBHostMouse.cpp Mon Jun 01 11:01:25 2015 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,153 +0,0 @@ -/* mbed USBHost Library - * Copyright (c) 2006-2013 ARM Limited - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "USBHostMouse.h" - -#if USBHOST_MOUSE - -USBHostMouse::USBHostMouse() { - host = USBHost::getHostInst(); - init(); -} - -void USBHostMouse::init() { - dev = NULL; - int_in = NULL; - onUpdate = NULL; - onButtonUpdate = NULL; - onXUpdate = NULL; - onYUpdate = NULL; - onZUpdate = NULL; - report_id = 0; - dev_connected = false; - mouse_device_found = false; - mouse_intf = -1; - - buttons = 0; - x = 0; - y = 0; - z = 0; -} - -bool USBHostMouse::connected() { - return dev_connected; -} - -bool USBHostMouse::connect() { - int len_listen; - - if (dev_connected) { - return true; - } - - for (uint8_t i = 0; i < MAX_DEVICE_CONNECTED; i++) { - if ((dev = host->getDevice(i)) != NULL) { - - if(host->enumerate(dev, this)) - break; - - if (mouse_device_found) { - - int_in = dev->getEndpoint(mouse_intf, INTERRUPT_ENDPOINT, IN); - if (!int_in) - break; - - USB_INFO("New Mouse device: VID:%04x PID:%04x [dev: %p - intf: %d]", dev->getVid(), dev->getPid(), dev, mouse_intf); - dev->setName("Mouse", mouse_intf); - host->registerDriver(dev, mouse_intf, this, &USBHostMouse::init); - - int_in->attach(this, &USBHostMouse::rxHandler); - len_listen = int_in->getSize(); - if (len_listen > sizeof(report)) { - len_listen = sizeof(report); - } - host->interruptRead(dev, int_in, report, len_listen, false); - - dev_connected = true; - return true; - } - } - } - init(); - return false; -} - -void USBHostMouse::rxHandler() { - int len_listen = int_in->getSize(); - - if (onUpdate) { - (*onUpdate)(report[0] & 0x07, report[1], report[2], report[3]); - } - - if (onButtonUpdate && (buttons != (report[0] & 0x07))) { - (*onButtonUpdate)(report[0] & 0x07); - } - - if (onXUpdate && (x != report[1])) { - (*onXUpdate)(report[1]); - } - - if (onYUpdate && (y != report[2])) { - (*onYUpdate)(report[2]); - } - - if (onZUpdate && (z != report[3])) { - (*onZUpdate)(report[3]); - } - - // update mouse state - buttons = report[0] & 0x07; - x = report[1]; - y = report[2]; - z = report[3]; - - if (len_listen > sizeof(report)) { - len_listen = sizeof(report); - } - - if (dev) - host->interruptRead(dev, int_in, report, len_listen, false); -} - -/*virtual*/ void USBHostMouse::setVidPid(uint16_t vid, uint16_t pid) -{ - // we don't check VID/PID for mouse driver -} - -/*virtual*/ bool USBHostMouse::parseInterface(uint8_t intf_nb, uint8_t intf_class, uint8_t intf_subclass, uint8_t intf_protocol) //Must return true if the interface should be parsed -{ - if ((mouse_intf == -1) && - (intf_class == HID_CLASS) && - (intf_subclass == 0x01) && - (intf_protocol == 0x02)) { - mouse_intf = intf_nb; - return true; - } - return false; -} - -/*virtual*/ bool USBHostMouse::useEndpoint(uint8_t intf_nb, ENDPOINT_TYPE type, ENDPOINT_DIRECTION dir) //Must return true if the endpoint will be used -{ - if (intf_nb == mouse_intf) { - if (type == INTERRUPT_ENDPOINT && dir == IN) { - mouse_device_found = true; - return true; - } - } - return false; -} - -#endif
--- a/USBHostHID/USBHostMouse.h Mon Jun 01 11:01:25 2015 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,139 +0,0 @@ -/* mbed USBHost Library - * Copyright (c) 2006-2013 ARM Limited - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef USBHOSTMOUSE_H -#define USBHOSTMOUSE_H - -#include "USBHostConf.h" - -#if USBHOST_MOUSE - -#include "USBHost.h" - -/** - * A class to communicate a USB mouse - */ -class USBHostMouse : public IUSBEnumerator { -public: - - /** - * Constructor - */ - USBHostMouse(); - - /** - * Try to connect a mouse device - * - * @return true if connection was successful - */ - bool connect(); - - /** - * Check if a mouse is connected - * - * @returns true if a mouse is connected - */ - bool connected(); - - /** - * Attach a callback called when a mouse event is received - * - * @param ptr function pointer - */ - inline void attachEvent(void (*ptr)(uint8_t buttons, int8_t x, int8_t y, int8_t z)) { - if (ptr != NULL) { - onUpdate = ptr; - } - } - - /** - * Attach a callback called when the button state changes - * - * @param ptr function pointer - */ - inline void attachButtonEvent(void (*ptr)(uint8_t buttons)) { - if (ptr != NULL) { - onButtonUpdate = ptr; - } - } - - /** - * Attach a callback called when the X axis value changes - * - * @param ptr function pointer - */ - inline void attachXEvent(void (*ptr)(int8_t x)) { - if (ptr != NULL) { - onXUpdate = ptr; - } - } - - /** - * Attach a callback called when the Y axis value changes - * - * @param ptr function pointer - */ - inline void attachYEvent(void (*ptr)(int8_t y)) { - if (ptr != NULL) { - onYUpdate = ptr; - } - } - - /** - * Attach a callback called when the Z axis value changes (scrolling) - * - * @param ptr function pointer - */ - inline void attachZEvent(void (*ptr)(int8_t z)) { - if (ptr != NULL) { - onZUpdate = ptr; - } - } - -protected: - //From IUSBEnumerator - virtual void setVidPid(uint16_t vid, uint16_t pid); - virtual bool parseInterface(uint8_t intf_nb, uint8_t intf_class, uint8_t intf_subclass, uint8_t intf_protocol); //Must return true if the interface should be parsed - virtual bool useEndpoint(uint8_t intf_nb, ENDPOINT_TYPE type, ENDPOINT_DIRECTION dir); //Must return true if the endpoint will be used - -private: - USBHost * host; - USBDeviceConnected * dev; - USBEndpoint * int_in; - uint8_t report[4]; - - bool dev_connected; - bool mouse_device_found; - int mouse_intf; - - uint8_t buttons; - int8_t x; - int8_t y; - int8_t z; - - void rxHandler(); - void (*onUpdate)(uint8_t buttons, int8_t x, int8_t y, int8_t z); - void (*onButtonUpdate)(uint8_t buttons); - void (*onXUpdate)(int8_t x); - void (*onYUpdate)(int8_t y); - void (*onZUpdate)(int8_t z); - int report_id; - void init(); -}; - -#endif - -#endif
--- a/USBHostHub/USBHostHub.cpp Mon Jun 01 11:01:25 2015 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,274 +0,0 @@ -/* mbed USBHost Library - * Copyright (c) 2006-2013 ARM Limited - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "USBHostHub.h" - -#if MAX_HUB_NB - -#include "USBHost.h" -#include "dbg.h" - -#define GET_STATUS 0x00 -#define CLEAR_FEATURE 0x01 -#define GET_STATE 0x02 -#define SET_FEATURE 0x03 -#define GET_DESCRIPTOR 0x06 - -#define PORT_CONNECTION_FEATURE (0x00) -#define PORT_ENABLE_FEATURE (0x01) -#define PORT_RESET_FEATURE (0x04) -#define PORT_POWER_FEATURE (0x08) - -#define C_PORT_CONNECTION_FEATURE (16) -#define C_PORT_ENABLE_FEATURE (17) -#define C_PORT_RESET_FEATURE (20) - -#define PORT_CONNECTION (1 << 0) -#define PORT_ENABLE (1 << 1) -#define PORT_SUSPEND (1 << 2) -#define PORT_OVER_CURRENT (1 << 3) -#define PORT_RESET (1 << 4) -#define PORT_POWER (1 << 8) -#define PORT_LOW_SPEED (1 << 9) - -#define C_PORT_CONNECTION (1 << 16) -#define C_PORT_ENABLE (1 << 17) -#define C_PORT_SUSPEND (1 << 18) -#define C_PORT_OVER_CURRENT (1 << 19) -#define C_PORT_RESET (1 << 20) - -USBHostHub::USBHostHub() { - host = NULL; - init(); -} - -void USBHostHub::init() { - dev_connected = false; - dev = NULL; - int_in = NULL; - dev_connected = false; - hub_intf = -1; - hub_device_found = false; - nb_port = 0; - hub_characteristics = 0; - - for (int i = 0; i < MAX_HUB_PORT; i++) { - device_children[i] = NULL; - } -} - -void USBHostHub::setHost(USBHost * host_) { - host = host_; -} - -bool USBHostHub::connected() -{ - return dev_connected; -} - -bool USBHostHub::connect(USBDeviceConnected * dev) -{ - if (dev_connected) { - return true; - } - - if(host->enumerate(dev, this)) { - init(); - return false; - } - - if (hub_device_found) { - this->dev = dev; - - int_in = dev->getEndpoint(hub_intf, INTERRUPT_ENDPOINT, IN); - - if (!int_in) { - init(); - return false; - } - - USB_INFO("New HUB: VID:%04x PID:%04x [dev: %p - intf: %d]", dev->getVid(), dev->getPid(), dev, hub_intf); - dev->setName("Hub", hub_intf); - host->registerDriver(dev, hub_intf, this, &USBHostHub::disconnect); - - int_in->attach(this, &USBHostHub::rxHandler); - - // get HUB descriptor - host->controlRead( dev, - USB_DEVICE_TO_HOST | USB_REQUEST_TYPE_CLASS, - GET_DESCRIPTOR, - 0x29 << 8, 0, buf, sizeof(HubDescriptor)); - nb_port = buf[2]; - hub_characteristics = buf[3]; - - USB_DBG("Hub has %d port", nb_port); - - for (uint8_t j = 1; j <= nb_port; j++) { - setPortFeature(PORT_POWER_FEATURE, j); - } - wait_ms(buf[5]*2); - - host->interruptRead(dev, int_in, buf, 1, false); - dev_connected = true; - return true; - } - - return false; -} - -void USBHostHub::disconnect() { - init(); -} - -/*virtual*/ void USBHostHub::setVidPid(uint16_t vid, uint16_t pid) -{ - // we don't check VID/PID for MSD driver -} - -/*virtual*/ bool USBHostHub::parseInterface(uint8_t intf_nb, uint8_t intf_class, uint8_t intf_subclass, uint8_t intf_protocol) //Must return true if the interface should be parsed -{ - if ((hub_intf == -1) && - (intf_class == HUB_CLASS) && - (intf_subclass == 0) && - (intf_protocol == 0)) { - hub_intf = intf_nb; - return true; - } - return false; -} - -/*virtual*/ bool USBHostHub::useEndpoint(uint8_t intf_nb, ENDPOINT_TYPE type, ENDPOINT_DIRECTION dir) //Must return true if the endpoint will be used -{ - if (intf_nb == hub_intf) { - if ((type == INTERRUPT_ENDPOINT) && (dir == IN)) { - hub_device_found = true; - return true; - } - } - return false; -} - -void USBHostHub::deviceConnected(USBDeviceConnected * dev) { - device_children[dev->getPort() - 1] = dev; -} - -void USBHostHub::deviceDisconnected(USBDeviceConnected * dev) { - device_children[dev->getPort() - 1] = NULL; -} - -void USBHostHub::hubDisconnected() { - for (uint8_t i = 0; i < MAX_HUB_PORT; i++) { - if (device_children[i] != NULL) { - host->freeDevice(device_children[i]); - } - } -} - -void USBHostHub::rxHandler() { - uint32_t status; - if (int_in) { - if (int_in->getState() == USB_TYPE_IDLE) { - for (int port = 1; port <= nb_port; port++) { - status = getPortStatus(port); - USB_DBG("[hub handler hub: %d] status port %d [hub: %p]: 0x%X", dev->getHub(), port, dev, status); - - // if connection status has changed - if (status & C_PORT_CONNECTION) { - if (status & PORT_CONNECTION) { - USB_DBG("[hub handler hub: %d - port: %d] new device connected", dev->getHub(), port); - host->deviceConnected(dev->getHub() + 1, port, status & PORT_LOW_SPEED, this); - } else { - USB_DBG("[hub handler hub: %d - port: %d] device disconnected", dev->getHub(), port); - host->deviceDisconnected(dev->getHub() + 1, port, this, 0); - } - - clearPortFeature(C_PORT_CONNECTION_FEATURE, port); - } - - if (status & C_PORT_RESET) { - clearPortFeature(C_PORT_RESET_FEATURE, port); - } - - if (status & C_PORT_ENABLE) { - clearPortFeature(C_PORT_ENABLE_FEATURE, port); - } - - if ((status & PORT_OVER_CURRENT)) { - USB_ERR("OVER CURRENT DETECTED\r\n"); - clearPortFeature(PORT_OVER_CURRENT, port); - host->deviceDisconnected(dev->getHub() + 1, port, this, 0); - } - } - } - host->interruptRead(dev, int_in, buf, 1, false); - } -} - -void USBHostHub::portReset(uint8_t port) { - // reset port - uint32_t status; - USB_DBG("reset port %d on hub: %p [this: %p]", port, dev, this) - setPortFeature(PORT_RESET_FEATURE, port); -#if defined(TARGET_RZ_A1H) - Thread::wait(50); // Reset release waiting for Hi-Speed check. -#endif - while(1) { - status = getPortStatus(port); - if (status & (PORT_ENABLE | PORT_RESET)) - break; - if (status & PORT_OVER_CURRENT) { - USB_ERR("OVER CURRENT DETECTED\r\n"); - clearPortFeature(PORT_OVER_CURRENT, port); - host->deviceDisconnected(dev->getHub() + 1, port, this, 0); - break; - } - Thread::wait(10); - } -} - -void USBHostHub::setPortFeature(uint32_t feature, uint8_t port) { - host->controlWrite( dev, - USB_HOST_TO_DEVICE | USB_REQUEST_TYPE_CLASS | USB_RECIPIENT_INTERFACE | USB_RECIPIENT_ENDPOINT, - SET_FEATURE, - feature, - port, - NULL, - 0); -} - -void USBHostHub::clearPortFeature(uint32_t feature, uint8_t port) { - host->controlWrite( dev, - USB_HOST_TO_DEVICE | USB_REQUEST_TYPE_CLASS | USB_RECIPIENT_INTERFACE | USB_RECIPIENT_ENDPOINT, - CLEAR_FEATURE, - feature, - port, - NULL, - 0); -} - -uint32_t USBHostHub::getPortStatus(uint8_t port) { - uint32_t st; - host->controlRead( dev, - USB_DEVICE_TO_HOST | USB_REQUEST_TYPE_CLASS | USB_RECIPIENT_INTERFACE | USB_RECIPIENT_ENDPOINT, - GET_STATUS, - 0, - port, - (uint8_t *)&st, - 4); - return st; -} - -#endif
--- a/USBHostHub/USBHostHub.h Mon Jun 01 11:01:25 2015 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,125 +0,0 @@ -/* mbed USBHost Library - * Copyright (c) 2006-2013 ARM Limited - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef USBHOSTHUB_H -#define USBHOSTHUB_H - -#include "USBHostConf.h" - -#if MAX_HUB_NB - -#include "USBHostTypes.h" -#include "IUSBEnumerator.h" - -class USBHost; -class USBDeviceConnected; -class USBEndpoint; - -/** - * A class to use a USB Hub - */ -class USBHostHub : public IUSBEnumerator { -public: - /** - * Constructor - */ - USBHostHub(); - - /** - * Check if a USB Hub is connected - * - * @return true if a serial device is connected - */ - bool connected(); - - /** - * Try to connect device - * - * @param dev device to connect - * @return true if connection was successful - */ - bool connect(USBDeviceConnected * dev); - - /** - * Automatically called by USBHost when a device - * has been enumerated by usb_thread - * - * @param dev device connected - */ - void deviceConnected(USBDeviceConnected * dev); - - /** - * Automatically called by USBHost when a device - * has been disconnected from this hub - * - * @param dev device disconnected - */ - void deviceDisconnected(USBDeviceConnected * dev); - - /** - * Rest a specific port - * - * @param port port number - */ - void portReset(uint8_t port); - - /* - * Called by USBHost to set the instance of USBHost - * - * @param host host instance - */ - void setHost(USBHost * host); - - /** - * Called by USBhost when a hub has been disconnected - */ - void hubDisconnected(); - -protected: - //From IUSBEnumerator - virtual void setVidPid(uint16_t vid, uint16_t pid); - virtual bool parseInterface(uint8_t intf_nb, uint8_t intf_class, uint8_t intf_subclass, uint8_t intf_protocol); //Must return true if the interface should be parsed - virtual bool useEndpoint(uint8_t intf_nb, ENDPOINT_TYPE type, ENDPOINT_DIRECTION dir); //Must return true if the endpoint will be used - -private: - USBHost * host; - USBDeviceConnected * dev; - bool dev_connected; - USBEndpoint * int_in; - uint8_t nb_port; - uint8_t hub_characteristics; - - void rxHandler(); - - uint8_t buf[sizeof(HubDescriptor)]; - - int hub_intf; - bool hub_device_found; - - void setPortFeature(uint32_t feature, uint8_t port); - void clearPortFeature(uint32_t feature, uint8_t port); - uint32_t getPortStatus(uint8_t port); - - USBDeviceConnected * device_children[MAX_HUB_PORT]; - - void init(); - void disconnect(); - -}; - -#endif - -#endif
--- a/USBHostMIDI/USBHostMIDI.cpp Mon Jun 01 11:01:25 2015 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,362 +0,0 @@ -/* Copyright (c) 2014 mbed.org, MIT License - * - * 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 "USBHostMIDI.h" - -#if USBHOST_MIDI - -#include "dbg.h" - -#define SET_LINE_CODING 0x20 - -USBHostMIDI::USBHostMIDI() { - host = USBHost::getHostInst(); - size_bulk_in = 0; - size_bulk_out = 0; - init(); -} - -void USBHostMIDI::init() { - dev = NULL; - bulk_in = NULL; - bulk_out = NULL; - dev_connected = false; - midi_intf = -1; - midi_device_found = false; - sysExBufferPos = 0; -} - -bool USBHostMIDI::connected() { - return dev_connected; -} - -bool USBHostMIDI::connect() { - if (dev_connected) { - return true; - } - - for (uint8_t i = 0; i < MAX_DEVICE_CONNECTED; i++) { - if ((dev = host->getDevice(i)) != NULL) { - - USB_DBG("Trying to connect MIDI device\r\n"); - - if (host->enumerate(dev, this)) { - break; - } - - if (midi_device_found) { - bulk_in = dev->getEndpoint(midi_intf, BULK_ENDPOINT, IN); - bulk_out = dev->getEndpoint(midi_intf, BULK_ENDPOINT, OUT); - - if (!bulk_in || !bulk_out) { - break; - } - - USB_INFO("New MIDI device: VID:%04x PID:%04x [dev: %p - intf: %d]", dev->getVid(), dev->getPid(), dev, midi_intf); - dev->setName("MIDI", midi_intf); - host->registerDriver(dev, midi_intf, this, &USBHostMIDI::init); - - size_bulk_in = bulk_in->getSize(); - size_bulk_out = bulk_out->getSize(); - - bulk_in->attach(this, &USBHostMIDI::rxHandler); - - host->bulkRead(dev, bulk_in, buf, size_bulk_in, false); - dev_connected = true; - return true; - } - } - } - - init(); - return false; -} - -void USBHostMIDI::rxHandler() { - uint8_t *midi; - if (bulk_in) { - int length = bulk_in->getLengthTransferred(); - if (bulk_in->getState() == USB_TYPE_IDLE || bulk_in->getState() == USB_TYPE_FREE) { - // MIDI event handling - for (int i = 0; i < length; i += 4) { - if (i + 4 > length) { - // length shortage, ignored. - break; - } - - // read each four bytes - midi = &buf[i]; - // process MIDI message - // switch by code index number - switch (midi[0] & 0xf) { - case 0: // miscellaneous function codes - miscellaneousFunctionCode(midi[1], midi[2], midi[3]); - break; - case 1: // cable events - cableEvent(midi[1], midi[2], midi[3]); - break; - case 2: // two bytes system common messages - systemCommonTwoBytes(midi[1], midi[2]); - break; - case 3: // three bytes system common messages - systemCommonThreeBytes(midi[1], midi[2], midi[3]); - break; - case 4: // SysEx starts or continues - sysExBuffer[sysExBufferPos++] = midi[1]; - if (sysExBufferPos >= 64) { - systemExclusive(sysExBuffer, sysExBufferPos, true); - sysExBufferPos = 0; - } - sysExBuffer[sysExBufferPos++] = midi[2]; - if (sysExBufferPos >= 64) { - systemExclusive(sysExBuffer, sysExBufferPos, true); - sysExBufferPos = 0; - } - sysExBuffer[sysExBufferPos++] = midi[3]; - // SysEx continues. don't send - break; - case 5: // SysEx ends with single byte - sysExBuffer[sysExBufferPos++] = midi[1]; - systemExclusive(sysExBuffer, sysExBufferPos, false); - sysExBufferPos = 0; - break; - case 6: // SysEx ends with two bytes - sysExBuffer[sysExBufferPos++] = midi[1]; - if (sysExBufferPos >= 64) { - systemExclusive(sysExBuffer, sysExBufferPos, true); - sysExBufferPos = 0; - } - sysExBuffer[sysExBufferPos++] = midi[2]; - systemExclusive(sysExBuffer, sysExBufferPos, false); - sysExBufferPos = 0; - break; - case 7: // SysEx ends with three bytes - sysExBuffer[sysExBufferPos++] = midi[1]; - if (sysExBufferPos >= 64) { - systemExclusive(sysExBuffer, sysExBufferPos, true); - sysExBufferPos = 0; - } - sysExBuffer[sysExBufferPos++] = midi[2]; - if (sysExBufferPos >= 64) { - systemExclusive(sysExBuffer, sysExBufferPos, true); - sysExBufferPos = 0; - } - sysExBuffer[sysExBufferPos++] = midi[3]; - systemExclusive(sysExBuffer, sysExBufferPos, false); - sysExBufferPos = 0; - break; - case 8: - noteOff(midi[1] & 0xf, midi[2], midi[3]); - break; - case 9: - if (midi[3]) { - noteOn(midi[1] & 0xf, midi[2], midi[3]); - } else { - noteOff(midi[1] & 0xf, midi[2], midi[3]); - } - break; - case 10: - polyKeyPress(midi[1] & 0xf, midi[2], midi[3]); - break; - case 11: - controlChange(midi[1] & 0xf, midi[2], midi[3]); - break; - case 12: - programChange(midi[1] & 0xf, midi[2]); - break; - case 13: - channelPressure(midi[1] & 0xf, midi[2]); - break; - case 14: - pitchBend(midi[1] & 0xf, midi[2] | (midi[3] << 7)); - break; - case 15: - singleByte(midi[1]); - break; - } - } - - // read another message - host->bulkRead(dev, bulk_in, buf, size_bulk_in, false); - } - } -} - -bool USBHostMIDI::sendMidiBuffer(uint8_t data0, uint8_t data1, uint8_t data2, uint8_t data3) { - if (bulk_out) { - uint8_t midi[4]; - - midi[0] = data0; - midi[1] = data1; - midi[2] = data2; - midi[3] = data3; - if (host->bulkWrite(dev, bulk_out, (uint8_t *)midi, 4) == USB_TYPE_OK) { - return true; - } - } - return false; -} - -bool USBHostMIDI::sendMiscellaneousFunctionCode(uint8_t data1, uint8_t data2, uint8_t data3) { - return sendMidiBuffer(0, data1, data2, data3); -} - -bool USBHostMIDI::sendCableEvent(uint8_t data1, uint8_t data2, uint8_t data3) { - return sendMidiBuffer(1, data1, data2, data3); -} - -bool USBHostMIDI::sendSystemCommmonTwoBytes(uint8_t data1, uint8_t data2) { - return sendMidiBuffer(2, data1, data2, 0); -} - -bool USBHostMIDI::sendSystemCommmonThreeBytes(uint8_t data1, uint8_t data2, uint8_t data3) { - return sendMidiBuffer(3, data1, data2, 0); -} - -bool USBHostMIDI::sendSystemExclusive(uint8_t *buffer, int length) { - uint8_t midi[64]; - int midiLength; - int midiPos; - if (bulk_out) { - for (int i = 0; i < length; i += 48) { - if (i + 48 >= length) { - // contains last data - midiLength = (((length - i) + 2) / 3) * 4; - for (int pos = i; pos < length; pos += 3) { - midiPos = (pos + 2) / 3 * 4; - if (pos + 3 >= length) { - // last data - switch (pos % 3) { - case 0: - midi[midiPos ] = 7; - midi[midiPos + 1] = buffer[pos ]; - midi[midiPos + 2] = buffer[pos + 1]; - midi[midiPos + 3] = buffer[pos + 2]; - break; - case 1: - midi[midiPos ] = 5; - midi[midiPos + 1] = buffer[pos ]; - midi[midiPos + 2] = 0; - midi[midiPos + 3] = 0; - break; - case 2: - midi[midiPos ] = 6; - midi[midiPos + 1] = buffer[pos ]; - midi[midiPos + 2] = buffer[pos + 1]; - midi[midiPos + 3] = 0; - break; - } - } else { - // has more data - midi[midiPos ] = 4; - midi[midiPos + 1] = buffer[pos ]; - midi[midiPos + 2] = buffer[pos + 1]; - midi[midiPos + 3] = buffer[pos + 2]; - } - } - } else { - // has more data - midiLength = 64; - for (int pos = i; pos < length; pos += 3) { - midiPos = (pos + 2) / 3 * 4; - midi[midiPos ] = 4; - midi[midiPos + 1] = buffer[pos ]; - midi[midiPos + 2] = buffer[pos + 1]; - midi[midiPos + 3] = buffer[pos + 2]; - } - } - - if (host->bulkWrite(dev, bulk_out, (uint8_t *)midi, midiLength) != USB_TYPE_OK) { - return false; - } - } - return true; - } - return false; -} - -bool USBHostMIDI::sendNoteOff(uint8_t channel, uint8_t note, uint8_t velocity) { - return sendMidiBuffer(8, channel & 0xf | 0x80, note & 0x7f, velocity & 0x7f); -} - -bool USBHostMIDI::sendNoteOn(uint8_t channel, uint8_t note, uint8_t velocity) { - return sendMidiBuffer(9, channel & 0xf | 0x90, note & 0x7f, velocity & 0x7f); -} - -bool USBHostMIDI::sendPolyKeyPress(uint8_t channel, uint8_t note, uint8_t pressure) { - return sendMidiBuffer(10, channel & 0xf | 0xa0, note & 0x7f, pressure & 0x7f); -} - -bool USBHostMIDI::sendControlChange(uint8_t channel, uint8_t key, uint8_t value) { - return sendMidiBuffer(11, channel & 0xf | 0xb0, key & 0x7f, value & 0x7f); -} - -bool USBHostMIDI::sendProgramChange(uint8_t channel, uint8_t program) { - return sendMidiBuffer(12, channel & 0xf | 0xc0, program & 0x7f, 0); -} - -bool USBHostMIDI::sendChannelPressure(uint8_t channel, uint8_t pressure) { - return sendMidiBuffer(13, channel & 0xf | 0xd0, pressure & 0x7f, 0); -} - -bool USBHostMIDI::sendPitchBend(uint8_t channel, uint16_t value) { - return sendMidiBuffer(14, channel & 0xf | 0xe0, value & 0x7f, (value >> 7) & 0x7f); -} - -bool USBHostMIDI::sendSingleByte(uint8_t data) { - return sendMidiBuffer(15, data, 0, 0); -} - -/*virtual*/ void USBHostMIDI::setVidPid(uint16_t vid, uint16_t pid) -{ - // we don't check VID/PID for this driver -} - -/*virtual*/ bool USBHostMIDI::parseInterface(uint8_t intf_nb, uint8_t intf_class, uint8_t intf_subclass, uint8_t intf_protocol) //Must return true if the interface should be parsed -{ - // USB MIDI class/subclass - if ((midi_intf == -1) && - (intf_class == AUDIO_CLASS) && - (intf_subclass == 0x03)) { - midi_intf = intf_nb; - return true; - } - - // vendor specific device - if ((midi_intf == -1) && - (intf_class == 0xff) && - (intf_subclass == 0x03)) { - midi_intf = intf_nb; - return true; - } - - return false; -} - -/*virtual*/ bool USBHostMIDI::useEndpoint(uint8_t intf_nb, ENDPOINT_TYPE type, ENDPOINT_DIRECTION dir) //Must return true if the endpoint will be used -{ - if (intf_nb == midi_intf) { - if (type == BULK_ENDPOINT) { - midi_device_found = true; - return true; - } - } - return false; -} - -#endif
--- a/USBHostMIDI/USBHostMIDI.h Mon Jun 01 11:01:25 2015 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,353 +0,0 @@ -/* Copyright (c) 2014 mbed.org, MIT License - * - * 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 USBHOSTMIDI_H -#define USBHOSTMIDI_H - -#include "USBHostConf.h" - -#if USBHOST_MIDI - -#include "USBHost.h" - -/** - * A class to communicate a USB MIDI device - */ -class USBHostMIDI : public IUSBEnumerator { -public: - /** - * Constructor - */ - USBHostMIDI(); - - /** - * Check if a USB MIDI device is connected - * - * @returns true if a midi device is connected - */ - bool connected(); - - /** - * Try to connect a midi device - * - * @return true if connection was successful - */ - bool connect(); - - /** - * Attach a callback called when miscellaneous function code is received - * - * @param ptr function pointer - * prototype: void onMiscellaneousFunctionCode(uint8_t data1, uint8_t data2, uint8_t data3); - */ - inline void attachMiscellaneousFunctionCode(void (*fn)(uint8_t, uint8_t, uint8_t)) { - miscellaneousFunctionCode = fn; - } - - /** - * Attach a callback called when cable event is received - * - * @param ptr function pointer - * prototype: void onCableEvent(uint8_t data1, uint8_t data2, uint8_t data3); - */ - inline void attachCableEvent(void (*fn)(uint8_t, uint8_t, uint8_t)) { - cableEvent = fn; - } - - /** - * Attach a callback called when system exclusive is received - * - * @param ptr function pointer - * prototype: void onSystemCommonTwoBytes(uint8_t data1, uint8_t data2); - */ - inline void attachSystemCommonTwoBytes(void (*fn)(uint8_t, uint8_t)) { - systemCommonTwoBytes = fn; - } - - /** - * Attach a callback called when system exclusive is received - * - * @param ptr function pointer - * prototype: void onSystemCommonThreeBytes(uint8_t data1, uint8_t data2, uint8_t data3); - */ - inline void attachSystemCommonThreeBytes(void (*fn)(uint8_t, uint8_t, uint8_t)) { - systemCommonThreeBytes = fn; - } - - /** - * Attach a callback called when system exclusive is received - * - * @param ptr function pointer - * prototype: void onSystemExclusive(uint8_t *data, uint16_t length, bool hasNextData); - */ - inline void attachSystemExclusive(void (*fn)(uint8_t *, uint16_t, bool)) { - systemExclusive = fn; - } - - /** - * Attach a callback called when note on is received - * - * @param ptr function pointer - * prototype: void onNoteOn(uint8_t channel, uint8_t note, uint8_t velocity); - */ - inline void attachNoteOn(void (*fn)(uint8_t, uint8_t, uint8_t)) { - noteOn = fn; - } - - /** - * Attach a callback called when note off is received - * - * @param ptr function pointer - * prototype: void onNoteOff(uint8_t channel, uint8_t note, uint8_t velocity); - */ - inline void attachNoteOff(void (*fn)(uint8_t, uint8_t, uint8_t)) { - noteOff = fn; - } - - /** - * Attach a callback called when poly keypress is received - * - * @param ptr function pointer - * prototype: void onPolyKeyPress(uint8_t channel, uint8_t note, uint8_t pressure); - */ - inline void attachPolyKeyPress(void (*fn)(uint8_t, uint8_t, uint8_t)) { - polyKeyPress = fn; - } - - /** - * Attach a callback called when control change is received - * - * @param ptr function pointer - * prototype: void onControlChange(uint8_t channel, uint8_t key, uint8_t value); - */ - inline void attachControlChange(void (*fn)(uint8_t, uint8_t, uint8_t)) { - controlChange = fn; - } - - /** - * Attach a callback called when program change is received - * - * @param ptr function pointer - * prototype: void onProgramChange(uint8_t channel, uint8_t program); - */ - inline void attachProgramChange(void (*fn)(uint8_t, uint8_t)) { - programChange = fn; - } - - /** - * Attach a callback called when channel pressure is received - * - * @param ptr function pointer - * prototype: void onChannelPressure(uint8_t channel, uint8_t pressure); - */ - inline void attachChannelPressure(void (*fn)(uint8_t, uint8_t)) { - channelPressure = fn; - } - - /** - * Attach a callback called when pitch bend is received - * - * @param ptr function pointer - * prototype: void onPitchBend(uint8_t channel, uint16_t value); - */ - inline void attachPitchBend(void (*fn)(uint8_t, uint16_t)) { - pitchBend = fn; - } - - /** - * Attach a callback called when single byte is received - * - * @param ptr function pointer - * prototype: void onSingleByte(uint8_t value); - */ - inline void attachSingleByte(void (*fn)(uint8_t)) { - singleByte = fn; - } - - /** - * Send a cable event with 3 bytes event - * - * @param data1 0-255 - * @param data2 0-255 - * @param data3 0-255 - * @return true if message sent successfully - */ - bool sendMiscellaneousFunctionCode(uint8_t data1, uint8_t data2, uint8_t data3); - - /** - * Send a cable event with 3 bytes event - * - * @param data1 0-255 - * @param data2 0-255 - * @param data3 0-255 - * @return true if message sent successfully - */ - bool sendCableEvent(uint8_t data1, uint8_t data2, uint8_t data3); - - /** - * Send a system common message with 2 bytes event - * - * @param data1 0-255 - * @param data2 0-255 - * @return true if message sent successfully - */ - bool sendSystemCommmonTwoBytes(uint8_t data1, uint8_t data2); - - /** - * Send a system common message with 3 bytes event - * - * @param data1 0-255 - * @param data2 0-255 - * @param data3 0-255 - * @return true if message sent successfully - */ - bool sendSystemCommmonThreeBytes(uint8_t data1, uint8_t data2, uint8_t data3); - - /** - * Send a system exclusive event - * - * @param buffer, starts with 0xF0, and end with 0xf7 - * @param length - * @return true if message sent successfully - */ - bool sendSystemExclusive(uint8_t *buffer, int length); - - /** - * Send a note off event - * - * @param channel 0-15 - * @param note 0-127 - * @param velocity 0-127 - * @return true if message sent successfully - */ - bool sendNoteOff(uint8_t channel, uint8_t note, uint8_t velocity); - - /** - * Send a note on event - * - * @param channel 0-15 - * @param note 0-127 - * @param velocity 0-127 (0 means note off) - * @return true if message sent successfully - */ - bool sendNoteOn(uint8_t channel, uint8_t note, uint8_t velocity); - - /** - * Send a poly keypress event - * - * @param channel 0-15 - * @param note 0-127 - * @param pressure 0-127 - * @return true if message sent successfully - */ - bool sendPolyKeyPress(uint8_t channel, uint8_t note, uint8_t pressure); - - /** - * Send a control change event - * - * @param channel 0-15 - * @param key 0-127 - * @param value 0-127 - * @return true if message sent successfully - */ - bool sendControlChange(uint8_t channel, uint8_t key, uint8_t value); - - /** - * Send a program change event - * - * @param channel 0-15 - * @param program 0-127 - * @return true if message sent successfully - */ - bool sendProgramChange(uint8_t channel, uint8_t program); - - /** - * Send a channel pressure event - * - * @param channel 0-15 - * @param pressure 0-127 - * @return true if message sent successfully - */ - bool sendChannelPressure(uint8_t channel, uint8_t pressure); - - /** - * Send a control change event - * - * @param channel 0-15 - * @param key 0(lower)-8191(center)-16383(higher) - * @return true if message sent successfully - */ - bool sendPitchBend(uint8_t channel, uint16_t value); - - /** - * Send a single byte event - * - * @param data 0-255 - * @return true if message sent successfully - */ - bool sendSingleByte(uint8_t data); - -protected: - //From IUSBEnumerator - virtual void setVidPid(uint16_t vid, uint16_t pid); - virtual bool parseInterface(uint8_t intf_nb, uint8_t intf_class, uint8_t intf_subclass, uint8_t intf_protocol); //Must return true if the interface should be parsed - virtual bool useEndpoint(uint8_t intf_nb, ENDPOINT_TYPE type, ENDPOINT_DIRECTION dir); //Must return true if the endpoint will be used - -private: - USBHost * host; - USBDeviceConnected * dev; - USBEndpoint * bulk_in; - USBEndpoint * bulk_out; - uint32_t size_bulk_in; - uint32_t size_bulk_out; - - bool dev_connected; - - void init(); - - uint8_t buf[64]; - - void rxHandler(); - - uint16_t sysExBufferPos; - uint8_t sysExBuffer[64]; - - void (*miscellaneousFunctionCode)(uint8_t, uint8_t, uint8_t); - void (*cableEvent)(uint8_t, uint8_t, uint8_t); - void (*systemCommonTwoBytes)(uint8_t, uint8_t); - void (*systemCommonThreeBytes)(uint8_t, uint8_t, uint8_t); - void (*systemExclusive)(uint8_t *, uint16_t, bool); - void (*noteOff)(uint8_t, uint8_t, uint8_t); - void (*noteOn)(uint8_t, uint8_t, uint8_t); - void (*polyKeyPress)(uint8_t, uint8_t, uint8_t); - void (*controlChange)(uint8_t, uint8_t, uint8_t); - void (*programChange)(uint8_t, uint8_t); - void (*channelPressure)(uint8_t, uint8_t); - void (*pitchBend)(uint8_t, uint16_t); - void (*singleByte)(uint8_t); - - bool sendMidiBuffer(uint8_t data0, uint8_t data1, uint8_t data2, uint8_t data3); - - int midi_intf; - bool midi_device_found; - -}; - -#endif /* USBHOST_MIDI */ - -#endif /* USBHOSTMIDI_H */
--- a/USBHostMSD/USBHostMSD.cpp Mon Jun 01 11:01:25 2015 +0100 +++ b/USBHostMSD/USBHostMSD.cpp Fri Oct 07 10:37:00 2016 +0000 @@ -29,6 +29,8 @@ #define GET_MAX_LUN (0xFE) #define BO_MASS_STORAGE_RESET (0xFF) +extern DigitalOut myled; + USBHostMSD::USBHostMSD(const char * rootdir) : FATFileSystem(rootdir) { host = USBHost::getHostInst();