PIC24FJ64GB002 with bluetooth USB dongle

PIC24FJ64GB002 with bluetooth USB dongle

PIC24FJ64GB002 with bluetooth USB dongle_第1张图片

PIC24FJ64GB002 with bluetooth USB dongle_第2张图片

PIC24FJ64GB002 with bluetooth USB dongle_第3张图片

I will explain my project (how to control a bluetooth USB dongle by PIC24FJ64G002 on HCI protocol layer) step by step. 

I recommend you to check your bluetooth USB dongle. Some dongle has a hub construction. Here, I concentrate only on a dongle that has “no” hub construction, because “USB Host - MCHPUSB - Generic Driver Demo” does not support the hub construction. You can check easily by using “usbview.exe” (distributed by Michrosoft) whether your dongle has the hub construction or not. I am using a PLANEX BT-MicroEDR1X bluetooth USB dongle as seen in the picture.

A picture of electric circuit and a schematic diagram are attached. A RS232C adapter is useful to debug your firmware for your target device, which will be uploaded later. Now I am drawing a schematic diagram for the adapter.

Specification Documents for HCI (Host Controller Interface) are described in "Core v2.0 + EDR.pdf". See Volume 2; Part E. This pdf file can be obtained from Web site ashttp://www.bluetooth.com/SiteCollectionDocuments/Core_v210_EDR.zip.

My project totally depends on the works of Mr. Ishikawa. Although he uses “a UART interface bluetooth module”, we can learn much about the HCI protocol. His Web site is http://www.asahi-net.or.jp/~QX5K-ISKW/robot/blue.html. The site is written in Japanese, but you can download and read his source codes (as for HCI, treservo.zip and treservopc.zip). He seems to develop a RFCOMM protocol layer.

newbluetooth.zip (144.02 KB) - downloaded 5678 times

Before you burn your PIC with the firmware I uploaded, please check a following few points.

#1. I use a resonator of 4 MHz. For example, if you are using a resonator (or oscillator) of 8 MHz, you should change from PLLDIV_NODIV to PLLDIV_DIV2 in _CONFIG2 (main.c).

#2. As for the RS232C connection, do not comfuse output and input pins (main.c).
    // Configure U2RX - put on pin 19 (RP8)
    RPINR19bits.U2RXR = 8;
    // Configure U2TX - put on pin 5  (RP7)
    RPOR3bits.RP7R = 5;
#3. When the target PIC can not enumerate your bluetooth USB dongle, change the delay time “DelayMs(30);//YTS” and try again (4906 lines from the top of usb_host.c)

I upload a picture of RS232C adapter and its schematic diagram. Connect JP2 of the PIC board with JP1 of this adapter (1pins are No Connection, 2pin to 2pin, etc.). You can connect a D_SUB 9pins’ connector to your PC and monitor the data send from the PIC. An application software “Hyperterminal” (included in Windows XP) is convenient to monitor the data (settings: 57600 bps, 8 bits, non-parity, 1 stop bit, and no flow-control). When you do not have a RS232C port in your PC, you can use a RS232C to USB conversion cable instead.

PIC24FJ64GB002 with bluetooth USB dongle_第4张图片

To avoid the confusion, I write the situation of my project. The communication between (PC:Windows XP + non brand bluethooth USB dongle) and (PIC24FJ64GB002 + PLANEX BT-MicroEDR1X) have been made with success.(*) Hereafter, the non brand bluethooth USB dongle is referred to as “the PC dongle” and the PLANEX BT-MicroEDR1X as “the PIC dongle”.

(*) Now I am trying to connect (PC:Windows XP + PLANEX BT-MicroEDR1X) with (PIC24FJ64GB002 + PLANEX BT-MicroEDR1X) for my responsibility. Please wait for a while.

The data link between (PC:Windows XP + PLANEX BT-MicroEDR1X) and (PIC24FJ64GB002 + PLANEX BT-MicroEDR1X) is established. Two changes were needed.
#1. Please comment out the following line (410 line from the top of main.c)
if(buf[0]!=0x20){ DemoState =BT_STATE_READ_EP1; break;}
as
//if(buf[0]!=0x20){ DemoState =BT_STATE_READ_EP1; break;}
#2. The other change was needed in an application software (bluehost.exe). See below.

