linux 蓝牙开发调试(rtl8821cs模块)

刚调完rtl8821cs的wifi功能,项目需要打通蓝牙配网功能。

调试过程中遇到各种问题中间几乎放弃,倒腾了几天最后还是打通了,顺便记录下过程。

通信接口:SDIO @WiFi、Uart @BT;
工作频段:2.4G/5.8G +BT4.2;

rtl8821cs是一个蓝牙wifi二合一模块,wifi走的是sdio接口,支持2.4G,5G.

bt走的是uart接口 支持bluez blue-alsa开源协议。

一、内核配置编译

我的内核版本是kernel-4.4.55

配置如下

make menuconfig
   <*> Networking Support-->
         <*>   Bluetooth subsystem support  ---> 
                 Bluetooth device drivers  ---> 

 

rtl8821cs 是uart接口的蓝牙

内核需要先配置如下两个配置项,如果是usb的接口则相应的配置usb的驱动支持即可。

在这里直接配置编译进内核,也可以配置成ko模块方便调试。

[*]   UART (H4) protocol support 
[*]   Realtek Three-wire UART (H5) protocol support 

------------------------------------------------------------------------------------------------------------------------------------+ |  
  | |                               < > RTK HCI USB driver                                                                               | |  
  | |                               < > HCI USB driver                                                                                   | |  
  | |                               < > HCI SDIO driver                                                                                  | |  
  | |                               <*> HCI UART driver                                                                                  | |  
  | |                               [*]   UART (H4) protocol support                                                                     | |  
  | |                               [ ]   BCSP protocol support                                                                          | |  
  | |                               [ ]   Atheros AR300x serial support                                                                  | |  
  | |                               [ ]   HCILL protocol support                                                                         | |  
  | |                               [*]   Realtek Three-wire UART (H5) protocol support                                                  | |  
  | |                               [ ]   Three-wire UART (H5) protocol support                                                          | |  
  | |                               [ ]   Intel protocol support                                                                         | |  
  | |                               [ ]   Broadcom protocol support                                                                      | |  
  | |                               [ ]   Qualcomm Atheros protocol support                                                              | |  
  | |                               < > HCI BCM203x USB driver                                                                           | |  
  | |                               < > HCI BPA10x USB driver                                                                            | |  
  | |                               < > HCI BlueFRITZ! USB driver                                                                        | |  
  | |                               < > HCI VHCI (Virtual HCI device) driver                                                             | |  
  | |                               < > Broadcom Bluetooth Low Power Manager Support                                                     | |  
  | |                               < > Broadcom Bluetooth Low Power Manager Support for AXP                                             | |  
  | |                               < > Broadcom Bluetooth Low Power Manager Support for remote control                                  | |  
  | |                               <*> Realtek Bluetooth Low Power Manager Support                                                      | |  
  | |                               < > Marvell Bluetooth driver support               

相关驱动代码在如下目录,
linux-4.4/drivers/bluetooth

二、蓝牙服务依赖包

1、dbus-1.10.4

2、bluez-5.38

3、bluez-alsa-20180913

4、glib-2.50.1

uart接口需要hciattach工具,bluez里面已经自带了,但是rtk的需要专门的工具rtk_hciattach 才行,

故还需要如下依赖包

5、rtk_hciattach

同时还需要对应的fireware版本。

配置好后编译打包烧录,开始调试。

====================调试=============================

1、首先应该插入或者已经启动wifi驱动,保证模块已经上电。

2、先启动dbus服务,待会bluetoothd 起来需要用到dbus。

/etc/init.d/dbus start

启动后用ps查看dbus服务进程,

/usr/sbin/dbus-daemon --system

并且可以在/var/run/dbus/查看到通信端口

root@airfly:/# ll /var/run/dbus
drwxr-xr-x    2 root     root            60 Jan  2 14:31 .
drwxr-xr-x    7 root     root           240 Jan  2 14:33 ..
srwxrwxrwx    1 root     root             0 Jan  2 14:31 system_bus_socket

3、 使用rfkill 解除bluetooth的block

        echo 0 > /sys/class/rfkill/rfkill0/state
     
        echo 1 > /sys/class/rfkill/rfkill0/state

 

