TWS headset solution design and development

With the advent of the era of all-round mobile communications, lightweight and compact Bluetooth products are gaining more and more favor. In recent years, Bluetooth TWS headsets and audio gateway pro

With the advent of the era of all-round mobile communications, lightweight and compact Bluetooth products are gaining more and more favor. In recent years, Bluetooth TWS headsets and audio gateway products have developed rapidly and have become a bright spot in the Bluetooth field. The Bluetooth TWS headset and audio gateway can ensure a fast and stable voice connection within 10m. The audio gateway is not only a wireless terminal, it is directly connected to the Bluetooth TWS headset for calls. Its more important function is to serve as the access point of earphones and expand the short distance of GSM modules, fixed voice terminals and other equipment


Bluetooth is a 214GHz short-range radio frequency wireless communication technology with a working frequency band that can be used freely all over the world. It uses short-range, low-cost wireless connections to replace cable connections, thereby providing existing data networks and small peripherals. The interface provides a unified connection. Bluetooth HeadSet uses Bluetooth technology to implement wireless communication between TWS Bluetooth headsets and audio gateways (such as mobile phones), thereby providing hands-free answering of mobile phone calls. In the Bluetooth HeadSet model, two roles need to be clarified first: one is AG (AudioGateway), which acts as an audio gateway, which can be used as both audio input and audio output; the other is HS (HeadSet), which is the TWS Bluetooth headset. A device used as Bluetooth remote audio input and output, and provides some remote control methods.


1. TWS headset development process

In the development of Bluetooth TWS headsets (Bluetoothheadset, HS) and audio gateways (audiogateway, AG), we adopted the fully embedded Bluetooth development program of British CSR Company, and designed with BlueCore22External Bluetooth chip as the core. CSR provides developers with several basic profiles of the underlying firmware (fireware) to implement the functions of the Bluetooth core protocol stack. The focus of our work is on the basis of the headset profile firmware and using its upwardly provided application APIs to write headsets and audio gateway applications that are suitable for actual needs and comply with the Bluetooth specification, and combine them with hardware entities. Completely realize all functions of HS/AG. To write Bluetooth embedded programs on a PC, a cross-compilation environment of Linux is required. The corresponding Bluetooth debugging, simulation, and download software are all based on the Windows operating system. The solution to this contradiction is to use Cygwin software, which provides a Unix system library based on Win32API, which can simulate the Linux environment running on the Windows platform. Enter the command makebc02 in Cygwin, the application program and corresponding firmware will be compiled into a .xdv download file, and through the PC parallel port to the SPI interface of the chip, finally all the software will be embedded in the Flash memory of the Bluetooth module.


深圳TWS耳机方案开发公司

2. TWS Bluetooth headset hardware development

CSR's BlueCore22External Bluetooth chip is fully compatible with Bluetooth V1.1 and V1.2 specifications, integrated radio frequency, baseband signal processor, microcontroller and 32KBRAM, and an external 8MBFlash is used to store the Bluetooth protocol stack and application programs. The air interface of the Bluetooth module provides 64KbpsA-law PCM, μ-law PCM and CVSD (continuously variable slope incremental modulation) three compression coding methods, so according to the coding method selected by the software, the corresponding codec with performance above 64Kbps should be adopted. . We adopt a fully embedded development method. All Bluetooth software stacks and applications run on a single chip, interacting with users through buttons and LEDs.


Third, the architecture of the TWS Bluetooth headset software

The Bluetooth protocol architecture adopts a layered approach, including the Bluetooth core protocol and some dedicated protocols. The headset profile defines the Bluetooth specification protocol used in the HS and AG applications and indicates some messages and processes in the Bluetooth specification, which can be regarded as the longitudinal section of the protocol stack. The mono headset and the audio gateway are fully compatible with the headset profile in the Bluetooth specification, and their software systems are basically the same, but the programs at the application layer are different.


Among them, baseband, link management (LMP), logical link control and adaptation protocol (L2CAP), and service search protocol (SDP) are the core parts of the Bluetooth stack, which complete frequency hopping, data frame disassembly, and data flow filtering and Functions such as transmission, link control, quality of service (QoS), protocol multiplexing, and service discovery. The Serial Cable Emulation Protocol (FCCOMM) provides cable substitution capabilities and provides services for advanced services that use serial lines as transmission mechanisms.


The Bluetooth baseband supports both ACL (asynchronous connectionless) and SCO (synchronous connection 2 oriented) connections on the same radio frequency link. ACL is used for asynchronous data transmission, and SCO is suitable for synchronous voice. Between HS and AG, the application program relies on the ACL-based RFCOMM connection to send and receive AT commands, perform call response, exchange MIC, speaker volume gain parameters and other control operations. The SCO-based voice stream is directly sent to the baseband transmission by the upper layer, which is also the key to ensuring real-time voice processing.