bluehost.zip (196.77 KB) - downloaded 4022 times

Here I upload the application software (bluehost.exe) working on the PC (Windows XP).As for the device driver, see below.

A generic device driver used here (Uusbd.sys) has been presented by Mr. Kashiwano. He owns his copyright and you can use this driver only for hobby.

dll.zip (32.09 KB) - downloaded 3107 times

Before you execute the software (bluehost.exe), you should enumerate your PC dongle on you PC. 
#1. Edit a “UUSBD.INF” file in “driver” folder to meet with your PC dongle. Example is written in the “UUSBD.INF” file.
#2. Move file “bth.inf” out of a WINDOWS\inf folder so that Windows XP cannot identify your PC dongle any more and asks you the place where the device deriver is. 
#3. You select “driver” folder to enumerate your PC dongle by “uusbd.sys”
#4. Comment out the following sentences of “bluehostDlg.cpp” in “bluehost folder” to meet your PC dongle and compile again.

.     //see the endpoint order by using "USBVIEW.EXE"
    h1=Uusbd_OpenPipe(husb,0,0);//first  Endpoint 1(81) for HCI event
    //for PLANEX
    h2=Uusbd_OpenPipe(husb,0,1);//second Endpoint 2(02) for ACL data
    h3=Uusbd_OpenPipe(husb,0,2);//third  Endpoint 2(82) for ACL data
    //for non brand product
//    h3=Uusbd_OpenPipe(husb,0,1);//second Endpoint 2(82) for ACL data
//    h2=Uusbd_OpenPipe(husb,0,2);//third  Endpoint 2(02) for ACL data

Now, let turn on your PIC board, then execute the application software (bluehost.exe). After the connection between the PC and the PIC is established, a dialog box with 2 edit boxes and 1 SEND button appears. When you write something in the upper box and push the SEND button, only 3 characters appear in the lower box. You find that only the first character is changed. For example, you get “223”, when you send “12345”. First character of ‘2’ = ‘1’ +1 in ASCII code. Here you can switch off the target PIC board. When you send some characters again, there is no response any more, meaning that the connection between PC and PIC through bluetooth dongles is really established. 

When you would like to send and receive more data, please change the following lines "consistently". Otherwise you will loose the connection. As for the format of ACL packets, see 5.4.2 HCI ACL Data Packets of Core v2.0 + EDR.pdf. 

PIC firmware: 
Change from 421 to 458 lines from the top in main.c to meet your purpose. Connection Handle is stored as handle[0]=buf[3];handle[1]=buf[4]+0x20 at 397th line from the top in main.c. Here 0x20 corresponds to PB and BC flags.

Application software:
Change from 222 to 231 lines from the top in bluehostDlg.cpp. Connection Handle is stored as aa[0]=buf[3];aa[1]=buf[4]|0x20; at 178th line from the top in bluehostDlg.cpp. Here 0x20 corresponds to PB and BC flags.

After you finish debuging your firmware, you can use your target PIC board with single power supply of 3V (lower limit is around 2.7V in case PLANEX BT-MicroEDR1X) as seen in an attached picture. This means that we can get a handy bluetooth remote controller in our hand. Please enjoy.

“My” non-brand BT USB Dongle has a hub-structure and cannot work with PIC24FJ64GB002. This is because “USB Host - MCHPUSB - Generic Driver Demo”, does not support the hub structure. If you get a (non-brand or brand) BT USB Dongle that “does not have a hub-structure”, I think it can work with PIC24FJ64GB002. As I wrote before, you can check easily by using “usbview.exe” (I have uploaded it by mistake) whether the dongle has the hub-structure or not.

Unfortunately, I am not familiar with PIC24FJ256GB106. However, please see the 87th line from the top in main.c (USB Host - MCHPUSB - Generic Driver Demo) that Microchip distributes. The line looks like “ # elif defined(__PIC24FJ256GB106__)”. This means that you can use this firmware (for USB host) with PIC24FJ256GB106 and that only what you should do is to change my firmware for PIC24FJ256GB106. Only the PIC dependent program lines (_CONFIG1 etc.) are needed to be change, I think. 