4、如果蓝牙驱动是ko模块,这时可以插入蓝牙驱动。

5、使用rtk_hciattach绑定uart端口

usb接口的蓝牙可以不用这一步。

 rtk_hciattach -n -s 115200 /dev/ttyS1 rtk_h5 &
注意这个过程需要加载固件才行,如果固件不存在、不匹配或者错误就会失败。
root@airfly:/# rtk_hciattach -n -s 115200 /dev/ttyS1 rtk_h5 & 
root@airfly:/# Realtek Bluetooth init uart with init speed:115200, type:HCI UART H5
Realtek Bluetooth :Realtek hciattach version 3.1.0099684.20181218-163903 

Realtek Bluetooth :Use epoll
Realtek Bluetooth :[SYNC] Get SYNC Resp Pkt
Realtek Bluetooth :[CONFIG] Get SYNC pkt
Realtek Bluetooth :[CONFIG] Get CONFG pkt
Realtek Bluetooth :[CONFIG] Get CONFG resp pkt
Realtek Bluetooth :dic is 1, cfg field 0x14
Realtek Bluetooth :H5 init finished

Realtek Bluetooth :Realtek H5 IC
Realtek Bluetooth :Receive cmd complete event of command: 1001
Realtek Bluetooth :HCI Version 0x08
Realtek Bluetooth :HCI Revision 0x000c
Realtek Bluetooth :LMP Subversion 0x8821
Realtek Bluetooth :Receive cmd complete event of command: fc6d
Realtek Bluetooth :Read ROM version 01
Realtek Bluetooth :LMP Subversion 0x8821
Realtek Bluetooth :EVersion 1
Realtek Bluetooth :IC: RTL8821CS
Realtek Bluetooth :Firmware/config: rtl8821cs_fw, rtl8821cs_config
Realtek Bluetooth :Couldnt access customer BT MAC file /opt/bdaddr
Realtek Bluetooth :Couldnt open extra config /opt/rtk_btconfig.txt, No such file or directory
Realtek Bluetooth :Original Cfg len 25
Realtek Bluetooth :Config baudrate: 04928002
Realtek Bluetooth :uart flow ctrl: 0
Realtek Bluetooth :55 ab 23 87 13 00 0c 00 10 02 80 92 04 50 c5 ea
Realtek Bluetooth :19 e1 1b fd af 5b 01 a4 0b
Realtek Bluetooth :Cfg length 25
Realtek Bluetooth :Vendor baud from Config file: 04928002
Realtek Bluetooth :Load FW /lib/firmware/rtlbt/rtl8821cs_fw OK, size 40448
Realtek Bluetooth :rtb_get_fw_project_id: opcode 0, len 1, data 10
Realtek Bluetooth :FW version 0xaa7b2a21, Patch num 2
Realtek Bluetooth :Chip id 0x0001
Realtek Bluetooth :Chip id 0x0002
Realtek Bluetooth :Patch length 0x6078
Realtek Bluetooth :Start offset 0x00003d40
Realtek Bluetooth :Svn version:    20445
Realtek Bluetooth :Coexistence: BTCOEX_20180125-2828

Realtek Bluetooth :FW  exists, Config file  exists
Realtek Bluetooth :Total len 24721 for fwc
Realtek Bluetooth :baudrate in change speed command: 0x02 0x80 0x92 0x04
Realtek Bluetooth :Receive cmd complete event of command: fc17
Realtek Bluetooth :Received cc of vendor change baud
Realtek Bluetooth :Final speed 1500000
Realtek Bluetooth :end_idx: 98, lp_len: 25, additional pkts: 1

Realtek Bluetooth :Start downloading...
Realtek Bluetooth :Last packet 227
Realtek Bluetooth :Send last pkt
Realtek Bluetooth :Disable host hw flow control
Realtek Bluetooth :h5_hci_reset: Issue hci reset cmd
Realtek Bluetooth :Receive cmd complete event of command: 0c03
Realtek Bluetooth :Received cc of hci reset cmd
Realtek Bluetooth :Init Process finished
Realtek Bluetooth post process
Device setup complete

此时hci0蓝牙端口已经生成,如果是usb接口的不用hciattach就有端口,使用hciconfig工具可以查看端口状态

当前应该还是DOWN状态