According to the profile regulation, both HS and AG can initiate the establishment of ACL. After the ACL is successfully established, the SCO can be established whenever one wishes. It can be that the AG initiates the SCO initiatively, or after receiving the AT+AKPD (indicating that the headset TALK button is triggered) from the HS, the AG responds to establish the SCO, but in essence, only the AG has the right to finally decide when to generate the SCO. Similarly, the removal of ACL and SCO is also the responsibility of AG.



深圳TWS耳机PCBA设计

Fourth, the software implementation of the Bluetooth link building process

The bluetooth application program runs on the virtual machine in the chip, adopts the message (message) drive mode. The scheduler (schedular) is responsible for collecting message events and starting the corresponding task function.


4.1 Initialisation (initialisation)


First initialize the headset library function, register the corresponding headset service with SDP, and set the device type (classofdevice). If there is a device that has already been paired, the application sends a CM_ADD_SM_DEVICE_REQ message to register it as a trusted device in the link manager, which simplifies the authentication steps of both parties and speeds up the link establishment.


4.2 Pairing


Pairing request CM_PAIR_REQ puts the device in pairing mode, and then can query the other party's Bluetooth address and exchange personal identification number (PIN) for authentication. The authentication is successful only if the PIN codes entered by the devices at both ends are the same. Finally, the link key (linkkey) used for communication is generated based on the PIN code, Bluetooth address and random number, and the PsStore() function is called to store it on the Flash for again Used when connecting.


4.3 Connecting


Either end can request to establish an RFCOMM connection as the master or slave. It is worth noting that only AG can then issue CM_SCO_CONNECT_REQ to request the establishment of a SCO connection. If both parties get the correct status return, the voice link is connected.


Five, the principle design scheme of TWS Bluetooth headset system

The basic functional modules of the Bluetooth system are shown in Figure 1. His functional modules include antenna unit, link controller, link management, and software functions.


图1 蓝牙系统的基本功能模块

5.1 Wireless Technical Specifications


The Bluetooth antenna is a microstrip antenna, and the Bluetooth wireless interface is designed based on a conventional wireless transmission power of 0dBm, which complies with the ISM band regulations of the United States United Communications Commission (FCC). The application of spread spectrum technology enables the power to be increased to 100dBm, which can meet the needs of different countries. In Japan, Spain, and France, due to the relatively narrow frequency band specified by the local regulations, it can be realized by internal software conversion.


5.2 Bluetooth Baseband Technical Specifications


The baseband describes the digital signal processing part of the device, namely the Bluetooth link controller, which completes the baseband protocol and other underlying link procedures. Mainly include the following aspects:


(1) The establishment of network connection; (2) Link type and packet type: The link type determines which packet mode can be used on a specific link, and Bluetooth baseband technology supports two link types: synchronous connection-oriented Type SCO (mainly used for voice) and asynchronous non-connection type ACL (mainly used for packet data). (3) Error correction: The baseband controller adopts 3 error correction methods: 1ö3 rate forward error correction coding (FEC), 2ö3 rate forward error correction coding (FEC), and automatic data retransmission (ACL). (4) Authentication and encryption: The Bluetooth baseband part provides users with protection and information confidentiality mechanisms at the physical layer. Authentication is based on the "request response" algorithm. Authentication is a key part of the Bluetooth system. It allows users to establish a trust domain for personal Bluetooth devices, such as allowing only the owner's own laptop to communicate through the owner's own mobile phone. Encryption is used to protect connected personal information. The key is managed by the upper level of the program. Network transfer protocols and applications can provide users with a strong security mechanism.


6. Protocol stack structure of HeadSet application model

The protocol stack of Bluetooth uses a layered structure, as shown in Figure 2.


图2 蓝牙协议栈

The functions of each layer are briefly introduced as follows:


Baseband is the physical layer of Bluetooth. It is mainly responsible for the encoding and decoding of the physical channel, the timing control of the bottom layer, and the link management during the transmission of a single packet. The address field and control field are added to the original load data. And perform error detection or correction.


The LC (LinkControl) layer is responsible for responding to upper layer LM commands (such as executing LM commands for establishing the transmission link of the data packet and maintaining the link) during the transmission of a batch of data packets.


The LM (LinkManager) layer is the link management layer protocol of the Bluetooth protocol stack. It is responsible for translating the upper-layer HCI commands into operations that the baseband can accept, establishing ACL (data) and SCO (voice) links, and making Bluetooth devices enter an energy-saving state Work mode, etc.