As you know there are some layers in a BT stack; HCI, L2CAP, SDP, RFCOMM, HID etc. Only the HCI layer has been established in my project. The HCI layer is enough for data exchange (data link). Possibly, you can contract L2CAP, SDP, RFCOMM, HID layers in PIC24FJ256GB106 if you want. 

Just information. In order to develop bluetooth stack, HID, RFCOMM etc., a monitor program working on Windows OS is useful. If you have bluetooth equipments, you can utilize “USBTrace” (free for 15 days: download “usbtrace.zip” and “WCDecoder.zip” from http://www.sysnucleus.com/) and would find how bluetooth equipments send/receive HCL and ACL data.

Here is a bluetooth-HID firmware of beta version for PIC24FJ64G002. PC (OS: Windows XP, bluetooth USB dongle:PLANEX BT-MicroEDR1X, bluetooth stack: bluesoleil 2.7.0.13 *, and application software: see below). 

The time-out for service connection is only about 2 seconds. If you double click “yts icon” in window of bluesoleil, then an “hourglass icon” appears. After the hourglass icon disappears, click a “mouse icon” (surrounded by a squire: see an attached picture)  within 2 seconds. Then you can connect PIC with PC on the HID profile. Otherwise, you lose connection. This is a problem. I hope that someone solves this problem. Time-out of 20 seconds would be better for me. 

I must say again that this firmware totally depends on the works of Mr. Ishikawa. Very useful information is in “blueme015.lzh, btcount015.lzh, and bluemelt_r8c15_050722.lzh” (http://www.asahi-net.or.jp/~qx5k-iskw/robot/blue.html). He starts to develop a RFCOMM layer on PIC24FJ64G002 now. Let’s wait for his success. 

*) Although bluesoleil version 6 cannot be used stably with my firmware (I don’t know the reason why), bluesoleil 2.1.2.0 can be. You can download the latter from the web site (http://www.pc210.com/downloadhtml/BlueSoleil_2.1.2.0_Release060301%2826.9MB%29_374.html ). 

hidbluetooth.zip (174.54 KB) - downloaded 3241 times

Thank you everybody who helps me. The forbidden word was " w i i m o t e ". 

I used Service Discovery Protocol (SDP) data of “Nintendo's w i i m o t e" in my firmware (data are in the arrays, att1, att2a, and so on, in main.c). As for the report ID, I used the input and output report IDs (0x21 and 0x18, respectively). One can send/receive data within 21 bytes by using these IDs. 
For Protocol and Service Multiplexor (PSM): SDP 0x0001, HID_Control 0x0011, HID_Interrupt 0x0013 and for SDP: SDP 0x0001, HIDP 0x0011 

More detailed information can be obtained from the following web-sites (remove spaces) 
Data of  w i i m o t e  : http://w i i brew.org/wiki/ W i i m o t e 
SDP of w i i m o t e: http://wiki. w i i m o t e project.com/Bluetooth_Specs 
uuid for SDP: http://hi.baidu.com/tangyuanan/blog/item/dbb1951b728db21d8718bfe1.html 
Bluetooth HID profile: http://www.bluetooth.com/SiteCollectionDocuments/HID_SPEC_V10.pdf 
DEVICE IDENTIFICATION SPECIFICATION: http://www.bluetooth.com/...nts/DI_SPEC_V12r00.pdf

Here is an application software for PC to communicate with PIC on the HID profile level. After connecting PIC with PC by using bluesoleil, execute "PICkit2.exe". A dialog box with 2 edit boxes and 1 SEND button appear. When you write something in the upper box (within 21 characters) and push the SEND button, the characters appear in the lower box. You find that only the first character is changed. For example, you get “22345”, when you send “12345”. First character of ‘2’ = ‘1’ +1 in ASCII code.

PICkit2.zip (59.96 KB) - downloaded 2807 times

Here is a hid-bluetooth firmware for users of “Microsoft bluetooth stack” (included in XP service pack 3). 

hidblueM_firm.zip (196.97 KB) - downloaded 2418 times

Here is a hid-bluetooth host application software for PC (“Microsoft bluetooth stack”). After turning on your PIC, you can execute hidblueM.exe. Only first connection will be failed (this may be due to time-out I described above). After second connection, you will be able to connect with success. As for usage, see post # 20. 
Microsoft blue stack is much convenient for me. 

hidblueM.zip (121.67 KB) - downloaded 2269 times

The firmware “hidblueM_firm.zip “ that I uploaded in the post #23 can be also used with bluesoleil 2.7.0.13. To use it, little change is needed for PICkit2Dlg (application software for PC: uploaded for the post #20). I add the following cords to the function OnSend() in PICkit2Dlg.cpp (see an attached file).
//YTS**********************************************
    USB.WriteReport ((unsigned char *)buf, 21);
    result=USB.ReadReport ((unsigned char *)buf);
//*************************************************

PICkit2M.zip (100.31 KB) - downloaded 2155 times

Though I wrote that “Microsoft blue stack is much convenient for me” in the post #24, I found that when I boot/reboot PC, the first connection is always failed (after second connection, everything is OK). This is the problem  and may come from the time-out of 2 second. In the meantime, 

hidblueM_firm.zip (for PIC) and bluesoleil 2.7.0.13/2.1.2.0 + PICKit2M (for PC) or 
hidblue.zip (for PIC) and bluesoleil 2.7.0.13/2.1.2.0 + PICKit2 (for PC) 

is better. Sorry for your inconvenience. 

Please comment out 1815th line from the top of main.c (hidblueM_firm) as

case  SDP_ATTR_END:
    buf[8]=0x07;
    data_size=16;
//    DemoState = BT_STATE_WRITE_ACL;
    HciState = L2CAP_CON_REQ;
    hid_flag++;//GO TO second or third stage for L2CAP connection
break;

Time-out becomes 10 seconds. As a result, “hidblueM.exe” (for Microsoft bluetooth stack) works well from the first connection.

I confirm that bluesoleil 6.4.275.0 can be used ( 6.4.314.3 cannot: unstable). 
http://www.downloadvyp.co...l-642750-download.html 

Following combinations are recommended now. 

1. Bluesoleil stack: 
          PIC:  hidblueM_firm.zip (revised in post #27) 
          PC  : bluesoleil 2.7.0.13 or 2.1.2.0 or 6.4.275.0 + PICKit2M.zip

2. Microsoft stack: 
          PIC: hidblueM_firm.zip (revised in post #27) 
          PC : hidblueM.zip 

Hello fisrt of all thanks, second i am from Brazil then my english is poor, i wanna know if is possible do what you do with the PIC18f4550 (http://www.microchip.com/wwwproducts/Devices.aspx?dDocName=en010300)  and how is the size of the firmware compilated? thanks

PIC18F4550 does not have a function of USB “host”. Therefore, it cannot be used. I recommend to use a PIC that supports USB host.

I used 11185 out of 22012 for Program Memory and 4226 out of 8192 for Data Memory in hidblueM_firm in PIC24FJ64GB002. I can say that PIC24FJ64GB002 has enough memories for bluetooth HID + RFCOMM.

I have used an ICD2 for writing the firmware in PIC. The RS232C adapter and PIC circuit are different each other. The adapter is used to monitor how the PIC works (and to debug the firmware) on PC.


ICD2------------------>PIC=========bluetooth==========>PC
        writing firmware   ||        (wireless (radio) connection)       ||
                                     ||                                                             ||
                                      =======RS232C adapter=========>
                                                  (wired connection)

  PIC can send any information including debug information to PC though RS232C adapter (wired connection), even if a bluetooth wireless connection is not established. Once you can establish bluetooth wireless connection, you may not need the RS232C adapter any more. By the way, ADM323 used in the RS232 adapter is an integrated circuit that works as a lever converter from 0~5V (PIC) to +12~-12V(PC).

Unfortunately, I do not know which programmer is suitable for you. I am satisfied with an old ICD2. 

Here is a firmware to establish RFCOMM with bluesoleil 6.4.275.0 http://www.downloadvyp.co...l-642750-download.html . As bluesoleil 6.4.314.3 is unstable, do not use it. This firmware does not work with Microsoft protocol stack.

Before you burn your PIC with the firmware, check a following two points.

#1. I use a resonator of 4 MHz. For example, if you are using a resonator (or oscillator) of 8 MHz, you should change from PLLDIV_NODIV to PLLDIV_DIV2 in _CONFIG2 (18 lines from the top in main.c).

#2. When the target PIC can not enumerate your bluetooth USB dongle, change the delay time “delay_time=30” and try again (155 lines from the top in main.c)

rfcomm_firm.zip (161.34 KB) - downloaded 2160 times

This is an RFCOMM application program for PC (windows XP SP3). After turning on PIC, execute the bluesoleil to connect with PC. Please remember the port number (say COM2) appearing in the connection process. Change the strings “COM6” in a line “CreateFile("COM6",GENERIC_READ | GENERIC_WRITE, 0,NULL,OPEN_EXISTING,0,NULL)” of rstestdlg.cpp to “COM2”. 

After compiling the program again, execute RsTest.exe. A dialog box with 2 edit boxes and 1 SEND button appear. When you write something in the upper box (within 50 characters) and push the SEND button, the characters appear in the lower box. You find that only the first character is changed. For example, you get “22345”, when you send “12345”. First character of ‘2’ = ‘1’ +1 in ASCII code.

RsTest.zip (71.09 KB) - downloaded 2100 times

New version of rfcomm_firm.zip (see Post #33). The problem of time-limitation (10 seconds), which is described in Post #27, has been solved. This new firmware still does not work with Microsoft bluetooth protocol stack.

rfcomm_firm2.zip (137.03 KB) - downloaded 2047 times

Things move on - 
I am attempting to produce code that allows a Bluetooth connected device to communicate with internal code but to appear as a serial port to the PC.

The code from yts has allowed me to proceed faster than I expected as I do not have to read the specifications in detail. I have the unit receiving data from the PC and placing it on the output data stream. This is currently set to the UART2 but may be to anywhere via the interface calls. This works well.

Problems occur when trying to communicate the other way. The problem is deciding when to initiate a write (see main.c function main). It transfers data OK but sending the first character after receiving data fails. I am convinced this is the root of the problem but as I do not have detailed knowledge of Bluetooth or USB I am missing the problem.

Feel free to use/modify the included code in any way. The copyrights are as in the file headers.

I have also modified the serial interface (uart2.c) to work through interrupts and a ring buffer. If you use this software make sure your buffers are big enough for your application as they over-write when full.

Any help would be appreciated or I have to 'READ THE MANUALS'!!!

The uploaded files does not contain the USB files. Get then from the post above (rfcomm_firm2.zip)

Have fun!

rfcomm_firm2a.zip (35.42 KB) - downloaded 2023 times

For the device --> PC communication, the same method as USB-UART chips may be applied to this case, too.

- Wait until the transfer queue reaches to the optimal packet size
- Latency timer

On USB-UART chips, the device queues UART RX data on a RX buffer, first. When more than 64 bytes is stored on the queue, the device puts a 64 bytes packet (full-size on full-speed bus) over bulk IN endpoint. While full-size packets are sent, the device hits the best speed performance on full-speed bus.

But UART RX does not always come in 64 bytes chunk. It may come in sporadic interval, and in short chunks. If the device would always wait for 64 bytes, no response could be returned from the device for long interval. To ensure the deadline response, a latency timer forces the packet transfer, even when the queue has less than 64 bytes.

These appnote touches to the implementation of this method.

FTDI
http://www.ftdichip.com/S...32b_04smalldataend.htm

SiLabs
https://www.silabs.com/Su...echnicalDocs/an205.pdf
9. USB Timeouts

Tsuneo

Here is a RFCOMM firmware for Microsoft bluetooth protocol stack. (*) It is little bit hard to explain how to use this firmware with PC, because my OS (Windows XP SP3) is not of English version. There may be some mistranslations in terms with double quotations (“…”). 

Double click a “bluetooth device” icon to use Microsoft bluetooth wizard on PC. Please connect with PIC without a pass key. When you connect PC with PIC, you will find a dialog window with two COM port numbers (“incoming and outgoing”). Memorize the number of incoming port. Following the post #34, rewrite the application program by using the memorized COM port number and execute RsTest.exe. Please wait for a while to find a “balloon” relating a pass key. After you click this “balloon”, a dialog window appears and you are asked to enter the pass key. The pass key should be 0000 (four zeros).(**)  Finishing the wizard, you can send and/or receive data.

(*) This firmware is not for bluesoleil.
(**) If you would like to change a pass key, rewrite  the 1001 lines from the top (and below) in main.c.

rfcomm_firmM.zip (140.54 KB) - downloaded 1968 times

There are many opensource bluetooth stack implementation, of course you know many of them, but I want to give links maybe it will be useful to solve compatibilitiy problems; 

These are use uart hci bluetooth module; 

this one was tested ok with; 

Mulle; 

  • Bluesoleil Bluetooth software for Windows
  • Linux Bluez Bluetooth stack
  • MacOS X Bluetooth stack
  • Compaq/HP iPAQ

http://staff.www.ltu.se/~jench/mulle.html 

BTnodes; 

http://www.btnode.ethz.ch/Main/Overview 

lwBT; 

http://github.com/lwalkera/lwBT 

btstack; 

http://code.google.com/p/btstack/ 


These are use usb hci module; 

http://www.fourwalledcubicle.com/LUFA.php (beta stage) 

http://micah.navi.cx/2010/07/propeller-bluetooth-stack-demo
 (this one is use software usb host, use propeller micro, not in c language but complete) 

If anyone know other than these please add links 

I simplify rfcomm_firmM.zip  (especially SDP).

rfcomm_firmM1.zip (137.07 KB) - downloaded 1928 times

Just information for RFCOMM and TS 07.10 is given in the following web-sites.

http://www.bluetooth.com/...nDocuments/rfcomm1.pdf
http://authors.phptr.com/...h/bray/pdf/cr_ch10.pdf
http://www.fer.hr/_downlo.../ts_101369v060300p.pdf

I revise the hid-bluetooth firmware (see post #23) for “Microsoft bluetooth stack of Windows XP(SP3). I had forgotten the initialization of the SDP. 

I am developing a new version of this firmware. If you can wait, you need not down load this one.

hidblueM_firm1.zip (139.54 KB) - downloaded 1874 times

This is a new version of the hid-bluetooth firmware (see post #57) and is for Bluesoleil and Microsoft protocol stacks.

hidblueM_firm2.zip (152.47 KB) - downloaded 1862 times

This is a revise version of PICkit2.zip working on PC (application software for hidblueM_firm2.zip: see post #20). After connecting PIC with PC, execute PICkit2.exe on PC. 

PICkit2.zip (100.22 KB) - downloaded 1865 times

Following three firmwares are of new version. “Disconnection Complete event” is checked during connection process. Endpoint1 and 2 are properly used for IN transaction.
First one is a new version of the hid-bluetooth firmware for both Bluesoleil and Microsoft protocol stacks. 

hidblueM_firm3.zip (153.77 KB) - downloaded 1826 times

Second one is a new version of the RFCOMM firmware for Bluesoleil protocol stack.

rfcomm_firm3.zip (141.54 KB) - downloaded 1829 times

Last one is a new version of the RFCOMM firmware for Microsoft protocol stack. Old version had serious errors for sending /receiving data. Sorry for your inconvenience.

rfcomm_firmM2.zip (140.99 KB) - downloaded 1834 times

I had posted a wrong firmware named “hidblueM_firm3” just here. One person has already downloaded it. Please replace it with new one. Sorry for his/her inconvenience. 

I modified the firmware “rfcomm_firmM2” for Windows 7 Professional 64 bit to have “rfcomm_firmM3”. The firmware “hidblueM_firm3”works well under (on?) this OS.

rfcomm_firmM3.zip (141.90 KB) - downloaded 1827 times

I found that SDP.zip (posted at #62) has contained “bugs”. I revise it. 

sdp.zip (47.62 KB) - downloaded 1798 times

Microsoft and Bluesoleil bluetooth protocol stacks

Windows XP SP3、Vista Business 32bit SP2、Windows 7 64bit Professional


I revise hidblueM_firm3 to get hidblueM_firm4.

hidblueM_firm4.zip (153.07 KB) - downloaded 1803 times

Microsoft bluetooth protocol stack

Windows XP SP3、Vista Business 32bit SP2、Windows 7 64bit Professional

I revise rfcomm_firmM3 to get rfcomm_firmM4.

rfcomm_firmM4.zip (141.17 KB) - downloaded 1834 times

 

你可能感兴趣的:(PIC24FJ64GB002 with bluetooth USB dongle)