root@airfly:/# hciconfig 
hci0:   Type: BR/EDR  Bus: UART
        BD Address: 48:98:CA:AD:3C:AB  ACL MTU: 1021:8  SCO MTU: 255:12
        DOWN 
        RX bytes:1152 acl:0 sco:0 events:33 errors:0
        TX bytes:814 acl:0 sco:0 commands:33 errors:0

6、启动hci0,状态更新为UP RUNNING

root@airfly:/# hciconfig hci0 up
root@airfly:/# hciconfig 
hci0:   Type: BR/EDR  Bus: UART
        BD Address: 48:98:CA:AD:3C:AB  ACL MTU: 1021:8  SCO MTU: 255:12
        UP RUNNING 
        RX bytes:2304 acl:0 sco:0 events:66 errors:0
        TX bytes:1492 acl:0 sco:0 commands:67 errors:0

 

7、启动bluetoothd 服务

 

root@airfly:/# bluetoothd -n & 
root@airfly:/# bluetoothd[5559]: Bluetooth daemon 5.38
bluetoothd[5559]: Starting SDP server
bluetoothd[5559]: Bluetooth management interface 1.10 initialized
bluetoothd[5559]: Not enough free handles to register service
bluetoothd[5559]: Error adding Link Loss service
bluetoothd[5559]: Not enough free handles to register service
bluetoothd[5559]: Not enough free handles to register service
bluetoothd[5559]: Not enough free handles to register service
bluetoothd[5559]: Current Time Service could not be registered
bluetoothd[5559]: gatt-time-server: Input/output error (5)
bluetoothd[5559]: Not enough free handles to register service
bluetoothd[5559]: Not enough free handles to register service
bluetoothd[5559]: Sap driver initialization failed.
bluetoothd[5559]: sap-server: Operation not permitted (1)

如果dbus服务没有启动,bluetoothd启动会失败报错

root@airfly:/# bluetoothd -n
bluetoothd[18793]: Bluetooth daemon 5.38
D-Bus setup failed: Failed to connect to socket /var/run/dbus/system_bus_socket: No such file or directory
bluetoothd[18793]: Unable to get on D-Bus

bluetoothd启动需要加载一些配置文件在/etc/bluetooth下面。

 

root@airfly:/# ll /etc/bluetooth/*.conf
-rw-r--r--    1 root     root            97 Jun 11  2020 /etc/bluetooth/hcidump.conf
-rw-r--r--    1 root     root           397 Jun 11  2020 /etc/bluetooth/input.conf
-rw-r--r--    1 root     root          3872 Jun 11  2020 /etc/bluetooth/main.conf
-rw-r--r--    1 root     root           120 Jun 11  2020 /etc/bluetooth/network.conf
-rw-r--r--    1 root     root           258 Jun 11  2020 /etc/bluetooth/proximity.conf

其中main.conf里面有几个关键配置,蓝牙名称,支持的蓝牙协议类型,扫描发现及匹配的超时设置等。

[General]

# Default adaper name
# Defaults to 'BlueZ X.YZ'
Name = AIRFLY_H3

# Default device class. Only the major and minor device class bits are
# considered. Defaults to '0x000000'.
Class = 0x000428

# How long to stay in discoverable mode before going back to non-discoverable
# The value is in seconds. Default is 180, i.e. 3 minutes.
# 0 = disable timer, i.e. stay discoverable forever
DiscoverableTimeout = 0

# How long to stay in pairable mode before going back to non-discoverable
# The value is in seconds. Default is 0.
# 0 = disable timer, i.e. stay pairable forever
PairableTimeout = 0

# Automatic connection for bonded devices driven by platform/user events.
# If a platform plugin uses this mechanism, automatic connections will be
# enabled during the interval defined below. Initially, this feature
# intends to be used to establish connections to ATT channels. Default is 60.
#AutoConnectTimeout = 60

8、启动blue-alsa daemo 

建立一个audio sink支持蓝牙“”媒体音频“协议。

root@airfly:/# bluealsa -p a2dp-sink & 
root@airfly:/# bluetoothd[4788]: Endpoint registered: sender=:1.1 path=/A2DP/SBC/Sink/1

 

 

9、使能扫描发现