ME (ManagementEntity) is a management entity. Its function is to discover remote devices and establish connections with them, and to provide interface APIs for the application layer and other system layers that need to manage the Bluetooth piconet.


SE (SecurityManager) and ME work together to allow applications to set authentication and encryption. The real authentication and encryption are realized by hardware.


Logical Link Control Protocol (L2CAP) uses the concept of a channel to establish different paths between different applications of Bluetooth devices, but it only supports ACL links. The main function is to provide protocol multiplexing for the high-level application layer, allowing them to share the low-level data link. It also performs grouping and reorganization operations for large data packets that exceed the support of the lower layer, while providing quality management of the upper layer service.


The Business Search Protocol (SDP) is an extremely important part. It provides a way for SDP clients to inquire about service information from the SDP server. The server maintains a service record list, which describes the service characteristics related to the server. The client can request service information, service and service characteristics from the server record by sending an SDP request, and establish a connection between two or more Bluetooth devices after querying.


RFCOMM provides serial port emulation, and the lower layer of Bluetooth is responsible for providing wireless connection methods. However, many traditional applications have been developed based on wired methods. If Bluetooth cannot solve the problem of adapting to traditional protocols, it will be difficult to become a A widely used standard. For this reason, Bluetooth has developed an adaptation protocol like RFCOMM to support traditional applications.


HeadSet control layer: It mainly implements the functions defined by the HeadSet Profile, such as volume control of the MIC (microphone) of the audio gateway, volume control of the SPK (speaker), establishment and release of SCO links, and audio transfer.


Audio port application layer: It is some application-specific functions. The application features that the headset application layer must implement are:


(1) The audio link can be established when receiving a call from the AG, and the audio link can be disconnected after the call. (2) A call to a remote party can be initiated, and the audio link can also be disconnected after the call. (3) Audio link transfer. (4) Volume control of the remote AG.


蓝牙TWS耳机设计开发商

Seven, the establishment of Bluetooth HeadSet chain

When the Bluetooth audio gateway AG receives a call and initiates a link with the HS, in order to make the application layer voice communication reliable, the Bluetooth protocol stack needs to establish a reliable communication link. According to the operation of the Bluetooth protocol stack, the chain construction from the bottom to the top generally goes through the following processes:


(1) Establish an ACL or link at the baseband layer; (2) Establish a link at the L2CAP layer; (3) Carry out the SDP query process; (4) Establish a link at the RFCOMM layer; (5) After the RFCOMM link is established, connect Transmit some AT commands of the HeadSet control layer through the RFCOMM channel; (6) Establish a SCO link; (7) Perform audio communication between Bluetooth HS and AG according to the action of the application layer. The process of building a Bluetooth protocol stack is discussed in detail as follows.


7.1 Establishing ACL, the link of the baseband layer


When the AG receives a call, it first establishes an ACL link with the HS. The HS and the AG need to be bound in advance, or the HS that was in the dormant state should be awakened. This is done through the page process. When the page process is used to establish the ACL, the AG is defined as the Master role, so the AG first initiates a query and obtains the Bluetooth address of the HS by querying the AG. Then AG initiates a page process to the queried HS under the drive of the application layer. When receiving the response returned by HS, the ACL link between AG and HS has been successfully established.


Once the ACL link is established, it can be used to transmit ringing signals. The sending of the ringing signal is completed by the AT command RING. The ringing signal can also be sent on the SCO link.


7.2 Establish a link to the L2CAP layer


After the ACL is connected, build the L2CAP link. AG first sends a link request signaling on the signaling channel, requiring the establishment of L2CAP with the channel label 0x0040 (0x0001~0x003F are reserved for protocol and cannot be dynamically allocated, 0x0040~0xFFFF can be freely allocated), the PSM (Protocol Service Multiplex) of this channel Use) sign 0001, when the other party returns a link response signal, it indicates that the 0x0040 channel has been established. Then configure this channel. After configuring the channel, you can use this L2CAP channel with a CID of 0x0040 for SDP query.


7.3 SDP query process


Use the L2CAP channel with CID of 0x0040 to perform the SDP query process. First, the AG sends an SDP query packet on this L2CAP channel, and the SDP query packet will query whether the SDP server HS has the required service. If the query is successful, build another L2CAP link labeled 0x0041 on the ACL link. The PSM of this channel is 0003, which is used to transmit RFCOMM data streams. At the same time, disconnect the L2CAP link labeled 0x0040 used for SDP query.


7.4 Establish a link at the RFCOMM layer


