BTstack for EA LPC4088 QSB example program
Dependencies: LPC4088-USBHost mbed
Fork of KL46Z-BTstack_example by
The usage is the same as KL46Z-BTstack_example.
使い方はKL46Z-BTstack_exampleと同じです。
Revision 6:d946cb517371, committed 2014-04-25
- Comitter:
- va009039
- Date:
- Fri Apr 25 05:34:24 2014 +0000
- Parent:
- 5:56897116e6b0
- Commit message:
- run on LPC4088
Changed in this revision
--- a/KL46Z-USBHost.lib Wed Feb 05 14:56:10 2014 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,1 +0,0 @@ -http://mbed.org/users/va009039/code/KL46Z-USBHost/#40c7f6788902
--- a/KL46Z-USBHostBTstack/USBHostBTstack.cpp Wed Feb 05 14:56:10 2014 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,187 +0,0 @@ -// USBHostBTstack.cpp -#include "USBHostBTstack.h" - -//#define BTSTACK_DEBUG - -#ifdef BTSTACK_DEBUG -#define BT_DBG(x, ...) std::printf("[%s:%d]"x"\r\n", __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__); -#define BT_DBG_BYTES(STR,BUF,LEN) _debug_bytes(__PRETTY_FUNCTION__,__LINE__,STR,BUF,LEN) -#else -#define BT_DBG(...) while(0); -#define BT_DBG_BYTES(S,BUF,LEN) while(0); -#endif -#define USB_INFO(...) do{fprintf(stderr,__VA_ARGS__);fprintf(stderr,"\r\n");}while(0); - -#define HCI_COMMAND_DATA_PACKET 0x01 -#define HCI_ACL_DATA_PACKET 0x02 -#define HCI_SCO_DATA_PACKET 0x03 -#define HCI_EVENT_PACKET 0x04 - -USBHostBTstack::USBHostBTstack() -{ - host = USBHost::getHostInst(); - init(); - m_pCb = NULL; -} - -void USBHostBTstack::init() -{ - BT_DBG(""); - dev = NULL; - int_in = NULL; - bulk_in = NULL; - bulk_out = NULL; - btstack_intf = -1; - btstack_device_found = false; - dev_connected = false; - ep_int_in = false; - ep_bulk_in = false; - ep_bulk_out = false; -} - -bool USBHostBTstack::connected() -{ - return dev_connected; -} - -bool USBHostBTstack::connect() -{ - if (dev_connected) { - return true; - } - - for (uint8_t i = 0; i < MAX_DEVICE_CONNECTED; i++) { - if ((dev = host->getDevice(i)) != NULL) { - - BT_DBG("Trying to connect BTstack device\r\n"); - - if(host->enumerate(dev, this)) { - break; - } - if (btstack_device_found) { - int_in = dev->getEndpoint(btstack_intf, INTERRUPT_ENDPOINT, IN); - bulk_in = dev->getEndpoint(btstack_intf, BULK_ENDPOINT, IN); - bulk_out = dev->getEndpoint(btstack_intf, BULK_ENDPOINT, OUT); - if (!int_in || !bulk_in || !bulk_out) { - continue; - } - USB_INFO("New BTstack device: VID:%04x PID:%04x [dev: %p - intf: %d]", dev->getVid(), dev->getPid(), dev, btstack_intf); - //dev->setName("BTstack", btstack_intf); - //host->registerDriver(dev, btstack_intf, this, &USBHostBTstack::init); - - //int_in->attach(this, &USBHostBTstack::int_rxHandler); - //host->interruptRead(dev, int_in, int_report, int_in->getSize(), false); - - //bulk_in->attach(this, &USBHostBTstack::bulk_rxHandler); - //host->bulkRead(dev, bulk_in, bulk_report, bulk_in->getSize(), false); - - dev_connected = true; - return true; - } - } - } - init(); - return false; -} - -/*virtual*/ void USBHostBTstack::setVidPid(uint16_t vid, uint16_t pid) -{ - BT_DBG("vid:%04x,pid:%04x", vid, pid); -} - -/*virtual*/ bool USBHostBTstack::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 -{ - BT_DBG("intf_nb=%d,intf_class=%02X,intf_subclass=%d,intf_protocol=%d", intf_nb, intf_class, intf_subclass, intf_protocol); - if ((btstack_intf == -1) && intf_class == 0xe0) { - btstack_intf = intf_nb; - return true; - } - return false; -} - -/*virtual*/ bool USBHostBTstack::useEndpoint(uint8_t intf_nb, ENDPOINT_TYPE type, ENDPOINT_DIRECTION dir) //Must return true if the endpoint will be used -{ - BT_DBG("intf_nb:%d,type:%d,dir:%d",intf_nb, type, dir); - if (intf_nb == btstack_intf) { - if (ep_int_in == false && type == INTERRUPT_ENDPOINT && dir == IN) { - ep_int_in = true; - } else if (ep_bulk_in == false && type == BULK_ENDPOINT && dir == IN) { - ep_bulk_in = true; - } else if (ep_bulk_out == false && type == BULK_ENDPOINT && dir == OUT) { - ep_bulk_out = true; - } else { - return false; - } - if (ep_int_in && ep_bulk_in && ep_bulk_out) { - btstack_device_found = true; - } - return true; - } - return false; -} - -int USBHostBTstack::open() -{ - BT_DBG("%p", this); - if (!connect()) { - error("Bluetooth not found.\n"); - } - return 0; -} - -int USBHostBTstack::send_packet(uint8_t packet_type, uint8_t* packet, int size) -{ - USB_TYPE res; - switch(packet_type){ - case HCI_COMMAND_DATA_PACKET: - BT_DBG_BYTES("HCI_CMD:", packet, size); - res = host->controlWrite(dev, - USB_HOST_TO_DEVICE | USB_REQUEST_TYPE_CLASS | USB_RECIPIENT_DEVICE, - 0, 0, 0, packet, size); - TEST_ASSERT(res == USB_TYPE_OK); - break; - case HCI_ACL_DATA_PACKET: - BT_DBG_BYTES("ACL_SND:", packet, size); - res = host->bulkWrite(dev, bulk_out, packet, size); - TEST_ASSERT(res == USB_TYPE_OK); - break; - default: - TEST_ASSERT(0); - } - return 0; -} - -void USBHostBTstack::register_packet_handler(void (*pMethod)(uint8_t, uint8_t*, uint16_t) ) -{ - BT_DBG("pMethod: %p", pMethod); - m_pCb = pMethod; -} - -void USBHostBTstack::poll() -{ - int result = host->interruptReadNB(int_in, int_report, sizeof(int_report)); - if (result >= 0) { - int len = int_in->getLengthTransferred(); - BT_DBG_BYTES("HCI_EVT:", int_report, len); - if (m_pCb) { - m_pCb(HCI_EVENT_PACKET, int_report, len); - } - } - result = host->bulkReadNB(bulk_in, bulk_report, sizeof(bulk_report)); - if (result >= 0) { - int len = bulk_in->getLengthTransferred(); - BT_DBG_BYTES("HCI_ACL_RECV:", bulk_report, len); - if (m_pCb) { - m_pCb(HCI_ACL_DATA_PACKET, bulk_report, len); - } - } -} - -void _debug_bytes(const char* pretty, int line, const char* s, uint8_t* buf, int len) -{ - printf("[%s:%d]\n%s", pretty, line, s); - for(int i = 0; i < len; i++) { - printf(" %02x", buf[i]); - } - printf("\n"); -}
--- a/KL46Z-USBHostBTstack/USBHostBTstack.h Wed Feb 05 14:56:10 2014 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,62 +0,0 @@ -#include "USBHostConf.h" -#include "USBHost.h" -#pragma once - -#define TEST_ASSERT(A) while(!(A)){fprintf(stderr,"\n\n%s@%d %s ASSERT!\n\n",__PRETTY_FUNCTION__,__LINE__,#A);exit(1);}; - -/** - * A class to communicate a BTstack - */ -class USBHostBTstack : public IUSBEnumerator { -public: - /** - * Constructor - * - */ - USBHostBTstack(); - - /** - * Check if a BTstack device is connected - * - * @return true if a BTstack device is connected - */ - bool connected(); - - /** - * Try to connect to a BTstack device - * - * @return true if connection was successful - */ - bool connect(); - - int open(); - int send_packet(uint8_t packet_type, uint8_t* packet, int size); - void register_packet_handler( void (*pMethod)(uint8_t, uint8_t*, uint16_t)); - void poll(); - -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; - uint8_t int_report[64]; - uint8_t bulk_report[64]; - USBEndpoint * int_in; - USBEndpoint * bulk_in; - USBEndpoint * bulk_out; - bool ep_int_in; - bool ep_bulk_in; - bool ep_bulk_out; - - bool btstack_device_found; - int btstack_intf; - void (*m_pCb)(uint8_t, uint8_t*, uint16_t); - void init(); -}; - -void _debug_bytes(const char* pretty, int line, const char* s, uint8_t* buf, int len);
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/LPC4088-USBHost.lib Fri Apr 25 05:34:24 2014 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/va009039/code/LPC4088-USBHost/#148fca6fd246
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/LPC4088-USBHostBTstack/USBHostBTstack.cpp Fri Apr 25 05:34:24 2014 +0000 @@ -0,0 +1,187 @@ +// USBHostBTstack.cpp +#include "USBHostBTstack.h" + +//#define BTSTACK_DEBUG + +#ifdef BTSTACK_DEBUG +#define BT_DBG(x, ...) std::printf("[%s:%d]"x"\r\n", __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__); +#define BT_DBG_BYTES(STR,BUF,LEN) _debug_bytes(__PRETTY_FUNCTION__,__LINE__,STR,BUF,LEN) +#else +#define BT_DBG(...) while(0); +#define BT_DBG_BYTES(S,BUF,LEN) while(0); +#endif +#define USB_INFO(...) do{fprintf(stderr,__VA_ARGS__);fprintf(stderr,"\r\n");}while(0); + +#define HCI_COMMAND_DATA_PACKET 0x01 +#define HCI_ACL_DATA_PACKET 0x02 +#define HCI_SCO_DATA_PACKET 0x03 +#define HCI_EVENT_PACKET 0x04 + +USBHostBTstack::USBHostBTstack() +{ + host = USBHost::getHostInst(); + init(); + m_pCb = NULL; +} + +void USBHostBTstack::init() +{ + BT_DBG(""); + dev = NULL; + int_in = NULL; + bulk_in = NULL; + bulk_out = NULL; + btstack_intf = -1; + btstack_device_found = false; + dev_connected = false; + ep_int_in = false; + ep_bulk_in = false; + ep_bulk_out = false; +} + +bool USBHostBTstack::connected() +{ + return dev_connected; +} + +bool USBHostBTstack::connect() +{ + if (dev_connected) { + return true; + } + + for (uint8_t i = 0; i < MAX_DEVICE_CONNECTED; i++) { + if ((dev = host->getDevice(i)) != NULL) { + + BT_DBG("Trying to connect BTstack device\r\n"); + + if(host->enumerate(dev, this)) { + break; + } + if (btstack_device_found) { + int_in = dev->getEndpoint(btstack_intf, INTERRUPT_ENDPOINT, IN); + bulk_in = dev->getEndpoint(btstack_intf, BULK_ENDPOINT, IN); + bulk_out = dev->getEndpoint(btstack_intf, BULK_ENDPOINT, OUT); + if (!int_in || !bulk_in || !bulk_out) { + continue; + } + USB_INFO("New BTstack device: VID:%04x PID:%04x [dev: %p - intf: %d]", dev->getVid(), dev->getPid(), dev, btstack_intf); + //dev->setName("BTstack", btstack_intf); + //host->registerDriver(dev, btstack_intf, this, &USBHostBTstack::init); + + //int_in->attach(this, &USBHostBTstack::int_rxHandler); + //host->interruptRead(dev, int_in, int_report, int_in->getSize(), false); + + //bulk_in->attach(this, &USBHostBTstack::bulk_rxHandler); + //host->bulkRead(dev, bulk_in, bulk_report, bulk_in->getSize(), false); + + dev_connected = true; + return true; + } + } + } + init(); + return false; +} + +/*virtual*/ void USBHostBTstack::setVidPid(uint16_t vid, uint16_t pid) +{ + BT_DBG("vid:%04x,pid:%04x", vid, pid); +} + +/*virtual*/ bool USBHostBTstack::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 +{ + BT_DBG("intf_nb=%d,intf_class=%02X,intf_subclass=%d,intf_protocol=%d", intf_nb, intf_class, intf_subclass, intf_protocol); + if ((btstack_intf == -1) && intf_class == 0xe0) { + btstack_intf = intf_nb; + return true; + } + return false; +} + +/*virtual*/ bool USBHostBTstack::useEndpoint(uint8_t intf_nb, ENDPOINT_TYPE type, ENDPOINT_DIRECTION dir) //Must return true if the endpoint will be used +{ + BT_DBG("intf_nb:%d,type:%d,dir:%d",intf_nb, type, dir); + if (intf_nb == btstack_intf) { + if (ep_int_in == false && type == INTERRUPT_ENDPOINT && dir == IN) { + ep_int_in = true; + } else if (ep_bulk_in == false && type == BULK_ENDPOINT && dir == IN) { + ep_bulk_in = true; + } else if (ep_bulk_out == false && type == BULK_ENDPOINT && dir == OUT) { + ep_bulk_out = true; + } else { + return false; + } + if (ep_int_in && ep_bulk_in && ep_bulk_out) { + btstack_device_found = true; + } + return true; + } + return false; +} + +int USBHostBTstack::open() +{ + BT_DBG("%p", this); + if (!connect()) { + error("Bluetooth not found.\n"); + } + return 0; +} + +int USBHostBTstack::send_packet(uint8_t packet_type, uint8_t* packet, int size) +{ + USB_TYPE res; + switch(packet_type){ + case HCI_COMMAND_DATA_PACKET: + BT_DBG_BYTES("HCI_CMD:", packet, size); + res = host->controlWrite(dev, + USB_HOST_TO_DEVICE | USB_REQUEST_TYPE_CLASS | USB_RECIPIENT_DEVICE, + 0, 0, 0, packet, size); + TEST_ASSERT(res == USB_TYPE_OK); + break; + case HCI_ACL_DATA_PACKET: + BT_DBG_BYTES("ACL_SND:", packet, size); + res = host->bulkWrite(dev, bulk_out, packet, size); + TEST_ASSERT(res == USB_TYPE_OK); + break; + default: + TEST_ASSERT(0); + } + return 0; +} + +void USBHostBTstack::register_packet_handler(void (*pMethod)(uint8_t, uint8_t*, uint16_t) ) +{ + BT_DBG("pMethod: %p", pMethod); + m_pCb = pMethod; +} + +void USBHostBTstack::poll() +{ + int result = host->interruptReadNB(int_in, int_report, sizeof(int_report)); + if (result >= 0) { + int len = int_in->getLengthTransferred(); + BT_DBG_BYTES("HCI_EVT:", int_report, len); + if (m_pCb) { + m_pCb(HCI_EVENT_PACKET, int_report, len); + } + } + result = host->bulkReadNB(bulk_in, bulk_report, sizeof(bulk_report)); + if (result >= 0) { + int len = bulk_in->getLengthTransferred(); + BT_DBG_BYTES("HCI_ACL_RECV:", bulk_report, len); + if (m_pCb) { + m_pCb(HCI_ACL_DATA_PACKET, bulk_report, len); + } + } +} + +void _debug_bytes(const char* pretty, int line, const char* s, uint8_t* buf, int len) +{ + printf("[%s:%d]\n%s", pretty, line, s); + for(int i = 0; i < len; i++) { + printf(" %02x", buf[i]); + } + printf("\n"); +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/LPC4088-USBHostBTstack/USBHostBTstack.h Fri Apr 25 05:34:24 2014 +0000 @@ -0,0 +1,62 @@ +#include "USBHostConf.h" +#include "USBHost.h" +#pragma once + +#define TEST_ASSERT(A) while(!(A)){fprintf(stderr,"\n\n%s@%d %s ASSERT!\n\n",__PRETTY_FUNCTION__,__LINE__,#A);exit(1);}; + +/** + * A class to communicate a BTstack + */ +class USBHostBTstack : public IUSBEnumerator { +public: + /** + * Constructor + * + */ + USBHostBTstack(); + + /** + * Check if a BTstack device is connected + * + * @return true if a BTstack device is connected + */ + bool connected(); + + /** + * Try to connect to a BTstack device + * + * @return true if connection was successful + */ + bool connect(); + + int open(); + int send_packet(uint8_t packet_type, uint8_t* packet, int size); + void register_packet_handler( void (*pMethod)(uint8_t, uint8_t*, uint16_t)); + void poll(); + +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; + uint8_t int_report[64]; + uint8_t bulk_report[64]; + USBEndpoint * int_in; + USBEndpoint * bulk_in; + USBEndpoint * bulk_out; + bool ep_int_in; + bool ep_bulk_in; + bool ep_bulk_out; + + bool btstack_device_found; + int btstack_intf; + void (*m_pCb)(uint8_t, uint8_t*, uint16_t); + void init(); +}; + +void _debug_bytes(const char* pretty, int line, const char* s, uint8_t* buf, int len);
--- a/main.cpp Wed Feb 05 14:56:10 2014 +0000 +++ b/main.cpp Fri Apr 25 05:34:24 2014 +0000 @@ -8,11 +8,14 @@ Serial pc(USBTX, USBRX); DigitalOut led1(LED1), led2(LED2); -#if defined(TARGET_KL46Z)||defined(TARGET_KL25Z) +#if defined(TARGET_LPC4088)||defined(TARGET_LPC1768) +#define LED_ON 1 +#define LED_OFF 0 +#elif defined(TARGET_KL46Z)||defined(TARGET_KL25Z) #define LED_ON 0 #define LED_OFF 1 #else -#error +#error target error #endif #define INQUIRY_INTERVAL 15
--- a/mbed.bld Wed Feb 05 14:56:10 2014 +0000 +++ b/mbed.bld Fri Apr 25 05:34:24 2014 +0000 @@ -1,1 +1,1 @@ -http://mbed.org/users/mbed_official/code/mbed/builds/824293ae5e43 \ No newline at end of file +http://mbed.org/users/mbed_official/code/mbed/builds/6473597d706e \ No newline at end of file