root@airfly:/# hciconfig hci0 piscan
root@airfly:/# hciconfig 
hci0:   Type: BR/EDR  Bus: UART
        BD Address: 48:98:CA:AD:3C:AB  ACL MTU: 1021:8  SCO MTU: 255:12
        UP RUNNING PSCAN ISCAN 
        RX bytes:2635 acl:0 sco:0 events:81 errors:0
        TX bytes:2773 acl:0 sco:0 commands:82 errors:0

 

hciconfig的功能很强大,具体使用命令查看--help帮助。

root@airfly:/# hciconfig --help
hciconfig - HCI device configuration utility
Usage:
        hciconfig
        hciconfig [-a] hciX [command ...]
Commands:
        up                      Open and initialize HCI device
        down                    Close HCI device
        reset                   Reset HCI device
        rstat                   Reset statistic counters
        auth                    Enable Authentication
        noauth                  Disable Authentication
        encrypt                 Enable Encryption
        noencrypt               Disable Encryption
        piscan                  Enable Page and Inquiry scan
        noscan                  Disable scan
        iscan                   Enable Inquiry scan
        pscan                   Enable Page scan
        ptype      [type]       Get/Set default packet type
        lm         [mode]       Get/Set default link mode
        lp         [policy]     Get/Set default link policy
        name       [name]       Get/Set local name
        class      [class]      Get/Set class of device
        voice      [voice]      Get/Set voice setting
        iac        [iac]        Get/Set inquiry access code
        inqtpl     [level]      Get/Set inquiry transmit power level
        inqmode    [mode]       Get/Set inquiry mode
        inqdata    [data]       Get/Set inquiry data
        inqtype    [type]       Get/Set inquiry scan type
        inqparms   [win:int]    Get/Set inquiry scan window and interval
        pageparms  [win:int]    Get/Set page scan window and interval
        pageto     [to]         Get/Set page timeout
        afhmode    [mode]       Get/Set AFH mode
        sspmode    [mode]       Get/Set Simple Pairing Mode
        aclmtu     <mtu:pkt>    Set ACL MTU and number of packets
        scomtu     <mtu:pkt>    Set SCO MTU and number of packets
        delkey     <bdaddr>     Delete link key from the device
        oobdata                 Get local OOB data
        commands                Display supported commands
        features                Display device features
        version                 Display version information
        revision                Display revision information
        block      <bdaddr>     Add a device to the blacklist
        unblock    <bdaddr>     Remove a device from the blacklist
        lerandaddr <bdaddr>     Set LE Random Address
        leadv      [type]       Enable LE advertising
                0 - Connectable undirected advertising (default)
                3 - Non connectable undirected advertising
        noleadv                 Disable LE advertising
        lestates                Display the supported LE states

 至此,蓝牙服务已经建立,如果一切正常,用手机蓝牙可以扫描匹配了。

10、连接播放音频

当有设备连接播放音频的时候,可以用bluealsa-aplay 播放接收到的媒体音频(通话音频是另外的协议)。

root@airfly:/# bluetoothd[4788]: Endpoint registered: sender=:1.1 path=/A2DP/SBC/Sink/2
bluetoothd[4788]: /org/bluez/hci0/dev_54_92_09_F7_CE_92/fd0: fd(17) ready
bluetoothd[4788]: Can't open input device: No such file or directory (2)
bluetoothd[4788]: AVRCP: failed to init uinput for 54:92:09:F7:CE:92

root@airfly:/# bluealsa-aplay 54:92:09:F7:CE:92  & 

 

 

 

另外================

还有一个好用的蓝牙调试工具

bluetoothctl

和网络服务类似,网络服务的服务端是wpa_supplicant,对应的有一个客户端wpa_clit,可以用来手动调试,连接网络等。

同样蓝牙服务的服务端是bluetood ,对应的也有一个蓝牙界的瑞士军刀bluetoothcl客户端,通过命令来调试。

启动bluetootctl,会自动连接到bluetoothd,进入命令交互界面自动显示当前的Controller信息,

包括地址,名称(main.conf配置的Name字段)

root@airfly:/# bluetoothctl 
[NEW] Controller 48:98:CA:AD:3C:AB AIRFLY_H3 [default]
[bluetooth]# 

输入show命令可以查看更多信息