After the L2CAP channel with CID of 0x0041 is established, the process of establishing the RFCOMM link follows, as shown in Figure 3. First, the control channel is established. The initiator of the link establishment AG sends a SABM frame on the channel, which requires the establishment of Channel 0 on the RFCOMM layer. If the responder HS wants to establish a link, it returns a UA frame, indicating that the Channel 0 RFCOMM channel has been established. . This channel is a control channel, used to transmit UIH frames that carry control messages and commands. If the responding HS does not want to establish a link, it returns a DM frame.


Secondly, the data channel is established. First, the parameters of the data transmission channel are negotiated. The PN parameters of the negotiation command include the priority of the channel to be established Channel1, the maximum frame length, etc. After the two parties negotiate, the data transmission channel Channel1 is established.


图3 RFCOMM的建链过程

After the data channel of the RFCOMM layer is built, the control commands of the HeadSet control layer are transmitted. That is, the UIH frame is used to transmit AT+CKPD=200 and other commands on the data transmission channel Channel1. After receiving the response OK from the other party, the SCO link can be established.


7.5 Establish SCO link


During initialization, HCI sends a WRITE_VOICE_SETTING command to set the audio status. When receiving a request from AG to establish a SCO link, if HS allows it, send a received HCI command. After the command is completed, the SCO link that transmits the audio signal is Once built, you can start voice communication at this time.


8. State machine design of Bluetooth application layer

After the communication link of the protocol stack is established, application layer communication can be carried out. The application layer has a main state machine running, as shown in Figure 4. According to the user's actions and the commands sent by the AG, the HS application process will run between states. There may be an ACL link between HS and AG in the idle state, but no RFCOMM channel is established, and there is no SCO link for audio signals. When the AG responds to internal events or user requests, it initiates a link establishment request. When HS receives the request to establish a link, HS and AG begin to build ACL, L2CAP, and RFCOMM links. After the RFCOMM channel is established, the AG sends one or more An AT+RING ringing instruction command informs HS users that the audio link for incoming calls has arrived. At this time, the status will change to the incoming call ringing status.


图4 应用层主状态机运行

Incoming call ringing state At this time, the audio gateway AG has established the ACL and RFCOMM channel with HS, and HS has received the ringing signal, which indicates that the audio gateway requires the establishment of the SCO link. If the user does not respond to the ringing, the audio gateway The RFCOMM channel will be terminated and the state will return to the idle state. If the user presses the Bluetooth button on the HS, it means that the user accepts the request for the incoming call audio link. At this time, the HS will send the AT control command AT+CKPD to the AG. SCO link will be established between the two, and the status will be transferred to the link status.


Call status When the HS user presses the Bluetooth button on the HS, the user sends a request to call the audio gateway. At this time, the HS will first establish an ACL link, and then make an SDP query to the audio gateway. If the SDP query is successful, the HS opens the RFCOMM channel linking with the AG, and sends the AT command +CKPD to request the establishment of the SCO audio link. When the SCO link is established, the state will shift to the connected state. If the SCO link fails to be established, the state will shift to the idle state.


Link state In link state, ACL, L2CAP, RFCOMM have been established, and SDP query has been executed, and the audio call is ready. If the user speaks to the HS, the voice will be transmitted to the AG through the SCO link, and the voice will be transmitted to the far end through the AG.


Link disconnection status The link disconnection can be completed by the Bluetooth button on the HS, or can be triggered by internal events of the AG or user intervention. Regardless of the cause, it is essentially the result of the release of the AG link. If HS sends out a request to disconnect the link and sends an AT command +CKPD to the audio gateway to request the audio gateway to disconnect, the audio gateway will disconnect the SCO link and RFCOMM channel. When RFCOMM is disconnected, the state will transition to the idle state. The ACL link may be in a disconnected state or in a linked state.


Nine, the realization of Bluetooth HeadSet

Take Motorola's Bluetooth solution as an example. Motorola is very experienced in advanced radio frequency technology. Motorola's Bluetooth solution can use UART, RS232, USB or SPI to connect to the main processor, and the main processor can handle functions such as the stack on the Bluetooth protocol and the interface of the main controller through these interfaces; and this Bluetooth solution is responsible for performing the following Stack (main controller interface, link management program protocol, baseband and radio frequency) remaining functions. And adopts Motorola's MCORETM 32-bit Reduced Instruction Set Computing (SISC) processor core, which contains a highly flexible set of peripheral devices, suitable for a variety of different embedded Bluetooth applications.


to sum up


In recent years, the rapid development of mobile communications, portable computers and the Internet has increased the demand for various data and information transmissions other than telephone communications. "Bluetooth" technology connects various portable computers and cellular mobile phones with wireless circuits, so that computers and communications are more closely integrated, so that people can exchange and transmit data anytime and anywhere.