[bluetooth]# show
Controller 48:98:CA:AD:3C:AB
        Name: AIRFLY_H3
        Alias: AIRFLY_H3
        Class: 0x040428
        Powered: yes
        Discoverable: yes
        Pairable: yes
        UUID: Audio Sink                (0000110b-0000-1000-8000-00805f9b34fb)
        UUID: A/V Remote Control        (0000110e-0000-1000-8000-00805f9b34fb)
        UUID: PnP Information           (00001200-0000-1000-8000-00805f9b34fb)
        UUID: A/V Remote Control Target (0000110c-0000-1000-8000-00805f9b34fb)
        Modalias: usb:v1D6Bp0246d0526
        Discovering: no
[bluetooth]# 

这里可以看到当前Controller的详细信息

Name:名称 main.conf配置

Alias:别名 可以通过hciconfig hci0 name alias_name设置

Class:支持的蓝牙服务类型,注意这里显示的是0x040428和main.conf配置的0x000428不一致了,是因为我们启动了

bluealsa -p a2dp-sink

支持了

UUID: Audio Sink                (0000110b-0000-1000-8000-00805f9b34fb)

服务 。

Discoverable:是否可以被扫描发现,如果打开了就是yes 如果没打开就是no

Pairable: 是否接受匹配 yes /no

 

更多命令支持查看help

[bluetooth]# help
Available commands:
  list                       List available controllers
  show [ctrl]                Controller information
  select <ctrl>              Select default controller
  devices                    List available devices
  paired-devices             List paired devices
  power <on/off>             Set controller power
  pairable <on/off>          Set controller pairable mode
  discoverable <on/off>      Set controller discoverable mode
  agent <on/off/capability>  Enable/disable agent with given capability
  default-agent              Set agent as the default one
  set-scan-filter-uuids [uuid1 uuid2 ...] Set scan filter uuids
  set-scan-filter-rssi [rssi] Set scan filter rssi, and clears pathloss
  set-scan-filter-pathloss [pathloss] Set scan filter pathloss, and clears rssi
  set-scan-filter-transport [transport] Set scan filter transport
  set-scan-filter-clear      Clears discovery filter.
  scan <on/off>              Scan for devices
  info [dev]                 Device information
  pair [dev]                 Pair with device
  trust [dev]                Trust device
  untrust [dev]              Untrust device
  block [dev]                Block device
  unblock [dev]              Unblock device
  remove <dev>               Remove device
  connect <dev>              Connect device
  disconnect [dev]           Disconnect device
  list-attributes [dev]      List attributes
  select-attribute <attribute> Select attribute
  attribute-info [attribute] Select attribute
  read                       Read attribute value
  write <data=[xx xx ...]>   Write attribute value
  notify <on/off>            Notify attribute value
  register-profile <UUID ...> Register profile to connect
  unregister-profile         Unregister profile
  version                    Display version
  quit                       Quit program

我们可以进行打开关闭扫描发现,扫描蓝牙设备,匹配蓝牙设备,连接蓝牙设备,移除蓝牙设备等操作。

scan on

scan off

pair 54:92:09:F7:CE:92

connect 54:92:09:F7:CE:92

disconnect 54:92:09:F7:CE:92

remove 54:92:09:F7:CE:92 

 

===================以下是踩坑爬坑过程=============================

1、bluetoothd启动后,无法发现蓝牙设备,设置了discoverable on也无效。

2、使用scan  pair connect可以扫描匹配连其他蓝牙设备,并且可以播放音频。

3、已经连接且保持了第一个设备后,Controller可以被其他设备扫描到,可以匹配、连接、播放音乐、断开连接等操作。

4、如果第一个连接的设备断开了,后面连接的所有设备自动被断开,Controller又变得扫描不可见,无法被连接,也无法连接别人,需要重启机器才可以。

就这个问题倒腾了一个礼拜,内核驱动换了一遍,bluetoothd倒腾了一遍,最后更换了rtl8821cs的固件才突然好了。

rtl8821cs_fw 
rtl8821cs_congig

杀千刀的模块固件有问题

========================================================

 

深圳宝安华美居
posted @ 2020-06-12 17:45  mcdull^0^  阅读(4524)  评论(1编辑  收藏  举报